content
stringlengths 0
894k
| type
stringclasses 2
values |
---|---|
# Copyright 2021 Massachusetts Institute of Technology
#
# @file image_gallery.py
# @author W. Nicholas Greene
# @date 2020-07-02 23:44:46 (Thu)
import os
import argparse
def create_simple_gallery(image_dir, num_per_row=3, output_file="index.html", title="Image Gallery"):
"""Create a simple gallery with num_per_row images per row.
"""
# Grab all images.
images = []
for root, dirs, files in os.walk(image_dir):
for filename in sorted(files):
filename_full_path = os.path.join(root, filename)
rel_path = os.path.relpath(filename_full_path, image_dir)
if filename_full_path.endswith(".png") or filename_full_path.endswith(".jpg"):
images.append(rel_path)
images = sorted(images)
# Write html file.
html_file = os.path.join(image_dir, output_file)
with open(html_file, "w") as target:
target.write("<html><head><title>{}</title></head><body><center>\n".format(title))
for image in images:
image_str = "<a href={}><img src=\"{}\" style=\"float: left; width: {}%; image-rendering: pixelated\"></a>\n".format(image, image, 100.0 / num_per_row)
target.write(image_str)
target.write("</center></body></html>\n")
return
def create_training_gallery(image_dir, image_height_pix=256, output_file="index.html", title="Image Gallery", delim="_"):
"""Create a gallery where each rows shows the evolution of an image during training.
Assumes images are in the following format:
<image_id>_<epoch>_<step>.jpg
Epoch and step are optional, but if provided must be zero padded so sorting
will put them in the appropriate order.
"""
# Grab all images.
id_to_images = {}
for root, dirs, files in os.walk(image_dir):
for filename in sorted(files):
filename_full_path = os.path.join(root, filename)
rel_path = os.path.relpath(filename_full_path, image_dir)
if filename_full_path.endswith(".png") or filename_full_path.endswith(".jpg"):
tokens = os.path.splitext(os.path.basename(rel_path))[0].split(delim)
image_id = tokens[0]
if image_id not in id_to_images:
id_to_images[image_id] = []
id_to_images[image_id].append(rel_path)
for image_id, images in id_to_images.items():
id_to_images[image_id] = sorted(images, reverse=True)
# Write html file.
html_file = os.path.join(image_dir, output_file)
with open(html_file, "w") as target:
target.write("<html><head><title>{}</title></head><body>\n".format(title))
target.write("<table>\n")
for image_id, images in id_to_images.items():
target.write("<tr align=\"left\">\n")
for image in images:
image_str = "<td><a href={}><img src=\"{}\" style=\"height: {}; image-rendering: pixelated\"></a></td>\n".format(
image, image, image_height_pix)
target.write(image_str)
target.write("</tr>\n")
target.write("</table>\n")
target.write("</body></html>\n")
return
def main():
# Parse args.
parser = argparse.ArgumentParser(description="Create simple image gallery from a folder of images.")
parser.add_argument("image_dir", help="Path to image directory.")
parser.add_argument("--num_per_row", type=int, default=3, help="Number of images per row.")
parser.add_argument("--output_file", default="index.html", help="Output file name.")
parser.add_argument("--title", default="Image Gallery", help="Gallery name.")
args = parser.parse_args()
create_simple_gallery(args.image_dir, args.num_per_row, args.output_file, args.title)
return
if __name__ == '__main__':
main()
| python |
# %%
# Imports
import torch
import torch.nn as nn
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from torch.utils.data import Dataset, random_split
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
import torch.optim as optim
import codecs
import tqdm
# %%
# Setting random seed and device
SEED = 1
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
# %%
# Load data
train_df = pd.read_csv('data/task-2/train.csv')
test_df = pd.read_csv('data/task-2/dev.csv')
# %%
# Number of epochs
epochs = 20
# Proportion of training data for train compared to dev
train_proportion = 0.8
# %% md
#### Approach 1: Using pre-trained word representations (GLOVE)
# %%
# We define our training loop
def train(train_iter, dev_iter, model, number_epoch):
"""
Training loop for the model, which calls on eval to evaluate after each epoch
"""
print("Training model.")
for epoch in range(1, number_epoch + 1):
model.train()
epoch_loss = 0
epoch_correct = 0
no_observations = 0 # Observations used for training so far
for batch in train_iter:
feature, target = batch
feature, target = feature.to(device), target.to(device)
# for RNN:
model.batch_size = target.shape[0]
no_observations = no_observations + target.shape[0]
model.hidden = model.init_hidden()
predictions = model(feature).squeeze(1)
optimizer.zero_grad()
loss = loss_fn(predictions, target)
correct, __ = model_performance(np.argmax(predictions.detach().cpu().numpy(), axis=1),
target.detach().cpu().numpy())
loss.backward()
optimizer.step()
epoch_loss += loss.item() * target.shape[0]
epoch_correct += correct
valid_loss, valid_acc, __, __ = eval(dev_iter, model)
epoch_loss, epoch_acc = epoch_loss / no_observations, epoch_correct / no_observations
print(f'| Epoch: {epoch:02} | Train Loss: {epoch_loss:.2f} | Train Accuracy: {epoch_acc:.2f} | \
Val. Loss: {valid_loss:.2f} | Val. Accuracy: {valid_acc:.2f} |')
# %%
# We evaluate performance on our dev set
def eval(data_iter, model):
"""
Evaluating model performance on the dev set
"""
model.eval()
epoch_loss = 0
epoch_correct = 0
pred_all = []
trg_all = []
no_observations = 0
with torch.no_grad():
for batch in data_iter:
feature, target = batch
feature, target = feature.to(device), target.to(device)
# for RNN:
model.batch_size = target.shape[0]
no_observations = no_observations + target.shape[0]
model.hidden = model.init_hidden()
predictions = model(feature).squeeze(1)
loss = loss_fn(predictions, target)
# We get the mse
pred, trg = predictions.detach().cpu().numpy(), target.detach().cpu().numpy()
correct, __ = model_performance(np.argmax(pred, axis=1), trg)
epoch_loss += loss.item() * target.shape[0]
epoch_correct += correct
pred_all.extend(pred)
trg_all.extend(trg)
return epoch_loss / no_observations, epoch_correct / no_observations, np.array(pred_all), np.array(trg_all)
# %%
# How we print the model performance
def model_performance(output, target, print_output=False):
"""
Returns accuracy per batch, i.e. if you get 8/10 right, this returns 0.8, NOT 8
"""
correct_answers = (output == target)
correct = sum(correct_answers)
acc = np.true_divide(correct, len(output))
if print_output:
print(f'| Acc: {acc:.2f} ')
return correct, acc
# %%
# To create our vocab
def create_vocab(data):
"""
Creating a corpus of all the tokens used
"""
tokenized_corpus = [] # Let us put the tokenized corpus in a list
for sentence in data:
tokenized_sentence = []
for token in sentence.split(' '): # simplest split is
tokenized_sentence.append(token)
tokenized_corpus.append(tokenized_sentence)
# Create single list of all vocabulary
vocabulary = [] # Let us put all the tokens (mostly words) appearing in the vocabulary in a list
for sentence in tokenized_corpus:
for token in sentence:
if token not in vocabulary:
if True:
vocabulary.append(token)
return vocabulary, tokenized_corpus
# %%
# Used for collating our observations into minibatches:
def collate_fn_padd(batch):
'''
We add padding to our minibatches and create tensors for our model
'''
batch_labels = [l for f, l in batch]
batch_features = [f for f, l in batch]
batch_features_len = [len(f) for f, l in batch]
seq_tensor = torch.zeros((len(batch), max(batch_features_len))).long()
for idx, (seq, seqlen) in enumerate(zip(batch_features, batch_features_len)):
seq_tensor[idx, :seqlen] = torch.LongTensor(seq)
batch_labels = torch.LongTensor(batch_labels)
return seq_tensor, batch_labels
# We create a Dataset so we can create minibatches
class Task2Dataset(Dataset):
def __init__(self, train_data, labels):
self.x_train = train_data
self.y_train = labels
def __len__(self):
return len(self.y_train)
def __getitem__(self, item):
return self.x_train[item], self.y_train[item]
# %%
class BiLSTM_classification(nn.Module):
def __init__(self, embedding_dim, hidden_dim, vocab_size, batch_size, device):
super(BiLSTM_classification, self).__init__()
self.hidden_dim = hidden_dim
self.embedding_dim = embedding_dim
self.device = device
self.batch_size = batch_size
self.embedding = nn.Embedding(vocab_size, embedding_dim, padding_idx=0)
# The LSTM takes word embeddings as inputs, and outputs hidden states
# with dimensionality hidden_dim.
self.lstm = nn.LSTM(embedding_dim, hidden_dim, bidirectional=True)
# The linear layer that maps from hidden state space to tag space
self.hidden2label = nn.Linear(hidden_dim * 2, 3)
self.hidden = self.init_hidden()
def init_hidden(self):
# Before we've done anything, we dont have any hidden state.
# Refer to the Pytorch documentation to see exactly why they have this dimensionality.
# The axes semantics are (num_layers * num_directions, minibatch_size, hidden_dim)
return torch.zeros(2, self.batch_size, self.hidden_dim).to(self.device), \
torch.zeros(2, self.batch_size, self.hidden_dim).to(self.device)
def forward(self, sentence):
embedded = self.embedding(sentence)
embedded = embedded.permute(1, 0, 2)
lstm_out, self.hidden = self.lstm(
embedded.view(len(embedded), self.batch_size, self.embedding_dim), self.hidden)
out = self.hidden2label(lstm_out[-1])
return out
# %%
## Approach 1 code, using functions defined above:
# We set our training data and test data
training_data = train_df['original1']
test_data = test_df['original1']
##### Preproceccing the data
train_df['original1']
# Creating word vectors
training_vocab, training_tokenized_corpus = create_vocab(training_data)
test_vocab, test_tokenized_corpus = create_vocab(test_data)
# Creating joint vocab from test and train:
joint_vocab, joint_tokenized_corpus = create_vocab(pd.concat([training_data, test_data]))
print("Vocab created.")
# We create representations for our tokens
wvecs = [] # word vectors
word2idx = [] # word2index
idx2word = []
# This is a large file, it will take a while to load in the memory!
with codecs.open('glove.6B.100d.txt', 'r', 'utf-8') as f:
# Change
index = 0
for line in f.readlines():
# Ignore the first line - first line typically contains vocab, dimensionality
if len(line.strip().split()) > 3:
word = line.strip().split()[0]
if word in joint_vocab:
(word, vec) = (word, list(map(float, line.strip().split()[1:])))
wvecs.append(vec)
word2idx.append((word, index))
idx2word.append((index, word))
index += 1
wvecs = np.array(wvecs)
word2idx = dict(word2idx)
idx2word = dict(idx2word)
vectorized_seqs = [[word2idx[tok] for tok in seq if tok in word2idx] for seq in training_tokenized_corpus]
INPUT_DIM = len(word2idx)
EMBEDDING_DIM = 100
BATCH_SIZE = 32
model = BiLSTM_classification(EMBEDDING_DIM, 50, INPUT_DIM, BATCH_SIZE, device)
print("Model initialised.")
model.to(device)
# We provide the model with our embeddings
model.embedding.weight.data.copy_(torch.from_numpy(wvecs))
feature = vectorized_seqs
# 'feature' is a list of lists, each containing embedding IDs for word tokens
train_and_dev = Task2Dataset(feature, train_df['label'])
train_examples = round(len(train_and_dev) * train_proportion)
dev_examples = len(train_and_dev) - train_examples
train_dataset, dev_dataset = random_split(train_and_dev,
(train_examples,
dev_examples))
train_loader = torch.utils.data.DataLoader(train_dataset, shuffle=True, batch_size=BATCH_SIZE,
collate_fn=collate_fn_padd)
dev_loader = torch.utils.data.DataLoader(dev_dataset, batch_size=BATCH_SIZE, collate_fn=collate_fn_padd)
print("Dataloaders created.")
loss_fn = nn.CrossEntropyLoss()
loss_fn = loss_fn.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
# %%
epochs = 10
train(train_loader, dev_loader, model, epochs)
# %%
#
# training_data, dev_data, training_y, dev_y = train_test_split(train_df['edit1'], train_df['label'],
# test_size=(1-train_proportion),
# random_state=42)
#
#
# test_loader = torch.utils.data.DataLoader(dev_dataset, batch_size=len(dev_dataset.dataset.y_train), collate_fn=collate_fn_padd)
#
# for batch in test_loader:
# batch_feature, batch_targets = batch
# batch_feature, batch_targets = batch_feature.to(device), batch_targets.to(device)
# model.batch_size = batch_targets.shape[0]
# batch_pred = model(batch_feature)
# batch_correct = model_performance(torch.tensor(np.argmax(batch_pred.detach().cpu().numpy(), axis=1)),
# batch_targets.detach().cpu(), True)
#
# pred = model(test_features)
training_data, dev_data, training_y, dev_y = train_test_split(train_df['edit1'], train_df['label'],
test_size=(1 - train_proportion),
random_state=42)
pred_baseline = torch.zeros(len(dev_y)) + 1 # 1 is most common class
print("\nBaseline performance:")
sse, mse = model_performance(pred_baseline, torch.tensor(dev_y.values), True)
# %%
def score_task_2(truth_loc, prediction_loc):
truth = pd.read_csv(truth_loc, usecols=['id', 'label'])
pred = pd.read_csv(prediction_loc, usecols=['id', 'pred'])
assert (sorted(truth.id) == sorted(pred.id)), "ID mismatch between ground truth and prediction!"
data = pd.merge(truth, pred)
data = data[data.label != 0]
accuracy = np.sum(data.label == data.pred) * 1.0 / len(data)
print("Accuracy = %.3f" % accuracy)
def predict(data_iter, model):
"""
Predict and return result
"""
model.eval()
epoch_loss = 0
epoch_correct = 0
pred_all = []
trg_all = []
no_observations = 0
with torch.no_grad():
for batch in data_iter:
feature, target = batch
feature, target = feature.to(device), target.to(device)
# for RNN:
model.batch_size = target.shape[0]
no_observations = no_observations + target.shape[0]
model.hidden = model.init_hidden()
predictions = model(feature).squeeze(1)
loss = loss_fn(predictions, target)
# We get the mse
pred, trg = predictions.detach().cpu().numpy(), target.detach().cpu().numpy()
correct, __ = model_performance(np.argmax(pred, axis=1), trg)
epoch_loss += loss.item() * target.shape[0]
epoch_correct += correct
pred_all.extend(pred)
trg_all.extend(trg)
return pred_all
#
test_vectorized_seqs = [[word2idx[tok] for tok in seq if tok in word2idx] for seq in test_tokenized_corpus]
outtest_dataset = Task2Dataset(test_vectorized_seqs, test_df['label'])
test_dataset, __ = random_split(test_dataset, (len(test_dataset), 0))
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=BATCH_SIZE, collate_fn=collate_fn_padd)
loss, accu, __, __ = eval(test_loader, model)
print("LOSS: {}, ACCURACY: {}".format(loss, accu))
| python |
"""
Ejercicio 02
Escriba un algoritmo, que dado como dato el sueldo de un trabajador, le aplique un aumento del 15% si su salario bruto es inferior
a $900.000 COP y 12% en caso contrario. Imprima el nuevo sueldo del trabajador.
Entradas
Sueldo_Bruto --> Float --> S_B
Salidas
Sueldo_Neto --> Float --> S_N
"""
# Instrucciones al usuario
print("Este programa le permitira determinar el sueldo neto de un trabajador aplicando un aumento")
# Entradas
S_B = float(input(f"Digite su salario bruto: "))
# Caja Negra
if(S_B < 900000):
S_N = S_B*0.15+S_B
else:
S_N = S_B*0.12+S_B
# Salidas
print(f"Su salario neto es de: ${S_N} COP")
| python |
import os
import shutil
import cv2
import numpy as np
import pandas as pd
from self_driving_car.augmentation import HorizontalFlipImageDataAugmenter
IMAGE_WIDTH, IMAGE_HEIGHT = 64, 64
CROP_TOP, CROP_BOTTOM = 30, 25
class DatasetHandler(object):
COLUMNS = ('center', 'left', 'right', 'steering_angle', 'speed',
'throttle', 'brake')
TRANSFORMED_COLUMNS = ('pov', 'path', 'steering_angle')
@classmethod
def read(cls, *paths, transform=True):
dataset = pd.concat(pd.read_csv(p, header=None, names=cls.COLUMNS)
for p in paths)
if transform:
dataset = pd.melt(dataset, id_vars=['steering_angle'],
value_vars=['center', 'left', 'right'],
var_name='pov', value_name='path')
return dataset
@classmethod
def write(cls, df, path, transformed=True):
cols = cls.TRANSFORMED_COLUMNS if transformed else cls.COLUMNS
df.to_csv(path, index=False, header=False, columns=cols)
class DatasetPreprocessor(object):
@classmethod
def strip_straight(cls, input_csv_path, output_path,
straight_threshold=0.1):
dataset = DatasetHandler.read(input_csv_path, transform=False)
dataset = dataset[dataset.steering_angle.abs() > straight_threshold]
dataset = cls._copy_images(dataset, output_path)
DatasetHandler.write(
dataset, os.path.join(output_path, 'driving_log.csv'),
transformed=False
)
return dataset
@classmethod
def _copy_images(cls, dataset, output_path):
def build_target_path(orig_path):
return os.path.join(
output_path, 'IMG', os.path.split(orig_path)[1])
def copy_images(row):
shutil.copy(row.center, row.center_target_path)
shutil.copy(row.left, row.left_target_path)
shutil.copy(row.right, row.right_target_path)
os.makedirs(os.path.join(output_path, 'IMG'))
extra_cols = ('center_target_path',
'left_target_path',
'right_target_path')
dataset = dataset.apply(
lambda r: pd.Series(
[r.center, r.left, r.right, r.steering_angle, r.speed,
r.throttle, r.brake, build_target_path(r.center),
build_target_path(r.left), build_target_path(r.right)],
index=DatasetHandler.COLUMNS + extra_cols), axis=1
)
dataset.apply(copy_images, axis=1)
dataset['center'] = dataset['center_target_path']
dataset['left'] = dataset['left_target_path']
dataset['right'] = dataset['right_target_path']
return dataset[list(DatasetHandler.COLUMNS)]
class DatasetGenerator(object):
def __init__(self, training_set, test_set, image_data_augmenters,
steering_correction=None):
self._training_set = training_set
self._test_set = test_set
self._augmenters = image_data_augmenters
if steering_correction:
steer_corr = {
'left': abs(steering_correction),
'center': 0,
'right': -abs(steering_correction)
}
else:
steer_corr = None
self._steering_correction = steer_corr
@classmethod
def from_csv(cls, image_data_augmenters, *csv_paths, test_size=0.25,
use_center_only=False, steering_correction=None):
dataset = DatasetHandler.read(*csv_paths)
center_only = dataset[dataset.pov == 'center']
not_center_only = dataset[dataset.pov != 'center']
test_set = center_only.sample(frac=test_size)
training_set = center_only.iloc[~center_only.index.isin(
test_set.index)]
if not use_center_only:
training_set = pd.concat([training_set, not_center_only])
return cls(training_set, test_set, image_data_augmenters,
steering_correction=steering_correction)
@classmethod
def shuffle_dataset(cls, dataset):
return dataset.sample(frac=1).reset_index(drop=True)
@property
def training_set(self):
return self._training_set
@property
def test_set(self):
return self._test_set
def training_set_batch_generator(self, batch_size):
yield from self._dataset_batch_generator(
self._training_set, batch_size)
def test_set_batch_generator(self, batch_size):
yield from self._dataset_batch_generator(
self._test_set, batch_size)
def _dataset_batch_generator(self, dataset, batch_size):
i = 0
batch_images = np.empty([batch_size, IMAGE_HEIGHT, IMAGE_WIDTH, 3],
dtype=np.uint8)
batch_steerings = np.empty(batch_size)
while True:
for image, steering_angle in self._flow(
self.shuffle_dataset(dataset)):
batch_images[i] = image
batch_steerings[i] = steering_angle
i += 1
if i == batch_size:
yield batch_images, batch_steerings
i = 0
def _flow(self, dataset):
for _, row in dataset.iterrows():
yield self._flow_from_row(row)
def _flow_from_row(self, row):
image = preprocess_image_from_path(row['path'])
steering_angle = row['steering_angle']
if self._steering_correction:
steering_angle += self._steering_correction[row['pov']]
for aug in self._augmenters:
image, steering_angle = self._augment(
aug, image, steering_angle)
return image, steering_angle
def _augment(self, augmenter, image, steering_angle):
augmented_image = augmenter.process_random(image)
if isinstance(augmenter, HorizontalFlipImageDataAugmenter):
steering_angle = -steering_angle
return augmented_image, steering_angle
def preprocess_image_from_path(image_path):
image = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
return preprocess_image(image)
def preprocess_image(image):
# Crop from bottom to remove car parts
# Crop from top to remove part of the sky
cropped_image = image[CROP_TOP:-CROP_BOTTOM, :]
return cv2.resize(cropped_image, (IMAGE_WIDTH, IMAGE_HEIGHT),
interpolation=cv2.INTER_AREA)
| python |
# Copyright (c) 2021, Rutwik Hiwalkar and Contributors
# See license.txt
# import frappe
import unittest
class TestQMailScheduleRule(unittest.TestCase):
pass
| python |
#!/usr/bin/python
#eval p@20
import sys
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'Usage:[pred]'
exit(0)
fi = open(sys.argv[1],'r')
K = 20
#precision@k
P = 0
unum = 943
rank = 0
for line in fi:
rank = int(line.strip())
if rank < K:
P += 1
P/= float(unum*K)
print 'Pre@%d:%.4f\n' %(K,P)
fi.close()
| python |
# Copyright (c) Facebook, Inc. and its affiliates.
import random
from typing import Optional, Tuple
import torch
from torch.nn import functional as F
from detectron2.config import CfgNode
from detectron2.structures import Instances
from densepose.converters.base import IntTupleBox
from .densepose_cse_base import DensePoseCSEBaseSampler
class DensePoseCSEConfidenceBasedSampler(DensePoseCSEBaseSampler):
"""
Samples DensePose data from DensePose predictions.
Samples for each class are drawn using confidence value estimates.
"""
def __init__(
self,
cfg: CfgNode,
use_gt_categories: bool,
embedder: torch.nn.Module,
confidence_channel: str,
count_per_class: int = 8,
search_count_multiplier: Optional[float] = None,
search_proportion: Optional[float] = None,
):
"""
Constructor
Args:
cfg (CfgNode): the config of the model
embedder (torch.nn.Module): necessary to compute mesh vertex embeddings
confidence_channel (str): confidence channel to use for sampling;
possible values:
"coarse_segm_confidence": confidences for coarse segmentation
(default: "coarse_segm_confidence")
count_per_class (int): the sampler produces at most `count_per_class`
samples for each category (default: 8)
search_count_multiplier (float or None): if not None, the total number
of the most confident estimates of a given class to consider is
defined as `min(search_count_multiplier * count_per_class, N)`,
where `N` is the total number of estimates of the class; cannot be
specified together with `search_proportion` (default: None)
search_proportion (float or None): if not None, the total number of the
of the most confident estimates of a given class to consider is
defined as `min(max(search_proportion * N, count_per_class), N)`,
where `N` is the total number of estimates of the class; cannot be
specified together with `search_count_multiplier` (default: None)
"""
super().__init__(cfg, use_gt_categories, embedder, count_per_class)
self.confidence_channel = confidence_channel
self.search_count_multiplier = search_count_multiplier
self.search_proportion = search_proportion
assert (search_count_multiplier is None) or (search_proportion is None), (
f"Cannot specify both search_count_multiplier (={search_count_multiplier})"
f"and search_proportion (={search_proportion})"
)
def _produce_index_sample(self, values: torch.Tensor, count: int):
"""
Produce a sample of indices to select data based on confidences
Args:
values (torch.Tensor): a tensor of length k that contains confidences
k: number of points labeled with part_id
count (int): number of samples to produce, should be positive and <= k
Return:
list(int): indices of values (along axis 1) selected as a sample
"""
k = values.shape[1]
if k == count:
index_sample = list(range(k))
else:
# take the best count * search_count_multiplier pixels,
# sample from them uniformly
# (here best = smallest variance)
_, sorted_confidence_indices = torch.sort(values[0])
if self.search_count_multiplier is not None:
search_count = min(int(count * self.search_count_multiplier), k)
elif self.search_proportion is not None:
search_count = min(max(int(k * self.search_proportion), count), k)
else:
search_count = min(count, k)
sample_from_top = random.sample(range(search_count), count)
index_sample = sorted_confidence_indices[-search_count:][sample_from_top]
return index_sample
def _produce_mask_and_results(
self, instance: Instances, bbox_xywh: IntTupleBox
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""
Method to get labels and DensePose results from an instance
Args:
instance (Instances): an instance of
`DensePoseEmbeddingPredictorOutputWithConfidences`
bbox_xywh (IntTupleBox): the corresponding bounding box
Return:
mask (torch.Tensor): shape [H, W], DensePose segmentation mask
embeddings (Tuple[torch.Tensor]): a tensor of shape [D, H, W]
DensePose CSE Embeddings
other_values: a tensor of shape [1, H, W], DensePose CSE confidence
"""
_, _, w, h = bbox_xywh
densepose_output = instance.pred_densepose
mask, embeddings, _ = super()._produce_mask_and_results(instance, bbox_xywh)
other_values = F.interpolate(
getattr(densepose_output, self.confidence_channel),
# pyre-fixme[6]: Expected `Optional[int]` for 2nd param but got
# `Tuple[int, int]`.
size=(h, w),
mode="bilinear",
)[0].cpu()
return mask, embeddings, other_values
| python |
try:
raise KeyboardInterrupt
finally:
print('Goodbye, world!') | python |
from django.db import models
class WelcomePage(models.Model):
"""
Welcome page model
"""
content = models.CharField(max_length=2000)
| python |
import uuid
import json
from textwrap import dedent
import hashlib
from flask import Flask, jsonify, request
from blockchain import Blockchain
# Using Flask as API to communicate with Blockchain
app = Flask(__name__)
# Unique address for node
node_identifier = str(uuid.uuid4()).replace("-", "")
# instantiate Blockchain
blockchain = Blockchain()
# ADD routing addresses
@app.route("/mine", methods=["GET"])
def mine():
# Run proof of work algorithm
last_block = blockchain.last_block
last_proof = last_block["proof"]
proof = blockchain.proof_of_work(last_proof)
# We receive one coin when mined a new block
blockchain.new_transaction(
sender="0",
recipient=node_identifier,
amount=1,
)
# Forge the new block
previous_hash = blockchain.hash(last_block)
block = blockchain.new_block(proof, previous_hash)
response = {
"message": "New Block Forged",
"index": block["index"],
"transactions": block["transactions"],
"proof": block["proof"],
"previous_hash": block["previous_hash"],
}
return jsonify(response), 200
@app.route("/transactions/new", methods=["POST"])
def new_transaction():
values = request.get_json()
# check required fields that are POST to this function
required = ["sender", "recipient", "amount"]
if not all(elem in values for elem in required):
return "Missing values", 400
# New transaction is created
index = blockchain.new_transaction(
values["sender"],
values["recipient"],
values["amount"])
response = {"message": f"Transaction will be added to Block {index}"}
return jsonify(response), 201
@app.route("/chain", methods=["GET"])
def full_chain():
response = {
"chain": blockchain.chain,
"length": len(blockchain.chain),
}
return jsonify(response), 200
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5000) | python |
from pathlib import Path
import numpy as np
import pandas as pd
import pytest
from py_muvr import FeatureSelector
from py_muvr.data_structures import (
FeatureEvaluationResults,
FeatureRanks,
InputDataset,
OuterLoopResults,
)
ASSETS_DIR = Path(__file__).parent / "assets"
@pytest.fixture(scope="session")
def raw_results():
return [
[
OuterLoopResults(
min_eval=FeatureEvaluationResults(
test_score=4,
model="model",
ranks=FeatureRanks(features=[0, 1], ranks=[1, 2], n_feats=10),
),
max_eval=FeatureEvaluationResults(
test_score=5,
model="model",
ranks=FeatureRanks(
features=[0, 1, 2, 3], ranks=[1, 2, 4, 3], n_feats=10
),
),
mid_eval=FeatureEvaluationResults(
test_score=5,
model="model",
ranks=FeatureRanks(features=[0, 1, 3], ranks=[1, 2, 3], n_feats=10),
),
n_features_to_score_map={5: 4, 4: 3, 3: 3, 2: 3},
),
OuterLoopResults(
min_eval=FeatureEvaluationResults(
test_score=3,
model="model",
ranks=FeatureRanks(
features=[0, 1, 4, 3], ranks=[1, 2, 3, 4], n_feats=10
),
),
max_eval=FeatureEvaluationResults(
test_score=3,
model="model",
ranks=FeatureRanks(
features=[0, 1, 4, 3], ranks=[1, 2, 3, 4], n_feats=10
),
),
mid_eval=FeatureEvaluationResults(
test_score=2,
model="model",
ranks=FeatureRanks(
features=[0, 1, 4, 3], ranks=[1, 2, 3, 4], n_feats=10
),
),
n_features_to_score_map={5: 5, 4: 4, 3: 5, 2: 5},
),
],
[
OuterLoopResults(
min_eval=FeatureEvaluationResults(
test_score=4,
model="model",
ranks=FeatureRanks(features=[0, 1], ranks=[1, 2], n_feats=10),
),
max_eval=FeatureEvaluationResults(
test_score=5,
model="model",
ranks=FeatureRanks(
features=[0, 1, 4, 2], ranks=[1, 2, 3, 4], n_feats=10
),
),
mid_eval=FeatureEvaluationResults(
test_score=5,
model="model",
ranks=FeatureRanks(features=[0, 1, 4], ranks=[2, 1, 3], n_feats=10),
),
n_features_to_score_map={5: 5, 4: 3, 3: 5, 2: 3},
),
OuterLoopResults(
min_eval=FeatureEvaluationResults(
test_score=2,
model="model",
ranks=FeatureRanks(features=[0, 1], ranks=[1, 2], n_feats=10),
),
max_eval=FeatureEvaluationResults(
test_score=2,
model="model",
ranks=FeatureRanks(
features=[0, 1, 2, 3, 4], ranks=[1, 2, 5, 4, 3], n_feats=10
),
),
mid_eval=FeatureEvaluationResults(
test_score=2,
model="model",
ranks=FeatureRanks(features=[0, 1, 4], ranks=[1, 2, 3], n_feats=10),
),
n_features_to_score_map={5: 5, 4: 6, 3: 5, 2: 5},
),
],
]
@pytest.fixture
def inner_loop_results():
return [
FeatureEvaluationResults(
ranks=FeatureRanks(features=[1, 2, 3, 4], ranks=[3, 2, 1, 4]),
test_score=0.2,
model="estimator",
),
FeatureEvaluationResults(
ranks=FeatureRanks(features=[1, 2, 3, 4], ranks=[1.5, 1.5, 3, 4]),
test_score=0.2,
model="estimator",
),
]
@pytest.fixture
def inner_loop_results_2():
return [
FeatureEvaluationResults(
ranks=FeatureRanks(features=[2, 3, 4], ranks=[3, 2, 1]),
test_score=0.1,
model="model",
),
FeatureEvaluationResults(
ranks=FeatureRanks(features=[2, 3, 4], ranks=[1.5, 1.5, 3]),
test_score=0.5,
model="model",
),
]
@pytest.fixture
def inner_loop_results_3():
return [
FeatureEvaluationResults(
ranks=FeatureRanks(features=[2, 4], ranks=[3, 2, 1]),
test_score=0.3,
model="model",
),
FeatureEvaluationResults(
ranks=FeatureRanks(features=[2, 4], ranks=[1.5, 1.5, 3]),
test_score=0.25,
model="model",
),
]
@pytest.fixture
def rfe_raw_results(inner_loop_results, inner_loop_results_2, inner_loop_results_3):
return {
(1, 2, 3, 4): inner_loop_results,
(2, 3, 4): inner_loop_results_2,
(2, 4): inner_loop_results_3,
}
@pytest.fixture
def dataset():
X = np.random.rand(12, 12)
y = np.random.choice([0, 1], 12)
return InputDataset(X=X, y=y, groups=np.arange(12))
@pytest.fixture(scope="session")
def mosquito():
df = pd.read_csv(ASSETS_DIR / "mosquito.csv", index_col=0)
df = df.sample(frac=1)
X = df.drop(columns=["Yotu"]).values
y = df.Yotu.values
groups = df.index
return InputDataset(X=X, y=y, groups=groups)
@pytest.fixture(scope="session")
def freelive():
df = pd.read_csv(ASSETS_DIR / "freelive.csv", index_col=0)
X = df.drop(columns=["YR"]).values
y = df.YR.values
groups = df.index
return InputDataset(X=X, y=y, groups=groups)
@pytest.fixture(scope="session")
def fs_results(raw_results):
fs = FeatureSelector(n_outer=3, metric="MISS", estimator="RFC")
fs._raw_results = raw_results
fs.is_fit = True
fs._selected_features = fs._post_processor.select_features(raw_results)
fs._n_features = 5
fs_results = fs.get_feature_selection_results(["A", "B", "C", "D", "E"])
return fs_results
| python |
from djitellopy import Tello
tello=Tello()
tello.connect()
#tello.takeoff()
#
#move
#tello.move_up(100)
#tello.move_forward(50)
#tello.rotate_clockwise(90)
#tello.move_back(50)
#tello.move_up(50)
#
#tello.land()
import cv2
panel=cv2.imread('./DroneBlocks_TT.jpg')
cv2.imshow('tello panel', panel)
while True:
key=cv2.waitKey(1)
if key==ord('q'):
break
elif key==ord('t'):
tello.takeoff()
elif key==ord('l'):
tello.land()
elif key==ord('u'):
tello.move('up', 50)
elif key==ord('d'):
tello.move('down', 50)
elif key==ord('f'):
tello.move('forward', 50)
elif key==ord('b'):
tello.move('back', 50)
elif key==ord('c'):
tello.rotate_clockwise(90)
elif key==ord('w'):
tello.rotate_counter_clockwise(90)
pass | python |
import pytest
from lemur.auth.ldap import * # noqa
from mock import patch, MagicMock
class LdapPrincipalTester(LdapPrincipal):
def __init__(self, args):
super().__init__(args)
self.ldap_server = 'ldap://localhost'
def bind_test(self):
groups = [('user', {'memberOf': ['CN=Lemur Access,OU=Groups,DC=example,DC=com'.encode('utf-8'),
'CN=Pen Pushers,OU=Groups,DC=example,DC=com'.encode('utf-8')]})]
self.ldap_client = MagicMock()
self.ldap_client.search_s.return_value = groups
self._bind()
def authorize_test_groups_to_roles_admin(self):
self.ldap_groups = ''.join(['CN=Pen Pushers,OU=Groups,DC=example,DC=com',
'CN=Lemur Admins,OU=Groups,DC=example,DC=com',
'CN=Lemur Read Only,OU=Groups,DC=example,DC=com'])
self.ldap_required_group = None
self.ldap_groups_to_roles = {'Lemur Admins': 'admin', 'Lemur Read Only': 'read-only'}
return self._authorize()
def authorize_test_required_group(self, group):
self.ldap_groups = ''.join(['CN=Lemur Access,OU=Groups,DC=example,DC=com',
'CN=Pen Pushers,OU=Groups,DC=example,DC=com'])
self.ldap_required_group = group
return self._authorize()
@pytest.fixture()
def principal(session):
args = {'username': 'user', 'password': 'p4ssw0rd'}
yield LdapPrincipalTester(args)
class TestLdapPrincipal:
@patch('ldap.initialize')
def test_bind(self, app, principal):
self.test_ldap_user = principal
self.test_ldap_user.bind_test()
group = 'Pen Pushers'
assert group in self.test_ldap_user.ldap_groups
assert self.test_ldap_user.ldap_principal == '[email protected]'
def test_authorize_groups_to_roles_admin(self, app, principal):
self.test_ldap_user = principal
roles = self.test_ldap_user.authorize_test_groups_to_roles_admin()
assert any(x.name == "admin" for x in roles)
def test_authorize_required_group_missing(self, app, principal):
self.test_ldap_user = principal
roles = self.test_ldap_user.authorize_test_required_group('Not Allowed')
assert not roles
def test_authorize_required_group_access(self, session, principal):
self.test_ldap_user = principal
roles = self.test_ldap_user.authorize_test_required_group('Lemur Access')
assert len(roles) >= 1
assert any(x.name == "[email protected]" for x in roles)
| python |
import pyglet
from inspect import getargspec
from element import Element
from processor import Processor
from draw import labelsGroup
from utils import font
class Node(Element, Processor):
'''
Node is a main pyno element, in fact it is a function with in/outputs
'''
def __init__(self, x, y, batch, color=(200, 200, 200), code=None,
connects=None, size=(300, 150)):
Element.__init__(self, x, y, color, batch)
Processor.init_processor(self) # node has a processor for calculation
self.editor_size = size
if connects:
self.connected_to = connects
if code:
self.code = code
else:
self.code = '''def newNode(a=0, b=0):
result = a + b
return result'''
self.name = ''
self.label = pyglet.text.Label(self.name, font_name=font,
bold=True, font_size=11,
anchor_x='center', anchor_y='center',
batch=batch, group=labelsGroup,
color=(255, 255, 255, 230))
self.new_code(self.code)
def new_code(self, code):
# New code, search for in/outputs
self.code = code
def_pos = code.find('def')
if def_pos > -1:
inputs, outputs = self.inputs, self.outputs
bracket = code[def_pos:].find('(')
if bracket > -1:
self.name = code[def_pos + 3:def_pos + bracket].strip()
self.label.text = self.name
S, G = {}, {} # temporally stores and globals to exec function
try:
exec(code[def_pos:]) # dummy function to eject args names
except Exception as ex:
self.problem = True
self.er_label.text = repr(ex)
else:
# got tuple with args names like ('a', 'b')
inputs = tuple(getargspec(eval(self.name)).args)
ret_pos = code.rfind('return')
if ret_pos > -1:
outputs = tuple(x.strip()
for x in code[ret_pos + 6:].split(','))
self.w = max(len(self.name) * 10 + 20,
len(inputs) * 20, len(outputs) * 20, 64)
self.cw = self.w // 2
self.insert_inouts({'inputs': inputs,
'outputs': outputs})
def render_base(self):
Element.render_base(self)
self.label.x, self.label.y = self.x, self.y
def delete(self, fully=False):
Element.delete(self, fully)
self.label.delete()
| python |
from . import account
from . import balance
from . import bigmap
from . import block
from . import commitment
from . import contract
from . import cycle
from . import delegate
from . import head
from . import operation
from . import protocol
from . import quote
from . import reward
from . import right
from . import software
from . import statistics
from . import voting
| python |
# Copyright 2019 The Vitess Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Base environment for full cluster tests.
Contains functions that all environments should implement along with functions
common to all environments.
"""
# pylint: disable=unused-argument
import json
import random
from vttest import sharding_utils
class VitessEnvironmentError(Exception):
pass
class BaseEnvironment(object):
"""Base Environment."""
def __init__(self):
self.vtctl_helper = None
def create(self, **kwargs):
"""Create the environment.
Args:
**kwargs: kwargs parameterizing the environment.
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'create unsupported in this environment')
def use_named(self, instance_name):
"""Populate this instance based on a pre-existing environment.
Args:
instance_name: Name of the existing environment instance (string).
"""
self.master_capable_tablets = {}
for keyspace, num_shards in zip(self.keyspaces, self.num_shards):
self.master_capable_tablets[keyspace] = {}
for shard_name in sharding_utils.get_shard_names(num_shards):
raw_shard_tablets = self.vtctl_helper.execute_vtctl_command(
['ListShardTablets', '%s/%s' % (keyspace, shard_name)])
split_shard_tablets = [
t.split(' ') for t in raw_shard_tablets.split('\n') if t]
self.master_capable_tablets[keyspace][shard_name] = [
t[0] for t in split_shard_tablets
if (self.get_tablet_cell(t[0]) in self.primary_cells
and (t[3] == 'master' or t[3] == 'replica'))]
def destroy(self):
"""Teardown the environment.
Raises:
VitessEnvironmentError: Raised if unsupported
"""
raise VitessEnvironmentError(
'destroy unsupported in this environment')
def create_table(self, table_name, schema=None, validate_deadline_s=60):
schema = schema or (
'create table %s (id bigint auto_increment, msg varchar(64), '
'keyspace_id bigint(20) unsigned NOT NULL, primary key (id)) '
'Engine=InnoDB' % table_name)
for keyspace in self.keyspaces:
self.vtctl_helper.execute_vtctl_command(
['ApplySchema', '-sql', schema, keyspace])
def delete_table(self, table_name):
for keyspace in self.keyspaces:
self.vtctl_helper.execute_vtctl_command(
['ApplySchema', '-sql', 'drop table if exists %s' % table_name,
keyspace])
def get_vtgate_conn(self, cell):
"""Gets a connection to a vtgate in a particular cell.
Args:
cell: cell to obtain a vtgate connection from (string).
Returns:
A vtgate connection.
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'get_vtgate_conn unsupported in this environment')
def restart_mysql_task(self, tablet_name, task_name, is_alloc=False):
"""Restart a job within the mysql alloc or the whole alloc itself.
Args:
tablet_name: tablet associated with the mysql instance (string).
task_name: Name of specific task (droid, vttablet, mysql, etc.).
is_alloc: True to restart entire alloc.
Returns:
return restart return val.
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'restart_mysql_task unsupported in this environment')
def restart_vtgate(self, cell=None, task_num=None):
"""Restarts a vtgate task.
If cell and task_num are unspecified, restarts a random task in a random
cell.
Args:
cell: cell containing the vtgate task to restart (string).
task_num: which vtgate task to restart (int).
Returns:
return val for restart.
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'restart_vtgate unsupported in this environment')
def wait_for_good_failover_status(
self, keyspace, shard_name, failover_completion_timeout_s=60):
"""Wait until failover status shows complete.
Repeatedly queries the master tablet for failover status until it is 'OFF'.
Most of the time the failover status check will immediately pass. When a
failover is in progress, it tends to take a good 5 to 10 attempts before
status is 'OFF'.
Args:
keyspace: Name of the keyspace to reparent (string).
shard_name: name of the shard to verify (e.g. '-80') (string).
failover_completion_timeout_s: Failover completion timeout (int).
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'wait_for_good_failover_status unsupported in this environment')
def wait_for_healthy_tablets(self, deadline_s=300):
"""Wait until all tablets report healthy status.
Args:
deadline_s: Deadline timeout (seconds) (int).
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'wait_for_healthy_tablets unsupported in this environment')
def is_tablet_healthy(self, tablet_name):
vttablet_stream_health = json.loads(self.vtctl_helper.execute_vtctl_command(
['VtTabletStreamHealth', tablet_name]))
return 'health_error' not in vttablet_stream_health['realtime_stats']
def get_next_master(self, keyspace, shard_name, cross_cell=False):
"""Determine what instance to select as the next master.
If the next master is cross-cell, rotate the master cell and use instance 0
as the master. Otherwise, rotate the instance number.
Args:
keyspace: the name of the keyspace to reparent (string).
shard_name: name of the shard to reparent (string).
cross_cell: Whether the desired reparent is to another cell (bool).
Returns:
Tuple of cell, task num, tablet uid (string, int, string).
"""
num_tasks = self.keyspace_alias_to_num_instances_dict[keyspace]['replica']
current_master = self.get_current_master_name(keyspace, shard_name)
current_master_cell = self.get_tablet_cell(current_master)
next_master_cell = current_master_cell
next_master_task = 0
if cross_cell:
next_master_cell = self.primary_cells[(
self.primary_cells.index(current_master_cell) + 1) % len(
self.primary_cells)]
else:
next_master_task = (
(self.get_tablet_task_number(current_master) + 1) % num_tasks)
tablets_in_cell = [tablet for tablet in
self.master_capable_tablets[keyspace][shard_name]
if self.get_tablet_cell(tablet) == next_master_cell]
return (next_master_cell, next_master_task,
tablets_in_cell[next_master_task])
def get_tablet_task_number(self, tablet_name):
"""Gets a tablet's 0 based task number.
Args:
tablet_name: Name of the tablet (string).
Returns:
0 based task number (int).
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'get_tablet_task_number unsupported in this environment')
def external_reparent(self, keyspace, shard_name, new_master_name):
"""Perform a reparent through external means (Orchestrator, etc.).
Args:
keyspace: name of the keyspace to reparent (string).
shard_name: shard name (string).
new_master_name: tablet name of the tablet to become master (string).
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'external_reparent unsupported in this environment')
def internal_reparent_available(self):
"""Checks if the environment can do a vtctl reparent."""
return 'PlannedReparentShard' in (
self.vtctl_helper.execute_vtctl_command(['help']))
def automatic_reparent_available(self):
"""Checks if the environment can automatically reparent."""
return False
def explicit_external_reparent_available(self):
"""Checks if the environment can explicitly reparent via external tools."""
return False
def internal_reparent(self, keyspace, shard_name, new_master_name,
emergency=False):
"""Performs an internal reparent through vtctl.
Args:
keyspace: name of the keyspace to reparent (string).
shard_name: string representation of the shard to reparent (e.g. '-80').
new_master_name: Name of the new master tablet (string).
emergency: True to perform an emergency reparent (bool).
"""
reparent_type = (
'EmergencyReparentShard' if emergency else 'PlannedReparentShard')
self.vtctl_helper.execute_vtctl_command(
[reparent_type, '%s/%s' % (keyspace, shard_name), new_master_name])
self.vtctl_helper.execute_vtctl_command(['RebuildKeyspaceGraph', keyspace])
def get_current_master_cell(self, keyspace):
"""Obtains current master cell.
This gets the master cell for the first shard in the keyspace, and assumes
that all shards share the same master.
Args:
keyspace: name of the keyspace to get the master cell for (string).
Returns:
master cell name (string).
"""
num_shards = self.num_shards[self.keyspaces.index(keyspace)]
first_shard_name = sharding_utils.get_shard_name(0, num_shards)
first_shard_master_tablet = (
self.get_current_master_name(keyspace, first_shard_name))
return self.get_tablet_cell(first_shard_master_tablet)
def get_current_master_name(self, keyspace, shard_name):
"""Obtains current master's tablet name (cell-uid).
Args:
keyspace: name of the keyspace to get information on the master.
shard_name: string representation of the shard in question (e.g. '-80').
Returns:
master tablet name (cell-uid) (string).
"""
shard_info = json.loads(self.vtctl_helper.execute_vtctl_command(
['GetShard', '{0}/{1}'.format(keyspace, shard_name)]))
master_alias = shard_info['master_alias']
return '%s-%s' % (master_alias['cell'], master_alias['uid'])
def get_random_tablet(self, keyspace=None, shard_name=None, cell=None,
tablet_type=None, task_number=None):
"""Get a random tablet name.
Args:
keyspace: name of the keyspace to get information on the master.
shard_name: shard to select tablet from (None for random) (string).
cell: cell to select tablet from (None for random) (string).
tablet_type: type of tablet to select (None for random) (string).
task_number: a specific task number (None for random) (int).
Returns:
random tablet name (cell-uid) (string).
"""
keyspace = keyspace or random.choice(self.keyspaces)
shard_name = shard_name or (
sharding_utils.get_shard_name(
random.randint(0, self.shards[self.keyspaces.index(keyspace)])))
cell = cell or random.choice(self.cells)
tablets = [t.split(' ') for t in self.vtctl_helper.execute_vtctl_command(
['ListShardTablets', '%s/%s' % (keyspace, shard_name)]).split('\n')]
cell_tablets = [t for t in tablets if self.get_tablet_cell(t[0]) == cell]
if task_number:
return cell_tablets[task_number][0]
if tablet_type:
return random.choice([t[0] for t in cell_tablets if t[3] == tablet_type])
return random.choice(cell_tablets)[0]
def get_tablet_cell(self, tablet_name):
"""Get the cell of a tablet.
Args:
tablet_name: Name of the tablet, including cell prefix (string).
Returns:
Tablet's cell (string).
"""
return tablet_name.split('-')[0]
def get_tablet_uid(self, tablet_name):
"""Get the uid of a tablet.
Args:
tablet_name: Name of the tablet, including cell prefix (string).
Returns:
Tablet's uid (int).
"""
return int(tablet_name.split('-')[-1])
def get_tablet_keyspace(self, tablet_name):
"""Get the keyspace of a tablet.
Args:
tablet_name: Name of the tablet, including cell prefix (string).
Returns:
Tablet's keyspace (string).
"""
return json.loads(self.vtctl_helper.execute_vtctl_command(
['GetTablet', tablet_name]))['keyspace']
def get_tablet_shard(self, tablet_name):
"""Get the shard of a tablet.
Args:
tablet_name: Name of the tablet, including cell prefix (string).
Returns:
Tablet's shard (string).
"""
return json.loads(self.vtctl_helper.execute_vtctl_command(
['GetTablet', tablet_name]))['shard']
def get_tablet_type(self, tablet_name):
"""Get the current type of the tablet as reported via vtctl.
Args:
tablet_name: Name of the tablet, including cell prefix (string).
Returns:
Current tablet type (e.g. spare, replica, rdonly) (string).
"""
return json.loads(self.vtctl_helper.execute_vtctl_command(
['GetTablet', tablet_name]))['type']
def get_tablet_ip_port(self, tablet_name):
"""Get the ip and port of the tablet as reported via vtctl.
Args:
tablet_name: Name of the tablet, including cell prefix (string).
Returns:
ip:port (string).
"""
tablet_info = json.loads(self.vtctl_helper.execute_vtctl_command(
['GetTablet', tablet_name]))
host = tablet_info['hostname']
if ':' in host:
# If host is an IPv6 address we need to put it into square brackets to
# form a correct "host:port" value.
host = '[%s]' % host
return '%s:%s' % (host, tablet_info['port_map']['vt'])
def get_tablet_types_for_shard(self, keyspace, shard_name):
"""Get the types for all tablets in a shard.
Args:
keyspace: Name of keyspace to get tablet information on (string).
shard_name: single shard to obtain tablet types from (string).
Returns:
List of pairs of tablet's name and type.
"""
tablet_info = []
raw_tablets = self.vtctl_helper.execute_vtctl_command(
['ListShardTablets', '{0}/{1}'.format(keyspace, shard_name)])
raw_tablets = filter(None, raw_tablets.split('\n'))
for tablet in raw_tablets:
tablet_words = tablet.split()
tablet_name = tablet_words[0]
tablet_type = tablet_words[3]
tablet_info.append((tablet_name, tablet_type))
return tablet_info
def get_all_tablet_types(self, keyspace, num_shards):
"""Get the types for all tablets in a keyspace.
Args:
keyspace: Name of keyspace to get tablet information on (string).
num_shards: number of shards in the keyspace (int).
Returns:
List of pairs of tablet's name and type.
"""
tablet_info = []
for shard_name in sharding_utils.get_shard_names(num_shards):
tablet_info += self.get_tablet_types_for_shard(keyspace, shard_name)
return tablet_info
def backup(self, tablet_name):
"""Wait until all tablets report healthy status.
Args:
tablet_name: Name of tablet to backup (string).
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'backup unsupported in this environment')
def drain_tablet(self, tablet_name, duration_s=600):
"""Add a drain from a tablet.
Args:
tablet_name: vttablet to drain (string).
duration_s: how long to have the drain exist for, in seconds (int).
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'drain_tablet unsupported in this environment')
def is_tablet_drained(self, tablet_name):
"""Checks whether a tablet is drained.
Args:
tablet_name: vttablet to drain (string).
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'is_tablet_drained unsupported in this environment')
def undrain_tablet(self, tablet_name):
"""Remove a drain from a tablet.
Args:
tablet_name: vttablet name to undrain (string).
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'undrain_tablet unsupported in this environment')
def is_tablet_undrained(self, tablet_name):
"""Checks whether a tablet is undrained.
Args:
tablet_name: vttablet to undrain (string).
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'is_tablet_undrained unsupported in this environment')
def poll_for_varz(self, tablet_name, varz, timeout=60.0,
condition_fn=None, converter=str, condition_msg=None):
"""Polls for varz to exist, or match specific conditions, within a timeout.
Args:
tablet_name: the name of the process that we're trying to poll vars from.
varz: name of the vars to fetch from varz.
timeout: number of seconds that we should attempt to poll for.
condition_fn: a function that takes the var as input, and returns a truthy
value if it matches the success conditions.
converter: function to convert varz value.
condition_msg: string describing the conditions that we're polling for,
used for error messaging.
Returns:
dict of requested varz.
Raises:
VitessEnvironmentError: Raised if unsupported or if the varz conditions
aren't met within the given timeout.
"""
raise VitessEnvironmentError(
'poll_for_varz unsupported in this environment')
def truncate_usertable(self, keyspace, shard, table=None):
tablename = table or self.tablename
master_tablet = self.get_current_master_name(keyspace, shard)
self.vtctl_helper.execute_vtctl_command(
['ExecuteFetchAsDba', master_tablet, 'truncate %s' % tablename])
def get_tablet_query_total_count(self, tablet_name):
"""Gets the total query count of a specified tablet.
Args:
tablet_name: Name of the tablet to get query count from (string).
Returns:
Query total count (int).
Raises:
VitessEnvironmentError: Raised if unsupported.
"""
raise VitessEnvironmentError(
'get_tablet_query_total_count unsupported in this environment')
| python |
# -*- coding: utf-8 -*-
# @Author: Amar Prakash Pandey
# @Co-Author: Aman Garg
# @Date: 25-10-2016
# @Email: [email protected]
# @Github username: @amarlearning
# MIT License. You can find a copy of the License
# @http://amarlearning.mit-license.org
# import library here
import pygame
import time
import random
from os import path
# Pygame module initialised
pygame.init()
# Material color init
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
darkBlue = (0,0,128)
white = (255,255,255)
black = (0,0,0)
grey = (211,211,211)
# Frames per second
FPS = 6
# Display width and height are defined
display_width = 950
display_height = 700
# Folder path init
assets = path.join(path.dirname(__file__), 'assets/image')
extras = path.join(path.dirname(__file__), 'extras')
# Init images & sounds
gameIcon = pygame.image.load(path.join(assets + '/gameicon.png'))
grassRoad = pygame.image.load(path.join(assets + '/grassslip.png'))
stripOne = pygame.image.load(path.join(assets + '/stripone.png'))
stripTwo = pygame.image.load(path.join(assets + '/striptwo.png'))
coverImage = pygame.image.load(path.join(assets + '/cover.png'))
SmartCarImage = [pygame.image.load(path.join(assets + '/newcar0_opt.png')),
pygame.image.load(path.join(assets + '/newcar2_opt.png')),
pygame.image.load(path.join(assets + '/newcar3_opt.png'))]
RivalCarImage =pygame.image.load(path.join(assets + '/Black_viper_opt.png'))
Boom =pygame.image.load(path.join(assets + '/exp.png'))
GameOver =pygame.image.load(path.join(assets + '/gameover.png'))
# Game windown, caption initialised
gameDisplay = pygame.display.set_mode((display_width, display_height))
# Game icon init
pygame.display.set_caption('SmartCar')
pygame.display.set_icon(gameIcon)
# Clock init for Frames
clock = pygame.time.Clock()
# Fonts Init
smallfont = pygame.font.SysFont("comicsansms", 15)
mediumfont = pygame.font.SysFont("comicsansms", 40)
largefont = pygame.font.SysFont("comicsansms", 60)
# Engine sound added
pygame.mixer.music.load(path.join(extras, "engine_sound.mp3"))
pygame.mixer.music.play(-1)
# function to init all game assets!
def init():
grassSlip = 0
grass_width = 170
grass_height = 700
# Road and Greenland seperator
border_width = 30
border_height = 700
# Game basic design init [Left side] & [Right side]
gameDisplay.fill(black)
pygame.draw.rect(gameDisplay, grey, (grass_width, 0, border_width, border_height))
pygame.draw.rect(gameDisplay, grey, (display_width - grass_width - border_width, 0, border_width, border_height))
for x in range(0,12):
gameDisplay.blit(grassRoad, (0, grassSlip))
gameDisplay.blit(grassRoad, (780, grassSlip))
grassSlip = grassSlip + 63
# Road under maintainance, be safe!
gameDisplay.blit(stripOne, (380,0))
gameDisplay.blit(stripTwo, (560,0))
pygame.display.update()
# smart car image function
def carImage(x,y, which):
gameDisplay.blit(SmartCarImage[which], (x,y))
# rival car image function
def rivalcarImage(x,y):
gameDisplay.blit(RivalCarImage, (x,y))
def Kaboom(score):
init()
gameDisplay.blit(GameOver,(382,175))
pygame.draw.rect(gameDisplay, white, (200, 400, 550, 50))
text = smallfont.render("Press [RETURN] to continue and [Q] to quit", True, darkBlue)
gameDisplay.blit(text, [370,400])
text = smallfont.render("Score : " + str(score), True, red)
gameDisplay.blit(text, [450,420])
pygame.display.update()
gameExit = True
while gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
gameExit = False
gameloop()
if event.key == pygame.K_q:
pygame.quit()
def Score(score):
pygame.draw.rect(gameDisplay, green, (0,0, 170,45))
text = smallfont.render("Score : " + str(score), True, darkBlue)
gameDisplay.blit(text, [10,10])
def gameloop():
# All necessary variable initalised
init()
# Kickstart variable
gameplay = True
score = 0
# Grass 2D image & Road Divider
Divider = True
# Road's divider width and height
divider_width = 20
divider_height = 80
# carImage Position
carX = 225
carY = 560
# Rival car coordinates
rcarX= [225,415,605]
rcarY= 0
Ya=rcarY
Yb=-140
Yc=-280
# speed Factor
factor = 20
# car change variable
which_car = 0
# Picturising car image, sorry SmartCar image
carImage(carX,carY, which_car)
change_x = 0
rivalcarImage(rcarX[0],rcarY)
# Heart starts beating, Don't stop it!
while gameplay:
# Police siren activated :P
if which_car == 2:
which_car = 0
else:
which_car += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameplay = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
change_x = 190
if event.key == pygame.K_LEFT:
change_x = -190
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
change_x = 0
init()
# changing position of SmartCar
carX += change_x
if (carX<=700 and carX>=205):
carImage(carX, carY, which_car)
else:
carX -= change_x
carImage(carX, carY, which_car)
# controlling movements of traffic
if score > 10:
rivalcarImage(rcarX[0],Ya)
Ya += factor
if Ya > random.randint(1000, 2000):
Ya = 0
if score > 32:
rivalcarImage(rcarX[1],Yb)
Yb += factor
if Yb > random.randint(1000, 2000):
Yb=0
if score > 75:
rivalcarImage(rcarX[2],Yc)
Yc += factor
if Yc > random.randint(1700, 2000):
Yc=0
# car conflict avoiding condition
if (abs(Ya-Yb) < 280) or (abs(Yb-Yc) < 280):
Yb -= 350
# car crash condiiton!
if (carX == rcarX[0] and 470 < Ya <700) or (carX == rcarX[1] and 470 < Yb <700) or (carX == rcarX[2] and 470 < Yc <700):
gameDisplay.blit(Boom, (carX,530))
pygame.display.flip()
time.sleep(1)
Kaboom(score)
# Updating Score
Score(score)
score = score + 1
# Car moving visualization
if Divider == True:
gameDisplay.blit(stripTwo, (380, 0))
gameDisplay.blit(stripOne, (560, 0))
Divider = False
else:
gameDisplay.blit(stripOne, (380, 0))
gameDisplay.blit(stripTwo, (560, 0))
Divider = True
pygame.display.update()
# speed of game.
clock.tick(FPS)
# Game speed increases with increase in time.
if not score %1000:
factor += 10
# Kickstart the game!
gameloop()
# You will win, try one more time. Don't Quit.
pygame.quit()
# you can signoff now, everything looks good!
quit() | python |
from django.urls import path
from django_admin_sticky_notes.views import StickyNoteView
urlpatterns = [
path("", StickyNoteView.as_view()),
]
| python |
import os
import pickle
import cv2
if __name__ == '__main__':
folder = './rearrangement-train/color/000001-2.pkl'
with open(folder, 'rb') as f:
tmp = pickle.load(f)
for i in range(3):
img = tmp[i, 0, ...]
cv2.imshow('haha', img)
cv2.waitKey(0)
| python |
def numRescueBoats(people, limit):
boats = 0
people.sort()
left = 0, right = len(people)-1
while left <= right:
if left == right:
boats += 1
break
current = people[left]+people[right]
if current <= limit:
left += 1
boats += 1
right -= 1
return boats
| python |
from django.apps import AppConfig
from orchestra.core import accounts
class ContactsConfig(AppConfig):
name = 'orchestra.contrib.contacts'
verbose_name = 'Contacts'
def ready(self):
from .models import Contact
accounts.register(Contact, icon='contact_book.png')
| python |
from ... import UndirectedGraph
from unittest import TestCase, main
class TestEq(TestCase):
def test_eq(self) -> None:
edges = {
("a", "b"): 10,
("b", "c"): 20,
("l", "m"): 30
}
vertices = {
"a": 10,
"b": 20,
"z": 30
}
g = UndirectedGraph(edges=edges, vertices=vertices)
self.assertEqual(g, g.copy(), "Should test equality of graphs.")
def test_empty(self) -> None:
self.assertEqual(UndirectedGraph(), UndirectedGraph(), "Should compare empty graphs.")
def test_negative(self) -> None:
edges_one = {
("a", "b"): 10,
("b", "c"): 20,
("l", "m"): 30
}
edges_two = {
("a", "b"): 10,
("b", "c"): 20,
("l", "q"): 30
}
one = UndirectedGraph(edges=edges_one)
two = UndirectedGraph(edges=edges_two)
self.assertNotEqual(one, two, "Should check different graphs.")
vertices_one = {
"a": 2,
"b": 3
}
vertices_two = {
"a": 1,
"b": 3
}
one = UndirectedGraph(vertices=vertices_one)
two = UndirectedGraph(vertices=vertices_two)
self.assertNotEqual(one, two, "Should check different graphs.")
base = {
("a", "b"): 10,
("b", "c"): 20,
("l", "m"): 30
}
one = UndirectedGraph(edges=base)
two = one.copy()
two.set_vertex_weight("a", 10)
self.assertNotEqual(one, two, "Should check different graphs.")
if __name__ == "__main__":
main()
| python |
from setuptools import setup, Extension, find_packages
import os
import glob
sources = []
sources += glob.glob("src/*.cpp")
sources += glob.glob("src/*.pyx")
root_dir = os.path.abspath(os.path.dirname(__file__))
ext = Extension("factorizer",
sources = sources,
language = "c++",
extra_compile_args = ["-v", "-std=c++14", "-Wall", "-O3", "-lboost_system"],
extra_link_args = ["-std=c++14"]
)
with open(os.path.join(root_dir, 'README.md'), "r") as fp:
long_description = fp.read()
with open(os.path.join(root_dir, 'requirements.txt'), "r") as fp:
install_requires = fp.read().splitlines()
setup(
name = "factorizer",
version = "0.9.6",
author = "Fulltea",
author_email = "[email protected]",
long_description = long_description,
long_description_content_type="text/markdown",
url = "https://github.com/FullteaOfEEIC/factorizer",
packages = find_packages(where="src"),
package_dir = {
"factorizer": "src"
},
install_requires = install_requires,
ext_modules = [ext]
)
if os.path.exists(os.path.join("src", "factorizer.cpp")):
os.remove(os.path.join("src", "factorizer.cpp"))
| python |
import os
import datetime
try:
from PIL import Image, ImageOps
except ImportError:
import Image
import ImageOps
from ckeditor import settings as ck_settings
from .common import get_media_url
def get_available_name(name):
"""
Returns a filename that's free on the target storage system, and
available for new content to be written to.
"""
dir_name, file_name = os.path.split(name)
file_root, file_ext = os.path.splitext(file_name)
# If the filename already exists, keep adding an underscore (before the
# file extension, if one exists) to the filename until the generated
# filename doesn't exist.
while os.path.exists(name):
file_root += '_'
# file_ext includes the dot.
name = os.path.join(dir_name, file_root + file_ext)
return name
def get_thumb_filename(file_name):
"""
Generate thumb filename by adding _thumb to end of
filename before . (if present)
"""
return '%s_thumb%s' % os.path.splitext(file_name)
def create_thumbnail(filename):
image = Image.open(filename)
# Convert to RGB if necessary
# Thanks to Limodou on DjangoSnippets.org
# http://www.djangosnippets.org/snippets/20/
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
# scale and crop to thumbnail
imagefit = ImageOps.fit(image, ck_settings.THUMBNAIL_SIZE, Image.ANTIALIAS)
imagefit.save(get_thumb_filename(filename))
return get_media_url(filename)
def get_upload_filename(upload_name, user):
# If CKEDITOR_RESTRICT_BY_USER is True upload file to user specific path.
if ck_settings.RESTRICT_BY_USER:
user_path = user.username
else:
user_path = ''
# Generate date based path to put uploaded file.
date_path = datetime.datetime.now().strftime('%Y/%m/%d')
# Complete upload path (upload_path + date_path).
upload_path = os.path.join(ck_settings.UPLOAD_PATH, user_path, \
date_path)
# Make sure upload_path exists.
if not os.path.exists(upload_path):
os.makedirs(upload_path)
# Get available name and return.
return get_available_name(os.path.join(upload_path, upload_name))
| python |
# Generated by Django 1.11.23 on 2019-08-19 01:00
from django.conf import settings
from django.db import migrations
def set_site_domain_based_on_setting(apps, schema_editor):
Site = apps.get_model('sites', 'Site')
# The site domain is used to build URLs in some places, such as in password
# reset emails, and 'view on site' links in the admin site's blog post edit
# view. Thus, the domain should correspond to the domain actually being
# used by the current environment: production, staging, or development.
#
# Previously (migration 0001) we hardcoded the domain to
# 'coralnet.ucsd.edu'. Now we set the domain to the environment-dependent
# settings.SITE_DOMAIN.
#
# Note that Django doesn't seem to use this site domain in testing
# environments. Tests will always use a domain of 'testserver' or something
# like that, and the tests should 'just work' that way.
site = Site.objects.get(pk=settings.SITE_ID)
site.domain = settings.SITE_DOMAIN
site.save()
class Migration(migrations.Migration):
dependencies = [
('lib', '0001_set_site_name'),
]
# Reverse operation is a no-op. The forward operation doesn't care if the
# domain is already set correctly.
operations = [
migrations.RunPython(
set_site_domain_based_on_setting, migrations.RunPython.noop),
]
| python |
#!/usr/bin/env python
import csv
import re
from rdkit.Chem import AllChem
from rdkit import Chem
from rdkit import DataStructs
compounds = {}
def load_compounds(filename):
comps = {}
bad_count = 0
blank_count = 0
with open(filename) as csv_file:
csvr = csv.DictReader(csv_file, delimiter='\t')
for row in csvr:
id, inchi = (row['id'], row['structure'])
if inchi:
# print( "input row {0} {1}".format( id, inchi ) )
try:
smarts = Chem.MolToSmarts(Chem.MolFromInchi(inchi))
comps[id] = smarts
# print( "output row {0} {1} {2}".format( id, inchi, smarts ) )
except Exception:
# print( "input row {0} {1}".format( id, inchi ) )
# print( "bizarre", sys.exc_info()[0] )
bad_count = bad_count + 1
else:
comps[id] = ""
blank_count = blank_count + 1
print("# bad inputs count: {0}".format(bad_count))
print("# blank inputs count: {0}".format(blank_count))
return(comps)
def comp_lookup(comp_id):
return(compounds.get(comp_id))
def load_reactions(filename):
rxns = {}
diff_fps = {}
obsolete_count = 0
with open(filename) as csv_file:
csvr = csv.DictReader(csv_file, delimiter='\t')
# for each reaction
for row in csvr:
rxn_id, stoich, is_obsolete = (row['id'], row['stoichiometry'], row['is_obsolete'])
if int(is_obsolete) > 0:
obsolete_count = obsolete_count+1
continue
# print( "{0} {1}".format( id, stoich) )
if stoich: # for now, skip blank stoichiometries (if any)
left_side_compounds = []
right_side_compounds = []
smarts = None
for cstruct in stoich.split(';'):
# print( " cstruct: {0}".format( cstruct ) )
n, compid, state, x, name = re.findall(r'(?:[^:"]|"(?:\\.|[^"])*")+', cstruct)
# print( " {0}: {1} {2} {3} {4}".format( cstruct, n, compid, state, name ) )
smarts = comp_lookup(compid)
if not smarts or (smarts == ""):
smarts = None
break
copies = int(abs(float(n)))
if copies == 0:
copies = copies + 1
if float(n) < 0:
for i in range(0, copies):
left_side_compounds.append(smarts)
else:
for i in range(0, copies):
right_side_compounds.append(smarts)
if smarts is not None:
# print( "left" )
# pprint( left_side_compounds )
# for s in left_side_compounds:
# print( s )
# print( "right" )
# pprint( right_side_compounds )
# for s in right_side_compounds:
# print( s )
rxn_string = ".".join(left_side_compounds) + ">>" + \
".".join(right_side_compounds)
# print( "rxn string {0}".format( rxn_string ) )
fingerprint = AllChem.CreateStructuralFingerprintForReaction(AllChem.ReactionFromSmarts(rxn_string))
# pprint( fingerprint )
# pprint( dir( fingerprint ) )
# pprint( fingerprint.GetNumBits() )
# pprint( fingerprint.ToBinary() )
diff_fingerprint = AllChem.CreateDifferenceFingerprintForReaction(
AllChem.ReactionFromSmarts(rxn_string))
# print( "diff_fingerprint is " )
# pprint( diff_fingerprint )
# pprint( dir( diff_fingerprint ) )
# pprint( diff_fingerprint.GetLength() )
# pprint( diff_fingerprint.GetNonzeroElements() )
# b = diff_fingerprint.ToBinary()
# print( type(b) )
# pprint( b )
rxns[rxn_id] = fingerprint
diff_fps[rxn_id] = diff_fingerprint
print("# obsolete_count = {0}".format(obsolete_count))
return(rxns, diff_fps)
# First load compounds and convert to SMARTS and put in table
# compounds = load_compounds( "compounds.tsv" )
compounds = load_compounds("new_compounds.tsv")
# pprint( compounds )
# Next, load reactions, capture reaction strings and replace compound ids with SMARTS
reactions, diffs = load_reactions("reactions.tsv")
rxn_list = list(reactions.keys()) # list() required for python 3
num_rxns = len(rxn_list)
# num_rxns = 10000
for i in range(0, num_rxns-1):
for j in range(i+1, num_rxns):
rxn_a = rxn_list[i]
rxn_b = rxn_list[j]
print("{0} {1} {2} {3}".format(rxn_a, rxn_b,
DataStructs.FingerprintSimilarity(reactions[rxn_a],
reactions[rxn_b]),
DataStructs.cDataStructs.TanimotoSimilarity(diffs[rxn_a],
diffs[rxn_b])
))
| python |
# Generated by Django 2.0.1 on 2018-01-23 11:13
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('pages', '0013_auto_20170829_0515'),
]
operations = [
migrations.AlterField(
model_name='page',
name='ad_section',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='pages_page_related', to='ads.AdSection', verbose_name='Ads'),
),
migrations.AlterField(
model_name='page',
name='module',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='pages_page_related', to='pages.PageModule', verbose_name='Module'),
),
]
| python |
from metaflow import resources
from metaflow.api import FlowSpec, step
class ResourcesFlow(FlowSpec):
@resources(memory=1_000)
@step
def one(self):
self.a = 111
@resources(memory=2_000)
@step
def two(self):
self.b = self.a * 2
class ResourcesFlow2(ResourcesFlow):
pass
| python |
import struct
from slmkiii.template.input.button import Button
class PadHit(Button):
def __init__(self, data=None):
super(PadHit, self).__init__(data)
self.max_velocity = self.data(28)
self.min_velocity = self.data(29)
self.range_method = self.data(30)
def from_dict(self, data):
super(PadHit, self).from_dict(data, extend=True)
self._data += struct.pack(
'>HBBB',
0,
data['max_velocity'],
data['min_velocity'],
data['range_method'],
)
self._data = self._data.ljust(self.length, '\0')
def export_dict(self):
data = super(PadHit, self).export_dict()
data.update({
'max_velocity': self.max_velocity,
'min_velocity': self.min_velocity,
'range_method': self.range_method,
'range_method_name': self.range_method_name,
})
return data
@property
def range_method_name(self):
method_names = {
0: 'None',
1: 'Clip',
2: 'Scale',
}
return method_names[self.data(30)]
| python |
"""Implementation of the MCTS algorithm for Tic Tac Toe Game."""
from typing import List
from typing import Optional
from typing import Tuple
import numpy as np
import numpy.typing as npt
from mctspy.games.common import TwoPlayersAbstractGameState
from mctspy.tree.nodes import TwoPlayersGameMonteCarloTreeSearchNode
from mctspy.tree.search import MonteCarloTreeSearch
class Move:
"""Move class."""
def __init__(self, x_coordinate: int, y_coordinate: int, value: float) -> None:
"""Inits."""
self.x_coordinate = x_coordinate
self.y_coordinate = y_coordinate
self.value = value
def __repr__(self) -> str:
"""Repr."""
return f"x:{self.x_coordinate} y:{self.y_coordinate} v:{self.value}"
class TicTacToeGameState(TwoPlayersAbstractGameState): # type: ignore[misc]
"""TicTacToeGameState class."""
x = 1
o = -1
def __init__(self, state: npt.NDArray[np.float64], next_to_move: float = 1) -> None:
"""Inits."""
if len(state.shape) != 2 or state.shape[0] != state.shape[1]:
raise ValueError("Only 2D square boards allowed")
self.board = state
self.board_size: int = state.shape[0]
self.next_to_move = next_to_move
@property
def game_result(self) -> Optional[float]:
"""Returns game result.
This property should return:
1 if player #1 wins
-1 if player #2 wins
0 if there is a draw
None if result is unknown
Returns
-------
int
"""
# check if game is over
rowsum = np.sum(self.board, 0)
colsum = np.sum(self.board, 1)
diag_sum_tl = self.board.trace()
diag_sum_tr = self.board[::-1].trace()
player_one_wins = any(rowsum == self.board_size)
# uses fact that python booleans are considered numeric type
player_one_wins += any(colsum == self.board_size) # type: ignore[assignment]
player_one_wins += diag_sum_tl == self.board_size
player_one_wins += diag_sum_tr == self.board_size
if player_one_wins:
return self.x
player_two_wins = any(rowsum == -self.board_size)
# uses fact that python booleans are considered numeric type
player_two_wins += any(colsum == -self.board_size) # type: ignore[assignment]
player_two_wins += diag_sum_tl == -self.board_size
player_two_wins += diag_sum_tr == -self.board_size
if player_two_wins:
return self.o
if np.all(self.board != 0):
return 0.0
# if not over - no result
return None
def is_game_over(self) -> bool:
"""Returns boolean indicating if the game is over.
Simplest implementation may just be
`return self.game_result() is not None`
Returns
-------
boolean
"""
return self.game_result is not None
def is_move_legal(self, move: Move) -> bool:
"""Checks if move is legal."""
# check if correct player moves
if move.value != self.next_to_move:
return False
# check if inside the board on x-axis
x_in_range = 0 <= move.x_coordinate < self.board_size
if not x_in_range:
return False
# check if inside the board on y-axis
y_in_range = 0 <= move.y_coordinate < self.board_size
if not y_in_range:
return False
# finally check if board field not occupied yet
return bool(self.board[move.x_coordinate, move.y_coordinate] == 0)
def move(self, move: Move) -> "TicTacToeGameState":
"""Consumes action and returns resulting TwoPlayersAbstractGameState.
Returns
-------
TwoPlayersAbstractGameState
"""
if not self.is_move_legal(move):
raise ValueError(f"move {move} on board {self.board} is not legal")
new_board = np.copy(self.board) # type: ignore[no-untyped-call]
new_board[move.x_coordinate, move.y_coordinate] = move.value
if self.next_to_move == TicTacToeGameState.x:
next_to_move = TicTacToeGameState.o
else:
next_to_move = TicTacToeGameState.x
return TicTacToeGameState(new_board, next_to_move)
def get_legal_actions(self) -> List[Move]:
"""Returns list of legal action at current game state.
Returns
-------
list of AbstractGameAction
"""
indices = np.where(self.board == 0)
return [
Move(coords[0], coords[1], self.next_to_move)
for coords in list(zip(indices[0], indices[1]))
]
def from_mcts_grid_format(grid: List[List[float]]) -> List[List[int]]:
"""Loads grid from a list of int."""
return [[int(elem) for elem in row] for row in grid]
def to_mcts_grid_format(grid: List[List[int]]) -> List[List[float]]:
"""Dumps grid to list of int."""
return [[float(elem) for elem in row] for row in grid]
def mcts_move(grid: List[List[int]], mark: int) -> Tuple[int, int]:
"""Computes best move."""
board = to_mcts_grid_format(grid)
current_player = float(mark)
state = np.array(board)
initial_board_state = TicTacToeGameState(state=state, next_to_move=current_player)
root = TwoPlayersGameMonteCarloTreeSearchNode(state=initial_board_state)
mcts = MonteCarloTreeSearch(root)
best_node = mcts.best_action(10000)
board_diff = best_node.state.board - best_node.parent.state.board
x_coords, y_coords = np.where(board_diff == current_player)
chosen_cell = (x_coords[0], y_coords[0])
return chosen_cell
| python |
from gettext import Catalog
from xml.etree.ElementInclude import include
from django.contrib import admin
from django.urls import re_path
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^catalog/', include(Catalog.urls)),
]
| python |
from phq.kafka.consumer import _latest_distinct_messages
from phq.kafka import Message
def test_latest_distinct_messages():
messages = [
Message(id='abc', payload={}),
Message(id='def', payload={}),
Message(id='xyz', payload={}),
Message(id='xyz', payload={}),
Message(id='abc', payload={}),
]
distinct_messages = _latest_distinct_messages(messages)
assert len(distinct_messages) == 3
assert distinct_messages[0] is messages[1]
assert distinct_messages[1] is messages[3]
assert distinct_messages[2] is messages[4]
| python |
#!/usr/bin/env python
# Copyright (c) 2012 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Checks that gyp fails on static_library targets which have several files with
the same basename.
"""
import TestGyp
test = TestGyp.TestGyp()
test.run_gyp('double-static.gyp', chdir='src', status=1, stderr=None)
test.pass_test()
| python |
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
pySnarlNetLib
author: Łukasz Bołdys
licence: MIT
Copyright (c) 2009 Łukasz Bołdys
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
import sys
import socket
__version__ = (0, 1, 1)
__author__ = "Łukasz Bołdys"
class SnarlNet(object):
lastAppName = ""
lastClassName = ""
addedClasses = []
lastTimeout = 10
ip = "127.0.0.1" #if no ip provided than use localhost
port = 9887 #if no port provided than use default snarl net port
def __init__(self, *args, **argv):
"""
Create object of class SnarlNet
IP and port can be passed as 'ip' and 'port' parameters
Ie. snarl = SnarlNet(ip="192.168.1.4", port=9887)
When no parameters are passed than ip='127.0.0.1' and port=9887 are used
"""
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if argv.has_key("ip"):
self.ip = argv["ip"]
if argv.has_key("port"):
self.port = argv["port"]
def __send(self, sendStr):
self.sock.connect((self.ip, self.port))
self.sock.send(sendStr)
self.sock.close()
def register(self, appName):
"""
Register application by appName
"""
sendStr = "type=SNP#?version=1.0#?action=register#?app=%s\r\n" % (appName,)
self.__send(sendStr)
self.lastAppName = appName;
def unregister(self, appName = ""):
"""
Unregister application by appName. If appName is empty then tries to
unregister application by self.lastAppName (last registred application).
If self.lastAppName is empty than do nothing
"""
if appName == "":
if lastAppName == "":
sys.stderr.write("No application to unregister")
return
appName = lastAppName
sendStr = "type=SNP#?version=1.0#?action=unregister#?app=%s\r\n" % (appName,)
self.__send(sendStr)
self.lastAppName = ""
def notify(self, title, text, **argv):
"""
Send message with given title and text.
If no appName or appClass is provided than uses
self.lastAppName and/or self.lastClassName
"""
appName = self.lastAppName
className = self.lastClassName
timeout = self.lastTimeout
if argv.has_key("timeout"):
timeout = timeout
if argv.has_key("appName") and argv["appName"] != "":
appName = argv["appName"]
if argv.has_key("className") and argv["className"] != "":
className = argv["className"]
if appName == "":
appName = "pySnarlNetLib"
if className == "":
className = "pySnarlNetLibClass"
sendStr = "type=SNP#?version=1.0#?action=notification#?app=%s#?class=%s#?title=%s#?text=%s#?timeout=%d\r\n" % (appName,className,title,text,timeout)
self.__send(sendStr)
self.lastAppName = appName
self.lastClassName = className
self.lastTimeout = timeout
pass
def addclass(self, className, classTitle="", **argv):
"""
Add class with provided name (className).
If no classTitle is provided than sets classTitle to className
If no appName is provided than use self.lastAppName.
If self.lastAppName is empty than do nothing
"""
className = str(className)
if className in self.addedClasses:
sys.stderr.write("Class already added")
return
if className == "":
sys.stderr.write("className can not be empty")
return
appName = self.lastAppName
if classTitle == "":
classTitle = className
if argv.has_key["appName"]:
appName = argv["appName"]
if appName == "":
sys.stderr.write("No application to add class to")
return
sendStr = "type=SNP#?version=1.0#?action=add_class#?app=%s#?class=%s#?title=%s\r\n" % (appName,className,classTitle)
self.__send(sendStr)
self.lastAppName = appName
self.lastClassName = className
self.addedClasses.append(className)
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser(usage="%prog -a ACTION [options] args", version="%prog " + ".".join([str(x) for x in __version__]))
parser.add_option("-i", "--ipaddr", dest="host",
help="IP address of the machine with snarl installed (default: %default)",
type="string", default="127.0.0.1")
parser.add_option("-p", "--port", dest="port",
help="Port on with Snarl is listening (default: %default)",
type="int", default=9887)
parser.add_option("-n", "--appname", dest="appName", help="Application name",
type="string")
parser.add_option("-c", "--classname", dest="className", help="Class name",
type="string")
parser.add_option("-a", "--action", dest="action", choices=["register","unregister","addclass","notify"],
help="Action to take (register, unregister, addclass, notify)", type="choice")
parser.add_option("-t", "--timeout", dest="timeout", type="int",
help="How long snarl should display message", default=10)
(options, args) = parser.parse_args()
snarl = SnarlNet(ip=options.host, port=options.port)
if not options.action:
parser.print_usage()
if options.action == "register":
if options.appName != None:
appName = options.appName
elif len(args) > 0:
appName = args[0]
else:
parser.error("You need to provide application name")
snarl.register(appName)
elif options.action == "unregister":
if options.appName != None:
appName = options.appName
elif len(args) > 0:
appName = args[0]
else:
parser.error("You need to provide application name")
snarl.unregister(appName)
elif options.action == "addclass":
if options.appName != None and options.className != None:
appName = options.appName
className = options.className
elif options.appName != None and options.className == None:
appName = options.appName
if len(args) == 1:
className = args[0]
else:
parser.error("You need to provide class name")
elif options.appName == None and options.className != None:
className = options.className
if len(args) == 1:
appName = args[0]
else:
parser.error("You need to provide application name")
else:
if len(args) > 1:
appName = args[0]
className = args[1]
parser.error("You need to provide application name and class name")
snarl.addclass(className, classTitle=options.classTitle, appName=appName)
elif options.action == "notify":
appName = ""
className = ""
if options.appName != None:
appName = options.appName
if options.className != None:
className = options.className
if len(args) > 0:
title = args[0]
text = " ".join(args[1:])
else:
parser.error("You need to provide at least a title")
snarl.notify(title, text, appName=appName, className=className)
| python |
from setuptools import setup
# read the contents of your README file
from os import path
this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, "README.md"), encoding="utf-8") as f:
long_description = f.read()
setup(
name="rubrix",
# other arguments omitted
description="Open-source tool for tracking, exploring and labelling data for AI projects.",
long_description=long_description,
author="recognai",
author_email="[email protected]",
maintainer="recognai",
maintainer_email="[email protected]",
url="https://recogn.ai",
license="Apache-2.0",
keywords="data-science natural-language-processing artificial-intelligence knowledged-graph developers-tools human-in-the-loop mlops",
long_description_content_type="text/markdown",
use_scm_version=True,
)
| python |
# Generated by Django 3.0.7 on 2021-01-19 13:36
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('payment_system', '0027_auto_20201216_1852'),
]
operations = [
migrations.AlterField(
model_name='project',
name='owner',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='owned_projects', to=settings.AUTH_USER_MODEL),
),
]
| python |
#!/usr/bin/env python3
"""
Author : kyclark
Date : 2018-11-02
Purpose: Rock the Casbah
"""
import argparse
import pandas as pd
import matplotlib.pyplot as plt
import sys
# --------------------------------------------------
def get_args():
"""get args"""
parser = argparse.ArgumentParser(
description='Argparse Python script',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'file', metavar='str', help='A positional argument')
parser.add_argument(
'-o',
'--outfile`',
help='Save to outfile',
metavar='str',
type=str,
default=None)
return parser.parse_args()
# --------------------------------------------------
def warn(msg):
"""Print a message to STDERR"""
print(msg, file=sys.stderr)
# --------------------------------------------------
def die(msg='Something bad happened'):
"""warn() and exit with error"""
warn(msg)
sys.exit(1)
# --------------------------------------------------
def main():
"""main"""
args = get_args()
data = pd.read_csv(args.file, names=['term', 'desc', 'domain', 'count'])
counts = data['counts']
#data.drop(data[data['count'] > 2 * data['count'].std()].index, inplace=True)
#std = data.describe['std']
print(data.describe())
plt.hist(counts[counts > 0])
plt.show()
# --------------------------------------------------
if __name__ == '__main__':
main()
| python |
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
s = ''
for i in zip(*strs):
if len(set(i)) != 1:
return s
else:
s += i[0]
return s
if __name__ == '__main__':
strs = ["flower", "flow", "flight"]
strs = ["dog", "racecar", "car"]
# strs = ["caa", "a", "acb"]
print(Solution().longestCommonPrefix(strs)) | python |
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: uber/cadence/api/v1/tasklist.proto
"""Generated protocol buffer code."""
from google.protobuf.internal import enum_type_wrapper
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
from google.protobuf import duration_pb2 as google_dot_protobuf_dot_duration__pb2
from google.protobuf import timestamp_pb2 as google_dot_protobuf_dot_timestamp__pb2
from google.protobuf import wrappers_pb2 as google_dot_protobuf_dot_wrappers__pb2
DESCRIPTOR = _descriptor.FileDescriptor(
name='uber/cadence/api/v1/tasklist.proto',
package='uber.cadence.api.v1',
syntax='proto3',
serialized_options=b'\n\027com.uber.cadence.api.v1B\010ApiProtoP\001Z/github.com/uber/cadence/.gen/proto/api/v1;apiv1',
create_key=_descriptor._internal_create_key,
serialized_pb=b'\n\"uber/cadence/api/v1/tasklist.proto\x12\x13uber.cadence.api.v1\x1a\x1egoogle/protobuf/duration.proto\x1a\x1fgoogle/protobuf/timestamp.proto\x1a\x1egoogle/protobuf/wrappers.proto\"I\n\x08TaskList\x12\x0c\n\x04name\x18\x01 \x01(\t\x12/\n\x04kind\x18\x02 \x01(\x0e\x32!.uber.cadence.api.v1.TaskListKind\"N\n\x10TaskListMetadata\x12:\n\x14max_tasks_per_second\x18\x01 \x01(\x0b\x32\x1c.google.protobuf.DoubleValue\"A\n\x19TaskListPartitionMetadata\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\x17\n\x0fowner_host_name\x18\x02 \x01(\t\"\xa5\x01\n\x0eTaskListStatus\x12\x1a\n\x12\x62\x61\x63klog_count_hint\x18\x01 \x01(\x03\x12\x12\n\nread_level\x18\x02 \x01(\x03\x12\x11\n\tack_level\x18\x03 \x01(\x03\x12\x17\n\x0frate_per_second\x18\x04 \x01(\x01\x12\x37\n\rtask_id_block\x18\x05 \x01(\x0b\x32 .uber.cadence.api.v1.TaskIDBlock\"/\n\x0bTaskIDBlock\x12\x10\n\x08start_id\x18\x01 \x01(\x03\x12\x0e\n\x06\x65nd_id\x18\x02 \x01(\x03\"m\n\nPollerInfo\x12\x34\n\x10last_access_time\x18\x01 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x10\n\x08identity\x18\x02 \x01(\t\x12\x17\n\x0frate_per_second\x18\x03 \x01(\x01\"\x92\x01\n\x19StickyExecutionAttributes\x12\x37\n\x10worker_task_list\x18\x01 \x01(\x0b\x32\x1d.uber.cadence.api.v1.TaskList\x12<\n\x19schedule_to_start_timeout\x18\x02 \x01(\x0b\x32\x19.google.protobuf.Duration*`\n\x0cTaskListKind\x12\x1a\n\x16TASK_LIST_KIND_INVALID\x10\x00\x12\x19\n\x15TASK_LIST_KIND_NORMAL\x10\x01\x12\x19\n\x15TASK_LIST_KIND_STICKY\x10\x02*d\n\x0cTaskListType\x12\x1a\n\x16TASK_LIST_TYPE_INVALID\x10\x00\x12\x1b\n\x17TASK_LIST_TYPE_DECISION\x10\x01\x12\x1b\n\x17TASK_LIST_TYPE_ACTIVITY\x10\x02\x42V\n\x17\x63om.uber.cadence.api.v1B\x08\x41piProtoP\x01Z/github.com/uber/cadence/.gen/proto/api/v1;apiv1b\x06proto3'
,
dependencies=[google_dot_protobuf_dot_duration__pb2.DESCRIPTOR,google_dot_protobuf_dot_timestamp__pb2.DESCRIPTOR,google_dot_protobuf_dot_wrappers__pb2.DESCRIPTOR,])
_TASKLISTKIND = _descriptor.EnumDescriptor(
name='TaskListKind',
full_name='uber.cadence.api.v1.TaskListKind',
filename=None,
file=DESCRIPTOR,
create_key=_descriptor._internal_create_key,
values=[
_descriptor.EnumValueDescriptor(
name='TASK_LIST_KIND_INVALID', index=0, number=0,
serialized_options=None,
type=None,
create_key=_descriptor._internal_create_key),
_descriptor.EnumValueDescriptor(
name='TASK_LIST_KIND_NORMAL', index=1, number=1,
serialized_options=None,
type=None,
create_key=_descriptor._internal_create_key),
_descriptor.EnumValueDescriptor(
name='TASK_LIST_KIND_STICKY', index=2, number=2,
serialized_options=None,
type=None,
create_key=_descriptor._internal_create_key),
],
containing_type=None,
serialized_options=None,
serialized_start=855,
serialized_end=951,
)
_sym_db.RegisterEnumDescriptor(_TASKLISTKIND)
TaskListKind = enum_type_wrapper.EnumTypeWrapper(_TASKLISTKIND)
_TASKLISTTYPE = _descriptor.EnumDescriptor(
name='TaskListType',
full_name='uber.cadence.api.v1.TaskListType',
filename=None,
file=DESCRIPTOR,
create_key=_descriptor._internal_create_key,
values=[
_descriptor.EnumValueDescriptor(
name='TASK_LIST_TYPE_INVALID', index=0, number=0,
serialized_options=None,
type=None,
create_key=_descriptor._internal_create_key),
_descriptor.EnumValueDescriptor(
name='TASK_LIST_TYPE_DECISION', index=1, number=1,
serialized_options=None,
type=None,
create_key=_descriptor._internal_create_key),
_descriptor.EnumValueDescriptor(
name='TASK_LIST_TYPE_ACTIVITY', index=2, number=2,
serialized_options=None,
type=None,
create_key=_descriptor._internal_create_key),
],
containing_type=None,
serialized_options=None,
serialized_start=953,
serialized_end=1053,
)
_sym_db.RegisterEnumDescriptor(_TASKLISTTYPE)
TaskListType = enum_type_wrapper.EnumTypeWrapper(_TASKLISTTYPE)
TASK_LIST_KIND_INVALID = 0
TASK_LIST_KIND_NORMAL = 1
TASK_LIST_KIND_STICKY = 2
TASK_LIST_TYPE_INVALID = 0
TASK_LIST_TYPE_DECISION = 1
TASK_LIST_TYPE_ACTIVITY = 2
_TASKLIST = _descriptor.Descriptor(
name='TaskList',
full_name='uber.cadence.api.v1.TaskList',
filename=None,
file=DESCRIPTOR,
containing_type=None,
create_key=_descriptor._internal_create_key,
fields=[
_descriptor.FieldDescriptor(
name='name', full_name='uber.cadence.api.v1.TaskList.name', index=0,
number=1, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=b"".decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='kind', full_name='uber.cadence.api.v1.TaskList.kind', index=1,
number=2, type=14, cpp_type=8, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=156,
serialized_end=229,
)
_TASKLISTMETADATA = _descriptor.Descriptor(
name='TaskListMetadata',
full_name='uber.cadence.api.v1.TaskListMetadata',
filename=None,
file=DESCRIPTOR,
containing_type=None,
create_key=_descriptor._internal_create_key,
fields=[
_descriptor.FieldDescriptor(
name='max_tasks_per_second', full_name='uber.cadence.api.v1.TaskListMetadata.max_tasks_per_second', index=0,
number=1, type=11, cpp_type=10, label=1,
has_default_value=False, default_value=None,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=231,
serialized_end=309,
)
_TASKLISTPARTITIONMETADATA = _descriptor.Descriptor(
name='TaskListPartitionMetadata',
full_name='uber.cadence.api.v1.TaskListPartitionMetadata',
filename=None,
file=DESCRIPTOR,
containing_type=None,
create_key=_descriptor._internal_create_key,
fields=[
_descriptor.FieldDescriptor(
name='key', full_name='uber.cadence.api.v1.TaskListPartitionMetadata.key', index=0,
number=1, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=b"".decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='owner_host_name', full_name='uber.cadence.api.v1.TaskListPartitionMetadata.owner_host_name', index=1,
number=2, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=b"".decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=311,
serialized_end=376,
)
_TASKLISTSTATUS = _descriptor.Descriptor(
name='TaskListStatus',
full_name='uber.cadence.api.v1.TaskListStatus',
filename=None,
file=DESCRIPTOR,
containing_type=None,
create_key=_descriptor._internal_create_key,
fields=[
_descriptor.FieldDescriptor(
name='backlog_count_hint', full_name='uber.cadence.api.v1.TaskListStatus.backlog_count_hint', index=0,
number=1, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='read_level', full_name='uber.cadence.api.v1.TaskListStatus.read_level', index=1,
number=2, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='ack_level', full_name='uber.cadence.api.v1.TaskListStatus.ack_level', index=2,
number=3, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='rate_per_second', full_name='uber.cadence.api.v1.TaskListStatus.rate_per_second', index=3,
number=4, type=1, cpp_type=5, label=1,
has_default_value=False, default_value=float(0),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='task_id_block', full_name='uber.cadence.api.v1.TaskListStatus.task_id_block', index=4,
number=5, type=11, cpp_type=10, label=1,
has_default_value=False, default_value=None,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=379,
serialized_end=544,
)
_TASKIDBLOCK = _descriptor.Descriptor(
name='TaskIDBlock',
full_name='uber.cadence.api.v1.TaskIDBlock',
filename=None,
file=DESCRIPTOR,
containing_type=None,
create_key=_descriptor._internal_create_key,
fields=[
_descriptor.FieldDescriptor(
name='start_id', full_name='uber.cadence.api.v1.TaskIDBlock.start_id', index=0,
number=1, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='end_id', full_name='uber.cadence.api.v1.TaskIDBlock.end_id', index=1,
number=2, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=546,
serialized_end=593,
)
_POLLERINFO = _descriptor.Descriptor(
name='PollerInfo',
full_name='uber.cadence.api.v1.PollerInfo',
filename=None,
file=DESCRIPTOR,
containing_type=None,
create_key=_descriptor._internal_create_key,
fields=[
_descriptor.FieldDescriptor(
name='last_access_time', full_name='uber.cadence.api.v1.PollerInfo.last_access_time', index=0,
number=1, type=11, cpp_type=10, label=1,
has_default_value=False, default_value=None,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='identity', full_name='uber.cadence.api.v1.PollerInfo.identity', index=1,
number=2, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=b"".decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='rate_per_second', full_name='uber.cadence.api.v1.PollerInfo.rate_per_second', index=2,
number=3, type=1, cpp_type=5, label=1,
has_default_value=False, default_value=float(0),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=595,
serialized_end=704,
)
_STICKYEXECUTIONATTRIBUTES = _descriptor.Descriptor(
name='StickyExecutionAttributes',
full_name='uber.cadence.api.v1.StickyExecutionAttributes',
filename=None,
file=DESCRIPTOR,
containing_type=None,
create_key=_descriptor._internal_create_key,
fields=[
_descriptor.FieldDescriptor(
name='worker_task_list', full_name='uber.cadence.api.v1.StickyExecutionAttributes.worker_task_list', index=0,
number=1, type=11, cpp_type=10, label=1,
has_default_value=False, default_value=None,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='schedule_to_start_timeout', full_name='uber.cadence.api.v1.StickyExecutionAttributes.schedule_to_start_timeout', index=1,
number=2, type=11, cpp_type=10, label=1,
has_default_value=False, default_value=None,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=707,
serialized_end=853,
)
_TASKLIST.fields_by_name['kind'].enum_type = _TASKLISTKIND
_TASKLISTMETADATA.fields_by_name['max_tasks_per_second'].message_type = google_dot_protobuf_dot_wrappers__pb2._DOUBLEVALUE
_TASKLISTSTATUS.fields_by_name['task_id_block'].message_type = _TASKIDBLOCK
_POLLERINFO.fields_by_name['last_access_time'].message_type = google_dot_protobuf_dot_timestamp__pb2._TIMESTAMP
_STICKYEXECUTIONATTRIBUTES.fields_by_name['worker_task_list'].message_type = _TASKLIST
_STICKYEXECUTIONATTRIBUTES.fields_by_name['schedule_to_start_timeout'].message_type = google_dot_protobuf_dot_duration__pb2._DURATION
DESCRIPTOR.message_types_by_name['TaskList'] = _TASKLIST
DESCRIPTOR.message_types_by_name['TaskListMetadata'] = _TASKLISTMETADATA
DESCRIPTOR.message_types_by_name['TaskListPartitionMetadata'] = _TASKLISTPARTITIONMETADATA
DESCRIPTOR.message_types_by_name['TaskListStatus'] = _TASKLISTSTATUS
DESCRIPTOR.message_types_by_name['TaskIDBlock'] = _TASKIDBLOCK
DESCRIPTOR.message_types_by_name['PollerInfo'] = _POLLERINFO
DESCRIPTOR.message_types_by_name['StickyExecutionAttributes'] = _STICKYEXECUTIONATTRIBUTES
DESCRIPTOR.enum_types_by_name['TaskListKind'] = _TASKLISTKIND
DESCRIPTOR.enum_types_by_name['TaskListType'] = _TASKLISTTYPE
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
TaskList = _reflection.GeneratedProtocolMessageType('TaskList', (_message.Message,), {
'DESCRIPTOR' : _TASKLIST,
'__module__' : 'uber.cadence.api.v1.tasklist_pb2'
# @@protoc_insertion_point(class_scope:uber.cadence.api.v1.TaskList)
})
_sym_db.RegisterMessage(TaskList)
TaskListMetadata = _reflection.GeneratedProtocolMessageType('TaskListMetadata', (_message.Message,), {
'DESCRIPTOR' : _TASKLISTMETADATA,
'__module__' : 'uber.cadence.api.v1.tasklist_pb2'
# @@protoc_insertion_point(class_scope:uber.cadence.api.v1.TaskListMetadata)
})
_sym_db.RegisterMessage(TaskListMetadata)
TaskListPartitionMetadata = _reflection.GeneratedProtocolMessageType('TaskListPartitionMetadata', (_message.Message,), {
'DESCRIPTOR' : _TASKLISTPARTITIONMETADATA,
'__module__' : 'uber.cadence.api.v1.tasklist_pb2'
# @@protoc_insertion_point(class_scope:uber.cadence.api.v1.TaskListPartitionMetadata)
})
_sym_db.RegisterMessage(TaskListPartitionMetadata)
TaskListStatus = _reflection.GeneratedProtocolMessageType('TaskListStatus', (_message.Message,), {
'DESCRIPTOR' : _TASKLISTSTATUS,
'__module__' : 'uber.cadence.api.v1.tasklist_pb2'
# @@protoc_insertion_point(class_scope:uber.cadence.api.v1.TaskListStatus)
})
_sym_db.RegisterMessage(TaskListStatus)
TaskIDBlock = _reflection.GeneratedProtocolMessageType('TaskIDBlock', (_message.Message,), {
'DESCRIPTOR' : _TASKIDBLOCK,
'__module__' : 'uber.cadence.api.v1.tasklist_pb2'
# @@protoc_insertion_point(class_scope:uber.cadence.api.v1.TaskIDBlock)
})
_sym_db.RegisterMessage(TaskIDBlock)
PollerInfo = _reflection.GeneratedProtocolMessageType('PollerInfo', (_message.Message,), {
'DESCRIPTOR' : _POLLERINFO,
'__module__' : 'uber.cadence.api.v1.tasklist_pb2'
# @@protoc_insertion_point(class_scope:uber.cadence.api.v1.PollerInfo)
})
_sym_db.RegisterMessage(PollerInfo)
StickyExecutionAttributes = _reflection.GeneratedProtocolMessageType('StickyExecutionAttributes', (_message.Message,), {
'DESCRIPTOR' : _STICKYEXECUTIONATTRIBUTES,
'__module__' : 'uber.cadence.api.v1.tasklist_pb2'
# @@protoc_insertion_point(class_scope:uber.cadence.api.v1.StickyExecutionAttributes)
})
_sym_db.RegisterMessage(StickyExecutionAttributes)
DESCRIPTOR._options = None
# @@protoc_insertion_point(module_scope)
| python |
import string
def print_rangoli(n):
alpha = string.ascii_lowercase
L = []
for i in range(n):
s = "-".join(alpha[i:n])
L.append((s[::-1]+s[1:]).center(4*n-3, "-"))
print('\n'.join(L[:0:-1]+L))
if __name__ == '__main__':
n = int(input())
print_rangoli(n)
# def print_rangoli(size):
# alp = 'abcdefghijklmnopqrstuvwxyz'
# for i in range(size-1,-size,-1):
# temp = '-'.join(alp[size-1:abs(i):-1]+alp[abs(i):size])
# print(temp.center(4*size-3,'-'))
# from string import ascii_lowercase as letters
# def print_rangoli(limit):
# # your code goes here
# for i in range(limit-1):
# print(('-'.join(letters[limit-1:limit-i-1:-1]+letters[ limit-i-1:limit])).center(limit*4-3,'-'))
# for i in range(limit):
# print(('-'.join((letters[limit-1 : i:-1])+letters[ i:limit])).center(limit*4-3,'-'))
| python |
from pydantic import BaseModel
class PartOfSpeech(BaseModel):
tag: str
| python |
# coding:utf-8
import os
import json
import numpy as np
import torch.utils.data as data
from detectron2.structures import (
Boxes,
PolygonMasks,
BoxMode
)
DATASETS = {
"coco_2017_train": {
"img_dir": "coco/train2017",
"ann_file": "coco/annotations/instances_train2017.json"
},
"coco_2017_val": {
"img_dir": "coco/val2017",
"ann_file": "coco/annotations/instances_val2017.json"
}
}
class MaskLoader(data.Dataset):
"""
Dataloader for Local Mask.
Arguments:
root (string): filepath to dataset folder.
dataset (string): mask to use (eg. 'train', 'val').
size (tuple): The size used for train/val (height, width).
transform (callable, optional): transformation to perform on the input mask.
"""
def __init__(self, root="datasets", dataset="coco_2017_train", size=28, transform=False):
self.root = root
self.dataset = dataset
self.transform = transform
if isinstance(size, int):
self.size = size
else:
raise TypeError
data_info = DATASETS[dataset]
img_dir, ann_file = data_info['img_dir'], data_info['ann_file']
img_dir = os.path.join(self.root, img_dir) # actually we do not use it.
ann_file = os.path.join(self.root, ann_file)
with open(ann_file, 'r') as f:
anns = json.load(f)
anns = anns['annotations']
coco = list()
for ann in anns:
if ann.get('iscrowd', 0) == 0:
coco.append(ann)
self.coco = coco
print("Removed {} images with no usable annotations. {} images left.".format(
len(anns) - len(self.coco), len(self.coco)))
def __len__(self):
return len(self.coco)
def __getitem__(self, index):
ann = self.coco[index]
# bbox transform.
bbox = np.array([ann["bbox"]]) # xmin, ymin, w, h
bbox = BoxMode.convert(bbox, BoxMode.XYWH_ABS, BoxMode.XYXY_ABS) # x1y1x2y2
bbox = Boxes(bbox)
# label
# mask transform.
mask = PolygonMasks([ann["segmentation"]])
mask = mask.crop_and_resize(bbox.tensor, self.size).float()
return mask
| python |
#Need to prebuild in maya first
#RenderScript.py
#MayaPythonScript : RenderScript
#A script that can use python to automativly render the scene
import maya.cmds as cmds
import maya.cmds as mc
import maya.app.general.createImageFormats as createImageFormats
from mtoa.cmds.arnoldRender import arnoldRender
#Function : getCameraCharacter()
#Usage : use to get the Camera of the Character
#There is only one Camera in the Scene:
# ->characterCamera
#Return : the Camera Get
def getCameraCharacter() :
#Define the list Camera Class
cmds.listCameras()
#get the listCamera
listCamera = cmds.listCameras()
#debug information print
#debug information for list of Cameras
#print 'listCamera : ' + str(listCamera)
cameraWant = listCamera[0]
return cameraWant;
#Function : renderSequence
#Usage : frome the startFrame to the endFrame , we render it with a advanced setting
#use the render to render the camera want
#cmds.render(cameraWant)
#Input : renderfn(The render Tool) . renderfn_args(The flag use to render)
#the parameter frameNum is look like 00,01,02 to record the Index
def renderSequenceWithMayaSoft(startFrame , endFrame , frameNum ,renderfn = mc.render, renderfn_args = None):
#save the state
now = mc.currentTime(q = True)
for x in range(startFrame, endFrame):
#for render information debug
#print 'RenderScript : Do Render :' + str( x )
mc.currentTime(x)
#Launch render process
renderfn(renderfn_args)
# Save the Picture in RenderView
savePicInRenderView(frameNum, x)
#restore state
mc.currentTime(now)
# How to use : RenderScript.renderSequenceWithArnold(0,2,12)
# The function is the same as mayaSoftRender , but it use the arnold
def renderSequenceWithArnold(startFrame, endFrame, frameNum
, renderfn = arnoldRender
, renderfn_args= [695, 449, True, True,'camera1', ' -layer defaultRenderLayer']):
# save the state
now = mc.currentTime(q=True)
#renderfn_args = [960, 720, True, True,'camera1', ' -layer defaultRenderLayer']
for x in range(startFrame, endFrame):
# for render information debug
# print 'RenderScript : Do Render :' + str( x )
mc.currentTime(x)
# Launch render process
renderfn(renderfn_args[0],renderfn_args[1],renderfn_args[2],renderfn_args[3],renderfn_args[4],renderfn_args[5])
#renderfn(960, 720, True, True,'camera1', ' -layer defaultRenderLayer')
# Save the Picture in RenderView
savePicInRenderView(frameNum,x)
# restore state
mc.currentTime(now)
# The function use to save the RenderView frame when being render
def savePicInRenderView(frameIndex,x):
# save the image to a exist folder
editor = 'renderView'
formatManager = createImageFormats.ImageFormats()
formatManager.pushRenderGlobalsForDesc("PNG")
# The name of the Image is CharacterImage'+str(x)+.jpg ,example CharacterImage1.jpg\
cmds.renderWindowEditor(editor, e=True, writeImage='E:/mayaStore/images/imageSequence/CharacterImage_'
+ str(frameIndex).zfill(2) + '_' + str(x).zfill(2) + '.png')
formatManager.popRenderGlobals()
#Test Function
#renderSequence(0,24,renderfn_args = getCameraCharacter()) | python |
import torch
from torch import nn
from torch.nn import functional as F
def normalization(feautures):
B, _, H, W = feautures.size()
outs = feautures.squeeze(1)
outs = outs.view(B, -1)
outs_min = outs.min(dim=1, keepdim=True)[0]
outs_max = outs.max(dim=1, keepdim=True)[0]
norm = outs_max - outs_min
norm[norm == 0] = 1e-5
outs = (outs - outs_min) / norm
outs = outs.view(B, 1, H, W)
return outs
def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=dilation, groups=groups, bias=False, dilation=dilation)
def conv1x1(in_planes, out_planes, stride=1):
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
class FABlock(nn.Module):
def __init__(self, in_channels, norm_layer=None, reduction=8):
super(FABlock, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self.conv1 = conv1x1(in_channels, 1)
self.channel_fc = nn.Sequential(
nn.Linear(in_channels, in_channels // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(in_channels // reduction, in_channels, bias=False)
)
self.conv2 = conv1x1(in_channels, in_channels)
self.conv3 = conv1x1(in_channels, 1)
self.conv4 = conv3x3(1, 1)
self.bn4 = norm_layer(1)
self.gamma = nn.Parameter(torch.zeros(1))
def forward(self, x):
B, C, H, W = x.size()
# channel attention
y = self.conv1(x).view(B, 1, -1)
y = F.softmax(y, dim=-1)
y = y.permute(0, 2, 1).contiguous()
y = torch.matmul(x.view(B, C, -1), y).view(B, -1)
y = self.channel_fc(y)
y = torch.sigmoid(y).unsqueeze(2).unsqueeze(3).expand_as(x)
x_y = self.conv2(x)
x_y = x_y * y
# position attention
x_y_z = self.conv3(x_y)
z = self.conv4(x_y_z)
z = self.bn4(z)
z = torch.sigmoid(z)
x_y_z = x_y_z * z
out = self.gamma*x_y_z + x
attention_outs = normalization(self.gamma*x_y_z)
return out, attention_outs | python |
from .nucleus_sampling import top_k_top_p_filtering
from .transformer_decoder import TransformerDecoder | python |
# -*- coding: utf-8 -*-
from odoo import http
# class ControleEquipement(http.Controller):
# @http.route('/controle_equipement/controle_equipement/', auth='public')
# def index(self, **kw):
# return "Hello, world"
# @http.route('/controle_equipement/controle_equipement/objects/', auth='public')
# def list(self, **kw):
# return http.request.render('controle_equipement.listing', {
# 'root': '/controle_equipement/controle_equipement',
# 'objects': http.request.env['controle_equipement.controle_equipement'].search([]),
# })
# @http.route('/controle_equipement/controle_equipement/objects/<model("controle_equipement.controle_equipement"):obj>/', auth='public')
# def object(self, obj, **kw):
# return http.request.render('controle_equipement.object', {
# 'object': obj
# }) | python |
import consts
quotes = []
fp = open(consts.quotes_file, "r")
for line in fp:
if line[0] == '*':
quotes.append(line[2:-1])
fp.close()
| python |
# Jogo da Forca versão 2
import tkinter as tk
import applic
window = tk.Tk()
applic.Application(window)
window.mainloop() | python |
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional
import torch
from omegaconf import DictConfig
from nemo.collections.asr.data import audio_to_text, audio_to_text_dali
def get_char_dataset(config: dict, augmentor: Optional['AudioAugmentor'] = None) -> audio_to_text.AudioToCharDataset:
"""
Instantiates a Character Encoding based AudioToCharDataset.
Args:
config: Config of the AudioToCharDataset.
augmentor: Optional AudioAugmentor object for augmentations on audio data.
Returns:
An instance of AudioToCharDataset.
"""
dataset = audio_to_text.AudioToCharDataset(
manifest_filepath=config['manifest_filepath'],
labels=config['labels'],
sample_rate=config['sample_rate'],
int_values=config.get('int_values', False),
augmentor=augmentor,
max_duration=config.get('max_duration', None),
min_duration=config.get('min_duration', None),
max_utts=config.get('max_utts', 0),
blank_index=config.get('blank_index', -1),
unk_index=config.get('unk_index', -1),
normalize=config.get('normalize_transcripts', False),
trim=config.get('trim_silence', False),
load_audio=config.get('load_audio', True),
parser=config.get('parser', 'en'),
add_misc=config.get('add_misc', False),
)
return dataset
def get_effective_dataset(config: dict, augmentor: Optional['AudioAugmentor'] = None) -> audio_to_text.AudioToCharDataset:
"""
Instantiates a Character Encoding based AudioToCharDataset.
Args:
config: Config of the AudioToCharDataset.
augmentor: Optional AudioAugmentor object for augmentations on audio data.
Returns:
An instance of AudioToCharDataset.
"""
dataset = audio_to_text.AudioToCharEffectiveDataset(
manifest_filepath=config['manifest_filepath'],
labels=config['labels'],
sample_rate=config['sample_rate'],
int_values=config.get('int_values', False),
augmentor=augmentor,
max_duration=config.get('max_duration', None),
min_duration=config.get('min_duration', None),
max_utts=config.get('max_utts', 0),
blank_index=config.get('blank_index', -1),
unk_index=config.get('unk_index', -1),
normalize=config.get('normalize_transcripts', False),
trim=config.get('trim_silence', False),
load_audio=config.get('load_audio', True),
parser=config.get('parser', 'en'),
add_misc=config.get('add_misc', False),
buffer_size=config.get('buffer_size', 3000),
batch_size=config.get('batch_size', 128),
)
return dataset
def get_rolling_buffer_dataset(config: dict, augmentor: Optional['AudioAugmentor'] = None) -> audio_to_text.AudioToCharRollingBufferDataset:
"""
Instantiates a Character Encoding based AudioToCharDataset.
Args:
config: Config of the AudioToCharDataset.
augmentor: Optional AudioAugmentor object for augmentations on audio data.
Returns:
An instance of AudioToCharDataset.
"""
dataset = audio_to_text.AudioToCharRollingBufferDataset(
manifest_filepath=config['manifest_filepath'],
labels=config['labels'],
sample_rate=config['sample_rate'],
int_values=config.get('int_values', False),
augmentor=augmentor,
max_duration=config.get('max_duration', None),
min_duration=config.get('min_duration', None),
max_utts=config.get('max_utts', 0),
blank_index=config.get('blank_index', -1),
unk_index=config.get('unk_index', -1),
normalize=config.get('normalize_transcripts', False),
trim=config.get('trim_silence', False),
load_audio=config.get('load_audio', True),
parser=config.get('parser', 'en'),
add_misc=config.get('add_misc', False),
buffer_size=config.get('buffer_size', 2000),
batch_size=config.get('batch_size', 128),
)
return dataset
def get_bpe_dataset(
config: dict, tokenizer: 'TokenizerSpec', augmentor: Optional['AudioAugmentor'] = None
) -> audio_to_text.AudioToBPEDataset:
"""
Instantiates a Byte Pair Encoding / Word Piece Encoding based AudioToBPEDataset.
Args:
config: Config of the AudioToBPEDataset.
tokenizer: An instance of a TokenizerSpec object.
augmentor: Optional AudioAugmentor object for augmentations on audio data.
Returns:
An instance of AudioToBPEDataset.
"""
dataset = audio_to_text.AudioToBPEDataset(
manifest_filepath=config['manifest_filepath'],
tokenizer=tokenizer,
sample_rate=config['sample_rate'],
int_values=config.get('int_values', False),
augmentor=augmentor,
max_duration=config.get('max_duration', None),
min_duration=config.get('min_duration', None),
max_utts=config.get('max_utts', 0),
trim=config.get('trim_silence', False),
load_audio=config.get('load_audio', True),
add_misc=config.get('add_misc', False),
use_start_end_token=config.get('use_start_end_token', True),
)
return dataset
def get_tarred_char_dataset(
config: dict, shuffle_n: int, global_rank: int, world_size: int, augmentor: Optional['AudioAugmentor'] = None
) -> audio_to_text.TarredAudioToCharDataset:
"""
Instantiates a Character Encoding based TarredAudioToCharDataset.
Args:
config: Config of the TarredAudioToCharDataset.
shuffle_n: How many samples to look ahead and load to be shuffled.
See WebDataset documentation for more details.
global_rank: Global rank of this device.
world_size: Global world size in the training method.
augmentor: Optional AudioAugmentor object for augmentations on audio data.
Returns:
An instance of TarredAudioToCharDataset.
"""
dataset = audio_to_text.TarredAudioToCharDataset(
audio_tar_filepaths=config['tarred_audio_filepaths'],
manifest_filepath=config['manifest_filepath'],
labels=config['labels'],
sample_rate=config['sample_rate'],
int_values=config.get('int_values', False),
augmentor=augmentor,
shuffle_n=shuffle_n,
max_duration=config.get('max_duration', None),
min_duration=config.get('min_duration', None),
max_utts=config.get('max_utts', 0),
blank_index=config.get('blank_index', -1),
unk_index=config.get('unk_index', -1),
normalize=config.get('normalize_transcripts', False),
trim=config.get('trim_silence', False),
parser=config.get('parser', 'en'),
add_misc=config.get('add_misc', False),
shard_strategy=config.get('tarred_shard_strategy', 'scatter'),
global_rank=global_rank,
world_size=world_size,
)
return dataset
def get_tarred_bpe_dataset(
config: dict,
tokenizer: 'TokenizerSpec',
shuffle_n: int,
global_rank: int,
world_size: int,
augmentor: Optional['AudioAugmentor'] = None,
) -> audio_to_text.TarredAudioToBPEDataset:
"""
Instantiates a Byte Pair Encoding / Word Piece Encoding based TarredAudioToBPEDataset.
Args:
config: Config of the TarredAudioToBPEDataset.
tokenizer: An instance of a TokenizerSpec object.
shuffle_n: How many samples to look ahead and load to be shuffled.
See WebDataset documentation for more details.
global_rank: Global rank of this device.
world_size: Global world size in the training method.
augmentor: Optional AudioAugmentor object for augmentations on audio data.
Returns:
An instance of TarredAudioToBPEDataset.
"""
dataset = audio_to_text.TarredAudioToBPEDataset(
audio_tar_filepaths=config['tarred_audio_filepaths'],
manifest_filepath=config['manifest_filepath'],
tokenizer=tokenizer,
sample_rate=config['sample_rate'],
int_values=config.get('int_values', False),
augmentor=augmentor,
shuffle_n=shuffle_n,
max_duration=config.get('max_duration', None),
min_duration=config.get('min_duration', None),
max_utts=config.get('max_utts', 0),
trim=config.get('trim_silence', False),
add_misc=config.get('add_misc', False),
use_start_end_token=config.get('use_start_end_token', True),
shard_strategy=config.get('tarred_shard_strategy', 'scatter'),
global_rank=global_rank,
world_size=world_size,
)
return dataset
def get_dali_char_dataset(
config: dict,
shuffle: bool,
device_id: int,
global_rank: int,
world_size: int,
preprocessor_cfg: Optional[DictConfig] = None,
) -> audio_to_text_dali.AudioToCharDALIDataset:
"""
Instantiates a Character Encoding based AudioToCharDALIDataset.
Args:
config: Config of the AudioToCharDALIDataset.
shuffle: Bool flag whether to shuffle the dataset.
device_id: Index of the GPU to be used (local_rank). Only applicable when device == 'gpu'. Defaults to 0.
global_rank: Global rank of this device.
world_size: Global world size in the training method.
augmentor: Optional AudioAugmentor object for augmentations on audio data.
Returns:
An instance of AudioToCharDALIDataset.
"""
device = 'gpu' if torch.cuda.is_available() else 'cpu'
dataset = audio_to_text_dali.AudioToCharDALIDataset(
manifest_filepath=config['manifest_filepath'],
device=device,
batch_size=config['batch_size'],
labels=config['labels'],
sample_rate=config['sample_rate'],
max_duration=config.get('max_duration', None),
min_duration=config.get('min_duration', None),
blank_index=config.get('blank_index', -1),
unk_index=config.get('unk_index', -1),
normalize=config.get('normalize_transcripts', False),
trim=config.get('trim_silence', False),
parser=config.get('parser', 'en'),
shuffle=shuffle,
device_id=device_id,
global_rank=global_rank,
world_size=world_size,
preprocessor_cfg=preprocessor_cfg,
)
return dataset
| python |
import pydocspec
from pydocspec import visitors
def dump(root:pydocspec.TreeRoot) -> None:
for mod in root.root_modules:
mod.walk(visitors.PrintVisitor())
# pydocspec_processes = {
# 90: dump
# }
| python |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import mock
from click.testing import CliRunner
from elasticsearch_loader import cli
def invoke(content, *args, **kwargs):
if sys.version_info[0] == 2:
content = content.encode('utf-8')
runner = CliRunner()
with runner.isolated_filesystem():
with open('sample.csv', 'w') as f:
f.write(content)
return runner.invoke(*args, **kwargs)
@mock.patch('elasticsearch_loader.single_bulk_to_es')
def test_should_iterate_over_csv(bulk):
content = """id,first,last\nMOZA,Moshe,Zada\nMICHO,Michelle,Obama\na,b,c\nf,g,א"""
result = invoke(content, cli, ['--index=index', '--type=type', 'csv', 'sample.csv'], catch_exceptions=False)
assert result.exit_code == 0
assert [x for x in bulk.call_args[0][0] if x is not None] == [{'first': 'Moshe', 'id': 'MOZA', 'last': 'Zada'},
{'first': 'Michelle', 'id': 'MICHO', 'last': 'Obama'},
{'first': 'b', 'id': 'a', 'last': 'c'},
{'first': 'g', 'id': 'f', 'last': 'א'}]
@mock.patch('elasticsearch_loader.single_bulk_to_es')
def test_should_iterate_over_tsv(bulk):
content = """id first last\nMOZA Moshe Zada\nMICHO Michelle Obama\na b c\nf g א"""
result = invoke(content, cli, ['--index=index', '--type=type', 'csv', '--delimiter=\\t', 'sample.csv'], catch_exceptions=False)
assert result.exit_code == 0
assert [x for x in bulk.call_args[0][0] if x is not None] == [{'first': 'Moshe', 'id': 'MOZA', 'last': 'Zada'},
{'first': 'Michelle', 'id': 'MICHO', 'last': 'Obama'},
{'first': 'b', 'id': 'a', 'last': 'c'},
{'first': 'g', 'id': 'f', 'last': 'א'}]
| python |
from dataclasses import dataclass
import os
from typing import Optional
@dataclass(frozen=True)
class ENV:
workspace_name: Optional[str] = os.environ.get('WORKSPACE_NAME')
subscription_id: Optional[str] = os.environ.get('SUBSCRIPTION_ID')
resource_group: Optional[str] = os.environ.get('RESOURCE_GROUP')
vm_priority: Optional[str] = os.environ.get('AML_CLUSTER_PRIORITY','lowpriority')
vm_priority_scoring: Optional[str] = os.environ.get('AML_CLUSTER_PRIORITY_SCORING','lowpriority')
vm_size: Optional[str] = os.environ.get('AML_COMPUTE_CLUSTER_CPU_SKU')
vm_size_scoring: Optional[str] = os.environ.get('AML_COMPUTE_CLUSTER_CPU_SKU_SCORING')
min_nodes: Optional[int] = int(os.environ.get('AML_CLUSTER_MIN_NODES',0))
min_nodes_scoring: Optional[int] = int(os.environ.get('AML_CLUSTER_MIN_NODES_SCORING',0))
max_nodes: Optional[int] = int(os.environ.get('AML_CLUSTER_MAX_NODES',4))
max_nodes_scoring: Optional[int] = int(os.environ.get('AML_CLUSTER_MAX_NODES_SCORING',4))
source_train_directory: Optional[str] = os.environ.get('SOURCE_TRAIN_DIRECTORY','diabetes')
aml_conda_train_dependent_files: Optional[str] = os.environ.get('AML_CONDA_TRAIN_DEPENDENT_FILES','conda_dependencies.yml')
aml_env_name: Optional[str] = os.environ.get('AML_ENV_NAME')
aml_env_scoring_name: Optional[str] = os.environ.get('AML_ENV_SCORING_NAME')
aml_env_scorecopy_name: Optional[str] = os.environ.get('AML_ENV_SCORECOPY_NAME')
rebuild_env: Optional[bool] = os.environ.get('AML_REBUILD_ENVIRONMENT')
model_name: Optional[str] = os.environ.get('MODEL_NAME')
model_name_scoring: Optional[str] = os.environ.get('MODEL_NAME_SCORING')
model_version: Optional[str] = os.environ.get('MODEL_VERSION')
model_version_scoring: Optional[str] = os.environ.get('MODEL_VERSION_SCORING')
dataset_name: Optional[str] = os.environ.get('DATASET_NAME')
build_id: Optional[str] = os.environ.get('BUILD_BUILDID')
pipeline_name: Optional[str] = os.environ.get('TRAINING_PIPELINE_NAME')
compute_name: Optional[str] = os.environ.get('AML_COMPUTE_CLUSTER_NAME')
datastore_name: Optional[str] = os.environ.get('DATASTORE_NAME')
dataset_version: Optional[str] = os.environ.get('DATASET_VERSION')
train_script_path: Optional[str] = os.environ.get('TRAIN_SCRIPT_PATH')
eval_script_path: Optional[str] = os.environ.get('EVAL_SCRIPT_PATH')
register_script_path: Optional[str] = os.environ.get('REGISTER_SCRIPT_PATH')
allow_run_cancel: Optional[str] = os.environ.get('ALLOW_RUN_CANCEL')
run_evaluation: Optional[str] = os.environ.get('RUN_EVALUATION')
experiment_name: Optional[str] = os.environ.get('EXPERIMENT_NAME')
build_uri: Optional[str] = os.environ.get('BUILD_URI')
scoring_datastore_access_key: Optional[str] = os.environ.get('SCORING_DATASTORE_ACCESS_KEY')
scoring_datastore_input_container: Optional[str] = os.environ.get('SCORING_DATASTORE_INPUT_CONTAINER')
scoring_datastore_output_container: Optional[str] = os.environ.get('SCORING_DATASTORE_OUTPUT_CONTAINER')
scoring_datastore_storage_name : Optional[str] = os.environ.get('SCORING_DATASTORE_STORAGE_NAME')
scoring_datastore_input_filename: Optional[str] = os.environ.get('SCORING_DATASTORE_INPUT_FILENAME')
scoring_datastore_output_filename: Optional[str] = os.environ.get('SCORING_DATASTORE_OUTPUT_FILENAME')
scoring_dataset_name: Optional[str] = os.environ.get('SCORING_DATASET_NAME')
scoring_pipeline_name: Optional[str] = os.environ.get('SCORING_PIPELINE_NAME')
use_gpu_for_scoring: Optional[str] = os.environ.get('USE_GPU_FOR_SCORING')
rebuild_scoring_env: Optional[str] = os.environ.get('AML_REBUILD_SCORING_ENV')
batchscore_script_path: Optional[str] = os.environ.get('BATCHSCORE_SCRIPT_PATH')
batch_scorecopy_script_path: Optional[str] = os.environ.get('BATCH_SCORECOPY_SCRIPT_PATH')
aml_conda_score_file: Optional[str] = os.environ.get('AML_CONDA_SCORE_FILE')
aml_conda_scorecopy_file: Optional[str] = os.environ.get('AML_CONDA_SCORECOPY_FILE')
compute_scoring_name: Optional[str] = os.environ.get('AML_COMPUTE_CLUSTER_SCORING')
pipeline_id: Optional[str] = os.environ.get('SCORING_PIPELINE_ID')
scoring_datastore_access_key: Optional[str] = os.environ.get('SCORING_DATASTORE_ACCESS_KEY') | python |
# Learn more: https://github.com/Ensembl/ols-client
import os
from setuptools import setup, find_packages
with open(os.path.join(os.path.dirname(__file__), 'README.md')) as f:
readme = f.read()
with open(os.path.join(os.path.dirname(__file__), 'LICENSE')) as f:
license_ct = f.read()
with open(os.path.join(os.path.dirname(__file__), 'VERSION')) as f:
version = f.read()
def import_requirements():
with open(os.path.join(os.path.dirname(__file__), 'requirements.txt')) as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
return content
setup(
name='production_services',
version=version,
description='Ensembl Production Database Application',
long_description=readme,
author='Marc Chakiachvili,James Allen,Luca Da Rin Fioretto,Vinay Kaikala',
author_email='[email protected],[email protected],[email protected],[email protected]',
maintainer='Ensembl Production Team',
maintainer_email='[email protected]',
url='https://github.com/Ensembl/production_services',
license='APACHE 2.0',
packages=find_packages(exclude=('tests', 'docs')),
install_requires=import_requirements(),
classifiers=[
"Development Status :: 4 - Beta",
"Environment :: Console",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: Apache Software License",
"Natural Language :: English",
"Programming Language :: Python :: 3.6",
"Topic :: Scientific/Engineering :: Bio-Informatics",
"Topic :: Software Development :: Libraries :: Python Modules",
]
)
| python |
from vidispine.base import EntityBase
from vidispine.errors import InvalidInput
from vidispine.typing import BaseJson
class Search(EntityBase):
"""Search
Search Vidispine objects.
:vidispine_docs:`Vidispine doc reference <collection>`
"""
entity = 'search'
def __call__(self, *args, **kwargs) -> BaseJson:
"""Browses items and collections
:param metadata: Optional metadata (search document) supplied
to perform a shared search query.
:param params: Optional query parameters.
:param matrix_params: Optional matrix parameters.
:return: JSON response from the request.
:rtype: vidispine.typing.BaseJson.
"""
return self._search(*args, **kwargs)
def _search(
self,
metadata: dict = None,
params: dict = None,
matrix_params: dict = None
) -> BaseJson:
if metadata is None:
return self._search_without_search_doc(params, matrix_params)
else:
return self._search_with_search_doc(
metadata, params, matrix_params
)
def _search_with_search_doc(
self,
metadata: dict,
params: dict = None,
matrix_params: dict = None
) -> BaseJson:
if not metadata:
raise InvalidInput('Please supply metadata.')
if params is None:
params = {}
endpoint = self._build_url(matrix_params=matrix_params)
return self.client.put(endpoint, json=metadata, params=params)
def _search_without_search_doc(
self,
params: dict = None,
matrix_params: dict = None
) -> BaseJson:
if params is None:
params = {}
endpoint = self._build_url(matrix_params=matrix_params)
return self.client.get(endpoint, params=params)
def shape(
self,
metadata: dict = None,
params: dict = None,
matrix_params: dict = None
) -> BaseJson:
"""Searches shapes
:param metadata: Optional metadata (shape document) supplied
to perform a search query.
:param params: Optional query parameters.
:param matrix_params: Optional matrix parameters.
:return: JSON response from the request.
:rtype: vidispine.typing.BaseJson.
"""
if metadata is None:
return self._search_shapes_without_search_doc(
params, matrix_params
)
else:
return self._search_shapes_with_search_doc(
metadata, params, matrix_params
)
def _search_shapes_without_search_doc(
self,
params: dict = None,
matrix_params: dict = None
) -> BaseJson:
if params is None:
params = {}
endpoint = self._build_url('shape', matrix_params=matrix_params)
return self.client.get(endpoint, params=params)
def _search_shapes_with_search_doc(
self,
metadata: dict,
params: dict = None,
matrix_params: dict = None
) -> BaseJson:
if not metadata:
raise InvalidInput('Please supply metadata.')
if params is None:
params = {}
endpoint = self._build_url('shape', matrix_params=matrix_params)
return self.client.put(endpoint, json=metadata, params=params)
| python |
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import copy
from datetime import datetime
from functools import partial
import os
from code import Code
import json_parse
# The template for the header file of the generated FeatureProvider.
HEADER_FILE_TEMPLATE = """
// Copyright %(year)s The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// GENERATED FROM THE FEATURES FILE:
// %(source_files)s
// DO NOT EDIT.
#ifndef %(header_guard)s
#define %(header_guard)s
#include "extensions/common/features/base_feature_provider.h"
namespace extensions {
class %(provider_class)s : public BaseFeatureProvider {
public:
%(provider_class)s();
~%(provider_class)s() override;
private:
DISALLOW_COPY_AND_ASSIGN(%(provider_class)s);
};
} // namespace extensions
#endif // %(header_guard)s
"""
# The beginning of the .cc file for the generated FeatureProvider.
CC_FILE_BEGIN = """
// Copyright %(year)s The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// GENERATED FROM THE FEATURES FILE:
// %(source_files)s
// DO NOT EDIT.
#include "%(header_file_path)s"
#include "extensions/common/features/api_feature.h"
#include "extensions/common/features/behavior_feature.h"
#include "extensions/common/features/complex_feature.h"
#include "extensions/common/features/manifest_feature.h"
#include "extensions/common/features/permission_feature.h"
namespace extensions {
"""
# The end of the .cc file for the generated FeatureProvider.
CC_FILE_END = """
%(provider_class)s::~%(provider_class)s() {}
} // namespace extensions
"""
# A "grammar" for what is and isn't allowed in the features.json files. This
# grammar has to list all possible keys and the requirements for each. The
# format of each entry is:
# 'key': {
# allowed_type_1: optional_properties,
# allowed_type_2: optional_properties,
# }
# |allowed_types| are the types of values that can be used for a given key. The
# possible values are list, unicode, bool, and int.
# |optional_properties| provide more restrictions on the given type. The options
# are:
# 'subtype': Only applicable for lists. If provided, this enforces that each
# entry in the list is of the specified type.
# 'enum_map': A map of strings to C++ enums. When the compiler sees the given
# enum string, it will replace it with the C++ version in the
# compiled code. For instance, if a feature specifies
# 'channel': 'stable', the generated C++ will assign
# version_info::Channel::STABLE to channel. The keys in this map
# also serve as a list all of possible values.
# 'allow_all': Only applicable for lists. If present, this will check for
# a value of "all" for a list value, and will replace it with
# the collection of all possible values. For instance, if a
# feature specifies 'contexts': 'all', the generated C++ will
# assign the list of Feature::BLESSED_EXTENSION_CONTEXT,
# Feature::BLESSED_WEB_PAGE_CONTEXT et al for contexts. If not
# specified, defaults to false.
# 'values': A list of all possible allowed values for a given key.
# If a type definition does not have any restrictions (beyond the type itself),
# an empty definition ({}) is used.
FEATURE_GRAMMAR = (
{
'blacklist': {
list: {'subtype': unicode}
},
'channel': {
unicode: {
'enum_map': {
'trunk': 'version_info::Channel::UNKNOWN',
'canary': 'version_info::Channel::CANARY',
'dev': 'version_info::Channel::DEV',
'beta': 'version_info::Channel::BETA',
'stable': 'version_info::Channel::STABLE',
}
}
},
'command_line_switch': {
unicode: {}
},
'component_extensions_auto_granted': {
bool: {}
},
'contexts': {
list: {
'enum_map': {
'blessed_extension': 'Feature::BLESSED_EXTENSION_CONTEXT',
'blessed_web_page': 'Feature::BLESSED_WEB_PAGE_CONTEXT',
'content_script': 'Feature::CONTENT_SCRIPT_CONTEXT',
'extension_service_worker': 'Feature::SERVICE_WORKER_CONTEXT',
'web_page': 'Feature::WEB_PAGE_CONTEXT',
'webui': 'Feature::WEBUI_CONTEXT',
'unblessed_extension': 'Feature::UNBLESSED_EXTENSION_CONTEXT',
},
'allow_all': True
},
},
'default_parent': {
bool: {'values': [True]}
},
'dependencies': {
list: {'subtype': unicode}
},
'extension_types': {
list: {
'enum_map': {
'extension': 'Manifest::TYPE_EXTENSION',
'hosted_app': 'Manifest::TYPE_HOSTED_APP',
'legacy_packaged_app': 'Manifest::TYPE_LEGACY_PACKAGED_APP',
'platform_app': 'Manifest::TYPE_PLATFORM_APP',
'shared_module': 'Manifest::TYPE_SHARED_MODULE',
'theme': 'Manifest::TYPE_THEME',
},
'allow_all': True
},
},
'location': {
unicode: {
'enum_map': {
'component': 'SimpleFeature::COMPONENT_LOCATION',
'external_component': 'SimpleFeature::EXTERNAL_COMPONENT_LOCATION',
'policy': 'SimpleFeature::POLICY_LOCATION',
}
}
},
'internal': {
bool: {'values': [True]}
},
'matches': {
list: {'subtype': unicode}
},
'max_manifest_version': {
int: {'values': [1]}
},
'min_manifest_version': {
int: {'values': [2]}
},
'noparent': {
bool: {'values': [True]}
},
'platforms': {
list: {
'enum_map': {
'chromeos': 'Feature::CHROMEOS_PLATFORM',
'linux': 'Feature::LINUX_PLATFORM',
'mac': 'Feature::MACOSX_PLATFORM',
'win': 'Feature::WIN_PLATFORM',
}
}
},
'session_types': {
list: {
'enum_map': {
'regular': 'FeatureSessionType::REGULAR',
'kiosk': 'FeatureSessionType::KIOSK',
}
}
},
'whitelist': {
list: {'subtype': unicode}
},
})
FEATURE_CLASSES = ['APIFeature', 'BehaviorFeature',
'ManifestFeature', 'PermissionFeature']
def HasProperty(property_name, value):
return property_name in value
def HasAtLeastOneProperty(property_names, value):
return any([HasProperty(name, value) for name in property_names])
def DoesNotHaveProperty(property_name, value):
return property_name not in value
VALIDATION = ({
'all': [
(partial(HasAtLeastOneProperty, ['channel', 'dependencies']),
'Features must specify either a channel or dependencies'),
],
'APIFeature': [
(partial(HasProperty, 'contexts'),
'APIFeatures must specify at least one context')
],
'ManifestFeature': [
(partial(HasProperty, 'extension_types'),
'ManifestFeatures must specify at least one extension type'),
(partial(DoesNotHaveProperty, 'contexts'),
'ManifestFeatures do not support contexts.'),
],
'BehaviorFeature': [],
'PermissionFeature': [
(partial(HasProperty, 'extension_types'),
'PermissionFeatures must specify at least one extension type'),
(partial(DoesNotHaveProperty, 'contexts'),
'PermissionFeatures do not support contexts.'),
],
})
# These keys are used to find the parents of different features, but are not
# compiled into the features themselves.
IGNORED_KEYS = ['default_parent']
# By default, if an error is encountered, assert to stop the compilation. This
# can be disabled for testing.
ENABLE_ASSERTIONS = True
# JSON parsing returns all strings of characters as unicode types. For testing,
# we can enable converting all string types to unicode to avoid writing u''
# everywhere.
STRINGS_TO_UNICODE = False
class Feature(object):
"""A representation of a single simple feature that can handle all parsing,
validation, and code generation.
"""
def __init__(self, name):
self.name = name
self.has_parent = False
self.errors = []
self.feature_values = {}
def _GetType(self, value):
"""Returns the type of the given value. This can be different than type() if
STRINGS_TO_UNICODE is enabled.
"""
t = type(value)
if not STRINGS_TO_UNICODE:
return t
if t is str:
return unicode
return t
def _AddError(self, error):
"""Adds an error to the feature. If ENABLE_ASSERTIONS is active, this will
also assert to stop the compilation process (since errors should never be
found in production).
"""
self.errors.append(error)
if ENABLE_ASSERTIONS:
assert False, error
def _AddKeyError(self, key, error):
"""Adds an error relating to a particular key in the feature.
"""
self._AddError('Error parsing feature "%s" at key "%s": %s' %
(self.name, key, error))
def _GetCheckedValue(self, key, expected_type, expected_values,
enum_map, value):
"""Returns a string to be used in the generated C++ code for a given key's
python value, or None if the value is invalid. For example, if the python
value is True, this returns 'true', for a string foo, this returns "foo",
and for an enum, this looks up the C++ definition in the enum map.
key: The key being parsed.
expected_type: The expected type for this value, or None if any type is
allowed.
expected_values: The list of allowed values for this value, or None if any
value is allowed.
enum_map: The map from python value -> cpp value for all allowed values,
or None if no special mapping should be made.
value: The value to check.
"""
valid = True
if expected_values and value not in expected_values:
self._AddKeyError(key, 'Illegal value: "%s"' % value)
valid = False
t = self._GetType(value)
if expected_type and t is not expected_type:
self._AddKeyError(key, 'Illegal value: "%s"' % value)
valid = False
if not valid:
return None
if enum_map:
return enum_map[value]
if t in [str, unicode]:
return '"%s"' % str(value)
if t is int:
return str(value)
if t is bool:
return 'true' if value else 'false'
assert False, 'Unsupported type: %s' % value
def _ParseKey(self, key, value, grammar):
"""Parses the specific key according to the grammar rule for that key if it
is present in the json value.
key: The key to parse.
value: The full value for this feature.
grammar: The rule for the specific key.
"""
if key not in value:
return
v = value[key]
is_all = False
if v == 'all' and list in grammar and 'allow_all' in grammar[list]:
v = []
is_all = True
value_type = self._GetType(v)
if value_type not in grammar:
self._AddKeyError(key, 'Illegal value: "%s"' % v)
return
expected = grammar[value_type]
expected_values = None
enum_map = None
if 'values' in expected:
expected_values = expected['values']
elif 'enum_map' in expected:
enum_map = expected['enum_map']
expected_values = enum_map.keys()
if is_all:
v = copy.deepcopy(expected_values)
expected_type = None
if value_type is list and 'subtype' in expected:
expected_type = expected['subtype']
cpp_value = None
# If this value is a list, iterate over each entry and validate. Otherwise,
# validate the single value.
if value_type is list:
cpp_value = []
for sub_value in v:
cpp_sub_value = self._GetCheckedValue(key, expected_type,
expected_values, enum_map,
sub_value)
if cpp_sub_value:
cpp_value.append(cpp_sub_value)
if cpp_value:
cpp_value = '{' + ','.join(cpp_value) + '}'
else:
cpp_value = self._GetCheckedValue(key, expected_type, expected_values,
enum_map, v)
if cpp_value:
self.feature_values[key] = cpp_value
elif key in self.feature_values:
# If the key is empty and this feature inherited a value from its parent,
# remove the inherited value.
del self.feature_values[key]
def SetParent(self, parent):
"""Sets the parent of this feature, and inherits all properties from that
parent.
"""
assert not self.feature_values, 'Parents must be set before parsing'
self.feature_values = copy.deepcopy(parent.feature_values)
self.has_parent = True
def Parse(self, parsed_json):
"""Parses the feature from the given json value."""
for key in parsed_json.keys():
if key not in FEATURE_GRAMMAR:
self._AddKeyError(key, 'Unrecognized key')
for key, key_grammar in FEATURE_GRAMMAR.iteritems():
self._ParseKey(key, parsed_json, key_grammar)
def Validate(self, feature_class):
for validator, error in (VALIDATION[feature_class] + VALIDATION['all']):
if not validator(self.feature_values):
self._AddError(error)
def GetCode(self, feature_class):
"""Returns the Code object for generating this feature."""
c = Code()
c.Append('%s* feature = new %s();' % (feature_class, feature_class))
c.Append('feature->set_name("%s");' % self.name)
for key in sorted(self.feature_values.keys()):
if key in IGNORED_KEYS:
continue;
c.Append('feature->set_%s(%s);' % (key, self.feature_values[key]))
return c
class FeatureCompiler(object):
"""A compiler to load, parse, and generate C++ code for a number of
features.json files."""
def __init__(self, chrome_root, source_files, feature_class,
provider_class, out_root, out_base_filename):
# See __main__'s ArgumentParser for documentation on these properties.
self._chrome_root = chrome_root
self._source_files = source_files
self._feature_class = feature_class
self._provider_class = provider_class
self._out_root = out_root
self._out_base_filename = out_base_filename
# The json value for the feature files.
self._json = {}
# The parsed features.
self._features = {}
def _Load(self):
"""Loads and parses the source from each input file and puts the result in
self._json."""
for f in self._source_files:
abs_source_file = os.path.join(self._chrome_root, f)
try:
with open(abs_source_file, 'r') as f:
f_json = json_parse.Parse(f.read())
except:
print('FAILED: Exception encountered while loading "%s"' %
abs_source_file)
raise
dupes = set(f_json) & set(self._json)
assert not dupes, 'Duplicate keys found: %s' % list(dupes)
self._json.update(f_json)
def _FindParent(self, feature_name, feature_value):
"""Checks to see if a feature has a parent. If it does, returns the
parent."""
no_parent = False
if type(feature_value) is list:
no_parent_values = ['noparent' in v for v in feature_value]
no_parent = all(no_parent_values)
assert no_parent or not any(no_parent_values), (
'"%s:" All child features must contain the same noparent value' %
feature_name)
else:
no_parent = 'noparent' in feature_value
sep = feature_name.rfind('.')
if sep is -1 or no_parent:
return None
parent_name = feature_name[:sep]
while sep != -1 and parent_name not in self._features:
# This recursion allows for a feature to have a parent that isn't a direct
# ancestor. For instance, we could have feature 'alpha', and feature
# 'alpha.child.child', where 'alpha.child.child' inherits from 'alpha'.
# TODO(devlin): Is this useful? Or logical?
sep = feature_name.rfind('.', 0, sep)
parent_name = feature_name[:sep]
if sep == -1:
# TODO(devlin): It'd be kind of nice to be able to assert that the
# deduced parent name is in our features, but some dotted features don't
# have parents and also don't have noparent, e.g. system.cpu. We should
# probably just noparent them so that we can assert this.
# raise KeyError('Could not find parent "%s" for feature "%s".' %
# (parent_name, feature_name))
return None
parent_value = self._features[parent_name]
parent = parent_value
if type(parent_value) is list:
for p in parent_value:
if 'default_parent' in p.feature_values:
parent = p
break
assert parent, 'No default parent found for %s' % parent_name
return parent
def _CompileFeature(self, feature_name, feature_value):
"""Parses a single feature."""
if 'nocompile' in feature_value:
assert feature_value['nocompile'], (
'nocompile should only be true; otherwise omit this key.')
return
def parse_and_validate(name, value, parent):
try:
feature = Feature(name)
if parent:
feature.SetParent(parent)
feature.Parse(value)
feature.Validate(self._feature_class)
return feature
except:
print('Failure to parse feature "%s"' % feature_name)
raise
parent = self._FindParent(feature_name, feature_value)
# Handle complex features, which are lists of simple features.
if type(feature_value) is list:
feature_list = []
# This doesn't handle nested complex features. I think that's probably for
# the best.
for v in feature_value:
feature_list.append(parse_and_validate(feature_name, v, parent))
self._features[feature_name] = feature_list
return
self._features[feature_name] = parse_and_validate(
feature_name, feature_value, parent)
def Compile(self):
"""Parses all features after loading the input files."""
self._Load();
# Iterate over in sorted order so that parents come first.
for k in sorted(self._json.keys()):
self._CompileFeature(k, self._json[k])
def Render(self):
"""Returns the Code object for the body of the .cc file, which handles the
initialization of all features."""
c = Code()
c.Append('%s::%s() {' % (self._provider_class, self._provider_class))
c.Sblock()
for k in sorted(self._features.keys()):
c.Sblock('{')
feature = self._features[k]
if type(feature) is list:
c.Append('std::vector<Feature*> features;')
for f in feature:
c.Sblock('{')
c.Concat(f.GetCode(self._feature_class))
c.Append('features.push_back(feature);')
c.Eblock('}')
c.Append('ComplexFeature* feature(new ComplexFeature(&features));')
c.Append('feature->set_name("%s");' % k)
else:
c.Concat(feature.GetCode(self._feature_class))
c.Append('AddFeature("%s", feature);' % k)
c.Eblock('}')
c.Eblock('}')
return c
def Write(self):
"""Writes the output."""
header_file_path = self._out_base_filename + '.h'
cc_file_path = self._out_base_filename + '.cc'
substitutions = ({
'header_file_path': header_file_path,
'header_guard': (header_file_path.replace('/', '_').
replace('.', '_').upper()),
'provider_class': self._provider_class,
'source_files': str(self._source_files),
'year': str(datetime.now().year)
})
if not os.path.exists(self._out_root):
os.makedirs(self._out_root)
# Write the .h file.
with open(os.path.join(self._out_root, header_file_path), 'w') as f:
header_file = Code()
header_file.Append(HEADER_FILE_TEMPLATE)
header_file.Substitute(substitutions)
f.write(header_file.Render().strip())
# Write the .cc file.
with open(os.path.join(self._out_root, cc_file_path), 'w') as f:
cc_file = Code()
cc_file.Append(CC_FILE_BEGIN)
cc_file.Substitute(substitutions)
cc_file.Concat(self.Render())
cc_end = Code()
cc_end.Append(CC_FILE_END)
cc_end.Substitute(substitutions)
cc_file.Concat(cc_end)
f.write(cc_file.Render().strip())
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Compile json feature files')
parser.add_argument('chrome_root', type=str,
help='The root directory of the chrome checkout')
parser.add_argument(
'feature_class', type=str,
help='The name of the class to use in feature generation ' +
'(e.g. APIFeature, PermissionFeature)')
parser.add_argument('provider_class', type=str,
help='The name of the class for the feature provider')
parser.add_argument('out_root', type=str,
help='The root directory to generate the C++ files into')
parser.add_argument(
'out_base_filename', type=str,
help='The base filename for the C++ files (.h and .cc will be appended)')
parser.add_argument('source_files', type=str, nargs='+',
help='The source features.json files')
args = parser.parse_args()
if args.feature_class not in FEATURE_CLASSES:
raise NameError('Unknown feature class: %s' % args.feature_class)
c = FeatureCompiler(args.chrome_root, args.source_files, args.feature_class,
args.provider_class, args.out_root,
args.out_base_filename)
c.Compile()
c.Write()
| python |
from flask_jsondash import settings
def test_settings_have_url_keys_specified():
for family, config in settings.CHARTS_CONFIG.items():
assert 'js_url' in config
assert 'css_url' in config
def test_settings_have_urls_list_or_none():
for family, config in settings.CHARTS_CONFIG.items():
assert isinstance(config['js_url'], list)
assert isinstance(config['css_url'], list)
def test_all_enabled_by_default():
for family, config in settings.CHARTS_CONFIG.items():
assert config['enabled']
def test_valid_helplink():
for family, config in settings.CHARTS_CONFIG.items():
if 'help_link' in config:
assert config['help_link'].startswith('http')
def test_families_with_dependencies_are_valid_in_config():
families = settings.CHARTS_CONFIG.keys()
for family, config in settings.CHARTS_CONFIG.items():
if config['dependencies']:
for dep in config['dependencies']:
assert dep in families
| python |
number ="+919769352682 " | python |
import asyncio
import statistics
import time
from typing import Optional
import pytest
import pytest_asyncio
from janus import Queue as JanusQueue
from utils import create_kafka_event_from_dict, create_kafka_message_from_dict
from eventbus.config import (
ConsumerConfig,
HttpSinkConfig,
HttpSinkMethod,
UseProducersConfig,
)
from eventbus.consumer import EventConsumer, KafkaConsumer
from eventbus.event import EventProcessStatus, KafkaEvent
@pytest.fixture
def consumer_conf():
consumer_conf = ConsumerConfig(
kafka_topics=["topic1"],
kafka_config={
"bootstrap.servers": "127.0.0.1:9093",
"group.id": "test-group-1",
},
use_producers=UseProducersConfig(producer_ids=["p1", "p2"]),
include_events=[r"test\..*"],
exclude_events=[r"test\.exclude"],
sink=HttpSinkConfig(
url="/", method=HttpSinkMethod.POST, timeout=0.2, max_retry_times=3
),
concurrent_per_partition=1,
)
yield consumer_conf
class MockInternalConsumer:
def __init__(self):
self.queue = JanusQueue(maxsize=100000)
self.committed_data = []
self.benchmark = False
self.closed = False
def put(self, item, block: bool = True, timeout: Optional[float] = None):
return self.queue.sync_q.put(item, block, timeout)
def poll(self, timeout):
if self.closed:
raise RuntimeError
try:
msg = self.queue.sync_q.get(block=True, timeout=timeout)
if self.benchmark:
msg._offset = int(time.time() * 1000000)
return msg
except:
return None
def commit(self, message=None, offsets=None, asynchronous=True):
if self.benchmark:
# self.committed_data.append(
# [time.time() - (t.offset / 1000000) for t in offsets][0]
# )
self.committed_data.append(time.time() - (message.offset() / 1000000))
else:
self.committed_data.append(message)
def store_offsets(self, message=None, offsets=None):
self.commit(message, offsets)
def close(self):
self.closed = True
@pytest_asyncio.fixture
async def event_consumer(mocker, consumer_conf):
async def mock_send_event(self, event: KafkaEvent):
# await asyncio.sleep(0.01)
return event, EventProcessStatus.DONE
mocker.patch("eventbus.sink.HttpSink.send_event", mock_send_event)
consumer = KafkaConsumer("t1", consumer_conf)
mock_consumer = MockInternalConsumer()
consumer._internal_consumer = mock_consumer
# commit_spy = mocker.spy(consumer._internal_consumer, "commit")
event_consumer = EventConsumer("t1", consumer_conf)
event_consumer._consumer = consumer
event_consumer._send_queue: JanusQueue = JanusQueue(maxsize=100)
event_consumer._commit_queue = JanusQueue(maxsize=100)
yield event_consumer
@pytest.mark.asyncio
async def test_send_events(consumer_conf):
send_queue = JanusQueue(maxsize=100)
consumer = KafkaConsumer("t1", consumer_conf)
mock_consumer = MockInternalConsumer()
consumer._internal_consumer = mock_consumer
asyncio.create_task(
consumer.fetch_events(send_queue)
) # trigger fetch events thread
test_msg_1 = create_kafka_message_from_dict({"title": "test.e1"})
mock_consumer.put(test_msg_1)
event = await send_queue.async_q.get()
assert event.title == "test.e1"
assert send_queue.async_q.empty() == True
test_msg_2 = create_kafka_message_from_dict({"title": "test.e2"})
test_msg_3 = create_kafka_message_from_dict({"title": "test.e3"})
mock_consumer.put(test_msg_2)
mock_consumer.put(test_msg_3)
event = await send_queue.async_q.get()
assert event.title == "test.e2"
event = await send_queue.async_q.get()
assert event.title == "test.e3"
assert send_queue.async_q.empty() == True
test_msg_4 = create_kafka_message_from_dict({"published": "xxx"})
mock_consumer.put(test_msg_4)
assert send_queue.async_q.empty() == True
await consumer.close()
# assert _send_one_event.call_count == 3
@pytest.mark.asyncio
async def test_commit_events(mocker, consumer_conf):
commit_queue = JanusQueue(maxsize=100)
consumer = KafkaConsumer("t1", consumer_conf)
consumer._internal_consumer = MockInternalConsumer()
store_spy = mocker.spy(consumer._internal_consumer, "store_offsets")
asyncio.create_task(
consumer.commit_events(commit_queue)
) # trigger commmit events thread
test_event_1 = create_kafka_event_from_dict({"title": "test.e1"})
test_event_2 = create_kafka_event_from_dict({"title": "test.e2"})
commit_queue.sync_q.put((test_event_1, EventProcessStatus.DONE))
commit_queue.sync_q.put((test_event_2, EventProcessStatus.DONE))
await asyncio.sleep(0.1)
await consumer.close()
assert store_spy.call_count == 2
# assert _send_one_event.call_count == 3
@pytest.mark.asyncio
async def test_event_consumer(event_consumer):
mock_consumer = event_consumer._consumer._internal_consumer
# let's do this two times to check if the coordinator are able to rerun
asyncio.create_task(event_consumer.run())
# check the whole pipeline, if can get all events in commit method
test_events_amount = 10
for i in range(test_events_amount):
mock_consumer.put(
create_kafka_message_from_dict({"title": f"test.e{i+1}", "offset": i + 1})
)
await asyncio.sleep(0.1)
await event_consumer.cancel()
assert len(mock_consumer.committed_data) == test_events_amount
# check how it acts when new events come after the coordinator cancelled
mock_consumer.put(
create_kafka_message_from_dict({"title": f"test.ne", "offset": -1})
)
await asyncio.sleep(0.1)
assert len(mock_consumer.committed_data) == test_events_amount
# check the order of received commits
assert [m.offset() for m in mock_consumer.committed_data] == [
i for i in range(1, 11)
]
@pytest.mark.asyncio
async def test_event_consumer_abnormal_cases(event_consumer):
pass
@pytest.mark.asyncio
@pytest.mark.benchmark
async def test_event_consumer_benchmark(event_consumer):
import cProfile
import io
import pstats
from pstats import SortKey
mock_consumer = event_consumer._consumer._internal_consumer
mock_consumer.benchmark = True
start_time = time.time()
test_events_amount = 10000
for i in range(test_events_amount):
partition = i % 10
mock_consumer.put(
create_kafka_message_from_dict(
{"title": f"test.e{i+1}", "partition": partition},
faster=True,
)
)
print("\nput events cost: ", time.time() - start_time)
# https://towardsdatascience.com/how-to-profile-your-code-in-python-e70c834fad89
pr = cProfile.Profile()
pr.enable()
# let's do this two times to check if the coordinator are able to rerun
asyncio.create_task(event_consumer.run())
# while True:
# await asyncio.sleep(0.1)
# if coordinator._send_queue.async_q.empty():
# break
await asyncio.sleep(10)
await event_consumer.cancel()
await asyncio.sleep(1)
print("\n---\n")
# print(mock_consumer.committed_data)
print("Length: ", len(mock_consumer.committed_data))
print("Max: ", max(mock_consumer.committed_data))
print("Median: ", statistics.median(mock_consumer.committed_data))
print("Mean: ", statistics.mean(mock_consumer.committed_data))
print("Min: ", min(mock_consumer.committed_data))
# print(mock_consumer.committed_data)
print("\n---\n")
pr.disable()
si = io.StringIO()
ps = pstats.Stats(pr, stream=si).sort_stats(SortKey.CUMULATIVE)
ps.print_stats(15)
print(si.getvalue())
assert len(mock_consumer.committed_data) == test_events_amount
@pytest.mark.asyncio
async def test_event_consumer_skip_events(event_consumer):
mock_consumer = event_consumer._consumer._internal_consumer
asyncio.create_task(event_consumer.run())
mock_consumer.put(
create_kafka_message_from_dict({"title": f"test.e1", "offset": 1})
)
mock_consumer.put(
create_kafka_message_from_dict({"title": f"test.e2", "offset": 2})
)
mock_consumer.put(
create_kafka_message_from_dict({"title": f"test.exclude", "offset": 3})
)
for i in range(4, 310):
mock_consumer.put(
create_kafka_message_from_dict({"title": f"skip.e{i+1}", "offset": i + 1})
)
await asyncio.sleep(0.5)
await event_consumer.cancel()
assert len(mock_consumer.committed_data) == 5
# check the order of received commits
assert [m.offset() for m in mock_consumer.committed_data] == [1, 2, 104, 205, 306]
| python |
import numpy as np
import pandas as pd
import numba
import multiprocessing as mp
import itertools as it
import analyzer as ana
import concurrent.futures as fut
def calculate_pvalues(df, blabel, tlabel, mlabel, n, f=np.mean, **kwargs):
"""
Calculates the p value of the sample.
Parmas:
df --- (pandas.DataFrame) data read from csv
blabel --- (str) grouping column
tlabel --- (str) total column
mlabel --- (str) measurement column
n --- (int) # of bootstraps
f --- (function) statistic to apply (default: np.mean)
kwargs:
s --- (boolean) whether to save matrix to csv (default: False)
fname --- (str) csv file name
ctrl --- (str) control
Returns:
p_vals --- (pandas.DataFrame) of pairwise p values
"""
s = kwargs.pop('s', False)
fname = kwargs.pop('fname', None)
ctrl = kwargs.pop('ctrl', None)
matrix = df.set_index(blabel) # set index
# get genotypes
matrix.index = matrix.index.map(str)
genotypes = list(matrix.index.unique())
p_vals = ana.make_empty_dataframe(len(genotypes),\
len(genotypes), genotypes, genotypes) # empty pandas dataframe
# 8/1/2017 Replaced with processes
# threads = []
# qu = queue.Queue()
cores = 4 # core number set to 4 for debugging purposes
# cores = mp.cpu_count() # number of available cores
# for loop to iterate through all pairwise comparisons (not permutation)
# for loop to iterate through all pairwise comparisons (not permutation)
print('#{} cores detected for this machine.'.format(cores))
print('#Starting {} processes for bootstrapping...'.format(cores))
with fut.ProcessPoolExecutor(max_workers=cores) as executor:
# if no control is given, perform all pairwise comparisons
if ctrl is None:
fs = [executor.submit(calculate_deltas_process, matrix, tlabel, mlabel,
pair[0], pair[1], n) for pair in it.combinations(genotypes, 2)]
# control given
else:
genotypes.remove(ctrl)
fs = [executor.submit(calculate_deltas_process, matrix, tlabel, mlabel,
ctrl, genotype, n) for genotype in genotypes]
# save to matrix
for f in fut.as_completed(fs):
gene_1, gene_2, delta_obs, deltas_bootstrapped = f.result()
p_vals[gene_1][gene_2] = ana.calculate_pvalue(delta_obs, deltas_bootstrapped)
# for pair in it.combinations(genotypes, 2):
#
# thread = threading.Thread(target=calculate_deltas_queue,\
# args=(matrix, tlabel, clabel, pair[0], pair[1], n, qu))
# threads.append(thread)
#
# thread.setDaemon(True)
# thread.start()
#
# # control given
# else:
# for genotype in genotypes:
# if genotype == ctrl:
# continue
#
# thread = threading.Thread(target=calculate_deltas_queue,
# args=(matrix, tlabel, clabel, ctrl, genotype, n, qu))
# threads.append(thread)
#
# thread.setDaemon(True)
# thread.start()
#
# for thread in threads:
# gene_1, gene_2, delta_obs, deltas_bootstrapped = qu.get()
# p_vals[gene_1][gene_2] = ana.calculate_pvalue(delta_obs, deltas_bootstrapped)
print('#Bootstrapping complete.\n')
p_vals.replace(0, 1/n, inplace=True)
print('#P-value matrix:')
print(p_vals)
print()
# save matrix to csv
if s:
print('#Saving p-value matrix\n')
ana.save_matrix(p_vals, fname)
return p_vals.astype(float)
def calculate_deltas_process(matrix, tlabel, mlabel, gene_1, gene_2, n):
"""
Function to calculate deltas with multithreading.
Saves p values as tuples in queue.
Params:
matrix --- (pandas.DataFrame) with index correctly set
tlabel --- (str) total column
mlabel --- (str) measurement column
gene_1, gene_2 --- (String) genotypes to be compared
n --- (int) # of bootstraps
f --- (function) to calculate deltas (default: np.mean)
Returns: (tuple) gene_1, gene_2, delta_obs, deltas_bootstrapped
"""
# matrices with only genes that are given
matrix_1 = matrix[matrix.index == gene_1]
matrix_2 = matrix[matrix.index == gene_2]
# total and measurement arrays
ts_1 = np.array(matrix_1[tlabel])
ms_1 = np.array(matrix_1[mlabel])
ts_2 = np.array(matrix_2[tlabel])
ms_2 = np.array(matrix_2[mlabel])
delta_obs, deltas_bootstrapped = calculate_deltas(ts_1, ms_1, ts_2, ms_2, n)
# queue.put((gene_1, gene_2, delta_obs, deltas_bootstrapped))
return gene_1, gene_2, delta_obs, deltas_bootstrapped
def calculate_deltas(ts_1, ms_1, ts_2, ms_2, n, f=np.mean):
"""
Calculates the observed and bootstrapped deltas.
Params:
ts_1 --- (np.array) total samples 1
ms_1 --- (np.array) measurements 1
ts_2 --- (np.array) total samples 2
ms_2 --- (np.array) measurements 2
n --- (int) # of bootstraps
f --- (function) statistic to apply (default: np.mean)
Returns: (tuple) delta_obs, deltas_bootstrapped
"""
# calculate observed delta
stat_1 = f(ms_1 / ts_1)
stat_2 = f(ms_2 / ts_2)
delta_obs = stat_2 - stat_1
deltas_bootstrapped = bootstrap_deltas(ts_1, ms_1, ts_2, ms_2, n, f)
return delta_obs, deltas_bootstrapped
def bootstrap_deltas(ts_1, ms_1, ts_2, ms_2, n, f=np.mean):
"""
Calculates bootstrapped deltas.
Params:
ts_1 --- (np.array) total samples 1
ms_1 --- (np.array) measurements 1
ts_2 --- (np.array) total samples 2
ms_2 --- (np.array) measurements 2
n --- (int) # of bootstraps
Returns:
deltas --- (np.array) of length n
"""
# @numba.jit(nopython=True, nogil=True)
# def calculate_stats(ts, p):
# l = len(ts)
# nullps = np.zeros(l)
# for i in np.arange(l):
# nullps[i] = np.random.binomial(ts[i], p) / ts[i]
# nullss = f(nullps)
#
# return nullss
#
# @numba.jit(nopython=True, nogil=True)
# def bootstrap_deltas_numba(ts_1, cs_1, ts_2, cs_2, n):
# p = (np.sum(cs_1) + np.sum(cs_2)) / (np.sum(ts_1) + np.sum(ts_2))
#
# deltas = np.zeros(n)
# for i in np.arange(n):
# deltas[i] = calculate_stats(ts_2, p) - calculate_stats(ts_1, p)
#
# return deltas
# @numba.jit(nopython=True, nogil=True)
# def bootstrap_deltas_numba(ts_1, cs_1, ts_2, cs_2, n):
# p = (np.sum(cs_1) + np.sum(cs_2)) / (np.sum(ts_1) + np.sum(ts_2))
#
# deltas = np.zeros(n)
# for i in np.arange(n):
# # for each plate 1
# nullps_1 = np.zeros(len(ts_1))
# for j in np.arange(len(ts_1)):
# nullps_1[j] = np.random.binomial(ts_1[j], p) / ts_1[j]
# nullms_1 = np.mean(nullps_1)
#
# # for each plate 2
# nullps_2 = np.zeros(len(ts_2))
# for j in np.arange(len(ts_2)):
# nullps_2[j] = np.random.binomial(ts_2[j], p) / ts_2[j]
# nullms_2 = np.mean(nullps_2)
#
# deltas[i] = nullms_2 - nullms_1
#
# return deltas
# 8/1/2017 numba can't compile array expressions
# 8/2/2017 fastest of all other algorithms (even without numba)
def bootstrap_deltas_numba(ts_1, ms_1, ts_2, ms_2, n):
p = (np.sum(ms_1) + np.sum(ms_2)) / (np.sum(ts_1) + np.sum(ts_2))
nullps_1 = np.zeros((len(ts_1), n)) # initialize blank array for sums
# for each plate 1
for i in np.arange(len(ts_1)):
nullps_1[i,:] = np.random.binomial(ts_1[i], p, n) / ts_1[i]
# find mean of plate 1
nullms_1 = np.mean(nullps_1, axis=0)
nullps_2 = np.zeros((len(ts_2), n)) # initialize blank array for sums
# for each plate 2
for i in np.arange(len(ts_2)):
nullps_2[i,:] = np.random.binomial(ts_2[i], p, n) / ts_2[i]
# find mean of plate 2
nullms_2 = np.mean(nullps_2, axis=0)
# find deltas
deltas = nullms_2 - nullms_1
return deltas
# 7/31/2017 This is a vectorized function, but numba does not support
# np.split and np.repeat
# def bootstrap_deltas_numba(ts_1, cs_1, ts_2, cs_2, n):
# # total probablity with labels removed
# p = (np.sum(cs_1) + np.sum(cs_2)) / (np.sum(ts_1) + np.sum(ts_2))
#
# # vectorized bootstraps
# # make 2D array, each row representing plates, each column a bootstrap
# nullts_1 = np.split(np.repeat(ts_1, n), len(ts_1))
# # calculate binomial picks
# nullcs_1 = np.random.binomial(nullts_1, p)
# # calculate probability by dividing by total sample
# nullps_1 = nullcs_1 / ts_1[:,None]
# # calculate statistic using f
# nullss_1 = f(nullps_1, axis=0)
#
# # make 2D array, each row representing plates, each column a bootstrap
# nullts_2 = np.split(np.repeat(ts_2, n), len(ts_2))
# # calculate binomial picks
# nullcs_2 = np.random.binomial(nullts_2, p)
# # calculate probability by dividing by total sample
# nullps_2 = nullcs_2 / ts_2[:,None]
# # calculate statistic using f
# nullss_2 = f(nullps_2, axis=0)
#
# deltas = nullss_2 - nullss_1
#
# return deltas
deltas = bootstrap_deltas_numba(ts_1, ms_1, ts_2, ms_2, n)
return deltas
# # 7/31/2017 vectorized by np.random.binomial
# # total number of samples
# ts_n = np.sum(ts_1) + np.sum(ts_2)
# cs_n = np.sum(cs_1) + np.sum(cs_2)
#
# # mixed array
# mixed = np.zeros(ts_n)
# mixed[0:cs_n] = np.ones(cs_n)
#
# # function to be numbaized
# @numba.jit(nopython=True, nogil=True)
# def difference(ts_1, cs_1, ts_2, cs_2, n):
# """
# Calculates delta based on function f.
# """
#
# # initialize deltas array
# deltas = np.zeros(n)
#
# # perform bootstraps
# # TODO: use np.random.binomial - can it be done without looping n times?
# for i in np.arange(n):
# nullp_1 = np.zeros(len(ts_1))
# nullp_2 = np.zeros(len(ts_2))
#
# for j in np.arange(len(ts_1)):
# nullc = np.sum(np.random.choice(mixed, cs_1[j], replace=True))
# nullp_1[j] = nullc / ts_1[j]
#
# for j in np.arange(len(ts_2)):
# nullc = np.sum(np.random.choice(mixed, cs_2[j], replace=True))
# nullp_2[j] = nullc / ts_2[j]
#
# # calculate difference of means
# delta = f(nullp_2) - f(nullp_1)
#
# deltas[i] = delta
#
# return deltas
#
# deltas = difference(ts_1, cs_1, ts_2, cs_2, n)
#
# return deltas
if __name__ == '__main__':
import argparse
import os
n = 10**4
stat = 'mean'
fs = {'mean': np.mean,
'median': np.median}
parser = argparse.ArgumentParser(description='Run analysis of binary data.')
# begin command line arguments
parser.add_argument('csv_data',
help='Path to the csv data file.',
type=str)
parser.add_argument('title',
help='Title of analysis. (without file \
extension)',
type=str)
parser.add_argument('-b',
help='Number of bootstraps. \
(default: {0})'.format(n),
type=int,
default=100)
parser.add_argument('-i',
help='Column to group measurements by. \
(defaults to first column)',
type=str,
default=None)
parser.add_argument('-c',
help='Control genotype. \
(performs one-vs-all analysis if given)',
type=str,
default=None)
parser.add_argument('-t',
help='Column for total sample size. \
(defaults to second column)',
type=str,
default=None)
parser.add_argument('-m',
help='Column for measurements. \
(defaults to third column)',
default=None)
parser.add_argument('-s',
help='Statistic to apply. \
(default: {})'.format(stat),
type=str,
choices=fs.keys(),
default='mean')
parser.add_argument('--save',
help='Save matrices to csv.',
action='store_true')
# end command line arguments
args = parser.parse_args()
csv_path = args.csv_data
title = args.title
n = args.b
blabel = args.i
ctrl = args.c
tlabel = args.t
mlabel = args.m
f = fs[args.s]
s = args.save
df = pd.read_csv(csv_path) # read csv data
# infer by, tot, and count columns
if blabel is None:
print('##No grouping column given...', end='')
blabel = df.keys()[0]
print('Inferred as \'{}\' from data.\n'.format(blabel))
if tlabel is None:
print('##No total column given...', end='')
tlabel = df.keys()[1]
print('Inferred as \'{}\' from data.\n'.format(tlabel))
if mlabel is None:
print('##No measurement column given...', end='')
mlabel = df.keys()[2]
print('Inferred as \'{}\' from data.\n'.format(mlabel))
# set directory to title
path = './{}'.format(title)
if os.path.exists(path):
os.chdir(path)
else:
os.mkdir(path)
os.chdir(path)
p_vals = calculate_pvalues(df, blabel, tlabel, mlabel, n, f=f, ctrl=ctrl, s=s, fname='p')
q_vals = ana.calculate_qvalues(p_vals, s=s, fname='q')
| python |
"""Create openapi schema from the given API."""
import typing as t
import inspect
import re
from http import HTTPStatus
from functools import partial
from apispec import APISpec, utils
from apispec.ext.marshmallow import MarshmallowPlugin
from http_router.routes import DynamicRoute, Route
from asgi_tools.response import CAST_RESPONSE
from muffin import Response
from muffin.typing import JSONType
from . import LIMIT_PARAM, OFFSET_PARAM, openapi
try:
from apispec import yaml_utils
except ImportError:
yaml_utils = None
DEFAULT_METHODS = 'get',
HTTP_METHODS = ['GET', 'POST', 'PUT', 'PATH', 'DELETE', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT']
RE_URL = re.compile(r'<(?:[^:<>]+:)?([^<>]+)>')
SKIP_PATH = {'/openapi.json', '/swagger', '/redoc'}
def render_openapi(api, request):
"""Prepare openapi specs."""
# Setup Specs
options = dict(api.openapi_options)
options.setdefault('servers', [{
'url': str(request.url.with_query('').with_path(api.prefix))
}])
spec = APISpec(
options['info'].pop('title', f"{ api.app.cfg.name.title() } API"),
options['info'].pop('version', '1.0.0'),
options.pop('openapi_version', '3.0.0'),
**options, plugins=[MarshmallowPlugin()])
spec.tags = {}
# Setup Authorization
if api.authorize:
_, _, schema = parse_docs(api.authorize)
spec.options['security'] = []
for key, value in schema.items():
spec.components.security_scheme(key, value)
spec.options['security'].append({key: []})
# Setup Paths
routes = api.router.routes()
for route in routes:
if route.path in SKIP_PATH:
continue
spec.path(route.path, **route_to_spec(route, spec))
return spec.to_dict()
def parse_docs(cb: t.Callable) -> t.Tuple[str, str, t.Dict]:
"""Parse docs from the given callback."""
if yaml_utils is None:
return '', '', {}
docs = cb.__doc__ or ''
schema = yaml_utils.load_yaml_from_docstring(docs)
docs = docs.split('---')[0]
docs = utils.dedent(utils.trim_docstring(docs))
summary, _, description = docs.partition('\n\n')
return summary, description.strip(), schema
def merge_dicts(source: t.Dict, merge: t.Dict) -> t.Dict:
"""Merge dicts."""
return dict(source, **{
key: ((
merge_dicts(source[key], merge[key])
if isinstance(source[key], dict) and isinstance(merge[key], dict)
else (
source[key] + merge[key]
if isinstance(source[key], list) and isinstance(merge[key], list)
else merge[key]
)
) if key in source else merge[key]) for key in merge})
def route_to_spec(route: Route, spec: APISpec) -> t.Dict:
"""Convert the given router to openapi operations."""
results: t.Dict = {'parameters': [], 'operations': {}}
if isinstance(route, DynamicRoute):
for param in route.params:
results['parameters'].append({'in': 'path', 'name': param})
target = t.cast(t.Callable, route.target)
if isinstance(target, partial):
target = target.func
if hasattr(target, 'openapi'):
results['operations'] = target.openapi(route, spec) # type: ignore
return results
summary, desc, schema = parse_docs(target)
responses = return_type_to_response(target)
for method in route_to_methods(route):
results['operations'][method] = {
'summary': summary,
'description': desc,
'responses': responses
}
results['operations'] = merge_dicts(results['operations'], schema)
return results
def route_to_methods(route: Route) -> t.List[str]:
"""Get sorted methods from the route."""
methods = [m for m in HTTP_METHODS if m in (route.methods or [])]
return [m.lower() for m in methods or DEFAULT_METHODS]
def return_type_to_response(fn: t.Callable) -> t.Dict:
"""Generate reponses specs based on the given function's return type."""
responses: t.Dict[int, t.Dict] = {}
return_type = fn.__annotations__.get('return')
return_type = CAST_RESPONSE.get(return_type, return_type) # type: ignore
if return_type is None:
return responses
if inspect.isclass(return_type) and issubclass(return_type, Response) and \
return_type.content_type:
responses[return_type.status_code] = {
'description': HTTPStatus(return_type.status_code).description,
'content': {
return_type.content_type: {
}
}
}
return responses
class OpenAPIMixin:
"""Render an endpoint to openapi specs."""
if t.TYPE_CHECKING:
from .endpoint import RESTOptions
meta: RESTOptions
@classmethod
def openapi(cls, route: Route, spec: APISpec) -> t.Dict:
"""Get openapi specs for the endpoint."""
if cls.meta.name is None:
return {}
operations: t.Dict = {}
summary, desc, schema = parse_docs(cls)
if cls not in spec.tags:
spec.tags[cls] = cls.meta.name
spec.tag({'name': cls.meta.name, 'description': summary})
spec.components.schema(cls.meta.Schema.__name__, schema=cls.meta.Schema)
schema_ref = {'$ref': f"#/components/schemas/{ cls.meta.Schema.__name__ }"}
for method in route_to_methods(route):
operations[method] = {'tags': [spec.tags[cls]]}
is_resource_route = isinstance(route, DynamicRoute) and \
route.params.get(cls.meta.name_id)
if method == 'get' and not is_resource_route:
operations[method]['parameters'] = []
if cls.meta.sorting:
operations[method]['parameters'].append(cls.meta.sorting.openapi)
if cls.meta.filters:
operations[method]['parameters'].append(cls.meta.filters.openapi)
if cls.meta.limit:
operations[method]['parameters'].append({
'name': LIMIT_PARAM, 'in': 'query',
'schema': {'type': 'integer', 'minimum': 1, 'maximum': cls.meta.limit},
'description': 'The number of items to return',
})
operations[method]['parameters'].append({
'name': OFFSET_PARAM, 'in': 'query',
'schema': {'type': 'integer', 'minimum': 0},
'description': 'The offset of items to return',
})
# Update from the method
meth = getattr(cls, method, None)
if isinstance(route.target, partial) and '__meth__' in route.target.keywords:
meth = getattr(cls, route.target.keywords['__meth__'], None)
elif method in {'post', 'put'}:
operations[method]['requestBody'] = {
'required': True, 'content': {'application/json': {'schema': schema_ref}}
}
if meth:
operations[method]['summary'], operations[method]['description'], mschema = openapi.parse_docs(meth) # noqa
return_type = meth.__annotations__.get('return')
if return_type == 'JSONType' or return_type == JSONType:
responses = {200: {'description': 'Request is successfull', 'content': {
'application/json': {'schema': schema_ref}
}}}
else:
responses = return_type_to_response(meth)
operations[method]['responses'] = responses
operations[method] = merge_dicts(operations[method], mschema)
return merge_dicts(operations, schema)
| python |
#!/usr/bin/env python
from setuptools import setup, find_packages
import versioneer
setup(name='hiwenet',
version=versioneer.get_version(),
cmdclass=versioneer.get_cmdclass(),
description='Histogram-weighted Networks for Feature Extraction and Advance Analysis in Neuroscience',
long_description='Histogram-weighted Networks for Feature Extraction and Advance Analysis in Neuroscience; hiwenet',
author='Pradeep Reddy Raamana',
author_email='[email protected]',
url='https://github.com/raamana/hiwenet',
packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]),
install_requires=['numpy', 'pyradigm', 'nibabel', 'networkx', 'medpy'],
classifiers=[
'Intended Audience :: Science/Research',
'Programming Language :: Python',
'Topic :: Software Development',
'Topic :: Scientific/Engineering',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Operating System :: Unix',
'Operating System :: MacOS',
'Programming Language :: Python :: 3.6',
],
entry_points={
"console_scripts": [
"hiwenet=hiwenet.__main__:main",
]
}
)
| python |
import os
import time
def main():
try:
os.remove("/etc/pmon.d/neutron-avs-agent.conf")
except:
pass
while True:
time.sleep(100)
if __name__ == "__main__":
main()
| python |
from rest_framework import serializers
from paste import constants
from paste.models import Snippet
class SnippetSerializer(serializers.ModelSerializer):
"""Snippet model serializer."""
class Meta:
model = Snippet
fields = '__all__'
read_only_fields = ['owner']
def create(self, validated_data: dict) -> Snippet:
"""Check that if current user is anonymous they are not trying to
create a private snippet, then create new instance.
"""
if (self.context['request'].user.is_anonymous
and validated_data.get('private', constants.DEFAULT_PRIVATE)):
raise serializers.ValidationError(
'anonymous users cannot create private snippets')
return super().create(validated_data)
| python |
""" Seeking Alpha View """
__docformat__ = "numpy"
import argparse
from typing import List
import pandas as pd
from datetime import datetime
from gamestonk_terminal.helper_funcs import (
check_positive,
parse_known_args_and_warn,
valid_date,
)
from gamestonk_terminal.discovery import seeking_alpha_model
def earnings_release_dates_view(other_args: List[str]):
"""Prints a data frame with earnings release dates
Parameters
----------
other_args : List[str]
argparse other args - ["-p", "20", "-n", "5"]
"""
parser = argparse.ArgumentParser(
add_help=False,
prog="up_earnings",
description="""Upcoming earnings release dates. [Source: Seeking Alpha]""",
)
parser.add_argument(
"-p",
"--pages",
action="store",
dest="n_pages",
type=check_positive,
default=10,
help="Number of pages to read upcoming earnings from in Seeking Alpha website.",
)
parser.add_argument(
"-n",
"--num",
action="store",
dest="n_num",
type=check_positive,
default=3,
help="Number of upcoming earnings release dates to print",
)
ns_parser = parse_known_args_and_warn(parser, other_args)
if not ns_parser:
return
df_earnings = seeking_alpha_model.get_next_earnings(ns_parser.n_pages)
pd.set_option("display.max_colwidth", None)
for n_days, earning_date in enumerate(df_earnings.index.unique()):
if n_days > (ns_parser.n_num - 1):
break
print(f"Earning Release on {earning_date.date()}")
print("----------------------------------------------")
print(
df_earnings[earning_date == df_earnings.index][
["Ticker", "Name"]
].to_string(index=False, header=False)
)
print("")
def latest_news_view(other_args: List[str]):
"""Prints the latest news article list
Parameters
----------
other_args : List[str]
argparse other args - ["-i", "123123", "-n", "5"]
"""
parser = argparse.ArgumentParser(
add_help=False,
prog="latest",
description="""Latest news articles. [Source: Seeking Alpha]""",
)
parser.add_argument(
"-i",
"--id",
action="store",
dest="n_id",
type=check_positive,
default=-1,
help="article ID number",
)
parser.add_argument(
"-n",
"--num",
action="store",
dest="n_num",
type=check_positive,
default=10,
help="number of articles being printed",
)
parser.add_argument(
"-d",
"--date",
action="store",
dest="n_date",
type=valid_date,
default=datetime.now().strftime("%Y-%m-%d"),
help="starting date",
)
if other_args:
if "-" not in other_args[0]:
other_args.insert(0, "-i")
ns_parser = parse_known_args_and_warn(parser, other_args)
if not ns_parser:
return
# User wants to see all latest news
if ns_parser.n_id == -1:
articles = seeking_alpha_model.get_article_list(
ns_parser.n_date, ns_parser.n_num
)
for idx, article in enumerate(articles):
print(
article["publishedAt"].replace("T", " ").replace("Z", ""),
"-",
article["id"],
"-",
article["title"],
)
print(article["url"])
print("")
if idx >= ns_parser.n_num - 1:
break
# User wants to access specific article
else:
article = seeking_alpha_model.get_article_data(ns_parser.n_id)
print(
article["publishedAt"][: article["publishedAt"].rfind(":") - 3].replace(
"T", " "
),
" ",
article["title"],
)
print(article["url"])
print("")
print(article["content"])
def trending_news_view(other_args: List[str]):
"""Prints the trending news article list
Parameters
----------
other_args : List[str]
argparse other args - ["i", "123123", "-n", "5"]
"""
parser = argparse.ArgumentParser(
add_help=False,
prog="trending",
description="""Trending news articles. [Source: Seeking Alpha]""",
)
parser.add_argument(
"-i",
"--id",
action="store",
dest="n_id",
type=check_positive,
default=-1,
help="article ID number",
)
parser.add_argument(
"-n",
"--num",
action="store",
dest="n_num",
type=check_positive,
default=10,
help="number of articles being printed",
)
if other_args:
if "-" not in other_args[0]:
other_args.insert(0, "-i")
ns_parser = parse_known_args_and_warn(parser, other_args)
if not ns_parser:
return
# User wants to see all trending articles
if ns_parser.n_id == -1:
articles = seeking_alpha_model.get_trending_list(ns_parser.n_num)
for idx, article in enumerate(articles):
print(
article["publishedAt"].replace("T", " ").replace("Z", ""),
"-",
article["id"],
"-",
article["title"],
)
print(article["url"])
print("")
if idx >= ns_parser.n_num - 1:
break
# User wants to access specific article
else:
article = seeking_alpha_model.get_article_data(ns_parser.n_id)
print(
article["publishedAt"][: article["publishedAt"].rfind(":") - 3].replace(
"T", " "
),
" ",
article["title"],
)
print(article["url"])
print("")
print(article["content"])
| python |
import os
import ntpath
from preprocessing.segmentation import segment
from preprocessing.augment import augment
from CNN.recognize_character import recognize
from Unicode.seqgen import sequenceGen
from Unicode.printdoc import unicode_to_kn
def segmentation_call(image):
rootdir = 'web_app/hwrkannada/hwrapp/static/hwrapp/images/Processed_' + \
os.path.splitext(ntpath.basename(image))[0]
if not os.path.exists(rootdir):
os.makedirs(rootdir)
dir = rootdir + '/Segmented_' + os.path.splitext(ntpath.basename(image))[0]
# call the segmentation script on the image
segment(image)
return rootdir, dir
def augmentation_call(image, rootdir):
augdir = rootdir + '/Augmented_' + \
os.path.splitext(ntpath.basename(image))[0]
# augment each of the segmented images
augment(rootdir, augdir)
return augdir
def prediction_call(augdir):
# recognize all images in the directory
predictions = recognize(os.path.join(os.getcwd(), augdir))
# generate the Unicode sequence based on predictions
sequence = sequenceGen(predictions)
# generate Kannada text from the Unicode sequence
kannada_text = unicode_to_kn(sequence)
return(kannada_text)
| python |
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from authors.apps.authentication.models import User
class ReadStats(models.Model):
"""
Users read statistics
"""
user = models.OneToOneField(User, on_delete=models.CASCADE, db_index=True)
reads = models.PositiveIntegerField(default=0)
views = models.PositiveIntegerField(default=0)
@receiver(post_save, sender=User)
def create_user_stats(sender, instance, created, **kwargs):
"""
Creates the user statistics on save of the user
model
"""
if created:
ReadStats.objects.create(user=instance)
| python |
import matplotlib.pyplot as plt
from flask import Flask
from flask_cors import CORS
from api.v1 import api_v1
app = Flask(__name__, static_url_path='', static_folder='frontend')
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
app.register_blueprint(api_v1, url_prefix='/api/v1')
app.config.SWAGGER_UI_DOC_EXPANSION = 'list'
plt.style.use('ggplot')
@app.route('/')
def default():
return app.send_static_file('index.html')
# import requests
# @app.route('/', defaults={'path': ''})
# @app.route('/<path:path>')
# def frontend_proxy(path):
# return requests.get('http://localhost:8080/{}'.format(path)).content
if __name__ == '__main__':
app.run()
| python |
from datetime import datetime
from django.utils import timezone
import factory
from .. import models
from faker.generator import random
random.seed(0xDEADBEEF)
class BundleFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Bundle
easydita_id = factory.Faker('first_name')
easydita_resource_id = factory.Faker('last_name')
time_queued = factory.LazyFunction(timezone.now)
| python |
from argparse import ArgumentParser
from irun.compiler import compile_node, construct
from irun.parser import parse
def compile_irun(source):
tree = parse(source)
rql_context = compile_node(tree)
return construct(rql_context)
def main(argv=None):
parser = ArgumentParser()
parser.add_argument("-c", "--cli", help="input from command line")
parser.add_argument("-f", "--file", help="input from file")
options = parser.parse_args(argv)
if options.cli:
source = options.cli
elif options.file:
with open(options.file) as stream:
source = stream.read()
else:
raise ValueError("run.py expects either -c/--cli or -f/--file to operate")
print(compile_irun(source))
if __name__ == "__main__":
main()
| python |
import torch
from torch.autograd import Variable
import render_pytorch
import image
import camera
import material
import light
import shape
import numpy as np
resolution = [256, 256]
position = Variable(torch.from_numpy(np.array([0, 0, -5], dtype=np.float32)))
look_at = Variable(torch.from_numpy(np.array([0, 0, 0], dtype=np.float32)))
up = Variable(torch.from_numpy(np.array([0, 1, 0], dtype=np.float32)))
fov = Variable(torch.from_numpy(np.array([45.0], dtype=np.float32)))
clip_near = Variable(torch.from_numpy(np.array([0.01], dtype=np.float32)))
clip_far = Variable(torch.from_numpy(np.array([10000.0], dtype=np.float32)))
cam = camera.Camera(position = position,
look_at = look_at,
up = up,
cam_to_world = None,
fov = fov,
clip_near = clip_near,
clip_far = clip_far,
resolution = resolution)
mat_grey=material.Material(\
diffuse_reflectance=torch.from_numpy(np.array([0.5,0.5,0.5],dtype=np.float32)))
materials=[mat_grey]
vertices=Variable(torch.from_numpy(\
np.array([[-1.3,1.0,0.0], [1.0,1.0,0.0], [-0.5,-2.0,-7.0]],dtype=np.float32)))
indices=torch.from_numpy(np.array([[0,1,2]],dtype=np.int32))
shape_triangle=shape.Shape(vertices,indices,None,None,0)
light_vertices=Variable(torch.from_numpy(\
np.array([[-1,-1,-7],[1,-1,-7],[-1,1,-7],[1,1,-7]],dtype=np.float32)))
light_indices=torch.from_numpy(\
np.array([[0,1,2],[1,3,2]],dtype=np.int32))
shape_light=shape.Shape(light_vertices,light_indices,None,None,0)
shapes=[shape_triangle,shape_light]
light_intensity=torch.from_numpy(\
np.array([20,20,20],dtype=np.float32))
light=light.Light(1,light_intensity)
lights=[light]
args=render_pytorch.RenderFunction.serialize_scene(\
cam,materials,shapes,lights,resolution,256,1)
# To apply our Function, we use Function.apply method. We alias this as 'render'.
render = render_pytorch.RenderFunction.apply
img = render(0, *args)
image.imwrite(img.data.numpy(), 'test/results/test_single_triangle_clipped/target.exr')
image.imwrite(img.data.numpy(), 'test/results/test_single_triangle_clipped/target.png')
target = Variable(torch.from_numpy(image.imread('test/results/test_single_triangle_clipped/target.exr')))
shape_triangle.vertices = Variable(torch.from_numpy(\
np.array([[-1.0,1.5,0.3], [0.9,1.2,-0.3], [0.0,-3.0,-6.5]],dtype=np.float32)),
requires_grad=True)
args=render_pytorch.RenderFunction.serialize_scene(cam,materials,shapes,lights,resolution,256,1)
img = render(1, *args)
image.imwrite(img.data.numpy(), 'test/results/test_single_triangle_clipped/init.png')
diff = torch.abs(target - img)
image.imwrite(diff.data.numpy(), 'test/results/test_single_triangle_clipped/init_diff.png')
optimizer = torch.optim.Adam([shape_triangle.vertices], lr=2e-2)
for t in range(200):
optimizer.zero_grad()
# Forward pass: render the image
args=render_pytorch.RenderFunction.serialize_scene(\
cam,materials,shapes,lights,resolution,4,1)
img = render(t+1, *args)
image.imwrite(img.data.numpy(), 'test/results/test_single_triangle_clipped/iter_{}.png'.format(t))
loss = (img - target).pow(2).sum()
print('loss:', loss.item())
loss.backward()
print('grad:', shape_triangle.vertices.grad)
optimizer.step()
print('vertices:', shape_triangle.vertices)
args=render_pytorch.RenderFunction.serialize_scene(\
cam,materials,shapes,lights,resolution,256,1)
img = render(202, *args)
image.imwrite(img.data.numpy(), 'test/results/test_single_triangle_clipped/final.exr')
image.imwrite(img.data.numpy(), 'test/results/test_single_triangle_clipped/final.png')
image.imwrite(np.abs(target.data.numpy() - img.data.numpy()), 'test/results/test_single_triangle_clipped/final_diff.png')
from subprocess import call
call(["ffmpeg", "-framerate", "24", "-i",
"test/results/test_single_triangle_clipped/iter_%d.png", "-vb", "20M",
"test/results/test_single_triangle_clipped/out.mp4"]) | python |
f=open("./CoA/2020/data/02a.txt","r")
valid=0
for line in f:
first=int(line[:line.index("-")])
print(first)
second=int(line[line.index("-")+1:line.index(" ")])
print(second)
rule = line[line.index(" ")+1:line.index(":")]
print(rule)
code = line[line.index(":")+2:]
print(code)
if code[first-1]==rule and code[second-1]!=rule:
valid+=1
print("found 1st "+code[first-1]+code[second-1] )
elif code[second-1]==rule and code[first-1]!=rule:
#elif code[second-1]==rule: #FOUT!! want sluit niet dubbeling uit
valid+=1
print("found 2nd "+code[first-1]+code[second-1] )
print(valid)
f.close() | python |
## An implementation of the credential scheme based on an algebraic
## MAC proposed by Chase, Meiklejohn and Zaverucha in Algebraic MACs and Keyed-Verification
## Anonymous Credentials", at ACM CCS 2014. The credentials scheme
## is based on the GGM based aMAC. (see section 4.2, pages 8-9)
from amacs import *
from genzkp import ZKEnv, ZKProof, ConstGen, Gen, Sec, ConstPub, Pub
from petlib.bn import Bn
def cred_setup():
""" Generates the parameters of the algebraic MAC scheme"""
params = setup_ggm()
return params
def cred_CredKeyge(params, n):
""" Generates keys and parameters for the credential issuer """
_, g, h, o = params
sk, iparams = keyGen_ggm(params, n)
x0_bar = o.random()
Cx0 = sk[0] * g + x0_bar * h
return (Cx0, iparams), (sk, x0_bar)
def cred_UserKeyge(params):
""" Generates keys and parameters for credential user """
G, g, h, o = params
priv = o.random()
pub = priv * g # This is just an EC El-Gamal key
return (priv, pub)
def secret_proof(params, n):
""" Builds a proof of correct El-Gamal encryption for a number of secret attributes. """
G, _, _, _ = params
# Contruct the proof
zk = ZKProof(G)
# Some constants and secrets
pub, g, h = zk.get(ConstGen, ["pub", "g", "h"])
priv = zk.get(Sec, "priv")
## The El-Gamal ciphertexts and secrets
ris = zk.get_array(Sec, "ri", n)
attrs = zk.get_array(Sec, "attri", n)
sKis = zk.get_array(ConstGen, "sKi", n)
Cis = zk.get_array(ConstGen, "Ci", n)
# The proof obligations
zk.add_proof(pub, priv * g)
for (Ci, sKi, ri, attr) in zip(Cis, sKis, ris, attrs):
zk.add_proof(sKi, ri * g)
zk.add_proof(Ci, ri * pub + attr * g)
return zk
def cred_secret_issue_user(params, keypair, attrib):
""" Encodes a number of secret attributes to be issued. """
# We simply encrypt all parameters and make a proof we know
# the decryption.
G, g, h, o = params
priv, pub = keypair
ris = []
sKis = []
Cis = []
for i, attr in enumerate(attrib):
ri = o.random()
ris += [ri]
sKis += [ri * g]
Cis += [ri * pub + attr * g]
zk = secret_proof(params, len(attrib))
## Run the proof
env = ZKEnv(zk)
env.g, env.h = g, h
env.pub = pub
env.priv = priv
env.ri = ris
env.attri = attrib
env.sKi = sKis
env.Ci = Cis
## Extract the proof
sig = zk.build_proof(env.get())
return (pub, (sKis, Cis), sig)
def _check_enc(params, keypair, EGenc, attrib):
G, g, h, o = params
priv, pub = keypair
for (a, b, atr) in zip(EGenc[0], EGenc[1], attrib):
assert (b - (priv * a)) == (atr * g)
def cred_secret_issue_user_check(params, pub, EGenc, sig):
""" Check the encrypted attributes of a user are well formed.
"""
G, g, h, o = params
(sKis, Cis) = EGenc
## First check the inputs (EG ciphertexts) are well formed.
assert len(sKis) == len(Cis)
zk = secret_proof(params, len(Cis))
## Run the proof
env = ZKEnv(zk)
env.g, env.h = g, h
env.pub = pub
env.sKi = sKis
env.Ci = Cis
## Extract the proof
if not zk.verify_proof(env.get(), sig):
raise Exception("Proof of knowledge of plaintexts failed.")
return True
def cred_secret_issue_proof(params, num_privs, num_pubs):
""" The proof that the mixed public / private credential issuing is correct """
G, _, _, _ = params
n = num_privs + num_pubs
# Contruct the proof
zk = ZKProof(G)
## The variables
bCx0 = zk.get(Gen, "bCx_0")
u, g, h, Cx0, pub = zk.get(ConstGen, ["u", "g", "h", "Cx_0", "pub"])
b, x0, x0_bar, bx0, bx0_bar = zk.get(Sec, ["b", "x_0", "x_0_bar", "bx_0", "bx_0_bar"])
xis = zk.get_array(Sec, "xi", n, 1)
bxis = zk.get_array(Sec, "bxi", n, 1)
Xis = zk.get_array(ConstGen, "Xi", n, 1)
bXis = zk.get_array(Gen, "bXi", n, 1)
## Proof of knowing the secret of MAC
zk.add_proof(Cx0, x0 * g + x0_bar * h)
zk.add_proof(bCx0, b * Cx0)
zk.add_proof(bCx0, bx0 * g + bx0_bar * h)
zk.add_proof(u, b * g)
## Proof of correct Xi's
for (xi, Xi, bXi, bxi) in zip(xis, Xis, bXis, bxis):
zk.add_proof(Xi, xi * h)
zk.add_proof(bXi, b * Xi)
zk.add_proof(bXi, bxi * h)
# Proof of correct Credential Ciphertext
mis = zk.get_array(ConstPub, "mi", num_pubs)
CredA, CredB = zk.get(ConstGen, ["CredA", "CredB"])
EGa = zk.get_array(ConstGen, "EGai", num_privs)
EGb = zk.get_array(ConstGen, "EGbi", num_privs)
r_prime = zk.get(Sec, "r_prime")
A = r_prime * g
B = r_prime * pub + bx0 * g
for mi, bxi in zip(mis, bxis[:num_pubs]):
B = B + bxi * (mi * g)
bxis_sec = bxis[num_pubs:num_pubs + num_privs]
for eg_a, eg_b, bxi in zip(EGa, EGb, bxis_sec):
A = A + bxi * eg_a
B = B + bxi * eg_b
zk.add_proof(CredA, A)
zk.add_proof(CredB, B)
return zk
def cred_secret_issue(params, pub, EGenc, publics, secrets, messages):
""" Encode a mixture of secret (EGenc) and public (messages) attributes"""
# Parse variables
G, g, h, o = params
sk, x0_bar = secrets
Cx0, iparams = publics
(sKis, Cis) = EGenc
assert len(sKis) == len(Cis)
assert len(iparams) == len(messages) + len(Cis)
# Get a blinding b
b = o.random()
u = b * g
bx0_bar = b.mod_mul(x0_bar, o)
bsk = []
for xi in sk:
bsk += [b.mod_mul(xi, o)]
bCx0 = b * Cx0
bXi = []
for Xi in iparams:
bXi += [b * Xi]
bsk0 = bsk[0]
open_bsk = bsk[1:len(messages)+1]
sec_bsk = bsk[len(messages)+1:len(messages)+1+len(Cis)]
assert [bsk0] + open_bsk + sec_bsk == bsk
# First build a proto-credential in clear using all public attribs
r_prime = o.random()
EG_a = r_prime * g
EG_b = r_prime * pub + bsk0 * g
for mi, bxi in zip(messages, open_bsk):
EG_b = EG_b + (bxi.mod_mul(mi,o) * g)
for (eg_ai, eg_bi, bxi) in zip(sKis, Cis, sec_bsk):
EG_a = EG_a + bxi * eg_ai
EG_b = EG_b + bxi * eg_bi
# Now build an epic proof for all this.
zk = cred_secret_issue_proof(params, len(Cis), len(messages))
env = ZKEnv(zk)
env.pub = pub
env.g, env.h = g, h
env.u = u
env.b = b
# These relate to the proof of x0 ...
env.x_0 = sk[0]
env.bx_0 = bsk0
env.x_0_bar = x0_bar
env.bx_0_bar = b.mod_mul(x0_bar, o)
env.Cx_0 = Cx0
env.bCx_0 = bCx0
# These relate to the knowledge of Xi, xi ...
env.xi = sk[1:]
env.Xi = iparams
env.bxi = bsk[1:]
env.bXi = bXi
# These relate to the knowledge of the plaintext ...
env.r_prime = r_prime
env.mi = messages
env.CredA = EG_a
env.CredB = EG_b
env.EGai = sKis
env.EGbi = Cis
## Extract the proof
sig = zk.build_proof(env.get())
if __debug__:
assert zk.verify_proof(env.get(), sig, strict=False)
return u, (EG_a, EG_b), sig
def _internal_ckeck(keypair, u, EncE, secrets, all_attribs):
""" Check the invariant that the ciphertexts are the encrypted attributes """
## First do decryption
priv, pub = keypair
(a, b) = EncE
Cred = b - (priv * a)
sk, _ = secrets
v = Hx(sk, all_attribs)
assert Cred == v * u
def cred_secret_issue_user_decrypt(params, keypair, u, EncE, publics, messages, EGab, sig):
""" Decrypts the private / public credential and checks the proof of its correct generation """
G, g, h, _ = params
Cx0, iparams = publics
priv, pub = keypair
(EG_a, EG_b) = EncE
uprime = EG_b - (priv * EG_a)
sKis, Cis = EGab
# Now build an epic proof for all this.
zk = cred_secret_issue_proof(params, len(Cis), len(messages))
env = ZKEnv(zk)
env.g, env.h = g, h
env.u = u
env.Cx_0 = Cx0
env.pub = pub
env.Xi = iparams
env.mi = messages
env.CredA = EG_a
env.CredB = EG_b
env.EGai = sKis
env.EGbi = Cis
## Extract the proof
if not zk.verify_proof(env.get(), sig):
raise Exception("Decryption of credential failed.")
return (u, uprime)
def cred_issue_proof(params, n):
""" The proof of public credential generation """
G, _, _, _ = params
# Contruct the proof
zk = ZKProof(G)
## The variables
u, up, g, h, Cx0 = zk.get(ConstGen, ["u", "up", "g", "h", "Cx0"])
x0, x0_bar = zk.get(Sec, ["x0", "x0_bar"])
xis = zk.get_array(Sec, "xi", n)
mis = zk.get_array(ConstPub, "mi", n)
Xis = zk.get_array(ConstGen, "Xi", n)
## Proof of correct MAC
Prod = x0 * u
for (xi, mi) in zip(xis, mis):
Prod = Prod + xi*(mi * u)
zk.add_proof(up, Prod)
## Proof of knowing the secret of MAC
zk.add_proof(Cx0, x0 * g + x0_bar * h)
## Proof of correct Xi's
for (xi, Xi) in zip(xis, Xis):
zk.add_proof(Xi, xi * h)
return zk
def cred_issue(params, publics, secrets, messages):
# Parse variables
G, g, h, _ = params
sk, x0_bar = secrets
Cx0, iparams = publics
(u, uprime) = mac_ggm(params, sk, messages)
# Build the proof and associate real variables
n = len(messages)
zk = cred_issue_proof(params, n)
env = ZKEnv(zk)
env.g, env.h = g, h
env.u, env.up = u, uprime
env.x0 = sk[0]
env.x0_bar = x0_bar
env.Cx0 = Cx0
env.xi = sk[1:]
env.mi = messages
env.Xi = iparams
## Extract the proof
sig = zk.build_proof(env.get())
if __debug__:
assert zk.verify_proof(env.get(), sig, strict=False)
## Return the credential (MAC) and proof of correctness
return (u, uprime), sig
def cred_issue_check(params, publics, mac, sig, messages):
# Parse public variables
G, g, h, _ = params
Cx0, iparams = publics
(u, uprime) = mac
# Build the proof and assign public variables
n = len(messages)
zk = cred_issue_proof(params, n)
env = ZKEnv(zk)
env.g, env.h = g, h
env.u, env.up = u, uprime
env.Cx0 = Cx0
env.mi = messages
env.Xi = iparams
# Return the result of the verification
return zk.verify_proof(env.get(), sig)
def cred_show_proof(params, n):
G, _, _, _ = params
# Contruct the proof
zk = ZKProof(G)
## The variables
u, g, h = zk.get(ConstGen, ["u", "g", "h"])
V = zk.get(ConstGen, "V")
minus_one = zk.get(ConstPub, "minus1")
r = zk.get(Sec, "r")
zis = zk.get_array(Sec, "zi", n)
mis = zk.get_array(Sec, "mi", n)
Xis = zk.get_array(ConstGen, "Xi", n)
Cmis = zk.get_array(ConstGen, "Cmi", n)
# Define the relations to prove
Vp = r * (minus_one * g)
for zi, Xi in zip(zis, Xis):
Vp = Vp + (zi * Xi)
zk.add_proof(V, Vp)
for (Cmi, mi, zi) in zip(Cmis, mis, zis):
zk.add_proof(Cmi, mi*u + zi*h)
return zk
def cred_show(params, publics, mac, sig, messages, cred_show_proof=cred_show_proof, xenv=None, export_zi=False):
## Parse and re-randomize
G, g, h, o = params
Cx0, iparams = publics
## WARNING: this step not in paper description of protocol
# Checked correctness with Sarah Meiklejohn.
u, uprime = rerandomize_sig_ggm(params, mac)
n = len(messages)
## Blinding variables for the proof
r = o.random()
zis = [o.random() for _ in range(n)]
Cup = uprime + r * g
Cmis = [mi * u + zi * h for (mi, zi) in zip(messages, zis)]
cred = (u, Cmis, Cup)
V = r * ( (-1) * g)
for zi, Xi in zip(zis, iparams):
V = V + zi * Xi
# Define the proof, and instanciate it with variables
zk = cred_show_proof(params, n)
env = ZKEnv(zk)
env.u = u
env.g, env.h = g, h
env.V = V
env.r = r
env.minus1 = -Bn(1)
env.zi = zis
env.mi = messages
env.Xi = iparams
env.Cmi = Cmis
if xenv:
xenv(env)
sig = zk.build_proof(env.get())
## Just a sanity check
if __debug__:
assert zk.verify_proof(env.get(), sig, strict=False)
if export_zi:
return cred, sig, zis
else:
return cred, sig
def cred_show_check(params, publics, secrets, creds, sig, cred_show_proof=cred_show_proof, xenv={}):
# Parse the inputs
G, g, h, _ = params
sk, _ = secrets
Cx0, iparams = publics
(u, Cmis, Cup) = creds
n = len(iparams)
## Recompute a V
V = sk[0] * u + (- Cup)
for xi, Cmi in zip(sk[1:], Cmis):
V = V + xi * Cmi
# Define the proof, and instanciate it with variables
zk = cred_show_proof(params, n)
env = ZKEnv(zk)
env.u = u
env.g, env.h = g, h
env.V = V
env.minus1 = -Bn(1)
env.Xi = iparams
env.Cmi = Cmis
if xenv:
xenv(env)
# Return the result of the verification
return zk.verify_proof(env.get(), sig)
def time_it_all(repetitions = 1000):
import time
print("Timings of operations (%s repetitions)" % repetitions)
t0 = time.clock()
for _ in range(repetitions):
i = 0
T = time.clock() - t0
print("%.3f ms\tIdle" % (1000 * T/repetitions))
t0 = time.clock()
for _ in range(repetitions):
## Setup from credential issuer.
params = cred_setup()
T = time.clock() - t0
print("%.3f ms\tCredential Group Setup" % (1000 * T/repetitions))
G, _, _, o = params
## Attriutes we want to encode
public_attr = [o.random(), o.random()]
private_attr = [o.random(), o.random()]
n = len(public_attr) + len(private_attr)
t0 = time.clock()
for _ in range(repetitions):
ipub, isec = cred_CredKeyge(params, n)
T = time.clock() - t0
print("%.3f ms\tCredential Key generation" % (1000 * T/repetitions))
## User generates keys and encrypts some secret attributes
# the secret attributes are [10, 20]
t0 = time.clock()
for _ in range(repetitions):
keypair = cred_UserKeyge(params)
T = time.clock() - t0
print("%.3f ms\tUser Key generation" % (1000 * T/repetitions))
t0 = time.clock()
for _ in range(repetitions):
pub, EGenc, sig = cred_secret_issue_user(params, keypair, private_attr)
T = time.clock() - t0
print("%.3f ms\tUser Key generation (proof)" % (1000 * T/repetitions))
if __debug__:
_check_enc(params, keypair, EGenc, private_attr)
## The issuer checks the secret attributes and encrypts a amac
# It also includes some public attributes, namely [30, 40].
t0 = time.clock()
for _ in range(repetitions):
if not cred_secret_issue_user_check(params, pub, EGenc, sig):
raise Exception("User key generation invalid")
T = time.clock() - t0
print("%.3f ms\tUser Key generation (verification)" % (1000 * T/repetitions))
t0 = time.clock()
for _ in range(repetitions):
u, EncE, sig = cred_secret_issue(params, pub, EGenc, ipub, isec, public_attr)
T = time.clock() - t0
print("%.3f ms\tCredential issuing" % (1000 * T/repetitions))
if __debug__:
_internal_ckeck(keypair, u, EncE, isec, public_attr + private_attr)
## The user decrypts the amac
t0 = time.clock()
for _ in range(repetitions):
mac = cred_secret_issue_user_decrypt(params, keypair, u, EncE, ipub, public_attr, EGenc, sig)
T = time.clock() - t0
print("%.3f ms\tCredential decryption & verification" % (1000 * T/repetitions))
## The show protocol using the decrypted amac
# The proof just proves knowledge of the attributes, but any other
# ZK statement is also possible by augmenting the proof.
t0 = time.clock()
for _ in range(repetitions):
(creds, sig) = cred_show(params, ipub, mac, sig, public_attr + private_attr)
T = time.clock() - t0
print("%.3f ms\tCredential Show (proof)" % (1000 * T/repetitions))
t0 = time.clock()
for _ in range(repetitions):
if not cred_show_check(params, ipub, isec, creds, sig):
raise Exception("Credential show failed.")
T = time.clock() - t0
print("%.3f ms\tCredential Show (verification)" % (1000 * T/repetitions))
def test_creds():
## Setup from credential issuer.
params = cred_setup()
ipub, isec = cred_CredKeyge(params, 2)
## Credential issuing and checking
mac, sig = cred_issue(params, ipub, isec, [10, 20])
assert cred_issue_check(params, ipub, mac, sig, [10, 20])
## The show protocol
(creds, sig) = cred_show(params, ipub, mac, sig, [10, 20])
assert cred_show_check(params, ipub, isec, creds, sig)
def test_creds_custom_show():
## Test attaching custom proofs to the show prototcol
# for the credential scheme. This should work with both
# all public and partly secret attributes.
## Setup from credential issuer. Can also setup with secrets (see test_secret_creds)
params = cred_setup()
ipub, isec = cred_CredKeyge(params, 2)
## Credential issuing and checking
mac, sig = cred_issue(params, ipub, isec, [10, 20])
assert cred_issue_check(params, ipub, mac, sig, [10, 20])
## Custom proofs require two things:
# - cred_show_proof_custom: a custom "cred_show_proof" with additional statements
# to prove on the Commitements Cmi = mi * u + zi * h
# - xenv: a custom function that instanciates the values of the proof, either
# public secret or constant.
# Example: Prove that the second attribute is double the first
def cred_show_proof_custom(params, n):
zk = cred_show_proof(params, n)
u, g, h = zk.get(ConstGen, ["u", "g", "h"])
zis = zk.get_array(Sec, "zi", n)
mis = zk.get_array(Sec, "mi", n)
Cmis = zk.get_array(ConstGen, "Cmi", n)
twou = zk.get(ConstGen, "twou")
# Statement that proves Cmi1 = (2 * m0) * u + z1 * h
zk.add_proof(Cmis[1], mis[0]*twou + zis[1]*h)
return zk
def xenv(env):
# Ensure the constant 2u is correct, both ends.
env.twou = 2 * env.u
## The show protocol -- note the use of "cred_show_proof_custom" and "xenv"
(creds, sig) = cred_show(params, ipub, mac, sig, [10, 20], cred_show_proof_custom, xenv)
assert cred_show_check(params, ipub, isec, creds, sig, cred_show_proof_custom, xenv)
def test_secret_creds():
## Setup from credential issuer.
params = cred_setup()
## Attriutes we want to encode
public_attr = [30, 40]
private_attr = [10, 20]
n = len(public_attr) + len(private_attr)
ipub, isec = cred_CredKeyge(params, n)
## User generates keys and encrypts some secret attributes
# the secret attributes are [10, 20]
keypair = cred_UserKeyge(params)
pub, EGenc, sig = cred_secret_issue_user(params, keypair, private_attr)
if __debug__:
_check_enc(params, keypair, EGenc, private_attr)
## The issuer checks the secret attributes and encrypts a amac
# It also includes some public attributes, namely [30, 40].
assert cred_secret_issue_user_check(params, pub, EGenc, sig)
u, EncE, sig = cred_secret_issue(params, pub, EGenc, ipub, isec, public_attr)
if __debug__:
_internal_ckeck(keypair, u, EncE, isec, public_attr + private_attr)
## The user decrypts the amac
mac = cred_secret_issue_user_decrypt(params, keypair, u, EncE, ipub, public_attr, EGenc, sig)
## The show protocol using the decrypted amac
# The proof just proves knowledge of the attributes, but any other
# ZK statement is also possible by augmenting the proof.
(creds, sig) = cred_show(params, ipub, mac, sig, public_attr + private_attr)
assert cred_show_check(params, ipub, isec, creds, sig)
if __name__ == "__main__":
time_it_all(repetitions=100)
params = cred_setup()
print("Proof of secret attributes")
zk1 = secret_proof(params, 2)
print(zk1.render_proof_statement())
print("Proof of secret issuing")
zk2 = cred_secret_issue_proof(params, 2, 2)
print(zk2.render_proof_statement())
print("Proof of public issuing")
zk3 = cred_issue_proof(params, 2)
print(zk3.render_proof_statement())
print("Proof of credential show")
zk4 = cred_show_proof(params, 4)
print(zk4.render_proof_statement())
| python |
import pygame
import math
from Tower import *
pygame.init()
class T_SuperTower(Tower):
def __init__(Self , sc , Images):
Self.L1 = Images
Self.image = Self.L1[0]
Self.level = 5
Self.range = 100
Self.damage = 100
Self.x = 0
Self.y = 0
Self.bulletx = 0
Self.bullety = 0
Self.angle = 0
Self.cooldown = 0
Self.screen = sc
Self.target = 0
Self.reset = 120
Self.color = (255 , 0 , 0)
| python |
import os
import tempfile
class Config:
IS_TRAIN = True # Set whether you want to Train (True) or Predict (False)
TICKER = 'EURUSD'
num_of_rows_read = 1000 # If set 0 then all the rows will be read
# Set MySQL inputs if True
IS_MYSQL = False
MYSQL_USER = 'Write your user name'
MYSQL_PASSWORD = 'Write your password'
MYSQL_HOST = 'Write the IP address of the MySQL'
MYSQL_DATABASE = 'Write the name of the database where your dataset can be found'
MYSQL_PORT = 0 # your mysql port number
MYSQL_HOST_PORT = MYSQL_HOST +':'+ str(MYSQL_PORT)
# Env params
env_name = 'trading-v0'
number_of_actions = 3 # Short (0), Flat (1), Long (2)
observation_dimension = 27 # Number of Features (you have to change it unless you have 27 features of your dataset)
gamma = 0.9
decay = 0.9
execution_penalty = 0.0001 #0.001
timestep_penalty = 0.0001
# Set the adaptive learning rate
# Changing points in episode number
first_lr_change = 500
sec_lr_change = 60000
third_lr_change = 80000
# Learning rate values
first_lr = 1e-4
sec_lr = 1e-3
third_lr = 1e-3
# Training params
NO_OF_EPISODES = 10000
LOG_FREQ = 10
LOGDIR = '/tensorboard/' # Log path for the tensorboard
MODEL_DIR = 'model/' # Path for saving models
# Extensions
csv_file = '.csv'
input_predict_extension = '_input_predict' + csv_file
simnet = 'simnet/'
simnet_path_extension = '_simnet.csv'
actions_path_extension = '_actions.csv'
# Path sources
INPUT_PREDICT_DATA_PATH = os.path.join('datasets', 'input_predict/')
TRAINING_DATA_PATH = os.path.join('datasets', 'training/')
PLOT_PATH = 'plot/'
OUTPUT_PREDICT_PATH = os.path.join('datasets', 'output_predict/') | python |
from typing import Any
class TonException(Exception):
def __init__(self, error: Any):
if type(error) is dict:
error = f"[{error.get('code')}] {error.get('message')} " \
f"(Core: {error.get('data', {}).get('core_version')})"
super(TonException, self).__init__(error)
| python |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# import Flask
'''
Created on Nov 22, 2016
@author: jmartan
'''
import os,signal
import requests
import argparse
import uni_func
import atexit
import unicornhat
def update_widget(codec_ip, username, password, widget_id, value, unset=False):
# "unset" is needed in a situation when you try to repeatedly set the same value of the widget
# and in the mean time someone changes the widget on the touch panel. Probably a bug.
widget_unset_xml = '''
<Command>
<UserInterface>
<Extensions>
<Widget>
<UnsetValue>
<WidgetId>{}</WidgetId>
</UnsetValue>
</Widget>
</Extensions>
</UserInterface>
</Command>
'''.format(widget_id)
widget_set_xml = '''
<Command>
<UserInterface>
<Extensions>
<Widget>
<SetValue>
<WidgetId>{}</WidgetId>
<Value>{}</Value>
</SetValue>
</Widget>
</Extensions>
</UserInterface>
</Command>
'''.format(widget_id, value)
# print('about to send: {}'.format(widget_xml))
print('sending XML command to codec {}, id: {}, value: {}'.format(codec_ip, widget_id, value))
headers = {'content-type':'text/xml'}
if unset:
res = requests.post('http://'+codec_ip+'/putxml', data=widget_unset_xml, headers=headers, auth=(username, password), timeout=1)
print('unset result: {}'.format(res))
res = requests.post('http://'+codec_ip+'/putxml', data=widget_set_xml, headers=headers, auth=(username, password), timeout=1)
print('set result: {}'.format(res))
# run the application
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Set widget values.')
parser.add_argument('widget_value', metavar='N', nargs='+',
help='"widget_id=value" list')
parser.add_argument('-c', dest='codec_ip', required=True,
help='codec ip address')
parser.add_argument('-u', dest='username', required=True,
help='codec API username')
parser.add_argument('-p', dest='password', required=True,
help='codec API password')
in_args = parser.parse_args()
print("args: {}".format(in_args))
# do not switch the LEDs off
atexit.unregister(unicornhat._clean_shutdown)
color_widgets = ['red', 'green', 'blue']
red, green, blue = (0, 0, 0)
update_color_widgets = False
for arg in in_args.widget_value:
widget_id, value = arg.split('=')
if widget_id == 'red':
red = int(value)
update_color_widgets = True
elif widget_id == 'green':
green = int(value)
update_color_widgets = True
elif widget_id == 'blue':
blue = int(value)
update_color_widgets = True
print('red: {}, green: {}, blue: {}'.format(red, green, blue))
if not widget_id in color_widgets:
update_widget(in_args.codec_ip, in_args.username, in_args.password, widget_id, value)
# time.sleep(0.3)
if update_color_widgets:
uni_func.change_fill(red, green, blue)
update_widget(in_args.codec_ip, in_args.username, in_args.password, 'red', red, unset=True)
update_widget(in_args.codec_ip, in_args.username, in_args.password, 'green', green, unset=True)
update_widget(in_args.codec_ip, in_args.username, in_args.password, 'blue', blue, unset=True)
# do not switch the LEDs off - another method
os.kill(os.getpid(), signal.SIGTERM)
'''
sample XML documents to send to codec
Authorization: Basic with API user_id and password
URL: http://<codec_ip>/putxml
Set Value example:
<Command>
<UserInterface>
<Extensions>
<Widget>
<SetValue>
<WidgetId>red</WidgetId>
<Value>128</Value>
</SetValue>
</Widget>
</Extensions>
</UserInterface>
</Command>
Unset Value example:
<Command>
<UserInterface>
<Extensions>
<Widget>
<UnsetValue>
<WidgetId>red</WidgetId>
</UnsetValue>
</Widget>
</Extensions>
</UserInterface>
</Command>
'''
| python |
from slackbot.bot import Bot
from slackbot.bot import respond_to
import re
import foobot_grapher
def main():
bot = Bot()
bot.run()
@respond_to('air quality', re.IGNORECASE)
def air_quality(message):
attachments = [
{
'fallback': 'Air quality graph',
'image_url': foobot_grapher.getSensorReadings(False)
}]
message.send_webapi('', json.dumps(attachments))
if __name__ == "__main__":
main()
| python |
"""
Дан список, заполненный произвольными целыми числами. Найдите в этом списке два числа, произведение которых
максимально. Выведите эти числа в порядке неубывания. Решение должно иметь сложность O(n), где n - размер списка. То
есть сортировку использовать нельзя.
"""
a = list(map(int, input().split()))
negative_max = min(a)
natural_max = max(a)
a.remove(negative_max)
a.remove(natural_max)
negative_prev = min(a)
natural_prev = max(a)
if negative_max * negative_prev > natural_max * natural_prev:
print(min(negative_prev, negative_max), max(negative_prev, negative_max))
else:
print(min(natural_prev, natural_max), max(natural_prev, natural_max))
| python |
from django.utils.translation import ugettext as _
from django.utils import timezone
from django.http import HttpResponse, HttpRequest
from zilencer.models import RemotePushDeviceToken, RemoteZulipServer
from zerver.lib.exceptions import JsonableError
from zerver.lib.push_notifications import send_android_push_notification, \
send_apple_push_notification
from zerver.lib.response import json_error, json_success
from zerver.lib.request import has_request_variables, REQ
from zerver.lib.validator import check_dict, check_int
from zerver.models import UserProfile, PushDeviceToken, Realm
from zerver.views.push_notifications import validate_token
from typing import Any, Dict, Optional, Union, Text, cast
def validate_entity(entity):
# type: (Union[UserProfile, RemoteZulipServer]) -> None
if not isinstance(entity, RemoteZulipServer):
raise JsonableError(_("Must validate with valid Zulip server API key"))
def validate_bouncer_token_request(entity, token, kind):
# type: (Union[UserProfile, RemoteZulipServer], bytes, int) -> None
if kind not in [RemotePushDeviceToken.APNS, RemotePushDeviceToken.GCM]:
raise JsonableError(_("Invalid token type"))
validate_entity(entity)
validate_token(token, kind)
@has_request_variables
def remote_server_register_push(request, entity, user_id=REQ(),
token=REQ(), token_kind=REQ(validator=check_int), ios_app_id=None):
# type: (HttpRequest, Union[UserProfile, RemoteZulipServer], int, bytes, int, Optional[Text]) -> HttpResponse
validate_bouncer_token_request(entity, token, token_kind)
server = cast(RemoteZulipServer, entity)
# If a user logged out on a device and failed to unregister,
# we should delete any other user associations for this token
# & RemoteServer pair
RemotePushDeviceToken.objects.filter(
token=token, kind=token_kind, server=server).exclude(user_id=user_id).delete()
# Save or update
remote_token, created = RemotePushDeviceToken.objects.update_or_create(
user_id=user_id,
server=server,
kind=token_kind,
token=token,
defaults=dict(
ios_app_id=ios_app_id,
last_updated=timezone.now()))
return json_success()
@has_request_variables
def remote_server_unregister_push(request, entity, token=REQ(),
token_kind=REQ(validator=check_int), ios_app_id=None):
# type: (HttpRequest, Union[UserProfile, RemoteZulipServer], bytes, int, Optional[Text]) -> HttpResponse
validate_bouncer_token_request(entity, token, token_kind)
server = cast(RemoteZulipServer, entity)
deleted = RemotePushDeviceToken.objects.filter(token=token,
kind=token_kind,
server=server).delete()
if deleted[0] == 0:
return json_error(_("Token does not exist"))
return json_success()
@has_request_variables
def remote_server_notify_push(request, # type: HttpRequest
entity, # type: Union[UserProfile, RemoteZulipServer]
payload=REQ(argument_type='body') # type: Dict[str, Any]
):
# type: (...) -> HttpResponse
validate_entity(entity)
server = cast(RemoteZulipServer, entity)
user_id = payload['user_id']
gcm_payload = payload['gcm_payload']
apns_payload = payload['apns_payload']
android_devices = list(RemotePushDeviceToken.objects.filter(
user_id=user_id,
kind=RemotePushDeviceToken.GCM,
server=server
))
apple_devices = list(RemotePushDeviceToken.objects.filter(
user_id=user_id,
kind=RemotePushDeviceToken.APNS,
server=server
))
if android_devices:
send_android_push_notification(android_devices, gcm_payload, remote=True)
if apple_devices:
send_apple_push_notification(user_id, apple_devices, apns_payload)
return json_success()
| python |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from conans import ConanFile, CMake, tools
import os
class InjaConan(ConanFile):
name = "inja"
version = "2.1.0"
url = "https://github.com/yasamoka/conan-inja"
description = "Template engine for modern C++, loosely inspired by jinja for Python"
license = "https://github.com/pantor/inja/blob/master/LICENSE"
no_copy_source = True
build_policy = "always"
requires = "jsonformoderncpp/3.7.3@vthiery/stable"
def source(self):
source_url = "https://github.com/pantor/inja"
tools.get("{0}/archive/v{1}.tar.gz".format(source_url, self.version))
extracted_dir = self.name + "-" + self.version
os.rename(extracted_dir, "sources")
#Rename to "sources" is a convention to simplify later steps
def package_id(self):
self.info.header_only()
def package(self):
self.copy(pattern="LICENSE")
self.copy(pattern="*.[i|h]pp", dst="include/inja", src="sources/include/inja", keep_path=True) | python |
class Learner(object):
def log_update(self, o, a, r, op, logpb, dist, done):
self.log(o, a, r, op, logpb, dist, done)
info0 = {'learned': False}
if self.learn_time(done):
info = self.learn()
self.post_learn()
info0.update(info)
info0['learned'] = True
return info0
def log(self, o, a, r, op, logpb, dist, done):
pass
def learn_time(self, done):
pass
def post_learn(self):
pass
def learn(self):
pass
| python |
import os
import shutil
import json
print("[+] Cleaning...")
with open("tree.json", "r") as f:
json_str = f.read()
json_data = json.loads(json_str)
f.close()
for (path, dirs, files) in os.walk(os.curdir):
if path not in json_data["dirs"]:
shutil.rmtree(path)
else:
for f in files:
f = f"{path}{os.sep}{f}"
if f not in json_data["files"]:
os.remove(f)
print("[-] Finished cleaning")
| python |
# BT5071 pop quiz 2
# Roll Number: BE17B037
# Name: Krushan Bauva
def bubble(A):
n = len(A)
if n%2 == 1:
A1 = A[0:n//2+1]
A2 = A[n//2+1:n]
else:
A1 = A[0:n//2]
A2 = A[n//2:n]
n1 = len(A1)
for i in range(n1-1, 0, -1):
for j in range(i):
if A1[j]>A1[j+1]:
A1[j], A1[j+1] = A1[j+1], A1[j]
n2 = len(A2)
for i in range(n2-1):
for j in range(n2-1, i, -1):
if A2[j]>A2[j-1]:
A2[j], A2[j-1] = A2[j-1], A2[j]
return (A1, A2)
# Bubble sort is a stable sort since it does not reorder for equal things. Only when one
# element is greater than the other, it does a mutual swap between them.
# Bubble sort's time complexity is O(n^2). Since the outer loop runs for n-1 times and the inner
# loop runs till the index of the outer loop. So if we add all these we get approx =
# (n-1)^2 + (n-2)^2 + (n-3)^2 + ..... (3)^2 + (2)^2 + (1)^2 = n(n-1)/2 = O(n^2)
# Hence the time complexity of bubble sort is O(n^2).
| python |
from __future__ import unicode_literals
from django.conf import settings
from django.contrib.auth.models import Permission, User
from django.db import models
from localflavor.us.models import USStateField
from phonenumber_field.modelfields import PhoneNumberField
from multiselectfield import MultiSelectField
from endorsements.models import Issue
from django_countries.fields import CountryField
from recurrence.fields import RecurrenceField
from django.contrib.gis.db.models import PointField
from wagtail.contrib.wagtailfrontendcache.utils import purge_url_from_cache
from bsd.api import BSD
import logging
logger = logging.getLogger(__name__)
# Get bsd api
bsdApi = BSD().api
group_rating_choices = (
(5, '5 - Strongly aligned with values and expectations'),
(4, '4 - Somewhat aligned with values and expectations'),
(3, '3 - Working toward alignment with values and expectations'),
(2, '2 - Somewhat misaligned or resistant to values and expectations'),
(1, '1 - Group inactive or very misaligned with values and expectations'),
)
def find_local_group_by_user(user):
"""
Find approved Local Group for User based on Affiliations and Roles
Parameters
----------
user : User
User to check for Local Group match
Returns
-------
LocalGroup
Return LocalGroup if a match is found, or None
"""
"""Find affiliation for approved group with non-empty roles"""
if hasattr(user, 'localgroupprofile'):
local_group_profile = user.localgroupprofile
# TODO: support multiple group affiliations?
local_group_affiliation = LocalGroupAffiliation.objects.filter(
local_group_profile=local_group_profile,
local_group__status__exact='approved',
).exclude(local_group_roles=None).first()
if local_group_affiliation:
local_group = local_group_affiliation.local_group
return local_group
"""Otherwise return None"""
return None
class Group(models.Model):
name = models.CharField(
max_length=64,
null=True, blank=False,
verbose_name="Group Name"
)
slug = models.SlugField(
null=True, blank=False,
unique=True,
max_length=100
)
signup_date = models.DateTimeField(
null=True,
blank=True,
auto_now_add=True
)
group_id = models.CharField(
max_length=4,
null=True,
blank=False,
unique=True
)
# Order by group priority
GROUP_TYPES = (
(1, 'State Organizing Committee'),
(2, 'State Chapter'),
(3, 'Campus'),
(4, 'Local Group')
)
group_type = models.IntegerField(
blank=False,
null=False,
choices=GROUP_TYPES,
default=4
)
# Individual Rep Email should match BSD authentication account
rep_email = models.EmailField(
null=True,
blank=False,
verbose_name="Contact Email",
max_length=254
)
# Public group email does not need to match BSD authentication account
group_contact_email = models.EmailField(
blank=True,
help_text="""Optional Group Contact Email to publicly display an email
different from Group Leader Email""",
max_length=254,
null=True,
)
rep_first_name = models.CharField(
max_length=35,
null=True,
blank=False,
verbose_name="First Name"
)
rep_last_name = models.CharField(
max_length=35,
null=True,
blank=False,
verbose_name="Last Name"
)
rep_postal_code = models.CharField(
max_length=12,
null=True,
blank=True,
verbose_name="Postal Code"
)
rep_phone = PhoneNumberField(
null=True,
blank=True,
verbose_name="Phone Number"
)
county = models.CharField(max_length=64, null=True, blank=True)
city = models.CharField(max_length=64, null=True, blank=True)
state = USStateField(max_length=2, null=True, blank=True)
postal_code = models.CharField(
max_length=12,
null=True,
blank=True,
verbose_name="Postal Code"
)
country = CountryField(null=True, blank=False, default="US")
point = PointField(null=True, blank=True)
size = models.CharField(
max_length=21,
null=True,
blank=True,
verbose_name="Group Size"
)
last_meeting = models.DateTimeField(
null=True,
blank=True,
verbose_name="Date of Last Meeting"
)
recurring_meeting = RecurrenceField(
null=True,
blank=True,
verbose_name="Recurring Meeting"
)
meeting_address_line1 = models.CharField(
"Address Line 1",
max_length=45,
null=True,
blank=True)
meeting_address_line2 = models.CharField(
"Address Line 2",
max_length=45,
null=True,
blank=True
)
meeting_postal_code = models.CharField(
"Postal Code",
max_length=12,
null=True,
blank=True
)
meeting_city = models.CharField(
max_length=64,
null=True,
blank=True,
verbose_name="City"
)
meeting_state_province = models.CharField(
"State/Province",
max_length=40,
null=True,
blank=True
)
meeting_country = CountryField(
null=True,
blank=True,
verbose_name="Country",
default='US'
)
TYPES_OF_ORGANIZING_CHOICES = (
('direct-action', 'Direct Action'),
('electoral', 'Electoral Organizing'),
('legistlative', 'Advocating for Legislation or Ballot Measures'),
('community', 'Community Organizing'),
('other', 'Other')
)
types_of_organizing = MultiSelectField(
null=True,
blank=True,
choices=TYPES_OF_ORGANIZING_CHOICES,
verbose_name="Types of Organizing"
)
other_types_of_organizing = models.TextField(
null=True,
blank=True,
verbose_name="Other Types of Organizing",
max_length=500
)
description = models.TextField(
null=True,
blank=False,
max_length=1000,
verbose_name="Description (1000 characters or less)"
)
issues = models.ManyToManyField(Issue, blank=True)
other_issues = models.TextField(
null=True,
blank=True,
max_length=250,
verbose_name="Other Issues")
constituency = models.TextField(null=True, blank=True, max_length=250)
facebook_url = models.URLField(
null=True,
blank=True,
verbose_name="Facebook URL",
max_length=255
)
twitter_url = models.URLField(
null=True,
blank=True,
verbose_name="Twitter URL",
max_length=255)
website_url = models.URLField(
null=True,
blank=True,
verbose_name="Website URL",
max_length=255
)
instagram_url = models.URLField(
null=True,
blank=True,
verbose_name="Instagram URL",
max_length=255
)
other_social = models.TextField(
null=True,
blank=True,
verbose_name="Other Social Media",
max_length=250
)
STATUSES = (
('submitted', 'Submitted'),
('signed-mou', 'Signed MOU'),
('inactive', 'Inactive'),
('approved', 'Approved'),
('removed', 'Removed')
)
status = models.CharField(
max_length=64,
choices=STATUSES,
default='submitted'
)
VERSIONS = (
('none', 'N/A'),
('1.0', 'Old'),
('1.1', 'Current'),
)
signed_mou_version = models.CharField(
max_length=64,
choices=VERSIONS,
default='none',
verbose_name='MOU Version',
null=True,
blank=True
)
ORGANIZERS = (
('juliana', 'Juliana'),
('basi', 'Basi'),
('kyle', 'Kyle'),
)
organizer = models.CharField(
max_length=64,
choices=ORGANIZERS,
default=None,
verbose_name='Organizer',
null=True,
blank=True
)
mou_url = models.URLField(
null=True,
blank=True,
verbose_name="MOU URL",
max_length=255
)
"""Admin Group Rating"""
group_rating = models.IntegerField(
blank=True,
choices=group_rating_choices,
null=True,
)
# Notes field for internal OR staff use
notes = models.TextField(
blank=True,
help_text="""Please include dates here along with notes to make
reporting easier.""",
null=True,
verbose_name="Notes"
)
def save(self, *args, **kwargs):
# TODO: make main groups url an environment variable
# and replace hardcoded /groups throughout site
super(Group, self).save(*args, **kwargs)
if self.slug:
purge_url_from_cache('/groups/')
purge_url_from_cache('/groups/' + self.slug +'/')
def __unicode__(self):
return self.name
class LocalGroupProfile(models.Model):
"""Local Group information for a user"""
user = models.OneToOneField(User, on_delete=models.CASCADE)
def get_affiliation_for_local_group(self, local_group):
"""Get Affiliation for Local Group, otherwise None"""
affiliation = self.localgroupaffiliation_set.filter(
local_group=local_group
).first()
return affiliation
def get_affiliations_for_local_group_role_id(self, local_group_role_id):
"""Get Affiliations for Local Group Role"""
affiliations = self.localgroupaffiliation_set.filter(
local_group_roles=local_group_role_id
)
return affiliations
def has_permission_for_local_group(self, local_group, permission):
"""Get Affiliation and check if any Role has permission"""
affiliation = self.get_affiliation_for_local_group(local_group)
if affiliation:
for role in affiliation.local_group_roles.all():
if role.has_permission(permission):
return True
return False
def has_permissions_for_local_group(self, local_group, permissions):
"""Verify if user has all permissions for local group"""
for permission in permissions:
if not self.has_permission_for_local_group(
local_group,
permission
):
return False
return True
def __unicode__(self):
return self.user.email + " [" + str(self.user.id) + "]"
class Meta:
ordering = ["user__email"]
class LocalGroupRole(models.Model):
"""Hardcode the role types, but also store role permissions in db"""
role_type_choices = (
(settings.LOCAL_GROUPS_ROLE_GROUP_LEADER_ID, 'Group Leader'),
(settings.LOCAL_GROUPS_ROLE_GROUP_ADMIN_ID, 'Group Admin'),
)
permissions = models.ManyToManyField(
Permission,
blank=True,
)
role_type = models.IntegerField(
choices=role_type_choices,
unique=True
)
def has_permission(self, permission):
for perm in self.permissions.all():
code = perm.content_type.app_label + '.' + perm.codename
if code == permission:
return True
return False
def __unicode__(self):
return self.get_role_type_display()
class LocalGroupAffiliation(models.Model):
"""
Local Group Affiliation is similar to Auth User Groups except it is
meant for a specific Local Group
"""
"""Link to specific User Profile and Local Group"""
local_group = models.ForeignKey(Group)
local_group_profile = models.ForeignKey(LocalGroupProfile)
"""Roles for this specific Local Group & User"""
local_group_roles = models.ManyToManyField(
LocalGroupRole,
blank=True,
)
def __unicode__(self):
return self.local_group.name + " [" + self.local_group.group_id + "], " + str(
self.local_group_profile
)
class Meta:
ordering = [
"local_group__name",
"local_group__group_id",
"local_group_profile__user__email"
]
unique_together = ["local_group", "local_group_profile"]
| python |
# -*- coding: utf-8 -*-createacsr_handler
from __future__ import unicode_literals
import json
import logging
import os
import uuid
import time
import secrets
import cryptography
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
from flask import abort
from flask import Flask
from flask import request
from flask import Response
from flask import render_template
from jinja2.exceptions import TemplateNotFound
from jwcrypto import jwk, jwt
import requests
from werkzeug.contrib.cache import SimpleCache
# ENV vars
FLASK_DEBUG = os.getenv('FLASK_DEBUG', True)
TEMPLATES_FOLDER = os.getenv('TEMPLATES_FOLDER')
CACHE_TIMEOUT = int(os.getenv('CACHE_TIMEOUT'))
TEST_API_ENDPOINT = os.getenv('TEST_API_ENDPOINT')
if FLASK_DEBUG:
# configure requests logging
import http.client as http_client
http_client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
logger = logging.getLogger(__name__)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
app = Flask(__name__, template_folder=TEMPLATES_FOLDER)
app.debug = FLASK_DEBUG
# Setting SECRET_KEY
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', secrets.token_hex(16))
cache = SimpleCache()
################################################################################
# Utilities
################################################################################
def make_private_key(key_size: int) -> bytes:
"""Return an RSA private key
:param key_size:
:return key:
"""
key = rsa.generate_private_key(
public_exponent=65537,
key_size=key_size,
backend=default_backend()
)
return key
def make_private_key_pem(private_key: bytes) -> str:
"""Convert RSA private key to PEM format
:param private_key:
:return pem:
"""
pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
return pem
def make_csr(private_key: bytes) -> str:
"""Return a CSR based on the given private key.
:param private_key:
:return csr:
"""
csr = x509.CertificateSigningRequestBuilder().subject_name(
x509.Name(
[
x509.NameAttribute(NameOID.COUNTRY_NAME, cache.get('csr_country_name') or 'GB'),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME,
cache.get('csr_state_or_province_name') or 'Middlesex'),
x509.NameAttribute(NameOID.LOCALITY_NAME, cache.get('csr_locality_name') or 'London'),
x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME,
cache.get('csr_organizational_unit_name') or 'My TPP'),
x509.NameAttribute(NameOID.COMMON_NAME, cache.get('csr_common_name') or 'IT'),
]
)
).sign(private_key, hashes.SHA256(), default_backend())
return csr
def make_jwk_from_pem(private_pem: str) -> dict:
"""Convert a PEM into a JWK
:param private_pem:
:return jwk_dict:
"""
jwk_dict = dict()
try:
key_obj = jwk.JWK.from_pem(private_pem.encode('latin-1'))
except Exception as e:
app.logger.debug('{}'.format(e))
else:
jwk_dict = json.loads(key_obj.export())
jwk_dict['kid'] = key_obj.thumbprint(hashalg=cryptography.hazmat.primitives.hashes.SHA1())
jwk_dict['x5t'] = key_obj.thumbprint(hashalg=cryptography.hazmat.primitives.hashes.SHA1())
jwk_dict['x5t#256'] = key_obj.thumbprint(hashalg=cryptography.hazmat.primitives.hashes.SHA256())
return jwk_dict
def make_token(kid: str, software_statement_id: str, client_scopes: str, token_url: str) -> str:
jwt_iat = int(time.time())
jwt_exp = jwt_iat + 3600
header = dict(alg='RS256', kid=kid, typ='JWT')
claims = dict(
iss=software_statement_id,
sub=software_statement_id,
scopes=client_scopes,
aud=token_url,
jti=str(uuid.uuid4()),
iat=jwt_iat,
exp=jwt_exp
)
token = jwt.JWT(header=header, claims=claims)
key_obj = jwk.JWK.from_pem(cache.get('private_key_pem').encode('latin-1'))
token.make_signed_token(key_obj)
signed_token = token.serialize()
return signed_token
def make_onboarding_token(kid: str, iss: str, aud: str, sub: str, scope: str, client_id: str, ssa: str) -> str:
jwt_iat = int(time.time())
jwt_exp = jwt_iat + 3600
header = dict(alg='RS256', kid=kid, typ='JWT')
claims = dict(
iss=iss,
iat=jwt_iat,
exp=jwt_exp,
aud=aud,
sub=sub,
scope=scope,
token_endpoint_auth_method='private_key_jwt',
grant_types=['authorization_code', 'refresh_token', 'client_credentials'],
response_types=['code', 'id_token'],
client_id=client_id,
software_statement=ssa
)
token = jwt.JWT(header=header, claims=claims)
key_obj = jwk.JWK.from_pem(cache.get('private_key_pem').encode('latin-1'))
token.make_signed_token(key_obj)
signed_token = token.serialize()
return signed_token
def get_context() -> dict:
context = dict()
# Home /
context['tpp_id'] = cache.get('tpp_id')
context['software_statement_id'] = cache.get('software_statement_id')
context['client_scopes'] = cache.get('client_scopes')
context['onboarding_scopes'] = cache.get('onboarding_scopes')
context['token_url'] = cache.get('token_url')
context['tpp_ssa_url'] = cache.get('tpp_ssa_url')
context['aspsp_list_url'] = cache.get('aspsp_list_url')
# Private key settings
context['key_size'] = cache.get('key_size')
# CSR settings
context['csr_common_name'] = cache.get('csr_common_name')
context['csr_organizational_unit_name'] = cache.get('csr_organizational_unit_name')
context['csr_country_name'] = cache.get('csr_country_name')
context['csr_state_or_province_name'] = cache.get('csr_state_or_province_name')
context['csr_locality_name'] = cache.get('csr_locality_name')
# Certs
context['private_key_pem'] = cache.get('private_key_pem')
context['kid'] = make_jwk_from_pem(context['private_key_pem']).get('kid')
context['csr_pem'] = cache.get('csr_pem')
# Access token
context['access_token'] = cache.get('access_token')
# SSA
context['software_statement_assertion'] = cache.get('software_statement_assertion')
# Authorization servers
context['authorization_servers'] = cache.get('authorization_servers')
# App onboarding
context['app_onboarding_status_exception'] = cache.get('app_onboarding_status_exception')
context['app_onboarding_status_url'] = cache.get('app_onboarding_status_url')
context['app_onboarding_status_code'] = cache.get('app_onboarding_status_code')
context['app_onboarding_reason'] = cache.get('app_onboarding_reason')
context['app_onboarding_text'] = cache.get('app_onboarding_text')
return context
################################################################################
# Route handlers
################################################################################
# / handler
@app.route('/', endpoint='root_handler', methods=['GET', 'POST'])
def root_handler() -> Response:
"""Home / handler
"""
if request.method == 'POST':
cache.set('tpp_id', request.form.get('tpp_id'), timeout=CACHE_TIMEOUT)
cache.set('software_statement_id', request.form.get('software_statement_id'), timeout=CACHE_TIMEOUT)
cache.set('client_scopes', request.form.get('client_scopes'), timeout=CACHE_TIMEOUT)
cache.set('onboarding_scopes', request.form.get('onboarding_scopes'), timeout=CACHE_TIMEOUT)
cache.set('token_url', request.form.get('token_url'), timeout=CACHE_TIMEOUT)
cache.set('tpp_ssa_url', request.form.get('tpp_ssa_url'), timeout=CACHE_TIMEOUT)
cache.set('aspsp_list_url', request.form.get('aspsp_list_url'), timeout=CACHE_TIMEOUT)
cache.set('private_key_pem', '', timeout=CACHE_TIMEOUT)
cache.set('kid', '', timeout=CACHE_TIMEOUT)
cache.set('csr_pem', '', timeout=CACHE_TIMEOUT)
context = dict(settings=get_context())
try:
return render_template('home.html', context=context)
except TemplateNotFound:
abort(404)
# create a csr handler
@app.route('/createcsr/', endpoint='createacsr_handler', methods=['GET', 'POST'])
def createacsr_handler() -> Response:
"""Private key & CSR creation handler.
"""
if request.method == 'POST':
cache.set('key_size', request.form.get('key_size'), timeout=CACHE_TIMEOUT)
cache.set('csr_country_name', request.form.get('csr_country_name'), timeout=CACHE_TIMEOUT)
cache.set('csr_state_or_province_name', request.form.get('csr_state_or_province_name'), timeout=CACHE_TIMEOUT)
cache.set('csr_locality_name', request.form.get('csr_locality_name'), timeout=CACHE_TIMEOUT)
cache.set('csr_organizational_unit_name', request.form.get('tpp_id'), timeout=CACHE_TIMEOUT)
cache.set('csr_common_name', request.form.get('software_statement_id'), timeout=CACHE_TIMEOUT)
private_key = make_private_key(int(request.form.get('key_size')))
private_key_pem = make_private_key_pem(private_key).decode(encoding='utf-8')
cache.set('private_key_pem', private_key_pem, timeout=CACHE_TIMEOUT)
csr = make_csr(private_key)
csr_pem = csr.public_bytes(serialization.Encoding.PEM).decode(encoding='utf-8')
cache.set('csr_pem', csr_pem, timeout=CACHE_TIMEOUT)
context = dict(settings=get_context())
try:
return render_template('createcsr.html', context=context)
except TemplateNotFound:
abort(404)
# obtain an access token from OB
@app.route('/getaccesstoken/', endpoint='createatoken_handler', methods=['GET', 'POST'])
def createatoken_handler() -> Response:
"""Access Token handler
"""
kid = cache.get('kid')
if request.method == 'POST':
kid = request.form.get('kid')
cache.set('kid', kid, timeout=CACHE_TIMEOUT)
if cache.get('kid') and cache.get('software_statement_id') and cache.get('client_scopes') and cache.get(
'token_url'):
signed_token = make_token(
cache.get('kid'),
cache.get('software_statement_id'),
cache.get('client_scopes'),
cache.get('token_url')
)
cache.set('signed_token', signed_token, timeout=CACHE_TIMEOUT)
data_dict = dict(
client_assertion_type='urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
grant_type='client_credentials',
client_id=cache.get('software_statement_id'),
client_assertion=cache.get('signed_token'),
scope=cache.get('client_scopes')
)
r = requests.post(cache.get('token_url'), data=data_dict)
if r.status_code == 200:
cache.set('access_token', r.json().get('access_token'), timeout=CACHE_TIMEOUT)
else:
cache.set('access_token', '', timeout=CACHE_TIMEOUT)
context = dict(settings=get_context())
context['settings']['kid'] = kid
try:
return render_template('createtoken.html', context=context)
except TemplateNotFound:
abort(404)
# get SSA
@app.route('/getssa/', endpoint='getssa_handler', methods=['GET', 'POST'])
def getssa_handler() -> Response:
"""Software Statement Assertion retrieval"""
if request.method == 'POST':
try:
r = requests.get(
'{}/tpp/{}/ssa/{}'.format(
cache.get('tpp_ssa_url'),
cache.get('tpp_id'),
cache.get('software_statement_id')
),
headers=dict(
Authorization='Bearer {}'.format(
cache.get('access_token')
)
)
)
except Exception as e:
app.logger.error('Could not retrieve the SSA because: {}'.format(e))
else:
if r.status_code == 200:
cache.set('software_statement_assertion', r.text, timeout=CACHE_TIMEOUT)
else:
app.logger.error('Could not retrieve the SSA, because: {}, {}'.format(r.status_code, r.reason))
context = dict(settings=get_context())
try:
return render_template('getssa.html', context=context)
except TemplateNotFound:
abort(404)
# get authorization servers
@app.route('/getauthservers/', endpoint='getauthservers_handler', methods=['GET', 'POST'])
def getauthservers_handler() -> Response:
"""Authorization server list retrieval handler
"""
if request.method == 'POST':
try:
r = requests.get(
cache.get('aspsp_list_url'),
headers=dict(
Authorization='Bearer {}'.format(
cache.get('access_token')
)
)
)
except Exception as e:
app.logger.error('Could not retrieve the list of authorization servers, because: {}'.format(e))
else:
if r.status_code == 200:
auth_servers_resources = r.json().get('Resources')
if auth_servers_resources:
auth_servers_list = [auth_server.get('AuthorisationServers') for auth_server in
auth_servers_resources if auth_server.get('AuthorisationServers')]
cache.set('authorization_servers', auth_servers_list, timeout=CACHE_TIMEOUT)
else:
app.logger.error(
'Could not retrieve the list of authorization servers, because: {}, {}'.format(
r.status_code,
r.reason
)
)
context = dict(settings=get_context())
try:
return render_template('getauthservers.html', context=context)
except TemplateNotFound:
abort(404)
# onboard app
@app.route('/onboard/', endpoint='onboardapp_handler', methods=['GET', 'POST'])
def onboardapp_handler() -> Response:
"""App Onboarding handler.
"""
if request.method == 'POST':
headers = dict()
headers['Content-Type'] = 'application/jwt'
headers['Accept'] = 'application/json'
try:
r = requests.post(
request.form.get('authorization_server'),
headers=headers,
data=make_onboarding_token(
kid=cache.get('kid'),
iss=cache.get('tpp_id'),
aud=request.form.get('authorization_server'),
sub=cache.get('software_statement_id'),
scope=cache.get('onboarding_scopes'),
client_id=cache.get('software_statement_id'),
ssa=cache.get('software_statement_assertion')
)
)
except Exception as e:
app.logger.error('Could not onboard the application, because: {}'.format(e))
cache.set('app_onboarding_status_exception', 'Could not onboard the application, because: {}'.format(e),
timeout=CACHE_TIMEOUT)
else:
cache.set('app_onboarding_status_url', r.url, timeout=CACHE_TIMEOUT)
cache.set('app_onboarding_status_code', r.status_code, timeout=CACHE_TIMEOUT)
cache.set('app_onboarding_reason', r.reason, timeout=CACHE_TIMEOUT)
cache.set('app_onboarding_text', r.text, timeout=CACHE_TIMEOUT)
context = dict(settings=get_context())
try:
return render_template('onboardapp.html', context=context)
except TemplateNotFound:
abort(404)
################################################################################
# End
################################################################################
# required host 0.0.0.0 for docker.
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=FLASK_DEBUG) | python |
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.core.management.base import BaseCommand
from credocommon.models import Detection
from credocommon.helpers import validate_image, rate_brightness
class Command(BaseCommand):
help = "Validate detections"
def handle(self, *args, **options):
detections = Detection.objects.all()
for d in detections:
if d.frame_content:
d.brightness = rate_brightness(d.frame_content)
d.save()
if (not d.frame_content) or validate_image(d.frame_content):
self.stdout.write(
"Hiding detection %s (image validation failed)" % d.id
)
d.visible = False
d.save()
if abs(d.time_received - d.timestamp) > 3600 * 24 * 365 * 5 * 1000:
self.stdout.write("Hiding detection %s (invalid date)" % d.id)
d.visible = False
d.save()
self.stdout.write("Done!")
| python |
"""Implement an error to indicate that a scaaml.io.Dataset already exists.
Creating scaaml.io.Dataset should not overwrite existing files. When it could
the constructor needs to raise an error, which should also contain the dataset
directory.
"""
from pathlib import Path
class DatasetExistsError(FileExistsError):
"""Error for signalling that the dataset already exists."""
def __init__(self, dataset_path: Path) -> None:
"""Represents that the dataset already exists.
Args:
dataset_path: The dataset path.
"""
super().__init__(
f'Dataset info file exists and would be overwritten. Use instead:'
f' Dataset.from_config(dataset_path="{dataset_path}")')
self.dataset_path = dataset_path
| python |
from datetime import datetime
from django.views.generic.edit import BaseCreateView
from braces.views import LoginRequiredMixin
from .base import BaseEditView
from forum.forms import ReplyForm
from forum.models import Topic, Reply
class ReplyCreateView(LoginRequiredMixin, BaseCreateView):
model = Topic
form_class = ReplyForm
http_method_names = ['post', 'put']
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.author = self.request.user
self.object.author_ip = self.request.META['REMOTE_ADDR']
self.object.topic = self.get_object()
self.object.topic.num_replies += 1
self.object.topic.last_reply_on = datetime.now()
self.object.topic.save()
return super(ReplyCreateView, self).form_valid(form)
def get_success_url(self):
return self.object.topic.get_absolute_url()
class ReplyEditView(LoginRequiredMixin, BaseEditView):
model = Reply
form_class = ReplyForm
template_name = 'forum/reply_edit_form.html'
def get_success_url(self):
return self.object.topic.get_absolute_url()
| python |
"""
See the problem description at: https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
"""
class Solution:
def minAddToMakeValid(self, S: str) -> int:
"""
Time complexity : O(n)
Space complexity: O(1)
"""
score1 = score2 = 0
for char in S:
if char == '(':
score1 += 1
else:
if score1 == 0:
score2 += 1
else:
score1 -= 1
return score1 + score2
| python |
from tests.seatsioClientTest import SeatsioClientTest
from tests.util.asserts import assert_that
class ListAllTagsTest(SeatsioClientTest):
def test(self):
chart1 = self.client.charts.create()
self.client.charts.add_tag(chart1.key, "tag1")
self.client.charts.add_tag(chart1.key, "tag2")
chart2 = self.client.charts.create()
self.client.charts.add_tag(chart2.key, "tag3")
tags = self.client.charts.list_all_tags()
assert_that(tags).contains_exactly_in_any_order("tag1", "tag2", "tag3")
| python |
"""empty message
Revision ID: 20210315_193805
Revises: 20210315_151433
Create Date: 2021-03-15 19:38:05.486503
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "20210315_193805"
down_revision = "20210315_151433"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"etl_job_results",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.DateTime(timezone=True), nullable=False),
sa.Column("deleted", sa.DateTime(timezone=True), nullable=False),
sa.Column("inserted", sa.DateTime(timezone=True), nullable=False),
sa.Column("errors", sa.JSON(), nullable=False),
sa.Column("error_summary", sa.Text(), nullable=False),
sa.Column("warning", sa.Text(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.alter_column(
"__crypto_ohlc_daily",
"t_cross",
existing_type=sa.INTEGER(),
comment="1=golden cross -1=dead cross 2021/3/15 t_sma_5 t_sma_25のクロスを検出",
existing_comment="1=golden cross -1=dead cross",
existing_nullable=False,
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column(
"__crypto_ohlc_daily",
"t_cross",
existing_type=sa.INTEGER(),
comment="1=golden cross -1=dead cross",
existing_comment="1=golden cross -1=dead cross 2021/3/15 t_sma_5 t_sma_25のクロスを検出",
existing_nullable=False,
)
op.drop_table("etl_job_results")
# ### end Alembic commands ###
| python |
def parse_full_text(status):
"""Param status (tweepy.models.Status)"""
return clean_text(status.full_text)
def clean_text(my_str):
"""Removes line-breaks for cleaner CSV storage. Handles string or null value.
Returns string or null value
Param my_str (str)
"""
try:
my_str = my_str.replace("\n", " ")
my_str = my_str.replace("\r", " ")
my_str = my_str.strip()
except AttributeError as err:
pass
return my_str
| python |
#!/usr/bin/env python
"""Command line utility to serve a Mapchete process."""
import click
import logging
import logging.config
import os
import pkgutil
from rasterio.io import MemoryFile
import mapchete
from mapchete.cli import options
from mapchete.tile import BufferedTilePyramid
logger = logging.getLogger(__name__)
@click.command(help="Serve a process on localhost.")
@options.arg_mapchete_files
@options.opt_port
@options.opt_internal_cache
@options.opt_zoom
@options.opt_bounds
@options.opt_overwrite
@options.opt_readonly
@options.opt_memory
@options.opt_input_file
@options.opt_debug
@options.opt_logfile
def serve(
mapchete_files,
port=None,
internal_cache=None,
zoom=None,
bounds=None,
overwrite=False,
readonly=False,
memory=False,
input_file=None,
debug=False,
logfile=None,
):
"""
Serve a Mapchete process.
Creates the Mapchete host and serves both web page with OpenLayers and the
WMTS simple REST endpoint.
"""
app = create_app(
mapchete_files=mapchete_files,
zoom=zoom,
bounds=bounds,
single_input_file=input_file,
mode=_get_mode(memory, readonly, overwrite),
debug=debug,
)
if os.environ.get("MAPCHETE_TEST") == "TRUE":
logger.debug("don't run flask app, MAPCHETE_TEST environment detected")
else: # pragma: no cover
app.run(
threaded=True,
debug=debug,
port=port,
host="0.0.0.0",
extra_files=mapchete_files,
)
def create_app(
mapchete_files=None,
zoom=None,
bounds=None,
single_input_file=None,
mode="continue",
debug=None,
):
"""Configure and create Flask app."""
from flask import Flask, render_template_string
app = Flask(__name__)
mapchete_processes = {
os.path.splitext(os.path.basename(mapchete_file))[0]: mapchete.open(
mapchete_file,
zoom=zoom,
bounds=bounds,
single_input_file=single_input_file,
mode=mode,
with_cache=True,
debug=debug,
)
for mapchete_file in mapchete_files
}
mp = next(iter(mapchete_processes.values()))
pyramid_type = mp.config.process_pyramid.grid
pyramid_srid = mp.config.process_pyramid.crs.to_epsg()
process_bounds = ",".join([str(i) for i in mp.config.bounds_at_zoom()])
grid = "g" if pyramid_srid == 3857 else "WGS84"
web_pyramid = BufferedTilePyramid(pyramid_type)
@app.route("/", methods=["GET"])
def index():
"""Render and hosts the appropriate OpenLayers instance."""
return render_template_string(
pkgutil.get_data("mapchete.static", "index.html").decode("utf-8"),
srid=pyramid_srid,
process_bounds=process_bounds,
is_mercator=(pyramid_srid == 3857),
process_names=mapchete_processes.keys(),
)
@app.route(
"/".join(
[
"",
"wmts_simple",
"1.0.0",
"<string:mp_name>",
"default",
grid,
"<int:zoom>",
"<int:row>",
"<int:col>.<string:file_ext>",
]
),
methods=["GET"],
)
def get(mp_name, zoom, row, col, file_ext):
"""Return processed, empty or error (in pink color) tile."""
logger.debug(
"received tile (%s, %s, %s) for process %s", zoom, row, col, mp_name
)
# convert zoom, row, col into tile object using web pyramid
return _tile_response(
mapchete_processes[mp_name], web_pyramid.tile(zoom, row, col), debug
)
return app
def _get_mode(memory, readonly, overwrite):
if memory:
return "memory"
elif readonly:
return "readonly"
elif overwrite:
return "overwrite"
else:
return "continue"
def _tile_response(mp, web_tile, debug):
try:
logger.debug("getting web tile %s", str(web_tile.id))
return _valid_tile_response(mp, mp.get_raw_output(web_tile))
except Exception: # pragma: no cover
logger.exception("getting web tile %s failed", str(web_tile.id))
if debug:
raise
else:
from flask import abort
abort(500)
def _valid_tile_response(mp, data):
from flask import send_file, make_response, jsonify
out_data, mime_type = mp.config.output.for_web(data)
logger.debug("create tile response %s", mime_type)
if isinstance(out_data, MemoryFile):
response = make_response(send_file(out_data, mime_type))
elif isinstance(out_data, list):
response = make_response(jsonify(data))
else:
response = make_response(out_data)
response.headers["Content-Type"] = mime_type
response.cache_control.no_write = True
return response
| python |
from .dualconv_mesh_net import DualConvMeshNet
from .singleconv_mesh_net import SingleConvMeshNet
| python |
from __future__ import print_function
import json
import urllib
import boto3
print('*Loading lambda: s3FileListRead')
s3 = boto3.client('s3')
def lambda_handler(event, context):
print('==== file list in bucket ====')
AWS_S3_BUCKET_NAME = 'yujitokiwa-jp-test'
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket(AWS_S3_BUCKET_NAME)
result = bucket.meta.client.list_objects(Bucket=bucket.name, Delimiter='/')
for o in result.get('Contents'):
print(o.get('Key')) # flie name will be printed
response = s3.get_object(Bucket=bucket.name, Key=o.get('Key'))
data = response['Body'].read()
print(data.decode('utf-8')) # file contents will be printed | python |
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 30 21:05:47 2020
@author: Richard
"""
from newsapi import NewsApiClient
newsapi = NewsApiClient(api_key='0566dfe86d9c44c6a3bf8ae60eafb8c6')
all_articles = newsapi.get_everything(q='apple',
from_param='2020-04-01',
to='2020-04-29',
language='en',
sort_by='relevancy',
page_size=100,
page=1)
authors = []
for art in all_articles["articles"]:
authors.append(art["source"]["id"])
authors = list(set(authors))
| python |
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas_datareader import data as web
from datetime import datetime, timedelta
from yahoo_finance import Share
from math import ceil, floor
from collections import deque
class Stock():
""" Historical data of a Stock
Attributes:
symbol - The official name of the stock
path - A path to the csv file containing information
data - Pandas DataFrame with all daily data
self.last_action
- A tuple of the latest action (buy or sell) and the date
Methods:
init_data - Gets a Pandas DataFrame with relevant information about the stock and saves it to a csv file with path from Stock.path.
init_data_csv
- Gets a Pandas DataFrame from a csv file with the path from Stock.path.
update_data - *TODO* Appends new data to existing data. Also saves to local csv.
splot - Plots a graph of closing price and closing averages specified in 'avg'.
get_avg - Finds the average closing price over 'avg_interval' number of days and adds a column to Stock.data.
print_data - Prints the Stock.data to the console.
create_avg - Creates the
do_rule_buy - Asserts if a buy-signal should be triggered.
rule_buy - Returns the latest index where Stock.do_rule_buy() returns True.
do_rule_sell- Asserts if a sell-signal should be triggered.
rule_sell - Returns the latest index where Stock.do_rule_sell() returns True.
"""
def __init__(self, symbol, path="C:\\Stockbot\\Stocks", num_days=1000):
"""
params:
symbol - (String) The unique character combination indicating a certain share.
path - (String) Default "C:\\Stockbot\\Stocks". The path directory where the Stocks related csv will be stored.
num_days - (Int) Default 1000. The number of days for data gathering including closing days.
returns:
None
Initializing method.
"""
self.symbol = symbol.upper()
self.path = "C:\\Stockbot\\Stocks\\{s}.csv".format(s=self.symbol)
# self.data = self.init_data(num_days)
self.data = self.init_data_csv()
self.last_action = (0,0) # Tuple of buy/sell and date
def init_data(self, num_days=1000):
"""
params:
num_days - (Int) Default 1000. Number of days to fetch data for, including closing days
returns:
(pandas.DataFrame) A DataFrame for the last num_days days' worth of stock data. Values [ High, Low, Close, Volume ] are kept.
Fetches data from Yahoo Finance using pandas_datareader the last num_days days. Writes the resulting csv to path as {symbol}.csv which is subsecuently is read and returned.
"""
end = datetime.today()
start = end - timedelta(days=num_days)
df = web.DataReader(self.symbol, "yahoo", start, end)
df.to_csv(path_or_buf=self.path,columns=["High","Low","Close","Volume"])
df = pd.read_csv(filepath_or_buffer=self.path)
return df
def init_data_csv(self):
"""
params:
None
returns:
(pandas.DataFrame) A DataFrame read from the csv stored in Stock.path.
Fetches data from a csv stored in Stock.path.
"""
return pd.read_csv(self.path)
def update_data(self):
"""
*TODO* Appends new data to existing data. Also saves to local csv.
"""
pass
def splot(self,avg=None):
"""
params:
avg - (List of Ints) Defualt None. If unchanged, plot only closing prices. Plot averages specified in avg.
returns:
None.
Plots a graph of closing price and closing averages specified in 'avg'.
"""
avgs = ["Close"]
for avg_interval in avg:
self.create_avg(avg_interval)
avgs.append("avg_{avg_interval}".format(avg_interval=avg_interval))
self.data.plot(x=self.data.index, y=avgs, grid=True, ylim=(max(self.data["Close"]*1.1),min(self.data["Close"])*0.9))
plt.gca().invert_yaxis()
plt.show()
def print_data(self):
"""
params:
None.
returns:
None.
Prints the Stock.data to the console.
"""
print("{s}\n{p}\n{d}".format(s=self.symbol,p=self.path,d=self.data))
def get_avg(self,avg_interval):
"""
params:
avg_interval - (Int) The interval of days that should be averaged.
returns:
(pandas.DataFrame) Stock.data including the newly created average column.
Finds the average closing price over 'avg_interval' number of days and adds a column to Stock.data.
"""
col = "avg_{avg_interval}".format(avg_interval=avg_interval)
prices = self.data["Close"]
dates = self.data["Date"]
self.data[col] = self.data["Close"].copy()
d = deque()
for idx, price in enumerate(prices):
if not np.isnan(price):
if len(d) < avg_interval:
d.append(price)
else:
d.popleft()
d.append(price)
if len(d) == avg_interval:
avg = sum(d)/avg_interval
self.data.loc[idx, col] = avg
else:
self.data.loc[idx, col] = np.nan
else:
self.data.loc[idx, col] = np.nan
return self.data
def create_avg(self, avg_interval):
"""
params:
avg_interval - (Int) The interval of days that should be averaged.
returns:
(pandas.DataFrame) Stock.data including the newly created average column, if any.
Finds the average closing price over 'avg_interval' number of days and adds a column to Stock.data if the column does not already exsists.
"""
if not (avg_interval in self.data.columns):
df = self.get_avg(avg_interval)
return df
def do_rule_buy(self, idx, col_x, col_y):
"""
params:
idx - (Int) The index of Stock.data that should be examined.
col_x - (String) Name of the first column for comparison.
col_y - (String) Name of the second column for comparison.
returns:
(Boolean) The evaluation of whether or not it would be recommended to buy this Stock based on the following rule: (closing_price > val_x and val_x < val_y).
Asserts if a buy-signal should be triggered.
"""
price = self.data.loc[idx, "Close"]
avg_x = self.data.loc[idx, col_x]
avg_y = self.data.loc[idx, col_y]
if price > avg_x and avg_x < avg_y:
return True
else:
return False
def rule_buy(self, x, y):
"""
params:
x - (Int) The first average to be compared.
y - (Int) The second average to be compared.
returns:
(Int) The latest index where a buy signal was triggered.
Returns the latest index where Stock.do_rule_buy() returns True.
"""
col_x = "avg_{x}".format(x=x)
self.create_avg(x)
col_y = "avg_{y}".format(y=y)
self.create_avg(y)
for idx in reversed(self.data.index):
if self.do_rule_buy(idx, col_x, col_y):
return idx
def do_rule_sell(self, idx, col_x, col_y):
"""
params:
idx - (Int) The index of Stock.data that should be examined.
col_x - (String) Name of the first column for comparison.
col_y - (String) Name of the second column for comparison.
returns:
(Boolean) The evaluation of whether or not it would be recommended to sell this Stock based on the following rule: (closing_price < val_x and val_x > val_y).
Asserts if a sell-signal should be triggered.
"""
price = self.data.loc[idx, "Close"]
avg_x = self.data.loc[idx, col_x]
avg_y = self.data.loc[idx, col_y]
if price < avg_x and avg_x > avg_y:
return True
else:
return False
def rule_sell(self, x, y):
"""
params:
x - (Int) The first average to be compared.
y - (Int) The second average to be compared.
returns:
(Int) The latest index where a sell signal was triggered.
Returns the latest index where Stock.do_rule_sell() returns True.
"""
col_x = "avg_{x}".format(x=x)
self.create_avg(x)
col_y = "avg_{y}".format(y=y)
self.create_avg(y)
for idx in reversed(self.data.index):
if self.do_rule_sell(idx, col_x, col_y):
return idx
def simulate_market(stock, start_money, avg=(2,10)):
""" avg - the lowest and highest averages to be examined
"""
# Create all averages from start through end intervals
start, end = avg
for x in range(start, end + 1):
col_x = "avg_{x}".format(x=x)
stock.create_avg(x)
# Variables to contain logging results
max_money = 0
max_avg = (0,0)
max_num_purchases = 0
# Loop across averages and find the optimal intervals, only use y where y > x + 1
for x in range(start, end):
col_x = "avg_{x}".format(x=x)
gen = (y for y in range(start + 1, end + 1) if y > x + 1)
for y in gen:
# Initializing variables
money, num_bought, num_purchases, mode = start_money, 0, 0, "buy"
idx, idx_max = y, stock.data.last_valid_index()
col_y = "avg_{y}".format(y=y)
for idx in range(0, idx_max + 1):
# Want to buy
if mode == "buy" and stock.do_rule_buy(idx, col_x, col_y):
mode = "sell"
price = stock.data.loc[idx, "Close"]
num_bought, money = money / price, 0
num_purchases += 1
# Want to sell
if mode == "sell" and stock.do_rule_sell(idx, col_x, col_y):
mode = "buy"
price = stock.data.loc[idx, "Close"]
money, num_bought = num_bought * price, 0
num_purchases += 1
# Finally sell all to see profit
money = num_bought * price
# # Printing result of x-, y-avg
# print("Avg: {x} {y} {t}\nGross: {profit} ({diff})\n\n\n".format(x=x, y=y, t=num_purchases, profit=round(money/start_money,3), diff=round(money-start_money,3)))
# Logging max values
if money >= max_money and num_purchases > 1:
max_money = money
max_avg = (x, y)
max_num_purchases = num_purchases
# Print logs
maxx, maxy = max_avg
print("MAX:: {p}% ({x}, {y}). Num {n}".format(p=round(max_money/start_money*100,3), x=maxx, y=maxy, n=max_num_purchases))
if __name__ == "__main__":
test_stock = Stock("AMZN")
# test_stock.get_avg(2)
# test_stock.print_data()
# test_stock.rule_buy(3, 4)
# test_stock.rule_sell(5, 6)
# simulate_market(test_stock, 10000, (7,10))
# test_stock.splot([11, 12])
"""
TODO:
Retry fetching data from web
Write the Stock.update_data() method
Create a proper test method
Check Stock.init_csv() in case no csv in Stock.path
Create notification system that provides insigh whether or not it recommends to buy/sell
"""
| python |
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
size = 1000
x = np.random.randn(size)
y = 1.051 * x + np.random.random(size)
plt.plot(x,y,'*',color='black',label="Dado original")
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Regressão Linear')
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
print("Coeficiente angular (slope)= %f" %slope)
print("Coeficiente linear (intercept)= %f" %intercept)
print("R quadrado (r-squared)= %f" %r_value**2)
print("Valor p (p-value)= %f" %p_value)
print("Erro (Std)= %f" %std_err)
ajuste = intercept + slope*x
plt.plot(x,ajuste,color='red',label="Dado ajustado")
plt.legend()
plt.show() | python |
"""
Contains functions to assist with stuff across the application.
ABSOLUTELY NO IMPORTS FROM OTHER PLACES IN THE REPOSITORY.
Created: 23 June 2020
"""
| python |
#!/usr/bin/env python
# The MIT License (MIT)
#
# Copyright (C) 2015 by Brian Horn, [email protected].
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
Provides a data structure used to model a linked list iterator.
"""
__author__ = "Brian Horn"
__copyright__ = "Copyright (c) 2015 Brian Horn"
__credits__ = "Brian Horn"
__license__ = "MIT"
__version__ = "1.0.2"
__maintainer__ = "Brian Horn"
__email__ = "[email protected]"
__status__ = "Prototype"
from py_alg_dat.iterator import Iterator
class LinkedListIterator(Iterator):
"""
The interface of a linked list iterator.
"""
def __init__(self, head):
"""
Constructs an iterator enumerating the linked list.
@param head: The first element in the linked list.
@type: C{object}
"""
super(LinkedListIterator, self).__init__(head)
self.current = head
def next(self):
"""
Returns the next element in the linked list.
@return: The next element in the linked list.
@rtype: C{object}
"""
if self.current is None:
raise StopIteration
retval = self.current
self.current = self.current.next
return retval
| python |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.