import pygame

pygame.init()
    #screen
screen = pygame.display.set_mode((700,700))
    #colors
RED = (255, 0, 0)
WHITE = (255, 255, 255)
    #variables
x = 350
y = 350
step = 20
radius = 25
clock = pygame.time.Clock()
    #events
done = False
while not done:
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                    done = True
    #keyboards
    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
        if y - step >= 25:
            y -= step
        else:
            y = 25
    elif pressed[pygame.K_DOWN]:
        if y + step <= 675:
            y += step
        else:
            y = 675
    elif pressed[pygame.K_LEFT]:
        if x - step >= 25:
            x -= step
        else:
            x = 25
    elif pressed[pygame.K_RIGHT]:
        if x + step <= 675:
            x += step
        else:
            x = 675
    screen.fill(WHITE)
    pygame.draw.circle(screen, RED, (x,y), radius) #ball

    pygame.display.flip()
    clock.tick(60) #FPS = frame per second(kak v igrah)


import bpy

projectname = bpy.path.display_name_from_filepath(bpy.data.filepath)

folderpath = "//render/" + projectname + "/"

scene = bpy.data.scenes["Scene"]

print("Rendering spritesheets for", projectname)

# copy since we modify this list mid iteration
render_objects = [bpy.data.objects.get("Root")]

i = 0

while i < len(render_objects):

    obj = render_objects[i]

    i = i + 1

    for obj_child in obj.children:
        render_objects.append(obj_child)

# Hide all objects
for obj in render_objects:
    obj.hide_render = True

# Unhide, render and hide again
for obj in render_objects:

    if obj.type != "MESH":
        continue

    obj.hide_render = False

    # Move object to origin
    location_backup = obj.location.copy()
    rotation_backup = obj.rotation_euler.copy()
    obj.location = [0,0,0]
    obj.rotation_euler = [0,0,0]

    print("Rendering part:", obj.name)

    for frame_current in range(scene.frame_start,scene.frame_end):

        filepath = folderpath + projectname + "_" + obj.name + "_" + str(frame_current) + ".png"

        scene.frame_current = frame_current

        bpy.context.scene.render.filepath = filepath

        bpy.ops.render.render(write_still=True)

    obj.hide_render = True

    obj.location = location_backup
    obj.rotation_euler = rotation_backup

scene.frame_current = scene.frame_start

print("Finished.")


from NDN.SafetyMessage import SafetyMessage
from NDN.Keys import Keys
from NDN.Name import Name
from NDN.SafetyMessage import SafetyMessageTLV

"""
This file is used to check the working of all the ten safety messages
"""
safety_message_tlvs = SafetyMessageTLV.tlvs
safety_messages = [ "EEBL", "PCN","RHCN","RFN","SVA","CCW" ,"CVW","CRN" ,"CL" ,"EVA" ]
key = Keys()
private_key=key.get_private_key()
public_key=key.get_public_key()
print(public_key)

while True:
   type = int(input("Please enter \n1 to send EEBL\t 2 to send PCN\t 3 to send RHCN"
                    "\t4 to send RFN\t 5 to send SVA\n 6 to send CCW\t 7 to send CVW"
                    "\t8 to send CRN\t 9 to send CL\t 10 to send EVA\t : "))
   if type not in range(1,11):
      break
   sm_type = safety_messages[type - 1]
   name = "/"+str(public_key)+ sm_type
   content = f"this is {sm_type} message"

   # user's public key and safety message type
   sm = SafetyMessage(Name(name), content, sm_type)
   sm.set_signature(private_key)
   print("safety_message  : "+sm.get_content())


import functools
import time
from typing import Any, Callable, Dict, Optional, Tuple, Union

from absl import logging
from brax import base
from brax import envs
from brax.io import model
from brax.training import acting
from brax.training import gradients
from brax.training import pmap
from brax.training import replay_buffers
from brax.training import types
from brax.training.acme import running_statistics
from brax.training.acme import specs
from brax.training.agents.sac import losses as sac_losses
from brax.training.agents.sac import networks as sac_networks
from brax.training.types import Params
from brax.training.types import PRNGKey
from brax.v1 import envs as envs_v1
import flax
import jax
import jax.numpy as jnp
import optax

Metrics = types.Metrics
Transition = types.Transition
InferenceParams = Tuple[running_statistics.NestedMeanStd, Params]

ReplayBufferState = Any

_PMAP_AXIS_NAME = 'i'

@flax.struct.dataclass
class TrainingState:
  """Contains training state for the learner."""
  policy_optimizer_state: optax.OptState
  policy_params: Params
  q_optimizer_state: optax.OptState
  q_params: Params
  target_q_params: Params
  gradient_steps: jnp.ndarray
  env_steps: jnp.ndarray
  alpha_optimizer_state: optax.OptState
  alpha_params: Params
  normalizer_params: running_statistics.RunningStatisticsState

def _unpmap(v):
  return jax.tree_util.tree_map(lambda x: x[0], v)

def _init_training_state(
    key: PRNGKey, obs_size: int, local_devices_to_use: int,
    sac_network: sac_networks.SACNetworks,
    alpha_optimizer: optax.GradientTransformation,
    policy_optimizer: optax.GradientTransformation,
    q_optimizer: optax.GradientTransformation) -> TrainingState:
  """Inits the training state and replicates it over devices."""
  key_policy, key_q = jax.random.split(key)
  log_alpha = jnp.asarray(0., dtype=jnp.float32)
  alpha_optimizer_state = alpha_optimizer.init(log_alpha)

  policy_params = sac_network.policy_network.init(key_policy)
  policy_optimizer_state = policy_optimizer.init(policy_params)
  q_params = sac_network.q_network.init(key_q)
  q_optimizer_state = q_optimizer.init(q_params)

  normalizer_params = running_statistics.init_state(
      specs.Array((obs_size,), jnp.dtype('float32')))

  training_state = TrainingState(
      policy_optimizer_state=policy_optimizer_state,
      policy_params=policy_params,
      q_optimizer_state=q_optimizer_state,
      q_params=q_params,
      target_q_params=q_params,
      gradient_steps=jnp.zeros(()),
      env_steps=jnp.zeros(()),
      alpha_optimizer_state=alpha_optimizer_state,
      alpha_params=log_alpha,
      normalizer_params=normalizer_params)
  return jax.device_put_replicated(training_state,
                                   jax.local_devices()[:local_devices_to_use])

def train(
    environment: Union[envs_v1.Env, envs.Env],
    num_timesteps,
    episode_length: int,
    action_repeat: int = 1,
    num_envs: int = 1,
    num_eval_envs: int = 128,
    learning_rate: float = 1e-4,
    discounting: float = 0.9,
    seed: int = 0,
    batch_size: int = 256,
    num_evals: int = 1,
    normalize_observations: bool = False,
    max_devices_per_host: Optional[int] = None,
    reward_scaling: float = 1.0,
    tau: float = 0.005,
    min_replay_size: int = 0,
    max_replay_size: Optional[int] = None,
    grad_updates_per_step: int = 1,
    deterministic_eval: bool = False,
    network_factory: types.NetworkFactory[
        sac_networks.SACNetworks
    ] = sac_networks.make_sac_networks,
    progress_fn: Callable[[int, Metrics], None] = lambda *args: None,
    checkpoint_logdir: Optional[str] = None,
    eval_env: Optional[envs.Env] = None,
    randomization_fn: Optional[
        Callable[[base.System, jnp.ndarray], Tuple[base.System, base.System]]
    ] = None,
):
  """SAC training."""
  process_id = jax.process_index()
  local_devices_to_use = jax.local_device_count()
  if max_devices_per_host is not None:
    local_devices_to_use = min(local_devices_to_use, max_devices_per_host)
  device_count = local_devices_to_use * jax.process_count()
  logging.info('local_device_count: %s; total_device_count: %s',
               local_devices_to_use, device_count)

  if min_replay_size >= num_timesteps:
    raise ValueError(
        'No training will happen because min_replay_size >= num_timesteps')

  if max_replay_size is None:
    max_replay_size = num_timesteps

  # The number of environment steps executed for every `actor_step()` call.
  env_steps_per_actor_step = action_repeat * num_envs
  # equals to ceil(min_replay_size / env_steps_per_actor_step)
  num_prefill_actor_steps = -(-min_replay_size // num_envs)
  num_prefill_env_steps = num_prefill_actor_steps * env_steps_per_actor_step
  assert num_timesteps - num_prefill_env_steps >= 0
  num_evals_after_init = max(num_evals - 1, 1)
  # The number of run_one_sac_epoch calls per run_sac_training.
  # equals to
  # ceil(num_timesteps - num_prefill_env_steps /
  #      (num_evals_after_init * env_steps_per_actor_step))
  num_training_steps_per_epoch = -(
      -(num_timesteps - num_prefill_env_steps) //
      (num_evals_after_init * env_steps_per_actor_step))

  assert num_envs % device_count == 0
  env = environment
  if isinstance(env, envs.Env):
    wrap_for_training = envs.training.wrap
  else:
    wrap_for_training = envs_v1.wrappers.wrap_for_training

  rng = jax.random.PRNGKey(seed)
  rng, key = jax.random.split(rng)
  v_randomization_fn = None
  if randomization_fn is not None:
    v_randomization_fn = functools.partial(
        randomization_fn,
        rng=jax.random.split(
            key, num_envs // jax.process_count() // local_devices_to_use),
    )
  env = wrap_for_training(
      env,
      episode_length=episode_length,
      action_repeat=action_repeat,
      randomization_fn=v_randomization_fn,
  )

  obs_size = env.observation_size
  action_size = env.action_size

  normalize_fn = lambda x, y: x
  if normalize_observations:
    normalize_fn = running_statistics.normalize
  sac_network = network_factory(
      observation_size=obs_size,
      action_size=action_size,
      preprocess_observations_fn=normalize_fn)
  make_policy = sac_networks.make_inference_fn(sac_network)

  alpha_optimizer = optax.adam(learning_rate=3e-4)

  policy_optimizer = optax.adam(learning_rate=learning_rate)
  q_optimizer = optax.adam(learning_rate=learning_rate)

  dummy_obs = jnp.zeros((obs_size,))
  dummy_action = jnp.zeros((action_size,))
  dummy_transition = Transition(  # pytype: disable=wrong-arg-types  # jax-ndarray
      observation=dummy_obs,
      action=dummy_action,
      reward=0.,
      discount=0.,
      next_observation=dummy_obs,
      extras={
          'state_extras': {
              'truncation': 0.
          },
          'policy_extras': {}
      })
  replay_buffer = replay_buffers.UniformSamplingQueue(
      max_replay_size=max_replay_size // device_count,
      dummy_data_sample=dummy_transition,
      sample_batch_size=batch_size * grad_updates_per_step // device_count)

  alpha_loss, critic_loss, actor_loss = sac_losses.make_losses(
      sac_network=sac_network,
      reward_scaling=reward_scaling,
      discounting=discounting,
      action_size=action_size)
  alpha_update = gradients.gradient_update_fn(  # pytype: disable=wrong-arg-types  # jax-ndarray
      alpha_loss, alpha_optimizer, pmap_axis_name=_PMAP_AXIS_NAME)
  critic_update = gradients.gradient_update_fn(  # pytype: disable=wrong-arg-types  # jax-ndarray
      critic_loss, q_optimizer, pmap_axis_name=_PMAP_AXIS_NAME)
  actor_update = gradients.gradient_update_fn(  # pytype: disable=wrong-arg-types  # jax-ndarray
      actor_loss, policy_optimizer, pmap_axis_name=_PMAP_AXIS_NAME)

  def sgd_step(
      carry: Tuple[TrainingState, PRNGKey],
      transitions: Transition) -> Tuple[Tuple[TrainingState, PRNGKey], Metrics]:
    training_state, key = carry

    key, key_alpha, key_critic, key_actor = jax.random.split(key, 4)

    alpha_loss, alpha_params, alpha_optimizer_state = alpha_update(
        training_state.alpha_params,
        training_state.policy_params,
        training_state.normalizer_params,
        transitions,
        key_alpha,
        optimizer_state=training_state.alpha_optimizer_state)
    alpha = jnp.exp(training_state.alpha_params)
    critic_loss, q_params, q_optimizer_state = critic_update(
        training_state.q_params,
        training_state.policy_params,
        training_state.normalizer_params,
        training_state.target_q_params,
        alpha,
        transitions,
        key_critic,
        optimizer_state=training_state.q_optimizer_state)
    actor_loss, policy_params, policy_optimizer_state = actor_update(
        training_state.policy_params,
        training_state.normalizer_params,
        training_state.q_params,
        alpha,
        transitions,
        key_actor,
        optimizer_state=training_state.policy_optimizer_state)

    new_target_q_params = jax.tree_util.tree_map(
        lambda x, y: x * (1 - tau) + y * tau, training_state.target_q_params,
        q_params)

    metrics = {
        'critic_loss': critic_loss,
        'actor_loss': actor_loss,
        'alpha_loss': alpha_loss,
        'alpha': jnp.exp(alpha_params),
    }

    new_training_state = TrainingState(
        policy_optimizer_state=policy_optimizer_state,
        policy_params=policy_params,
        q_optimizer_state=q_optimizer_state,
        q_params=q_params,
        target_q_params=new_target_q_params,
        gradient_steps=training_state.gradient_steps + 1,
        env_steps=training_state.env_steps,
        alpha_optimizer_state=alpha_optimizer_state,
        alpha_params=alpha_params,
        normalizer_params=training_state.normalizer_params)
    return (new_training_state, key), metrics

  def get_experience(
      normalizer_params: running_statistics.RunningStatisticsState,
      policy_params: Params, env_state: Union[envs.State, envs_v1.State],
      buffer_state: ReplayBufferState, key: PRNGKey
  ) -> Tuple[running_statistics.RunningStatisticsState,
             Union[envs.State, envs_v1.State], ReplayBufferState]:
    policy = make_policy((normalizer_params, policy_params))
    env_state, transitions = acting.actor_step(
        env, env_state, policy, key, extra_fields=('truncation',))

    normalizer_params = running_statistics.update(
        normalizer_params,
        transitions.observation,
        pmap_axis_name=_PMAP_AXIS_NAME)

    buffer_state = replay_buffer.insert(buffer_state, transitions)
    return normalizer_params, env_state, buffer_state

  def training_step(
      training_state: TrainingState, env_state: envs.State,
      buffer_state: ReplayBufferState, key: PRNGKey
  ) -> Tuple[TrainingState, Union[envs.State, envs_v1.State],
             ReplayBufferState, Metrics]:
    experience_key, training_key = jax.random.split(key)
    normalizer_params, env_state, buffer_state = get_experience(
        training_state.normalizer_params, training_state.policy_params,
        env_state, buffer_state, experience_key)
    training_state = training_state.replace(
        normalizer_params=normalizer_params,
        env_steps=training_state.env_steps + env_steps_per_actor_step)

    buffer_state, transitions = replay_buffer.sample(buffer_state)
    # Change the front dimension of transitions so 'update_step' is called
    # grad_updates_per_step times by the scan.
    transitions = jax.tree_util.tree_map(
        lambda x: jnp.reshape(x, (grad_updates_per_step, -1) + x.shape[1:]),
        transitions)
    (training_state, _), metrics = jax.lax.scan(sgd_step,
                                                (training_state, training_key),
                                                transitions)

    metrics['buffer_current_size'] = replay_buffer.size(buffer_state)
    return training_state, env_state, buffer_state, metrics

  def prefill_replay_buffer(
      training_state: TrainingState, env_state: envs.State,
      buffer_state: ReplayBufferState, key: PRNGKey
  ) -> Tuple[TrainingState, envs.State, ReplayBufferState, PRNGKey]:

    def f(carry, unused):
      del unused
      training_state, env_state, buffer_state, key = carry
      key, new_key = jax.random.split(key)
      new_normalizer_params, env_state, buffer_state = get_experience(
          training_state.normalizer_params, training_state.policy_params,
          env_state, buffer_state, key)
      new_training_state = training_state.replace(
          normalizer_params=new_normalizer_params,
          env_steps=training_state.env_steps + env_steps_per_actor_step)
      return (new_training_state, env_state, buffer_state, new_key), ()

    return jax.lax.scan(
        f, (training_state, env_state, buffer_state, key), (),
        length=num_prefill_actor_steps)[0]

  prefill_replay_buffer = jax.pmap(
      prefill_replay_buffer, axis_name=_PMAP_AXIS_NAME)

  def training_epoch(
      training_state: TrainingState, env_state: envs.State,
      buffer_state: ReplayBufferState, key: PRNGKey
  ) -> Tuple[TrainingState, envs.State, ReplayBufferState, Metrics]:

    def f(carry, unused_t):
      ts, es, bs, k = carry
      k, new_key = jax.random.split(k)
      ts, es, bs, metrics = training_step(ts, es, bs, k)
      return (ts, es, bs, new_key), metrics

    (training_state, env_state, buffer_state, key), metrics = jax.lax.scan(
        f, (training_state, env_state, buffer_state, key), (),
        length=num_training_steps_per_epoch)
    metrics = jax.tree_util.tree_map(jnp.mean, metrics)
    return training_state, env_state, buffer_state, metrics

  training_epoch = jax.pmap(training_epoch, axis_name=_PMAP_AXIS_NAME)

  # Note that this is NOT a pure jittable method.
  def training_epoch_with_timing(
      training_state: TrainingState, env_state: envs.State,
      buffer_state: ReplayBufferState, key: PRNGKey
  ) -> Tuple[TrainingState, envs.State, ReplayBufferState, Metrics]:
    nonlocal training_walltime
    t = time.time()
    (training_state, env_state, buffer_state,
     metrics) = training_epoch(training_state, env_state, buffer_state, key)
    metrics = jax.tree_util.tree_map(jnp.mean, metrics)
    jax.tree_util.tree_map(lambda x: x.block_until_ready(), metrics)

    epoch_training_time = time.time() - t
    training_walltime += epoch_training_time
    sps = (env_steps_per_actor_step *
           num_training_steps_per_epoch) / epoch_training_time
    metrics = {
        'training/sps': sps,
        'training/walltime': training_walltime,
        **{f'training/{name}': value for name, value in metrics.items()}
    }
    return training_state, env_state, buffer_state, metrics  # pytype: disable=bad-return-type  # py311-upgrade

  global_key, local_key = jax.random.split(rng)
  local_key = jax.random.fold_in(local_key, process_id)

  # Training state init
  training_state = _init_training_state(
      key=global_key,
      obs_size=obs_size,
      local_devices_to_use=local_devices_to_use,
      sac_network=sac_network,
      alpha_optimizer=alpha_optimizer,
      policy_optimizer=policy_optimizer,
      q_optimizer=q_optimizer)
  del global_key

  local_key, rb_key, env_key, eval_key = jax.random.split(local_key, 4)

  # Env init
  env_keys = jax.random.split(env_key, num_envs // jax.process_count())
  env_keys = jnp.reshape(env_keys,
                         (local_devices_to_use, -1) + env_keys.shape[1:])
  env_state = jax.pmap(env.reset)(env_keys)

  # Replay buffer init
  buffer_state = jax.pmap(replay_buffer.init)(
      jax.random.split(rb_key, local_devices_to_use))

  if not eval_env:
    eval_env = environment
  if randomization_fn is not None:
    v_randomization_fn = functools.partial(
        randomization_fn, rng=jax.random.split(eval_key, num_eval_envs)
    )
  eval_env = wrap_for_training(
      eval_env,
      episode_length=episode_length,
      action_repeat=action_repeat,
      randomization_fn=v_randomization_fn,
  )

  evaluator = acting.Evaluator(
      eval_env,
      functools.partial(make_policy, deterministic=deterministic_eval),
      num_eval_envs=num_eval_envs,
      episode_length=episode_length,
      action_repeat=action_repeat,
      key=eval_key)

  # Run initial eval
  metrics = {}
  if process_id == 0 and num_evals > 1:
    metrics = evaluator.run_evaluation(
        _unpmap(
            (training_state.normalizer_params, training_state.policy_params)),
        training_metrics={})
    logging.info(metrics)
    progress_fn(0, metrics)

  # Create and initialize the replay buffer.
  t = time.time()
  prefill_key, local_key = jax.random.split(local_key)
  prefill_keys = jax.random.split(prefill_key, local_devices_to_use)
  training_state, env_state, buffer_state, _ = prefill_replay_buffer(
      training_state, env_state, buffer_state, prefill_keys)

  replay_size = jnp.sum(jax.vmap(
      replay_buffer.size)(buffer_state)) * jax.process_count()
  logging.info('replay size after prefill %s', replay_size)
  assert replay_size >= min_replay_size
  training_walltime = time.time() - t

  current_step = 0
  for _ in range(num_evals_after_init):
    logging.info('step %s', current_step)

    # Optimization
    epoch_key, local_key = jax.random.split(local_key)
    epoch_keys = jax.random.split(epoch_key, local_devices_to_use)
    (training_state, env_state, buffer_state,
     training_metrics) = training_epoch_with_timing(training_state, env_state,
                                                    buffer_state, epoch_keys)
    current_step = int(_unpmap(training_state.env_steps))

    # Eval and logging
    if process_id == 0:
      if checkpoint_logdir:
        # Save current policy.
        params = _unpmap(
            (training_state.normalizer_params, training_state.policy_params))
        path = f'{checkpoint_logdir}_sac_{current_step}.pkl'
        model.save_params(path, params)

      # Run evals.
      metrics = evaluator.run_evaluation(
          _unpmap(
              (training_state.normalizer_params, training_state.policy_params)),
          training_metrics)
      logging.info(metrics)
      progress_fn(current_step, metrics)

  total_steps = current_step
  assert total_steps >= num_timesteps

  params = _unpmap(
      (training_state.normalizer_params, training_state.policy_params))

  # If there was no mistakes the training_state should still be identical on all
  # devices.
  pmap.assert_is_replicated(training_state)
  logging.info('total steps: %s', total_steps)
  pmap.synchronize_hosts()
  return (make_policy, params, metrics)


from django.urls import path

from . import views

app_name = 'quotes_divination'

urlpatterns = [
    path('', views.CategoryListView.as_view(), name='category_quotes_list'),
    path('<slug:slug>/', views.RandomQuoteView.as_view(), name='random_quote'),
]


# #1 Write a Python program to sum all the items in a list.
# list_values = [2,5,7,56,34,24,89,28,90,67]
# result = 0
# for i in list_values:
#     result += i
# print(result)
#
# # print(2*5*7*56*34*24*89*28*90*67)
#
# def sum_list(list_items):
#     result = 0
#     for value in list_items:
#         result += value
#     return  result
# print(sum_list([2,5,7,56,34,24,89,28,90,67])) #402
#
# #2.  Write a Python program to multiply all the items in a list.
#
# def list_multiple(list_items):
#     multiple = 1
#     for i in list_items:
#         multiple *= i
#     return multiple
# print(list_multiple([2,5,7,56,34,24,89,28,90,67]))
#
# #3. Write a Python program to get the largest number from a list.
#
# def max_no_list(values):
#     result = max(values)
#     return result
# print(max_no_list([2,5,7,56,34,24,89,28,90,67]))
#
#
# #4.Write a Python program to get the smallest number from a list
#
# def max_no_list(values):
#     result = min(values)
#     return result
# print(max_no_list([2,5,7,56,34,24,89,28,90,67]))

"""5. Write a Python program to count the number of strings where the string length is 2 or more and the first and
last character are same from a given list of strings.
Sample List : ['abc', 'xyz', 'aba', '1221']"""
# sample_list = ['abc', 'xyz', 'aba', '1221']
# def count_strings(value):
#     count = 0
#     for i in value:
#         if len(i) >= 2 and (i[0] == i[-1]):
#             count += 1
#     return count
# print(count_strings(sample_list))

"""6. Write a Python program to get a list, sorted in increasing order by the last element in each tuple from a given list of non-empty tuples.
Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]"""
# def arrange(m):
#     return m[-1]
# def sorted_list(list):
#     print(sorted(list,key=arrange))
#
# sorted_list([(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)])

"""7. Write a Python program to remove duplicates from a list."""
def remove_duplicate_val(list):
    return set(list)
value = [10,20,30,20,10,50,60,40,80,50,40]
print(list(remove_duplicate_val(value)))

# alternate sol:
list_values = [10,20,30,20,10,50,60,40,80,50,40]
set_values = set()
list_unique = []
for value in list_values:
    if value not in set_values:
        list_unique.append(value)
        set_values.add(value)
print(f"Unique list : {list_unique}")

# print(set.__dict__)

"""8. Write a Python program to check a list is empty or not."""
def check_list(value):
    if value == []:
        print(f"list is empty")
    else:
        print(f"list is not empty")

list = [10,45]
check_list(list)

"""9. Write a Python program to clone or copy a list"""
list = [1,34,45,78,79]
a = [12]
a= list.copy()
print(a)


import torchvision.transforms as transforms
from face_modules.model import Backbone
from face_modules.mtcnn import *
import PIL.Image as Image
from network.aei import *
import numpy as np
import torch
import time
import cv2

def swap_faces(Xs_raw, Xt_raw):
    detector = MTCNN()
    device = torch.device('cpu')
    G = AEI_Net(c_id=512)
    G.eval()
    G.load_state_dict(torch.load('./saved_models/G_latest.pth', map_location=device))
    G = G.cpu()

    arcface = Backbone(50, 0.6, 'ir_se').to(device)
    arcface.eval()
    arcface.load_state_dict(torch.load('./saved_models/model_ir_se50.pth', map_location=device), strict=False)

    test_transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

    Xs_img = Image.fromarray(Xs_raw)
    Xs = detector.align(Xs_img, crop_size=(64, 64))

    Xs = test_transform(Xs)
    Xs = Xs.unsqueeze(0).cpu()
    with torch.no_grad():
        embeds, Xs_feats = arcface(F.interpolate(Xs, (112, 112), mode='bilinear', align_corners=True))
        embeds = embeds.mean(dim=0, keepdim=True)

    mask = np.zeros([64, 64], dtype=np.float)
    for i in range(64):
        for j in range(64):
            dist = np.sqrt((i-32)**2 + (j-32)**2)/32
            dist = np.minimum(dist, 1)
            mask[i, j] = 1-dist
    mask = cv2.dilate(mask, None, iterations=20)

    Xt_img = Image.fromarray(Xt_raw)

    Xt, trans_inv = detector.align(Xt_img, crop_size=(64, 64), return_trans_inv=True)

    Xt = test_transform(Xt)

    Xt = Xt.unsqueeze(0).cpu()
    with torch.no_grad():
        st = time.time()
        Yt, _ = G(Xt, embeds)
        Yt = Yt.squeeze().detach().cpu().numpy()
        st = time.time() - st
        print(f'inference time: {st} sec')
        Yt = Yt.transpose([1, 2, 0])*0.5 + 0.5
        Yt_trans_inv = cv2.warpAffine(Yt, trans_inv, (np.size(Xt_raw, 1), np.size(Xt_raw, 0)), borderMode=cv2.BORDER_TRANSPARENT)
        mask_ = cv2.warpAffine(mask, trans_inv, (np.size(Xt_raw, 1), np.size(Xt_raw, 0)), borderMode=cv2.BORDER_TRANSPARENT)
        mask_ = np.expand_dims(mask_, 2)
        Yt_trans_inv = mask_*Yt_trans_inv + (1-mask_)*(Xt_raw.astype(np.float)/255.)

        merge = Yt_trans_inv

        return merge


import tensorflow as tf

def truncate_sequences(sequence, max_length):
    input_sequence = sequence[:max_length - 1]
    target_sequence = sequence[1:max_length]
    return input_sequence, target_sequence

def pad_sequences(sequence, max_length):
    padding_amount = max_length - len(sequence)
    padding = [0] * padding_amount
    input_sequence = sequence[:-1] + padding
    target_sequence = sequence[1:] + padding
    return input_sequence, target_sequence

# LSTM Language Model
class LanguageModel(object):
    # Model Initialization
    def __init__(self, vocab_size, max_length, num_lstm_units, num_lstm_layers):
        self.vocab_size = vocab_size
        self.max_length = max_length
        self.num_lstm_units = num_lstm_units
        self.num_lstm_layers = num_lstm_layers
        self.tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=vocab_size)

    def get_input_target_sequence(self, sequence):
        seq_len = len(sequence)
        if seq_len >= self.max_length:
            input_sequence, target_sequence = truncate_sequences(
                sequence, self.max_length
            )
        else:
            input_sequence, target_sequence = pad_sequences(
                sequence, self.max_length
            )
        return input_sequence, target_sequence

    # Create a cell for the LSTM
    def make_lstm_cell(self, dropout_keep_prob):
        cell = tf.nn.rnn_cell.LSTMCell(self.num_lstm_units)
        return tf.nn.rnn_cell.DropoutWrapper(
            cell, output_keep_prob=dropout_keep_prob)

    # Stack multiple layers for the LSTM
    def stacked_lstm_cells(self, is_training):
        dropout_keep_prob = 0.5 if is_training else 1.0
        cell_list = [self.make_lstm_cell(dropout_keep_prob) for i in range(self.num_lstm_layers)]
        cell = tf.nn.rnn_cell.MultiRNNCell(cell_list)
        return cell

    # Convert input sequences to embeddings
    def get_input_embeddings(self, input_sequences):
        embedding_dim = int(self.vocab_size ** 0.25)
        initial_bounds = 0.5 / embedding_dim
        initializer = tf.random_uniform(
            [self.vocab_size, embedding_dim],
            minval=-initial_bounds,
            maxval=initial_bounds)
        self.input_embedding_matrix = tf.get_variable('input_embedding_matrix',
                                                      initializer=initializer)
        input_embeddings = tf.nn.embedding_lookup(self.input_embedding_matrix, input_sequences)
        return input_embeddings

    # Run the LSTM on the input sequences
    def run_lstm(self, input_sequences, is_training):
        cell = self.stacked_lstm_cells(is_training)
        input_embeddings = self.get_input_embeddings(input_sequences)
        binary_sequences = tf.sign(input_sequences)
        sequence_lengths = tf.reduce_sum(binary_sequences, axis=1)
        lstm_outputs, _ = tf.nn.dynamic_rnn(
            cell,
            input_embeddings,
            sequence_length=sequence_lengths,
            dtype=tf.float32)
        return lstm_outputs, binary_sequences

    # Calculate model loss
    def calculate_loss(self, lstm_outputs, binary_sequences, output_sequences):
        logits = tf.layers.dense(lstm_outputs, self.vocab_size)
        batch_sequence_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=output_sequences, logits=logits)
        unpadded_loss = batch_sequence_loss * tf.cast(binary_sequences, tf.float32)
        overall_loss = tf.reduce_sum(unpadded_loss)
        return overall_loss

    # Predict the next word ID
    def get_word_predictions(self, word_preds, binary_sequences, batch_size):
        row_indices = tf.range(batch_size)
        final_indexes = tf.reduce_sum(binary_sequences, axis=1) - 1
        gather_indices = tf.transpose([row_indices, final_indexes])
        final_id_predictions = tf.gather_nd(word_preds, gather_indices)
        return final_id_predictions


import re

f = open('inputfinal')

stacks = []
for line in f:
    if len(line.strip()) == 0:
        break

    crates = [line[i:i+3] for i in range(0, len(line), 4)]
    spot = 0
    while spot < len(crates):
        if crates[spot][0] == '[':
            while len(stacks) <= spot:
                stacks.append([])
            stacks[spot].insert(0, crates[spot][1])
        spot += 1

for line in f:
    m,f,t = map(int, re.findall('\d+', line))
    for i in range(m):
        stacks[t-1].append(stacks[f-1].pop())

for stack in stacks:
    print(stack.pop(), end='')


#!/usr/bin/env python

import os
import sys
import Queue
import threading
import hashlib
import tarfile
from multiprocessing import Pool
from functools import partial
from subprocess import call

THREADS = 8

def pack_and_send(output_path, path):
    md5 = hashlib.md5()
    md5.update(path)
    tarname = md5.hexdigest()[:10] + '.tar'
    print(tarname)
    tarpath = output_path + '/' + tarname, "w"
    tar = tarfile.open(tarpath)
    tar.add(path)
    tar.close()
    call("scp %s hadoop.vls:/mnt/tmp/pszostek_pubmed" % tarpath)
    print(tarpath)

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: " + sys.argv[0] + " input_path output_path")
        exit(1)

    path = sys.argv[1]
    prefixes = ["%02x" % x for x in xrange(0,256)]
    paths = [path + '/' + prefix for prefix in prefixes]
    output_path = sys.argv[2]

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    pool = Pool(processes=8)
    pool.map(partial(pack_and_send, output_path=output_path), paths)


import unicodedata

class Vigenere:
    def normalize(text):
        normalized = unicodedata.normalize('NFKD', text).encode('ASCII', 'ignore')
        return (normalized.decode("utf-8"))
    def encode(string, key):
        M = Vigenere.normalize(string)
        new_message = []
        ukey = key.upper()
        i, j = 0, 0
        while (i < len(M)):
            j %= len(key)
            if (not M[i].isalpha()):
                new_message.append(M[i])
                i += 1
                continue
            new = chr((ord(M[i].upper()) - 2 * ord('A') + ord(ukey[j])) % 26 + ord('A'))
            if (M[i].islower()):
                new = new.lower()
            new_message.append(new)
            j += 1
            i += 1

        return ''.join(new_message)
    def decode(string, key):
        new_message = []
        ukey = key.upper()
        i, j = 0, 0
        while (i < len(string)):
            j %= len(key)
            if (not string[i].isalpha()):
                new_message.append(string[i])
                i += 1
                continue
            new = chr((ord(string[i].upper()) - 2 * ord('A') - ord(ukey[j])) % 26 + ord('A'))
            if (string[i].islower()):
                new = new.lower()
            new_message.append(new)
            j += 1
            i += 1

        return ''.join(new_message)
    def analyse(message):
        cleaned = []
        for char in message:
            if char.isalnum():
                cleaned.append(char)

        cleaned = "".join(cleaned)

        key_len = Vigenere.vig_key_length(cleaned)

        key = ""
        for i in range(key_len):
            occ = [0 for i in range(26)]
            letters = cleaned[i:len(cleaned):key_len]
            for letter in letters:
                occ[ord(letter.upper()) - 65] += 1
            c = occ.index(max(occ))
            key += chr(((c - ord('E') + 65) % 26) + 65)
        return key

    def PGCD(a, b):
        if b == 0:
            return a
        else:
            r = a % b
            return Vigenere.PGCD(b, r)

    def vig_key_length(message, word_length=3, max_key=0):
        if word_length > len(message) / 2:
            return max_key

        words = {}

        for k in range(0, len(message) - word_length + 1):
            word = message[k:k + word_length].upper()
            if word in words:
                words[word].append(k)
            else:
                words[word] = [k]

        distance = []
        for w in words:
            occ = words[w]
            if len(occ) > 1:
                for i in range(0, len(occ) - 1):
                    distance.append(occ[i + 1] - occ[i])
        if (len(distance) < 1):
            return max(max_key, 0)
        elif len(distance) == 1:
            return max(max_key, distance[0])
        else:
            key_len = Vigenere.PGCD(distance[0], distance[1])
            for k in range(len(distance)):
                key_len = Vigenere.PGCD(key_len, distance[k])
            if key_len < 3:
                return Vigenere.vig_key_length(message, word_length + 1, key_len)
            else:
                return key_len


def leer_lista():
    x='a'
    l=[]
    while x!='.':
        print("Introduce un '.' para parar de leer")
        x=input("Introduce una palabra: ")
        if x!='.':
            l.append(x)
    return l
def index_palabra(a):
    x=input("Introduce una palabra para indexear: ")
    for i,e in enumerate(a):
        if x==e:
            print("La palabra {} esta situada en {}".format(x,i))
        else:
            print("-1")

#PP
a=leer_lista()
index_palabra(a)


from django.shortcuts import redirect, render
from django.contrib.auth.decorators import login_required
from .models import UsersLogs,Notifications
from django.utils import timezone
from .decorators import allowed_users
# Create your views here.
@login_required
def home(request):
    if request.user.groups.filter(name="Employee").exists():
        return redirect('Home')
    elif request.user.groups.filter(name="Manager").exists():
        return redirect('Dashboard')
    else:
        return redirect('logout')

@login_required
@allowed_users(allowed_roles=['Employee'])
def employee_home(request):
    UserLog = None
    try:
        UserLog = UsersLogs.objects.filter(is_logedout = False,user_id=request.user).first()
    except:
        UserLog=None
    if UserLog == None:
        new_UserLog = UsersLogs()
        new_UserLog.user_id = request.user
        new_UserLog.logedin_at = timezone.now()
        new_UserLog.save()
        UserLog = new_UserLog

    return render(request,'employee_home.html',{"CurrentLog":UserLog})

@login_required
@allowed_users(allowed_roles=['Manager'])
def manager_home(request):
    # counters
    attendants = UsersLogs.objects.filter(is_logedout = False).count()
    leave_requests = UsersLogs.objects.filter(leave_request_at__isnull=False,is_logedout = False,allow_leave=False).count()
    context = {
        "attendants":attendants,
        "leave_requests":leave_requests
        }
    return render(request,'manager_home.html',context)

@login_required
@allowed_users(allowed_roles=['Manager'])
def employees_requests(request):
    leave_req = UsersLogs.objects.filter(leave_request_at__isnull=False,allow_leave=False,is_logedout = False)
    not_read_notific = Notifications.objects.all().filter(is_readed = 0)
    for noti in not_read_notific:
        noti.is_readed = 1
        noti.save()
    return render(request,'employees_requests.html',{"leave_requests":leave_req})

def forbeddin(request):
    return render(request,"registration/forbeddin.html")


import csv
import numpy as np
from sklearn.metrics import mean_absolute_error

# # Doc du lieu
def loadData(path):
    f = open(path, "r") #Mo file
    data = csv.reader(f) #Doc file dang csv
    data = np.array(list(data)) #Chuyen doi tap du lieu thanh dang ma tran
    data = np.delete(data, 0, 0) #Xoa dong dau tien
    np.random.shuffle(data) #Tron ngau nhien tap du lieu
    f.close() #Dong file
    trainSet = data[:int(len(data)*2/3.0)] #Tap training chiem 2/3 tap du lieu
    testSet = data[int(len(data)*2/3.0):] #Tap test chiem 1/3 tap du lieu
    return trainSet, testSet

# #tinh khoang cach
def calcDistance(pointA, pointB, numOfFeature=10): #Vi thuoc tinh la roi rac
    tmp = 0
    for i in range(numOfFeature): #Duyet qua cac thuoc tinh cua diem
        if pointA[i] == pointB[i]: #Neu 2 diem co thuoc tinh giong nhau
            tmp += 1 #Tang bien dem len 1
    return float((numOfFeature-tmp)/numOfFeature) #Tinh khoang cach dua vao so thuoc tinh giong nhau

# Tim k diem du lieu gan nhat
def kNearestNeighbor(trainSet, point, k):
    distances = [] #list luu khoang cach
    for item in trainSet:
        distances.append({ #append 1 dictionary
            "label1": int(item[-3]), # nhan thu nhat
            "label2": int(item[-2]), # nhan thu hai
            "label3": int(item[-1]), # nhan thu ba
            "value": calcDistance(item, point) # khoang cach
        })
    distances.sort(key=lambda x: x["value"]) #Sap xep cac khoang cach theo thu tu tang dan
    labels1 = [item["label1"] for item in distances] #Luu lai cac nhan thu nhat
    labels2 = [item["label2"] for item in distances] #Luu lai cac nhan thu hai
    labels3 = [item["label3"] for item in distances] #Luu lai cac nhan thu ba
    labels1 = labels1[:k] # lay k du lieu dau tien
    labels2 = labels2[:k]
    labels3 = labels3[:k]
    labels = [sum(labels1)/k, sum(labels2)/k, sum(labels3)/k] # vi nhan lien tuc nen nhan du doan se lay trung binh
    return labels

if __name__ == "__main__":
    trainSet, testSet = loadData("flare1.csv")
    label1_expected = list() #Dung de luu lai cac gia tri nhan thuc su
    label2_expected = list()
    label3_expected = list()
    label1_predicted = list() #Dung de luu lai cac gia tri nhan du doan
    label2_predicted = list()
    label3_predicted = list()
    hien_thi_phan_tu = 10
    for item in range(hien_thi_phan_tu):
        label1_expected.append(int(testSet[item][-3]))
        label2_expected.append(int(testSet[item][-2]))
        label3_expected.append(int(testSet[item][-1]))
        knn = kNearestNeighbor(trainSet, testSet[item], 5) #KNN voi 5 diem gan nhat
        print("{}) label: {}, {}, {} -> predicted: {}, {}, {}".format(item+1, testSet[item][-3], testSet[item][-2], testSet[item][-1], knn[0], knn[1], knn[2]))
        label1_predicted.append(knn[0])
        label2_predicted.append(knn[1])
        label3_predicted.append(knn[2])
    errors1 = float(mean_absolute_error(label1_expected, label1_predicted)) #Chi so MAE
    errors2 = float(mean_absolute_error(label2_expected, label2_predicted))
    errors3 = float(mean_absolute_error(label3_expected, label3_predicted))
    print("MAE cua nhan 1: "+str(errors1))
    print("MAE cua nhan 2: "+str(errors2))
    print("MAE cua nhan 3: "+str(errors3))


import random
opcoes=['pedra', 'papel', 'tesoura']
jogador=['pedra', 'papel', 'tesoura']
print('Escolha, pedra papel ou tesoura')
computador=random.choice(opcoes)

if jogador==opcoes and computador=='papel':
    print('Você perdeu')
elif jogador=='pedra' and computador=='pedra':
    print('Empate!')
elif jogador=='pedra' and computador=='tesoura':
    print('Você ganhou')


from app.deprecated.QuizServiceHTTP.Entities.entities import Question, Answer, Quiz, Game

fake_db = {
    "answer": {},
    "question": {},
    "quiz": {},
    "game": {}
}

def init_db():
    fake_db["answer"] = dict({
        "ans1": Answer(id_token="ans1", text="ans1", points=0),
        "ans2": Answer(id_token="ans2", text="ans2", points=1),
        "ans3": Answer(id_token="ans3", text="ans3", points=1),
        "ans4": Answer(id_token="ans4", text="ans4", points=0),
        "ans5": Answer(id_token="ans5", text="ans5", points=2)
    })
    fake_db["question"] = dict({
        "ques1": Question(
            id_token="ques1",
            text="ques1",
            answer_ids=["ans1", "ans2"]
        ),
        "ques2": Question(
            id_token="ques2",
            text="ques2?",
            answer_ids=["ans3", "ans4", "ans5"]
        ),
    })
    fake_db["quiz"] = dict({
        "quiz1": Quiz(
            id_token="ques1",
            text="ques1",
            question_ids=["ques1", "ques2"]
        )
    })
    fake_db["game"] = dict({
        "game1": Game(
            user_id_token="u1",
            quiz_id_token="quiz1",
            status="unseen",
            points=0
        )
    })

def get_question(id_token: str) -> Question:
    return fake_db["question"][id_token]

def add_question(question: Question):
    fake_db["question"][question.id_token] = question

def check_exists_question(id_token: str):
    if id_token in fake_db["question"]:
        return True
    else:
        return False


class Node:
    def __init__(self, data) -> None:
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self) -> None:
        self.head = None

    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node

    def length(self):
        current = self.head
        count = 0
        while current:
            count += 1
            current = current.next
        return count

    def cheak_data(self, target_data):
        current = self.head
        while current:
            if current.data is target_data:
                return True
            current = current.next
        return False

    def cheak_data_recursive(self, target_data, current):
        if(current is not None) and (current.data == target_data):
            return True
        if(not current):
            return False
        return self.cheak_data_recursive(target_data, current.next)

    def cheak_data_recursive_short(self, targrt_data):
        return self.cheak_data_recursive(targrt_data, self.head)

    def search_data(self, target_position):
        current = self.head
        index = 0
        while current:
            if index == target_position:
                return current.data
            current = current.next
            index += 1
        # assert(False) // syntax error
        return None

    def search_data_recursive(self, start_index, target_position, current):
        if (current is not None) and (start_index == target_position):
            return current.data
        if not current:
            return None
        return self.search_data_recursive(start_index + 1, target_position, current.next)

    def search_data_recursive_short(self, target_position):
        return self.search_data_recursive(0, target_position, self.head)

    def search_data_from_last(self, target_position):
        current = self.head
        count = 0
        while current:
            count += 1
            current = current.next
        if count < target_position:
            print("Location is greater than length of LinkedList")
            return None
        current = self.head
        for i in range(0,count-target_position):
            current = current.next
        return current.data

    def search_data_middle(self):
        current = self.head
        count = 0
        while current:
            count += 1
            current = current.next
        current = self.head
        for i in range(0,int(count/2)):
            current = current.next
        return current.data

    def search_data_middle_double_pointer(self):
        current = self.head
        middle = self.head
        count = 0
        while current:
            count += 1
            if count%2 == 0:
                middle = middle.next
            current = current.next
        if middle:
            return middle.data
        else:
            return None

    def search_data_frequency(self, target_data):
        current = self.head
        count = 0
        while current:
            if current.data == target_data:
                count += 1
            current = current.next
        return count

    def search_data_frequency_recursive(self, target_data, current):
        if current and current.data == target_data:
            return 1 + self.search_data_frequency_recursive(target_data, current.next)
        if not current:
            return 0
        return self.search_data_frequency_recursive(target_data, current.next)

    def search_data_frequency_recursive_short(self,target_data):
        return self.search_data_frequency_recursive(target_data, self.head)

    def print_list(self):
        current = self.head
        while current:
            print(current.data, end=" ")
            current = current.next
        print()

linkedlist = LinkedList()

for i in range(0, 10):
    linkedlist.push(i)
linkedlist.print_list()

# check data
print(linkedlist.cheak_data(4))
print(linkedlist.cheak_data(10))

print()

# check data with recursive
print(linkedlist.cheak_data_recursive(4, linkedlist.head))
print(linkedlist.cheak_data_recursive_short(4))

print(linkedlist.cheak_data_recursive(10, linkedlist.head))
print(linkedlist.cheak_data_recursive_short(10))

print()

# search data
print(linkedlist.search_data(3))
print(linkedlist.search_data(10))

print()

# search data with recursive
print(linkedlist.search_data_recursive(0, 4, linkedlist.head))
print(linkedlist.search_data_recursive_short(4))

print(linkedlist.search_data_recursive(0, 10, linkedlist.head))
print(linkedlist.search_data_recursive_short(10))

print()

# search data from last node
print(linkedlist.search_data_from_last(4))
print(linkedlist.search_data_from_last(9))
print(linkedlist.search_data_from_last(10))
print(linkedlist.search_data_from_last(11))

print()

# search data middle
linkedlist.print_list()
print(linkedlist.search_data_middle())
print(linkedlist.search_data_middle_double_pointer())
linkedlist.push(10)
print()
linkedlist.print_list()
print(linkedlist.search_data_middle())
print(linkedlist.search_data_middle_double_pointer())

print()

# search frequency of data
linkedlist.push(8)
linkedlist.push(8)
linkedlist.push(4)
print(linkedlist.search_data_frequency(0))
print(linkedlist.search_data_frequency(1))
print(linkedlist.search_data_frequency(4))
print(linkedlist.search_data_frequency(8))

print()

# search frequency of data
print(linkedlist.search_data_frequency_recursive_short(0))
print(linkedlist.search_data_frequency_recursive_short(1))
print(linkedlist.search_data_frequency_recursive_short(4))
print(linkedlist.search_data_frequency_recursive_short(8))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 16 00:00:00 2021
@author: Fred Chun

Version as of 23 of May 2023
@editor: Nat Espinosa
"""

import argparse
from ast import arg
import numpy as np
import pandas as pd
from plotly import graph_objs as go
# import plotly.io as pio

# pio.renderers.default = "pdf"

def main():

    # See details at
    # http://www.aspirespace.org.uk/downloads/Rocket%20vehicle%20loads%20and%20airframe%20design.pdf

    # Rocket's documentation
    parser = argparse.ArgumentParser()
    parser.add_argument('--Rocket_info', type=str, help='input desired rockets main information file', required=True)
    parser.add_argument('--Rocket_COP', type=str, help='input desired rockets COP file', required=True)
    parser.add_argument('--Rocket_CL', type=str, help='input desired rockets lift coefficient file', required=True)
    parser.add_argument('--Open_Rocket', type=str, help='input desired rockets OpenRocket information file', required=True)
    args = parser.parse_args()

    rocket_info_df = pd.read_csv(args.Rocket_info, sep=",", header=None, skiprows=1, names=["Length", "Reference Area", "Rocket_part", "mass_m", "x_cm"])
    rocket_info_df["Length"] = pd.to_numeric(rocket_info_df["Length"], errors='coerce')
    rocket_info_df["Reference Area"] = pd.to_numeric(rocket_info_df["Reference Area"], errors='coerce')

    # Length [m] Note: please double check that the information is in [cm]
    length_l = rocket_info_df.iloc[0, 0]

    # Reference area
    s = np.pi * ((rocket_info_df.iloc[0, 1]/2)**2)

    # Rocket Parts
    rocket_parts_df = rocket_info_df.iloc[:, [2, 3, 4]]

    # AeroLab data
    aerolab_x_cp_df = pd.read_csv(args.Rocket_COP, sep="\s+", header=None, skiprows=1)
    aerolab_cl_alpha_df = pd.read_csv(args.Rocket_CL, sep="\s+", header=None, skiprows=1)

    # Max q data       --> manually change <--
    open_rocket_df = pd.read_csv(args.Open_Rocket, sep=',', header=None, skiprows=1)

    rho_inf = open_rocket_df.iloc[0,0] # density [kg m^-3]
    velocity_v_inf = open_rocket_df.iloc[0,1] # velocity [m s^-1]
    velocity_v_wind_gust = open_rocket_df.iloc[0,2] # wind gust velocity [m s^-1]

    angle_of_attack_alpha = np.arctan(velocity_v_wind_gust / velocity_v_inf)  # [rad]

    # Lift coefficient curve slopes [rad^-1]
    nose_body_cl_alpha = aerolab_cl_alpha_df.iloc[-1, 2]
    wing_cl_alpha = aerolab_cl_alpha_df.iloc[-1, 3]
    body_wing_cl_alpha = aerolab_cl_alpha_df.iloc[-1, 4]

    # Lift coefficients
    nose_body_cl = nose_body_cl_alpha * angle_of_attack_alpha
    wing_cl = wing_cl_alpha * angle_of_attack_alpha
    body_wing_cl = body_wing_cl_alpha * angle_of_attack_alpha

    # Lifts [N]
    nose_body_lift_l = 0.5 * rho_inf * (velocity_v_inf ** 2) * s * nose_body_cl
    wing_lift_l = 0.5 * rho_inf * (velocity_v_inf ** 2) * s * wing_cl
    body_wing_lift_l = 0.5 * rho_inf * (velocity_v_inf ** 2) * s * body_wing_cl

    rocket_lift_l = nose_body_lift_l + wing_lift_l + body_wing_lift_l

    # Centres of pressure [m]
    nose_body_x_cp = aerolab_x_cp_df.iloc[-1, 2] / 1000
    wing_x_cp = aerolab_x_cp_df.iloc[-1, 3] / 1000
    body_wing_x_cp = aerolab_x_cp_df.iloc[-1, 4] / 1000

    # Masses [kg]
    rocket_parts_mass_m = rocket_parts_df["mass_m"]

    rocket_mass_m = sum(rocket_parts_mass_m)

    # Centres of mass [m]
    rocket_parts_x_cm = rocket_parts_df["x_cm"]

    rocket_x_cm = sum(rocket_parts_mass_m * rocket_parts_x_cm) / sum(
        rocket_parts_mass_m)

    # Moments of inertia [kg m^2]
    rocket_parts_i = rocket_parts_mass_m * (
            rocket_parts_x_cm - rocket_x_cm) ** 2

    rocket_i = sum(rocket_parts_i)

    # Moments [N m]
    nose_body_moment_m = nose_body_lift_l * (nose_body_x_cp - rocket_x_cm)
    wing_moment_m = wing_lift_l * (wing_x_cp - rocket_x_cm)
    body_wing_moment_m = body_wing_lift_l * (body_wing_x_cp - rocket_x_cm)

    rocket_moment_m = nose_body_moment_m + wing_moment_m + body_wing_moment_m

    # Angular acceleration [rad s^-2]
    angular_acceleration_alpha = rocket_moment_m / rocket_i

    # Accelerations [m s^-2]
    rocket_parts_a = (rocket_lift_l / rocket_mass_m) + (
            angular_acceleration_alpha * (rocket_parts_x_cm - rocket_x_cm))

    # Point loads [N]
    rocket_parts_p = -(rocket_parts_mass_m * rocket_parts_a)

    # Load diagram
    loads_d = {
        "x": np.array(
            rocket_parts_x_cm.tolist() + [nose_body_x_cp, wing_x_cp,
                                          body_wing_x_cp] + [0, length_l]),
        "p": np.array(
            rocket_parts_p.tolist() + [nose_body_lift_l, wing_lift_l,
                                       body_wing_lift_l] + [0, 0]),
    }
    loads_df = pd.DataFrame(data=loads_d).sort_values(by=["x"])

    loads_x = loads_df["x"]  # [m]
    loads_p = loads_df["p"]  # [N]

    fig = go.Figure()
    fig.add_trace(go.Bar(x=loads_x, y=loads_p))
    fig.update_layout(xaxis_range=[0, length_l], xaxis_title="Position, x [m]",
                      yaxis_title="Load, P [N]")
    fig.update_traces(marker=dict(line=dict(width=10, color='blue')))
    fig.show()

    # Shear diagram
    shears_d = {
        "x": np.repeat(np.copy(loads_x), 2)[1:-1],
        "shear_v": np.repeat(np.add.accumulate(np.copy(loads_p)), 2)[:-2],
    }
    shears_df = pd.DataFrame(data=shears_d)

    shears_x = shears_df["x"]  # [m]
    shears_shear_v = shears_df["shear_v"]  # [N]

    fig = go.Figure()
    fig.add_trace(go.Scatter(x=shears_x, y=shears_shear_v))
    fig.update_layout(xaxis_range=[0, length_l], xaxis_title="Position, x [m]",
                      yaxis_title="Shear, V [N]")
    fig.show()

    # Moment diagram
    moments_d = {
        "x": np.copy(loads_x),
        "moment_m": np.concatenate((np.zeros(1), np.add.accumulate(
            np.concatenate(
                (np.diff(np.copy(loads_x)), np.zeros(1))) * np.add.accumulate(
                np.copy(loads_p)))))[:-1],
    }
    moments_df = pd.DataFrame(data=moments_d)

    moments_x = moments_df["x"]  # [m]
    moments_moment_m = moments_df["moment_m"]  # [N m]

    fig = go.Figure()
    fig.add_trace(go.Scatter(x=moments_x, y=moments_moment_m))
    fig.update_layout(xaxis_range=[0, length_l], xaxis_title="Position, x [m]",
                      yaxis_title="Moment, M [N m]")
    fig.show()

    return True

if __name__ == '__main__':
    main()


import os
import glob
from shutil import copy2
from PIL import Image
import json
import numpy as np
import argparse
import json
from publiccode.helpers.annotation import Annotation
from publiccode.helpers.anue_labels import labels, name2label
from collections import namedtuple
Rectangle = namedtuple('Rectangle', 'ymin xmin ymax xmax')

ra = Rectangle(3., 3., 5., 5.)
rb = Rectangle(1., 1., 4., 3.5)

def check_bbox_occlusion(bbox1, bbox2):

    def x_touch(bbox1, bbox2):
        return bbox1[2] == bbox2[0] and ((bbox1[1] <= bbox2[1] <= bbox1[3]) or (bbox1[1] <= bbox2[3] <= bbox1[3]))

    def y_touch(bbox1, bbox2):
        return bbox1[1] == bbox2[3] and ((bbox1[0] <= bbox2[0] <= bbox1[2]) or (bbox1[0] <= bbox2[2] <= bbox1[2]))

    def touch(bbox1, bbox2):
        return x_touch(bbox1, bbox2) or x_touch(bbox2, bbox1) or y_touch(bbox1, bbox2) or y_touch(bbox2, bbox1)

    def overlap(a, b):  # returns None if rectangles don't intersect
        height = float(a[2] - a[0] + 1)
        width = float(a[3] - a[1] + 1)
        a = Rectangle(a[0],a[1], a[2], a[3])
        b = Rectangle(b[0], b[1], b[2], b[3])
        dx = min(a.xmax, b.xmax) - max(a.xmin, b.xmin)
        dy = min(a.ymax, b.ymax) - max(a.ymin, b.ymin)
        if (dx>=0) and (dy>=0):
            if dx * dy > 0.05 * height * width:
                return True
            else:
                return False
        else:
            return False
    return touch(bbox1, bbox2) or overlap(bbox1, bbox2)

def check_occlusion(json_data, img):
    occlusions = {}
    for obj_id in json_data:
        occlusion = list((map(lambda x: json_data[x]["global_id"] > json_data[obj_id]["global_id"] and check_bbox_occlusion(json_data[obj_id]["bbox"], json_data[x]["bbox"]), json_data)))
        occlusions[obj_id] = any(occlusion)
        json_data[obj_id]["occlusion"] = any(occlusion)

    return json_data

def generate_json_data_inst(fname, img, src):
    json_name = "_".join(fname.split('_')[:-1]) + "_polygons.json"
    annotation = Annotation()
    annotation.fromJsonFile(json_name)
    nbInstances = {}
    instances_ids = {}
    json_data = {}
    global_id = 0
    for labelTuple in labels:
        if labelTuple.hasInstances:
            nbInstances[labelTuple.name] = 0
    for obj in annotation.objects:
        label   = obj.label
        polygon = obj.polygon

        # If the object is deleted, skip it
        if obj.deleted:
            continue

        # if the label is not known, but ends with a 'group' (e.g. cargroup)
        # try to remove the s and see if that works
        # also we know that this polygon describes a group
        isGroup = False
        if ( not label in name2label ) and label.endswith('group'):
            label = label[:-len('group')]
            isGroup = True

        if not label in name2label:
            print( "Label '{}' not known.".format(label) )
            continue

        # the label tuple
        labelTuple = name2label[label]
        id = labelTuple.train_id
        if labelTuple.hasInstances and not isGroup and id != 255:
            if id not in instances_ids:
                instances_ids[id] = []
            id = id * 1000 + nbInstances[label]
            instances_ids[labelTuple.train_id].append(id)
            nbInstances[label] += 1
            object_id = global_id
            global_id += 1
            ins_map = (np.squeeze(img) == np.squeeze(id)).astype(float)
            ins_area = np.sum(ins_map)
            if ins_area > 0:
                yy, xx = np.where(ins_map > 0)
                height = float(yy.max() - yy.min() + 1)
                width = float(xx.max() - xx.min() + 1)
                box_area = height * width
                area_ratio = float(ins_area) / box_area
                width_ratio = float(512) / width
                height_ratio = float(256) / height
                aspect_ratio = np.max((height / width, width / height))
                json_data[str(id)] = {"class_id": int(labelTuple.train_id),
                                      "global_id": int(object_id),
                                      "train_id": int(labelTuple.train_id),
                                      "area": float(ins_area),
                                      "height": height,
                                      "width": width,
                                      "box_area": box_area,
                                      "area_ratio": area_ratio,
                                      "width_ratio": width_ratio,
                                      "height_ratio": height_ratio,
                                      "aspect_ratio": float(aspect_ratio),
                                      "bbox": [float(yy.min()), float(xx.min()), float(yy.max()), float(xx.max())]
                                      }
    json_data = check_occlusion(json_data, img)
    return json_data

def copy_file(src, src_ext, dst, type):
    # find all files ends up with ext
    flist = sorted(glob.glob(os.path.join(src, '*', src_ext)))
    for fname in flist:
        src_path = fname
        img = Image.open(src_path)
        if img.size != (1920, 1080):
            continue
        size = 512, 512
        if type == "img":
            img = img.resize((512,288), Image.BICUBIC)
            img = img.crop((0,0,512,256))
        elif type in ["inst", "label"]:
            img = img.resize((512,288), Image.NEAREST)
            img = img.crop((0, 0, 512, 256))
            if type == "inst":
                json_data = generate_json_data_inst(fname, img, src)
                json.dump(json_data, open(os.path.join(dst,"_".join(src_path.replace("_instanceIds.png", "").split("/")[-2:])  + "_data.json"),
                                          "w"), indent=4)
        dst_path = ".".join([".".join(os.path.join(dst,"_".join(fname.split("/")[-2:])).split(".")[:-1]), "png"])
        img.save(dst_path)
        print('copied %s to %s' % (src_path, os.path.join(dst,"_".join(fname.split("/")[-2:]))))

# organize image
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--dataroot', type=str,
                        default='../datasets')

    opt = parser.parse_args()
    folder_name = opt.dataroot
    train_img_dst = os.path.join(folder_name, 'train_img')
    train_label_dst = os.path.join(folder_name, 'train_label')
    train_inst_dst = os.path.join(folder_name, 'train_inst')
    train_bbox_dst = os.path.join(folder_name, 'train_bbox')
    val_img_dst = os.path.join(folder_name, 'val_img')
    val_label_dst = os.path.join(folder_name, 'val_label')
    val_inst_dst = os.path.join(folder_name, 'val_inst')
    val_bbox_dst = os.path.join(folder_name, 'val_bbox')
    if not os.path.exists(train_img_dst):
        os.makedirs(train_img_dst)
    if not os.path.exists(train_label_dst):
        os.makedirs(train_label_dst)
    if not os.path.exists(train_inst_dst):
        os.makedirs(train_inst_dst)
    if not os.path.exists(val_img_dst):
        os.makedirs(val_img_dst)
    if not os.path.exists(val_label_dst):
        os.makedirs(val_label_dst)
    if not os.path.exists(val_inst_dst):
        os.makedirs(val_inst_dst)
    # train_image
    copy_file(os.path.join(opt.dataroot,'leftImg8bit/train'),
              '*_leftImg8bit.*', train_img_dst,"img")
    # train_label
    copy_file(os.path.join(opt.dataroot,'gtFine/train'),
              '*_trainIds.*', train_label_dst, "label")
    # train_inst
    copy_file(os.path.join(opt.dataroot,'gtFine/train'),
            '*_instanceIds.*', train_inst_dst, "inst")
    # val_image
    copy_file(os.path.join(opt.dataroot,'leftImg8bit/val'),
            '*_leftImg8bit.*', val_img_dst, "img")
    # val_label
    copy_file(os.path.join(opt.dataroot,'gtFine/val'),
            '*_trainIds.*', val_label_dst, "label")
    # val_inst
    copy_file(os.path.join(opt.dataroot,'gtFine/val'),
            '*_instanceIds.*', val_inst_dst, "inst")


import json
from azure.storage.blob import BlobServiceClient, ContainerClient
import logging
import os

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class SourceProvider:
    def __init__(
        self
    ):
        self.cnxn_string = os.environ['AZURE_STORAGE_CONNECTION_STRING']
        self.source_config_path = os.environ.get(
            'SOURCE_CONFIG_PATH') or 'source.json'
        self.source_config = self.get_source_config()
        self.container = self.get_container_client()

    def get_source_config(self):
        with open(self.source_config_path, 'r') as file:
            return json.loads(file.read())

    def get_container_client(self):
        logger.info(f'Storage connection string: {self.cnxn_string}')

        client = BlobServiceClient.from_connection_string(
            conn_str=self.cnxn_string)
        logger.info(f'Created blob service client')

        container = client.get_container_client(
            container='build-resources')
        logger.info(f'Fetched build-resources blob container')

        return container

    def get_source_target_pair(self, data):
        return (
            data.get('source'),
            data.get('target')
        )

    def get_blob_stream(self, path):
        blob_client = self.container.get_blob_client(path)

        if not blob_client.exists():
            raise Exception(f"No blob exists at path '{path}'")

        return blob_client.download_blob()

    def handle_source(self, data):
        source, target = self.get_source_target_pair(
            data=data)
        logger.info(f'[Source -> Target]: {source} -> {target}')

        if not source:
            raise Exception("A value is required for parameter 'source'")
        if not target:
            raise Exception("A value is required for parameter 'target'")

        logger.info(f"[Source -> Target]: Fetching blob stream @ '{source}'")
        stream = self.get_blob_stream(path=source)

        logger.info(
            f"[Source -> Target]: Writing blob stream to target file buffer")
        with open(target, 'wb') as file:
            stream.readinto(file)
        logger.info(f"[Source -> Target]: Source completed successfully")

    def go(self):
        logger.info(f'[init]: {len(self.source_config)} sources defined')
        for config in self.source_config:
            self.handle_source(data=config)

util = SourceProvider()
util.go()


import json
import datetime

import node

class MapGraph:
    def __init__(self, location):
        """
        :param location: int
        :return: MapGraph
        """
        with open("data/nodes.json") as json_file:
            json_file = json_file.read()
            self.nodes = json.loads(json_file, object_hook=self._decode_json_to_nodes)
        self.nodes = {node.id: node for node in self.nodes}
        self.location_id = location
        self.services = []

    def add_service(self, service):
        """

        :param service: Serivce
        """
        self.services.append(service)

    def create_time_table(self):
        time_table = []
        for srv in self.services:
            if self.location_id in srv.get_path():
                time_arrive = srv.get_time() + self._count_up_weights(srv.get_path())
                time_table.append(srv.get_name() + " " + str(time_arrive.time()))
        return time_table

    def _count_up_weights(self, path):
        len_path = datetime.timedelta(hours = 0)
        for i in range(len(path) - 1):
            if path[i] == self.location_id:
                break
            len_path = len_path + self.nodes[path[i]].get_edge_weight_from_id(path[i+1])
        return len_path

    def _decode_json_to_nodes(self, dct):
        """
        object_hook for json.loads
        :return: Node
        """
        if "__complex__" in dct:
            return node.Node(dct["id"], dct["edges"])
        return dct


import numpy as np
import pandas as pd

import math
from scipy.stats import binom
from multiprocessing import Pool

from scipy.stats import bootstrap

def Count_Prob_EEI(A):
    '''
    Calculate the counts and probabilities of exclusive events

    Parameters
    ---------
    A
        Gene expression matrix (n_cell x n_gene)

    Returns
    -------
    Count_excl
        Count matrix of exclusive events.
        Prob_excl[i, j]: the probability that gene i is expressed
                                          but gene j is not expressed
    Prob_excl
        Probability matrix of exclusive events.
        Count_excl[i, j]: number of occurrence that gene i is expressed
                                                but gene j is not expressed
    '''

    Allcell = A.shape[0]
    Allgene = A.shape[1]

    Count_excl = np.zeros((Allgene, Allgene), dtype=np.int64)
    is_nonzeroMat = (A.values > 0.05)
    is_zeroMat = (A.values <= 0.05)

    for i in range(Allgene):
        Count_excl[i] = np.sum(np.logical_and(is_nonzeroMat[:, [i]],
                                              is_zeroMat), axis=0)
    p_nonzero = np.sum(A.values > 0.05, axis=0) / Allcell
    p_zero = np.sum(A.values <= 0.05, axis=0) / Allcell

    Prob_excl = p_nonzero * p_zero[:, np.newaxis]

    return Count_excl, Prob_excl

def DEEI_sub(ij:list, Count_excl:np.array, Prob_excl:np.array, A_S:np.array,
            A_E:np.array, genes:list, n_cell:int, p_coef:int):
    '''
    Calculate Balanced Mutual exclusivity and Directed exclusively express index (subprocess)

    Parameters
    ---------
    ij
        indices of gene pairs
    Count_excl
        Count matrix of exclusive events.
        Prob_excl[i, j]: the probability that gene i is expressed
                                          but gene j is not expressed
    Prob_excl
        Probability matrix of exclusive events.
        Count_excl[i, j]: number of occurrence that gene i is expressed
                                                but gene j is not expressed
    A_S
        boolean matrix, True if a gene is suppressed in one cell. Shape is (cell, gene).
    A_E
        boolean matrix, True if a gene is expressed in one cell. Shape is (cell, gene).
    genes
        A list of genes
    n_cell
        number of cells
    p_coef
        penalty factor

    Returns
    -------
    DEEI
        Mutual exclusivity statistics for given gene pairs ij
    '''

    DEEI = pd.DataFrame(np.zeros((len(ij), 7), dtype=np.float64),
                        columns=['Gene1', 'Gene2', 'BME', 'DEEI(1Express_2Silence)',
                                 'P(1E_2S)', 'DEEI(1Silence_2Express)', 'P(1S_2E)'])

    for count, ijt in enumerate(ij):

        i, j = ijt

        # BME
        e1 = (A_S.iloc[:, i] & A_E.iloc[:, j]).sum()
        e2 = (A_S.iloc[:, j] & A_E.iloc[:, i]).sum()
        e12sumsqr = e1 ** 2 + e2 ** 2
        e1e2 = e1 * e2
        penalty = (e12sumsqr + 2 * e1e2) / e12sumsqr / 2
        e = (e1 + e2) / n_cell * (penalty ** p_coef)

        DEEI.loc[count, 'BME'] = e

        # DEEI
        x1 = Count_excl[i][j]
        p1 = Prob_excl[i][j]
        prob1 = binom.sf(x1 - 1, n_cell, p1)

        x2 = Count_excl[j][i]
        p2 = Prob_excl[j][i]
        prob2 = binom.sf(x2 - 1, n_cell, p2)

        DEEI.loc[count, 'Gene1'] = genes[i]
        DEEI.loc[count, 'Gene2'] = genes[j]
        DEEI.loc[count, 'Prob(1E_2S)'] = prob1
        DEEI.loc[count, 'Prob(1S_2E)'] = prob2

        if (prob1 <= 0):
            deei_1e2s = 1e3
        else:
            deei_1e2s = -(math.log10(prob1))
        if (prob2 <= 0):
            deei_1s2e = 1e3
        else:
            deei_1s2e = -(math.log10(prob2))

        DEEI.loc[count, 'DEEI(1Express_2Silence)'] = deei_1e2s
        DEEI.loc[count, 'DEEI(1Silence_2Express)'] = deei_1s2e

    return DEEI

def calc_DEEI(A, cutoff=0.05, p_coef=1, n_process=16):
    '''
    Calculate Balanced Mutual Exclusivity and Directed exclusively express index

    Parameters
    ---------
    A
        Gene expression matrix (n_cell x n_gene)
    cutoff
        gene expression cutoff that determines if a gene is expressed
    p_coef
        penalty factor for the BME statistic
    n_process
        number of cores to be used

    Returns
    -------
    DEEI
            Mutual exclusivity statistics
    '''

    Count_excl, Prob_excl = Count_Prob_EEI(A)

    A_S = A < cutoff
    A_E = A >= cutoff

    genes = A.columns.tolist()
    n_cell = A.shape[0]
    n_gene = A.shape[1]

    ij = []
    for i in range(0, n_gene):
        for j in range(i + 1, n_gene):
            ij.append([i, j])

    step = int(len(ij) / n_process)
    ijs = [ij[i * step:(i + 1) * step] for i in range(n_process - 1)]
    ijs.append(ij[(n_process - 1) * step:])

    with Pool(n_process) as p:
        DEEI = p.starmap(DEEI_sub, [(ij, Count_excl, Prob_excl,
                                    A_S, A_E, genes, n_cell, p_coef) for ij in ijs])

    DEEI = pd.concat(DEEI).sort_values('BME', ascending=False).reset_index(drop=True)

    return DEEI

def BME_stat(A1, A2, p_coef=1):
    '''
    Calculate Balanced Mutual exclusivity score between Gene 1 and Gene 2

    Parameters
    ---------
    A1
        Expression vector of gene 1
    A2
        Expression vector of gene 2

    Returns
    -------
    e
        Balanced Mutual exclusivity score between Gene 1 and Gene 2
    '''

    e1 = ((A1 < 0.05) & (A2 > 0.05)).sum()
    e2 = ((A2 < 0.05) & (A1 > 0.05)).sum()

    n_cell = len(A1)

    e12sumsqr = e1 ** 2 + e2 ** 2
    e1e2 = e1 * e2
    penalty = (e12sumsqr + 2 * e1e2) / e12sumsqr / 2
    e = (e1 + e2) / n_cell * (penalty ** p_coef)

    return e

def calc_BME_sub(ij: list, A: np.array, genes: list, n_resamples: int):
    '''
    Calculate Balanced Mutual exclusivity (subprocess)

    Parameters
    ---------
    ij
        indices of gene pairs
    A
        gene expression matrix. Shape is (cell, gene).
    genes
        A list of genes. The order of genes matches with A.
    n_resamples
        The number of resamples performed to form the bootstrap distribution of the statistic

    Returns
    -------
    BME_score
        Mutual exclusivity statistics for given gene pairs ij
    '''

    BME_score = pd.DataFrame(np.zeros((len(ij), 7), dtype=np.float64),
                             columns=['Gene1', 'Gene2', 'BME', 'CI_lower',
                                      'CI_upper', 'SE', 'p_value'])
    rng = np.random.default_rng()

    for count, ijt in enumerate(ij):
        i, j = ijt

        # BME
        e = BME_stat(A.iloc[:, i], A.iloc[:, j])

        BME_score.loc[count, 'Gene1'] = genes[i]
        BME_score.loc[count, 'Gene2'] = genes[j]
        BME_score.loc[count, 'BME'] = e

        CI_BME = bootstrap((A.iloc[:, i], A.iloc[:, j]), BME_stat, n_resamples=n_resamples,
                           vectorized=False, paired=True, method='percentile', random_state=rng)
        BME_score.loc[count, 'CI_lower'] = CI_BME.confidence_interval.low
        BME_score.loc[count, 'CI_upper'] = CI_BME.confidence_interval.high
        BME_score.loc[count, 'SE'] = CI_BME.standard_error
        z = e / BME_score.loc[count, 'SE']
        p_value = np.exp(-0.717 * z - 0.416 * (z ** 2))
        BME_score.loc[count, 'p_value'] = p_value

    return BME_score

def calc_BME(A, n_process=16, n_resamples=200):
    '''
    Calculate Balanced Mutual Exclusivity

    Parameters
    ---------
    A
        Gene expression matrix (n_cell x n_gene)
    n_process
        number of cores to be used
    n_resamples
        The number of resamples performed to form the bootstrap distribution of the statistic

    Returns
    -------
    BME
        BME statistics
    '''

    genes = A.columns.tolist()
    n_gene = A.shape[1]

    ij = []
    for i in range(0, n_gene):
        for j in range(i + 1, n_gene):
            ij.append([i, j])

    step = int(len(ij) / n_process)
    ijs = [ij[i * step:(i + 1) * step] for i in range(n_process - 1)]
    ijs.append(ij[(n_process - 1) * step:])

    with Pool(n_process) as p:
        BME = p.starmap(calc_BME_sub, [(ij, A, genes, n_resamples) for ij in ijs])

    BME = pd.concat(BME).sort_values('BME', ascending=False).reset_index(drop=True)

    return BME


from pyscripts.database import CursorFromConnectionFromPool

def get_all_stock(active='y', service='n'):
    with CursorFromConnectionFromPool() as cursor:
        cursor.execute('SELECT * FROM stock_items WHERE active=%s AND service_item=%s ORDER BY item_name ASC',
                       (active, service))
        stock = cursor.fetchall()
        if stock:
            stock_list = []
            for item in stock:
                stock_list.append({'id': item[0], 'item_name': item[1], 'item_cost': item[2], 'labour_charge': item[3],
                                   'retail_price': item[4], 'stock_category_id': item[5], 'stock_qty': item[6],
                                   'order_qty': item[7], 'supplier_id': item[8], 'pending_qty': item[11]})
            return stock_list

def get_one_stock_item(item_id):
    with CursorFromConnectionFromPool() as cursor:
        cursor.execute('SELECT * FROM stock_items WHERE id=%s', (item_id,))
        item = cursor.fetchone()
        if item:
            return [{'id': item[0], 'item_name': item[1], 'item_cost': item[2], 'labour_charge': item[3],
                     'retail_price': item[4], 'stock_category_id': item[5], 'stock_qty': item[6],
                     'order_qty': item[7], 'supplier_id': item[8], 'pending_qty': item[11]}]

def retrieve_supplier_names():
    """Returns all suppliers."""
    with CursorFromConnectionFromPool() as cursor:
        cursor.execute('SELECT * FROM suppliers')
        suppliers = cursor.fetchall()
        if suppliers:
            better_suppliers = []
            for supplier in suppliers:
                better_suppliers.append({'id': supplier[0], 'name': supplier[1]})
            return better_suppliers

def retrieve_category_names():
    with CursorFromConnectionFromPool() as cursor:
        cursor.execute('SELECT * FROM stock_category')
        categories = cursor.fetchall()
        if categories:
            category_list = []
            for category in categories:
                category_list.append({'category_id': category[0], 'category_name': category[1]})
            return category_list

def toggle_item(active, item_id):
    with CursorFromConnectionFromPool() as cursor:
        cursor.execute('UPDATE stock_items SET active=%s WHERE id=%s', (active, item_id))

def update_item(data):
    with CursorFromConnectionFromPool() as cursor:
        cursor.execute('UPDATE stock_items SET item_name=%s, labour_charge=%s, retail_price=%s WHERE id=%s',
                       (data['item_name'], data['labour_charge'], data['rrp'], data['item_id']))


import time
import board
import keypad

from keyboard_send import press_keys, release_keys, send_keys, release_all_keys
from configs import left_mat_pos_to_key, right_mat_pos_to_key, lin_key

mat_pos_to_key = left_mat_pos_to_key

time.sleep(1)
KEY_PINS = (board.GP12, board.GP13, board.GP14, board.GP15, board.GP16, board.GP17)
linear_keys = keypad.Keys(KEY_PINS, value_when_pressed=False, pull=True)
mat_keys = keypad.KeyMatrix(
    row_pins=(board.GP1, board.GP2, board.GP3, board.GP4, board.GP5),
    column_pins=(board.GP6, board.GP7, board.GP8, board.GP9, board.GP10, board.GP11),
)

fn = 0
while True:
    linear_event = linear_keys.events.get()
    mat_event = mat_keys.events.get()

    if linear_event:
        if linear_event.pressed:
            if linear_event.key_number == 0:
                fn = 1
            elif linear_event.key_number == 5:
                fn = 2
            else:
                press_keys(lin_key[linear_event.key_number])
        if linear_event.released:
            if linear_event.key_number == 0 or linear_event.key_number == 5:
                fn = 0
                release_all_keys()
            else:
                release_keys(lin_key[linear_event.key_number])

    if mat_event:
        if mat_event.pressed:
            press_keys(mat_pos_to_key[fn][mat_event.key_number])

        if mat_event.released:
            release_keys(mat_pos_to_key[fn][mat_event.key_number])

USB Version circuitpython keypad/code.py


##########################################################################################
# This script defines the pipelines for post processing of scraped items. Item Pipelines are
# a standard component of Scrapy implementation. After an item has been scraped by a spider,
# it is sent through this Item Pipeline for processing through several components, executed
# sequentially.
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
##########################################################################################

import copy
from typing import Union
from itemadapter import ItemAdapter
from datetime import datetime
import os
from pathlib import Path
import json
from jsonschema.exceptions import ValidationError

import scrapy
from scrapy.pipelines.media import MediaPipeline
from scrapy.exceptions import DropItem

from dataPipelines.gc_scrapy.gc_scrapy.utils import unzip_docs_as_needed
from .validators import DefaultOutputSchemaValidator, SchemaValidator
from . import OUTPUT_FOLDER_NAME
from .utils import dict_to_sha256_hex_digest, get_fqdn_from_web_url

SUPPORTED_FILE_EXTENSIONS = [
    "pdf",
    "html",
    "txt",
    "zip",
] # File types the item pipeline supports

class FileDownloadPipeline(MediaPipeline):
    def __init__(self, download_func=None, settings=None):
        settings = dict(settings) if settings else {}
        settings.setdefault("MEDIA_ALLOW_REDIRECTS", True)
        super().__init__(download_func, settings)

    previous_hashes = set()
    output_dir: Path
    previous_manifest_path: Path
    job_manifest_path: Path
    dont_filter_previous_hashes: bool

    def open_spider(self, spider):
        super().open_spider(spider)
        print("++ Initiating downloader for", spider.name)

        self.output_dir = Path(spider.download_output_dir).resolve()
        self.job_manifest_path = Path(self.output_dir, "manifest.json").resolve()

        self.previous_manifest_path = Path(spider.previous_manifest_location).resolve()

        if not spider.dont_filter_previous_hashes:
            self.load_hashes_from_cumulative_manifest(self.previous_manifest_path, spider.name)

    def load_hashes_from_cumulative_manifest(self, previous_manifest_path, spider_name):
        file_location = Path(previous_manifest_path).resolve() if previous_manifest_path else None

        if not file_location or not os.path.isfile(file_location):
            print(f"\n\nPrevious manifest at {file_location} is not a file! Nothing will be filtered!\n\n")
            if self.dont_filter_previous_hashes:
                return
            else:
                exit(1)

        print("Reading in previous manifest")
        count = 0
        with file_location.open(mode="r") as f:
            for line in f.readlines():
                count += 1
                if count % 1000 == 0:
                    print(f"{count} lines read in")
                if not line.strip():
                    continue

                jdoc = json.loads(line)
                crawler_used = jdoc.get("crawler_used")
                # covers old manifest items with no crawler info
                if not crawler_used:
                    self.previous_hashes.add(jdoc["version_hash"])
                # skips adding hashes for other spiders for combined manifest files with that info
                elif crawler_used == spider_name:
                    self.previous_hashes.add(jdoc["version_hash"])

        num_hashes = len(self.previous_hashes)
        print(f"Previous manifest loaded, will filter {num_hashes} hashes")

    @staticmethod
    def create_items_from_nested_zip(zipped_item_paths, item):
        for sub_path in zipped_item_paths:
            new_item = copy.deepcopy(item)
            new_item["doc_name"] = sub_path.stem
            if item['crawler_used'] == "far_subpart_regs":
                new_item["doc_title"] = sub_path.stem
            else:
                new_item["doc_title"] = sub_path.stem.split("-", 1)[1].strip()
            new_item["version_hash_raw_data"]["doc_name"] = new_item["doc_name"]
            new_item["version_hash_raw_data"]["sub_file_version_hash"] = dict_to_sha256_hex_digest(
                new_item["version_hash_raw_data"]
            )
            yield new_item

    @staticmethod
    def get_first_supported_downloadable_item(downloadable_items: list) -> Union[dict, None]:
        """Get first supported downloadable item corresponding to doc, has correct type and is not cac blocked"""
        return next((item for item in downloadable_items if item["doc_type"] in SUPPORTED_FILE_EXTENSIONS), None)

    def get_media_requests(self, item, info):
        """Called per DocItem from spider output, yields the media requests to download, response sent to media_downloaded"""

        # info = SpiderInfo
        # class SpiderInfo:
        # self.spider = spider
        # self.downloading = set()
        # self.downloaded = {}
        # self.waiting = defaultdict(list)

        doc_name = item["doc_name"]
        if item["version_hash"] in self.previous_hashes:
            # dont download anything just send item to crawl output
            print(f"Skipping download of {item.get('doc_name')} because it was in previous_hashes")
            info.spider.increment_in_previous_hashes()
            return item

        if item["cac_login_required"]:
            print(f"Skipping download of {item.get('doc_name')} because it requires cac login")
            info.spider.increment_required_cac()
            return item

        # currently we only associate 1 file with each doc, this gets the first we know how to parse
        file_item = self.get_first_supported_downloadable_item(item["downloadable_items"])

        if file_item:
            url = file_item["download_url"]
            extension = file_item["doc_type"]
            output_file_name = f"{doc_name}.{extension}"

            meta = {
                "output_file_name": output_file_name,
                "doc_type": file_item["doc_type"],
                "compression_type": file_item["compression_type"],
            }

            try:
                if info.spider.download_request_headers:
                    yield scrapy.Request(url, headers=info.spider.download_request_headers, meta=meta)
                else:
                    yield scrapy.Request(url, meta=meta)
            except Exception as probably_url_error:
                print("~~~~ REQUEST ERR", probably_url_error)
        else:
            print(f"No supported downloadable item for {item['doc_name']}")
            return item

    def media_downloaded(self, response, request, info):
        """Called for each completed response from get_media_requests, returned to item_completed"""
        # I dont know why this isnt being handled automatically here
        # Just filtering by response code
        if 200 <= response.status < 300:
            return (True, response, None)
        elif not len(response.body):
            return (False, response, "Response has empty body")
        else:
            return (False, response, None)

    def media_failed(self, failure, request, info):
        # I have never seen this called
        print("**** MEDIA FAILED")
        print(failure)
        print(info.spider)
        return (False, failure, "Pipeline Media Request Failed")

    def add_to_dead_queue(self, item, reason):
        path = Path(self.output_dir, "dead_queue.json").resolve() if self.output_dir else None
        if isinstance(reason, int):
            reason_text = f"HTTP Response Code {reason}"
        elif isinstance(reason, str):
            reason_text = reason
        else:
            reason_text = "Unknown failure"

        with open(path, "a+") as f:
            dead_dict = {"document": dict(item), "failure_reason": reason_text}
            try:
                f.write(json.dumps(dead_dict))
                f.write("\n")

            except Exception as e:
                print("Failed to write to dead_queue file", path, e)

    def add_to_manifest(self, item):
        path = self.job_manifest_path
        with open(self.job_manifest_path, "a") as f:
            try:
                f.write(
                    json.dumps(
                        {
                            "version_hash": item["version_hash"],
                            "doc_name": item["doc_name"],
                            "crawler_used": item["crawler_used"],
                            "access_timestamp": item["access_timestamp"],
                        }
                    )
                )
                f.write("\n")

            except Exception as e:
                print("Failed to write to manifest file", path, e)

    def item_completed(self, results, item, info):
        """The function is called for each item after all media requests have been processed"""

        if not info.downloaded:
            return item # return item for crawler output if download was skipped

        ### first in results is supposed to be 'ok' status but it always returns true b/c 404 doesnt cause failure for some reason :(
        ### so added to the media_downloaded function as a sub-tuple in return
        file_downloads = []
        unzipped_items = []
        for (_, (okay, response, reason)) in results: # Loop over results of requests made during crawling
            if not okay:
                self.add_to_dead_queue(item, reason if reason else int(response.status))
            else:
                # Get values from metadata:
                output_file_name = response.meta["output_file_name"] # Assigned to metadata above in get_media_requests function
                doc_type = response.meta["doc_type"]
                compression_type = response.meta["compression_type"]
                # Build a path to each file associated with an item:
                if compression_type:
                    file_download_path = Path(self.output_dir, output_file_name).with_suffix(f".{compression_type}") # Path for downloaded zipped file
                    file_unzipped_path = Path(self.output_dir, output_file_name)
                    # Path for unzipped files
                    metadata_download_path = f"{file_unzipped_path}.metadata" # Path for the accompanying metadata file
                else:
                    # If it is a jbook crawler (and needs a different file output style)
                    if 'rdte;' in output_file_name or 'procurement;' in output_file_name:
                        jbook_output_file_path = output_file_name.replace(';', '/')
                        # self.output_dir is set when the crawler is crawled and is the high level directory information
                        # Should point to bronze/jbook/pdfs instead of bronze/gamechanger/pdf
                        # jbook_output_file_path is type/year/filename
                        file_download_path = Path(self.output_dir, jbook_output_file_path)
                    else:
                        file_download_path = Path(self.output_dir, output_file_name)  # Path for downloaded file
                    metadata_download_path = f"{file_download_path}.metadata"  # Path for the accompanying metadata file

                with open(file_download_path, "wb") as f: # Download each file to it's download path
                    try:
                        to_write = info.spider.download_response_handler(response)
                        f.write(to_write)
                        f.close()
                    except Exception as e:
                        print("Failed to write file to", file_download_path, "Error:", e)
                        return item

                if compression_type:
                    if compression_type.lower() == "zip":
                        unzipped_files = unzip_docs_as_needed(file_download_path, file_unzipped_path, doc_type) # Unzip downloaded zip documents

                        if unzipped_files: # If files have been unzipped...
                            for unzipped_item in self.create_items_from_nested_zip(unzipped_files, item): # Create new DocItem for each unzipped file
                                self.add_to_manifest(unzipped_item)

                                metadata_download_path = Path(self.output_dir, unzipped_item["doc_name"])
                                suffix_doc_type = f'{unzipped_item["downloadable_items"][0]["doc_type"]}'

                                # when making metadata_download_path, need to add the previous suffix in case there are
                                # periods in filename. will mess up metadata names otherwise
                                metadata_download_path = metadata_download_path.with_suffix(metadata_download_path.suffix + f'.{suffix_doc_type}.metadata')

                                with open(metadata_download_path, "w") as f: # Write the metadata for each unzipped file
                                    try:
                                        f.write(json.dumps(dict(unzipped_item)))

                                    except Exception as e:
                                        print("Failed to write metadata", metadata_download_path, e)

                                unzipped_items.append(unzipped_item)
                else: # If original download is not a compressed file...
                    with open(metadata_download_path, "w") as f: # Write the metadata for each file
                        try:
                            f.write(json.dumps(dict(item)))
                        except Exception as e:
                            print("Failed to write metadata", file_download_path, e)

                file_downloads.append(file_download_path)

        if file_downloads: # If file was downloaded, add to manifest
            self.add_to_manifest(item)

        if len(unzipped_items) > 1: # If there were unzipped files, return each as item in list 'unzipped_items'
            return unzipped_items

        return item # Return item to crawl output (if no unzipped files)

class DeduplicaterPipeline:
    def __init__(self):
        self.ids_seen = set()

    def process_item(self, item, spider):
        if not item["doc_name"]:
            raise DropItem("No doc_name")

        elif item["doc_name"] in self.ids_seen:
            raise DropItem("Duplicate doc_name found")

        else:
            self.ids_seen.add(item["doc_name"])

        return item

class AdditionalFieldsPipeline:
    def process_item(self, item, spider):

        if getattr(spider, "display_org", None): # If DocItem.display_org= None, propogate value with value of spider class variable display_org
            item["display_org"] = spider.display_org

        if getattr(spider, "data_source", None): # If DocItem.data_source = None, propogate value with value of spider class variable data_source
            item["data_source"] = spider.data_source

        if getattr(spider, "source_title", None): # If DocItem.source_title = None, propogate value with value of spider class variable source_title
            item["source_title"] = spider.source_title

        if getattr(spider, "display_source", None):
            item["display_source"] = spider.display_source

        if not item.get("crawler_used"):
            item["crawler_used"] = spider.name

        source_page_url = item.get("source_page_url")
        if not source_page_url:
            if getattr(spider, "source_page_url", None):
                item["source_page_url"] = spider.source_page_url
            else:
                source_page_url = spider.start_urls[0]
                item["source_page_url"] = source_page_url

        if not item.get("source_fqdn"):
            item["source_fqdn"] = get_fqdn_from_web_url(source_page_url)

 #       if not item.get("version_hash"):
            # ensure doc_name is part of hash
            # item["version_hash_raw_data"]["doc_name"] = item["doc_name"]
            # item["version_hash"] = dict_to_sha256_hex_digest(item["version_hash_raw_data"])

        if not item.get("access_timestamp"):
            item["access_timestamp"] = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") # T added as delimiter between date and time

        if not item.get("publication_date"):
            item["publication_date"] = None

        if not item.get("cac_login_required"):
            item["cac_login_required"] = getattr(spider, "cac_login_required", False)

        if not item.get("doc_type"):
            item["doc_type"] = getattr(spider, "doc_type", None)

        if not item.get("doc_num"):
            item["doc_num"] = None

        # if item.get("is_revoked") is not None:
           # ensure is_revoked is part of hash
           # item["version_hash_raw_data"]["is_revoked"] = item["is_revoked"]

        return item

class ValidateJsonPipeline:
    """Validates json as Scrapy passes each item to be validated to self.process_item
    :param validator: output validator"""

    def __init__(self, validator: SchemaValidator = DefaultOutputSchemaValidator()):

        if not isinstance(validator, SchemaValidator):
            raise TypeError("arg: validator must be of type SchemaValidator")

        self.validator = validator

    def process_item(self, item, spider):
        item_dict = ItemAdapter(item).asdict()
        name = item_dict.get("doc_name", str(item_dict))

        try:
            self.validator.validate_dict(item_dict)
            return item
        except ValidationError as ve:
            raise DropItem(f"Dropped Item: {name} failed validation: {ve}")

class JsonWriterPipeline(object):
    def open_spider(self, spider):
        if not os.path.exists(OUTPUT_FOLDER_NAME):
            os.makedirs(OUTPUT_FOLDER_NAME)
        json_name = "./" + OUTPUT_FOLDER_NAME + "/" + spider.name + ".json"

        self.file = open(json_name, "w")
        # Your scraped items will be saved in the file 'scraped_items.json'.
        # You can change the filename to whatever you want.

    def close_spider(self, spider):
        self.file.close()

    def process_item(self, item, spider):
        doc = item["document"]

        validator = DefaultOutputSchemaValidator()
        validator.validate(doc)
        self.file.write(doc + "\n")
        return doc

class FileNameFixerPipeline:
    def process_item(self, item, spider):
        if not item["doc_name"]:
            raise DropItem("No doc_name")

        # limit length for OS filename limitations, replace / for filename dir confusion
        item["doc_name"] = item["doc_name"].replace("/", "_")[0:235]
        return item


from django.conf.urls.defaults import *

from django.contrib import admin
from amortization.task.views import *
from amortization.views import main
from amortization.account.views import list_users, user_info
from amortization.account.views import login, logout

admin.autodiscover()

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
    (r'^$', main),
    (r'^user/(\S{3}.*)/$', list_users),
    (r'^user_info/(\S{3}.*)/$', user_info),
    (r'^my_requests/$', user_requests),
    (r'^all_requests/$', requests),
    (r'^closed_requests/$', ended_requests),
    (r'^deleted/$', deleted),
    (r'^delete_request/$', delete_request),
    (r'^all_tasks/$', tasks),
    (r'^closed_tasks/$', closed_tasks),
    (r'^task_status/$', task_status),
    (r'^task/(\d+)/', task),
    (r'^process_requests/$', request_actions),
    (r'^process_tasks/$', tasks_actions),
    (r'^get_url/request/$', get_resultdoc_url),
    (r'^get_url/task/$', get_actdoc_url),
    (r'^user_requests/(\d+)/$', user_requests_admin),
    (r'^comments/(\d+)/$', comments),

    # ACCOUNTS
    (r'^accounts/login/$', login),
    (r'^accounts/logout/$', logout),

    # HTML PRINT
    (r'print/task/(\d+)/', print_task),
)