import os
import re
import hashlib
import subprocess
import SCons
from os.path import join as pjoin
from site_scons import ac

Import("appname env extern")

def md5it(input):
    # python <= 2.4 have different haslib APIs. sux, but we need to support old python.
    m = hashlib.md5()
    m.update(input)
    return m.hexdigest()

def to_bytes(input):
  output = ""
  d = [str(ord(i)) for i in input]
  block_size = 10
  while len(d):
    output += ", ".join(d[:block_size])
    output += ",\n        "
    d = d[block_size:]
  return output

# TODO: move to site_scons
def embed_lua_blob(target, source, env):
  #print 'embed lua', target, source
  func = env['LUABLOBFUNC']
  for s in source:
    # TODO: use luac built from our source
    assert(subprocess.Popen([extern['luac'].get_path(), '-p', s.get_path()]).wait() == 0)
  modules = [[os.path.basename(s.get_path().replace(".lua", "")), s, md5it(s.get_path())] for s in source]
  header_output = """
  /* THIS IS A GENERATED HEADER FILE. DO NOT EDIT */
  #include <string.h>
  #include <stdint.h>
  #ifndef _header_%s_h_
  #define _header_%s_h_
  int %s_has_module(const char *input);
  int %s_get_module(const char *input, const char **p, size_t *len);
  int %s_get_pubkey(const char *input, const char **p, size_t *len);
  int %s_get_signature(const char *input, const char **p, size_t *len);
  #endif
  """ % (func, func, func, func, func, func)

  output = """
  /* THIS IS A GENERATED C FILE. DO NOT EDIT */
  #include "%s"
  """ % (target[1].get_path())

  output += """
  int %s_has_module(const char *input){
  """ % (func)
  for m in modules:
    output += """
      if (strcmp(input, "%s") == 0) {
        return 0;
      }
    """ % (m[0])
  output += """
    return 1;
  }
  """
  wantpubkey = 0
  for m in modules:
    content = m[1].get_contents()
    bytes = to_bytes(content);
    output += """
        static const char const EQUUS_SCRIPT_BODY_%s[] = {%s};
    """ % (m[2], bytes)
    sig = m[1].get_path().replace(".lua", ".sig")
    if os.path.exists(sig):
      wantpubkey = 1
      sig = env.File(sig)
      sigcon = sig.get_contents()
      bytes = to_bytes(sigcon);
      output += """
          static const char const EQUUS_SCRIPT_SIG_%s[] = {%s};
      """ % (m[2], bytes)

  output += """
  int %s_get_module(const char *input, const char **p, size_t *len){
  """ % (func)
  for m in modules:
    output += """
      if (strcmp(input, "%s") == 0) {
        *p = &EQUUS_SCRIPT_BODY_%s[0];
        *len = sizeof(EQUUS_SCRIPT_BODY_%s);
        return 0;
      }
    """ % (m[0], m[2], m[2])
  output += """
    return 1;
  }
  """
  if wantpubkey:
    bytes = to_bytes(env['pubkey'].get_contents())
    output += """
    int %s_get_pubkey(const char *input, const char **p, size_t *len){
      static const char const pubkey[] = {%s};
      *p = &pubkey[0];
      *len = sizeof(pubkey);
      return 0;
    }
    """ % (func, bytes)

    output += """
    int %s_get_signature(const char *input, const char **p, size_t *len){
    """ % (func)
    for m in modules:
      sig = m[1].get_path().replace(".lua", ".sig")
      if os.path.exists(sig):
        output += """
          if (strcmp(input, "%s") == 0) {
            *p = &EQUUS_SCRIPT_SIG_%s[0];
            *len = sizeof(EQUUS_SCRIPT_SIG_%s);
            return 0;
          }
        """ % (m[0], m[2], m[2])
    output += """
      return 1;
    }
    """

  fw = open(target[1].get_abspath(), "wb+")
  fw.write(header_output)

  fw = open(target[0].get_abspath(), "wb+")
  fw.write(output)

def sig_emitter(target, source, env):
  return (target, source)

def luac_emitter(target, source, env):
  for t in target:
    env.Depends(t, extern['luac'])
  return (target, source)

lblob = Builder(action = embed_lua_blob, emitter=[sig_emitter, luac_emitter])
env.Append(BUILDERS = {'LuaBlob' : lblob})

def embed_c_blob(target, source, env):
  func = env['CBLOBFUNC']
  s = source[0]
  content = s.get_contents()
  bytes = to_bytes(content);
  key = md5it(s.get_path())

  header_output = """
  /* THIS IS A GENERATED HEADER FILE. DO NOT EDIT */
  #include <string.h>
  #include <stdint.h>
  #ifndef _header_%s_h_
  #define _header_%s_h_
  int %s_get(const char **p, size_t *len);
  #endif
  """ % (func, func, func)

  output = """
  /* THIS IS A GENERATED C FILE. DO NOT EDIT */
  #include "%s"

  static const char const EQUUS_BLOB_BODY_%s[] = {%s};

  """ % (target[1].get_path(), key, bytes)
  output += """
  int %s_get(const char **p, size_t *len){
  """ % (func)
  output += """
        *p = &EQUUS_BLOB_BODY_%s[0];
        *len = sizeof(EQUUS_BLOB_BODY_%s);
        return 0;
    }
    """ % (key, key)

  fw = open(target[1].get_abspath(), "wb+")
  fw.write(header_output)

  fw = open(target[0].get_abspath(), "wb+")
  fw.write(output)

cblob = Builder(action = embed_c_blob)
env.Append(BUILDERS = {'CBlob' : cblob})

lenv = env.Clone()
lenv['LUABLOBFUNC'] = 'equus_loader'
mods = SConscript("lua/modules/SConscript")
luablob = lenv.LuaBlob(['equus_loader_blob.c', 'equus_loader_blob.h'], mods)
lenv = env.Clone()
lenv['LUABLOBFUNC'] = 'equus_bootstrap'
bsfile = 'lua/bootstrap-http.lua'
if env['LOCAL_DEV']:
  bsfile = "lua/bootstrap-local.lua"
bootstrap = lenv.LuaBlob(['equus_bootstrap.c', 'equus_bootstrap.h'], [bsfile])

lenv = env.Clone()
lenv['LUABLOBFUNC'] = 'equus_bootstrap'
testbootstrap = lenv.LuaBlob(['equus_testbootstrap.c', 'equus_testbootstrap.h'], ["lua/bootstrap-test.lua"])

lenv.Clone()
lenv['CBLOBFUNC'] = "equus_pubkey"
pubkey = lenv.CBlob(['equus_pubkey.c', 'equus_pubkey.h'], lenv['pubkey'])

localdev = [pubkey[0]]

luas = env.Glob("lua/remote/*.lua")

luas = list(set(luas))
def s2l_builder(target, source, env):
  orig = SCons.Script.sconscript_reading
  SCons.Script.sconscript_reading=True
  output = """
-- GENERATED LUA MODULE. DO NOT EDIT --
module(..., package.seeall);

"""
  conf = Configure(env, custom_tests = {'CheckStructOffset': ac.CheckStructOffset})
  args = env['S2L_ARGS']
  for k in args.keys():
    struct = k
    output += k.split(" ")[-1]+" = {"
    headers = "\n".join(["#include <%s>" % s for s in args[k][0]])
    a = []
    a.append(["__size", conf.CheckTypeSize(k, headers)])
    for t in args[k][1]:
      r = conf.CheckStructOffset(k, headers, t)
      a.append([t, r])
      r = conf.CheckTypeSize("(("+k+" *) 0)->"+t, headers)
      a.append([t+"__size", r])
    for p in a:
      output += ""+ p[0] +"="+ str(p[1]) + ","
    output += "}\n"
  conf.Finish()
  fw = open(target[0].get_abspath(), "wb+")
  fw.write(output)
  SCons.Script.sconscript_reading = orig

env.Append(BUILDERS = {'Struct2LuaBuilder':  Builder(action = s2l_builder)})

headers = ['sys/param.h', 'sys/mount.h']
if env['PLATFORM'] != 'darwin' and env['PLATFORM'] != 'freebsd':
  headers.append('sys/statfs.h')
env['S2L_ARGS'] = {'struct statfs': [headers,
                                     ['f_bfree', 'f_blocks', 'f_bsize', 'f_bavail']],
                   'struct dirent': [['dirent.h'], ['d_name']],
                   'struct timespec': [['time.h'],
                                       ['tv_sec',
                                        'tv_nsec',
                                      ]],
                   'struct stat': [['sys/stat.h'],
                                   ['st_size',
                                    # bits/stat.h
                                    ## __time_t
                                    'st_mtime',
                                    'st_ctime',
                                    'st_atime',
                                    ## struct timespec
                                    'st_atim',
                                    'st_mtim',
                                    'st_ctim',
                                    ## __time_t OSX/BSD?
                                    'st_mtimespec',
                                    'st_ctimespec',
                                    ]],
                   'MYSQL_FIELD': [[env['MYSQL_HEADER']],
                                   ['name',
                                    'org_name',
                                    'table',
                                    'org_table',
                                    'db',
                                    'catalog',
                                    'def',
                                    'length',
                                    'max_length',
                                    'name_length',
                                    'org_name_length',
                                    'table_length',
                                    'org_table_length',
                                    'db_length',
                                    'catalog_length',
                                    'def_length',
                                    'flags',
                                    'decimals',
                                    'charsetnr',
                                    'type',
                                    'extension',
                                    ]],
                   'MYSQL': [[env['MYSQL_HEADER']],
                                   ['host',
                                    ]],
                   'MYSQL_RES': [[env['MYSQL_HEADER']],
                                   ['row_count',
                                    'fields',
                                    'data',
                                    'data_cursor',
                                    'lengths',
                                    'handle',
                                    'methods',
                                    'row',
                                    'current_row',
                                    'field_alloc',
                                    'field_count',
                                    'current_field',
                                    'eof',
                                    'unbuffered_fetch_cancelled',
                                    'extension',
                                    ]]}
s = env.Struct2LuaBuilder('lua/remote/structs.lua',
                                  SCons.Node.Python.Value(env['S2L_ARGS']))

luas.extend(s)
luas = list(set([str(x) for x in luas]))

jmxversion = '1.1'
jmxsrc = env.Java(target = 'classes', source = 'jmx')
jarfile = env.Jar(target=env.File('lua/remote/jmxquery-%s.jar' % (jmxversion)), \
                  source=jmxsrc + ['jmx/MANIFEST.MF'])

if env['LOCAL_DEV']:
  lenv = env.Clone()
  lenv['LUABLOBFUNC'] = 'equus_localdev'
  blob = lenv.LuaBlob(['equus_localdev.c', 'equus_localdev.h'], luas)
  lenv.Depends(blob, sigs)
  localdev.append(blob[0])

lenv.Clone()
lenv["ENV"]["LUA_PATH"] = ";".join([pjoin(lenv.Dir("lua/remote").get_abspath(),  "/?.lua"), pjoin(lenv.Dir("lua/modules").get_abspath(), "?.lua")])
lenv["ENV"]["PATH"] += ":" + lenv.Dir("#/extern/").get_abspath()
lint = [lenv.Command(str(x)+".lint", x, ["'%s' '%s' -r '$SOURCE' || exit 0" % (str(extern['luacmd']), str(extern['lualint']))]) for x in luas]
env.AlwaysBuild(lint)
env.Depends(lint, extern['luacmd'])
env.Alias('lint', lint)

lenv = env.Clone()
lenv.Append(SWIGPATH="$CPPPATH")
lenv.Append(SWIGFLAGS='-lua')
if env['isMSVC']:
  lenv.Append(SWIGFLAGS=['-D_WIN32=1'])

if env.get('use_sigar'):
  lenv.Append(LIBS=[extern['sigarstatic']], CFLAGS=['-DUSE_SIGAR'], SWIGFLAGS=['-DUSE_SIGAR'])

USING_SSL=1
lenv.Append(LIBS=[extern['lualib'],
                  extern['luasocket'],
                  extern['luapack'],
                  extern['lualanes'],
                  extern['libalien'],
                  extern['libffi']])

extralibs = []
if USING_SSL:
  lenv.Append(LIBS=[extern['luasec']])
  if env["PLATFORM"] != "win32":
    # TODO: this is wrong.
    lenv.Append(LIBS=['ssl', 'crypto'])
  else:
    if env['isMSVC']:
      lenv.Append(LIBS=['ssleay32MTd', 'libeay32MTd'])
    else:
      lenv.Append(LIBS=[lenv.File(pjoin(env['SSL_ROOT'], "lib", "MinGW", "ssleay32.a"))])
      lenv.Append(LIBS=['eay32'])

if env["PLATFORM"] != "win32":
  lenv.Append(LIBS=['m', 'z'])
  if env["PLATFORM"] != "darwin" and env["PLATFORM"] != "freebsd":
    if 0:
      lenv['LINKCOM'] = lenv['LINKCOM'].replace('_LIBFLAGS', '_LIBFLAGS $LINKSUFFIX')
      lenv.Append(LINKFLAGS = ['-Wl,-Bstatic'])
      lenv.Append(LINKSUFFIX = ['-Wl,-Bdynamic', '-lpthread', '-ldl', '-lc'])
    else:
      lenv.Append(LIBS=['pthread', 'dl', 'c'])
  elif env["PLATFORM"] == "freebsd":
    lenv.Append(LIBS=['pthread', 'c', 'kvm'])
  else:
    lenv.Append(LIBS=['pthread', 'c'])
else:
  lenv.Append(LIBS=['Advapi32', 'Ws2_32', 'psapi'])
  # things needed by OpenSSL:
  lenv.Append(LIBS=['User32', 'Gdi32'])
  if not env['isMSVC']:
    lenv.Append(CFLAGS=['-mwindows'])
    lenv.Append(LIBS=['msvcr90'])

lenv.Append(CPPPATH=["#src", "#", "#extern/sigar/include"])

if lenv["PLATFORM"] == "darwin":
  lenv['FRAMEWORKS'] = ['CoreServices', 'IOKit']

eqsrc = [ 'equus_util.c', 'equus_loader.c', 'equus_service.c', 'mojosetup_luadebugger.c',
          'equus_portable.c', 'equus_conf.c', 'swig/equus.i', localdev, luablob[0]]
base = [lenv.Object(o) for o in eqsrc]
aenv = lenv.Clone()
if env["PLATFORM"] == "win32":
  base.append(aenv.RES('equus.rc'))
  aenv['PDB'] = "%s.pdb" % appname
  #env['CCPDBFLAGS']  = ['${(PDB and "/Fd%s_incremental.pdb /Zi" % File(PDB)) or ""}']
aenv.VariantDir('build-agent', '#src', duplicate=0)
sources = [base, 'build-agent/equus.c', pjoin('build-agent', str(bootstrap[0]))]
sources.extend(extralibs)
equus = aenv.Program(appname, source=sources)

aenv = lenv.Clone()
aenv.Append(CPPFLAGS=['-DEQ_TEST_HARNESS'])
aenv.VariantDir('build-test', '#src', duplicate=0)
sources = [base, 'build-test/equus.c', pjoin('build-test', str(testbootstrap[0]))]
sources.extend(extralibs)
eqtest = aenv.Program("eqtest", source=sources)

targets = [equus, eqtest]

Return("targets")


from io import StringIO
from unittest.mock import MagicMock
import pytest
from django.core.management import call_command

from rapidpro.followup_campaigns import FollowupCampaign

def call():
    out = StringIO()
    call_command("validate_rapidpro_settings", stdout=out)
    return out.getvalue()

@pytest.fixture(autouse=True)
def setup_fixture(settings, monkeypatch):
    settings.RAPIDPRO_API_TOKEN = "boop"

def test_it_works_when_no_campaigns_are_configured():
    assert "Follow-up campaign RH is not configured" in call()

def test_it_validates_campaigns(settings, monkeypatch):
    settings.RAPIDPRO_FOLLOWUP_CAMPAIGN_RH = "Boop Group,date_of_boop"
    validate = MagicMock()
    monkeypatch.setattr(FollowupCampaign, "validate", validate)
    assert "Validating RH " in call()
    validate.assert_called_once()


# Using dict comprehensions

def main():
    # Defining a list of temperatures
    temps = [0, 12, 34, 100]

    # Use a comprehension to build a dictionary
    # Tip: Formula to convert to Fahrneheit -> (t * 9/5) + 32
    temp_diction = {t: (t * 9 / 5) + 32 for t in temps if t < 100}
    print(temp_diction)
    print(temp_diction[12])

    # Defining two teams
    time_a = {"John": 24, "Jessica": 18, "Gustavo": 58, "Barbara": 7}
    time_b = {"Leticia": 12, "Gabriel": 88, "Joseph": 4}
    teams = (time_a, time_b)

    # Combining two dictionaries with one comprehension
    new_time = {k: v for time in teams for k, v in time.items()}
    print(new_time)

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
"""
Hypermedia pagination
"""

import csv
import math
from typing import List

index_range = __import__('0-simple_helper_function').index_range

class Server:
    """
    Server class to paginate a database of popular baby names.
    """
    DATA_FILE = "Popular_Baby_Names.csv"

    def __init__(self):
        self.__dataset = None

    def dataset(self) -> List[List]:
        """
        Cached dataset
        """
        if self.__dataset is None:
            with open(self.DATA_FILE) as f:
                reader = csv.reader(f)
                dataset = [row for row in reader]
            self.__dataset = dataset[1:]

        return self.__dataset

    def get_page(self, page: int = 1, page_size: int = 10) -> List[List]:
        """
        This function takes two integer arguments page with default
        value 1 and page_size with default value 10.

        Args:
            page (int): an integer type page with default value 1.
            page_size (int): an integer type page_size with default
            value 10.

        Returns:
            empty list if input argument is out of range for the dataset.
        """
        assert isinstance(page, int) and isinstance(page_size, int)
        assert page > 0 and page_size > 0

        start_index = index_range(page, page_size)[0]
        end_index = index_range(page, page_size)[1]

        try:
            return self.dataset()[start_index:end_index]
        except IndexError:
            return []

    def get_hyper(self, page: int = 1, page_size: int = 10) -> dict:
        """
        This function returns a dictionary containing the following
        key-value pairs:
          page_size: the length of the returned dataset page
          page: the current page number
          data: the dataset page (equivalent to return from previous task)
          next_page: number of the next page, None if no next page
          prev_page: number of the previous page, None if no previous page
          total_pages: the total number of pages in the dataset as an integer
        """
        assert isinstance(page, int) and isinstance(page_size, int)
        assert page > 0 and page_size > 0

        data = self.get_page(page=page, page_size=page_size)

        total_pages = math.ceil(len(self.dataset()) / page_size)

        next_page = None if (page + 1) > total_pages else (page + 1)

        prev_page = None if (page - 1) < 1 else (page - 1)

        return {
            "page_size": len(data),
            "page": page,
            "data": data,
            "next_page": next_page,
            "prev_page": prev_page,
            "total_pages": total_pages
        }


import unittest
import numpy as np
from chariot.transformer.vocabulary import Vocabulary
from gcn.graph import DependencyGraph, SimilarityGraph, StaticGraph
from gcn.visualize.draw import AttentionDrawer

class TestDraw(unittest.TestCase):

    def test_draw_dependency_graph(self):
        sentence = "I am living at house"
        graph_builder = DependencyGraph("en")
        attention = np.array([
            [0, 0, 1, 0, 0],
            [0, 0, 0.2, 0, 0],
            [0, 0, 0.7, 0, 0],
            [0, 0, 1, 0, 0],
            [0, 0, 0, 0.5, 0],
        ])

        drawer = AttentionDrawer(graph_builder)
        graph = drawer.draw(sentence, attention)
        drawer.show(graph)

    def test_draw_similarity_graph(self):
        sentence = "I am building similarity graph structure"
        graph_builder = SimilarityGraph("en")
        drawer = AttentionDrawer(graph_builder)
        graph = drawer.draw(sentence)
        drawer.show(graph)

    def test_draw_static_graph(self):
        sentence = "I am static graph"
        graph_builder = StaticGraph("en", kind="previous")
        drawer = AttentionDrawer(graph_builder)
        graph = drawer.draw(sentence)
        drawer.show(graph)


import constants
from game.casting.actor import Actor
from game.shared.point import Point
from game.shared.color import Color

class Snake(Actor):
    """
    A long limbless reptile.

    The responsibility of Snake is to move itself.

    Attributes:
        _points (int): The number of points the food is worth.
    """
    def __init__(self, position):
        super().__init__()
        self._segments = []
        self._prepare_body(position)
        self._color = Color(255, 255, 255)
        self._name= ""

    def get_segments(self):
        return self._segments

    def move_next(self):
        # move all segments
        for segment in self._segments:
            segment.move_next()
        # update velocities
        for i in range(len(self._segments) - 1, 0, -1):
            trailing = self._segments[i]
            previous = self._segments[i - 1]
            velocity = previous.get_velocity()
            trailing.set_velocity(velocity)

    def get_head(self):
        return self._segments[0]

    def grow_tail(self, match):
        tail = self._segments[-1]
        velocity = tail.get_velocity()
        offset = velocity.reverse()
        position = tail.get_position().add(offset)

        segment = Actor()
        segment.set_position(position)
        segment.set_velocity(velocity)
        segment.set_text("#")
        self._segments.append(segment)

        if not match:
            segment.set_color(self._color)
        else:
            segment.set_color(constants.WHITE)

    def turn_head(self, velocity):
        self._segments[0].set_velocity(velocity)

    def _prepare_body(self, position):
        x = position.get_x()
        y = position.get_y()

        for i in range(constants.SNAKE_LENGTH):
            position = Point(x, y + i * constants.CELL_SIZE)
            velocity = Point(0, 1 * -constants.CELL_SIZE)
            text = "8" if i == 0 else "#"
            #color = constants.YELLOW if i == 0 else constants.GREEN

            segment = Actor()
            segment.set_position(position)
            segment.set_velocity(velocity)
            segment.set_text(text)
            segment.set_color(self._color)
            self._segments.append(segment)

    # Return players' name
    def get_name(self):
        return self._name

    # Store players' name
    def set_name(self, name):
        self._name = name

    # Set snake color
    def set_color(self, color):
        self._color = color

        for segment in self._segments:
            segment.set_color(self._color)

    def snake_tail(self, game):

        tail = self._segments[-1]
        velocity = tail.get_velocity()
        offset = velocity.reverse()
        position = tail.get_position().add(offset)

        segment = Actor()
        segment.set_position(position)
        segment.set_velocity(velocity)
        segment.set_text("#")
        if not game:
            segment.set_color(self._color)
        else:
            segment.set_color(constants.WHITE)
        self._segments.append(segment)


#!/usr/bin/python3 # This is client.py file
import socket
# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = socket.gethostname()
port = 9999
print(host)
# connection to hostname on the port.
s.connect((host, port))
# Receive no more than 1024 bytes

x = 0
while (x==0):
    i = input(" enter the country name  : ")
    s.send(i.encode('ascii'))

    msg = s.recv(1024)
    print ("========> ", msg.decode('ascii'))
    msg_str = msg.decode('ascii')
    if(msg_str == "STOPPED"):
        break

s.close()


from django.urls import path

from .views import JokeListView, JokeDetailView, JokeCreateView, JokeUpdateView, JokeDeleteView

app_name = 'jokes'
urlpatterns = [
    path('', JokeListView.as_view(), name='list'),
    path('joke/<int:pk>/', JokeDetailView.as_view(), name='detail'),
    path('joke/create/', JokeCreateView.as_view(), name='create'),
    path('joke/<int:pk>/delete/', JokeDeleteView.as_view(), name='delete'),
    path('joke/<int:pk>/update/', JokeUpdateView.as_view(), name='update'),
]


class Solution:
    def minCostII(self, costs: list[list[int]]) -> int:
      n = len(costs)
      k = len(costs[0])
      dp = {}

      def dfs(idx,lastc):

        if idx == n:
          return 0

        if (idx,lastc) in dp:
          return dp[(idx,lastc)]

        best = float('inf')
        for i in range(k):
          if i != lastc:
            best = min(dfs(idx+1,i)+costs[idx][i],best)

        dp[(idx,lastc)] = best

        return best

      return dfs(0,-1)

Leetcode/Leetcode Weekly's/265. Paint House II/265-Paint-House-II.py


import seaborn as sns
from contextual_encoders import (
    TreeContext,
    WuPalmer,
    ContextualEncoder,
    GraphContext,
    PathLengthMeasure,
    MultidimensionalScalingReducer,
)
import matplotlib.pyplot as plt
import numpy as np

# noinspection DuplicatedCode
def readme_example():
    # Create a sample dataset
    x = np.array(["Fri", "Tue", "Fri", "Sat", "Mon", "Tue", "Wed", "Tue", "Fri", "Fri"])

    # Step 1: Define the context
    day = GraphContext("day")
    day.add_concept("Mon", "Tue")
    day.add_concept("Tue", "Wed")
    day.add_concept("Wed", "Thur")
    day.add_concept("Thur", "Fri")
    day.add_concept("Fri", "Sat")
    day.add_concept("Sat", "Sun")
    day.add_concept("Sun", "Mon")

    # Step 2: Define the measure
    day_measure = PathLengthMeasure(day)

    # Step 3+4: Calculate Dissimilarity Matrix
    #           and map to euclidean vectors
    encoder = ContextualEncoder(day_measure)
    encoded_data = encoder.transform(x)

    print_data_points(encoded_data)

    print_matrix(encoder.get_similarity_matrix(), "Similarity Matrix")
    print_matrix(encoder.get_dissimilarity_matrix(), "Dissimilarity Matrix")

    return

# noinspection DuplicatedCode
def multiple_attributes_example():
    # Create a sample dataset with two columns
    x = np.array(
        [
            ["Fri", "Teacher"],
            ["Tue", "Student,Police Man"],
            ["Fri", "Safety"],
            ["Sat", "Pilot"],
            ["Mon", "Police Man"],
            ["Tue", "Pilot,Student"],
            ["Wed", "Student"],
            ["Tue", "Student"],
            ["Fri", "Education"],
            ["Fri", "Teacher,Pilot"],
        ]
    )

    # Step 1: Define the context
    day = GraphContext("day")
    day.add_concept("Mon", "Tue")
    day.add_concept("Tue", "Wed")
    day.add_concept("Wed", "Thur")
    day.add_concept("Thur", "Fri")
    day.add_concept("Fri", "Sat")
    day.add_concept("Sat", "Sun")
    day.add_concept("Sun", "Mon")

    job = TreeContext("job")
    job.add_concept("Education")
    job.add_concept("Transportation")
    job.add_concept("Safety")
    job.add_concept("Teacher", "Education")
    job.add_concept("Student", "Education")
    job.add_concept("Police Man", "Safety")
    job.add_concept("Pilot", "Transportation")

    # Step 2: Define the measure
    day_measure = PathLengthMeasure(day)
    job_measure = WuPalmer(job)

    # Step 3+4: Calculate Dissimilarity Matrix
    #           and map to euclidean vectors
    encoder = ContextualEncoder(
        [day_measure, job_measure], reducer=MultidimensionalScalingReducer(n_components=1)
    )
    encoded_data = encoder.transform(x)

    print_data_points(encoded_data)

    print_matrix(encoder.get_similarity_matrix(), "Similarity Matrix")
    print_matrix(encoder.get_dissimilarity_matrix(), "Dissimilarity Matrix")

    return

# noinspection DuplicatedCode
def simple_example():
    # Load the tips dataset
    tips = sns.load_dataset("tips")

    # Select a subset of categorical variables
    data = tips["day"]

    subset = data.sample(frac=1, random_state=1).reset_index(drop=True).head(10)

    # Step 1: Define the context
    day = GraphContext("day")
    day.add_concept("Mon", "Tue")
    day.add_concept("Tue", "Wed")
    day.add_concept("Wed", "Thur")
    day.add_concept("Thur", "Fri")
    day.add_concept("Fri", "Sat")
    day.add_concept("Sat", "Sun")
    day.add_concept("Sun", "Mon")

    # Step 2: Define the measure
    day_measure = PathLengthMeasure(day)

    # Step 3+4: Calculate Distance Matrix
    #           and map to euclidean vectors
    encoder = ContextualEncoder(day_measure, inverters="sqrt")
    encoded_data = encoder.transform(subset)

    print_data_points(encoded_data)

    print_matrix(encoder.get_similarity_matrix(), "Similarities")
    print_matrix(encoder.get_dissimilarity_matrix(), "Dissimilarity")

    return

# noinspection DuplicatedCode
def advanced_example():
    # Load the tips dataset
    tips = sns.load_dataset("tips")

    # Select a subset of categorical variables
    data = tips[["sex", "smoker", "day", "time"]]

    subset = data.sample(frac=1, random_state=1).reset_index(drop=True).head(20)

    # Step 1: Define the context
    sex = TreeContext("sex")
    sex.add_concept("Female")
    sex.add_concept("Male")

    smoker = TreeContext("smoker")
    smoker.add_concept("No")
    smoker.add_concept("Yes")

    day = GraphContext("day")
    day.add_concept("Mon", "Tue")
    day.add_concept("Tue", "Wed")
    day.add_concept("Wed", "Thur")
    day.add_concept("Thur", "Fri")
    day.add_concept("Fri", "Sat")
    day.add_concept("Sat", "Sun")
    day.add_concept("Sun", "Mon")

    time = TreeContext("time")
    time.add_concept("Dinner")
    time.add_concept("Lunch")

    # Step 2: Define the measure
    sex_measure = WuPalmer(sex)
    smoker_measure = WuPalmer(smoker)
    day_measure = PathLengthMeasure(day)
    time_measure = WuPalmer(time)

    # Step 3+4: Calculate Distance Matrix
    #           and map to euclidean vectors
    encoder = ContextualEncoder(
        [sex_measure, smoker_measure, day_measure, time_measure],
        aggregator="mean",
        inverters=["lin", "lin", "sqrt", "lin"],
    )
    encoded_data = encoder.transform(subset)

    print_data_points(encoded_data)
    print_matrix(encoder.get_similarity_matrix(), "Similarity")
    print_matrix(encoder.get_dissimilarity_matrix(), "Dissimilarity")

    return

def print_matrix(matrix, title):
    fig, ax = plt.subplots()
    ax.matshow(matrix, cmap=plt.cm.get_cmap("Blues"))

    for i in range(len(matrix)):
        for j in range(len(matrix)):
            ax.text(i, j, str(round(matrix[j, i], 2)), va="center", ha="center")

    ax.set_title(title)

    plt.show()

def print_data_points(data_points):
    # colors = np.random.rand(len(data_points))
    colors = np.ones(len(data_points))

    plt.scatter(data_points[:, 0], data_points[:, 1], c=colors, alpha=0.5)
    plt.title("Euclidean Data Points")
    plt.show()

if __name__ == "__main__":
    # readme_example()
    # simple_example()
    # advanced_example()
    multiple_attributes_example()


import argparse
import logging
import os
import re

from matplotlib import pylab as plt
import numpy as np

from parse_trace import LoadTrace, GetAllocatorDumps, ProcessNamesAndLabels

def _ParseTrace(trace: dict) -> dict:
  """Parses a trace, and returns thread cache stats.

  Args:
    trace: As returned by LoadTrace()

  Returns:
    {pid  -> {'name': str, 'labels': str, 'data': np.array}.
    Where the data array contains 'size' and 'count' columns.
  """
  dumps = GetAllocatorDumps(trace)
  pid_to_name, pid_to_labels = ProcessNamesAndLabels(trace)

  result = {}
  for dump in dumps:
    pid = dump['pid']
    allocators = dump['args']['dumps']['allocators']

    # The browser process also has global dumps, we do not care about these.
    if 'global' in allocators:
      continue

    result[pid] = {
        'name': pid_to_name[pid],
        'labels': pid_to_labels.get(pid, '')
    }
    size_counts = []
    pattern = re.compile("malloc/partitions/allocator/buckets/bucket_\d+")
    for allocator in allocators:
      if (not pattern.match(allocator)):
        continue
      attrs = allocators[allocator]['attrs']
      allocated_objects_size = int(attrs['allocated_objects_size']['value'],
                                   base=16)
      slot_size = int(attrs['slot_size']['value'], base=16)
      try:
        size = int(attrs['size']['value'], base=16)
        fragmentation = 1 - allocated_objects_size / size
      except KeyError:
        assert allocated_objects_size == 0
        size = 0
        fragmentation = 0
      assert allocated_objects_size <= size

      size_counts.append(
          (slot_size, 100 * fragmentation, size - allocated_objects_size))
      size_counts.sort()
      result[pid]['data'] = np.array(size_counts,
                                     dtype=[('size', np.int),
                                            ('fragmentation', np.int),
                                            ('unused', np.int)])

  return result

def _PlotProcess(all_data: dict, pid: int, output_prefix: str):
  """Represents the allocation size distribution.

  Args:
    all_data: As returned by _ParseTrace().
    pid: PID to plot the data for.
    output_prefix: Prefix of the output file.
  """
  data = all_data[pid]
  logging.info('Plotting data for PID %d' % pid)

  # Fragmentation vs size.
  plt.figure(figsize=(16, 8))
  plt.title('Fragmentation (%%) vs Size - %s - %s' %
            (data['name'], data['labels']))
  plt.stem(data['data']['size'], data['data']['fragmentation'])
  plt.xscale('log', base=2)
  plt.yscale('linear')
  plt.ylim(ymin=0, ymax=100)
  plt.xlabel('Size (log)')
  plt.ylabel('Fragmentation (%)')
  plt.savefig('%s_%s_fragmentation.png' % (output_prefix, pid),
              bbox_inches='tight')
  plt.close()

  # Unused vs size.
  plt.figure(figsize=(16, 8))
  plt.title('Unused Memory vs Size - %s - %s' % (data['name'], data['labels']))
  plt.xscale('log', base=2)
  plt.yscale('log', base=2)
  plt.stem(data['data']['size'][data['data']['unused'] != 0],
           data['data']['unused'][data['data']['unused'] != 0])
  plt.ylim(ymin=1, ymax=2**20)
  plt.xlabel('Size (log)')
  plt.ylabel('Unused Size (log)')
  plt.savefig('%s_%d_unused.png' % (output_prefix, pid), bbox_inches='tight')
  plt.close()

def _CreateArgumentParser():
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--trace',
      type=str,
      required=True,
      help='Path to a trace.json[.gz] with memory-infra enabled.')
  parser.add_argument('--output-dir',
                      type=str,
                      required=True,
                      help='Output directory for graphs.')
  return parser

def main():
  logging.basicConfig(level=logging.INFO)
  parser = _CreateArgumentParser()
  args = parser.parse_args()

  logging.info('Loading the trace')
  trace = LoadTrace(args.trace)

  logging.info('Parsing the trace')
  stats_per_process = _ParseTrace(trace)

  logging.info('Plotting the results')
  for pid in stats_per_process:
    if 'data' in stats_per_process[pid]:
      _PlotProcess(stats_per_process, pid,
                   os.path.join(args.output_dir, 'fragmentation_result'))

if __name__ == '__main__':
  main()


from copy import deepcopy
from torchvision import models
import torch.nn as nn
import torch.nn.functional as F
import torch
from cd_trans_model.make_model import make_model
from .res50 import CosineClassifier
from pcs.utils import (AverageMeter, datautils, is_div, per, reverse_domain,
                       torchutils, utils)

class Vit(nn.Module):
    def __init__(self, num_classes, bottleneck=True, pretrained=True, extra=False,cfg=None,inc=256,temp=0.1):
        super(Vit, self).__init__()
        self.name = "vit"
        self.bottleneck = bottleneck
        self.features = make_model(cfg,inc)
        self.in_planes = 384 if 'small' in cfg.MODEL.Transformer_TYPE else 768
        #self.fc = nn.Linear(self.in_planes, inc)
        self.fc = nn.Sequential(nn.Linear(self.in_planes,1000), nn.BatchNorm1d(1000),nn.Linear(1000,inc))
        if bottleneck:
            self.classifer = CosineClassifier(num_class=num_classes,inc=inc,temp=temp)
            torchutils.weights_init(self.classifer)

        else:
            ori_fc  = self.features.fc
            self.classifer = nn.Linear(768, num_classes)
        print('pretrained:',pretrained)

        self.num_classes = num_classes
    def forward(self, x,  return_feat_for_pcs=False,images_for_cdd=False,x2=None):
        if len(x.shape)>4:
            x = x.squeeze()
        assert len(x.shape)==4
        if images_for_cdd:
            #x_len = int(x.shape[0]/2)
            #x1 = x[:x_len]
            #x2 = x[x_len:]
            feat1, feat2, feat_mix = self.features(x,x2,images_for_cdd=True)
            feat1 = self.fc(feat1)
            feat2 = self.fc(feat2)
            feat_mix = self.fc(feat_mix)
            return feat1,feat2,feat_mix

        else:
            feat = self.features(x,x)
            feat = feat.squeeze()
        #print(feat.shape)
        if return_feat_for_pcs:
            feat = self.fc(feat)
            return feat
        if self.bottleneck:
            #_, bottleneck, prob, af_softmax = self.classifer(feat)
            bottleneck = self.fc(feat)
            prob = self.classifer(bottleneck)
        else:
            prob = self.classifer(feat)
            bottleneck = feat
        return feat, bottleneck, prob, F.softmax(prob, dim=-1)

    def optim_parameters(self, lr, conv_ratio=0.1):
        d = [{'params': self.features.parameters(), 'lr': lr * conv_ratio},
                {'params': self.fc.parameters(), 'lr': lr },
                {'params': self.classifer.parameters(), 'lr':  lr}]
        return d


import numpy as np
import matplotlib.pyplot as plt
import scipy.io

# ЗАДАНИЕ №1
a=10
print('a =' ,a)

arr = np.array([1, 2, 3])
print('Матрица с заданными значениями 1\n', arr)

arr1 = [[1], [1, 2], [1, 2, 3]]
print('Матрица  \n', arr1)

arr2 = np.zeros((3, 2))
print('Матрица с нулевыми значениями 2 \n', arr2)

arr3 = np.ones((2, 3))
print('Матрица с единичными значениями \n', arr3)

arr4 = np.random.randint(2, 6, (3, 3))
print('Матрица с случайными целочисленными значениями \n',arr4)

# ЗАДАНИЕ №2
data = np.loadtxt(r'text.txt', dtype=np.int32)
print('Переменные из текстового файла \n')
print(data)

# ЗАДАНИЕ №3
data1 = scipy.io.loadmat(r'var3.mat')
dataA = data1['n']
maxx = np.max(dataA) # максимальная
print('Рассчет максимального = ', maxx)

minn = np.min(dataA) # минимальная
print('Рассчет минимального = ', minn)

med = np.median(dataA) # медиана
print('Рассчет медианы = ', med)

mato = np.mean(dataA) # мат. ожидание
print('Рассчет математического ожидания = ', mato)

dis = np.var(dataA) # дисперсия
print('Рассчет дисперсии = ',dis)

srotk = np.std(dataA) # среднеквадратическое отклонение
print('Рассчет среднеквадратического отклонения = ', srotk)

# ЗАДАНИЕ №4
plt.plot(dataA)
plt.show()
mean = np.mean(dataA) * np.ones(len(dataA))
var = np.var(dataA) * np.ones(len(dataA))
plt.plot(dataA, 'b-', mean, 'r-', mean-var, 'g--', mean+var, 'g--')
plt.grid()
plt.show()
plt.hist(dataA, bins=20)
plt.grid()
plt.show()

# ЗАДАНИЕ №5
def autocorrelate(a):
 n = len(a)
 cor = []

 for i in range(n//2, n//2+n):
  a1 = a[:i+1]   if i< n else a[i-n+1:]
  a2 = a[n-i-1:] if i< n else a[:2*n-i-1]
  cor.append(np.corrcoef(a1, a2)[0, 1])
 return np.array(cor)

dataA = np.ravel(dataA)
cor = autocorrelate(dataA)
plt.plot(cor)
plt.show()

# ЗАДАНИЕ №6
data2 = scipy.io.loadmat(r'var2.mat')
dataB = data2['mn']

# ЗАДАНИЕ №7
n = dataB.shape[1]
corr_matrix = np.zeros((n, n))
for i in range(0, n):
  for j in range(0, n):
    col = dataB[:, i] # выбор i-го столбца
    col2 = dataB[:, j] # выбор j-го столбца
    corr_matrix[i,j] = np.corrcoef(col, col2)[0, 1]
np.set_printoptions(precision=2)
print(corr_matrix)

plt.plot(dataB[:, 2], dataB[:, 5], 'b.')
plt.grid()
plt.show()


import math

def me(num: int) -> list:
	numbers = [True for _ in range(num + 1)]
	for i in range(2, math.floor(math.sqrt(num)) + 1):
		if numbers[i]:
			j = i ** 2
			while j < num:
				numbers[j] = False
				j += i
	return numbers[2:-1]

def alex(n):
	numbers = [True for i in range(n + 1)]
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		if numbers[i]:
			j = i ** 2
			while j <= n:
				numbers[j] = False
				j += i
	return numbers[2:]

def oliver(end):
	marks = [True] * end
	for i in range(2, round(end ** 0.5)):
		if marks[i] == True:
			for x in range(i ** 2, end, i):
				marks[x] = False
	return marks

if __name__ == '__main__':
	import timeit
	import threading
	import matplotlib.pyplot as plt

	# Not using Patrick, Isobel, or Rowan as were all very slow
	# Normalised others to return same data

	TIMES = {}
	ns = [i for i in range(0, 1000, 20)]

	def timer(func: callable, name: str):
		times = []
		for num in ns:
			time = timeit.timeit(lambda: func(num), number=10000)
			times.append(time)
			print(f"person={name}, num={num}, time={time}")
		TIMES[name] = times

	threads = []
	for person in [(me, "me"), (alex, "alex"), (oliver, "oliver")]:
		thread = threading.Thread(target=timer, args=(person[0], person[1]))
		thread.start()
		threads.append(thread)

	for thread in threads:
		thread.join()

	for person in TIMES:
		plt.plot(ns, TIMES[person], label=person)

	plt.legend()
	plt.show()


import decoder_utils
import pickle as pk
import numpy as np
import warnings
import time

warnings.filterwarnings("ignore", category=RuntimeWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
start_time = time.time()

sample_data_path = '/om/user/rishir/lib/NeuronalRecycling/sample_data/' # <- EDIT THIS TO POINT TO CORRECT PATH
sample_data_fn_IT = '%s/data_IT_base616.pkl' % sample_data_path
sample_data_fn_bab = '%s/bab.pkl' % sample_data_path

IT_base616 = pk.load(open(sample_data_fn_IT, 'rb'))
features, meta = IT_base616['IT_features'], IT_base616['meta']
oc = decoder_utils.ObjectClassifier()
IT_rec = oc.get_classification_record(features, meta)

perf_mu = np.nanmean([x['all']['perf'].mean() for x in IT_rec])
perf_sig = np.nanstd([x['all']['perf'].mean() for x in IT_rec])
print('IT decoder performance: %2.2f +- %2.2f' % (perf_mu, perf_sig))

bc = decoder_utils.BehaviorCharacterizer()
met_IT = bc.get_metrics(IT_rec, meta)

bab_dat = pk.load(open(sample_data_fn_bab, 'rb'), encoding='latin1')
perf_mu = np.nanmean([x['all']['perf'].mean() for x in bab_dat])
perf_sig = np.nanstd([x['all']['perf'].mean() for x in bab_dat])
print('Baboon performance: %2.2f +- %2.2f' % (perf_mu, perf_sig))

bc = decoder_utils.BehaviorCharacterizer(resp_var='resp')
met_bab = bc.get_metrics(bab_dat, meta)
#
out = decoder_utils.get_consistency(met_IT, met_bab, metricn='grp5_bigram_freq_dprime')
rho_mu = np.nanmean(out['rho_n'])
rho_sig = np.nanstd(out['rho_n'])
print('IT-baboon consistency: %2.2f +- %2.2f' % (rho_mu, rho_sig))

# your code
elapsed_time = time.time() - start_time
tmp = time.strftime("%H:%M:%S", time.gmtime(elapsed_time))
print('Elapsed time: %s' % tmp)


import pydot
from graph import Graph

class DiGraph(Graph):

    def add_edge(self, v1, v2):
        self.adj[v1].append(v2)

    def show_graph(self, name="digraph_show.png"):
        graph = pydot.Dot(graph_type='digraph')
        node_label_tmpl = "%s"
        for index, adj in enumerate(self.adj):
            v1_node = pydot.Node(node_label_tmpl % index)
            graph.add_node(v1_node)
            for v2 in adj:
                v2_node = pydot.Node(node_label_tmpl % v2)
                graph.add_node(v2_node)
                edge = pydot.Edge(v1_node, v2_node)
                graph.add_edge(edge)
        graph.write_png(name)

    def reverse(self):
        reversed_graph = DiGraph(self.num_of_vert())
        for source, dest_list in enumerate(self.adj):
            # import pdb; pdb.set_trace()
            for dest in dest_list:
                reversed_graph.add_edge(dest, source)
        return reversed_graph

if __name__ == "__main__":
    graph_data = [(0, 1),
                  (1, 0),
                  (2, 0),
                  (3, 0),
                  (0, 2),
                  (0, 3),
                  (1, 4),
                  (5, 4),
                  (4, 2),
                  (6, 3),
                  (6, 1),
                  (8, 9),
                  (5, 7),
                  (7, 1)]
    a = DiGraph(10)
    for edge in graph_data:
        a.add_edge(edge[0], edge[1])

    a.show_graph()
    b = a.reverse()
    b.show_graph(name="reversed_digraph.png")


import numpy as np
import torch

USE_GPU = torch.cuda.is_available()
CUDA_DEVICE = torch.device('cuda')
CPU_DEVICE = torch.device('cpu')

def set_gpu(enable=True):
    global USE_GPU
    USE_GPU = enable

def default_device():
    if USE_GPU:
        return CUDA_DEVICE
    else:
        return CPU_DEVICE

def tensor(array, pin=False, dtype=torch.float32):
    if array is None:
        return None
    if isinstance(array, torch.Tensor):
        return array
    device = default_device()
    var = torch.tensor(array, dtype=dtype, device=device)
    if pin:
        var.pin_memory()
    return var

def all_tensor(arrays, **kwargs):
    return [tensor(arr, **kwargs) for arr in arrays]

def to_numpy(tensor):
    if isinstance(tensor, np.ndarray):
        return tensor
    if USE_GPU:
        return tensor.detach().cpu().numpy()
    else:
        return tensor.detach().numpy()

def logsumexp(input, dim, keepdim=False, alpha=1.0):
    if alpha == 1.0:
        return torch.logsumexp(input, dim, keepdim=keepdim)
    else:
        return alpha * torch.logsumexp( input/alpha, dim, keepdim=keepdim)

def one_hot(y, n_dims=None):
    """ Take integer y (tensor or variable) with n dims and convert it to 1-hot representation with n+1 dims. """
    y_tensor = y.view(-1, 1)
    n_dims = n_dims if n_dims is not None else int(torch.max(y_tensor)) + 1
    y_one_hot = torch.zeros(y_tensor.size()[0], n_dims, device=default_device()).scatter_(1, y_tensor, 1)
    y_one_hot = y_one_hot.view(*y.shape, -1)
    return y_one_hot

def copy_params_from_to(source, target):
    for target_param, param in zip(target.parameters(), source.parameters()):
        target_param.data.copy_(param.data)


from django.shortcuts import render, get_object_or_404, redirect, get_list_or_404
from django.contrib.auth.decorators import login_required
from django.views import generic
from events.models import Organizer, Event, Field
import airsoft_project.forms as my_forms
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from datetime import datetime
from django.db.models import Q

# Create your views here.

def index(request):
    current_month = datetime.now().month
    events = Event.objects.filter(status='a')
    context = {'events': events,
               'current_month': current_month,
               }

    return render(request, 'index.html', context=context)

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

class EventListView(generic.ListView):
    model = Event
    context_object_name = 'events'
    template_name = 'event_list.html'
    paginate_by = 4

class FieldListView(generic.ListView):
    model = Field
    context_object_name = 'fields'
    template_name = 'field_list.html'
    paginate_by = 4

class OrganizerListView(generic.ListView):
    model = Organizer
    context_object_name = 'organizers'
    template_name = 'organizer_list.html'
    paginate_by = 4

class EventDetailView(generic.DetailView):
    model = Event
    template_name = 'event_detail.html'

class FieldDetailView(generic.DetailView):
    model = Field
    template_name = 'field_detail.html'

class OrganizerDetailView(generic.DetailView):
    model = Organizer
    template_name = 'organizer_detail.html'

class OrganizerEventListView(LoginRequiredMixin, generic.ListView):
    model = Event
    context_object_name = 'events'
    template_name = 'organizer_events.html'
    paginate_by = 4

    def get_queryset(self):
        return Event.objects.filter(organizer=self.request.user.profile.organizer)

class OrganizerEventDetailView(LoginRequiredMixin, generic.DetailView):
    model = Event
    context_object_name = 'event'
    template_name = 'organizer_event.html'

class OrganizerEventCreateView(LoginRequiredMixin, generic.CreateView):
    model = Event
    form_class = my_forms.OrganizerEventCreateForm
    success_url = "/events/list/"
    template_name = 'organizer_event_form.html'

    def form_valid(self, form):
        form.instance.organizer = self.request.user.profile.organizer
        return super().form_valid(form)

class OrganizerEventUpdateView(LoginRequiredMixin, UserPassesTestMixin, generic.UpdateView):
    model = Event
    form_class = my_forms.OrganizerEventUpdateForm
    success_url = "/events/list/"
    template_name = 'organizer_update_event_form.html'

    def form_valid(self, form):
        form.instance.organizer = self.request.user.profile.organizer
        return super().form_valid(form)

    def test_func(self):
        event = self.get_object()
        return self.request.user == event.organizer.profile.user

class OrganizerEventDeleteView(LoginRequiredMixin, UserPassesTestMixin, generic.DeleteView):
    model = Event
    success_url = "/events/list/"
    template_name = 'organizer_delete_event.html'

    def test_func(self):
        event = self.get_object()
        return self.request.user == event.organizer.profile.user

class PlayerEventListView(LoginRequiredMixin, generic.ListView):
    model = Event
    context_object_name = 'events'
    template_name = 'player_events.html'
    paginate_by = 4

    def get_queryset(self):
        return Event.objects.filter(registered_players=self.request.user.profile.player).filter(status = 'a')

class PlayerEventDetailView(LoginRequiredMixin, generic.DetailView):
    model = Event
    context_object_name = 'event'
    template_name = 'player_event.html'

class PlayerCompletedEventListView(LoginRequiredMixin, generic.ListView):
    model = Event
    context_object_name = 'events'
    template_name = 'player_completed_events.html'
    paginate_by = 4

    def get_queryset(self):
        return Event.objects.filter(registered_players=self.request.user.profile.player).filter(status = 'i')

class PlayerCompletedEventDetailView(LoginRequiredMixin, generic.DetailView):
    model = Event
    context_object_name = 'event'
    template_name = 'player_completed_event.html'

class OrganizerFieldListView(LoginRequiredMixin, generic.ListView):
    model = Field
    context_object_name = 'fields'
    template_name = 'organizer_fields.html'
    paginate_by = 4

    def get_queryset(self):
        return Field.objects.filter(created_by=self.request.user.profile.organizer)

class OrganizerFieldDetailView(LoginRequiredMixin, generic.DetailView):
    model = Field
    context_object_name = 'field'
    template_name = 'organizer_field.html'

class OrganizerFieldCreateView(LoginRequiredMixin, generic.CreateView):
    model = Field
    success_url = "/events/fields/"
    template_name = 'organizer_field_form.html'
    form_class = my_forms.OrganizerFieldForm

    def form_valid(self, form):
        form.instance.created_by = self.request.user.profile.organizer
        print('form valid')
        return super().form_valid(form)

    def form_invalid(self, form):
        print('form invalid')
        return super().form_invalid(form)

class OrganizerFieldUpdateView(LoginRequiredMixin, UserPassesTestMixin, generic.UpdateView):
    model = Field
    success_url = "/events/fields/"
    template_name = 'organizer_update_field_form.html'
    form_class = my_forms.OrganizerFieldForm

    def form_valid(self, form):
        form.instance.created_by = self.request.user.profile.organizer
        print('form valid')
        return super().form_valid(form)

    def form_invalid(self, form):
        print('form invalid')
        return super().form_invalid(form)

    def test_func(self):
        field = self.get_object()
        return self.request.user == field.created_by.profile.user

@login_required
def register_to_event(request, pk):

    selected_event = get_object_or_404(Event, pk = pk)
    player = request.user.profile.player
    context={'id':id,
             'event':selected_event,
             'player':player,}

    if request.method == "POST":
        player.events.add(selected_event)
        player.save()

        return redirect('event_detail', pk=pk)

    return render(request, "register_to_event.html", context)

def search_events(request):
    query = request.GET.get('query')
    search_results = Event.objects.filter(Q(name__icontains=query) | Q(organizer__name__icontains=query))
    return render(request, 'search_events.html', {'events': search_results, 'query': query})

def search_by_date(request):
    query = request.GET.get('query')
    search_results = Event.objects.filter(organizer=request.user.profile.organizer).filter(Q(date__contains=query))
    return render(request, 'search_events_by_date.html', {'events': search_results, 'query': query})


from celda import Celda

class Lista:
    __primer = None
    __cant = None
    def __init__(self):
        self.__primer = None
        self.__cant = 0
    def vacia(self):
        return self.__cant == 0
    def primerElemento(self):
        valor = -1
        if(not self.vacia()):
            valor = self.__primer.getDato()
        return valor
    def ultimoElemento(self):
        valor = -1
        if(not self.vacia()):
            aux = self.__primer
            while aux.getSiguiente() != None: #recorre hasta el final
                aux = aux.getSiguiente()
            valor = aux.getDato()
        return valor
    def siguiente(self, pos):
        valor = -1
        if(not self.vacia()):
            if(pos == 0):
                valor = self.__primer.getSiguiente()
            else:
                i = 0 #para que se coloque en la posición deseada
                aux = self.__primer
                while aux.getSiguiente() != None and i < pos:
                    i += 1
                    aux = aux.getSiguiente()
                if(i == pos):
                    valor = aux.getSiguiente() #retorna el siguiente
        return valor
    def anterior(self, pos):
        valor = -1
        if(not self.vacia()):
            if(pos == 0):
                valor = None
            else:
                i = 1 #para que se coloque en la posición anterior deseada
                aux = self.__primer
                while aux.getSiguiente() != None and i < pos:
                    i += 1
                    aux = aux.getSiguiente()
                if(i == pos):
                    valor = aux #retorna el anterior
        return valor
    def insertar(self, dato, pos): #MODIFICAR
        if(self.vacia and pos == 0):
            self.__primer = Celda(dato)
            self.__cant += 1
        else:
            if(pos == 0):
                nodo = Celda(dato)
                nodo.setSiguiente(self.__primer)
                self.__primer = nodo
                self.__cant += 1
            else:
                nodo = Celda(dato)
                anterior = self.anterior(pos)
                if(anterior != -1):
                    siguiente = anterior.getSiguiente()
                    nodo.setSiguiente(siguiente)
                    anterior.setSiguiente(nodo)
                    self.__cant += 1
                else:
                    raise IndexError
    def suprimir(self, pos): #MODIFICAR
        valor = -1
        if(not self.vacia()):
            if(pos == 0):
                valor = self.__primer.getDato()
                self.__primer = self.__primer.getSiguiente()
                self.__cant -= 1
            else:
                anterior = self.anterior(pos)
                if(anterior != -1):
                    siguiente = self.siguiente(pos)
                    if(siguiente != -1):
                        anterior.setSiguiente(siguiente)
                        self.__cant -= 1
                    else:
                        raise IndexError
                else:
                    raise IndexError
        return valor
    def recuperar(self, pos):
        valor = -1
        if(not self.vacia()):
            if(pos == 0):
                valor = self.__primer.getDato()
            else:
                i = 0
                aux = self.__primer
                while aux.getSiguiente() != None and i < pos:
                    i += 1
                    aux = aux.getSiguiente()
                valor = aux.getDato()
        return valor
    def buscar(self, dato):
        pos = -1
        if(not self.vacia()):
            i = 0
            aux = self.__primer
            while aux.getSiguiente() != None and pos == -1:
                if(aux.getDato() == dato):
                    pos = i
                i += 1
                aux = aux.getSiguiente()
        return pos


import re
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.utils.html import escape
from django.template.defaultfilters import pluralize
from ajax_select import LookupChannel
from zurnatikl.apps.people.models import Person

class PersonLookup(LookupChannel):
    '''Custom :class:`~zurnatikl.apps.people.models.Person` lookup
    for ajax autocompletion on edit forms.  Searches on
    street address, city, state name, and country name (case-insensitive,
    partial matching).'''

    model = Person

    help_text = 'Enter text to search for and add people.  Searches ' + \
            'on any one of last or first name, last or first alternate name,' + \
            ' or pen name.'

    def get_query(self, q, request):
        # NOTE: could configure mysql full-text indexing to allow using
        # a search filter

        # split the query into words (on spaces or comma space)
        # and match any field on any word
        words = re.split(',? +', q)
        people = Person.objects.all().order_by('last_name').distinct()
        # distinct required in case a term matches multiple names

        for w in words:
            people = people.filter(
                Q(first_name__icontains=w) |
                Q(last_name__icontains=w) |
                Q(name__first_name__icontains=w) |
                Q(name__last_name__icontains=w) |
                Q(penname__name__icontains=w))

        return people

    def name_info(self, obj):
        # formatted name info for display in match & item display
        names = {
            # main name
            'name': escape(unicode(obj)),  # lastname, first or just lastname
            # alternate names
            'alt_names': '; '.join(escape(unicode(n)) for n in obj.name_set.all()),
            # pen names
            'pennames': '; '.join(escape(unicode(n)) for n in obj.penname_set.all()),
        }
        return names

    def format_match(self, obj):
        """HTML formatted item for display in the dropdown """
        name_info = self.name_info(obj)
        name_info['break'] = name_info['btw'] = ''
        if name_info['alt_names'] or name_info['pennames']:
            name_info['break'] = '
'
        if name_info['alt_names'] and name_info['pennames']:
            name_info['btw'] = '; '
        # NOTE: using bold to help differentiate name from alternate names
        return u'''

           %(name)s %(break)s
           %(alt_names)s%(btw)s%(pennames)s

''' % name_info

    def format_item_display(self, obj):
        """HTML formatted item for display in the selected area"""
        names = self.name_info(obj)
        formatted_names = {
            'name': names['name'],
            'alt_names': 'Alternate names: %(alt_names)s' % names
                          if names['alt_names'] else '',
            'pennames': 'Pen names: %(pennames)s' % names
                         if names['pennames'] else '',
        }
        formatted_names['break'] = formatted_names['btw'] = ''
        if formatted_names['alt_names'] or formatted_names['pennames']:
            formatted_names['break'] = '
'
        if formatted_names['alt_names'] and formatted_names['pennames']:
            formatted_names['btw'] = '; '

        return u'''

           %(name)s
           %(break)s%(alt_names)s%(btw)s%(pennames)s

''' % formatted_names


import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button

class MyGridLayout(GridLayout):
    def __init__(self, **kwargs):

        super(MyGridLayout, self).__init__(**kwargs)

        self.cols = 2

        self.add_widget(Label(text="Title: "))
        self.title = TextInput(multiline=False)
        self.add_widget(self.title)

        self.add_widget(Label(text="Author: "))
        self.author = TextInput(multiline=False)
        self.add_widget(self.author)

        self.add_widget(Label(text="Id: "))
        self.id = TextInput(multiline=False)
        self.add_widget(self.id)

        self.submit = Button(text="Submit")
        self.submit.bind(on_press=self.press)
        self.add_widget(self.submit)

    def press(self, instance):
        title = self.title.text
        author = self.author.text
        idd = self.id.text

        self.add_widget(Label(text=f"Book {title} has been publish by {author} {idd}"))
        self.title.text = ""
        self.author.text = ""
        self.id.text = ""

class MyApp(App):
    def build(self):
        return MyGridLayout()

if __name__ == "__main__":
    MyApp().run()


from Dimer.dimerbreak import DimerBreak
import matplotlib.pyplot as plt
import json, statistics

results_temp = "300_s1.05_mix.json"

v_start = 0.005
v = v_start
v_max = 0.3
v_inc = 0.001

# Setting temperature. Using E = kBT, E = 2.3eV, kB = 8.62e-5eVK-1

e_si_si = 2.3
kB = 8.62e-5

T = 300

e_av = (kB * T)/e_si_si
optical_fraction = 1
acoustic_fraction = 1

f_opt = e_av * optical_fraction
f_aco = e_av * acoustic_fraction

f_timestep = 0.002
f_stiffness = 1.05

run_info = (v, v_max, v_inc)

nconfig = 100

v_list = []
w_means = []
w_errbar_size = []

results = {"params": (v, v_max, v_inc, f_timestep, f_opt, f_aco, f_stiffness, nconfig)}

total_breaks = ((v_max - v)/v_inc)*nconfig
breaks = 0

eks = []
ek_ebs = []

eps = []
ep_ebs = []

while v < v_max:

    t = DimerBreak(v, f_timestep, nconfig, f_opt, f_aco, f_stiffness, results_temp.replace(".json", ""),  run_info)
    if v == v_start:
        print(" -!-!-!- Mode = ", t.i_potential, " -!-!-!-")
        input("Continue?")
    works = t.run()

    n_successful_runs = len(works)
    print(n_successful_runs)

    if n_successful_runs == 0:
        print("Stiff bond(s) broke for every config at v = " + str(v))
        v = v_max
    else:
        w_mean = statistics.mean(works)
        w_dev = statistics.pstdev(works)

        results[v] = (w_mean, w_dev, n_successful_runs)

        w_means.append(w_mean)
        w_errbar_size.append(w_dev/(n_successful_runs**0.5))

        v_list.append(v)
        v += v_inc

        breaks += 1

        with open(results_temp, 'w') as q:
            json.dump(results, q, indent=2)

T_approx = round(max([f_aco, f_opt]) / 3.747e-5)

plt.errorbar(v_list, w_means, yerr=w_errbar_size, ls="none", marker="+")#, color=(0, 0.5, 0.5))
# plt.errorbar(v_list, eks, yerr=ek_ebs, ls="none", marker="+", color=(0.5, 0, 0.5))
# plt.errorbar(v_list, eps, yerr=ep_ebs, ls="none", marker="+", color=(0.5, 0.5, 0))
plt.xlabel("$\\frac{v}{v_s}$", size=24)
plt.ylabel("$\\bar W$ / $\epsilon$", size=18)
param_string = "Approximate Temperature = " + str(T_approx) + "K" + "\nopt = " + str(format(f_opt, ".3g")) +\
                   "\naco = " + str(format(f_aco, ".3g")) + "\nStiffness = " + str(f_stiffness)
plt.title(param_string, loc="left", fontsize=12)
plt.show()


#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
# -*- coding: utf-8 -*-
# Parent class
class Dragon:

    def __init__(self, name, snuggled,
        sleep, hunger, cloaca,
         fire, energy, maturity):
        self.name = name
        self.snuggled = snuggled
        self.sleep = sleep
        self.hunger = hunger
        self.cloaca = cloaca
        self.fire = fire
        self.energy = energy
        self.maturity = maturity

    # instance attribute
    def time_passes(self):
        self.snuggled -= 1
        self.sleep -= 1
        self.hunger += 1
        self.cloaca += 1
        self.fire += 0.5
        self.energy -= 0.5
        self.maturity += 0.5

        return("Time has passed.")

    def snuggle(self):
        if self.snuggled < 5:
            self.snuggled += 1
            return ("""
            Your dragon's snuggle meter
            increased by 1 point to {} on a scale of zero to 5.
            """.format(self.snuggled))
        else:
            return("Your dragon's snuggle meter is FULL!.")

    def sleep_dragon(self):
        if self.sleep < 3:
            self.sleep += 1
            return("""Your dragon slept,
             their sleep increased by 1 point.
             Their sleep meter is now {} on
              a scale of zero to 5""".format(self.sleep))
        else:
            return("""Your dragon's sleep meter is {}
            on a scale of zero to 5,
            they're not sleepy and the refused to go to bed.""".format(self.sleep))

    def feed_dragon(self):
        if self.hunger < 2:
            self.energy = 0.5
            self.hunger += 0.25
            return("""
            You let your dragon get too hungry!
            They have almost no energy and
            you'll have to feed him more slowly
            or they'll get sick!
            """)
        elif self.hunger < 4:
            self.hunger += 1
            self.energy = 4
            return(
            """Your dragon was hungry.
            They ate all the food.
            They're hunger level is {}
            on a scale of zero to 5.""".format(self.hunger))
        else:
            return("""Your dragon wasn't hungry,
            and they made a replica castle
            out of the food you gave them.

            Then then they smashed it with a look of glee.
            """)

    def life_stage(self):
        if self.maturity <= 2:
            return("baby")
        elif self.maturity >= 2 and self.maturity <= 4:
            return("(oh gawd) teenage")
        else:
            return("adult")

    def flight(self):
        # dragon has to be at least a teenager to fly
        if self.maturity >= 2:
            # energy good load light
            if self.energy >= 3 and self.cloaca < 3:
                return("Your dragon flies fast!")
            # energy low load light
            elif self.energy >= 2 and self.cloaca < 3:
                return("Your dragon flies slow! Their energies low.")
            # energy good load high
            elif self.energy >= 3 and self.cloaca >= 3:
                return("Your dragon flight is laggy! Their didn't 'go' before take off.")
            # engery low load high
            elif self.energy <=3 and self.cloaca >= 3:
                return("Your dragon flaps it's wings futily. They too heavy and have no energy.")
            else:
                return("""
                Your dragon rolls over on its bakc and feigns sleep.
                They've been hanging out with the local cats far to often this week.
                """)
        else:
            return("""
            Your dragon looks up at you with big puppy eyes and wiggles it's cute
            widdle wings and runs off as fast as it can on four legs.
            The little one zooms back and forth
            with obvious joy flapping their wings furiously.
            They've got some growing to do be they can take flight.
            But they sure are cute at this age.
            """)

    def description(self):
        return """{} is snuggled at level {},
         has {} units of sleep,
         is {} level hungry,
         needs to use the bath room on a scale of {} on a scale of 0 - 5,
         has a fire power of {} on a scale of 0 - 5,
         and has {} energy on a 0 - 5 scale,
         and they are a {} dragon.
        """.format(self.name, self.snuggled, self.sleep,
        self.hunger, self.cloaca,
         self.fire, self.energy, self.life_stage())

# need status prompt for hunger, bathroom, etcself.

# test code
timmy = Dragon('Timmy', 5, 5, 0, 4, 0, 2, 2)
# for i in range(0, 7):
#     timmy.time_passes()
print(timmy.description())
print(timmy.flight())


import curses
import random

from async_tools import sleep
from settings import MAX_NUMBER_OF_STARS, MIN_NUMBER_OF_STARS , STAR_FRAMES

def make_stars(canvas, canvas_height, canvas_width):
    number_of_stars = random.randint(MIN_NUMBER_OF_STARS, MAX_NUMBER_OF_STARS)
    coroutines = []
    for _ in range(number_of_stars):
        row = random.randint(2, canvas_height - 2)
        column = random.randint(2, canvas_width - 2)
        star = random.choice(STAR_FRAMES)
        coroutine = blink(canvas, row, column, symbol=star)
        coroutines.append(coroutine)
    return coroutines

async def blink(canvas, row, column, symbol='*'):
    while True:
        canvas.addstr(row, column, symbol, curses.A_DIM)
        await sleep(random.randint(1, 20))

        canvas.addstr(row, column, symbol)
        await sleep(random.randint(1, 3))

        canvas.addstr(row, column, symbol, curses.A_BOLD)
        await sleep(random.randint(1, 5))

        canvas.addstr(row, column, symbol)
        await sleep(random.randint(1, 3))


from app import app
from flask import render_template
from flask import render_template, session, jsonify, request, json

from app.controller import *

@app.route('/', endpoint='render_root')
def render_root():
    return render_home()

@app.route('/block_home.html', endpoint='render_home')
def render_home():
    topic, length = learning()
    courses = get_course()

    course = {
        "course" : []
    }

    for cs in courses:
        tcs = get_techercourse_of_courseID(cs.id)
        tc = ""
        # print (tcs)
        for name in tcs:
            tc = tc + str(name)[2:-3] + ","
        course['course'].append({
            "id" : cs.id,
            "title" : cs.title,
            "topic_id" : cs.topic_id,
            "description" : cs.description,
            "create_date" : cs.create_date,
            "duration" : cs.duration,
            "techer_course" : tc[:-1],
            "link" : "/course/" + str(cs.id)
        })
    return render_template('block_home.html', topic= topic, len=length, course=course)


import matplotlib.pyplot as plt
import numpy as np

# create some data
x = np.arange(0, 10, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

# create the first plot in a new figure
fig1 = plt.figure()
plt.plot(x, y1)
plt.title('Plot 1')

# create the second plot in a new figure
fig2 = plt.figure()
plt.plot(x, y2)
plt.title('Plot 2')

# show the plots
plt.show()


import os
import sys
from collections import defaultdict

import torch
from accelerate import Accelerator
from torch.utils.data import DataLoader, RandomSampler
from tqdm import tqdm
from transformers import get_constant_schedule_with_warmup

import logging
from custom_peft import get_peft_model, PromptTuningInit, PromptTuningConfig, TaskType
from utils.config import get_config
from utils.custom import log_dist
from utils.data import MBPP_Dataset_w_CodeBERT as CustomDataset
from utils.model import get_response_log_probs, compute_responsibilities, ClarificationCodeBERTPredictor, \
	compute_grad_norm
from utils.xformer import load_tokenizer, load_base_model, get_huggingface_path

def learn(args, logger):
	# No need to specify fp16 while launching script with $ accelerate launch
	# (since we specify it during $ accelerate config) else specify mixed_precision = args.mixed_precision
	if args.wandb_logging:
		accelerator = Accelerator(log_with=["wandb"])
	else:
		accelerator = Accelerator()

	# We need to initialize the trackers we use, and also store our configuration
	experiment_config = vars(args)
	accelerator.init_trackers(args.project_name, experiment_config)
	args.device = accelerator.device  # Overwrite device to use accelerator device

	# Get the tokenizer
	tokenizer = load_tokenizer(args.model_type, args.tokenizer_name)

	# Get the config
	peft_config = PromptTuningConfig(
		task_type=TaskType.MULTI_CAUSAL_LM,  # CAUSAL_LM, SEQ_2_SEQ_LM for Dec-only, Enc-Dec. MULTI is my custom field.
		prompt_tuning_init=PromptTuningInit.RANDOM,  # TEXT for text, RANDOM for random
		num_virtual_tokens=args.num_virtual_tokens,
		# prompt_tuning_init_text="Classify if tweet is a complaint or not:",  # Use this if prompt_tuning_init is TEXT
		# tokenizer_name_or_path=args.model_name_or_path,  # Use this if prompt_tuning_init is TEXT
		num_init_clusters=args.num_libraries,  # My custom field
	)

	# Get the dataset
	with accelerator.main_process_first():
		dataset = CustomDataset(
			path_to_data=args.path_to_data,
			tokenizer=tokenizer,
			max_prompt_length=args.max_prompt_length,
			max_length=args.max_length,
			sample_problems=args.num_train_problems,
			mode='train'
		)

	args.batch_size = args.per_gpu_train_batch_size * max(1, args.n_gpu)

	# Prepare training data loader
	sampler = RandomSampler(dataset)
	train_dataloader = DataLoader(dataset, sampler=sampler, batch_size=args.batch_size, num_workers=0, pin_memory=False)
	args.num_training_steps = (len(train_dataloader) * args.num_epochs)

	# Get the model
	_, model = load_base_model(
		model_type=args.model_type,
		config_name=args.config_name,
		model_path=args.model_name_or_path,
		load_in_8bit=args.load_in_8bit
	)
	# Load checkpoint
	if args.load_base_from_path is not None:
		# We load the model state dict on the CPU to avoid an OOM error.
		loaded_state_dict = torch.load(args.load_base_from_path, map_location="cpu")
		loaded_state_dict = {k.replace('module.', ''): v for k, v in loaded_state_dict.items()}
		model.load_state_dict(loaded_state_dict, strict=True)

		# release memory
		del loaded_state_dict

		# Log the loaded checkpoint
		msg = "Loaded model checkpoint from path: {}".format(args.load_base_from_path)
		if accelerator.is_local_main_process:
			logger.info(msg)
			print(msg)

	model = get_peft_model(model, peft_config)

	if accelerator.is_local_main_process:
		trainable_params, all_param = model.get_nb_trainable_parameters()
		msg = (f"trainable params: {trainable_params:,d} || all params: {all_param:,d} ||"
			   f" trainable%: {100 * trainable_params / all_param}")
		logger.info(msg)
		print(msg)  # Prompt tuning: embedding_dim * num_virtual_tokens * num_libraries

	# Get the optimizer
	optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr)
	lr_scheduler = get_constant_schedule_with_warmup(
		optimizer=optimizer,
		num_warmup_steps=0,
	)

	# Load the library predictor
	prior = ClarificationCodeBERTPredictor(args=args, output_dim=args.num_libraries)
	prior_optimizer = torch.optim.Adam(prior.parameters(), lr=args.prior_lr)
	prior.to(args.device)

	model,  optimizer, train_dataloader, lr_scheduler = accelerator.prepare(
		model, optimizer, train_dataloader, lr_scheduler
	)

	if accelerator.is_local_main_process:
		logger.info("Starting EM for Library Learning")

	global_step = 0
	# ############################################################################################################# #
	# ######################################### Initialisation for EM ############################################## #
	# ############################################################################################################# #

	# Initialise the model parameters i.e. latent prompt embeddings for each library
	# This is equivalent to latching each library to a random sample from the dataset
	if args.pre_num_iters > 0:

		# Set the lr for the initialisation phase
		for param_group in optimizer.param_groups:
			param_group['lr'] = args.init_lr

		rdm_idxs = torch.randint(0, len(dataset), (args.num_libraries,))
		if accelerator.is_local_main_process:
			print(f"[DEBUG] Random Indices: {rdm_idxs}")

		model.train()
		for k in range(args.num_libraries):

			if accelerator.is_local_main_process:
				logger.info("Initialisation for Library %d", k)

			idx = rdm_idxs[k]
			batch = dataset.sample(idx)
			batch = tuple(t.unsqueeze(0).to(args.device) for t in batch)
			bert_prompt, bert_prompt_mask, prompt, prompt_mask, response, response_mask = batch

			for i in tqdm(range(args.pre_num_iters), desc=f"Init. Iterations Clf {k}", position=0, leave=True,
						  disable=not accelerator.is_local_main_process):

				# Get the response log-probability of the sample coming from the latent prompt of library k
				resp_log_prob = get_response_log_probs(
					args,
					(prompt, prompt_mask, response, response_mask),
					tokenizer,
					model,
					k
				)

				# Normalise along the decoding length dimension
				resp_log_prob = resp_log_prob / (args.max_length+args.max_prompt_length)

				loss = -resp_log_prob.mean()  # Across the batch (here batch_size=1)

				# Update the model parameters
				accelerator.backward(loss)
				optimizer.step()
				lr_scheduler.step()  # Make sure this is constant schedule with no warmup
				optimizer.zero_grad()

				# if accelerator.is_local_main_process:
				# 	logger.info(f"Iter {i} Loss: {loss.detach().cpu().numpy().item()}")

				if args.wandb_logging:
					accelerator.log({f'init_loss/clf_{k}': loss.detach().cpu().numpy().item()}, step=global_step)

				global_step += 1

			with torch.no_grad():
				responsibilities = compute_responsibilities(args, (prompt, prompt_mask, response, response_mask),
															tokenizer, model)
				if accelerator.is_local_main_process:
					logger.info(
						f"[Init Responsibilities, After Training w Cluster {k}] {dataset.ids[idx]}: {responsibilities.cpu().numpy()[0].tolist()}")
					print(
						f"\n[Init Responsibilities, After Training w Cluster {k}] {dataset.ids[idx]}: {responsibilities.cpu().numpy()[0].tolist()}\n")

		#  [Debug] Verification - no cluster is empty
		with torch.no_grad():
			for k in range(args.num_libraries):
				idx = rdm_idxs[k]
				batch = dataset.sample(idx)
				batch = tuple(t.unsqueeze(0).to(args.device) for t in batch)
				bert_prompt, bert_prompt_mask, prompt, prompt_mask, response, response_mask = batch
				responsibilities = compute_responsibilities(args,
															(prompt, prompt_mask, response, response_mask),
															tokenizer, model)
				if accelerator.is_local_main_process:
					logger.info(
						f"[Init Responsibilities, Final, GT Cluster {k}] {dataset.ids[idx]}: {responsibilities.cpu().numpy()[0].tolist()}")
					print(
						f"[Init Responsibilities, Final, GT Cluster {k}] {dataset.ids[idx]}: {responsibilities.cpu().numpy()[0].tolist()}")

	# sys.exit()
	# ############################################################################################################# #
	# ################################################## EM ####################################################### #
	# ############################################################################################################# #
	if accelerator.is_local_main_process:
		logger.info("Starting EM for Clarification Prompt Tuning")

	# Reset the lr for the EM phase
	for param_group in optimizer.param_groups:
		param_group['lr'] = args.lr

	# Wait for all processes to be ready before starting the training loop
	accelerator.wait_for_everyone()

	for ep in range(args.num_epochs):
		model.train()
		prior.train()
		for _ in tqdm(range(len(train_dataloader)), desc=f"EM Iterations Epoch {ep}", position=0, leave=True,
					  disable=not accelerator.is_local_main_process):

			# ################################################################################################# #
			# ############################################### E-Step ########################################## #
			# E-Step: Compute resp. corresponding to each program coming from some latent prompt of a library
			batch = next(iter(train_dataloader))
			batch = tuple(t.to(args.device) for t in batch)

			# Posterior probabilities of the sample coming from the latent prompt of each library := p(z_k|x_n)
			responsibilities = compute_responsibilities(args, batch, tokenizer, model, prior)

			# ################################################################################################# #
			# ############################################### M-Step ########################################## #
			# M-Step: Update the model parameters i.e. latent prompt embeddings for each clarification
			#         by maximizing the likelihood of the data coming from it

			bert_prompt, bert_prompt_mask, prompt, prompt_mask, response, response_mask = batch

			for _ in range(args.max_m_steps):
				lib_train_logs = {}

				# ################################### Train the clarification prior ################################ #
				clf_preds = prior(bert_prompt, bert_prompt_mask)
				prior_loss = - (responsibilities * torch.log(clf_preds + 1e-8)).sum(dim=-1).mean()
				# Compute entropy regularisation
				entropy = - (clf_preds * torch.log(clf_preds + 1e-8)).sum(dim=-1).mean()
				# Add entropy regularisation to the loss to avoid local optima
				total_loss = prior_loss - args.ent_coeff * entropy

				accelerator.backward(total_loss)
				grad_norm = compute_grad_norm(prior)
				prior_optimizer.step()
				prior_optimizer.zero_grad()

				# [Q-function] Initialise  with the prior component
				q_func = - prior_loss.detach().cpu().numpy().item()

				# Bookkeeping
				lib_train_logs['clf_predictor_loss'] = total_loss.detach().cpu().numpy().item()
				lib_train_logs['clf_predictor_prior_loss'] = prior_loss.detach().cpu().numpy().item()
				lib_train_logs['clf_predictor_entropy'] = entropy.detach().cpu().numpy().item()
				lib_train_logs['grad_norm/prior'] = grad_norm

				# ############################# Train clarification by clarification ############################### #
				for k in range(args.num_libraries):

					k_responsibilities = responsibilities[:, k]
					# # [For numerical stability] Re-normalise the respo. for library k TODO: affects EM ? (Yes)
					# k_responsibilities = responsibilities[:, k] / responsibilities[:, k].sum()

					# Likelihood of the sample coming from the latent prompt of library := p(x_n|z_k)
					resp_log_prob = get_response_log_probs(
						args,
						(prompt, prompt_mask, response, response_mask),
						tokenizer,
						model,
						k
					)
					# Compute Loss = Negative Log Likelihood of the sample coming from the latent prompt of library k
					loss = -(resp_log_prob * k_responsibilities.detach()).mean()

					# Update the model parameters
					accelerator.backward(loss)
					grad_norm = compute_grad_norm(model)
					optimizer.step()
					lr_scheduler.step()
					optimizer.zero_grad()

					# [Q-function] Add the LL component w.r.t each clf to the Q-function
					q_func += -loss.detach().cpu().numpy().item()

					# Bookkeeping
					lib_train_logs[f"loss/clf_{k}"] = loss.detach().cpu().numpy().item()
					lib_train_logs[f"grad_norm/clf_{k}"] = grad_norm

				# ######################################## Log the results ###################################### #

				if args.wandb_logging:
					lib_train_logs.update({'q_func': q_func})
					lib_train_logs.update({'lr': lr_scheduler.get_last_lr()[0]})  # Log the learning rate
					accelerator.log(lib_train_logs, step=global_step)

				global_step += 1

	# ################################################ Evaluate ################################################## #
	# Count the frequency of  the sample-cluster assignments of the trained model
	if accelerator.is_local_main_process:
		logger.info("Starting Evaluation")

	model.eval()
	prior.eval()
	with torch.no_grad():
		prior_freq_count = defaultdict(int)
		posterior_freq_count = defaultdict(int)
		for i in tqdm(range(len(dataset)), desc=f"Evaluating", position=0, leave=True,
					  disable=not accelerator.is_local_main_process):
			batch = dataset.sample(i)
			batch = tuple(t.unsqueeze(0).to(args.device) for t in batch)

			# Compute the prior probabilities
			prior_probs = prior(batch[0], batch[1])
			prior_probs = prior_probs.detach().cpu().numpy()[0]

			# Get the library index with the highest prior probability
			library_idx = int(prior_probs.argmax())

			# Store the library index
			prior_freq_count[library_idx] += 1

			if accelerator.is_local_main_process:
				logger.info(f"[Prior] Sample {i} assigned to library {library_idx} from  {prior_probs}")

			# Compute the posterior probabilities
			responsibilities = compute_responsibilities(args, batch, tokenizer, model, prior)
			responsibilities = responsibilities.detach().cpu().numpy()[0]

			# Get the library index with the highest responsibility
			library_idx = int(responsibilities.argmax())

			# Store the library index
			posterior_freq_count[library_idx] += 1

			if accelerator.is_local_main_process:
				logger.info(f"[Posterior] Sample {i} assigned to library {library_idx} from {responsibilities}")

	if accelerator.is_local_main_process:
		logger.info("[Final] Prior Frequency Count: %s", prior_freq_count)
		logger.info("[Final] Posterior Frequency Count: %s", posterior_freq_count)

	# ################################################ Save Model ################################################## #
	if accelerator.is_local_main_process:  # only create checkpoint directory on main process
		logger.info("Saving the model at: %s", args.save_at)
		if not os.path.exists(args.save_at):
			os.makedirs(args.save_at)

	accelerator.wait_for_everyone()
	model = accelerator.unwrap_model(model)
	model.save_pretrained(save_directory=args.save_at)  # In place of $ accelerator.save(model.state_dict(), path)

	prior = accelerator.unwrap_model(prior)
	accelerator.save(prior.state_dict(), os.path.join(args.log_dir, "clf_predictor.pt"))

def main():
	args, logger = get_config()

	# Add BERT specific args
	args.bert_model_type = "codebert-base"
	args.bert_tokenizer_name = get_huggingface_path(args.bert_model_type)
	args.bert_model_name_or_path = get_huggingface_path(args.bert_model_type)
	args.bert_config_name = get_huggingface_path(args.bert_model_type)
	msg = f"Added model specific args for {args.bert_model_type}"
	log_dist(message=msg, level=logging.INFO, ranks=[0])

	learn(args, logger)

if __name__ == '__main__':
	# To run with accelerate, $ accelerate launch --config_file ds_zero3.yaml train_bert_coupled_oracle.py
	main()


import random  # sample, shuffle, seed

nodes = {}
unvisited_nodes = []

EVN_NBS = ((0, 1), (0, -1), (-1, 0), (1, 0), (-1, -1), (1, -1))
ODD_NBS = ((0, 1), (0, -1), (-1, 0), (1, 0), (-1,  1), (1,  1))
SIN_60 = sqrt(3) / 2  # sin(radians(60))
W = 30
H = SIN_60 * W
OX, OY = W / 2, H / 2  # deslocamento (offset)

def setup():
    size(900, 900)
    no_fill()
    start(2022)

def start(s):
    global rnd_seed
    rnd_seed = s
    random.seed(s)
    nodes.clear()
    unvisited_nodes[:] = []
    for _ in range(6):
        i, j = random.randint(-12, 12), random.randint(-12, 12)
        nodes[(i, j)] = (i, j, 0)
        unvisited_nodes.append((i, j))

def draw():
    background(240)
    translate(width / 2, height / 2)
    for n, v in nodes.items():
        ia, ja = n
        ib, jb, gen = v
        w = W * sqrt(gen + 1) / sqrt(W)
        stroke(0)
        xa, ya = ij_to_xy(ia, ja)
        xb, yb = ij_to_xy(ib, jb)
        fill(0, 255 - w * 15)
        if gen % 2:
            stroke_weight(2)
            line(xa, ya, xb, yb)
            circle(xa, ya, 3)
            circle(xb, yb, 3)
        stroke_weight(1)
        hexagon(xa, ya, w)
        #fill(255, 0, 0)
        #text(gen, xa, ya)
    grow()

def ij_to_xy(i, j):
    if i % 2 == 0:
        y = j * H  + OY
    else:
        y = j * H + H / 2 + OY
    x = i * W * 3 / 4 + OX
    return x, y

def grow():
    new_nodes = []

    for n, (i, j) in enumerate(unvisited_nodes):
        nbs = EVN_NBS if i % 2 == 0 else ODD_NBS
        _, _, gen = nodes[(i, j)]
        random.seed(min(6, n) + gen // 10)
        xnbs = random.sample(nbs, 4)
        random.shuffle(xnbs)
        for ni, nj in xnbs:
            ini, jnj= i + ni, j + nj
            if (ini, jnj) not in nodes and visible(ini, jnj):
                nodes[(ini, jnj)] = (i, j, gen + 1)
                new_nodes.append((ini, jnj))
    unvisited_nodes[:] = new_nodes

def visible(i, j):
    x, y = ij_to_xy(i, j)
    return (abs(x) < width / 2 - W * 2 and
            abs(y) < height / 2 - W * 2)

def key_pressed():
    if key == ' ':
        print(frame_count)
        start(frame_count)
    elif key == 's':
        save_frame(f'{rnd_seed}.png')

def hexagon(xo, yo, r):
    with begin_closed_shape(): # começa a desenhar a forma
        for i in range(6):
            ang = i * TWO_PI / 6
            x = xo + cos(ang) * r
            y = yo + sin(ang) * r
            vertex(x, y)


# Authors: Soledad Galli <solegalli@protonmail.com>
# License: BSD 3 clause

from typing import List, Optional, Union

import numpy as np
import pandas as pd

from feature_engine._docstrings.fit_attributes import (
    _feature_names_in_docstring,
    _n_features_in_docstring,
    _variables_attribute_docstring,
)
from feature_engine._docstrings.methods import (
    _fit_transform_docstring,
    _transform_imputers_docstring,
)
from feature_engine._docstrings.substitute import Substitution
from feature_engine.dataframe_checks import check_X
from feature_engine.imputation.base_imputer import BaseImputer
from feature_engine.tags import _return_tags
from feature_engine.variable_handling._init_parameter_checks import (
    _check_init_parameter_variables,
)
from feature_engine.variable_handling.variable_type_selection import find_all_variables

# for RandomSampleImputer
def _define_seed(
    X: pd.DataFrame,
    index: int,
    seed_variables: Union[str, int, List[Union[str, int]]],
    how: str = "add",
) -> int:
    # determine seed by adding or multiplying the value of 1 or
    # more variables
    if how == "add":
        internal_seed = int(np.round(X.loc[index, seed_variables].sum(), 0))
    elif how == "multiply":
        internal_seed = int(np.round(X.loc[index, seed_variables].product(), 0))
    return internal_seed

@Substitution(
    variables_=_variables_attribute_docstring,
    feature_names_in_=_feature_names_in_docstring,
    n_features_in_=_n_features_in_docstring,
    transform=_transform_imputers_docstring,
    fit_transform=_fit_transform_docstring,
)
class RandomSampleImputer(BaseImputer):
    """
    The RandomSampleImputer() replaces missing data with a random sample extracted from
    the variables in the training set.

    The RandomSampleImputer() works with both numerical and categorical variables.

    **Note**

    The Random samples used to replace missing values may vary from execution to
    execution. This may affect the results of your work. Thus, it is advisable to set a
    seed.

    More details in the :ref:`User Guide <random_sample_imputer>`.

    Parameters
    ----------
    variables: list, default=None
        The list of variables to be imputed. If None, the imputer will select
        all variables in the train set.

    random_state: int, str or list, default=None
        The random_state can take an integer to set the seed when extracting the
        random samples. Alternatively, it can take a variable name or a list of
        variables, which values will be used to determine the seed, observation per
        observation.

    seed: str, default='general'
        Indicates whether the seed should be set for each observation with missing
        values, or if one seed should be used to impute all observations in one go.

        **'general'**: one seed will be used to impute the entire dataframe. This is
        equivalent to setting the seed in pandas.sample(random_state).

        **'observation'**: the seed will be set for each observation using the values
        of the variables indicated in the random_state for that particular
        observation.

    seeding_method: str, default='add'
        If more than one variable are indicated to seed the random sampling per
        observation, you can choose to combine those values as an addition or a
        multiplication. Can take the values 'add' or 'multiply'.

    Attributes
    ----------
    X_:
        Copy of the training dataframe from which to extract the random samples.

    {variables_}

    {feature_names_in_}

    {n_features_in_}

    Methods
    -------
    fit:
        Make a copy of the train set

    {fit_transform}

    {transform}

    Examples
    --------

    >>> import pandas as pd
    >>> import numpy as np
    >>> from feature_engine.imputation import RandomSampleImputer
    >>> X = pd.DataFrame(dict(
    >>>        x1 = [np.nan,1,1,0,np.nan],
    >>>        x2 = ["a", np.nan, "b", np.nan, "a"],
    >>>        ))
    >>> rsi = RandomSampleImputer()
    >>> rsi.fit(X)
    >>> rsi.transform(X)
        x1 x2
    0  1.0  a
    1  1.0  b
    2  1.0  b
    3  0.0  a
    4  1.0  a
    """

    def __init__(
        self,
        variables: Union[None, int, str, List[Union[str, int]]] = None,
        random_state: Union[None, int, str, List[Union[str, int]]] = None,
        seed: str = "general",
        seeding_method: str = "add",
    ) -> None:

        if seed not in ["general", "observation"]:
            raise ValueError("seed takes only values 'general' or 'observation'")

        if seeding_method not in ["add", "multiply"]:
            raise ValueError("seeding_method takes only values 'add' or 'multiply'")

        if seed == "general" and random_state:
            if not isinstance(random_state, int):
                raise ValueError(
                    "if seed == 'general' then random_state must take an integer"
                )

        if seed == "observation" and not random_state:
            raise ValueError(
                "if seed == 'observation' the random state must take the name of one "
                "or more variables which will be used to seed the imputer"
            )

        self.variables = _check_init_parameter_variables(variables)
        self.random_state = random_state
        self.seed = seed
        self.seeding_method = seeding_method

    def fit(self, X: pd.DataFrame, y: Optional[pd.Series] = None):
        """
        Makes a copy of the train set. Only stores a copy of the variables to impute.
        This copy is then used to randomly extract the values to fill the missing data
        during transform.

        Parameters
        ----------

        X: pandas dataframe of shape = [n_samples, n_features]
            The training dataset.

        y: None
            y is not needed in this imputation. You can pass None or y.
        """

        # check input dataframe
        X = check_X(X)

        # find variables to impute
        self.variables_ = find_all_variables(X, self.variables)

        # take a copy of the selected variables
        self.X_ = X[self.variables_].copy()

        # check the variables assigned to the random state
        if self.seed == "observation":
            self.random_state = _check_init_parameter_variables(self.random_state)
            if isinstance(self.random_state, (int, str)):
                self.random_state = [self.random_state]
            if self.random_state and any(
                var for var in self.random_state if var not in X.columns
            ):
                raise ValueError(
                    "There are variables assigned as random state which are not part "
                    "of the training dataframe."
                )

        self._get_feature_names_in(X)

        return self

    def transform(self, X: pd.DataFrame) -> pd.DataFrame:
        """
        Replace missing data with random values taken from the train set.

        Parameters
        ----------

        X: pandas dataframe of shape = [n_samples, n_features]
            The dataframe to be transformed.

        Returns
        -------
        X_new: pandas dataframe of shape = [n_samples, n_features]
            The dataframe without missing values in the transformed variables.
        """

        X = self._transform(X)

        # random sampling with a general seed
        if self.seed == "general":
            for feature in self.variables_:
                if X[feature].isnull().sum() > 0:
                    # determine number of data points to extract at random
                    n_samples = X[feature].isnull().sum()

                    # extract values
                    random_sample = (
                        self.X_[feature]
                        .dropna()
                        .sample(n_samples, replace=True, random_state=self.random_state)
                    )
                    # re-index: pandas needs this to add the values to the right
                    # observations
                    random_sample.index = X[X[feature].isnull()].index

                    # replace na
                    X.loc[X[feature].isnull(), feature] = random_sample

        # random sampling observation per observation
        elif self.seed == "observation" and self.random_state:
            for feature in self.variables_:
                if X[feature].isnull().sum() > 0:

                    # loop over each observation with missing data
                    for i in X[X[feature].isnull()].index:
                        # find the seed using additional variables
                        internal_seed = _define_seed(
                            X, i, self.random_state, how=self.seeding_method
                        )

                        # extract 1 value at random
                        random_sample = (
                            self.X_[feature]
                            .dropna()
                            .sample(1, replace=True, random_state=internal_seed)
                        )
                        random_sample = random_sample.values[0]

                        # replace the missing data point
                        X.loc[i, feature] = random_sample
        return X

    def _more_tags(self):
        tags_dict = _return_tags()
        tags_dict["allow_nan"] = True
        tags_dict["variables"] = "all"
        return tags_dict


from telethon import TelegramClient, events

CHANNEL = "https://t.me/coin_listing"
api_id = ""
api_hash = ""

client = TelegramClient('amirforwarder', api_id, api_hash)

@client.on(events.NewMessage(chats = "https://t.me/coin_listing"))
async def my_event_handler(event):
    if event:
        await client.forward_messages(
        -1001458955463,
        event.message
         )
client.start()
client.run_until_disconnected()


"""
Используя цикл for in ( сколько угодно раз )  и кортежи в списке как исх. данные (задать самому):
вывести в консоль через запятую каждый элемент каждого внутреннего кортежа
"""

DATA = [('one', 'two', 'three'), ('four', 'five', 'six')]

for container in DATA:
    for el in container:
        print(el, end=',')