import time
import color
import numpy as np
import math

class FadeMarquisEffect():
    def __init__(self,velocity,pixels,n_pixels,args):
        self.flashing = False
        self.color1 = (155,255,155)
        self.color2 = (0,0,0)
        self.delay = 0.001
        self.isOn = False
        self.isClosed = False
        self.pixels = pixels
        self.n_pixels = n_pixels
        self.start = time.time()
        self.total_seconds = 4.
        self.period_seconds = .005
        self.separation = 5

        if "color1" in args:
            self.color1 = args["color1"]
        if "color2" in args:
            self.color2 = args["color2"]
        if "transition_time" in args:
            self.total_seconds = args["transition_time"]
        if "period" in args:
            self.period_seconds = args["period"]
        if "n_period" in args:
            self.period_seconds = self.total_second / args["n_period"]
        if "separation" in args:
            self.separation = args["separation"]

        self.time_0_pixels = np.arange(self.n_pixels/(self.separation + 1))*(self.separation+1)

    def beat(self):
        elapsed = time.time() - self.start # Need Paul
        # print("%f seconds" % elapsed)
        if self.isClosed:
           return
        if elapsed >= self.total_seconds:
            self.pixels.fill((0,0,0))
            self.pixels.show()
            return
        out_color = color.rescale(
                self.color1,
                self.color2,
                1-elapsed/self.total_seconds
                )
        for j in ((self.time_0_pixels + math.floor(elapsed / self.period_seconds)) % self.n_pixels):
            self.pixels[int(j)] = out_color
        self.pixels.show()
        for j in ((self.time_0_pixels + math.floor(elapsed / self.period_seconds)) % self.n_pixels):
            self.pixels[int(j)-1] = (0,0,0)
        self.pixels.show()
        time.sleep(self.delay)

    def close(self):
        self.isOn = False
        self.isClosed = True
        self.pixels.fill(self.color2)
        self.pixels.show()


#Write a function that will take an integer as a parameter, and will create a list of the size passed in as an argument, and will populate the list with a random number between 0-1023 (inclusive). You're going to be using "getrandbits()" method so think about how to arrive at that outcome.
import random

def ranNum (num):
    myList = []
    for i in range (0, num):
        myList.append(random.getrandbits(10))
    return myList

num = 4
print(ranNum(num))

#Problem: Let's say that we want to generate a random number, but we don't want to represent the range by the amount of bits, but explicitly with a range and using decimal number.
#Using ".randrange(x,y)"

#Activity 10.2:
#Similar to the previous problem, create a function that creates a random list with numbers from 20-40 (excliuding 40)

def ranNum2 (num):
    myList = []
    for i in range (0,num):
        ranNumOfMyList = random.randrange(20,40)
        myList.append(ranNumOfMyList)
    return myList

num2 = 5
solution = ranNum2(num2)
print(solution)


int(input())
ls = list(map(int, input().split()))

def swap(lst, f, s):
    tmp = lst[s]
    lst[s] = lst[f]
    lst[f] = tmp
    return lst

def part(lst, l, r):
    i, j = l, r
    m = (lst[l] + lst[r])/2
    while i <= j:
        while lst[i] < m:
            i += 1
        while lst[j] > m:
            j -= 1
        if i >= j:
            break
        lst = swap(lst, i, j)
        i += 1
        j -= 1
    return j

def qsort(lst, l, r):
    if l < r:
        q = part(lst, l, r)
        qsort(lst, l, q)
        qsort(lst, q + 1, r)
    return lst

print(' '.join(map(str, qsort(ls, 0, len(ls)-1))))


import argparse

from components.muc4_tools import get_event_keywords
from components.load_muc4 import load_muc4

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--all-human-names", type=str,
                        default="data/muc34/outputs/all-human-names.txt")
    args = parser.parse_args()
    return args

def main(args):

    _, dev_events, _, tst_events, _ = load_muc4()
    keywords = list(map(get_event_keywords, dev_events)) +\
        list(map(get_event_keywords, tst_events))
    human_names = [
        _name
        for _event_keywords in keywords
        for _name, _category in _event_keywords.items()
        if _category.endswith("NAME")
        # for _subname in _name.split(" ")
    ]
    human_names = list(set(human_names))

    with open(args.all_human_names, 'w') as f:
        f.writelines(
            list(map(lambda x: x + "\n", human_names))
        )

if __name__ == "__main__":
    args = parse_args()
    main(args)


chocBars = 2
people = 3
SQUARES_PER_BAR = 7
wholeBarsEach = chocBars // people
chocSquares = chocBars % people * SQUARES_PER_BAR
pricePerBar = 4
print("Whole chocolates bars each: ", wholeBarsEach)
print("Extra squares each: ", chocSquares // people)
print("Squares left over: ", chocSquares % people)
print("Cost per person: ", round(pricePerBar * chocBars / people, 2))


import pytorch_lightning as pl
from pytorch_lightning.callbacks import (DeviceStatsMonitor, ModelCheckpoint,
                                          LearningRateMonitor)
from pytorch_lightning.callbacks.progress import TQDMProgressBar
from pytorch_lightning import Trainer, loggers as pl_loggers

from configs import *
from utils import *
from models import *
from dataset import *
from pl_modules import *
from augmentations import *

## setting seeds for reproducable results.
pl.seed_everything(hash("setting random seeds") % 2**32 - 1)

## Inserting keypoint in single tube images.
insert_dummy_keypoints(TRAIN_PATH)
insert_dummy_keypoints(TEST_PATH)

#defining loss function
criterion = nn.MSELoss()

## defining the callbacks
tb_logger = pl_loggers.TensorBoardLogger(logdir, name="glue_tube_length_model")

checkpoint_callback = ModelCheckpoint(dirpath=ckptdir,
                                      every_n_epochs=10,
                                      save_top_k=2,
                                      monitor = "val_loss",
                                      filename='{epoch}-{val_loss:.2f}')

progressbar_callback = TQDMProgressBar(refresh_rate= 3)
lr_monitor_callback = LearningRateMonitor()
callbacks = [lr_monitor_callback,progressbar_callback,checkpoint_callback]

## Configuring the trainer for pytorch lightning
if torch.cuda.is_available():
    trainer = Trainer(max_epochs = EPOCHS,
                  num_sanity_val_steps=1,
                  log_every_n_steps = 6,
                  weights_summary='top',
                  gpus = -1,
                  precision = 16,
                  logger=tb_logger,
                  callbacks = callbacks,
                  accumulate_grad_batches=4,
                  auto_lr_find = True,
                  reload_dataloaders_every_n_epochs = True
                  )
else:
    trainer = Trainer(max_epochs = EPOCHS,
                  num_sanity_val_steps=1,
                  log_every_n_steps = 6,
                  weights_summary='top',
                  logger=tb_logger,
                  callbacks = callbacks,
                  auto_lr_find = True,
                  reload_dataloaders_every_n_epochs = True
                  )

network = SimpleKeypointModel() # creating the model object
pl_model = plModel(network, criterion, learning_rate = LR) # creating the PL model wrapper object

base_dataset = TubeLengthDataset(TRAIN_PATH) # creating the base dataset object
test_dataset = TubeLengthDataset(TEST_PATH,transform = test_transforms) #creating the test dataset object
#creating the pl datamodule
pl_datamodule = plDataModule(base_dataset,test_dataset,transforms_dict,val_split = VAL_SPLIT, batch_size = BATCH_SIZE)

#fit the model
trainer.fit(model = pl_model,datamodule= pl_datamodule)

#evaluate the model
val_score = trainer.test(model = pl_model,datamodule= pl_datamodule)
print(val_score)

#save only the weight separately
torch.save(pl_model.network.state_dict(),"./model/tube_length_model.pt")


import os
import json
import collections

from pathlib import Path
from appdirs import AppDirs

from typing import Any, Optional, Iterable, Dict, Tuple

from ..extra.utils import nested_dict_update, load_playlist

class Model:
    def __init__(
        self,
        state_fname: Optional[Path] = None,
        log_fname: Optional[Path] = None
    ) -> None:
        self.adirs = AppDirs('vydia', 'kpj')

        self.STATE_FILE: Path = state_fname \
            or Path(self.adirs.user_data_dir) / 'state.json'
        self.LOG_FILE: Path = log_fname \
            or Path(self.adirs.user_log_dir) / 'log.txt'

        self._ensure_dir(str(self.LOG_FILE))

    def get_playlist_list(self) -> Iterable[str]:
        return sorted(self._load_state().keys())

    def get_playlist_info(self, pid: str) -> Dict[str, str]:
        cur = self._load_state()[pid]
        cur.update({'name': pid})
        return cur

    def delete_playlist_by_name(self, name: str) -> None:
        _state = self._load_state()
        _state.pop(name)
        self._save_state(_state)

    def get_current_video(self, pid: str) -> Optional[Dict[str, str]]:
        _state = self._load_state()
        return _state[pid].get('current', None)

    def add_new_playlist(self, plid: str) -> Optional[Tuple[str, str]]:
        try:
            plugin_name, pl = load_playlist(plid)
        except ValueError:
            print(f'No plugin found for "{plid}"')
            return None

        self.update_state(pl.title, {'id': pl.id, 'episodes': {}})
        return pl.title, plugin_name

    def update_state(
        self,
        pid: str, data: Dict[str, Any]
    ) -> None:
        _state = self._load_state()

        if pid in _state:
            _state[pid] = nested_dict_update(_state[pid], data)
        else:
            _state[pid] = data

        self._save_state(_state)

    def _ensure_dir(self, fname: str) -> None:
        """ Make sure that directory exists
        """
        dir_ = os.path.dirname(fname)
        if not os.path.isdir(dir_):
            os.makedirs(dir_)

    def _load_state(self, fn: Optional[str] = None) -> Dict[Any, Any]:
        """ Load state
        """
        fname = str(fn or self.STATE_FILE)
        self._ensure_dir(fname)

        if not os.path.isfile(fname):
            res: Dict[Any, Any] = {}
        else:
            try:
                with open(fname) as fd:
                    res = json.load(fd)
            except json.decoder.JSONDecodeError:
                print(f'Invalid state file: "{fname}"')
                exit(-1)

        return collections.defaultdict(dict, res)

    def _save_state(
        self,
        state: Dict[Any, Any], fn: Optional[str] = None
    ) -> None:
        fname = str(fn or self.STATE_FILE)
        self._ensure_dir(fname)

        with open(fname, 'w') as fd:
            json.dump(state, fd)


import click
import json
import os
from LapisParser import *

@click.command()
@click.option('--source')
@click.option('--name')
@click.option('--method')
@click.option('--out')
def main(source, name, method, out):
    ans = dict()
    ans['data'] = dict()
    ans['succeed'] = False
    ans['errors'] = list()

    source = str(source)
    name = str(name)
    method = str(method)
    out = str(out)

    ans['errors'] = [{'line': 1, 'col': 1, 'errno': 0, 'msg': 'Format Error'}]
    with open(out, 'w') as f:
        json.dump(ans, f)

    try:
        ret1 = addDocFromFile(source)

        assert ret1 is True
        ret2 = parseAPI()

        assert ret2 is True
        ret3 = generateRandomDataFromAPISchema(name, method)

        if ret3 is not None:
            ans['data'] = ret3
            ans['succeed'] = True
        ans['errors'] = list()
        if len(getErrors()) > 0:
            ans['errors'] = getErrors()
    except Exception:
        ans['errors'] = getErrors()

    with open(out, 'w') as f:
        json.dump(ans, f)

if __name__ == '__main__':
    main()


import matplotlib.pyplot as plt
# 그래프 생성/이미지화 모듈

x = [1,4,9,16,25,36,49,64]
y = [i for i in range(1, 9)]

plt.plot(x, y, 'r')
# 문자열은 색상/반환타입의 속성이다

plt.title('matplotlib sample')
plt.xlabel('x value')
plt.ylabel('y value')
plt.show()


from django.urls import path
from .views import PostsList, PostDetail, PostCreate, PostUpdate, PostDelete, PostSearch, CategoryListView, subscribe, unsubscribe
from django.views.decorators.cache import cache_page

urlpatterns = [
   path('', cache_page(60)(PostsList.as_view()), name='posts_list'),
   path('<int:pk>', cache_page(60*5)(PostDetail.as_view()), name='post_detail'),
   path('search/', PostSearch.as_view(), name='post_search'),
   path('create/', PostCreate.as_view(), name='post_create'),
   path('<int:pk>/update/', PostUpdate.as_view(), name='post_update'),
   path('<int:pk>/delete/', PostDelete.as_view(), name='post_delete'),
   path('categories/<int:pk>', CategoryListView.as_view(), name='category_list'),
   path('categories/<int:pk>/subscribe', subscribe, name='subscribe'),
   path('categories/<int:pk>/unsubscribe', unsubscribe, name='unsubscribe'),
]


#!/usr/bin/python3
"""
External Module - BaseGeometry
Module - Rectangle
"""

BaseGeometry = __import__('7-base_geometry').BaseGeometry

class Rectangle(BaseGeometry):
    """
    Subclass - Rectangle, inherits from class BaseGeometry
    """
    def __init__(self, width, height):
        """
        attribute - width
        attribute - height
        Initialize private instance attributes width and height
        and validates that both attributes are integers greater than 0
        """
        self.integer_validator("width", width)
        self.__width = width
        self.integer_validator("height", height)
        self.__height = height


"""
- Link: https://app.codesignal.com/tournaments/ThyE5pQLXR6tqrGE8/A
"""

#Solution 1:
def __mergeSort(sequence):
    def merge(sequence, left, middle, right):

        result = []

        i = left
        j = middle
        while i < middle and j < right:
            if sequence[i] < sequence[j]:
                result.append(sequence[i])
                i += 1
            else:
                result.append(sequence[j])
                j += 1

        while i < middle:
            result.append(sequence[i])
            i += 1

        while j < right:
            result.append(sequence[j])
            j += 1

        for i in range(left, right):
            sequence[i] = result[i - left]

    def split(sequence, left, right):
        middle = (left + right) // 2

        if left + 1 == right:
            return
        split(sequence, left, middle)
        split(sequence, middle, right)
        merge(sequence, left, middle, right)

    split(sequence, 0, len(sequence))

    return sequence

#Solution 2: clearly ---> python standard

def optimizedMergeSort(sequence):
    if len(sequence) > 1:
        mid = len(sequence) // 2
        left = sequence[:mid]
        right = sequence[mid:]

        optimizedMergeSort(left)
        optimizedMergeSort(right)

        i = j = k = 0

        while i < len(left) and j < len(right):
            if left[i] < right[j]:
                sequence[k] = left[i]
                i += 1
            else:
                sequence[k] = right[j]
                j += 1
            k += 1

        while i < len(left):
            sequence[k] = left[i]
            i += 1
            k += 1

        while j < len(right):
            sequence[k] = right[j]
            j += 1
            k += 1

    return sequence

if __name__ == "__main__":
    sequence = [100, 99]

    # print("After implementing merge sort: {}".format(__mergeSort(sequence)))
    print("{}".format(optimizedMergeSort(sequence)))


import sys
import math

def slope_intercept_points(y2, x2, y1, x1):
    print("On the form y=mx+b, where m represents the slope, and b represents the y-intercept, the linear equation is:")
    m = round((y2 - y1)/(x2 - x1), 3)
    b = round(y1 - (m*x1), 3)
    if m.is_integer():
        m = math.trunc((y2 - y1)/(x2 - x1))
    if b.is_integer():
        b = math.trunc(y1 - (m*x1))

    tuple_equation = "y=",str(m),"x+",str(b)
    joined_equation = "".join(tuple_equation)
    return joined_equation

try:
    print(slope_intercept_points(-4, 1, 2, 3))
except:
    print(sys.exc_info()[1])


import boltons.fileutils as bf
import yaml
import pkg_resources
from distutils.dir_util import copy_tree
import pprint
from pathlib import Path
import shutil
import re

from jinja2 import Environment, PackageLoader, select_autoescape

resource_package = __name__

env = Environment(
    loader=PackageLoader("docker_app_generator", "templates"),
    autoescape=select_autoescape(["html", "xml"]),
)

def regex_match(string, p):
    return bool(re.search(p,string))

def regex_split(string, p):
    return string[:string.rfind(p)]

env.filters['regex_match'] = regex_match
env.filters['regex_split'] = regex_split

pp = pprint.PrettyPrinter(indent=4)

def load_config_file(filename):
    if is_valid_file(pkg_resources.resource_filename(resource_package, filename)):
        return yaml.load(pkg_resources.resource_string(resource_package, filename))
    else:
        return []

def create_app_structure(path_app):
    bf.mkdir_p(path_app)
    bf.mkdir_p(path_app + "/dockerfiles")

def cleanup_directory(dir):
    if is_valid_directory(dir):
        shutil.rmtree(dir)

def copy_files_old(from_path, to_path):
    copy_tree(from_path, to_path) if is_valid_file(from_path) else None

def copy_files(from_path_base, stack_element, name, to_path_base):
    # copy files
    for file_type in ["config", "files"]:
        from_path = from_path_base + "/" + stack_element + "/" + name + "/" + file_type
        to_path = to_path_base + "/" + file_type + "/" + name
        print("from {}, to {}".format(from_path, to_path))
        copy_tree(from_path, to_path,) if is_valid_file(from_path) else None

    # copy config
    # copy_tree(from_path, to_path) if is_valid_file(from_path) else None

def generate_app_dockerfile(
    path_app, docker_stack, docker_base, docker_flavors, variant_name=None
):
    # print('Dockerfile config {}\ndocker_base: {}\ndocker_stack: {}\n'.format(
    # path_app, docker_base, docker_stack))

    data = {
        "docker_base": docker_base,
        "docker_stack": docker_stack,
        "docker_flavors": docker_flavors,
    }
    generate_file(
        data,
        "Dockerfile",
        path_app
        + "/dockerfiles/"
        + (variant_name + "." if variant_name else "")
        + "Dockerfile",
    )

def generate_build_shell(path_app, docker_image_name, docker_bases, settings):
    # print('Shell config\npath_app: {}\docker_image_name: {}\docker_bases: {}\n'.format(
    # path_app, docker_image_name, docker_bases))
    data = {
        "docker_image_name": docker_image_name,
        "docker_bases": docker_bases,
        "settings": settings,
    }
    generate_file(data, "build.sh", path_app + "/build.sh")

def generate_docker_compose(path_app, app_config, settings):
    data = {
        "app_config": app_config,
        "compose": app_config["compose"],
        "settings": settings,
    }
    generate_file(data, "docker-compose.yml", path_app + "/docker-compose.yml")
    for env in app_config["compose"]["env"]:
        data['env'] = env
        data['compose_env'] = app_config["compose"]["env"][env]
        generate_file(
            data,
            "docker-compose.env.yml",
            path_app + "/docker-compose." + env + ".yml",
        )
    generate_file(
        data, "docker-compose.common.yml", path_app + "/docker-compose.common.yml"
    )

def generate_file(data, filename, target):
    template = env.get_template(filename + ".j2")
    template.stream(data).dump(target)

def is_valid_file(filename):
    p = Path(filename)
    return p.exists()

def is_valid_directory(filename):
    p = Path(filename)
    return p.exists() and p.is_dir()


"""
# For loop with Range

# Range
    - Default behavior will step through all numbers from start to end of
        specified range increasing by one.
    - Changing the length of step is done by adding a third parameter.
"""
# prints numbers 1 through 9.
for number in range(1,10):
    print(number)

# prints every 2nd number in range.
for stepsByTwo in range(1,12,2):
    print(stepsByTwo)

# adds every digit in range 1 thru 100.
rangeTotal = 0
for accumulator in range(1,101):
    rangeTotal += accumulator
print(rangeTotal)


l = []
read = True
while read:
    num = input('Digite um número ou \'Done\' para encerrar. \n')

    try:
        l.append(int(num))

    except:
        if num == 'Done':
            read = False
        else:
            print("Valor inválido!")
            continue

total = sum(l)
quantidade = len(l)
media = sum(l)/len(l)

print("Total: {}\nQuantidade: {}\nMédia: {}".format(total, quantidade, media))


"""
https://leetcode.com/problems/maximum-subarray/

did this before but still ran into nails..
kind of subtle.. not sure if I can come up with this in a stressful interview environment
"""

from typing import List

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:

        prefixSum = nums[0]
        s = nums[0]
        for num in nums[1:]:
            s = max(s, prefixSum+num, num) # either w/ or w/o prefixSum
            prefixSum = max(prefixSum+num, num, 0) # either taking current num+prefix, or only current num, or nothing

        return s

"""
lets see the divide and conquer approach? prefix sum again?

  [5,4,-1,7,8]
[0,5,9,8,15,23]

  [-2,1,-3,4,-1,2,1,-5,4]
[0,-2,-1,-4,0,-1,1,2,-3,1] the max - min max is to the right of min..

not divide and conquer

I guess divide and conquer if finding left max subarray and right max subarray
and the max subarray cross middle line??

yes

https://leetcode.com/problems/maximum-subarray/discuss/1595195/C%2B%2BPython-7-Simple-Solutions-w-Explanation-or-Brute-Force-%2B-DP-%2B-Kadane-%2B-Divide-and-Conquer

class Solution:
    def maxSubArray(self, nums):
        def maxSubArray(A, L, R):
            if L > R: return -inf
            mid, left_sum, right_sum, cur_sum = (L + R) // 2, 0, 0, 0
            for i in range(mid-1, L-1, -1):
                left_sum = max(left_sum, cur_sum := cur_sum + A[i])
            cur_sum = 0
            for i in range(mid+1, R+1):
                right_sum = max(right_sum, cur_sum := cur_sum + A[i])
            return max(maxSubArray(A, L, mid-1), maxSubArray(A, mid+1, R), left_sum + A[mid] + right_sum)
        return maxSubArray(nums, 0, len(nums)-1)

the cross mid, sum is just adding everything together and see how big it can retain
            for i in range(mid-1, L-1, -1):
                left_sum = max(left_sum, cur_sum := cur_sum + A[i])
            cur_sum = 0
            for i in range(mid+1, R+1):
                right_sum = max(right_sum, cur_sum := cur_sum + A[i])

also it can be precomputed.. pretty smart...

"""


from typing import Dict, List, Optional, Tuple, Callable
import numpy as np

from tensorflow.keras.preprocessing.image import ImageDataGenerator, Iterator

def _sanitize_class_balance(
    classes: List[int], class_balance: Optional[Dict[int, float]] = None
) -> Dict[int, float]:
    if class_balance is None:
        output = {c: 1.0 / len(classes) for c in classes}
    else:
        # normalize the class fractions
        total = np.sum(list(class_balance.values()))
        output = {c: v / total for c, v in class_balance.items()}
    assert np.sum(list(output.values())) == 1.0
    return output

def sample_balanced(
    input_labels: np.ndarray,
    required_samples: int,
    class_balance: Optional[
        Dict[int, float]
    ] = None,  # by default sample classes equally
    shuffle: bool = True,
) -> Dict[int, List[int]]:
    assert input_labels.ndim == 1
    # split input_labels
    classes, indices = np.unique(input_labels, return_index=True)
    classes = list(map(int, classes))
    class_balance = _sanitize_class_balance(
        classes=classes, class_balance=class_balance
    )
    index_dict = {
        c: np.where(input_labels == c)[0]
        if not shuffle
        else np.random.permutation(np.where(input_labels == c)[0])
        for c in classes
    }
    indices_per_class = {
        c: index_dict[c][: int(class_balance[c] * required_samples)] for c in classes
    }
    assert np.sum(list(map(len, indices_per_class.values()))) == required_samples
    return indices_per_class

def sample_balanced_batches(
    input_labels: np.ndarray,
    required_batches: int,
    batch_size: int,
    class_balance: Optional[Dict[int, float]] = None,
    shuffle: bool = True,
) -> np.ndarray:
    assert input_labels.ndim == 1

    indices_per_class = sample_balanced(
        input_labels=input_labels,
        required_samples=required_batches * batch_size,
        class_balance=class_balance,
        shuffle=shuffle,
    )
    classes = list(indices_per_class.keys())

    samples_per_batch = {c: int(class_balance[c] * batch_size) for c in classes}
    assert np.sum(list(samples_per_batch.values())) == batch_size

    sampled = []
    for i in range(required_batches):
        batch = []
        for c in classes:
            samples = samples_per_batch[c]
            batch.append(indices_per_class[c][samples * i : samples * (i + 1)])
        sampled.append(np.concatenate(batch))

    return np.array(sampled)

class UndersamplingIterator(Iterator):
    def __init__(
        self,
        inputs: np.ndarray,
        labels: np.ndarray,
        batch_size: int = 32,
        class_balance: Optional[Dict[int, float]] = None,
        shuffle: bool = True,
        preprocess_fn: Callable = None,
        seed: np.random.RandomState = None,
    ):
        self._inputs = inputs
        self._labels = labels
        self._preprocess_fn = preprocess_fn
        self._labels_argmax = np.argmax(self._labels, axis=1)
        self._batch_size = batch_size
        self._class_balance = class_balance
        self._shuffle = shuffle
        self._req_batches = self._compute_required_batches()
        self._seed = seed
        super(UndersamplingIterator, self).__init__(
            n=self._req_batches,
            batch_size=self._batch_size,
            shuffle=self._shuffle,
            seed=self._seed,
        )
        self._sampled_batch_indices = []
        self._apply_balance_strategy()

    def _compute_required_batches(self) -> int:
        classes, counts = np.unique(self._labels_argmax, return_counts=True)
        self._class_balance = _sanitize_class_balance(
            classes=classes, class_balance=self._class_balance
        )
        min_class = np.argmin(
            [counts[i] / self._class_balance[c] for i, c in enumerate(classes)]
        )
        required_batches = int(
            np.floor(
                int(counts[min_class] / self._class_balance[min_class])
                // self._batch_size
            )
        )
        assert required_batches > 0
        return required_batches

    def on_epoch_end(self):
        """Method called at the end of every epoch.
        """
        self._apply_balance_strategy()

    def _apply_balance_strategy(self):
        self._sampled_batch_indices = sample_balanced_batches(
            input_labels=self._labels_argmax,
            required_batches=self._req_batches,
            batch_size=self._batch_size,
            class_balance=self._class_balance,
            shuffle=self._shuffle,
        )

    def __getitem__(self, index: int) -> Tuple[np.ndarray, np.ndarray]:
        """Gets batch at position `index`.
        Args:
            index: position of the batch in the Sequence.
        Returns:
            A batch
        """
        indices = self._sampled_batch_indices[index]
        X, y = self._inputs[indices, :], self._labels[indices, :]
        if self._preprocess_fn is not None:
            X = self._preprocess_fn(X)
        return X, y

    def __len__(self) -> int:
        return self._req_batches


import mpmath;

class Scorer:

	def score_edge(self,Gain,Sigma):

		G=Gain[0];
		S=Sigma[0];

		if G<=0:
			return mpmath.ln(1);

		if G<S:
			S=G;

		eps = mpmath.ln( 1.0/mpmath.ln(2)) / mpmath.ln(2);

		t0 = mpmath.power(2,-G-eps);

		t1 = mpmath.power(2,-S-eps);
		#print 't1: ',t1;
		t2 = (1.0/G) * mpmath.power(2,-G);
		#print 't2: ',t2;
		t3 = mpmath.power(t1,1) + mpmath.power(t2,1);
		#print 't3: ',t3;
		t4 = -(S)#mpmath.ln(mpmath.power(2,-S));
		#t4 = mpmath.ln(t0 * t3);

		return t4;

	def score_edge_crude(self,Gain,Sigma):
		G=Gain[0];
		S=Sigma[0];
		if G<=0:
			return mpmath.ln(1);

		t0 = mpmath.power(2,-(G+S));
		t1 = mpmath.ln(t0);
		return t1;


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 17/8/1 上午11:49
# @Author  : C.C
# @File    : demo88.py

# 题目：读取7个数（1—50）的整数值，每读取一个值，程序打印出该值个数的＊。

if __name__ == '__main__':
    index = 1

    while index < 8:
        number = int(input("请输入一个(1~50)数字:\n"))
        while number < 1 or number > 50:
            number = int(input("请输入一个(1~50)数字:\n"))

        print(number * "*")
        index += 1


import cv2
import os
import numpy as np

def bytescaling(data, cmin=None, cmax=None, high=255, low=0):
    if data.dtype == np.uint8:
        return data
    if high > 255:
        high = 255
    if low < 0:
        low = 0
    if high < low:
        raise ValueError("`high` should be greater than or equal to `low`.")
    if cmin is None:
        cmin = data.min()
    if cmax is None:
        cmax = data.max()
    cscale = cmax - cmin
    if cscale == 0:
        cscale = 1
    scale = float(high - low) / cscale
    bytedata = (data - cmin) * scale + low
    return (bytedata.clip(low, high) + 0.5).astype(np.uint8)

class analisiss():

    def GetAver(self):
        return None


from rest_framework import serializers
from rest_framework.relations import SlugRelatedField
from rest_framework.validators import UniqueTogetherValidator

from posts.models import Comment, Post, Group, Follow, User

class CommentSerializer(serializers.ModelSerializer):
    """Класс для преобразования сложных данных в простые типы данных Python,
    которые конвертируются в JSON.
    """
    author = SlugRelatedField(read_only=True, slug_field='username')

    class Meta:
        fields = '__all__'
        model = Comment
        read_only_fields = ('post',)

class FollowSerializer(serializers.ModelSerializer):
    user = SlugRelatedField(slug_field='username',
                            queryset=User.objects.all(),
                            default=serializers.CurrentUserDefault())
    following = serializers.SlugRelatedField(
        slug_field="username", queryset=User.objects.all()
    )

    class Meta:
        fields = ("id", "user", "following")
        model = Follow

        validators = [
            UniqueTogetherValidator(
                queryset=Follow.objects.all(),
                fields=['user', 'following']
            )
        ]

    def validate(self, data):
        """
        Проверяет: 'user не равен 'following'.
        """
        if data['user'] == data['following']:
            raise serializers.ValidationError('Вы не можете'
                                              'подписаться на себя.')
        return data

class PostSerializer(serializers.ModelSerializer):
    """Класс для преобразования сложных данных в простые типы данных Python,
    которые конвертируются в JSON.
    """
    author = SlugRelatedField(slug_field='username',
                              read_only=True)

    class Meta:
        fields = ('id', 'text', 'author',
                  'group', 'image', 'pub_date')
        model = Post

class GroupSerializer(serializers.ModelSerializer):
    """Класс для преобразования сложных данных в простые типы данных Python,
    которые конвертируются в JSON.
    """
    class Meta:
        fields = ('id', 'title', 'slug', 'description')
        model = Group


"""Defines core consumer functionality"""
import logging

import confluent_kafka
from confluent_kafka import Consumer
from confluent_kafka.avro import AvroConsumer
from confluent_kafka.avro.serializer import SerializerError
from tornado import gen

logger = logging.getLogger(__name__)

class KafkaConsumer:
    """Defines the base kafka consumer class"""

    def __init__(
        self,
        topic_name_pattern,
        message_handler,
        is_avro=True,
        offset_earliest=False,
        sleep_secs=1.0,
        consume_timeout=0.1,
    ):
        """Creates a consumer object for asynchronous use"""
        logger.info(f"Creating kafka consumer for {topic_name_pattern}")
        self.topic_name_pattern = topic_name_pattern
        self.message_handler = message_handler
        self.sleep_secs = sleep_secs
        self.consume_timeout = consume_timeout
        self.offset_earliest = offset_earliest

        #
        #
        # TODO: Configure the broker properties below. Make sure to reference the project README
        # and use the Host URL for Kafka and Schema Registry!
        #
        #
        self.broker_properties = {
                #
                # TODO
                #
            'bootstrap.servers': 'PLAINTEXT://localhost:9092',
            "default.topic.config": {"auto.offset.reset": "earliest"},
            'group.id': f'{topic_name_pattern}'
        }

        # TODO: Create the Consumer, using the appropriate type.
        if is_avro is True:
            self.broker_properties["schema.registry.url"] = "http://localhost:8001"
            self.consumer = AvroConsumer(self.broker_properties)
        else:
            self.consumer = Consumer(self.broker_properties)

        # TODO: Configure the AvroConsumer and subscribe to the topics. Make sure to think about
        # how the `on_assign` callback should be invoked.
        logger.info(f"Attempting to subscribe to topic {self.topic_name_pattern}")
        self.consumer.subscribe([self.topic_name_pattern], on_assign=self.on_assign)
        logger.info(f"Successful subscription to topic {self.topic_name_pattern}")

    def on_assign(self, consumer, partitions):
        """Callback for when topic assignment takes place"""
        # TODO: If the topic is configured to use `offset_earliest` set the partition offset to
        # the beginning or earliest
        logger.info(f"on_assign {self.topic_name_pattern} consumer {consumer} and partitions {partitions}")
        for partition in partitions:
            # TODO
            logger.info(f"setting offset ...")
            if self.offset_earliest is True:
#                logger.info("xxx")
                #partition.offset = OFFSET_BEGINNING
                partition.offset = 0 # hanging when using OFFSET_BEGINNING
#                logger.info("yyy")
            logger.info(f"done setting offset")

        logger.info("partitions assigned for %s", self.topic_name_pattern)
        consumer.assign(partitions)

    async def consume(self):
        """Asynchronously consumes data from kafka topic"""
        logger.info(f"Consuming data {self.topic_name_pattern}")
        while True:
            num_results = 1
            while num_results > 0:
                num_results = self._consume()
            logger.info("Sleeping ...")
            await gen.sleep(self.sleep_secs)

    def _consume(self):
        """Polls for a message. Returns 1 if a message was received, 0 otherwise"""
        # TODO: Poll Kafka for messages. Make sure to handle any errors or exceptions.

        # Additionally, make sure you return 1 when a message is processed, and 0 when no message
        # is retrieved.

        logger.info(f"poll message from {self.topic_name_pattern}")
        try:
            message = self.consumer.poll(self.consume_timeout)
            logger.info(f"message returned from poll call is {message}")
        except error:
            logger.info(f'Could not poll message from {self.topic_name_pattern}')
            return 0

        if message is None:
            logger.info(f"No message received")
            return 0
        elif message.error() is not None:
            logger.info(f"Message error: {message.error()}")
            return 0
        else:
            logger.info(f"consumed message {message.topic()}")
            self.message_handler(message)
        return 1

    def close(self):
        """Cleans up any open kafka consumers"""
        # TODO: Cleanup the kafka consumer
        logger.info(f"closing {self.topic_name_pattern}")
        self.consumer.close()


"""Create reads

Revision ID: 49b4cc7895ba
Revises: 3f30d324d67d
Create Date: 2023-09-20 21:01:20.431436

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '49b4cc7895ba'
down_revision = '3f30d324d67d'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('reads',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('readable', sa.Integer(), nullable=True),
    sa.Column('readable_id', sa.Integer(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('read_at', sa.DateTime(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('reads', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_reads_readable'), ['readable'], unique=False)

    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('reads', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_reads_readable'))

    op.drop_table('reads')
    # ### end Alembic commands ###


products = {
        "Baja Taco": 4.00,
        "Burrito": 7.50,
        "Bowl": 8.50,
        "Nachos": 11.00,
        "Quesadilla": 8.50,
        "Super Burrito": 8.50,
        "Super Quesadilla": 9.50,
        "Taco": 3.00,
        "Tortilla Salad": 8.00
        }

price = 0

while True:

    try:
        product = input('Item: ').title()
        if product in products:
            price += products[product]
            print(f'Total: ${price:.2f}')

    except EOFError:
        print()
        break


from collections import defaultdict

import numpy as np

from abaqus_python_interface import ABQInterface

from fat_eval.weakest_link.FEM_functions.elements import element_types
from fat_eval.weakest_link.hazard_functions import weibull
from fat_eval.fatigue_materials import materials
from fat_eval.utilities.steel_data import abaqus_fields, SteelData

def setup_weakest_link_evaluator(odb_file, heat_treatment, element_set, instance_name,
                                 symmetry_factor, abaqus):
    abq = ABQInterface(abaqus)
    element_data = abq.get_element_data(odb_file, element_set, instance_name)
    heat_treatment_data = {}
    element_labels = None
    for i, heat_treatment_field in enumerate(abaqus_fields):
        field, _, element_labels = abq.read_data_from_odb(
            odb_file_name=heat_treatment.odb_file_name,
            field_id=heat_treatment_field,
            step_name=heat_treatment.step_name,
            frame_number=heat_treatment.frame_number,
            set_name=heat_treatment.element_set,
            instance_name=heat_treatment.instance,
            get_position_numbers=True
        )
        heat_treatment_data[heat_treatment_field] = field
    elements = {}
    for element_type, element_coordinates in element_data.items():
        for element_label, coords in element_coordinates.items():
            elements[element_label] = element_types[element_type](coords)

    evaluator = WeakestLinkEvaluator(elements, element_labels, SteelData(heat_treatment_data),
                                     symmetry_factor)
    return evaluator

class WeakestLinkEvaluator:
    def __init__(self, elements, element_labels, steel_data, symmetry_factor=1.):
        self.elements = elements
        self.element_labels = element_labels
        self.symmetry_factor = symmetry_factor
        self.steel_data = steel_data

    def evaluate(self, stress_state, material_name, hazard_function=weibull, cycles=2e6):
        material = materials[material_name]
        functional_values = hazard_function(stress_state, self.steel_data, material, cycles=cycles)
        values_dict = defaultdict(list)
        for s, label in zip(functional_values, self.element_labels):
            values_dict[label].append(s)
        integral = 0
        for e_label, values in values_dict.items():
            element = self.elements[e_label]
            for vol, w, val in zip(element.gauss_point_volumes, element.gauss_weights, values):
                integral += val*w*vol

        pf = 1 - np.exp(-integral)
        return 1 - (1 - pf)**self.symmetry_factor


import pytest
from fastapi.testclient import TestClient
from pytest_mock import MockerFixture

from merino.config import settings
from merino.exceptions import InvalidProviderError
from merino.main import app
from merino.providers import ProviderType, get_providers, init_providers

@pytest.mark.asyncio
async def test_init_providers() -> None:
    """Test for the `init_providers` method of the Merino providers module."""

    await init_providers()

    providers, default_providers = get_providers()

    assert len(providers) == 2
    assert ProviderType.ADM in providers
    assert ProviderType.WIKI_FRUIT in providers

    assert len(default_providers) == 2

@pytest.mark.asyncio
async def test_init_providers_unknown_provider_type(mocker: MockerFixture) -> None:
    """Test for the `init_providers` with an unknown provider."""

    mocker.patch.dict(settings.providers, values={"unknown-provider": {}})

    with pytest.raises(InvalidProviderError) as excinfo:
        await init_providers()

    assert str(excinfo.value) == "Unknown provider type: unknown-provider"

def test_unknow_providers_should_shutdown_app(mocker: MockerFixture) -> None:
    """Test Merino should shut down upon an unknown provider."""

    mocker.patch.dict(settings.providers, values={"unknown-provider": {}})

    with pytest.raises(InvalidProviderError) as excinfo:
        # This will run all the FastAPI startup event handlers.
        with TestClient(app):
            # should never make to here
            assert False

    assert str(excinfo.value) == "Unknown provider type: unknown-provider"


frase = str(input('Digite uma frase: '))

fraseSemEspaço = frase.strip().upper()
qtd = fraseSemEspaço.count('A')
primeiraVez = fraseSemEspaço.find('A') + 1
ultimaVez = fraseSemEspaço.rfind("A") + 1

print('A letra A aparece {} vezes na frase.'.format(qtd))
print('A primeira Letra A apareceu na posição {}'.format(primeiraVez))
print('A última letra A apareceu na posição {}'.format(ultimaVez))


import plotly.graph_objects as go

fig = go.Figure(data=[
    go.Mesh3d(
        # Order of vertex matters! in this case is: A-B-C-D-DRONE
        x=[3, 9, 1, 3, 0],
        y=[9, 3, 3, 1, 0],
        z=[0, 0, 0, 0, 6],
        colorbar_title='z',
        # i, j and k give the vertices of triangles
        # here we represent the 4 triangles of the tetrahedron surface
        i=[0, 0, 0, 1, 2, 0],
        j=[2, 1, 1, 3, 3, 2],
        k=[3, 3, 4, 4, 4, 4],
        name='y',
        showscale=True,
        opacity=0.40,
        color="#FF0000"
    )
])

fig.show()


# This file hold EXTRACT functions
from bs4 import BeautifulSoup

from fipe.scripts.loggers import get_logger

logger = get_logger(__name__)

def scrape_complete_tbody(driver, new_columns: list[str]) -> dict:
    """
    Extracts the HTML table within the tbody tags and returns the information as a dictionary.

    Args:
        driver (WebDriver): The WebDriver instance for controlling the browser.
        new_columns (list[str]): New columns coming from the YML configuration file.

    Returns:
        dict: A dictionary containing the extracted information. The keys are the columns from `new_columns`
              and the values are the corresponding values from the HTML table.
    """
    # Get the new URL
    soup = BeautifulSoup(driver.page_source, "html.parser")

    tbody = soup.find_all("tbody")

    row_tbody = tbody[0]
    td_infos = row_tbody.findChildren("td")

    keys_to_remove = [
        "Mês de referência:",
        "Código Fipe:",
        "Marca:",
        "Modelo:",
        "Ano Modelo:",
        "Autenticação",
        "Data da consulta",
        "Preço Médio",
    ]

    value_tab = [
        " ".join(tr.string.split())
        for tr in td_infos
        if tr.string not in keys_to_remove
    ]

    complete_info = dict(zip(new_columns, value_tab))

    return complete_info

def scrape_options_month_year(driver) -> list[str]:
    """
    Extracts all available Reference Months from the HTML.

    Args:
        driver (WebDriver): The WebDriver instance for controlling the browser.

    Returns:
        list[str]: A list of strings representing the available Reference Months.
    """

    logger.info("Extracting all Reference Months available")
    # I can pick the values AVAILABLE, or using Requests or BeautifulSoup
    soup = BeautifulSoup(driver.page_source, "html.parser")

    # Month and Year Available
    options_months_years = soup.find_all(
        "select", attrs={"data-tabref": "selectTabelaReferenciacarro"}
    )

    months_years_available = [
        " ".join(month_year.text.split())
        for month_year in options_months_years[0].contents
        if month_year.text != ""
    ]

    return months_years_available

def scrape_options_brands(driver) -> list[str]:
    """
    Extracts all available brands from the HTML.

    Args:
        driver (WebDriver): The WebDriver instance for controlling the browser.

    Returns:
        list[str]: A list of strings representing the available brands.
    """

    logger.info("Extracting all Brands available")
    # I can pick the values AVAILABLE, or using Requests or BeautifulSoup

    soup = BeautifulSoup(driver.page_source, "html.parser")

    # Brands Available
    options_brands = soup.find_all(
        "select",
        attrs={
            "data-placeholder": "Digite ou selecione a marca do veiculo",
            "data-tipo": "marca",
        },
    )

    brands = [
        brand.text.strip()
        for brand in options_brands[0].children
        if brand.text.strip() != "" and brand.text.strip() != " "
    ]

    return brands

def scrape_options_models(driver) -> list[str]:
    """
    Extract all Models available for the current Brand
    If Brand available is Nissan, then will get Sentra, Versa, Frontier and so on.
    Args:
        driver (WebDriver): The WebDriver instance for controlling the browser.

    Returns:
        list[str]: List of models.
    """

    logger.info("Extracting all models available")
    # Get the new URL
    soup = BeautifulSoup(driver.page_source, "html.parser")
    options_models_years = soup.find("div", attrs={"class": "step-2"})

    # Get ALL Models available according to a BRAND selected
    models_select = options_models_years.findChildren("select")[0]
    models = models_select.contents

    # My List of Models
    all_models = [model.text for model in models if model.text != ""]
    return all_models

def scrape_manufacturing_year_fuel(driver) -> list[str]:
    """
    Extracts the available MANUFACTURING YEAR - FUEL options.

    Args:
        driver (WebDriver): The WebDriver instance for controlling the browser.

    Returns:
        list[str]: A list of strings representing the available MANUFACTURING YEAR - FUEL options.
    """
    logger.info("Extracting all MANUFACTURING YEAR - FUEL available")
    # Get the new URL
    soup = BeautifulSoup(driver.page_source, "html.parser")

    year_fuel_for_especific_brand = soup.find(
        "select",
        attrs={"data-placeholder": "Digite ou selecione o ano modelo do veiculo"},
    )

    year_fuel_according_brand_and_model = [
        year.text for year in year_fuel_for_especific_brand if year.text != ""
    ]

    return year_fuel_according_brand_and_model


import json
import time

def genere_solution():
    # a*x+b*y=c [10]

    resultat = []
    for a in range(0, 10):
        for b in range(0, 10):
            for c in range(0, 10):

                tmp = {'a': a, 'b': b, 'c': c, 'result': []}
                resultat.append(tmp)
                for x in range(0, 10):
                    for y in range(0, 10):
                        if (a * x + b * y - c) % 10 == 0:
                            print(f'{a}*{x}+{b}*{y}={c}[10]')
                            tmp['result'].append({'x': x, 'y': y})

    return resultat

def main():
    with open('files/solution.json', "w") as f:
        start = time.time()

        res = genere_solution()

        end = time.time()
        elapsed = end * 1000 - start * 1000

        f.write(json.dumps(res, sort_keys=True, indent=4) + "\n")

        print(f'Temps d\'exécution : {elapsed}ms')

if __name__ == '__main__':
    main()


#!/usr/bin/env python3

"""
This module reads the data in...
"""

import numpy as np
import pickle
import os.path
import random

import logging
logging.basicConfig(level=0)

# This named tuple holds the matrices which make one minibatch
class Matrices:
    def __init__(self, minibatch_size, max_sent_len, ngrams):
        self.source_ngrams = dict()  # { N -> matrix}
        self.target_ngrams = dict()  # { N -> matrix}

        for N in ngrams:
            self.source_ngrams[N] = np.zeros((minibatch_size, max_sent_len), np.int)
            self.target_ngrams[N] = np.zeros((minibatch_size, max_sent_len), np.int)

        self.src_len = np.zeros((minibatch_size, 1), np.int)
        self.trg_len = np.zeros((minibatch_size, 1), np.int)
        self.targets = np.zeros((minibatch_size, 1), np.float32)
        self.matrix_dict = dict()

        for N in ngrams:
            self.matrix_dict["source_ngrams_{}".format(N)] = self.source_ngrams[N]  # we need a string identifier
            self.matrix_dict["target_ngrams_{}".format(N)] = self.target_ngrams[N]  # we need a string identifier

        self.matrix_dict["src_len"] = self.src_len
        self.matrix_dict["trg_len"] = self.trg_len

class Vocabularies(object):
    def __init__(self, ngrams):
        self.source_ngrams = dict()  # {order -> {"<MASK>":0,"<UNK>":1}} # source language ngrams
        self.target_ngrams = dict()  # {order -> {"<MASK>":0,"<UNK>":1}} # target language characters

        for N in ngrams:
            self.source_ngrams[N] = {"<MASK>": 0, "<UNK>": 1}  # source language ngrams
            self.target_ngrams[N] = {"<MASK>": 0, "<UNK>": 1}  # # target language characters

        self.trainable = True    # If false, it will use <UNK>

    def get_id(self, label, dict_obj):
        if self.trainable:
            return dict_obj.setdefault(label, len(dict_obj))  # auto-grows
        else:
            return dict_obj.get(label, dict_obj["<UNK>"])

def iter_data(training_source, training_target, max_pairs=None):
    counter = 0

    with open(training_source, encoding='utf-8') as src, open(training_target, encoding='utf-8') as trg:
        for src_line, trg_line in zip(src, trg):
            # if 5 <= len(src_line.strip().split()) <= 30 and \
            #         5 <= len(trg_line.strip().split()) <= 30: # sentence length must be between 5 and 30 tokens
            if True:
                yield src_line, trg_line
                counter += 1

                if max_pairs is not None and counter >= max_pairs > 0:
                    break

class InfiniteDataIterator:

    def __init__(self, training_source, training_target, max_iterations=None, max_pairs=None):
        self.training_source = training_source
        self.training_target = training_target
        self.max_iterations = max_iterations
        self.max_pairs = max_pairs
        self.data = list(iter_data(self.training_source, self.training_target, self.max_pairs))  # must memorize

    def __iter__(self):
        """
        Returns randomized training pairs as ((source_sent,target_sent),-1/+1)
        """
        # [(0,0),(1,1),(2,2)...]  #indices of source, target
        positive_indices = list(zip(range(len(self.data)), range(len(self.data))))
        counter = 0

        while True:
            indices = list(range(len(self.data)))
            random.shuffle(indices)  # shuffled indices
            negative_indices = list(enumerate(indices))  # [(0,326543),(1,96457),...]
            all_examples = positive_indices + negative_indices
            random.shuffle(all_examples)

            for src_idx, trg_idx in all_examples:
                # index where I should take the source sentence, index where I should take the target sentence
                if src_idx == trg_idx:  # same -> positive example
                    yield self.data[src_idx], 1.0
                else:  # different -> negative example
                    yield (self.data[src_idx][0], self.data[trg_idx][1]), 0.0

            counter += 1

            if self.max_iterations is not None and counter == self.max_iterations:
                break

def ngram_iterator(src, N, max_sentence_len):
    if N == 1:
        yield from src[:max_sentence_len]
    else:
        for i in range(len(src) - N + 1):
            if i >= max_sentence_len:
                break

            yield src[i: i + N]

def read_vocabularies(voc_fname, training_source, training_target, force_rebuild, ngrams):
    # ngrams -> (1,2,3)... iterable of Ns
    counter = 0

    if force_rebuild or not os.path.exists(voc_fname):
        # make sure no feature has 0 index
        logging.info("Making one pass to gather vocabulary")
        vs = Vocabularies(ngrams)

        for (sent_src, sent_target), _ in InfiniteDataIterator(training_source,
                                                               training_target,
                                                               max_iterations=1,
                                                               max_pairs=1000000):
            counter += 1

            if counter > 0 and counter % 1000 == 0:
                logging.info("Seen {} sentence pairs...".format(counter))

            for N in ngrams:
                for ngram in ngram_iterator(sent_src, N, len(sent_src)):
                    vs.get_id(ngram, vs.source_ngrams[N])

                for ngram in ngram_iterator(sent_target, N, len(sent_target)):
                    vs.get_id(ngram, vs.target_ngrams[N])

        logging.info("Saving new vocabularies to " + voc_fname)
        save_vocabularies(vs, voc_fname)
    else:
        logging.info("Loading vocabularies from " + voc_fname)
        vs = load_vocabularies(voc_fname)
    return vs

def save_vocabularies(vs, f_name):
    with open(f_name, "wb") as f:
        pickle.dump(vs, f, pickle.HIGHEST_PROTOCOL)

def load_vocabularies(f_name):
    with open(f_name, "rb") as f:
        return pickle.load(f)

def fill_batch(minibatch_size, max_sent_len, vs, data_iterator, ngrams):
    """ Iterates over the data_iterator and fills the index matrices with fresh data
        ms = matrices, vs = vocabularies
    """
    ms = Matrices(minibatch_size, max_sent_len, ngrams)
    batchsize, max_sentence_len = ms.source_ngrams[ngrams[0]].shape
    row = 0

    for (sent_src, sent_target), target in data_iterator:
        for N in ngrams:
            for j, ngram in enumerate(ngram_iterator(sent_src, N, max_sent_len)):
                ms.source_ngrams[N][row, j] = vs.get_id(ngram, vs.source_ngrams[N])
            for j, ngram in enumerate(ngram_iterator(sent_target, N, max_sent_len)):
                ms.target_ngrams[N][row, j] = vs.get_id(ngram, vs.target_ngrams[N])

        ms.src_len[row] = len(sent_src.strip().split())
        ms.trg_len[row] = len(sent_target.strip().split())
        ms.targets[row] = target
        row += 1

        if row == batchsize:
            yield ms.matrix_dict, ms.targets

            row = 0
            ms = Matrices(minibatch_size, max_sent_len, ngrams)
    else:
        if row > 0:
            yield ms.matrix_dict, ms.targets

# if __name__ == "__main__":
#     vs = read_vocabularies('bidirectional-ngrams',
#                            "data/JRC-Acquis.en-fi.fi",
#                            "data/JRC-Acquis.en-fi.en",
#                            False,
#                            (3, 4))
#     vs.trainable = False
#     ms = Matrices(100, 500, (3, 4))  # minibatchsize, max_sent_len, ngrams
#     raw_data = InfiniteDataIterator("data/JRC-Acquis.en-fi.fi", "data/JRC-Acquis.en-fi.en", 1)
#     for minibatch, targets in fill_batch(100, 500, vs, raw_data, (3, 4)):
#         print(minibatch["source_ngrams_3"])
#         print(minibatch["target_ngrams_3"])
#         print(targets)
#         break


"""
GPU-based coordinate transformation demo: (log/lat)==>(x/y), relative to a
camera origin

Note: camera configuration is read from a CSV file using Panda
"""

import numpy as np
import pandas as pd

import cuspatial

def get_ts_struct(ts):
    y = ts & 0x3F
    ts = ts >> 6
    m = ts & 0xF
    ts = ts >> 4
    d = ts & 0x1F
    ts = ts >> 5
    hh = ts & 0x1F
    ts = ts >> 5
    mm = ts & 0x3F
    ts = ts >> 6
    ss = ts & 0x3F
    ts = ts >> 6
    wd = ts & 0x8
    ts = ts >> 3
    yd = ts & 0x1FF
    ts = ts >> 9
    ms = ts & 0x3FF
    ts = ts >> 10
    pid = ts & 0x3FF

    return y, m, d, hh, mm, ss, wd, yd, ms, pid

data_dir = "./data/"
df = pd.read_csv(data_dir + "its_camera_2.csv")
this_cam = df.loc[df["cameraIdString"] == "HWY_20_AND_LOCUST"]
cam_lon = np.double(this_cam.iloc[0]["originLon"])
cam_lat = np.double(this_cam.iloc[0]["originLat"])

lonlats = cuspatial.read_points_lonlat(data_dir + "locust.location")
ids = cuspatial.read_uint(data_dir + "locust.objectid")
ts = cuspatial.read_its_timestamps(data_dir + "locust.time")

# examine binary representatons
ts_0 = ts.data.to_array()[0]
out1 = format(ts_0, "016x")
print(out1)
out2 = format(ts_0, "064b")
print(out2)

y, m, d, hh, mm, ss, wd, yd, ms, pid = get_ts_struct(ts_0)

xys = cuspatial.lonlat_to_cartesian(
    cam_lon, cam_lat, lonlats["lon"], lonlats["lat"]
)
num_traj, trajectories = cuspatial.derive(xys["x"], xys["y"], ids, ts)
#  = num_traj, tid, len, pos =
y, m, d, hh, mm, ss, wd, yd, ms, pid = get_ts_struct(ts_0)
distspeed = cuspatial.distance_and_speed(
    xys["x"], xys["y"], ts, trajectories["length"], trajectories["position"]
)
print(distspeed)

boxes = cuspatial.spatial_bounds(
    xys["x"], xys["y"], trajectories["length"], trajectories["position"]
)
print(boxes.head())


import pandas as pd
import math
from scipy import spatial
from sklearn.feature_extraction.text import TfidfVectorizer
import pickle
import nltk
nltk.download('stopwords')
nltk.download('punkt')
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

# ***************************************************************************************************************************************
# Query
#query = "aubergine fennel bulb red onion mushrooms olive oil garlic cloves"
#query = query.lower()
#q_list = query.split()
# print (q_list)

# ***************************************************************************************************************************************
# Read file
df = pd.read_csv("/Users/placid_brain/Documents/IR stuff/lsa/recipes_w_search_terms.csv",error_bad_lines=False, engine="python", nrows=1000)

# ***************************************************************************************************************************************
# Processing
def function(ini_list):
  new_cell = ini_list.strip('][').split(', ')
  for item in new_cell:
    item = item.replace("'","")
  return new_cell

df['ingredients'] = df['ingredients'].apply(function)
df['steps'] = df['steps'].apply(function)
df1 = df[['id', 'name','ingredients','steps']]
df1.to_pickle("./df1_926.pkl")
# print (df1)
# print(df1['ingredients'])
# print ((df1['ingredients'][0]))
# print ((df1['ingredients'][1]))

# ***************************************************************************************************************************************
# tf-idf vector for documents
data = []
for i in range (len(df1['ingredients'])):
    ingre_str = ''.join(df1['ingredients'][i])
    # print (ingre_str)
    data.append(ingre_str)

# print(data)

tfvec = TfidfVectorizer()
tdf = tfvec.fit_transform(data)
tf_idf_docs_matrix = pd.DataFrame(tdf.toarray(), columns = tfvec.get_feature_names())

'''term_list = tfvec.get_feature_names()
with open('list_pickle', 'wb') as fh:
  pickle.dump(term_list, fh)
tf_idf_docs_matrix.to_pickle("./tf_idf_docs_matrix.pkl") '''
# print(tf_idf_docs_matrix)

# get list of tf-idf vectors of recipes
# print (tf_idf_docs_matrix.values.tolist())

# ***************************************************************************************************************************************

def vector_gen(q_list):
  # tf-idf vector for query
  # print(tfvec.get_feature_names())

  query_tfidf_vector = []
  for i in tfvec.get_feature_names():
      # print(i)
      if i not in q_list:
          query_tfidf_vector.append(0.0)
      else:
          # append tf-idf of the term for query
          # print(i)
          query_tfidf_vector.append(tfvec.idf_[tfvec.vocabulary_[i]])
  return query_tfidf_vector

# print (query_tfidf_vector)
# print(len(query_tfidf_vector))

def search(query):
  sw = set(stopwords.words('english'))

  query = query.lower()

  query_tokens = word_tokenize(query)
  q_list = []

  for w in query_tokens:
      if w not in sw:
          q_list.append(w)
  query_tfidf_vector = vector_gen(q_list)

  # similarity - cosine similarity
  # dict to store cosine similarity between query and documents (key - doc index, value - score)
  similarity_list = []

  for i in range(len(tf_idf_docs_matrix.values.tolist())):
      # print(tf_idf_docs_matrix.values.tolist()[i])
      cosine_similarity = 1 - spatial.distance.cosine(query_tfidf_vector, tf_idf_docs_matrix.values.tolist()[i])
      similarity_list.append(cosine_similarity)

  # print(similarity_list)

  top_5_recipes = sorted(range(len(similarity_list)), key=lambda i: similarity_list[i], reverse=True)[:5]

  # print(top_5_recipes)

  for i in range(len(top_5_recipes)):
    # print(top_5_recipes[i])
    print("{}. {}".format(i+1, df1['name'][top_5_recipes[i]]))

#search('aubergine fennel bulb red onion mushrooms olive oil garlic cloves')

#pickle.dump(model, open("model", 'wb'))

class VSM:
  def __init__(self):
    #super(VSM, self).__init__()
    #self.query = query
    #self.search(self.query)
    #super(VSM, self).__init__()
    pass
  def vector_gen(self, q_list):
    # tf-idf vector for query
    # print(tfvec.get_feature_names())

    query_tfidf_vector = []
    for i in tfvec.get_feature_names():
        # print(i)
        if i not in q_list:
            query_tfidf_vector.append(0.0)
        else:
            # append tf-idf of the term for query
            # print(i)
            query_tfidf_vector.append(tfvec.idf_[tfvec.vocabulary_[i]])
    return query_tfidf_vector

  def search(self,query):
    query = query.lower()
    q_list = query.split()

    if set(q_list).issubset(set(tfvec.get_feature_names()))== True:
      query_tfidf_vector = self.vector_gen(q_list)

      # similarity - cosine similarity
      # dict to store cosine similarity between query and documents (key - doc index, value - score)
      similarity_list = []
      final_list = []

      for i in range(len(tf_idf_docs_matrix.values.tolist())):
          # print(tf_idf_docs_matrix.values.tolist()[i])
          cosine_similarity = 1 - spatial.distance.cosine(query_tfidf_vector, tf_idf_docs_matrix.values.tolist()[i])
          similarity_list.append(cosine_similarity)

      # print(similarity_list)

      top_5_recipes = sorted(range(len(similarity_list)), key=lambda i: similarity_list[i], reverse=True)[:5]

      for i in range(len(top_5_recipes)):
        final_list.append(df1['name'][top_5_recipes[i]])
        print("{}. {}".format(top_5_recipes[i], df1['name'][top_5_recipes[i]]))
      return final_list
    else:
      ["No matching results found"]

vsm = VSM()
vsm.search('aubergine fennel bulb red onion mushrooms olive oil garlic cloves')

with open('model_pickle_926','wb') as f:
  pickle.dump(vsm,f)


import requests

def get_info():
    response = requests.get(url="https://yobit.net/api/3/info") # Получаем инфу по парам

    with open("output_data/Yobit_data/info.txt", "w") as file:
        file.write(response.text)

    return response.text

def get_ticker(coin1="btc", coin2="usd"): # Статистика за последние 24 часа
    response = requests.get(url=f"https://yobit.net/api/3/ticker/{coin1}_{coin2}?ignore_invalid=1") # /Перечисляем пару   ?ignore_invalid=1 если какой-то пары нет, то вместо ошибки выдаст то, что есть

    with open("output_data/Yobit_data/ticker.txt", "w") as file:
        file.write(response.text)

    return response.text

def get_depth(coin1="btc", coin2="usd", limit=50): # Список активных ордеров  limit - глубина для выводa(150 дефолт, 2000 макс)
    response = requests.get(url=f"https://yobit.net/api/3/depth/{coin1}_{coin2}?limit={limit}?ignore_invalid=1") # /Перечисляем пару   ?ignore_invalid=1 если какой-то пары нет, то вместо ошибки выдаст то, что есть

    with open("output_data/Yobit_data/depth.txt", "w") as file:
        file.write(response.text)

    """        Список предложений на покупку         """

    bids = response.json()[f"{coin1}_{coin2}"]["bids"] # Отсеиваем ненужное и оставляем нужный список предложений на покупку с ценой и количеством

    total_bids_amount = 0
    for item in bids: # (цена1 * количество1) + (цена2 * количество2) +... не понял для чего это
        price = item[0]
        coin_amount = item[1]
        total_bids_amount += price * coin_amount

    # Минимальная цена с количеством
    min_bid_price = bids[0][0]
    min_bid_amount = bids[0][1]

    '''        Список предложений на продажу         '''

    asks = response.json()[f"{coin1}_{coin2}"]["asks"] # Отсеиваем ненужное и оставляем нужный список предложений на продажу с ценой и количеством

    total_asks_amount = 0
    for item in asks: # (цена1 * количество1) + (цена2 * количество2) +... не понял для чего это
        price = item[0]
        coin_amount = item[1]
        total_asks_amount += price * coin_amount

    # Минимальная цена с количеством
    min_ask_price = asks[0][0]
    min_ask_amount = asks[0][1]

    # Визуальное оформление валюты результата
    if coin2 == "usd":
        currency = "$"
    elif coin2 == "rur":
        currency = "₽"

    return f"\nTotal bids: {total_bids_amount} {currency}\nMin bid price: {min_bid_price} {currency}\nAmount: {min_bid_amount}\n\nTotal asks: {total_asks_amount} {currency}\nMin ask price: {min_ask_price} {currency}\nAmount: {min_ask_amount}"

def get_trades(coin1="btc", coin2="usd", limit=150): # Инфа о последних сделках указанных пар
    response = requests.get(url=f"https://yobit.net/api/3/trades/{coin1}_{coin2}?limit={limit}?ignore_invalid=1") # /Перечисляем пару   ?ignore_invalid=1 если какой-то пары нет, то вместо ошибки выдаст то, что есть

    with open("output_data/Yobit_data/trades.txt", "w") as file:
        file.write(response.text)

    total_trade_ask = 0
    total_trade_bid = 0

    for item in response.json()[f"{coin1}_{coin2}"]:
        if item["type"] == "ask":
            total_trade_ask += item["price"] * item["amount"]
        else:
            total_trade_bid += item["price"] * item["amount"]

    info = f"[-] TOTAL {coin1} SELL: {round(total_trade_ask, 2)} $\n[+] TOTAL {coin1} BUY: {round(total_trade_bid, 2)} $"

    return info

def main():
    coin1 = input("Введите криптовалюту: ")
    coin2 = input("Введите желаемую валюту: ")
    #print(get_info())
    #print(get_ticker())
    #print(get_ticker(coin1="eth", coin2=coin2))
    print(get_depth(coin1=coin1, coin2=coin2))
    #print(get_depth(coin1="doge"))
    #print(get_depth(coin1="doge", limit=2000))
    #print(get_trades(coin1=coin1, coin2=coin2))

if __name__== "__main__":
    main()


#!/usr/bin/python3
from main import *

about = """
+-------------------------------+
| SCRIPT : WEBSC                |
| BY : Aymen Benchiheub         |
| DATE : 07/05/2017             |
|                               |
|My Facebbok:			|
|   www.facebook.com/benchiehub |
+-------------------------------+

"""
def start(url):
	time = get_local_time()
	# Just to Get The Domain Name !
	domain = get_domain(url)
	# To Get The Ip adress
	ip = get_ip(domain)
	# Start Scaning The Website using Nmap Tool
	n = nmap("-F",ip)
	#Scaning The Website using Whois Tool
	w = whois(domain)
	# CREATE the folder where we save the files
	name = domain
	# write the informations into the file
	informations = [ip,n,w]
	name = str(name + time)
	#Create the dir
	create_dir(domain)
	for data in informations:
		write_file(name,data)
	bye()

#Get The Url From The User
print(about)
url = input('[+] - Please Put The Link Here => : ')
#Start !!!
if __name__ == '__main__':
	start(url)


"""
    Parity in a binary word accounts for the number of ones (set bits).

    Even number of set bits  --> 0 parity
    Odd  number of set bits  --> 1 parity

"""
def main():
    """
    Following code prints the parity of numbers starting from 1 and extending
    to 10
    """
    for i in range (1, 10):
        #print ("Basic method......\n")
        #print (i, bin(i)[2:].zfill(8), " parity: ", parity_basic(i))
        #print ("Slight improvement over basic method......\n")
        print (i, bin(i)[2:].zfill(8), " parity: ", parity(i))

#This function has small improvement over parity_basic
def parity (x):
    parity_result =0

    while (x):
        parity_result ^= x & 1
        x >>= 1

    return parity_result

def parity_basic(x):
    """
        count the number of set bits and then find modulo to decide if it has
        even or odd number of ones
    """

    count_set_bits=0
    while (x):
        count_set_bits+=x&1
        x>>=1
    if count_set_bits%2==0:
        print ("Even parity...........")
    else:
        print ("Odd parity ...........")
    return count_set_bits%2

if __name__=="__main__":
    main()


"""empty message

Revision ID: 2e7f50426ccf
Revises: a2f5a0132bde
Create Date: 2018-03-13 12:20:00.301645

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '2e7f50426ccf'
down_revision = 'a2f5a0132bde'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('tag',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=50), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('translation_tags',
    sa.Column('translation_id', sa.Integer(), nullable=False),
    sa.Column('tag_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['tag_id'], ['tag.id'], ),
    sa.ForeignKeyConstraint(['translation_id'], ['translation.id'], ),
    sa.PrimaryKeyConstraint('translation_id', 'tag_id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('translation_tags')
    op.drop_table('tag')
    # ### end Alembic commands ###


import logging
from io import SEEK_CUR
from typing import Iterable, Iterator

from .exceptions import FlvDataError
from .format import FlvDumper, FlvParser
from .io_protocols import RandomIO
from .models import BACK_POINTER_SIZE, FlvHeader, FlvTag
from .utils import AutoRollbacker, OffsetRepositor

__all__ = 'FlvReader', 'FlvWriter'

logger = logging.getLogger(__name__)

class FlvReader:
    def __init__(
        self,
        stream: RandomIO,
        *,
        backup_timestamp: bool = False,
        restore_timestamp: bool = False,
    ) -> None:
        self._stream = stream
        self._backup_timestamp = backup_timestamp
        self._restore_timestamp = restore_timestamp
        self._parser = FlvParser(
            stream,
            backup_timestamp=backup_timestamp,
            restore_timestamp=restore_timestamp,
        )

    def read_header(self) -> FlvHeader:
        header = self._parser.parse_header()
        previous_tag_size = self._parser.parse_previous_tag_size()
        if previous_tag_size != 0:
            raise FlvDataError(f'First back-pointer must be 0: {previous_tag_size}')
        return header

    def read_tag(self, *, no_body: bool = False) -> FlvTag:
        tag = self._parser.parse_tag(no_body=no_body)
        previous_tag_size = self._parser.parse_previous_tag_size()
        if previous_tag_size != tag.tag_size:
            raise FlvDataError(f'Wrong back-pointer: {previous_tag_size}')
        return tag

    def read_tags(self, *, no_body: bool = False) -> Iterator[FlvTag]:
        while True:
            try:
                yield self.read_tag(no_body=no_body)
            except EOFError:
                break

    def rread_tag(self, *, no_body: bool = False) -> FlvTag:
        self._seek_to_previous_tag()
        with OffsetRepositor(self._stream):
            return self.read_tag(no_body=no_body)

    def rread_tags(self, *, no_body: bool = False) -> Iterator[FlvTag]:
        while True:
            try:
                yield self.rread_tag(no_body=no_body)
            except EOFError:
                break

    def read_body(self, tag: FlvTag) -> bytes:
        with OffsetRepositor(self._stream):
            self._stream.seek(tag.body_offset)
            return self._stream.read(tag.body_size)

    def _seek_to_previous_tag(self) -> int:
        try:
            self._stream.seek(-BACK_POINTER_SIZE, SEEK_CUR)
        except OSError as e:
            if e.errno == 22 and e.strerror == 'Invalid argument':
                raise EOFError()
            else:
                raise
        previous_tag_size = self._parser.parse_previous_tag_size()
        return self._stream.seek(-(4 + previous_tag_size), SEEK_CUR)

class FlvWriter:
    def __init__(self, stream: RandomIO) -> None:
        self._stream = stream
        self._dumper = FlvDumper(stream)

    def write_header(self, header: FlvHeader) -> int:
        with AutoRollbacker(self._stream):
            self._dumper.dump_header(header)
            self._dumper.dump_previous_tag_size(0)
            return header.size + BACK_POINTER_SIZE

    def write_tag(self, tag: FlvTag) -> int:
        with AutoRollbacker(self._stream):
            self._dumper.dump_tag(tag)
            self._dumper.dump_previous_tag_size(tag.tag_size)
            return tag.tag_size + BACK_POINTER_SIZE

    def write_tags(self, tags: Iterable[FlvTag]) -> int:
        return sum(map(self.write_tag, tags))


# -*- coding: utf-8 -*-
"""
Created on Sat Jan  2 00:02:58 2016

@author: owner
"""

import re
import cv2
import numpy as np
from decimal import Decimal

def is_string_like(obj):
    try:
        obj + ''
    except (TypeError, ValueError):
        return False
    return True

def array2string(array):

    if array.ndim != 1 and array.ndim != 2:
        raise ValueError('This function only supports array with ndim=1 or ndim=2')

    string = ''

    if array.ndim == 1:
        n_elems = array.shape[0]

        for n in range(n_elems):
            string += str(Decimal(float(array[n]))) if issubclass(array.dtype.type, np.floating) else str(Decimal(int(array[n])))
            if n != n_elems - 1:
                string += ' '
    else:
        rows = array.shape[0]
        cols = array.shape[1]

        for row in range(rows):
            for col in range(cols):
                string += str(Decimal(float(array[row, col]))) if issubclass(array.dtype.type, np.floating) else str(Decimal(int(array([row, col]))))
                if col != (cols - 1):
                    string += ' '
            if row != rows - 1:
                string += '\n'

    return string

def string2array2D(rows, cols, string, dtype=np.float64):

    array = np.zeros([rows, cols], dtype)

    string_rows = string.split('\n')

    for row, string_row in enumerate(string_rows):
        string_cols = string_row.split(' ')
        for col, string_col in enumerate(string_cols):
            array[row, col] = float(string_col)

    return array

def string2array1D(length, string, dtype=np.float64):

    array = np.zeros([length], dtype)

    elems = string.split(' ')

    for n, elem in enumerate(elems):
        array[n] = float(elem)

    return array

## Save numpy ndarray with ndim==3 to text file
#  @param filename Output text file name
#  @param data Array object to save
def saveText3D(filename, data):

    with open(filename, 'wb') as outfile:

        # writing a header just for the sake of readability
        outfile.write(bytes('# Array shape: {0}\n'.format(data.shape), 'utf-8'))

        # iterating through an array along the last axis
        for data_slice in data:

            # writing data into outfile
            np.savetxt(outfile, data_slice)

            # writing out a break to indicate different slices
            outfile.write(bytes('# New slice\n', 'utf-8'))

## Read random offset points
#  @param filenames File names that contains depth data
#  @param array_shape Array shape of reading data. [n_joints, n_offsets, n_dimensions]
#  @return offset_points list of 3D array that contains random offset points from true joint positions.
def readText3D(filenames):

    shape_check = re.compile('\(.*\)')

    data = []
    for filename in filenames:
        # check array shape
        with open(filename, 'rb') as infile:
            header = infile.readline().decode('utf-8')
            array_shape_str = shape_check.search(header).group(0)
            array_shape = tuple([int(x) for x in  array_shape_str[1:-1].split(', ')])

        data_slice = np.loadtxt(filename)
        data_slice = data_slice.reshape(array_shape)
        data.append(data_slice)

    return data

## Read numpy ndarray with ndim==2 from text file
#  @param filenames File names that contains silhouette data
#  @return joints 2d array that contains 3D joint positions
def saveText2D(filename, data):

    np.savetxt(filename, data)

## Read numpy ndarray with ndim==2 from text file
#  @param filenames File names that contains silhouette data
#  @return joints 2d array that contains 3D joint positions
def readText2D(filenames):

    data = []
    for filename in filenames:
        data_slice = np.loadtxt(filename)
        data.append(data_slice)

    return data

## Read depth data
#  @param filenames File names that contains depth data
#  @return depthmaps 16-bit, single-channel depth maps
def readDepthMap(filenames):

    depthmaps = []
    for filename in filenames:
        img = cv2.imread(filename, cv2.IMREAD_ANYDEPTH)
        depthmaps.append(img)

    return depthmaps

## Read joint and depth data for pose estimation
#  @param joint_prefix Directory that contains reading file.
#  @param depth_prefix
#  @param frame_begin Frame number that reading start with.
#  @param frame_end Frame number that reading stop with.
#  @param frame_step Step size of reading.
#  @param joint_extension
#  @param depth_extension
#  @return joints 2d array that contains 3D joint positions
#  @return depthmaps List of 16-bit, single-channel depth maps
def readDepthAndJoints(depth_prefix, joints_prefix, frame_begin, frame_end, frame_step, depth_extension, joints_extension):

    if(frame_step == 0):
        raise ValueError('frame_step must not be 0')

    # file names
    depth_names = []
    joints_names = []

    for i in range(frame_begin, frame_end + 1, frame_step):
        depth_names.append(depth_prefix + str(i) + depth_extension)
        joints_names.append(joints_prefix + str(i) + joints_extension)

    depthmaps = readDepthMap(depth_names)
    joints = readText2D(joints_names)

    return depthmaps, joints


import json
from django.shortcuts import render
from django.core.paginator import Paginator
from django.http import HttpResponse
from django.shortcuts import redirect
from django.db.models import Q
from django.utils import timezone

from apps.pregunta.models import Pregunta, Respuesta
from apps.usuario.models import Usuario
from apps.pregunta.forms import PreguntaForm, RespuestaForm
from apps.utils.ServiceResponse import ServiceResponse

# Create your views here.

def list(request):
    pregunta = Pregunta.objects.all().order_by('-fecha')
    query = request.GET.get("buscar")
    if query:
        pregunta = Pregunta.objects\
        .filter(titulo__icontains=query)

    paginator = Paginator(pregunta, 10)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    contexto = {'page_obj': page_obj, 'page': page_number}
    return render(request, 'pregunta/list.html', contexto)

def delete(request, id_pregunta):
    mensaje = ''
    userId = request.user.id
    usuario = Usuario.objects.filter(user_id = userId).exists()
    pregunta = Pregunta.objects.get(id=id_pregunta)
    if usuario:
        usu = Usuario.objects.get(user_id = userId)
        if pregunta.usuario_id == usu.id:
            if request.method == 'POST':
                pregunta.delete()
                return redirect('list')
        else:
            mensaje='No es el creador de la pregunta'
    else:
        mensaje='Debe loguearse para poder eliminar una pregunta'
    return render(request, 'pregunta/delete.html', {'pregunta': pregunta, 'msg': mensaje})

def delete_respuesta(request, id_respuesta):
    mensaje = ''
    userId = request.user.id
    usuario = Usuario.objects.filter(user_id = userId).exists()
    respuesta = Respuesta.objects.get(id=id_respuesta)
    if usuario:
        usu = Usuario.objects.get(user_id = userId)
        if respuesta.pregunta.usuario_id == usu.id:
            if request.method == 'POST':
                respuesta.delete()
                return redirect('list')
        else:
            mensaje = 'No es el creador de la pregunta'
    return render(request, 'pregunta/delete_respuesta.html', {'respuesta': respuesta, 'msg': mensaje})

def respuesta_correcta(request, id_respuesta):
    userId = request.user.id
    respuesta = Respuesta.objects.get(id__exact=id_respuesta)
    id_pregunta = respuesta.pregunta_id
    usu = Usuario.objects.get(user_id = userId)
    if respuesta.pregunta.usuario_id == usu.id:
        cont = Respuesta.objects.filter(Q(pregunta_id=id_pregunta) & Q(correcta=True)).count()
        if cont == 0:
            respuesta.correcta = True
            respuesta.save()
            response = ServiceResponse()
            response.addData('ok')
            data = json.dumps(response.getResult())
            return HttpResponse(data, content_type='application/json')

def nuevo(request):
    mensaje = ''
    userId = request.user.id
    usuario = Usuario.objects.filter(user_id = userId).exists()
    if request.method == 'POST':
        form = PreguntaForm(request.POST, request.FILES)
        if form.is_valid():
            existe_pregunta = Pregunta.objects.filter(detalle_pregunta=request.POST['detalle_pregunta']).exists()
            if existe_pregunta:
                mensaje = 'Existe la pregunta'
            else:
                pregunta = form.save(commit=False)
                pregunta.fecha = timezone.now()
                if usuario:
                    usu = Usuario.objects.get(user_id = userId)
                    pregunta.usuario_id = usu.id
                pregunta.save()
                return redirect('list')
    else:
        form = PreguntaForm()
    return render(request, 'pregunta/form.html', {'form': form, 'msg': mensaje})

def edit(request, id_pregunta):
    mensaje = ''
    pregunta = Pregunta.objects.get(id=id_pregunta)
    if request.method == 'GET':
        form = PreguntaForm(instance=pregunta)
    else :
        form = PreguntaForm(request.POST, instance=pregunta)
        if form.is_valid():
            count = Pregunta.objects.filter(Q(detalle_pregunta=request.POST['detalle_pregunta']) & ~Q(id = pregunta.id)).count()
            if count == 0:
                form.save()
                return redirect('list')
            else:
                mensaje = 'Ya existe la pregunta'
    return render(request, 'pregunta/form.html', {'form':form, 'msg': mensaje })

def nueva_respuesta(request, id_pregunta):
    mensaje = ''
    if request.method == 'POST':
        form = RespuestaForm(request.POST, request.FILES)
        if form.is_valid():
            respuesta = form.save(commit=False)
            respuesta.pregunta_id = id_pregunta
            respuesta.fecha = timezone.now()
            respuesta.save()
            return redirect('/pregunta/list_respuesta/'+str(id_pregunta))

    else:
        form = RespuestaForm()
    return render(request, 'pregunta/form_respuesta.html', {'form': form, 'msg': mensaje})

def list_respuesta(request, id_pregunta):
    respuesta = Respuesta.objects.filter(pregunta_id=id_pregunta).order_by('-correcta')
    query = request.GET.get("buscar")
    if query:
        respuesta = Respuesta.objects\
        .filter(Q(pregunta_id=id_pregunta) & Q(correcta__icontains=query))

    paginator = Paginator(respuesta, 10)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    contexto = {'page_obj': page_obj, 'page': page_number}
    return render(request, 'pregunta/list_respuesta.html', contexto)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
    example_worker_process

    Docs:
        https://docs.unmanic.app/docs/plugins/writing_plugins/plugin_runner_types#post-processor---file-movements

"""
import os

def add_path_to_unmanic_ignore_file(path):
    basename = os.path.basename(path)
    dirname = os.path.dirname(path)
    unmanic_ignore_file = os.path.join(dirname, '.unmanicignore')

    # If the file exists, check if the file basename is already in it
    exists_in_file = False
    if os.path.exists(unmanic_ignore_file):
        with open(unmanic_ignore_file) as f:
            for line in f:
                if basename in line:
                    exists_in_file = True

    # If the file does not yet exist or it does exists but the basename is not already appended:
    if not exists_in_file:
        # Append the basename to the file
        with open(unmanic_ignore_file, 'a') as outfile:
            outfile.write("{}\n".format(basename))

def on_postprocessor_file_movement(data):
    """
    Runner function - configures additional postprocessor file movements during the postprocessor stage of a task.

    The 'data' object argument includes:
        source_data             - Dictionary containing data pertaining to the original source file.
        remove_source_file      - Boolean, should Unmanic remove the original source file after all copy operations are complete.
        copy_file               - Boolean, should Unmanic run a copy operation with the returned data variables.
        file_in                 - The converted cache file to be copied by the postprocessor.
        file_out                - The destination file that the file will be copied to.

    :param data:
    :return:
    """

    # keep the original file. We are going to create a copy
    data['remove_source_file'] = False

    # Add an 'UNMANIC tag to the end of the file'
    tmp_file_out = os.path.splitext(data['file_out'])
    data['file_out'] = "{}-{}{}".format(tmp_file_out[0], 'UNMANIC', tmp_file_out[1])

    # Add original file's name to '.unmanicignore' file to prevent this directory being added again.
    if data['source_data'].get('abspath'):
        add_path_to_unmanic_ignore_file(data['source_data'].get('abspath'))

    return data


from collections import Counter
def common(str1, str2):
    dict1 = Counter(str1)
    dict2 = Counter(str2)

    common_dict = dict1 & dict2

    if len(common_dict) == 0:
        return (-1)

    commonChars = list(common_dict.elements())

    commonChars = sorted(commonChars)
    print("".join(commonChars))

if __name__ == '__main__':
    str1 = "geeks"
    str2 = "forgeeks"

    common(str1, str2)


# keys = ['Ten', 'Twenty', 'Thirty']
# values = [10, 20, 30]
# dict1 = {key: value for key, value in zip(keys, values)}
# print(dict1)
#
# sampleDict = dict(zip(keys, values))
#
#
# Merge following two Python dictionaries into one
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
dict1.update(dict2)
print(dict1)

dict3 = {**dict1, **dict2}
print(dict3)
#
#
# sampleDict = {
#    "class":{
#       "student":{
#          "name":"Mike",
#          "marks":{
#             "physics":70,
#             "history":80
#          }
#       }
#    }
# }
#
# print(sampleDict['class']['student']['marks']['history'])
# # remove key from dictionary
# sampleDict = {
#     "name": "Kelly",
#     "age": 25,
#     "salary": 8000,
#     "city": "New york"
#
# }
# keysToRemove = ["name", "salary"]
# for key in keysToRemove:
#     del sampleDict[key]

# print(sampleDict)

sampleDict = {'a': 100, 'b': 200, 'c': 300}
search = 200
if search in sampleDict.values():
    print(True)

# #change city key to location
# sampleDict = {
#   "name": "Kelly",
#   "age":25,
#   "salary": 8000,
#   "city": "New york"
# }
#
# sampleDict['location'] = sampleDict.pop('city')
# print(sampleDict)

sampleDict = {
    'Physics': 82,
    'Math': 65,
    'history': 75
}

print(min(sampleDict.keys()))

empDict = {
    'emp1': {'name': 'Jhon', 'salary': 7500},
    'emp2': {'name': 'Emma', 'salary': 8000},
    'emp3': {'name': 'Brad', 'salary': 6500}
}

empDict['emp3']['salary'] = 8500

print(empDict)


import csv
import os
import pickle

import shutil
import tqdm

import pandas as pd

data_path = '/data/zak/robot/labels'

# def get_labels(joint_filenames):
#     labels = []
#     for joint_path in joint_filenames:
#         with open(joint_path, 'rb') as joint_file:
#             vel = pickle.load(joint_file)['velocity']
#             front_avg_vel = (vel[2] + vel[3]) / 2
#             if min(vel) > 1:
#                 label = 'no_obstacle'
#             elif -0.1 < front_avg_vel < 0.1:
#                 label = 'obstacle'
#             else:
#                 label = 'unknown'
#         labels.append(label)
#     return labels

# def create_csv(img_filenames, joint_filenames, dataset_path):
#     labels = get_labels(joint_filenames)
#     path_labels = list(zip(img_filenames, labels))
#     print(f'Writing labels.csv to {dataset_path}')
#     with open(os.path.join(dataset_path, 'labels.csv'), 'w') as csv_file:
#         writer = csv.writer(csv_file)
#         writer.writerow(['img_path', 'label'])
#         writer.writerows(path_labels)

def get_direction(joint_path):
    with open(joint_path, 'rb') as f:
        values = pickle.load(f)
    velocities = values['velocity']
    BL, BR, FL, FR = velocities
    # Check stop condition
    if all([-0.5 < v < 0.5 for v in velocities]):
        return 'STOP'
    elif all([v > 2 for v in velocities]):
        return 'FORWARD'
    elif all([FL > FR, BL > BR]):
        return 'RIGHT'
    elif all([FL < FR, BL < BR]):
        return 'LEFT'
    else:
        return 'UNKNOWN'

for dataset in os.listdir(data_path):
    if '.ipynb_checkpoints' == dataset:
        continue
    info_csv_path = os.path.join(data_path, dataset, 'info.csv')

    # Read info.csv file that contains img_paths, joints_paths, for all jointLabeled == True
    info_df = pd.read_csv(info_csv_path)
    # Get all labeled_data
    labeled_data = info_df.loc[info_df['jointLabeled'] == True]
    # Drop 'Unnamed: 0' Column and 'label' column
    labeled_data = labeled_data.loc[:, ~labeled_data.columns.str.contains('^Unnamed')]

    # Get directions for labeling the data
    labeled_data['label'] = labeled_data.joints.apply(lambda joint_path: get_direction(joint_path))

    print('---------------------------------')
    print(f'For {dataset} Dataset:')
    print(labeled_data.label.value_counts())

    labeled_data.to_csv(os.path.join(data_path, dataset, 'labels_directions_ashish.csv'), index=False)


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

# Define here the models for your spider middleware
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html

from settings import USER_AGENTS
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from lxml import etree
from scrapy.contrib.downloadermiddleware.useragent import UserAgentMiddleware
from scrapy.contrib.downloadermiddleware.httpproxy import HttpProxyMiddleware
import signal
import random
import time
import requests
import base64
import scrapy

# User-Agent 下载中间件
class RandomUserAgent(object):
    def __init__(self):
        # 初始化User-Agent'
        self.user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) \
        Chrome/22.0.1207.1 Safari/537.1"

    def process_request(self, request, spider):
        # 随机选中User-Agent
        self.user_agent = random.choice(USER_AGENTS)
        request.headers.setdefault('User-Agent', self.user_agent)

# ip_port 下载中间件
class RandomProxy(object):
    def __init__(self):
        self.proxy_auth = ""
        self.proxy_api = "http://dps.kuaidaili.com/api/getdps/?orderid=958655825381063&num=50&ut=1&sep=3"
        # 返回的时代理ip:port列表
        self.proxy_list = requests.get(self.proxy_api).text.split()

    def process_request(self, request, spider):
        proxy = random.choice(self.proxy_list)
        base64_userpass = base64.b64encode(self.proxy_auth)
        request.meta['proxy'] = "http://" + proxy
        # if self.proxy_auth != None:
        request.headers['Proxy-Authorization'] = "Basic " + base64_userpass

# article ChromeDriver中间件
class ArticleChromeDriverMiddleware(object):
    def process_request(self, request, spider):
        if request.meta.has_key('ChromeDriver'):
            options = Options()
            options.set_headless(headless=True)
            driver = webdriver.Chrome(chrome_options=options)
            try:
                driver.get(request.url)
                # 睡眠1.5秒等待ChromeDriver加载资源
                time.sleep(1.5)
                body = driver.page_source
                url = driver.current_url
                # 无论是否在try还是except里return，finally都会执行
                return scrapy.http.HtmlResponse(url, body=body, request=request, encoding='utf-8')
            except Exception:
                return scrapy.http.HtmlResponse(request.url, body=body, request=request, encoding='utf-8')
            # driver.service.process.send_signal(signal.SIGTERM)
            finally:
                driver.quit()

# comment ChromeDriver中间件
class CommentChromeDriverMiddleware(object):
    def process_request(self, request, spider):
        if request.meta.has_key('ChromeDriver'):
            options = Options()
            options.set_headless(headless=True)
            driver = webdriver.Chrome(chrome_options=options)
            driver.get(request.url)
            time.sleep(0.5)
            selector = etree.HTML(driver.page_source)
            # 循环退出条件：当页面加载更多的a标签不存在时
            while selector.xpath("//div[@class='more']/a"):
                selector = etree.HTML(driver.page_source)
                # 一直点击加载更多
                driver.find_element_by_class_name("more").click()

            body = driver.page_source
            url = driver.current_url
            # 解决driver.quit出现错误
            # driver.service.process.send_signal(signal.SIGTERM)
            driver.quit()
            # 返回处理后请求响应
            return scrapy.http.HtmlResponse(url, body=body, request=request, encoding='utf-8')


#!/usr/bin/python
from product import Product

def get_string_input(question):
  result = ''
  while len(result) == 0:
    result = input(question)
  return result

def get_boolean_input(question):
  alts = []
  alts.append("y")
  alts.append("n")
  answer = ''
  while answer not in alts:
    answer = input(question)
  return answer == 'y'

def get_int_input(question):
  result = 0
  correct = False
  while not correct:
    try:
      result = int(input(question))
      if result >= 0:
        correct = True
    except ValueError:
      print("ERROR: Please provide a correct value!")
  return result

def get_float_input(question):
  result = 0
  correct = False
  while not correct:
    try:
      result = float(input(question))
      if result >= 0:
        correct = True
    except ValueError:
      print("ERROR: Please enter a correct value!")
  return result

def get_product_info(productname):
  product = ''
  price = get_float_input("What is the price? ")
  quantity = get_int_input("What is the quantity? ")
  product = Product(productname, price, quantity)
  return product


import csv
import collections
from data_display.utils.string_display import get_strings_from_cache

# use streams pre-emptively in case table sizes get very big
from django.http import StreamingHttpResponse

# Some copy-pasta from the django docs:
class Echo:
    """
        A dummy class used to fit the "writer" interface in csv.
    """
    def write(self, value):
        return value

def export_as_csv(model):
    """
    :param Django model:
    :return: csv file representation of the Django model, wrapped in an HTTP Response
    """
    rows = collections.deque(model.objects.all().values_list())
    rows.appendleft(
        get_strings_from_cache(model._meta.get_fields())
    )
    pseudo_buffer = Echo()
    writer = csv.writer(pseudo_buffer)
    response = StreamingHttpResponse((writer.writerow(row) for row in rows),
                                     content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
    return response