content
stringlengths
27
928k
path
stringlengths
4
230
size
int64
27
928k
nl_text
stringlengths
21
396k
nl_size
int64
21
396k
nl_language
stringlengths
2
3
nl_language_score
float64
0.04
1
from django.db import models from django.utils import timezone from django.core.exceptions import ValidationError # from django.contrib.auth.models import User from users.models import Student, College from django.urls import reverse from django.core import validators class AbstractPostModel(models.Model): title = models.CharField(validators=[validators.MinLengthValidator(10)], null=False, max_length=500) content = models.TextField(validators=[validators.MinLengthValidator(10)], null=False) post_date = models.DateTimeField(default=timezone.now) author = models.ForeignKey(Student, on_delete=models.CASCADE) rating = models.IntegerField(default=0) college = models.ForeignKey(College, on_delete=models.CASCADE) class Meta: abstract = True def __str__(self): return self.title def get_absolute_url(self): return reverse('post-detail', kwargs={'pk': self.pk, 'title': self.title}) class Question(AbstractPostModel): is_answered = models.BooleanField(default=False) class Answer(AbstractPostModel): is_approved = models.BooleanField(default=False) question = models.ForeignKey(Question, on_delete=models.CASCADE, null=True) class Voter(models.Model): Question = models.ForeignKey(Question, on_delete=models.CASCADE) Answer = models.ForeignKey(Answer, on_delete=models.CASCADE, null=True) user = models.ForeignKey(Student, on_delete=models.CASCADE) def __str__(self): return self.user.username + ' vote on post: ' + self.Question.title class Comment(AbstractPostModel): Question = models.ForeignKey(Question, on_delete=models.CASCADE) author = models.ForeignKey(Student, on_delete=models.CASCADE) content = models.TextField(null=False) def __str__(self): return self.author.username + ' comment on post: ' + self.Question.title def get_absolute_url(self): return reverse('post-detail', kwargs={'pk': self.pk, 'title': self.Question.title})
app/blog/models.py
2,018
from django.contrib.auth.models import User
43
en
0.676588
""" Wrapper to get ROVA calendar from Rova's API Acces to this ROVA API has been simplified since version 0.2.1 of this wrapper Just use https://www.rova.nl/api/waste-calendar/upcoming?postalcode=1000AA&houseNumber=1&addition=&take=5 with a existing combination of postalcode, housenumber, housenumber addition Be aware that this API has not been officially published by ROVA. """ from datetime import datetime import random import requests __title__ = "rova" __version__ = "0.3.0" __author__ = "Gido Hakvoort and synoniem <synoniem@hotmail.com>" __license__ = "MIT" class Rova: """ ROVA class """ def __init__(self, zip_code, house_number, house_addition=""): """ To fetch the garbage calendar, you need to set a zip_code and house_number. """ self.zip_code = zip_code.replace(' ', '') self.house_number = house_number.strip() self.house_addition = house_addition.strip() def is_rova_area(self): """ Check if ROVA collects garbage at this address """ url = 'https://www.rova.nl/api/waste-calendar/upcoming' # request data from rova API and check if garbage is collected at this address # requesting with a non-existing postalcode will result in a error message response = requests.get(url, params={ 'postalcode': self.zip_code, 'houseNumber': self.house_number, 'addition': self.house_addition, 'take': '1', }) response.raise_for_status() rova_response = response.text.strip() if rova_response != '[]': rova_response = "OK" return rova_response == "OK" def get_calendar_items(self, take=5): """ Get next pickup date for each garbage types """ url = 'https://www.rova.nl/api/waste-calendar/upcoming' # request data from rova API and save response first 5 items (default) response = requests.get(url, params={ 'postalcode': self.zip_code, 'houseNumber': self.house_number, 'addition': self.house_addition, 'take': take, }) response.raise_for_status() rova_response = response.json() items = [] types = [] # add next pickup date for each garbage type for item in rova_response: date = datetime.strptime(item["date"], "%Y-%m-%dT%H:%M:%SZ") date = date.strftime("%Y-%m-%dT%H:%M:%S") garbage_type = item["garbageTypeCode"].upper() items.append({ 'GarbageTypeCode': garbage_type, 'Date': date }) types.append(garbage_type) return items
rova/rova.py
2,751
ROVA class To fetch the garbage calendar, you need to set a zip_code and house_number. Get next pickup date for each garbage types Check if ROVA collects garbage at this address Wrapper to get ROVA calendar from Rova's API Acces to this ROVA API has been simplified since version 0.2.1 of this wrapper Just use https://www.rova.nl/api/waste-calendar/upcoming?postalcode=1000AA&houseNumber=1&addition=&take=5 with a existing combination of postalcode, housenumber, housenumber addition Be aware that this API has not been officially published by ROVA. request data from rova API and check if garbage is collected at this address requesting with a non-existing postalcode will result in a error message request data from rova API and save response first 5 items (default) add next pickup date for each garbage type
815
en
0.814054
import numpy as np import random from collections import namedtuple, deque from model import QNetwork import torch import torch.nn.functional as F import torch.optim as optim BUFFER_SIZE = int(1e5) # replay buffer size BATCH_SIZE = 64 # minibatch size GAMMA = 0.99 # discount factor TAU = 1e-3 # for soft update of target parameters LR = 5e-4 # learning rate UPDATE_EVERY = 4 # how often to update the network device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") class Agent(): """Interacts with and learns from the environment.""" def __init__(self, state_size, action_size, seed): """Initialize an Agent object. Params ====== state_size (int): dimension of each state action_size (int): dimension of each action seed (int): random seed """ self.state_size = state_size self.action_size = action_size self.seed = random.seed(seed) # Q-Network self.qnetwork_local = QNetwork(state_size, action_size, seed).to(device) self.qnetwork_target = QNetwork(state_size, action_size, seed).to(device) self.optimizer = optim.Adam(self.qnetwork_local.parameters(), lr=LR) # Replay memory self.memory = ReplayBuffer(action_size, BUFFER_SIZE, BATCH_SIZE, seed) # Initialize time step (for updating every UPDATE_EVERY steps) self.t_step = 0 def step(self, state, action, reward, next_state, done): # Save experience in replay memory self.memory.add(state, action, reward, next_state, done) # Learn every UPDATE_EVERY time steps. self.t_step = (self.t_step + 1) % UPDATE_EVERY if self.t_step == 0: # If enough samples are available in memory, get random subset and learn if len(self.memory) > BATCH_SIZE: experiences = self.memory.sample() self.learn(experiences, GAMMA) def act(self, state, eps=0.): """Returns actions for given state as per current policy. Params ====== state (array_like): current state eps (float): epsilon, for epsilon-greedy action selection """ # from_numpy creates tensor without copying numpy array data # float == to(float), to() can be used for dtype and device conversions state = torch.from_numpy(state).float().unsqueeze(0).to(device) # eval mode as opposed to training (ignores dropout, batchnorm) self.qnetwork_local.eval() with torch.no_grad(): # call the nn.Module rather than explicitly using nn.Module.forward() action_values = self.qnetwork_local(state) self.qnetwork_local.train() # Epsilon-greedy action selection if random.random() > eps: return np.argmax(action_values.cpu().data.numpy()) else: return random.choice(np.arange(self.action_size)) def learn(self, experiences, gamma): """Update value parameters using given batch of experience tuples. Params ====== experiences (Tuple[torch.Tensor]): tuple of (s, a, r, s', done) tuples gamma (float): discount factor """ states, actions, rewards, next_states, dones = experiences ## TODO: compute and minimize the loss "*** YOUR CODE HERE ***" # Max q value over all next actions given their next states (this is for a whole batch) # i.e. max_a(Q(s_{j+1}, a, w-)) from the one step look ahead Q_targets_next = self.qnetwork_local(next_states).detach().max(1)[0].unsqueeze(1) # Compute Q targets for current states Q_targets = rewards + gamma * Q_targets_next * (1 - dones) # set y_i = r if done # Get expected Q values from local model - used in gradient update as diff from target Q_expected = self.qnetwork_local(states).gather(1, actions) # Compute Loss loss = F.mse_loss(Q_expected, Q_targets) # Minimise loss by backprop self.optimizer.zero_grad() loss.backward() self.optimizer.step() # ------------------- update target network ------------------- # self.soft_update(self.qnetwork_local, self.qnetwork_target, TAU) def soft_update(self, local_model, target_model, tau): """Soft update model parameters. θ_target = τ*θ_local + (1 - τ)*θ_target Params ====== local_model (PyTorch model): weights will be copied from target_model (PyTorch model): weights will be copied to tau (float): interpolation parameter """ for target_param, local_param in zip(target_model.parameters(), local_model.parameters()): target_param.data.copy_(tau*local_param.data + (1.0-tau)*target_param.data) class ReplayBuffer: """Fixed-size buffer to store experience tuples.""" def __init__(self, action_size, buffer_size, batch_size, seed): """Initialize a ReplayBuffer object. Params ====== action_size (int): dimension of each action buffer_size (int): maximum size of buffer batch_size (int): size of each training batch seed (int): random seed """ self.action_size = action_size self.memory = deque(maxlen=buffer_size) self.batch_size = batch_size self.experience = namedtuple("Experience", field_names=["state", "action", "reward", "next_state", "done"]) self.seed = random.seed(seed) def add(self, state, action, reward, next_state, done): """Add a new experience to memory.""" e = self.experience(state, action, reward, next_state, done) self.memory.append(e) def sample(self): """Randomly sample a batch of experiences from memory.""" experiences = random.sample(self.memory, k=self.batch_size) states = torch.from_numpy(np.vstack([e.state for e in experiences if e is not None])).float().to(device) actions = torch.from_numpy(np.vstack([e.action for e in experiences if e is not None])).long().to(device) rewards = torch.from_numpy(np.vstack([e.reward for e in experiences if e is not None])).float().to(device) next_states = torch.from_numpy(np.vstack([e.next_state for e in experiences if e is not None])).float().to(device) dones = torch.from_numpy(np.vstack([e.done for e in experiences if e is not None]).astype(np.uint8)).float().to(device) return (states, actions, rewards, next_states, dones) def __len__(self): """Return the current size of internal memory.""" return len(self.memory)
dqn/exercise/dqn_agent.py
6,836
Interacts with and learns from the environment. Fixed-size buffer to store experience tuples. Initialize an Agent object. Params ====== state_size (int): dimension of each state action_size (int): dimension of each action seed (int): random seed Initialize a ReplayBuffer object. Params ====== action_size (int): dimension of each action buffer_size (int): maximum size of buffer batch_size (int): size of each training batch seed (int): random seed Return the current size of internal memory. Returns actions for given state as per current policy. Params ====== state (array_like): current state eps (float): epsilon, for epsilon-greedy action selection Add a new experience to memory. Update value parameters using given batch of experience tuples. Params ====== experiences (Tuple[torch.Tensor]): tuple of (s, a, r, s', done) tuples gamma (float): discount factor Randomly sample a batch of experiences from memory. Soft update model parameters. θ_target = τ*θ_local + (1 - τ)*θ_target Params ====== local_model (PyTorch model): weights will be copied from target_model (PyTorch model): weights will be copied to tau (float): interpolation parameter replay buffer size minibatch size discount factor for soft update of target parameters learning rate how often to update the network Q-Network Replay memory Initialize time step (for updating every UPDATE_EVERY steps) Save experience in replay memory Learn every UPDATE_EVERY time steps. If enough samples are available in memory, get random subset and learn from_numpy creates tensor without copying numpy array data float == to(float), to() can be used for dtype and device conversions eval mode as opposed to training (ignores dropout, batchnorm) call the nn.Module rather than explicitly using nn.Module.forward() Epsilon-greedy action selection TODO: compute and minimize the loss Max q value over all next actions given their next states (this is for a whole batch) i.e. max_a(Q(s_{j+1}, a, w-)) from the one step look ahead Compute Q targets for current states set y_i = r if done Get expected Q values from local model - used in gradient update as diff from target Compute Loss Minimise loss by backprop ------------------- update target network -------------------
2,292
en
0.711204
from __future__ import print_function, division, absolute_import import os import unittest from six import string_types from .. import * from ..compat import as_text, as_str, as_bytes DEFAULT_VP_TEST_HOST = '127.0.0.1' DEFAULT_VP_TEST_PORT = 5433 DEFAULT_VP_TEST_USER = 'dbadmin' DEFAULT_VP_TEST_PASSWD = '' DEFAULT_VP_TEST_DB = 'docker' DEFAULT_VP_TEST_TABLE = 'vertica_python_unit_test' class VerticaPythonTestCase(unittest.TestCase): """Base class for tests that query Vertica.""" @classmethod def setUpClass(cls): cls._host = os.getenv('VP_TEST_HOST', DEFAULT_VP_TEST_HOST) cls._port = int(os.getenv('VP_TEST_PORT', DEFAULT_VP_TEST_PORT)) cls._user = os.getenv('VP_TEST_USER', DEFAULT_VP_TEST_USER) cls._password = os.getenv('VP_TEST_PASSWD', DEFAULT_VP_TEST_PASSWD) cls._database = os.getenv('VP_TEST_DB', DEFAULT_VP_TEST_DB) cls._table = os.getenv('VP_TEST_TABLE', DEFAULT_VP_TEST_TABLE) cls._conn_info = { 'host': cls._host, 'port': cls._port, 'database': cls._database, 'user': cls._user, 'password': cls._password, } @classmethod def tearDownClass(cls): with cls._connect() as conn: cur = conn.cursor() cur.execute("DROP TABLE IF EXISTS {0}".format(cls._table)) @classmethod def _connect(cls): """Connects to vertica. :return: a connection to vertica. """ return connect(**cls._conn_info) def _query_and_fetchall(self, query): """Creates a new connection, executes a query and fetches all the results. :param query: query to execute :return: all fetched results as returned by cursor.fetchall() """ with self._connect() as conn: cur = conn.cursor() cur.execute(query) results = cur.fetchall() return results def _query_and_fetchone(self, query): """Creates a new connection, executes a query and fetches one result. :param query: query to execute :return: the first result fetched by cursor.fetchone() """ with self._connect() as conn: cur = conn.cursor() cur.execute(query) result = cur.fetchone() return result def assertTextEqual(self, first, second, msg=None): first_text = as_text(first) second_text = as_text(second) self.assertEqual(first=first_text, second=second_text, msg=msg) def assertStrEqual(self, first, second, msg=None): first_str = as_str(first) second_str = as_str(second) self.assertEqual(first=first_str, second=second_str, msg=msg) def assertBytesEqual(self, first, second, msg=None): first_bytes = as_bytes(first) second_bytes = as_bytes(second) self.assertEqual(first=first_bytes, second=second_bytes, msg=msg) def assertResultEqual(self, value, result, msg=None): if isinstance(value, string_types): self.assertTextEqual(first=value, second=result, msg=msg) else: self.assertEqual(first=value, second=result, msg=msg) def assertListOfListsEqual(self, list1, list2, msg=None): self.assertEqual(len(list1), len(list2), msg=msg) for l1, l2 in zip(list1, list2): self.assertListEqual(l1, l2, msg=msg)
vertica_python/tests/base.py
3,429
Base class for tests that query Vertica. Connects to vertica. :return: a connection to vertica. Creates a new connection, executes a query and fetches all the results. :param query: query to execute :return: all fetched results as returned by cursor.fetchall() Creates a new connection, executes a query and fetches one result. :param query: query to execute :return: the first result fetched by cursor.fetchone()
416
en
0.845625
from selenium_test.selenium_utils import * from file_and_system.windows_os_utils import WindowsOsUtil from python_common.global_param import GlobalParam from http_request.request_utils import request_download_file_by_url import cv2 as cv import time WindowsOsUtil.kill_process_by_name('MicrosoftWebDriver.exe') # mail_lists=['mail.hoperun.com', 'mail.qq.com', 'mail.163.com] mail_lists = ['mail.163.com'] mail_driver = init_driver('edge', GlobalParam.get_edge_driver_path()) open_browser_multi_tab(mail_driver, mail_lists) wait_for_page_full_loaded(mail_driver) def hoperun_login(hoperun_driver, user_name, user_pass): hoperun_driver.execute_script("document.getElementById('usernameTip').removeAttribute('readonly');") element = find_element_by_id(hoperun_driver, 'usernameTip') element.click() element = find_element_by_id(hoperun_driver, 'username') element.send_keys(user_name) element = find_element_by_id(hoperun_driver, 'userType') element.click() element = find_element_by_id(hoperun_driver, 'userTypePwd') element.send_keys(user_pass) element = find_element_by_id(hoperun_driver, 'wmSubBtn') element.click() def hoperun_check_mail(hoperun_driver, mail_sender, mail_title): wait_for_frame_and_switch_to_frame(hoperun_driver, 'treeBox') element = find_element_by_id(hoperun_driver, 'tree_folder_1_span') element.click() wait_for_page_full_loaded(hoperun_driver) wait_for_frame_and_switch_to_frame(hoperun_driver, 'tabsHome') wait_for_page_full_loaded(hoperun_driver) element = hoperun_driver.find_elements_by_xpath(''.join(('//div[text()="', mail_sender, '"]/../../../..'))) for e in element: if e.find_element_by_xpath('li[2]/div[3]/span').text.__contains__(mail_title): e.find_element_by_xpath('li[2]/div[3]/span').click() def qq_login(qq_driver, user_name, user_pass): element = find_element_by_id(qq_driver, 'qqLoginTab') element.click() qq_driver.switch_to.frame('login_frame') element = find_element_by_id(qq_driver, 'u') element.click() element.send_keys(user_name) element = find_element_by_id(qq_driver, 'p') element.click() element.send_keys(user_pass) element = find_element_by_id(qq_driver, 'login_button') element.click() wait_for_frame_and_switch_to_frame(qq_driver, 'tcaptcha_iframe') img_element = find_element_by_id(qq_driver, 'slideBg') wait_for_element_appeared(qq_driver, img_element) big = img_element.get_attribute('src') request_download_file_by_url(big, GlobalParam.get_test_image_path() + 'test_qq_mail_big.png') img_element = find_element_by_id(qq_driver, 'slideBlock') wait_for_element_appeared(qq_driver, img_element) small = img_element.get_attribute('src') request_download_file_by_url(small, GlobalParam.get_test_image_path() + 'test_qq_mail_small.png') def netcase_163_login(netcase_163_driver, user_name, user_pass): netcase_login_frame = netcase_163_driver.find_element_by_tag_name('iframe') wait_for_frame_and_switch_to_frame(netcase_163_driver, netcase_login_frame) wait_for_element_exist(netcase_163_driver, '//input[@name="email"]') element = find_element_by_name(netcase_163_driver, 'email') element.click() element.send_keys(user_name) wait_for_element_exist(netcase_163_driver, '//input[@name="password"]') element = find_element_by_name(netcase_163_driver, 'password') element.click() element.send_keys(user_pass) element = find_element_by_id(netcase_163_driver, 'dologin') element.click() # ------------------------security mail captcha not show---------------------- # wait_for_element_exist(netcase_163_driver,'//div[@class="yidun_panel"]') # element = find_element_by_class_name(netcase_163_driver, 'yidun_panel') # netcase_163_driver.execute_script("arguments[0].style['display'] = 'block';",element) # # element = find_element_by_class_name(netcase_163_driver, 'yidun_bg-img') # # netcase_mail_captcha = element.get_attribute('src') # # request_download_file_by_url(netcase_mail_captcha, test_image_path+'test_netcase_mail_captcha.png') # time.sleep(4) # element = find_element_by_class_name(netcase_163_driver, 'yidun_refresh') # element.click() # # element = find_element_by_class_name(netcase_163_driver, 'yidun_tips__point') # print(element.location) # # # element = find_element_by_class_name(netcase_163_driver, 'yidun_tips__point') # # print(element.get_attribute("innerHTML")) # ------------------------security mail captcha not show---------------------- def netcase_163_check_mail(netcase_163_driver, mail_sender, mail_title): wait_for_element_to_be_clickable(netcase_163_driver, '//div[@id="_mail_component_140_140"]/span[@title="收件箱"]') time.sleep(2) # rF0 kw0 nui-txt-flag0 : not read # rF0 nui-txt-flag0 : readed # element = netcase_163_driver.find_elements_by_xpath('//div[@class="rF0 nui-txt-flag0"]/div/div[2]/span') element = netcase_163_driver.find_elements_by_xpath('//div[@class="rF0 nui-txt-flag0"]') for e in element: print(e.find_element_by_xpath('.//div/div[2]/span').text) # if e.text.__contains__(mail_title): # print(e.text) def qq_captcha_pass(): big_image = cv.imread(GlobalParam.get_test_image_path() + 'test_qq_mail_big.png') small_image = cv.imread(GlobalParam.get_test_image_path() + 'test_qq_mail_small.png') cv.imshow('1', small_image) cv.waitKey(0) def netcase_captcha_pass(): return '' # login hoperun mail and check mail # hoperun_login(mail_driver, 'user', 'password') # wait_for_page_full_loaded(mail_driver) # hoperun_check_mail(mail_driver, 'sender', 'title') netcase_163_login(mail_driver, '****', '****') wait_for_page_full_loaded(mail_driver) netcase_163_check_mail(mail_driver, '', '123') # qq_login(mail_driver, '', '') # netcase_163_login(mail_driver, '', '') # captcha_pass()
selenium_test/sele_test_mail_login.py
5,974
mail_lists=['mail.hoperun.com', 'mail.qq.com', 'mail.163.com] ------------------------security mail captcha not show---------------------- wait_for_element_exist(netcase_163_driver,'//div[@class="yidun_panel"]') element = find_element_by_class_name(netcase_163_driver, 'yidun_panel') netcase_163_driver.execute_script("arguments[0].style['display'] = 'block';",element) element = find_element_by_class_name(netcase_163_driver, 'yidun_bg-img') netcase_mail_captcha = element.get_attribute('src') request_download_file_by_url(netcase_mail_captcha, test_image_path+'test_netcase_mail_captcha.png') time.sleep(4) element = find_element_by_class_name(netcase_163_driver, 'yidun_refresh') element.click() element = find_element_by_class_name(netcase_163_driver, 'yidun_tips__point') print(element.location) element = find_element_by_class_name(netcase_163_driver, 'yidun_tips__point') print(element.get_attribute("innerHTML")) ------------------------security mail captcha not show---------------------- rF0 kw0 nui-txt-flag0 : not read rF0 nui-txt-flag0 : readed element = netcase_163_driver.find_elements_by_xpath('//div[@class="rF0 nui-txt-flag0"]/div/div[2]/span') if e.text.__contains__(mail_title): print(e.text) login hoperun mail and check mail hoperun_login(mail_driver, 'user', 'password') wait_for_page_full_loaded(mail_driver) hoperun_check_mail(mail_driver, 'sender', 'title') qq_login(mail_driver, '', '') netcase_163_login(mail_driver, '', '') captcha_pass()
1,476
en
0.370209
# -*- coding: utf-8 -*- # @Time : 2020/10/3 # @Author : Changxin Tian # @Email : cx.tian@outlook.com r""" KGNNLS ################################################ Reference: Hongwei Wang et al. "Knowledge-aware Graph Neural Networks with Label Smoothness Regularization for Recommender Systems." in KDD 2019. Reference code: https://github.com/hwwang55/KGNN-LS """ import torch import torch.nn as nn import numpy as np import random from recbole.utils import InputType from recbole.model.abstract_recommender import KnowledgeRecommender from recbole.model.loss import BPRLoss, EmbLoss from recbole.model.init import xavier_normal_initialization class KGNNLS(KnowledgeRecommender): r"""KGNN-LS is a knowledge-based recommendation model. KGNN-LS transforms the knowledge graph into a user-specific weighted graph and then apply a graph neural network to compute personalized item embeddings. To provide better inductive bias, KGNN-LS relies on label smoothness assumption, which posits that adjacent items in the knowledge graph are likely to have similar user relevance labels/scores. Label smoothness provides regularization over the edge weights and it is equivalent to a label propagation scheme on a graph. """ input_type = InputType.PAIRWISE def __init__(self, config, dataset): super(KGNNLS, self).__init__(config, dataset) # load parameters info self.embedding_size = config['embedding_size'] self.neighbor_sample_size = config['neighbor_sample_size'] self.aggregator_class = config['aggregator'] # which aggregator to use # number of iterations when computing entity representation self.n_iter = config['n_iter'] self.reg_weight = config['reg_weight'] # weight of l2 regularization # weight of label Smoothness regularization self.ls_weight = config['ls_weight'] # define embedding self.user_embedding = nn.Embedding(self.n_users, self.embedding_size) self.entity_embedding = nn.Embedding( self.n_entities, self.embedding_size) self.relation_embedding = nn.Embedding( self.n_relations + 1, self.embedding_size) # sample neighbors and construct interaction table kg_graph = dataset.kg_graph(form='coo', value_field='relation_id') adj_entity, adj_relation = self.construct_adj(kg_graph) self.adj_entity, self.adj_relation = adj_entity.to( self.device), adj_relation.to(self.device) inter_feat = dataset.dataset.inter_feat.values pos_users = torch.from_numpy(inter_feat[:, 0]) pos_items = torch.from_numpy(inter_feat[:, 1]) pos_label = torch.ones(pos_items.shape) pos_interaction_table, self.offset = self.get_interaction_table( pos_users, pos_items, pos_label) self.interaction_table = self.sample_neg_interaction( pos_interaction_table, self.offset) # define function self.softmax = nn.Softmax(dim=-1) self.linear_layers = torch.nn.ModuleList() for i in range(self.n_iter): self.linear_layers.append(nn.Linear( self.embedding_size if not self.aggregator_class == 'concat' else self.embedding_size * 2, self.embedding_size)) self.ReLU = nn.ReLU() self.Tanh = nn.Tanh() self.bce_loss = nn.BCEWithLogitsLoss() self.l2_loss = EmbLoss() # parameters initialization self.apply(xavier_normal_initialization) def get_interaction_table(self, user_id, item_id, y): r"""Get interaction_table that is used for fetching user-item interaction label in LS regularization. Args: user_id(torch.Tensor): the user id in user-item interactions, shape: [n_interactions, 1] item_id(torch.Tensor): the item id in user-item interactions, shape: [n_interactions, 1] y(torch.Tensor): the label in user-item interactions, shape: [n_interactions, 1] Returns: tuple: - interaction_table(dict): key: user_id * 10^offset + item_id; value: y_{user_id, item_id} - offset(int): The offset that is used for calculating the key(index) in interaction_table """ offset = len(str(self.n_entities)) offset = 10 ** offset keys = user_id * offset + item_id keys = keys.int().cpu().numpy().tolist() values = y.float().cpu().numpy().tolist() interaction_table = dict(zip(keys, values)) return interaction_table, offset def sample_neg_interaction(self, pos_interaction_table, offset): r"""Sample neg_interaction to construct train data. Args: pos_interaction_table(dict): the interaction_table that only contains pos_interaction. offset(int): The offset that is used for calculating the key(index) in interaction_table Returns: interaction_table(dict): key: user_id * 10^offset + item_id; value: y_{user_id, item_id} """ pos_num = len(pos_interaction_table) neg_num = 0 neg_interaction_table = {} while neg_num < pos_num: user_id = random.randint(0, self.n_users) item_id = random.randint(0, self.n_items) keys = user_id * offset + item_id if keys not in pos_interaction_table: neg_interaction_table[keys] = 0. neg_num += 1 interaction_table = {**pos_interaction_table, **neg_interaction_table} return interaction_table def construct_adj(self, kg_graph): r"""Get neighbors and corresponding relations for each entity in the KG. Args: kg_graph(scipy.sparse.coo_matrix): an undirected graph Returns: tuple: - adj_entity (torch.LongTensor): each line stores the sampled neighbor entities for a given entity, shape: [n_entities, neighbor_sample_size] - adj_relation (torch.LongTensor): each line stores the corresponding sampled neighbor relations, shape: [n_entities, neighbor_sample_size] """ # self.logger.info('constructing knowledge graph ...') # treat the KG as an undirected graph kg_dict = dict() for triple in zip(kg_graph.row, kg_graph.data, kg_graph.col): head = triple[0] relation = triple[1] tail = triple[2] if head not in kg_dict: kg_dict[head] = [] kg_dict[head].append((tail, relation)) if tail not in kg_dict: kg_dict[tail] = [] kg_dict[tail].append((head, relation)) # self.logger.info('constructing adjacency matrix ...') # each line of adj_entity stores the sampled neighbor entities for a given entity # each line of adj_relation stores the corresponding sampled neighbor relations entity_num = kg_graph.shape[0] adj_entity = np.zeros( [entity_num, self.neighbor_sample_size], dtype=np.int64) adj_relation = np.zeros( [entity_num, self.neighbor_sample_size], dtype=np.int64) for entity in range(entity_num): if entity not in kg_dict.keys(): adj_entity[entity] = np.array( [entity] * self.neighbor_sample_size) adj_relation[entity] = np.array( [0] * self.neighbor_sample_size) continue neighbors = kg_dict[entity] n_neighbors = len(neighbors) if n_neighbors >= self.neighbor_sample_size: sampled_indices = np.random.choice(list(range(n_neighbors)), size=self.neighbor_sample_size, replace=False) else: sampled_indices = np.random.choice(list(range(n_neighbors)), size=self.neighbor_sample_size, replace=True) adj_entity[entity] = np.array( [neighbors[i][0] for i in sampled_indices]) adj_relation[entity] = np.array( [neighbors[i][1] for i in sampled_indices]) return torch.from_numpy(adj_entity), torch.from_numpy(adj_relation) def get_neighbors(self, items): r"""Get neighbors and corresponding relations for each entity in items from adj_entity and adj_relation. Args: items(torch.LongTensor): The input tensor that contains item's id, shape: [batch_size, ] Returns: tuple: - entities(list): Entities is a list of i-iter (i = 0, 1, ..., n_iter) neighbors for the batch of items. dimensions of entities: {[batch_size, 1], [batch_size, n_neighbor], [batch_size, n_neighbor^2], ..., [batch_size, n_neighbor^n_iter]} - relations(list): Relations is a list of i-iter (i = 0, 1, ..., n_iter) corresponding relations for entities. Relations have the same shape as entities. """ items = torch.unsqueeze(items, dim=1) entities = [items] relations = [] for i in range(self.n_iter): index = torch.flatten(entities[i]) neighbor_entities = torch.reshape(torch.index_select( self.adj_entity, 0, index), (self.batch_size, -1)) neighbor_relations = torch.reshape(torch.index_select( self.adj_relation, 0, index), (self.batch_size, -1)) entities.append(neighbor_entities) relations.append(neighbor_relations) return entities, relations def aggregate(self, user_embeddings, entities, relations): r"""For each item, aggregate the entity representation and its neighborhood representation into a single vector. Args: user_embeddings(torch.FloatTensor): The embeddings of users, shape: [batch_size, embedding_size] entities(list): entities is a list of i-iter (i = 0, 1, ..., n_iter) neighbors for the batch of items. dimensions of entities: {[batch_size, 1], [batch_size, n_neighbor], [batch_size, n_neighbor^2], ..., [batch_size, n_neighbor^n_iter]} relations(list): relations is a list of i-iter (i = 0, 1, ..., n_iter) corresponding relations for entities. relations have the same shape as entities. Returns: item_embeddings(torch.FloatTensor): The embeddings of items, shape: [batch_size, embedding_size] """ entity_vectors = [self.entity_embedding(i) for i in entities] relation_vectors = [self.relation_embedding(i) for i in relations] for i in range(self.n_iter): entity_vectors_next_iter = [] for hop in range(self.n_iter - i): shape = (self.batch_size, -1, self.neighbor_sample_size, self.embedding_size) self_vectors = entity_vectors[hop] neighbor_vectors = torch.reshape( entity_vectors[hop + 1], shape) neighbor_relations = torch.reshape( relation_vectors[hop], shape) # mix_neighbor_vectors user_embeddings = torch.reshape(user_embeddings, (self.batch_size, 1, 1, self.embedding_size)) # [batch_size, 1, 1, dim] user_relation_scores = torch.mean(user_embeddings * neighbor_relations, dim=-1) # [batch_size, -1, n_neighbor] user_relation_scores_normalized = torch.unsqueeze(self.softmax(user_relation_scores), dim=-1) # [batch_size, -1, n_neighbor, 1] neighbors_agg = torch.mean(user_relation_scores_normalized * neighbor_vectors, dim=2) # [batch_size, -1, dim] if self.aggregator_class == 'sum': output = torch.reshape( self_vectors + neighbors_agg, (-1, self.embedding_size)) # [-1, dim] elif self.aggregator_class == 'neighbor': output = torch.reshape( neighbors_agg, (-1, self.embedding_size)) # [-1, dim] elif self.aggregator_class == 'concat': # [batch_size, -1, dim * 2] output = torch.cat([self_vectors, neighbors_agg], dim=-1) output = torch.reshape( output, (-1, self.embedding_size * 2)) # [-1, dim * 2] else: raise Exception("Unknown aggregator: " + self.aggregator_class) output = self.linear_layers[i](output) # [batch_size, -1, dim] output = torch.reshape( output, [self.batch_size, -1, self.embedding_size]) if i == self.n_iter - 1: vector = self.Tanh(output) else: vector = self.ReLU(output) entity_vectors_next_iter.append(vector) entity_vectors = entity_vectors_next_iter res = torch.reshape( entity_vectors[0], (self.batch_size, self.embedding_size)) return res def label_smoothness_predict(self, user_embeddings, user, entities, relations): r"""Predict the label of items by label smoothness. Args: user_embeddings(torch.FloatTensor): The embeddings of users, shape: [batch_size*2, embedding_size], user(torch.FloatTensor): the index of users, shape: [batch_size*2] entities(list): entities is a list of i-iter (i = 0, 1, ..., n_iter) neighbors for the batch of items. dimensions of entities: {[batch_size*2, 1], [batch_size*2, n_neighbor], [batch_size*2, n_neighbor^2], ..., [batch_size*2, n_neighbor^n_iter]} relations(list): relations is a list of i-iter (i = 0, 1, ..., n_iter) corresponding relations for entities. relations have the same shape as entities. Returns: predicted_labels(torch.FloatTensor): The predicted label of items, shape: [batch_size*2] """ # calculate initial labels; calculate updating masks for label propagation entity_labels = [] # True means the label of this item is reset to initial value during label propagation reset_masks = [] holdout_item_for_user = None for entities_per_iter in entities: users = torch.unsqueeze(user, dim=1) # [batch_size, 1] user_entity_concat = users * self.offset + \ entities_per_iter # [batch_size, n_neighbor^i] # the first one in entities is the items to be held out if holdout_item_for_user is None: holdout_item_for_user = user_entity_concat def lookup_interaction_table(x, _): x = int(x) label = self.interaction_table.setdefault(x, 0.5) return label initial_label = user_entity_concat.clone().cpu().double() initial_label.map_(initial_label, lookup_interaction_table) initial_label = initial_label.float().to(self.device) # False if the item is held out holdout_mask = (holdout_item_for_user - user_entity_concat).bool() # True if the entity is a labeled item reset_mask = (initial_label - 0.5).bool() reset_mask = torch.logical_and( reset_mask, holdout_mask) # remove held-out items initial_label = holdout_mask.float() * initial_label + torch.logical_not( holdout_mask).float() * 0.5 # label initialization reset_masks.append(reset_mask) entity_labels.append(initial_label) # we do not need the reset_mask for the last iteration reset_masks = reset_masks[:-1] # label propagation relation_vectors = [self.relation_embedding(i) for i in relations] for i in range(self.n_iter): entity_labels_next_iter = [] for hop in range(self.n_iter - i): masks = reset_masks[hop] self_labels = entity_labels[hop] neighbor_labels = torch.reshape(entity_labels[hop + 1], [self.batch_size, -1, self.neighbor_sample_size]) neighbor_relations = torch.reshape(relation_vectors[hop], [self.batch_size, -1, self.neighbor_sample_size, self.embedding_size]) # mix_neighbor_labels user_embeddings = torch.reshape(user_embeddings, [self.batch_size, 1, 1, self.embedding_size]) # [batch_size, 1, 1, dim] user_relation_scores = torch.mean(user_embeddings * neighbor_relations, dim=-1) # [batch_size, -1, n_neighbor] user_relation_scores_normalized = self.softmax( user_relation_scores) # [batch_size, -1, n_neighbor] neighbors_aggregated_label = torch.mean(user_relation_scores_normalized * neighbor_labels, dim=2) # [batch_size, -1, dim] # [batch_size, -1] output = masks.float() * self_labels + torch.logical_not(masks).float() * \ neighbors_aggregated_label entity_labels_next_iter.append(output) entity_labels = entity_labels_next_iter predicted_labels = entity_labels[0].squeeze(-1) return predicted_labels def forward(self, user, item): self.batch_size = item.shape[0] # [batch_size, dim] user_e = self.user_embedding(user) # entities is a list of i-iter (i = 0, 1, ..., n_iter) neighbors for the batch of items. dimensions of entities: # {[batch_size, 1], [batch_size, n_neighbor], [batch_size, n_neighbor^2], ..., [batch_size, n_neighbor^n_iter]} entities, relations = self.get_neighbors(item) # [batch_size, dim] item_e = self.aggregate(user_e, entities, relations) return user_e, item_e def calculate_ls_loss(self, user, item, target): r"""Calculate label smoothness loss. Args: user(torch.FloatTensor): the index of users, shape: [batch_size*2], item(torch.FloatTensor): the index of items, shape: [batch_size*2], target(torch.FloatTensor): the label of user-item, shape: [batch_size*2], Returns: ls_loss: label smoothness loss """ user_e = self.user_embedding(user) entities, relations = self.get_neighbors(item) predicted_labels = self.label_smoothness_predict( user_e, user, entities, relations) ls_loss = self.bce_loss(predicted_labels, target) return ls_loss def calculate_loss(self, interaction): user = interaction[self.USER_ID] pos_item = interaction[self.ITEM_ID] neg_item = interaction[self.NEG_ITEM_ID] target = torch.zeros( len(user) * 2, dtype=torch.float32).to(self.device) target[:len(user)] = 1 users = torch.cat((user, user)) items = torch.cat((pos_item, neg_item)) user_e, item_e = self.forward(users, items) predict = torch.mul(user_e, item_e).sum(dim=1) rec_loss = self.bce_loss(predict, target) ls_loss = self.calculate_ls_loss(users, items, target) l2_loss = self.l2_loss(user_e, item_e) loss = rec_loss + self.ls_weight * ls_loss + self.reg_weight * l2_loss return loss def predict(self, interaction): user = interaction[self.USER_ID] item = interaction[self.ITEM_ID] user_e, item_e = self.forward(user, item) return torch.mul(user_e, item_e).sum(dim=1) def full_sort_predict(self, interaction): user_index = interaction[self.USER_ID] item_index = torch.tensor(range(self.n_items)).to(self.device) user = torch.unsqueeze(user_index, dim=1).repeat( 1, item_index.shape[0]) user = torch.flatten(user) item = torch.unsqueeze(item_index, dim=0).repeat( user_index.shape[0], 1) item = torch.flatten(item) user_e, item_e = self.forward(user, item) score = torch.mul(user_e, item_e).sum(dim=1) return score.view(-1)
recbole/model/knowledge_aware_recommender/kgnnls.py
21,110
KGNN-LS is a knowledge-based recommendation model. KGNN-LS transforms the knowledge graph into a user-specific weighted graph and then apply a graph neural network to compute personalized item embeddings. To provide better inductive bias, KGNN-LS relies on label smoothness assumption, which posits that adjacent items in the knowledge graph are likely to have similar user relevance labels/scores. Label smoothness provides regularization over the edge weights and it is equivalent to a label propagation scheme on a graph. For each item, aggregate the entity representation and its neighborhood representation into a single vector. Args: user_embeddings(torch.FloatTensor): The embeddings of users, shape: [batch_size, embedding_size] entities(list): entities is a list of i-iter (i = 0, 1, ..., n_iter) neighbors for the batch of items. dimensions of entities: {[batch_size, 1], [batch_size, n_neighbor], [batch_size, n_neighbor^2], ..., [batch_size, n_neighbor^n_iter]} relations(list): relations is a list of i-iter (i = 0, 1, ..., n_iter) corresponding relations for entities. relations have the same shape as entities. Returns: item_embeddings(torch.FloatTensor): The embeddings of items, shape: [batch_size, embedding_size] Calculate label smoothness loss. Args: user(torch.FloatTensor): the index of users, shape: [batch_size*2], item(torch.FloatTensor): the index of items, shape: [batch_size*2], target(torch.FloatTensor): the label of user-item, shape: [batch_size*2], Returns: ls_loss: label smoothness loss Get neighbors and corresponding relations for each entity in the KG. Args: kg_graph(scipy.sparse.coo_matrix): an undirected graph Returns: tuple: - adj_entity (torch.LongTensor): each line stores the sampled neighbor entities for a given entity, shape: [n_entities, neighbor_sample_size] - adj_relation (torch.LongTensor): each line stores the corresponding sampled neighbor relations, shape: [n_entities, neighbor_sample_size] Get interaction_table that is used for fetching user-item interaction label in LS regularization. Args: user_id(torch.Tensor): the user id in user-item interactions, shape: [n_interactions, 1] item_id(torch.Tensor): the item id in user-item interactions, shape: [n_interactions, 1] y(torch.Tensor): the label in user-item interactions, shape: [n_interactions, 1] Returns: tuple: - interaction_table(dict): key: user_id * 10^offset + item_id; value: y_{user_id, item_id} - offset(int): The offset that is used for calculating the key(index) in interaction_table Get neighbors and corresponding relations for each entity in items from adj_entity and adj_relation. Args: items(torch.LongTensor): The input tensor that contains item's id, shape: [batch_size, ] Returns: tuple: - entities(list): Entities is a list of i-iter (i = 0, 1, ..., n_iter) neighbors for the batch of items. dimensions of entities: {[batch_size, 1], [batch_size, n_neighbor], [batch_size, n_neighbor^2], ..., [batch_size, n_neighbor^n_iter]} - relations(list): Relations is a list of i-iter (i = 0, 1, ..., n_iter) corresponding relations for entities. Relations have the same shape as entities. Predict the label of items by label smoothness. Args: user_embeddings(torch.FloatTensor): The embeddings of users, shape: [batch_size*2, embedding_size], user(torch.FloatTensor): the index of users, shape: [batch_size*2] entities(list): entities is a list of i-iter (i = 0, 1, ..., n_iter) neighbors for the batch of items. dimensions of entities: {[batch_size*2, 1], [batch_size*2, n_neighbor], [batch_size*2, n_neighbor^2], ..., [batch_size*2, n_neighbor^n_iter]} relations(list): relations is a list of i-iter (i = 0, 1, ..., n_iter) corresponding relations for entities. relations have the same shape as entities. Returns: predicted_labels(torch.FloatTensor): The predicted label of items, shape: [batch_size*2] Sample neg_interaction to construct train data. Args: pos_interaction_table(dict): the interaction_table that only contains pos_interaction. offset(int): The offset that is used for calculating the key(index) in interaction_table Returns: interaction_table(dict): key: user_id * 10^offset + item_id; value: y_{user_id, item_id} KGNNLS ################################################ Reference: Hongwei Wang et al. "Knowledge-aware Graph Neural Networks with Label Smoothness Regularization for Recommender Systems." in KDD 2019. Reference code: https://github.com/hwwang55/KGNN-LS -*- coding: utf-8 -*- @Time : 2020/10/3 @Author : Changxin Tian @Email : cx.tian@outlook.com load parameters info which aggregator to use number of iterations when computing entity representation weight of l2 regularization weight of label Smoothness regularization define embedding sample neighbors and construct interaction table define function parameters initialization self.logger.info('constructing knowledge graph ...') treat the KG as an undirected graph self.logger.info('constructing adjacency matrix ...') each line of adj_entity stores the sampled neighbor entities for a given entity each line of adj_relation stores the corresponding sampled neighbor relations mix_neighbor_vectors [batch_size, 1, 1, dim] [batch_size, -1, n_neighbor] [batch_size, -1, n_neighbor, 1] [batch_size, -1, dim] [-1, dim] [-1, dim] [batch_size, -1, dim * 2] [-1, dim * 2] [batch_size, -1, dim] calculate initial labels; calculate updating masks for label propagation True means the label of this item is reset to initial value during label propagation [batch_size, 1] [batch_size, n_neighbor^i] the first one in entities is the items to be held out False if the item is held out True if the entity is a labeled item remove held-out items label initialization we do not need the reset_mask for the last iteration label propagation mix_neighbor_labels [batch_size, 1, 1, dim] [batch_size, -1, n_neighbor] [batch_size, -1, n_neighbor] [batch_size, -1, dim] [batch_size, -1] [batch_size, dim] entities is a list of i-iter (i = 0, 1, ..., n_iter) neighbors for the batch of items. dimensions of entities: {[batch_size, 1], [batch_size, n_neighbor], [batch_size, n_neighbor^2], ..., [batch_size, n_neighbor^n_iter]} [batch_size, dim]
6,632
en
0.750823
""" JSONField automatically serializes most Python terms to JSON data. Creates a TEXT field with a default value of "{}". See test_json.py for more information. from django.db import models from django_extensions.db.fields import json class LOL(models.Model): extra = json.JSONField() """ import datetime from decimal import Decimal from django.db import models from django.conf import settings from django.utils import simplejson from django.utils.encoding import smart_unicode class JSONEncoder(simplejson.JSONEncoder): def default(self, obj): if isinstance(obj, Decimal): return str(obj) elif isinstance(obj, datetime.datetime): assert settings.TIME_ZONE == 'UTC' return obj.strftime('%Y-%m-%dT%H:%M:%SZ') return simplejson.JSONEncoder.default(self, obj) def dumps(value): return JSONEncoder().encode(value) def loads(txt): value = simplejson.loads( txt, parse_float=Decimal, encoding=settings.DEFAULT_CHARSET ) return value class JSONDict(dict): """ Hack so repr() called by dumpdata will output JSON instead of Python formatted data. This way fixtures will work! """ def __repr__(self): return dumps(self) class JSONList(list): """ As above """ def __repr__(self): return dumps(self) class JSONField(models.TextField): """JSONField is a generic textfield that neatly serializes/unserializes JSON objects seamlessly. Main thingy must be a dict object.""" # Used so to_python() is called __metaclass__ = models.SubfieldBase def __init__(self, *args, **kwargs): if 'default' not in kwargs: kwargs['default'] = '{}' models.TextField.__init__(self, *args, **kwargs) def to_python(self, value): """Convert our string value to JSON after we load it from the DB""" if value is None or value == '': return {} elif isinstance(value, basestring): res = loads(value) if isinstance(res, dict): return JSONDict(**res) else: return JSONList(res) else: return value def get_db_prep_save(self, value, connection): """Convert our JSON object to a string before we save""" if not isinstance(value, (list, dict)): return super(JSONField, self).get_db_prep_save("", connection=connection) else: return super(JSONField, self).get_db_prep_save(dumps(value), connection=connection) def south_field_triple(self): "Returns a suitable description of this field for South." # We'll just introspect the _actual_ field. from south.modelsinspector import introspector field_class = "django.db.models.fields.TextField" args, kwargs = introspector(self) # That's our definition! return (field_class, args, kwargs)
vendor-local/src/django-extensions/build/lib/django_extensions/db/fields/json.py
3,020
Hack so repr() called by dumpdata will output JSON instead of Python formatted data. This way fixtures will work! JSONField is a generic textfield that neatly serializes/unserializes JSON objects seamlessly. Main thingy must be a dict object. As above Convert our JSON object to a string before we save Returns a suitable description of this field for South. Convert our string value to JSON after we load it from the DB JSONField automatically serializes most Python terms to JSON data. Creates a TEXT field with a default value of "{}". See test_json.py for more information. from django.db import models from django_extensions.db.fields import json class LOL(models.Model): extra = json.JSONField() Used so to_python() is called We'll just introspect the _actual_ field. That's our definition!
811
en
0.697464
import uuid import arrow from collections import namedtuple HEADERS = ('start', 'stop', 'project', 'id', 'tags', 'updated_at') class Frame(namedtuple('Frame', HEADERS)): def __new__(cls, start, stop, project, id, tags=None, updated_at=None,): try: if not isinstance(start, arrow.Arrow): start = arrow.get(start) if not isinstance(stop, arrow.Arrow): stop = arrow.get(stop) if updated_at is None: updated_at = arrow.utcnow() elif not isinstance(updated_at, arrow.Arrow): updated_at = arrow.get(updated_at) except (ValueError, TypeError) as e: from .watson import WatsonError raise WatsonError("Error converting date: {}".format(e)) start = start.to('local') stop = stop.to('local') if tags is None: tags = [] return super(Frame, cls).__new__( cls, start, stop, project, id, tags, updated_at ) def dump(self): start = self.start.to('utc').int_timestamp stop = self.stop.to('utc').int_timestamp updated_at = self.updated_at.int_timestamp return (start, stop, self.project, self.id, self.tags, updated_at) @property def day(self): return self.start.floor('day') def __lt__(self, other): return self.start < other.start def __lte__(self, other): return self.start <= other.start def __gt__(self, other): return self.start > other.start def __gte__(self, other): return self.start >= other.start class Span(object): def __init__(self, start, stop, timeframe='day'): self.timeframe = timeframe self.start = start.floor(self.timeframe) self.stop = stop.ceil(self.timeframe) def overlaps(self, frame): return frame.start <= self.stop and frame.stop >= self.start def __contains__(self, frame): return frame.start >= self.start and frame.stop <= self.stop class Frames(object): def __init__(self, frames=None): if not frames: frames = [] rows = [Frame(*frame) for frame in frames] self._rows = rows self.changed = False def __len__(self): return len(self._rows) def __getitem__(self, key): if key in HEADERS: return tuple(self._get_col(key)) elif isinstance(key, int): return self._rows[key] else: return self._rows[self._get_index_by_id(key)] def __setitem__(self, key, value): self.changed = True if isinstance(value, Frame): frame = value else: frame = self.new_frame(*value) if isinstance(key, int): self._rows[key] = frame else: frame = frame._replace(id=key) try: self._rows[self._get_index_by_id(key)] = frame except KeyError: self._rows.append(frame) def __delitem__(self, key): self.changed = True if isinstance(key, int): del self._rows[key] else: del self._rows[self._get_index_by_id(key)] def _get_index_by_id(self, id): try: return next( i for i, v in enumerate(self['id']) if v.startswith(id) ) except StopIteration: raise KeyError("Frame with id {} not found.".format(id)) def _get_col(self, col): index = HEADERS.index(col) for row in self._rows: yield row[index] def add(self, *args, **kwargs): self.changed = True frame = self.new_frame(*args, **kwargs) self._rows.append(frame) return frame def new_frame(self, project, start, stop, tags=None, id=None, updated_at=None): if not id: id = uuid.uuid4().hex return Frame(start, stop, project, id, tags=tags, updated_at=updated_at) def dump(self): return tuple(frame.dump() for frame in self._rows) def filter( self, projects=None, tags=None, ignore_projects=None, ignore_tags=None, span=None, include_partial_frames=False, ): for frame in self._rows: if projects is not None and frame.project not in projects: continue if ignore_projects is not None and\ frame.project in ignore_projects: continue if tags is not None and not any(tag in frame.tags for tag in tags): continue if ignore_tags is not None and\ any(tag in frame.tags for tag in ignore_tags): continue if span is None: yield frame elif frame in span: yield frame elif include_partial_frames and span.overlaps(frame): # If requested, return the part of the frame that is within the # span, for frames that are *partially* within span or reaching # over span start = span.start if frame.start < span.start else frame.start stop = span.stop if frame.stop > span.stop else frame.stop yield frame._replace(start=start, stop=stop) def span(self, start, stop): return Span(start, stop)
watson/frames.py
5,446
If requested, return the part of the frame that is within the span, for frames that are *partially* within span or reaching over span
133
en
0.931035
import torch import numpy as np from allennlp.nn import util from relex.modules.offset_embedders import OffsetEmbedder def position_encoding_init(n_position: int, embedding_dim: int): position_enc = np.array([[pos / np.power(10000, 2 * (j // 2) / embedding_dim) for j in range(embedding_dim)] if pos != 0 else np.zeros(embedding_dim) for pos in range(n_position)]) # apply sin on 0th,2nd,4th...embedding_dim position_enc[1:, 0::2] = np.sin(position_enc[1:, 0::2]) # apply cos on 1st,3rd,5th...embedding_dim position_enc[1:, 1::2] = np.cos(position_enc[1:, 1::2]) return torch.from_numpy(position_enc).type(torch.FloatTensor) @OffsetEmbedder.register("sine") class SineOffsetEmbedder(OffsetEmbedder): def __init__(self, n_position: int, embedding_dim: int) -> None: super(SineOffsetEmbedder, self).__init__() self._n_position = n_position self._embedding_dim = embedding_dim self._embedding = torch.nn.Embedding(2 * n_position + 1, embedding_dim, padding_idx=0) self._embedding.weight.data = position_encoding_init(2 * n_position + 1, embedding_dim) # TODO: add zero vector for padding def get_output_dim(self) -> int: return self._embedding_dim def is_additive(self) -> bool: return True def forward(self, inputs: torch.Tensor, mask: torch.Tensor, span: torch.Tensor) -> torch.Tensor: # pylint: disable=arguments-differ # input -> [B x seq_len x d], offset -> [B x 2] batch_size, seq_len, _ = inputs.size() offset = span[:, 0] position_range = util.get_range_vector( seq_len, util.get_device_of(inputs)).repeat((batch_size, 1)) relative_positions = (1 + self._n_position + position_range - offset.unsqueeze(dim=1)) # mask padding so it won't receive a positional embedding relative_positions = relative_positions * mask.long() return self._embedding(relative_positions)
relex/modules/offset_embedders/sine_offset_embedder.py
2,323
apply sin on 0th,2nd,4th...embedding_dim apply cos on 1st,3rd,5th...embedding_dim TODO: add zero vector for padding pylint: disable=arguments-differ input -> [B x seq_len x d], offset -> [B x 2] mask padding so it won't receive a positional embedding
250
en
0.490677
""" Quotes API For Digital Portals The quotes API combines endpoints for retrieving security end-of-day, delayed, and realtime prices with performance key figures and basic reference data on the security and market level. The API supports over 20 different price types for each quote and comes with basic search endpoints based on security identifiers and instrument names. Market coverage is included in the *Sample Use Cases* section below. The Digital Portal use case is focused on high-performance applications that are * serving millions of end-users, * accessible by client browsers via the internet, * supporting subscriptions for streamed updates out-of-the-box, * typically combining a wide variety of *for Digital Portals*-APIs into a highly use-case specific solution for customers, * integrated into complex infrastructures such as existing frontend frameworks, authentication services. All APIs labelled *for Digital Portals* have been designed for direct use by client web applications and feature extreme low latency: The average response time across all endpoints is 30 ms whereas 99% of all requests are answered in close to under 300ms. See the Time Series API for Digital Portals for direct access to price histories, and the News API for Digital Portals for searching and fetching related news. # noqa: E501 The version of the OpenAPI document: 2 Generated by: https://openapi-generator.tech """ import re # noqa: F401 import sys # noqa: F401 from fds.sdk.QuotesAPIforDigitalPortals.model_utils import ( # noqa: F401 ApiTypeError, ModelComposed, ModelNormal, ModelSimple, cached_property, change_keys_js_to_python, convert_js_args_to_python_args, date, datetime, file_type, none_type, validate_get_composed_info, OpenApiModel ) from fds.sdk.QuotesAPIforDigitalPortals.exceptions import ApiAttributeError def lazy_import(): from fds.sdk.QuotesAPIforDigitalPortals.model.inline_response20013_data import InlineResponse20013Data from fds.sdk.QuotesAPIforDigitalPortals.model.inline_response200_meta import InlineResponse200Meta globals()['InlineResponse20013Data'] = InlineResponse20013Data globals()['InlineResponse200Meta'] = InlineResponse200Meta class InlineResponse20013(ModelNormal): """NOTE: This class is auto generated by OpenAPI Generator. Ref: https://openapi-generator.tech Do not edit the class manually. Attributes: allowed_values (dict): The key is the tuple path to the attribute and the for var_name this is (var_name,). The value is a dict with a capitalized key describing the allowed value and an allowed value. These dicts store the allowed enum values. attribute_map (dict): The key is attribute name and the value is json key in definition. discriminator_value_class_map (dict): A dict to go from the discriminator variable value to the discriminator class name. validations (dict): The key is the tuple path to the attribute and the for var_name this is (var_name,). The value is a dict that stores validations for max_length, min_length, max_items, min_items, exclusive_maximum, inclusive_maximum, exclusive_minimum, inclusive_minimum, and regex. additional_properties_type (tuple): A tuple of classes accepted as additional properties values. """ allowed_values = { } validations = { } @cached_property def additional_properties_type(): """ This must be a method because a model may have properties that are of type self, this must run after the class is loaded """ lazy_import() return (bool, date, datetime, dict, float, int, list, str, none_type,) # noqa: E501 _nullable = False @cached_property def openapi_types(): """ This must be a method because a model may have properties that are of type self, this must run after the class is loaded Returns openapi_types (dict): The key is attribute name and the value is attribute type. """ lazy_import() return { 'data': ([InlineResponse20013Data],), # noqa: E501 'meta': (InlineResponse200Meta,), # noqa: E501 } @cached_property def discriminator(): return None attribute_map = { 'data': 'data', # noqa: E501 'meta': 'meta', # noqa: E501 } read_only_vars = { } _composed_schemas = {} @classmethod @convert_js_args_to_python_args def _from_openapi_data(cls, *args, **kwargs): # noqa: E501 """InlineResponse20013 - a model defined in OpenAPI Keyword Args: _check_type (bool): if True, values for parameters in openapi_types will be type checked and a TypeError will be raised if the wrong type is input. Defaults to True _path_to_item (tuple/list): This is a list of keys or values to drill down to the model in received_data when deserializing a response _spec_property_naming (bool): True if the variable names in the input data are serialized names, as specified in the OpenAPI document. False if the variable names in the input data are pythonic names, e.g. snake case (default) _configuration (Configuration): the instance to use when deserializing a file_type parameter. If passed, type conversion is attempted If omitted no type conversion is done. _visited_composed_classes (tuple): This stores a tuple of classes that we have traveled through so that if we see that class again we will not use its discriminator again. When traveling through a discriminator, the composed schema that is is traveled through is added to this set. For example if Animal has a discriminator petType and we pass in "Dog", and the class Dog allOf includes Animal, we move through Animal once using the discriminator, and pick Dog. Then in Dog, we will make an instance of the Animal class but this time we won't travel through its discriminator because we passed in _visited_composed_classes = (Animal,) data ([InlineResponse20013Data]): List of Internet media types.. [optional] # noqa: E501 meta (InlineResponse200Meta): [optional] # noqa: E501 """ _check_type = kwargs.pop('_check_type', True) _spec_property_naming = kwargs.pop('_spec_property_naming', False) _path_to_item = kwargs.pop('_path_to_item', ()) _configuration = kwargs.pop('_configuration', None) _visited_composed_classes = kwargs.pop('_visited_composed_classes', ()) self = super(OpenApiModel, cls).__new__(cls) if args: raise ApiTypeError( "Invalid positional arguments=%s passed to %s. Remove those invalid positional arguments." % ( args, self.__class__.__name__, ), path_to_item=_path_to_item, valid_classes=(self.__class__,), ) self._data_store = {} self._check_type = _check_type self._spec_property_naming = _spec_property_naming self._path_to_item = _path_to_item self._configuration = _configuration self._visited_composed_classes = _visited_composed_classes + (self.__class__,) for var_name, var_value in kwargs.items(): if var_name not in self.attribute_map and \ self._configuration is not None and \ self._configuration.discard_unknown_keys and \ self.additional_properties_type is None: # discard variable. continue setattr(self, var_name, var_value) return self required_properties = set([ '_data_store', '_check_type', '_spec_property_naming', '_path_to_item', '_configuration', '_visited_composed_classes', ]) @convert_js_args_to_python_args def __init__(self, *args, **kwargs): # noqa: E501 """InlineResponse20013 - a model defined in OpenAPI Keyword Args: _check_type (bool): if True, values for parameters in openapi_types will be type checked and a TypeError will be raised if the wrong type is input. Defaults to True _path_to_item (tuple/list): This is a list of keys or values to drill down to the model in received_data when deserializing a response _spec_property_naming (bool): True if the variable names in the input data are serialized names, as specified in the OpenAPI document. False if the variable names in the input data are pythonic names, e.g. snake case (default) _configuration (Configuration): the instance to use when deserializing a file_type parameter. If passed, type conversion is attempted If omitted no type conversion is done. _visited_composed_classes (tuple): This stores a tuple of classes that we have traveled through so that if we see that class again we will not use its discriminator again. When traveling through a discriminator, the composed schema that is is traveled through is added to this set. For example if Animal has a discriminator petType and we pass in "Dog", and the class Dog allOf includes Animal, we move through Animal once using the discriminator, and pick Dog. Then in Dog, we will make an instance of the Animal class but this time we won't travel through its discriminator because we passed in _visited_composed_classes = (Animal,) data ([InlineResponse20013Data]): List of Internet media types.. [optional] # noqa: E501 meta (InlineResponse200Meta): [optional] # noqa: E501 """ _check_type = kwargs.pop('_check_type', True) _spec_property_naming = kwargs.pop('_spec_property_naming', False) _path_to_item = kwargs.pop('_path_to_item', ()) _configuration = kwargs.pop('_configuration', None) _visited_composed_classes = kwargs.pop('_visited_composed_classes', ()) if args: raise ApiTypeError( "Invalid positional arguments=%s passed to %s. Remove those invalid positional arguments." % ( args, self.__class__.__name__, ), path_to_item=_path_to_item, valid_classes=(self.__class__,), ) self._data_store = {} self._check_type = _check_type self._spec_property_naming = _spec_property_naming self._path_to_item = _path_to_item self._configuration = _configuration self._visited_composed_classes = _visited_composed_classes + (self.__class__,) for var_name, var_value in kwargs.items(): if var_name not in self.attribute_map and \ self._configuration is not None and \ self._configuration.discard_unknown_keys and \ self.additional_properties_type is None: # discard variable. continue setattr(self, var_name, var_value) if var_name in self.read_only_vars: raise ApiAttributeError(f"`{var_name}` is a read-only attribute. Use `from_openapi_data` to instantiate " f"class with read only attributes.")
code/python/QuotesAPIforDigitalPortals/v3/fds/sdk/QuotesAPIforDigitalPortals/model/inline_response20013.py
13,055
NOTE: This class is auto generated by OpenAPI Generator. Ref: https://openapi-generator.tech Do not edit the class manually. Attributes: allowed_values (dict): The key is the tuple path to the attribute and the for var_name this is (var_name,). The value is a dict with a capitalized key describing the allowed value and an allowed value. These dicts store the allowed enum values. attribute_map (dict): The key is attribute name and the value is json key in definition. discriminator_value_class_map (dict): A dict to go from the discriminator variable value to the discriminator class name. validations (dict): The key is the tuple path to the attribute and the for var_name this is (var_name,). The value is a dict that stores validations for max_length, min_length, max_items, min_items, exclusive_maximum, inclusive_maximum, exclusive_minimum, inclusive_minimum, and regex. additional_properties_type (tuple): A tuple of classes accepted as additional properties values. InlineResponse20013 - a model defined in OpenAPI Keyword Args: _check_type (bool): if True, values for parameters in openapi_types will be type checked and a TypeError will be raised if the wrong type is input. Defaults to True _path_to_item (tuple/list): This is a list of keys or values to drill down to the model in received_data when deserializing a response _spec_property_naming (bool): True if the variable names in the input data are serialized names, as specified in the OpenAPI document. False if the variable names in the input data are pythonic names, e.g. snake case (default) _configuration (Configuration): the instance to use when deserializing a file_type parameter. If passed, type conversion is attempted If omitted no type conversion is done. _visited_composed_classes (tuple): This stores a tuple of classes that we have traveled through so that if we see that class again we will not use its discriminator again. When traveling through a discriminator, the composed schema that is is traveled through is added to this set. For example if Animal has a discriminator petType and we pass in "Dog", and the class Dog allOf includes Animal, we move through Animal once using the discriminator, and pick Dog. Then in Dog, we will make an instance of the Animal class but this time we won't travel through its discriminator because we passed in _visited_composed_classes = (Animal,) data ([InlineResponse20013Data]): List of Internet media types.. [optional] # noqa: E501 meta (InlineResponse200Meta): [optional] # noqa: E501 InlineResponse20013 - a model defined in OpenAPI Keyword Args: _check_type (bool): if True, values for parameters in openapi_types will be type checked and a TypeError will be raised if the wrong type is input. Defaults to True _path_to_item (tuple/list): This is a list of keys or values to drill down to the model in received_data when deserializing a response _spec_property_naming (bool): True if the variable names in the input data are serialized names, as specified in the OpenAPI document. False if the variable names in the input data are pythonic names, e.g. snake case (default) _configuration (Configuration): the instance to use when deserializing a file_type parameter. If passed, type conversion is attempted If omitted no type conversion is done. _visited_composed_classes (tuple): This stores a tuple of classes that we have traveled through so that if we see that class again we will not use its discriminator again. When traveling through a discriminator, the composed schema that is is traveled through is added to this set. For example if Animal has a discriminator petType and we pass in "Dog", and the class Dog allOf includes Animal, we move through Animal once using the discriminator, and pick Dog. Then in Dog, we will make an instance of the Animal class but this time we won't travel through its discriminator because we passed in _visited_composed_classes = (Animal,) data ([InlineResponse20013Data]): List of Internet media types.. [optional] # noqa: E501 meta (InlineResponse200Meta): [optional] # noqa: E501 This must be a method because a model may have properties that are of type self, this must run after the class is loaded This must be a method because a model may have properties that are of type self, this must run after the class is loaded Returns openapi_types (dict): The key is attribute name and the value is attribute type. Quotes API For Digital Portals The quotes API combines endpoints for retrieving security end-of-day, delayed, and realtime prices with performance key figures and basic reference data on the security and market level. The API supports over 20 different price types for each quote and comes with basic search endpoints based on security identifiers and instrument names. Market coverage is included in the *Sample Use Cases* section below. The Digital Portal use case is focused on high-performance applications that are * serving millions of end-users, * accessible by client browsers via the internet, * supporting subscriptions for streamed updates out-of-the-box, * typically combining a wide variety of *for Digital Portals*-APIs into a highly use-case specific solution for customers, * integrated into complex infrastructures such as existing frontend frameworks, authentication services. All APIs labelled *for Digital Portals* have been designed for direct use by client web applications and feature extreme low latency: The average response time across all endpoints is 30 ms whereas 99% of all requests are answered in close to under 300ms. See the Time Series API for Digital Portals for direct access to price histories, and the News API for Digital Portals for searching and fetching related news. # noqa: E501 The version of the OpenAPI document: 2 Generated by: https://openapi-generator.tech noqa: F401 noqa: F401 noqa: F401 noqa: E501 noqa: E501 noqa: E501 noqa: E501 noqa: E501 noqa: E501 discard variable. noqa: E501 discard variable.
7,299
en
0.81673
import argparse import os import numpy as np import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader from torch.utils.tensorboard import SummaryWriter from ignite.metrics import IoU, Precision, Recall import torchsat.transforms.transforms_cd as T from torchsat.datasets.folder import ChangeDetectionDataset from torchsat.models import FC_EF, FC_Siam_Conc, FC_Siam_Diff def train_one_epoch(epoch, dataloader, model, criterion, optimizer, device, writer): print('train epoch {}'.format(epoch)) model.train() for idx, (pre_img, post_img, targets) in enumerate(dataloader): pre_img, post_img, targets = pre_img.to(device), post_img.to(device), targets.to(device) outputs = model(pre_img, post_img) loss = criterion(outputs, targets) optimizer.zero_grad() loss.backward() optimizer.step() print('train-epoch:{} [{}/{}], loss: {:5.3}'.format(epoch, idx+1, len(dataloader), loss.item())) writer.add_scalar('train/loss', loss.item(), len(dataloader)*epoch+idx) def evalidation(epoch, dataloader, model, criterion, device, writer, tb_test_imgs): print('\neval epoch {}'.format(epoch)) model.eval() recall = Recall(lambda x: (x[0], x[1])) precision = Precision(lambda x: (x[0], x[1])) mean_recall = [] mean_precision = [] mean_loss = [] with torch.no_grad(): for idx, (pre_img, post_img, targets) in enumerate(dataloader): pre_img, post_img, targets = pre_img.to(device), post_img.to(device), targets.to(device) outputs = model(pre_img, post_img) loss = criterion(outputs, targets) preds = outputs.argmax(1) precision.update((preds, targets)) recall.update((preds, targets)) mean_loss.append(loss.item()) mean_recall.append(recall.compute().item()) mean_precision.append(precision.compute().item()) # print('val-epoch:{} [{}/{}], loss: {:5.3}'.format(epoch, idx + 1, len(dataloader), loss.item())) writer.add_scalar('test/loss', loss.item(), len(dataloader) * epoch + idx) if idx < tb_test_imgs: writer.add_image('test/pre', pre_img[0], idx) writer.add_image('test/post', post_img[0], idx) writer.add_image('test/label', label[0], idx) writer.add_image('test/pred', preds, idx) mean_precision, mean_recall = np.array(mean_precision).mean(), np.array(mean_recall).mean() f1 = mean_precision * mean_recall * 2 / (mean_precision + mean_recall + 1e-20) print('precision: {:07.5}, recall: {:07.5}, f1: {:07.5}\n'.format(mean_precision, mean_recall, f1)) writer.add_scalar('test/epoch-loss', np.array(mean_loss).mean(), epoch) writer.add_scalar('test/f1', f1, epoch) writer.add_scalar('test/precision', mean_precision, epoch) writer.add_scalar('test/recall', mean_recall, epoch) def load_data(traindir, valdir, **kwargs): """generate the train and val dataloader, you can change this for your specific task Args: traindir (str): train dataset dir valdir (str): validation dataset dir Returns: tuple: the train dataset and validation dataset """ train_transform = T.Compose([ T.RandomCrop(512), T.RandomHorizontalFlip(), T.RandomVerticalFlip(), T.ToTensor(), T.Normalize(), ]) val_transform = T.Compose([ T.ToTensor(), T.Normalize(), ]) dataset_train = ChangeDetectionDataset(traindir, extentions=kwargs['extensions'], transforms=train_transform, ) dataset_val = ChangeDetectionDataset(valdir, extentions=kwargs['extensions'], transforms=val_transform) return dataset_train, dataset_val def main(args): torch.backends.cudnn.benchmark = True device = torch.device('cuda' if args.device == 'cuda' else 'cpu') # dataset and dataloader train_data, val_data = load_data(args.train_path, args.val_path, extensions=args.extensions) train_loader = DataLoader(train_data, batch_size=args.batch_size, shuffle=True) val_loader = DataLoader(val_data, batch_size=1, shuffle=False) # model # model = get_model(args.model, args.num_classes, pretrained=args.pretrained) # model = FC_EF(num_classes=args.num_classes) model = FC_Siam_Diff(num_classes=args.num_classes) model.to(device) if args.resume: model.load_state_dict(torch.load(args.resume, map_location=device)) # TODO: resume learning rate # loss criterion = nn.CrossEntropyLoss().to(device) criterion = nn.BCELoss() # optim and lr scheduler optimizer = optim.Adam(model.parameters(), lr=args.lr) lr_scheduler = optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0=10, T_mult=1, eta_min=1e-8) # lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1) writer = SummaryWriter(args.ckp_dir) for epoch in range(args.epochs): writer.add_scalar('train/lr', lr_scheduler.get_lr()[0], epoch) train_one_epoch(epoch, train_loader, model, criterion, optimizer, device, writer) evalidation(epoch, val_loader, model, criterion, device, writer, args.tb_test_imgs) lr_scheduler.step() if epoch % 2 == 0: torch.save(model.state_dict(), os.path.join(args.ckp_dir, 'cd_epoch_{}.pth'.format(epoch))) def parse_args(): parser = argparse.ArgumentParser(description='TorchSat Change Detection Training Script') parser.add_argument('--train-path', help='train dataset path') parser.add_argument('--val-path', help='validate dataset path') parser.add_argument('--extensions', nargs='+', default='jpg', help='the train image extension') parser.add_argument('--model', default="unet34", help='model name. default, unet34') parser.add_argument('--pretrained', default=True, help='use ImageNet pretrained params') parser.add_argument('--resume', default='', type=str, metavar='PATH', help='path to latest checkpoint (default: none)') parser.add_argument('--num-classes', default=3, type=int, help='num of classes') parser.add_argument('--in-channels', default=3, type=int, help='input image channels') parser.add_argument('--device', default='cpu', help='device') parser.add_argument('-b', '--batch-size', default=16, type=int, help='batch size') parser.add_argument('--epochs', default=90, type=int, help='epochs') parser.add_argument('--lr', default=0.01, type=float, help='initial learning rate') parser.add_argument('--print-freq', default=10, type=int, help='print frequency') parser.add_argument('--ckp-dir', default='./', help='path to save checkpoint') parser.add_argument('--tb-test-imgs', default=10, help='the num of test image show in tensorboard') args = parser.parse_args() return args if __name__ == "__main__": args = parse_args() main(args)
torchsat/scripts/train_cd.py
6,995
generate the train and val dataloader, you can change this for your specific task Args: traindir (str): train dataset dir valdir (str): validation dataset dir Returns: tuple: the train dataset and validation dataset print('val-epoch:{} [{}/{}], loss: {:5.3}'.format(epoch, idx + 1, len(dataloader), loss.item())) dataset and dataloader model model = get_model(args.model, args.num_classes, pretrained=args.pretrained) model = FC_EF(num_classes=args.num_classes) TODO: resume learning rate loss optim and lr scheduler lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)
609
en
0.429662
# -*- coding: utf-8 -*- """Converts .pyfr[m, s] files to a Paraview VTK UnstructuredGrid File""" from collections import defaultdict import os import numpy as np from pyfr.shapes import BaseShape from pyfr.util import subclass_where from pyfr.writers import BaseWriter class ParaviewWriter(BaseWriter): # Supported file types and extensions name = 'paraview' extn = ['.vtu', '.pvtu'] def __init__(self, args): super().__init__(args) self.dtype = np.dtype(args.precision).type self.divisor = args.divisor or self.cfg.getint('solver', 'order') def _get_npts_ncells_nnodes(self, mk): m_inf = self.mesh_inf[mk] # Get the shape and sub division classes shapecls = subclass_where(BaseShape, name=m_inf[0]) subdvcls = subclass_where(BaseShapeSubDiv, name=m_inf[0]) # Number of vis points npts = shapecls.nspts_from_order(self.divisor + 1)*m_inf[1][1] # Number of sub cells and nodes ncells = len(subdvcls.subcells(self.divisor))*m_inf[1][1] nnodes = len(subdvcls.subnodes(self.divisor))*m_inf[1][1] return npts, ncells, nnodes def _get_array_attrs(self, mk=None): dtype = 'Float32' if self.dtype == np.float32 else 'Float64' dsize = np.dtype(self.dtype).itemsize ndims = self.ndims vvars = self.elementscls.visvarmap[ndims] names = ['', 'connectivity', 'offsets', 'types'] types = [dtype, 'Int32', 'Int32', 'UInt8'] comps = ['3', '', '', ''] for fname, varnames in vvars.items(): names.append(fname.capitalize()) types.append(dtype) comps.append(str(len(varnames))) # If a mesh has been given the compute the sizes if mk: npts, ncells, nnodes = self._get_npts_ncells_nnodes(mk) nb = npts*dsize sizes = [3*nb, 4*nnodes, 4*ncells, ncells] sizes.extend(len(varnames)*nb for varnames in vvars.values()) return names, types, comps, sizes else: return names, types, comps def write_out(self): name, extn = os.path.splitext(self.outf) parallel = extn == '.pvtu' parts = defaultdict(list) for mk, sk in zip(self.mesh_inf, self.soln_inf): prt = mk.split('_')[-1] pfn = '{0}_{1}.vtu'.format(name, prt) if parallel else self.outf parts[pfn].append((mk, sk)) write_s_to_fh = lambda s: fh.write(s.encode('utf-8')) for pfn, misil in parts.items(): with open(pfn, 'wb') as fh: write_s_to_fh('<?xml version="1.0" ?>\n<VTKFile ' 'byte_order="LittleEndian" ' 'type="UnstructuredGrid" ' 'version="0.1">\n<UnstructuredGrid>\n') # Running byte-offset for appended data off = 0 # Header for mk, sk in misil: off = self._write_serial_header(fh, mk, off) write_s_to_fh('</UnstructuredGrid>\n' '<AppendedData encoding="raw">\n_') # Data for mk, sk in misil: self._write_data(fh, mk, sk) write_s_to_fh('\n</AppendedData>\n</VTKFile>') if parallel: with open(self.outf, 'wb') as fh: write_s_to_fh('<?xml version="1.0" ?>\n<VTKFile ' 'byte_order="LittleEndian" ' 'type="PUnstructuredGrid" ' 'version="0.1">\n<PUnstructuredGrid>\n') # Header self._write_parallel_header(fh) # Constitutent pieces for pfn in parts: write_s_to_fh('<Piece Source="{0}"/>\n' .format(os.path.basename(pfn))) write_s_to_fh('</PUnstructuredGrid>\n</VTKFile>\n') def _write_darray(self, array, vtuf, dtype): array = array.astype(dtype) np.uint32(array.nbytes).tofile(vtuf) array.tofile(vtuf) def _write_serial_header(self, vtuf, mk, off): names, types, comps, sizes = self._get_array_attrs(mk) npts, ncells = self._get_npts_ncells_nnodes(mk)[:2] write_s = lambda s: vtuf.write(s.encode('utf-8')) write_s('<Piece NumberOfPoints="{0}" NumberOfCells="{1}">\n' .format(npts, ncells)) write_s('<Points>\n') # Write vtk DaraArray headers for i, (n, t, c, s) in enumerate(zip(names, types, comps, sizes)): write_s('<DataArray Name="{0}" type="{1}" ' 'NumberOfComponents="{2}" ' 'format="appended" offset="{3}"/>\n' .format(n, t, c, off)) off += 4 + s # Write ends/starts of vtk file objects if i == 0: write_s('</Points>\n<Cells>\n') elif i == 3: write_s('</Cells>\n<PointData>\n') # Write end of vtk element data write_s('</PointData>\n</Piece>\n') # Return the current offset return off def _write_parallel_header(self, vtuf): names, types, comps = self._get_array_attrs() write_s = lambda s: vtuf.write(s.encode('utf-8')) write_s('<PPoints>\n') # Write vtk DaraArray headers for i, (n, t, s) in enumerate(zip(names, types, comps)): write_s('<PDataArray Name="{0}" type="{1}" ' 'NumberOfComponents="{2}"/>\n'.format(n, t, s)) if i == 0: write_s('</PPoints>\n<PCells>\n') elif i == 3: write_s('</PCells>\n<PPointData>\n') write_s('</PPointData>\n') def _write_data(self, vtuf, mk, sk): name = self.mesh_inf[mk][0] mesh = self.mesh[mk] soln = self.soln[sk] # Get the shape and sub division classes shapecls = subclass_where(BaseShape, name=name) subdvcls = subclass_where(BaseShapeSubDiv, name=name) # Dimensions nspts, neles = mesh.shape[:2] # Sub divison points inside of a standard element svpts = shapecls.std_ele(self.divisor) nsvpts = len(svpts) # Shape soln_b = shapecls(nspts, self.cfg) # Generate the operator matrices mesh_vtu_op = soln_b.sbasis.nodal_basis_at(svpts) soln_vtu_op = soln_b.ubasis.nodal_basis_at(svpts) # Calculate node locations of vtu elements vpts = np.dot(mesh_vtu_op, mesh.reshape(nspts, -1)) vpts = vpts.reshape(nsvpts, -1, self.ndims) # Calculate solution at node locations of vtu elements vsol = np.dot(soln_vtu_op, soln.reshape(-1, self.nvars*neles)) vsol = vsol.reshape(nsvpts, self.nvars, -1).swapaxes(0, 1) # Append dummy z dimension for points in 2D if self.ndims == 2: vpts = np.pad(vpts, [(0, 0), (0, 0), (0, 1)], 'constant') # Write element node locations to file self._write_darray(vpts.swapaxes(0, 1), vtuf, self.dtype) # Perform the sub division nodes = subdvcls.subnodes(self.divisor) # Prepare vtu cell arrays vtu_con = np.tile(nodes, (neles, 1)) vtu_con += (np.arange(neles)*nsvpts)[:, None] # Generate offset into the connectivity array vtu_off = np.tile(subdvcls.subcelloffs(self.divisor), (neles, 1)) vtu_off += (np.arange(neles)*len(nodes))[:, None] # Tile vtu cell type numbers vtu_typ = np.tile(subdvcls.subcelltypes(self.divisor), neles) # Write vtu node connectivity, connectivity offsets and cell types self._write_darray(vtu_con, vtuf, np.int32) self._write_darray(vtu_off, vtuf, np.int32) self._write_darray(vtu_typ, vtuf, np.uint8) # Primitive and visualisation variable maps privarmap = self.elementscls.privarmap[self.ndims] visvarmap = self.elementscls.visvarmap[self.ndims] # Convert from conservative to primitive variables vsol = np.array(self.elementscls.conv_to_pri(vsol, self.cfg)) # Write out the various fields for vnames in visvarmap.values(): ix = [privarmap.index(vn) for vn in vnames] self._write_darray(vsol[ix].T, vtuf, self.dtype) class BaseShapeSubDiv(object): vtk_types = dict(tri=5, quad=9, tet=10, pyr=14, pri=13, hex=12) vtk_nodes = dict(tri=3, quad=4, tet=4, pyr=5, pri=6, hex=8) @classmethod def subcells(cls, n): pass @classmethod def subcelloffs(cls, n): return np.cumsum([cls.vtk_nodes[t] for t in cls.subcells(n)]) @classmethod def subcelltypes(cls, n): return np.array([cls.vtk_types[t] for t in cls.subcells(n)]) @classmethod def subnodes(cls, n): pass class TensorProdShapeSubDiv(BaseShapeSubDiv): @classmethod def subnodes(cls, n): conbase = np.array([0, 1, n + 2, n + 1]) # Extend quad mapping to hex mapping if cls.ndim == 3: conbase = np.hstack((conbase, conbase + (1 + n)**2)) # Calculate offset of each subdivided element's nodes nodeoff = np.zeros((n,)*cls.ndim) for dim, off in enumerate(np.ix_(*(range(n),)*cls.ndim)): nodeoff += off*(n + 1)**dim # Tile standard element node ordering mapping, then apply offsets internal_con = np.tile(conbase, (n**cls.ndim, 1)) internal_con += nodeoff.T.flatten()[:, None] return np.hstack(internal_con) class QuadShapeSubDiv(TensorProdShapeSubDiv): name = 'quad' ndim = 2 @classmethod def subcells(cls, n): return ['quad']*(n**2) class HexShapeSubDiv(TensorProdShapeSubDiv): name = 'hex' ndim = 3 @classmethod def subcells(cls, n): return ['hex']*(n**3) class TriShapeSubDiv(BaseShapeSubDiv): name = 'tri' @classmethod def subcells(cls, n): return ['tri']*(n**2) @classmethod def subnodes(cls, n): conlst = [] for row in range(n, 0, -1): # Lower and upper indices l = (n - row)*(n + row + 3) // 2 u = l + row + 1 # Base offsets off = [l, l + 1, u, u + 1, l + 1, u] # Generate current row subin = np.ravel(np.arange(row - 1)[..., None] + off) subex = [ix + row - 1 for ix in off[:3]] # Extent list conlst.extend([subin, subex]) return np.hstack(conlst) class TetShapeSubDiv(BaseShapeSubDiv): name = 'tet' @classmethod def subcells(cls, nsubdiv): return ['tet']*(nsubdiv**3) @classmethod def subnodes(cls, nsubdiv): conlst = [] jump = 0 for n in range(nsubdiv, 0, -1): for row in range(n, 0, -1): # Lower and upper indices l = (n - row)*(n + row + 3) // 2 + jump u = l + row + 1 # Lower and upper for one row up ln = (n + 1)*(n + 2) // 2 + l - n + row un = ln + row rowm1 = np.arange(row - 1)[..., None] # Base offsets offs = [(l, l + 1, u, ln), (l + 1, u, ln, ln + 1), (u, u + 1, ln + 1, un), (u, ln, ln + 1, un), (l + 1, u, u+1, ln + 1), (u + 1, ln + 1, un, un + 1)] # Current row conlst.extend(rowm1 + off for off in offs[:-1]) conlst.append(rowm1[:-1] + offs[-1]) conlst.append([ix + row - 1 for ix in offs[0]]) jump += (n + 1)*(n + 2) // 2 return np.hstack(np.ravel(c) for c in conlst) class PriShapeSubDiv(BaseShapeSubDiv): name = 'pri' @classmethod def subcells(cls, n): return ['pri']*(n**3) @classmethod def subnodes(cls, n): # Triangle connectivity tcon = TriShapeSubDiv.subnodes(n).reshape(-1, 3) # Layer these rows of triangles to define prisms loff = (n + 1)*(n + 2) // 2 lcon = [[tcon + i*loff, tcon + (i + 1)*loff] for i in range(n)] return np.hstack(np.hstack(l).flat for l in lcon) class PyrShapeSubDiv(BaseShapeSubDiv): name = 'pyr' @classmethod def subcells(cls, n): cells = [] for i in range(n, 0, -1): cells += ['pyr']*(i**2 + (i - 1)**2) cells += ['tet']*(2*i*(i - 1)) return cells @classmethod def subnodes(cls, nsubdiv): lcon = [] # Quad connectivity qcon = [QuadShapeSubDiv.subnodes(n + 1).reshape(-1, 4) for n in range(nsubdiv)] # Simple functions def _row_in_quad(n, a=0, b=0): return np.array([(n*i + j, n*i + j + 1) for i in range(a, n + b) for j in range(n - 1)]) def _col_in_quad(n, a=0, b=0): return np.array([(n*i + j, n*(i + 1) + j) for i in range(n - 1) for j in range(a, n + b)]) u = 0 for n in range(nsubdiv, 0, -1): l = u u += (n + 1)**2 lower_quad = qcon[n - 1] + l upper_pts = np.arange(n**2) + u # First set of pyramids lcon.append([lower_quad, upper_pts]) if n > 1: upper_quad = qcon[n - 2] + u lower_pts = np.hstack(range(k*(n + 1)+1, (k + 1)*n + k) for k in range(1, n)) + l # Second set of pyramids lcon.append([upper_quad[:, ::-1], lower_pts]) lower_row = _row_in_quad(n + 1, 1, -1) + l lower_col = _col_in_quad(n + 1, 1, -1) + l upper_row = _row_in_quad(n) + u upper_col = _col_in_quad(n) + u # Tetrahedra lcon.append([lower_col, upper_row]) lcon.append([lower_row[:, ::-1], upper_col]) return np.hstack(np.column_stack(l).flat for l in lcon)
pyfr/writers/paraview.py
14,232
Converts .pyfr[m, s] files to a Paraview VTK UnstructuredGrid File -*- coding: utf-8 -*- Supported file types and extensions Get the shape and sub division classes Number of vis points Number of sub cells and nodes If a mesh has been given the compute the sizes Running byte-offset for appended data Header Data Header Constitutent pieces Write vtk DaraArray headers Write ends/starts of vtk file objects Write end of vtk element data Return the current offset Write vtk DaraArray headers Get the shape and sub division classes Dimensions Sub divison points inside of a standard element Shape Generate the operator matrices Calculate node locations of vtu elements Calculate solution at node locations of vtu elements Append dummy z dimension for points in 2D Write element node locations to file Perform the sub division Prepare vtu cell arrays Generate offset into the connectivity array Tile vtu cell type numbers Write vtu node connectivity, connectivity offsets and cell types Primitive and visualisation variable maps Convert from conservative to primitive variables Write out the various fields Extend quad mapping to hex mapping Calculate offset of each subdivided element's nodes Tile standard element node ordering mapping, then apply offsets Lower and upper indices Base offsets Generate current row Extent list Lower and upper indices Lower and upper for one row up Base offsets Current row Triangle connectivity Layer these rows of triangles to define prisms Quad connectivity Simple functions First set of pyramids Second set of pyramids Tetrahedra
1,564
en
0.720048
#!/c/python27/python import os from utils import * def cli_cpp(parms): return os.path.join(parms['OVPN3'], "core", "test", "ovpncli", "cli.cpp") def src_fn(parms, srcfile): # Get source file name if srcfile: if '.' not in os.path.basename(srcfile): srcfile += ".cpp" else: srcfile = cli_cpp(parms) return srcfile def is_unit_test(argv): unit_test = False if len(argv) >= 2: unit_test = argv[1] == "unittest" return unit_test def src_fn_argv(parms, argv): srcfile = None if len(argv) >= 1: srcfile = argv[0] return src_fn(parms, srcfile) def build(parms, srcfile, unit_test=False): # Debug? if parms['DEBUG']: dbg_rel_flags = "/Zi" else: dbg_rel_flags = "/O2" # Dictionary we will use to substitute parameters # onto VC command line. options = { "ovpn3" : parms['OVPN3'], "tap" : os.path.join(parms['TAP'], 'src'), "tap_component_id" : parms['TAP_WIN_COMPONENT_ID'], "asio" : os.path.join(build_dir(parms), "asio"), "mbedtls" : os.path.join(build_dir(parms), "mbedtls"), "lz4" : os.path.join(build_dir(parms), "lz4", "lib"), "srcfile" : srcfile, "extra_defs" : parms['CPP_EXTRA'], "extra_inc" : "", "extra_lib_path" : "", "extra_lib" : "", } vc_parms(parms, options) # Do we need to support XP and Win 2003? arch = os.environ.get("ARCH", parms['ARCH']) if arch == "x86_xp": options['extra_defs'] += " /D_WIN32_WINNT=0x0501" # pre-Vista else: options['extra_defs'] += " /D_WIN32_WINNT=0x0600" # Vista and later options['extra_lib'] += " fwpuclnt.lib" # Add jsoncpp (optional) if 'jsoncpp' in parms['LIB_VERSIONS']: options["jsoncpp"] = os.path.join(build_dir(parms), "jsoncpp") options['extra_inc'] += " /DHAVE_JSONCPP /I %(jsoncpp)s/dist" % options options['extra_lib_path'] += " /LIBPATH:%(jsoncpp)s/dist" % options options['extra_lib'] += " jsoncpp.lib" if unit_test: options['extra_lib'] += " gtest.lib" options['extra_inc'] += " /I %s" % os.path.join(parms["GTEST_ROOT"], "googletest", "include") options['extra_lib_path'] += " /LIBPATH:%s" % os.path.join(parms["GTEST_ROOT"], "googlemock", "gtest", "Debug") # Build OpenVPN Connect if parms.get("CONNECT"): options['extra_inc'] += " /I " + os.path.join(parms['OVPN3'], "common") # build it vc_cmd(parms, r"cl %(extra_defs)s /DNOMINMAX /D_CRT_SECURE_NO_WARNINGS /DUSE_ASIO /DASIO_STANDALONE /DASIO_NO_DEPRECATED /I %(asio)s\asio\include /DUSE_MBEDTLS /I %(mbedtls)s\include /DHAVE_LZ4 /I %(lz4)s%(extra_inc)s -DTAP_WIN_COMPONENT_ID=%(tap_component_id)s /I %(tap)s /I %(ovpn3)s\core /EHsc %(link_static_dynamic_flags)s /W0 %(dbg_rel_flags)s /nologo %(srcfile)s /link /LIBPATH:%(mbedtls)s\library /LIBPATH:%(lz4)s%(extra_lib_path)s mbedtls.lib lz4.lib%(extra_lib)s ws2_32.lib crypt32.lib iphlpapi.lib winmm.lib user32.lib gdi32.lib advapi32.lib wininet.lib shell32.lib ole32.lib rpcrt4.lib" % options, arch=os.environ.get("ARCH")) if __name__ == "__main__": import sys from parms import PARMS # some parameters might be redefined, like in Jenkins multibranch pipeline case PARMS['BUILD'] = os.environ.get('BUILD', PARMS['BUILD']) PARMS['OVPN3'] = os.environ.get('OVPN3', PARMS['OVPN3']) src = src_fn_argv(PARMS, sys.argv[1:]) unit_test = is_unit_test(sys.argv[1:]) build(PARMS, src, unit_test)
Carthage/Checkouts/openvpn-adapter/OpenVPN Adapter/Vendors/openvpn/win/build.py
3,561
!/c/python27/python Get source file name Debug? Dictionary we will use to substitute parameters onto VC command line. Do we need to support XP and Win 2003? pre-Vista Vista and later Add jsoncpp (optional) Build OpenVPN Connect build it some parameters might be redefined, like in Jenkins multibranch pipeline case
314
en
0.659056
#!/usr/bin/env python ## @package teleop_joy A node for controlling the P3DX with an XBox controller import rospy from geometry_msgs.msg import Twist from nav_msgs.msg import Odometry from sensor_msgs.msg import Joy import numpy as np def quat2yaw(q): return np.arctan2(2*(q.y*q.z + q.w*q.x), 1 - 2*(q.z**2 + q.w**2)) def joyCallback(msg): global cmd_vel_pub global linear_axis global linear_scale global rotation_axis global rotation_scale global yaw cmd_vel_msg = Twist() cmd_vel_msg.linear.x = msg.axes[linear_axis] * linear_scale cmd_vel_msg.angular.z = msg.axes[rotation_axis] * rotation_scale cmd_vel_msg.angular.y = np.inf cmd_vel_pub.publish(cmd_vel_msg) if __name__ == '__main__': rospy.init_node('teleop_joy') global cmd_vel_pub global linear_axis global linear_scale global rotation_axis global rotation_scale global yaw linear_axis = rospy.get_param('linear_axis' , 1) linear_scale = rospy.get_param('linear_scale' , 5) rotation_axis = rospy.get_param('rotation_axis' , 3) rotation_scale = rospy.get_param('rotation_scale', 1) cmd_vel_pub = rospy.Publisher("/asv/cmd_vel", Twist, queue_size=1) rospy.Subscriber("joy", Joy, joyCallback) rospy.spin()
nodes/teleop_joy.py
1,280
!/usr/bin/env python @package teleop_joy A node for controlling the P3DX with an XBox controller
96
en
0.471691
import os import fs from .utils import Docs, custom_dedent class TestTutorial(Docs): def test_level_1(self): expected = "world" folder = "level-1-jinja2-cli" self._moban(folder, expected) def test_level_1_custom_define(self): expected = "maailman" folder = "level-1-jinja2-cli" args = [ "moban", "-d", "hello=maailman", "-t", "a.template", "-o", "moban.output", ] self.run_moban(args, folder, [("moban.output", expected)]) def test_level_2(self): expected = """ ========header============ world ========footer============ """ expected = custom_dedent(expected) folder = "level-2-template-inheritance" self._moban(folder, expected) def test_level_3(self): expected = """ ========header============ world shijie ========footer============ """ expected = custom_dedent(expected) folder = "level-3-data-override" self._moban(folder, expected) def test_level_4(self): expected = """ ========header============ world shijie ========footer============ """ expected = custom_dedent(expected) folder = "level-4-single-command" self.run_moban(["moban"], folder, [("a.output", expected)]) def test_level_5(self): expected = """ ========header============ world shijie this demonstrates jinja2's include statement ========footer============ """ expected = custom_dedent(expected) folder = "level-5-custom-configuration" self.run_moban(["moban"], folder, [("a.output", expected)]) def test_level_6(self): expected = """ ========header============ world2 shijie this demonstrates jinja2's include statement ========footer============ """ expected = custom_dedent(expected) folder = "level-6-complex-configuration" self.run_moban(["moban"], folder, [("a.output2", expected)]) def test_level_20(self): expected = """ ========header============ world2 shijie this demonstrates jinja2's include statement ========footer============ """ expected = custom_dedent(expected) folder = "level-20-templates-configs-in-zip-or-tar" self.run_moban_with_fs( ["moban"], folder, [("zip://a.zip!/a.output2", expected)] ) def test_level_7(self): expected = """ Hello, you are in level 7 example Hello, you are not in level 7 """ expected = custom_dedent(expected) folder = "level-7-use-custom-jinja2-filter-test-n-global" self.run_moban(["moban"], folder, [("test.output", expected)]) def test_level_8(self): expected = "it is a test\n" folder = "level-8-pass-a-folder-full-of-templates" check_file = fs.path.join("templated-folder", "my") self.run_moban(["moban"], folder, [(check_file, expected)]) def test_level_9(self): expected = "pypi-mobans: moban dependency as pypi package" folder = "level-9-moban-dependency-as-pypi-package" self.run_moban(["moban"], folder, [("test.txt", expected)]) def test_level_24(self): expected = "pypi-mobans: files over http protocol" folder = "level-24-files-over-http" self.run_moban(["moban"], folder, [("test.txt", expected)]) def test_level_9_deprecated(self): expected = "pypi-mobans: moban dependency as pypi package" folder = "deprecated-level-9-moban-dependency-as-pypi-package" self.run_moban(["moban"], folder, [("test.txt", expected)]) def test_level_10(self): expected = "pypi-mobans: moban dependency as git repo" folder = "level-10-moban-dependency-as-git-repo" self.run_moban(["moban"], folder, [("test.txt", expected)]) def test_level_10_deprecated(self): expected = "pypi-mobans: moban dependency as git repo" folder = "deprecated-level-10-moban-dependency-as-git-repo" self.run_moban(["moban"], folder, [("test.txt", expected)]) def test_level_11(self): expected = "handlebars does not support inheritance\n" folder = "level-11-use-handlebars" self.run_moban(["moban"], folder, [("a.output", expected)]) def test_level_12(self): expected_a = """ world world world world b.template exists a/b Static text generator using any template, any data and any location. """ expected_b = """ 142 42 142 """ expected_a = custom_dedent(expected_a) expected_b = custom_dedent(expected_b) folder = "level-12-use-template-engine-extensions" self.run_moban( ["moban"], folder, [("a.output", expected_a), ("b.output", expected_b)], ) def test_level_13_json(self): expected = """ ========header============ world from child.json shijie from parent.yaml ========footer============ """ expected = custom_dedent(expected) folder = "level-13-any-data-override-any-data" commands = [ "moban", "-c", "child.json", "-t", "a.template", "-o", "moban.output", ] self.run_moban(commands, folder, [("moban.output", expected)]) def test_level_13_yaml(self): expected = """ ========header============ world from child.yaml shijie from parent.json ========footer============ """ expected = custom_dedent(expected) folder = "level-13-any-data-override-any-data" commands = [ "moban", "-c", "child.yaml", "-t", "a.template", "-o", "moban.output", ] self.run_moban(commands, folder, [("moban.output", expected)]) def test_level_14_custom(self): expected = """ ========header============ world from child.cusom shijie from parent.json ========footer============ """ expected = custom_dedent(expected) folder = "level-14-custom-data-loader" commands = ["moban"] self.run_moban(commands, folder, [("a.output", expected)]) def test_level_15_copy_templates_as_target(self): expected = "test file\n" folder = "level-15-copy-templates-as-target" assertions = [ ("simple.file", expected), ( "target_without_template_type", "file extension will trigger copy engine\n", ), ( "target_in_short_form", ( "it is OK to have a short form, " + "but the file to be 'copied' shall have 'copy' extension, " + "so as to trigger ContentForwardEngine, 'copy' engine.\n" ), ), ( "output_is_copied.same_file_extension", "it is implicit copy as well", ), ] self.run_moban(["moban"], folder, assertions) def test_level_21_copy_templates_into_zips(self): expected = "test file\n" folder = "level-21-copy-templates-into-an-alien-file-system" long_url = ( "zip://my.zip!/test-recursive-dir/sub_directory_is_copied" + "/because_star_star_is_specified.txt" ) criterias = [ ["zip://my.zip!/simple.file", expected], [ "zip://my.zip!/target_without_template_type", "file extension will trigger copy engine\n", ], [ "zip://my.zip!/target_in_short_form", ( "it is OK to have a short form, " + "but the file to be 'copied' shall have 'copy' extension, " + "so as to trigger ContentForwardEngine, 'copy' engine.\n" ), ], ["zip://my.zip!/test-dir/afile.txt", "dir for copying\n"], [long_url, "dest_directory: source_directory/**\n"], ] self.run_moban_with_fs(["moban"], folder, criterias) def test_level_16_group_targets_using_template_type(self): expected = "test file\n" folder = "level-16-group-targets-using-template-type" self.run_moban(["moban"], folder, [("simple.file", expected)]) def test_level_17_force_template_type_from_moban_file(self): expected = "test file\n" folder = "level-17-force-template-type-from-moban-file" self.run_moban(["moban"], folder, [("simple.file", expected)]) def test_level_18_user_defined_template_types(self): from datetime import datetime expected = "{date}\n".format(date=datetime.now().strftime("%Y-%m-%d")) folder = "level-18-user-defined-template-types" self.run_moban( ["moban"], folder, [("a.output", expected), ("b.output", "shijie\n")], ) def test_level_19_without_group_target(self): expected = "test file\n" folder = "level-19-moban-a-sub-group-in-targets" assertions = [ ("simple.file", expected), ("a.output", "I will not be selected in level 19\n"), ] self.run_moban(["moban"], folder, assertions) def test_level_19_with_group_target(self): expected = "test file\n" folder = "level-19-moban-a-sub-group-in-targets" self.run_moban( ["moban", "-g", "copy"], folder, [("simple.file", expected)] ) # make sure only copy target is executed assert False == os.path.exists("a.output") def test_level_22_intermediate_targets(self): expected = "a world\n" folder = "level-22-intermediate-targets" self.run_moban(["moban"], folder, [("final", expected)]) assert os.path.exists("intermediate.jj2") def test_level_25_delete_intermediate_targets(self): expected = "a world\n" folder = "level-25-delete-intermediate-targets" self.run_moban(["moban"], folder, [("final", expected)]) assert not os.path.exists("intermediate.jj2") assert not os.path.exists("intermediate2.jj2") assert not os.path.exists("intermediate3.jj2") def test_level_26_strip_intermediate_targets(self): expected = "a world" folder = "level-26-strip-rendered-content" self.run_moban(["moban"], folder, [("final", expected)]) assert not os.path.exists("intermediate.strip") def test_level_23_inherit_parent_moban_file(self): folder = "level-23-inherit-organisational-moban-file" self.run_moban( ["moban"], folder, [("output_a", "I am template a"), ("output_b", "I am template b")], ) def test_misc_1(self): expected = "test file\n" folder = "misc-1-copying-templates" self.run_moban(["moban"], folder, [("simple.file", expected)]) def _moban(self, folder, expected): args = [ "moban", "-c", "data.yml", "-t", "a.template", "-o", "moban.output", ] self.run_moban(args, folder, [("moban.output", expected)])
tests/test_docs.py
11,782
make sure only copy target is executed
38
en
0.97389
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # This source code is licensed under the BSD-style license found in the # LICENSE file in the root directory of this source tree. An additional grant # of patent rights can be found in the PATENTS file in the same directory. from parlai.core.agents import Agent from parlai.core.dict import DictionaryAgent from torch.autograd import Variable from torch import optim import torch.nn as nn import torch import os import random import pdb class Seq2seqAgent(Agent): """Simple agent which uses an RNN to process incoming text observations. The RNN generates a vector which is used to represent the input text, conditioning on the context to generate an output token-by-token. For more information, see Sequence to Sequence Learning with Neural Networks `(Sutskever et al. 2014) <https://arxiv.org/abs/1409.3215>`_. """ @staticmethod def add_cmdline_args(argparser): """Add command-line arguments specifically for this agent.""" DictionaryAgent.add_cmdline_args(argparser) agent = argparser.add_argument_group('Seq2Seq Arguments') agent.add_argument('-hs', '--hiddensize', type=int, default=128, help='size of the hidden layers and embeddings') agent.add_argument('-nl', '--numlayers', type=int, default=2, help='number of hidden layers') agent.add_argument('-lr', '--learningrate', type=float, default=0.001, help='learning rate') agent.add_argument('-dr', '--dropout', type=float, default=0.1, help='dropout rate') # agent.add_argument('-att', '--attention', type='bool', default=False, # help='whether to use attention over the context during decoding') # agent.add_argument('-bi', '--bidirectional', type='bool', default=False, # help='whether to encode the context with a bidirectional RNN') agent.add_argument('--no-cuda', action='store_true', default=False, help='disable GPUs even if available') agent.add_argument('--gpu', type=int, default=-1, help='which GPU device to use') agent.add_argument('-rc', '--rank-candidates', type='bool', default=False, help='rank candidates if available. this is done by computing the' + ' mean score per token for each candidate and selecting the ' + 'highest scoring one.') def __init__(self, opt, shared=None): # initialize defaults first super().__init__(opt, shared) if not shared: # this is not a shared instance of this class, so do full # initialization. if shared is set, only set up shared members. # check for cuda self.use_cuda = not opt.get('no_cuda') and torch.cuda.is_available() if self.use_cuda: print('[ Using CUDA ]') torch.cuda.set_device(opt['gpu']) if opt.get('model_file') and os.path.isfile(opt['model_file']): # load model parameters if available print('Loading existing model params from ' + opt['model_file']) new_opt, self.states = self.load(opt['model_file']) # override options with stored ones opt = self.override_opt(new_opt) self.dict = DictionaryAgent(opt) self.id = 'Seq2Seq' # we use START markers to start our output self.START = self.dict.start_token self.START_TENSOR = torch.LongTensor(self.dict.parse(self.START)) # we use END markers to end our output self.END = self.dict.end_token self.END_TENSOR = torch.LongTensor(self.dict.parse(self.END)) # get index of null token from dictionary (probably 0) self.NULL_IDX = self.dict.txt2vec(self.dict.null_token)[0] # logFile #self.logFile = opt['logFile'] # store important params directly hsz = opt['hiddensize'] self.hidden_size = hsz self.num_layers = opt['numlayers'] self.learning_rate = opt['learningrate'] self.rank = opt['rank_candidates'] self.longest_label = 1 # set up tensors self.zeros = torch.zeros(self.num_layers, 1, hsz) self.xs = torch.LongTensor(1, 1) self.ys = torch.LongTensor(1, 1) self.cands = torch.LongTensor(1, 1, 1) self.cand_scores = torch.FloatTensor(1) self.cand_lengths = torch.LongTensor(1) # set up modules self.criterion = nn.NLLLoss() # lookup table stores word embeddings self.lt = nn.Embedding() # FILL HERE # encoder captures the input text self.encoder = nn.GRU() # FILL HERE # decoder produces our output states self.decoder = nn.GRU() # FILL HERE # linear layer helps us produce outputs from final decoder state self.h2o = nn.Linear() # FILL HERE # droput on the linear layer helps us generalize self.dropout = nn.Dropout(opt['dropout']) # softmax maps output scores to probabilities self.softmax = nn.LogSoftmax() # set up optims for each module lr = opt['learningrate'] """ self.optims = { 'lt': optim.SGD(self.lt.parameters(), lr=lr), 'encoder': optim.SGD(self.encoder.parameters(), lr=lr), 'decoder': optim.SGD(self.decoder.parameters(), lr=lr), 'h2o': optim.SGD(self.h2o.parameters(), lr=lr), } """ self.optims = { 'lt': optim.Adam(self.lt.parameters(), lr=lr), 'encoder': optim.Adam(self.encoder.parameters(), lr=lr), 'decoder': optim.Adam(self.decoder.parameters(), lr=lr), 'h2o': optim.Adam(self.h2o.parameters(), lr=lr), } if hasattr(self, 'states'): # set loaded states if applicable self.set_states(self.states) if self.use_cuda: self.cuda() self.episode_done = True def override_opt(self, new_opt): """Print out each added key and each overriden key. Only override args specific to the model. """ model_args = {'hiddensize', 'numlayers'} for k, v in new_opt.items(): if k not in model_args: # skip non-model args continue if k not in self.opt: print('Adding new option [ {k}: {v} ]'.format(k=k, v=v)) elif self.opt[k] != v: print('Overriding option [ {k}: {old} => {v}]'.format( k=k, old=self.opt[k], v=v)) self.opt[k] = v return self.opt def parse(self, text): return self.dict.txt2vec(text) def v2t(self, vec): return self.dict.vec2txt(vec) def cuda(self): self.START_TENSOR = self.START_TENSOR.cuda(async=True) self.END_TENSOR = self.END_TENSOR.cuda(async=True) self.zeros = self.zeros.cuda(async=True) self.xs = self.xs.cuda(async=True) self.ys = self.ys.cuda(async=True) self.cands = self.cands.cuda(async=True) self.cand_scores = self.cand_scores.cuda(async=True) self.cand_lengths = self.cand_lengths.cuda(async=True) self.criterion.cuda() self.lt.cuda() self.encoder.cuda() self.decoder.cuda() self.h2o.cuda() self.dropout.cuda() self.softmax.cuda() def hidden_to_idx(self, hidden, dropout=False): """Converts hidden state vectors into indices into the dictionary.""" if hidden.size(0) > 1: raise RuntimeError('bad dimensions of tensor:', hidden) hidden = hidden.squeeze(0) scores = # FILL HERE if dropout: scores = # FILL HERE scores = # FILL HERE _max_score, idx = scores.max(1) return idx, scores def zero_grad(self): for optimizer in self.optims.values(): optimizer.zero_grad() def update_params(self): for optimizer in self.optims.values(): optimizer.step() def reset(self): self.observation = None self.episode_done = True def observe(self, observation): # shallow copy observation (deep copy can be expensive) observation = observation.copy() if not self.episode_done: # if the last example wasn't the end of an episode, then we need to # recall what was said in that example prev_dialogue = self.observation['text'] observation['text'] = prev_dialogue + '\n' + observation['text'] self.observation = observation self.episode_done = observation['episode_done'] return observation def predict(self, xs, ys=None, cands=None): """Produce a prediction from our model. Update the model using the targets if available. """ batchsize = len(xs) text_cand_inds = None # first encode context #xes = self.lt(xs).t() xes = self.lt(xs).transpose(0,1) # Ken if self.zeros.size(1) != batchsize: self.zeros.resize_(self.num_layers, batchsize, self.hidden_size).fill_(0) h0 = Variable(self.zeros) _output, hn = self.encoder(xes, h0) # next we use END as an input to kick off our decoder x = Variable(self.START_TENSOR) xe = self.lt(x).unsqueeze(1) xes = xe.expand(xe.size(0), batchsize, xe.size(2)) # list of output tokens for each example in the batch output_lines = [[] for _ in range(batchsize)] if ys is not None: # update the model based on the labels self.zero_grad() loss = 0 # keep track of longest label we've ever seen self.longest_label = max(self.longest_label, ys.size(1)) for i in range(ys.size(1)): output, hn = self.decoder(xes, hn) preds, scores = self.hidden_to_idx(output, dropout=True) y = ys.select(1, i) loss += self.criterion(scores, y) # use the true token as the next input instead of predicted # this produces a biased prediction but better training xes = self.lt(y).unsqueeze(0) for b in range(batchsize): # convert the output scores to tokens token = self.v2t([preds.data[b][0]]) #token = self.v2t([preds.data[b]]) # Ken output_lines[b].append(token) loss.backward() #pdb.set_trace() self.update_params() if random.random() < 0.01: # sometimes output a prediction for debugging self.nWord = ys.data.nonzero().size()[0] self.nll_per_word = loss.data[0]/self.nWord print('prediction:', ' '.join(output_lines[0]), '\nlabel:', self.dict.vec2txt(ys.data[0])) else: # just produce a prediction without training the model done = [False for _ in range(batchsize)] total_done = 0 max_len = 0 if cands: # score each candidate separately # cands are exs_with_cands x cands_per_ex x words_per_cand # cview is total_cands x words_per_cand cview = cands.view(-1, cands.size(2)) cands_xes = xe.expand(xe.size(0), cview.size(0), xe.size(2)) sz = hn.size() cands_hn = ( hn.view(sz[0], sz[1], 1, sz[2]) .expand(sz[0], sz[1], cands.size(1), sz[2]) .contiguous() .view(sz[0], -1, sz[2]) ) cand_scores = Variable( self.cand_scores.resize_(cview.size(0)).fill_(0)) cand_lengths = Variable( self.cand_lengths.resize_(cview.size(0)).fill_(0)) for i in range(cview.size(1)): output, cands_hn = self.decoder(cands_xes, cands_hn) preds, scores = self.hidden_to_idx(output, dropout=False) cs = cview.select(1, i) non_nulls = cs.ne(self.NULL_IDX) cand_lengths += non_nulls.long() score_per_cand = torch.gather(scores, 1, cs.unsqueeze(1)) cand_scores += score_per_cand.squeeze() * non_nulls.float() cands_xes = self.lt(cs).unsqueeze(0) # set empty scores to -1, so when divided by 0 they become -inf cand_scores -= cand_lengths.eq(0).float() # average the scores per token cand_scores /= cand_lengths.float() cand_scores = cand_scores.view(cands.size(0), cands.size(1)) srtd_scores, text_cand_inds = cand_scores.sort(1, True) text_cand_inds = text_cand_inds.data # now, generate a response from scratch while(total_done < batchsize) and max_len < self.longest_label: # keep producing tokens until we hit END or max length for each # example in the batch #pdb.set_trace() output, hn = self.decoder(xes, hn) preds, scores = self.hidden_to_idx(output, dropout=False) #xes = self.lt(preds.t()) # original """ if (self.opt['mode'] == 'train'): xes = torch.unsqueeze(self.lt(preds),0) # torch.unsqueeze makes error when prediction elif(self.opt['mode'] == 'interactive'): xes = self.lt(preds) """ xes = torch.unsqueeze(self.lt(preds),0) # KB-KAIST max_len += 1 for b in range(batchsize): if not done[b]: # only add more tokens for examples that aren't done yet #pdb.set_trace() #token = self.v2t(preds.data[b]) token = self.v2t([preds.data[b]]) # KB-KAIST if token == self.END: # if we produced END, we're done done[b] = True total_done += 1 else: output_lines[b].append(token) if (random.random() < 0.1 and self.opt['mode'] == 'train'): # sometimes output a prediction for debugging print('prediction:', ' '.join(output_lines[0])) return output_lines, text_cand_inds def batchify(self, observations): """Convert a list of observations into input & target tensors.""" # valid examples exs = [ex for ex in observations if 'text' in ex] # the indices of the valid (non-empty) tensors valid_inds = [i for i, ex in enumerate(observations) if 'text' in ex] # set up the input tensors batchsize = len(exs) # tokenize the text xs = None if batchsize > 0: parsed = [self.parse(ex['text']) for ex in exs] min_x_len = min([len(x) for x in parsed]) max_x_len = max([len(x) for x in parsed]) parsed_x_len = min(min_x_len + 12, max_x_len, 48) # shrink xs to to limit batch computation parsed = [x[:parsed_x_len] for x in parsed] xs = torch.LongTensor(batchsize, parsed_x_len).fill_(0) # pack the data to the right side of the tensor for this model for i, x in enumerate(parsed): offset = parsed_x_len - len(x) for j, idx in enumerate(x): xs[i][j + offset] = idx if self.use_cuda: # copy to gpu self.xs.resize_(xs.size()) self.xs.copy_(xs, async=True) xs = Variable(self.xs) else: xs = Variable(xs) # set up the target tensors ys = None if batchsize > 0 and any(['labels' in ex for ex in exs]): # randomly select one of the labels to update on, if multiple # append END to each label labels = [random.choice(ex.get('labels', [''])) + ' ' + self.END for ex in exs] parsed = [self.parse(y) for y in labels] min_y_len = min(len(y) for y in parsed) max_y_len = max(len(y) for y in parsed) # shrink ys to to limit batch computation parsed_y_len = min(min_y_len + 6, max_y_len) parsed = [y[:parsed_y_len] for y in parsed] ys = torch.LongTensor(batchsize, parsed_y_len).fill_(0) for i, y in enumerate(parsed): for j, idx in enumerate(y): ys[i][j] = idx if self.use_cuda: # copy to gpu self.ys.resize_(ys.size()) self.ys.copy_(ys, async=True) ys = Variable(self.ys) else: ys = Variable(ys) # set up candidates cands = None valid_cands = None if ys is None and self.rank: # only do ranking when no targets available and ranking flag set parsed = [] valid_cands = [] for i in valid_inds: if 'label_candidates' in observations[i]: # each candidate tuple is a pair of the parsed version and # the original full string cs = list(observations[i]['label_candidates']) parsed.append([self.parse(c) for c in cs]) valid_cands.append((i, cs)) if len(parsed) > 0: # TODO: store lengths of cands separately, so don't have zero # padding for varying number of cands per example # found cands, pack them into tensor max_c_len = max(max(len(c) for c in cs) for cs in parsed) max_c_cnt = max(len(cs) for cs in parsed) cands = torch.LongTensor(len(parsed), max_c_cnt, max_c_len).fill_(0) for i, cs in enumerate(parsed): for j, c in enumerate(cs): for k, idx in enumerate(c): cands[i][j][k] = idx if self.use_cuda: # copy to gpu self.cands.resize_(cands.size()) self.cands.copy_(cands, async=True) cands = Variable(self.cands) else: cands = Variable(cands) return xs, ys, valid_inds, cands, valid_cands def batch_act(self, observations): batchsize = len(observations) # initialize a table of replies with this agent's id batch_reply = [{'id': self.getID()} for _ in range(batchsize)] # convert the observations into batches of inputs and targets # valid_inds tells us the indices of all valid examples # e.g. for input [{}, {'text': 'hello'}, {}, {}], valid_inds is [1] # since the other three elements had no 'text' field xs, ys, valid_inds, cands, valid_cands = self.batchify(observations) if xs is None: # no valid examples, just return the empty responses we set up return batch_reply # produce predictions either way, but use the targets if available predictions, text_cand_inds = self.predict(xs, ys, cands) for i in range(len(predictions)): # map the predictions back to non-empty examples in the batch # we join with spaces since we produce tokens one at a time curr = batch_reply[valid_inds[i]] curr['text'] = ' '.join(c for c in predictions[i] if c != self.END and c != self.dict.null_token) if text_cand_inds is not None: for i in range(len(valid_cands)): order = text_cand_inds[i] batch_idx, curr_cands = valid_cands[i] curr = batch_reply[batch_idx] curr['text_candidates'] = [curr_cands[idx] for idx in order if idx < len(curr_cands)] return batch_reply def act(self): # call batch_act with this batch of one return self.batch_act([self.observation])[0] def save(self, path=None): path = self.opt.get('model_file', None) if path is None else path if path and hasattr(self, 'lt'): model = {} model['lt'] = self.lt.state_dict() model['encoder'] = self.encoder.state_dict() model['decoder'] = self.decoder.state_dict() model['h2o'] = self.h2o.state_dict() model['longest_label'] = self.longest_label model['opt'] = self.opt with open(path, 'wb') as write: torch.save(model, write) def shutdown(self): """Save the state of the model when shutdown.""" path = self.opt.get('model_file', None) if path is not None: self.save(path + '.shutdown_state') super().shutdown() def load(self, path): """Return opt and model states.""" with open(path, 'rb') as read: model = torch.load(read) return model['opt'], model def set_states(self, states): """Set the state dicts of the modules from saved states.""" #pdb.set_trace() self.lt.load_state_dict(states['lt']) self.encoder.load_state_dict(states['encoder']) self.decoder.load_state_dict(states['decoder']) self.h2o.load_state_dict(states['h2o']) self.longest_label = states['longest_label']
parlai/agents/seq2seq/seq2seq.py
22,182
Copyright (c) 2017-present, Facebook, Inc. All rights reserved. This source code is licensed under the BSD-style license found in the LICENSE file in the root directory of this source tree. An additional grant of patent rights can be found in the PATENTS file in the same directory. agent.add_argument('-att', '--attention', type='bool', default=False, help='whether to use attention over the context during decoding') agent.add_argument('-bi', '--bidirectional', type='bool', default=False, help='whether to encode the context with a bidirectional RNN') initialize defaults first this is not a shared instance of this class, so do full initialization. if shared is set, only set up shared members. check for cuda load model parameters if available override options with stored ones we use START markers to start our output we use END markers to end our output get index of null token from dictionary (probably 0) logFileself.logFile = opt['logFile'] store important params directly set up tensors set up modules lookup table stores word embeddings FILL HERE encoder captures the input text FILL HERE decoder produces our output states FILL HERE linear layer helps us produce outputs from final decoder state FILL HERE droput on the linear layer helps us generalize softmax maps output scores to probabilities set up optims for each module set loaded states if applicable skip non-model args FILL HERE FILL HERE FILL HERE shallow copy observation (deep copy can be expensive) if the last example wasn't the end of an episode, then we need to recall what was said in that example first encode contextxes = self.lt(xs).t() Ken next we use END as an input to kick off our decoder list of output tokens for each example in the batch update the model based on the labels keep track of longest label we've ever seen use the true token as the next input instead of predicted this produces a biased prediction but better training convert the output scores to tokenstoken = self.v2t([preds.data[b]]) Kenpdb.set_trace() sometimes output a prediction for debugging just produce a prediction without training the model score each candidate separately cands are exs_with_cands x cands_per_ex x words_per_cand cview is total_cands x words_per_cand set empty scores to -1, so when divided by 0 they become -inf average the scores per token now, generate a response from scratch keep producing tokens until we hit END or max length for each example in the batchpdb.set_trace()xes = self.lt(preds.t()) original KB-KAIST only add more tokens for examples that aren't done yetpdb.set_trace()token = self.v2t(preds.data[b]) KB-KAIST if we produced END, we're done sometimes output a prediction for debugging valid examples the indices of the valid (non-empty) tensors set up the input tensors tokenize the text shrink xs to to limit batch computation pack the data to the right side of the tensor for this model copy to gpu set up the target tensors randomly select one of the labels to update on, if multiple append END to each label shrink ys to to limit batch computation copy to gpu set up candidates only do ranking when no targets available and ranking flag set each candidate tuple is a pair of the parsed version and the original full string TODO: store lengths of cands separately, so don't have zero padding for varying number of cands per example found cands, pack them into tensor copy to gpu initialize a table of replies with this agent's id convert the observations into batches of inputs and targets valid_inds tells us the indices of all valid examples e.g. for input [{}, {'text': 'hello'}, {}, {}], valid_inds is [1] since the other three elements had no 'text' field no valid examples, just return the empty responses we set up produce predictions either way, but use the targets if available map the predictions back to non-empty examples in the batch we join with spaces since we produce tokens one at a time call batch_act with this batch of onepdb.set_trace()
3,973
en
0.748747
# Copyright 2018 The TensorFlow Authors. 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. # ============================================================================== """Utilities for cross_device_ops.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections as pycoll import threading from tensorflow.python.distribute import all_reduce from tensorflow.python.distribute import values as value_lib from tensorflow.python.framework import device as pydev from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops from tensorflow.python.ops import array_ops from tensorflow.python.ops import collective_ops from tensorflow.python.ops import gradients_impl from tensorflow.python.ops import math_ops from tensorflow.python.ops import nccl_ops def aggregate_gradients_using_nccl(replica_grads): """Aggregate gradients using nccl allreduce.""" agg_all_g_and_v = [] for single_g_and_v in zip(*replica_grads): single_grads = [g for g, _ in single_g_and_v] agg_grads = nccl_ops.all_sum(single_grads) agg_all_g_and_v.append( [(g, v) for g, (_, v) in zip(agg_grads, single_g_and_v)]) agg_all_g_and_v = list(zip(*agg_all_g_and_v)) return agg_all_g_and_v def aggregate_gradients_using_hierarchical_copy(avail_devices, replica_grads): """Aggregate gradients using hierarchical copies. Args: avail_devices: available GPU devices. replica_grads: List of lists of (gradient, variable) tuples. The outer list is over replicas. The inner list is over individual gradients. Returns: The list of (aggregated_gradient, variable), where the gradient has been summed across all replicas and the variable is chosen from the first replica. """ # This only works for DGX-1 type of machine topology # Device peer to peer matrix # DMA: 0 1 2 3 4 5 6 7 # 0: Y Y Y Y Y N N N # 1: Y Y Y Y N Y N N # 2: Y Y Y Y N N Y N # 3: Y Y Y Y N N N Y # 4: Y N N N Y Y Y Y # 5: N Y N N Y Y Y Y # 6: N N Y N Y Y Y Y # 7: N N N Y Y Y Y Y agg_grads = [] num_devices = len(avail_devices) # In the special case of DGX-1 machine topology, the two groups have equal # size. group_size = num_devices // 2 for i, single_grads in enumerate(zip(*replica_grads)): group_0_main_device = i % num_devices group_1_main_device = (group_0_main_device + group_size) % num_devices if group_0_main_device < group_size: group_0_begin = 0 group_1_begin = group_size else: group_0_begin = group_size group_1_begin = 0 # Aggregate the first group. group_0_device_grads = single_grads[group_0_begin: group_0_begin + group_size] with ops.device(avail_devices[group_0_main_device]): group_0_agg_grads, _ = aggregate_single_gradient_using_copy( group_0_device_grads, False, False) # Aggregate the second group. group_1_device_grads = single_grads[group_1_begin: group_1_begin + group_size] with ops.device(avail_devices[group_1_main_device]): group_1_agg_grads, _ = aggregate_single_gradient_using_copy( group_1_device_grads, False, False) # Aggregate between the groups. with ops.device(avail_devices[group_0_main_device]): (agg_total_grads, _), _ = aggregate_single_gradient_using_copy( [group_0_agg_grads, group_1_agg_grads], False, False) # Broadcast the result back into the root of each group. with ops.device(avail_devices[group_0_main_device]): group_0_agg_grads_bcast = array_ops.identity(agg_total_grads) with ops.device(avail_devices[group_1_main_device]): group_1_agg_grads_bcast = array_ops.identity(agg_total_grads) agg_grads_bcast = [] for j in range(len(single_grads)): with ops.device(avail_devices[j]): # Broadcast the result back to each member in the group from the root. if (group_0_main_device < group_size) == (j < group_size): src_device_grad = group_0_agg_grads_bcast else: src_device_grad = group_1_agg_grads_bcast agg_grads_bcast.append(array_ops.identity(src_device_grad)) agg_grads.append( [(g, v) for g, (_, v) in zip(agg_grads_bcast, single_grads)]) agg_grads = list(zip(*agg_grads)) return agg_grads def aggregate_single_gradient_using_copy(grad_and_vars, use_mean, check_inf_nan): """Calculate the average gradient for a shared variable across all replicas. Note that this function provides a synchronization point across all replicas. Args: grad_and_vars: A list or tuple of (gradient, variable) tuples. Each (gradient, variable) pair within the outer list represents the gradient of the variable calculated for a single replica, and the number of pairs equals the number of replicas. use_mean: if True, mean is taken, else sum of gradients is taken. check_inf_nan: check grads for nans and infs. Returns: The tuple ([(average_gradient, variable),], has_nan_or_inf) where the gradient has been averaged across all replicas. The variable is chosen from the first replica. The has_nan_or_inf indicates the grads has nan or inf. """ grads = [g for g, _ in grad_and_vars] grad = math_ops.add_n(grads) if use_mean and len(grads) > 1: grad = array_ops.multiply(grad, 1.0 / len(grads)) v = grad_and_vars[0][1] if check_inf_nan: has_nan_or_inf = array_ops.logical_not( array_ops.reduce_all(array_ops.is_finite(grads))) return (grad, v), has_nan_or_inf else: return (grad, v), None def group_device_names(devices, group_size): """Group device names into groups of group_size. Args: devices: a list of canonical device strings. group_size: integer which is equal to or greater than 1. Returns: list of lists of devices, where each inner list is group_size long, and each device appears at least once in an inner list. If len(devices) % group_size == 0 then each device will appear exactly once. Raises: ValueError: if group_size > len(devices) """ num_devices = len(devices) if group_size > num_devices: raise ValueError( 'only %d devices, but group_size=%d' % (num_devices, group_size)) num_groups = ( num_devices // group_size + (1 if (num_devices % group_size != 0) else 0)) groups = [[] for i in range(num_groups)] for i in range(num_groups * group_size): groups[i % num_groups].append(devices[i % num_devices]) return groups def split_grads_by_size(threshold_size, device_grads): """Break gradients into two sets according to tensor size. Args: threshold_size: int size cutoff for small vs large tensor. device_grads: List of lists of (gradient, variable) tuples. The outer list is over devices. The inner list is over individual gradients. Returns: small_grads: Subset of device_grads where shape is <= threshold_size elements. large_grads: Subset of device_grads where shape is > threshold_size elements. """ small_grads = [] large_grads = [] for dl in device_grads: small_dl = [] large_dl = [] for (g, v) in dl: tensor_size = g.get_shape().num_elements() if tensor_size <= threshold_size: small_dl.append([g, v]) else: large_dl.append([g, v]) if small_dl: small_grads.append(small_dl) if large_dl: large_grads.append(large_dl) return small_grads, large_grads # threading.Lock() and threading.local() cannot be pickled and therefore cannot # be a field of CollectiveKeys. Right now _thread_local is not necessary to be # an instance member of CollectiveKeys since we always create a new thread for # each replica. _lock = threading.Lock() _thread_local = threading.local() # TODO(yuefengz): use random key starts to avoid reusing keys? class CollectiveKeys(object): """Class that manages collective keys. We need to manage three different keys for collective: *Group key*: an integer key to identify the set of cooperative devices. Collective ops work under the same set of devices must using the same group key. *Instance key*: an integer key to identify the set of same counterpart of tensors on different devices in a device group that need to be all-reduced. "Graph key": an integer key that is unique key graph. This is used to support multiple graphs per client session. It must be non-zero and set in the `config` argument of each call to `session.run`. """ def __init__(self, group_key_start=1, instance_key_start=100, instance_key_with_id_start=10000): """Initializes the object. Args: group_key_start: the starting integer of group key. instance_key_start: the starting integer of instance key. instance_key_with_id_start: the starting integer of instance key that is recorded with an id. """ self._group_key = group_key_start self._group_key_table = dict() # For instance keys with ids self._instance_key_id_to_key_table = dict() self._instance_key_with_id_counter = instance_key_with_id_start # For instance keys without ids self._instance_key_start = instance_key_start def _get_thread_local_object(self): # We make instance key without key ids thread local so that it will work # with MirroredStrategy and distribute coordinator. if not hasattr(_thread_local, 'instance_key'): _thread_local.instance_key = self._instance_key_start return _thread_local def get_group_key(self, devices): """Returns a group key for the set of devices. Args: devices: list of strings naming devices in a collective group. Returns: int key uniquely identifying the set of device names. """ parsed = [pydev.DeviceSpec.from_string(d) for d in devices] # In the between-graph replicated training, different workers need to get # the same device key. So we remove the task_type and task_id from the # devices. # TODO(yuefengz): in the in-graph replicated training, we need to include # task_type and task_id. names = sorted(['%s:%d' % (d.device_type, d.device_index) for d in parsed]) key_id = ','.join(names) with _lock: if key_id not in self._group_key_table: new_key = self._group_key self._group_key += 1 self._group_key_table[key_id] = new_key return self._group_key_table[key_id] def get_instance_key(self, key_id=None): """Returns a new instance key for use in defining a collective op. Args: key_id: optional string. If set, key will be recorded and the same key will be returned when the same key_id is provided. If not, an increasing instance key will be returned. """ if key_id: with _lock: if key_id not in self._instance_key_id_to_key_table: self._instance_key_with_id_counter += 1 self._instance_key_id_to_key_table[key_id] = ( self._instance_key_with_id_counter) return self._instance_key_id_to_key_table[key_id] else: v = self._get_thread_local_object().instance_key self._get_thread_local_object().instance_key += 1 return v def build_collective_reduce(input_tensors, num_workers, collective_keys, reduction_op='Add', unary_op='Id'): """Build a subgraph that does one full all-reduce, using the collective Op. Args: input_tensors: tensors within a single worker graph that are to be reduced together; must be one per device. num_workers: total number of workers with identical independent graphs that will be doing this same reduction. The reduction will actually include the corresponding tensors at all these workers. collective_keys: a CollectiveKeys object. reduction_op: string naming the reduction op. unary_op: string naming the unary final op. Returns: An array of final tensors, one per device, computed by the full reduction. Raises: ValueError: There must be at least two tensors over all the workers. """ group_size = len(input_tensors) * num_workers if group_size < 2: raise ValueError('num_workers * len(input_tensors) must be 2 or greater') devices = [t.device for t in input_tensors] num_devices = len(devices) group_key = collective_keys.get_group_key(devices) instance_key = collective_keys.get_instance_key() out_tensors = [] subdiv_offsets = [0] # TODO(tucker): maybe support non-default subdiv spec for d in range(num_devices): with ops.device(devices[d]): reduce_op = collective_ops.all_reduce( input_tensors[d], group_size, group_key, instance_key, reduction_op, unary_op, subdiv_offsets) out_tensors.append(reduce_op) return out_tensors def sum_grad_and_var_all_reduce(grad_and_vars, num_workers, alg, gpu_indices, aux_devices=None, num_shards=1): """Apply all-reduce algorithm over specified gradient tensors.""" with ops.name_scope('allreduce'): # Note that each grad_and_vars looks like the following: # ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN)) scaled_grads = [g for g, _ in grad_and_vars] if alg == 'nccl': summed_grads = nccl_ops.all_sum(scaled_grads) elif alg == 'xring': summed_grads = all_reduce.build_ring_all_reduce( scaled_grads, num_workers, num_shards, gpu_indices, math_ops.add) elif alg == 'nccl/xring': summed_grads = all_reduce.build_nccl_then_ring(scaled_grads, num_shards, math_ops.add) elif alg == 'nccl/rechd': summed_grads = all_reduce.build_nccl_then_recursive_hd( scaled_grads, math_ops.add) elif alg == 'nccl/pscpu': summed_grads = all_reduce.build_nccl_then_shuffle( scaled_grads, aux_devices, math_ops.add, math_ops.add_n) elif alg == 'pscpu/pscpu': second_gather_devices = aux_devices[:num_shards] summed_grads = all_reduce.build_shuffle_then_shuffle( scaled_grads, aux_devices, second_gather_devices, math_ops.add_n) elif alg in ['pscpu', 'psgpu']: summed_grads = all_reduce.build_shuffle_all_reduce( scaled_grads, aux_devices, math_ops.add_n) else: raise ValueError('unsupported all_reduce alg: ', alg) result = [] for (_, v), g in zip(grad_and_vars, summed_grads): result.append([g, v]) return result def sum_gradients_all_reduce(dev_prefixes, replica_grads, num_workers, alg, num_shards, gpu_indices): """Apply all-reduce algorithm over specified gradient tensors. Args: dev_prefixes: list of prefix strings to use to generate PS device names. replica_grads: the gradients to reduce. num_workers: number of worker processes across entire job. alg: the all-reduce algorithm to apply. num_shards: alg-specific sharding factor. gpu_indices: indices of local GPUs in order usable for ring-reduce. Returns: list of reduced tensors """ alg_contains_shuffle = any([n in alg for n in ['pscpu', 'psgpu']]) is_hierarchical = '/' in alg if 'pscpu' in alg: aux_devices = [prefix + '/cpu:0' for prefix in dev_prefixes] elif 'psgpu' in alg: aux_devices = [ prefix + '/gpu:%d' % i for i in range(len(gpu_indices)) for prefix in dev_prefixes ] else: aux_devices = ['/job:localhost/cpu:0'] # Auxiliary devices for hierarchical all-reduces. aux_device_groups = group_device_names( aux_devices, num_shards if alg_contains_shuffle else 1) group_index = 0 reduced_gv_list = [] for grad_and_vars in zip(*replica_grads): reduced_gv_list.append( sum_grad_and_var_all_reduce( grad_and_vars, num_workers, alg, gpu_indices, aux_devices if is_hierarchical else aux_device_groups[group_index], num_shards)) group_index = (group_index + 1) % len(aux_device_groups) new_replica_grads = [list(x) for x in zip(*reduced_gv_list)] return new_replica_grads def extract_ranges(index_list, range_size_limit=32): """Extract consecutive ranges and singles from index_list. Args: index_list: List of monotone increasing non-negative integers. range_size_limit: Largest size range to return. If a larger consecutive range exists, it will be returned as multiple ranges. Returns: (ranges, singles) where ranges is a list of [first, last] pairs of consecutive elements in index_list, and singles is all of the other elements, in original order. """ if not index_list: return [], [] first = index_list[0] last = first ranges = [] singles = [] for i in index_list[1:]: if i == last + 1 and (last - first) <= range_size_limit: last = i else: if last > first: ranges.append([first, last]) else: singles.append(first) first = i last = i if last > first: ranges.append([first, last]) else: singles.append(first) return ranges, singles GradPackTuple = pycoll.namedtuple('GradPackTuple', 'indices vars shapes') def pack_range(key, packing, grad_vars, rng): """Form the concatenation of a specified range of gradient tensors. Args: key: Value under which to store meta-data in packing that will be used later to restore the grad_var list structure. packing: Dict holding data describing packed ranges of small tensors. grad_vars: List of (grad, var) pairs for one replica. rng: A pair of integers giving the first, last indices of a consecutive range of tensors to be packed. Returns: A tensor that is the concatenation of all the specified small tensors. """ to_pack = grad_vars[rng[0]:rng[1] + 1] members = [] variables = [] restore_shapes = [] with ops.name_scope('pack'): for g, v in to_pack: variables.append(v) restore_shapes.append(g.shape) with ops.device(g.device): members.append(array_ops.reshape(g, [-1])) packing[key] = GradPackTuple( indices=range(rng[0], rng[1] + 1), vars=variables, shapes=restore_shapes) with ops.device(members[0].device): return array_ops.concat(members, 0) def unpack_grad_tuple(gv, gpt): """Unpack a previously packed collection of gradient tensors. Args: gv: A (grad, var) pair to be unpacked. gpt: A GradPackTuple describing the packing operation that produced gv. Returns: A list of (grad, var) pairs corresponding to the values that were originally packed into gv, maybe following subsequent operations like reduction. """ elt_widths = [x.num_elements() for x in gpt.shapes] with ops.device(gv[0][0].device): with ops.name_scope('unpack'): splits = array_ops.split(gv[0], elt_widths) unpacked_gv = [] for idx, s in enumerate(splits): unpacked_gv.append((array_ops.reshape(s, gpt.shapes[idx]), gpt.vars[idx])) return unpacked_gv def pack_small_tensors(replica_grads, max_bytes=0, max_group=0): """Concatenate small gradient tensors together for reduction. Args: replica_grads: List of lists of (gradient, variable) tuples. max_bytes: Int giving max number of bytes in a tensor that may be considered small. max_group: Int giving max number of small tensors that may be concatenated into one new tensor. Returns: new_replica_grads, packing where new_replica_grads is identical to replica_grads except that all feasible small_tensors have been removed from their places and concatenated into larger tensors that are now in the front of the list for each replica, and packing contains the data necessary to restore the replica_grads structure. Look through the first replica for gradients of the same type (float), and small size, that are all sequential. For each such group, replace by a new tensor that is a flattened concatenation. Note that the corresponding variable will be absent, which doesn't matter because it isn't used during all-reduce. Requires: Every gv_list in replicas must have isomorphic structure including identical tensor sizes and types. """ small_indices = [] large_indices = [] for idx, (g, _) in enumerate(replica_grads[0]): if g.dtype == dtypes.float32 and (4 * g.shape.num_elements()) <= max_bytes: small_indices.append(idx) else: large_indices.append(idx) small_ranges, small_singles = extract_ranges( small_indices, range_size_limit=max_group) large_indices = sorted(large_indices + small_singles) num_gv = len(replica_grads[0]) packing = {} if small_ranges: new_replica_grads = [] for dev_idx, gv_list in enumerate(replica_grads): assert len(gv_list) == num_gv new_gv_list = [] for r in small_ranges: key = '%d:%d' % (dev_idx, len(new_gv_list)) new_gv_list.append((pack_range(key, packing, gv_list, r), 'packing_var_placeholder')) for i in large_indices: new_gv_list.append(gv_list[i]) new_replica_grads.append(new_gv_list) return new_replica_grads, packing else: return replica_grads, None def unpack_small_tensors(replica_grads, packing): """Undo the structure alterations to replica_grads done by pack_small_tensors. Args: replica_grads: List of List of (grad, var) tuples. packing: A dict generated by pack_small_tensors describing the changes it made to replica_grads. Returns: new_replica_grads: identical to replica_grads except that concatenations of small tensors have been split apart and returned to their original positions, paired with their original variables. """ if not packing: return replica_grads new_replica_grads = [] num_devices = len(replica_grads) num_packed = len(packing.keys()) // num_devices for dev_idx, gv_list in enumerate(replica_grads): gv_list = list(gv_list) new_gv_list = gv_list[num_packed:] for i in range(num_packed): k = '%d:%d' % (dev_idx, i) gpt = packing[k] gv = unpack_grad_tuple(gv_list[i], gpt) for gi, idx in enumerate(gpt.indices): assert idx == gpt.indices[gi] new_gv_list.insert(idx, gv[gi]) new_replica_grads.append(new_gv_list) return new_replica_grads def aggregate_tensors_or_indexed_slices(values, accumulation_fn=math_ops.add_n): """Aggregate tensors using `accumulation_fn` and IndexedSlices via concat.""" if any(isinstance(v, ops.IndexedSlices) for v in values): return gradients_impl._AggregateIndexedSlicesGradients(values) # pylint: disable=protected-access else: return accumulation_fn(values) def divide_by_n_tensors_or_indexed_slices(value, n): if isinstance(value, ops.IndexedSlices): value = gradients_impl._HandleNestedIndexedSlices(value) # pylint: disable=protected-access return ops.IndexedSlices( value.values / n, value.indices, value.dense_shape) else: return value / n def copy_tensor_or_indexed_slices_to_device(value, device): with ops.device(device): if isinstance(value, ops.IndexedSlices): copied_values = array_ops.identity(value.values) copied_indices = array_ops.identity(value.indices) copied_shape = array_ops.identity(value.dense_shape) result = ops.IndexedSlices(copied_values, copied_indices, copied_shape) else: result = array_ops.identity(value) return result def contains_indexed_slices(value): """Check whether the value is `IndexedSlices` or contains `IndexedSlices`.""" if isinstance(value, ops.IndexedSlices): return True elif isinstance(value, (list, tuple)) and value: return any(contains_indexed_slices(v) for v in value) elif isinstance(value, value_lib.DistributedValues): return contains_indexed_slices(list(value._index.values())) # pylint: disable=protected-access else: return False
tensorflow/python/distribute/cross_device_utils.py
24,867
Class that manages collective keys. We need to manage three different keys for collective: *Group key*: an integer key to identify the set of cooperative devices. Collective ops work under the same set of devices must using the same group key. *Instance key*: an integer key to identify the set of same counterpart of tensors on different devices in a device group that need to be all-reduced. "Graph key": an integer key that is unique key graph. This is used to support multiple graphs per client session. It must be non-zero and set in the `config` argument of each call to `session.run`. Initializes the object. Args: group_key_start: the starting integer of group key. instance_key_start: the starting integer of instance key. instance_key_with_id_start: the starting integer of instance key that is recorded with an id. Aggregate gradients using hierarchical copies. Args: avail_devices: available GPU devices. replica_grads: List of lists of (gradient, variable) tuples. The outer list is over replicas. The inner list is over individual gradients. Returns: The list of (aggregated_gradient, variable), where the gradient has been summed across all replicas and the variable is chosen from the first replica. Aggregate gradients using nccl allreduce. Calculate the average gradient for a shared variable across all replicas. Note that this function provides a synchronization point across all replicas. Args: grad_and_vars: A list or tuple of (gradient, variable) tuples. Each (gradient, variable) pair within the outer list represents the gradient of the variable calculated for a single replica, and the number of pairs equals the number of replicas. use_mean: if True, mean is taken, else sum of gradients is taken. check_inf_nan: check grads for nans and infs. Returns: The tuple ([(average_gradient, variable),], has_nan_or_inf) where the gradient has been averaged across all replicas. The variable is chosen from the first replica. The has_nan_or_inf indicates the grads has nan or inf. Aggregate tensors using `accumulation_fn` and IndexedSlices via concat. Build a subgraph that does one full all-reduce, using the collective Op. Args: input_tensors: tensors within a single worker graph that are to be reduced together; must be one per device. num_workers: total number of workers with identical independent graphs that will be doing this same reduction. The reduction will actually include the corresponding tensors at all these workers. collective_keys: a CollectiveKeys object. reduction_op: string naming the reduction op. unary_op: string naming the unary final op. Returns: An array of final tensors, one per device, computed by the full reduction. Raises: ValueError: There must be at least two tensors over all the workers. Check whether the value is `IndexedSlices` or contains `IndexedSlices`. Extract consecutive ranges and singles from index_list. Args: index_list: List of monotone increasing non-negative integers. range_size_limit: Largest size range to return. If a larger consecutive range exists, it will be returned as multiple ranges. Returns: (ranges, singles) where ranges is a list of [first, last] pairs of consecutive elements in index_list, and singles is all of the other elements, in original order. Returns a group key for the set of devices. Args: devices: list of strings naming devices in a collective group. Returns: int key uniquely identifying the set of device names. Returns a new instance key for use in defining a collective op. Args: key_id: optional string. If set, key will be recorded and the same key will be returned when the same key_id is provided. If not, an increasing instance key will be returned. Group device names into groups of group_size. Args: devices: a list of canonical device strings. group_size: integer which is equal to or greater than 1. Returns: list of lists of devices, where each inner list is group_size long, and each device appears at least once in an inner list. If len(devices) % group_size == 0 then each device will appear exactly once. Raises: ValueError: if group_size > len(devices) Form the concatenation of a specified range of gradient tensors. Args: key: Value under which to store meta-data in packing that will be used later to restore the grad_var list structure. packing: Dict holding data describing packed ranges of small tensors. grad_vars: List of (grad, var) pairs for one replica. rng: A pair of integers giving the first, last indices of a consecutive range of tensors to be packed. Returns: A tensor that is the concatenation of all the specified small tensors. Concatenate small gradient tensors together for reduction. Args: replica_grads: List of lists of (gradient, variable) tuples. max_bytes: Int giving max number of bytes in a tensor that may be considered small. max_group: Int giving max number of small tensors that may be concatenated into one new tensor. Returns: new_replica_grads, packing where new_replica_grads is identical to replica_grads except that all feasible small_tensors have been removed from their places and concatenated into larger tensors that are now in the front of the list for each replica, and packing contains the data necessary to restore the replica_grads structure. Look through the first replica for gradients of the same type (float), and small size, that are all sequential. For each such group, replace by a new tensor that is a flattened concatenation. Note that the corresponding variable will be absent, which doesn't matter because it isn't used during all-reduce. Requires: Every gv_list in replicas must have isomorphic structure including identical tensor sizes and types. Break gradients into two sets according to tensor size. Args: threshold_size: int size cutoff for small vs large tensor. device_grads: List of lists of (gradient, variable) tuples. The outer list is over devices. The inner list is over individual gradients. Returns: small_grads: Subset of device_grads where shape is <= threshold_size elements. large_grads: Subset of device_grads where shape is > threshold_size elements. Apply all-reduce algorithm over specified gradient tensors. Apply all-reduce algorithm over specified gradient tensors. Args: dev_prefixes: list of prefix strings to use to generate PS device names. replica_grads: the gradients to reduce. num_workers: number of worker processes across entire job. alg: the all-reduce algorithm to apply. num_shards: alg-specific sharding factor. gpu_indices: indices of local GPUs in order usable for ring-reduce. Returns: list of reduced tensors Unpack a previously packed collection of gradient tensors. Args: gv: A (grad, var) pair to be unpacked. gpt: A GradPackTuple describing the packing operation that produced gv. Returns: A list of (grad, var) pairs corresponding to the values that were originally packed into gv, maybe following subsequent operations like reduction. Undo the structure alterations to replica_grads done by pack_small_tensors. Args: replica_grads: List of List of (grad, var) tuples. packing: A dict generated by pack_small_tensors describing the changes it made to replica_grads. Returns: new_replica_grads: identical to replica_grads except that concatenations of small tensors have been split apart and returned to their original positions, paired with their original variables. Utilities for cross_device_ops. Copyright 2018 The TensorFlow Authors. 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. ============================================================================== This only works for DGX-1 type of machine topology Device peer to peer matrix DMA: 0 1 2 3 4 5 6 7 0: Y Y Y Y Y N N N 1: Y Y Y Y N Y N N 2: Y Y Y Y N N Y N 3: Y Y Y Y N N N Y 4: Y N N N Y Y Y Y 5: N Y N N Y Y Y Y 6: N N Y N Y Y Y Y 7: N N N Y Y Y Y Y In the special case of DGX-1 machine topology, the two groups have equal size. Aggregate the first group. Aggregate the second group. Aggregate between the groups. Broadcast the result back into the root of each group. Broadcast the result back to each member in the group from the root. threading.Lock() and threading.local() cannot be pickled and therefore cannot be a field of CollectiveKeys. Right now _thread_local is not necessary to be an instance member of CollectiveKeys since we always create a new thread for each replica. TODO(yuefengz): use random key starts to avoid reusing keys? For instance keys with ids For instance keys without ids We make instance key without key ids thread local so that it will work with MirroredStrategy and distribute coordinator. In the between-graph replicated training, different workers need to get the same device key. So we remove the task_type and task_id from the devices. TODO(yuefengz): in the in-graph replicated training, we need to include task_type and task_id. TODO(tucker): maybe support non-default subdiv spec Note that each grad_and_vars looks like the following: ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN)) Auxiliary devices for hierarchical all-reduces. pylint: disable=protected-access pylint: disable=protected-access pylint: disable=protected-access
9,899
en
0.836441
from time import sleep from ec2mc import __main__ def test_user_commands(): """test all user commands.""" assert __main__.main([ "user", "create", "ec2mc_test_user", "setup_users", "--default" ]) is not False sleep(5) assert __main__.main([ "user", "list" ]) is not False assert __main__.main([ "user", "set_group", "EC2MC_TEST_USER", "basic_users" ]) is not False assert __main__.main([ "user", "be", "takingitcasual" ]) is not False assert __main__.main([ "user", "rotate_key", "Ec2Mc_TeSt_UsEr" ]) is not False assert __main__.main([ "user", "delete", "eC2mC_tEsT_uSeR" ]) is not False
tests/test_user_commands.py
695
test all user commands.
23
en
0.946798
import numpy as np import tensorlayerx as tlx import gammagl.mpops as mpops from .num_nodes import maybe_num_nodes from .check import check_is_numpy def coalesce(edge_index, edge_attr=None, num_nodes=None, reduce="add", is_sorted=False, sort_by_row=True): """Row-wise sorts :obj:`edge_index` and removes its duplicated entries. Duplicate entries in :obj:`edge_attr` are merged by scattering them together according to the given :obj:`reduce` option. Args: edge_index (LongTensor): The edge indices. edge_attr (Tensor or List[Tensor], optional): Edge weights or multi- dimensional edge features. If given as a list, will re-shuffle and remove duplicates for all its entries. (default: :obj:`None`) num_nodes (int, optional): The number of nodes, *i.e.* :obj:`max_val + 1` of :attr:`edge_index`. (default: :obj:`None`) reduce (string, optional): The reduce operation to use for merging edge features (:obj:`"add"`, :obj:`"mean"`, :obj:`"min"`, :obj:`"max"`, :obj:`"mul"`). (default: :obj:`"add"`) is_sorted (bool, optional): If set to :obj:`True`, will expect :obj:`edge_index` to be already sorted row-wise. sort_by_row (bool, optional): If set to :obj:`False`, will sort :obj:`edge_index` column-wise. :rtype: :class:`LongTensor` if :attr:`edge_attr` is :obj:`None`, else (:class:`LongTensor`, :obj:`Tensor` or :obj:`List[Tensor]]`) """ if tlx.is_tensor(edge_index): edge_index = tlx.convert_to_numpy(edge_index) nnz = edge_index.shape[1] num_nodes = maybe_num_nodes(edge_index, num_nodes) idx = np.zeros(nnz+1) idx[0] = -1 idx[1:] = edge_index[1 - int(sort_by_row)] idx[1:] = (np.add(np.multiply(idx[1:], num_nodes), edge_index[int(sort_by_row)])) if not is_sorted: perm = np.argsort(idx[1:]) idx[1:] = np.sort(idx[1:]) edge_index = edge_index[:, perm] if edge_attr is not None and tlx.ops.is_tensor(edge_attr): edge_attr = tlx.gather(edge_attr, tlx.convert_to_tensor(perm), axis=0) elif edge_attr is not None and check_is_numpy(edge_attr): edge_attr = edge_attr[perm] elif edge_attr is not None: # edge_attr is List. edge_attr = [tlx.gather(e, perm, axis=0) for e in edge_attr] mask = idx[1:] > idx[:-1] # Only perform expensive merging in case there exists duplicates: if mask.all(): edge_index = tlx.convert_to_tensor(edge_index, dtype=tlx.int64) return edge_index if edge_attr is None else (edge_index, edge_attr) edge_index = edge_index[:, mask] edge_index = tlx.convert_to_tensor(edge_index, dtype=tlx.int64) if edge_attr is None: return edge_index idx = np.arange(0, nnz) idx = tlx.convert_to_tensor(idx - (1 - mask).cumsum(axis=0)) if tlx.ops.is_tensor(edge_attr): edge_attr = mpops.segment_sum(edge_attr, idx) return edge_index, edge_attr
gammagl/utils/coalesce.py
3,032
Row-wise sorts :obj:`edge_index` and removes its duplicated entries. Duplicate entries in :obj:`edge_attr` are merged by scattering them together according to the given :obj:`reduce` option. Args: edge_index (LongTensor): The edge indices. edge_attr (Tensor or List[Tensor], optional): Edge weights or multi- dimensional edge features. If given as a list, will re-shuffle and remove duplicates for all its entries. (default: :obj:`None`) num_nodes (int, optional): The number of nodes, *i.e.* :obj:`max_val + 1` of :attr:`edge_index`. (default: :obj:`None`) reduce (string, optional): The reduce operation to use for merging edge features (:obj:`"add"`, :obj:`"mean"`, :obj:`"min"`, :obj:`"max"`, :obj:`"mul"`). (default: :obj:`"add"`) is_sorted (bool, optional): If set to :obj:`True`, will expect :obj:`edge_index` to be already sorted row-wise. sort_by_row (bool, optional): If set to :obj:`False`, will sort :obj:`edge_index` column-wise. :rtype: :class:`LongTensor` if :attr:`edge_attr` is :obj:`None`, else (:class:`LongTensor`, :obj:`Tensor` or :obj:`List[Tensor]]`) edge_attr is List. Only perform expensive merging in case there exists duplicates:
1,252
en
0.610232
# 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 mox3 import mox from nova.compute import power_state from nova.compute import utils as compute_utils from nova.conductor.tasks import live_migrate from nova import db from nova import exception from nova import objects from nova.scheduler import utils as scheduler_utils from nova import test from nova.tests.unit import fake_instance class LiveMigrationTaskTestCase(test.NoDBTestCase): def setUp(self): super(LiveMigrationTaskTestCase, self).setUp() self.context = "context" self.instance_host = "host" self.instance_uuid = "uuid" self.instance_image = "image_ref" db_instance = fake_instance.fake_db_instance( host=self.instance_host, uuid=self.instance_uuid, power_state=power_state.RUNNING, memory_mb=512, image_ref=self.instance_image) self.instance = objects.Instance._from_db_object( self.context, objects.Instance(), db_instance) self.destination = "destination" self.block_migration = "bm" self.disk_over_commit = "doc" self._generate_task() def _generate_task(self): self.task = live_migrate.LiveMigrationTask(self.context, self.instance, self.destination, self.block_migration, self.disk_over_commit) def test_execute_with_destination(self): self.mox.StubOutWithMock(self.task, '_check_host_is_up') self.mox.StubOutWithMock(self.task, '_check_requested_destination') self.mox.StubOutWithMock(self.task.compute_rpcapi, 'live_migration') self.task._check_host_is_up(self.instance_host) self.task._check_requested_destination() self.task.compute_rpcapi.live_migration(self.context, host=self.instance_host, instance=self.instance, dest=self.destination, block_migration=self.block_migration, migrate_data=None).AndReturn("bob") self.mox.ReplayAll() self.assertEqual("bob", self.task.execute()) def test_execute_without_destination(self): self.destination = None self._generate_task() self.assertIsNone(self.task.destination) self.mox.StubOutWithMock(self.task, '_check_host_is_up') self.mox.StubOutWithMock(self.task, '_find_destination') self.mox.StubOutWithMock(self.task.compute_rpcapi, 'live_migration') self.task._check_host_is_up(self.instance_host) self.task._find_destination().AndReturn("found_host") self.task.compute_rpcapi.live_migration(self.context, host=self.instance_host, instance=self.instance, dest="found_host", block_migration=self.block_migration, migrate_data=None).AndReturn("bob") self.mox.ReplayAll() self.assertEqual("bob", self.task.execute()) def test_check_instance_is_running_passes(self): self.task._check_instance_is_running() def test_check_instance_is_running_fails_when_shutdown(self): self.task.instance['power_state'] = power_state.SHUTDOWN self.assertRaises(exception.InstanceNotRunning, self.task._check_instance_is_running) def test_check_instance_host_is_up(self): self.mox.StubOutWithMock(db, 'service_get_by_compute_host') self.mox.StubOutWithMock(self.task.servicegroup_api, 'service_is_up') db.service_get_by_compute_host(self.context, "host").AndReturn("service") self.task.servicegroup_api.service_is_up("service").AndReturn(True) self.mox.ReplayAll() self.task._check_host_is_up("host") def test_check_instance_host_is_up_fails_if_not_up(self): self.mox.StubOutWithMock(db, 'service_get_by_compute_host') self.mox.StubOutWithMock(self.task.servicegroup_api, 'service_is_up') db.service_get_by_compute_host(self.context, "host").AndReturn("service") self.task.servicegroup_api.service_is_up("service").AndReturn(False) self.mox.ReplayAll() self.assertRaises(exception.ComputeServiceUnavailable, self.task._check_host_is_up, "host") def test_check_instance_host_is_up_fails_if_not_found(self): self.mox.StubOutWithMock(db, 'service_get_by_compute_host') db.service_get_by_compute_host(self.context, "host").AndRaise(exception.NotFound) self.mox.ReplayAll() self.assertRaises(exception.ComputeServiceUnavailable, self.task._check_host_is_up, "host") def test_check_requested_destination(self): self.mox.StubOutWithMock(db, 'service_get_by_compute_host') self.mox.StubOutWithMock(self.task, '_get_compute_info') self.mox.StubOutWithMock(self.task.servicegroup_api, 'service_is_up') self.mox.StubOutWithMock(self.task.compute_rpcapi, 'check_can_live_migrate_destination') db.service_get_by_compute_host(self.context, self.destination).AndReturn("service") self.task.servicegroup_api.service_is_up("service").AndReturn(True) hypervisor_details = { "hypervisor_type": "a", "hypervisor_version": 6.1, "free_ram_mb": 513 } self.task._get_compute_info(self.destination)\ .AndReturn(hypervisor_details) self.task._get_compute_info(self.instance_host)\ .AndReturn(hypervisor_details) self.task._get_compute_info(self.destination)\ .AndReturn(hypervisor_details) self.task.compute_rpcapi.check_can_live_migrate_destination( self.context, self.instance, self.destination, self.block_migration, self.disk_over_commit).AndReturn( "migrate_data") self.mox.ReplayAll() self.task._check_requested_destination() self.assertEqual("migrate_data", self.task.migrate_data) def test_check_requested_destination_fails_with_same_dest(self): self.task.destination = "same" self.task.source = "same" self.assertRaises(exception.UnableToMigrateToSelf, self.task._check_requested_destination) def test_check_requested_destination_fails_when_destination_is_up(self): self.mox.StubOutWithMock(db, 'service_get_by_compute_host') db.service_get_by_compute_host(self.context, self.destination).AndRaise(exception.NotFound) self.mox.ReplayAll() self.assertRaises(exception.ComputeServiceUnavailable, self.task._check_requested_destination) def test_check_requested_destination_fails_with_not_enough_memory(self): self.mox.StubOutWithMock(self.task, '_check_host_is_up') self.mox.StubOutWithMock(db, 'service_get_by_compute_host') self.task._check_host_is_up(self.destination) db.service_get_by_compute_host(self.context, self.destination).AndReturn({ "compute_node": [{"free_ram_mb": 511}] }) self.mox.ReplayAll() self.assertRaises(exception.MigrationPreCheckError, self.task._check_requested_destination) def test_check_requested_destination_fails_with_hypervisor_diff(self): self.mox.StubOutWithMock(self.task, '_check_host_is_up') self.mox.StubOutWithMock(self.task, '_check_destination_has_enough_memory') self.mox.StubOutWithMock(self.task, '_get_compute_info') self.task._check_host_is_up(self.destination) self.task._check_destination_has_enough_memory() self.task._get_compute_info(self.instance_host).AndReturn({ "hypervisor_type": "b" }) self.task._get_compute_info(self.destination).AndReturn({ "hypervisor_type": "a" }) self.mox.ReplayAll() self.assertRaises(exception.InvalidHypervisorType, self.task._check_requested_destination) def test_check_requested_destination_fails_with_hypervisor_too_old(self): self.mox.StubOutWithMock(self.task, '_check_host_is_up') self.mox.StubOutWithMock(self.task, '_check_destination_has_enough_memory') self.mox.StubOutWithMock(self.task, '_get_compute_info') self.task._check_host_is_up(self.destination) self.task._check_destination_has_enough_memory() self.task._get_compute_info(self.instance_host).AndReturn({ "hypervisor_type": "a", "hypervisor_version": 7 }) self.task._get_compute_info(self.destination).AndReturn({ "hypervisor_type": "a", "hypervisor_version": 6 }) self.mox.ReplayAll() self.assertRaises(exception.DestinationHypervisorTooOld, self.task._check_requested_destination) def test_find_destination_works(self): self.mox.StubOutWithMock(compute_utils, 'get_image_metadata') self.mox.StubOutWithMock(scheduler_utils, 'build_request_spec') self.mox.StubOutWithMock(scheduler_utils, 'setup_instance_group') self.mox.StubOutWithMock(self.task.scheduler_client, 'select_destinations') self.mox.StubOutWithMock(self.task, '_check_compatible_with_source_hypervisor') self.mox.StubOutWithMock(self.task, '_call_livem_checks_on_host') compute_utils.get_image_metadata(self.context, self.task.image_api, self.instance_image, self.instance).AndReturn("image") scheduler_utils.build_request_spec(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({}) scheduler_utils.setup_instance_group( self.context, {}, {'ignore_hosts': [self.instance_host]}) self.task.scheduler_client.select_destinations(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( [{'host': 'host1'}]) self.task._check_compatible_with_source_hypervisor("host1") self.task._call_livem_checks_on_host("host1") self.mox.ReplayAll() self.assertEqual("host1", self.task._find_destination()) def test_find_destination_no_image_works(self): self.instance['image_ref'] = '' self.mox.StubOutWithMock(scheduler_utils, 'build_request_spec') self.mox.StubOutWithMock(scheduler_utils, 'setup_instance_group') self.mox.StubOutWithMock(self.task.scheduler_client, 'select_destinations') self.mox.StubOutWithMock(self.task, '_check_compatible_with_source_hypervisor') self.mox.StubOutWithMock(self.task, '_call_livem_checks_on_host') scheduler_utils.build_request_spec(self.context, None, mox.IgnoreArg()).AndReturn({}) scheduler_utils.setup_instance_group( self.context, {}, {'ignore_hosts': [self.instance_host]}) self.task.scheduler_client.select_destinations(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( [{'host': 'host1'}]) self.task._check_compatible_with_source_hypervisor("host1") self.task._call_livem_checks_on_host("host1") self.mox.ReplayAll() self.assertEqual("host1", self.task._find_destination()) def _test_find_destination_retry_hypervisor_raises(self, error): self.mox.StubOutWithMock(compute_utils, 'get_image_metadata') self.mox.StubOutWithMock(scheduler_utils, 'build_request_spec') self.mox.StubOutWithMock(scheduler_utils, 'setup_instance_group') self.mox.StubOutWithMock(self.task.scheduler_client, 'select_destinations') self.mox.StubOutWithMock(self.task, '_check_compatible_with_source_hypervisor') self.mox.StubOutWithMock(self.task, '_call_livem_checks_on_host') compute_utils.get_image_metadata(self.context, self.task.image_api, self.instance_image, self.instance).AndReturn("image") scheduler_utils.build_request_spec(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({}) scheduler_utils.setup_instance_group( self.context, {}, {'ignore_hosts': [self.instance_host]}) self.task.scheduler_client.select_destinations(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( [{'host': 'host1'}]) self.task._check_compatible_with_source_hypervisor("host1")\ .AndRaise(error) scheduler_utils.setup_instance_group( self.context, {}, {'ignore_hosts': [self.instance_host, "host1"]}) self.task.scheduler_client.select_destinations(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( [{'host': 'host2'}]) self.task._check_compatible_with_source_hypervisor("host2") self.task._call_livem_checks_on_host("host2") self.mox.ReplayAll() self.assertEqual("host2", self.task._find_destination()) def test_find_destination_retry_with_old_hypervisor(self): self._test_find_destination_retry_hypervisor_raises( exception.DestinationHypervisorTooOld) def test_find_destination_retry_with_invalid_hypervisor_type(self): self._test_find_destination_retry_hypervisor_raises( exception.InvalidHypervisorType) def test_find_destination_retry_with_invalid_livem_checks(self): self.flags(migrate_max_retries=1) self.mox.StubOutWithMock(compute_utils, 'get_image_metadata') self.mox.StubOutWithMock(scheduler_utils, 'build_request_spec') self.mox.StubOutWithMock(scheduler_utils, 'setup_instance_group') self.mox.StubOutWithMock(self.task.scheduler_client, 'select_destinations') self.mox.StubOutWithMock(self.task, '_check_compatible_with_source_hypervisor') self.mox.StubOutWithMock(self.task, '_call_livem_checks_on_host') compute_utils.get_image_metadata(self.context, self.task.image_api, self.instance_image, self.instance).AndReturn("image") scheduler_utils.build_request_spec(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({}) scheduler_utils.setup_instance_group( self.context, {}, {'ignore_hosts': [self.instance_host]}) self.task.scheduler_client.select_destinations(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( [{'host': 'host1'}]) self.task._check_compatible_with_source_hypervisor("host1") self.task._call_livem_checks_on_host("host1")\ .AndRaise(exception.Invalid) scheduler_utils.setup_instance_group( self.context, {}, {'ignore_hosts': [self.instance_host, "host1"]}) self.task.scheduler_client.select_destinations(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( [{'host': 'host2'}]) self.task._check_compatible_with_source_hypervisor("host2") self.task._call_livem_checks_on_host("host2") self.mox.ReplayAll() self.assertEqual("host2", self.task._find_destination()) def test_find_destination_retry_exceeds_max(self): self.flags(migrate_max_retries=0) self.mox.StubOutWithMock(compute_utils, 'get_image_metadata') self.mox.StubOutWithMock(scheduler_utils, 'build_request_spec') self.mox.StubOutWithMock(scheduler_utils, 'setup_instance_group') self.mox.StubOutWithMock(self.task.scheduler_client, 'select_destinations') self.mox.StubOutWithMock(self.task, '_check_compatible_with_source_hypervisor') compute_utils.get_image_metadata(self.context, self.task.image_api, self.instance_image, self.instance).AndReturn("image") scheduler_utils.build_request_spec(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({}) scheduler_utils.setup_instance_group( self.context, {}, {'ignore_hosts': [self.instance_host]}) self.task.scheduler_client.select_destinations(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( [{'host': 'host1'}]) self.task._check_compatible_with_source_hypervisor("host1")\ .AndRaise(exception.DestinationHypervisorTooOld) self.mox.ReplayAll() self.assertRaises(exception.NoValidHost, self.task._find_destination) def test_find_destination_when_runs_out_of_hosts(self): self.mox.StubOutWithMock(compute_utils, 'get_image_metadata') self.mox.StubOutWithMock(scheduler_utils, 'build_request_spec') self.mox.StubOutWithMock(scheduler_utils, 'setup_instance_group') self.mox.StubOutWithMock(self.task.scheduler_client, 'select_destinations') compute_utils.get_image_metadata(self.context, self.task.image_api, self.instance_image, self.instance).AndReturn("image") scheduler_utils.build_request_spec(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({}) scheduler_utils.setup_instance_group( self.context, {}, {'ignore_hosts': [self.instance_host]}) self.task.scheduler_client.select_destinations(self.context, mox.IgnoreArg(), mox.IgnoreArg()).AndRaise( exception.NoValidHost(reason="")) self.mox.ReplayAll() self.assertRaises(exception.NoValidHost, self.task._find_destination) def test_not_implemented_rollback(self): self.assertRaises(NotImplementedError, self.task.rollback)
nova/tests/unit/conductor/tasks/test_live_migrate.py
18,931
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.
546
en
0.872906
""" Script to show the wireframe of a given mesh (read from a file) in an interactive Viewer. """ from viewer import * from mesh.obj import OBJFile import sys if __name__ == "__main__": app = Viewer() if len(sys.argv) > 1: try: obj = OBJFile.read(sys.argv[1]) app.scene.addObject(obj) app.title(sys.argv[1]) app.scene.setTarget(obj.centroid) except Exception as e: raise e else: print("No input file given. Nothing to render.") print("Try 'python3 wireframe.py yourobj.obj'") app.show()
wireframe.py
599
Script to show the wireframe of a given mesh (read from a file) in an interactive Viewer.
89
en
0.762258
# The MIT License (MIT) # Copyright (c) 2015 Yanzheng Li # 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. ## ----------------------------------------------------------------------------- def test_assert_true(): try: assert True assert True, 'I want to believe.' except AssertionError: print 'This should not happen' ## ----------------------------------------------------------------------------- def test_assert_false(): try: assert False except AssertionError: print 'I cannot believe' ## ----------------------------------------------------------------------------- def test_assert_on_truthy_exprs(): try: assert 1 assert 1 + 1 assert 3.14 - 3.12 assert not False except AssertionError: print 'This should not happen' ## ----------------------------------------------------------------------------- def test_assert_on_falsy_exprs(): try: assert 0 except AssertionError: print 'I cannot believe' try: assert 0 - 1 except AssertionError: print 'I cannot believe' try: assert not True except AssertionError: print 'I cannot believe' try: assert 3.12 - 3.14 except AssertionError: print 'I cannot believe' ## ----------------------------------------------------------------------------- test_assert_true() test_assert_false() test_assert_on_truthy_exprs() test_assert_on_falsy_exprs() ## -----------------------------------------------------------------------------
python/tests/assert.py
2,585
The MIT License (MIT) Copyright (c) 2015 Yanzheng Li 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. ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- -----------------------------------------------------------------------------
1,541
en
0.678162
import logging import pandas as pd from flask import Flask, request from gevent.pywsgi import WSGIServer from time import sleep from func import rms, meas_to_influx, rms_to_influx, config logger = logging.getLogger(config['log_name']) logger.setLevel(logging.INFO) h_stream = logging.StreamHandler() h_stream.setLevel(logging.INFO) logger.addHandler(h_stream) app = Flask(__name__) @app.post('/save') def save(): headers = request.headers if 'X-API-KEY' not in headers or headers['X-API-KEY'] != config['api_key']: sleep(5) return '', 401 data = request.json dt = pd.Timestamp(data['dt']) s_data, power = rms(data['payload'], data['ticks'], dt) if power < 0: logger.error(data) return '', 204 if power < 100: return str(power) # print(s_data) # print(power) rms_to_influx(power, dt) meas_to_influx(s_data) return str(power) if __name__ == '__main__': # app.run(host=config['url'], port=config['port']) WSGIServer((config['url'], config['port']), app).serve_forever()
server/server.py
1,075
print(s_data) print(power) app.run(host=config['url'], port=config['port'])
75
en
0.182798
# coding: utf-8 """ Provides the exporter tool. The exporter can be used to export ComodIT entities to local directories. """ from __future__ import print_function from builtins import object import os from comodit_client.api.collection import EntityNotFoundException from comodit_client.api.exceptions import PythonApiException from comodit_client.api.host import Host from comodit_client.rest.exceptions import ApiException from comodit_client.util.path import ensure import six from comodit_client.api import orchestration class ExportException(Exception): """ Exception raised by exporter in case of error. """ pass class Export(object): """ The exporter is a tool that enables to export entities to local directories. Exported entities may later be (re-)imported (see L{Import}). """ def __init__(self, force = False): """ Creates an exporter instance. If force flag is set, all data already present in a destination folder are overwritten on export. @param force: If True, force flag is set. It is not otherwise. @type force: bool """ self._force = force def _export_files_content(self, entity, output_folder): for template in entity.files(): file_name = template.name try: with open(os.path.join(output_folder, file_name), "w") as f: if six.PY2: f.write(template.read_content().encode('utf-8')) else: f.write(template.read_content()) except ApiException as e: if e.code == 404: pass else: raise e def _export_entity(self, res, res_folder, export_files = False, export_thumb = False, backup = False): if backup: print("backup", res.name, "to", res_folder) else: print("exporting", res.name, "to", res_folder) # Ensures local repository does not contain stale data if(os.path.exists(res_folder) and len(os.listdir(res_folder)) > 0) and not self._force: raise ExportException(res_folder + " already exists and is not empty.") res.dump(res_folder) if export_files: # Dump files' content to disk files_folder = os.path.join(res_folder, "files") ensure(files_folder) self._export_files_content(res, files_folder) if export_thumb: # Dump thumbnail to disk try: content = res.read_thumbnail_content() with open(os.path.join(res_folder, "thumb"), "wb") as f: f.write(content) except ApiException as e: if e.code == 404: pass else: raise e def export_application(self, app, path, backup = False): """ Exports an application to a local folder. @param app: The application to export. @type app: L{Application} @param path: Path to local directory. @type path: string @param backup: indicate is a backup. @type path: bool """ self._export_entity(app, path, True, True, backup) def export_distribution(self, dist, path, backup = False): """ Exports a distribution to a local folder. @param dist: The distribution to export. @type dist: L{Distribution} @param path: Path to local directory. @type path: string @param backup: indicate is a backup. @type path: bool """ self._export_entity(dist, path, True, True, backup) def export_platform(self, plat, path, backup = False): """ Exports a platform to a local folder. @param plat: The platform to export. @type plat: L{Platform} @param path: Path to local directory. @type path: string @param backup: indicate is a backup. @type path: bool """ self._export_entity(plat, path, True, backup=backup) def export_environment(self, env, path): """ Exports an environment to a local folder. Hosts of the environment are exported also. @param env: The environment to export. @type env: L{Environment} @param path: Path to local directory. @type path: string """ self._export_entity(env, path) hosts_folder = os.path.join(path, "hosts") for host in env.hosts(): self.export_host(host, os.path.join(hosts_folder, host.name)) def export_job(self, job, path): """ Exports a job to a local folder. @param job: The job to export. @type job: L{Job} @param path: Path to local directory. @type path: string """ self._export_entity(job, path) def export_orchestration(self, orchestration, path): """ Exports a orchestration to a local folder. @param job: The orchestration to export. @type orchestration: L{Orchestration} @param path: Path to local directory. @type path: string """ self._export_entity(orchestration, path) def export_notification(self, notification, path): """ Exports a jobnotificationto a local folder. @param notification: The notification to export. @type notification: L{Notification} @param path: Path to local directory. @type path: string """ self._export_entity(notification, path) def export_host(self, host, path): """ Exports a host to a local folder. Contexts and instance are exported also. @param host: The host to export. @type host: L{Host} @param path: Path to local directory. @type path: string """ self._export_entity(host, path) # Export instance if host.state != Host.State.DEFINED: try: instance = host.get_instance() instance.dump_json(os.path.join(path, "instance.json")) except PythonApiException: pass # Export application contexts app_folder = os.path.join(path, "applications") ensure(app_folder) for context in host.applications(): context.dump_json(os.path.join(app_folder, context.application + ".json")) # Export platform context try: host.get_platform().dump_json(os.path.join(path, "platform.json")) except EntityNotFoundException: pass # Export distribution context try: host.get_distribution().dump_json(os.path.join(path, "distribution.json")) except EntityNotFoundException: pass def export_organization(self, org, path): """ Exports an organization to a local folder. Environments, applications, distributions and platforms are exported also. @param org: The organization to export. @type org: L{Organization} @param path: Path to local directory. @type path: string """ self._export_entity(org, path) for app in org.applications(): self.export_application(app, os.path.join(path, "applications", app.name)) for dist in org.distributions(): self.export_distribution(dist, os.path.join(path, "distributions", dist.name)) for plat in org.platforms(): self.export_platform(plat, os.path.join(path, "platforms", plat.name)) for job in org.jobs(): self.export_job(job, os.path.join(path, "jobs", job.name)) for orch in org.orchestrations(): self.export_orchestration(orch, os.path.join(path, "orchestrations", orch.name)) for env in org.environments(): self.export_environment(env, os.path.join(path, "environments", env.name))
comodit_client/api/exporter.py
8,051
The exporter is a tool that enables to export entities to local directories. Exported entities may later be (re-)imported (see L{Import}). Exception raised by exporter in case of error. Creates an exporter instance. If force flag is set, all data already present in a destination folder are overwritten on export. @param force: If True, force flag is set. It is not otherwise. @type force: bool Exports an application to a local folder. @param app: The application to export. @type app: L{Application} @param path: Path to local directory. @type path: string @param backup: indicate is a backup. @type path: bool Exports a distribution to a local folder. @param dist: The distribution to export. @type dist: L{Distribution} @param path: Path to local directory. @type path: string @param backup: indicate is a backup. @type path: bool Exports an environment to a local folder. Hosts of the environment are exported also. @param env: The environment to export. @type env: L{Environment} @param path: Path to local directory. @type path: string Exports a host to a local folder. Contexts and instance are exported also. @param host: The host to export. @type host: L{Host} @param path: Path to local directory. @type path: string Exports a job to a local folder. @param job: The job to export. @type job: L{Job} @param path: Path to local directory. @type path: string Exports a jobnotificationto a local folder. @param notification: The notification to export. @type notification: L{Notification} @param path: Path to local directory. @type path: string Exports a orchestration to a local folder. @param job: The orchestration to export. @type orchestration: L{Orchestration} @param path: Path to local directory. @type path: string Exports an organization to a local folder. Environments, applications, distributions and platforms are exported also. @param org: The organization to export. @type org: L{Organization} @param path: Path to local directory. @type path: string Exports a platform to a local folder. @param plat: The platform to export. @type plat: L{Platform} @param path: Path to local directory. @type path: string @param backup: indicate is a backup. @type path: bool Provides the exporter tool. The exporter can be used to export ComodIT entities to local directories. coding: utf-8 Ensures local repository does not contain stale data Dump files' content to disk Dump thumbnail to disk Export instance Export application contexts Export platform context Export distribution context
2,511
en
0.677437
from __future__ import absolute_import, division, print_function import argparse import sys import os import py import pytest from _pytest.config import argparsing as parseopt @pytest.fixture def parser(): return parseopt.Parser() class TestParser(object): def test_no_help_by_default(self, capsys): parser = parseopt.Parser(usage="xyz") pytest.raises(SystemExit, lambda: parser.parse(["-h"])) out, err = capsys.readouterr() assert err.find("error: unrecognized arguments") != -1 def test_argument(self): with pytest.raises(parseopt.ArgumentError): # need a short or long option argument = parseopt.Argument() argument = parseopt.Argument("-t") assert argument._short_opts == ["-t"] assert argument._long_opts == [] assert argument.dest == "t" argument = parseopt.Argument("-t", "--test") assert argument._short_opts == ["-t"] assert argument._long_opts == ["--test"] assert argument.dest == "test" argument = parseopt.Argument("-t", "--test", dest="abc") assert argument.dest == "abc" assert ( str(argument) == ("Argument(_short_opts: ['-t'], _long_opts: ['--test'], dest: 'abc')") ) def test_argument_type(self): argument = parseopt.Argument("-t", dest="abc", type=int) assert argument.type is int argument = parseopt.Argument("-t", dest="abc", type=str) assert argument.type is str argument = parseopt.Argument("-t", dest="abc", type=float) assert argument.type is float with pytest.warns(DeprecationWarning): with pytest.raises(KeyError): argument = parseopt.Argument("-t", dest="abc", type="choice") argument = parseopt.Argument( "-t", dest="abc", type=str, choices=["red", "blue"] ) assert argument.type is str def test_argument_processopt(self): argument = parseopt.Argument("-t", type=int) argument.default = 42 argument.dest = "abc" res = argument.attrs() assert res["default"] == 42 assert res["dest"] == "abc" def test_group_add_and_get(self, parser): group = parser.getgroup("hello", description="desc") assert group.name == "hello" assert group.description == "desc" def test_getgroup_simple(self, parser): group = parser.getgroup("hello", description="desc") assert group.name == "hello" assert group.description == "desc" group2 = parser.getgroup("hello") assert group2 is group def test_group_ordering(self, parser): parser.getgroup("1") parser.getgroup("2") parser.getgroup("3", after="1") groups = parser._groups groups_names = [x.name for x in groups] assert groups_names == list("132") def test_group_addoption(self): group = parseopt.OptionGroup("hello") group.addoption("--option1", action="store_true") assert len(group.options) == 1 assert isinstance(group.options[0], parseopt.Argument) def test_group_addoption_conflict(self): group = parseopt.OptionGroup("hello again") group.addoption("--option1", "--option-1", action="store_true") with pytest.raises(ValueError) as err: group.addoption("--option1", "--option-one", action="store_true") assert str({"--option1"}) in str(err.value) def test_group_shortopt_lowercase(self, parser): group = parser.getgroup("hello") pytest.raises( ValueError, """ group.addoption("-x", action="store_true") """, ) assert len(group.options) == 0 group._addoption("-x", action="store_true") assert len(group.options) == 1 def test_parser_addoption(self, parser): group = parser.getgroup("custom options") assert len(group.options) == 0 group.addoption("--option1", action="store_true") assert len(group.options) == 1 def test_parse(self, parser): parser.addoption("--hello", dest="hello", action="store") args = parser.parse(["--hello", "world"]) assert args.hello == "world" assert not getattr(args, parseopt.FILE_OR_DIR) def test_parse2(self, parser): args = parser.parse([py.path.local()]) assert getattr(args, parseopt.FILE_OR_DIR)[0] == py.path.local() def test_parse_known_args(self, parser): parser.parse_known_args([py.path.local()]) parser.addoption("--hello", action="store_true") ns = parser.parse_known_args(["x", "--y", "--hello", "this"]) assert ns.hello assert ns.file_or_dir == ["x"] def test_parse_known_and_unknown_args(self, parser): parser.addoption("--hello", action="store_true") ns, unknown = parser.parse_known_and_unknown_args( ["x", "--y", "--hello", "this"] ) assert ns.hello assert ns.file_or_dir == ["x"] assert unknown == ["--y", "this"] def test_parse_will_set_default(self, parser): parser.addoption("--hello", dest="hello", default="x", action="store") option = parser.parse([]) assert option.hello == "x" del option.hello parser.parse_setoption([], option) assert option.hello == "x" def test_parse_setoption(self, parser): parser.addoption("--hello", dest="hello", action="store") parser.addoption("--world", dest="world", default=42) class A(object): pass option = A() args = parser.parse_setoption(["--hello", "world"], option) assert option.hello == "world" assert option.world == 42 assert not args def test_parse_special_destination(self, parser): parser.addoption("--ultimate-answer", type=int) args = parser.parse(["--ultimate-answer", "42"]) assert args.ultimate_answer == 42 def test_parse_split_positional_arguments(self, parser): parser.addoption("-R", action="store_true") parser.addoption("-S", action="store_false") args = parser.parse(["-R", "4", "2", "-S"]) assert getattr(args, parseopt.FILE_OR_DIR) == ["4", "2"] args = parser.parse(["-R", "-S", "4", "2", "-R"]) assert getattr(args, parseopt.FILE_OR_DIR) == ["4", "2"] assert args.R is True assert args.S is False args = parser.parse(["-R", "4", "-S", "2"]) assert getattr(args, parseopt.FILE_OR_DIR) == ["4", "2"] assert args.R is True assert args.S is False def test_parse_defaultgetter(self): def defaultget(option): if not hasattr(option, "type"): return if option.type is int: option.default = 42 elif option.type is str: option.default = "world" parser = parseopt.Parser(processopt=defaultget) parser.addoption("--this", dest="this", type=int, action="store") parser.addoption("--hello", dest="hello", type=str, action="store") parser.addoption("--no", dest="no", action="store_true") option = parser.parse([]) assert option.hello == "world" assert option.this == 42 assert option.no is False def test_drop_short_helper(self): parser = argparse.ArgumentParser( formatter_class=parseopt.DropShorterLongHelpFormatter ) parser.add_argument( "-t", "--twoword", "--duo", "--two-word", "--two", help="foo" ).map_long_option = { "two": "two-word" } # throws error on --deux only! parser.add_argument( "-d", "--deuxmots", "--deux-mots", action="store_true", help="foo" ).map_long_option = { "deux": "deux-mots" } parser.add_argument("-s", action="store_true", help="single short") parser.add_argument("--abc", "-a", action="store_true", help="bar") parser.add_argument("--klm", "-k", "--kl-m", action="store_true", help="bar") parser.add_argument( "-P", "--pq-r", "-p", "--pqr", action="store_true", help="bar" ) parser.add_argument( "--zwei-wort", "--zweiwort", "--zweiwort", action="store_true", help="bar" ) parser.add_argument( "-x", "--exit-on-first", "--exitfirst", action="store_true", help="spam" ).map_long_option = { "exitfirst": "exit-on-first" } parser.add_argument("files_and_dirs", nargs="*") args = parser.parse_args(["-k", "--duo", "hallo", "--exitfirst"]) assert args.twoword == "hallo" assert args.klm is True assert args.zwei_wort is False assert args.exit_on_first is True assert args.s is False args = parser.parse_args(["--deux-mots"]) with pytest.raises(AttributeError): assert args.deux_mots is True assert args.deuxmots is True args = parser.parse_args(["file", "dir"]) assert "|".join(args.files_and_dirs) == "file|dir" def test_drop_short_0(self, parser): parser.addoption("--funcarg", "--func-arg", action="store_true") parser.addoption("--abc-def", "--abc-def", action="store_true") parser.addoption("--klm-hij", action="store_true") args = parser.parse(["--funcarg", "--k"]) assert args.funcarg is True assert args.abc_def is False assert args.klm_hij is True def test_drop_short_2(self, parser): parser.addoption("--func-arg", "--doit", action="store_true") args = parser.parse(["--doit"]) assert args.func_arg is True def test_drop_short_3(self, parser): parser.addoption("--func-arg", "--funcarg", "--doit", action="store_true") args = parser.parse(["abcd"]) assert args.func_arg is False assert args.file_or_dir == ["abcd"] def test_drop_short_help0(self, parser, capsys): parser.addoption("--func-args", "--doit", help="foo", action="store_true") parser.parse([]) help = parser.optparser.format_help() assert "--func-args, --doit foo" in help # testing would be more helpful with all help generated def test_drop_short_help1(self, parser, capsys): group = parser.getgroup("general") group.addoption("--doit", "--func-args", action="store_true", help="foo") group._addoption( "-h", "--help", action="store_true", dest="help", help="show help message and configuration info", ) parser.parse(["-h"]) help = parser.optparser.format_help() assert "-doit, --func-args foo" in help def test_multiple_metavar_help(self, parser): """ Help text for options with a metavar tuple should display help in the form "--preferences=value1 value2 value3" (#2004). """ group = parser.getgroup("general") group.addoption( "--preferences", metavar=("value1", "value2", "value3"), nargs=3 ) group._addoption("-h", "--help", action="store_true", dest="help") parser.parse(["-h"]) help = parser.optparser.format_help() assert "--preferences=value1 value2 value3" in help def test_argcomplete(testdir, monkeypatch): if not py.path.local.sysfind("bash"): pytest.skip("bash not available") script = str(testdir.tmpdir.join("test_argcomplete")) pytest_bin = sys.argv[0] if "pytest" not in os.path.basename(pytest_bin): pytest.skip("need to be run with pytest executable, not %s" % (pytest_bin,)) with open(str(script), "w") as fp: # redirect output from argcomplete to stdin and stderr is not trivial # http://stackoverflow.com/q/12589419/1307905 # so we use bash fp.write('COMP_WORDBREAKS="$COMP_WORDBREAKS" %s 8>&1 9>&2' % pytest_bin) # alternative would be exteneded Testdir.{run(),_run(),popen()} to be able # to handle a keyword argument env that replaces os.environ in popen or # extends the copy, advantage: could not forget to restore monkeypatch.setenv("_ARGCOMPLETE", "1") monkeypatch.setenv("_ARGCOMPLETE_IFS", "\x0b") monkeypatch.setenv("COMP_WORDBREAKS", " \\t\\n\"\\'><=;|&(:") arg = "--fu" monkeypatch.setenv("COMP_LINE", "pytest " + arg) monkeypatch.setenv("COMP_POINT", str(len("pytest " + arg))) result = testdir.run("bash", str(script), arg) if result.ret == 255: # argcomplete not found pytest.skip("argcomplete not available") elif not result.stdout.str(): pytest.skip("bash provided no output, argcomplete not available?") else: result.stdout.fnmatch_lines(["--funcargs", "--fulltrace"]) os.mkdir("test_argcomplete.d") arg = "test_argc" monkeypatch.setenv("COMP_LINE", "pytest " + arg) monkeypatch.setenv("COMP_POINT", str(len("pytest " + arg))) result = testdir.run("bash", str(script), arg) result.stdout.fnmatch_lines(["test_argcomplete", "test_argcomplete.d/"])
tools/third_party/pytest/testing/test_parseopt.py
13,227
Help text for options with a metavar tuple should display help in the form "--preferences=value1 value2 value3" (#2004). need a short or long option throws error on --deux only! testing would be more helpful with all help generated redirect output from argcomplete to stdin and stderr is not trivial http://stackoverflow.com/q/12589419/1307905 so we use bash alternative would be exteneded Testdir.{run(),_run(),popen()} to be able to handle a keyword argument env that replaces os.environ in popen or extends the copy, advantage: could not forget to restore argcomplete not found
582
en
0.737485
# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- # pylint: disable=too-few-public-methods, too-many-instance-attributes # pylint: disable=super-init-not-called, too-many-lines from enum import Enum from azure.storage.blob import LeaseProperties as BlobLeaseProperties from azure.storage.blob import AccountSasPermissions as BlobAccountSasPermissions from azure.storage.blob import ResourceTypes as BlobResourceTypes from azure.storage.blob import UserDelegationKey as BlobUserDelegationKey from azure.storage.blob import ContentSettings as BlobContentSettings from azure.storage.blob import AccessPolicy as BlobAccessPolicy from azure.storage.blob import DelimitedTextDialect as BlobDelimitedTextDialect from azure.storage.blob import DelimitedJsonDialect as BlobDelimitedJSON from azure.storage.blob import ArrowDialect as BlobArrowDialect from azure.storage.blob._models import ContainerPropertiesPaged from ._shared.models import DictMixin class FileSystemProperties(object): """File System properties class. :ivar ~datetime.datetime last_modified: A datetime object representing the last time the file system was modified. :ivar str etag: The ETag contains a value that you can use to perform operations conditionally. :ivar ~azure.storage.filedatalake.LeaseProperties lease: Stores all the lease information for the file system. :ivar str public_access: Specifies whether data in the file system may be accessed publicly and the level of access. :ivar bool has_immutability_policy: Represents whether the file system has an immutability policy. :ivar bool has_legal_hold: Represents whether the file system has a legal hold. :ivar dict metadata: A dict with name-value pairs to associate with the file system as metadata. Returned ``FileSystemProperties`` instances expose these values through a dictionary interface, for example: ``file_system_props["last_modified"]``. Additionally, the file system name is available as ``file_system_props["name"]``. """ def __init__(self): self.name = None self.last_modified = None self.etag = None self.lease = None self.public_access = None self.has_immutability_policy = None self.has_legal_hold = None self.metadata = None @classmethod def _from_generated(cls, generated): props = cls() props.name = generated.name props.last_modified = generated.properties.last_modified props.etag = generated.properties.etag props.lease = LeaseProperties._from_generated(generated) # pylint: disable=protected-access props.public_access = PublicAccess._from_generated( # pylint: disable=protected-access generated.properties.public_access) props.has_immutability_policy = generated.properties.has_immutability_policy props.has_legal_hold = generated.properties.has_legal_hold props.metadata = generated.metadata return props @classmethod def _convert_from_container_props(cls, container_properties): container_properties.__class__ = cls container_properties.public_access = PublicAccess._from_generated( # pylint: disable=protected-access container_properties.public_access) container_properties.lease.__class__ = LeaseProperties return container_properties class FileSystemPropertiesPaged(ContainerPropertiesPaged): """An Iterable of File System properties. :ivar str service_endpoint: The service URL. :ivar str prefix: A file system name prefix being used to filter the list. :ivar str marker: The continuation token of the current page of results. :ivar int results_per_page: The maximum number of results retrieved per API call. :ivar str continuation_token: The continuation token to retrieve the next page of results. :ivar str location_mode: The location mode being used to list results. The available options include "primary" and "secondary". :ivar current_page: The current page of listed results. :vartype current_page: list(~azure.storage.filedatalake.FileSystemProperties) :param callable command: Function to retrieve the next page of items. :param str prefix: Filters the results to return only file systems whose names begin with the specified prefix. :param int results_per_page: The maximum number of file system names to retrieve per call. :param str continuation_token: An opaque continuation token. """ def __init__(self, *args, **kwargs): super(FileSystemPropertiesPaged, self).__init__( *args, **kwargs ) @staticmethod def _build_item(item): return FileSystemProperties._from_generated(item) # pylint: disable=protected-access class DirectoryProperties(DictMixin): """ :ivar str name: name of the directory :ivar str etag: The ETag contains a value that you can use to perform operations conditionally. :ivar bool deleted: if the current directory marked as deleted :ivar dict metadata: Name-value pairs associated with the directory as metadata. :ivar ~azure.storage.filedatalake.LeaseProperties lease: Stores all the lease information for the directory. :ivar ~datetime.datetime last_modified: A datetime object representing the last time the directory was modified. :ivar ~datetime.datetime creation_time: Indicates when the directory was created, in UTC. :ivar int remaining_retention_days: The number of days that the directory will be retained before being permanently deleted by the service. :var ~azure.storage.filedatalake.ContentSettings content_settings: """ def __init__(self, **kwargs): self.name = kwargs.get('name') self.etag = kwargs.get('ETag') self.deleted = None self.metadata = kwargs.get('metadata') self.lease = LeaseProperties(**kwargs) self.last_modified = kwargs.get('Last-Modified') self.creation_time = kwargs.get('x-ms-creation-time') self.deleted_time = None self.remaining_retention_days = None class FileProperties(DictMixin): """ :ivar str name: name of the file :ivar str etag: The ETag contains a value that you can use to perform operations conditionally. :ivar bool deleted: if the current file marked as deleted :ivar dict metadata: Name-value pairs associated with the file as metadata. :ivar ~azure.storage.filedatalake.LeaseProperties lease: Stores all the lease information for the file. :ivar ~datetime.datetime last_modified: A datetime object representing the last time the file was modified. :ivar ~datetime.datetime creation_time: Indicates when the file was created, in UTC. :ivar int size: size of the file :ivar int remaining_retention_days: The number of days that the file will be retained before being permanently deleted by the service. :var ~azure.storage.filedatalake.ContentSettings content_settings: """ def __init__(self, **kwargs): self.name = kwargs.get('name') self.etag = kwargs.get('ETag') self.deleted = None self.metadata = kwargs.get('metadata') self.lease = LeaseProperties(**kwargs) self.last_modified = kwargs.get('Last-Modified') self.creation_time = kwargs.get('x-ms-creation-time') self.size = kwargs.get('Content-Length') self.deleted_time = None self.expiry_time = kwargs.get("x-ms-expiry-time") self.remaining_retention_days = None self.content_settings = ContentSettings(**kwargs) class PathProperties(object): """Path properties listed by get_paths api. :ivar str name: the full path for a file or directory. :ivar str owner: The owner of the file or directory. :ivar str group: he owning group of the file or directory. :ivar str permissions: Sets POSIX access permissions for the file owner, the file owning group, and others. Each class may be granted read, write, or execute permission. The sticky bit is also supported. Both symbolic (rwxrw-rw-) and 4-digit octal notation (e.g. 0766) are supported. :ivar datetime last_modified: A datetime object representing the last time the directory/file was modified. :ivar bool is_directory: is the path a directory or not. :ivar str etag: The ETag contains a value that you can use to perform operations conditionally. :ivar content_length: the size of file if the path is a file. """ def __init__(self, **kwargs): super(PathProperties, self).__init__( **kwargs ) self.name = kwargs.pop('name', None) self.owner = kwargs.get('owner', None) self.group = kwargs.get('group', None) self.permissions = kwargs.get('permissions', None) self.last_modified = kwargs.get('last_modified', None) self.is_directory = kwargs.get('is_directory', False) self.etag = kwargs.get('etag', None) self.content_length = kwargs.get('content_length', None) @classmethod def _from_generated(cls, generated): path_prop = PathProperties() path_prop.name = generated.name path_prop.owner = generated.owner path_prop.group = generated.group path_prop.permissions = generated.permissions path_prop.last_modified = generated.last_modified path_prop.is_directory = bool(generated.is_directory) path_prop.etag = generated.additional_properties.get('etag') path_prop.content_length = generated.content_length return path_prop class LeaseProperties(BlobLeaseProperties): """DataLake Lease Properties. :ivar str status: The lease status of the file. Possible values: locked|unlocked :ivar str state: Lease state of the file. Possible values: available|leased|expired|breaking|broken :ivar str duration: When a file is leased, specifies whether the lease is of infinite or fixed duration. """ class ContentSettings(BlobContentSettings): """The content settings of a file or directory. :ivar str content_type: The content type specified for the file or directory. If no content type was specified, the default content type is application/octet-stream. :ivar str content_encoding: If the content_encoding has previously been set for the file, that value is stored. :ivar str content_language: If the content_language has previously been set for the file, that value is stored. :ivar str content_disposition: content_disposition conveys additional information about how to process the response payload, and also can be used to attach additional metadata. If content_disposition has previously been set for the file, that value is stored. :ivar str cache_control: If the cache_control has previously been set for the file, that value is stored. :ivar str content_md5: If the content_md5 has been set for the file, this response header is stored so that the client can check for message content integrity. :keyword str content_type: The content type specified for the file or directory. If no content type was specified, the default content type is application/octet-stream. :keyword str content_encoding: If the content_encoding has previously been set for the file, that value is stored. :keyword str content_language: If the content_language has previously been set for the file, that value is stored. :keyword str content_disposition: content_disposition conveys additional information about how to process the response payload, and also can be used to attach additional metadata. If content_disposition has previously been set for the file, that value is stored. :keyword str cache_control: If the cache_control has previously been set for the file, that value is stored. :keyword str content_md5: If the content_md5 has been set for the file, this response header is stored so that the client can check for message content integrity. """ def __init__( self, **kwargs): super(ContentSettings, self).__init__( **kwargs ) class AccountSasPermissions(BlobAccountSasPermissions): def __init__(self, read=False, write=False, delete=False, list=False, # pylint: disable=redefined-builtin create=False): super(AccountSasPermissions, self).__init__( read=read, create=create, write=write, list=list, delete=delete ) class FileSystemSasPermissions(object): """FileSystemSasPermissions class to be used with the :func:`~azure.storage.filedatalake.generate_file_system_sas` function. :param bool read: Read the content, properties, metadata etc. :param bool write: Create or write content, properties, metadata. Lease the file system. :param bool delete: Delete the file system. :param bool list: List paths in the file system. :keyword bool move: Move any file in the directory to a new location. Note the move operation can optionally be restricted to the child file or directory owner or the parent directory owner if the saoid parameter is included in the token and the sticky bit is set on the parent directory. :keyword bool execute: Get the status (system defined properties) and ACL of any file in the directory. If the caller is the owner, set access control on any file in the directory. :keyword bool manage_ownership: Allows the user to set owner, owning group, or act as the owner when renaming or deleting a file or directory within a folder that has the sticky bit set. :keyword bool manage_access_control: Allows the user to set permissions and POSIX ACLs on files and directories. """ def __init__(self, read=False, write=False, delete=False, list=False, # pylint: disable=redefined-builtin **kwargs): self.read = read self.write = write self.delete = delete self.list = list self.move = kwargs.pop('move', None) self.execute = kwargs.pop('execute', None) self.manage_ownership = kwargs.pop('manage_ownership', None) self.manage_access_control = kwargs.pop('manage_access_control', None) self._str = (('r' if self.read else '') + ('w' if self.write else '') + ('d' if self.delete else '') + ('l' if self.list else '') + ('m' if self.move else '') + ('e' if self.execute else '') + ('o' if self.manage_ownership else '') + ('p' if self.manage_access_control else '')) def __str__(self): return self._str @classmethod def from_string(cls, permission): """Create a FileSystemSasPermissions from a string. To specify read, write, or delete permissions you need only to include the first letter of the word in the string. E.g. For read and write permissions, you would provide a string "rw". :param str permission: The string which dictates the read, add, create, write, or delete permissions. :return: A FileSystemSasPermissions object :rtype: ~azure.storage.fildatalake.FileSystemSasPermissions """ p_read = 'r' in permission p_write = 'w' in permission p_delete = 'd' in permission p_list = 'l' in permission p_move = 'm' in permission p_execute = 'e' in permission p_manage_ownership = 'o' in permission p_manage_access_control = 'p' in permission parsed = cls(read=p_read, write=p_write, delete=p_delete, list=p_list, move=p_move, execute=p_execute, manage_ownership=p_manage_ownership, manage_access_control=p_manage_access_control) return parsed class DirectorySasPermissions(object): """DirectorySasPermissions class to be used with the :func:`~azure.storage.filedatalake.generate_directory_sas` function. :param bool read: Read the content, properties, metadata etc. :param bool create: Create a new directory :param bool write: Create or write content, properties, metadata. Lease the directory. :param bool delete: Delete the directory. :keyword bool list: List any files in the directory. Implies Execute. :keyword bool move: Move any file in the directory to a new location. Note the move operation can optionally be restricted to the child file or directory owner or the parent directory owner if the saoid parameter is included in the token and the sticky bit is set on the parent directory. :keyword bool execute: Get the status (system defined properties) and ACL of any file in the directory. If the caller is the owner, set access control on any file in the directory. :keyword bool manage_ownership: Allows the user to set owner, owning group, or act as the owner when renaming or deleting a file or directory within a folder that has the sticky bit set. :keyword bool manage_access_control: Allows the user to set permissions and POSIX ACLs on files and directories. """ def __init__(self, read=False, create=False, write=False, delete=False, **kwargs): self.read = read self.create = create self.write = write self.delete = delete self.list = kwargs.pop('list', None) self.move = kwargs.pop('move', None) self.execute = kwargs.pop('execute', None) self.manage_ownership = kwargs.pop('manage_ownership', None) self.manage_access_control = kwargs.pop('manage_access_control', None) self._str = (('r' if self.read else '') + ('c' if self.create else '') + ('w' if self.write else '') + ('d' if self.delete else '') + ('l' if self.list else '') + ('m' if self.move else '') + ('e' if self.execute else '') + ('o' if self.manage_ownership else '') + ('p' if self.manage_access_control else '')) def __str__(self): return self._str @classmethod def from_string(cls, permission): """Create a DirectorySasPermissions from a string. To specify read, create, write, or delete permissions you need only to include the first letter of the word in the string. E.g. For read and write permissions, you would provide a string "rw". :param str permission: The string which dictates the read, add, create, write, or delete permissions. :return: A DirectorySasPermissions object :rtype: ~azure.storage.filedatalake.DirectorySasPermissions """ p_read = 'r' in permission p_create = 'c' in permission p_write = 'w' in permission p_delete = 'd' in permission p_list = 'l' in permission p_move = 'm' in permission p_execute = 'e' in permission p_manage_ownership = 'o' in permission p_manage_access_control = 'p' in permission parsed = cls(read=p_read, create=p_create, write=p_write, delete=p_delete, list=p_list, move=p_move, execute=p_execute, manage_ownership=p_manage_ownership, manage_access_control=p_manage_access_control) return parsed class FileSasPermissions(object): """FileSasPermissions class to be used with the :func:`~azure.storage.filedatalake.generate_file_sas` function. :param bool read: Read the content, properties, metadata etc. Use the file as the source of a read operation. :param bool create: Write a new file :param bool write: Create or write content, properties, metadata. Lease the file. :param bool delete: Delete the file. :keyword bool move: Move any file in the directory to a new location. Note the move operation can optionally be restricted to the child file or directory owner or the parent directory owner if the saoid parameter is included in the token and the sticky bit is set on the parent directory. :keyword bool execute: Get the status (system defined properties) and ACL of any file in the directory. If the caller is the owner, set access control on any file in the directory. :keyword bool manage_ownership: Allows the user to set owner, owning group, or act as the owner when renaming or deleting a file or directory within a folder that has the sticky bit set. :keyword bool manage_access_control: Allows the user to set permissions and POSIX ACLs on files and directories. """ def __init__(self, read=False, create=False, write=False, delete=False, **kwargs): self.read = read self.create = create self.write = write self.delete = delete self.list = list self.move = kwargs.pop('move', None) self.execute = kwargs.pop('execute', None) self.manage_ownership = kwargs.pop('manage_ownership', None) self.manage_access_control = kwargs.pop('manage_access_control', None) self._str = (('r' if self.read else '') + ('c' if self.create else '') + ('w' if self.write else '') + ('d' if self.delete else '') + ('m' if self.move else '') + ('e' if self.execute else '') + ('o' if self.manage_ownership else '') + ('p' if self.manage_access_control else '')) def __str__(self): return self._str @classmethod def from_string(cls, permission): """Create a FileSasPermissions from a string. To specify read, write, or delete permissions you need only to include the first letter of the word in the string. E.g. For read and write permissions, you would provide a string "rw". :param str permission: The string which dictates the read, add, create, write, or delete permissions. :return: A FileSasPermissions object :rtype: ~azure.storage.fildatalake.FileSasPermissions """ p_read = 'r' in permission p_create = 'c' in permission p_write = 'w' in permission p_delete = 'd' in permission p_move = 'm' in permission p_execute = 'e' in permission p_manage_ownership = 'o' in permission p_manage_access_control = 'p' in permission parsed = cls(read=p_read, create=p_create, write=p_write, delete=p_delete, move=p_move, execute=p_execute, manage_ownership=p_manage_ownership, manage_access_control=p_manage_access_control) return parsed class AccessPolicy(BlobAccessPolicy): """Access Policy class used by the set and get access policy methods in each service. A stored access policy can specify the start time, expiry time, and permissions for the Shared Access Signatures with which it's associated. Depending on how you want to control access to your resource, you can specify all of these parameters within the stored access policy, and omit them from the URL for the Shared Access Signature. Doing so permits you to modify the associated signature's behavior at any time, as well as to revoke it. Or you can specify one or more of the access policy parameters within the stored access policy, and the others on the URL. Finally, you can specify all of the parameters on the URL. In this case, you can use the stored access policy to revoke the signature, but not to modify its behavior. Together the Shared Access Signature and the stored access policy must include all fields required to authenticate the signature. If any required fields are missing, the request will fail. Likewise, if a field is specified both in the Shared Access Signature URL and in the stored access policy, the request will fail with status code 400 (Bad Request). :param permission: The permissions associated with the shared access signature. The user is restricted to operations allowed by the permissions. Required unless an id is given referencing a stored access policy which contains this field. This field must be omitted if it has been specified in an associated stored access policy. :type permission: str or ~azure.storage.datalake.FileSystemSasPermissions :param expiry: The time at which the shared access signature becomes invalid. Required unless an id is given referencing a stored access policy which contains this field. This field must be omitted if it has been specified in an associated stored access policy. Azure will always convert values to UTC. If a date is passed in without timezone info, it is assumed to be UTC. :type expiry: ~datetime.datetime or str :keyword start: The time at which the shared access signature becomes valid. If omitted, start time for this call is assumed to be the time when the storage service receives the request. Azure will always convert values to UTC. If a date is passed in without timezone info, it is assumed to be UTC. :paramtype start: ~datetime.datetime or str """ def __init__(self, permission=None, expiry=None, **kwargs): super(AccessPolicy, self).__init__( permission=permission, expiry=expiry, start=kwargs.pop('start', None) ) class ResourceTypes(BlobResourceTypes): """ Specifies the resource types that are accessible with the account SAS. :param bool service: Access to service-level APIs (e.g.List File Systems) :param bool file_system: Access to file_system-level APIs (e.g., Create/Delete file system, List Directories/Files) :param bool object: Access to object-level APIs for files(e.g. Create File, etc.) """ def __init__(self, service=False, file_system=False, object=False # pylint: disable=redefined-builtin ): super(ResourceTypes, self).__init__(service=service, container=file_system, object=object) class UserDelegationKey(BlobUserDelegationKey): """ Represents a user delegation key, provided to the user by Azure Storage based on their Azure Active Directory access token. The fields are saved as simple strings since the user does not have to interact with this object; to generate an identify SAS, the user can simply pass it to the right API. :ivar str signed_oid: Object ID of this token. :ivar str signed_tid: Tenant ID of the tenant that issued this token. :ivar str signed_start: The datetime this token becomes valid. :ivar str signed_expiry: The datetime this token expires. :ivar str signed_service: What service this key is valid for. :ivar str signed_version: The version identifier of the REST service that created this token. :ivar str value: The user delegation key. """ @classmethod def _from_generated(cls, generated): delegation_key = cls() delegation_key.signed_oid = generated.signed_oid delegation_key.signed_tid = generated.signed_tid delegation_key.signed_start = generated.signed_start delegation_key.signed_expiry = generated.signed_expiry delegation_key.signed_service = generated.signed_service delegation_key.signed_version = generated.signed_version delegation_key.value = generated.value return delegation_key class PublicAccess(str, Enum): """ Specifies whether data in the file system may be accessed publicly and the level of access. """ File = 'blob' """ Specifies public read access for files. file data within this file system can be read via anonymous request, but file system data is not available. Clients cannot enumerate files within the container via anonymous request. """ FileSystem = 'container' """ Specifies full public read access for file system and file data. Clients can enumerate files within the file system via anonymous request, but cannot enumerate file systems within the storage account. """ @classmethod def _from_generated(cls, public_access): if public_access == "blob": # pylint:disable=no-else-return return cls.File elif public_access == "container": return cls.FileSystem return None class LocationMode(object): """ Specifies the location the request should be sent to. This mode only applies for RA-GRS accounts which allow secondary read access. All other account types must use PRIMARY. """ PRIMARY = 'primary' #: Requests should be sent to the primary location. SECONDARY = 'secondary' #: Requests should be sent to the secondary location, if possible. class DelimitedJsonDialect(BlobDelimitedJSON): """Defines the input or output JSON serialization for a datalake query. :keyword str delimiter: The line separator character, default value is '\n' """ class DelimitedTextDialect(BlobDelimitedTextDialect): """Defines the input or output delimited (CSV) serialization for a datalake query request. :keyword str delimiter: Column separator, defaults to ','. :keyword str quotechar: Field quote, defaults to '"'. :keyword str lineterminator: Record separator, defaults to '\n'. :keyword str escapechar: Escape char, defaults to empty. :keyword bool has_header: Whether the blob data includes headers in the first line. The default value is False, meaning that the data will be returned inclusive of the first line. If set to True, the data will be returned exclusive of the first line. """ class ArrowDialect(BlobArrowDialect): """field of an arrow schema. All required parameters must be populated in order to send to Azure. :param str type: Required. :keyword str name: The name of the field. :keyword int precision: The precision of the field. :keyword int scale: The scale of the field. """ class ArrowType(str, Enum): INT64 = "int64" BOOL = "bool" TIMESTAMP_MS = "timestamp[ms]" STRING = "string" DOUBLE = "double" DECIMAL = 'decimal' class DataLakeFileQueryError(object): """The error happened during quick query operation. :ivar str error: The name of the error. :ivar bool is_fatal: If true, this error prevents further query processing. More result data may be returned, but there is no guarantee that all of the original data will be processed. If false, this error does not prevent further query processing. :ivar str description: A description of the error. :ivar int position: The blob offset at which the error occurred. """ def __init__(self, error=None, is_fatal=False, description=None, position=None): self.error = error self.is_fatal = is_fatal self.description = description self.position = position class AccessControlChangeCounters(DictMixin): """ AccessControlChangeCounters contains counts of operations that change Access Control Lists recursively. :ivar int directories_successful: Number of directories where Access Control List has been updated successfully. :ivar int files_successful: Number of files where Access Control List has been updated successfully. :ivar int failure_count: Number of paths where Access Control List update has failed. """ def __init__(self, directories_successful, files_successful, failure_count): self.directories_successful = directories_successful self.files_successful = files_successful self.failure_count = failure_count class AccessControlChangeResult(DictMixin): """ AccessControlChangeResult contains result of operations that change Access Control Lists recursively. :ivar ~azure.storage.filedatalake.AccessControlChangeCounters counters: Contains counts of paths changed from start of the operation. :ivar str continuation: Optional continuation token. Value is present when operation is split into multiple batches and can be used to resume progress. """ def __init__(self, counters, continuation): self.counters = counters self.continuation = continuation class AccessControlChangeFailure(DictMixin): """ Represents an entry that failed to update Access Control List. :ivar str name: Name of the entry. :ivar bool is_directory: Indicates whether the entry is a directory. :ivar str error_message: Indicates the reason why the entry failed to update. """ def __init__(self, name, is_directory, error_message): self.name = name self.is_directory = is_directory self.error_message = error_message class AccessControlChanges(DictMixin): """ AccessControlChanges contains batch and cumulative counts of operations that change Access Control Lists recursively. Additionally it exposes path entries that failed to update while these operations progress. :ivar ~azure.storage.filedatalake.AccessControlChangeCounters batch_counters: Contains counts of paths changed within single batch. :ivar ~azure.storage.filedatalake.AccessControlChangeCounters aggregate_counters: Contains counts of paths changed from start of the operation. :ivar list(~azure.storage.filedatalake.AccessControlChangeFailure) batch_failures: List of path entries that failed to update Access Control List within single batch. :ivar str continuation: An opaque continuation token that may be used to resume the operations in case of failures. """ def __init__(self, batch_counters, aggregate_counters, batch_failures, continuation): self.batch_counters = batch_counters self.aggregate_counters = aggregate_counters self.batch_failures = batch_failures self.continuation = continuation class DataLakeAclChangeFailedError(Exception): """The error happened during set/update/remove acl recursive operation. :ivar ~azure.core.exceptions.AzureError error: The exception. :ivar str description: A description of the error. :ivar str continuation: An opaque continuation token that may be used to resume the operations in case of failures. """ def __init__(self, error, description, continuation): self.error = error self.description = description self.continuation = continuation
sdk/storage/azure-storage-file-datalake/azure/storage/filedatalake/_models.py
35,842
AccessControlChangeCounters contains counts of operations that change Access Control Lists recursively. :ivar int directories_successful: Number of directories where Access Control List has been updated successfully. :ivar int files_successful: Number of files where Access Control List has been updated successfully. :ivar int failure_count: Number of paths where Access Control List update has failed. Represents an entry that failed to update Access Control List. :ivar str name: Name of the entry. :ivar bool is_directory: Indicates whether the entry is a directory. :ivar str error_message: Indicates the reason why the entry failed to update. AccessControlChangeResult contains result of operations that change Access Control Lists recursively. :ivar ~azure.storage.filedatalake.AccessControlChangeCounters counters: Contains counts of paths changed from start of the operation. :ivar str continuation: Optional continuation token. Value is present when operation is split into multiple batches and can be used to resume progress. AccessControlChanges contains batch and cumulative counts of operations that change Access Control Lists recursively. Additionally it exposes path entries that failed to update while these operations progress. :ivar ~azure.storage.filedatalake.AccessControlChangeCounters batch_counters: Contains counts of paths changed within single batch. :ivar ~azure.storage.filedatalake.AccessControlChangeCounters aggregate_counters: Contains counts of paths changed from start of the operation. :ivar list(~azure.storage.filedatalake.AccessControlChangeFailure) batch_failures: List of path entries that failed to update Access Control List within single batch. :ivar str continuation: An opaque continuation token that may be used to resume the operations in case of failures. Access Policy class used by the set and get access policy methods in each service. A stored access policy can specify the start time, expiry time, and permissions for the Shared Access Signatures with which it's associated. Depending on how you want to control access to your resource, you can specify all of these parameters within the stored access policy, and omit them from the URL for the Shared Access Signature. Doing so permits you to modify the associated signature's behavior at any time, as well as to revoke it. Or you can specify one or more of the access policy parameters within the stored access policy, and the others on the URL. Finally, you can specify all of the parameters on the URL. In this case, you can use the stored access policy to revoke the signature, but not to modify its behavior. Together the Shared Access Signature and the stored access policy must include all fields required to authenticate the signature. If any required fields are missing, the request will fail. Likewise, if a field is specified both in the Shared Access Signature URL and in the stored access policy, the request will fail with status code 400 (Bad Request). :param permission: The permissions associated with the shared access signature. The user is restricted to operations allowed by the permissions. Required unless an id is given referencing a stored access policy which contains this field. This field must be omitted if it has been specified in an associated stored access policy. :type permission: str or ~azure.storage.datalake.FileSystemSasPermissions :param expiry: The time at which the shared access signature becomes invalid. Required unless an id is given referencing a stored access policy which contains this field. This field must be omitted if it has been specified in an associated stored access policy. Azure will always convert values to UTC. If a date is passed in without timezone info, it is assumed to be UTC. :type expiry: ~datetime.datetime or str :keyword start: The time at which the shared access signature becomes valid. If omitted, start time for this call is assumed to be the time when the storage service receives the request. Azure will always convert values to UTC. If a date is passed in without timezone info, it is assumed to be UTC. :paramtype start: ~datetime.datetime or str field of an arrow schema. All required parameters must be populated in order to send to Azure. :param str type: Required. :keyword str name: The name of the field. :keyword int precision: The precision of the field. :keyword int scale: The scale of the field. The content settings of a file or directory. :ivar str content_type: The content type specified for the file or directory. If no content type was specified, the default content type is application/octet-stream. :ivar str content_encoding: If the content_encoding has previously been set for the file, that value is stored. :ivar str content_language: If the content_language has previously been set for the file, that value is stored. :ivar str content_disposition: content_disposition conveys additional information about how to process the response payload, and also can be used to attach additional metadata. If content_disposition has previously been set for the file, that value is stored. :ivar str cache_control: If the cache_control has previously been set for the file, that value is stored. :ivar str content_md5: If the content_md5 has been set for the file, this response header is stored so that the client can check for message content integrity. :keyword str content_type: The content type specified for the file or directory. If no content type was specified, the default content type is application/octet-stream. :keyword str content_encoding: If the content_encoding has previously been set for the file, that value is stored. :keyword str content_language: If the content_language has previously been set for the file, that value is stored. :keyword str content_disposition: content_disposition conveys additional information about how to process the response payload, and also can be used to attach additional metadata. If content_disposition has previously been set for the file, that value is stored. :keyword str cache_control: If the cache_control has previously been set for the file, that value is stored. :keyword str content_md5: If the content_md5 has been set for the file, this response header is stored so that the client can check for message content integrity. The error happened during set/update/remove acl recursive operation. :ivar ~azure.core.exceptions.AzureError error: The exception. :ivar str description: A description of the error. :ivar str continuation: An opaque continuation token that may be used to resume the operations in case of failures. The error happened during quick query operation. :ivar str error: The name of the error. :ivar bool is_fatal: If true, this error prevents further query processing. More result data may be returned, but there is no guarantee that all of the original data will be processed. If false, this error does not prevent further query processing. :ivar str description: A description of the error. :ivar int position: The blob offset at which the error occurred. Defines the input or output JSON serialization for a datalake query. :keyword str delimiter: The line separator character, default value is ' ' Defines the input or output delimited (CSV) serialization for a datalake query request. :keyword str delimiter: Column separator, defaults to ','. :keyword str quotechar: Field quote, defaults to '"'. :keyword str lineterminator: Record separator, defaults to ' '. :keyword str escapechar: Escape char, defaults to empty. :keyword bool has_header: Whether the blob data includes headers in the first line. The default value is False, meaning that the data will be returned inclusive of the first line. If set to True, the data will be returned exclusive of the first line. :ivar str name: name of the directory :ivar str etag: The ETag contains a value that you can use to perform operations conditionally. :ivar bool deleted: if the current directory marked as deleted :ivar dict metadata: Name-value pairs associated with the directory as metadata. :ivar ~azure.storage.filedatalake.LeaseProperties lease: Stores all the lease information for the directory. :ivar ~datetime.datetime last_modified: A datetime object representing the last time the directory was modified. :ivar ~datetime.datetime creation_time: Indicates when the directory was created, in UTC. :ivar int remaining_retention_days: The number of days that the directory will be retained before being permanently deleted by the service. :var ~azure.storage.filedatalake.ContentSettings content_settings: DirectorySasPermissions class to be used with the :func:`~azure.storage.filedatalake.generate_directory_sas` function. :param bool read: Read the content, properties, metadata etc. :param bool create: Create a new directory :param bool write: Create or write content, properties, metadata. Lease the directory. :param bool delete: Delete the directory. :keyword bool list: List any files in the directory. Implies Execute. :keyword bool move: Move any file in the directory to a new location. Note the move operation can optionally be restricted to the child file or directory owner or the parent directory owner if the saoid parameter is included in the token and the sticky bit is set on the parent directory. :keyword bool execute: Get the status (system defined properties) and ACL of any file in the directory. If the caller is the owner, set access control on any file in the directory. :keyword bool manage_ownership: Allows the user to set owner, owning group, or act as the owner when renaming or deleting a file or directory within a folder that has the sticky bit set. :keyword bool manage_access_control: Allows the user to set permissions and POSIX ACLs on files and directories. :ivar str name: name of the file :ivar str etag: The ETag contains a value that you can use to perform operations conditionally. :ivar bool deleted: if the current file marked as deleted :ivar dict metadata: Name-value pairs associated with the file as metadata. :ivar ~azure.storage.filedatalake.LeaseProperties lease: Stores all the lease information for the file. :ivar ~datetime.datetime last_modified: A datetime object representing the last time the file was modified. :ivar ~datetime.datetime creation_time: Indicates when the file was created, in UTC. :ivar int size: size of the file :ivar int remaining_retention_days: The number of days that the file will be retained before being permanently deleted by the service. :var ~azure.storage.filedatalake.ContentSettings content_settings: FileSasPermissions class to be used with the :func:`~azure.storage.filedatalake.generate_file_sas` function. :param bool read: Read the content, properties, metadata etc. Use the file as the source of a read operation. :param bool create: Write a new file :param bool write: Create or write content, properties, metadata. Lease the file. :param bool delete: Delete the file. :keyword bool move: Move any file in the directory to a new location. Note the move operation can optionally be restricted to the child file or directory owner or the parent directory owner if the saoid parameter is included in the token and the sticky bit is set on the parent directory. :keyword bool execute: Get the status (system defined properties) and ACL of any file in the directory. If the caller is the owner, set access control on any file in the directory. :keyword bool manage_ownership: Allows the user to set owner, owning group, or act as the owner when renaming or deleting a file or directory within a folder that has the sticky bit set. :keyword bool manage_access_control: Allows the user to set permissions and POSIX ACLs on files and directories. File System properties class. :ivar ~datetime.datetime last_modified: A datetime object representing the last time the file system was modified. :ivar str etag: The ETag contains a value that you can use to perform operations conditionally. :ivar ~azure.storage.filedatalake.LeaseProperties lease: Stores all the lease information for the file system. :ivar str public_access: Specifies whether data in the file system may be accessed publicly and the level of access. :ivar bool has_immutability_policy: Represents whether the file system has an immutability policy. :ivar bool has_legal_hold: Represents whether the file system has a legal hold. :ivar dict metadata: A dict with name-value pairs to associate with the file system as metadata. Returned ``FileSystemProperties`` instances expose these values through a dictionary interface, for example: ``file_system_props["last_modified"]``. Additionally, the file system name is available as ``file_system_props["name"]``. An Iterable of File System properties. :ivar str service_endpoint: The service URL. :ivar str prefix: A file system name prefix being used to filter the list. :ivar str marker: The continuation token of the current page of results. :ivar int results_per_page: The maximum number of results retrieved per API call. :ivar str continuation_token: The continuation token to retrieve the next page of results. :ivar str location_mode: The location mode being used to list results. The available options include "primary" and "secondary". :ivar current_page: The current page of listed results. :vartype current_page: list(~azure.storage.filedatalake.FileSystemProperties) :param callable command: Function to retrieve the next page of items. :param str prefix: Filters the results to return only file systems whose names begin with the specified prefix. :param int results_per_page: The maximum number of file system names to retrieve per call. :param str continuation_token: An opaque continuation token. FileSystemSasPermissions class to be used with the :func:`~azure.storage.filedatalake.generate_file_system_sas` function. :param bool read: Read the content, properties, metadata etc. :param bool write: Create or write content, properties, metadata. Lease the file system. :param bool delete: Delete the file system. :param bool list: List paths in the file system. :keyword bool move: Move any file in the directory to a new location. Note the move operation can optionally be restricted to the child file or directory owner or the parent directory owner if the saoid parameter is included in the token and the sticky bit is set on the parent directory. :keyword bool execute: Get the status (system defined properties) and ACL of any file in the directory. If the caller is the owner, set access control on any file in the directory. :keyword bool manage_ownership: Allows the user to set owner, owning group, or act as the owner when renaming or deleting a file or directory within a folder that has the sticky bit set. :keyword bool manage_access_control: Allows the user to set permissions and POSIX ACLs on files and directories. DataLake Lease Properties. :ivar str status: The lease status of the file. Possible values: locked|unlocked :ivar str state: Lease state of the file. Possible values: available|leased|expired|breaking|broken :ivar str duration: When a file is leased, specifies whether the lease is of infinite or fixed duration. Specifies the location the request should be sent to. This mode only applies for RA-GRS accounts which allow secondary read access. All other account types must use PRIMARY. Path properties listed by get_paths api. :ivar str name: the full path for a file or directory. :ivar str owner: The owner of the file or directory. :ivar str group: he owning group of the file or directory. :ivar str permissions: Sets POSIX access permissions for the file owner, the file owning group, and others. Each class may be granted read, write, or execute permission. The sticky bit is also supported. Both symbolic (rwxrw-rw-) and 4-digit octal notation (e.g. 0766) are supported. :ivar datetime last_modified: A datetime object representing the last time the directory/file was modified. :ivar bool is_directory: is the path a directory or not. :ivar str etag: The ETag contains a value that you can use to perform operations conditionally. :ivar content_length: the size of file if the path is a file. Specifies whether data in the file system may be accessed publicly and the level of access. Specifies the resource types that are accessible with the account SAS. :param bool service: Access to service-level APIs (e.g.List File Systems) :param bool file_system: Access to file_system-level APIs (e.g., Create/Delete file system, List Directories/Files) :param bool object: Access to object-level APIs for files(e.g. Create File, etc.) Represents a user delegation key, provided to the user by Azure Storage based on their Azure Active Directory access token. The fields are saved as simple strings since the user does not have to interact with this object; to generate an identify SAS, the user can simply pass it to the right API. :ivar str signed_oid: Object ID of this token. :ivar str signed_tid: Tenant ID of the tenant that issued this token. :ivar str signed_start: The datetime this token becomes valid. :ivar str signed_expiry: The datetime this token expires. :ivar str signed_service: What service this key is valid for. :ivar str signed_version: The version identifier of the REST service that created this token. :ivar str value: The user delegation key. Create a FileSystemSasPermissions from a string. To specify read, write, or delete permissions you need only to include the first letter of the word in the string. E.g. For read and write permissions, you would provide a string "rw". :param str permission: The string which dictates the read, add, create, write, or delete permissions. :return: A FileSystemSasPermissions object :rtype: ~azure.storage.fildatalake.FileSystemSasPermissions Create a DirectorySasPermissions from a string. To specify read, create, write, or delete permissions you need only to include the first letter of the word in the string. E.g. For read and write permissions, you would provide a string "rw". :param str permission: The string which dictates the read, add, create, write, or delete permissions. :return: A DirectorySasPermissions object :rtype: ~azure.storage.filedatalake.DirectorySasPermissions Create a FileSasPermissions from a string. To specify read, write, or delete permissions you need only to include the first letter of the word in the string. E.g. For read and write permissions, you would provide a string "rw". :param str permission: The string which dictates the read, add, create, write, or delete permissions. :return: A FileSasPermissions object :rtype: ~azure.storage.fildatalake.FileSasPermissions ------------------------------------------------------------------------- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See License.txt in the project root for license information. -------------------------------------------------------------------------- pylint: disable=too-few-public-methods, too-many-instance-attributes pylint: disable=super-init-not-called, too-many-lines pylint: disable=protected-access pylint: disable=protected-access pylint: disable=protected-access pylint: disable=protected-access pylint: disable=redefined-builtin pylint: disable=redefined-builtin pylint: disable=redefined-builtin pylint:disable=no-else-return: Requests should be sent to the primary location.: Requests should be sent to the secondary location, if possible.
20,122
en
0.811518
# Copyright 2019 The TensorFlow Authors. 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. # ============================================================================== """Init module for TensorFlow Model Optimization Python API. ``` import tensorflow_model_optimization as tfmot ``` """ from __future__ import absolute_import from __future__ import division from __future__ import print_function # We need to put some imports inside a function call below, and the function # call needs to come before the *actual* imports that populate the # tensorflow_model_optimization namespace. Hence, we disable this lint check # throughout the file. # # pylint: disable=g-import-not-at-top # Ensure TensorFlow is importable and its version is sufficiently recent. This # needs to happen before anything else, since the imports below will try to # import tensorflow, too. def _ensure_tf_install(): # pylint: disable=g-statement-before-imports """Attempt to import tensorflow, and ensure its version is sufficient. Raises: ImportError: if either tensorflow is not importable or its version is inadequate. """ try: import tensorflow as tf except ImportError: # Print more informative error message, then reraise. print( '\n\nFailed to import TensorFlow. Please note that TensorFlow is not ' 'installed by default when you install TensorFlow Model Optimization. This ' 'is so that users can decide whether to install the GPU-enabled ' 'TensorFlow package. To use TensorFlow Model Optimization, please install ' 'the most recent version of TensorFlow, by following instructions at ' 'https://tensorflow.org/install.\n\n') raise import distutils.version # # Update this whenever we need to depend on a newer TensorFlow release. # required_tensorflow_version = '1.14.0' if (distutils.version.LooseVersion(tf.version.VERSION) < distutils.version.LooseVersion(required_tensorflow_version)): raise ImportError( 'This version of TensorFlow Model Optimization requires TensorFlow ' 'version >= {required}; Detected an installation of version {present}. ' 'Please upgrade TensorFlow to proceed.'.format( required=required_tensorflow_version, present=tf.__version__)) _ensure_tf_install() import inspect as _inspect import os as _os import sys as _sys # To ensure users only access the expected public API, the API structure is # created in the `api` directory. Import all api modules. # pylint: disable=wildcard-import from tensorflow_model_optimization.python.core.api import * # pylint: enable=wildcard-import # Use sparsity module to fetch the path for the `api` directory. # This handles all techniques, not just sparsity. _API_MODULE = sparsity # pylint: disable=undefined-variable # Returns $(install_dir)/tensorflow_model_optimization/api _sparsity_api_dir = _os.path.dirname( _os.path.dirname(_inspect.getfile(_API_MODULE))) # Add the `api` directory to `__path__` so that `from * import module` works. _current_module = _sys.modules[__name__] if not hasattr(_current_module, '__path__'): __path__ = [_sparsity_api_dir] elif _os.path.dirname(_inspect.getfile(_API_MODULE)) not in __path__: __path__.append(_sparsity_api_dir) # Delete python module so that users only access the code using the API path # rather than using the code directory structure. # This will disallow usage such as `tfmot.python.core.sparsity.keras`. # pylint: disable=undefined-variable try: del python except NameError: pass # pylint: enable=undefined-variable
tensorflow_model_optimization/__init__.py
4,116
Attempt to import tensorflow, and ensure its version is sufficient. Raises: ImportError: if either tensorflow is not importable or its version is inadequate. Init module for TensorFlow Model Optimization Python API. ``` import tensorflow_model_optimization as tfmot ``` Copyright 2019 The TensorFlow Authors. 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. ============================================================================== We need to put some imports inside a function call below, and the function call needs to come before the *actual* imports that populate the tensorflow_model_optimization namespace. Hence, we disable this lint check throughout the file. pylint: disable=g-import-not-at-top Ensure TensorFlow is importable and its version is sufficiently recent. This needs to happen before anything else, since the imports below will try to import tensorflow, too. pylint: disable=g-statement-before-imports Print more informative error message, then reraise. Update this whenever we need to depend on a newer TensorFlow release. To ensure users only access the expected public API, the API structure is created in the `api` directory. Import all api modules. pylint: disable=wildcard-import pylint: enable=wildcard-import Use sparsity module to fetch the path for the `api` directory. This handles all techniques, not just sparsity. pylint: disable=undefined-variable Returns $(install_dir)/tensorflow_model_optimization/api Add the `api` directory to `__path__` so that `from * import module` works. Delete python module so that users only access the code using the API path rather than using the code directory structure. This will disallow usage such as `tfmot.python.core.sparsity.keras`. pylint: disable=undefined-variable pylint: enable=undefined-variable
2,283
en
0.781845
""" Constructor functions intended to be shared by pd.array, Series.__init__, and Index.__new__. These should not depend on core.internals. """ from __future__ import annotations from collections import abc from typing import TYPE_CHECKING, Any, Optional, Sequence, Union, cast import numpy as np import numpy.ma as ma from pandas._libs import lib from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj from pandas.core.dtypes.base import ExtensionDtype, registry from pandas.core.dtypes.cast import ( construct_1d_arraylike_from_scalar, construct_1d_ndarray_preserving_na, construct_1d_object_array_from_listlike, infer_dtype_from_scalar, maybe_cast_to_datetime, maybe_cast_to_integer_array, maybe_castable, maybe_convert_platform, maybe_upcast, ) from pandas.core.dtypes.common import ( is_datetime64_ns_dtype, is_extension_array_dtype, is_float_dtype, is_integer_dtype, is_iterator, is_list_like, is_object_dtype, is_sparse, is_string_dtype, is_timedelta64_ns_dtype, ) from pandas.core.dtypes.generic import ( ABCExtensionArray, ABCIndexClass, ABCPandasArray, ABCSeries, ) from pandas.core.dtypes.missing import isna import pandas.core.common as com if TYPE_CHECKING: from pandas import ExtensionArray, Index, Series def array( data: Union[Sequence[object], AnyArrayLike], dtype: Optional[Dtype] = None, copy: bool = True, ) -> ExtensionArray: """ Create an array. .. versionadded:: 0.24.0 Parameters ---------- data : Sequence of objects The scalars inside `data` should be instances of the scalar type for `dtype`. It's expected that `data` represents a 1-dimensional array of data. When `data` is an Index or Series, the underlying array will be extracted from `data`. dtype : str, np.dtype, or ExtensionDtype, optional The dtype to use for the array. This may be a NumPy dtype or an extension type registered with pandas using :meth:`pandas.api.extensions.register_extension_dtype`. If not specified, there are two possibilities: 1. When `data` is a :class:`Series`, :class:`Index`, or :class:`ExtensionArray`, the `dtype` will be taken from the data. 2. Otherwise, pandas will attempt to infer the `dtype` from the data. Note that when `data` is a NumPy array, ``data.dtype`` is *not* used for inferring the array type. This is because NumPy cannot represent all the types of data that can be held in extension arrays. Currently, pandas will infer an extension dtype for sequences of ============================== ===================================== Scalar Type Array Type ============================== ===================================== :class:`pandas.Interval` :class:`pandas.arrays.IntervalArray` :class:`pandas.Period` :class:`pandas.arrays.PeriodArray` :class:`datetime.datetime` :class:`pandas.arrays.DatetimeArray` :class:`datetime.timedelta` :class:`pandas.arrays.TimedeltaArray` :class:`int` :class:`pandas.arrays.IntegerArray` :class:`float` :class:`pandas.arrays.FloatingArray` :class:`str` :class:`pandas.arrays.StringArray` :class:`bool` :class:`pandas.arrays.BooleanArray` ============================== ===================================== For all other cases, NumPy's usual inference rules will be used. .. versionchanged:: 1.0.0 Pandas infers nullable-integer dtype for integer data, string dtype for string data, and nullable-boolean dtype for boolean data. .. versionchanged:: 1.2.0 Pandas now also infers nullable-floating dtype for float-like input data copy : bool, default True Whether to copy the data, even if not necessary. Depending on the type of `data`, creating the new array may require copying data, even if ``copy=False``. Returns ------- ExtensionArray The newly created array. Raises ------ ValueError When `data` is not 1-dimensional. See Also -------- numpy.array : Construct a NumPy array. Series : Construct a pandas Series. Index : Construct a pandas Index. arrays.PandasArray : ExtensionArray wrapping a NumPy array. Series.array : Extract the array stored within a Series. Notes ----- Omitting the `dtype` argument means pandas will attempt to infer the best array type from the values in the data. As new array types are added by pandas and 3rd party libraries, the "best" array type may change. We recommend specifying `dtype` to ensure that 1. the correct array type for the data is returned 2. the returned array type doesn't change as new extension types are added by pandas and third-party libraries Additionally, if the underlying memory representation of the returned array matters, we recommend specifying the `dtype` as a concrete object rather than a string alias or allowing it to be inferred. For example, a future version of pandas or a 3rd-party library may include a dedicated ExtensionArray for string data. In this event, the following would no longer return a :class:`arrays.PandasArray` backed by a NumPy array. >>> pd.array(['a', 'b'], dtype=str) <PandasArray> ['a', 'b'] Length: 2, dtype: str32 This would instead return the new ExtensionArray dedicated for string data. If you really need the new array to be backed by a NumPy array, specify that in the dtype. >>> pd.array(['a', 'b'], dtype=np.dtype("<U1")) <PandasArray> ['a', 'b'] Length: 2, dtype: str32 Finally, Pandas has arrays that mostly overlap with NumPy * :class:`arrays.DatetimeArray` * :class:`arrays.TimedeltaArray` When data with a ``datetime64[ns]`` or ``timedelta64[ns]`` dtype is passed, pandas will always return a ``DatetimeArray`` or ``TimedeltaArray`` rather than a ``PandasArray``. This is for symmetry with the case of timezone-aware data, which NumPy does not natively support. >>> pd.array(['2015', '2016'], dtype='datetime64[ns]') <DatetimeArray> ['2015-01-01 00:00:00', '2016-01-01 00:00:00'] Length: 2, dtype: datetime64[ns] >>> pd.array(["1H", "2H"], dtype='timedelta64[ns]') <TimedeltaArray> ['0 days 01:00:00', '0 days 02:00:00'] Length: 2, dtype: timedelta64[ns] Examples -------- If a dtype is not specified, pandas will infer the best dtype from the values. See the description of `dtype` for the types pandas infers for. >>> pd.array([1, 2]) <IntegerArray> [1, 2] Length: 2, dtype: Int64 >>> pd.array([1, 2, np.nan]) <IntegerArray> [1, 2, <NA>] Length: 3, dtype: Int64 >>> pd.array([1.1, 2.2]) <FloatingArray> [1.1, 2.2] Length: 2, dtype: Float64 >>> pd.array(["a", None, "c"]) <StringArray> ['a', <NA>, 'c'] Length: 3, dtype: string >>> pd.array([pd.Period('2000', freq="D"), pd.Period("2000", freq="D")]) <PeriodArray> ['2000-01-01', '2000-01-01'] Length: 2, dtype: period[D] You can use the string alias for `dtype` >>> pd.array(['a', 'b', 'a'], dtype='category') ['a', 'b', 'a'] Categories (2, object): ['a', 'b'] Or specify the actual dtype >>> pd.array(['a', 'b', 'a'], ... dtype=pd.CategoricalDtype(['a', 'b', 'c'], ordered=True)) ['a', 'b', 'a'] Categories (3, object): ['a' < 'b' < 'c'] If pandas does not infer a dedicated extension type a :class:`arrays.PandasArray` is returned. >>> pd.array([1 + 1j, 3 + 2j]) <PandasArray> [(1+1j), (3+2j)] Length: 2, dtype: complex128 As mentioned in the "Notes" section, new extension types may be added in the future (by pandas or 3rd party libraries), causing the return value to no longer be a :class:`arrays.PandasArray`. Specify the `dtype` as a NumPy dtype if you need to ensure there's no future change in behavior. >>> pd.array([1, 2], dtype=np.dtype("int32")) <PandasArray> [1, 2] Length: 2, dtype: int32 `data` must be 1-dimensional. A ValueError is raised when the input has the wrong dimensionality. >>> pd.array(1) Traceback (most recent call last): ... ValueError: Cannot pass scalar '1' to 'pandas.array'. """ from pandas.core.arrays import ( BooleanArray, DatetimeArray, FloatingArray, IntegerArray, IntervalArray, PandasArray, StringArray, TimedeltaArray, period_array, ) if lib.is_scalar(data): msg = f"Cannot pass scalar '{data}' to 'pandas.array'." raise ValueError(msg) if dtype is None and isinstance( data, (ABCSeries, ABCIndexClass, ABCExtensionArray) ): dtype = data.dtype data = extract_array(data, extract_numpy=True) # this returns None for not-found dtypes. if isinstance(dtype, str): dtype = registry.find(dtype) or dtype if is_extension_array_dtype(dtype): cls = cast(ExtensionDtype, dtype).construct_array_type() return cls._from_sequence(data, dtype=dtype, copy=copy) if dtype is None: inferred_dtype = lib.infer_dtype(data, skipna=True) if inferred_dtype == "period": try: return period_array(data, copy=copy) except IncompatibleFrequency: # We may have a mixture of frequencies. # We choose to return an ndarray, rather than raising. pass elif inferred_dtype == "interval": try: return IntervalArray(data, copy=copy) except ValueError: # We may have a mixture of `closed` here. # We choose to return an ndarray, rather than raising. pass elif inferred_dtype.startswith("datetime"): # datetime, datetime64 try: return DatetimeArray._from_sequence(data, copy=copy) except ValueError: # Mixture of timezones, fall back to PandasArray pass elif inferred_dtype.startswith("timedelta"): # timedelta, timedelta64 return TimedeltaArray._from_sequence(data, copy=copy) elif inferred_dtype == "string": return StringArray._from_sequence(data, copy=copy) elif inferred_dtype == "integer": return IntegerArray._from_sequence(data, copy=copy) elif inferred_dtype in ("floating", "mixed-integer-float"): return FloatingArray._from_sequence(data, copy=copy) elif inferred_dtype == "boolean": return BooleanArray._from_sequence(data, copy=copy) # Pandas overrides NumPy for # 1. datetime64[ns] # 2. timedelta64[ns] # so that a DatetimeArray is returned. if is_datetime64_ns_dtype(dtype): return DatetimeArray._from_sequence(data, dtype=dtype, copy=copy) elif is_timedelta64_ns_dtype(dtype): return TimedeltaArray._from_sequence(data, dtype=dtype, copy=copy) result = PandasArray._from_sequence(data, dtype=dtype, copy=copy) return result def extract_array(obj: AnyArrayLike, extract_numpy: bool = False) -> ArrayLike: """ Extract the ndarray or ExtensionArray from a Series or Index. For all other types, `obj` is just returned as is. Parameters ---------- obj : object For Series / Index, the underlying ExtensionArray is unboxed. For Numpy-backed ExtensionArrays, the ndarray is extracted. extract_numpy : bool, default False Whether to extract the ndarray from a PandasArray Returns ------- arr : object Examples -------- >>> extract_array(pd.Series(['a', 'b', 'c'], dtype='category')) ['a', 'b', 'c'] Categories (3, object): ['a', 'b', 'c'] Other objects like lists, arrays, and DataFrames are just passed through. >>> extract_array([1, 2, 3]) [1, 2, 3] For an ndarray-backed Series / Index a PandasArray is returned. >>> extract_array(pd.Series([1, 2, 3])) <PandasArray> [1, 2, 3] Length: 3, dtype: int64 To extract all the way down to the ndarray, pass ``extract_numpy=True``. >>> extract_array(pd.Series([1, 2, 3]), extract_numpy=True) array([1, 2, 3]) """ if isinstance(obj, (ABCIndexClass, ABCSeries)): obj = obj.array if extract_numpy and isinstance(obj, ABCPandasArray): obj = obj.to_numpy() # error: Incompatible return value type (got "Index", expected "ExtensionArray") # error: Incompatible return value type (got "Series", expected "ExtensionArray") return obj # type: ignore[return-value] def sanitize_array( data, index: Optional[Index], dtype: Optional[DtypeObj] = None, copy: bool = False, raise_cast_failure: bool = False, ) -> ArrayLike: """ Sanitize input data to an ndarray or ExtensionArray, copy if specified, coerce to the dtype if specified. """ if isinstance(data, ma.MaskedArray): mask = ma.getmaskarray(data) if mask.any(): data, fill_value = maybe_upcast(data, copy=True) data.soften_mask() # set hardmask False if it was True data[mask] = fill_value else: data = data.copy() # extract ndarray or ExtensionArray, ensure we have no PandasArray data = extract_array(data, extract_numpy=True) # GH#846 if isinstance(data, np.ndarray): if dtype is not None and is_float_dtype(data.dtype) and is_integer_dtype(dtype): # possibility of nan -> garbage try: subarr = _try_cast(data, dtype, copy, True) except ValueError: if copy: subarr = data.copy() else: subarr = np.array(data, copy=False) else: # we will try to copy be-definition here subarr = _try_cast(data, dtype, copy, raise_cast_failure) elif isinstance(data, ABCExtensionArray): # it is already ensured above this is not a PandasArray subarr = data if dtype is not None: subarr = subarr.astype(dtype, copy=copy) elif copy: subarr = subarr.copy() return subarr elif isinstance(data, (list, tuple, abc.Set, abc.ValuesView)) and len(data) > 0: if isinstance(data, set): # Raise only for unordered sets, e.g., not for dict_keys raise TypeError("Set type is unordered") data = list(data) if dtype is not None: subarr = _try_cast(data, dtype, copy, raise_cast_failure) else: subarr = maybe_convert_platform(data) subarr = maybe_cast_to_datetime(subarr, dtype) elif isinstance(data, range): # GH#16804 arr = np.arange(data.start, data.stop, data.step, dtype="int64") subarr = _try_cast(arr, dtype, copy, raise_cast_failure) elif lib.is_scalar(data) and index is not None and dtype is not None: data = maybe_cast_to_datetime(data, dtype) if not lib.is_scalar(data): data = data[0] subarr = construct_1d_arraylike_from_scalar(data, len(index), dtype) else: subarr = _try_cast(data, dtype, copy, raise_cast_failure) # scalar like, GH if getattr(subarr, "ndim", 0) == 0: if isinstance(data, list): # pragma: no cover subarr = np.array(data, dtype=object) elif index is not None: value = data # figure out the dtype from the value (upcast if necessary) if dtype is None: dtype, value = infer_dtype_from_scalar(value, pandas_dtype=True) else: # need to possibly convert the value here value = maybe_cast_to_datetime(value, dtype) subarr = construct_1d_arraylike_from_scalar(value, len(index), dtype) else: return subarr.item() # the result that we want elif subarr.ndim == 1: if index is not None: # a 1-element ndarray if len(subarr) != len(index) and len(subarr) == 1: subarr = construct_1d_arraylike_from_scalar( subarr[0], len(index), subarr.dtype ) elif subarr.ndim > 1: if isinstance(data, np.ndarray): raise ValueError("Data must be 1-dimensional") else: subarr = com.asarray_tuplesafe(data, dtype=dtype) if not (is_extension_array_dtype(subarr.dtype) or is_extension_array_dtype(dtype)): # This is to prevent mixed-type Series getting all casted to # NumPy string type, e.g. NaN --> '-1#IND'. if issubclass(subarr.dtype.type, str): # GH#16605 # If not empty convert the data to dtype # GH#19853: If data is a scalar, subarr has already the result if not lib.is_scalar(data): if not np.all(isna(data)): data = np.array(data, dtype=dtype, copy=False) subarr = np.array(data, dtype=object, copy=copy) is_object_or_str_dtype = is_object_dtype(dtype) or is_string_dtype(dtype) if is_object_dtype(subarr.dtype) and not is_object_or_str_dtype: inferred = lib.infer_dtype(subarr, skipna=False) if inferred in {"interval", "period"}: subarr = array(subarr) return subarr def _try_cast(arr, dtype: Optional[DtypeObj], copy: bool, raise_cast_failure: bool): """ Convert input to numpy ndarray and optionally cast to a given dtype. Parameters ---------- arr : ndarray, scalar, list, tuple, iterator (catchall) Excludes: ExtensionArray, Series, Index. dtype : np.dtype, ExtensionDtype or None copy : bool If False, don't copy the data if not needed. raise_cast_failure : bool If True, and if a dtype is specified, raise errors during casting. Otherwise an object array is returned. """ # perf shortcut as this is the most common case if isinstance(arr, np.ndarray): if maybe_castable(arr) and not copy and dtype is None: return arr if isinstance(dtype, ExtensionDtype) and (dtype.kind != "M" or is_sparse(dtype)): # create an extension array from its dtype # DatetimeTZ case needs to go through maybe_cast_to_datetime but # SparseDtype does not array_type = dtype.construct_array_type()._from_sequence subarr = array_type(arr, dtype=dtype, copy=copy) return subarr try: # GH#15832: Check if we are requesting a numeric dtype and # that we can convert the data to the requested dtype. if is_integer_dtype(dtype): # this will raise if we have e.g. floats maybe_cast_to_integer_array(arr, dtype) subarr = arr else: subarr = maybe_cast_to_datetime(arr, dtype) # Take care in creating object arrays (but iterators are not # supported): if is_object_dtype(dtype) and ( is_list_like(subarr) and not (is_iterator(subarr) or isinstance(subarr, np.ndarray)) ): subarr = construct_1d_object_array_from_listlike(subarr) elif not is_extension_array_dtype(subarr): subarr = construct_1d_ndarray_preserving_na(subarr, dtype, copy=copy) except OutOfBoundsDatetime: # in case of out of bound datetime64 -> always raise raise except (ValueError, TypeError): if dtype is not None and raise_cast_failure: raise else: subarr = np.array(arr, dtype=object, copy=copy) return subarr def is_empty_data(data: Any) -> bool: """ Utility to check if a Series is instantiated with empty data, which does not contain dtype information. Parameters ---------- data : array-like, Iterable, dict, or scalar value Contains data stored in Series. Returns ------- bool """ is_none = data is None is_list_like_without_dtype = is_list_like(data) and not hasattr(data, "dtype") is_simple_empty = is_list_like_without_dtype and not data return is_none or is_simple_empty def create_series_with_explicit_dtype( data: Any = None, index: Optional[Union[ArrayLike, Index]] = None, dtype: Optional[Dtype] = None, name: Optional[str] = None, copy: bool = False, fastpath: bool = False, dtype_if_empty: Dtype = object, ) -> Series: """ Helper to pass an explicit dtype when instantiating an empty Series. This silences a DeprecationWarning described in GitHub-17261. Parameters ---------- data : Mirrored from Series.__init__ index : Mirrored from Series.__init__ dtype : Mirrored from Series.__init__ name : Mirrored from Series.__init__ copy : Mirrored from Series.__init__ fastpath : Mirrored from Series.__init__ dtype_if_empty : str, numpy.dtype, or ExtensionDtype This dtype will be passed explicitly if an empty Series will be instantiated. Returns ------- Series """ from pandas.core.series import Series if is_empty_data(data) and dtype is None: dtype = dtype_if_empty return Series( data=data, index=index, dtype=dtype, name=name, copy=copy, fastpath=fastpath )
pandas/core/construction.py
21,927
Convert input to numpy ndarray and optionally cast to a given dtype. Parameters ---------- arr : ndarray, scalar, list, tuple, iterator (catchall) Excludes: ExtensionArray, Series, Index. dtype : np.dtype, ExtensionDtype or None copy : bool If False, don't copy the data if not needed. raise_cast_failure : bool If True, and if a dtype is specified, raise errors during casting. Otherwise an object array is returned. Create an array. .. versionadded:: 0.24.0 Parameters ---------- data : Sequence of objects The scalars inside `data` should be instances of the scalar type for `dtype`. It's expected that `data` represents a 1-dimensional array of data. When `data` is an Index or Series, the underlying array will be extracted from `data`. dtype : str, np.dtype, or ExtensionDtype, optional The dtype to use for the array. This may be a NumPy dtype or an extension type registered with pandas using :meth:`pandas.api.extensions.register_extension_dtype`. If not specified, there are two possibilities: 1. When `data` is a :class:`Series`, :class:`Index`, or :class:`ExtensionArray`, the `dtype` will be taken from the data. 2. Otherwise, pandas will attempt to infer the `dtype` from the data. Note that when `data` is a NumPy array, ``data.dtype`` is *not* used for inferring the array type. This is because NumPy cannot represent all the types of data that can be held in extension arrays. Currently, pandas will infer an extension dtype for sequences of ============================== ===================================== Scalar Type Array Type ============================== ===================================== :class:`pandas.Interval` :class:`pandas.arrays.IntervalArray` :class:`pandas.Period` :class:`pandas.arrays.PeriodArray` :class:`datetime.datetime` :class:`pandas.arrays.DatetimeArray` :class:`datetime.timedelta` :class:`pandas.arrays.TimedeltaArray` :class:`int` :class:`pandas.arrays.IntegerArray` :class:`float` :class:`pandas.arrays.FloatingArray` :class:`str` :class:`pandas.arrays.StringArray` :class:`bool` :class:`pandas.arrays.BooleanArray` ============================== ===================================== For all other cases, NumPy's usual inference rules will be used. .. versionchanged:: 1.0.0 Pandas infers nullable-integer dtype for integer data, string dtype for string data, and nullable-boolean dtype for boolean data. .. versionchanged:: 1.2.0 Pandas now also infers nullable-floating dtype for float-like input data copy : bool, default True Whether to copy the data, even if not necessary. Depending on the type of `data`, creating the new array may require copying data, even if ``copy=False``. Returns ------- ExtensionArray The newly created array. Raises ------ ValueError When `data` is not 1-dimensional. See Also -------- numpy.array : Construct a NumPy array. Series : Construct a pandas Series. Index : Construct a pandas Index. arrays.PandasArray : ExtensionArray wrapping a NumPy array. Series.array : Extract the array stored within a Series. Notes ----- Omitting the `dtype` argument means pandas will attempt to infer the best array type from the values in the data. As new array types are added by pandas and 3rd party libraries, the "best" array type may change. We recommend specifying `dtype` to ensure that 1. the correct array type for the data is returned 2. the returned array type doesn't change as new extension types are added by pandas and third-party libraries Additionally, if the underlying memory representation of the returned array matters, we recommend specifying the `dtype` as a concrete object rather than a string alias or allowing it to be inferred. For example, a future version of pandas or a 3rd-party library may include a dedicated ExtensionArray for string data. In this event, the following would no longer return a :class:`arrays.PandasArray` backed by a NumPy array. >>> pd.array(['a', 'b'], dtype=str) <PandasArray> ['a', 'b'] Length: 2, dtype: str32 This would instead return the new ExtensionArray dedicated for string data. If you really need the new array to be backed by a NumPy array, specify that in the dtype. >>> pd.array(['a', 'b'], dtype=np.dtype("<U1")) <PandasArray> ['a', 'b'] Length: 2, dtype: str32 Finally, Pandas has arrays that mostly overlap with NumPy * :class:`arrays.DatetimeArray` * :class:`arrays.TimedeltaArray` When data with a ``datetime64[ns]`` or ``timedelta64[ns]`` dtype is passed, pandas will always return a ``DatetimeArray`` or ``TimedeltaArray`` rather than a ``PandasArray``. This is for symmetry with the case of timezone-aware data, which NumPy does not natively support. >>> pd.array(['2015', '2016'], dtype='datetime64[ns]') <DatetimeArray> ['2015-01-01 00:00:00', '2016-01-01 00:00:00'] Length: 2, dtype: datetime64[ns] >>> pd.array(["1H", "2H"], dtype='timedelta64[ns]') <TimedeltaArray> ['0 days 01:00:00', '0 days 02:00:00'] Length: 2, dtype: timedelta64[ns] Examples -------- If a dtype is not specified, pandas will infer the best dtype from the values. See the description of `dtype` for the types pandas infers for. >>> pd.array([1, 2]) <IntegerArray> [1, 2] Length: 2, dtype: Int64 >>> pd.array([1, 2, np.nan]) <IntegerArray> [1, 2, <NA>] Length: 3, dtype: Int64 >>> pd.array([1.1, 2.2]) <FloatingArray> [1.1, 2.2] Length: 2, dtype: Float64 >>> pd.array(["a", None, "c"]) <StringArray> ['a', <NA>, 'c'] Length: 3, dtype: string >>> pd.array([pd.Period('2000', freq="D"), pd.Period("2000", freq="D")]) <PeriodArray> ['2000-01-01', '2000-01-01'] Length: 2, dtype: period[D] You can use the string alias for `dtype` >>> pd.array(['a', 'b', 'a'], dtype='category') ['a', 'b', 'a'] Categories (2, object): ['a', 'b'] Or specify the actual dtype >>> pd.array(['a', 'b', 'a'], ... dtype=pd.CategoricalDtype(['a', 'b', 'c'], ordered=True)) ['a', 'b', 'a'] Categories (3, object): ['a' < 'b' < 'c'] If pandas does not infer a dedicated extension type a :class:`arrays.PandasArray` is returned. >>> pd.array([1 + 1j, 3 + 2j]) <PandasArray> [(1+1j), (3+2j)] Length: 2, dtype: complex128 As mentioned in the "Notes" section, new extension types may be added in the future (by pandas or 3rd party libraries), causing the return value to no longer be a :class:`arrays.PandasArray`. Specify the `dtype` as a NumPy dtype if you need to ensure there's no future change in behavior. >>> pd.array([1, 2], dtype=np.dtype("int32")) <PandasArray> [1, 2] Length: 2, dtype: int32 `data` must be 1-dimensional. A ValueError is raised when the input has the wrong dimensionality. >>> pd.array(1) Traceback (most recent call last): ... ValueError: Cannot pass scalar '1' to 'pandas.array'. Helper to pass an explicit dtype when instantiating an empty Series. This silences a DeprecationWarning described in GitHub-17261. Parameters ---------- data : Mirrored from Series.__init__ index : Mirrored from Series.__init__ dtype : Mirrored from Series.__init__ name : Mirrored from Series.__init__ copy : Mirrored from Series.__init__ fastpath : Mirrored from Series.__init__ dtype_if_empty : str, numpy.dtype, or ExtensionDtype This dtype will be passed explicitly if an empty Series will be instantiated. Returns ------- Series Extract the ndarray or ExtensionArray from a Series or Index. For all other types, `obj` is just returned as is. Parameters ---------- obj : object For Series / Index, the underlying ExtensionArray is unboxed. For Numpy-backed ExtensionArrays, the ndarray is extracted. extract_numpy : bool, default False Whether to extract the ndarray from a PandasArray Returns ------- arr : object Examples -------- >>> extract_array(pd.Series(['a', 'b', 'c'], dtype='category')) ['a', 'b', 'c'] Categories (3, object): ['a', 'b', 'c'] Other objects like lists, arrays, and DataFrames are just passed through. >>> extract_array([1, 2, 3]) [1, 2, 3] For an ndarray-backed Series / Index a PandasArray is returned. >>> extract_array(pd.Series([1, 2, 3])) <PandasArray> [1, 2, 3] Length: 3, dtype: int64 To extract all the way down to the ndarray, pass ``extract_numpy=True``. >>> extract_array(pd.Series([1, 2, 3]), extract_numpy=True) array([1, 2, 3]) Utility to check if a Series is instantiated with empty data, which does not contain dtype information. Parameters ---------- data : array-like, Iterable, dict, or scalar value Contains data stored in Series. Returns ------- bool Sanitize input data to an ndarray or ExtensionArray, copy if specified, coerce to the dtype if specified. Constructor functions intended to be shared by pd.array, Series.__init__, and Index.__new__. These should not depend on core.internals. this returns None for not-found dtypes. We may have a mixture of frequencies. We choose to return an ndarray, rather than raising. We may have a mixture of `closed` here. We choose to return an ndarray, rather than raising. datetime, datetime64 Mixture of timezones, fall back to PandasArray timedelta, timedelta64 Pandas overrides NumPy for 1. datetime64[ns] 2. timedelta64[ns] so that a DatetimeArray is returned. error: Incompatible return value type (got "Index", expected "ExtensionArray") error: Incompatible return value type (got "Series", expected "ExtensionArray") type: ignore[return-value] set hardmask False if it was True extract ndarray or ExtensionArray, ensure we have no PandasArray GH846 possibility of nan -> garbage we will try to copy be-definition here it is already ensured above this is not a PandasArray Raise only for unordered sets, e.g., not for dict_keys GH16804 scalar like, GH pragma: no cover figure out the dtype from the value (upcast if necessary) need to possibly convert the value here the result that we want a 1-element ndarray This is to prevent mixed-type Series getting all casted to NumPy string type, e.g. NaN --> '-1IND'. GH16605 If not empty convert the data to dtype GH19853: If data is a scalar, subarr has already the result perf shortcut as this is the most common case create an extension array from its dtype DatetimeTZ case needs to go through maybe_cast_to_datetime but SparseDtype does not GH15832: Check if we are requesting a numeric dtype and that we can convert the data to the requested dtype. this will raise if we have e.g. floats Take care in creating object arrays (but iterators are not supported): in case of out of bound datetime64 -> always raise
10,703
en
0.601845
""" DuckDuckGo (Images) @website https://duckduckgo.com/ @provide-api yes (https://duckduckgo.com/api), but images are not supported @using-api no @results JSON (site requires js to get images) @stable no (JSON can change) @parse url, title, img_src @todo avoid extra request """ from json import loads from searx.engines.xpath import extract_text from searx.engines.duckduckgo import ( _fetch_supported_languages, supported_languages_url, get_region_code, language_aliases ) from searx.poolrequests import get from searx.url_utils import urlencode # engine dependent config categories = ['images'] paging = True language_support = True safesearch = True # search-url images_url = 'https://duckduckgo.com/i.js?{query}&s={offset}&p={safesearch}&o=json&vqd={vqd}' site_url = 'https://duckduckgo.com/?{query}&iar=images&iax=1&ia=images' # run query in site to get vqd number needed for requesting images # TODO: find a way to get this number without an extra request (is it a hash of the query?) def get_vqd(query): res = get(site_url.format(query=urlencode({'q': query}))) content = res.text vqd = content[content.find('vqd=\'') + 5:] vqd = vqd[:vqd.find('\'')] return vqd # do search-request def request(query, params): # to avoid running actual external requests when testing if 'is_test' not in params: vqd = get_vqd(query) else: vqd = '12345' offset = (params['pageno'] - 1) * 50 safesearch = params['safesearch'] - 1 region_code = get_region_code(params['language'], lang_list=supported_languages) params['url'] = images_url.format( query=urlencode({'q': query, 'l': region_code}), offset=offset, safesearch=safesearch, vqd=vqd) return params # get response from search-request def response(resp): results = [] content = resp.text try: res_json = loads(content) except: return [] # parse results for result in res_json['results']: title = result['title'] url = result['url'] thumbnail = result['thumbnail'] image = result['image'] # append result results.append({'template': 'images.html', 'title': title, 'content': '', 'thumbnail_src': thumbnail, 'img_src': image, 'url': url}) return results
master/searx-master/searx/engines/duckduckgo_images.py
2,464
DuckDuckGo (Images) @website https://duckduckgo.com/ @provide-api yes (https://duckduckgo.com/api), but images are not supported @using-api no @results JSON (site requires js to get images) @stable no (JSON can change) @parse url, title, img_src @todo avoid extra request engine dependent config search-url run query in site to get vqd number needed for requesting images TODO: find a way to get this number without an extra request (is it a hash of the query?) do search-request to avoid running actual external requests when testing get response from search-request parse results append result
641
en
0.619775
# -*- coding: utf-8 -*- # Copyright (c) 2020, Havenir and contributors # For license information, please see license.txt from __future__ import unicode_literals # import frappe from frappe.model.document import Document class PortOfLoading(Document): pass
shipments/shipments/doctype/port_of_loading/port_of_loading.py
259
-*- coding: utf-8 -*- Copyright (c) 2020, Havenir and contributors For license information, please see license.txt import frappe
128
en
0.68789
import os import sys import argparse import numpy as np from PIL import Image import matplotlib.pyplot as plt import torch import torch.nn as nn import torch.nn.functional as F from torch.optim import Adam from torch.utils.data import Dataset import torchvision.transforms as transforms import pickle def normalize(image): return (image - image.min()) / (image.max() - image.min()) layer_activations = None def filter_explanation(x, model, cnnid, filterid, iteration=100, lr=1): # x: 需要训练的图片 # cnnid, filterid: 指定第几层cnn中第几个filter model.eval() def hook(model, input, output): global layer_activations layer_activations = output hook_handle = model.cnn[cnnid].register_forward_hook(hook) # 当forward了第cnnid层cnn后, 要先呼叫hook, 才可以继续forward下一层cnn # Filter activation: 我们先观察x经过被指定filter的activation map model(x.cuda()) # 正式执行forward的步骤 filter_activations = layer_activations[:, filterid, :, :].detach().cpu() # 根据function argument 指定的filterid把待定filter的activation map取出来 x = x.cuda() x.requires_grad_() optimizer = Adam([x], lr=lr) # 利用偏微分和optimizer, 逐步修改input image来让filter activation越来越大 for iter in range(iteration): optimizer.zero_grad() model(x) objective = -layer_activations[:, filterid, :, :].sum() # 探究image的微量变化会怎样影响activation的程度,加负号代表做maximization objective.backward() optimizer.step() # 修改input image来最大化filter activation filter_visualization = x.detach().cpu().squeeze()[0] # 完成图片修改,只剩下要画出来,因此可以直接detach并转成cpu tensor hook_handle.remove() # 一旦model register hook, 该hook就一致存在。如果之后继续register更多hook # 那model一次forward要做的事情就越来越来越多,因此需要把hook拿掉 return filter_activations, filter_visualization
CNN/code/filter_visualiton.py
2,173
x: 需要训练的图片 cnnid, filterid: 指定第几层cnn中第几个filter 当forward了第cnnid层cnn后, 要先呼叫hook, 才可以继续forward下一层cnn Filter activation: 我们先观察x经过被指定filter的activation map 正式执行forward的步骤 根据function argument 指定的filterid把待定filter的activation map取出来 利用偏微分和optimizer, 逐步修改input image来让filter activation越来越大 探究image的微量变化会怎样影响activation的程度,加负号代表做maximization 修改input image来最大化filter activation 完成图片修改,只剩下要画出来,因此可以直接detach并转成cpu tensor 一旦model register hook, 该hook就一致存在。如果之后继续register更多hook 那model一次forward要做的事情就越来越来越多,因此需要把hook拿掉
500
zh
0.741019
from functools import wraps import sys import traceback from ploomber.io import TerminalWriter from ploomber.exceptions import DAGBuildError, DAGRenderError # TODO: there are two types of cli commands: the ones that execute user's # code (ploomber build/task) and the ones that parse a dag/task but do not # execute it. For the former, we want to capture errors and display them with # colors so it's easier for the user to understand what went wrong with their # code. For the latter, the errors are raise by us, hence, we only need to # print the message and exit. Currently, all CLI end points (except ploomber # nb) are decorated with @cli_endpoint but we should change it to # @command_endpoint def cli_endpoint(fn): """ Decorator for command line endpoints that execute dags or tasks. It runs the decorated function, captures exception (if any), sends a colored traceback to standard error and exits with code 1. Notes ----- Functions decorated with this must be called with keyword arguments Call some_endpoint(catch_exception=False) to disable this behavior (e.g. for testing) """ @wraps(fn) def wrapper(catch_exception=True, **kwargs): if catch_exception: try: fn(**kwargs) # these already color output except (DAGBuildError, DAGRenderError): error = traceback.format_exc() color = False except Exception: error = traceback.format_exc() color = True else: error = None if error: if color: tw = TerminalWriter(file=sys.stderr) tw._write_source(error.splitlines()) else: print(error, file=sys.stderr) sys.exit(1) else: fn(**kwargs) return wrapper # FIXME: capture only certain types of exceptions. If it's something we dind't # raise, we'd like to see the full traceback def command_endpoint(fn): """ Decorator for command line endpoints that only parse dags or tasks but do not execute them. If it tails, it prints error message to stderror, then calls with exit code 1. """ @wraps(fn) def wrapper(**kwargs): try: fn(**kwargs) except Exception as e: print(f'Error: {e}', file=sys.stderr) sys.exit(1) return wrapper
src/ploomber/cli/io.py
2,474
Decorator for command line endpoints that execute dags or tasks. It runs the decorated function, captures exception (if any), sends a colored traceback to standard error and exits with code 1. Notes ----- Functions decorated with this must be called with keyword arguments Call some_endpoint(catch_exception=False) to disable this behavior (e.g. for testing) Decorator for command line endpoints that only parse dags or tasks but do not execute them. If it tails, it prints error message to stderror, then calls with exit code 1. TODO: there are two types of cli commands: the ones that execute user's code (ploomber build/task) and the ones that parse a dag/task but do not execute it. For the former, we want to capture errors and display them with colors so it's easier for the user to understand what went wrong with their code. For the latter, the errors are raise by us, hence, we only need to print the message and exit. Currently, all CLI end points (except ploomber nb) are decorated with @cli_endpoint but we should change it to @command_endpoint these already color output FIXME: capture only certain types of exceptions. If it's something we dind't raise, we'd like to see the full traceback
1,207
en
0.892736
import json import os import shutil import tempfile def copytree(src, dst, symlinks=False, ignore=None): for item in os.listdir(src): s = os.path.join(src, item) d = os.path.join(dst, item) if os.path.isdir(s): shutil.copytree(s, d, symlinks, ignore) else: shutil.copy2(s, d) def call(command, ignore_error=False): ret = os.system(command) if ret != 0 and not ignore_error: raise Exception("Command failed: %s" % command) def clean_gh_pages(): call('git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" 1>/dev/null') call("git fetch origin -q") call("git checkout gh-pages") if os.path.exists("en"): shutil.rmtree("en") def build_and_copy(branch, folder_name, versions_available, themes_dir, validate_links=False): call("git checkout %s" % branch) call("git pull origin %s" % branch) with open('versions.json', 'w') as f: f.write(json.dumps(versions_available)) shutil.rmtree("_themes") copytree(themes_dir, "_themes") call("make html > /dev/null") if validate_links: call("make spelling > /dev/null") call("make linkcheck") call("make latexpdf > /dev/null") tmp_dir = tempfile.mkdtemp() copytree("_build/html/", tmp_dir) shutil.copy2("_build/latex/conan.pdf", tmp_dir) shutil.rmtree("_build") # Go to deploy branch, copy new files and commit call("git stash") call("git stash drop || true") call("git clean -d -f") call("git checkout gh-pages") if not os.path.exists("en"): os.mkdir("en") version_folders = ["en/%s" % folder_name] if branch == "master": version_folders.append("en/latest") for version_folder in version_folders: if os.path.exists(version_folder): shutil.rmtree(version_folder) os.mkdir(version_folder) copytree(tmp_dir, version_folder) call("git add -A .") call("git commit --message 'committed version %s'" % folder_name, ignore_error=True) def should_deploy(): if not os.getenv("TRAVIS_BRANCH", None) == "master": print("Skipping deploy for not master branch") return False if os.getenv("TRAVIS_PULL_REQUEST", "") != "false": print("Deploy skipped, This is a PR in the main repository") return False if not os.getenv("GITHUB_API_KEY"): print("Deploy skipped, missing GITHUB_API_KEY. Is this a PR?") return False return True def deploy(): call('rm -rf .git') call('git init .') call('git add .') call('git checkout -b gh-pages') call('git commit -m "Cleared web"') call('git remote add origin-pages ' 'https://%s@github.com/conan-io/docs.git > /dev/null 2>&1' % os.getenv("GITHUB_API_KEY")) call('git push origin-pages gh-pages --force') if __name__ == "__main__": if should_deploy(): # Copy the _themes to be able to share them between old versions themes_dir = tempfile.mkdtemp() copytree("_themes", themes_dir) clean_gh_pages() versions_dict = {"master": "1.25", "release/1.24.1": "1.24", "release/1.23.0": "1.23", "release/1.22.3": "1.22", "release/1.21.3": "1.21", "release/1.20.5": "1.20", "release/1.19.3": "1.19", "release/1.18.5": "1.18", "release/1.17.2": "1.17", "release/1.16.1": "1.16", "release/1.15.2": "1.15", "release/1.14.5": "1.14", "release/1.13.3": "1.13", "release/1.12.3": "1.12", "release/1.11.2": "1.11", "release/1.10.2": "1.10", "release/1.9.4": "1.9", "release/1.8.4": "1.8", "release/1.7.4": "1.7", "release/1.6.1": "1.6", "release/1.5.2": "1.5", "release/1.4.5": "1.4", "release/1.3.3": "1.3"} for branch, folder_name in versions_dict.items(): print("Building {}...".format(branch)) build_and_copy(branch, folder_name, versions_dict, themes_dir) deploy() else: call("make html > /dev/null") call("make spelling") call("make linkcheck")
deploy_gh_pages.py
4,573
Go to deploy branch, copy new files and commit Copy the _themes to be able to share them between old versions
109
en
0.955091
# -*- coding: utf-8 -*- """爬虫配置文件""" import os # MYSQL MYSQL_IP = "localhost" MYSQL_PORT = 3306 MYSQL_DB = "feapder" MYSQL_USER_NAME = "feapder" MYSQL_USER_PASS = "feapder123" # REDIS # IP:PORT REDISDB_IP_PORTS = "localhost:6379" REDISDB_USER_PASS = "" REDISDB_DB = 0 # # 爬虫相关 # # COLLECTOR COLLECTOR_SLEEP_TIME = 1 # 从任务队列中获取任务到内存队列的间隔 COLLECTOR_TASK_COUNT = 100 # 每次获取任务数量 # # # SPIDER SPIDER_THREAD_COUNT = 1 # 爬虫并发数 # SPIDER_SLEEP_TIME = 0 # 下载时间间隔(解析完一个response后休眠时间) # SPIDER_MAX_RETRY_TIMES = 100 # 每个请求最大重试次数 # # 重新尝试失败的requests 当requests重试次数超过允许的最大重试次数算失败 # RETRY_FAILED_REQUESTS = False # # request 超时时间,超过这个时间重新做(不是网络请求的超时时间)单位秒 # REQUEST_LOST_TIMEOUT = 600 # 10分钟 # # 保存失败的request # SAVE_FAILED_REQUEST = True # # # 下载缓存 利用redis缓存,由于内存小,所以仅供测试时使用 # RESPONSE_CACHED_ENABLE = False # 是否启用下载缓存 成本高的数据或容易变需求的数据,建议设置为True # RESPONSE_CACHED_EXPIRE_TIME = 3600 # 缓存时间 秒 # RESPONSE_CACHED_USED = False # 是否使用缓存 补采数据时可设置为True # # WARNING_FAILED_COUNT = 1000 # 任务失败数 超过WARNING_FAILED_COUNT则报警 # # # 爬虫初始化工作 # # 爬虫做完request后是否自动结束或者等待任务 # AUTO_STOP_WHEN_SPIDER_DONE = True # # # # 设置代理 # PROXY_EXTRACT_API = None # 代理提取API ,返回的代理分割符为\r\n # PROXY_ENABLE = True # # # 随机headers # RANDOM_HEADERS = True # # requests 使用session # USE_SESSION = False # # # 去重 # ITEM_FILTER_ENABLE = False # item 去重 # REQUEST_FILTER_ENABLE = False # request 去重 # # # 报警 # DINGDING_WARNING_URL = "" # 钉钉机器人api # DINGDING_WARNING_PHONE = "" # 报警人 # LINGXI_TOKEN = "" # 灵犀报警token # # LOG_NAME = os.path.basename(os.getcwd()) # LOG_PATH = "log/%s.log" % LOG_NAME # log存储路径 # LOG_LEVEL = "DEBUG" # LOG_IS_WRITE_TO_FILE = False # OTHERS_LOG_LEVAL = "ERROR" # 第三方库的log等级
tests/spider/setting.py
2,239
爬虫配置文件 -*- coding: utf-8 -*- MYSQL REDIS IP:PORT 爬虫相关 COLLECTOR 从任务队列中获取任务到内存队列的间隔 每次获取任务数量 SPIDER 爬虫并发数 SPIDER_SLEEP_TIME = 0 下载时间间隔(解析完一个response后休眠时间) SPIDER_MAX_RETRY_TIMES = 100 每个请求最大重试次数 重新尝试失败的requests 当requests重试次数超过允许的最大重试次数算失败 RETRY_FAILED_REQUESTS = False request 超时时间,超过这个时间重新做(不是网络请求的超时时间)单位秒 REQUEST_LOST_TIMEOUT = 600 10分钟 保存失败的request SAVE_FAILED_REQUEST = True 下载缓存 利用redis缓存,由于内存小,所以仅供测试时使用 RESPONSE_CACHED_ENABLE = False 是否启用下载缓存 成本高的数据或容易变需求的数据,建议设置为True RESPONSE_CACHED_EXPIRE_TIME = 3600 缓存时间 秒 RESPONSE_CACHED_USED = False 是否使用缓存 补采数据时可设置为True WARNING_FAILED_COUNT = 1000 任务失败数 超过WARNING_FAILED_COUNT则报警 爬虫初始化工作 爬虫做完request后是否自动结束或者等待任务 AUTO_STOP_WHEN_SPIDER_DONE = True 设置代理 PROXY_EXTRACT_API = None 代理提取API ,返回的代理分割符为\r\n PROXY_ENABLE = True 随机headers RANDOM_HEADERS = True requests 使用session USE_SESSION = False 去重 ITEM_FILTER_ENABLE = False item 去重 REQUEST_FILTER_ENABLE = False request 去重 报警 DINGDING_WARNING_URL = "" 钉钉机器人api DINGDING_WARNING_PHONE = "" 报警人 LINGXI_TOKEN = "" 灵犀报警token LOG_NAME = os.path.basename(os.getcwd()) LOG_PATH = "log/%s.log" % LOG_NAME log存储路径 LOG_LEVEL = "DEBUG" LOG_IS_WRITE_TO_FILE = False OTHERS_LOG_LEVAL = "ERROR" 第三方库的log等级
1,225
zh
0.580997
""" Small script to generate gdal_warp commands for projecting rasters to the Behrmann projection to be able to run the generated bat file you should have gdalwarp in your path or run it from an OSGeo4W Shell """ import os root = r"D:\a\data\BioOracle_scenarios_30s_min250" output = root + r"_equal_area" #os.path.abspath(os.path.join(root, r'..\ascii_equalarea')) nodata = "-9999" def create_bat(): proj = "+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +ellps=WGS84 +units=m +no_defs" with open('project_to_behrmann.bat', 'w') as bat: for r, dirs, files in os.walk(root): for f in files: n, ext = os.path.splitext(f) if ext == '.asc': ## output of ascii files from gdalwarp is not supported temptiff = os.path.join(output, n + '.tiff') bat.write('gdalwarp -of GTiff -multi -srcnodata %s -dstnodata %s -t_srs "%s" "%s" "%s"\n' % (nodata, proj, os.path.join(r, f), temptiff)) ## convert output tiff to ascii outdir = r.replace(root, output) if not os.path.exists(outdir): os.makedirs(outdir) bat.write('gdal_translate -of AAIGrid "%s" "%s"\n' % (temptiff, os.path.join(outdir,f))) ## delete temp file bat.write('del "%s"\n'%temptiff) if __name__ == '__main__': create_bat()
rasters/project_to_behrmann.py
1,433
Small script to generate gdal_warp commands for projecting rasters to the Behrmann projection to be able to run the generated bat file you should have gdalwarp in your path or run it from an OSGeo4W Shell os.path.abspath(os.path.join(root, r'..\ascii_equalarea')) output of ascii files from gdalwarp is not supported convert output tiff to ascii delete temp file
363
en
0.835444
import sqlite3 from checktheplug.models.Server import Server """ Operations to manage accessing the server database. """ class ServerDao: """ Sets up the object with the sql connection. """ def __init__(self, settings): self.conn = sqlite3.connect(settings.database) """ Add Server to the database. """ def add(self, new_server): if new_server: try: with self.conn: cur = self.conn.cursor() cur.execute("INSERT INTO servers(host, url) values(?, ?)", (new_server.host, new_server.url)) return(Server(cur.lastrowid, new_server.host, new_server.url), None) except sqlite3.IntegrityError as er: return (None, "There was a db issue: " + str(er)) else: return (None, "No server passed in") """ Find all the servers for a particular app. """ def find_by_app_id(self, app_id): try: with self.conn: cur = self.conn.cursor() cur.execute("SELECT id, host, url from servers where app_id = ?", (app_id,)) server_rows = cur.fetchall() return (list(map(lambda x: Server(x[0], x[1], x[2], app_id), server_rows)), None) except Exception as er: return (None, "There was a db issue: " + str(er)) """ Find x number of available servers or all that are available. """ def find_available_servers(self, quantity): try: with self.conn: cur = self.conn.cursor() cur.execute("SELECT id, host, url from servers where app_id = null limit = ?", (quantity,)) server_rows = cur.fetchall() return (list(map(lambda x: Server(x[0], x[1], x[2], None), server_rows)), None) except Exception as er: return (None, "There was a db issue: " + str(er)) """ Retrieve all servers. """ def retrieve_all_servers(self): try: with self.conn: cur = self.conn.cursor() cur.execute("SELECT id, host, url from servers") server_rows = cur.fetchall() return (list(map(lambda x: Server(x[0], x[1], x[2], None), server_rows)), None) except Exception as er: return (None, "There was a db issue: " + str(er)) """ Tie an app to a number of servers. """ def tie_app_to_servers(self, app_id, available_servers): try: with self.conn: cur = self.conn.cursor() server_id_string = ', '.join("?" * available_servers) cur.execute("update servers set app_id = ? where id in ({0})".format(server_id_string), tuple([app_id] + available_servers)) return (None, "ok") except Exception as er: return (None, "There was a db issue: " + str(er))
checktheplug/data/ServerDao.py
2,998
Sets up the object with the sql connection.
43
en
0.859927
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 5/15/20 4:49 PM # @File : grover.py # qubit number=4 # total number=25 import cirq import cirq.google as cg from typing import Optional import sys from math import log2 import numpy as np #thatsNoCode def make_circuit(n: int, input_qubit): c = cirq.Circuit() # circuit begin c.append(cirq.H.on(input_qubit[0])) # number=1 c.append(cirq.H.on(input_qubit[1])) # number=2 c.append(cirq.H.on(input_qubit[1])) # number=7 c.append(cirq.H.on(input_qubit[2])) # number=3 c.append(cirq.H.on(input_qubit[3])) # number=4 c.append(cirq.H.on(input_qubit[0])) # number=18 c.append(cirq.CZ.on(input_qubit[3],input_qubit[0])) # number=19 c.append(cirq.H.on(input_qubit[0])) # number=20 c.append(cirq.CNOT.on(input_qubit[0],input_qubit[2])) # number=22 c.append(cirq.X.on(input_qubit[2])) # number=23 c.append(cirq.CNOT.on(input_qubit[0],input_qubit[2])) # number=24 c.append(cirq.H.on(input_qubit[0])) # number=10 c.append(cirq.CZ.on(input_qubit[3],input_qubit[0])) # number=11 c.append(cirq.H.on(input_qubit[0])) # number=12 c.append(cirq.CNOT.on(input_qubit[2],input_qubit[0])) # number=8 c.append(cirq.H.on(input_qubit[0])) # number=13 c.append(cirq.CZ.on(input_qubit[2],input_qubit[0])) # number=14 c.append(cirq.H.on(input_qubit[0])) # number=15 c.append(cirq.X.on(input_qubit[2])) # number=16 c.append(cirq.X.on(input_qubit[2])) # number=17 # circuit end return c def bitstring(bits): return ''.join(str(int(b)) for b in bits) if __name__ == '__main__': qubit_count = 4 input_qubits = [cirq.GridQubit(i, 0) for i in range(qubit_count)] circuit = make_circuit(qubit_count,input_qubits) circuit = cg.optimized_for_sycamore(circuit, optimizer_type='sqrt_iswap') circuit_sample_count =0 info = cirq.final_state_vector(circuit) qubits = round(log2(len(info))) frequencies = { np.binary_repr(i, qubits): round((info[i]*(info[i].conjugate())).real,3) for i in range(2 ** qubits) } writefile = open("../data/startCirq_Class840.csv","w+") print(format(frequencies),file=writefile) print("results end", file=writefile) print(circuit.__len__(), file=writefile) print(circuit,file=writefile) writefile.close()
data/cirq_new/cirq_program/startCirq_Class840.py
2,341
!/usr/bin/env python -*- coding: utf-8 -*- @Time : 5/15/20 4:49 PM @File : grover.py qubit number=4 total number=25thatsNoCode circuit begin number=1 number=2 number=7 number=3 number=4 number=18 number=19 number=20 number=22 number=23 number=24 number=10 number=11 number=12 number=8 number=13 number=14 number=15 number=16 number=17 circuit end
352
en
0.243248
from seleniumbase import BaseCase from werkzeug.security import generate_password_hash from qa327_test.conftest import base_url from qa327.models import User, Ticket # Mock a sample user TEST_USER = User( email='test_frontend@test.com', name='test_frontend', password=generate_password_hash('test_frontend'), balance=500 ) TEST_USER_SELLER = User( email='test_seller@test.com', name='test_seller', password=generate_password_hash('Password99!'), balance=500 ) # Mock a sample ticket TEST_TICKET = Ticket( name='helloworld', seller=TEST_USER_SELLER, price=20, quantity=20, expires="20220101" ) class GeekBaseCase(BaseCase): ''' Selenium base case with some GeekSeek utilities ''' def assert_flash(self, text): '''asserts that message exists in flashes''' for flash_dom in self.find_elements('.flash'): if flash_dom.text == text: return print(flash_dom.text) raise AssertionError(f'Flash not found for text "{text}"') def login_test_user(self, email=TEST_USER.email, password='test_frontend'): '''login our test user''' self.open(base_url+'/login') self.input('#email', email) self.input('#password', password) self.click('#btn-submit')
qa327_test/frontend/geek_base.py
1,320
Selenium base case with some GeekSeek utilities asserts that message exists in flashes login our test user Mock a sample user Mock a sample ticket
148
en
0.776928
from __future__ import division import numpy as np from numpy.random import rand import pandas as pd # --- List of available filters FILTERS=[ {'name':'Moving average','param':100,'paramName':'Window Size','paramRange':[0,100000],'increment':1}, {'name':'Low pass 1st order','param':1.0,'paramName':'Cutoff Freq.','paramRange':[0.0001,100000],'increment':0.1}, {'name':'High pass 1st order','param':1.0,'paramName':'Cutoff Freq.','paramRange':[0.0001,100000],'increment':0.1}, ] SAMPLERS=[ {'name':'Replace', 'param':[], 'paramName':'New x'}, {'name':'Insert', 'param':[], 'paramName':'Insert list'}, {'name':'Remove', 'param':[], 'paramName':'Remove list'}, {'name':'Every n', 'param':2 , 'paramName':'n'}, {'name':'Delta x', 'param':0.1, 'paramName':'dx'}, ] def reject_outliers(y, x=None, m = 2., replaceNaN=True): """ Reject outliers: If replaceNaN is true: they are replaced by NaN Otherwise they are removed """ if m==0: # No rejection... pass else: dd = np.abs(y - np.nanmedian(y)) mdev = np.nanmedian(dd) if mdev: ss = dd/mdev b=ss<m if replaceNaN: y=y.copy() y[~b]=np.nan else: y=y[b] if x is not None: x= x[b] if x is None: return y else: return x, y # --------------------------------------------------------------------------------} # --- Resampling # --------------------------------------------------------------------------------{ def multiInterp(x, xp, fp, extrap='bounded'): j = np.searchsorted(xp, x) - 1 dd = np.zeros(len(x)) bOK = np.logical_and(j>=0, j< len(xp)-1) bLower =j<0 bUpper =j>=len(xp)-1 jOK = j[bOK] #import pdb; pdb.set_trace() dd[bOK] = (x[bOK] - xp[jOK]) / (xp[jOK + 1] - xp[jOK]) jBef=j jAft=j+1 # # Use first and last values for anything beyond xp jAft[bUpper] = len(xp)-1 jBef[bUpper] = len(xp)-1 jAft[bLower] = 0 jBef[bLower] = 0 if extrap=='bounded': pass # OK elif extrap=='nan': dd[~bOK] = np.nan else: raise NotImplementedError() return (1 - dd) * fp[:,jBef] + fp[:,jAft] * dd def resample_interp(x_old, x_new, y_old=None, df_old=None): #x_new=np.sort(x_new) if df_old is not None: # --- Method 1 (pandas) #df_new = df_old.copy() #df_new = df_new.set_index(x_old) #df_new = df_new.reindex(df_new.index | x_new) #df_new = df_new.interpolate().loc[x_new] #df_new = df_new.reset_index() # --- Method 2 interp storing dx data_new=multiInterp(x_new, x_old, df_old.values.T) df_new = pd.DataFrame(data=data_new.T, columns=df_old.columns.values) return x_new, df_new if y_old is not None: return x_new, np.interp(x_new, x_old, y_old) def applySamplerDF(df_old, x_col, sampDict): x_old=df_old[x_col].values x_new, df_new =applySampler(x_old, y_old=None, sampDict=sampDict, df_old=df_old) df_new[x_col]=x_new return df_new def applySampler(x_old, y_old, sampDict, df_old=None): param = np.asarray(sampDict['param']).ravel() if sampDict['name']=='Replace': if len(param)==0: raise Exception('Error: At least one value is required to resample the x values with') x_new = param return resample_interp(x_old, x_new, y_old, df_old) elif sampDict['name']=='Insert': if len(param)==0: raise Exception('Error: provide a list of values to insert') x_new = np.sort(np.concatenate((x_old.ravel(),param))) return resample_interp(x_old, x_new, y_old, df_old) elif sampDict['name']=='Remove': I=[] if len(param)==0: raise Exception('Error: provide a list of values to remove') for d in param: Ifound= np.where(np.abs(x_old-d)<1e-3)[0] if len(Ifound)>0: I+=list(Ifound.ravel()) x_new=np.delete(x_old,I) return resample_interp(x_old, x_new, y_old, df_old) elif sampDict['name']=='Delta x': if len(param)==0: raise Exception('Error: provide value for dx') dx = param[0] x_new = np.arange(x_old[0], x_old[-1]+dx/2, dx) return resample_interp(x_old, x_new, y_old, df_old) elif sampDict['name']=='Every n': if len(param)==0: raise Exception('Error: provide value for n') n = int(param[0]) if n==0: raise Exception('Error: |n| should be at least 1') x_new=x_old[::n] if df_old is not None: return x_new, (df_old.copy()).iloc[::n,:] if y_old is not None: return x_new, y_old[::n] else: raise NotImplementedError('{}'.format(sampDict)) pass # --------------------------------------------------------------------------------} # --- Filters # --------------------------------------------------------------------------------{ # def moving_average(x, w): # #t_new = np.arange(0,Tmax,dt) # #nt = len(t_new) # #nw=400 # #u_new = moving_average(np.floor(np.linspace(0,3,nt+nw-1))*3+3.5, nw) # return np.convolve(x, np.ones(w), 'valid') / w # def moving_average(x,N,mode='same'): # y=np.convolve(x, np.ones((N,))/N, mode=mode) # return y def moving_average(a, n=3) : """ perform moving average, return a vector of same length as input NOTE: also in kalman.filters """ a = a.ravel() a = np.concatenate(([a[0]]*(n-1),a)) # repeating first values ret = np.cumsum(a, dtype = float) ret[n:] = ret[n:] - ret[:-n] ret=ret[n - 1:] / n return ret def lowpass1(y, dt, fc=3) : """ 1st order low pass filter """ tau=1/(2*np.pi*fc) alpha=dt/(tau+dt) y_filt=np.zeros(y.shape) y_filt[0]=y[0] for i in np.arange(1,len(y)): y_filt[i]=alpha*y[i] + (1-alpha)*y_filt[i-1] return y_filt def highpass1(y, dt, fc=3) : """ 1st order high pass filter """ tau=1/(2*np.pi*fc) alpha=tau/(tau+dt) y_filt=np.zeros(y.shape) y_filt[0]=0 for i in np.arange(1,len(y)): y_filt[i]=alpha*y_filt[i-1] + alpha*(y[i]-y[i-1]) m0=np.mean(y) m1=np.mean(y_filt) y_filt+=m0-m1 return y_filt def applyFilter(x, y,filtDict): if filtDict['name']=='Moving average': return moving_average(y, n=np.round(filtDict['param']).astype(int)) elif filtDict['name']=='Low pass 1st order': dt = x[1]-x[0] return lowpass1(y, dt=dt, fc=filtDict['param']) elif filtDict['name']=='High pass 1st order': dt = x[1]-x[0] return highpass1(y, dt=dt, fc=filtDict['param']) else: raise NotImplementedError('{}'.format(filtDict)) # --------------------------------------------------------------------------------} # --- # --------------------------------------------------------------------------------{ def zero_crossings(y,x=None,direction=None): """ Find zero-crossing points in a discrete vector, using linear interpolation. direction: 'up' or 'down', to select only up-crossings or down-crossings returns: x values xzc such that y(yzc)==0 indexes izc, such that the zero is between y[izc] (excluded) and y[izc+1] (included) if direction is not provided, also returns: sign, equal to 1 for up crossing """ if x is None: x=np.arange(len(y)) if np.any((x[1:] - x[0:-1]) <= 0.0): raise Exception('x values need to be in ascending order') # Indices before zero-crossing iBef = np.where(y[1:]*y[0:-1] < 0.0)[0] # Find the zero crossing by linear interpolation xzc = x[iBef] - y[iBef] * (x[iBef+1] - x[iBef]) / (y[iBef+1] - y[iBef]) # Selecting points that are exactly 0 and where neighbor change sign iZero = np.where(y == 0.0)[0] iZero = iZero[np.where((iZero > 0) & (iZero < x.size-1))] iZero = iZero[np.where(y[iZero-1]*y[iZero+1] < 0.0)] # Concatenate xzc = np.concatenate((xzc, x[iZero])) iBef = np.concatenate((iBef, iZero)) # Sort iSort = np.argsort(xzc) xzc, iBef = xzc[iSort], iBef[iSort] # Return up-crossing, down crossing or both sign = np.sign(y[iBef+1]-y[iBef]) if direction == 'up': I= np.where(sign==1)[0] return xzc[I],iBef[I] elif direction == 'down': I= np.where(sign==-1)[0] return xzc[I],iBef[I] elif direction is not None: raise Exception('Direction should be either `up` or `down`') return xzc, iBef, sign # --------------------------------------------------------------------------------} # --- # --------------------------------------------------------------------------------{ def correlation(x, nMax=80, dt=1, method='manual'): """ Compute auto correlation of a signal """ nvec = np.arange(0,nMax) sigma2 = np.var(x) R = np.zeros(nMax) R[0] =1 for i,nDelay in enumerate(nvec[1:]): R[i+1] = np.mean( x[0:-nDelay] * x[nDelay:] ) / sigma2 tau = nvec*dt return R, tau def correlated_signal(coeff, n=1000): """ Create a correlated random signal of length `n` based on the correlation coefficient `coeff` value[t] = coeff * value[t-1] + (1-coeff) * random """ if coeff<0 or coeff>1: raise Exception('Correlation coefficient should be between 0 and 1') x = np.zeros(n) rvec = rand(n) x[0] = rvec[0] for m in np.arange(1,n): x[m] = coeff*x[m-1] + (1-coeff)*rvec[m] x-=np.mean(x) return x if __name__=='__main__': import numpy as np import matplotlib.pyplot as plt # Input dt = 1 n = 10000 coeff = 0.95 # 1:full corr, 00-corr nMax = 180 # Create a correlated time series tvec = np.arange(0,n)*dt ts = correlated_signal(coeff, n) # --- Compute correlation coefficient R, tau = correlation(x, nMax=nMax) fig,axes = plt.subplots(2, 1, sharey=False, figsize=(6.4,4.8)) # (6.4,4.8) fig.subplots_adjust(left=0.12, right=0.95, top=0.95, bottom=0.11, hspace=0.20, wspace=0.20) ax=axes[0] # Plot time series ax.plot(tvec,ts) ax.set_xlabel('t [s]') ax.set_ylabel('u [m/s]') ax.tick_params(direction='in') # Plot correlation ax=axes[1] ax.plot(tau, R ,'b-o', label='computed') ax.plot(tau, coeff**(tau/dt) , 'r--' ,label='coeff^{tau/dt}') # analytical coeff^n trend ax.set_xlabel(r'$\tau$ [s]') ax.set_ylabel(r'$R(\tau)$ [-]') ax.legend() plt.show()
pydatview/tools/signal.py
10,758
Create a correlated random signal of length `n` based on the correlation coefficient `coeff` value[t] = coeff * value[t-1] + (1-coeff) * random Compute auto correlation of a signal 1st order high pass filter 1st order low pass filter perform moving average, return a vector of same length as input NOTE: also in kalman.filters Reject outliers: If replaceNaN is true: they are replaced by NaN Otherwise they are removed Find zero-crossing points in a discrete vector, using linear interpolation. direction: 'up' or 'down', to select only up-crossings or down-crossings returns: x values xzc such that y(yzc)==0 indexes izc, such that the zero is between y[izc] (excluded) and y[izc+1] (included) if direction is not provided, also returns: sign, equal to 1 for up crossing --- List of available filters No rejection... --------------------------------------------------------------------------------} --- Resampling --------------------------------------------------------------------------------{import pdb; pdb.set_trace() Use first and last values for anything beyond xp OKx_new=np.sort(x_new) --- Method 1 (pandas)df_new = df_old.copy()df_new = df_new.set_index(x_old)df_new = df_new.reindex(df_new.index | x_new)df_new = df_new.interpolate().loc[x_new]df_new = df_new.reset_index() --- Method 2 interp storing dx --------------------------------------------------------------------------------} --- Filters --------------------------------------------------------------------------------{ def moving_average(x, w): t_new = np.arange(0,Tmax,dt) nt = len(t_new) nw=400 u_new = moving_average(np.floor(np.linspace(0,3,nt+nw-1))*3+3.5, nw) return np.convolve(x, np.ones(w), 'valid') / w def moving_average(x,N,mode='same'): y=np.convolve(x, np.ones((N,))/N, mode=mode) return y repeating first values --------------------------------------------------------------------------------} --- --------------------------------------------------------------------------------{ Indices before zero-crossing Find the zero crossing by linear interpolation Selecting points that are exactly 0 and where neighbor change sign Concatenate Sort Return up-crossing, down crossing or both --------------------------------------------------------------------------------} --- --------------------------------------------------------------------------------{ Input 1:full corr, 00-corr Create a correlated time series --- Compute correlation coefficient (6.4,4.8) Plot time series Plot correlation analytical coeff^n trend
2,610
en
0.621851
# -*- coding: utf-8 -*- # Generated by Django 1.11 on 2017-04-15 06:13 from __future__ import unicode_literals from django.conf import settings from django.db import migrations, models import django.db.models.deletion import django.utils.timezone import jsonfield.fields class Migration(migrations.Migration): initial = True dependencies = [ ('pinax_teams', '0002_add_simple_models'), migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ migrations.CreateModel( name='Item', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('data', jsonfield.fields.JSONField()), ], ), migrations.CreateModel( name='ItemResponse', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('created_at', models.DateTimeField(default=django.utils.timezone.now)), ('answer', models.TextField()), ('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='oxlos.Item')), ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ], ), migrations.CreateModel( name='Project', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=250)), ('description', models.TextField()), ('description_html', models.TextField(blank=True, editable=False)), ('team', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='pinax_teams.SimpleTeam')), ], ), migrations.CreateModel( name='Task', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=250)), ('description', models.TextField()), ('description_html', models.TextField(blank=True, editable=False)), ('instructions', models.TextField()), ('instructions_html', models.TextField(blank=True, editable=False)), ('question_template', models.TextField()), ('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tasks', to='oxlos.Project')), ], ), migrations.AddField( model_name='item', name='task', field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='oxlos.Task'), ), ]
oxlos/migrations/0001_initial.py
2,852
-*- coding: utf-8 -*- Generated by Django 1.11 on 2017-04-15 06:13
66
en
0.712374
from setuptools import find_packages, setup setup( name="hacker_news", version="dev", author="Elementl", author_email="hello@elementl.com", classifiers=[ "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Operating System :: OS Independent", ], packages=find_packages(exclude=["test"]), package_data={"hacker_news": ["hacker_news_dbt/*"]}, install_requires=[ "aiobotocore==1.3.3", "dagster", "dagster-aws", "dagster-dbt", "dagster-pandas", "dagster-pyspark", "dagster-slack", "dagster-postgres", "dagstermill", "dbt>=0.19.0", "mock", # DataFrames were not written to Snowflake, causing errors "pandas<1.4.0", "pyarrow>=4.0.0", "pyspark", "requests", "fsspec", "s3fs", "scipy", "sklearn", "snowflake-sqlalchemy", "matplotlib", ], extras_require={"tests": ["mypy", "pylint", "pytest"]}, )
examples/hacker_news/setup.py
1,113
DataFrames were not written to Snowflake, causing errors
56
en
0.984565
# -*- coding: utf-8 -*- """ Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community Edition) available. Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://opensource.org/licenses/MIT 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 requests_mock import ANY from backend.components.cluster_manager import ClusterManagerClient class TestClusterManagerClient: def test_get_nodes(self, cluster_id, request_user, requests_mock): expected_data = [{"innerIP": "127.0.0.1"}] requests_mock.get(ANY, json={"code": 0, "data": expected_data}) client = ClusterManagerClient(request_user.token.access_token) data = client.get_nodes(cluster_id) assert data == expected_data
bcs-ui/backend/tests/components/test_cm.py
1,234
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community Edition) available. Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://opensource.org/licenses/MIT 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. -*- coding: utf-8 -*-
727
en
0.86374
import os import sys import json import datetime import numpy as np import skimage.draw from bs4 import BeautifulSoup as bs import cv2 import imgaug from utils import * # Root directory of the project ROOT_DIR = os.path.abspath("../../") # Inference result directory RESULTS_DIR = os.path.abspath("./inference/") # Import Mask RCNN sys.path.append(ROOT_DIR) # To find local version of the library from configs import Config # from mrcnn import model as modellib, utils # from mrcnn import visualize import matplotlib # Agg backend runs without a display matplotlib.use('Agg') import matplotlib.pyplot as plt DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs") DEFAULT_DATASET_YEAR = '2012' COCO_WEIGHTS_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") # VOC DATASET MASK MAP FUNCTION # Following codes are mapping each mask color(SegmentationClass) to ground truth index. # - reference: https://d2l.ai/chapter_computer-vision/semantic-segmentation-and-dataset.html VOC_COLORMAP = [[0, 0, 0], [128, 0, 0], [0, 128, 0], [128, 128, 0], [0, 0, 128], [128, 0, 128], [0, 128, 128], [128, 128, 128], [64, 0, 0], [192, 0, 0], [64, 128, 0], [192, 128, 0], [64, 0, 128], [192, 0, 128], [64, 128, 128], [192, 128, 128], [0, 64, 0], [128, 64, 0], [0, 192, 0], [128, 192, 0], [0, 64, 128]] VOC_CLASSES = ['background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'potted plant', 'sheep', 'sofa', 'train', 'tv/monitor'] def build_colormap2label(): """Build a RGB color to label mapping for segmentation.""" colormap2label = np.zeros(256 ** 3) for i, colormap in enumerate(VOC_COLORMAP): colormap2label[(colormap[0]*256 + colormap[1])*256 + colormap[2]] = i return colormap2label def voc_label_indices(colormap, colormap2label): """Map a RGB color to a label.""" colormap = colormap.astype('int32') idx = ((colormap[:, :, 0] * 256 + colormap[:, :, 1]) * 256 + colormap[:, :, 2]) return colormap2label[idx] # VOC DATASET MASK MAP FUNCTION class VocConfig(Config): NAME = "voc" IMAGE_PER_GPU = 2 NUM_CLASSES = 1 + 20 # VOC 2012 have 20 classes. "1" is for background. class InferenceConfig(VocConfig): # Set batch size to 1 since we'll be running inference on # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU GPU_COUNT = 1 IMAGES_PER_GPU = 1 DETECTION_MIN_CONFIDENCE = 0 class VocDataset(Dataset): def load_voc(self, dataset_dir, trainval, year='2012'): """Load a voc_year of the VOC dataset. dataset_dir: The root directory of the VOC dataset, example: '/mnt/disk1/VOCdevkit' trainval: 'train' or 'val' for Training or Validation year: '2007' or '2012' for VOC dataset """ voc_year = 'VOC' + year Segmentation = os.path.join(dataset_dir, voc_year, 'ImageSets', 'Segmentation') JPEGImages = os.path.join(dataset_dir, voc_year, 'JPEGImages') Annotations = os.path.join(dataset_dir, voc_year, 'Annotations') SegmentationClass = os.path.join(dataset_dir, voc_year, 'SegmentationClass') SegmentationObject = os.path.join(dataset_dir, voc_year, 'SegmentationObject') # load classes of VOC, BG is initialed in parent class. for idx, class_name in enumerate(VOC_CLASSES[1:]): self.add_class("voc", idx + 1, class_name) assert trainval in ['train', 'val'] # read segmentation annotation file annotation_file = os.path.join(Segmentation, trainval + '.txt') image_ids = [] with open(annotation_file) as f: image_id_list = [line.strip() for line in f] image_ids += image_id_list for image_id in image_ids: image_file_name = '{}.jpg'.format(image_id) mask_file_name = '{}.png'.format(image_id) xml_file_name = '{}.xml'.format(image_id) image_path = os.path.join(JPEGImages, image_file_name) # Parse Annotations XML File with open(os.path.join(Annotations, xml_file_name)) as f: soup = bs(f, 'lxml') objects = soup.find_all('object') image_contains_class_flag = False for obj in objects: class_name = obj.find('name').text if class_name in VOC_CLASSES: image_contains_class_flag = True continue if image_contains_class_flag: class_mask_path = os.path.join(SegmentationClass, mask_file_name) object_mask_path = os.path.join(SegmentationObject, mask_file_name) self.add_image("voc", image_id=image_file_name, path=image_path, class_mask_path=class_mask_path, object_mask_path=object_mask_path) def load_raw_mask(self, image_id, class_or_object): '''load two kinds of mask of VOC dataset. image_id: id of mask class_or_object: 'class_mask' or 'object_mask' for SegmentationClass or SegmentationObject Returns: image: numpy of mask image. ''' assert class_or_object in ['class_mask', 'object_mask'] image = skimage.io.imread(self.image_info[image_id][class_or_object+'_path']) if image.ndim != 3: image = skimage.color.gray2rgb(image) # If has an alpha channel, remove it for consistency if image.shape[-1] == 4: image = image[..., :3] return image def load_class_label(self, image_id): '''Mapping SegmentationClass image's color to indice of ground truth image_id: id of mask Return: class_label: [height, width] matrix contains values form 0 to 20 ''' raw_mask = self.load_raw_mask(image_id, 'class_mask') class_label = voc_label_indices(raw_mask, build_colormap2label()) return class_label def load_mask(self, image_id): '''Mapping annotation images to real Masks(MRCNN needed) image_id: id of mask Returns: masks: A bool array of shape [height, width, instance count] with one mask per instance. class_ids: a 1D array of class IDs of the instance masks. ''' class_label = self.load_class_label(image_id) instance_mask = self.load_raw_mask(image_id, 'object_mask') max_indice = int(np.max(class_label)) instance_label = [] instance_class = [] for i in range(1, max_indice+1): if not np.any(class_label==i): continue gt_indice = i object_filter = class_label == i object_filter = object_filter.astype(np.uint8) object_filter = np.dstack((object_filter,object_filter,object_filter)) filtered = np.multiply(object_filter, instance_mask) gray = cv2.cvtColor(filtered, cv2.COLOR_RGB2GRAY) max_gray = np.max(gray) for sub_index in range(1, max_gray+1): if not np.any(gray==sub_index): continue instance_filter = gray == sub_index instance_label += [instance_filter] instance_class += [gt_indice] masks = np.asarray(instance_label).transpose((1,2,0)) classes_ids = np.asarray(instance_class) return masks, classes_ids ############################################################ # Inference ############################################################ def inference(model, dataset, limit): """Run detection on images in the given directory.""" # Create directory if not os.path.exists(RESULTS_DIR): os.makedirs(RESULTS_DIR) time_dir = "{:%Y%m%dT%H%M%S}".format(datetime.datetime.now()) time_dir = os.path.join(RESULTS_DIR, time_dir) os.makedirs(time_dir) # Load over images for image_id in dataset.image_ids[:limit]: # Load image and run detection image = dataset.load_image(image_id) # Detect objects r = model.detect([image], verbose=0)[0] # Encode image to RLE. Returns a string of multiple lines source_id = dataset.image_info[image_id]["id"] # Save image with masks if len(r['class_ids']) > 0: print('[*] {}th image has {} instance(s).'.format(image_id, len(r['class_ids']))) visualize.display_instances( image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores'], show_bbox=True, show_mask=True, title="Predictions") plt.savefig("{}/{}".format(time_dir, dataset.image_info[image_id]["id"])) plt.close() else: plt.imshow(image) plt.savefig("{}/noinstance_{}".format(time_dir, dataset.image_info[image_id]["id"])) print('[*] {}th image have no instance.'.format(image_id)) plt.close() if __name__ == '__main__': import argparse # Parse command line arguments parser = argparse.ArgumentParser( description='Train Mask R-CNN on PASCAL VOC.') parser.add_argument("--command", metavar="<command>", default='train', help="'train' or 'inference' on PASCAL VOC") parser.add_argument('--dataset', default="/data/lktime-seg-tp/dataset/PASCALVOC/VOCdevkit/", help='Directory of the PASCAL VOC dataset') parser.add_argument('--year', default='2012', help='Year of the PASCAL VOC dataset (2007 or 2012) (default=2012)') parser.add_argument('--model', default="/path/to/weights.h5", help="Path to weights .h5 file or 'voc'") parser.add_argument('--logs', default='./logs', metavar="/path/to/logs/", help='Logs and checkpoints directory (default=logs/)') parser.add_argument('--limit', required=False, default=10, metavar="<image count>", help='Images to use for evaluation (default=10)') # TODO ''' parser.add_argument('--download', required=False, default=False, metavar="<True|False>", help='Automatically download and unzip PASCAL VOC files (default=False)', type=bool) ''' args = parser.parse_args() print("Command: ", args.command) print("Model: ", args.model) print("Dataset: ", args.dataset) print("Year: ", args.year) print("Logs: ", args.logs) #print("Auto Download: ", args.download) # Configurations if args.command == "train": config = VocConfig() else: config = InferenceConfig() config.display() # Create model # if args.command == "train": # model = modellib.MaskRCNN(mode="training", config=config, # model_dir=args.logs) # else: # model = modellib.MaskRCNN(mode="inference", config=config, # model_dir=args.logs) # Select weights file to load # if args.model.lower() == "coco": # model_path = COCO_WEIGHTS_PATH # elif args.model.lower() == "last": # # Find last trained weights # model_path = model.find_last() # elif args.model.lower() == "imagenet": # # Start from ImageNet trained weights # model_path = model.get_imagenet_weights() # else: # model_path = args.model # Load weights # if args.model.lower() == "coco": # # Exclude the last layers because they require a matching # # number of classes # model.load_weights(model_path, by_name=True, exclude=[ # "mrcnn_class_logits", "mrcnn_bbox_fc", # "mrcnn_bbox", "mrcnn_mask"]) # else: # print("Loading weights ", model_path) # model.load_weights(model_path, by_name=True) # Train or evaluate if args.command == "train": # Training dataset. Use the training set and 35K from the # validation set, as as in the Mask RCNN paper. dataset_train = VocDataset() dataset_train.load_voc(args.dataset, "train", year=args.year) dataset_train.prepare() # Validation dataset dataset_val = VocDataset() dataset_val.load_voc(args.dataset, "val", year=args.year) dataset_val.prepare() # Image Augmentation # Right/Left flip 50% of the time augmentation = imgaug.augmenters.Fliplr(0.5) # *** This training schedule is an example. Update to your needs *** # # Training - Stage 1 # print("Training network heads") # model.train(dataset_train, dataset_val, # learning_rate=config.LEARNING_RATE, # epochs=40, # layers='heads', # augmentation=augmentation) # # Training - Stage 2 # # Finetune layers from ResNet stage 4 and up # print("Fine tune Resnet stage 4 and up") # model.train(dataset_train, dataset_val, # learning_rate=config.LEARNING_RATE, # epochs=120, # layers='4+', # augmentation=augmentation) # # Training - Stage 3 # # Fine tune all layers # print("Fine tune all layers") # model.train(dataset_train, dataset_val, # learning_rate=config.LEARNING_RATE / 10, # epochs=160, # layers='all', # augmentation=augmentation) # elif args.command == "inference": # #print("evaluate have not been implemented") # # Validation dataset # dataset_val = VocDataset() # voc = dataset_val.load_voc(args.dataset, "val", year=args.year) # dataset_val.prepare() # print("Running voc inference on {} images.".format(args.limit)) # inference(model, dataset_val, int(args.limit)) # else: # print("'{}' is not recognized. " # "Use 'train' or 'inference'".format(args.command))
tools/convet_voc2coco/voc2coco.py
14,589
Build a RGB color to label mapping for segmentation. Run detection on images in the given directory. Mapping SegmentationClass image's color to indice of ground truth image_id: id of mask Return: class_label: [height, width] matrix contains values form 0 to 20 Mapping annotation images to real Masks(MRCNN needed) image_id: id of mask Returns: masks: A bool array of shape [height, width, instance count] with one mask per instance. class_ids: a 1D array of class IDs of the instance masks. load two kinds of mask of VOC dataset. image_id: id of mask class_or_object: 'class_mask' or 'object_mask' for SegmentationClass or SegmentationObject Returns: image: numpy of mask image. Load a voc_year of the VOC dataset. dataset_dir: The root directory of the VOC dataset, example: '/mnt/disk1/VOCdevkit' trainval: 'train' or 'val' for Training or Validation year: '2007' or '2012' for VOC dataset Map a RGB color to a label. Root directory of the project Inference result directory Import Mask RCNN To find local version of the library from mrcnn import model as modellib, utils from mrcnn import visualize Agg backend runs without a display VOC DATASET MASK MAP FUNCTION Following codes are mapping each mask color(SegmentationClass) to ground truth index. - reference: https://d2l.ai/chapter_computer-vision/semantic-segmentation-and-dataset.html VOC DATASET MASK MAP FUNCTION VOC 2012 have 20 classes. "1" is for background. Set batch size to 1 since we'll be running inference on one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU load classes of VOC, BG is initialed in parent class. read segmentation annotation file Parse Annotations XML File If has an alpha channel, remove it for consistency Inference Create directory Load over images Load image and run detection Detect objects Encode image to RLE. Returns a string of multiple lines Save image with masks Parse command line arguments TODOprint("Auto Download: ", args.download) Configurations Create model if args.command == "train": model = modellib.MaskRCNN(mode="training", config=config, model_dir=args.logs) else: model = modellib.MaskRCNN(mode="inference", config=config, model_dir=args.logs) Select weights file to load if args.model.lower() == "coco": model_path = COCO_WEIGHTS_PATH elif args.model.lower() == "last": Find last trained weights model_path = model.find_last() elif args.model.lower() == "imagenet": Start from ImageNet trained weights model_path = model.get_imagenet_weights() else: model_path = args.model Load weights if args.model.lower() == "coco": Exclude the last layers because they require a matching number of classes model.load_weights(model_path, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]) else: print("Loading weights ", model_path) model.load_weights(model_path, by_name=True) Train or evaluate Training dataset. Use the training set and 35K from the validation set, as as in the Mask RCNN paper. Validation dataset Image Augmentation Right/Left flip 50% of the time *** This training schedule is an example. Update to your needs *** Training - Stage 1 print("Training network heads") model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=40, layers='heads', augmentation=augmentation) Training - Stage 2 Finetune layers from ResNet stage 4 and up print("Fine tune Resnet stage 4 and up") model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=120, layers='4+', augmentation=augmentation) Training - Stage 3 Fine tune all layers print("Fine tune all layers") model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE / 10, epochs=160, layers='all', augmentation=augmentation) elif args.command == "inference": print("evaluate have not been implemented") Validation dataset dataset_val = VocDataset() voc = dataset_val.load_voc(args.dataset, "val", year=args.year) dataset_val.prepare() print("Running voc inference on {} images.".format(args.limit)) inference(model, dataset_val, int(args.limit)) else: print("'{}' is not recognized. " "Use 'train' or 'inference'".format(args.command))
4,537
en
0.600697
from tint.ssl.context import PFSContextFactory from tint.log import Logger from tint.protocols.tintp import ConnectionPool from tint.protocols.tintp import TintProtocolFactory from tint.friends import FriendsList class Peer(object): def __init__(self, keyStore, storage, resolver): self.keyStore = keyStore self.storage = storage self.contextFactory = PFSContextFactory(self.keyStore) self.pool = ConnectionPool(resolver, self.contextFactory, self.keyStore, self.storage) self.protocolFactory = TintProtocolFactory(self.pool) self.friends = FriendsList(self.storage, self.keyStore, resolver) self.log = Logger(system=self) def getKeyId(self): """ Get the keyId used by this peer (this peer's identifier). This is stored in the key store. """ return self.keyStore.getKeyId() def getPublicKey(self): """ Get the keyId used by this peer (this peer's identifier). This is stored in the key store. """ return self.keyStore.getPublicKey() def set(self, hostKeyId, storagePath, storageValue): """ Set a value on a host. @param hostKeyId: The key id for the destination host to set the given key. This could be the local host, in which case the hostKey will be the same as this C{Peer}'s keyStore keyId. @param storagePath: The path to the key to set. For instance, this could be something like /chat/<somekey>/inbox. @param storageValue: The value to set. """ if hostKeyId == self.getKeyId(): return self.storage.set(hostKeyId, storagePath, storageValue) return self.pool.send(hostKeyId, 'set', storagePath, storageValue) def get(self, hostKeyId, storagePath): """ Get a value from a host. @param hostKeyId: The key id for the destination host to get the given key. This could be the local host, in which case the hostKey will be the same as this C{Peer}'s keyStore keyId. @param storagePath: The path to the key to get. For instance, this could be something like /chat/<somekey>/inbox. """ if hostKeyId == self.getKeyId(): self.log.debug("getting storagePath %s on self" % storagePath) return self.storage.get(hostKeyId, storagePath) self.log.debug("getting storagePath %s on %s" % (storagePath, hostKeyId)) return self.pool.send(hostKeyId, 'get', storagePath) def push(self, hostKeyId, storagePath, storageValue): """ Given key, create a new key at <key>/<id> with the given value, where <id> is an auto-incrementing integer value starting at 0. """ if hostKeyId == self.getKeyId(): return self.storage.push(hostKeyId, storagePath, storageValue) return self.pool.send(hostKeyId, 'push', storagePath, storageValue) def ls(self, hostKeyId, storagePath, offset, length): """ Given key, get all children keys (with the given offset and length). Length cannot be more than 1000. """ if hostKeyId == self.getKeyId(): return self.storage.ls(hostKeyId, storagePath, offset, length) return self.pool.send(hostKeyId, 'ls', storagePath, offset, length)
tint/peer.py
3,354
Get a value from a host. @param hostKeyId: The key id for the destination host to get the given key. This could be the local host, in which case the hostKey will be the same as this C{Peer}'s keyStore keyId. @param storagePath: The path to the key to get. For instance, this could be something like /chat/<somekey>/inbox. Get the keyId used by this peer (this peer's identifier). This is stored in the key store. Get the keyId used by this peer (this peer's identifier). This is stored in the key store. Given key, get all children keys (with the given offset and length). Length cannot be more than 1000. Given key, create a new key at <key>/<id> with the given value, where <id> is an auto-incrementing integer value starting at 0. Set a value on a host. @param hostKeyId: The key id for the destination host to set the given key. This could be the local host, in which case the hostKey will be the same as this C{Peer}'s keyStore keyId. @param storagePath: The path to the key to set. For instance, this could be something like /chat/<somekey>/inbox. @param storageValue: The value to set.
1,104
en
0.830432
"""Count Encoder""" import numpy as np import pandas as pd import category_encoders.utils as util from copy import copy from sklearn.base import BaseEstimator, TransformerMixin __author__ = 'joshua t. dunn' class CountEncoder(BaseEstimator, TransformerMixin): def __init__(self, verbose=0, cols=None, drop_invariant=False, return_df=True, handle_unknown=None, handle_missing='count', min_group_size=None, combine_min_nan_groups=True, min_group_name=None, normalize=False): """Count encoding for categorical features. For a given categorical feature, replace the names of the groups with the group counts. Parameters ---------- verbose: int integer indicating verbosity of output. 0 for none. cols: list a list of columns to encode, if None, all string and categorical columns will be encoded. drop_invariant: bool boolean for whether or not to drop columns with 0 variance. return_df: bool boolean for whether to return a pandas DataFrame from transform (otherwise it will be a numpy array). handle_missing: str how to handle missing values at fit time. Options are 'error', 'return_nan', and 'count'. Default 'count', which treat NaNs as a countable category at fit time. handle_unknown: str, int or dict of. how to handle unknown labels at transform time. Options are 'error' 'return_nan' and an int. Defaults to None which uses NaN behaviour specified at fit time. Passing an int will fill with this int value. normalize: bool or dict of. whether to normalize the counts to the range (0, 1). See Pandas `value_counts` for more details. min_group_size: int, float or dict of. the minimal count threshold of a group needed to ensure it is not combined into a "leftovers" group. If float in the range (0, 1), `min_group_size` is calculated as int(X.shape[0] * min_group_size). Note: This value may change type based on the `normalize` variable. If True this will become a float. If False, it will be an int. min_group_name: None, str or dict of. Set the name of the combined minimum groups when the defaults become too long. Default None. In this case the category names will be joined alphabetically with a `_` delimiter. Note: The default name can be long ae may keep changing, for example, in cross-validation. combine_min_nan_groups: bool or dict of. whether to combine the leftovers group with NaN group. Default True. Can also be forced to combine with 'force' meaning small groups are effectively counted as NaNs. Force can only used when 'handle_missing' is 'count' or 'error'. Example ------- >>> import pandas as pd >>> from sklearn.datasets import load_boston >>> from category_encoders import CountEncoder >>> bunch = load_boston() >>> y = bunch.target >>> X = pd.DataFrame(bunch.data, columns=bunch.feature_names) >>> enc = CountEncoder(cols=['CHAS', 'RAD']).fit(X, y) >>> numeric_dataset = enc.transform(X) >>> print(numeric_dataset.info()) <class 'pandas.core.frame.DataFrame'> RangeIndex: 506 entries, 0 to 505 Data columns (total 13 columns): CRIM 506 non-null float64 ZN 506 non-null float64 INDUS 506 non-null float64 CHAS 506 non-null int64 NOX 506 non-null float64 RM 506 non-null float64 AGE 506 non-null float64 DIS 506 non-null float64 RAD 506 non-null int64 TAX 506 non-null float64 PTRATIO 506 non-null float64 B 506 non-null float64 LSTAT 506 non-null float64 dtypes: float64(11), int64(2) memory usage: 51.5 KB None References ---------- """ self.return_df = return_df self.drop_invariant = drop_invariant self.drop_cols = [] self.verbose = verbose self.cols = cols self._dim = None self.mapping = None self.handle_unknown = handle_unknown self.handle_missing = handle_missing self.normalize = normalize self.min_group_size = min_group_size self.min_group_name = min_group_name self.combine_min_nan_groups = combine_min_nan_groups self._min_group_categories = {} self._normalize = {} self._min_group_name = {} self._combine_min_nan_groups = {} self._min_group_size = {} self._handle_unknown = {} self._handle_missing = {} def fit(self, X, y=None, **kwargs): """Fit encoder according to X. Parameters ---------- X : array-like, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] Target values. Returns ------- self : encoder Returns self. """ # first check the type X = util.convert_input(X) self._dim = X.shape[1] # if columns aren't passed, just use every string column if self.cols is None: self.cols = util.get_obj_cols(X) else: self.cols = util.convert_cols_to_list(self.cols) self._check_set_create_dict_attrs() self._fit_count_encode(X, y) if self.drop_invariant: self.drop_cols = [] X_temp = self.transform(X) generated_cols = util.get_generated_cols(X, X_temp, self.cols) self.drop_cols = [ x for x in generated_cols if X_temp[x].var() <= 10e-5 ] return self def transform(self, X, y=None): """Perform the transformation to new categorical data. Parameters ---------- X : array-like, shape = [n_samples, n_features] y : array-like, shape = [n_samples] Returns ------- p : array, shape = [n_samples, n_numeric + N] Transformed values with encoding applied. """ if self._dim is None: raise ValueError( 'Must train encoder before it can be used to transform data.' ) # first check the type X = util.convert_input(X) # then make sure that it is the right size if X.shape[1] != self._dim: raise ValueError( 'Unexpected input dimension %d, expected %d' % (X.shape[1], self._dim,) ) if not self.cols: return X X, _ = self._transform_count_encode(X, y) if self.drop_invariant: for col in self.drop_cols: X.drop(col, 1, inplace=True) if self.return_df: return X else: return X.values def _fit_count_encode(self, X_in, y): """Perform the count encoding.""" X = X_in.copy(deep=True) if self.cols is None: self.cols = X.columns.values self.mapping = {} for col in self.cols: if X[col].isna().any(): if self._handle_missing[col] == 'error': raise ValueError( 'Missing data found in column %s at fit time.' % (col,) ) elif self._handle_missing[col] not in ['count', 'return_nan', 'error']: raise ValueError( '%s key in `handle_missing` should be one of: ' ' `value`, `return_nan` and `error`.' % (col,) ) self.mapping[col] = X[col].value_counts( normalize=self._normalize[col], dropna=False ) if self._handle_missing[col] == 'return_nan': self.mapping[col][np.NaN] = np.NaN if any([val is not None for val in self._min_group_size.values()]): self.combine_min_categories(X) def _transform_count_encode(self, X_in, y): """Perform the transform count encoding.""" X = X_in.copy(deep=True) for col in self.cols: if self._min_group_size is not None: if col in self._min_group_categories.keys(): X[col] = ( X[col].map(self._min_group_categories[col]) .fillna(X[col]) ) X[col] = X[col].map(self.mapping[col]) if isinstance(self._handle_unknown[col], np.integer): X[col] = X[col].fillna(self._handle_unknown[col]) elif ( self._handle_unknown[col] == 'error' and X[col].isna().any() ): raise ValueError( 'Missing data found in column %s at transform time.' % (col,) ) return X, self.mapping def combine_min_categories(self, X): """Combine small categories into a single category.""" for col, mapper in self.mapping.items(): if self._normalize[col] and isinstance(self._min_group_size[col], int): self._min_group_size[col] = self._min_group_size[col] / X.shape[0] elif not self._normalize and isinstance(self._min_group_size[col], float): self._min_group_size[col] = self._min_group_size[col] * X.shape[0] if self._combine_min_nan_groups[col] is True: min_groups_idx = mapper < self._min_group_size[col] elif self._combine_min_nan_groups[col] == 'force': min_groups_idx = ( (mapper < self._min_group_size[col]) | (mapper.index.isna()) ) else: min_groups_idx = ( (mapper < self._min_group_size[col]) & (~mapper.index.isna()) ) min_groups_sum = mapper.loc[min_groups_idx].sum() if min_groups_sum > 0 and (min_groups_idx).sum() > 1: if isinstance(self._min_group_name[col], str): min_group_mapper_name = self._min_group_name else: min_group_mapper_name = '_'.join([ str(idx) for idx in mapper.loc[min_groups_idx].index.astype(str).sort_values() ]) self._min_group_categories[col] = { cat: min_group_mapper_name for cat in mapper.loc[min_groups_idx].index.tolist() } if not min_groups_idx.all(): mapper = mapper.loc[~min_groups_idx] if mapper.index.is_categorical(): mapper.index = mapper.index.add_categories( min_group_mapper_name ) mapper[min_group_mapper_name] = min_groups_sum self.mapping[col] = mapper def _check_set_create_dict_attrs(self): """Check attributes that can be dicts and format for all self.cols.""" dict_attrs = { 'normalize': False, 'min_group_name': None, 'combine_min_nan_groups': True, 'min_group_size': None, 'handle_unknown': 'value', 'handle_missing': 'value', } for attr_name, attr_default in dict_attrs.items(): attr = copy(getattr(self, attr_name)) if isinstance(attr, dict): for col in self.cols: if col not in attr: attr[col] = attr_default setattr(self, '_' + attr_name, attr) else: attr_dict = {} for col in self.cols: attr_dict[col] = attr setattr(self, '_' + attr_name, attr_dict) for col in self.cols: if ( self._handle_missing[col] == 'return_nan' and self._combine_min_nan_groups[col] == 'force' ): raise ValueError( "Cannot have `handle_missing` == 'return_nan' and " "'combine_min_nan_groups' == 'force' for columns `%s`." % (col,) )
category_encoders/count.py
12,938
Count encoding for categorical features. For a given categorical feature, replace the names of the groups with the group counts. Parameters ---------- verbose: int integer indicating verbosity of output. 0 for none. cols: list a list of columns to encode, if None, all string and categorical columns will be encoded. drop_invariant: bool boolean for whether or not to drop columns with 0 variance. return_df: bool boolean for whether to return a pandas DataFrame from transform (otherwise it will be a numpy array). handle_missing: str how to handle missing values at fit time. Options are 'error', 'return_nan', and 'count'. Default 'count', which treat NaNs as a countable category at fit time. handle_unknown: str, int or dict of. how to handle unknown labels at transform time. Options are 'error' 'return_nan' and an int. Defaults to None which uses NaN behaviour specified at fit time. Passing an int will fill with this int value. normalize: bool or dict of. whether to normalize the counts to the range (0, 1). See Pandas `value_counts` for more details. min_group_size: int, float or dict of. the minimal count threshold of a group needed to ensure it is not combined into a "leftovers" group. If float in the range (0, 1), `min_group_size` is calculated as int(X.shape[0] * min_group_size). Note: This value may change type based on the `normalize` variable. If True this will become a float. If False, it will be an int. min_group_name: None, str or dict of. Set the name of the combined minimum groups when the defaults become too long. Default None. In this case the category names will be joined alphabetically with a `_` delimiter. Note: The default name can be long ae may keep changing, for example, in cross-validation. combine_min_nan_groups: bool or dict of. whether to combine the leftovers group with NaN group. Default True. Can also be forced to combine with 'force' meaning small groups are effectively counted as NaNs. Force can only used when 'handle_missing' is 'count' or 'error'. Example ------- >>> import pandas as pd >>> from sklearn.datasets import load_boston >>> from category_encoders import CountEncoder >>> bunch = load_boston() >>> y = bunch.target >>> X = pd.DataFrame(bunch.data, columns=bunch.feature_names) >>> enc = CountEncoder(cols=['CHAS', 'RAD']).fit(X, y) >>> numeric_dataset = enc.transform(X) >>> print(numeric_dataset.info()) <class 'pandas.core.frame.DataFrame'> RangeIndex: 506 entries, 0 to 505 Data columns (total 13 columns): CRIM 506 non-null float64 ZN 506 non-null float64 INDUS 506 non-null float64 CHAS 506 non-null int64 NOX 506 non-null float64 RM 506 non-null float64 AGE 506 non-null float64 DIS 506 non-null float64 RAD 506 non-null int64 TAX 506 non-null float64 PTRATIO 506 non-null float64 B 506 non-null float64 LSTAT 506 non-null float64 dtypes: float64(11), int64(2) memory usage: 51.5 KB None References ---------- Check attributes that can be dicts and format for all self.cols. Perform the count encoding. Perform the transform count encoding. Combine small categories into a single category. Fit encoder according to X. Parameters ---------- X : array-like, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] Target values. Returns ------- self : encoder Returns self. Perform the transformation to new categorical data. Parameters ---------- X : array-like, shape = [n_samples, n_features] y : array-like, shape = [n_samples] Returns ------- p : array, shape = [n_samples, n_numeric + N] Transformed values with encoding applied. Count Encoder first check the type if columns aren't passed, just use every string column first check the type then make sure that it is the right size
4,010
en
0.655054
#The term schedule that gets displayed. Can do multiple terms in the case of displaying #summer and fall at the same time. ie termNames ['2201','2208'] termNames=['2218'] majorTemplate='in/majorPage.html.mako' #Add new majors here. #Name: short name for the major #classFile: the csv file containing all the classes in this majors curriculum #asof: the date that the major curriculum was aquired majors=[ {'name': 'SNRE', 'classFile': 'majorClassLists/SNREList.csv', 'asof': 'Oct 10,2015'}, {'name': 'WEC', 'classFile': 'majorClassLists/WECList.csv', 'asof': 'Oct 10,2015'} ] #Add new semesters here. #Name: The term code, see below. #prettyName: the more comprehendable name. eg. Fall 2015 #termSchedule: the filename for the downloaded csv file for the schedule. All should be semesterData/YYYYXX.csv # #The new API started being the sole source in spring 2020. With that term codes are: # CYYM, where C = 2, YY = the last 2 digits of the year, and M is 8 or 1 for fall or spring # #TODO: New codes for Summer. Its special since it has several mini-terms. terms=[ {'name' :'2218', 'prettyName':'Fall 2021', 'termSchedule': 'semesterData/fall2021.csv'}, {'name' :'2211', 'prettyName':'Spring 2021', 'termSchedule': 'semesterData/spring2021.csv'}, {'name' :'2208', 'prettyName':'Fall 2020', 'termSchedule': 'semesterData/fall2020.csv'}, {'name' :'2201', 'prettyName':'Spring 2020', 'termSchedule': 'semesterData/spring2020.csv'}, {'name' :'201908', 'prettyName':'Fall 2019', 'termSchedule': 'semesterData/201908.csv'}, {'name' :'201906', 'prettyName':'Summer 2019', 'termSchedule': 'semesterData/201906.csv'}, {'name' :'201901', 'prettyName':'Spring 2019', 'termSchedule': 'semesterData/201901.csv'}, {'name' :'201808', 'prettyName':'Fall 2018', 'termSchedule': 'semesterData/201808.csv'}, {'name' :'201806', 'prettyName':'Summer 2018', 'termSchedule': 'semesterData/201806.csv'}, {'name' :'201801', 'prettyName':'Spring 2018', 'termSchedule': 'semesterData/201801.csv'}, {'name' :'201708', 'prettyName':'Fall 2017', 'termSchedule': 'semesterData/201708.csv'}, {'name' :'201706', 'prettyName':'Summer 2017', 'termSchedule': 'semesterData/201706.csv'}, {'name' :'201701', 'prettyName':'Spring 2017', 'termSchedule': 'semesterData/201701.csv'}, {'name' :'201608', 'prettyName':'Fall 2016', 'termSchedule': 'semesterData/201608.csv'}, {'name' :'201606', 'prettyName':'Summer 2016', 'termSchedule': 'semesterData/201606.csv'}, {'name' :'201601', 'prettyName':'Spring 2016', 'termSchedule': 'semesterData/201601.csv'}, {'name' :'201508', 'prettyName':'Fall 2015', 'termSchedule': 'semesterData/201508.csv'}, {'name' :'201506', 'prettyName':'Summer 2015', 'termSchedule': 'semesterData/201506.csv'}, {'name' :'201501', 'prettyName':'Spring 2015', 'termSchedule': 'semesterData/201501.csv'}, {'name' :'201408', 'prettyName':'Fall 2014', 'termSchedule': 'semesterData/201408.csv'}, {'name' :'201406', 'prettyName':'Summer 2014', 'termSchedule': 'semesterData/201406.csv'}, {'name' :'201401', 'prettyName':'Spring 2014', 'termSchedule': 'semesterData/201401.csv'}, {'name' :'201308', 'prettyName':'Fall 2013', 'termSchedule': 'semesterData/201308.csv'}, {'name' :'201301', 'prettyName':'Spring 2013', 'termSchedule': 'semesterData/201301.csv'}, {'name' :'201208', 'prettyName':'Fall 2012', 'termSchedule': 'semesterData/201208.csv'}, {'name' :'201201', 'prettyName':'Spring 2012', 'termSchedule': 'semesterData/201201.csv'}, {'name' :'201108', 'prettyName':'Fall 2011', 'termSchedule': 'semesterData/201108.csv'}, {'name' :'201101', 'prettyName':'Spring 2011', 'termSchedule': 'semesterData/201101.csv'}, {'name' :'201008', 'prettyName':'Fall 2010', 'termSchedule': 'semesterData/201008.csv'} ] #To deal with 100's of special topic classes that may or may not be on the curriculum (and if not, still deserve #to be considered), show *all* special topcis classes from a few relevant departments relevantDepts=['BOT','ZOO','FAS','WIS','FOR','GEO','ENV'] #Exclude any classes with these titles. Designed for research credits which I don't need to have on the site classTitleExclusions=['SUPERVISED RESEARCH','MASTERS RESEARCH','DOCTORAL RESEARCH','ADVANCED RESEARCH', 'SUPERVISED TEACHING','INDIVIDUAL WORK','INDIVIDUAL STUDIES','SPECIAL TOPICS'] #Every dept has 'Special Topic' codes that are not necessarily in the curriculum. #Since they all share the same course codes with things thare *are* in the curriclum, #all special topics are included. #This list is to go and find them and mark them "special topics" to indicate the class might #need prior approval. #Theres probably a better way to account for these. maybe scrape the grad catalog website specialTopicClasses=['ZOO6927', 'WIS6934', 'SWS6932', 'ALS5932', 'AOM6932', 'AEC6932', 'STA6934', 'ANS6932', 'ENY6932', 'NEM6932', 'AEB6933', 'ABE6933', 'PHC6937', 'LAS6938', 'GEO6938', 'HOS6932', 'MCB6937', 'PBC6937', 'FAS6932', 'AGR6932', 'BOT6935', 'ANG6930', 'ENV6935', 'ENV6932', 'FOR6934', 'MAT6932', 'LAW6930', 'SYA7933', 'GEB6930', 'AFS6905', 'VME6934' ]
config.py
5,881
The term schedule that gets displayed. Can do multiple terms in the case of displayingsummer and fall at the same time. ie termNames ['2201','2208']Add new majors here.Name: short name for the majorclassFile: the csv file containing all the classes in this majors curriculumasof: the date that the major curriculum was aquiredAdd new semesters here.Name: The term code, see below. prettyName: the more comprehendable name. eg. Fall 2015termSchedule: the filename for the downloaded csv file for the schedule. All should be semesterData/YYYYXX.csvThe new API started being the sole source in spring 2020. With that term codes are: CYYM, where C = 2, YY = the last 2 digits of the year, and M is 8 or 1 for fall or springTODO: New codes for Summer. Its special since it has several mini-terms.To deal with 100's of special topic classes that may or may not be on the curriculum (and if not, still deserveto be considered), show *all* special topcis classes from a few relevant departmentsExclude any classes with these titles. Designed for research credits which I don't need to have on the siteEvery dept has 'Special Topic' codes that are not necessarily in the curriculum. Since they all share the same course codes with things thare *are* in the curriclum, all special topics are included.This list is to go and find them and mark them "special topics" to indicate the class mightneed prior approval. Theres probably a better way to account for these. maybe scrape the grad catalog website
1,492
en
0.910143
# --------------------------------------------------------------------- # Syslog server # --------------------------------------------------------------------- # Copyright (C) 2007-2020 The NOC Project # See LICENSE for details # --------------------------------------------------------------------- # Python modules import logging import time from typing import Tuple # NOC modules from noc.config import config from noc.core.perf import metrics from noc.core.ioloop.udpserver import UDPServer from noc.core.comp import smart_text logger = logging.getLogger(__name__) class SyslogServer(UDPServer): def __init__(self, service): super().__init__() self.service = service def enable_reuseport(self): return config.syslogcollector.enable_reuseport def enable_freebind(self): return config.syslogcollector.enable_freebind def on_read(self, data: bytes, address: Tuple[str, int]): metrics["syslog_msg_in"] += 1 cfg = self.service.lookup_config(address[0]) if not cfg: return # Invalid event source # Convert data to valid UTF8 data = smart_text(data, errors="ignore") # Parse priority priority = 0 if data.startswith("<"): idx = data.find(">") if idx == -1: return try: priority = int(data[1:idx]) except ValueError: pass data = data[idx + 1 :].strip() # Get timestamp ts = int(time.time()) # self.service.register_message(cfg, ts, data, facility=priority >> 3, severity=priority & 7)
services/syslogcollector/syslogserver.py
1,654
--------------------------------------------------------------------- Syslog server --------------------------------------------------------------------- Copyright (C) 2007-2020 The NOC Project See LICENSE for details --------------------------------------------------------------------- Python modules NOC modules Invalid event source Convert data to valid UTF8 Parse priority Get timestamp
391
en
0.220579
import logging import unittest import numpy as np import pandas as pd import scipy.stats as stats import diffxpy.api as de class _TestPairwiseNull: noise_model: str def _prepate_data( self, n_cells: int, n_genes: int, n_groups: int ): if self.noise_model == "nb": from batchglm.api.models.glm_nb import Simulator rand_fn_loc = lambda shape: np.random.uniform(0.1, 1, shape) rand_fn_scale = lambda shape: np.random.uniform(0.5, 1, shape) elif self.noise_model == "norm" or self.noise_model is None: from batchglm.api.models.glm_norm import Simulator rand_fn_loc = lambda shape: np.random.uniform(500, 1000, shape) rand_fn_scale = lambda shape: np.random.uniform(1, 2, shape) else: raise ValueError("noise model %s not recognized" % self.noise_model) sim = Simulator(num_observations=n_cells, num_features=n_genes) sim.generate_sample_description(num_batches=0, num_conditions=0) sim.generate_params( rand_fn_loc=rand_fn_loc, rand_fn_scale=rand_fn_scale ) sim.generate_data() random_sample_description = pd.DataFrame({ "condition": np.random.randint(n_groups, size=sim.nobs) }) return sim, random_sample_description def _test_null_distribution_basic( self, test: str, lazy: bool, quick_scale: bool = False, n_cells: int = 3000, n_genes: int = 200, n_groups: int = 3 ): """ Test if de.wald() generates a uniform p-value distribution if it is given data simulated based on the null model. Returns the p-value of the two-side Kolmgorov-Smirnov test for equality of the observed p-value distriubution and a uniform distribution. :param n_cells: Number of cells to simulate (number of observations per test). :param n_genes: Number of genes to simulate (number of tests). """ sim, sample_description = self._prepate_data( n_cells=n_cells, n_genes=n_genes, n_groups=n_groups ) test = de.test.pairwise( data=sim.input_data, sample_description=sample_description, grouping="condition", test=test, lazy=lazy, quick_scale=quick_scale, noise_model=self.noise_model ) _ = test.summary() # Compare p-value distribution under null model against uniform distribution. if lazy: pval_h0 = stats.kstest(test.pval_pairs(groups0=0, groups1=1).flatten(), 'uniform').pvalue else: pval_h0 = stats.kstest(test.pval[0, 1, :].flatten(), 'uniform').pvalue logging.getLogger("diffxpy").info('KS-test pvalue for null model match of wald(): %f' % pval_h0) assert pval_h0 > 0.05, "KS-Test failed: pval_h0=%f is <= 0.05!" % np.round(pval_h0, 5) return True class TestPairwiseNullStandard(unittest.TestCase, _TestPairwiseNull): def test_null_distribution_ttest(self): logging.getLogger("tensorflow").setLevel(logging.ERROR) logging.getLogger("batchglm").setLevel(logging.WARNING) logging.getLogger("diffxpy").setLevel(logging.WARNING) np.random.seed(1) self.noise_model = None self._test_null_distribution_basic(test="t-test", lazy=False) def test_null_distribution_rank(self): logging.getLogger("tensorflow").setLevel(logging.ERROR) logging.getLogger("batchglm").setLevel(logging.WARNING) logging.getLogger("diffxpy").setLevel(logging.WARNING) np.random.seed(1) self.noise_model = None self._test_null_distribution_basic(test="rank", lazy=False) class TestPairwiseNullNb(unittest.TestCase, _TestPairwiseNull): def test_null_distribution_ztest(self): logging.getLogger("tensorflow").setLevel(logging.ERROR) logging.getLogger("batchglm").setLevel(logging.WARNING) logging.getLogger("diffxpy").setLevel(logging.WARNING) np.random.seed(1) self.noise_model = "nb" self._test_null_distribution_basic(test="z-test", lazy=False, quick_scale=False) self._test_null_distribution_basic(test="z-test", lazy=False, quick_scale=True) def test_null_distribution_ztest_lazy(self): logging.getLogger("tensorflow").setLevel(logging.ERROR) logging.getLogger("batchglm").setLevel(logging.WARNING) logging.getLogger("diffxpy").setLevel(logging.WARNING) np.random.seed(1) self.noise_model = "nb" self._test_null_distribution_basic(test="z-test", lazy=True, quick_scale=False) self._test_null_distribution_basic(test="z-test", lazy=True, quick_scale=True) def test_null_distribution_wald(self): logging.getLogger("tensorflow").setLevel(logging.ERROR) logging.getLogger("batchglm").setLevel(logging.WARNING) logging.getLogger("diffxpy").setLevel(logging.WARNING) np.random.seed(1) self.noise_model = "nb" self._test_null_distribution_basic(test="wald", lazy=False, quick_scale=False) self._test_null_distribution_basic(test="wald", lazy=False, quick_scale=True) def test_null_distribution_lrt(self): logging.getLogger("tensorflow").setLevel(logging.ERROR) logging.getLogger("batchglm").setLevel(logging.WARNING) logging.getLogger("diffxpy").setLevel(logging.WARNING) np.random.seed(1) self.noise_model = "nb" self._test_null_distribution_basic(test="lrt", lazy=False, quick_scale=False) if __name__ == '__main__': unittest.main()
diffxpy/unit_test/test_pairwise.py
5,798
Test if de.wald() generates a uniform p-value distribution if it is given data simulated based on the null model. Returns the p-value of the two-side Kolmgorov-Smirnov test for equality of the observed p-value distriubution and a uniform distribution. :param n_cells: Number of cells to simulate (number of observations per test). :param n_genes: Number of genes to simulate (number of tests). Compare p-value distribution under null model against uniform distribution.
473
en
0.727981
"""pythonic_orcfighter This is one of the different GameUnits that are used in the desing patterns examples. :copyright: 2020, Jean Tardelli :license: The MIT license (MIT). See LICENSE file for further details. """ from pythonic_abstractgameunit import AbstractGameUnit class OrcFighter(AbstractGameUnit): """Create a OrcFighter instance""" def info(self): """Print info about this unit, overrides superclass method.""" print("Grrr, I am the Orc Figher!")
wargame/designpatterns/pythonic_orcfighter.py
485
Create a OrcFighter instance Print info about this unit, overrides superclass method. pythonic_orcfighter This is one of the different GameUnits that are used in the desing patterns examples. :copyright: 2020, Jean Tardelli :license: The MIT license (MIT). See LICENSE file for further details.
297
en
0.678708
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ ('documents', '0004_uuidfield'), ('meetings', '0009_auto_20170106_1414'), ] operations = [ migrations.AddField( model_name='meeting', name='documents_zip', field=models.ForeignKey(to='documents.Document', related_name='zip_for_meeting', null=True, on_delete=django.db.models.deletion.SET_NULL), ), ]
ecs/meetings/migrations/0010_meeting_documents_zip.py
583
-*- coding: utf-8 -*-
21
en
0.767281
import collections import functools import itertools import operator from contextlib import suppress from typing import Any, Dict, List import numpy as np import toolz from cached_property import cached_property import ibis.common.exceptions as com import ibis.expr.datatypes as dt import ibis.expr.rules as rlz import ibis.expr.schema as sch import ibis.expr.types as ir from ibis import util from ibis.expr.schema import HasSchema, Schema from ibis.expr.signature import Annotable from ibis.expr.signature import Argument as Arg def _safe_repr(x, memo=None): return x._repr(memo=memo) if isinstance(x, (ir.Expr, Node)) else repr(x) # TODO: move to analysis def distinct_roots(*expressions): roots = toolz.concat(expr.op().root_tables() for expr in expressions) return list(toolz.unique(roots)) class Node(Annotable): __slots__ = '_expr_cached', '_hash' def __repr__(self): return self._repr() def _repr(self, memo=None): if memo is None: from ibis.expr.format import FormatMemo memo = FormatMemo() opname = type(self).__name__ pprint_args = [] def _pp(x): return _safe_repr(x, memo=memo) for x in self.args: if isinstance(x, (tuple, list)): pp = repr(list(map(_pp, x))) else: pp = _pp(x) pprint_args.append(pp) return '{}({})'.format(opname, ', '.join(pprint_args)) def __getstate__(self) -> Dict[str, Any]: """The attributes _expr_cached and _hash are used as caches; they can be excluded from serialization without affecting correctness. Excluding _expr_cached and _hash from serialization will allow the serialized bytes to be the same for equivalent Node objets. Returns ------- Dict[str, Any] A dictionary storing the objects attributes. """ excluded_slots = {'_expr_cached', '_hash'} return { slot: getattr(self, slot) for slot in self.__slots__ if slot not in excluded_slots } def __setstate__(self, state: Dict[str, Any]) -> None: """ Parameters ---------- state: Dict[str, Any] A dictionary storing the objects attributes. """ for slot in state: setattr(self, slot, state[slot]) @property def inputs(self): return tuple(self.args) def blocks(self): # The contents of this node at referentially distinct and may not be # analyzed deeper return False def flat_args(self): for arg in self.args: if not isinstance(arg, str) and isinstance( arg, collections.abc.Iterable ): for x in arg: yield x else: yield arg def __hash__(self): if not hasattr(self, '_hash'): self._hash = hash( (type(self),) + tuple( element.op() if isinstance(element, ir.Expr) else element for element in self.flat_args() ) ) return self._hash def __eq__(self, other): return self.equals(other) def equals(self, other, cache=None): if cache is None: cache = {} key = self, other try: return cache[key] except KeyError: cache[key] = result = self is other or ( type(self) == type(other) and all_equal(self.args, other.args, cache=cache) ) return result def compatible_with(self, other): return self.equals(other) def is_ancestor(self, other): if isinstance(other, ir.Expr): other = other.op() return self.equals(other) def to_expr(self): if not hasattr(self, '_expr_cached'): self._expr_cached = self._make_expr() return self._expr_cached def _make_expr(self): klass = self.output_type() return klass(self) def output_type(self): """ This function must resolve the output type of the expression and return the node wrapped in the appropriate ValueExpr type. """ raise NotImplementedError class ValueOp(Node): def root_tables(self): exprs = [arg for arg in self.args if isinstance(arg, ir.Expr)] return distinct_roots(*exprs) def resolve_name(self): raise com.ExpressionError(f'Expression is not named: {type(self)}') def has_resolved_name(self): return False def all_equal(left, right, cache=None): """Check whether two objects `left` and `right` are equal. Parameters ---------- left : Union[object, Expr, Node] right : Union[object, Expr, Node] cache : Optional[Dict[Tuple[Node, Node], bool]] A dictionary indicating whether two Nodes are equal """ if cache is None: cache = {} if util.is_iterable(left): # check that left and right are equal length iterables and that all # of their elements are equal return ( util.is_iterable(right) and len(left) == len(right) and all( itertools.starmap( functools.partial(all_equal, cache=cache), zip(left, right) ) ) ) if hasattr(left, 'equals'): return left.equals(right, cache=cache) return left == right _table_names = ('unbound_table_{:d}'.format(i) for i in itertools.count()) def genname(): return next(_table_names) class TableNode(Node): def get_type(self, name): return self.schema[name] def output_type(self): return ir.TableExpr def aggregate(self, this, metrics, by=None, having=None): return Aggregation(this, metrics, by=by, having=having) def sort_by(self, expr, sort_exprs): return Selection(expr, [], sort_keys=sort_exprs) def is_ancestor(self, other): import ibis.expr.lineage as lin if isinstance(other, ir.Expr): other = other.op() if self.equals(other): return True fn = lambda e: (lin.proceed, e.op()) # noqa: E731 expr = self.to_expr() for child in lin.traverse(fn, expr): if child.equals(other): return True return False class TableColumn(ValueOp): """Selects a column from a TableExpr""" name = Arg((str, int)) table = Arg(ir.TableExpr) def __init__(self, name, table): schema = table.schema() if isinstance(name, int): name = schema.name_at_position(name) super().__init__(name, table) def _validate(self): if self.name not in self.table.schema(): raise com.IbisTypeError( "'{}' is not a field in {}".format( self.name, self.table.columns ) ) def parent(self): return self.table def resolve_name(self): return self.name def has_resolved_name(self): return True def root_tables(self): return self.table.op().root_tables() def _make_expr(self): dtype = self.table._get_type(self.name) klass = dtype.column_type() return klass(self, name=self.name) class RowID(ValueOp): """The row number (an autonumeric) of the returned result.""" def output_type(self): return dt.int64.column_type() def resolve_name(self): return 'rowid' def has_resolved_name(self): return True def find_all_base_tables(expr, memo=None): if memo is None: memo = {} node = expr.op() if isinstance(expr, ir.TableExpr) and node.blocks(): if expr not in memo: memo[node] = expr return memo for arg in expr.op().flat_args(): if isinstance(arg, ir.Expr): find_all_base_tables(arg, memo) return memo class PhysicalTable(TableNode, HasSchema): def blocks(self): return True class UnboundTable(PhysicalTable): schema = Arg(sch.Schema) name = Arg(str, default=genname) class DatabaseTable(PhysicalTable): name = Arg(str) schema = Arg(sch.Schema) source = Arg(rlz.client) def change_name(self, new_name): return type(self)(new_name, self.args[1], self.source) class SQLQueryResult(TableNode, HasSchema): """A table sourced from the result set of a select query""" query = Arg(rlz.noop) schema = Arg(sch.Schema) source = Arg(rlz.client) def blocks(self): return True class TableArrayView(ValueOp): """ (Temporary?) Helper operation class for SQL translation (fully formed table subqueries to be viewed as arrays) """ table = Arg(ir.TableExpr) name = Arg(str) def __init__(self, table): schema = table.schema() if len(schema) > 1: raise com.ExpressionError('Table can only have a single column') name = schema.names[0] return super().__init__(table, name) def _make_expr(self): ctype = self.table._get_type(self.name) klass = ctype.column_type() return klass(self, name=self.name) class UnaryOp(ValueOp): arg = Arg(rlz.any) class BinaryOp(ValueOp): """A binary operation""" left = Arg(rlz.any) right = Arg(rlz.any) class Cast(ValueOp): arg = Arg(rlz.any) to = Arg(dt.dtype) # see #396 for the issue preventing this # def resolve_name(self): # return self.args[0].get_name() def output_type(self): return rlz.shape_like(self.arg, dtype=self.to) class TypeOf(UnaryOp): output_type = rlz.shape_like('arg', dt.string) class Negate(UnaryOp): arg = Arg(rlz.one_of((rlz.numeric(), rlz.interval()))) output_type = rlz.typeof('arg') class IsNull(UnaryOp): """Returns true if values are null Returns ------- isnull : boolean with dimension of caller """ output_type = rlz.shape_like('arg', dt.boolean) class NotNull(UnaryOp): """Returns true if values are not null Returns ------- notnull : boolean with dimension of caller """ output_type = rlz.shape_like('arg', dt.boolean) class ZeroIfNull(UnaryOp): output_type = rlz.typeof('arg') class IfNull(ValueOp): """Equivalent to (but perhaps implemented differently): case().when(expr.notnull(), expr) .else_(null_substitute_expr) """ arg = Arg(rlz.any) ifnull_expr = Arg(rlz.any) output_type = rlz.shape_like('args') class NullIf(ValueOp): """Set values to NULL if they equal the null_if_expr""" arg = Arg(rlz.any) null_if_expr = Arg(rlz.any) output_type = rlz.shape_like('args') class NullIfZero(ValueOp): """ Set values to NULL if they equal to zero. Commonly used in cases where divide-by-zero would produce an overflow or infinity. Equivalent to (value == 0).ifelse(ibis.NA, value) Returns ------- maybe_nulled : type of caller """ arg = Arg(rlz.numeric) output_type = rlz.typeof('arg') class IsNan(ValueOp): arg = Arg(rlz.floating) output_type = rlz.shape_like('arg', dt.boolean) class IsInf(ValueOp): arg = Arg(rlz.floating) output_type = rlz.shape_like('arg', dt.boolean) class CoalesceLike(ValueOp): # According to Impala documentation: # Return type: same as the initial argument value, except that integer # values are promoted to BIGINT and floating-point values are promoted to # DOUBLE; use CAST() when inserting into a smaller numeric column arg = Arg(rlz.list_of(rlz.any)) def output_type(self): first = self.arg[0] if isinstance(first, (ir.IntegerValue, ir.FloatingValue)): dtype = first.type().largest else: dtype = first.type() # self.arg is a list of value expressions return rlz.shape_like(self.arg, dtype) class Coalesce(CoalesceLike): pass class Greatest(CoalesceLike): pass class Least(CoalesceLike): pass class Abs(UnaryOp): """Absolute value""" output_type = rlz.typeof('arg') class Ceil(UnaryOp): """ Round up to the nearest integer value greater than or equal to this value Returns ------- ceiled : type depending on input Decimal values: yield decimal Other numeric values: yield integer (int32) """ arg = Arg(rlz.numeric) def output_type(self): if isinstance(self.arg.type(), dt.Decimal): return self.arg._factory return rlz.shape_like(self.arg, dt.int64) class Floor(UnaryOp): """ Round down to the nearest integer value less than or equal to this value Returns ------- floored : type depending on input Decimal values: yield decimal Other numeric values: yield integer (int32) """ arg = Arg(rlz.numeric) def output_type(self): if isinstance(self.arg.type(), dt.Decimal): return self.arg._factory return rlz.shape_like(self.arg, dt.int64) class Round(ValueOp): arg = Arg(rlz.numeric) digits = Arg(rlz.numeric, default=None) def output_type(self): if isinstance(self.arg, ir.DecimalValue): return self.arg._factory elif self.digits is None: return rlz.shape_like(self.arg, dt.int64) else: return rlz.shape_like(self.arg, dt.double) class Clip(ValueOp): arg = Arg(rlz.strict_numeric) lower = Arg(rlz.strict_numeric, default=None) upper = Arg(rlz.strict_numeric, default=None) output_type = rlz.typeof('arg') class BaseConvert(ValueOp): arg = Arg(rlz.one_of([rlz.integer, rlz.string])) from_base = Arg(rlz.integer) to_base = Arg(rlz.integer) def output_type(self): return rlz.shape_like(tuple(self.flat_args()), dt.string) class MathUnaryOp(UnaryOp): arg = Arg(rlz.numeric) def output_type(self): arg = self.arg if isinstance(self.arg, ir.DecimalValue): dtype = arg.type() else: dtype = dt.double return rlz.shape_like(arg, dtype) class ExpandingTypeMathUnaryOp(MathUnaryOp): def output_type(self): if not isinstance(self.arg, ir.DecimalValue): return super().output_type() arg = self.arg return rlz.shape_like(arg, arg.type().largest) class Exp(ExpandingTypeMathUnaryOp): pass class Sign(UnaryOp): arg = Arg(rlz.numeric) output_type = rlz.typeof('arg') class Sqrt(MathUnaryOp): pass class Logarithm(MathUnaryOp): arg = Arg(rlz.strict_numeric) class Log(Logarithm): arg = Arg(rlz.strict_numeric) base = Arg(rlz.strict_numeric, default=None) class Ln(Logarithm): """Natural logarithm""" class Log2(Logarithm): """Logarithm base 2""" class Log10(Logarithm): """Logarithm base 10""" class Degrees(ExpandingTypeMathUnaryOp): """Converts radians to degrees""" arg = Arg(rlz.numeric) class Radians(MathUnaryOp): """Converts degrees to radians""" arg = Arg(rlz.numeric) # TRIGONOMETRIC OPERATIONS class TrigonometricUnary(MathUnaryOp): """Trigonometric base unary""" arg = Arg(rlz.numeric) class TrigonometricBinary(BinaryOp): """Trigonometric base binary""" left = Arg(rlz.numeric) right = Arg(rlz.numeric) output_type = rlz.shape_like('args', dt.float64) class Acos(TrigonometricUnary): """Returns the arc cosine of x""" class Asin(TrigonometricUnary): """Returns the arc sine of x""" class Atan(TrigonometricUnary): """Returns the arc tangent of x""" class Atan2(TrigonometricBinary): """Returns the arc tangent of x and y""" class Cos(TrigonometricUnary): """Returns the cosine of x""" class Cot(TrigonometricUnary): """Returns the cotangent of x""" class Sin(TrigonometricUnary): """Returns the sine of x""" class Tan(TrigonometricUnary): """Returns the tangent of x""" class StringUnaryOp(UnaryOp): arg = Arg(rlz.string) output_type = rlz.shape_like('arg', dt.string) class Uppercase(StringUnaryOp): """Convert string to all uppercase""" class Lowercase(StringUnaryOp): """Convert string to all lowercase""" class Reverse(StringUnaryOp): """Reverse string""" class Strip(StringUnaryOp): """Remove whitespace from left and right sides of string""" class LStrip(StringUnaryOp): """Remove whitespace from left side of string""" class RStrip(StringUnaryOp): """Remove whitespace from right side of string""" class Capitalize(StringUnaryOp): """Return a capitalized version of input string""" class Substring(ValueOp): arg = Arg(rlz.string) start = Arg(rlz.integer) length = Arg(rlz.integer, default=None) output_type = rlz.shape_like('arg', dt.string) class StrRight(ValueOp): arg = Arg(rlz.string) nchars = Arg(rlz.integer) output_type = rlz.shape_like('arg', dt.string) class Repeat(ValueOp): arg = Arg(rlz.string) times = Arg(rlz.integer) output_type = rlz.shape_like('arg', dt.string) class StringFind(ValueOp): arg = Arg(rlz.string) substr = Arg(rlz.string) start = Arg(rlz.integer, default=None) end = Arg(rlz.integer, default=None) output_type = rlz.shape_like('arg', dt.int64) class Translate(ValueOp): arg = Arg(rlz.string) from_str = Arg(rlz.string) to_str = Arg(rlz.string) output_type = rlz.shape_like('arg', dt.string) class LPad(ValueOp): arg = Arg(rlz.string) length = Arg(rlz.integer) pad = Arg(rlz.string, default=None) output_type = rlz.shape_like('arg', dt.string) class RPad(ValueOp): arg = Arg(rlz.string) length = Arg(rlz.integer) pad = Arg(rlz.string, default=None) output_type = rlz.shape_like('arg', dt.string) class FindInSet(ValueOp): needle = Arg(rlz.string) values = Arg(rlz.list_of(rlz.string, min_length=1)) output_type = rlz.shape_like('needle', dt.int64) class StringJoin(ValueOp): sep = Arg(rlz.string) arg = Arg(rlz.list_of(rlz.string, min_length=1)) def output_type(self): return rlz.shape_like(tuple(self.flat_args()), dt.string) class StartsWith(ValueOp): arg = Arg(rlz.string) start = Arg(rlz.string) output_type = rlz.shape_like("arg", dt.boolean) class EndsWith(ValueOp): arg = Arg(rlz.string) end = Arg(rlz.string) output_type = rlz.shape_like("arg", dt.boolean) class BooleanValueOp: pass class FuzzySearch(ValueOp, BooleanValueOp): arg = Arg(rlz.string) pattern = Arg(rlz.string) output_type = rlz.shape_like('arg', dt.boolean) class StringSQLLike(FuzzySearch): arg = Arg(rlz.string) pattern = Arg(rlz.string) escape = Arg(str, default=None) class StringSQLILike(StringSQLLike): """SQL ilike operation""" class RegexSearch(FuzzySearch): pass class RegexExtract(ValueOp): arg = Arg(rlz.string) pattern = Arg(rlz.string) index = Arg(rlz.integer) output_type = rlz.shape_like('arg', dt.string) class RegexReplace(ValueOp): arg = Arg(rlz.string) pattern = Arg(rlz.string) replacement = Arg(rlz.string) output_type = rlz.shape_like('arg', dt.string) class StringReplace(ValueOp): arg = Arg(rlz.string) pattern = Arg(rlz.string) replacement = Arg(rlz.string) output_type = rlz.shape_like('arg', dt.string) class StringSplit(ValueOp): arg = Arg(rlz.string) delimiter = Arg(rlz.string) output_type = rlz.shape_like('arg', dt.Array(dt.string)) class StringConcat(ValueOp): arg = Arg(rlz.list_of(rlz.string)) output_type = rlz.shape_like('arg', dt.string) class ParseURL(ValueOp): arg = Arg(rlz.string) extract = Arg( rlz.isin( { 'PROTOCOL', 'HOST', 'PATH', 'REF', 'AUTHORITY', 'FILE', 'USERINFO', 'QUERY', } ) ) key = Arg(rlz.string, default=None) output_type = rlz.shape_like('arg', dt.string) class StringLength(UnaryOp): """ Compute length of strings Returns ------- length : int32 """ output_type = rlz.shape_like('arg', dt.int32) class StringAscii(UnaryOp): output_type = rlz.shape_like('arg', dt.int32) # ---------------------------------------------------------------------- class Reduction(ValueOp): _reduction = True class Count(Reduction): arg = Arg((ir.ColumnExpr, ir.TableExpr)) where = Arg(rlz.boolean, default=None) def output_type(self): return functools.partial(ir.IntegerScalar, dtype=dt.int64) class Arbitrary(Reduction): arg = Arg(rlz.column(rlz.any)) how = Arg(rlz.isin({'first', 'last', 'heavy'}), default=None) where = Arg(rlz.boolean, default=None) output_type = rlz.scalar_like('arg') class BitAnd(Reduction): """Aggregate bitwise AND operation. All elements in an integer column are ANDed together. This can be used to determine which bit flags are set on all elements. Resources: * `BigQuery BIT_AND <https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions#bit_and>`_ * `MySQL BIT_AND <https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html#function_bit-and>`_ """ arg = Arg(rlz.column(rlz.integer)) where = Arg(rlz.boolean, default=None) output_type = rlz.scalar_like('arg') class BitOr(Reduction): """Aggregate bitwise OR operation. All elements in an integer column are ORed together. This can be used to determine which bit flags are set on any element. Resources: * `BigQuery BIT_OR <https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions#bit_or>`_ * `MySQL BIT_OR <https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html#function_bit-or>`_ """ arg = Arg(rlz.column(rlz.integer)) where = Arg(rlz.boolean, default=None) output_type = rlz.scalar_like('arg') class BitXor(Reduction): """Aggregate bitwise XOR operation. All elements in an integer column are XORed together. This can be used as a parity checksum of element values. Resources: * `BigQuery BIT_XOR <https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions#bit_xor>`_ * `MySQL BIT_XOR <https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html#function_bit-xor>`_ """ arg = Arg(rlz.column(rlz.integer)) where = Arg(rlz.boolean, default=None) output_type = rlz.scalar_like('arg') class Sum(Reduction): arg = Arg(rlz.column(rlz.numeric)) where = Arg(rlz.boolean, default=None) def output_type(self): if isinstance(self.arg, ir.BooleanValue): dtype = dt.int64 else: dtype = self.arg.type().largest return dtype.scalar_type() class Mean(Reduction): arg = Arg(rlz.column(rlz.numeric)) where = Arg(rlz.boolean, default=None) def output_type(self): if isinstance(self.arg, ir.DecimalValue): dtype = self.arg.type() else: dtype = dt.float64 return dtype.scalar_type() class Quantile(Reduction): arg = Arg(rlz.any) quantile = Arg(rlz.strict_numeric) interpolation = Arg( rlz.isin({'linear', 'lower', 'higher', 'midpoint', 'nearest'}), default='linear', ) def output_type(self): return dt.float64.scalar_type() class MultiQuantile(Quantile): arg = Arg(rlz.any) quantile = Arg(rlz.value(dt.Array(dt.float64))) interpolation = Arg( rlz.isin({'linear', 'lower', 'higher', 'midpoint', 'nearest'}), default='linear', ) def output_type(self): return dt.Array(dt.float64).scalar_type() class VarianceBase(Reduction): arg = Arg(rlz.column(rlz.numeric)) how = Arg(rlz.isin({'sample', 'pop'}), default=None) where = Arg(rlz.boolean, default=None) def output_type(self): if isinstance(self.arg, ir.DecimalValue): dtype = self.arg.type().largest else: dtype = dt.float64 return dtype.scalar_type() class StandardDev(VarianceBase): pass class Variance(VarianceBase): pass class Correlation(Reduction): """Coefficient of correlation of a set of number pairs.""" left = Arg(rlz.column(rlz.numeric)) right = Arg(rlz.column(rlz.numeric)) how = Arg(rlz.isin({'sample', 'pop'}), default=None) where = Arg(rlz.boolean, default=None) def output_type(self): return dt.float64.scalar_type() class Covariance(Reduction): """Covariance of a set of number pairs.""" left = Arg(rlz.column(rlz.numeric)) right = Arg(rlz.column(rlz.numeric)) how = Arg(rlz.isin({'sample', 'pop'}), default=None) where = Arg(rlz.boolean, default=None) def output_type(self): return dt.float64.scalar_type() class Max(Reduction): arg = Arg(rlz.column(rlz.any)) where = Arg(rlz.boolean, default=None) output_type = rlz.scalar_like('arg') class Min(Reduction): arg = Arg(rlz.column(rlz.any)) where = Arg(rlz.boolean, default=None) output_type = rlz.scalar_like('arg') class HLLCardinality(Reduction): """Approximate number of unique values using HyperLogLog algorithm. Impala offers the NDV built-in function for this. """ arg = Arg(rlz.column(rlz.any)) where = Arg(rlz.boolean, default=None) def output_type(self): # Impala 2.0 and higher returns a DOUBLE # return ir.DoubleScalar return functools.partial(ir.IntegerScalar, dtype=dt.int64) class GroupConcat(Reduction): arg = Arg(rlz.column(rlz.any)) sep = Arg(rlz.string, default=',') where = Arg(rlz.boolean, default=None) def output_type(self): return dt.string.scalar_type() class CMSMedian(Reduction): """ Compute the approximate median of a set of comparable values using the Count-Min-Sketch algorithm. Exposed in Impala using APPX_MEDIAN. """ arg = Arg(rlz.column(rlz.any)) where = Arg(rlz.boolean, default=None) output_type = rlz.scalar_like('arg') # ---------------------------------------------------------------------- # Analytic functions class AnalyticOp(ValueOp): pass class WindowOp(ValueOp): expr = Arg(rlz.noop) window = Arg(rlz.noop) output_type = rlz.array_like('expr') display_argnames = False def __init__(self, expr, window): from ibis.expr.analysis import is_analytic from ibis.expr.window import propagate_down_window if not is_analytic(expr): raise com.IbisInputError( 'Expression does not contain a valid window operation' ) table = ir.find_base_table(expr) if table is not None: window = window.bind(table) if window.max_lookback is not None: error_msg = ( "'max lookback' windows must be ordered " "by a timestamp column" ) if len(window._order_by) != 1: raise com.IbisInputError(error_msg) order_var = window._order_by[0].op().args[0] if not isinstance(order_var.type(), dt.Timestamp): raise com.IbisInputError(error_msg) expr = propagate_down_window(expr, window) super().__init__(expr, window) def over(self, window): new_window = self.window.combine(window) return WindowOp(self.expr, new_window) @property def inputs(self): return self.expr.op().inputs[0], self.window def root_tables(self): return distinct_roots( self.expr, *self.window._order_by, *self.window._group_by ) class ShiftBase(AnalyticOp): arg = Arg(rlz.column(rlz.any)) offset = Arg(rlz.one_of((rlz.integer, rlz.interval)), default=None) default = Arg(rlz.any, default=None) output_type = rlz.typeof('arg') class Lag(ShiftBase): pass class Lead(ShiftBase): pass class RankBase(AnalyticOp): def output_type(self): return dt.int64.column_type() class MinRank(RankBase): """ Compute position of first element within each equal-value group in sorted order. Examples -------- values ranks 1 0 1 0 2 2 2 2 2 2 3 5 Returns ------- ranks : Int64Column, starting from 0 """ # Equivalent to SQL RANK() arg = Arg(rlz.column(rlz.any)) class DenseRank(RankBase): """ Compute position of first element within each equal-value group in sorted order, ignoring duplicate values. Examples -------- values ranks 1 0 1 0 2 1 2 1 2 1 3 2 Returns ------- ranks : Int64Column, starting from 0 """ # Equivalent to SQL DENSE_RANK() arg = Arg(rlz.column(rlz.any)) class RowNumber(RankBase): """ Compute row number starting from 0 after sorting by column expression Examples -------- >>> import ibis >>> t = ibis.table([('values', dt.int64)]) >>> w = ibis.window(order_by=t.values) >>> row_num = ibis.row_number().over(w) >>> result = t[t.values, row_num.name('row_num')] Returns ------- row_number : Int64Column, starting from 0 """ # Equivalent to SQL ROW_NUMBER() class CumulativeOp(AnalyticOp): pass class CumulativeSum(CumulativeOp): """Cumulative sum. Requires an order window.""" arg = Arg(rlz.column(rlz.numeric)) def output_type(self): if isinstance(self.arg, ir.BooleanValue): dtype = dt.int64 else: dtype = self.arg.type().largest return dtype.column_type() class CumulativeMean(CumulativeOp): """Cumulative mean. Requires an order window.""" arg = Arg(rlz.column(rlz.numeric)) def output_type(self): if isinstance(self.arg, ir.DecimalValue): dtype = self.arg.type().largest else: dtype = dt.float64 return dtype.column_type() class CumulativeMax(CumulativeOp): """Cumulative max. Requires an order window.""" arg = Arg(rlz.column(rlz.any)) output_type = rlz.array_like('arg') class CumulativeMin(CumulativeOp): """Cumulative min. Requires an order window.""" arg = Arg(rlz.column(rlz.any)) output_type = rlz.array_like('arg') class PercentRank(AnalyticOp): arg = Arg(rlz.column(rlz.any)) output_type = rlz.shape_like('arg', dt.double) class NTile(AnalyticOp): arg = Arg(rlz.column(rlz.any)) buckets = Arg(rlz.integer) output_type = rlz.shape_like('arg', dt.int64) class FirstValue(AnalyticOp): arg = Arg(rlz.column(rlz.any)) output_type = rlz.typeof('arg') class LastValue(AnalyticOp): arg = Arg(rlz.column(rlz.any)) output_type = rlz.typeof('arg') class NthValue(AnalyticOp): arg = Arg(rlz.column(rlz.any)) nth = Arg(rlz.integer) output_type = rlz.typeof('arg') # ---------------------------------------------------------------------- # Distinct stuff class Distinct(TableNode, HasSchema): """ Distinct is a table-level unique-ing operation. In SQL, you might have: SELECT DISTINCT foo FROM table SELECT DISTINCT foo, bar FROM table """ table = Arg(ir.TableExpr) def _validate(self): # check whether schema has overlapping columns or not assert self.schema @cached_property def schema(self): return self.table.schema() def blocks(self): return True class DistinctColumn(ValueOp): """ COUNT(DISTINCT ...) is really just syntactic suger, but we provide a distinct().count() nicety for users nonetheless. For all intents and purposes, like Distinct, but can be distinguished later for evaluation if the result should be array-like versus table-like. Also for calling count() """ arg = Arg(rlz.noop) output_type = rlz.typeof('arg') def count(self): """Only valid if the distinct contains a single column""" return CountDistinct(self.arg) class CountDistinct(Reduction): arg = Arg(rlz.column(rlz.any)) where = Arg(rlz.boolean, default=None) def output_type(self): return dt.int64.scalar_type() # --------------------------------------------------------------------- # Boolean reductions and semi/anti join support class Any(ValueOp): # Depending on the kind of input boolean array, the result might either be # array-like (an existence-type predicate) or scalar (a reduction) arg = Arg(rlz.column(rlz.boolean)) @property def _reduction(self): roots = self.arg.op().root_tables() return len(roots) < 2 def output_type(self): if self._reduction: return dt.boolean.scalar_type() else: return dt.boolean.column_type() def negate(self): return NotAny(self.arg) class All(ValueOp): arg = Arg(rlz.column(rlz.boolean)) output_type = rlz.scalar_like('arg') _reduction = True def negate(self): return NotAll(self.arg) class NotAny(Any): def negate(self): return Any(self.arg) class NotAll(All): def negate(self): return All(self.arg) class CumulativeAny(CumulativeOp): arg = Arg(rlz.column(rlz.boolean)) output_type = rlz.typeof('arg') class CumulativeAll(CumulativeOp): arg = Arg(rlz.column(rlz.boolean)) output_type = rlz.typeof('arg') # --------------------------------------------------------------------- class TypedCaseBuilder: __slots__ = () def type(self): types = [result.type() for result in self.results] return dt.highest_precedence(types) def else_(self, result_expr): """ Specify Returns ------- builder : CaseBuilder """ kwargs = { slot: getattr(self, slot) for slot in self.__slots__ if slot != 'default' } result_expr = ir.as_value_expr(result_expr) kwargs['default'] = result_expr # Maintain immutability return type(self)(**kwargs) def end(self): default = self.default if default is None: default = ir.null().cast(self.type()) args = [ getattr(self, slot) for slot in self.__slots__ if slot != 'default' ] args.append(default) op = self.__class__.case_op(*args) return op.to_expr() class SimpleCase(ValueOp): base = Arg(rlz.any) cases = Arg(rlz.list_of(rlz.any)) results = Arg(rlz.list_of(rlz.any)) default = Arg(rlz.any) def _validate(self): assert len(self.cases) == len(self.results) def root_tables(self): return distinct_roots( *itertools.chain( [self.base], self.cases, self.results, [] if self.default is None else [self.default], ) ) def output_type(self): exprs = self.results + [self.default] return rlz.shape_like(self.base, dtype=exprs.type()) class SimpleCaseBuilder(TypedCaseBuilder): __slots__ = 'base', 'cases', 'results', 'default' case_op = SimpleCase def __init__(self, base, cases=None, results=None, default=None): self.base = base self.cases = list(cases if cases is not None else []) self.results = list(results if results is not None else []) self.default = default def when(self, case_expr, result_expr): """ Add a new case-result pair. Parameters ---------- case : Expr Expression to equality-compare with base expression. Must be comparable with the base. result : Expr Value when the case predicate evaluates to true. Returns ------- builder : CaseBuilder """ case_expr = ir.as_value_expr(case_expr) result_expr = ir.as_value_expr(result_expr) if not rlz.comparable(self.base, case_expr): raise TypeError( 'Base expression and passed case are not ' 'comparable' ) cases = list(self.cases) cases.append(case_expr) results = list(self.results) results.append(result_expr) # Maintain immutability return type(self)(self.base, cases, results, self.default) class SearchedCase(ValueOp): cases = Arg(rlz.list_of(rlz.boolean)) results = Arg(rlz.list_of(rlz.any)) default = Arg(rlz.any) def _validate(self): assert len(self.cases) == len(self.results) def root_tables(self): cases, results, default = self.args return distinct_roots( *itertools.chain( cases.values, results.values, [] if default is None else [default], ) ) def output_type(self): exprs = self.results + [self.default] dtype = rlz.highest_precedence_dtype(exprs) return rlz.shape_like(self.cases, dtype) class SearchedCaseBuilder(TypedCaseBuilder): __slots__ = 'cases', 'results', 'default' case_op = SearchedCase def __init__(self, cases=None, results=None, default=None): self.cases = list(cases if cases is not None else []) self.results = list(results if results is not None else []) self.default = default def when(self, case_expr, result_expr): """ Add a new case-result pair. Parameters ---------- case : Expr Expression to equality-compare with base expression. Must be comparable with the base. result : Expr Value when the case predicate evaluates to true. Returns ------- builder : CaseBuilder """ case_expr = ir.as_value_expr(case_expr) result_expr = ir.as_value_expr(result_expr) if not isinstance(case_expr, ir.BooleanValue): raise TypeError(case_expr) cases = list(self.cases) cases.append(case_expr) results = list(self.results) results.append(result_expr) # Maintain immutability return type(self)(cases, results, self.default) class Where(ValueOp): """ Ternary case expression, equivalent to bool_expr.case() .when(True, true_expr) .else_(false_or_null_expr) """ bool_expr = Arg(rlz.boolean) true_expr = Arg(rlz.any) false_null_expr = Arg(rlz.any) def output_type(self): return rlz.shape_like(self.bool_expr, self.true_expr.type()) def _validate_join_tables(left, right): if not isinstance(left, ir.TableExpr): raise TypeError( 'Can only join table expressions, got {} for ' 'left table'.format(type(left).__name__) ) if not isinstance(right, ir.TableExpr): raise TypeError( 'Can only join table expressions, got {} for ' 'right table'.format(type(right).__name__) ) def _make_distinct_join_predicates(left, right, predicates): # see GH #667 # If left and right table have a common parent expression (e.g. they # have different filters), must add a self-reference and make the # appropriate substitution in the join predicates if left.equals(right): right = right.view() predicates = _clean_join_predicates(left, right, predicates) return left, right, predicates def _clean_join_predicates(left, right, predicates): import ibis.expr.analysis as L result = [] if not isinstance(predicates, (list, tuple)): predicates = [predicates] for pred in predicates: if isinstance(pred, tuple): if len(pred) != 2: raise com.ExpressionError('Join key tuple must be ' 'length 2') lk, rk = pred lk = left._ensure_expr(lk) rk = right._ensure_expr(rk) pred = lk == rk elif isinstance(pred, str): pred = left[pred] == right[pred] elif not isinstance(pred, ir.Expr): raise NotImplementedError if not isinstance(pred, ir.BooleanColumn): raise com.ExpressionError('Join predicate must be comparison') preds = L.flatten_predicate(pred) result.extend(preds) _validate_join_predicates(left, right, result) return result def _validate_join_predicates(left, right, predicates): from ibis.expr.analysis import fully_originate_from # Validate join predicates. Each predicate must be valid jointly when # considering the roots of each input table for predicate in predicates: if not fully_originate_from(predicate, [left, right]): raise com.RelationError( 'The expression {!r} does not fully ' 'originate from dependencies of the table ' 'expression.'.format(predicate) ) class Join(TableNode): left = Arg(rlz.noop) right = Arg(rlz.noop) predicates = Arg(rlz.noop) def __init__(self, left, right, predicates): _validate_join_tables(left, right) left, right, predicates = _make_distinct_join_predicates( left, right, predicates ) super().__init__(left, right, predicates) def _get_schema(self): # For joins retaining both table schemas, merge them together here left = self.left right = self.right if not left._is_materialized(): left = left.materialize() if not right._is_materialized(): right = right.materialize() sleft = left.schema() sright = right.schema() overlap = set(sleft.names) & set(sright.names) if overlap: raise com.RelationError( 'Joined tables have overlapping names: %s' % str(list(overlap)) ) return sleft.append(sright) def has_schema(self): return False def root_tables(self): if util.all_of([self.left.op(), self.right.op()], (Join, Selection)): # Unraveling is not possible return [self.left.op(), self.right.op()] else: return distinct_roots(self.left, self.right) class InnerJoin(Join): pass class LeftJoin(Join): pass class RightJoin(Join): pass class OuterJoin(Join): pass class AnyInnerJoin(Join): pass class AnyLeftJoin(Join): pass class LeftSemiJoin(Join): def _get_schema(self): return self.left.schema() class LeftAntiJoin(Join): def _get_schema(self): return self.left.schema() class MaterializedJoin(TableNode, HasSchema): join = Arg(ir.TableExpr) def _validate(self): assert isinstance(self.join.op(), Join) # check whether the underlying schema has overlapping columns or not assert self.schema @cached_property def schema(self): return self.join.op()._get_schema() def root_tables(self): return self.join.op().root_tables() def blocks(self): return True class CrossJoin(InnerJoin): """ Some databases have a CROSS JOIN operator, that may be preferential to use over an INNER JOIN with no predicates. """ def __init__(self, *args, **kwargs): if 'prefixes' in kwargs: raise NotImplementedError if len(args) < 2: raise com.IbisInputError('Must pass at least 2 tables') left = args[0] right = args[1] for t in args[2:]: right = right.cross_join(t) InnerJoin.__init__(self, left, right, []) class AsOfJoin(Join): left = Arg(rlz.noop) right = Arg(rlz.noop) predicates = Arg(rlz.noop) by = Arg(rlz.noop, default=None) tolerance = Arg(rlz.interval(), default=None) def __init__(self, left, right, predicates, by, tolerance): super().__init__(left, right, predicates) self.by = _clean_join_predicates(self.left, self.right, by) self.tolerance = tolerance self._validate_args(['by', 'tolerance']) def _validate_args(self, args: List[str]): for arg in args: argument = self.signature[arg] value = argument.validate(getattr(self, arg)) setattr(self, arg, value) class SetOp(TableNode, HasSchema): left = Arg(rlz.noop) right = Arg(rlz.noop) def _validate(self): if not self.left.schema().equals(self.right.schema()): raise com.RelationError( 'Table schemas must be equal for set operations' ) @cached_property def schema(self): return self.left.schema() def blocks(self): return True class Union(SetOp): distinct = Arg(rlz.validator(bool), default=False) class Intersection(SetOp): pass class Difference(SetOp): pass class Limit(TableNode): table = Arg(ir.TableExpr) n = Arg(rlz.validator(int)) offset = Arg(rlz.validator(int)) def blocks(self): return True @property def schema(self): return self.table.schema() def has_schema(self): return self.table.op().has_schema() def root_tables(self): return [self] # -------------------------------------------------------------------- # Sorting def to_sort_key(table, key): if isinstance(key, DeferredSortKey): key = key.resolve(table) if isinstance(key, ir.SortExpr): return key if isinstance(key, (tuple, list)): key, sort_order = key else: sort_order = True if not isinstance(key, ir.Expr): key = table._ensure_expr(key) if isinstance(key, (ir.SortExpr, DeferredSortKey)): return to_sort_key(table, key) if isinstance(sort_order, str): if sort_order.lower() in ('desc', 'descending'): sort_order = False elif not isinstance(sort_order, bool): sort_order = bool(sort_order) return SortKey(key, ascending=sort_order).to_expr() class SortKey(Node): expr = Arg(rlz.column(rlz.any)) ascending = Arg(rlz.validator(bool), default=True) def __repr__(self): # Temporary rows = [ 'Sort key:', ' ascending: {0!s}'.format(self.ascending), util.indent(_safe_repr(self.expr), 2), ] return '\n'.join(rows) def output_type(self): return ir.SortExpr def root_tables(self): return self.expr.op().root_tables() def equals(self, other, cache=None): # TODO: might generalize this equals based on fields # requires a proxy class with equals for non expr values return ( isinstance(other, SortKey) and self.expr.equals(other.expr, cache=cache) and self.ascending == other.ascending ) def resolve_name(self): return self.expr.get_name() class DeferredSortKey: def __init__(self, what, ascending=True): self.what = what self.ascending = ascending def resolve(self, parent): what = parent._ensure_expr(self.what) return SortKey(what, ascending=self.ascending).to_expr() class SelfReference(TableNode, HasSchema): table = Arg(ir.TableExpr) @cached_property def schema(self): return self.table.schema() def root_tables(self): # The dependencies of this operation are not walked, which makes the # table expression holding this relationally distinct from other # expressions, so things like self-joins are possible return [self] def blocks(self): return True class Selection(TableNode, HasSchema): table = Arg(ir.TableExpr) selections = Arg(rlz.noop, default=None) predicates = Arg(rlz.noop, default=None) sort_keys = Arg(rlz.noop, default=None) def __init__( self, table, selections=None, predicates=None, sort_keys=None ): import ibis.expr.analysis as L # Argument cleaning selections = util.promote_list( selections if selections is not None else [] ) projections = [] for selection in selections: if isinstance(selection, str): projection = table[selection] else: projection = selection projections.append(projection) sort_keys = [ to_sort_key(table, k) for k in util.promote_list( sort_keys if sort_keys is not None else [] ) ] predicates = list( toolz.concat( map( L.flatten_predicate, predicates if predicates is not None else [], ) ) ) super().__init__( table=table, selections=projections, predicates=predicates, sort_keys=sort_keys, ) def _validate(self): from ibis.expr.analysis import FilterValidator # Need to validate that the column expressions are compatible with the # input table; this means they must either be scalar expressions or # array expressions originating from the same root table expression dependent_exprs = self.selections + self.sort_keys self.table._assert_valid(dependent_exprs) # Validate predicates validator = FilterValidator([self.table]) validator.validate_all(self.predicates) # Validate no overlapping columns in schema assert self.schema @cached_property def schema(self): # Resolve schema and initialize if not self.selections: return self.table.schema() types = [] names = [] for projection in self.selections: if isinstance(projection, ir.DestructColumn): # If this is a destruct, then we destructure # the result and assign to multiple columns struct_type = projection.type() for name in struct_type.names: names.append(name) types.append(struct_type[name]) elif isinstance(projection, ir.ValueExpr): names.append(projection.get_name()) types.append(projection.type()) elif isinstance(projection, ir.TableExpr): schema = projection.schema() names.extend(schema.names) types.extend(schema.types) return Schema(names, types) def blocks(self): return bool(self.selections) def substitute_table(self, table_expr): return Selection(table_expr, self.selections) def root_tables(self): return [self] def can_add_filters(self, wrapped_expr, predicates): pass @staticmethod def empty_or_equal(lefts, rights): return not lefts or not rights or all_equal(lefts, rights) def compatible_with(self, other): # self and other are equivalent except for predicates, selections, or # sort keys any of which is allowed to be empty. If both are not empty # then they must be equal if self.equals(other): return True if not isinstance(other, type(self)): return False return self.table.equals(other.table) and ( self.empty_or_equal(self.predicates, other.predicates) and self.empty_or_equal(self.selections, other.selections) and self.empty_or_equal(self.sort_keys, other.sort_keys) ) # Operator combination / fusion logic def aggregate(self, this, metrics, by=None, having=None): if len(self.selections) > 0: return Aggregation(this, metrics, by=by, having=having) else: helper = AggregateSelection(this, metrics, by, having) return helper.get_result() def sort_by(self, expr, sort_exprs): sort_exprs = util.promote_list(sort_exprs) if not self.blocks(): resolved_keys = _maybe_convert_sort_keys(self.table, sort_exprs) if resolved_keys and self.table._is_valid(resolved_keys): return Selection( self.table, self.selections, predicates=self.predicates, sort_keys=self.sort_keys + resolved_keys, ) return Selection(expr, [], sort_keys=sort_exprs) class AggregateSelection: # sort keys cannot be discarded because of order-dependent # aggregate functions like GROUP_CONCAT def __init__(self, parent, metrics, by, having): self.parent = parent self.op = parent.op() self.metrics = metrics self.by = by self.having = having def get_result(self): if self.op.blocks(): return self._plain_subquery() else: return self._attempt_pushdown() def _plain_subquery(self): return Aggregation( self.parent, self.metrics, by=self.by, having=self.having ) def _attempt_pushdown(self): metrics_valid, lowered_metrics = self._pushdown_exprs(self.metrics) by_valid, lowered_by = self._pushdown_exprs(self.by) having_valid, lowered_having = self._pushdown_exprs( self.having or None ) if metrics_valid and by_valid and having_valid: return Aggregation( self.op.table, lowered_metrics, by=lowered_by, having=lowered_having, predicates=self.op.predicates, sort_keys=self.op.sort_keys, ) else: return self._plain_subquery() def _pushdown_exprs(self, exprs): import ibis.expr.analysis as L if exprs is None: return True, [] resolved = self.op.table._resolve(exprs) subbed_exprs = [] valid = False if resolved: for x in util.promote_list(resolved): subbed = L.sub_for(x, [(self.parent, self.op.table)]) subbed_exprs.append(subbed) valid = self.op.table._is_valid(subbed_exprs) else: valid = False return valid, subbed_exprs def _maybe_convert_sort_keys(table, exprs): try: return [to_sort_key(table, k) for k in util.promote_list(exprs)] except com.IbisError: return None class Aggregation(TableNode, HasSchema): """ metrics : per-group scalar aggregates by : group expressions having : post-aggregation predicate TODO: not putting this in the aggregate operation yet where : pre-aggregation predicate """ table = Arg(ir.TableExpr) metrics = Arg(rlz.noop) by = Arg(rlz.noop) having = Arg(rlz.noop, default=None) predicates = Arg(rlz.noop, default=None) sort_keys = Arg(rlz.noop, default=None) def __init__( self, table, metrics, by=None, having=None, predicates=None, sort_keys=None, ): # For tables, like joins, that are not materialized metrics = self._rewrite_exprs(table, metrics) by = [] if by is None else by by = table._resolve(by) having = [] if having is None else having predicates = [] if predicates is None else predicates # order by only makes sense with group by in an aggregation sort_keys = [] if not by or sort_keys is None else sort_keys sort_keys = [ to_sort_key(table, k) for k in util.promote_list(sort_keys) ] by = self._rewrite_exprs(table, by) having = self._rewrite_exprs(table, having) predicates = self._rewrite_exprs(table, predicates) sort_keys = self._rewrite_exprs(table, sort_keys) super().__init__( table=table, metrics=metrics, by=by, having=having, predicates=predicates, sort_keys=sort_keys, ) def _validate(self): from ibis.expr.analysis import FilterValidator, is_reduction # All aggregates are valid for expr in self.metrics: if not isinstance(expr, ir.ScalarExpr) or not is_reduction(expr): raise TypeError( 'Passed a non-aggregate expression: %s' % _safe_repr(expr) ) for expr in self.having: if not isinstance(expr, ir.BooleanScalar): raise com.ExpressionError( 'Having clause must be boolean ' 'expression, was: {0!s}'.format(_safe_repr(expr)) ) # All non-scalar refs originate from the input table all_exprs = self.metrics + self.by + self.having + self.sort_keys self.table._assert_valid(all_exprs) # Validate predicates validator = FilterValidator([self.table]) validator.validate_all(self.predicates) # Validate schema has no overlapping columns assert self.schema def _rewrite_exprs(self, table, what): what = util.promote_list(what) all_exprs = [] for expr in what: if isinstance(expr, ir.ExprList): all_exprs.extend(expr.exprs()) else: bound_expr = ir.bind_expr(table, expr) all_exprs.append(bound_expr) return all_exprs # TODO - #2832 # this optimization becomes O(n^2) when it calls into # _lift_TableColumn in analysis.py, which itself is O(n) and is # called on each input to the aggregation - thus creating the # aggregation expression can be extremely slow on wide tables # that contain a Selection. # return [ # substitute_parents(x, past_projection=False) for x in all_exprs # ] def blocks(self): return True def substitute_table(self, table_expr): return Aggregation( table_expr, self.metrics, by=self.by, having=self.having ) @cached_property def schema(self): names = [] types = [] for e in self.by + self.metrics: if isinstance(e, ir.DestructValue): # If this is a destruct, then we destructure # the result and assign to multiple columns struct_type = e.type() for name in struct_type.names: names.append(name) types.append(struct_type[name]) else: names.append(e.get_name()) types.append(e.type()) return Schema(names, types) def sort_by(self, expr, sort_exprs): sort_exprs = util.promote_list(sort_exprs) resolved_keys = _maybe_convert_sort_keys(self.table, sort_exprs) if resolved_keys and self.table._is_valid(resolved_keys): return Aggregation( self.table, self.metrics, by=self.by, having=self.having, predicates=self.predicates, sort_keys=self.sort_keys + resolved_keys, ) return Selection(expr, [], sort_keys=sort_exprs) class NumericBinaryOp(BinaryOp): left = Arg(rlz.numeric) right = Arg(rlz.numeric) class Add(NumericBinaryOp): output_type = rlz.numeric_like('args', operator.add) class Multiply(NumericBinaryOp): output_type = rlz.numeric_like('args', operator.mul) class Power(NumericBinaryOp): def output_type(self): if util.all_of(self.args, ir.IntegerValue): return rlz.shape_like(self.args, dt.float64) else: return rlz.shape_like(self.args) class Subtract(NumericBinaryOp): output_type = rlz.numeric_like('args', operator.sub) class Divide(NumericBinaryOp): output_type = rlz.shape_like('args', dt.float64) class FloorDivide(Divide): output_type = rlz.shape_like('args', dt.int64) class LogicalBinaryOp(BinaryOp): left = Arg(rlz.boolean) right = Arg(rlz.boolean) output_type = rlz.shape_like('args', dt.boolean) class Not(UnaryOp): arg = Arg(rlz.boolean) output_type = rlz.shape_like('arg', dt.boolean) class Modulus(NumericBinaryOp): output_type = rlz.numeric_like('args', operator.mod) class And(LogicalBinaryOp): pass class Or(LogicalBinaryOp): pass class Xor(LogicalBinaryOp): pass class Comparison(BinaryOp, BooleanValueOp): left = Arg(rlz.any) right = Arg(rlz.any) def __init__(self, left, right): """ Casting rules for type promotions (for resolving the output type) may depend in some cases on the target backend. TODO: how will overflows be handled? Can we provide anything useful in Ibis to help the user avoid them? :param left: :param right: """ super().__init__(*self._maybe_cast_args(left, right)) def _maybe_cast_args(self, left, right): # it might not be necessary? with suppress(com.IbisTypeError): return left, rlz.cast(right, left) with suppress(com.IbisTypeError): return rlz.cast(left, right), right return left, right def output_type(self): if not rlz.comparable(self.left, self.right): raise TypeError( 'Arguments with datatype {} and {} are ' 'not comparable'.format(self.left.type(), self.right.type()) ) return rlz.shape_like(self.args, dt.boolean) class Equals(Comparison): pass class NotEquals(Comparison): pass class GreaterEqual(Comparison): pass class Greater(Comparison): pass class LessEqual(Comparison): pass class Less(Comparison): pass class IdenticalTo(Comparison): pass class Between(ValueOp, BooleanValueOp): arg = Arg(rlz.any) lower_bound = Arg(rlz.any) upper_bound = Arg(rlz.any) def output_type(self): arg, lower, upper = self.args if not (rlz.comparable(arg, lower) and rlz.comparable(arg, upper)): raise TypeError('Arguments are not comparable') return rlz.shape_like(self.args, dt.boolean) class BetweenTime(Between): arg = Arg(rlz.one_of([rlz.timestamp, rlz.time])) lower_bound = Arg(rlz.one_of([rlz.time, rlz.string])) upper_bound = Arg(rlz.one_of([rlz.time, rlz.string])) class Contains(ValueOp, BooleanValueOp): value = Arg(rlz.any) options = Arg( rlz.one_of( [ rlz.list_of(rlz.any), rlz.set_, rlz.column(rlz.any), rlz.array_of(rlz.any), ] ) ) def __init__(self, value, options): # it can be a single expression, like a column if not isinstance(options, ir.Expr): if util.any_of(options, ir.Expr): # or a list of expressions options = ir.sequence(options) else: # or a set of scalar values options = frozenset(options) super().__init__(value, options) def output_type(self): all_args = [self.value] if isinstance(self.options, ir.ListExpr): all_args += self.options else: all_args += [self.options] return rlz.shape_like(all_args, dt.boolean) class NotContains(Contains): pass class ReplaceValues(ValueOp): """ Apply a multi-value replacement on a particular column. As an example from SQL, given DAYOFWEEK(timestamp_col), replace 1 through 5 to "WEEKDAY" and 6 and 7 to "WEEKEND" """ pass class SummaryFilter(ValueOp): expr = Arg(rlz.noop) def output_type(self): return dt.boolean.column_type() class TopK(ValueOp): arg = Arg(rlz.noop) k = Arg(int) by = Arg(rlz.noop) def __init__(self, arg, k, by=None): if by is None: by = arg.count() if not isinstance(arg, ir.ColumnExpr): raise TypeError(arg) if not isinstance(k, int) or k < 0: raise ValueError('k must be positive integer, was: {0}'.format(k)) super().__init__(arg, k, by) def output_type(self): return ir.TopKExpr def blocks(self): return True class Constant(ValueOp): pass class TimestampNow(Constant): def output_type(self): return dt.timestamp.scalar_type() class RandomScalar(Constant): def output_type(self): return dt.float64.scalar_type() class E(Constant): def output_type(self): return functools.partial(ir.FloatingScalar, dtype=dt.float64) class Pi(Constant): """ The constant pi """ def output_type(self): return functools.partial(ir.FloatingScalar, dtype=dt.float64) class TemporalUnaryOp(UnaryOp): arg = Arg(rlz.temporal) class TimestampUnaryOp(UnaryOp): arg = Arg(rlz.timestamp) _date_units = { 'Y': 'Y', 'y': 'Y', 'year': 'Y', 'YEAR': 'Y', 'YYYY': 'Y', 'SYYYY': 'Y', 'YYY': 'Y', 'YY': 'Y', 'Q': 'Q', 'q': 'Q', 'quarter': 'Q', 'QUARTER': 'Q', 'M': 'M', 'month': 'M', 'MONTH': 'M', 'w': 'W', 'W': 'W', 'week': 'W', 'WEEK': 'W', 'd': 'D', 'D': 'D', 'J': 'D', 'day': 'D', 'DAY': 'D', } _time_units = { 'h': 'h', 'H': 'h', 'HH24': 'h', 'hour': 'h', 'HOUR': 'h', 'm': 'm', 'MI': 'm', 'minute': 'm', 'MINUTE': 'm', 's': 's', 'second': 's', 'SECOND': 's', 'ms': 'ms', 'millisecond': 'ms', 'MILLISECOND': 'ms', 'us': 'us', 'microsecond': 'ms', 'MICROSECOND': 'ms', 'ns': 'ns', 'nanosecond': 'ns', 'NANOSECOND': 'ns', } _timestamp_units = toolz.merge(_date_units, _time_units) class TimestampTruncate(ValueOp): arg = Arg(rlz.timestamp) unit = Arg(rlz.isin(_timestamp_units)) output_type = rlz.shape_like('arg', dt.timestamp) class DateTruncate(ValueOp): arg = Arg(rlz.date) unit = Arg(rlz.isin(_date_units)) output_type = rlz.shape_like('arg', dt.date) class TimeTruncate(ValueOp): arg = Arg(rlz.time) unit = Arg(rlz.isin(_time_units)) output_type = rlz.shape_like('arg', dt.time) class Strftime(ValueOp): arg = Arg(rlz.temporal) format_str = Arg(rlz.string) output_type = rlz.shape_like('arg', dt.string) class StringToTimestamp(ValueOp): arg = Arg(rlz.string) format_str = Arg(rlz.string) timezone = Arg(rlz.string, default=None) output_type = rlz.shape_like('arg', dt.Timestamp(timezone='UTC')) class ExtractTemporalField(TemporalUnaryOp): output_type = rlz.shape_like('arg', dt.int32) ExtractTimestampField = ExtractTemporalField class ExtractDateField(ExtractTemporalField): arg = Arg(rlz.one_of([rlz.date, rlz.timestamp])) class ExtractTimeField(ExtractTemporalField): arg = Arg(rlz.one_of([rlz.time, rlz.timestamp])) class ExtractYear(ExtractDateField): pass class ExtractMonth(ExtractDateField): pass class ExtractDay(ExtractDateField): pass class ExtractDayOfYear(ExtractDateField): pass class ExtractQuarter(ExtractDateField): pass class ExtractEpochSeconds(ExtractDateField): pass class ExtractWeekOfYear(ExtractDateField): pass class ExtractHour(ExtractTimeField): pass class ExtractMinute(ExtractTimeField): pass class ExtractSecond(ExtractTimeField): pass class ExtractMillisecond(ExtractTimeField): pass class DayOfWeekIndex(UnaryOp): arg = Arg(rlz.one_of([rlz.date, rlz.timestamp])) output_type = rlz.shape_like('arg', dt.int16) class DayOfWeekName(UnaryOp): arg = Arg(rlz.one_of([rlz.date, rlz.timestamp])) output_type = rlz.shape_like('arg', dt.string) class DayOfWeekNode(Node): arg = Arg(rlz.one_of([rlz.date, rlz.timestamp])) def output_type(self): return ir.DayOfWeek class Time(UnaryOp): output_type = rlz.shape_like('arg', dt.time) class Date(UnaryOp): output_type = rlz.shape_like('arg', dt.date) class TimestampFromUNIX(ValueOp): arg = Arg(rlz.any) # Only pandas-based backends support 'ns' unit = Arg(rlz.isin({'s', 'ms', 'us', 'ns'})) output_type = rlz.shape_like('arg', dt.timestamp) class DecimalUnaryOp(UnaryOp): arg = Arg(rlz.decimal) class DecimalPrecision(DecimalUnaryOp): output_type = rlz.shape_like('arg', dt.int32) class DecimalScale(UnaryOp): output_type = rlz.shape_like('arg', dt.int32) class Hash(ValueOp): arg = Arg(rlz.any) how = Arg(rlz.isin({'fnv', 'farm_fingerprint'})) output_type = rlz.shape_like('arg', dt.int64) class HashBytes(ValueOp): arg = Arg(rlz.one_of({rlz.value(dt.string), rlz.value(dt.binary)})) how = Arg(rlz.isin({'md5', 'sha1', 'sha256', 'sha512'})) output_type = rlz.shape_like('arg', dt.binary) class DateAdd(BinaryOp): left = Arg(rlz.date) right = Arg(rlz.interval(units={'Y', 'Q', 'M', 'W', 'D'})) output_type = rlz.shape_like('left') class DateSub(BinaryOp): left = Arg(rlz.date) right = Arg(rlz.interval(units={'Y', 'Q', 'M', 'W', 'D'})) output_type = rlz.shape_like('left') class DateDiff(BinaryOp): left = Arg(rlz.date) right = Arg(rlz.date) output_type = rlz.shape_like('left', dt.Interval('D')) class TimeAdd(BinaryOp): left = Arg(rlz.time) right = Arg(rlz.interval(units={'h', 'm', 's', 'ms', 'us', 'ns'})) output_type = rlz.shape_like('left') class TimeSub(BinaryOp): left = Arg(rlz.time) right = Arg(rlz.interval(units={'h', 'm', 's', 'ms', 'us', 'ns'})) output_type = rlz.shape_like('left') class TimeDiff(BinaryOp): left = Arg(rlz.time) right = Arg(rlz.time) output_type = rlz.shape_like('left', dt.Interval('s')) class TimestampAdd(BinaryOp): left = Arg(rlz.timestamp) right = Arg( rlz.interval( units={'Y', 'Q', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'} ) ) output_type = rlz.shape_like('left') class TimestampSub(BinaryOp): left = Arg(rlz.timestamp) right = Arg( rlz.interval( units={'Y', 'Q', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'} ) ) output_type = rlz.shape_like('left') class TimestampDiff(BinaryOp): left = Arg(rlz.timestamp) right = Arg(rlz.timestamp) output_type = rlz.shape_like('left', dt.Interval('s')) class IntervalBinaryOp(BinaryOp): def output_type(self): args = [ arg.cast(arg.type().value_type) if isinstance(arg.type(), dt.Interval) else arg for arg in self.args ] expr = rlz.numeric_like(args, self.__class__.op)(self) left_dtype = self.left.type() dtype_type = type(left_dtype) additional_args = { attr: getattr(left_dtype, attr) for attr in dtype_type.__slots__ if attr not in {'unit', 'value_type'} } dtype = dtype_type(left_dtype.unit, expr.type(), **additional_args) return rlz.shape_like(self.args, dtype=dtype) class IntervalAdd(IntervalBinaryOp): left = Arg(rlz.interval) right = Arg(rlz.interval) op = operator.add class IntervalSubtract(IntervalBinaryOp): left = Arg(rlz.interval) right = Arg(rlz.interval) op = operator.sub class IntervalMultiply(IntervalBinaryOp): left = Arg(rlz.interval) right = Arg(rlz.numeric) op = operator.mul class IntervalFloorDivide(IntervalBinaryOp): left = Arg(rlz.interval) right = Arg(rlz.numeric) op = operator.floordiv class IntervalFromInteger(ValueOp): arg = Arg(rlz.integer) unit = Arg( rlz.isin({'Y', 'Q', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'}) ) @property def resolution(self): return dt.Interval(self.unit).resolution def output_type(self): dtype = dt.Interval(self.unit, self.arg.type()) return rlz.shape_like(self.arg, dtype=dtype) class ArrayColumn(ValueOp): cols = Arg(rlz.list_of(rlz.column(rlz.any), min_length=1)) def _validate(self): if len({col.type() for col in self.cols}) > 1: raise com.IbisTypeError( f'The types of all input columns must match exactly in a ' f'{type(self).__name__} operation.' ) def output_type(self): first_dtype = self.cols[0].type() return dt.Array(first_dtype).column_type() class ArrayLength(UnaryOp): arg = Arg(rlz.array) output_type = rlz.shape_like('arg', dt.int64) class ArraySlice(ValueOp): arg = Arg(rlz.array) start = Arg(rlz.integer) stop = Arg(rlz.integer, default=None) output_type = rlz.typeof('arg') class ArrayIndex(ValueOp): arg = Arg(rlz.array) index = Arg(rlz.integer) def output_type(self): value_dtype = self.arg.type().value_type return rlz.shape_like(self.arg, value_dtype) class ArrayConcat(ValueOp): left = Arg(rlz.array) right = Arg(rlz.array) output_type = rlz.shape_like('left') def _validate(self): left_dtype, right_dtype = self.left.type(), self.right.type() if left_dtype != right_dtype: raise com.IbisTypeError( 'Array types must match exactly in a {} operation. ' 'Left type {} != Right type {}'.format( type(self).__name__, left_dtype, right_dtype ) ) class ArrayRepeat(ValueOp): arg = Arg(rlz.array) times = Arg(rlz.integer) output_type = rlz.typeof('arg') class ArrayCollect(Reduction): arg = Arg(rlz.column(rlz.any)) def output_type(self): dtype = dt.Array(self.arg.type()) return dtype.scalar_type() class MapLength(ValueOp): arg = Arg(rlz.mapping) output_type = rlz.shape_like('arg', dt.int64) class MapValueForKey(ValueOp): arg = Arg(rlz.mapping) key = Arg(rlz.one_of([rlz.string, rlz.integer])) def output_type(self): return rlz.shape_like(tuple(self.args), self.arg.type().value_type) class MapValueOrDefaultForKey(ValueOp): arg = Arg(rlz.mapping) key = Arg(rlz.one_of([rlz.string, rlz.integer])) default = Arg(rlz.any) def output_type(self): arg = self.arg default = self.default map_type = arg.type() value_type = map_type.value_type default_type = default.type() if default is not None and not dt.same_kind(default_type, value_type): raise com.IbisTypeError( "Default value\n{}\nof type {} cannot be cast to map's value " "type {}".format(default, default_type, value_type) ) result_type = dt.highest_precedence((default_type, value_type)) return rlz.shape_like(tuple(self.args), result_type) class MapKeys(ValueOp): arg = Arg(rlz.mapping) def output_type(self): arg = self.arg return rlz.shape_like(arg, dt.Array(arg.type().key_type)) class MapValues(ValueOp): arg = Arg(rlz.mapping) def output_type(self): arg = self.arg return rlz.shape_like(arg, dt.Array(arg.type().value_type)) class MapConcat(ValueOp): left = Arg(rlz.mapping) right = Arg(rlz.mapping) output_type = rlz.typeof('left') class StructField(ValueOp): arg = Arg(rlz.struct) field = Arg(str) def output_type(self): struct_dtype = self.arg.type() value_dtype = struct_dtype[self.field] return rlz.shape_like(self.arg, value_dtype) class Literal(ValueOp): value = Arg(rlz.noop) dtype = Arg(dt.dtype) def __repr__(self): return '{}({})'.format( type(self).__name__, ', '.join(map(repr, self.args)) ) def equals(self, other, cache=None): # Check types if not ( isinstance(other, Literal) and isinstance(other.value, type(self.value)) and self.dtype == other.dtype ): return False # Check values if isinstance(self.value, np.ndarray): return np.array_equal(self.value, other.value) else: return self.value == other.value def output_type(self): return self.dtype.scalar_type() def root_tables(self): return [] def __hash__(self) -> int: """Return the hash of a literal value. We override this method to make sure that we can handle things that aren't eminently hashable like an ``array<array<int64>>``. """ return hash(self.dtype._literal_value_hash_key(self.value)) class NullLiteral(Literal): """Typeless NULL literal""" value = Arg(type(None), default=None) dtype = Arg(dt.Null, default=dt.null) class ScalarParameter(ValueOp): _counter = itertools.count() dtype = Arg(dt.dtype) counter = Arg(int, default=lambda: next(ScalarParameter._counter)) def resolve_name(self): return 'param_{:d}'.format(self.counter) def __repr__(self): return '{}(type={})'.format(type(self).__name__, self.dtype) def __hash__(self): return hash((self.dtype, self.counter)) def output_type(self): return self.dtype.scalar_type() def equals(self, other, cache=None): return ( isinstance(other, ScalarParameter) and self.counter == other.counter and self.dtype.equals(other.dtype, cache=cache) ) @property def inputs(self): return () def root_tables(self): return [] class ExpressionList(Node): """Data structure for a list of arbitrary expressions""" exprs = Arg(rlz.noop) def __init__(self, values): super().__init__(list(map(rlz.any, values))) @property def inputs(self): return (tuple(self.exprs),) def root_tables(self): return distinct_roots(self.exprs) def output_type(self): return ir.ExprList class ValueList(ValueOp): """Data structure for a list of value expressions""" values = Arg(rlz.noop) display_argnames = False # disable showing argnames in repr def __init__(self, values): super().__init__(tuple(map(rlz.any, values))) def output_type(self): dtype = rlz.highest_precedence_dtype(self.values) return functools.partial(ir.ListExpr, dtype=dtype) def root_tables(self): return distinct_roots(*self.values) # ---------------------------------------------------------------------- # GeoSpatial operations class GeoSpatialBinOp(BinaryOp): """Geo Spatial base binary""" left = Arg(rlz.geospatial) right = Arg(rlz.geospatial) class GeoSpatialUnOp(UnaryOp): """Geo Spatial base unary""" arg = Arg(rlz.geospatial) class GeoDistance(GeoSpatialBinOp): """Returns minimum distance between two geo spatial data""" output_type = rlz.shape_like('args', dt.float64) class GeoContains(GeoSpatialBinOp): """Check if the first geo spatial data contains the second one""" output_type = rlz.shape_like('args', dt.boolean) class GeoContainsProperly(GeoSpatialBinOp): """Check if the first geo spatial data contains the second one, and no boundary points are shared.""" output_type = rlz.shape_like('args', dt.boolean) class GeoCovers(GeoSpatialBinOp): """Returns True if no point in Geometry B is outside Geometry A""" output_type = rlz.shape_like('args', dt.boolean) class GeoCoveredBy(GeoSpatialBinOp): """Returns True if no point in Geometry/Geography A is outside Geometry/Geography B""" output_type = rlz.shape_like('args', dt.boolean) class GeoCrosses(GeoSpatialBinOp): """Returns True if the supplied geometries have some, but not all, interior points in common.""" output_type = rlz.shape_like('args', dt.boolean) class GeoDisjoint(GeoSpatialBinOp): """Returns True if the Geometries do not “spatially intersect” - if they do not share any space together.""" output_type = rlz.shape_like('args', dt.boolean) class GeoEquals(GeoSpatialBinOp): """Returns True if the given geometries represent the same geometry.""" output_type = rlz.shape_like('args', dt.boolean) class GeoGeometryN(GeoSpatialUnOp): """Returns the Nth Geometry of a Multi geometry.""" n = Arg(rlz.integer) output_type = rlz.shape_like('args', dt.geometry) class GeoGeometryType(GeoSpatialUnOp): """Returns the type of the geometry.""" output_type = rlz.shape_like('args', dt.string) class GeoIntersects(GeoSpatialBinOp): """Returns True if the Geometries/Geography “spatially intersect in 2D” - (share any portion of space) and False if they don’t (they are Disjoint). """ output_type = rlz.shape_like('args', dt.boolean) class GeoIsValid(GeoSpatialUnOp): """Returns true if the geometry is well-formed.""" output_type = rlz.shape_like('args', dt.boolean) class GeoLineLocatePoint(GeoSpatialBinOp): """ Locate the distance a point falls along the length of a line. Returns a float between zero and one representing the location of the closest point on the linestring to the given point, as a fraction of the total 2d line length. """ left = Arg(rlz.linestring) right = Arg(rlz.point) output_type = rlz.shape_like('args', dt.halffloat) class GeoLineMerge(GeoSpatialUnOp): """ Merge a MultiLineString into a LineString. Returns a (set of) LineString(s) formed by sewing together the constituent line work of a multilinestring. If a geometry other than a linestring or multilinestring is given, this will return an empty geometry collection. """ output_type = rlz.shape_like('args', dt.geometry) class GeoLineSubstring(GeoSpatialUnOp): """ Clip a substring from a LineString. Returns a linestring that is a substring of the input one, starting and ending at the given fractions of the total 2d length. The second and third arguments are floating point values between zero and one. This only works with linestrings. """ arg = Arg(rlz.linestring) start = Arg(rlz.floating) end = Arg(rlz.floating) output_type = rlz.shape_like('args', dt.linestring) class GeoOrderingEquals(GeoSpatialBinOp): """ Check if two geometries are equal and have the same point ordering. Returns true if the two geometries are equal and the coordinates are in the same order. """ output_type = rlz.shape_like('args', dt.boolean) class GeoOverlaps(GeoSpatialBinOp): """Returns True if the Geometries share space, are of the same dimension, but are not completely contained by each other.""" output_type = rlz.shape_like('args', dt.boolean) class GeoTouches(GeoSpatialBinOp): """Returns True if the geometries have at least one point in common, but their interiors do not intersect.""" output_type = rlz.shape_like('args', dt.boolean) class GeoUnaryUnion(Reduction): """Returns the pointwise union of the geometries in the column.""" arg = Arg(rlz.column(rlz.geospatial)) def output_type(self): return dt.geometry.scalar_type() class GeoUnion(GeoSpatialBinOp): """Returns the pointwise union of the two geometries.""" output_type = rlz.shape_like('args', dt.geometry) class GeoArea(GeoSpatialUnOp): """Area of the geo spatial data""" output_type = rlz.shape_like('args', dt.float64) class GeoPerimeter(GeoSpatialUnOp): """Perimeter of the geo spatial data""" output_type = rlz.shape_like('args', dt.float64) class GeoLength(GeoSpatialUnOp): """Length of geo spatial data""" output_type = rlz.shape_like('args', dt.float64) class GeoMaxDistance(GeoSpatialBinOp): """Returns the 2-dimensional maximum distance between two geometries in projected units. If g1 and g2 is the same geometry the function will return the distance between the two vertices most far from each other in that geometry """ output_type = rlz.shape_like('args', dt.float64) class GeoX(GeoSpatialUnOp): """Return the X coordinate of the point, or NULL if not available. Input must be a point """ output_type = rlz.shape_like('args', dt.float64) class GeoY(GeoSpatialUnOp): """Return the Y coordinate of the point, or NULL if not available. Input must be a point """ output_type = rlz.shape_like('args', dt.float64) class GeoXMin(GeoSpatialUnOp): """Returns Y minima of a bounding box 2d or 3d or a geometry""" output_type = rlz.shape_like('args', dt.float64) class GeoXMax(GeoSpatialUnOp): """Returns X maxima of a bounding box 2d or 3d or a geometry""" output_type = rlz.shape_like('args', dt.float64) class GeoYMin(GeoSpatialUnOp): """Returns Y minima of a bounding box 2d or 3d or a geometry""" output_type = rlz.shape_like('args', dt.float64) class GeoYMax(GeoSpatialUnOp): """Returns Y maxima of a bounding box 2d or 3d or a geometry""" output_type = rlz.shape_like('args', dt.float64) class GeoStartPoint(GeoSpatialUnOp): """Returns the first point of a LINESTRING geometry as a POINT or NULL if the input parameter is not a LINESTRING """ output_type = rlz.shape_like('arg', dt.point) class GeoEndPoint(GeoSpatialUnOp): """Returns the last point of a LINESTRING geometry as a POINT or NULL if the input parameter is not a LINESTRING """ output_type = rlz.shape_like('arg', dt.point) class GeoPoint(GeoSpatialBinOp): """ Return a point constructed on the fly from the provided coordinate values. Constant coordinates result in construction of a POINT literal. """ left = Arg(rlz.numeric) right = Arg(rlz.numeric) output_type = rlz.shape_like('args', dt.point) class GeoPointN(GeoSpatialUnOp): """Return the Nth point in a single linestring in the geometry. Negative values are counted backwards from the end of the LineString, so that -1 is the last point. Returns NULL if there is no linestring in the geometry """ n = Arg(rlz.integer) output_type = rlz.shape_like('args', dt.point) class GeoNPoints(GeoSpatialUnOp): """Return the number of points in a geometry. Works for all geometries""" output_type = rlz.shape_like('args', dt.int64) class GeoNRings(GeoSpatialUnOp): """If the geometry is a polygon or multi-polygon returns the number of rings. It counts the outer rings as well """ output_type = rlz.shape_like('args', dt.int64) class GeoSRID(GeoSpatialUnOp): """Returns the spatial reference identifier for the ST_Geometry.""" output_type = rlz.shape_like('args', dt.int64) class GeoSetSRID(GeoSpatialUnOp): """Set the spatial reference identifier for the ST_Geometry.""" srid = Arg(rlz.integer) output_type = rlz.shape_like('args', dt.geometry) class GeoBuffer(GeoSpatialUnOp): """Returns a geometry that represents all points whose distance from this Geometry is less than or equal to distance. Calculations are in the Spatial Reference System of this Geometry. """ radius = Arg(rlz.floating) output_type = rlz.shape_like('args', dt.geometry) class GeoCentroid(GeoSpatialUnOp): """Returns the geometric center of a geometry.""" output_type = rlz.shape_like('arg', dt.point) class GeoDFullyWithin(GeoSpatialBinOp): """Returns True if the geometries are fully within the specified distance of one another. """ distance = Arg(rlz.floating) output_type = rlz.shape_like('args', dt.boolean) class GeoDWithin(GeoSpatialBinOp): """Returns True if the geometries are within the specified distance of one another. """ distance = Arg(rlz.floating) output_type = rlz.shape_like('args', dt.boolean) class GeoEnvelope(GeoSpatialUnOp): """Returns a geometry representing the boundingbox of the supplied geometry. """ output_type = rlz.shape_like('arg', dt.polygon) class GeoAzimuth(GeoSpatialBinOp): """Returns the angle in radians from the horizontal of the vector defined by pointA and pointB. Angle is computed clockwise from down-to-up: on the clock: 12=0; 3=PI/2; 6=PI; 9=3PI/2. """ left = Arg(rlz.point) right = Arg(rlz.point) output_type = rlz.shape_like('args', dt.float64) class GeoWithin(GeoSpatialBinOp): """Returns True if the geometry A is completely inside geometry B""" output_type = rlz.shape_like('args', dt.boolean) class GeoIntersection(GeoSpatialBinOp): """Returns a geometry that represents the point set intersection of the Geometries. """ output_type = rlz.shape_like('args', dt.geometry) class GeoDifference(GeoSpatialBinOp): """Returns a geometry that represents that part of geometry A that does not intersect with geometry B """ output_type = rlz.shape_like('args', dt.geometry) class GeoSimplify(GeoSpatialUnOp): """Returns a simplified version of the given geometry.""" tolerance = Arg(rlz.floating) preserve_collapsed = Arg(rlz.boolean) output_type = rlz.shape_like('arg', dt.geometry) class GeoTransform(GeoSpatialUnOp): """Returns a transformed version of the given geometry into a new SRID.""" srid = Arg(rlz.integer) output_type = rlz.shape_like('arg', dt.geometry) class GeoAsBinary(GeoSpatialUnOp): """Return the Well-Known Binary (WKB) representation of the geometry/geography without SRID meta data. """ output_type = rlz.shape_like('arg', dt.binary) class GeoAsEWKB(GeoSpatialUnOp): """Return the Well-Known Binary (WKB) representation of the geometry/geography with SRID meta data. """ output_type = rlz.shape_like('arg', dt.binary) class GeoAsEWKT(GeoSpatialUnOp): """Return the Well-Known Text (WKT) representation of the geometry/geography with SRID meta data. """ output_type = rlz.shape_like('arg', dt.string) class GeoAsText(GeoSpatialUnOp): """Return the Well-Known Text (WKT) representation of the geometry/geography without SRID metadata. """ output_type = rlz.shape_like('arg', dt.string) class ElementWiseVectorizedUDF(ValueOp): """Node for element wise UDF.""" func = Arg(callable) func_args = Arg(tuple) input_type = Arg(rlz.shape_like('func_args')) _output_type = Arg(rlz.noop) def __init__(self, func, args, input_type, output_type): self.func = func self.func_args = args self.input_type = input_type self._output_type = output_type @property def inputs(self): return self.func_args def output_type(self): return self._output_type.column_type() def root_tables(self): return distinct_roots(*self.func_args) class ReductionVectorizedUDF(Reduction): """Node for reduction UDF.""" func = Arg(callable) func_args = Arg(tuple) input_type = Arg(rlz.shape_like('func_args')) _output_type = Arg(rlz.noop) def __init__(self, func, args, input_type, output_type): self.func = func self.func_args = args self.input_type = input_type self._output_type = output_type @property def inputs(self): return self.func_args def output_type(self): return self._output_type.scalar_type() def root_tables(self): return distinct_roots(*self.func_args) class AnalyticVectorizedUDF(AnalyticOp): """Node for analytics UDF.""" func = Arg(callable) func_args = Arg(tuple) input_type = Arg(rlz.shape_like('func_args')) _output_type = Arg(rlz.noop) def __init__(self, func, args, input_type, output_type): self.func = func self.func_args = args self.input_type = input_type self._output_type = output_type @property def inputs(self): return self.func_args def output_type(self): return self._output_type.column_type() def root_tables(self): return distinct_roots(*self.func_args) class ExistsSubquery(Node): """Helper class""" foreign_table = Arg(rlz.noop) predicates = Arg(rlz.noop) def output_type(self): return ir.ExistsExpr class NotExistsSubquery(Node): foreign_table = Arg(rlz.noop) predicates = Arg(rlz.noop) def output_type(self): return ir.ExistsExpr
ibis/expr/operations.py
92,852
Absolute value Returns the arc cosine of x metrics : per-group scalar aggregates by : group expressions having : post-aggregation predicate TODO: not putting this in the aggregate operation yet where : pre-aggregation predicate Node for analytics UDF. Returns the arc sine of x Returns the arc tangent of x Returns the arc tangent of x and y A binary operation Aggregate bitwise AND operation. All elements in an integer column are ANDed together. This can be used to determine which bit flags are set on all elements. Resources: * `BigQuery BIT_AND <https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions#bit_and>`_ * `MySQL BIT_AND <https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html#function_bit-and>`_ Aggregate bitwise OR operation. All elements in an integer column are ORed together. This can be used to determine which bit flags are set on any element. Resources: * `BigQuery BIT_OR <https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions#bit_or>`_ * `MySQL BIT_OR <https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html#function_bit-or>`_ Aggregate bitwise XOR operation. All elements in an integer column are XORed together. This can be used as a parity checksum of element values. Resources: * `BigQuery BIT_XOR <https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions#bit_xor>`_ * `MySQL BIT_XOR <https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html#function_bit-xor>`_ Compute the approximate median of a set of comparable values using the Count-Min-Sketch algorithm. Exposed in Impala using APPX_MEDIAN. Return a capitalized version of input string Round up to the nearest integer value greater than or equal to this value Returns ------- ceiled : type depending on input Decimal values: yield decimal Other numeric values: yield integer (int32) Coefficient of correlation of a set of number pairs. Returns the cosine of x Returns the cotangent of x Covariance of a set of number pairs. Some databases have a CROSS JOIN operator, that may be preferential to use over an INNER JOIN with no predicates. Cumulative max. Requires an order window. Cumulative mean. Requires an order window. Cumulative min. Requires an order window. Cumulative sum. Requires an order window. Converts radians to degrees Compute position of first element within each equal-value group in sorted order, ignoring duplicate values. Examples -------- values ranks 1 0 1 0 2 1 2 1 2 1 3 2 Returns ------- ranks : Int64Column, starting from 0 Distinct is a table-level unique-ing operation. In SQL, you might have: SELECT DISTINCT foo FROM table SELECT DISTINCT foo, bar FROM table COUNT(DISTINCT ...) is really just syntactic suger, but we provide a distinct().count() nicety for users nonetheless. For all intents and purposes, like Distinct, but can be distinguished later for evaluation if the result should be array-like versus table-like. Also for calling count() Node for element wise UDF. Helper class Data structure for a list of arbitrary expressions Round down to the nearest integer value less than or equal to this value Returns ------- floored : type depending on input Decimal values: yield decimal Other numeric values: yield integer (int32) Area of the geo spatial data Return the Well-Known Binary (WKB) representation of the geometry/geography without SRID meta data. Return the Well-Known Binary (WKB) representation of the geometry/geography with SRID meta data. Return the Well-Known Text (WKT) representation of the geometry/geography with SRID meta data. Return the Well-Known Text (WKT) representation of the geometry/geography without SRID metadata. Returns the angle in radians from the horizontal of the vector defined by pointA and pointB. Angle is computed clockwise from down-to-up: on the clock: 12=0; 3=PI/2; 6=PI; 9=3PI/2. Returns a geometry that represents all points whose distance from this Geometry is less than or equal to distance. Calculations are in the Spatial Reference System of this Geometry. Returns the geometric center of a geometry. Check if the first geo spatial data contains the second one Check if the first geo spatial data contains the second one, and no boundary points are shared. Returns True if no point in Geometry/Geography A is outside Geometry/Geography B Returns True if no point in Geometry B is outside Geometry A Returns True if the supplied geometries have some, but not all, interior points in common. Returns True if the geometries are fully within the specified distance of one another. Returns True if the geometries are within the specified distance of one another. Returns a geometry that represents that part of geometry A that does not intersect with geometry B Returns True if the Geometries do not “spatially intersect” - if they do not share any space together. Returns minimum distance between two geo spatial data Returns the last point of a LINESTRING geometry as a POINT or NULL if the input parameter is not a LINESTRING Returns a geometry representing the boundingbox of the supplied geometry. Returns True if the given geometries represent the same geometry. Returns the Nth Geometry of a Multi geometry. Returns the type of the geometry. Returns a geometry that represents the point set intersection of the Geometries. Returns True if the Geometries/Geography “spatially intersect in 2D” - (share any portion of space) and False if they don’t (they are Disjoint). Returns true if the geometry is well-formed. Length of geo spatial data Locate the distance a point falls along the length of a line. Returns a float between zero and one representing the location of the closest point on the linestring to the given point, as a fraction of the total 2d line length. Merge a MultiLineString into a LineString. Returns a (set of) LineString(s) formed by sewing together the constituent line work of a multilinestring. If a geometry other than a linestring or multilinestring is given, this will return an empty geometry collection. Clip a substring from a LineString. Returns a linestring that is a substring of the input one, starting and ending at the given fractions of the total 2d length. The second and third arguments are floating point values between zero and one. This only works with linestrings. Returns the 2-dimensional maximum distance between two geometries in projected units. If g1 and g2 is the same geometry the function will return the distance between the two vertices most far from each other in that geometry Return the number of points in a geometry. Works for all geometries If the geometry is a polygon or multi-polygon returns the number of rings. It counts the outer rings as well Check if two geometries are equal and have the same point ordering. Returns true if the two geometries are equal and the coordinates are in the same order. Returns True if the Geometries share space, are of the same dimension, but are not completely contained by each other. Perimeter of the geo spatial data Return a point constructed on the fly from the provided coordinate values. Constant coordinates result in construction of a POINT literal. Return the Nth point in a single linestring in the geometry. Negative values are counted backwards from the end of the LineString, so that -1 is the last point. Returns NULL if there is no linestring in the geometry Returns the spatial reference identifier for the ST_Geometry. Set the spatial reference identifier for the ST_Geometry. Returns a simplified version of the given geometry. Geo Spatial base binary Geo Spatial base unary Returns the first point of a LINESTRING geometry as a POINT or NULL if the input parameter is not a LINESTRING Returns True if the geometries have at least one point in common, but their interiors do not intersect. Returns a transformed version of the given geometry into a new SRID. Returns the pointwise union of the geometries in the column. Returns the pointwise union of the two geometries. Returns True if the geometry A is completely inside geometry B Return the X coordinate of the point, or NULL if not available. Input must be a point Returns X maxima of a bounding box 2d or 3d or a geometry Returns Y minima of a bounding box 2d or 3d or a geometry Return the Y coordinate of the point, or NULL if not available. Input must be a point Returns Y maxima of a bounding box 2d or 3d or a geometry Returns Y minima of a bounding box 2d or 3d or a geometry Approximate number of unique values using HyperLogLog algorithm. Impala offers the NDV built-in function for this. Equivalent to (but perhaps implemented differently): case().when(expr.notnull(), expr) .else_(null_substitute_expr) Returns true if values are null Returns ------- isnull : boolean with dimension of caller Remove whitespace from left side of string Natural logarithm Logarithm base 10 Logarithm base 2 Convert string to all lowercase Compute position of first element within each equal-value group in sorted order. Examples -------- values ranks 1 0 1 0 2 2 2 2 2 2 3 5 Returns ------- ranks : Int64Column, starting from 0 Returns true if values are not null Returns ------- notnull : boolean with dimension of caller Set values to NULL if they equal the null_if_expr Set values to NULL if they equal to zero. Commonly used in cases where divide-by-zero would produce an overflow or infinity. Equivalent to (value == 0).ifelse(ibis.NA, value) Returns ------- maybe_nulled : type of caller Typeless NULL literal The constant pi Remove whitespace from right side of string Converts degrees to radians Node for reduction UDF. Apply a multi-value replacement on a particular column. As an example from SQL, given DAYOFWEEK(timestamp_col), replace 1 through 5 to "WEEKDAY" and 6 and 7 to "WEEKEND" Reverse string The row number (an autonumeric) of the returned result. Compute row number starting from 0 after sorting by column expression Examples -------- >>> import ibis >>> t = ibis.table([('values', dt.int64)]) >>> w = ibis.window(order_by=t.values) >>> row_num = ibis.row_number().over(w) >>> result = t[t.values, row_num.name('row_num')] Returns ------- row_number : Int64Column, starting from 0 A table sourced from the result set of a select query Returns the sine of x Compute length of strings Returns ------- length : int32 SQL ilike operation Remove whitespace from left and right sides of string (Temporary?) Helper operation class for SQL translation (fully formed table subqueries to be viewed as arrays) Selects a column from a TableExpr Returns the tangent of x Trigonometric base binary Trigonometric base unary Convert string to all uppercase Data structure for a list of value expressions Ternary case expression, equivalent to bool_expr.case() .when(True, true_expr) .else_(false_or_null_expr) The attributes _expr_cached and _hash are used as caches; they can be excluded from serialization without affecting correctness. Excluding _expr_cached and _hash from serialization will allow the serialized bytes to be the same for equivalent Node objets. Returns ------- Dict[str, Any] A dictionary storing the objects attributes. Return the hash of a literal value. We override this method to make sure that we can handle things that aren't eminently hashable like an ``array<array<int64>>``. Casting rules for type promotions (for resolving the output type) may depend in some cases on the target backend. TODO: how will overflows be handled? Can we provide anything useful in Ibis to help the user avoid them? :param left: :param right: Parameters ---------- state: Dict[str, Any] A dictionary storing the objects attributes. Check whether two objects `left` and `right` are equal. Parameters ---------- left : Union[object, Expr, Node] right : Union[object, Expr, Node] cache : Optional[Dict[Tuple[Node, Node], bool]] A dictionary indicating whether two Nodes are equal Only valid if the distinct contains a single column Specify Returns ------- builder : CaseBuilder This function must resolve the output type of the expression and return the node wrapped in the appropriate ValueExpr type. Add a new case-result pair. Parameters ---------- case : Expr Expression to equality-compare with base expression. Must be comparable with the base. result : Expr Value when the case predicate evaluates to true. Returns ------- builder : CaseBuilder Add a new case-result pair. Parameters ---------- case : Expr Expression to equality-compare with base expression. Must be comparable with the base. result : Expr Value when the case predicate evaluates to true. Returns ------- builder : CaseBuilder TODO: move to analysis The contents of this node at referentially distinct and may not be analyzed deeper check that left and right are equal length iterables and that all of their elements are equal noqa: E731 see 396 for the issue preventing this def resolve_name(self): return self.args[0].get_name() According to Impala documentation: Return type: same as the initial argument value, except that integer values are promoted to BIGINT and floating-point values are promoted to DOUBLE; use CAST() when inserting into a smaller numeric column self.arg is a list of value expressions TRIGONOMETRIC OPERATIONS ---------------------------------------------------------------------- Impala 2.0 and higher returns a DOUBLE return ir.DoubleScalar ---------------------------------------------------------------------- Analytic functions Equivalent to SQL RANK() Equivalent to SQL DENSE_RANK() Equivalent to SQL ROW_NUMBER() ---------------------------------------------------------------------- Distinct stuff check whether schema has overlapping columns or not --------------------------------------------------------------------- Boolean reductions and semi/anti join support Depending on the kind of input boolean array, the result might either be array-like (an existence-type predicate) or scalar (a reduction) --------------------------------------------------------------------- Maintain immutability Maintain immutability Maintain immutability see GH 667 If left and right table have a common parent expression (e.g. they have different filters), must add a self-reference and make the appropriate substitution in the join predicates Validate join predicates. Each predicate must be valid jointly when considering the roots of each input table For joins retaining both table schemas, merge them together here Unraveling is not possible check whether the underlying schema has overlapping columns or not -------------------------------------------------------------------- Sorting Temporary TODO: might generalize this equals based on fields requires a proxy class with equals for non expr values The dependencies of this operation are not walked, which makes the table expression holding this relationally distinct from other expressions, so things like self-joins are possible Argument cleaning Need to validate that the column expressions are compatible with the input table; this means they must either be scalar expressions or array expressions originating from the same root table expression Validate predicates Validate no overlapping columns in schema Resolve schema and initialize If this is a destruct, then we destructure the result and assign to multiple columns self and other are equivalent except for predicates, selections, or sort keys any of which is allowed to be empty. If both are not empty then they must be equal Operator combination / fusion logic sort keys cannot be discarded because of order-dependent aggregate functions like GROUP_CONCAT For tables, like joins, that are not materialized order by only makes sense with group by in an aggregation All aggregates are valid All non-scalar refs originate from the input table Validate predicates Validate schema has no overlapping columns TODO - 2832 this optimization becomes O(n^2) when it calls into _lift_TableColumn in analysis.py, which itself is O(n) and is called on each input to the aggregation - thus creating the aggregation expression can be extremely slow on wide tables that contain a Selection. return [ substitute_parents(x, past_projection=False) for x in all_exprs ] If this is a destruct, then we destructure the result and assign to multiple columns it might not be necessary? it can be a single expression, like a column or a list of expressions or a set of scalar values Only pandas-based backends support 'ns' Check types Check values disable showing argnames in repr ---------------------------------------------------------------------- GeoSpatial operations
16,739
en
0.735784
# coding=utf-8 # *** WARNING: this file was generated by the Pulumi SDK Generator. *** # *** Do not edit by hand unless you're certain you know what you are doing! *** import warnings import pulumi import pulumi.runtime from typing import Any, Mapping, Optional, Sequence, Union from .. import _utilities, _tables from . import outputs from ._enums import * from ._inputs import * __all__ = ['Schedule'] class Schedule(pulumi.CustomResource): def __init__(__self__, resource_name: str, opts: Optional[pulumi.ResourceOptions] = None, daily_recurrence: Optional[pulumi.Input[pulumi.InputType['DayDetailsArgs']]] = None, hourly_recurrence: Optional[pulumi.Input[pulumi.InputType['HourDetailsArgs']]] = None, lab_name: Optional[pulumi.Input[str]] = None, location: Optional[pulumi.Input[str]] = None, name: Optional[pulumi.Input[str]] = None, notification_settings: Optional[pulumi.Input[pulumi.InputType['NotificationSettingsArgs']]] = None, resource_group_name: Optional[pulumi.Input[str]] = None, status: Optional[pulumi.Input[Union[str, 'EnableStatus']]] = None, tags: Optional[pulumi.Input[Mapping[str, pulumi.Input[str]]]] = None, target_resource_id: Optional[pulumi.Input[str]] = None, task_type: Optional[pulumi.Input[str]] = None, time_zone_id: Optional[pulumi.Input[str]] = None, weekly_recurrence: Optional[pulumi.Input[pulumi.InputType['WeekDetailsArgs']]] = None, __props__=None, __name__=None, __opts__=None): """ A schedule. API Version: 2018-09-15. :param str resource_name: The name of the resource. :param pulumi.ResourceOptions opts: Options for the resource. :param pulumi.Input[pulumi.InputType['DayDetailsArgs']] daily_recurrence: If the schedule will occur once each day of the week, specify the daily recurrence. :param pulumi.Input[pulumi.InputType['HourDetailsArgs']] hourly_recurrence: If the schedule will occur multiple times a day, specify the hourly recurrence. :param pulumi.Input[str] lab_name: The name of the lab. :param pulumi.Input[str] location: The location of the resource. :param pulumi.Input[str] name: The name of the schedule. :param pulumi.Input[pulumi.InputType['NotificationSettingsArgs']] notification_settings: Notification settings. :param pulumi.Input[str] resource_group_name: The name of the resource group. :param pulumi.Input[Union[str, 'EnableStatus']] status: The status of the schedule (i.e. Enabled, Disabled) :param pulumi.Input[Mapping[str, pulumi.Input[str]]] tags: The tags of the resource. :param pulumi.Input[str] target_resource_id: The resource ID to which the schedule belongs :param pulumi.Input[str] task_type: The task type of the schedule (e.g. LabVmsShutdownTask, LabVmAutoStart). :param pulumi.Input[str] time_zone_id: The time zone ID (e.g. Pacific Standard time). :param pulumi.Input[pulumi.InputType['WeekDetailsArgs']] weekly_recurrence: If the schedule will occur only some days of the week, specify the weekly recurrence. """ if __name__ is not None: warnings.warn("explicit use of __name__ is deprecated", DeprecationWarning) resource_name = __name__ if __opts__ is not None: warnings.warn("explicit use of __opts__ is deprecated, use 'opts' instead", DeprecationWarning) opts = __opts__ if opts is None: opts = pulumi.ResourceOptions() if not isinstance(opts, pulumi.ResourceOptions): raise TypeError('Expected resource options to be a ResourceOptions instance') if opts.version is None: opts.version = _utilities.get_version() if opts.id is None: if __props__ is not None: raise TypeError('__props__ is only valid when passed in combination with a valid opts.id to get an existing resource') __props__ = dict() __props__['daily_recurrence'] = daily_recurrence __props__['hourly_recurrence'] = hourly_recurrence if lab_name is None and not opts.urn: raise TypeError("Missing required property 'lab_name'") __props__['lab_name'] = lab_name __props__['location'] = location __props__['name'] = name __props__['notification_settings'] = notification_settings if resource_group_name is None and not opts.urn: raise TypeError("Missing required property 'resource_group_name'") __props__['resource_group_name'] = resource_group_name __props__['status'] = status __props__['tags'] = tags __props__['target_resource_id'] = target_resource_id __props__['task_type'] = task_type __props__['time_zone_id'] = time_zone_id __props__['weekly_recurrence'] = weekly_recurrence __props__['created_date'] = None __props__['provisioning_state'] = None __props__['type'] = None __props__['unique_identifier'] = None alias_opts = pulumi.ResourceOptions(aliases=[pulumi.Alias(type_="azure-nextgen:devtestlab/latest:Schedule"), pulumi.Alias(type_="azure-nextgen:devtestlab/v20150521preview:Schedule"), pulumi.Alias(type_="azure-nextgen:devtestlab/v20160515:Schedule"), pulumi.Alias(type_="azure-nextgen:devtestlab/v20180915:Schedule")]) opts = pulumi.ResourceOptions.merge(opts, alias_opts) super(Schedule, __self__).__init__( 'azure-nextgen:devtestlab:Schedule', resource_name, __props__, opts) @staticmethod def get(resource_name: str, id: pulumi.Input[str], opts: Optional[pulumi.ResourceOptions] = None) -> 'Schedule': """ Get an existing Schedule resource's state with the given name, id, and optional extra properties used to qualify the lookup. :param str resource_name: The unique name of the resulting resource. :param pulumi.Input[str] id: The unique provider ID of the resource to lookup. :param pulumi.ResourceOptions opts: Options for the resource. """ opts = pulumi.ResourceOptions.merge(opts, pulumi.ResourceOptions(id=id)) __props__ = dict() return Schedule(resource_name, opts=opts, __props__=__props__) @property @pulumi.getter(name="createdDate") def created_date(self) -> pulumi.Output[str]: """ The creation date of the schedule. """ return pulumi.get(self, "created_date") @property @pulumi.getter(name="dailyRecurrence") def daily_recurrence(self) -> pulumi.Output[Optional['outputs.DayDetailsResponse']]: """ If the schedule will occur once each day of the week, specify the daily recurrence. """ return pulumi.get(self, "daily_recurrence") @property @pulumi.getter(name="hourlyRecurrence") def hourly_recurrence(self) -> pulumi.Output[Optional['outputs.HourDetailsResponse']]: """ If the schedule will occur multiple times a day, specify the hourly recurrence. """ return pulumi.get(self, "hourly_recurrence") @property @pulumi.getter def location(self) -> pulumi.Output[Optional[str]]: """ The location of the resource. """ return pulumi.get(self, "location") @property @pulumi.getter def name(self) -> pulumi.Output[str]: """ The name of the resource. """ return pulumi.get(self, "name") @property @pulumi.getter(name="notificationSettings") def notification_settings(self) -> pulumi.Output[Optional['outputs.NotificationSettingsResponse']]: """ Notification settings. """ return pulumi.get(self, "notification_settings") @property @pulumi.getter(name="provisioningState") def provisioning_state(self) -> pulumi.Output[str]: """ The provisioning status of the resource. """ return pulumi.get(self, "provisioning_state") @property @pulumi.getter def status(self) -> pulumi.Output[Optional[str]]: """ The status of the schedule (i.e. Enabled, Disabled) """ return pulumi.get(self, "status") @property @pulumi.getter def tags(self) -> pulumi.Output[Optional[Mapping[str, str]]]: """ The tags of the resource. """ return pulumi.get(self, "tags") @property @pulumi.getter(name="targetResourceId") def target_resource_id(self) -> pulumi.Output[Optional[str]]: """ The resource ID to which the schedule belongs """ return pulumi.get(self, "target_resource_id") @property @pulumi.getter(name="taskType") def task_type(self) -> pulumi.Output[Optional[str]]: """ The task type of the schedule (e.g. LabVmsShutdownTask, LabVmAutoStart). """ return pulumi.get(self, "task_type") @property @pulumi.getter(name="timeZoneId") def time_zone_id(self) -> pulumi.Output[Optional[str]]: """ The time zone ID (e.g. Pacific Standard time). """ return pulumi.get(self, "time_zone_id") @property @pulumi.getter def type(self) -> pulumi.Output[str]: """ The type of the resource. """ return pulumi.get(self, "type") @property @pulumi.getter(name="uniqueIdentifier") def unique_identifier(self) -> pulumi.Output[str]: """ The unique immutable identifier of a resource (Guid). """ return pulumi.get(self, "unique_identifier") @property @pulumi.getter(name="weeklyRecurrence") def weekly_recurrence(self) -> pulumi.Output[Optional['outputs.WeekDetailsResponse']]: """ If the schedule will occur only some days of the week, specify the weekly recurrence. """ return pulumi.get(self, "weekly_recurrence") def translate_output_property(self, prop): return _tables.CAMEL_TO_SNAKE_CASE_TABLE.get(prop) or prop def translate_input_property(self, prop): return _tables.SNAKE_TO_CAMEL_CASE_TABLE.get(prop) or prop
sdk/python/pulumi_azure_nextgen/devtestlab/schedule.py
10,550
A schedule. API Version: 2018-09-15. :param str resource_name: The name of the resource. :param pulumi.ResourceOptions opts: Options for the resource. :param pulumi.Input[pulumi.InputType['DayDetailsArgs']] daily_recurrence: If the schedule will occur once each day of the week, specify the daily recurrence. :param pulumi.Input[pulumi.InputType['HourDetailsArgs']] hourly_recurrence: If the schedule will occur multiple times a day, specify the hourly recurrence. :param pulumi.Input[str] lab_name: The name of the lab. :param pulumi.Input[str] location: The location of the resource. :param pulumi.Input[str] name: The name of the schedule. :param pulumi.Input[pulumi.InputType['NotificationSettingsArgs']] notification_settings: Notification settings. :param pulumi.Input[str] resource_group_name: The name of the resource group. :param pulumi.Input[Union[str, 'EnableStatus']] status: The status of the schedule (i.e. Enabled, Disabled) :param pulumi.Input[Mapping[str, pulumi.Input[str]]] tags: The tags of the resource. :param pulumi.Input[str] target_resource_id: The resource ID to which the schedule belongs :param pulumi.Input[str] task_type: The task type of the schedule (e.g. LabVmsShutdownTask, LabVmAutoStart). :param pulumi.Input[str] time_zone_id: The time zone ID (e.g. Pacific Standard time). :param pulumi.Input[pulumi.InputType['WeekDetailsArgs']] weekly_recurrence: If the schedule will occur only some days of the week, specify the weekly recurrence. The creation date of the schedule. If the schedule will occur once each day of the week, specify the daily recurrence. Get an existing Schedule resource's state with the given name, id, and optional extra properties used to qualify the lookup. :param str resource_name: The unique name of the resulting resource. :param pulumi.Input[str] id: The unique provider ID of the resource to lookup. :param pulumi.ResourceOptions opts: Options for the resource. If the schedule will occur multiple times a day, specify the hourly recurrence. The location of the resource. The name of the resource. Notification settings. The provisioning status of the resource. The status of the schedule (i.e. Enabled, Disabled) The tags of the resource. The resource ID to which the schedule belongs The task type of the schedule (e.g. LabVmsShutdownTask, LabVmAutoStart). The time zone ID (e.g. Pacific Standard time). The type of the resource. The unique immutable identifier of a resource (Guid). If the schedule will occur only some days of the week, specify the weekly recurrence. coding=utf-8 *** WARNING: this file was generated by the Pulumi SDK Generator. *** *** Do not edit by hand unless you're certain you know what you are doing! ***
2,703
en
0.577085
# Qiwi module advanced usage example v1.00 # 17/05/2021 # https://t.me/ssleg © 2021 import logging import qiwi_module # настройка логфлайла test,log, туда будут записываться все ошибки и предупреждения. lfile = logging.FileHandler('test.log', 'a', 'utf-8') lfile.setFormatter(logging.Formatter('%(levelname)s %(module)-13s [%(asctime)s] %(message)s')) # noinspection PyArgumentList logging.basicConfig(level=logging.INFO, handlers=[lfile]) # простой вариант использования смотрите в файле sample.py # если у вас настроен свой внешний вид формы платежа, необходимо передать код темы модулю. # это делается один раз, при его инициализации. # сам код и настройки формы находятся на странице https://qiwi.com/p2p-admin/transfers/link theme_code = 'Ivanov-XX-vvv-k_' # перед любым использованием необходима однократная инициализация модуля. qiwi_module.init(theme_code) # создание счета на 1 рубль. При успехе получаете url с формой оплаты для клиента. # при неуспехе возвращается False с подробной записью в лог. # идентификаторы счетов придумываете и сохраняете вы сами, они должны быть уникальными всегда. bill_id = 'bill_2021_00000002' # по умолчанию счет действителен 15 минут, но вы можете поставить свое время, например сутки и 1 минуту. valid_hours = 24 valid_minutes = 1 # есть так же поле для комментария, его видит клиент в форме оплаты. например, туда можно записать детали заказа comment = 'Винт с левой резьбой для Сидорова.' invoice_url = qiwi_module.create_bill(1.00, bill_id, comment, valid_hours, valid_minutes) print(invoice_url) # проверка статуса оплаты. # возвращает одно из четырех возможных значений, если успешно или False и запись в лог. # 'WAITING' - cчет выставлен, ожидает оплаты. # 'PAID' - cчет оплачен. # 'REJECTED' - счет отменен с вашей стороны. # 'EXPIRED' - счет не оплачен и истек срок его действия. # можно вызывать ежесекундно или реже. pay_status = qiwi_module.bill_status(bill_id) print(pay_status) # отмена счета, если вам это необходимо. # возврашает 'REJECTED' если успешно, иначе False и запись в лог. bill_status = qiwi_module.cancel_bill(bill_id) print(bill_status)
adv_sample.py
3,101
Qiwi module advanced usage example v1.00 17/05/2021 https://t.me/ssleg © 2021 настройка логфлайла test,log, туда будут записываться все ошибки и предупреждения. noinspection PyArgumentList простой вариант использования смотрите в файле sample.py если у вас настроен свой внешний вид формы платежа, необходимо передать код темы модулю. это делается один раз, при его инициализации. сам код и настройки формы находятся на странице https://qiwi.com/p2p-admin/transfers/link перед любым использованием необходима однократная инициализация модуля. создание счета на 1 рубль. При успехе получаете url с формой оплаты для клиента. при неуспехе возвращается False с подробной записью в лог. идентификаторы счетов придумываете и сохраняете вы сами, они должны быть уникальными всегда. по умолчанию счет действителен 15 минут, но вы можете поставить свое время, например сутки и 1 минуту. есть так же поле для комментария, его видит клиент в форме оплаты. например, туда можно записать детали заказа проверка статуса оплаты. возвращает одно из четырех возможных значений, если успешно или False и запись в лог. 'WAITING' - cчет выставлен, ожидает оплаты. 'PAID' - cчет оплачен. 'REJECTED' - счет отменен с вашей стороны. 'EXPIRED' - счет не оплачен и истек срок его действия. можно вызывать ежесекундно или реже. отмена счета, если вам это необходимо. возврашает 'REJECTED' если успешно, иначе False и запись в лог.
1,406
ru
0.989675
# -*- coding: utf-8 -*- # PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN: # https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code from ccxt.base.exchange import Exchange # ----------------------------------------------------------------------------- try: basestring # Python 3 except NameError: basestring = str # Python 2 import hashlib from ccxt.base.errors import ExchangeError from ccxt.base.errors import AuthenticationError from ccxt.base.errors import PermissionDenied from ccxt.base.errors import AccountSuspended from ccxt.base.errors import ArgumentsRequired from ccxt.base.errors import BadRequest from ccxt.base.errors import BadSymbol from ccxt.base.errors import InsufficientFunds from ccxt.base.errors import InvalidAddress from ccxt.base.errors import InvalidOrder from ccxt.base.errors import OrderNotFound from ccxt.base.errors import CancelPending from ccxt.base.errors import NotSupported from ccxt.base.errors import DDoSProtection from ccxt.base.errors import RateLimitExceeded from ccxt.base.errors import ExchangeNotAvailable from ccxt.base.errors import OnMaintenance from ccxt.base.errors import InvalidNonce from ccxt.base.errors import RequestTimeout from ccxt.base.decimal_to_precision import TRUNCATE from ccxt.base.decimal_to_precision import TICK_SIZE class okex(Exchange): def describe(self): return self.deep_extend(super(okex, self).describe(), { 'id': 'okex', 'name': 'OKEX', 'countries': ['CN', 'US'], 'version': 'v3', 'rateLimit': 1000, # up to 3000 requests per 5 minutes ≈ 600 requests per minute ≈ 10 requests per second ≈ 100 ms 'pro': True, 'has': { 'cancelOrder': True, 'CORS': False, 'createOrder': True, 'fetchBalance': True, 'fetchClosedOrders': True, 'fetchCurrencies': False, # see below 'fetchDepositAddress': True, 'fetchDeposits': True, 'fetchLedger': True, 'fetchMarkets': True, 'fetchMyTrades': True, 'fetchOHLCV': True, 'fetchOpenOrders': True, 'fetchOrder': True, 'fetchOrderBook': True, 'fetchOrders': False, 'fetchOrderTrades': True, 'fetchTime': True, 'fetchTicker': True, 'fetchTickers': True, 'fetchTrades': True, 'fetchTransactions': False, 'fetchWithdrawals': True, 'futures': True, 'withdraw': True, }, 'timeframes': { '1m': '60', '3m': '180', '5m': '300', '15m': '900', '30m': '1800', '1h': '3600', '2h': '7200', '4h': '14400', '6h': '21600', '12h': '43200', '1d': '86400', '1w': '604800', '1M': '2678400', '3M': '8035200', '6M': '16070400', '1y': '31536000', }, 'hostname': 'okex.com', 'urls': { 'logo': 'https://user-images.githubusercontent.com/1294454/32552768-0d6dd3c6-c4a6-11e7-90f8-c043b64756a7.jpg', 'api': { 'rest': 'https://www.{hostname}', }, 'www': 'https://www.okex.com', 'doc': 'https://www.okex.com/docs/en/', 'fees': 'https://www.okex.com/pages/products/fees.html', 'referral': 'https://www.okex.com/join/1888677', 'test': { 'rest': 'https://testnet.okex.com', }, }, 'api': { 'general': { 'get': [ 'time', ], }, 'account': { 'get': [ 'wallet', 'sub-account', 'asset-valuation', 'wallet/{currency}', 'withdrawal/history', 'withdrawal/history/{currency}', 'ledger', 'deposit/address', 'deposit/history', 'deposit/history/{currency}', 'currencies', 'withdrawal/fee', ], 'post': [ 'transfer', 'withdrawal', ], }, 'spot': { 'get': [ 'accounts', 'accounts/{currency}', 'accounts/{currency}/ledger', 'orders', 'orders_pending', 'orders/{order_id}', 'orders/{client_oid}', 'trade_fee', 'fills', 'algo', # public 'instruments', 'instruments/{instrument_id}/book', 'instruments/ticker', 'instruments/{instrument_id}/ticker', 'instruments/{instrument_id}/trades', 'instruments/{instrument_id}/candles', 'instruments/{instrument_id}/history/candles', ], 'post': [ 'order_algo', 'orders', 'batch_orders', 'cancel_orders/{order_id}', 'cancel_orders/{client_oid}', 'cancel_batch_algos', 'cancel_batch_orders', ], }, 'margin': { 'get': [ 'accounts', 'accounts/{instrument_id}', 'accounts/{instrument_id}/ledger', 'accounts/availability', 'accounts/{instrument_id}/availability', 'accounts/borrowed', 'accounts/{instrument_id}/borrowed', 'orders', 'accounts/{instrument_id}/leverage', 'orders/{order_id}', 'orders/{client_oid}', 'orders_pending', 'fills', # public 'instruments/{instrument_id}/mark_price', ], 'post': [ 'accounts/borrow', 'accounts/repayment', 'orders', 'batch_orders', 'cancel_orders', 'cancel_orders/{order_id}', 'cancel_orders/{client_oid}', 'cancel_batch_orders', 'accounts/{instrument_id}/leverage', ], }, 'futures': { 'get': [ 'position', '{instrument_id}/position', 'accounts', 'accounts/{underlying}', 'accounts/{underlying}/leverage', 'accounts/{underlying}/ledger', 'order_algo/{instrument_id}', 'orders/{instrument_id}', 'orders/{instrument_id}/{order_id}', 'orders/{instrument_id}/{client_oid}', 'fills', 'trade_fee', 'accounts/{instrument_id}/holds', 'order_algo/{instrument_id}', # public 'instruments', 'instruments/{instrument_id}/book', 'instruments/ticker', 'instruments/{instrument_id}/ticker', 'instruments/{instrument_id}/trades', 'instruments/{instrument_id}/candles', 'instruments/{instrument_id}/history/candles', 'instruments/{instrument_id}/index', 'rate', 'instruments/{instrument_id}/estimated_price', 'instruments/{instrument_id}/open_interest', 'instruments/{instrument_id}/price_limit', 'instruments/{instrument_id}/mark_price', 'instruments/{instrument_id}/liquidation', ], 'post': [ 'accounts/{underlying}/leverage', 'order', 'orders', 'cancel_order/{instrument_id}/{order_id}', 'cancel_order/{instrument_id}/{client_oid}', 'cancel_batch_orders/{instrument_id}', 'accounts/margin_mode', 'close_position', 'cancel_all', 'order_algo', 'cancel_algos', ], }, 'swap': { 'get': [ 'position', '{instrument_id}/position', 'accounts', '{instrument_id}/accounts', 'accounts/{instrument_id}/settings', 'accounts/{instrument_id}/ledger', 'orders/{instrument_id}', 'orders/{instrument_id}/{order_id}', 'orders/{instrument_id}/{client_oid}', 'fills', 'accounts/{instrument_id}/holds', 'trade_fee', 'order_algo/{instrument_id}', # public 'instruments', 'instruments/{instrument_id}/depth', 'instruments/ticker', 'instruments/{instrument_id}/ticker', 'instruments/{instrument_id}/trades', 'instruments/{instrument_id}/candles', 'instruments/{instrument_id}/history/candles', 'instruments/{instrument_id}/index', 'rate', 'instruments/{instrument_id}/open_interest', 'instruments/{instrument_id}/price_limit', 'instruments/{instrument_id}/liquidation', 'instruments/{instrument_id}/funding_time', 'instruments/{instrument_id}/mark_price', 'instruments/{instrument_id}/historical_funding_rate', ], 'post': [ 'accounts/{instrument_id}/leverage', 'order', 'orders', 'cancel_order/{instrument_id}/{order_id}', 'cancel_order/{instrument_id}/{client_oid}', 'cancel_batch_orders/{instrument_id}', 'order_algo', 'cancel_algos', 'close_position', 'cancel_all', 'order_algo', 'cancel_algos', ], }, 'option': { 'get': [ 'accounts', 'position', '{underlying}/position', 'accounts/{underlying}', 'orders/{underlying}', 'fills/{underlying}', 'accounts/{underlying}/ledger', 'trade_fee', 'orders/{underlying}/{order_id}', 'orders/{underlying}/{client_oid}', # public 'underlying', 'instruments/{underlying}', 'instruments/{underlying}/summary', 'instruments/{underlying}/summary/{instrument_id}', 'instruments/{instrument_id}/book', 'instruments/{instrument_id}/trades', 'instruments/{instrument_id}/ticker', 'instruments/{instrument_id}/candles', ], 'post': [ 'order', 'orders', 'cancel_order/{underlying}/{order_id}', 'cancel_order/{underlying}/{client_oid}', 'cancel_batch_orders/{underlying}', 'amend_order/{underlying}', 'amend_batch_orders/{underlying}', ], }, 'index': { 'get': [ '{instrument_id}/constituents', ], }, }, 'fees': { 'trading': { 'taker': 0.0015, 'maker': 0.0010, }, 'spot': { 'taker': 0.0015, 'maker': 0.0010, }, 'futures': { 'taker': 0.0005, 'maker': 0.0002, }, 'swap': { 'taker': 0.00075, 'maker': 0.00020, }, }, 'requiredCredentials': { 'apiKey': True, 'secret': True, 'password': True, }, 'exceptions': { # http error codes # 400 Bad Request — Invalid request format # 401 Unauthorized — Invalid API Key # 403 Forbidden — You do not have access to the requested resource # 404 Not Found # 429 Client Error: Too Many Requests for url # 500 Internal Server Error — We had a problem with our server 'exact': { '1': ExchangeError, # {"code": 1, "message": "System error"} # undocumented 'failure to get a peer from the ring-balancer': ExchangeNotAvailable, # {"message": "failure to get a peer from the ring-balancer"} 'Server is busy, please try again.': ExchangeNotAvailable, # {"message": "Server is busy, please try again."} 'An unexpected error occurred': ExchangeError, # {"message": "An unexpected error occurred"} 'System error': ExchangeError, # {"error_message":"System error","message":"System error"} '4010': PermissionDenied, # {"code": 4010, "message": "For the security of your funds, withdrawals are not permitted within 24 hours after changing fund password / mobile number / Google Authenticator settings "} # common # '0': ExchangeError, # 200 successful,when the order placement / cancellation / operation is successful '4001': ExchangeError, # no data received in 30s '4002': ExchangeError, # Buffer full. cannot write data # -------------------------------------------------------- '30001': AuthenticationError, # {"code": 30001, "message": 'request header "OK_ACCESS_KEY" cannot be blank'} '30002': AuthenticationError, # {"code": 30002, "message": 'request header "OK_ACCESS_SIGN" cannot be blank'} '30003': AuthenticationError, # {"code": 30003, "message": 'request header "OK_ACCESS_TIMESTAMP" cannot be blank'} '30004': AuthenticationError, # {"code": 30004, "message": 'request header "OK_ACCESS_PASSPHRASE" cannot be blank'} '30005': InvalidNonce, # {"code": 30005, "message": "invalid OK_ACCESS_TIMESTAMP"} '30006': AuthenticationError, # {"code": 30006, "message": "invalid OK_ACCESS_KEY"} '30007': BadRequest, # {"code": 30007, "message": 'invalid Content_Type, please use "application/json" format'} '30008': RequestTimeout, # {"code": 30008, "message": "timestamp request expired"} '30009': ExchangeError, # {"code": 30009, "message": "system error"} '30010': AuthenticationError, # {"code": 30010, "message": "API validation failed"} '30011': PermissionDenied, # {"code": 30011, "message": "invalid IP"} '30012': AuthenticationError, # {"code": 30012, "message": "invalid authorization"} '30013': AuthenticationError, # {"code": 30013, "message": "invalid sign"} '30014': DDoSProtection, # {"code": 30014, "message": "request too frequent"} '30015': AuthenticationError, # {"code": 30015, "message": 'request header "OK_ACCESS_PASSPHRASE" incorrect'} '30016': ExchangeError, # {"code": 30015, "message": "you are using v1 apiKey, please use v1 endpoint. If you would like to use v3 endpoint, please subscribe to v3 apiKey"} '30017': ExchangeError, # {"code": 30017, "message": "apikey's broker id does not match"} '30018': ExchangeError, # {"code": 30018, "message": "apikey's domain does not match"} '30019': ExchangeNotAvailable, # {"code": 30019, "message": "Api is offline or unavailable"} '30020': BadRequest, # {"code": 30020, "message": "body cannot be blank"} '30021': BadRequest, # {"code": 30021, "message": "Json data format error"}, {"code": 30021, "message": "json data format error"} '30022': PermissionDenied, # {"code": 30022, "message": "Api has been frozen"} '30023': BadRequest, # {"code": 30023, "message": "{0} parameter cannot be blank"} '30024': BadSymbol, # {"code":30024,"message":"\"instrument_id\" is an invalid parameter"} '30025': BadRequest, # {"code": 30025, "message": "{0} parameter category error"} '30026': DDoSProtection, # {"code": 30026, "message": "requested too frequent"} '30027': AuthenticationError, # {"code": 30027, "message": "login failure"} '30028': PermissionDenied, # {"code": 30028, "message": "unauthorized execution"} '30029': AccountSuspended, # {"code": 30029, "message": "account suspended"} '30030': ExchangeNotAvailable, # {"code": 30030, "message": "endpoint request failed. Please try again"} '30031': BadRequest, # {"code": 30031, "message": "token does not exist"} '30032': BadSymbol, # {"code": 30032, "message": "pair does not exist"} '30033': BadRequest, # {"code": 30033, "message": "exchange domain does not exist"} '30034': ExchangeError, # {"code": 30034, "message": "exchange ID does not exist"} '30035': ExchangeError, # {"code": 30035, "message": "trading is not supported in self website"} '30036': ExchangeError, # {"code": 30036, "message": "no relevant data"} '30037': ExchangeNotAvailable, # {"code": 30037, "message": "endpoint is offline or unavailable"} # '30038': AuthenticationError, # {"code": 30038, "message": "user does not exist"} '30038': OnMaintenance, # {"client_oid":"","code":"30038","error_code":"30038","error_message":"Matching engine is being upgraded. Please try in about 1 minute.","message":"Matching engine is being upgraded. Please try in about 1 minute.","order_id":"-1","result":false} '30044': RequestTimeout, # {"code":30044, "message":"Endpoint request timeout"} # futures '32001': AccountSuspended, # {"code": 32001, "message": "futures account suspended"} '32002': PermissionDenied, # {"code": 32002, "message": "futures account does not exist"} '32003': CancelPending, # {"code": 32003, "message": "canceling, please wait"} '32004': ExchangeError, # {"code": 32004, "message": "you have no unfilled orders"} '32005': InvalidOrder, # {"code": 32005, "message": "max order quantity"} '32006': InvalidOrder, # {"code": 32006, "message": "the order price or trigger price exceeds USD 1 million"} '32007': InvalidOrder, # {"code": 32007, "message": "leverage level must be the same for orders on the same side of the contract"} '32008': InvalidOrder, # {"code": 32008, "message": "Max. positions to open(cross margin)"} '32009': InvalidOrder, # {"code": 32009, "message": "Max. positions to open(fixed margin)"} '32010': ExchangeError, # {"code": 32010, "message": "leverage cannot be changed with open positions"} '32011': ExchangeError, # {"code": 32011, "message": "futures status error"} '32012': ExchangeError, # {"code": 32012, "message": "futures order update error"} '32013': ExchangeError, # {"code": 32013, "message": "token type is blank"} '32014': ExchangeError, # {"code": 32014, "message": "your number of contracts closing is larger than the number of contracts available"} '32015': ExchangeError, # {"code": 32015, "message": "margin ratio is lower than 100% before opening positions"} '32016': ExchangeError, # {"code": 32016, "message": "margin ratio is lower than 100% after opening position"} '32017': ExchangeError, # {"code": 32017, "message": "no BBO"} '32018': ExchangeError, # {"code": 32018, "message": "the order quantity is less than 1, please try again"} '32019': ExchangeError, # {"code": 32019, "message": "the order price deviates from the price of the previous minute by more than 3%"} '32020': ExchangeError, # {"code": 32020, "message": "the price is not in the range of the price limit"} '32021': ExchangeError, # {"code": 32021, "message": "leverage error"} '32022': ExchangeError, # {"code": 32022, "message": "self function is not supported in your country or region according to the regulations"} '32023': ExchangeError, # {"code": 32023, "message": "self account has outstanding loan"} '32024': ExchangeError, # {"code": 32024, "message": "order cannot be placed during delivery"} '32025': ExchangeError, # {"code": 32025, "message": "order cannot be placed during settlement"} '32026': ExchangeError, # {"code": 32026, "message": "your account is restricted from opening positions"} '32027': ExchangeError, # {"code": 32027, "message": "cancelled over 20 orders"} '32028': ExchangeError, # {"code": 32028, "message": "account is suspended and liquidated"} '32029': ExchangeError, # {"code": 32029, "message": "order info does not exist"} '32030': InvalidOrder, # The order cannot be cancelled '32031': ArgumentsRequired, # client_oid or order_id is required. '32038': AuthenticationError, # User does not exist '32040': ExchangeError, # User have open contract orders or position '32044': ExchangeError, # {"code": 32044, "message": "The margin ratio after submitting self order is lower than the minimum requirement({0}) for your tier."} '32045': ExchangeError, # String of commission over 1 million '32046': ExchangeError, # Each user can hold up to 10 trade plans at the same time '32047': ExchangeError, # system error '32048': InvalidOrder, # Order strategy track range error '32049': ExchangeError, # Each user can hold up to 10 track plans at the same time '32050': InvalidOrder, # Order strategy rang error '32051': InvalidOrder, # Order strategy ice depth error '32052': ExchangeError, # String of commission over 100 thousand '32053': ExchangeError, # Each user can hold up to 6 ice plans at the same time '32057': ExchangeError, # The order price is zero. Market-close-all function cannot be executed '32054': ExchangeError, # Trade not allow '32055': InvalidOrder, # cancel order error '32056': ExchangeError, # iceberg per order average should between {0}-{1} contracts '32058': ExchangeError, # Each user can hold up to 6 initiative plans at the same time '32059': InvalidOrder, # Total amount should exceed per order amount '32060': InvalidOrder, # Order strategy type error '32061': InvalidOrder, # Order strategy initiative limit error '32062': InvalidOrder, # Order strategy initiative range error '32063': InvalidOrder, # Order strategy initiative rate error '32064': ExchangeError, # Time Stringerval of orders should set between 5-120s '32065': ExchangeError, # Close amount exceeds the limit of Market-close-all(999 for BTC, and 9999 for the rest tokens) '32066': ExchangeError, # You have open orders. Please cancel all open orders before changing your leverage level. '32067': ExchangeError, # Account equity < required margin in self setting. Please adjust your leverage level again. '32068': ExchangeError, # The margin for self position will fall short of the required margin in self setting. Please adjust your leverage level or increase your margin to proceed. '32069': ExchangeError, # Target leverage level too low. Your account balance is insufficient to cover the margin required. Please adjust the leverage level again. '32070': ExchangeError, # Please check open position or unfilled order '32071': ExchangeError, # Your current liquidation mode does not support self action. '32072': ExchangeError, # The highest available margin for your order’s tier is {0}. Please edit your margin and place a new order. '32073': ExchangeError, # The action does not apply to the token '32074': ExchangeError, # The number of contracts of your position, open orders, and the current order has exceeded the maximum order limit of self asset. '32075': ExchangeError, # Account risk rate breach '32076': ExchangeError, # Liquidation of the holding position(s) at market price will require cancellation of all pending close orders of the contracts. '32077': ExchangeError, # Your margin for self asset in futures account is insufficient and the position has been taken over for liquidation.(You will not be able to place orders, close positions, transfer funds, or add margin during self period of time. Your account will be restored after the liquidation is complete.) '32078': ExchangeError, # Please cancel all open orders before switching the liquidation mode(Please cancel all open orders before switching the liquidation mode) '32079': ExchangeError, # Your open positions are at high risk.(Please add margin or reduce positions before switching the mode) '32080': ExchangeError, # Funds cannot be transferred out within 30 minutes after futures settlement '32083': ExchangeError, # The number of contracts should be a positive multiple of %%. Please place your order again # token and margin trading '33001': PermissionDenied, # {"code": 33001, "message": "margin account for self pair is not enabled yet"} '33002': AccountSuspended, # {"code": 33002, "message": "margin account for self pair is suspended"} '33003': InsufficientFunds, # {"code": 33003, "message": "no loan balance"} '33004': ExchangeError, # {"code": 33004, "message": "loan amount cannot be smaller than the minimum limit"} '33005': ExchangeError, # {"code": 33005, "message": "repayment amount must exceed 0"} '33006': ExchangeError, # {"code": 33006, "message": "loan order not found"} '33007': ExchangeError, # {"code": 33007, "message": "status not found"} '33008': InsufficientFunds, # {"code": 33008, "message": "loan amount cannot exceed the maximum limit"} '33009': ExchangeError, # {"code": 33009, "message": "user ID is blank"} '33010': ExchangeError, # {"code": 33010, "message": "you cannot cancel an order during session 2 of call auction"} '33011': ExchangeError, # {"code": 33011, "message": "no new market data"} '33012': ExchangeError, # {"code": 33012, "message": "order cancellation failed"} '33013': InvalidOrder, # {"code": 33013, "message": "order placement failed"} '33014': OrderNotFound, # {"code": 33014, "message": "order does not exist"} '33015': InvalidOrder, # {"code": 33015, "message": "exceeded maximum limit"} '33016': ExchangeError, # {"code": 33016, "message": "margin trading is not open for self token"} '33017': InsufficientFunds, # {"code": 33017, "message": "insufficient balance"} '33018': ExchangeError, # {"code": 33018, "message": "self parameter must be smaller than 1"} '33020': ExchangeError, # {"code": 33020, "message": "request not supported"} '33021': BadRequest, # {"code": 33021, "message": "token and the pair do not match"} '33022': InvalidOrder, # {"code": 33022, "message": "pair and the order do not match"} '33023': ExchangeError, # {"code": 33023, "message": "you can only place market orders during call auction"} '33024': InvalidOrder, # {"code": 33024, "message": "trading amount too small"} '33025': InvalidOrder, # {"code": 33025, "message": "base token amount is blank"} '33026': ExchangeError, # {"code": 33026, "message": "transaction completed"} '33027': InvalidOrder, # {"code": 33027, "message": "cancelled order or order cancelling"} '33028': InvalidOrder, # {"code": 33028, "message": "the decimal places of the trading price exceeded the limit"} '33029': InvalidOrder, # {"code": 33029, "message": "the decimal places of the trading size exceeded the limit"} '33034': ExchangeError, # {"code": 33034, "message": "You can only place limit order after Call Auction has started"} '33035': ExchangeError, # This type of order cannot be canceled(This type of order cannot be canceled) '33036': ExchangeError, # Exceeding the limit of entrust order '33037': ExchangeError, # The buy order price should be lower than 130% of the trigger price '33038': ExchangeError, # The sell order price should be higher than 70% of the trigger price '33039': ExchangeError, # The limit of callback rate is 0 < x <= 5% '33040': ExchangeError, # The trigger price of a buy order should be lower than the latest transaction price '33041': ExchangeError, # The trigger price of a sell order should be higher than the latest transaction price '33042': ExchangeError, # The limit of price variance is 0 < x <= 1% '33043': ExchangeError, # The total amount must be larger than 0 '33044': ExchangeError, # The average amount should be 1/1000 * total amount <= x <= total amount '33045': ExchangeError, # The price should not be 0, including trigger price, order price, and price limit '33046': ExchangeError, # Price variance should be 0 < x <= 1% '33047': ExchangeError, # Sweep ratio should be 0 < x <= 100% '33048': ExchangeError, # Per order limit: Total amount/1000 < x <= Total amount '33049': ExchangeError, # Total amount should be X > 0 '33050': ExchangeError, # Time interval should be 5 <= x <= 120s '33051': ExchangeError, # cancel order number not higher limit: plan and track entrust no more than 10, ice and time entrust no more than 6 '33059': BadRequest, # {"code": 33059, "message": "client_oid or order_id is required"} '33060': BadRequest, # {"code": 33060, "message": "Only fill in either parameter client_oid or order_id"} '33061': ExchangeError, # Value of a single market price order cannot exceed 100,000 USD '33062': ExchangeError, # The leverage ratio is too high. The borrowed position has exceeded the maximum position of self leverage ratio. Please readjust the leverage ratio '33063': ExchangeError, # Leverage multiple is too low, there is insufficient margin in the account, please readjust the leverage ratio '33064': ExchangeError, # The setting of the leverage ratio cannot be less than 2, please readjust the leverage ratio '33065': ExchangeError, # Leverage ratio exceeds maximum leverage ratio, please readjust leverage ratio '33085': InvalidOrder, # The value of the position and buying order has reached the position limit, and no further buying is allowed. # account '21009': ExchangeError, # Funds cannot be transferred out within 30 minutes after swap settlement(Funds cannot be transferred out within 30 minutes after swap settlement) '34001': PermissionDenied, # {"code": 34001, "message": "withdrawal suspended"} '34002': InvalidAddress, # {"code": 34002, "message": "please add a withdrawal address"} '34003': ExchangeError, # {"code": 34003, "message": "sorry, self token cannot be withdrawn to xx at the moment"} '34004': ExchangeError, # {"code": 34004, "message": "withdrawal fee is smaller than minimum limit"} '34005': ExchangeError, # {"code": 34005, "message": "withdrawal fee exceeds the maximum limit"} '34006': ExchangeError, # {"code": 34006, "message": "withdrawal amount is lower than the minimum limit"} '34007': ExchangeError, # {"code": 34007, "message": "withdrawal amount exceeds the maximum limit"} '34008': InsufficientFunds, # {"code": 34008, "message": "insufficient balance"} '34009': ExchangeError, # {"code": 34009, "message": "your withdrawal amount exceeds the daily limit"} '34010': ExchangeError, # {"code": 34010, "message": "transfer amount must be larger than 0"} '34011': ExchangeError, # {"code": 34011, "message": "conditions not met"} '34012': ExchangeError, # {"code": 34012, "message": "the minimum withdrawal amount for NEO is 1, and the amount must be an integer"} '34013': ExchangeError, # {"code": 34013, "message": "please transfer"} '34014': ExchangeError, # {"code": 34014, "message": "transfer limited"} '34015': ExchangeError, # {"code": 34015, "message": "subaccount does not exist"} '34016': PermissionDenied, # {"code": 34016, "message": "transfer suspended"} '34017': AccountSuspended, # {"code": 34017, "message": "account suspended"} '34018': AuthenticationError, # {"code": 34018, "message": "incorrect trades password"} '34019': PermissionDenied, # {"code": 34019, "message": "please bind your email before withdrawal"} '34020': PermissionDenied, # {"code": 34020, "message": "please bind your funds password before withdrawal"} '34021': InvalidAddress, # {"code": 34021, "message": "Not verified address"} '34022': ExchangeError, # {"code": 34022, "message": "Withdrawals are not available for sub accounts"} '34023': PermissionDenied, # {"code": 34023, "message": "Please enable futures trading before transferring your funds"} '34026': RateLimitExceeded, # transfer too frequently(transfer too frequently) '34036': ExchangeError, # Parameter is incorrect, please refer to API documentation '34037': ExchangeError, # Get the sub-account balance interface, account type is not supported '34038': ExchangeError, # Since your C2C transaction is unusual, you are restricted from fund transfer. Please contact our customer support to cancel the restriction '34039': ExchangeError, # You are now restricted from transferring out your funds due to abnormal trades on C2C Market. Please transfer your fund on our website or app instead to verify your identity # swap '35001': ExchangeError, # {"code": 35001, "message": "Contract does not exist"} '35002': ExchangeError, # {"code": 35002, "message": "Contract settling"} '35003': ExchangeError, # {"code": 35003, "message": "Contract paused"} '35004': ExchangeError, # {"code": 35004, "message": "Contract pending settlement"} '35005': AuthenticationError, # {"code": 35005, "message": "User does not exist"} '35008': InvalidOrder, # {"code": 35008, "message": "Risk ratio too high"} '35010': InvalidOrder, # {"code": 35010, "message": "Position closing too large"} '35012': InvalidOrder, # {"code": 35012, "message": "Incorrect order size"} '35014': InvalidOrder, # {"code": 35014, "message": "Order price is not within limit"} '35015': InvalidOrder, # {"code": 35015, "message": "Invalid leverage level"} '35017': ExchangeError, # {"code": 35017, "message": "Open orders exist"} '35019': InvalidOrder, # {"code": 35019, "message": "Order size too large"} '35020': InvalidOrder, # {"code": 35020, "message": "Order price too high"} '35021': InvalidOrder, # {"code": 35021, "message": "Order size exceeded current tier limit"} '35022': BadRequest, # {"code": 35022, "message": "Contract status error"} '35024': BadRequest, # {"code": 35024, "message": "Contract not initialized"} '35025': InsufficientFunds, # {"code": 35025, "message": "No account balance"} '35026': BadRequest, # {"code": 35026, "message": "Contract settings not initialized"} '35029': OrderNotFound, # {"code": 35029, "message": "Order does not exist"} '35030': InvalidOrder, # {"code": 35030, "message": "Order size too large"} '35031': InvalidOrder, # {"code": 35031, "message": "Cancel order size too large"} '35032': ExchangeError, # {"code": 35032, "message": "Invalid user status"} '35037': ExchangeError, # No last traded price in cache '35039': ExchangeError, # {"code": 35039, "message": "Open order quantity exceeds limit"} '35040': InvalidOrder, # {"error_message":"Invalid order type","result":"true","error_code":"35040","order_id":"-1"} '35044': ExchangeError, # {"code": 35044, "message": "Invalid order status"} '35046': InsufficientFunds, # {"code": 35046, "message": "Negative account balance"} '35047': InsufficientFunds, # {"code": 35047, "message": "Insufficient account balance"} '35048': ExchangeError, # {"code": 35048, "message": "User contract is frozen and liquidating"} '35049': InvalidOrder, # {"code": 35049, "message": "Invalid order type"} '35050': InvalidOrder, # {"code": 35050, "message": "Position settings are blank"} '35052': InsufficientFunds, # {"code": 35052, "message": "Insufficient cross margin"} '35053': ExchangeError, # {"code": 35053, "message": "Account risk too high"} '35055': InsufficientFunds, # {"code": 35055, "message": "Insufficient account balance"} '35057': ExchangeError, # {"code": 35057, "message": "No last traded price"} '35058': ExchangeError, # {"code": 35058, "message": "No limit"} '35059': BadRequest, # {"code": 35059, "message": "client_oid or order_id is required"} '35060': BadRequest, # {"code": 35060, "message": "Only fill in either parameter client_oid or order_id"} '35061': BadRequest, # {"code": 35061, "message": "Invalid instrument_id"} '35062': InvalidOrder, # {"code": 35062, "message": "Invalid match_price"} '35063': InvalidOrder, # {"code": 35063, "message": "Invalid order_size"} '35064': InvalidOrder, # {"code": 35064, "message": "Invalid client_oid"} '35066': InvalidOrder, # Order interval error '35067': InvalidOrder, # Time-weighted order ratio error '35068': InvalidOrder, # Time-weighted order range error '35069': InvalidOrder, # Time-weighted single transaction limit error '35070': InvalidOrder, # Algo order type error '35071': InvalidOrder, # Order total must be larger than single order limit '35072': InvalidOrder, # Maximum 6 unfulfilled time-weighted orders can be held at the same time '35073': InvalidOrder, # Order price is 0. Market-close-all not available '35074': InvalidOrder, # Iceberg order single transaction average error '35075': InvalidOrder, # Failed to cancel order '35076': InvalidOrder, # LTC 20x leverage. Not allowed to open position '35077': InvalidOrder, # Maximum 6 unfulfilled iceberg orders can be held at the same time '35078': InvalidOrder, # Order amount exceeded 100,000 '35079': InvalidOrder, # Iceberg order price variance error '35080': InvalidOrder, # Callback rate error '35081': InvalidOrder, # Maximum 10 unfulfilled trail orders can be held at the same time '35082': InvalidOrder, # Trail order callback rate error '35083': InvalidOrder, # Each user can only hold a maximum of 10 unfulfilled stop-limit orders at the same time '35084': InvalidOrder, # Order amount exceeded 1 million '35085': InvalidOrder, # Order amount is not in the correct range '35086': InvalidOrder, # Price exceeds 100 thousand '35087': InvalidOrder, # Price exceeds 100 thousand '35088': InvalidOrder, # Average amount error '35089': InvalidOrder, # Price exceeds 100 thousand '35090': ExchangeError, # No stop-limit orders available for cancelation '35091': ExchangeError, # No trail orders available for cancellation '35092': ExchangeError, # No iceberg orders available for cancellation '35093': ExchangeError, # No trail orders available for cancellation '35094': ExchangeError, # Stop-limit order last traded price error '35095': BadRequest, # Instrument_id error '35096': ExchangeError, # Algo order status error '35097': ExchangeError, # Order status and order ID cannot exist at the same time '35098': ExchangeError, # An order status or order ID must exist '35099': ExchangeError, # Algo order ID error '35102': RateLimitExceeded, # {"error_message":"The operation that close all at market price is too frequent","result":"true","error_code":"35102","order_id":"-1"} # option '36001': BadRequest, # Invalid underlying index. '36002': BadRequest, # Instrument does not exist. '36005': ExchangeError, # Instrument status is invalid. '36101': AuthenticationError, # Account does not exist. '36102': PermissionDenied, # Account status is invalid. '36103': PermissionDenied, # Account is suspended due to ongoing liquidation. '36104': PermissionDenied, # Account is not enabled for options trading. '36105': PermissionDenied, # Please enable the account for option contract. '36106': PermissionDenied, # Funds cannot be transferred in or out, as account is suspended. '36107': PermissionDenied, # Funds cannot be transferred out within 30 minutes after option exercising or settlement. '36108': InsufficientFunds, # Funds cannot be transferred in or out, as equity of the account is less than zero. '36109': PermissionDenied, # Funds cannot be transferred in or out during option exercising or settlement. '36201': PermissionDenied, # New order function is blocked. '36202': PermissionDenied, # Account does not have permission to short option. '36203': InvalidOrder, # Invalid format for client_oid. '36204': ExchangeError, # Invalid format for request_id. '36205': BadRequest, # Instrument id does not match underlying index. '36206': BadRequest, # Order_id and client_oid can not be used at the same time. '36207': InvalidOrder, # Either order price or fartouch price must be present. '36208': InvalidOrder, # Either order price or size must be present. '36209': InvalidOrder, # Either order_id or client_oid must be present. '36210': InvalidOrder, # Either order_ids or client_oids must be present. '36211': InvalidOrder, # Exceeding max batch size for order submission. '36212': InvalidOrder, # Exceeding max batch size for oder cancellation. '36213': InvalidOrder, # Exceeding max batch size for order amendment. '36214': ExchangeError, # Instrument does not have valid bid/ask quote. '36216': OrderNotFound, # Order does not exist. '36217': InvalidOrder, # Order submission failed. '36218': InvalidOrder, # Order cancellation failed. '36219': InvalidOrder, # Order amendment failed. '36220': InvalidOrder, # Order is pending cancel. '36221': InvalidOrder, # Order qty is not valid multiple of lot size. '36222': InvalidOrder, # Order price is breaching highest buy limit. '36223': InvalidOrder, # Order price is breaching lowest sell limit. '36224': InvalidOrder, # Exceeding max order size. '36225': InvalidOrder, # Exceeding max open order count for instrument. '36226': InvalidOrder, # Exceeding max open order count for underlying. '36227': InvalidOrder, # Exceeding max open size across all orders for underlying '36228': InvalidOrder, # Exceeding max available qty for instrument. '36229': InvalidOrder, # Exceeding max available qty for underlying. '36230': InvalidOrder, # Exceeding max position limit for underlying. }, 'broad': { }, }, 'precisionMode': TICK_SIZE, 'options': { 'fetchOHLCV': { 'type': 'Candles', # Candles or HistoryCandles }, 'createMarketBuyOrderRequiresPrice': True, 'fetchMarkets': ['spot', 'futures', 'swap', 'option'], 'defaultType': 'spot', # 'account', 'spot', 'margin', 'futures', 'swap', 'option' 'auth': { 'time': 'public', 'currencies': 'private', 'instruments': 'public', 'rate': 'public', '{instrument_id}/constituents': 'public', }, }, 'commonCurrencies': { # OKEX refers to ERC20 version of Aeternity(AEToken) 'AE': 'AET', # https://github.com/ccxt/ccxt/issues/4981 'BOX': 'DefiBox', 'HOT': 'Hydro Protocol', 'HSR': 'HC', 'MAG': 'Maggie', 'SBTC': 'Super Bitcoin', 'YOYO': 'YOYOW', 'WIN': 'WinToken', # https://github.com/ccxt/ccxt/issues/5701 }, }) def fetch_time(self, params={}): response = self.generalGetTime(params) # # { # "iso": "2015-01-07T23:47:25.201Z", # "epoch": 1420674445.201 # } # return self.parse8601(self.safe_string(response, 'iso')) def fetch_markets(self, params={}): types = self.safe_value(self.options, 'fetchMarkets') result = [] for i in range(0, len(types)): markets = self.fetch_markets_by_type(types[i], params) result = self.array_concat(result, markets) return result def parse_markets(self, markets): result = [] for i in range(0, len(markets)): result.append(self.parse_market(markets[i])) return result def parse_market(self, market): # # spot markets # # { # base_currency: "EOS", # instrument_id: "EOS-OKB", # min_size: "0.01", # quote_currency: "OKB", # size_increment: "0.000001", # tick_size: "0.0001" # } # # futures markets # # { # instrument_id: "XRP-USD-200320", # underlying_index: "XRP", # quote_currency: "USD", # tick_size: "0.0001", # contract_val: "10", # listing: "2020-03-06", # delivery: "2020-03-20", # trade_increment: "1", # alias: "self_week", # underlying: "XRP-USD", # base_currency: "XRP", # settlement_currency: "XRP", # is_inverse: "true", # contract_val_currency: "USD", # } # # swap markets # # { # instrument_id: "BSV-USD-SWAP", # underlying_index: "BSV", # quote_currency: "USD", # coin: "BSV", # contract_val: "10", # listing: "2018-12-21T07:53:47.000Z", # delivery: "2020-03-14T08:00:00.000Z", # size_increment: "1", # tick_size: "0.01", # base_currency: "BSV", # underlying: "BSV-USD", # settlement_currency: "BSV", # is_inverse: "true", # contract_val_currency: "USD" # } # # options markets # # { # instrument_id: 'BTC-USD-200327-4000-C', # underlying: 'BTC-USD', # settlement_currency: 'BTC', # contract_val: '0.1000', # option_type: 'C', # strike: '4000', # tick_size: '0.0005', # lot_size: '1.0000', # listing: '2019-12-25T08:30:36.302Z', # delivery: '2020-03-27T08:00:00.000Z', # state: '2', # trading_start_time: '2019-12-25T08:30:36.302Z', # timestamp: '2020-03-13T08:05:09.456Z', # } # id = self.safe_string(market, 'instrument_id') marketType = 'spot' spot = True future = False swap = False option = False baseId = self.safe_string(market, 'base_currency') quoteId = self.safe_string(market, 'quote_currency') contractVal = self.safe_float(market, 'contract_val') if contractVal is not None: if 'option_type' in market: marketType = 'option' spot = False option = True underlying = self.safe_string(market, 'underlying') parts = underlying.split('-') baseId = self.safe_string(parts, 0) quoteId = self.safe_string(parts, 1) else: marketType = 'swap' spot = False swap = True futuresAlias = self.safe_string(market, 'alias') if futuresAlias is not None: swap = False future = True marketType = 'futures' baseId = self.safe_string(market, 'underlying_index') base = self.safe_currency_code(baseId) quote = self.safe_currency_code(quoteId) symbol = (base + '/' + quote) if spot else id lotSize = self.safe_float_2(market, 'lot_size', 'trade_increment') precision = { 'amount': self.safe_float(market, 'size_increment', lotSize), 'price': self.safe_float(market, 'tick_size'), } minAmount = self.safe_float_2(market, 'min_size', 'base_min_size') active = True fees = self.safe_value_2(self.fees, marketType, 'trading', {}) return self.extend(fees, { 'id': id, 'symbol': symbol, 'base': base, 'quote': quote, 'baseId': baseId, 'quoteId': quoteId, 'info': market, 'type': marketType, 'spot': spot, 'futures': future, 'swap': swap, 'option': option, 'active': active, 'precision': precision, 'limits': { 'amount': { 'min': minAmount, 'max': None, }, 'price': { 'min': precision['price'], 'max': None, }, 'cost': { 'min': precision['price'], 'max': None, }, }, }) def fetch_markets_by_type(self, type, params={}): if type == 'option': underlying = self.optionGetUnderlying(params) result = [] for i in range(0, len(underlying)): response = self.optionGetInstrumentsUnderlying({ 'underlying': underlying[i], }) # # options markets # # [ # { # instrument_id: 'BTC-USD-200327-4000-C', # underlying: 'BTC-USD', # settlement_currency: 'BTC', # contract_val: '0.1000', # option_type: 'C', # strike: '4000', # tick_size: '0.0005', # lot_size: '1.0000', # listing: '2019-12-25T08:30:36.302Z', # delivery: '2020-03-27T08:00:00.000Z', # state: '2', # trading_start_time: '2019-12-25T08:30:36.302Z', # timestamp: '2020-03-13T08:05:09.456Z', # }, # ] # result = self.array_concat(result, response) return self.parse_markets(result) elif (type == 'spot') or (type == 'futures') or (type == 'swap'): method = type + 'GetInstruments' response = getattr(self, method)(params) # # spot markets # # [ # { # base_currency: "EOS", # instrument_id: "EOS-OKB", # min_size: "0.01", # quote_currency: "OKB", # size_increment: "0.000001", # tick_size: "0.0001" # } # ] # # futures markets # # [ # { # instrument_id: "XRP-USD-200320", # underlying_index: "XRP", # quote_currency: "USD", # tick_size: "0.0001", # contract_val: "10", # listing: "2020-03-06", # delivery: "2020-03-20", # trade_increment: "1", # alias: "self_week", # underlying: "XRP-USD", # base_currency: "XRP", # settlement_currency: "XRP", # is_inverse: "true", # contract_val_currency: "USD", # } # ] # # swap markets # # [ # { # instrument_id: "BSV-USD-SWAP", # underlying_index: "BSV", # quote_currency: "USD", # coin: "BSV", # contract_val: "10", # listing: "2018-12-21T07:53:47.000Z", # delivery: "2020-03-14T08:00:00.000Z", # size_increment: "1", # tick_size: "0.01", # base_currency: "BSV", # underlying: "BSV-USD", # settlement_currency: "BSV", # is_inverse: "true", # contract_val_currency: "USD" # } # ] # return self.parse_markets(response) else: raise NotSupported(self.id + ' fetchMarketsByType does not support market type ' + type) def fetch_currencies(self, params={}): # has['fetchCurrencies'] is currently set to False # despite that their docs say these endpoints are public: # https://www.okex.com/api/account/v3/withdrawal/fee # https://www.okex.com/api/account/v3/currencies # it will still reply with {"code":30001, "message": "OK-ACCESS-KEY header is required"} # if you attempt to access it without authentication response = self.accountGetCurrencies(params) # # [ # { # name: '', # currency: 'BTC', # can_withdraw: '1', # can_deposit: '1', # min_withdrawal: '0.0100000000000000' # }, # ] # result = {} for i in range(0, len(response)): currency = response[i] id = self.safe_string(currency, 'currency') code = self.safe_currency_code(id) precision = 0.00000001 # default precision, todo: fix "magic constants" name = self.safe_string(currency, 'name') canDeposit = self.safe_integer(currency, 'can_deposit') canWithdraw = self.safe_integer(currency, 'can_withdraw') active = True if (canDeposit and canWithdraw) else False result[code] = { 'id': id, 'code': code, 'info': currency, 'type': None, 'name': name, 'active': active, 'fee': None, # todo: redesign 'precision': precision, 'limits': { 'amount': {'min': None, 'max': None}, 'price': {'min': None, 'max': None}, 'cost': {'min': None, 'max': None}, 'withdraw': { 'min': self.safe_float(currency, 'min_withdrawal'), 'max': None, }, }, } return result def fetch_order_book(self, symbol, limit=None, params={}): self.load_markets() market = self.market(symbol) method = market['type'] + 'GetInstrumentsInstrumentId' method += 'Depth' if (market['type'] == 'swap') else 'Book' request = { 'instrument_id': market['id'], } if limit is not None: request['size'] = limit # max 200 response = getattr(self, method)(self.extend(request, params)) # # { asks: [["0.02685268", "0.242571", "1"], # ["0.02685493", "0.164085", "1"], # ... # ["0.02779", "1.039", "1"], # ["0.027813", "0.0876", "1"] ], # bids: [["0.02684052", "10.371849", "1"], # ["0.02684051", "3.707", "4"], # ... # ["0.02634963", "0.132934", "1"], # ["0.02634962", "0.264838", "2"] ], # timestamp: "2018-12-17T20:24:16.159Z" } # timestamp = self.parse8601(self.safe_string(response, 'timestamp')) return self.parse_order_book(response, timestamp) def parse_ticker(self, ticker, market=None): # # { best_ask: "0.02665472", # best_bid: "0.02665221", # instrument_id: "ETH-BTC", # product_id: "ETH-BTC", # last: "0.02665472", # ask: "0.02665472", # missing in the docs # bid: "0.02665221", # not mentioned in the docs # open_24h: "0.02645482", # high_24h: "0.02714633", # low_24h: "0.02614109", # base_volume_24h: "572298.901923", # timestamp: "2018-12-17T21:20:07.856Z", # quote_volume_24h: "15094.86831261" } # timestamp = self.parse8601(self.safe_string(ticker, 'timestamp')) symbol = None marketId = self.safe_string(ticker, 'instrument_id') if marketId in self.markets_by_id: market = self.markets_by_id[marketId] symbol = market['symbol'] elif marketId is not None: parts = marketId.split('-') numParts = len(parts) if numParts == 2: baseId, quoteId = parts base = self.safe_currency_code(baseId) quote = self.safe_currency_code(quoteId) symbol = base + '/' + quote else: symbol = marketId if (symbol is None) and (market is not None): symbol = market['symbol'] last = self.safe_float(ticker, 'last') open = self.safe_float(ticker, 'open_24h') return { 'symbol': symbol, 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'high': self.safe_float(ticker, 'high_24h'), 'low': self.safe_float(ticker, 'low_24h'), 'bid': self.safe_float(ticker, 'best_bid'), 'bidVolume': self.safe_float(ticker, 'best_bid_size'), 'ask': self.safe_float(ticker, 'best_ask'), 'askVolume': self.safe_float(ticker, 'best_ask_size'), 'vwap': None, 'open': open, 'close': last, 'last': last, 'previousClose': None, 'change': None, 'percentage': None, 'average': None, 'baseVolume': self.safe_float(ticker, 'base_volume_24h'), 'quoteVolume': self.safe_float(ticker, 'quote_volume_24h'), 'info': ticker, } def fetch_ticker(self, symbol, params={}): self.load_markets() market = self.market(symbol) method = market['type'] + 'GetInstrumentsInstrumentIdTicker' request = { 'instrument_id': market['id'], } response = getattr(self, method)(self.extend(request, params)) # # { best_ask: "0.02665472", # best_bid: "0.02665221", # instrument_id: "ETH-BTC", # product_id: "ETH-BTC", # last: "0.02665472", # ask: "0.02665472", # bid: "0.02665221", # open_24h: "0.02645482", # high_24h: "0.02714633", # low_24h: "0.02614109", # base_volume_24h: "572298.901923", # timestamp: "2018-12-17T21:20:07.856Z", # quote_volume_24h: "15094.86831261" } # return self.parse_ticker(response) def fetch_tickers_by_type(self, type, symbols=None, params={}): self.load_markets() method = type + 'GetInstrumentsTicker' response = getattr(self, method)(params) result = {} for i in range(0, len(response)): ticker = self.parse_ticker(response[i]) symbol = ticker['symbol'] result[symbol] = ticker return self.filter_by_array(result, 'symbol', symbols) def fetch_tickers(self, symbols=None, params={}): defaultType = self.safe_string_2(self.options, 'fetchTickers', 'defaultType') type = self.safe_string(params, 'type', defaultType) return self.fetch_tickers_by_type(type, symbols, self.omit(params, 'type')) def parse_trade(self, trade, market=None): # # fetchTrades(public) # # spot trades # # { # time: "2018-12-17T23:31:08.268Z", # timestamp: "2018-12-17T23:31:08.268Z", # trade_id: "409687906", # price: "0.02677805", # size: "0.923467", # side: "sell" # } # # futures trades, swap trades # # { # trade_id: "1989230840021013", # side: "buy", # price: "92.42", # qty: "184", # missing in swap markets # size: "5", # missing in futures markets # timestamp: "2018-12-17T23:26:04.613Z" # } # # fetchOrderTrades(private) # # spot trades, margin trades # # { # "created_at":"2019-03-15T02:52:56.000Z", # "exec_type":"T", # whether the order is taker or maker # "fee":"0.00000082", # "instrument_id":"BTC-USDT", # "ledger_id":"3963052721", # "liquidity":"T", # whether the order is taker or maker # "order_id":"2482659399697408", # "price":"3888.6", # "product_id":"BTC-USDT", # "side":"buy", # "size":"0.00055306", # "timestamp":"2019-03-15T02:52:56.000Z" # }, # # futures trades, swap trades # # { # "trade_id":"197429674631450625", # "instrument_id":"EOS-USD-SWAP", # "order_id":"6a-7-54d663a28-0", # "price":"3.633", # "order_qty":"1.0000", # "fee":"-0.000551", # "created_at":"2019-03-21T04:41:58.0Z", # missing in swap trades # "timestamp":"2019-03-25T05:56:31.287Z", # missing in futures trades # "exec_type":"M", # whether the order is taker or maker # "side":"short", # "buy" in futures trades # } # symbol = None marketId = self.safe_string(trade, 'instrument_id') base = None quote = None if marketId in self.markets_by_id: market = self.markets_by_id[marketId] symbol = market['symbol'] base = market['base'] quote = market['quote'] elif marketId is not None: parts = marketId.split('-') numParts = len(parts) if numParts == 2: baseId, quoteId = parts base = self.safe_currency_code(baseId) quote = self.safe_currency_code(quoteId) symbol = base + '/' + quote else: symbol = marketId if (symbol is None) and (market is not None): symbol = market['symbol'] base = market['base'] quote = market['quote'] timestamp = self.parse8601(self.safe_string_2(trade, 'timestamp', 'created_at')) price = self.safe_float(trade, 'price') amount = self.safe_float_2(trade, 'size', 'qty') amount = self.safe_float(trade, 'order_qty', amount) takerOrMaker = self.safe_string_2(trade, 'exec_type', 'liquidity') if takerOrMaker == 'M': takerOrMaker = 'maker' elif takerOrMaker == 'T': takerOrMaker = 'taker' side = self.safe_string(trade, 'side') cost = None if amount is not None: if price is not None: cost = amount * price feeCost = self.safe_float(trade, 'fee') fee = None if feeCost is not None: feeCurrency = base if (side == 'buy') else quote fee = { # fee is either a positive number(invitation rebate) # or a negative number(transaction fee deduction) # therefore we need to invert the fee # more about it https://github.com/ccxt/ccxt/issues/5909 'cost': -feeCost, 'currency': feeCurrency, } orderId = self.safe_string(trade, 'order_id') return { 'info': trade, 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'symbol': symbol, 'id': self.safe_string_2(trade, 'trade_id', 'ledger_id'), 'order': orderId, 'type': None, 'takerOrMaker': takerOrMaker, 'side': side, 'price': price, 'amount': amount, 'cost': cost, 'fee': fee, } def fetch_trades(self, symbol, since=None, limit=None, params={}): self.load_markets() market = self.market(symbol) method = market['type'] + 'GetInstrumentsInstrumentIdTrades' if (limit is None) or (limit > 100): limit = 100 # maximum = default = 100 request = { 'instrument_id': market['id'], 'limit': limit, # from: 'id', # to: 'id', } response = getattr(self, method)(self.extend(request, params)) # # spot markets # # [ # { # time: "2018-12-17T23:31:08.268Z", # timestamp: "2018-12-17T23:31:08.268Z", # trade_id: "409687906", # price: "0.02677805", # size: "0.923467", # side: "sell" # } # ] # # futures markets, swap markets # # [ # { # trade_id: "1989230840021013", # side: "buy", # price: "92.42", # qty: "184", # missing in swap markets # size: "5", # missing in futures markets # timestamp: "2018-12-17T23:26:04.613Z" # } # ] # return self.parse_trades(response, market, since, limit) def parse_ohlcv(self, ohlcv, market=None): # # spot markets # # { # close: "0.02684545", # high: "0.02685084", # low: "0.02683312", # open: "0.02683894", # time: "2018-12-17T20:28:00.000Z", # volume: "101.457222" # } # # futures markets # # [ # 1545072720000, # 0.3159, # 0.3161, # 0.3144, # 0.3149, # 22886, # 725179.26172331, # ] # if isinstance(ohlcv, list): numElements = len(ohlcv) volumeIndex = 6 if (numElements > 6) else 5 timestamp = self.safe_value(ohlcv, 0) if isinstance(timestamp, basestring): timestamp = self.parse8601(timestamp) return [ timestamp, # timestamp self.safe_float(ohlcv, 1), # Open self.safe_float(ohlcv, 2), # High self.safe_float(ohlcv, 3), # Low self.safe_float(ohlcv, 4), # Close # self.safe_float(ohlcv, 5), # Quote Volume # self.safe_float(ohlcv, 6), # Base Volume self.safe_float(ohlcv, volumeIndex), # Volume, okex will return base volume in the 7th element for future markets ] else: return [ self.parse8601(self.safe_string(ohlcv, 'time')), self.safe_float(ohlcv, 'open'), # Open self.safe_float(ohlcv, 'high'), # High self.safe_float(ohlcv, 'low'), # Low self.safe_float(ohlcv, 'close'), # Close self.safe_float(ohlcv, 'volume'), # Base Volume ] def fetch_ohlcv(self, symbol, timeframe='1m', since=None, limit=None, params={}): self.load_markets() market = self.market(symbol) duration = self.parse_timeframe(timeframe) request = { 'instrument_id': market['id'], 'granularity': self.timeframes[timeframe], } options = self.safe_value(self.options, 'fetchOHLCV', {}) defaultType = self.safe_string(options, 'type', 'Candles') # Candles or HistoryCandles type = self.safe_string(params, 'type', defaultType) params = self.omit(params, 'type') method = market['type'] + 'GetInstrumentsInstrumentId' + type if type == 'Candles': if since is not None: if limit is not None: request['end'] = self.iso8601(self.sum(since, limit * duration * 1000)) request['start'] = self.iso8601(since) else: if limit is not None: now = self.milliseconds() request['start'] = self.iso8601(now - limit * duration * 1000) request['end'] = self.iso8601(now) elif type == 'HistoryCandles': if market['option']: raise NotSupported(self.id + ' fetchOHLCV does not have ' + type + ' for ' + market['type'] + ' markets') if since is not None: if limit is None: limit = 300 # default request['start'] = self.iso8601(self.sum(since, limit * duration * 1000)) request['end'] = self.iso8601(since) else: if limit is not None: now = self.milliseconds() request['end'] = self.iso8601(now - limit * duration * 1000) request['start'] = self.iso8601(now) response = getattr(self, method)(self.extend(request, params)) # # spot markets # # [ # { # close: "0.02683401", # high: "0.02683401", # low: "0.02683401", # open: "0.02683401", # time: "2018-12-17T23:47:00.000Z", # volume: "0" # }, # { # close: "0.02684545", # high: "0.02685084", # low: "0.02683312", # open: "0.02683894", # time: "2018-12-17T20:28:00.000Z", # volume: "101.457222" # } # ] # # futures # # [ # [ # 1545090660000, # 0.3171, # 0.3174, # 0.3171, # 0.3173, # 1648, # 51930.38579450868 # ], # [ # 1545072720000, # 0.3159, # 0.3161, # 0.3144, # 0.3149, # 22886, # 725179.26172331 # ] # ] # return self.parse_ohlcvs(response, market, timeframe, since, limit) def parse_account_balance(self, response): # # account # # [ # { # balance: 0, # available: 0, # currency: "BTC", # hold: 0 # }, # { # balance: 0, # available: 0, # currency: "ETH", # hold: 0 # } # ] # # spot # # [ # { # frozen: "0", # hold: "0", # id: "2149632", # currency: "BTC", # balance: "0.0000000497717339", # available: "0.0000000497717339", # holds: "0" # }, # { # frozen: "0", # hold: "0", # id: "2149632", # currency: "ICN", # balance: "0.00000000925", # available: "0.00000000925", # holds: "0" # } # ] # result = {'info': response} for i in range(0, len(response)): balance = response[i] currencyId = self.safe_string(balance, 'currency') code = self.safe_currency_code(currencyId) account = self.account() account['total'] = self.safe_float(balance, 'balance') account['used'] = self.safe_float(balance, 'hold') account['free'] = self.safe_float(balance, 'available') result[code] = account return self.parse_balance(result) def parse_margin_balance(self, response): # # [ # { # "currency:BTC": { # "available":"0", # "balance":"0", # "borrowed":"0", # "can_withdraw":"0", # "frozen":"0", # "hold":"0", # "holds":"0", # "lending_fee":"0" # }, # "currency:USDT": { # "available":"100", # "balance":"100", # "borrowed":"0", # "can_withdraw":"100", # "frozen":"0", # "hold":"0", # "holds":"0", # "lending_fee":"0" # }, # "instrument_id":"BTC-USDT", # "liquidation_price":"0", # "product_id":"BTC-USDT", # "risk_rate":"" # }, # ] # result = {'info': response} for i in range(0, len(response)): balance = response[i] marketId = self.safe_string(balance, 'instrument_id') market = self.safe_value(self.markets_by_id, marketId) symbol = None if market is None: baseId, quoteId = marketId.split('-') base = self.safe_currency_code(baseId) quote = self.safe_currency_code(quoteId) symbol = base + '/' + quote else: symbol = market['symbol'] omittedBalance = self.omit(balance, [ 'instrument_id', 'liquidation_price', 'product_id', 'risk_rate', 'margin_ratio', 'maint_margin_ratio', 'tiers', ]) keys = list(omittedBalance.keys()) accounts = {} for k in range(0, len(keys)): key = keys[k] marketBalance = balance[key] if key.find(':') >= 0: parts = key.split(':') currencyId = parts[1] code = self.safe_currency_code(currencyId) account = self.account() account['total'] = self.safe_float(marketBalance, 'balance') account['used'] = self.safe_float(marketBalance, 'hold') account['free'] = self.safe_float(marketBalance, 'available') accounts[code] = account else: raise NotSupported(self.id + ' margin balance response format has changed!') result[symbol] = self.parse_balance(accounts) return result def parse_futures_balance(self, response): # # { # "info":{ # "eos":{ # "auto_margin":"0", # "contracts": [ # { # "available_qty":"40.37069445", # "fixed_balance":"0", # "instrument_id":"EOS-USD-190329", # "margin_for_unfilled":"0", # "margin_frozen":"0", # "realized_pnl":"0", # "unrealized_pnl":"0" # }, # { # "available_qty":"40.37069445", # "fixed_balance":"14.54895721", # "instrument_id":"EOS-USD-190628", # "margin_for_unfilled":"0", # "margin_frozen":"10.64042157", # "realized_pnl":"-3.90853564", # "unrealized_pnl":"-0.259" # }, # ], # "equity":"50.75220665", # "margin_mode":"fixed", # "total_avail_balance":"40.37069445" # }, # } # } # # their root field name is "info", so our info will contain their info result = {'info': response} info = self.safe_value(response, 'info', {}) ids = list(info.keys()) for i in range(0, len(ids)): id = ids[i] code = self.safe_currency_code(id) balance = self.safe_value(info, id, {}) account = self.account() totalAvailBalance = self.safe_float(balance, 'total_avail_balance') if self.safe_string(balance, 'margin_mode') == 'fixed': contracts = self.safe_value(balance, 'contracts', []) free = totalAvailBalance for i in range(0, len(contracts)): contract = contracts[i] fixedBalance = self.safe_float(contract, 'fixed_balance') realizedPnl = self.safe_float(contract, 'realized_pnl') marginFrozen = self.safe_float(contract, 'margin_frozen') marginForUnfilled = self.safe_float(contract, 'margin_for_unfilled') margin = self.sum(fixedBalance, realizedPnl) - marginFrozen - marginForUnfilled free = self.sum(free, margin) account['free'] = free else: realizedPnl = self.safe_float(balance, 'realized_pnl') unrealizedPnl = self.safe_float(balance, 'unrealized_pnl') marginFrozen = self.safe_float(balance, 'margin_frozen') marginForUnfilled = self.safe_float(balance, 'margin_for_unfilled') account['free'] = self.sum(totalAvailBalance, realizedPnl, unrealizedPnl) - marginFrozen - marginForUnfilled # it may be incorrect to use total, free and used for swap accounts account['total'] = self.safe_float(balance, 'equity') result[code] = account return self.parse_balance(result) def parse_swap_balance(self, response): # # { # "info": [ # { # "equity":"3.0139", # "fixed_balance":"0.0000", # "instrument_id":"EOS-USD-SWAP", # "margin":"0.5523", # "margin_frozen":"0.0000", # "margin_mode":"crossed", # "margin_ratio":"1.0913", # "realized_pnl":"-0.0006", # "timestamp":"2019-03-25T03:46:10.336Z", # "total_avail_balance":"3.0000", # "unrealized_pnl":"0.0145" # } # ] # } # # their root field name is "info", so our info will contain their info result = {'info': response} info = self.safe_value(response, 'info', []) for i in range(0, len(info)): balance = info[i] marketId = self.safe_string(balance, 'instrument_id') symbol = marketId if marketId in self.markets_by_id: symbol = self.markets_by_id[marketId]['symbol'] account = self.account() # it may be incorrect to use total, free and used for swap accounts account['total'] = self.safe_float(balance, 'equity') account['free'] = self.safe_float(balance, 'total_avail_balance') result[symbol] = account return self.parse_balance(result) def fetch_balance(self, params={}): defaultType = self.safe_string_2(self.options, 'fetchBalance', 'defaultType') type = self.safe_string(params, 'type', defaultType) if type is None: raise ArgumentsRequired(self.id + " fetchBalance() requires a type parameter(one of 'account', 'spot', 'margin', 'futures', 'swap')") self.load_markets() suffix = 'Wallet' if (type == 'account') else 'Accounts' method = type + 'Get' + suffix query = self.omit(params, 'type') response = getattr(self, method)(query) # # account # # [ # { # balance: 0, # available: 0, # currency: "BTC", # hold: 0 # }, # { # balance: 0, # available: 0, # currency: "ETH", # hold: 0 # } # ] # # spot # # [ # { # frozen: "0", # hold: "0", # id: "2149632", # currency: "BTC", # balance: "0.0000000497717339", # available: "0.0000000497717339", # holds: "0" # }, # { # frozen: "0", # hold: "0", # id: "2149632", # currency: "ICN", # balance: "0.00000000925", # available: "0.00000000925", # holds: "0" # } # ] # # margin # # [ # { # "currency:BTC": { # "available":"0", # "balance":"0", # "borrowed":"0", # "can_withdraw":"0", # "frozen":"0", # "hold":"0", # "holds":"0", # "lending_fee":"0" # }, # "currency:USDT": { # "available":"100", # "balance":"100", # "borrowed":"0", # "can_withdraw":"100", # "frozen":"0", # "hold":"0", # "holds":"0", # "lending_fee":"0" # }, # "instrument_id":"BTC-USDT", # "liquidation_price":"0", # "product_id":"BTC-USDT", # "risk_rate":"" # }, # ] # # futures # # { # "info":{ # "eos":{ # "auto_margin":"0", # "contracts": [ # { # "available_qty":"40.37069445", # "fixed_balance":"0", # "instrument_id":"EOS-USD-190329", # "margin_for_unfilled":"0", # "margin_frozen":"0", # "realized_pnl":"0", # "unrealized_pnl":"0" # }, # { # "available_qty":"40.37069445", # "fixed_balance":"14.54895721", # "instrument_id":"EOS-USD-190628", # "margin_for_unfilled":"0", # "margin_frozen":"10.64042157", # "realized_pnl":"-3.90853564", # "unrealized_pnl":"-0.259" # }, # ], # "equity":"50.75220665", # "margin_mode":"fixed", # "total_avail_balance":"40.37069445" # }, # } # } # # swap # # { # "info": [ # { # "equity":"3.0139", # "fixed_balance":"0.0000", # "instrument_id":"EOS-USD-SWAP", # "margin":"0.5523", # "margin_frozen":"0.0000", # "margin_mode":"crossed", # "margin_ratio":"1.0913", # "realized_pnl":"-0.0006", # "timestamp":"2019-03-25T03:46:10.336Z", # "total_avail_balance":"3.0000", # "unrealized_pnl":"0.0145" # } # ] # } # return self.parse_balance_by_type(type, response) def parse_balance_by_type(self, type, response): if (type == 'account') or (type == 'spot'): return self.parse_account_balance(response) elif type == 'margin': return self.parse_margin_balance(response) elif type == 'futures': return self.parse_futures_balance(response) elif type == 'swap': return self.parse_swap_balance(response) raise NotSupported(self.id + " fetchBalance does not support the '" + type + "' type(the type must be one of 'account', 'spot', 'margin', 'futures', 'swap')") def create_order(self, symbol, type, side, amount, price=None, params={}): self.load_markets() market = self.market(symbol) request = { 'instrument_id': market['id'], # 'client_oid': 'abcdef1234567890', # [a-z0-9]{1,32} # 'order_type': '0', # 0 = Normal limit order, 1 = Post only, 2 = Fill Or Kill, 3 = Immediatel Or Cancel, 4 = Market for futures only } clientOrderId = self.safe_string_2(params, 'client_oid', 'clientOrderId') if clientOrderId is not None: request['client_oid'] = clientOrderId params = self.omit(params, ['client_oid', 'clientOrderId']) method = None if market['futures'] or market['swap']: size = self.number_to_string(amount) if market['futures'] else self.amount_to_precision(symbol, amount) request = self.extend(request, { 'type': type, # 1:open long 2:open short 3:close long 4:close short for futures 'size': size, # 'match_price': '0', # Order at best counter party price?(0:no 1:yes). The default is 0. If it is set as 1, the price parameter will be ignored. When posting orders at best bid price, order_type can only be 0(regular order). }) orderType = self.safe_string(params, 'order_type') # order_type == '4' means a market order isMarketOrder = (type == 'market') or (orderType == '4') if isMarketOrder: request['order_type'] = '4' else: request['price'] = self.price_to_precision(symbol, price) if market['futures']: request['leverage'] = '10' # or '20' method = market['type'] + 'PostOrder' else: marginTrading = self.safe_string(params, 'margin_trading', '1') # 1 = spot, 2 = margin request = self.extend(request, { 'side': side, 'type': type, # limit/market 'margin_trading': marginTrading, # 1 = spot, 2 = margin }) if type == 'limit': request['price'] = self.price_to_precision(symbol, price) request['size'] = self.amount_to_precision(symbol, amount) elif type == 'market': # for market buy it requires the amount of quote currency to spend if side == 'buy': notional = self.safe_float(params, 'notional') createMarketBuyOrderRequiresPrice = self.safe_value(self.options, 'createMarketBuyOrderRequiresPrice', True) if createMarketBuyOrderRequiresPrice: if price is not None: if notional is None: notional = amount * price elif notional is None: raise InvalidOrder(self.id + " createOrder() requires the price argument with market buy orders to calculate total order cost(amount to spend), where cost = amount * price. Supply a price argument to createOrder() call if you want the cost to be calculated for you from price and amount, or, alternatively, add .options['createMarketBuyOrderRequiresPrice'] = False and supply the total cost value in the 'amount' argument or in the 'notional' extra parameter(the exchange-specific behaviour)") else: notional = amount if (notional is None) else notional precision = market['precision']['price'] request['notional'] = self.decimal_to_precision(notional, TRUNCATE, precision, self.precisionMode) else: request['size'] = self.amount_to_precision(symbol, amount) method = 'marginPostOrders' if (marginTrading == '2') else 'spotPostOrders' response = getattr(self, method)(self.extend(request, params)) # # { # "client_oid":"oktspot79", # "error_code":"", # "error_message":"", # "order_id":"2510789768709120", # "result":true # } # order = self.parse_order(response, market) return self.extend(order, { 'type': type, 'side': side, }) def cancel_order(self, id, symbol=None, params={}): if symbol is None: raise ArgumentsRequired(self.id + ' cancelOrder() requires a symbol argument') self.load_markets() market = self.market(symbol) type = None if market['futures'] or market['swap']: type = market['type'] else: defaultType = self.safe_string_2(self.options, 'cancelOrder', 'defaultType', market['type']) type = self.safe_string(params, 'type', defaultType) if type is None: raise ArgumentsRequired(self.id + " cancelOrder() requires a type parameter(one of 'spot', 'margin', 'futures', 'swap').") method = type + 'PostCancelOrder' request = { 'instrument_id': market['id'], } if market['futures'] or market['swap']: method += 'InstrumentId' else: method += 's' clientOrderId = self.safe_string_2(params, 'client_oid', 'clientOrderId') if clientOrderId is not None: method += 'ClientOid' request['client_oid'] = clientOrderId else: method += 'OrderId' request['order_id'] = id query = self.omit(params, ['type', 'client_oid', 'clientOrderId']) response = getattr(self, method)(self.extend(request, query)) result = response if ('result' in response) else self.safe_value(response, market['id'], {}) # # spot, margin # # { # "btc-usdt": [ # { # "result":true, # "client_oid":"a123", # "order_id": "2510832677225473" # } # ] # } # # futures, swap # # { # "result": True, # "client_oid": "oktfuture10", # missing if requested by order_id # "order_id": "2517535534836736", # "instrument_id": "EOS-USD-190628" # } # return self.parse_order(result, market) def parse_order_status(self, status): statuses = { '-2': 'failed', '-1': 'canceled', '0': 'open', '1': 'open', '2': 'closed', '3': 'open', '4': 'canceled', } return self.safe_string(statuses, status, status) def parse_order_side(self, side): sides = { '1': 'buy', # open long '2': 'sell', # open short '3': 'sell', # close long '4': 'buy', # close short } return self.safe_string(sides, side, side) def parse_order(self, order, market=None): # # createOrder # # { # "client_oid":"oktspot79", # "error_code":"", # "error_message":"", # "order_id":"2510789768709120", # "result":true # } # # cancelOrder # # { # "result": True, # "client_oid": "oktfuture10", # missing if requested by order_id # "order_id": "2517535534836736", # # instrument_id is missing for spot/margin orders # # available in futures and swap orders only # "instrument_id": "EOS-USD-190628", # } # # fetchOrder, fetchOrdersByState, fetchOpenOrders, fetchClosedOrders # # # spot and margin orders # # { # "client_oid":"oktspot76", # "created_at":"2019-03-18T07:26:49.000Z", # "filled_notional":"3.9734", # "filled_size":"0.001", # filled_qty in futures and swap orders # "funds":"", # self is most likely the same as notional # "instrument_id":"BTC-USDT", # "notional":"", # "order_id":"2500723297813504", # "order_type":"0", # "price":"4013", # "product_id":"BTC-USDT", # missing in futures and swap orders # "side":"buy", # "size":"0.001", # "status":"filled", # "state": "2", # "timestamp":"2019-03-18T07:26:49.000Z", # "type":"limit" # } # # # futures and swap orders # # { # "instrument_id":"EOS-USD-190628", # "size":"10", # "timestamp":"2019-03-20T10:04:55.000Z", # "filled_qty":"10", # filled_size in spot and margin orders # "fee":"-0.00841043", # "order_id":"2512669605501952", # "price":"3.668", # "price_avg":"3.567", # missing in spot and margin orders # "status":"2", # "state": "2", # "type":"4", # "contract_val":"10", # "leverage":"10", # missing in swap, spot and margin orders # "client_oid":"", # "pnl":"1.09510794", # missing in swap, spo and margin orders # "order_type":"0" # } # id = self.safe_string(order, 'order_id') timestamp = self.parse8601(self.safe_string(order, 'timestamp')) side = self.safe_string(order, 'side') type = self.safe_string(order, 'type') if (side != 'buy') and (side != 'sell'): side = self.parse_order_side(type) symbol = None marketId = self.safe_string(order, 'instrument_id') if marketId in self.markets_by_id: market = self.markets_by_id[marketId] symbol = market['symbol'] else: symbol = marketId if market is not None: if symbol is None: symbol = market['symbol'] amount = self.safe_float(order, 'size') filled = self.safe_float_2(order, 'filled_size', 'filled_qty') remaining = None if amount is not None: if filled is not None: amount = max(amount, filled) remaining = max(0, amount - filled) if type == 'market': remaining = 0 cost = self.safe_float_2(order, 'filled_notional', 'funds') price = self.safe_float(order, 'price') average = self.safe_float(order, 'price_avg') if cost is None: if filled is not None and average is not None: cost = average * filled else: if (average is None) and (filled is not None) and (filled > 0): average = cost / filled status = self.parse_order_status(self.safe_string(order, 'state')) feeCost = self.safe_float(order, 'fee') fee = None if feeCost is not None: feeCurrency = None fee = { 'cost': feeCost, 'currency': feeCurrency, } clientOrderId = self.safe_string(order, 'client_oid') if (clientOrderId is not None) and (len(clientOrderId) < 1): clientOrderId = None # fix empty clientOrderId string stopPrice = self.safe_float(order, 'trigger_price') return { 'info': order, 'id': id, 'clientOrderId': clientOrderId, 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'lastTradeTimestamp': None, 'symbol': symbol, 'type': type, 'timeInForce': None, 'postOnly': None, 'side': side, 'price': price, 'stopPrice': stopPrice, 'average': average, 'cost': cost, 'amount': amount, 'filled': filled, 'remaining': remaining, 'status': status, 'fee': fee, 'trades': None, } def fetch_order(self, id, symbol=None, params={}): if symbol is None: raise ArgumentsRequired(self.id + ' fetchOrder() requires a symbol argument') self.load_markets() market = self.market(symbol) defaultType = self.safe_string_2(self.options, 'fetchOrder', 'defaultType', market['type']) type = self.safe_string(params, 'type', defaultType) if type is None: raise ArgumentsRequired(self.id + " fetchOrder() requires a type parameter(one of 'spot', 'margin', 'futures', 'swap').") instrumentId = 'InstrumentId' if (market['futures'] or market['swap']) else '' method = type + 'GetOrders' + instrumentId request = { 'instrument_id': market['id'], # 'client_oid': 'abcdef12345', # optional, [a-z0-9]{1,32} # 'order_id': id, } clientOid = self.safe_string(params, 'client_oid') if clientOid is not None: method += 'ClientOid' request['client_oid'] = clientOid else: method += 'OrderId' request['order_id'] = id query = self.omit(params, 'type') response = getattr(self, method)(self.extend(request, query)) # # spot, margin # # { # "client_oid":"oktspot70", # "created_at":"2019-03-15T02:52:56.000Z", # "filled_notional":"3.8886", # "filled_size":"0.001", # "funds":"", # "instrument_id":"BTC-USDT", # "notional":"", # "order_id":"2482659399697408", # "order_type":"0", # "price":"3927.3", # "product_id":"BTC-USDT", # "side":"buy", # "size":"0.001", # "status":"filled", # "state": "2", # "timestamp":"2019-03-15T02:52:56.000Z", # "type":"limit" # } # # futures, swap # # { # "instrument_id":"EOS-USD-190628", # "size":"10", # "timestamp":"2019-03-20T02:46:38.000Z", # "filled_qty":"10", # "fee":"-0.0080819", # "order_id":"2510946213248000", # "price":"3.712", # "price_avg":"3.712", # "status":"2", # "state": "2", # "type":"2", # "contract_val":"10", # "leverage":"10", # "client_oid":"", # missing in swap orders # "pnl":"0", # missing in swap orders # "order_type":"0" # } # return self.parse_order(response) def fetch_orders_by_state(self, state, symbol=None, since=None, limit=None, params={}): if symbol is None: raise ArgumentsRequired(self.id + ' fetchOrdersByState() requires a symbol argument') self.load_markets() market = self.market(symbol) type = None if market['futures'] or market['swap']: type = market['type'] else: defaultType = self.safe_string_2(self.options, 'fetchOrder', 'defaultType', market['type']) type = self.safe_string(params, 'type', defaultType) if type is None: raise ArgumentsRequired(self.id + " fetchOrdersByState() requires a type parameter(one of 'spot', 'margin', 'futures', 'swap').") request = { 'instrument_id': market['id'], # '-2': failed, # '-1': cancelled, # '0': open , # '1': partially filled, # '2': fully filled, # '3': submitting, # '4': cancelling, # '6': incomplete(open+partially filled), # '7': complete(cancelled+fully filled), 'state': state, } method = type + 'GetOrders' if market['futures'] or market['swap']: method += 'InstrumentId' query = self.omit(params, 'type') response = getattr(self, method)(self.extend(request, query)) # # spot, margin # # [ # # in fact, self documented API response does not correspond # # to their actual API response for spot markets # # OKEX v3 API returns a plain array of orders(see below) # [ # { # "client_oid":"oktspot76", # "created_at":"2019-03-18T07:26:49.000Z", # "filled_notional":"3.9734", # "filled_size":"0.001", # "funds":"", # "instrument_id":"BTC-USDT", # "notional":"", # "order_id":"2500723297813504", # "order_type":"0", # "price":"4013", # "product_id":"BTC-USDT", # "side":"buy", # "size":"0.001", # "status":"filled", # "state": "2", # "timestamp":"2019-03-18T07:26:49.000Z", # "type":"limit" # }, # ], # { # "before":"2500723297813504", # "after":"2500650881647616" # } # ] # # futures, swap # # { # "result":true, # missing in swap orders # "order_info": [ # { # "instrument_id":"EOS-USD-190628", # "size":"10", # "timestamp":"2019-03-20T10:04:55.000Z", # "filled_qty":"10", # "fee":"-0.00841043", # "order_id":"2512669605501952", # "price":"3.668", # "price_avg":"3.567", # "status":"2", # "state": "2", # "type":"4", # "contract_val":"10", # "leverage":"10", # missing in swap orders # "client_oid":"", # "pnl":"1.09510794", # missing in swap orders # "order_type":"0" # }, # ] # } # orders = None if market['swap'] or market['futures']: orders = self.safe_value(response, 'order_info', []) else: orders = response responseLength = len(response) if responseLength < 1: return [] # in fact, self documented API response does not correspond # to their actual API response for spot markets # OKEX v3 API returns a plain array of orders if responseLength > 1: before = self.safe_value(response[1], 'before') if before is not None: orders = response[0] return self.parse_orders(orders, market, since, limit) def fetch_open_orders(self, symbol=None, since=None, limit=None, params={}): # '-2': failed, # '-1': cancelled, # '0': open , # '1': partially filled, # '2': fully filled, # '3': submitting, # '4': cancelling, # '6': incomplete(open+partially filled), # '7': complete(cancelled+fully filled), return self.fetch_orders_by_state('6', symbol, since, limit, params) def fetch_closed_orders(self, symbol=None, since=None, limit=None, params={}): # '-2': failed, # '-1': cancelled, # '0': open , # '1': partially filled, # '2': fully filled, # '3': submitting, # '4': cancelling, # '6': incomplete(open+partially filled), # '7': complete(cancelled+fully filled), return self.fetch_orders_by_state('7', symbol, since, limit, params) def parse_deposit_addresses(self, addresses): result = {} for i in range(0, len(addresses)): address = self.parse_deposit_address(addresses[i]) code = address['currency'] result[code] = address return result def parse_deposit_address(self, depositAddress, currency=None): # # { # address: '0x696abb81974a8793352cbd33aadcf78eda3cfdfa', # currency: 'eth' # tag: 'abcde12345', # will be missing if the token does not require a deposit tag # payment_id: 'abcde12345', # will not be returned if the token does not require a payment_id # # can_deposit: 1, # 0 or 1, documented but missing # # can_withdraw: 1, # 0 or 1, documented but missing # } # address = self.safe_string(depositAddress, 'address') tag = self.safe_string_2(depositAddress, 'tag', 'payment_id') tag = self.safe_string(depositAddress, 'memo', tag) currencyId = self.safe_string(depositAddress, 'currency') code = self.safe_currency_code(currencyId) self.check_address(address) return { 'currency': code, 'address': address, 'tag': tag, 'info': depositAddress, } def fetch_deposit_address(self, code, params={}): self.load_markets() currency = self.currency(code) request = { 'currency': currency['id'], } response = self.accountGetDepositAddress(self.extend(request, params)) # # [ # { # address: '0x696abb81974a8793352cbd33aadcf78eda3cfdfa', # currency: 'eth' # } # ] # addresses = self.parse_deposit_addresses(response) address = self.safe_value(addresses, code) if address is None: raise InvalidAddress(self.id + ' fetchDepositAddress cannot return nonexistent addresses, you should create withdrawal addresses with the exchange website first') return address def withdraw(self, code, amount, address, tag=None, params={}): self.check_address(address) self.load_markets() currency = self.currency(code) if tag: address = address + ':' + tag fee = self.safe_string(params, 'fee') if fee is None: raise ArgumentsRequired(self.id + " withdraw() requires a `fee` string parameter, network transaction fee must be ≥ 0. Withdrawals to OKCoin or OKEx are fee-free, please set '0'. Withdrawing to external digital asset address requires network transaction fee.") request = { 'currency': currency['id'], 'to_address': address, 'destination': '4', # 2 = OKCoin International, 3 = OKEx 4 = others 'amount': self.number_to_string(amount), 'fee': fee, # String. Network transaction fee ≥ 0. Withdrawals to OKCoin or OKEx are fee-free, please set as 0. Withdrawal to external digital asset address requires network transaction fee. } if 'password' in params: request['trade_pwd'] = params['password'] elif 'trade_pwd' in params: request['trade_pwd'] = params['trade_pwd'] elif self.password: request['trade_pwd'] = self.password query = self.omit(params, ['fee', 'password', 'trade_pwd']) if not ('trade_pwd' in request): raise ExchangeError(self.id + ' withdraw() requires self.password set on the exchange instance or a password / trade_pwd parameter') response = self.accountPostWithdrawal(self.extend(request, query)) # # { # "amount":"0.1", # "withdrawal_id":"67485", # "currency":"btc", # "result":true # } # return { 'info': response, 'id': self.safe_string(response, 'withdrawal_id'), } def fetch_deposits(self, code=None, since=None, limit=None, params={}): self.load_markets() request = {} method = 'accountGetDepositHistory' currency = None if code is not None: currency = self.currency(code) request['currency'] = currency['id'] method += 'Currency' response = getattr(self, method)(self.extend(request, params)) return self.parse_transactions(response, currency, since, limit, params) def fetch_withdrawals(self, code=None, since=None, limit=None, params={}): self.load_markets() request = {} method = 'accountGetWithdrawalHistory' currency = None if code is not None: currency = self.currency(code) request['currency'] = currency['id'] method += 'Currency' response = getattr(self, method)(self.extend(request, params)) return self.parse_transactions(response, currency, since, limit, params) def parse_transaction_status(self, status): # # deposit statuses # # { # '0': 'waiting for confirmation', # '1': 'confirmation account', # '2': 'recharge success' # } # # withdrawal statues # # { # '-3': 'pending cancel', # '-2': 'cancelled', # '-1': 'failed', # '0': 'pending', # '1': 'sending', # '2': 'sent', # '3': 'email confirmation', # '4': 'manual confirmation', # '5': 'awaiting identity confirmation' # } # statuses = { '-3': 'pending', '-2': 'canceled', '-1': 'failed', '0': 'pending', '1': 'pending', '2': 'ok', '3': 'pending', '4': 'pending', '5': 'pending', } return self.safe_string(statuses, status, status) def parse_transaction(self, transaction, currency=None): # # withdraw # # { # "amount":"0.1", # "withdrawal_id":"67485", # "currency":"btc", # "result":true # } # # fetchWithdrawals # # { # amount: "4.72100000", # withdrawal_id: "1729116", # fee: "0.01000000eth", # txid: "0xf653125bbf090bcfe4b5e8e7b8f586a9d87aa7de94598702758c0802b…", # currency: "ETH", # from: "7147338839", # to: "0x26a3CB49578F07000575405a57888681249c35Fd", # timestamp: "2018-08-17T07:03:42.000Z", # status: "2" # } # # fetchDeposits # # { # "amount": "4.19511659", # "txid": "14c9a8c925647cdb7e5b2937ea9aefe2b29b2c273150ad3f44b3b8a4635ed437", # "currency": "XMR", # "from": "", # "to": "48PjH3ksv1fiXniKvKvyH5UtFs5WhfS2Vf7U3TwzdRJtCc7HJWvCQe56dRahyhQyTAViXZ8Nzk4gQg6o4BJBMUoxNy8y8g7", # "tag": "1234567", # "deposit_id": 11571659, <-- we can use self # "timestamp": "2019-10-01T14:54:19.000Z", # "status": "2" # } # type = None id = None address = None withdrawalId = self.safe_string(transaction, 'withdrawal_id') addressFrom = self.safe_string(transaction, 'from') addressTo = self.safe_string(transaction, 'to') tagTo = self.safe_string(transaction, 'tag') if withdrawalId is not None: type = 'withdrawal' id = withdrawalId address = addressTo else: # the payment_id will appear on new deposits but appears to be removed from the response after 2 months id = self.safe_string_2(transaction, 'payment_id', 'deposit_id') type = 'deposit' address = addressTo currencyId = self.safe_string(transaction, 'currency') code = self.safe_currency_code(currencyId) amount = self.safe_float(transaction, 'amount') status = self.parse_transaction_status(self.safe_string(transaction, 'status')) txid = self.safe_string(transaction, 'txid') timestamp = self.parse8601(self.safe_string(transaction, 'timestamp')) feeCost = None if type == 'deposit': feeCost = 0 else: if currencyId is not None: feeWithCurrencyId = self.safe_string(transaction, 'fee') if feeWithCurrencyId is not None: # https://github.com/ccxt/ccxt/pull/5748 lowercaseCurrencyId = currencyId.lower() feeWithoutCurrencyId = feeWithCurrencyId.replace(lowercaseCurrencyId, '') feeCost = float(feeWithoutCurrencyId) # todo parse tags return { 'info': transaction, 'id': id, 'currency': code, 'amount': amount, 'addressFrom': addressFrom, 'addressTo': addressTo, 'address': address, 'tagFrom': None, 'tagTo': tagTo, 'tag': tagTo, 'status': status, 'type': type, 'updated': None, 'txid': txid, 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'fee': { 'currency': code, 'cost': feeCost, }, } def parse_my_trade(self, pair, market=None): # check that trading symbols match in both entries userTrade = self.safe_value(pair, 1) otherTrade = self.safe_value(pair, 0) firstMarketId = self.safe_string(otherTrade, 'instrument_id') secondMarketId = self.safe_string(userTrade, 'instrument_id') if firstMarketId != secondMarketId: raise NotSupported(self.id + ' parseMyTrade() received unrecognized response format, differing instrument_ids in one fill, the exchange API might have changed, paste your verbose output: https://github.com/ccxt/ccxt/wiki/FAQ#what-is-required-to-get-help') marketId = firstMarketId market = self.safe_market(marketId, market) symbol = market['symbol'] quoteId = market['quoteId'] side = None amount = None cost = None receivedCurrencyId = self.safe_string(userTrade, 'currency') feeCurrencyId = None if receivedCurrencyId == quoteId: side = self.safe_string(otherTrade, 'side') amount = self.safe_float(otherTrade, 'size') cost = self.safe_float(userTrade, 'size') feeCurrencyId = self.safe_string(otherTrade, 'currency') else: side = self.safe_string(userTrade, 'side') amount = self.safe_float(userTrade, 'size') cost = self.safe_float(otherTrade, 'size') feeCurrencyId = self.safe_string(userTrade, 'currency') id = self.safe_string(userTrade, 'trade_id') price = self.safe_float(userTrade, 'price') feeCostFirst = self.safe_float(otherTrade, 'fee') feeCostSecond = self.safe_float(userTrade, 'fee') feeCurrencyCodeFirst = self.safe_currency_code(self.safe_string(otherTrade, 'currency')) feeCurrencyCodeSecond = self.safe_currency_code(self.safe_string(userTrade, 'currency')) fee = None fees = None # fee is either a positive number(invitation rebate) # or a negative number(transaction fee deduction) # therefore we need to invert the fee # more about it https://github.com/ccxt/ccxt/issues/5909 if (feeCostFirst is not None) and (feeCostFirst != 0): if (feeCostSecond is not None) and (feeCostSecond != 0): fees = [ { 'cost': -feeCostFirst, 'currency': feeCurrencyCodeFirst, }, { 'cost': -feeCostSecond, 'currency': feeCurrencyCodeSecond, }, ] else: fee = { 'cost': -feeCostFirst, 'currency': feeCurrencyCodeFirst, } elif (feeCostSecond is not None) and (feeCostSecond != 0): fee = { 'cost': -feeCostSecond, 'currency': feeCurrencyCodeSecond, } else: fee = { 'cost': 0, 'currency': self.safe_currency_code(feeCurrencyId), } # # simplified structures to show the underlying semantics # # # market/limit sell # # { # "currency":"USDT", # "fee":"-0.04647925", # ←--- fee in received quote currency # "price":"129.13", # ←------ price # "size":"30.98616393", # ←-- cost # }, # { # "currency":"ETH", # "fee":"0", # "price":"129.13", # "size":"0.23996099", # ←--- amount # }, # # # market/limit buy # # { # "currency":"ETH", # "fee":"-0.00036049", # ←--- fee in received base currency # "price":"129.16", # ←------ price # "size":"0.240322", # ←----- amount # }, # { # "currency":"USDT", # "fee":"0", # "price":"129.16", # "size":"31.03998952", # ←-- cost # } # timestamp = self.parse8601(self.safe_string_2(userTrade, 'timestamp', 'created_at')) takerOrMaker = self.safe_string_2(userTrade, 'exec_type', 'liquidity') if takerOrMaker == 'M': takerOrMaker = 'maker' elif takerOrMaker == 'T': takerOrMaker = 'taker' orderId = self.safe_string(userTrade, 'order_id') result = { 'info': pair, 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'symbol': symbol, 'id': id, 'order': orderId, 'type': None, 'takerOrMaker': takerOrMaker, 'side': side, 'price': price, 'amount': amount, 'cost': cost, 'fee': fee, } if fees is not None: result['fees'] = fees return result def parse_my_trades(self, trades, market=None, since=None, limit=None, params={}): grouped = self.group_by(trades, 'trade_id') tradeIds = list(grouped.keys()) result = [] for i in range(0, len(tradeIds)): tradeId = tradeIds[i] pair = grouped[tradeId] # make sure it has exactly 2 trades, no more, no less numTradesInPair = len(pair) if numTradesInPair == 2: trade = self.parse_my_trade(pair) result.append(trade) symbol = None if market is not None: symbol = market['symbol'] return self.filter_by_symbol_since_limit(result, symbol, since, limit) def fetch_my_trades(self, symbol=None, since=None, limit=None, params={}): # okex actually returns ledger entries instead of fills here, so each fill in the order # is represented by two trades with opposite buy/sell sides, not one :\ # self aspect renders the 'fills' endpoint unusable for fetchOrderTrades # until either OKEX fixes the API or we workaround self on our side somehow if symbol is None: raise ArgumentsRequired(self.id + ' fetchMyTrades() requires a symbol argument') self.load_markets() market = self.market(symbol) if (limit is not None) and (limit > 100): limit = 100 request = { 'instrument_id': market['id'], # 'order_id': id, # string # 'after': '1', # pagination of data to return records earlier than the requested ledger_id # 'before': '1', # P=pagination of data to return records newer than the requested ledger_id # 'limit': limit, # optional, number of results per request, default = maximum = 100 } defaultType = self.safe_string_2(self.options, 'fetchMyTrades', 'defaultType') type = self.safe_string(params, 'type', defaultType) query = self.omit(params, 'type') method = type + 'GetFills' response = getattr(self, method)(self.extend(request, query)) # # [ # # sell # { # "created_at":"2020-03-29T11:55:25.000Z", # "currency":"USDT", # "exec_type":"T", # "fee":"-0.04647925", # "instrument_id":"ETH-USDT", # "ledger_id":"10562924353", # "liquidity":"T", # "order_id":"4636470489136128", # "price":"129.13", # "product_id":"ETH-USDT", # "side":"buy", # "size":"30.98616393", # "timestamp":"2020-03-29T11:55:25.000Z", # "trade_id":"18551601" # }, # { # "created_at":"2020-03-29T11:55:25.000Z", # "currency":"ETH", # "exec_type":"T", # "fee":"0", # "instrument_id":"ETH-USDT", # "ledger_id":"10562924352", # "liquidity":"T", # "order_id":"4636470489136128", # "price":"129.13", # "product_id":"ETH-USDT", # "side":"sell", # "size":"0.23996099", # "timestamp":"2020-03-29T11:55:25.000Z", # "trade_id":"18551601" # }, # # buy # { # "created_at":"2020-03-29T11:55:16.000Z", # "currency":"ETH", # "exec_type":"T", # "fee":"-0.00036049", # "instrument_id":"ETH-USDT", # "ledger_id":"10562922669", # "liquidity":"T", # "order_id": "4636469894136832", # "price":"129.16", # "product_id":"ETH-USDT", # "side":"buy", # "size":"0.240322", # "timestamp":"2020-03-29T11:55:16.000Z", # "trade_id":"18551600" # }, # { # "created_at":"2020-03-29T11:55:16.000Z", # "currency":"USDT", # "exec_type":"T", # "fee":"0", # "instrument_id":"ETH-USDT", # "ledger_id":"10562922668", # "liquidity":"T", # "order_id":"4636469894136832", # "price":"129.16", # "product_id":"ETH-USDT", # "side":"sell", # "size":"31.03998952", # "timestamp":"2020-03-29T11:55:16.000Z", # "trade_id":"18551600" # } # ] # return self.parse_my_trades(response, market, since, limit, params) def fetch_order_trades(self, id, symbol=None, since=None, limit=None, params={}): request = { # 'instrument_id': market['id'], 'order_id': id, # 'after': '1', # return the page after the specified page number # 'before': '1', # return the page before the specified page number # 'limit': limit, # optional, number of results per request, default = maximum = 100 } return self.fetch_my_trades(symbol, since, limit, self.extend(request, params)) def fetch_position(self, symbol, params={}): self.load_markets() market = self.market(symbol) method = None request = { 'instrument_id': market['id'], # 'order_id': id, # string # 'after': '1', # pagination of data to return records earlier than the requested ledger_id # 'before': '1', # P=pagination of data to return records newer than the requested ledger_id # 'limit': limit, # optional, number of results per request, default = maximum = 100 } type = market['type'] if (type == 'futures') or (type == 'swap'): method = type + 'GetInstrumentIdPosition' elif type == 'option': underlying = self.safe_string(params, 'underlying') if underlying is None: raise ArgumentsRequired(self.id + ' fetchPosition() requires an underlying parameter for ' + type + ' market ' + symbol) method = type + 'GetUnderlyingPosition' else: raise NotSupported(self.id + ' fetchPosition() does not support ' + type + ' market ' + symbol + ', supported market types are futures, swap or option') response = getattr(self, method)(self.extend(request, params)) # # futures # # crossed margin mode # # { # "result": True, # "holding": [ # { # "long_qty": "2", # "long_avail_qty": "2", # "long_avg_cost": "8260", # "long_settlement_price": "8260", # "realised_pnl": "0.00020928", # "short_qty": "2", # "short_avail_qty": "2", # "short_avg_cost": "8259.99", # "short_settlement_price": "8259.99", # "liquidation_price": "113.81", # "instrument_id": "BTC-USD-191227", # "leverage": "10", # "created_at": "2019-09-25T07:58:42.129Z", # "updated_at": "2019-10-08T14:02:51.029Z", # "margin_mode": "crossed", # "short_margin": "0.00242197", # "short_pnl": "6.63E-6", # "short_pnl_ratio": "0.002477997", # "short_unrealised_pnl": "6.63E-6", # "long_margin": "0.00242197", # "long_pnl": "-6.65E-6", # "long_pnl_ratio": "-0.002478", # "long_unrealised_pnl": "-6.65E-6", # "long_settled_pnl": "0", # "short_settled_pnl": "0", # "last": "8257.57" # } # ], # "margin_mode": "crossed" # } # # fixed margin mode # # { # "result": True, # "holding": [ # { # "long_qty": "4", # "long_avail_qty": "4", # "long_margin": "0.00323844", # "long_liqui_price": "7762.09", # "long_pnl_ratio": "0.06052306", # "long_avg_cost": "8234.43", # "long_settlement_price": "8234.43", # "realised_pnl": "-0.00000296", # "short_qty": "2", # "short_avail_qty": "2", # "short_margin": "0.00241105", # "short_liqui_price": "9166.74", # "short_pnl_ratio": "0.03318052", # "short_avg_cost": "8295.13", # "short_settlement_price": "8295.13", # "instrument_id": "BTC-USD-191227", # "long_leverage": "15", # "short_leverage": "10", # "created_at": "2019-09-25T07:58:42.129Z", # "updated_at": "2019-10-08T13:12:09.438Z", # "margin_mode": "fixed", # "short_margin_ratio": "0.10292507", # "short_maint_margin_ratio": "0.005", # "short_pnl": "7.853E-5", # "short_unrealised_pnl": "7.853E-5", # "long_margin_ratio": "0.07103743", # "long_maint_margin_ratio": "0.005", # "long_pnl": "1.9841E-4", # "long_unrealised_pnl": "1.9841E-4", # "long_settled_pnl": "0", # "short_settled_pnl": "0", # "last": "8266.99" # } # ], # "margin_mode": "fixed" # } # # swap # # crossed margin mode # # { # "margin_mode": "crossed", # "timestamp": "2019-09-27T03:49:02.018Z", # "holding": [ # { # "avail_position": "3", # "avg_cost": "59.49", # "instrument_id": "LTC-USD-SWAP", # "last": "55.98", # "leverage": "10.00", # "liquidation_price": "4.37", # "maint_margin_ratio": "0.0100", # "margin": "0.0536", # "position": "3", # "realized_pnl": "0.0000", # "unrealized_pnl": "0", # "settled_pnl": "-0.0330", # "settlement_price": "55.84", # "side": "long", # "timestamp": "2019-09-27T03:49:02.018Z" # }, # ] # } # # fixed margin mode # # { # "margin_mode": "fixed", # "timestamp": "2019-09-27T03:47:37.230Z", # "holding": [ # { # "avail_position": "20", # "avg_cost": "8025.0", # "instrument_id": "BTC-USD-SWAP", # "last": "8113.1", # "leverage": "15.00", # "liquidation_price": "7002.6", # "maint_margin_ratio": "0.0050", # "margin": "0.0454", # "position": "20", # "realized_pnl": "-0.0001", # "unrealized_pnl": "0", # "settled_pnl": "0.0076", # "settlement_price": "8279.2", # "side": "long", # "timestamp": "2019-09-27T03:47:37.230Z" # } # ] # } # # option # # { # "holding":[ # { # "instrument_id":"BTC-USD-190927-12500-C", # "position":"20", # "avg_cost":"3.26", # "avail_position":"20", # "settlement_price":"0.017", # "total_pnl":"50", # "pnl_ratio":"0.3", # "realized_pnl":"40", # "unrealized_pnl":"10", # "pos_margin":"100", # "option_value":"70", # "created_at":"2019-08-30T03:09:20.315Z", # "updated_at":"2019-08-30T03:40:18.318Z" # }, # { # "instrument_id":"BTC-USD-190927-12500-P", # "position":"20", # "avg_cost":"3.26", # "avail_position":"20", # "settlement_price":"0.019", # "total_pnl":"50", # "pnl_ratio":"0.3", # "realized_pnl":"40", # "unrealized_pnl":"10", # "pos_margin":"100", # "option_value":"70", # "created_at":"2019-08-30T03:09:20.315Z", # "updated_at":"2019-08-30T03:40:18.318Z" # } # ] # } # # todo unify parsePosition/parsePositions return response def fetch_positions(self, symbols=None, since=None, limit=None, params={}): self.load_markets() method = None defaultType = self.safe_string_2(self.options, 'fetchPositions', 'defaultType') type = self.safe_string(params, 'type', defaultType) if (type == 'futures') or (type == 'swap'): method = type + 'GetPosition' elif type == 'option': underlying = self.safe_string(params, 'underlying') if underlying is None: raise ArgumentsRequired(self.id + ' fetchPositions() requires an underlying parameter for ' + type + ' markets') method = type + 'GetUnderlyingPosition' else: raise NotSupported(self.id + ' fetchPositions() does not support ' + type + ' markets, supported market types are futures, swap or option') params = self.omit(params, 'type') response = getattr(self, method)(params) # # futures # # ... # # # swap # # ... # # option # # { # "holding":[ # { # "instrument_id":"BTC-USD-190927-12500-C", # "position":"20", # "avg_cost":"3.26", # "avail_position":"20", # "settlement_price":"0.017", # "total_pnl":"50", # "pnl_ratio":"0.3", # "realized_pnl":"40", # "unrealized_pnl":"10", # "pos_margin":"100", # "option_value":"70", # "created_at":"2019-08-30T03:09:20.315Z", # "updated_at":"2019-08-30T03:40:18.318Z" # }, # { # "instrument_id":"BTC-USD-190927-12500-P", # "position":"20", # "avg_cost":"3.26", # "avail_position":"20", # "settlement_price":"0.019", # "total_pnl":"50", # "pnl_ratio":"0.3", # "realized_pnl":"40", # "unrealized_pnl":"10", # "pos_margin":"100", # "option_value":"70", # "created_at":"2019-08-30T03:09:20.315Z", # "updated_at":"2019-08-30T03:40:18.318Z" # } # ] # } # # todo unify parsePosition/parsePositions return response def fetch_ledger(self, code=None, since=None, limit=None, params={}): self.load_markets() defaultType = self.safe_string_2(self.options, 'fetchLedger', 'defaultType') type = self.safe_string(params, 'type', defaultType) query = self.omit(params, 'type') suffix = '' if (type == 'account') else 'Accounts' argument = '' request = { # 'from': 'id', # 'to': 'id', } if limit is not None: request['limit'] = limit currency = None if (type == 'spot') or (type == 'futures'): if code is None: raise ArgumentsRequired(self.id + " fetchLedger() requires a currency code argument for '" + type + "' markets") argument = 'Currency' currency = self.currency(code) request['currency'] = currency['id'] elif (type == 'margin') or (type == 'swap'): if code is None: raise ArgumentsRequired(self.id + " fetchLedger() requires a code argument(a market symbol) for '" + type + "' markets") argument = 'InstrumentId' market = self.market(code) # we intentionally put a market inside here for the margin and swap ledgers currency = self.currency(market['base']) request['instrument_id'] = market['id'] # # if type == 'margin': # # # # 3. Borrow # # 4. Repayment # # 5. Interest # # 7. Buy # # 8. Sell # # 9. From capital account # # 10. From C2C # # 11. From Futures # # 12. From Spot # # 13. From ETT # # 14. To capital account # # 15. To C2C # # 16. To Spot # # 17. To Futures # # 18. To ETT # # 19. Mandatory Repayment # # 20. From Piggybank # # 21. To Piggybank # # 22. From Perpetual # # 23. To Perpetual # # 24. Liquidation Fee # # 54. Clawback # # 59. Airdrop Return. # # # request['type'] = 'number' # All types will be returned if self filed is left blank # } # elif type == 'account': if code is not None: currency = self.currency(code) request['currency'] = currency['id'] # # # # # 1. deposit # # 2. withdrawal # # 13. cancel withdrawal # # 18. into futures account # # 19. out of futures account # # 20. into sub account # # 21. out of sub account # # 28. claim # # 29. into ETT account # # 30. out of ETT account # # 31. into C2C account # # 32. out of C2C account # # 33. into margin account # # 34. out of margin account # # 37. into spot account # # 38. out of spot account # # # request['type'] = 'number' # else: raise NotSupported(self.id + " fetchLedger does not support the '" + type + "' type(the type must be one of 'account', 'spot', 'margin', 'futures', 'swap')") method = type + 'Get' + suffix + argument + 'Ledger' response = getattr(self, method)(self.extend(request, query)) # # transfer funds transfer in/out # trade funds moved as a result of a trade, spot and margin accounts only # rebate fee rebate as per fee schedule, spot and margin accounts only # match open long/open short/close long/close short(futures) or a change in the amount because of trades(swap) # fee fee, futures only # settlement settlement/clawback/settle long/settle short # liquidation force close long/force close short/deliver close long/deliver close short # funding funding fee, swap only # margin a change in the amount after adjusting margin, swap only # # account # # [ # { # "amount":0.00051843, # "balance":0.00100941, # "currency":"BTC", # "fee":0, # "ledger_id":8987285, # "timestamp":"2018-10-12T11:01:14.000Z", # "typename":"Get from activity" # } # ] # # spot # # [ # { # "timestamp":"2019-03-18T07:08:25.000Z", # "ledger_id":"3995334780", # "created_at":"2019-03-18T07:08:25.000Z", # "currency":"BTC", # "amount":"0.0009985", # "balance":"0.0029955", # "type":"trade", # "details":{ # "instrument_id":"BTC-USDT", # "order_id":"2500650881647616", # "product_id":"BTC-USDT" # } # } # ] # # margin # # [ # [ # { # "created_at":"2019-03-20T03:45:05.000Z", # "ledger_id":"78918186", # "timestamp":"2019-03-20T03:45:05.000Z", # "currency":"EOS", # "amount":"0", # ? # "balance":"0.59957711", # "type":"transfer", # "details":{ # "instrument_id":"EOS-USDT", # "order_id":"787057", # "product_id":"EOS-USDT" # } # } # ], # { # "before":"78965766", # "after":"78918186" # } # ] # # futures # # [ # { # "ledger_id":"2508090544914461", # "timestamp":"2019-03-19T14:40:24.000Z", # "amount":"-0.00529521", # "balance":"0", # "currency":"EOS", # "type":"fee", # "details":{ # "order_id":"2506982456445952", # "instrument_id":"EOS-USD-190628" # } # } # ] # # swap # # [ # { # "amount":"0.004742", # "fee":"-0.000551", # "type":"match", # "instrument_id":"EOS-USD-SWAP", # "ledger_id":"197429674941902848", # "timestamp":"2019-03-25T05:56:31.286Z" # }, # ] # responseLength = len(response) if responseLength < 1: return [] isArray = isinstance(response[0], list) isMargin = (type == 'margin') entries = response[0] if (isMargin and isArray) else response if type == 'swap': ledgerEntries = self.parse_ledger(entries) return self.filter_by_symbol_since_limit(ledgerEntries, code, since, limit) return self.parse_ledger(entries, currency, since, limit) def parse_ledger_entry_type(self, type): types = { 'transfer': 'transfer', # # funds transfer in/out 'trade': 'trade', # funds moved as a result of a trade, spot and margin accounts only 'rebate': 'rebate', # fee rebate as per fee schedule, spot and margin accounts only 'match': 'trade', # open long/open short/close long/close short(futures) or a change in the amount because of trades(swap) 'fee': 'fee', # fee, futures only 'settlement': 'trade', # settlement/clawback/settle long/settle short 'liquidation': 'trade', # force close long/force close short/deliver close long/deliver close short 'funding': 'fee', # funding fee, swap only 'margin': 'margin', # a change in the amount after adjusting margin, swap only } return self.safe_string(types, type, type) def parse_ledger_entry(self, item, currency=None): # # # account # # { # "amount":0.00051843, # "balance":0.00100941, # "currency":"BTC", # "fee":0, # "ledger_id":8987285, # "timestamp":"2018-10-12T11:01:14.000Z", # "typename":"Get from activity" # } # # spot # # { # "timestamp":"2019-03-18T07:08:25.000Z", # "ledger_id":"3995334780", # "created_at":"2019-03-18T07:08:25.000Z", # "currency":"BTC", # "amount":"0.0009985", # "balance":"0.0029955", # "type":"trade", # "details":{ # "instrument_id":"BTC-USDT", # "order_id":"2500650881647616", # "product_id":"BTC-USDT" # } # } # # margin # # { # "created_at":"2019-03-20T03:45:05.000Z", # "ledger_id":"78918186", # "timestamp":"2019-03-20T03:45:05.000Z", # "currency":"EOS", # "amount":"0", # ? # "balance":"0.59957711", # "type":"transfer", # "details":{ # "instrument_id":"EOS-USDT", # "order_id":"787057", # "product_id":"EOS-USDT" # } # } # # futures # # { # "ledger_id":"2508090544914461", # "timestamp":"2019-03-19T14:40:24.000Z", # "amount":"-0.00529521", # "balance":"0", # "currency":"EOS", # "type":"fee", # "details":{ # "order_id":"2506982456445952", # "instrument_id":"EOS-USD-190628" # } # } # # swap # # { # "amount":"0.004742", # "fee":"-0.000551", # "type":"match", # "instrument_id":"EOS-USD-SWAP", # "ledger_id":"197429674941902848", # "timestamp":"2019-03-25T05:56:31.286Z" # }, # id = self.safe_string(item, 'ledger_id') account = None details = self.safe_value(item, 'details', {}) referenceId = self.safe_string(details, 'order_id') referenceAccount = None type = self.parse_ledger_entry_type(self.safe_string(item, 'type')) code = self.safe_currency_code(self.safe_string(item, 'currency'), currency) amount = self.safe_float(item, 'amount') timestamp = self.parse8601(self.safe_string(item, 'timestamp')) fee = { 'cost': self.safe_float(item, 'fee'), 'currency': code, } before = None after = self.safe_float(item, 'balance') status = 'ok' marketId = self.safe_string(item, 'instrument_id') symbol = None if marketId in self.markets_by_id: market = self.markets_by_id[marketId] symbol = market['symbol'] return { 'info': item, 'id': id, 'account': account, 'referenceId': referenceId, 'referenceAccount': referenceAccount, 'type': type, 'currency': code, 'symbol': symbol, 'amount': amount, 'before': before, # balance before 'after': after, # balance after 'status': status, 'timestamp': timestamp, 'datetime': self.iso8601(timestamp), 'fee': fee, } def sign(self, path, api='public', method='GET', params={}, headers=None, body=None): isArray = isinstance(params, list) request = '/api/' + api + '/' + self.version + '/' request += path if isArray else self.implode_params(path, params) query = params if isArray else self.omit(params, self.extract_params(path)) url = self.implode_params(self.urls['api']['rest'], {'hostname': self.hostname}) + request type = self.get_path_authentication_type(path) if type == 'public': if query: url += '?' + self.urlencode(query) elif type == 'private': self.check_required_credentials() timestamp = self.iso8601(self.milliseconds()) headers = { 'OK-ACCESS-KEY': self.apiKey, 'OK-ACCESS-PASSPHRASE': self.password, 'OK-ACCESS-TIMESTAMP': timestamp, # 'OK-FROM': '', # 'OK-TO': '', # 'OK-LIMIT': '', } auth = timestamp + method + request if method == 'GET': if query: urlencodedQuery = '?' + self.urlencode(query) url += urlencodedQuery auth += urlencodedQuery else: if isArray or query: body = self.json(query) auth += body headers['Content-Type'] = 'application/json' signature = self.hmac(self.encode(auth), self.encode(self.secret), hashlib.sha256, 'base64') headers['OK-ACCESS-SIGN'] = signature return {'url': url, 'method': method, 'body': body, 'headers': headers} def get_path_authentication_type(self, path): # https://github.com/ccxt/ccxt/issues/6651 # a special case to handle the optionGetUnderlying interefering with # other endpoints containing self keyword if path == 'underlying': return 'public' auth = self.safe_value(self.options, 'auth', {}) key = self.find_broadly_matched_key(auth, path) return self.safe_string(auth, key, 'private') def handle_errors(self, code, reason, url, method, headers, body, response, requestHeaders, requestBody): if not response: return # fallback to default error handler feedback = self.id + ' ' + body if code == 503: # {"message":"name resolution failed"} raise ExchangeNotAvailable(feedback) # # {"error_message":"Order does not exist","result":"true","error_code":"35029","order_id":"-1"} # message = self.safe_string(response, 'message') errorCode = self.safe_string_2(response, 'code', 'error_code') nonEmptyMessage = ((message is not None) and (message != '')) nonZeroErrorCode = (errorCode is not None) and (errorCode != '0') if nonEmptyMessage: self.throw_exactly_matched_exception(self.exceptions['exact'], message, feedback) self.throw_broadly_matched_exception(self.exceptions['broad'], message, feedback) if nonZeroErrorCode: self.throw_exactly_matched_exception(self.exceptions['exact'], errorCode, feedback) if nonZeroErrorCode or nonEmptyMessage: raise ExchangeError(feedback) # unknown message
python/ccxt/okex.py
164,036
-*- coding: utf-8 -*- PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN: https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.mdhow-to-contribute-code ----------------------------------------------------------------------------- Python 3 Python 2 up to 3000 requests per 5 minutes ≈ 600 requests per minute ≈ 10 requests per second ≈ 100 ms see below public public public public public http error codes 400 Bad Request — Invalid request format 401 Unauthorized — Invalid API Key 403 Forbidden — You do not have access to the requested resource 404 Not Found 429 Client Error: Too Many Requests for url 500 Internal Server Error — We had a problem with our server {"code": 1, "message": "System error"} undocumented {"message": "failure to get a peer from the ring-balancer"} {"message": "Server is busy, please try again."} {"message": "An unexpected error occurred"} {"error_message":"System error","message":"System error"} {"code": 4010, "message": "For the security of your funds, withdrawals are not permitted within 24 hours after changing fund password / mobile number / Google Authenticator settings "} common '0': ExchangeError, 200 successful,when the order placement / cancellation / operation is successful no data received in 30s Buffer full. cannot write data -------------------------------------------------------- {"code": 30001, "message": 'request header "OK_ACCESS_KEY" cannot be blank'} {"code": 30002, "message": 'request header "OK_ACCESS_SIGN" cannot be blank'} {"code": 30003, "message": 'request header "OK_ACCESS_TIMESTAMP" cannot be blank'} {"code": 30004, "message": 'request header "OK_ACCESS_PASSPHRASE" cannot be blank'} {"code": 30005, "message": "invalid OK_ACCESS_TIMESTAMP"} {"code": 30006, "message": "invalid OK_ACCESS_KEY"} {"code": 30007, "message": 'invalid Content_Type, please use "application/json" format'} {"code": 30008, "message": "timestamp request expired"} {"code": 30009, "message": "system error"} {"code": 30010, "message": "API validation failed"} {"code": 30011, "message": "invalid IP"} {"code": 30012, "message": "invalid authorization"} {"code": 30013, "message": "invalid sign"} {"code": 30014, "message": "request too frequent"} {"code": 30015, "message": 'request header "OK_ACCESS_PASSPHRASE" incorrect'} {"code": 30015, "message": "you are using v1 apiKey, please use v1 endpoint. If you would like to use v3 endpoint, please subscribe to v3 apiKey"} {"code": 30017, "message": "apikey's broker id does not match"} {"code": 30018, "message": "apikey's domain does not match"} {"code": 30019, "message": "Api is offline or unavailable"} {"code": 30020, "message": "body cannot be blank"} {"code": 30021, "message": "Json data format error"}, {"code": 30021, "message": "json data format error"} {"code": 30022, "message": "Api has been frozen"} {"code": 30023, "message": "{0} parameter cannot be blank"} {"code":30024,"message":"\"instrument_id\" is an invalid parameter"} {"code": 30025, "message": "{0} parameter category error"} {"code": 30026, "message": "requested too frequent"} {"code": 30027, "message": "login failure"} {"code": 30028, "message": "unauthorized execution"} {"code": 30029, "message": "account suspended"} {"code": 30030, "message": "endpoint request failed. Please try again"} {"code": 30031, "message": "token does not exist"} {"code": 30032, "message": "pair does not exist"} {"code": 30033, "message": "exchange domain does not exist"} {"code": 30034, "message": "exchange ID does not exist"} {"code": 30035, "message": "trading is not supported in self website"} {"code": 30036, "message": "no relevant data"} {"code": 30037, "message": "endpoint is offline or unavailable"} '30038': AuthenticationError, {"code": 30038, "message": "user does not exist"} {"client_oid":"","code":"30038","error_code":"30038","error_message":"Matching engine is being upgraded. Please try in about 1 minute.","message":"Matching engine is being upgraded. Please try in about 1 minute.","order_id":"-1","result":false} {"code":30044, "message":"Endpoint request timeout"} futures {"code": 32001, "message": "futures account suspended"} {"code": 32002, "message": "futures account does not exist"} {"code": 32003, "message": "canceling, please wait"} {"code": 32004, "message": "you have no unfilled orders"} {"code": 32005, "message": "max order quantity"} {"code": 32006, "message": "the order price or trigger price exceeds USD 1 million"} {"code": 32007, "message": "leverage level must be the same for orders on the same side of the contract"} {"code": 32008, "message": "Max. positions to open(cross margin)"} {"code": 32009, "message": "Max. positions to open(fixed margin)"} {"code": 32010, "message": "leverage cannot be changed with open positions"} {"code": 32011, "message": "futures status error"} {"code": 32012, "message": "futures order update error"} {"code": 32013, "message": "token type is blank"} {"code": 32014, "message": "your number of contracts closing is larger than the number of contracts available"} {"code": 32015, "message": "margin ratio is lower than 100% before opening positions"} {"code": 32016, "message": "margin ratio is lower than 100% after opening position"} {"code": 32017, "message": "no BBO"} {"code": 32018, "message": "the order quantity is less than 1, please try again"} {"code": 32019, "message": "the order price deviates from the price of the previous minute by more than 3%"} {"code": 32020, "message": "the price is not in the range of the price limit"} {"code": 32021, "message": "leverage error"} {"code": 32022, "message": "self function is not supported in your country or region according to the regulations"} {"code": 32023, "message": "self account has outstanding loan"} {"code": 32024, "message": "order cannot be placed during delivery"} {"code": 32025, "message": "order cannot be placed during settlement"} {"code": 32026, "message": "your account is restricted from opening positions"} {"code": 32027, "message": "cancelled over 20 orders"} {"code": 32028, "message": "account is suspended and liquidated"} {"code": 32029, "message": "order info does not exist"} The order cannot be cancelled client_oid or order_id is required. User does not exist User have open contract orders or position {"code": 32044, "message": "The margin ratio after submitting self order is lower than the minimum requirement({0}) for your tier."} String of commission over 1 million Each user can hold up to 10 trade plans at the same time system error Order strategy track range error Each user can hold up to 10 track plans at the same time Order strategy rang error Order strategy ice depth error String of commission over 100 thousand Each user can hold up to 6 ice plans at the same time The order price is zero. Market-close-all function cannot be executed Trade not allow cancel order error iceberg per order average should between {0}-{1} contracts Each user can hold up to 6 initiative plans at the same time Total amount should exceed per order amount Order strategy type error Order strategy initiative limit error Order strategy initiative range error Order strategy initiative rate error Time Stringerval of orders should set between 5-120s Close amount exceeds the limit of Market-close-all(999 for BTC, and 9999 for the rest tokens) You have open orders. Please cancel all open orders before changing your leverage level. Account equity < required margin in self setting. Please adjust your leverage level again. The margin for self position will fall short of the required margin in self setting. Please adjust your leverage level or increase your margin to proceed. Target leverage level too low. Your account balance is insufficient to cover the margin required. Please adjust the leverage level again. Please check open position or unfilled order Your current liquidation mode does not support self action. The highest available margin for your order’s tier is {0}. Please edit your margin and place a new order. The action does not apply to the token The number of contracts of your position, open orders, and the current order has exceeded the maximum order limit of self asset. Account risk rate breach Liquidation of the holding position(s) at market price will require cancellation of all pending close orders of the contracts. Your margin for self asset in futures account is insufficient and the position has been taken over for liquidation.(You will not be able to place orders, close positions, transfer funds, or add margin during self period of time. Your account will be restored after the liquidation is complete.) Please cancel all open orders before switching the liquidation mode(Please cancel all open orders before switching the liquidation mode) Your open positions are at high risk.(Please add margin or reduce positions before switching the mode) Funds cannot be transferred out within 30 minutes after futures settlement The number of contracts should be a positive multiple of %%. Please place your order again token and margin trading {"code": 33001, "message": "margin account for self pair is not enabled yet"} {"code": 33002, "message": "margin account for self pair is suspended"} {"code": 33003, "message": "no loan balance"} {"code": 33004, "message": "loan amount cannot be smaller than the minimum limit"} {"code": 33005, "message": "repayment amount must exceed 0"} {"code": 33006, "message": "loan order not found"} {"code": 33007, "message": "status not found"} {"code": 33008, "message": "loan amount cannot exceed the maximum limit"} {"code": 33009, "message": "user ID is blank"} {"code": 33010, "message": "you cannot cancel an order during session 2 of call auction"} {"code": 33011, "message": "no new market data"} {"code": 33012, "message": "order cancellation failed"} {"code": 33013, "message": "order placement failed"} {"code": 33014, "message": "order does not exist"} {"code": 33015, "message": "exceeded maximum limit"} {"code": 33016, "message": "margin trading is not open for self token"} {"code": 33017, "message": "insufficient balance"} {"code": 33018, "message": "self parameter must be smaller than 1"} {"code": 33020, "message": "request not supported"} {"code": 33021, "message": "token and the pair do not match"} {"code": 33022, "message": "pair and the order do not match"} {"code": 33023, "message": "you can only place market orders during call auction"} {"code": 33024, "message": "trading amount too small"} {"code": 33025, "message": "base token amount is blank"} {"code": 33026, "message": "transaction completed"} {"code": 33027, "message": "cancelled order or order cancelling"} {"code": 33028, "message": "the decimal places of the trading price exceeded the limit"} {"code": 33029, "message": "the decimal places of the trading size exceeded the limit"} {"code": 33034, "message": "You can only place limit order after Call Auction has started"} This type of order cannot be canceled(This type of order cannot be canceled) Exceeding the limit of entrust order The buy order price should be lower than 130% of the trigger price The sell order price should be higher than 70% of the trigger price The limit of callback rate is 0 < x <= 5% The trigger price of a buy order should be lower than the latest transaction price The trigger price of a sell order should be higher than the latest transaction price The limit of price variance is 0 < x <= 1% The total amount must be larger than 0 The average amount should be 1/1000 * total amount <= x <= total amount The price should not be 0, including trigger price, order price, and price limit Price variance should be 0 < x <= 1% Sweep ratio should be 0 < x <= 100% Per order limit: Total amount/1000 < x <= Total amount Total amount should be X > 0 Time interval should be 5 <= x <= 120s cancel order number not higher limit: plan and track entrust no more than 10, ice and time entrust no more than 6 {"code": 33059, "message": "client_oid or order_id is required"} {"code": 33060, "message": "Only fill in either parameter client_oid or order_id"} Value of a single market price order cannot exceed 100,000 USD The leverage ratio is too high. The borrowed position has exceeded the maximum position of self leverage ratio. Please readjust the leverage ratio Leverage multiple is too low, there is insufficient margin in the account, please readjust the leverage ratio The setting of the leverage ratio cannot be less than 2, please readjust the leverage ratio Leverage ratio exceeds maximum leverage ratio, please readjust leverage ratio The value of the position and buying order has reached the position limit, and no further buying is allowed. account Funds cannot be transferred out within 30 minutes after swap settlement(Funds cannot be transferred out within 30 minutes after swap settlement) {"code": 34001, "message": "withdrawal suspended"} {"code": 34002, "message": "please add a withdrawal address"} {"code": 34003, "message": "sorry, self token cannot be withdrawn to xx at the moment"} {"code": 34004, "message": "withdrawal fee is smaller than minimum limit"} {"code": 34005, "message": "withdrawal fee exceeds the maximum limit"} {"code": 34006, "message": "withdrawal amount is lower than the minimum limit"} {"code": 34007, "message": "withdrawal amount exceeds the maximum limit"} {"code": 34008, "message": "insufficient balance"} {"code": 34009, "message": "your withdrawal amount exceeds the daily limit"} {"code": 34010, "message": "transfer amount must be larger than 0"} {"code": 34011, "message": "conditions not met"} {"code": 34012, "message": "the minimum withdrawal amount for NEO is 1, and the amount must be an integer"} {"code": 34013, "message": "please transfer"} {"code": 34014, "message": "transfer limited"} {"code": 34015, "message": "subaccount does not exist"} {"code": 34016, "message": "transfer suspended"} {"code": 34017, "message": "account suspended"} {"code": 34018, "message": "incorrect trades password"} {"code": 34019, "message": "please bind your email before withdrawal"} {"code": 34020, "message": "please bind your funds password before withdrawal"} {"code": 34021, "message": "Not verified address"} {"code": 34022, "message": "Withdrawals are not available for sub accounts"} {"code": 34023, "message": "Please enable futures trading before transferring your funds"} transfer too frequently(transfer too frequently) Parameter is incorrect, please refer to API documentation Get the sub-account balance interface, account type is not supported Since your C2C transaction is unusual, you are restricted from fund transfer. Please contact our customer support to cancel the restriction You are now restricted from transferring out your funds due to abnormal trades on C2C Market. Please transfer your fund on our website or app instead to verify your identity swap {"code": 35001, "message": "Contract does not exist"} {"code": 35002, "message": "Contract settling"} {"code": 35003, "message": "Contract paused"} {"code": 35004, "message": "Contract pending settlement"} {"code": 35005, "message": "User does not exist"} {"code": 35008, "message": "Risk ratio too high"} {"code": 35010, "message": "Position closing too large"} {"code": 35012, "message": "Incorrect order size"} {"code": 35014, "message": "Order price is not within limit"} {"code": 35015, "message": "Invalid leverage level"} {"code": 35017, "message": "Open orders exist"} {"code": 35019, "message": "Order size too large"} {"code": 35020, "message": "Order price too high"} {"code": 35021, "message": "Order size exceeded current tier limit"} {"code": 35022, "message": "Contract status error"} {"code": 35024, "message": "Contract not initialized"} {"code": 35025, "message": "No account balance"} {"code": 35026, "message": "Contract settings not initialized"} {"code": 35029, "message": "Order does not exist"} {"code": 35030, "message": "Order size too large"} {"code": 35031, "message": "Cancel order size too large"} {"code": 35032, "message": "Invalid user status"} No last traded price in cache {"code": 35039, "message": "Open order quantity exceeds limit"} {"error_message":"Invalid order type","result":"true","error_code":"35040","order_id":"-1"} {"code": 35044, "message": "Invalid order status"} {"code": 35046, "message": "Negative account balance"} {"code": 35047, "message": "Insufficient account balance"} {"code": 35048, "message": "User contract is frozen and liquidating"} {"code": 35049, "message": "Invalid order type"} {"code": 35050, "message": "Position settings are blank"} {"code": 35052, "message": "Insufficient cross margin"} {"code": 35053, "message": "Account risk too high"} {"code": 35055, "message": "Insufficient account balance"} {"code": 35057, "message": "No last traded price"} {"code": 35058, "message": "No limit"} {"code": 35059, "message": "client_oid or order_id is required"} {"code": 35060, "message": "Only fill in either parameter client_oid or order_id"} {"code": 35061, "message": "Invalid instrument_id"} {"code": 35062, "message": "Invalid match_price"} {"code": 35063, "message": "Invalid order_size"} {"code": 35064, "message": "Invalid client_oid"} Order interval error Time-weighted order ratio error Time-weighted order range error Time-weighted single transaction limit error Algo order type error Order total must be larger than single order limit Maximum 6 unfulfilled time-weighted orders can be held at the same time Order price is 0. Market-close-all not available Iceberg order single transaction average error Failed to cancel order LTC 20x leverage. Not allowed to open position Maximum 6 unfulfilled iceberg orders can be held at the same time Order amount exceeded 100,000 Iceberg order price variance error Callback rate error Maximum 10 unfulfilled trail orders can be held at the same time Trail order callback rate error Each user can only hold a maximum of 10 unfulfilled stop-limit orders at the same time Order amount exceeded 1 million Order amount is not in the correct range Price exceeds 100 thousand Price exceeds 100 thousand Average amount error Price exceeds 100 thousand No stop-limit orders available for cancelation No trail orders available for cancellation No iceberg orders available for cancellation No trail orders available for cancellation Stop-limit order last traded price error Instrument_id error Algo order status error Order status and order ID cannot exist at the same time An order status or order ID must exist Algo order ID error {"error_message":"The operation that close all at market price is too frequent","result":"true","error_code":"35102","order_id":"-1"} option Invalid underlying index. Instrument does not exist. Instrument status is invalid. Account does not exist. Account status is invalid. Account is suspended due to ongoing liquidation. Account is not enabled for options trading. Please enable the account for option contract. Funds cannot be transferred in or out, as account is suspended. Funds cannot be transferred out within 30 minutes after option exercising or settlement. Funds cannot be transferred in or out, as equity of the account is less than zero. Funds cannot be transferred in or out during option exercising or settlement. New order function is blocked. Account does not have permission to short option. Invalid format for client_oid. Invalid format for request_id. Instrument id does not match underlying index. Order_id and client_oid can not be used at the same time. Either order price or fartouch price must be present. Either order price or size must be present. Either order_id or client_oid must be present. Either order_ids or client_oids must be present. Exceeding max batch size for order submission. Exceeding max batch size for oder cancellation. Exceeding max batch size for order amendment. Instrument does not have valid bid/ask quote. Order does not exist. Order submission failed. Order cancellation failed. Order amendment failed. Order is pending cancel. Order qty is not valid multiple of lot size. Order price is breaching highest buy limit. Order price is breaching lowest sell limit. Exceeding max order size. Exceeding max open order count for instrument. Exceeding max open order count for underlying. Exceeding max open size across all orders for underlying Exceeding max available qty for instrument. Exceeding max available qty for underlying. Exceeding max position limit for underlying. Candles or HistoryCandles 'account', 'spot', 'margin', 'futures', 'swap', 'option' OKEX refers to ERC20 version of Aeternity(AEToken) https://github.com/ccxt/ccxt/issues/4981 https://github.com/ccxt/ccxt/issues/5701 { "iso": "2015-01-07T23:47:25.201Z", "epoch": 1420674445.201 } spot markets { base_currency: "EOS", instrument_id: "EOS-OKB", min_size: "0.01", quote_currency: "OKB", size_increment: "0.000001", tick_size: "0.0001" } futures markets { instrument_id: "XRP-USD-200320", underlying_index: "XRP", quote_currency: "USD", tick_size: "0.0001", contract_val: "10", listing: "2020-03-06", delivery: "2020-03-20", trade_increment: "1", alias: "self_week", underlying: "XRP-USD", base_currency: "XRP", settlement_currency: "XRP", is_inverse: "true", contract_val_currency: "USD", } swap markets { instrument_id: "BSV-USD-SWAP", underlying_index: "BSV", quote_currency: "USD", coin: "BSV", contract_val: "10", listing: "2018-12-21T07:53:47.000Z", delivery: "2020-03-14T08:00:00.000Z", size_increment: "1", tick_size: "0.01", base_currency: "BSV", underlying: "BSV-USD", settlement_currency: "BSV", is_inverse: "true", contract_val_currency: "USD" } options markets { instrument_id: 'BTC-USD-200327-4000-C', underlying: 'BTC-USD', settlement_currency: 'BTC', contract_val: '0.1000', option_type: 'C', strike: '4000', tick_size: '0.0005', lot_size: '1.0000', listing: '2019-12-25T08:30:36.302Z', delivery: '2020-03-27T08:00:00.000Z', state: '2', trading_start_time: '2019-12-25T08:30:36.302Z', timestamp: '2020-03-13T08:05:09.456Z', } options markets [ { instrument_id: 'BTC-USD-200327-4000-C', underlying: 'BTC-USD', settlement_currency: 'BTC', contract_val: '0.1000', option_type: 'C', strike: '4000', tick_size: '0.0005', lot_size: '1.0000', listing: '2019-12-25T08:30:36.302Z', delivery: '2020-03-27T08:00:00.000Z', state: '2', trading_start_time: '2019-12-25T08:30:36.302Z', timestamp: '2020-03-13T08:05:09.456Z', }, ] spot markets [ { base_currency: "EOS", instrument_id: "EOS-OKB", min_size: "0.01", quote_currency: "OKB", size_increment: "0.000001", tick_size: "0.0001" } ] futures markets [ { instrument_id: "XRP-USD-200320", underlying_index: "XRP", quote_currency: "USD", tick_size: "0.0001", contract_val: "10", listing: "2020-03-06", delivery: "2020-03-20", trade_increment: "1", alias: "self_week", underlying: "XRP-USD", base_currency: "XRP", settlement_currency: "XRP", is_inverse: "true", contract_val_currency: "USD", } ] swap markets [ { instrument_id: "BSV-USD-SWAP", underlying_index: "BSV", quote_currency: "USD", coin: "BSV", contract_val: "10", listing: "2018-12-21T07:53:47.000Z", delivery: "2020-03-14T08:00:00.000Z", size_increment: "1", tick_size: "0.01", base_currency: "BSV", underlying: "BSV-USD", settlement_currency: "BSV", is_inverse: "true", contract_val_currency: "USD" } ] has['fetchCurrencies'] is currently set to False despite that their docs say these endpoints are public: https://www.okex.com/api/account/v3/withdrawal/fee https://www.okex.com/api/account/v3/currencies it will still reply with {"code":30001, "message": "OK-ACCESS-KEY header is required"} if you attempt to access it without authentication [ { name: '', currency: 'BTC', can_withdraw: '1', can_deposit: '1', min_withdrawal: '0.0100000000000000' }, ] default precision, todo: fix "magic constants" todo: redesign max 200 { asks: [["0.02685268", "0.242571", "1"], ["0.02685493", "0.164085", "1"], ... ["0.02779", "1.039", "1"], ["0.027813", "0.0876", "1"] ], bids: [["0.02684052", "10.371849", "1"], ["0.02684051", "3.707", "4"], ... ["0.02634963", "0.132934", "1"], ["0.02634962", "0.264838", "2"] ], timestamp: "2018-12-17T20:24:16.159Z" } { best_ask: "0.02665472", best_bid: "0.02665221", instrument_id: "ETH-BTC", product_id: "ETH-BTC", last: "0.02665472", ask: "0.02665472", missing in the docs bid: "0.02665221", not mentioned in the docs open_24h: "0.02645482", high_24h: "0.02714633", low_24h: "0.02614109", base_volume_24h: "572298.901923", timestamp: "2018-12-17T21:20:07.856Z", quote_volume_24h: "15094.86831261" } { best_ask: "0.02665472", best_bid: "0.02665221", instrument_id: "ETH-BTC", product_id: "ETH-BTC", last: "0.02665472", ask: "0.02665472", bid: "0.02665221", open_24h: "0.02645482", high_24h: "0.02714633", low_24h: "0.02614109", base_volume_24h: "572298.901923", timestamp: "2018-12-17T21:20:07.856Z", quote_volume_24h: "15094.86831261" } fetchTrades(public) spot trades { time: "2018-12-17T23:31:08.268Z", timestamp: "2018-12-17T23:31:08.268Z", trade_id: "409687906", price: "0.02677805", size: "0.923467", side: "sell" } futures trades, swap trades { trade_id: "1989230840021013", side: "buy", price: "92.42", qty: "184", missing in swap markets size: "5", missing in futures markets timestamp: "2018-12-17T23:26:04.613Z" } fetchOrderTrades(private) spot trades, margin trades { "created_at":"2019-03-15T02:52:56.000Z", "exec_type":"T", whether the order is taker or maker "fee":"0.00000082", "instrument_id":"BTC-USDT", "ledger_id":"3963052721", "liquidity":"T", whether the order is taker or maker "order_id":"2482659399697408", "price":"3888.6", "product_id":"BTC-USDT", "side":"buy", "size":"0.00055306", "timestamp":"2019-03-15T02:52:56.000Z" }, futures trades, swap trades { "trade_id":"197429674631450625", "instrument_id":"EOS-USD-SWAP", "order_id":"6a-7-54d663a28-0", "price":"3.633", "order_qty":"1.0000", "fee":"-0.000551", "created_at":"2019-03-21T04:41:58.0Z", missing in swap trades "timestamp":"2019-03-25T05:56:31.287Z", missing in futures trades "exec_type":"M", whether the order is taker or maker "side":"short", "buy" in futures trades } fee is either a positive number(invitation rebate) or a negative number(transaction fee deduction) therefore we need to invert the fee more about it https://github.com/ccxt/ccxt/issues/5909 maximum = default = 100 from: 'id', to: 'id', spot markets [ { time: "2018-12-17T23:31:08.268Z", timestamp: "2018-12-17T23:31:08.268Z", trade_id: "409687906", price: "0.02677805", size: "0.923467", side: "sell" } ] futures markets, swap markets [ { trade_id: "1989230840021013", side: "buy", price: "92.42", qty: "184", missing in swap markets size: "5", missing in futures markets timestamp: "2018-12-17T23:26:04.613Z" } ] spot markets { close: "0.02684545", high: "0.02685084", low: "0.02683312", open: "0.02683894", time: "2018-12-17T20:28:00.000Z", volume: "101.457222" } futures markets [ 1545072720000, 0.3159, 0.3161, 0.3144, 0.3149, 22886, 725179.26172331, ] timestamp Open High Low Close self.safe_float(ohlcv, 5), Quote Volume self.safe_float(ohlcv, 6), Base Volume Volume, okex will return base volume in the 7th element for future markets Open High Low Close Base Volume Candles or HistoryCandles default spot markets [ { close: "0.02683401", high: "0.02683401", low: "0.02683401", open: "0.02683401", time: "2018-12-17T23:47:00.000Z", volume: "0" }, { close: "0.02684545", high: "0.02685084", low: "0.02683312", open: "0.02683894", time: "2018-12-17T20:28:00.000Z", volume: "101.457222" } ] futures [ [ 1545090660000, 0.3171, 0.3174, 0.3171, 0.3173, 1648, 51930.38579450868 ], [ 1545072720000, 0.3159, 0.3161, 0.3144, 0.3149, 22886, 725179.26172331 ] ] account [ { balance: 0, available: 0, currency: "BTC", hold: 0 }, { balance: 0, available: 0, currency: "ETH", hold: 0 } ] spot [ { frozen: "0", hold: "0", id: "2149632", currency: "BTC", balance: "0.0000000497717339", available: "0.0000000497717339", holds: "0" }, { frozen: "0", hold: "0", id: "2149632", currency: "ICN", balance: "0.00000000925", available: "0.00000000925", holds: "0" } ] [ { "currency:BTC": { "available":"0", "balance":"0", "borrowed":"0", "can_withdraw":"0", "frozen":"0", "hold":"0", "holds":"0", "lending_fee":"0" }, "currency:USDT": { "available":"100", "balance":"100", "borrowed":"0", "can_withdraw":"100", "frozen":"0", "hold":"0", "holds":"0", "lending_fee":"0" }, "instrument_id":"BTC-USDT", "liquidation_price":"0", "product_id":"BTC-USDT", "risk_rate":"" }, ] { "info":{ "eos":{ "auto_margin":"0", "contracts": [ { "available_qty":"40.37069445", "fixed_balance":"0", "instrument_id":"EOS-USD-190329", "margin_for_unfilled":"0", "margin_frozen":"0", "realized_pnl":"0", "unrealized_pnl":"0" }, { "available_qty":"40.37069445", "fixed_balance":"14.54895721", "instrument_id":"EOS-USD-190628", "margin_for_unfilled":"0", "margin_frozen":"10.64042157", "realized_pnl":"-3.90853564", "unrealized_pnl":"-0.259" }, ], "equity":"50.75220665", "margin_mode":"fixed", "total_avail_balance":"40.37069445" }, } } their root field name is "info", so our info will contain their info it may be incorrect to use total, free and used for swap accounts { "info": [ { "equity":"3.0139", "fixed_balance":"0.0000", "instrument_id":"EOS-USD-SWAP", "margin":"0.5523", "margin_frozen":"0.0000", "margin_mode":"crossed", "margin_ratio":"1.0913", "realized_pnl":"-0.0006", "timestamp":"2019-03-25T03:46:10.336Z", "total_avail_balance":"3.0000", "unrealized_pnl":"0.0145" } ] } their root field name is "info", so our info will contain their info it may be incorrect to use total, free and used for swap accounts account [ { balance: 0, available: 0, currency: "BTC", hold: 0 }, { balance: 0, available: 0, currency: "ETH", hold: 0 } ] spot [ { frozen: "0", hold: "0", id: "2149632", currency: "BTC", balance: "0.0000000497717339", available: "0.0000000497717339", holds: "0" }, { frozen: "0", hold: "0", id: "2149632", currency: "ICN", balance: "0.00000000925", available: "0.00000000925", holds: "0" } ] margin [ { "currency:BTC": { "available":"0", "balance":"0", "borrowed":"0", "can_withdraw":"0", "frozen":"0", "hold":"0", "holds":"0", "lending_fee":"0" }, "currency:USDT": { "available":"100", "balance":"100", "borrowed":"0", "can_withdraw":"100", "frozen":"0", "hold":"0", "holds":"0", "lending_fee":"0" }, "instrument_id":"BTC-USDT", "liquidation_price":"0", "product_id":"BTC-USDT", "risk_rate":"" }, ] futures { "info":{ "eos":{ "auto_margin":"0", "contracts": [ { "available_qty":"40.37069445", "fixed_balance":"0", "instrument_id":"EOS-USD-190329", "margin_for_unfilled":"0", "margin_frozen":"0", "realized_pnl":"0", "unrealized_pnl":"0" }, { "available_qty":"40.37069445", "fixed_balance":"14.54895721", "instrument_id":"EOS-USD-190628", "margin_for_unfilled":"0", "margin_frozen":"10.64042157", "realized_pnl":"-3.90853564", "unrealized_pnl":"-0.259" }, ], "equity":"50.75220665", "margin_mode":"fixed", "total_avail_balance":"40.37069445" }, } } swap { "info": [ { "equity":"3.0139", "fixed_balance":"0.0000", "instrument_id":"EOS-USD-SWAP", "margin":"0.5523", "margin_frozen":"0.0000", "margin_mode":"crossed", "margin_ratio":"1.0913", "realized_pnl":"-0.0006", "timestamp":"2019-03-25T03:46:10.336Z", "total_avail_balance":"3.0000", "unrealized_pnl":"0.0145" } ] } 'client_oid': 'abcdef1234567890', [a-z0-9]{1,32} 'order_type': '0', 0 = Normal limit order, 1 = Post only, 2 = Fill Or Kill, 3 = Immediatel Or Cancel, 4 = Market for futures only 1:open long 2:open short 3:close long 4:close short for futures 'match_price': '0', Order at best counter party price?(0:no 1:yes). The default is 0. If it is set as 1, the price parameter will be ignored. When posting orders at best bid price, order_type can only be 0(regular order). order_type == '4' means a market order or '20' 1 = spot, 2 = margin limit/market 1 = spot, 2 = margin for market buy it requires the amount of quote currency to spend { "client_oid":"oktspot79", "error_code":"", "error_message":"", "order_id":"2510789768709120", "result":true } spot, margin { "btc-usdt": [ { "result":true, "client_oid":"a123", "order_id": "2510832677225473" } ] } futures, swap { "result": True, "client_oid": "oktfuture10", missing if requested by order_id "order_id": "2517535534836736", "instrument_id": "EOS-USD-190628" } open long open short close long close short createOrder { "client_oid":"oktspot79", "error_code":"", "error_message":"", "order_id":"2510789768709120", "result":true } cancelOrder { "result": True, "client_oid": "oktfuture10", missing if requested by order_id "order_id": "2517535534836736", instrument_id is missing for spot/margin orders available in futures and swap orders only "instrument_id": "EOS-USD-190628", } fetchOrder, fetchOrdersByState, fetchOpenOrders, fetchClosedOrders spot and margin orders { "client_oid":"oktspot76", "created_at":"2019-03-18T07:26:49.000Z", "filled_notional":"3.9734", "filled_size":"0.001", filled_qty in futures and swap orders "funds":"", self is most likely the same as notional "instrument_id":"BTC-USDT", "notional":"", "order_id":"2500723297813504", "order_type":"0", "price":"4013", "product_id":"BTC-USDT", missing in futures and swap orders "side":"buy", "size":"0.001", "status":"filled", "state": "2", "timestamp":"2019-03-18T07:26:49.000Z", "type":"limit" } futures and swap orders { "instrument_id":"EOS-USD-190628", "size":"10", "timestamp":"2019-03-20T10:04:55.000Z", "filled_qty":"10", filled_size in spot and margin orders "fee":"-0.00841043", "order_id":"2512669605501952", "price":"3.668", "price_avg":"3.567", missing in spot and margin orders "status":"2", "state": "2", "type":"4", "contract_val":"10", "leverage":"10", missing in swap, spot and margin orders "client_oid":"", "pnl":"1.09510794", missing in swap, spo and margin orders "order_type":"0" } fix empty clientOrderId string 'client_oid': 'abcdef12345', optional, [a-z0-9]{1,32} 'order_id': id, spot, margin { "client_oid":"oktspot70", "created_at":"2019-03-15T02:52:56.000Z", "filled_notional":"3.8886", "filled_size":"0.001", "funds":"", "instrument_id":"BTC-USDT", "notional":"", "order_id":"2482659399697408", "order_type":"0", "price":"3927.3", "product_id":"BTC-USDT", "side":"buy", "size":"0.001", "status":"filled", "state": "2", "timestamp":"2019-03-15T02:52:56.000Z", "type":"limit" } futures, swap { "instrument_id":"EOS-USD-190628", "size":"10", "timestamp":"2019-03-20T02:46:38.000Z", "filled_qty":"10", "fee":"-0.0080819", "order_id":"2510946213248000", "price":"3.712", "price_avg":"3.712", "status":"2", "state": "2", "type":"2", "contract_val":"10", "leverage":"10", "client_oid":"", missing in swap orders "pnl":"0", missing in swap orders "order_type":"0" } '-2': failed, '-1': cancelled, '0': open , '1': partially filled, '2': fully filled, '3': submitting, '4': cancelling, '6': incomplete(open+partially filled), '7': complete(cancelled+fully filled), spot, margin [ in fact, self documented API response does not correspond to their actual API response for spot markets OKEX v3 API returns a plain array of orders(see below) [ { "client_oid":"oktspot76", "created_at":"2019-03-18T07:26:49.000Z", "filled_notional":"3.9734", "filled_size":"0.001", "funds":"", "instrument_id":"BTC-USDT", "notional":"", "order_id":"2500723297813504", "order_type":"0", "price":"4013", "product_id":"BTC-USDT", "side":"buy", "size":"0.001", "status":"filled", "state": "2", "timestamp":"2019-03-18T07:26:49.000Z", "type":"limit" }, ], { "before":"2500723297813504", "after":"2500650881647616" } ] futures, swap { "result":true, missing in swap orders "order_info": [ { "instrument_id":"EOS-USD-190628", "size":"10", "timestamp":"2019-03-20T10:04:55.000Z", "filled_qty":"10", "fee":"-0.00841043", "order_id":"2512669605501952", "price":"3.668", "price_avg":"3.567", "status":"2", "state": "2", "type":"4", "contract_val":"10", "leverage":"10", missing in swap orders "client_oid":"", "pnl":"1.09510794", missing in swap orders "order_type":"0" }, ] } in fact, self documented API response does not correspond to their actual API response for spot markets OKEX v3 API returns a plain array of orders '-2': failed, '-1': cancelled, '0': open , '1': partially filled, '2': fully filled, '3': submitting, '4': cancelling, '6': incomplete(open+partially filled), '7': complete(cancelled+fully filled), '-2': failed, '-1': cancelled, '0': open , '1': partially filled, '2': fully filled, '3': submitting, '4': cancelling, '6': incomplete(open+partially filled), '7': complete(cancelled+fully filled), { address: '0x696abb81974a8793352cbd33aadcf78eda3cfdfa', currency: 'eth' tag: 'abcde12345', will be missing if the token does not require a deposit tag payment_id: 'abcde12345', will not be returned if the token does not require a payment_id can_deposit: 1, 0 or 1, documented but missing can_withdraw: 1, 0 or 1, documented but missing } [ { address: '0x696abb81974a8793352cbd33aadcf78eda3cfdfa', currency: 'eth' } ] 2 = OKCoin International, 3 = OKEx 4 = others String. Network transaction fee ≥ 0. Withdrawals to OKCoin or OKEx are fee-free, please set as 0. Withdrawal to external digital asset address requires network transaction fee. { "amount":"0.1", "withdrawal_id":"67485", "currency":"btc", "result":true } deposit statuses { '0': 'waiting for confirmation', '1': 'confirmation account', '2': 'recharge success' } withdrawal statues { '-3': 'pending cancel', '-2': 'cancelled', '-1': 'failed', '0': 'pending', '1': 'sending', '2': 'sent', '3': 'email confirmation', '4': 'manual confirmation', '5': 'awaiting identity confirmation' } withdraw { "amount":"0.1", "withdrawal_id":"67485", "currency":"btc", "result":true } fetchWithdrawals { amount: "4.72100000", withdrawal_id: "1729116", fee: "0.01000000eth", txid: "0xf653125bbf090bcfe4b5e8e7b8f586a9d87aa7de94598702758c0802b…", currency: "ETH", from: "7147338839", to: "0x26a3CB49578F07000575405a57888681249c35Fd", timestamp: "2018-08-17T07:03:42.000Z", status: "2" } fetchDeposits { "amount": "4.19511659", "txid": "14c9a8c925647cdb7e5b2937ea9aefe2b29b2c273150ad3f44b3b8a4635ed437", "currency": "XMR", "from": "", "to": "48PjH3ksv1fiXniKvKvyH5UtFs5WhfS2Vf7U3TwzdRJtCc7HJWvCQe56dRahyhQyTAViXZ8Nzk4gQg6o4BJBMUoxNy8y8g7", "tag": "1234567", "deposit_id": 11571659, <-- we can use self "timestamp": "2019-10-01T14:54:19.000Z", "status": "2" } the payment_id will appear on new deposits but appears to be removed from the response after 2 months https://github.com/ccxt/ccxt/pull/5748 todo parse tags check that trading symbols match in both entries fee is either a positive number(invitation rebate) or a negative number(transaction fee deduction) therefore we need to invert the fee more about it https://github.com/ccxt/ccxt/issues/5909 simplified structures to show the underlying semantics market/limit sell { "currency":"USDT", "fee":"-0.04647925", ←--- fee in received quote currency "price":"129.13", ←------ price "size":"30.98616393", ←-- cost }, { "currency":"ETH", "fee":"0", "price":"129.13", "size":"0.23996099", ←--- amount }, market/limit buy { "currency":"ETH", "fee":"-0.00036049", ←--- fee in received base currency "price":"129.16", ←------ price "size":"0.240322", ←----- amount }, { "currency":"USDT", "fee":"0", "price":"129.16", "size":"31.03998952", ←-- cost } make sure it has exactly 2 trades, no more, no less okex actually returns ledger entries instead of fills here, so each fill in the order is represented by two trades with opposite buy/sell sides, not one :\ self aspect renders the 'fills' endpoint unusable for fetchOrderTrades until either OKEX fixes the API or we workaround self on our side somehow 'order_id': id, string 'after': '1', pagination of data to return records earlier than the requested ledger_id 'before': '1', P=pagination of data to return records newer than the requested ledger_id 'limit': limit, optional, number of results per request, default = maximum = 100 [ sell { "created_at":"2020-03-29T11:55:25.000Z", "currency":"USDT", "exec_type":"T", "fee":"-0.04647925", "instrument_id":"ETH-USDT", "ledger_id":"10562924353", "liquidity":"T", "order_id":"4636470489136128", "price":"129.13", "product_id":"ETH-USDT", "side":"buy", "size":"30.98616393", "timestamp":"2020-03-29T11:55:25.000Z", "trade_id":"18551601" }, { "created_at":"2020-03-29T11:55:25.000Z", "currency":"ETH", "exec_type":"T", "fee":"0", "instrument_id":"ETH-USDT", "ledger_id":"10562924352", "liquidity":"T", "order_id":"4636470489136128", "price":"129.13", "product_id":"ETH-USDT", "side":"sell", "size":"0.23996099", "timestamp":"2020-03-29T11:55:25.000Z", "trade_id":"18551601" }, buy { "created_at":"2020-03-29T11:55:16.000Z", "currency":"ETH", "exec_type":"T", "fee":"-0.00036049", "instrument_id":"ETH-USDT", "ledger_id":"10562922669", "liquidity":"T", "order_id": "4636469894136832", "price":"129.16", "product_id":"ETH-USDT", "side":"buy", "size":"0.240322", "timestamp":"2020-03-29T11:55:16.000Z", "trade_id":"18551600" }, { "created_at":"2020-03-29T11:55:16.000Z", "currency":"USDT", "exec_type":"T", "fee":"0", "instrument_id":"ETH-USDT", "ledger_id":"10562922668", "liquidity":"T", "order_id":"4636469894136832", "price":"129.16", "product_id":"ETH-USDT", "side":"sell", "size":"31.03998952", "timestamp":"2020-03-29T11:55:16.000Z", "trade_id":"18551600" } ] 'instrument_id': market['id'], 'after': '1', return the page after the specified page number 'before': '1', return the page before the specified page number 'limit': limit, optional, number of results per request, default = maximum = 100 'order_id': id, string 'after': '1', pagination of data to return records earlier than the requested ledger_id 'before': '1', P=pagination of data to return records newer than the requested ledger_id 'limit': limit, optional, number of results per request, default = maximum = 100 futures crossed margin mode { "result": True, "holding": [ { "long_qty": "2", "long_avail_qty": "2", "long_avg_cost": "8260", "long_settlement_price": "8260", "realised_pnl": "0.00020928", "short_qty": "2", "short_avail_qty": "2", "short_avg_cost": "8259.99", "short_settlement_price": "8259.99", "liquidation_price": "113.81", "instrument_id": "BTC-USD-191227", "leverage": "10", "created_at": "2019-09-25T07:58:42.129Z", "updated_at": "2019-10-08T14:02:51.029Z", "margin_mode": "crossed", "short_margin": "0.00242197", "short_pnl": "6.63E-6", "short_pnl_ratio": "0.002477997", "short_unrealised_pnl": "6.63E-6", "long_margin": "0.00242197", "long_pnl": "-6.65E-6", "long_pnl_ratio": "-0.002478", "long_unrealised_pnl": "-6.65E-6", "long_settled_pnl": "0", "short_settled_pnl": "0", "last": "8257.57" } ], "margin_mode": "crossed" } fixed margin mode { "result": True, "holding": [ { "long_qty": "4", "long_avail_qty": "4", "long_margin": "0.00323844", "long_liqui_price": "7762.09", "long_pnl_ratio": "0.06052306", "long_avg_cost": "8234.43", "long_settlement_price": "8234.43", "realised_pnl": "-0.00000296", "short_qty": "2", "short_avail_qty": "2", "short_margin": "0.00241105", "short_liqui_price": "9166.74", "short_pnl_ratio": "0.03318052", "short_avg_cost": "8295.13", "short_settlement_price": "8295.13", "instrument_id": "BTC-USD-191227", "long_leverage": "15", "short_leverage": "10", "created_at": "2019-09-25T07:58:42.129Z", "updated_at": "2019-10-08T13:12:09.438Z", "margin_mode": "fixed", "short_margin_ratio": "0.10292507", "short_maint_margin_ratio": "0.005", "short_pnl": "7.853E-5", "short_unrealised_pnl": "7.853E-5", "long_margin_ratio": "0.07103743", "long_maint_margin_ratio": "0.005", "long_pnl": "1.9841E-4", "long_unrealised_pnl": "1.9841E-4", "long_settled_pnl": "0", "short_settled_pnl": "0", "last": "8266.99" } ], "margin_mode": "fixed" } swap crossed margin mode { "margin_mode": "crossed", "timestamp": "2019-09-27T03:49:02.018Z", "holding": [ { "avail_position": "3", "avg_cost": "59.49", "instrument_id": "LTC-USD-SWAP", "last": "55.98", "leverage": "10.00", "liquidation_price": "4.37", "maint_margin_ratio": "0.0100", "margin": "0.0536", "position": "3", "realized_pnl": "0.0000", "unrealized_pnl": "0", "settled_pnl": "-0.0330", "settlement_price": "55.84", "side": "long", "timestamp": "2019-09-27T03:49:02.018Z" }, ] } fixed margin mode { "margin_mode": "fixed", "timestamp": "2019-09-27T03:47:37.230Z", "holding": [ { "avail_position": "20", "avg_cost": "8025.0", "instrument_id": "BTC-USD-SWAP", "last": "8113.1", "leverage": "15.00", "liquidation_price": "7002.6", "maint_margin_ratio": "0.0050", "margin": "0.0454", "position": "20", "realized_pnl": "-0.0001", "unrealized_pnl": "0", "settled_pnl": "0.0076", "settlement_price": "8279.2", "side": "long", "timestamp": "2019-09-27T03:47:37.230Z" } ] } option { "holding":[ { "instrument_id":"BTC-USD-190927-12500-C", "position":"20", "avg_cost":"3.26", "avail_position":"20", "settlement_price":"0.017", "total_pnl":"50", "pnl_ratio":"0.3", "realized_pnl":"40", "unrealized_pnl":"10", "pos_margin":"100", "option_value":"70", "created_at":"2019-08-30T03:09:20.315Z", "updated_at":"2019-08-30T03:40:18.318Z" }, { "instrument_id":"BTC-USD-190927-12500-P", "position":"20", "avg_cost":"3.26", "avail_position":"20", "settlement_price":"0.019", "total_pnl":"50", "pnl_ratio":"0.3", "realized_pnl":"40", "unrealized_pnl":"10", "pos_margin":"100", "option_value":"70", "created_at":"2019-08-30T03:09:20.315Z", "updated_at":"2019-08-30T03:40:18.318Z" } ] } todo unify parsePosition/parsePositions futures ... swap ... option { "holding":[ { "instrument_id":"BTC-USD-190927-12500-C", "position":"20", "avg_cost":"3.26", "avail_position":"20", "settlement_price":"0.017", "total_pnl":"50", "pnl_ratio":"0.3", "realized_pnl":"40", "unrealized_pnl":"10", "pos_margin":"100", "option_value":"70", "created_at":"2019-08-30T03:09:20.315Z", "updated_at":"2019-08-30T03:40:18.318Z" }, { "instrument_id":"BTC-USD-190927-12500-P", "position":"20", "avg_cost":"3.26", "avail_position":"20", "settlement_price":"0.019", "total_pnl":"50", "pnl_ratio":"0.3", "realized_pnl":"40", "unrealized_pnl":"10", "pos_margin":"100", "option_value":"70", "created_at":"2019-08-30T03:09:20.315Z", "updated_at":"2019-08-30T03:40:18.318Z" } ] } todo unify parsePosition/parsePositions 'from': 'id', 'to': 'id', we intentionally put a market inside here for the margin and swap ledgers if type == 'margin': 3. Borrow 4. Repayment 5. Interest 7. Buy 8. Sell 9. From capital account 10. From C2C 11. From Futures 12. From Spot 13. From ETT 14. To capital account 15. To C2C 16. To Spot 17. To Futures 18. To ETT 19. Mandatory Repayment 20. From Piggybank 21. To Piggybank 22. From Perpetual 23. To Perpetual 24. Liquidation Fee 54. Clawback 59. Airdrop Return. request['type'] = 'number' All types will be returned if self filed is left blank } 1. deposit 2. withdrawal 13. cancel withdrawal 18. into futures account 19. out of futures account 20. into sub account 21. out of sub account 28. claim 29. into ETT account 30. out of ETT account 31. into C2C account 32. out of C2C account 33. into margin account 34. out of margin account 37. into spot account 38. out of spot account request['type'] = 'number' transfer funds transfer in/out trade funds moved as a result of a trade, spot and margin accounts only rebate fee rebate as per fee schedule, spot and margin accounts only match open long/open short/close long/close short(futures) or a change in the amount because of trades(swap) fee fee, futures only settlement settlement/clawback/settle long/settle short liquidation force close long/force close short/deliver close long/deliver close short funding funding fee, swap only margin a change in the amount after adjusting margin, swap only account [ { "amount":0.00051843, "balance":0.00100941, "currency":"BTC", "fee":0, "ledger_id":8987285, "timestamp":"2018-10-12T11:01:14.000Z", "typename":"Get from activity" } ] spot [ { "timestamp":"2019-03-18T07:08:25.000Z", "ledger_id":"3995334780", "created_at":"2019-03-18T07:08:25.000Z", "currency":"BTC", "amount":"0.0009985", "balance":"0.0029955", "type":"trade", "details":{ "instrument_id":"BTC-USDT", "order_id":"2500650881647616", "product_id":"BTC-USDT" } } ] margin [ [ { "created_at":"2019-03-20T03:45:05.000Z", "ledger_id":"78918186", "timestamp":"2019-03-20T03:45:05.000Z", "currency":"EOS", "amount":"0", ? "balance":"0.59957711", "type":"transfer", "details":{ "instrument_id":"EOS-USDT", "order_id":"787057", "product_id":"EOS-USDT" } } ], { "before":"78965766", "after":"78918186" } ] futures [ { "ledger_id":"2508090544914461", "timestamp":"2019-03-19T14:40:24.000Z", "amount":"-0.00529521", "balance":"0", "currency":"EOS", "type":"fee", "details":{ "order_id":"2506982456445952", "instrument_id":"EOS-USD-190628" } } ] swap [ { "amount":"0.004742", "fee":"-0.000551", "type":"match", "instrument_id":"EOS-USD-SWAP", "ledger_id":"197429674941902848", "timestamp":"2019-03-25T05:56:31.286Z" }, ] funds transfer in/out funds moved as a result of a trade, spot and margin accounts only fee rebate as per fee schedule, spot and margin accounts only open long/open short/close long/close short(futures) or a change in the amount because of trades(swap) fee, futures only settlement/clawback/settle long/settle short force close long/force close short/deliver close long/deliver close short funding fee, swap only a change in the amount after adjusting margin, swap only account { "amount":0.00051843, "balance":0.00100941, "currency":"BTC", "fee":0, "ledger_id":8987285, "timestamp":"2018-10-12T11:01:14.000Z", "typename":"Get from activity" } spot { "timestamp":"2019-03-18T07:08:25.000Z", "ledger_id":"3995334780", "created_at":"2019-03-18T07:08:25.000Z", "currency":"BTC", "amount":"0.0009985", "balance":"0.0029955", "type":"trade", "details":{ "instrument_id":"BTC-USDT", "order_id":"2500650881647616", "product_id":"BTC-USDT" } } margin { "created_at":"2019-03-20T03:45:05.000Z", "ledger_id":"78918186", "timestamp":"2019-03-20T03:45:05.000Z", "currency":"EOS", "amount":"0", ? "balance":"0.59957711", "type":"transfer", "details":{ "instrument_id":"EOS-USDT", "order_id":"787057", "product_id":"EOS-USDT" } } futures { "ledger_id":"2508090544914461", "timestamp":"2019-03-19T14:40:24.000Z", "amount":"-0.00529521", "balance":"0", "currency":"EOS", "type":"fee", "details":{ "order_id":"2506982456445952", "instrument_id":"EOS-USD-190628" } } swap { "amount":"0.004742", "fee":"-0.000551", "type":"match", "instrument_id":"EOS-USD-SWAP", "ledger_id":"197429674941902848", "timestamp":"2019-03-25T05:56:31.286Z" }, balance before balance after 'OK-FROM': '', 'OK-TO': '', 'OK-LIMIT': '', https://github.com/ccxt/ccxt/issues/6651 a special case to handle the optionGetUnderlying interefering with other endpoints containing self keyword fallback to default error handler {"message":"name resolution failed"} {"error_message":"Order does not exist","result":"true","error_code":"35029","order_id":"-1"} unknown message
64,210
en
0.615587
from pipeline import * class SentenceLimiter: """ Limit the text, word boundaries and sentence boundaries of a given document to the number of sentences given """ def run(self, document, number_sentences): """ :param: number_sentences, starts with 0 for the fist sentence """ boundaries = (document.sentences_boundaries[0][0], document.sentences_boundaries[:number_sentences+1][-1][1]) document.text = document.text[boundaries[0]:boundaries[1]] document.sentences_boundaries = self._limitSenteceBoundaries(document.sentences_boundaries, boundaries[1]) document.words_boundaries = self._limitWordBoundaries(document.words_boundaries, boundaries[1]) document.entities = self._limitEntities(document.entities, boundaries[1]) document.triples = self._limitTriples(document.triples, boundaries[1]) return document def _limitSenteceBoundaries(self, sentences_boundaries, maxi): sentences_boundaries_new = [] for sent in sentences_boundaries: if sent[1] <= maxi: sentences_boundaries_new.append(sent) return sentences_boundaries_new def _limitEntities(self, entities, maxi): entities_new = [] for e in entities: if e.boundaries[1] <= maxi: entities_new.append(e) return entities_new def _limitTriples(self, triples, maxi): triples_new = [] for t in triples: if t.sentence_id == 0: triples_new.append(t) return triples_new def _limitWordBoundaries(self, words_boundaries, maxi): words_boundaries_new = [] for word in words_boundaries: if word[1] <= maxi: words_boundaries_new.append(word) return words_boundaries_new class MainEntityLimiter: """ Remove a document's content if the main entity is not aligned """ def run(self, document): if not document.uri in [i.uri for i in document.entities]: document = None return document class EntityTypeFilter: """ Remove all documents that are of a certain type """ def __init__(self, all_triples, entities): """ :param: input TripleReaderTriples object :param: a list of entity that should be filtered """ self.wikidata_triples = all_triples self.entities = entities def run(self, document): # P31: instance of prop_id = 'http://www.wikidata.org/prop/direct/P31' if any([i for i in self.wikidata_triples.get(document.docid) if i[1] == prop_id and i[2] in self.entities]): document = None return document
pipeline/filter.py
2,736
Remove all documents that are of a certain type Remove a document's content if the main entity is not aligned Limit the text, word boundaries and sentence boundaries of a given document to the number of sentences given :param: input TripleReaderTriples object :param: a list of entity that should be filtered :param: number_sentences, starts with 0 for the fist sentence P31: instance of
390
en
0.765705
# Copyright 2021 Red Hat, Inc. # # 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. import json from jinja2 import Template from oslo_concurrency import processutils from oslo_log import log as logging from networking_bgp_ovn import constants LOG = logging.getLogger(__name__) ADD_VRF_TEMPLATE = ''' vrf {{ vrf_name }} vni {{ vni }} exit-vrf router bgp {{ bgp_as }} vrf {{ vrf_name }} address-family ipv4 unicast redistribute connected exit-address-family address-family ipv6 unicast redistribute connected exit-address-family address-family l2vpn evpn advertise ipv4 unicast advertise ipv6 unicast exit-address-family ''' DEL_VRF_TEMPLATE = ''' no vrf {{ vrf_name }} no router bgp {{ bgp_as }} vrf {{ vrf_name }} ''' LEAK_VRF_TEMPLATE = ''' router bgp {{ bgp_as }} address-family ipv4 unicast import vrf {{ vrf_name }} exit-address-family address-family ipv6 unicast import vrf {{ vrf_name }} exit-address-family router bgp {{ bgp_as }} vrf {{ vrf_name }} bgp router-id {{ bgp_router_id }} address-family ipv4 unicast redistribute connected exit-address-family address-family ipv6 unicast redistribute connected exit-address-family ''' def _run_vtysh_config(frr_config_file): vtysh_command = "copy {} running-config".format(frr_config_file) full_args = ['/usr/bin/vtysh', '--vty_socket', constants.FRR_SOCKET_PATH, '-c', vtysh_command] try: return processutils.execute(*full_args, run_as_root=True) except Exception as e: print("Unable to execute vtysh with {}. Exception: {}".format( full_args, e)) raise def _run_vtysh_command(command): full_args = ['/usr/bin/vtysh', '--vty_socket', constants.FRR_SOCKET_PATH, '-c', command] try: return processutils.execute(*full_args, run_as_root=True)[0] except Exception as e: print("Unable to execute vtysh with {}. Exception: {}".format( full_args, e)) raise def _get_router_id(bgp_as): output = _run_vtysh_command(command='show ip bgp summary json') return json.loads(output).get('ipv4Unicast', {}).get('routerId') def vrf_leak(vrf, bgp_as, bgp_router_id=None): LOG.info("Add VRF leak for VRF {} on router bgp {}".format(vrf, bgp_as)) if not bgp_router_id: bgp_router_id = _get_router_id(bgp_as) if not bgp_router_id: LOG.error("Unknown router-id, needed for route leaking") return vrf_template = Template(LEAK_VRF_TEMPLATE) vrf_config = vrf_template.render(vrf_name=vrf, bgp_as=bgp_as, bgp_router_id=bgp_router_id) frr_config_file = "frr-config-vrf-leak-{}".format(vrf) with open(frr_config_file, 'w') as vrf_config_file: vrf_config_file.write(vrf_config) _run_vtysh_config(frr_config_file) def vrf_reconfigure(evpn_info, action): LOG.info("FRR reconfiguration (action = {}) for evpn: {}".format( action, evpn_info)) frr_config_file = None if action == "add-vrf": vrf_template = Template(ADD_VRF_TEMPLATE) vrf_config = vrf_template.render( vrf_name="{}{}".format(constants.OVN_EVPN_VRF_PREFIX, evpn_info['vni']), bgp_as=evpn_info['bgp_as'], vni=evpn_info['vni']) frr_config_file = "frr-config-add-vrf-{}".format(evpn_info['vni']) elif action == "del-vrf": vrf_template = Template(DEL_VRF_TEMPLATE) vrf_config = vrf_template.render( vrf_name="{}{}".format(constants.OVN_EVPN_VRF_PREFIX, evpn_info['vni']), bgp_as=evpn_info['bgp_as']) frr_config_file = "frr-config-del-vrf-{}".format(evpn_info['vni']) else: LOG.error("Unknown FRR reconfiguration action: %s", action) return with open(frr_config_file, 'w') as vrf_config_file: vrf_config_file.write(vrf_config) _run_vtysh_config(frr_config_file)
networking_bgp_ovn/drivers/openstack/utils/frr.py
4,519
Copyright 2021 Red Hat, Inc. 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.
548
en
0.858906
# -*- coding: utf-8 -*- # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you 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 airflow import DAG from airflow.contrib.operators.jenkins_job_trigger_operator import JenkinsJobTriggerOperator from airflow.operators.python_operator import PythonOperator from airflow.contrib.hooks.jenkins_hook import JenkinsHook from six.moves.urllib.request import Request import jenkins from datetime import datetime from datetime import timedelta datetime_start_date = datetime(2018, 5, 3) default_args = { "owner": "airflow", "start_date": datetime_start_date, "retries": 1, "retry_delay": timedelta(minutes=5), "depends_on_past": False, "concurrency": 8, "max_active_runs": 8 } dag = DAG("test_jenkins", default_args=default_args, schedule_interval=None) #This DAG shouldn't be executed and is only here to provide example of how to use the JenkinsJobTriggerOperator #(it requires a jenkins server to be executed) job_trigger = JenkinsJobTriggerOperator( dag=dag, task_id="trigger_job", job_name="red-beta-build-deploy", parameters={"BRANCH":"origin/master", "USER_ENV":"shameer"}, #parameters="resources/paremeter.json", You can also pass a path to a json file containing your param jenkins_connection_id="jenkins_nqa" #The connection must be configured first ) def grabArtifactFromJenkins(**context): """ Grab an artifact from the previous job The python-jenkins library doesn't expose a method for that But it's totally possible to build manually the request for that """ hook = JenkinsHook("jenkins_nqa") jenkins_server = hook.get_jenkins_server() url = context['task_instance'].xcom_pull(task_ids='trigger_job') #The JenkinsJobTriggerOperator store the job url in the xcom variable corresponding to the task #You can then use it to access things or to get the job number #This url looks like : http://jenkins_url/job/job_name/job_number/ url = url + "artifact/myartifact.xml" #Or any other artifact name self.log.info("url : %s", url) request = Request(url) response = jenkins_server.jenkins_open(request) self.log.info("response: %s", response) return response #We store the artifact content in a xcom variable for later use artifact_grabber = PythonOperator( task_id='artifact_grabber', provide_context=True, python_callable=grabArtifactFromJenkins, dag=dag) artifact_grabber.set_upstream(job_trigger)
dags/jenkins_dag.py
3,188
Grab an artifact from the previous job The python-jenkins library doesn't expose a method for that But it's totally possible to build manually the request for that -*- coding: utf-8 -*- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.This DAG shouldn't be executed and is only here to provide example of how to use the JenkinsJobTriggerOperator(it requires a jenkins server to be executed)parameters="resources/paremeter.json", You can also pass a path to a json file containing your paramThe connection must be configured firstThe JenkinsJobTriggerOperator store the job url in the xcom variable corresponding to the taskYou can then use it to access things or to get the job numberThis url looks like : http://jenkins_url/job/job_name/job_number/Or any other artifact nameWe store the artifact content in a xcom variable for later use
1,544
en
0.877352
# coding: utf-8 """ FlashBlade REST API A lightweight client for FlashBlade REST API 2.3, developed by Pure Storage, Inc. (http://www.purestorage.com/). OpenAPI spec version: 2.3 Generated by: https://github.com/swagger-api/swagger-codegen.git """ from __future__ import absolute_import import re # python 2 and python 3 compatibility library import six from typing import List, Optional from .. import models class LinkAggregationGroupsApi(object): def __init__(self, api_client): self.api_client = api_client def api23_link_aggregation_groups_delete_with_http_info( self, ids=None, # type: List[str] names=None, # type: List[str] async_req=False, # type: bool _return_http_data_only=False, # type: bool _preload_content=True, # type: bool _request_timeout=None, # type: Optional[int] ): # type: (...) -> None """DELETE link-aggregation-groups Remove a link aggregation group to unbind the ports. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.api23_link_aggregation_groups_delete_with_http_info(async_req=True) >>> result = thread.get() :param list[str] ids: A comma-separated list of resource IDs. If after filtering, there is not at least one resource that matches each of the elements of `ids`, then an error is returned. This cannot be provided together with the `name` or `names` query parameters. :param list[str] names: A comma-separated list of resource names. If there is not at least one resource that matches each of the elements of `names`, then an error is returned. :param bool async_req: Request runs in separate thread and method returns multiprocessing.pool.ApplyResult. :param bool _return_http_data_only: Returns only data field. :param bool _preload_content: Response is converted into objects. :param int _request_timeout: Total request timeout in seconds. It can also be a tuple of (connection time, read time) timeouts. :return: None If the method is called asynchronously, returns the request thread. """ if ids is not None: if not isinstance(ids, list): ids = [ids] if names is not None: if not isinstance(names, list): names = [names] params = {k: v for k, v in six.iteritems(locals()) if v is not None} # Convert the filter into a string if params.get('filter'): params['filter'] = str(params['filter']) if params.get('sort'): params['sort'] = [str(_x) for _x in params['sort']] collection_formats = {} path_params = {} query_params = [] if 'ids' in params: query_params.append(('ids', params['ids'])) collection_formats['ids'] = 'csv' if 'names' in params: query_params.append(('names', params['names'])) collection_formats['names'] = 'csv' header_params = {} form_params = [] local_var_files = {} body_params = None # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept( ['application/json']) # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type( ['application/json']) # Authentication setting auth_settings = ['AuthorizationHeader'] return self.api_client.call_api( '/api/2.3/link-aggregation-groups', 'DELETE', path_params, query_params, header_params, body=body_params, post_params=form_params, files=local_var_files, response_type=None, auth_settings=auth_settings, async_req=async_req, _return_http_data_only=_return_http_data_only, _preload_content=_preload_content, _request_timeout=_request_timeout, collection_formats=collection_formats, ) def api23_link_aggregation_groups_get_with_http_info( self, continuation_token=None, # type: str filter=None, # type: str ids=None, # type: List[str] limit=None, # type: int names=None, # type: List[str] offset=None, # type: int sort=None, # type: List[str] async_req=False, # type: bool _return_http_data_only=False, # type: bool _preload_content=True, # type: bool _request_timeout=None, # type: Optional[int] ): # type: (...) -> models.LinkAggregationGroupGetResponse """GET link-aggregation-groups List the status and attributes of the Ethernet ports in the configured link aggregation groups. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.api23_link_aggregation_groups_get_with_http_info(async_req=True) >>> result = thread.get() :param str continuation_token: An opaque token used to iterate over a collection. The token to use on the next request is returned in the `continuation_token` field of the result. :param str filter: Exclude resources that don't match the specified criteria. :param list[str] ids: A comma-separated list of resource IDs. If after filtering, there is not at least one resource that matches each of the elements of `ids`, then an error is returned. This cannot be provided together with the `name` or `names` query parameters. :param int limit: Limit the size of the response to the specified number of resources. A `limit` of `0` can be used to get the number of resources without getting all of the resources. It will be returned in the `total_item_count` field. If a client asks for a page size larger than the maximum number, the request is still valid. In that case the server just returns the maximum number of items, disregarding the client's page size request. :param list[str] names: A comma-separated list of resource names. If there is not at least one resource that matches each of the elements of `names`, then an error is returned. :param int offset: The offset of the first resource to return from a collection. :param list[str] sort: Sort the response by the specified fields (in descending order if '-' is appended to the field name). NOTE: If you provide a sort you will not get a `continuation_token` in the response. :param bool async_req: Request runs in separate thread and method returns multiprocessing.pool.ApplyResult. :param bool _return_http_data_only: Returns only data field. :param bool _preload_content: Response is converted into objects. :param int _request_timeout: Total request timeout in seconds. It can also be a tuple of (connection time, read time) timeouts. :return: LinkAggregationGroupGetResponse If the method is called asynchronously, returns the request thread. """ if ids is not None: if not isinstance(ids, list): ids = [ids] if names is not None: if not isinstance(names, list): names = [names] if sort is not None: if not isinstance(sort, list): sort = [sort] params = {k: v for k, v in six.iteritems(locals()) if v is not None} # Convert the filter into a string if params.get('filter'): params['filter'] = str(params['filter']) if params.get('sort'): params['sort'] = [str(_x) for _x in params['sort']] if 'limit' in params and params['limit'] < 1: raise ValueError("Invalid value for parameter `limit` when calling `api23_link_aggregation_groups_get`, must be a value greater than or equal to `1`") if 'offset' in params and params['offset'] < 0: raise ValueError("Invalid value for parameter `offset` when calling `api23_link_aggregation_groups_get`, must be a value greater than or equal to `0`") collection_formats = {} path_params = {} query_params = [] if 'continuation_token' in params: query_params.append(('continuation_token', params['continuation_token'])) if 'filter' in params: query_params.append(('filter', params['filter'])) if 'ids' in params: query_params.append(('ids', params['ids'])) collection_formats['ids'] = 'csv' if 'limit' in params: query_params.append(('limit', params['limit'])) if 'names' in params: query_params.append(('names', params['names'])) collection_formats['names'] = 'csv' if 'offset' in params: query_params.append(('offset', params['offset'])) if 'sort' in params: query_params.append(('sort', params['sort'])) collection_formats['sort'] = 'csv' header_params = {} form_params = [] local_var_files = {} body_params = None # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept( ['application/json']) # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type( ['application/json']) # Authentication setting auth_settings = ['AuthorizationHeader'] return self.api_client.call_api( '/api/2.3/link-aggregation-groups', 'GET', path_params, query_params, header_params, body=body_params, post_params=form_params, files=local_var_files, response_type='LinkAggregationGroupGetResponse', auth_settings=auth_settings, async_req=async_req, _return_http_data_only=_return_http_data_only, _preload_content=_preload_content, _request_timeout=_request_timeout, collection_formats=collection_formats, ) def api23_link_aggregation_groups_patch_with_http_info( self, link_aggregation_group=None, # type: models.Linkaggregationgroup ids=None, # type: List[str] names=None, # type: List[str] async_req=False, # type: bool _return_http_data_only=False, # type: bool _preload_content=True, # type: bool _request_timeout=None, # type: Optional[int] ): # type: (...) -> models.LinkAggregationGroupResponse """PATCH link-aggregation-groups Modify link aggregation groups by adding and removing Ethernet ports. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.api23_link_aggregation_groups_patch_with_http_info(link_aggregation_group, async_req=True) >>> result = thread.get() :param Linkaggregationgroup link_aggregation_group: (required) :param list[str] ids: A comma-separated list of resource IDs. If after filtering, there is not at least one resource that matches each of the elements of `ids`, then an error is returned. This cannot be provided together with the `name` or `names` query parameters. :param list[str] names: A comma-separated list of resource names. If there is not at least one resource that matches each of the elements of `names`, then an error is returned. :param bool async_req: Request runs in separate thread and method returns multiprocessing.pool.ApplyResult. :param bool _return_http_data_only: Returns only data field. :param bool _preload_content: Response is converted into objects. :param int _request_timeout: Total request timeout in seconds. It can also be a tuple of (connection time, read time) timeouts. :return: LinkAggregationGroupResponse If the method is called asynchronously, returns the request thread. """ if ids is not None: if not isinstance(ids, list): ids = [ids] if names is not None: if not isinstance(names, list): names = [names] params = {k: v for k, v in six.iteritems(locals()) if v is not None} # Convert the filter into a string if params.get('filter'): params['filter'] = str(params['filter']) if params.get('sort'): params['sort'] = [str(_x) for _x in params['sort']] # verify the required parameter 'link_aggregation_group' is set if link_aggregation_group is None: raise TypeError("Missing the required parameter `link_aggregation_group` when calling `api23_link_aggregation_groups_patch`") collection_formats = {} path_params = {} query_params = [] if 'ids' in params: query_params.append(('ids', params['ids'])) collection_formats['ids'] = 'csv' if 'names' in params: query_params.append(('names', params['names'])) collection_formats['names'] = 'csv' header_params = {} form_params = [] local_var_files = {} body_params = None if 'link_aggregation_group' in params: body_params = params['link_aggregation_group'] # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept( ['application/json']) # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type( ['application/json']) # Authentication setting auth_settings = ['AuthorizationHeader'] return self.api_client.call_api( '/api/2.3/link-aggregation-groups', 'PATCH', path_params, query_params, header_params, body=body_params, post_params=form_params, files=local_var_files, response_type='LinkAggregationGroupResponse', auth_settings=auth_settings, async_req=async_req, _return_http_data_only=_return_http_data_only, _preload_content=_preload_content, _request_timeout=_request_timeout, collection_formats=collection_formats, ) def api23_link_aggregation_groups_post_with_http_info( self, link_aggregation_group=None, # type: models.LinkAggregationGroup names=None, # type: List[str] async_req=False, # type: bool _return_http_data_only=False, # type: bool _preload_content=True, # type: bool _request_timeout=None, # type: Optional[int] ): # type: (...) -> models.LinkAggregationGroupResponse """POST link-aggregation-groups Create a link aggregation group of Ethernet ports on the array. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.api23_link_aggregation_groups_post_with_http_info(link_aggregation_group, names, async_req=True) >>> result = thread.get() :param LinkAggregationGroup link_aggregation_group: (required) :param list[str] names: A comma-separated list of resource names. (required) :param bool async_req: Request runs in separate thread and method returns multiprocessing.pool.ApplyResult. :param bool _return_http_data_only: Returns only data field. :param bool _preload_content: Response is converted into objects. :param int _request_timeout: Total request timeout in seconds. It can also be a tuple of (connection time, read time) timeouts. :return: LinkAggregationGroupResponse If the method is called asynchronously, returns the request thread. """ if names is not None: if not isinstance(names, list): names = [names] params = {k: v for k, v in six.iteritems(locals()) if v is not None} # Convert the filter into a string if params.get('filter'): params['filter'] = str(params['filter']) if params.get('sort'): params['sort'] = [str(_x) for _x in params['sort']] # verify the required parameter 'link_aggregation_group' is set if link_aggregation_group is None: raise TypeError("Missing the required parameter `link_aggregation_group` when calling `api23_link_aggregation_groups_post`") # verify the required parameter 'names' is set if names is None: raise TypeError("Missing the required parameter `names` when calling `api23_link_aggregation_groups_post`") collection_formats = {} path_params = {} query_params = [] if 'names' in params: query_params.append(('names', params['names'])) collection_formats['names'] = 'csv' header_params = {} form_params = [] local_var_files = {} body_params = None if 'link_aggregation_group' in params: body_params = params['link_aggregation_group'] # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept( ['application/json']) # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type( ['application/json']) # Authentication setting auth_settings = ['AuthorizationHeader'] return self.api_client.call_api( '/api/2.3/link-aggregation-groups', 'POST', path_params, query_params, header_params, body=body_params, post_params=form_params, files=local_var_files, response_type='LinkAggregationGroupResponse', auth_settings=auth_settings, async_req=async_req, _return_http_data_only=_return_http_data_only, _preload_content=_preload_content, _request_timeout=_request_timeout, collection_formats=collection_formats, )
pypureclient/flashblade/FB_2_3/api/link_aggregation_groups_api.py
18,575
DELETE link-aggregation-groups Remove a link aggregation group to unbind the ports. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.api23_link_aggregation_groups_delete_with_http_info(async_req=True) >>> result = thread.get() :param list[str] ids: A comma-separated list of resource IDs. If after filtering, there is not at least one resource that matches each of the elements of `ids`, then an error is returned. This cannot be provided together with the `name` or `names` query parameters. :param list[str] names: A comma-separated list of resource names. If there is not at least one resource that matches each of the elements of `names`, then an error is returned. :param bool async_req: Request runs in separate thread and method returns multiprocessing.pool.ApplyResult. :param bool _return_http_data_only: Returns only data field. :param bool _preload_content: Response is converted into objects. :param int _request_timeout: Total request timeout in seconds. It can also be a tuple of (connection time, read time) timeouts. :return: None If the method is called asynchronously, returns the request thread. GET link-aggregation-groups List the status and attributes of the Ethernet ports in the configured link aggregation groups. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.api23_link_aggregation_groups_get_with_http_info(async_req=True) >>> result = thread.get() :param str continuation_token: An opaque token used to iterate over a collection. The token to use on the next request is returned in the `continuation_token` field of the result. :param str filter: Exclude resources that don't match the specified criteria. :param list[str] ids: A comma-separated list of resource IDs. If after filtering, there is not at least one resource that matches each of the elements of `ids`, then an error is returned. This cannot be provided together with the `name` or `names` query parameters. :param int limit: Limit the size of the response to the specified number of resources. A `limit` of `0` can be used to get the number of resources without getting all of the resources. It will be returned in the `total_item_count` field. If a client asks for a page size larger than the maximum number, the request is still valid. In that case the server just returns the maximum number of items, disregarding the client's page size request. :param list[str] names: A comma-separated list of resource names. If there is not at least one resource that matches each of the elements of `names`, then an error is returned. :param int offset: The offset of the first resource to return from a collection. :param list[str] sort: Sort the response by the specified fields (in descending order if '-' is appended to the field name). NOTE: If you provide a sort you will not get a `continuation_token` in the response. :param bool async_req: Request runs in separate thread and method returns multiprocessing.pool.ApplyResult. :param bool _return_http_data_only: Returns only data field. :param bool _preload_content: Response is converted into objects. :param int _request_timeout: Total request timeout in seconds. It can also be a tuple of (connection time, read time) timeouts. :return: LinkAggregationGroupGetResponse If the method is called asynchronously, returns the request thread. PATCH link-aggregation-groups Modify link aggregation groups by adding and removing Ethernet ports. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.api23_link_aggregation_groups_patch_with_http_info(link_aggregation_group, async_req=True) >>> result = thread.get() :param Linkaggregationgroup link_aggregation_group: (required) :param list[str] ids: A comma-separated list of resource IDs. If after filtering, there is not at least one resource that matches each of the elements of `ids`, then an error is returned. This cannot be provided together with the `name` or `names` query parameters. :param list[str] names: A comma-separated list of resource names. If there is not at least one resource that matches each of the elements of `names`, then an error is returned. :param bool async_req: Request runs in separate thread and method returns multiprocessing.pool.ApplyResult. :param bool _return_http_data_only: Returns only data field. :param bool _preload_content: Response is converted into objects. :param int _request_timeout: Total request timeout in seconds. It can also be a tuple of (connection time, read time) timeouts. :return: LinkAggregationGroupResponse If the method is called asynchronously, returns the request thread. POST link-aggregation-groups Create a link aggregation group of Ethernet ports on the array. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.api23_link_aggregation_groups_post_with_http_info(link_aggregation_group, names, async_req=True) >>> result = thread.get() :param LinkAggregationGroup link_aggregation_group: (required) :param list[str] names: A comma-separated list of resource names. (required) :param bool async_req: Request runs in separate thread and method returns multiprocessing.pool.ApplyResult. :param bool _return_http_data_only: Returns only data field. :param bool _preload_content: Response is converted into objects. :param int _request_timeout: Total request timeout in seconds. It can also be a tuple of (connection time, read time) timeouts. :return: LinkAggregationGroupResponse If the method is called asynchronously, returns the request thread. FlashBlade REST API A lightweight client for FlashBlade REST API 2.3, developed by Pure Storage, Inc. (http://www.purestorage.com/). OpenAPI spec version: 2.3 Generated by: https://github.com/swagger-api/swagger-codegen.git coding: utf-8 python 2 and python 3 compatibility library type: List[str] type: List[str] type: bool type: bool type: bool type: Optional[int] type: (...) -> None Convert the filter into a string HTTP header `Accept` HTTP header `Content-Type` Authentication setting type: str type: str type: List[str] type: int type: List[str] type: int type: List[str] type: bool type: bool type: bool type: Optional[int] type: (...) -> models.LinkAggregationGroupGetResponse Convert the filter into a string HTTP header `Accept` HTTP header `Content-Type` Authentication setting type: models.Linkaggregationgroup type: List[str] type: List[str] type: bool type: bool type: bool type: Optional[int] type: (...) -> models.LinkAggregationGroupResponse Convert the filter into a string verify the required parameter 'link_aggregation_group' is set HTTP header `Accept` HTTP header `Content-Type` Authentication setting type: models.LinkAggregationGroup type: List[str] type: bool type: bool type: bool type: Optional[int] type: (...) -> models.LinkAggregationGroupResponse Convert the filter into a string verify the required parameter 'link_aggregation_group' is set verify the required parameter 'names' is set HTTP header `Accept` HTTP header `Content-Type` Authentication setting
7,371
en
0.715546
# model settings model = dict( type='CenterNet', pretrained='./pretrain/darknet53.pth', backbone=dict( type='DarknetV3', layers=[1, 2, 8, 8, 4], inplanes=[3, 32, 64, 128, 256, 512], planes=[32, 64, 128, 256, 512, 1024], norm_cfg=dict(type='BN'), out_indices=(1, 2, 3, 4), frozen_stages=1, norm_eval=False), neck=dict(type='None'), bbox_head=dict( type='CXTHead', inplanes=(128, 256, 512, 1024), head_conv=128, wh_conv=64, use_deconv=False, norm_after_upsample=False, hm_head_conv_num=2, wh_head_conv_num=2, ct_head_conv_num=1, fovea_hm=False, num_classes=81, use_exp_wh=False, wh_offset_base=16, wh_agnostic=True, wh_heatmap=True, shortcut_cfg=(1, 2, 3), shortcut_attention=(False, False, False), norm_cfg=dict(type='BN'), norm_wh=False, hm_center_ratio=0.27, hm_init_value=None, giou_weight=5., merge_weight=1., hm_weight=1., ct_weight=1.)) cudnn_benchmark = True # training and testing settings train_cfg = dict( vis_every_n_iters=100, debug=False) test_cfg = dict( score_thr=0.01, max_per_img=100) # dataset settings dataset_type = 'CocoDataset' data_root = 'data/coco/' img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) data = dict( imgs_per_gpu=12, workers_per_gpu=4, train=dict( type=dataset_type, ann_file=data_root + 'annotations/instances_train2017.json', img_prefix=data_root + 'train2017/', pipeline=train_pipeline), val=dict( type=dataset_type, ann_file=data_root + 'annotations/instances_val2017.json', img_prefix=data_root + 'val2017/', pipeline=test_pipeline), test=dict( type=dataset_type, ann_file=data_root + 'annotations/instances_val2017.json', img_prefix=data_root + 'val2017/', pipeline=test_pipeline)) # optimizer optimizer = dict(type='SGD', lr=0.003, momentum=0.9, weight_decay=0.0004, paramwise_options=dict(bias_lr_mult=2., bias_decay_mult=0.)) optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2)) # learning policy lr_config = dict( policy='step', warmup='linear', warmup_iters=500, warmup_ratio=1.0 / 5, step=[9, 11]) checkpoint_config = dict(save_every_n_steps=200, max_to_keep=1, keep_every_n_epochs=9) bbox_head_hist_config = dict( model_type=['ConvModule', 'DeformConvPack'], sub_modules=['bbox_head'], save_every_n_steps=200) # yapf:disable log_config = dict(interval=20) # yapf:enable # runtime settings total_epochs = 12 dist_params = dict(backend='nccl') log_level = 'INFO' work_dir = 'ttf53_whh_3lr_1x' load_from = None resume_from = None workflow = [('train', 1)]
configs/eftnet/R2_ttf53_whh_3lr_1x.py
2,930
model settings training and testing settings dataset settings optimizer learning policy yapf:disable yapf:enable runtime settings
129
en
0.778863
## @package onnx #Module caffe2.python.onnx.onnxifi """ ONNXIFI a Caffe2 net """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from caffe2.proto import caffe2_pb2 from caffe2.python import core, workspace import caffe2.python._import_c_extension as C import numpy as np def onnxifi_caffe2_net( pred_net, input_shapes, max_batch_size=1, max_seq_size=1, debug=False, use_onnx=True, merge_fp32_inputs_into_fp16=False, adjust_batch=True, black_list=None, weight_names=None): """ Transform the caffe2_net by collapsing ONNXIFI-runnable nodes into Onnxifi c2 ops """ shape_hints = {} for k, v in input_shapes.items(): shape_hints[k] = v pred_net_str = C.onnxifi(pred_net.SerializeToString(), shape_hints, black_list if black_list else [], weight_names if weight_names is not None else [], max_batch_size, max_seq_size, adjust_batch, debug, merge_fp32_inputs_into_fp16, use_onnx) pred_net_cut = caffe2_pb2.NetDef() pred_net_cut.ParseFromString(pred_net_str) return pred_net_cut
detectron/lib/python3.6/site-packages/caffe2/python/onnx/onnxifi.py
1,464
Transform the caffe2_net by collapsing ONNXIFI-runnable nodes into Onnxifi c2 ops ONNXIFI a Caffe2 net @package onnxModule caffe2.python.onnx.onnxifi
151
en
0.285494
#!/usr/bin/env python """TcEx Framework Validate Module.""" # standard library import ast import importlib import json import os import sys import traceback from collections import deque from pathlib import Path from typing import Dict, Union # third-party import colorama as c # from jsonschema import SchemaError, ValidationError, validate from pydantic import ValidationError from stdlib_list import stdlib_list # first-party from tcex.app_config.install_json import InstallJson from tcex.app_config.job_json import JobJson from tcex.app_config.layout_json import LayoutJson from tcex.app_config.tcex_json import TcexJson from tcex.bin.bin_abc import BinABC try: # standard library import sqlite3 except ModuleNotFoundError: # this module is only required for certain CLI commands pass class Validate(BinABC): """Validate syntax, imports, and schemas. * Python and JSON file syntax * Python import modules * install.json schema * layout.json schema """ def __init__(self, ignore_validation: bool) -> None: """Initialize Class properties.""" super().__init__() self.ignore_validation = ignore_validation # class properties self._app_packages = [] self._install_json_schema = None self._layout_json_schema = None self.config = {} self.ij = InstallJson() self.invalid_json_files = [] self.lj = LayoutJson() self.tj = TcexJson() # initialize validation data self.validation_data = self._validation_data @property def _validation_data(self) -> Dict[str, list]: """Return structure for validation data.""" return { 'errors': [], 'fileSyntax': [], 'layouts': [], 'moduleImports': [], 'schema': [], 'feeds': [], } def _check_node_import(self, node: Union[ast.Import, ast.ImportFrom], filename: str) -> None: """.""" if isinstance(node, ast.Import): for n in node.names: m = n.name.split('.')[0] if not self.check_import_stdlib(m): m_status = self.check_imported(m) if not m_status: self.validation_data['errors'].append( f'Module validation failed for {filename} ' f'module "{m}" could not be imported).' ) self.validation_data['moduleImports'].append( {'filename': filename, 'module': m, 'status': m_status} ) elif isinstance(node, ast.ImportFrom): m = node.module.split('.')[0] if not self.check_import_stdlib(m): m_status = self.check_imported(m) if not m_status: self.validation_data['errors'].append( f'Module validation failed for {filename} ' f'module "{m}" could not be imported).' ) self.validation_data['moduleImports'].append( {'filename': filename, 'module': m, 'status': m_status} ) def check_imports(self) -> None: """Check the projects top level directory for missing imports. This method will check only files ending in **.py** and does not handle imports validation for sub-directories. """ for filename in sorted(os.listdir(self.app_path)): if not filename.endswith('.py'): continue fq_path = os.path.join(self.app_path, filename) with open(fq_path, 'rb') as f: # TODO: [low] is there a better way? code_lines = deque([(f.read(), 1)]) while code_lines: code, _ = code_lines.popleft() # pylint: disable=unused-variable try: parsed_code = ast.parse(code) for node in ast.walk(parsed_code): self._check_node_import(node, filename) except SyntaxError: pass @staticmethod def check_import_stdlib(module: str) -> bool: """Check if module is in Python stdlib. Args: module: The name of the module to check. Returns: bool: Returns True if the module is in the stdlib or template. """ if ( module in stdlib_list('3.6') or module in stdlib_list('3.7') or module in stdlib_list('3.8') or module in ['app', 'args', 'base_app_input', 'job_app', 'playbook_app', 'run', 'service_app'] ): return True return False @staticmethod def check_imported(module: str) -> bool: """Check whether the provide module can be imported (package installed). Args: module: The name of the module to check availability. Returns: bool: True if the module can be imported, False otherwise. """ try: del sys.modules[module] except (AttributeError, KeyError): pass # https://docs.python.org/3/library/importlib.html#checking-if-a-module-can-be-imported find_spec = importlib.util.find_spec(module) found = find_spec is not None if found is True: # if dist-packages|site-packages in module_path the import doesn't count try: if 'dist-packages' in find_spec.origin: found = False except TypeError: pass try: if 'site-packages' in find_spec.origin: found = False except TypeError: pass return found def check_install_json(self) -> None: """Check all install.json files for valid schema.""" if 'install.json' in self.invalid_json_files: return status = True try: self.ij.model except ValidationError as ex: self.invalid_json_files.append(self.ij.fqfn.name) status = False for error in json.loads(ex.json()): location = [str(location) for location in error.get('loc')] self.validation_data['errors'].append( '''Schema validation failed for install.json. ''' f'''{error.get('msg')}: {' -> '.join(location)}''' ) except ValueError: # any JSON decode error will be caught during syntax validation return self.validation_data['schema'].append({'filename': self.ij.fqfn.name, 'status': status}) def check_job_json(self) -> None: """Validate feed files for feed job apps.""" if 'install.json' in self.invalid_json_files: # can't proceed if install.json can't be read return # use developer defined app version (deprecated) or package_version from InstallJson model app_version = self.tj.model.package.app_version or self.ij.model.package_version program_name = (f'''{self.tj.model.package.app_name}_{app_version}''').replace('_', ' ') status = True for feed in self.ij.model.feeds: if feed.job_file in self.invalid_json_files: # no need to check if schema if json is invalid continue jj = JobJson(filename=feed.job_file) # validate the job file exists if not jj.fqfn.is_file(): self.validation_data['errors'].append( f'''Schema validation failed for {feed.job_file}. ''' f'''The job.json file could not be found.''' ) continue try: # validate the schema jj.model except ValidationError as ex: status = False for error in json.loads(ex.json()): location = [str(location) for location in error.get('loc')] self.validation_data['errors'].append( f'''Schema validation failed for {feed.job_file}. ''' f'''{error.get('msg')}: {' -> '.join(location)}''' ) # validate program name if status is True and jj.model.program_name != program_name: status = False self.validation_data['errors'].append( f'''Schema validation failed for {feed.job_file}. ''' f'''The job.json programName {jj.model.program_name} != {program_name}.''' ) # validate program version if status is True and jj.model.program_version != self.ij.model.program_version: status = False self.validation_data['errors'].append( f'''Schema validation failed for {feed.job_file}. The job.json program''' f'''Version {jj.model.program_version} != {self.ij.model.program_version}.''' ) self.validation_data['schema'].append({'filename': feed.job_file, 'status': status}) def check_layout_json(self) -> None: """Check all layout.json files for valid schema.""" if not self.lj.has_layout or 'layout.json' in self.invalid_json_files: return status = True try: self.lj.model except ValidationError as ex: self.invalid_json_files.append(self.ij.fqfn.name) status = False for error in json.loads(ex.json()): location = [str(location) for location in error.get('loc')] self.validation_data['errors'].append( f'''Schema validation failed for layout.json. ''' f'''{error.get('msg')}: {' -> '.join(location)}''' ) except ValueError: # any JSON decode error will be caught during syntax validation return self.validation_data['schema'].append({'filename': self.lj.fqfn.name, 'status': status}) if status is True: self.check_layout_params() def check_layout_params(self) -> None: """Check that the layout.json is consistent with install.json. The layout.json files references the params.name from the install.json file. The method will validate that no reference appear for inputs in install.json that don't exist. """ # do not track hidden or serviceConfig inputs as they should not be in layouts.json ij_input_names = list(self.ij.model.filter_params(service_config=False, hidden=False)) ij_output_names = [o.name for o in self.ij.model.playbook.output_variables] # Check for duplicate inputs for name in self.ij.validate.validate_duplicate_input(): self.validation_data['errors'].append( f'Duplicate input name found in install.json ({name})' ) status = False # Check for duplicate sequence numbers for sequence in self.ij.validate.validate_duplicate_sequence(): self.validation_data['errors'].append( f'Duplicate sequence number found in install.json ({sequence})' ) status = False # Check for duplicate outputs variables for output in self.ij.validate.validate_duplicate_output(): self.validation_data['errors'].append( f'Duplicate output variable name found in install.json ({output})' ) status = False if 'sqlite3' in sys.modules: # create temporary inputs tables self.permutations.db_create_table(self.permutations._input_table, ij_input_names) # inputs status = True for i in self.lj.model.inputs: for p in i.parameters: if p.name not in ij_input_names: # update validation data errors self.validation_data['errors'].append( 'Layouts input.parameters[].name validations failed ' f'''("{p.get('name')}" is defined in layout.json, ''' 'but hidden or not found in install.json).' ) status = False else: # any item in list afterwards is a problem ij_input_names.remove(p.name) if 'sqlite3' in sys.modules: if p.display: display_query = ( f'''SELECT * FROM {self.permutations._input_table}''' # nosec f''' WHERE {p.display}''' ) try: self.permutations.db_conn.execute(display_query.replace('"', '')) except sqlite3.Error: self.validation_data['errors'].append( '''Layouts input.parameters[].display validations failed ''' f'''("{p.display}" query is an invalid statement).''' ) status = False # update validation data for module self.validation_data['layouts'].append({'params': 'inputs', 'status': status}) if ij_input_names: input_names = ','.join(ij_input_names) # update validation data errors self.validation_data['errors'].append( f'Layouts input.parameters[].name validations failed ("{input_names}" ' 'values from install.json were not included in layout.json.' ) status = False # outputs status = True for o in self.lj.model.outputs: if o.name not in ij_output_names: # update validation data errors self.validation_data['errors'].append( f'''Layouts output validations failed ({o.name} is defined ''' '''in layout.json, but not found in install.json).''' ) status = False if 'sqlite3' in sys.modules: if o.display: display_query = ( f'''SELECT * FROM {self.permutations._input_table} ''' # nosec f'''WHERE {o.display}''' ) try: self.permutations.db_conn.execute(display_query.replace('"', '')) except sqlite3.Error: self.validation_data['errors'].append( f"""Layouts outputs.display validations failed ("{o.display}" """ f"""query is an invalid statement).""" ) status = False # update validation data for module self.validation_data['layouts'].append({'params': 'outputs', 'status': status}) def check_syntax(self, app_path=None) -> None: """Run syntax on each ".py" and ".json" file. Args: app_path (str, optional): The path of Python files. """ fqpn = Path(app_path or os.getcwd()) for fqfn in sorted(fqpn.iterdir()): error = None status = True if fqfn.name.endswith('.py'): try: with fqfn.open(mode='rb') as fh: ast.parse(fh.read(), filename=fqfn.name) except SyntaxError: status = False # cleanup output e = [] for line in traceback.format_exc().split('\n')[-5:-2]: e.append(line.strip()) error = ' '.join(e) elif fqfn.name.endswith('.json'): try: with fqfn.open() as fh: json.load(fh) except ValueError as e: # update tracker for common files self.invalid_json_files.append(fqfn.name) status = False error = e else: # skip unsupported file types continue if error: # update validation data errors self.validation_data['errors'].append( f'Syntax validation failed for {fqfn.name} ({error}).' ) # store status for this file self.validation_data['fileSyntax'].append({'filename': fqfn.name, 'status': status}) def interactive(self) -> None: """[App Builder] Run in interactive mode.""" while True: line = sys.stdin.readline().strip() if line == 'quit': sys.exit() elif line == 'validate': self.check_syntax() self.check_imports() self.check_install_json() self.check_layout_json() self.check_job_json() self.print_json() # reset validation_data self.validation_data = self._validation_data def print_json(self) -> None: """[App Builder] Print JSON output.""" print(json.dumps({'validation_data': self.validation_data})) # TODO: [low] switch to typer echo? def _print_file_syntax_results(self) -> None: """Print file syntax results.""" if self.validation_data.get('fileSyntax'): print(f'\n{c.Style.BRIGHT}{c.Fore.BLUE}Validated File Syntax:') print(f'''{c.Style.BRIGHT}{'File:'!s:<60}{'Status:'!s:<25}''') for f in self.validation_data.get('fileSyntax'): status_color = self.status_color(f.get('status')) status_value = self.status_value(f.get('status')) print(f"{f.get('filename')!s:<60}{status_color}{status_value!s:<25}") def _print_imports_results(self) -> None: """Print import results.""" if self.validation_data.get('moduleImports'): print(f'\n{c.Style.BRIGHT}{c.Fore.BLUE}Validated Imports:') print(f'''{c.Style.BRIGHT}{'File:'!s:<30}{'Module:'!s:<30}{'Status:'!s:<25}''') for f in self.validation_data.get('moduleImports'): status_color = self.status_color(f.get('status')) status_value = self.status_value(f.get('status')) print( f'''{f.get('filename')!s:<30}{c.Fore.WHITE}''' f'''{f.get('module')!s:<30}{status_color}{status_value!s:<25}''' ) def _print_schema_results(self) -> None: """Print schema results.""" if self.validation_data.get('schema'): print(f'\n{c.Style.BRIGHT}{c.Fore.BLUE}Validated Schema:') print(f'''{c.Style.BRIGHT}{'File:'!s:<60}{'Status:'!s:<25}''') for f in self.validation_data.get('schema'): status_color = self.status_color(f.get('status')) status_value = self.status_value(f.get('status')) print(f'''{f.get('filename')!s:<60}{status_color}{status_value!s:<25}''') def _print_layouts_results(self) -> None: """Print layout results.""" if self.validation_data.get('layouts'): print(f'\n{c.Style.BRIGHT}{c.Fore.BLUE}Validated Layouts:') print(f'''{c.Style.BRIGHT}{'Params:'!s:<60}{'Status:'!s:<25}''') for f in self.validation_data.get('layouts'): status_color = self.status_color(f.get('status')) status_value = self.status_value(f.get('status')) print(f"{f.get('params')!s:<60}{status_color}{status_value!s:<25}") def _print_feed_results(self) -> None: """Print feed results.""" if self.validation_data.get('feeds'): print(f'\n{c.Style.BRIGHT}{c.Fore.BLUE}Validated Feed Jobs:') print(f'''{c.Style.BRIGHT}{'Feeds:'!s:<60}{'Status:'!s:<25}''') for f in self.validation_data.get('feeds'): status_color = self.status_color(f.get('status')) status_value = self.status_value(f.get('status')) print(f"{f.get('name')!s:<60}{status_color}{status_value!s:<25}") def _print_errors(self) -> None: """Print errors results.""" if self.validation_data.get('errors'): print('\n') # separate errors from normal output for error in self.validation_data.get('errors'): # print all errors print(f'* {c.Fore.RED}{error}') # ignore exit code if not self.ignore_validation: self.exit_code = 1 def print_results(self) -> None: """Print results.""" # Validating Syntax self._print_file_syntax_results() # Validating Imports self._print_imports_results() # Validating Schema self._print_schema_results() # Validating Layouts self._print_layouts_results() # Validating Feed Job Definition Files self._print_feed_results() self._print_errors() @staticmethod def status_color(status) -> str: """Return the appropriate status color.""" return c.Fore.GREEN if status else c.Fore.RED @staticmethod def status_value(status) -> str: """Return the appropriate status color.""" return 'passed' if status else 'failed'
tcex/bin/validate.py
21,749
Validate syntax, imports, and schemas. * Python and JSON file syntax * Python import modules * install.json schema * layout.json schema Initialize Class properties. . Print errors results. Print feed results. Print file syntax results. Print import results. Print layout results. Print schema results. Return structure for validation data. Check if module is in Python stdlib. Args: module: The name of the module to check. Returns: bool: Returns True if the module is in the stdlib or template. Check whether the provide module can be imported (package installed). Args: module: The name of the module to check availability. Returns: bool: True if the module can be imported, False otherwise. Check the projects top level directory for missing imports. This method will check only files ending in **.py** and does not handle imports validation for sub-directories. Check all install.json files for valid schema. Validate feed files for feed job apps. Check all layout.json files for valid schema. Check that the layout.json is consistent with install.json. The layout.json files references the params.name from the install.json file. The method will validate that no reference appear for inputs in install.json that don't exist. Run syntax on each ".py" and ".json" file. Args: app_path (str, optional): The path of Python files. [App Builder] Run in interactive mode. [App Builder] Print JSON output. Print results. Return the appropriate status color. Return the appropriate status color. TcEx Framework Validate Module. !/usr/bin/env python standard library third-party from jsonschema import SchemaError, ValidationError, validate first-party standard library this module is only required for certain CLI commands class properties initialize validation data TODO: [low] is there a better way? pylint: disable=unused-variable https://docs.python.org/3/library/importlib.htmlchecking-if-a-module-can-be-imported if dist-packages|site-packages in module_path the import doesn't count any JSON decode error will be caught during syntax validation can't proceed if install.json can't be read use developer defined app version (deprecated) or package_version from InstallJson model no need to check if schema if json is invalid validate the job file exists validate the schema validate program name validate program version any JSON decode error will be caught during syntax validation do not track hidden or serviceConfig inputs as they should not be in layouts.json Check for duplicate inputs Check for duplicate sequence numbers Check for duplicate outputs variables create temporary inputs tables inputs update validation data errors any item in list afterwards is a problem nosec update validation data for module update validation data errors outputs update validation data errors nosec update validation data for module cleanup output update tracker for common files skip unsupported file types update validation data errors store status for this file reset validation_data TODO: [low] switch to typer echo? separate errors from normal output print all errors ignore exit code Validating Syntax Validating Imports Validating Schema Validating Layouts Validating Feed Job Definition Files
3,223
en
0.57364
# -*- coding: utf-8 -*- #Chucky_Bot import LINETCR from LINETCR.lib.curve.ttypes import * from datetime import datetime from bs4 import BeautifulSoup from threading import Thread from googletrans import Translator from gtts import gTTS import time,random,sys,json,codecs,threading,glob,urllib,urllib2,urllib3,re,ast,os,subprocess,requests,tempfile nadya = LINETCR.LINE() #nadya.login(qr=True) nadya.login(token='Eq8HO0fhYMrll5V2r6v3.uFyCY3rEW6udwsHCnFj70W.KD1Mlw3UQ67PLM8N+4pVdjTi1joYo3zu7hlhQV6XWuo=') nadya.loginResult() print "Nadya-Login Success\n\n=====[Sukses Login]=====" reload(sys) sys.setdefaultencoding('utf-8') selfMessage =""" ╔═════════════════════════ ║ ☆☞ FRYANT S E L F ☜☆ ╠═════════════════════════ ╠➩〘Hi〙 ╠➩〘Me〙 ╠➩〘Mymid〙 ╠➩〘Mid @〙 ╠➩〘SearchID: (ID LINE)〙 ╠➩〘Checkdate (DD/MM/YY)〙 ╠➩〘Kalender〙 ╠➩〘Steal contact〙 ╠➩〘Pp @〙 ╠➩〘Cover @〙 ╠➩〘Auto like〙 ╠➩〘Scbc Text〙 ╠➩〘Cbc Text〙 ╠➩〘Gbc Text〙 ╠➩〘Getbio @〙 ╠➩〘Getinfo @〙 ╠➩〘Getname @〙 ╠➩〘Getprofile @〙 ╠➩〘Getcontact @〙 ╠➩〘Getvid @〙 ╠➩〘Friendlist〙 ╠➩〘Micadd @〙 ╠➩〘Micdel @〙 ╠➩〘Miclist〙 ╠═════════════════════════ ║ ༄ིৡ❍ᶜʰᵉ+Sepri‮࿐ৡ  SelfBot Versi 124V ╚═════════════════════════ """ botMessage =""" ╔═════════════════════════ ║ ☆☞ FRYANT B O T ☜☆ ╠═════════════════════════ ╠➩〘Absen〙 ╠➩〘Respon〙 ╠➩〘Runtime〙 ╠➩〘Mycopy @〙 ╠➩〘Copycontact〙 ╠➩〘Mybackup〙 ╠➩〘Mybio (Text)〙 ╠➩〘Myname (Text)〙 ╠➩〘@bye〙 ╠➩〘Bot on/off〙 ╠═════════════════════════ ║ ༄ིৡ❍ᶜʰᵉ+Sepri‮࿐ৡ  SelfBot Versi 124V ╚═════════════════════════ """ mediaMessage =""" ╔═════════════════════════ ║ ☆☞ FRYANT M E D I A ☜☆ ╠═════════════════════════ ╠➩〘Gift〙 ╠➩〘Gift1 @ s/d Gift10 @〙 ╠➩〘Giftbycontact〙 ╠➩〘Gif gore〙 ╠➩〘Google: (Text)〙 ╠➩〘Playstore NamaApp〙 ╠➩〘Fancytext: Text〙 ╠➩〘/musik Judul-Penyanyi〙 ╠➩〘/lirik Judul-Penyanyi〙 ╠➩〘/musrik Judul-Penyanyi〙 ╠➩〘/ig UrsnameInstagram〙 ╠➩〘Checkig UrsnameInstagram〙 ╠➩〘/apakah Text (Kerang Ajaib)〙 ╠➩〘/kapan Text (Kerang Ajaib)〙 ╠➩〘/hari Text (Kerang Ajaib)〙 ╠➩〘/berapa Text (Kerang Ajaib)〙 ╠➩〘/berapakah Text〙 ╠➩〘Youtubelink: Judul Video〙 ╠➩〘Youtubevideo: Judul Video〙 ╠➩〘Youtubesearch: Judul Video〙 ╠➩〘Image NamaGambar〙 ╠➩〘Say-id Text〙 ╠➩〘Say-en Text〙 ╠➩〘Say-jp Text〙 ╠➩〘Image NamaGambar〙 ╠➩〘Tr-id Text (Translate En Ke ID〙 ╠➩〘Tr-en Text (Translate ID Ke En〙 ╠➩〘Tr-th Text (Translate ID Ke Th〙 ╠➩〘Id@en Text (Translate ID Ke En〙 ╠➩〘Id@th Text (Translate ID Ke TH〙 ╠➩〘En@id Text (Translate En Ke ID〙 ╠═════════════════════════ ║ ༄ིৡ❍ᶜʰᵉ+Sepri‮࿐ৡ  SelfBot Versi 124V ╚═════════════════════════ """ groupMessage =""" ╔═════════════════════════ ║ ☆☞ FRYANT G R O U P ☜☆ ╠═════════════════════════ ╠➩〘Welcome〙 ╠➩〘Say welcome〙 ╠➩〘Invite creator〙 ╠➩〘Setview〙 ╠➩〘Viewseen〙 ╠➩〘Gn: (NamaGroup)〙 ╠➩〘Tag all〙 ╠➩〘Recover〙 ╠➩〘Cancel〙 ╠➩〘Cancelall〙 ╠➩〘Gcreator〙 ╠➩〘Ginfo〙 ╠➩〘Gurl〙 ╠➩〘List group〙 ╠➩〘Pict group: (NamaGroup)〙 ╠➩〘Spam: (Text)〙 ╠➩〘Add all〙 ╠➩〘Kick: (Mid)〙 ╠➩〘Invite: (Mid)〙 ╠➩〘Invite〙 ╠➩〘Memlist〙 ╠➩〘Getgroup image〙 ╠➩〘Urlgroup Image〙 ╠═════════════════════════ ║ ༄ིৡ❍ᶜʰᵉ+Sepri‮࿐ৡ  SelfBot Versi 124V ╚═════════════════════════ """ tjia="u9f09cfcb17d037e2936b751bd9d40ead" setMessage =""" ╔═════════════════════════ ║ ☆☞ FRYANT S E T ☜☆ ╠═════════════════════════ ╠➩〘Sambutan on/off〙 ╠➩〘Mimic on/off〙 ╠➩〘Url on/off〙 ╠➩〘Alwaysread on/off〙 ╠➩〘Sider on/off〙 ╠➩〘Contact on/off〙 ╠➩〘Sticker on〙 ╠➩〘Simisimi on/off〙 ╠═════════════════════════ ║ ༄ིৡ❍ᶜʰᵉ+Sepri‮࿐ৡ  SelfBot Versi 124V ╚═════════════════════════ """ creatorMessage =""" ╔═════════════════════════ ║ ☆☞ FRYANT C R E A T O R ☜☆ ╠═════════════════════════ ╠➩〘Crash〙 ╠➩〘Kickall〙 ╠➩〘Bc: (Text)〙 ╠➩〘Join group: (NamaGroup〙 ╠➩〘Leave group: (NamaGroup〙 ╠➩〘Leave all group〙 ╠➩〘Tag on/off〙 ╠➩〘Bot restart〙 ╠➩〘Turn off〙 ╠═════════════════════════ ║ ༄ིৡ❍ᶜʰᵉ+Sepri‮࿐ৡ  SelfBot Versi 124V ╚═════════════════════════ """ adminMessage =""" ╔═════════════════════════ ║ ☆☞ A D M I N ☜☆ ╠═════════════════════════ ╠➩〘Allprotect on/off〙 ╠➩〘Ban〙 ╠➩〘Unban〙 ╠➩〘Ban @〙 ╠➩〘Unban @〙 ╠➩〘Ban list〙 ╠➩〘Clear ban〙 ╠➩〘Kill〙 ╠➩〘Kick @〙 ╠➩〘Set member: (Jumblah)〙 ╠➩〘Ban group: (NamaGroup〙 ╠➩〘Del ban: (NamaGroup〙 ╠➩〘List ban〙 ╠➩〘Kill ban〙 ╠➩〘Glist〙 ╠➩〘Glistmid〙 ╠➩〘Details group: (Gid)〙 ╠➩〘Cancel invite: (Gid)〙 ╠➩〘Invitemeto: (Gid)〙 ╠➩〘Acc invite〙 ╠➩〘Removechat〙 ╠➩〘Qr on/off〙 ╠➩〘Autokick on/off〙 ╠➩〘Autocancel on/off〙 ╠➩〘Invitepro on/off〙 ╠➩〘Join on/off〙 ╠➩〘Joincancel on/off〙 ╠➩〘Respon1 on/off〙 ╠➩〘Respon2 on/off〙 ╠➩〘Respon3 on/off〙 ╠➩〘Responkick on/off〙 ╠═════════════════════════ ║ ༄ིৡ❍ᶜʰᵉ+Sepri‮࿐ৡ  SelfBot Versi 124V ╚═════════════════════════ """ helpMessage =""" ╔═════════════════════════ ║ ☆☞ FRYANT H E L P ☜☆ ╠═════════════════════════ ╠➩〘Help self〙 ╠➩〘Help bot〙 ╠➩〘Help group〙 ╠➩〘Help set〙 ╠➩〘Help media〙 ╠➩〘Help admin〙 ╠➩〘Help creator〙 ╠➩〘Owner〙 ╠➩〘Pap owner〙 ╠➩〘Speed〙 ╠➩〘Speed test〙 ╠➩〘Status〙 ╠═════════════════════════ ║ ༄ིৡ❍ᶜʰᵉ+Sepri‮࿐ৡ  SelfBot Versi 124V ╚═════════════════════════ """ KAC=[nadya] mid = nadya.getProfile().mid Bots=[mid] Creator=["u51f61ccb745ec3a50359285c35f27bd3"] admin=["u51f61ccb745ec3a50359285c35f27bd3"] contact = nadya.getProfile() backup1 = nadya.getProfile() backup1.displayName = contact.displayName backup1.statusMessage = contact.statusMessage backup1.pictureStatus = contact.pictureStatus responsename = nadya.getProfile().displayName wait = { "LeaveRoom":True, "Bot":True, "AutoJoin":False, "AutoJoinCancel":False, "memberscancel":30, "Members":1, "AutoCancel":False, "AutoKick":False, 'pap':{}, 'invite':{}, 'steal':{}, 'gift':{}, 'copy':{}, 'likeOn':{}, 'detectMention':False, 'detectMention2':False, 'detectMention3':True, 'kickMention':False, 'sticker':False, 'timeline':True, "Timeline":True, "comment":"Bot Auto Like ©By : Nadya\nContact Me : 👉 line.me/ti/p/~sepriche.", "commentOn":True, "commentBlack":{}, "message":"Thx For Add Me (^_^)\nInvite Me To Your Group ヘ(^_^)ヘ", "blacklist":{}, "wblacklist":False, "dblacklist":False, "Qr":False, "Contact":False, "Sambutan":True, "inviteprotect":False, "alwaysRead":False, "Sider":{}, "Simi":{}, "lang":"JP", "BlGroup":{} } settings = { "simiSimi":{} } cctv = { "cyduk":{}, "point":{}, "sidermem":{} } wait2 = { "readPoint":{}, "readMember":{}, "setTime":{}, "ROM":{} } mimic = { "copy":False, "copy2":False, "status":False, "target":{} } setTime = {} setTime = wait2['setTime'] mulai = time.time() def download_page(url): version = (3,0) cur_version = sys.version_info if cur_version >= version: import urllib,request try: headers = {} headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36" req = urllib,request.Request(url, headers = headers) resp = urllib,request.urlopen(req) respData = str(resp.read()) return respData except Exception as e: print(str(e)) else: import urllib2 try: headers = {} headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17" req = urllib2.Request(url, headers = headers) response = urllib2.urlopen(req) page = response.read() return page except: return"Page Not found" def _images_get_next_item(s): start_line = s.find('rg_di') if start_line == -1: end_quote = 0 link = "no_links" return link, end_quote else: start_line = s.find('"class="rg_meta"') start_content = s.find('"ou"',start_line+90) end_content = s.find(',"ow"',start_content-90) content_raw = str(s[start_content+6:end_content-1]) return content_raw, end_content def _images_get_all_items(page): items = [] while True: item, end_content = _images_get_next_item(page) if item == "no_links": break else: items.append(item) time.sleep(0.1) page = page[end_content:] return items def waktu(secs): mins, secs = divmod(secs,60) hours, mins = divmod(mins,60) return '%02d Jam %02d Menit %02d Detik' % (hours, mins, secs) def cms(string, commands): #/XXX, >XXX, ;XXX, ^XXX, %XXX, $XXX... tex = ["+","@","/",">",";","^","%","$","^","サテラ:","サテラ:","サテラ:","サテラ:"] for texX in tex: for command in commands: if string ==command: return True return False def upload_tempimage(client): ''' Upload a picture of a kitten. We don't ship one, so get creative! ''' config = { 'album': album, 'name': 'bot auto upload', 'title': 'bot auto upload', 'description': 'bot auto upload' } print("Uploading image... ") image = client.upload_from_path(image_path, config=config, anon=False) print("Done") print() return image def sendAudio(self, to_, path): M = Message() M.text = None M.to = to_ M.contentMetadata = None M.contentPreview = None M.contentType = 3 M_id = self._client.sendMessage(0,M).id files = { 'file': open(path, 'rb'), } def sendMessage(to, text, contentMetadata={}, contentType=0): mes = Message() mes.to, mes.from_ = to, profile.mid mes.text = text mes.contentType, mes.contentMetadata = contentType, contentMetadata if to not in messageReq: messageReq[to] = -1 messageReq[to] += 1 def sendImage(self, to_, path): M = Message(to=to_, text=None, contentType = 1) M.contentMetadata = None M.contentPreview = None M2 = self._client.sendMessage(0,M) M_id = M2.id files = { 'file': open(path, 'rb'), } params = { 'name': 'media', 'oid': M_id, 'size': len(open(path, 'rb').read()), 'type': 'image', 'ver': '1.0', } data = { 'params': json.dumps(params) } r = self.post_content('https://obs-sg.line-apps.com/talk/m/upload.nhn', data=data, files=files) if r.status_code != 201: raise Exception('Upload image failure.') return True def sendImageWithURL(self, to_, url): path = '%s/pythonLine-%i.data' % (tempfile.gettempdir(), randint(0, 9)) r = requests.get(url, stream=True) if r.status_code == 200: with open(path, 'w') as f: shutil.copyfileobj(r.raw, f) else: raise Exception('Download image failure.') try: self.sendImage(to_, path) except: try: self.sendImage(to_, path) except Exception as e: raise e def sendAudioWithURL(self, to_, url): path = self.downloadFileWithURL(url) try: self.sendAudio(to_, path) except Exception as e: raise Exception(e) def sendAudioWithUrl(self, to_, url): path = '%s/pythonLine-%1.data' % (tempfile.gettempdir(), randint(0, 9)) r = requests.get(url, stream=True, verify=False) if r.status_code == 200: with open(path, 'w') as f: shutil.copyfileobj(r.raw, f) else: raise Exception('Download audio failure.') try: self.sendAudio(to_, path) except Exception as e: raise e def downloadFileWithURL(self, fileUrl): saveAs = '%s/pythonLine-%i.data' % (tempfile.gettempdir(), randint(0, 9)) r = self.get_content(fileUrl) if r.status_code == 200: with open(saveAs, 'wb') as f: shutil.copyfileobj(r.raw, f) return saveAs else: raise Exception('Download file failure.') def summon(to, nama): aa = "" bb = "" strt = int(14) akh = int(14) nm = nama for mm in nm: akh = akh + 2 aa += """{"S":"""+json.dumps(str(strt))+""","E":"""+json.dumps(str(akh))+""","M":"""+json.dumps(mm)+"},""" strt = strt + 6 akh = akh + 4 bb += "\xe2\x95\xa0 @x \n" aa = (aa[:int(len(aa)-1)]) msg = Message() msg.to = to msg.text = "\xe2\x95\x94\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\n"+bb+"\xe2\x95\x9a\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90" msg.contentMetadata ={'MENTION':'{"MENTIONEES":['+aa+']}','EMTVER':'4'} print "[Command] Tag All" try: nadya.sendMessage(msg) except Exception as error: print error def restart_program(): python = sys.executable os.execl(python, python, * sys.argv) def bot(op): try: if op.type == 0: return if op.type == 5: if wait["autoAdd"] == True: nadya.findAndAddContactsByMid(op.param1) if(wait["message"]in[""," ","\n",None]): pass else: nadya.sendText(op.param1,str(wait["message"])) if op.type == 55: try: group_id = op.param1 user_id=op.param2 subprocess.Popen('echo "'+ user_id+'|'+str(op.createdTime)+'" >> dataSeen/%s.txt' % group_id, shell=True, stdout=subprocess.PIPE, ) except Exception as e: print e if op.type == 55: try: if cctv['cyduk'][op.param1]==True: if op.param1 in cctv['point']: Name = nadya.getContact(op.param2).displayName # Name = summon(op.param2) if Name in cctv['sidermem'][op.param1]: pass else: cctv['sidermem'][op.param1] += "\n• " + Name if " " in Name: nick = Name.split(' ') if len(nick) == 2: nadya.sendText(op.param1, "Haii " + "☞ " + Name + " ☜" + "\nNgintip Aja Niih. . .\nChat Kek Idiih (-__-) ") time.sleep(0.2) summon(op.param1,[op.param2]) else: nadya.sendText(op.param1, "Haii " + "☞ " + Name + " ☜" + "\nBetah Banget Jadi Penonton. . .\nChat Napa (-__-) ") time.sleep(0.2) summon(op.param1,[op.param2]) else: nadya.sendText(op.param1, "Haii " + "☞ " + Name + " ☜" + "\nNgapain Kak Ngintip Aja???\nSini Gabung Chat... ") time.sleep(0.2) summon(op.param1,[op.param2]) else: pass else: pass except: pass else: pass if op.type == 22: nadya.leaveRoom(op.param1) if op.type == 21: nadya.leaveRoom(op.param1) if op.type == 13: print op.param3 if op.param3 in mid: if op.param2 in Creator: nadya.acceptGroupInvitation(op.param1) if mid in op.param3: if wait["AutoJoinCancel"] == True: G = nadya.getGroup(op.param1) if len(G.members) <= wait["memberscancel"]: nadya.acceptGroupInvitation(op.param1) nadya.sendText(op.param1,"Maaf " + nadya.getContact(op.param2).displayName + "\nMember Kurang Dari 30 Orang\nUntuk Info, Silahkan Chat Owner Kami!") nadya.leaveGroup(op.param1) else: nadya.acceptGroupInvitation(op.param1) nadya.sendText(op.param1,"☆Ketik ☞Help☜ Untuk Bantuan☆\n☆Harap Gunakan Dengan Bijak ^_^ ☆") if mid in op.param3: if wait["AutoJoin"] == True: G = nadya.getGroup(op.param1) if len(G.members) <= wait["Members"]: nadya.rejectGroupInvitation(op.param1) else: nadya.acceptGroupInvitation(op.param1) nadya.sendText(op.param1,"☆Ketik ☞Help☜ Untuk Bantuan☆\n☆Harap Gunakan Dengan Bijak ^_^ ☆") else: if wait["AutoCancel"] == True: if op.param3 in Bots: pass else: nadya.cancelGroupInvitation(op.param1, [op.param3]) else: if op.param3 in wait["blacklist"]: nadya.cancelGroupInvitation(op.param1, [op.param3]) nadya.sendText(op.param1, "Blacklist Detected") else: pass if op.type == 13: if op.param2 not in Creator: if op.param2 not in admin: if op.param2 not in Bots: if op.param2 in Creator: if op.param2 in admin: if op.param2 in Bots: pass elif wait["inviteprotect"] == True: wait ["blacklist"][op.param2] = True nadya.cancelGroupInvitation(op.param1,[op.param3]) nadya.kickoutFromGroup(op.param1,[op.param2]) if op.param2 not in Creator: if op.param2 not in admin: if op.param2 not in Bots: if op.param2 in Creator: if op.param2 in admin: if op.param2 in Bots: pass if op.type == 19: if wait["AutoKick"] == True: try: if op.param3 in Creator: if op.param3 in admin: if op.param3 in Bots: pass if op.param2 in Creator: if op.param2 in admin: if op.param2 in Bots: pass else: nadya.kickoutFromGroup(op.param1,[op.param2]) if op.param2 in wait["blacklist"]: pass else: nadya.inviteIntoGroup(op.param1,[op.param3]) except: try: if op.param2 not in Creator: if op.param2 not in admin: if op.param2 not in Bots: nadya.kickoutFromGroup(op.param1,[op.param2]) if op.param2 in wait["blacklist"]: pass else: nadya.inviteIntoGroup(op.param1,[op.param3]) except: print ("client Kick regulation or Because it does not exist in the group\ngid=["+op.param1+"]\nmid=["+op.param2+"]") if op.param2 in wait["blacklist"]: pass else: if op.param2 in Creator: if op.param2 in admin: if op.param2 in Bots: pass else: wait["blacklist"][op.param2] = True if op.param2 in wait["blacklist"]: pass else: if op.param2 in Creator: if op.param2 in admin: if op.param2 in Bots: pass else: wait["blacklist"][op.param2] = True else: pass if mid in op.param3: if op.param2 in Creator: if op.param2 in Bots: pass try: nadya.kickoutFromGroup(op.param1,[op.param2]) nadya.kickoutFromGroup(op.param1,[op.param2]) except: try: nadya.kickoutFromGroup(op.param1,[op.param2]) except: print ("client Kick regulation or Because it does not exist in the group\ngid=["+op.param1+"]\nmid=["+op.param2+"]") if op.param2 in wait["blacklist"]: pass else: if op.param2 in Bots: pass if op.param2 in wait["blacklist"]: pass else: if op.param2 in Bots: pass else: wait["blacklist"][op.param2] = True if Creator in op.param3: if admin in op.param3: if op.param2 in Bots: pass try: nadya.kickoutFromGroup(op.param1,[op.param2]) nadya.kickoutFromGroup(op.param1,[op.param2]) except: try: if op.param2 not in Bots: nadya.kickoutFromGroup(op.param1,[op.param2]) if op.param2 in wait["blacklist"]: pass else: nadya.inviteIntoGroup(op.param1,[op.param3]) except: print ("client Kick regulation or Because it does not exist in the group\ngid=["+op.param1+"]\nmid=["+op.param2+"]") if op.param2 in wait["blacklist"]: pass if op.param2 in wait["whitelist"]: pass else: wait["blacklist"][op.param2] = True nadya.inviteIntoGroup(op.param1,[op.param3]) if op.param2 in wait["blacklist"]: pass if op.param2 in wait["whitelist"]: pass else: wait["blacklist"][op.param2] = True if op.type == 11: if wait["Qr"] == True: if op.param2 in Creator: if op.param2 in admin: if op.param2 in Bots: pass else: nadya.kickoutFromGroup(op.param1,[op.param2]) else: pass if op.type == 17: if wait["Sambutan"] == True: if op.param2 in Creator: return ginfo = nadya.getGroup(op.param1) contact = nadya.getContact(op.param2) image = "http://dl.profile.line-cdn.net/" + contact.pictureStatus nadya.sendText(op.param1,"Hallo " + nadya.getContact(op.param2).displayName + "\nWelcome To ☞ " + str(ginfo.name) + " ☜" + "\nBudayakan Cek Note\nDan Semoga Betah Disini ^_^") c = Message(to=op.param1, from_=None, text=None, contentType=13) c.contentMetadata={'mid':op.param2} nadya.sendMessage(c) nadya.sendImageWithURL(op.param1,image) d = Message(to=op.param1, from_=None, text=None, contentType=7) d.contentMetadata={ "STKID": "13269548", "STKPKGID": "1329191", "STKVER": "1" } nadya.sendMessage(d) print "MEMBER JOIN TO GROUP" if op.type == 15: if wait["Sambutan"] == True: if op.param2 in Creator: return nadya.sendText(op.param1,"Good Bye " + nadya.getContact(op.param2).displayName + "\nSee You Next Time . . . (p′︵‵。) 🤗") d = Message(to=op.param1, from_=None, text=None, contentType=7) d.contentMetadata={ "STKID": "13269542", "STKPKGID": "1329191", "STKVER": "1" } nadya.sendMessage(d) print "MEMBER HAS LEFT THE GROUP" if op.type == 26: msg = op.message if msg.from_ in mimic["target"] and mimic["status"] == True and mimic["target"][msg.from_] == True: text = msg.text if text is not None: nadya.sendText(msg.to,text) if msg.to in settings["simiSimi"]: if settings["simiSimi"][msg.to] == True: if msg.text is not None: text = msg.text r = requests.get("http://api.ntcorp.us/chatbot/v1/?text=" + text.replace(" ","+") + "&key=beta1.nt") data = r.text data = json.loads(data) if data['status'] == 200: if data['result']['result'] == 100: nadya.sendText(msg.to,data['result']['response'].encode('utf-8')) if 'MENTION' in msg.contentMetadata.keys() != None: if wait["kickMention"] == True: contact = nadya.getContact(msg.from_) cName = contact.displayName balas = ["Aku Bilang Jangan Ngetag Lagi " + cName + "\nAku Kick Kamu! Sorry, Byee!!!"] ret_ = random.choice(balas) name = re.findall(r'@(\w+)', msg.text) mention = ast.literal_eval(msg.contentMetadata['MENTION']) mentionees = mention['MENTIONEES'] for mention in mentionees: if mention['M'] in Bots: nadya.sendText(msg.to,ret_) nadya.kickoutFromGroup(msg.to,[msg.from_]) break if 'MENTION' in msg.contentMetadata.keys() != None: if wait["detectMention"] == True: contact = nadya.getContact(msg.from_) cName = contact.displayName balas = ["Sekali tag, berarti naksir aim😅",cName + " Follow ya id smuleku @Fryant_BSS1",cName + " Iya sayank, I love you too muacchhh😘","aih, org ganteng, ditag mulu🙄", cName + " kaka mau nikung aku yah??🙄","kalau mau didesahin\npm aja kak😂 " + cName, "kangen ya sayank??🙄 " + cName, "Follow id smule ku ya ka @Fryant_BSS1 " + cName + "😘😘😘", "Kaka mau nikung aku yah " + cName + "😰","orang ganteng " + cName + " pasti ditag mulu 😆"] ret_ = random.choice(balas) name = re.findall(r'@(\w+)', msg.text) mention = ast.literal_eval(msg.contentMetadata['MENTION']) mentionees = mention['MENTIONEES'] for mention in mentionees: if mention['M'] in Bots: nadya.sendText(msg.to,ret_) break if 'MENTION' in msg.contentMetadata.keys() != None: if wait["detectMention2"] == True: contact = nadya.getContact(msg.from_) cName = contact.displayName balas = ["kenapa sayank,, kangen yah??","jangan tag kalau ga mau aku hamilin","jangan tag " + cName + " tuan muda lagi meeting"] ret_ = random.choice(balas) name = re.findall(r'@(\w+)', msg.text) mention = ast.literal_eval(msg.contentMetadata['MENTION']) mentionees = mention['MENTIONEES'] for mention in mentionees: if mention['M'] in Bots: nadya.sendText(msg.to,ret_) msg.contentType = 7 msg.text = None msg.contentMetadata = { "STKID": "157", "STKPKGID": "2", "STKVER": "100" } nadya.sendMessage(msg) break if 'MENTION' in msg.contentMetadata.keys() != None: if wait["detectMention3"] == True: contact = nadya.getContact(msg.from_) cName = contact.displayName balas = ["Iya sayank " + cName + ", Syg kangen ya...aku lg kerja buat menata masa depan kita"] balas1 = "Supaya aq dan kamu, bahagia selalu😘😘😘" ret_ = random.choice(balas) image = "http://dl.profile.line-cdn.net/" + contact.pictureStatus name = re.findall(r'@(\w+)', msg.text) mention = ast.literal_eval(msg.contentMetadata['MENTION']) mentionees = mention['MENTIONEES'] for mention in mentionees: if mention['M'] in Bots: nadya.sendText(msg.to,ret_) nadya.sendText(msg.to,balas1) nadya.sendImageWithURL(msg.to,image) msg.contentType = 7 msg.text = None msg.contentMetadata = { "STKID": "11764508", "STKPKGID": "6641", "STKVER": "1" } nadya.sendMessage(msg) break if op.type == 25: msg = op.message if msg.text in ["Bot on"]: wait["Bot"] = True nadya.sendText(msg.to,"Bot Sudah On Kembali.") if op.type == 25: if wait["Bot"] == True: msg = op.message if msg.contentType == 7: if wait["sticker"] == True: msg.contentType = 0 stk_id = msg.contentMetadata['STKID'] stk_ver = msg.contentMetadata['STKVER'] pkg_id = msg.contentMetadata['STKPKGID'] filler = "『 Sticker Check 』\nSTKID : %s\nSTKPKGID : %s\nSTKVER : %s\n『 Link 』\nline://shop/detail/%s" % (stk_id,pkg_id,stk_ver,pkg_id) nadya.sendText(msg.to, filler) wait["sticker"] = False else: pass if wait["alwaysRead"] == True: if msg.toType == 0: nadya.sendChatChecked(msg.from_,msg.id) else: nadya.sendChatChecked(msg.to,msg.id) if msg.contentType == 16: if wait['likeOn'] == True: url = msg.contentMetadata["postEndUrl"] nadya.like(url[25:58], url[66:], likeType=1005) nadya.comment(url[25:58], url[66:], wait["comment"]) nadya.sendText(msg.to,"Like Success") wait['likeOn'] = False if msg.contentType == 13: if wait["wblacklist"] == True: if msg.contentMetadata["mid"] not in admin: if msg.contentMetadata["mid"] in wait["blacklist"]: nadya.sendText(msg.to,"Sudah") wait["wblacklist"] = False else: wait["blacklist"][msg.contentMetadata["mid"]] = True wait["wblacklist"] = False nadya.sendText(msg.to,"Ditambahkan") else: nadya.sendText(msg.to,"Admin Detected~") elif wait["dblacklist"] == True: if msg.contentMetadata["mid"] in wait["blacklist"]: del wait["blacklist"][msg.contentMetadata["mid"]] nadya.sendText(msg.to,"Terhapus") wait["dblacklist"] = False else: wait["dblacklist"] = False nadya.sendText(msg.to,"Tidak Ada Black List") elif wait["Contact"] == True: msg.contentType = 0 nadya.sendText(msg.to,msg.contentMetadata["mid"]) if 'displayName' in msg.contentMetadata: contact = nadya.getContact(msg.contentMetadata["mid"]) try: cu = nadya.channel.getCover(msg.contentMetadata["mid"]) except: cu = "" nadya.sendText(msg.to,"Nama:\n" + msg.contentMetadata["displayName"] + "\n\nMid:\n" + msg.contentMetadata["mid"] + "\n\nStatus:\n" + contact.statusMessage + "\n\nPhoto Profile:\nhttp://dl.profile.line-cdn.net/" + contact.pictureStatus + "\n\nPhoto Cover:\n" + str(cu)) else: contact = nadya.getContact(msg.contentMetadata["mid"]) try: cu = nadya.channel.getCover(msg.contentMetadata["mid"]) except: cu = "" nadya.sendText(msg.to,"Nama:\n" + msg.contentMetadata["displayName"] + "\n\nMid:\n" + msg.contentMetadata["mid"] + "\n\nStatus:\n" + contact.statusMessage + "\n\nPhoto Profile:\nhttp://dl.profile.line-cdn.net/" + contact.pictureStatus + "\n\nPhoto Cover:\n" + str(cu)) elif msg.text == "Ginfo": if msg.toType == 2: ginfo = nadya.getGroup(msg.to) try: gCreator = ginfo.creator.displayName except: gCreator = "Error" if wait["lang"] == "JP": if ginfo.invitee is None: sinvitee = "0" else: sinvitee = str(len(ginfo.invitee)) if ginfo.preventJoinByTicket == True: u = "close" else: u = "open" nadya.sendText(msg.to,"[Group name]\n" + str(ginfo.name) + "\n\n[Gid]\n" + msg.to + "\n\n[Group creator]\n" + gCreator + "\n\n[Profile status]\nhttp://dl.profile.line.naver.jp/" + ginfo.pictureStatus + "\n\nMembers:" + str(len(ginfo.members)) + "members\nPending:" + sinvitee + "people\nURL:" + u + "it is inside") else: nadya.sendText(msg.to,"[group name]\n" + str(ginfo.name) + "\n[gid]\n" + msg.to + "\n[group creator]\n" + gCreator + "\n[profile status]\nhttp://dl.profile.line.naver.jp/" + ginfo.pictureStatus) else: if wait["lang"] == "JP": nadya.sendText(msg.to,"Can not be used outside the group") else: nadya.sendText(msg.to,"Not for use less than group") elif msg.text is None: return elif msg.text in ["Creator","Owner"]: msg.contentType = 13 msg.contentMetadata = {'mid': tjia} nadya.sendMessage(msg) nadya.sendText(msg.to,"Itu Majikan Kami (^_^)") elif msg.text in ["Group creator","Gcreator","gcreator"]: ginfo = nadya.getGroup(msg.to) gCreator = ginfo.creator.mid msg.contentType = 13 msg.contentMetadata = {'mid': gCreator} nadya.sendMessage(msg) nadya.sendText(msg.to,"Itu Yang Buat Grup Ini") elif msg.contentType == 16: if wait["Timeline"] == True: msg.contentType = 0 msg.text = "post URL\n" + msg.contentMetadata["postEndUrl"] nadya.sendText(msg.to,msg.text) if msg.contentType == 13: if wait["steal"] == True: _name = msg.contentMetadata["displayName"] copy = msg.contentMetadata["mid"] groups = nadya.getGroup(msg.to) pending = groups.invitee targets = [] for s in groups.members: if _name in s.displayName: print "[Target] Stealed" break else: targets.append(copy) if targets == []: pass else: for target in targets: try: nadya.findAndAddContactsByMid(target) contact = nadya.getContact(target) cu = nadya.channel.getCover(target) path = str(cu) image = "http://dl.profile.line-cdn.net/" + contact.pictureStatus nadya.sendText(msg.to,"Nama :\n" + contact.displayName + "\n\nMid :\n" + msg.contentMetadata["mid"] + "\n\nBio :\n" + contact.statusMessage) nadya.sendText(msg.to,"Profile Picture " + contact.displayName) nadya.sendImageWithURL(msg.to,image) nadya.sendText(msg.to,"Cover " + contact.displayName) nadya.sendImageWithURL(msg.to,path) wait["steal"] = False break except: pass if msg.contentType == 13: if wait["gift"] == True: _name = msg.contentMetadata["displayName"] copy = msg.contentMetadata["mid"] groups = nadya.getGroup(msg.to) pending = groups.invitee targets = [] for s in groups.members: if _name in s.displayName: print "[Target] Gift" break else: targets.append(copy) if targets == []: pass else: for target in targets: try: nadya.sendText(msg.to,"Gift Sudah Terkirim!") msg.contentType = 9 msg.contentMetadata= {'PRDTYPE': 'STICKER', 'STKVER': '1', 'MSGTPL': '1', 'STKPKGID': '1296261'} msg.to = target msg.text = None nadya.sendMessage(msg) wait['gift'] = False break except: msg.contentMetadata = {'mid': target} wait["gift"] = False break if msg.contentType == 13: if wait["copy"] == True: _name = msg.contentMetadata["displayName"] copy = msg.contentMetadata["mid"] groups = nadya.getGroup(msg.to) targets = [] for s in groups.members: if _name in s.displayName: print "[Target] Copy" break else: targets.append(copy) if targets == []: nadya.sendText(msg.to, "Not Found...") pass else: for target in targets: try: nadya.CloneContactProfile(target) nadya.sendText(msg.to, "Copied (^_^)") wait['copy'] = False break except: msg.contentMetadata = {'mid': target} wait["copy"] = False break if msg.contentType == 13: if wait['invite'] == True: _name = msg.contentMetadata["displayName"] invite = msg.contentMetadata["mid"] groups = nadya.getGroup(msg.to) pending = groups.invitee targets = [] for s in groups.members: if _name in s.displayName: nadya.sendText(msg.to, _name + " Berada DiGrup Ini") else: targets.append(invite) if targets == []: pass else: for target in targets: try: nadya.findAndAddContactsByMid(target) nadya.inviteIntoGroup(msg.to,[target]) nadya.sendText(msg.to,"Invite " + _name) wait['invite'] = False break except: nadya.sendText(msg.to,"Limit Invite") wait['invite'] = False break elif msg.text in ["Key creator","help creator","Fryant 1"]: nadya.sendText(msg.to,creatorMessage) elif msg.text in ["Key group","help group","Fryant 2"]: nadya.sendText(msg.to,groupMessage) elif msg.text in ["Key","Fryant","Help"]: nadya.sendText(msg.to,helpMessage) elif msg.text in ["Key self","help self","Fryant 3"]: nadya.sendText(msg.to,selfMessage) elif msg.text in ["Key bot","help bot","Fryant 4"]: nadya.sendText(msg.to,botMessage) elif msg.text in ["Key set","help set","Fryant 5"]: nadya.sendText(msg.to,setMessage) elif msg.text in ["Key media","help media","Fryant 6"]: nadya.sendText(msg.to,mediaMessage) elif msg.text in ["Key admin","help admin","Fryant 7"]: nadya.sendText(msg.to,adminMessage) elif msg.text in ["Fryant group"]: gid = nadya.getGroupIdsJoined() h = "" jml = 0 for i in gid: gn = nadya.getGroup(i).name h += "♦【%s】\n" % (gn) jml += 1 nadya.sendText(msg.to,"=======[List Group]=======\n"+ h +"\nTotal Group: "+str(jml)) elif "Ban group: " in msg.text: grp = msg.text.replace("Ban group: ","") gid = nadya.getGroupIdsJoined() if msg.from_ in admin: for i in gid: h = nadya.getGroup(i).name if h == grp: wait["BlGroup"][i]=True nadya.sendText(msg.to, "Success Ban Group : "+grp) else: pass else: nadya.sendText(msg.to, "Khusus Nadya") elif msg.text in ["List ban","List ban group"]: if msg.from_ in admin: if wait["BlGroup"] == {}: nadya.sendText(msg.to,"Tidak Ada") else: mc = "" for gid in wait["BlGroup"]: mc += "-> " +nadya.getGroup(gid).name + "\n" nadya.sendText(msg.to,"===[Ban Group]===\n"+mc) else: nadya.sendText(msg.to, "Khusus Admin") elif msg.text in ["Del ban: "]: if msg.from_ in admin: ng = msg.text.replace("Del ban: ","") for gid in wait["BlGroup"]: if nadya.getGroup(gid).name == ng: del wait["BlGroup"][gid] nadya.sendText(msg.to, "Success del ban "+ng) else: pass else: nadya.sendText(msg.to, "Khusus Nadya") elif "Join group: " in msg.text: ng = msg.text.replace("Join group: ","") gid = nadya.getGroupIdsJoined() try: if msg.from_ in Creator: for i in gid: h = nadya.getGroup(i).name if h == ng: nadya.inviteIntoGroup(i,[Creator]) nadya.sendText(msg.to,"Success Join To ["+ h +"] Group") else: pass else: nadya.sendText(msg.to,"Khusus Nadya") except Exception as e: nadya.sendText(msg.to, str(e)) elif "Leave group: " in msg.text: ng = msg.text.replace("Leave group: ","") gid = nadya.getGroupIdsJoined() if msg.from_ in Creator: for i in gid: h = nadya.getGroup(i).name if h == ng: nadya.sendText(i,"Bot Di Paksa Keluar Oleh Owner!") nadya.leaveGroup(i) nadya.sendText(msg.to,"Success Left ["+ h +"] group") else: pass else: nadya.sendText(msg.to,"Khusus Nadya") elif "Leave all group" == msg.text: gid = nadya.getGroupIdsJoined() if msg.from_ in Creator: for i in gid: nadya.sendText(i,"Bot Di Paksa Keluar Oleh Owner!") nadya.leaveGroup(i) nadya.sendText(msg.to,"Success Leave All Group") else: nadya.sendText(msg.to,"Khusus Nadya") elif "Pict group: " in msg.text: saya = msg.text.replace('Pict group: ','') gid = nadya.getGroupIdsJoined() for i in gid: h = nadya.getGroup(i).name gna = nadya.getGroup(i) if h == saya: nadya.sendImageWithURL(msg.to,"http://dl.profile.line.naver.jp/"+ gna.pictureStatus) elif msg.text in ["cancelall","Cancelall"]: if msg.toType == 2: X = nadya.getGroup(msg.to) if X.invitee is not None: gInviMids = [contact.mid for contact in X.invitee] nadya.cancelGroupInvitation(msg.to, gInviMids) else: nadya.sendText(msg.to,"Tidak Ada Yang Pending") else: nadya.sendText(msg.to,"Tidak Bisa Digunakan Diluar Group") elif msg.text in ["Ourl","Url on"]: if msg.toType == 2: X = nadya.getGroup(msg.to) X.preventJoinByTicket = False nadya.updateGroup(X) nadya.sendText(msg.to,"Url Sudah Aktif") else: nadya.sendText(msg.to,"Can not be used outside the group") elif msg.text in ["Curl","Url off"]: if msg.toType == 2: X = nadya.getGroup(msg.to) X.preventJoinByTicket = True nadya.updateGroup(X) nadya.sendText(msg.to,"Url Sudah Di Nonaktifkan") else: nadya.sendText(msg.to,"Can not be used outside the group") elif msg.text in ["Join on","Autojoin on"]: if msg.from_ in admin: wait["AutoJoin"] = True wait["AutoJoinCancel"] = False nadya.sendText(msg.to,"Auto Join Sudah Aktif") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Join off","Autojoin off"]: if msg.from_ in admin: wait["AutoJoin"] = False nadya.sendText(msg.to,"Auto Join Sudah Di Nonaktifkan") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Joincancel on","Autojoincancel on"]: if msg.from_ in admin: wait["AutoJoinCancel"] = True wait["AutoJoin"] = False nadya.sendText(msg.to,"Auto Join Cancel Sudah Aktif") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Joincancel off","Autojoincancel off"]: if msg.from_ in admin: wait["AutoJoinCancel"] = False nadya.sendText(msg.to,"Auto Join Cancel Sudah Di Nonaktifkan") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Respon1 on"]: if msg.from_ in admin: wait["detectMention"] = True wait["detectMention2"] = False wait["detectMention3"] = False wait["kickMention"] = False nadya.sendText(msg.to,"Auto Respon1 Sudah Aktif") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Respon1 off"]: if msg.from_ in admin: wait["detectMention"] = False nadya.sendText(msg.to,"Auto Respon1 Sudah Off") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Respon2 on"]: if msg.from_ in admin: wait["detectMention"] = False wait["detectMention2"] = True wait["detectMention3"] = False wait["kickMention"] = False nadya.sendText(msg.to,"Auto Respon2 Sudah Aktif") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Respon2 off"]: if msg.from_ in admin: wait["detectMention2"] = False nadya.sendText(msg.to,"Auto Respon2 Sudah Off") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Respon3 on"]: if msg.from_ in admin: wait["detectMention"] = False wait["detectMention2"] = False wait["detectMention3"] = True wait["kickMention"] = False nadya.sendText(msg.to,"Auto Respon3 Sudah Aktif") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Respon3 off"]: if msg.from_ in admin: wait["detectMention3"] = False nadya.sendText(msg.to,"Auto Respon3 Sudah Off") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Responkick on"]: if msg.from_ in admin: wait["kickMention"] = True wait["detectMention"] = False wait["detectMention2"] = False wait["detectMention3"] = False nadya.sendText(msg.to,"Auto Respon Kick Sudah Aktif") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Responkick off"]: if msg.from_ in admin: wait["kickMention"] = False nadya.sendText(msg.to,"Auto Respon Kick Sudah Off") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Autocancel on"]: if msg.from_ in admin: wait["AutoCancel"] = True nadya.sendText(msg.to,"Auto Cancel Sudah Aktif") print wait["AutoCancel"] else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Autocancel off"]: if msg.from_ in admin: wait["AutoCancel"] = False nadya.sendText(msg.to,"Auto Cancel Sudah Di Nonaktifkan") print wait["AutoCancel"] else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Invitepro on"]: if msg.from_ in admin: wait["inviteprotect"] = True nadya.sendText(msg.to,"Invite Protect Sudah Aktif") print wait["inviteprotect"] else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Invitepro off"]: if msg.from_ in admin: wait["inviteprotect"] = False nadya.sendText(msg.to,"Invite Protect Sudah Di Nonaktifkan") print wait["inviteprotect"] else: nadya.sendText(msg.to,"Khusus Nadya") elif "Qr on" in msg.text: if msg.from_ in admin: wait["Qr"] = True nadya.sendText(msg.to,"QR Protect Sudah Aktif") else: nadya.sendText(msg.to,"Khusus Nadya") elif "Qr off" in msg.text: if msg.from_ in admin: wait["Qr"] = False nadya.sendText(msg.to,"Qr Protect Sudah Di Nonaktifkan") else: nadya.sendText(msg.to,"Khusus Nadya") elif "Autokick on" in msg.text: if msg.from_ in admin: wait["AutoKick"] = True nadya.sendText(msg.to,"Auto Kick Sudah Aktif") else: nadya.sendText(msg.to,"Khusus Nadya") elif "Autokick off" in msg.text: if msg.from_ in admin: wait["AutoKick"] = False nadya.sendText(msg.to,"Auto Kick Sudah Di Nonaktifkan") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Allprotect on"]: if msg.from_ in admin: wait["AutoCancel"] = True wait["inviteprotect"] = True wait["AutoKick"] = True wait["Qr"] = True nadya.sendText(msg.to,"All Protect Sudah Aktif Semua") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["Allprotect off"]: if msg.from_ in admin: wait["AutoCancel"] = False wait["inviteprotect"] = False wait["AutoKick"] = False wait["Qr"] = False nadya.sendText(msg.to,"All Protect Sudah Di Nonaktifkan Semua") else: nadya.sendText(msg.to,"Khusus Nadya") elif msg.text in ["K on","Contact on"]: wait["Contact"] = True nadya.sendText(msg.to,"Contact Sudah Aktif") elif msg.text in ["K off","Contact off"]: wait["Contact"] = False nadya.sendText(msg.to,"Contact Sudah Di Nonaktifkan") elif msg.text in ["Alwaysread on"]: wait["alwaysRead"] = True nadya.sendText(msg.to,"Always Read Sudah Aktif") elif msg.text in ["Alwaysread off"]: wait["alwaysRead"] = False nadya.sendText(msg.to,"Always Read Sudah Di Nonaktifkan") elif msg.text in ["Sambutan on"]: if wait["Sambutan"] == True: if wait["lang"] == "JP": nadya.sendText(msg.to,"Sambutan Di Aktifkanヾ(*´∀`*)ノ") else: wait["Sambutan"] = True if wait["lang"] == "JP": nadya.sendText(msg.to,"Sudah Onヽ(´▽`)/") elif msg.text in ["Sambutan off"]: if wait["Sambutan"] == False: if wait["lang"] == "JP": nadya.sendText(msg.to,"Sambutan Di Nonaktifkan( ^∇^)") else: wait["Sambutan"] = False if wait["lang"] == "JP": nadya.sendText(msg.to,"Sudah Off(p′︵‵。)") elif "Sider on" in msg.text: try: del cctv['point'][msg.to] del cctv['sidermem'][msg.to] del cctv['cyduk'][msg.to] except: pass cctv['point'][msg.to] = msg.id cctv['sidermem'][msg.to] = "" cctv['cyduk'][msg.to]=True wait["Sider"] = True nadya.sendText(msg.to,"Siap On Cek Sider") elif "Sider off" in msg.text: if msg.to in cctv['point']: cctv['cyduk'][msg.to]=False wait["Sider"] = False nadya.sendText(msg.to, "Cek Sider Off") else: nadya.sendText(msg.to, "Heh Belom Di Set") elif msg.text in ["Status"]: md = "" if wait["Sambutan"] == True: md+="╠➩✔️ Sambutan : On\n" else:md+="╠➩❌ Sambutan : Off\n" if wait["AutoJoin"] == True: md+="╠➩✔️ Auto Join : On\n" else: md +="╠➩❌ Auto Join : Off\n" if wait["AutoJoinCancel"] == True: md+="╠➩✔️ Auto Join Cancel : On\n" else: md +="╠➩❌ Auto Join Cancel : Off\n" if wait["Contact"] == True: md+="╠➩✔️ Info Contact : On\n" else: md+="╠➩❌ Info Contact : Off\n" if wait["AutoCancel"] == True:md+="╠➩✔️ Auto Cancel : On\n" else: md+= "╠➩❌ Auto Cancel : Off\n" if wait["inviteprotect"] == True:md+="╠➩✔️ Invite Protect : On\n" else: md+= "╠➩❌ Invite Protect : Off\n" if wait["Qr"] == True: md+="╠➩✔️ Qr Protect : On\n" else:md+="╠➩❌ Qr Protect : Off\n" if wait["AutoKick"] == True: md+="╠➩✔️ Auto Kick : On\n" else:md+="╠➩❌ Auto Kick : Off\n" if wait["alwaysRead"] == True: md+="╠➩✔️ Always Read : On\n" else:md+="╠➩❌ Always Read: Off\n" if wait["detectMention"] == True: md+="╠➩✔️ Auto Respon1 : On\n" else:md+="╠➩❌ Auto Respon1 : Off\n" if wait["detectMention2"] == True: md+="╠➩✔️ Auto Respon2 : On\n" else:md+="╠➩❌ Auto Respon2 : Off\n" if wait["detectMention3"] == True: md+="╠➩✔️ Auto Respon3 : On\n" else:md+="╠➩❌ Auto Respon3 : Off\n" if wait["kickMention"] == True: md+="╠➩✔️ Auto Respon Kick : On\n" else:md+="╠➩❌ Auto Respon Kick : Off\n" if wait["Sider"] == True: md+="╠➩✔️ Auto Sider : On\n" else:md+="╠➩❌ Auto Sider: Off\n" if wait["Simi"] == True: md+="╠➩✔️ Simisimi : On\n" else:md+="╠➩❌ Simisimi: Off\n" nadya.sendText(msg.to,"╔════════════════════\n""║ ☆☞ F R Y A N T S T A T U S ☜☆\n""╠════════════════════\n"+md+"╚════════════════════") elif msg.text in ["Gift","gift"]: msg.contentType = 9 msg.contentMetadata={'PRDID': 'a0768339-c2d3-4189-9653-2909e9bb6f58', 'PRDTYPE': 'THEME', 'MSGTPL': '5'} msg.text = None nadya.sendMessage(msg) elif "Gift1 " in msg.text: msg.contentType = 13 nk0 = msg.text.replace("Gift1 ","") nk1 = nk0.lstrip() nk2 = nk1.replace("@","") nk3 = nk2.rstrip() _name = nk3 gs = nadya.getGroup(msg.to) targets = [] for s in gs.members: if _name in s.displayName: targets.append(s.mid) if targets == []: sendMessage(msg.to,"user does not exist") pass else: for target in targets: try: nadya.sendText(msg.to,_name + " Check Your Gift") msg.contentType = 9 msg.contentMetadata= {'PRDTYPE': 'STICKER', 'STKVER': '1', 'MSGTPL': '1', 'STKPKGID': '1380280'} msg.to = target msg.text = None nadya.sendMessage(msg) except: msg.contentMetadata = {'mid': target} elif "Gift2 " in msg.text: msg.contentType = 13 nk0 = msg.text.replace("Gift2 ","") nk1 = nk0.lstrip() nk2 = nk1.replace("@","") nk3 = nk2.rstrip() _name = nk3 gs = nadya.getGroup(msg.to) targets = [] for s in gs.members: if _name in s.displayName: targets.append(s.mid) if targets == []: sendMessage(msg.to,"user does not exist") pass else: for target in targets: try: nadya.sendText(msg.to,_name + " Check Your Gift") msg.contentType = 9 msg.contentMetadata= {'PRDTYPE': 'STICKER', 'STKVER': '1', 'MSGTPL': '2', 'STKPKGID': '1360738'} msg.to = target msg.text = None nadya.sendMessage(msg) except: msg.contentMetadata = {'mid': target} elif "Gift3 " in msg.text: msg.contentType = 13 nk0 = msg.text.replace("Gift3 ","") nk1 = nk0.lstrip() nk2 = nk1.replace("@","") nk3 = nk2.rstrip() _name = nk3 gs = nadya.getGroup(msg.to) targets = [] for s in gs.members: if _name in s.displayName: targets.append(s.mid) if targets == []: sendMessage(msg.to,"user does not exist") pass else: for target in targets: try: nadya.sendText(msg.to,_name + " Check Your Gift") msg.contentType = 9 msg.contentMetadata= {'PRDTYPE': 'STICKER', 'STKVER': '1', 'MSGTPL': '3', 'STKPKGID': '1395389'} msg.to = target msg.text = None nadya.sendMessage(msg) except: msg.contentMetadata = {'mid': target} elif "Gift4 " in msg.text: msg.contentType = 13 nk0 = msg.text.replace("Gift4 ","") nk1 = nk0.lstrip() nk2 = nk1.replace("@","") nk3 = nk2.rstrip() _name = nk3 gs = nadya.getGroup(msg.to) targets = [] for s in gs.members: if _name in s.displayName: targets.append(s.mid) if targets == []: sendMessage(msg.to,"user does not exist") pass else: for target in targets: try: nadya.sendText(msg.to,_name + " Check Your Gift") msg.contentType = 9 msg.contentMetadata= {'PRDTYPE': 'STICKER', 'STKVER': '1', 'MSGTPL': '4', 'STKPKGID': '1329191'} msg.to = target msg.text = None nadya.sendMessage(msg) except: msg.contentMetadata = {'mid': target} elif "Gift5 " in msg.text: msg.contentType = 13 nk0 = msg.text.replace("Gift5 ","") nk1 = nk0.lstrip() nk2 = nk1.replace("@","") nk3 = nk2.rstrip() _name = nk3 gs = nadya.getGroup(msg.to) targets = [] for s in gs.members: if _name in s.displayName: targets.append(s.mid) if targets == []: sendMessage(msg.to,"user does not exist") pass else: for target in targets: try: nadya.sendText(msg.to,_name + " Check Your Gift") msg.contentType = 9 msg.contentMetadata= {'PRDTYPE': 'STICKER', 'STKVER': '1', 'MSGTPL': '1', 'STKPKGID': '9057'} msg.to = target msg.text = None nadya.sendMessage(msg) except: msg.contentMetadata = {'mid': target} elif "Gift6 " in msg.text: msg.contentType = 13 nk0 = msg.text.replace("Gift6 ","") nk1 = nk0.lstrip() nk2 = nk1.replace("@","") nk3 = nk2.rstrip() _name = nk3 gs = nadya.getGroup(msg.to) targets = [] for s in gs.members: if _name in s.displayName: targets.append(s.mid) if targets == []: sendMessage(msg.to,"user does not exist") pass else: for target in targets: try: nadya.sendText(msg.to,_name + " Check Your Gift") msg.contentType = 9 msg.contentMetadata= {'PRDTYPE': 'STICKER', 'STKVER': '1', 'MSGTPL': '2', 'STKPKGID': '9167'} msg.to = target msg.text = None nadya.sendMessage(msg) except: msg.contentMetadata = {'mid': target} elif "Gift7 " in msg.text: msg.contentType = 13 nk0 = msg.text.replace("Gift7 ","") nk1 = nk0.lstrip() nk2 = nk1.replace("@","") nk3 = nk2.rstrip() _name = nk3 gs = nadya.getGroup(msg.to) targets = [] for s in gs.members: if _name in s.displayName: targets.append(s.mid) if targets == []: sendMessage(msg.to,"user does not exist") pass else: for target in targets: try: nadya.sendText(msg.to,_name + " Check Your Gift") msg.contentType = 9 msg.contentMetadata= {'PRDTYPE': 'STICKER', 'STKVER': '1', 'MSGTPL': '3', 'STKPKGID': '7334'} msg.to = target msg.text = None nadya.sendMessage(msg) except: msg.contentMetadata = {'mid': target} elif "Gift8 " in msg.text: msg.contentType = 13 nk0 = msg.text.replace("Gift8 ","") nk1 = nk0.lstrip() nk2 = nk1.replace("@","") nk3 = nk2.rstrip() _name = nk3 gs = nadya.getGroup(msg.to) targets = [] for s in gs.members: if _name in s.displayName: targets.append(s.mid) if targets == []: sendMessage(msg.to,"user does not exist") pass else: for target in targets: try: nadya.sendText(msg.to,_name + " Check Your Gift") msg.contentType = 9 msg.contentMetadata= {'PRDTYPE': 'STICKER', 'STKVER': '1', 'MSGTPL': '1', 'STKPKGID': '1380280'} msg.to = target msg.text = None nadya.sendMessage(msg) except: msg.contentMetadata = {'mid': target} elif "Gift9 " in msg.text: msg.contentType = 13 nk0 = msg.text.replace("Gift9 ","") nk1 = nk0.lstrip() nk2 = nk1.replace("@","") nk3 = nk2.rstrip() _name = nk3 gs = nadya.getGroup(msg.to) targets = [] for s in gs.members: if _name in s.displayName: targets.append(s.mid) if targets == []: sendMessage(msg.to,"user does not exist") pass else: for target in targets: try: nadya.sendText(msg.to,_name + " Check Your Gift") msg.contentType = 9 msg.contentMetadata= {'PRDTYPE': 'STICKER', 'STKVER': '1', 'MSGTPL': '4', 'STKPKGID': '1405277'} msg.to = target msg.text = None nadya.sendMessage(msg) except: msg.contentMetadata = {'mid': target} elif "Gift10 " in msg.text: msg.contentType = 13 nk0 = msg.text.replace("Gift10 ","") nk1 = nk0.lstrip() nk2 = nk1.replace("@","") nk3 = nk2.rstrip() _name = nk3 gs = nadya.getGroup(msg.to) targets = [] for s in gs.members: if _name in s.displayName: targets.append(s.mid) if targets == []: sendMessage(msg.to,"user does not exist") pass else: for target in targets: try: nadya.sendText(msg.to,_name + " Check Your Gift") msg.contentType = 9 msg.contentMetadata= {'PRDTYPE': 'STICKER', 'STKVER': '1', 'MSGTPL': '1', 'STKPKGID': '1296261'} msg.to = target msg.text = None nadya.sendMessage(msg) except: msg.contentMetadata = {'mid': target} elif msg.text.lower() in ["wkwkwk","wkwk","hahaha","haha"]: msg.contentType = 7 msg.contentMetadata={'STKID': '100', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["hehehe","hehe"]: msg.contentType = 7 msg.contentMetadata={'STKID': '10', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["galau"]: msg.contentType = 7 msg.contentMetadata={'STKID': '9', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["you","kau","kamu"]: msg.contentType = 7 msg.contentMetadata={'STKID': '7', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["marah","hadeuh","hadeh"]: msg.contentType = 7 msg.contentMetadata={'STKID': '6', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["please","pliss","mohon","tolong"]: msg.contentType = 7 msg.contentMetadata={'STKID': '4', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["haa","haaa","kaget"]: msg.contentType = 7 msg.contentMetadata={'STKID': '3', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["lucu","ngakak","lol"]: msg.contentType = 7 msg.contentMetadata={'STKID': '110', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["hmm","hmmm"]: msg.contentType = 7 msg.contentMetadata={'STKID': '101', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["tidur"]: msg.contentType = 7 msg.contentMetadata={'STKID': '1', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["gemes"]: msg.contentType = 7 msg.contentMetadata={'STKID': '2', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["cantik","imut"]: msg.contentType = 7 msg.contentMetadata={'STKID': '5', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["nyanyi","lalala"]: msg.contentType = 7 msg.contentMetadata={'STKID': '11', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["gugup"]: msg.contentType = 7 msg.contentMetadata={'STKID': '8', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["ok","oke","okay","oce","okee","sip","siph"]: msg.contentType = 7 msg.contentMetadata={'STKID': '13', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["mantab","mantap","nice","keren"]: msg.contentType = 7 msg.contentMetadata={'STKID': '14', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["ngejek"]: msg.contentType = 7 msg.contentMetadata={'STKID': '15', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["nangis","sedih"]: msg.contentType = 7 msg.contentMetadata={'STKID': '16', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["woi","kampret"]: msg.contentType = 7 msg.contentMetadata={'STKID': '102', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif msg.text.lower() in ["huft"]: msg.contentType = 7 msg.contentMetadata={'STKID': '104', 'STKPKGID': '1', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif "tag all" == msg.text.lower(): group = nadya.getGroup(msg.to) nama = [contact.mid for contact in group.members] nm1, nm2, nm3, nm4, nm5, jml = [], [], [], [], [], len(nama) if jml <= 100: summon(msg.to, nama) if jml > 100 and jml < 200: for i in range(0, 99): nm1 += [nama[i]] summon(msg.to, nm1) for j in range(100, len(nama)-1): nm2 += [nama[j]] summon(msg.to, nm2) if jml > 200 and jml < 300: for i in range(0, 99): nm1 += [nama[i]] summon(msg.to, nm1) for j in range(100, 199): nm2 += [nama[j]] summon(msg.to, nm2) for k in range(200, len(nama)-1): nm3 += [nama[k]] summon(msg.to, nm3) if jml > 300 and jml < 400: for i in range(0, 99): nm1 += [nama[i]] summon(msg.to, nm1) for j in range(100, 199): nm2 += [nama[j]] summon(msg.to, nm2) for k in range(200, 299): nm3 += [nama[k]] summon(msg.to, nm3) for l in range(300, len(nama)-1): nm4 += [nama[l]] summon(msg.to, nm4) if jml > 400 and jml < 500: for i in range(0, 99): nm1 += [nama[i]] summon(msg.to, nm1) for j in range(100, 199): nm2 += [nama[j]] summon(msg.to, nm2) for k in range(200, 299): nm3 += [nama[k]] summon(msg.to, nm3) for l in range(300, 399): nm4 += [nama[l]] summon(msg.to, nm4) for m in range(400, len(nama)-1): nm5 += [nama[m]] summon(msg.to, nm5) if jml > 500: print "Terlalu Banyak Men 500+" cnt = Message() cnt.text = "Jumlah:\n" + str(jml) + " Members" cnt.to = msg.to nadya.sendMessage(cnt) elif "tagall" == msg.text.lower(): group = nadya.getGroup(msg.to) nama = [contact.mid for contact in group.members] nm1, nm2, nm3, nm4, nm5, jml = [], [], [], [], [], len(nama) if jml <= 100: summon(msg.to, nama) if jml > 100 and jml < 200: for i in range(0, 99): nm1 += [nama[i]] summon(msg.to, nm1) for j in range(100, len(nama)-1): nm2 += [nama[j]] summon(msg.to, nm2) if jml > 200 and jml < 300: for i in range(0, 99): nm1 += [nama[i]] summon(msg.to, nm1) for j in range(100, 199): nm2 += [nama[j]] summon(msg.to, nm2) for k in range(200, len(nama)-1): nm3 += [nama[k]] summon(msg.to, nm3) if jml > 300 and jml < 400: for i in range(0, 99): nm1 += [nama[i]] summon(msg.to, nm1) for j in range(100, 199): nm2 += [nama[j]] summon(msg.to, nm2) for k in range(200, 299): nm3 += [nama[k]] summon(msg.to, nm3) for l in range(300, len(nama)-1): nm4 += [nama[l]] summon(msg.to, nm4) if jml > 400 and jml < 500: for i in range(0, 99): nm1 += [nama[i]] summon(msg.to, nm1) for j in range(100, 199): nm2 += [nama[j]] summon(msg.to, nm2) for k in range(200, 299): nm3 += [nama[k]] summon(msg.to, nm3) for l in range(300, 399): nm4 += [nama[l]] summon(msg.to, nm4) for m in range(400, len(nama)-1): nm5 += [nama[m]] summon(msg.to, nm5) if jml > 500: print "Terlalu Banyak Men 500+" cnt = Message() cnt.text = "Jumlah:\n" + str(jml) + " Members" cnt.to = msg.to nadya.sendMessage(cnt) elif msg.text in ["Setview","Setpoint","Cctv"]: subprocess.Popen("echo '' > dataSeen/"+msg.to+".txt", shell=True, stdout=subprocess.PIPE) nadya.sendText(msg.to, "☆Checkpoint Checked☆") print "Setview" elif msg.text in ["Viewseen","Check","Ciduk","Cyduk"]: lurkGroup = "" dataResult, timeSeen, contacts, userList, timelist, recheckData = [], [], [], [], [], [] with open('dataSeen/'+msg.to+'.txt','r') as rr: contactArr = rr.readlines() for v in xrange(len(contactArr) -1,0,-1): num = re.sub(r'\n', "", contactArr[v]) contacts.append(num) pass contacts = list(set(contacts)) for z in range(len(contacts)): arg = contacts[z].split('|') userList.append(arg[0]) timelist.append(arg[1]) uL = list(set(userList)) for ll in range(len(uL)): try: getIndexUser = userList.index(uL[ll]) timeSeen.append(time.strftime("%H:%M:%S", time.localtime(int(timelist[getIndexUser]) / 1000))) recheckData.append(userList[getIndexUser]) except IndexError: conName.append('nones') pass contactId = nadya.getContacts(recheckData) for v in range(len(recheckData)): dataResult.append(contactId[v].displayName + ' ('+timeSeen[v]+')') pass if len(dataResult) > 0: tukang = "╔═════════════════════════\n║ ☆☞ LIST VIEWERS ☜☆\n╠═════════════════════════\n╠➩" grp = '\n╠➩ '.join(str(f) for f in dataResult) total = '\n╠═════════════════════════\n╠➩ Total %i Viewers (%s)' % (len(dataResult), datetime.now().strftime('%H:%M:%S')) + "\n╚═════════════════════════" nadya.sendText(msg.to, "%s %s %s" % (tukang, grp, total)) subprocess.Popen("echo '' > dataSeen/"+msg.to+".txt", shell=True, stdout=subprocess.PIPE) nadya.sendText(msg.to, "☆Auto Checkpoint☆") else: nadya.sendText(msg.to, "☆Belum Ada Viewers☆") print "Viewseen" elif "Kick " in msg.text: if msg.from_ in admin: if 'MENTION' in msg.contentMetadata.keys()!= None: names = re.findall(r'@(\w+)', msg.text) mention = ast.literal_eval(msg.contentMetadata['MENTION']) mentionees = mention['MENTIONEES'] print mentionees for mention in mentionees: nadya.kickoutFromGroup(msg.to,[mention['M']]) elif "Set member: " in msg.text: if msg.from_ in admin: jml = msg.text.replace("Set member: ","") wait["Members"] = int(jml) nadya.sendText(msg.to, "Jumlah minimal member telah di set : "+jml) elif "Add all" in msg.text: thisgroup = nadya.getGroups([msg.to]) Mids = [contact.mid for contact in thisgroup[0].members] mi_d = Mids[:33] nadya.findAndAddContactsByMids(mi_d) nadya.sendText(msg.to,"Success Add all") elif msg.text in ["Invite"]: wait["invite"] = True nadya.sendText(msg.to,"Send Contact") elif msg.text in ["Auto like"]: wait["likeOn"] = True nadya.sendText(msg.to,"Shere Post Kamu Yang Mau Di Like!") elif msg.text in ["Steal contact"]: wait["steal"] = True nadya.sendText(msg.to,"Send Contact") elif msg.text in ["Giftbycontact"]: wait["gift"] = True nadya.sendText(msg.to,"Send Contact") elif msg.text in ["Copycontact"]: wait["copy"] = True nadya.sendText(msg.to,"Send Contact") elif msg.text in ["Sticker on"]: wait["sticker"] = True nadya.sendText(msg.to,"Sticker ID Detect Already On.") elif msg.text in ["Bot off"]: wait["Bot"] = False nadya.sendText(msg.to,"Bot Sudah Di Nonaktifkan.") elif "Recover" in msg.text: thisgroup = nadya.getGroups([msg.to]) Mids = [contact.mid for contact in thisgroup[0].members] mi_d = Mids[:33] nadya.createGroup("Recover", mi_d) nadya.sendText(msg.to,"Success recover") elif ("Gn: " in msg.text): if msg.toType == 2: X = nadya.getGroup(msg.to) X.name = msg.text.replace("Gn: ","") nadya.updateGroup(X) else: nadya.sendText(msg.to,"It can't be used besides the group.") elif "Kick: " in msg.text: midd = msg.text.replace("Kick: ","") if midd not in admin: nadya.kickoutFromGroup(msg.to,[midd]) else: nadya.sendText(msg.to,"Admin Detected") elif "Invite: " in msg.text: midd = msg.text.replace("Invite: ","") nadya.findAndAddContactsByMid(midd) nadya.inviteIntoGroup(msg.to,[midd]) elif "Invite creator" in msg.text: midd = "u14f64e139a3817afaabe27d237afb36b" nadya.inviteIntoGroup(msg.to,[midd]) elif msg.text in ["Welcome","welcome","Welkam","welkam","Wc","wc"]: gs = nadya.getGroup(msg.to) nadya.sendText(msg.to,"Selamat Datang Di "+ gs.name) msg.contentType = 7 msg.contentMetadata={'STKID': '247', 'STKPKGID': '3', 'STKVER': '100'} msg.text = None nadya.sendMessage(msg) elif "Bc: " in msg.text: bc = msg.text.replace("Bc: ","") gid = nadya.getGroupIdsJoined() if msg.from_ in Creator: for i in gid: nadya.sendText(i,"=======[BROADCAST]=======\n\n"+bc+"\n\nContact Me : line.me/ti/p/~nad_nad.") nadya.sendText(msg.to,"Success BC BosQ") else: nadya.sendText(msg.to,"Khusus Admin") elif msg.text in ["Cancel"]: gid = nadya.getGroupIdsInvited() for i in gid: nadya.rejectGroupInvitation(i) nadya.sendText(msg.to,"All invitations have been refused") elif msg.text in ["Gurl"]: if msg.toType == 2: x = nadya.getGroup(msg.to) if x.preventJoinByTicket == True: x.preventJoinByTicket = False nadya.updateGroup(x) gurl = nadya.reissueGroupTicket(msg.to) nadya.sendText(msg.to,"line://ti/g/" + gurl) else: if wait["lang"] == "JP": nadya.sendText(msg.to,"Can't be used outside the group") else: nadya.sendText(msg.to,"Not for use less than group") elif msg.text in ["timeline"]: try: url = nadya.activity(limit=5) nadya.sendText(msg.to,url['result']['posts'][0]['postInfo']['postId']) except Exception as E: print E elif msg.text in ["@bye","@Bye"]: nadya.leaveGroup(msg.to) elif msg.text in ["Absen"]: nadya.sendText(msg.to,"Hadir!!") elif msg.text.lower() in ["respon"]: nadya.sendText(msg.to,responsename) elif msg.text in ["Sp","Speed","speed"]: start = time.time() print("Speed") elapsed_time = time.time() - start nadya.sendText(msg.to, "Progress...") nadya.sendText(msg.to, "%sseconds" % (elapsed_time)) elif msg.text in ["Speed test"]: start = time.time() nadya.sendText(msg.to, "Progress...") elapsed_time = time.time() - start nadya.sendText(msg.to, "%sseconds" % (elapsed_time)) elif msg.text in ["Ban"]: if msg.from_ in admin: wait["wblacklist"] = True nadya.sendText(msg.to,"send contact") elif msg.text in ["Unban"]: if msg.from_ in admin: wait["dblacklist"] = True nadya.sendText(msg.to,"send contact") elif "Ban @" in msg.text: if msg.from_ in admin: if msg.toType == 2: print "@Ban by mention" _name = msg.text.replace("Ban @","") _nametarget = _name.rstrip(' ') gs = nadya.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: nadya.sendText(msg.to,"Not found") else: for target in targets: if target not in admin: try: wait["blacklist"][target] = True f=codecs.open('st2__b.json','w','utf-8') json.dump(wait["blacklist"], f, sort_keys=True, indent=4,ensure_ascii=False) nadya.sendText(msg.to,"Succes BosQ") except: nadya.sendText(msg.to,"Error") else: nadya.sendText(msg.to,"Admin Detected~") elif msg.text in ["Banlist","Ban list"]: if msg.from_ in admin: if wait["blacklist"] == {}: nadya.sendText(msg.to,"Tidak Ada") else: mc = "" for mi_d in wait["blacklist"]: mc += "->" +nadya.getContact(mi_d).displayName + "\n" nadya.sendText(msg.to,"===[Blacklist User]===\n"+mc) elif "Unban @" in msg.text: if msg.toType == 2: print "@Unban by mention" if msg.from_ in admin: _name = msg.text.replace("Unban @","") _nametarget = _name.rstrip(' ') gs = nadya.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: nadya.sendText(msg.to,"Not found") else: for target in targets: try: del wait["blacklist"][target] f=codecs.open('st2__b.json','w','utf-8') json.dump(wait["blacklist"], f, sort_keys=True, indent=4,ensure_ascii=False) nadya.sendText(msg.to,"Succes BosQ") except: nadya.sendText(msg.to,"Succes BosQ") elif msg.text.lower() == 'clear ban': if msg.from_ in admin: wait["blacklist"] = {} nadya.sendText(msg.to,"ヽ( ^ω^)ノ└ ❉Unbanned All Success❉ ┐") elif msg.text in ["Kill ban"]: if msg.from_ in admin: if msg.toType == 2: group = nadya.getGroup(msg.to) gMembMids = [contact.mid for contact in group.members] matched_list = [] for tag in wait["blacklist"]: matched_list+=filter(lambda str: str == tag, gMembMids) if matched_list == []: nadya.sendText(msg.to,"There was no blacklist user") return for jj in matched_list: nadya.kickoutFromGroup(msg.to,[jj]) nadya.sendText(msg.to,"Blacklist emang pantas tuk di usir") else: nadya.sendText(msg.to, "Khusus creator") elif msg.text in ["Kill"]: if msg.toType == 2: if msg.from_ in admin: group = nadya.getGroup(msg.to) gMembMids = [contact.mid for contact in group.members] matched_list = [] for tag in wait["blacklist"]: matched_list+=filter(lambda str: str == tag, gMembMids) if matched_list == []: nadya.sendText(msg.to,"Fuck You") return for jj in matched_list: try: nadya.kickoutFromGroup(msg.to,[jj]) print (msg.to,[jj]) except: pass elif "Kickall" == msg.text: if msg.from_ in Creator: if msg.toType == 2: print "Kick all member" _name = msg.text.replace("Kickall","") gs = nadya.getGroup(msg.to) nadya.sendText(msg.to,"Dadaaah~") targets = [] for g in gs.members: if _name in g.displayName: targets.append(g.mid) if targets == []: nadya.sendText(msg.to,"Not found.") else: for target in targets: if target not in admin: try: nadya.kickoutFromGroup(msg.to,[target]) print (msg.to,[g.mid]) except Exception as e: nadya.sendText(msg.to,str(e)) nadya.inviteIntoGroup(msg.to, targets) elif msg.text in ["Bot restart","Reboot"]: if msg.from_ in Creator: nadya.sendText(msg.to, "Bot Has Been Restarted...") restart_program() print "@Restart" else: nadya.sendText(msg.to, "No Access") elif msg.text in ["Turn off"]: if msg.from_ in Creator: try: import sys sys.exit() except: pass elif 'Crash' in msg.text: if msg.from_ in Creator: msg.contentType = 13 msg.contentMetadata = {'mid': "NADYA,'"} nadya.sendMessage(msg) elif "Mycopy @" in msg.text: print "[COPY] Ok" _name = msg.text.replace("Mycopy @","") _nametarget = _name.rstrip(' ') gs = nadya.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: nadya.sendText(msg.to, "Not Found...") else: for target in targets: try: nadya.CloneContactProfile(target) nadya.sendText(msg.to, "Copied (^_^)") except Exception as e: print e elif msg.text in ["Mybackup"]: try: nadya.updateDisplayPicture(backup1.pictureStatus) nadya.updateProfile(backup1) nadya.sendText(msg.to, "Done (^_^)") except Exception as e: nadya.sendText(msg.to, str(e)) elif "/musik " in msg.text: songname = msg.text.replace("/musik ","") params = {"songname": songname} r = requests.get('http://ide.fdlrcn.com/workspace/yumi-apis/joox?' + urllib.urlencode(params)) data = r.text data = json.loads(data) for song in data: abc = song[3].replace('https://','http://') nadya.sendText(msg.to, "Title : " + song[0] + "\nLength : " + song[1] + "\nLink download : " + song[4]) nadya.sendText(msg.to, "Lagu " + song[0] + "\nSedang Di Prosses... Tunggu Sebentar ^_^ ") nadya.sendAudioWithURL(msg.to,abc) nadya.sendText(msg.to, "Selamat Mendengarkan Lagu " + song[0]) elif '/lirik ' in msg.text.lower(): try: songname = msg.text.lower().replace('/lirik ','') params = {'songname': songname} r = requests.get('http://ide.fdlrcn.com/workspace/yumi-apis/joox?' + urllib.urlencode(params)) data = r.text data = json.loads(data) for song in data: hasil = 'Lyric Lagu (' hasil += song[0] hasil += ')\n\n' hasil += song[5] nadya.sendText(msg.to, hasil) except Exception as wak: nadya.sendText(msg.to, str(wak)) elif "/musrik " in msg.text: songname = msg.text.replace("/musrik ","") params = {"songname": songname} r = requests.get('http://ide.fdlrcn.com/workspace/yumi-apis/joox?' + urllib.urlencode(params)) data = r.text data = json.loads(data) for song in data: abc = song[3].replace('https://','http://') hasil = 'Lyric Lagu (' hasil += song[0] hasil += ')\n\n' hasil += song[5] nadya.sendText(msg.to, "Lagu " + song[0] + "\nSedang Di Prosses... Tunggu Sebentar ^_^ ") nadya.sendAudioWithURL(msg.to,abc) nadya.sendText(msg.to, "Title : " + song[0] + "\nLength : " + song[1] + "\nLink download : " + song[4] +"\n\n" + hasil) nadya.sendText(msg.to, "Selamat Mendengarkan Lagu " + song[0]) elif "Fancytext: " in msg.text: txt = msg.text.replace("Fancytext: ", "") nadya.kedapkedip(msg.to,txt) print "[Command] Kedapkedip" elif "cover @" in msg.text: if msg.toType == 2: cover = msg.text.replace("cover @","") _nametarget = cover.rstrip(' ') gs = nadya.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: nadya.sendText(msg.to,"Not found") else: for target in targets: try: h = nadya.channel.getHome(target) objId = h["result"]["homeInfo"]["objectId"] nadya.sendImageWithURL(msg.to,"http://dl.profile.line-cdn.net/myhome/c/download.nhn?userid=" + target + "&oid=" + objId) except Exception as error: print error nadya.sendText(msg.to,"Upload image failed.") elif "Cover @" in msg.text: if msg.toType == 2: cover = msg.text.replace("Cover @","") _nametarget = cover.rstrip(' ') gs = nadya.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: nadya.sendText(msg.to,"Not found") else: for target in targets: try: h = nadya.channel.getHome(target) objId = h["result"]["homeInfo"]["objectId"] nadya.sendImageWithURL(msg.to,"http://dl.profile.line-cdn.net/myhome/c/download.nhn?userid=" + target + "&oid=" + objId) except Exception as error: print error nadya.sendText(msg.to,"Upload image failed.") elif "Cpp" in msg.text: if msg.from_ in admin: path = "nadya.jpg" nadya.sendText(msg.to,"Update PP :") nadya.sendImage(msg.to,path) nadya.updateProfilePicture(path) elif "pp @" in msg.text: if msg.toType == 2: cover = msg.text.replace("pp @","") _nametarget = cover.rstrip(' ') gs = nadya.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: nadya.sendText(msg.to,"Not found") else: for target in targets: try: h = nadya.getContact(target) nadya.sendImageWithURL(msg.to,"http://dl.profile.line-cdn.net/" + h.pictureStatus) except Exception as error: print error nadya.sendText(msg.to,"Upload image failed.") elif "Pp @" in msg.text: if msg.toType == 2: cover = msg.text.replace("Pp @","") _nametarget = cover.rstrip(' ') gs = nadya.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: nadya.sendText(msg.to,"Not found") else: for target in targets: try: h = nadya.getContact(target) nadya.sendImageWithURL(msg.to,"http://dl.profile.line-cdn.net/" + h.pictureStatus) except Exception as error: print error nadya.sendText(msg.to,"Upload image failed.") elif msg.text.lower() in ["pap owner","pap creator"]: link = ["http://dl.profile.line-cdn.net/0hNPsZWL9WEX9OIz0lhyFuKHJmHxI5DRc3NkJaETwkRklqGwQoJkNbTGklHRo2G1B7cxFXH2NxSU03"] pilih = random.choice(link) nadya.sendImageWithURL(msg.to,pilih) elif "Spam: " in msg.text: bctxt = msg.text.replace("Spam: ", "") t = 10 while(t): nadya.sendText(msg.to, (bctxt)) t-=1 elif "Scbc " in msg.text: bctxt = msg.text.replace("Scbc ", "") orang = nadya.getAllContactIds() t = 20 for manusia in orang: while(t): nadya.sendText(manusia, (bctxt)) t-=1 elif "Cbc " in msg.text: broadcasttxt = msg.text.replace("Cbc ", "") orang = nadya.getAllContactIds() for manusia in orang: nadya.sendText(manusia, (broadcasttxt)) elif '/ig ' in msg.text.lower(): try: instagram = msg.text.lower().replace("/ig ","") html = requests.get('https://www.instagram.com/' + instagram + '/?') soup = BeautifulSoup(html.text, 'html.parser') data = soup.find_all('meta', attrs={'property':'og:description'}) text = data[0].get('content').split() data1 = soup.find_all('meta', attrs={'property':'og:image'}) text1 = data1[0].get('content').split() tj = text1[0].replace("s150x150/","") user = "Name: " + text[-2] + "\n" user1 = "Username: " + text[-1] + "\n" followers = "Followers: " + text[0] + "\n" following = "Following: " + text[2] + "\n" post = "Post: " + text[4] + "\n" link = "Link: " + "https://www.instagram.com/" + instagram detail = "========INSTAGRAM INFO ========\n" details = "\n========INSTAGRAM INFO ========" nadya.sendText(msg.to, detail + user + user1 + followers + following + post + link + details) nadya.sendImageWithURL(msg.to, tj) except Exception as njer: nadya.sendText(msg.to, str(njer)) elif "Checkig " in msg.text: separate = msg.text.split(" ") user = msg.text.replace(separate[0] + " ","") if user.startswith("@"): user = user.replace("@","") profile = "https://www.instagram.com/" + user with requests.session() as x: x.headers['user-agent'] = 'Mozilla/5.0' end_cursor = '' for count in range(1, 999): print('PAGE: ', count) r = x.get(profile, params={'max_id': end_cursor}) data = re.search(r'window._sharedData = (\{.+?});</script>', r.text).group(1) j = json.loads(data) for node in j['entry_data']['ProfilePage'][0]['user']['media']['nodes']: if node['is_video']: page = 'https://www.instagram.com/p/' + node['code'] r = x.get(page) url = re.search(r'"video_url": "([^"]+)"', r.text).group(1) print(url) nadya.sendVideoWithURL(msg.to,url) else: print (node['display_src']) nadya.sendImageWithURL(msg.to,node['display_src']) end_cursor = re.search(r'"end_cursor": "([^"]+)"', r.text).group(1) elif 'Youtubelink: ' in msg.text: try: textToSearch = (msg.text).replace('Youtube ', "").strip() query = urllib.quote(textToSearch) url = "https://www.youtube.com/results?search_query=" + query response = urllib2.urlopen(url) html = response.read() soup = BeautifulSoup(html, "html.parser") results = soup.find(attrs={'class':'yt-uix-tile-link'}) nadya.sendText(msg.to,'https://www.youtube.com' + results['href']) except: nadya.sendText(msg.to,"Could not find it") elif 'Youtubevideo: ' in msg.text: try: textToSearch = (msg.text).replace('Youtubevideo: ', "").strip() query = urllib.quote(textToSearch) url = "https://www.youtube.com/results?search_query=" + query response = urllib2.urlopen(url) html = response.read() soup = BeautifulSoup(html, "html.parser") results = soup.find(attrs={'class': 'yt-uix-tile-link'}) nadya.sendVideoWithURL(msg.to,'https://www.youtube.com' + results['href']) except: nadya.sendText(msg.to, "Could not find it") elif "Say-id " in msg.text: say = msg.text.replace("Say-id ","") lang = 'id' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") nadya.sendAudio(msg.to,"hasil.mp3") elif "Say-en " in msg.text: say = msg.text.replace("Say-en ","") lang = 'en' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") nadya.sendAudio(msg.to,"hasil.mp3") elif "Say-jp " in msg.text: say = msg.text.replace("Say-jp ","") lang = 'ja' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") nadya.sendAudio(msg.to,"hasil.mp3") elif "Say welcome" in msg.text: gs = nadya.getGroup(msg.to) say = msg.text.replace("Say welcome","Selamat Datang Di "+ gs.name) lang = 'id' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") nadya.sendAudio(msg.to,"hasil.mp3") elif msg.text.lower() in ["hi","hai","halo","hallo"]: beb = "Hi Sayang 😘 " +nadya.getContact(msg.from_).displayName + " 􀸂􀆇starry heart􏿿" nadya.sendText(msg.to,beb) elif "playstore " in msg.text.lower(): tob = msg.text.lower().replace("playstore ","") nadya.sendText(msg.to,"Sedang Mencari...") nadya.sendText(msg.to,"Title : "+tob+"\nSource : Google Play\nLink : https://play.google.com/store/search?q=" + tob) nadya.sendText(msg.to,"Tuh Linknya Kak (^_^)") elif "Mid @" in msg.text: _name = msg.text.replace("Mid @","") _nametarget = _name.rstrip(' ') gs = nadya.getGroup(msg.to) for g in gs.members: if _nametarget == g.displayName: nadya.sendText(msg.to, g.mid) else: pass elif "Mybio " in msg.text: string = msg.text.replace("Mybio ","") if len(string.decode('utf-8')) <= 500: profile = nadya.getProfile() profile.statusMessage = string nadya.updateProfile(profile) nadya.sendText(msg.to,"Done") elif "Myname " in msg.text: if msg.from_ in Creator: string = msg.text.replace("Myname ","") if len(string.decode('utf-8')) <= 5000: profile = nadya.getProfile() profile.displayName = string nadya.updateProfile(profile) nadya.sendText(msg.to,"Done") elif msg.text.lower() in ["mymid","myid"]: middd = "Name : " +nadya.getContact(msg.from_).displayName + "\nMid : " +msg.from_ nadya.sendText(msg.to,middd) elif msg.text.lower() in ["me"]: msg.contentType = 13 msg.contentMetadata = {'mid': msg.from_} nadya.sendMessage(msg) elif "/apakah " in msg.text: apk = msg.text.replace("/apakah ","") rnd = ["Ya","Tidak","Bisa Jadi","Mungkin"] p = random.choice(rnd) lang = 'id' tts = gTTS(text=p, lang=lang) tts.save("hasil.mp3") nadya.sendAudio(msg.to,"hasil.mp3") elif "/hari " in msg.text: apk = msg.text.replace("/hari ","") rnd = ["Senin","Selasa","Rabu","Kamis","Jumat","Sabtu","Minggu"] p = random.choice(rnd) lang = 'id' tts = gTTS(text=p, lang=lang) tts.save("hasil.mp3") nadya.sendAudio(msg.to,"hasil.mp3") elif "/berapa " in msg.text: apk = msg.text.replace("/berapa ","") rnd = ['10%','20%','30%','40%','50%','60%','70%','80%','90%','100%','0%'] p = random.choice(rnd) lang = 'id' tts = gTTS(text=p, lang=lang) tts.save("hasil.mp3") nadya.sendAudio(msg.to,"hasil.mp3") elif "/berapakah " in msg.text: apk = msg.text.replace("/berapakah ","") rnd = ['1','2','3','4','5','6','7','8','9','10','Tidak Ada'] p = random.choice(rnd) lang = 'id' tts = gTTS(text=p, lang=lang) tts.save("hasil.mp3") nadya.sendAudio(msg.to,"hasil.mp3") elif "/kapan " in msg.text: apk = msg.text.replace("/kapan ","") rnd = ["kapan kapan","besok","satu abad lagi","Hari ini","Tahun depan","Minggu depan","Bulan depan","Sebentar lagi","Tidak Akan Pernah"] p = random.choice(rnd) lang = 'id' tts = gTTS(text=p, lang=lang) tts.save("hasil.mp3") nadya.sendAudio(msg.to,"hasil.mp3") elif msg.text in ["Simisimi on","Simisimi:on"]: settings["simiSimi"][msg.to] = True wait["Simi"] = True nadya.sendText(msg.to," Simisimi Di Aktifkan") elif msg.text in ["Simisimi off","Simisimi:off"]: settings["simiSimi"][msg.to] = False wait["Simi"] = False nadya.sendText(msg.to,"Simisimi Di Nonaktifkan") elif "Image " in msg.text: search = msg.text.replace("Image ","") url = 'https://www.google.com/search?espv=2&biw=1366&bih=667&tbm=isch&oq=kuc&aqs=mobile-gws-lite.0.0l5&q=' + search raw_html = (download_page(url)) items = [] items = items + (_images_get_all_items(raw_html)) path = random.choice(items) print path try: nadya.sendImageWithURL(msg.to,path) except: pass elif "Youtubesearch: " in msg.text: query = msg.text.replace("Youtube ","") with requests.session() as s: s.headers['user-agent'] = 'Mozilla/5.0' url = 'http://www.youtube.com/results' params = {'search_query': query} r = s.get(url, params=params) soup = BeautifulSoup(r.content, 'html.parser') hasil = "" for a in soup.select('.yt-lockup-title > a[title]'): if '&list=' not in a['href']: hasil += ''.join((a['title'],'\nUrl : http://www.youtube.com' + a['href'],'\n\n')) nadya.sendText(msg.to,hasil) print '[Command] Youtube Search' elif "Tr-id " in msg.text: isi = msg.text.replace("Tr-id ","") translator = Translator() hasil = translator.translate(isi, dest='id') A = hasil.text A = A.encode('utf-8') nadya.sendText(msg.to, A) elif "Tr-en " in msg.text: isi = msg.text.replace("Tr-en ","") translator = Translator() hasil = translator.translate(isi, dest='en') A = hasil.text A = A.encode('utf-8') nadya.sendText(msg.to, A) elif "Tr-th " in msg.text: isi = msg.text.replace("Tr-th ","") translator = Translator() hasil = translator.translate(isi, dest='th') A = hasil.text A = A.encode('utf-8') nadya.sendText(msg.to, A) elif "Id@en" in msg.text: bahasa_awal = 'id' bahasa_tujuan = 'en' kata = msg.text.replace("Id@en ","") url = 'https://translate.google.com/m?sl=%s&tl=%s&ie=UTF-8&prev=_m&q=%s' % (bahasa_awal, bahasa_tujuan, kata.replace(" ", "+")) agent = {'User-Agent':'Mozilla/5.0'} cari_hasil = 'class="t0">' request = urllib2.Request(url, headers=agent) page = urllib2.urlopen(request).read() result = page[page.find(cari_hasil)+len(cari_hasil):] result = result.split("<")[0] nadya.sendText(msg.to,"----Dari Indonesia----\n" + "" + kata + "\n\n----Ke Inggris----\n" + "" + result) elif "En@id" in msg.text: bahasa_awal = 'en' bahasa_tujuan = 'id' kata = msg.text.replace("En@id ","") url = 'https://translate.google.com/m?sl=%s&tl=%s&ie=UTF-8&prev=_m&q=%s' % (bahasa_awal, bahasa_tujuan, kata.replace(" ", "+")) agent = {'User-Agent':'Mozilla/5.0'} cari_hasil = 'class="t0">' request = urllib2.Request(url, headers=agent) page = urllib2.urlopen(request).read() result = page[page.find(cari_hasil)+len(cari_hasil):] result = result.split("<")[0] nadya.sendText(msg.to,"----Dari Inggris----\n" + "" + kata + "\n\n----Ke Indonesia----\n" + "" + result) elif "Id@th" in msg.text: bahasa_awal = 'id' bahasa_tujuan = 'th' kata = msg.text.replace("Id@en ","") url = 'https://translate.google.com/m?sl=%s&tl=%s&ie=UTF-8&prev=_m&q=%s' % (bahasa_awal, bahasa_tujuan, kata.replace(" ", "+")) agent = {'User-Agent':'Mozilla/5.0'} cari_hasil = 'class="t0">' request = urllib2.Request(url, headers=agent) page = urllib2.urlopen(request).read() result = page[page.find(cari_hasil)+len(cari_hasil):] result = result.split("<")[0] nadya.sendText(msg.to,"----Dari Indonesia----\n" + "" + kata + "\n\n----Ke Thailand----\n" + "" + result) elif "Th@id" in msg.text: bahasa_awal = 'th' bahasa_tujuan = 'id' kata = msg.text.replace("Id@en ","") url = 'https://translate.google.com/m?sl=%s&tl=%s&ie=UTF-8&prev=_m&q=%s' % (bahasa_awal, bahasa_tujuan, kata.replace(" ", "+")) agent = {'User-Agent':'Mozilla/5.0'} cari_hasil = 'class="t0">' request = urllib2.Request(url, headers=agent) page = urllib2.urlopen(request).read() result = page[page.find(cari_hasil)+len(cari_hasil):] result = result.split("<")[0] nadya.sendText(msg.to,"----Dari Thailand----\n" + "" + kata + "\n\n----Ke Indonesia----\n" + "" + result) elif msg.text in ["Friendlist"]: contactlist = nadya.getAllContactIds() kontak = nadya.getContacts(contactlist) num=1 msgs="═════════List Friend═════════" for ids in kontak: msgs+="\n[%i] %s" % (num, ids.displayName) num=(num+1) msgs+="\n═════════List Friend═════════\n\nTotal Friend : %i" % len(kontak) nadya.sendText(msg.to, msgs) elif msg.text in ["Memlist"]: kontak = nadya.getGroup(msg.to) group = kontak.members num=1 msgs="═════════List Member═�����═══════-" for ids in group: msgs+="\n[%i] %s" % (num, ids.displayName) num=(num+1) msgs+="\n═════════List Member═════════\n\nTotal Members : %i" % len(group) nadya.sendText(msg.to, msgs) elif "Getvid @" in msg.text: print "[Command]dp executing" _name = msg.text.replace("Getvid @","") _nametarget = _name.rstrip(' ') gs = nadya.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: nadya.sendText(msg.to,"Contact not found") else: for target in targets: try: contact = nadya.getContact(target) path = "http://dl.profile.line-cdn.net/" + contact.pictureStatus nadya.sendVideoWithURL(msg.to, path) except Exception as e: raise e print "[Command]dp executed" elif "Getgroup image" in msg.text: group = nadya.getGroup(msg.to) path = "http://dl.profile.line-cdn.net/" + group.pictureStatus nadya.sendImageWithURL(msg.to,path) elif "Urlgroup image" in msg.text: group = nadya.getGroup(msg.to) path = "http://dl.profile.line-cdn.net/" + group.pictureStatus nadya.sendText(msg.to,path) elif "Getname" in msg.text: key = eval(msg.contentMetadata["MENTION"]) key1 = key["MENTIONEES"][0]["M"] contact = nadya.getContact(key1) cu = nadya.channel.getCover(key1) try: nadya.sendText(msg.to, "===[DisplayName]===\n" + contact.displayName) except: nadya.sendText(msg.to, "===[DisplayName]===\n" + contact.displayName) elif "Getprofile" in msg.text: key = eval(msg.contentMetadata["MENTION"]) key1 = key["MENTIONEES"][0]["M"] contact = nadya.getContact(key1) cu = nadya.channel.getCover(key1) path = str(cu) image = "http://dl.profile.line-cdn.net/" + contact.pictureStatus try: nadya.sendText(msg.to,"Nama :\n" + contact.displayName + "\n\nBio :\n" + contact.statusMessage) nadya.sendText(msg.to,"Profile Picture " + contact.displayName) nadya.sendImageWithURL(msg.to,image) nadya.sendText(msg.to,"Cover " + contact.displayName) nadya.sendImageWithURL(msg.to,path) except: pass elif "Getcontact" in msg.text: key = eval(msg.contentMetadata["MENTION"]) key1 = key["MENTIONEES"][0]["M"] mmid = nadya.getContact(key1) msg.contentType = 13 msg.contentMetadata = {"mid": key1} nadya.sendMessage(msg) elif "Getinfo" in msg.text: key = eval(msg.contentMetadata["MENTION"]) key1 = key["MENTIONEES"][0]["M"] contact = nadya.getContact(key1) cu = nadya.channel.getCover(key1) try: nadya.sendText(msg.to,"Nama :\n" + contact.displayName + "\n\nMid :\n" + contact.mid + "\n\nBio :\n" + contact.statusMessage + "\n\nProfile Picture :\nhttp://dl.profile.line-cdn.net/" + contact.pictureStatus + "\n\nHeader :\n" + str(cu)) except: nadya.sendText(msg.to,"Nama :\n" + contact.displayName + "\n\nMid :\n" + contact.mid + "\n\nBio :\n" + contact.statusMessage + "\n\nProfile Picture :\n" + str(cu)) elif "Getbio" in msg.text: key = eval(msg.contentMetadata["MENTION"]) key1 = key["MENTIONEES"][0]["M"] contact = nadya.getContact(key1) cu = nadya.channel.getCover(key1) try: nadya.sendText(msg.to, "===[StatusMessage]===\n" + contact.statusMessage) except: nadya.sendText(msg.to, "===[StatusMessage]===\n" + contact.statusMessage) elif msg.text.lower() == 'runtime': eltime = time.time() - mulai van = "Bot Sudah Berjalan Selama :\n"+waktu(eltime) nadya.sendText(msg.to,van) elif "Checkdate " in msg.text: tanggal = msg.text.replace("Checkdate ","") r=requests.get('https://script.google.com/macros/exec?service=AKfycbw7gKzP-WYV2F5mc9RaR7yE3Ve1yN91Tjs91hp_jHSE02dSv9w&nama=ervan&tanggal='+tanggal) data=r.text data=json.loads(data) lahir = data["data"]["lahir"] usia = data["data"]["usia"] ultah = data["data"]["ultah"] zodiak = data["data"]["zodiak"] nadya.sendText(msg.to,"========== I N F O R M A S I ==========\n"+"Date Of Birth : "+lahir+"\nAge : "+usia+"\nUltah : "+ultah+"\nZodiak : "+zodiak+"\n========== I N F O R M A S I ==========") elif msg.text in ["Kalender","Time","Waktu"]: timeNow = datetime.now() timeHours = datetime.strftime(timeNow,"(%H:%M)") day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday"] hari = ["Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"] bulan = ["Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"] inihari = datetime.today() hr = inihari.strftime('%A') bln = inihari.strftime('%m') for i in range(len(day)): if hr == day[i]: hasil = hari[i] for k in range(0, len(bulan)): if bln == str(k): bln = bulan[k-1] rst = hasil + ", " + inihari.strftime('%d') + " - " + bln + " - " + inihari.strftime('%Y') + "\nJam : [ " + inihari.strftime('%H:%M:%S') + " ]" nadya.sendText(msg.to, rst) elif "SearchID: " in msg.text: userid = msg.text.replace("SearchID: ","") contact = nadya.findContactsByUserid(userid) msg.contentType = 13 msg.contentMetadata = {'mid': contact.mid} nadya.sendMessage(msg) elif "Searchid: " in msg.text: userid = msg.text.replace("Searchid: ","") contact = nadya.findContactsByUserid(userid) msg.contentType = 13 msg.contentMetadata = {'mid': contact.mid} nadya.sendMessage(msg) elif "removechat" in msg.text.lower(): if msg.from_ in admin: try: nadya.removeAllMessages(op.param2) print "[Command] Remove Chat" nadya.sendText(msg.to,"Done") except Exception as error: print error nadya.sendText(msg.to,"Error") elif "Invitemeto: " in msg.text: if msg.from_ in admin: gid = msg.text.replace("Invitemeto: ","") if gid == "": nadya.sendText(msg.to,"Invalid group id") else: try: nadya.findAndAddContactsByMid(msg.from_) nadya.inviteIntoGroup(gid,[msg.from_]) except: nadya.sendText(msg.to,"Mungkin Saya Tidak Di Dalaam Grup Itu") elif msg.text in ["Glist"]: nadya.sendText(msg.to, "Tunggu Sebentar. . .") gid = nadya.getGroupIdsJoined() h = "" for i in gid: h += "╠➩" + "%s\n" % (nadya.getGroup(i).name +" ~> ["+str(len(nadya.getGroup(i).members))+"]") nadya.sendText(msg.to,"╔═════════════════════════\n║ ☆☞ LIST GROUPS☜☆\n╠═════════════════════════\n" + h + "╠═════════════════════════" + "\n║ Total Groups =" +" ["+str(len(gid))+"]\n╚═════════════════════════") elif msg.text in ["Glistmid"]: gruplist = nadya.getGroupIdsJoined() kontak = nadya.getGroups(gruplist) num=1 msgs="═════════List GrupMid═════════" for ids in kontak: msgs+="\n[%i] %s" % (num, ids.id) num=(num+1) msgs+="\n═════════List GrupMid═════════\n\nTotal Grup : %i" % len(kontak) nadya.sendText(msg.to, msgs) elif "Google: " in msg.text: a = msg.text.replace("Google: ","") b = urllib.quote(a) nadya.sendText(msg.to,"Sedang Mencari...") nadya.sendText(msg.to, "https://www.google.com/" + b) nadya.sendText(msg.to,"Itu Dia Linknya. . .") elif "Details group: " in msg.text: if msg.from_ in admin: gid = msg.text.replace("Details group: ","") if gid in [""," "]: nadya.sendText(msg.to,"Grup id tidak valid") else: try: groups = nadya.getGroup(gid) if groups.members is not None: members = str(len(groups.members)) else: members = "0" if groups.invitee is not None: pendings = str(len(groups.invitee)) else: pendings = "0" h = "[" + groups.name + "]\n -+GroupID : " + gid + "\n -+Members : " + members + "\n -+MembersPending : " + pendings + "\n -+Creator : " + groups.creator.displayName + "\n -+GroupPicture : http://dl.profile.line.naver.jp/" + groups.pictureStatus nadya.sendText(msg.to,h) except Exception as error: nadya.sendText(msg.to,(error)) elif "Cancel invite: " in msg.text: if msg.from_ in admin: gids = msg.text.replace("Cancel invite: ","") gid = nadya.getGroup(gids) for i in gid: if i is not None: try: nadya.rejectGroupInvitation(i) except: nadya.sendText(msg.to,"Error!") break else: break if gid is not None: nadya.sendText(msg.to,"Berhasil tolak undangan dari grup " + gid.name) else: nadya.sendText(msg.to,"Grup tidak ditemukan") elif msg.text in ["Acc invite"]: if msg.from_ in admin: gid = nadya.getGroupIdsInvited() _list = "" for i in gid: if i is not None: gids = nadya.getGroup(i) _list += gids.name nadya.acceptGroupInvitation(i) else: break if gid is not None: nadya.sendText(msg.to,"Berhasil terima semua undangan dari grup :\n" + _list) else: nadya.sendText(msg.to,"Tidak ada grup yang tertunda saat ini") elif "Gif gore" in msg.text: gif = ("https://media.giphy.com/media/l2JHVsQiOZrNMGzYs/giphy.gif","https://media.giphy.com/media/OgltQ2hbilzJS/200w.gif") gore = random.choice(gif) nadya.sendGifWithURL(msg.to,gore) elif ("Micadd " in msg.text): targets = [] key = eval(msg.contentMetadata["MENTION"]) key["MENTIONEES"][0]["M"] for x in key["MENTIONEES"]: targets.append(x["M"]) for target in targets: try: mimic["target"][target] = True nadya.sendText(msg.to,"Target ditambahkan!") break except: nadya.sendText(msg.to,"Fail !") break elif ("Micdel " in msg.text): targets = [] key = eval(msg.contentMetadata["MENTION"]) key["MENTIONEES"][0]["M"] for x in key["MENTIONEES"]: targets.append(x["M"]) for target in targets: try: del mimic["target"][target] nadya.sendText(msg.to,"Target dihapuskan!") break except: nadya.sendText(msg.to,"Fail !") break elif msg.text in ["Miclist"]: if mimic["target"] == {}: nadya.sendText(msg.to,"Nothing") else: mc = "Target Mimic User:\n" for mi_d in mimic["target"]: mc += "?? "+nadya.getContact(mi_d).displayName + "\n" nadya.sendText(msg.to,mc) elif "Mimic target " in msg.text: if mimic["copy"] == True: siapa = msg.text.replace("Mimic target ","") if siapa.rstrip(' ') == "me": mimic["copy2"] = "me" nadya.sendText(msg.to,"Mimic change to me") elif siapa.rstrip(' ') == "target": mimic["copy2"] = "target" nadya.sendText(msg.to,"Mimic change to target") else: nadya.sendText(msg.to,"I dont know") elif "Mimic " in msg.text: cmd = msg.text.replace("Mimic ","") if cmd == "on": if mimic["status"] == False: mimic["status"] = True nadya.sendText(msg.to,"Reply Message on") else: nadya.sendText(msg.to,"Sudah on") elif cmd == "off": if mimic["status"] == True: mimic["status"] = False nadya.sendText(msg.to,"Reply Message off") else: nadya.sendText(msg.to,"Sudah off") if op.type == 59: print op except Exception as error: print error while True: try: Ops = nadya.fetchOps(nadya.Poll.rev, 5) except EOFError: raise Exception("It might be wrong revision\n" + str(nadya.Poll.rev)) for Op in Ops: if (Op.type != OpType.END_OF_OPERATION): nadya.Poll.rev = max(nadya.Poll.rev, Op.revision) bot(Op)
Chuckysb.py
148,684
-*- coding: utf-8 -*-Chucky_Botnadya.login(qr=True)/XXX, >XXX, ;XXX, ^XXX, %XXX, $XXX... Name = summon(op.param2)
140
en
0.114094
# -*- coding: utf-8 -*- # Copyright (c) 2019, TUSHAR TAJNE and contributors # For license information, please see license.txt from __future__ import unicode_literals import frappe from frappe.model.document import Document from frappe import _ class District(Document): def validate(self): name = str(self.district.capitalize()) self.name = _(name) pass
sps/sps/doctype/district/district.py
361
-*- coding: utf-8 -*- Copyright (c) 2019, TUSHAR TAJNE and contributors For license information, please see license.txt
119
en
0.767719
from django.db import models from django.contrib.auth import get_user_model from django.urls import reverse from django.utils.translation import gettext_lazy as _ from tinymce import HTMLField # Create your models here. User = get_user_model() class PostView(models.Model): user = models.ForeignKey(User, verbose_name=_( "User"), on_delete=models.CASCADE) post = models.ForeignKey('Post', verbose_name=_( "Post"), on_delete=models.CASCADE) def __str__(self): return self.user.username class Author(models.Model): user = models.OneToOneField(User, verbose_name=_( "Author"), on_delete=models.CASCADE) profile_picture = models.ImageField(_("Profile picture")) def __str__(self): return self.user.username class Category(models.Model): title = models.CharField(_("Title"), max_length=50) def __str__(self): return self.title class Comment(models.Model): user = models.ForeignKey( User, verbose_name=_("User"), on_delete=models.CASCADE) timestamp = models.DateTimeField(_("Timestamp"), auto_now_add=True) content = models.TextField(_("Comment text")) post = models.ForeignKey('Post', verbose_name=_( "Post"), related_name='comments', on_delete=models.CASCADE) def __str__(self): return self.user.username class Post(models.Model): title = models.CharField(_("Title"), max_length=50) overview = models.TextField(_("Overview")) timestamp = models.DateTimeField( _("Timestamp"), auto_now=False, auto_now_add=True) content = HTMLField() # comment_count = models.IntegerField(_("Comment count"), default=0) # view_count = models.IntegerField(_("View count"), default=0) author = models.ForeignKey(Author, verbose_name=_( "Author"), on_delete=models.CASCADE) thumbnail = models.ImageField(_("Thumbnail")) categories = models.ManyToManyField(Category, verbose_name=_("Categories")) featured = models.BooleanField(_("Featured"), default=False) previous_post = models.ForeignKey("self", verbose_name=_( "Previous post"), related_name='previous', on_delete=models.SET_NULL, blank=True, null=True) next_post = models.ForeignKey("self", verbose_name=_( "Next post"), related_name='next', on_delete=models.SET_NULL, blank=True, null=True) def __str__(self): return self.title def get_absolute_url(self): return reverse("post-detail", kwargs={"pk": self.pk}) def get_update_url(self): return reverse("post-update", kwargs={"pk": self.pk}) def get_delete_url(self): return reverse("post-delete", kwargs={"pk": self.pk}) @property def get_comments(self): return self.comments.all().order_by('-timestamp') @property def comment_count(self): return Comment.objects.filter(post=self).count() @property def view_count(self): return PostView.objects.filter(post=self).count()
src/posts/models.py
3,080
Create your models here. comment_count = models.IntegerField(_("Comment count"), default=0) view_count = models.IntegerField(_("View count"), default=0)
152
en
0.468293
import unittest import os import numpy as np from dotenv import load_dotenv from nlpaug.util import AudioLoader import nlpaug.augmenter.spectrogram as nas class TestLoudnessSpec(unittest.TestCase): @classmethod def setUpClass(cls): env_config_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "..", "..", "..", ".env") ) load_dotenv(env_config_path) # https://freewavesamples.com/yamaha-v50-rock-beat-120-bpm cls.sample_wav_file = os.path.join( os.environ.get("TEST_DIR"), "res", "audio", "Yamaha-V50-Rock-Beat-120bpm.wav", ) def test_no_change_source(self): data = AudioLoader.load_mel_spectrogram(self.sample_wav_file, n_mels=128) aug = nas.LoudnessAug(stateless=False) aug_data = aug.augment(data) comparison = data == aug_data self.assertFalse(comparison.all()) def test_substitute(self): data = AudioLoader.load_mel_spectrogram(self.sample_wav_file, n_mels=128) aug = nas.LoudnessAug(stateless=False) aug_data = aug.augment(data) comparison = ( data[:, aug.time_start : aug.time_end] == aug_data[:, aug.time_start : aug.time_end] ) self.assertFalse(comparison.all()) comparison = data[:, : aug.time_start] == aug_data[:, : aug.time_start] self.assertTrue(comparison.all()) comparison = data[:, aug.time_end :] == aug_data[:, aug.time_end :] self.assertTrue(comparison.all())
test/augmenter/spectrogram/test_loudness_spec.py
1,569
https://freewavesamples.com/yamaha-v50-rock-beat-120-bpm
56
en
0.436808
""" Create the numpy.core.multiarray namespace for backward compatibility. In v1.16 the multiarray and umath c-extension modules were merged into a single _multiarray_umath extension module. So we replicate the old namespace by importing from the extension module. """ import functools import warnings from . import overrides from . import _multiarray_umath import numpy as np from numpy.core._multiarray_umath import * from numpy.core._multiarray_umath import ( _fastCopyAndTranspose, _flagdict, _insert, _reconstruct, _vec_string, _ARRAY_API, _monotonicity ) __all__ = [ '_ARRAY_API', 'ALLOW_THREADS', 'BUFSIZE', 'CLIP', 'DATETIMEUNITS', 'ITEM_HASOBJECT', 'ITEM_IS_POINTER', 'LIST_PICKLE', 'MAXDIMS', 'MAY_SHARE_BOUNDS', 'MAY_SHARE_EXACT', 'NEEDS_INIT', 'NEEDS_PYAPI', 'RAISE', 'USE_GETITEM', 'USE_SETITEM', 'WRAP', '_fastCopyAndTranspose', '_flagdict', '_insert', '_reconstruct', '_vec_string', '_monotonicity', 'add_docstring', 'arange', 'array', 'bincount', 'broadcast', 'busday_count', 'busday_offset', 'busdaycalendar', 'can_cast', 'compare_chararrays', 'concatenate', 'copyto', 'correlate', 'correlate2', 'count_nonzero', 'c_einsum', 'datetime_as_string', 'datetime_data', 'digitize', 'dot', 'dragon4_positional', 'dragon4_scientific', 'dtype', 'empty', 'empty_like', 'error', 'flagsobj', 'flatiter', 'format_longfloat', 'frombuffer', 'fromfile', 'fromiter', 'fromstring', 'getbuffer', 'inner', 'int_asbuffer', 'interp', 'interp_complex', 'is_busday', 'lexsort', 'matmul', 'may_share_memory', 'min_scalar_type', 'ndarray', 'nditer', 'nested_iters', 'newbuffer', 'normalize_axis_index', 'packbits', 'promote_types', 'putmask', 'ravel_multi_index', 'result_type', 'scalar', 'set_datetimeparse_function', 'set_legacy_print_mode', 'set_numeric_ops', 'set_string_function', 'set_typeDict', 'shares_memory', 'test_interrupt', 'tracemalloc_domain', 'typeinfo', 'unpackbits', 'unravel_index', 'vdot', 'where', 'zeros'] # For backward compatibility, make sure pickle imports these functions from here _reconstruct.__module__ = 'numpy.core.multiarray' scalar.__module__ = 'numpy.core.multiarray' arange.__module__ = 'numpy' array.__module__ = 'numpy' datetime_data.__module__ = 'numpy' empty.__module__ = 'numpy' frombuffer.__module__ = 'numpy' fromfile.__module__ = 'numpy' fromiter.__module__ = 'numpy' frompyfunc.__module__ = 'numpy' fromstring.__module__ = 'numpy' geterrobj.__module__ = 'numpy' may_share_memory.__module__ = 'numpy' nested_iters.__module__ = 'numpy' promote_types.__module__ = 'numpy' set_numeric_ops.__module__ = 'numpy' seterrobj.__module__ = 'numpy' zeros.__module__ = 'numpy' # We can't verify dispatcher signatures because NumPy's C functions don't # support introspection. array_function_from_c_func_and_dispatcher = functools.partial( overrides.array_function_from_dispatcher, module='numpy', docs_from_dispatcher=True, verify=False) @array_function_from_c_func_and_dispatcher(_multiarray_umath.empty_like) def empty_like(prototype, dtype=None, order=None, subok=None): """ empty_like(prototype, dtype=None, order='K', subok=True) Return a new array with the same shape and type as a given array. Parameters ---------- prototype : array_like The shape and data-type of `prototype` define these same attributes of the returned array. dtype : data-type, optional Overrides the data type of the result. .. versionadded:: 1.6.0 order : {'C', 'F', 'A', or 'K'}, optional Overrides the memory layout of the result. 'C' means C-order, 'F' means F-order, 'A' means 'F' if ``prototype`` is Fortran contiguous, 'C' otherwise. 'K' means match the layout of ``prototype`` as closely as possible. .. versionadded:: 1.6.0 subok : bool, optional. If True, then the newly created array will use the sub-class type of 'a', otherwise it will be a base-class array. Defaults to True. Returns ------- out : ndarray Array of uninitialized (arbitrary) data with the same shape and type as `prototype`. See Also -------- ones_like : Return an array of ones with shape and type of input. zeros_like : Return an array of zeros with shape and type of input. full_like : Return a new array with shape of input filled with value. empty : Return a new uninitialized array. Notes ----- This function does *not* initialize the returned array; to do that use `zeros_like` or `ones_like` instead. It may be marginally faster than the functions that do set the array values. Examples -------- >>> a = ([1,2,3], [4,5,6]) # a is array-like >>> np.empty_like(a) array([[-1073741821, -1073741821, 3], #random [ 0, 0, -1073741821]]) >>> a = np.array([[1., 2., 3.],[4.,5.,6.]]) >>> np.empty_like(a) array([[ -2.00000715e+000, 1.48219694e-323, -2.00000572e+000],#random [ 4.38791518e-305, -2.00000715e+000, 4.17269252e-309]]) """ return (prototype,) @array_function_from_c_func_and_dispatcher(_multiarray_umath.concatenate) def concatenate(arrays, axis=None, out=None): """ concatenate((a1, a2, ...), axis=0, out=None) Join a sequence of arrays along an existing axis. Parameters ---------- a1, a2, ... : sequence of array_like The arrays must have the same shape, except in the dimension corresponding to `axis` (the first, by default). axis : int, optional The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0. out : ndarray, optional If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified. Returns ------- res : ndarray The concatenated array. See Also -------- ma.concatenate : Concatenate function that preserves input masks. array_split : Split an array into multiple sub-arrays of equal or near-equal size. split : Split array into a list of multiple sub-arrays of equal size. hsplit : Split array into multiple sub-arrays horizontally (column wise) vsplit : Split array into multiple sub-arrays vertically (row wise) dsplit : Split array into multiple sub-arrays along the 3rd axis (depth). stack : Stack a sequence of arrays along a new axis. hstack : Stack arrays in sequence horizontally (column wise) vstack : Stack arrays in sequence vertically (row wise) dstack : Stack arrays in sequence depth wise (along third dimension) block : Assemble arrays from blocks. Notes ----- When one or more of the arrays to be concatenated is a MaskedArray, this function will return a MaskedArray object instead of an ndarray, but the input masks are *not* preserved. In cases where a MaskedArray is expected as input, use the ma.concatenate function from the masked array module instead. Examples -------- >>> a = np.array([[1, 2], [3, 4]]) >>> b = np.array([[5, 6]]) >>> np.concatenate((a, b), axis=0) array([[1, 2], [3, 4], [5, 6]]) >>> np.concatenate((a, b.T), axis=1) array([[1, 2, 5], [3, 4, 6]]) >>> np.concatenate((a, b), axis=None) array([1, 2, 3, 4, 5, 6]) This function will not preserve masking of MaskedArray inputs. >>> a = np.ma.arange(3) >>> a[1] = np.ma.masked >>> b = np.arange(2, 5) >>> a masked_array(data=[0, --, 2], mask=[False, True, False], fill_value=999999) >>> b array([2, 3, 4]) >>> np.concatenate([a, b]) masked_array(data=[0, 1, 2, 2, 3, 4], mask=False, fill_value=999999) >>> np.ma.concatenate([a, b]) masked_array(data=[0, --, 2, 2, 3, 4], mask=[False, True, False, False, False, False], fill_value=999999) """ if out is not None: # optimize for the typical case where only arrays is provided arrays = list(arrays) arrays.append(out) return arrays @array_function_from_c_func_and_dispatcher(_multiarray_umath.inner) def inner(a, b): """ inner(a, b) Inner product of two arrays. Ordinary inner product of vectors for 1-D arrays (without complex conjugation), in higher dimensions a sum product over the last axes. Parameters ---------- a, b : array_like If `a` and `b` are nonscalar, their last dimensions must match. Returns ------- out : ndarray `out.shape = a.shape[:-1] + b.shape[:-1]` Raises ------ ValueError If the last dimension of `a` and `b` has different size. See Also -------- tensordot : Sum products over arbitrary axes. dot : Generalised matrix product, using second last dimension of `b`. einsum : Einstein summation convention. Notes ----- For vectors (1-D arrays) it computes the ordinary inner-product:: np.inner(a, b) = sum(a[:]*b[:]) More generally, if `ndim(a) = r > 0` and `ndim(b) = s > 0`:: np.inner(a, b) = np.tensordot(a, b, axes=(-1,-1)) or explicitly:: np.inner(a, b)[i0,...,ir-1,j0,...,js-1] = sum(a[i0,...,ir-1,:]*b[j0,...,js-1,:]) In addition `a` or `b` may be scalars, in which case:: np.inner(a,b) = a*b Examples -------- Ordinary inner product for vectors: >>> a = np.array([1,2,3]) >>> b = np.array([0,1,0]) >>> np.inner(a, b) 2 A multidimensional example: >>> a = np.arange(24).reshape((2,3,4)) >>> b = np.arange(4) >>> np.inner(a, b) array([[ 14, 38, 62], [ 86, 110, 134]]) An example where `b` is a scalar: >>> np.inner(np.eye(2), 7) array([[ 7., 0.], [ 0., 7.]]) """ return (a, b) @array_function_from_c_func_and_dispatcher(_multiarray_umath.where) def where(condition, x=None, y=None): """ where(condition, [x, y]) Return elements chosen from `x` or `y` depending on `condition`. .. note:: When only `condition` is provided, this function is a shorthand for ``np.asarray(condition).nonzero()``. Using `nonzero` directly should be preferred, as it behaves correctly for subclasses. The rest of this documentation covers only the case where all three arguments are provided. Parameters ---------- condition : array_like, bool Where True, yield `x`, otherwise yield `y`. x, y : array_like Values from which to choose. `x`, `y` and `condition` need to be broadcastable to some shape. Returns ------- out : ndarray An array with elements from `x` where `condition` is True, and elements from `y` elsewhere. See Also -------- choose nonzero : The function that is called when x and y are omitted Notes ----- If all the arrays are 1-D, `where` is equivalent to:: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples -------- >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ... [[1, 2], [3, 4]], ... [[9, 8], [7, 6]]) array([[1, 8], [3, 4]]) The shapes of x, y, and the condition are broadcast together: >>> x, y = np.ogrid[:3, :4] >>> np.where(x < y, x, 10 + y) # both x and 10+y are broadcast array([[10, 0, 0, 0], [10, 11, 1, 1], [10, 11, 12, 2]]) >>> a = np.array([[0, 1, 2], ... [0, 2, 4], ... [0, 3, 6]]) >>> np.where(a < 4, a, -1) # -1 is broadcast array([[ 0, 1, 2], [ 0, 2, -1], [ 0, 3, -1]]) """ return (condition, x, y) @array_function_from_c_func_and_dispatcher(_multiarray_umath.lexsort) def lexsort(keys, axis=None): """ lexsort(keys, axis=-1) Perform an indirect stable sort using a sequence of keys. Given multiple sorting keys, which can be interpreted as columns in a spreadsheet, lexsort returns an array of integer indices that describes the sort order by multiple columns. The last key in the sequence is used for the primary sort order, the second-to-last key for the secondary sort order, and so on. The keys argument must be a sequence of objects that can be converted to arrays of the same shape. If a 2D array is provided for the keys argument, it's rows are interpreted as the sorting keys and sorting is according to the last row, second last row etc. Parameters ---------- keys : (k, N) array or tuple containing k (N,)-shaped sequences The `k` different "columns" to be sorted. The last column (or row if `keys` is a 2D array) is the primary sort key. axis : int, optional Axis to be indirectly sorted. By default, sort over the last axis. Returns ------- indices : (N,) ndarray of ints Array of indices that sort the keys along the specified axis. See Also -------- argsort : Indirect sort. ndarray.sort : In-place sort. sort : Return a sorted copy of an array. Examples -------- Sort names: first by surname, then by name. >>> surnames = ('Hertz', 'Galilei', 'Hertz') >>> first_names = ('Heinrich', 'Galileo', 'Gustav') >>> ind = np.lexsort((first_names, surnames)) >>> ind array([1, 2, 0]) >>> [surnames[i] + ", " + first_names[i] for i in ind] ['Galilei, Galileo', 'Hertz, Gustav', 'Hertz, Heinrich'] Sort two columns of numbers: >>> a = [1,5,1,4,3,4,4] # First column >>> b = [9,4,0,4,0,2,1] # Second column >>> ind = np.lexsort((b,a)) # Sort by a, then by b >>> print(ind) [2 0 4 6 5 3 1] >>> [(a[i],b[i]) for i in ind] [(1, 0), (1, 9), (3, 0), (4, 1), (4, 2), (4, 4), (5, 4)] Note that sorting is first according to the elements of ``a``. Secondary sorting is according to the elements of ``b``. A normal ``argsort`` would have yielded: >>> [(a[i],b[i]) for i in np.argsort(a)] [(1, 9), (1, 0), (3, 0), (4, 4), (4, 2), (4, 1), (5, 4)] Structured arrays are sorted lexically by ``argsort``: >>> x = np.array([(1,9), (5,4), (1,0), (4,4), (3,0), (4,2), (4,1)], ... dtype=np.dtype([('x', int), ('y', int)])) >>> np.argsort(x) # or np.argsort(x, order=('x', 'y')) array([2, 0, 4, 6, 5, 3, 1]) """ if isinstance(keys, tuple): return keys else: return (keys,) @array_function_from_c_func_and_dispatcher(_multiarray_umath.can_cast) def can_cast(from_, to, casting=None): """ can_cast(from_, to, casting='safe') Returns True if cast between data types can occur according to the casting rule. If from is a scalar or array scalar, also returns True if the scalar value can be cast without overflow or truncation to an integer. Parameters ---------- from_ : dtype, dtype specifier, scalar, or array Data type, scalar, or array to cast from. to : dtype or dtype specifier Data type to cast to. casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional Controls what kind of data casting may occur. * 'no' means the data types should not be cast at all. * 'equiv' means only byte-order changes are allowed. * 'safe' means only casts which can preserve values are allowed. * 'same_kind' means only safe casts or casts within a kind, like float64 to float32, are allowed. * 'unsafe' means any data conversions may be done. Returns ------- out : bool True if cast can occur according to the casting rule. Notes ----- Starting in NumPy 1.9, can_cast function now returns False in 'safe' casting mode for integer/float dtype and string dtype if the string dtype length is not long enough to store the max integer/float value converted to a string. Previously can_cast in 'safe' mode returned True for integer/float dtype and a string dtype of any length. See also -------- dtype, result_type Examples -------- Basic examples >>> np.can_cast(np.int32, np.int64) True >>> np.can_cast(np.float64, complex) True >>> np.can_cast(complex, float) False >>> np.can_cast('i8', 'f8') True >>> np.can_cast('i8', 'f4') False >>> np.can_cast('i4', 'S4') False Casting scalars >>> np.can_cast(100, 'i1') True >>> np.can_cast(150, 'i1') False >>> np.can_cast(150, 'u1') True >>> np.can_cast(3.5e100, np.float32) False >>> np.can_cast(1000.0, np.float32) True Array scalar checks the value, array does not >>> np.can_cast(np.array(1000.0), np.float32) True >>> np.can_cast(np.array([1000.0]), np.float32) False Using the casting rules >>> np.can_cast('i8', 'i8', 'no') True >>> np.can_cast('<i8', '>i8', 'no') False >>> np.can_cast('<i8', '>i8', 'equiv') True >>> np.can_cast('<i4', '>i8', 'equiv') False >>> np.can_cast('<i4', '>i8', 'safe') True >>> np.can_cast('<i8', '>i4', 'safe') False >>> np.can_cast('<i8', '>i4', 'same_kind') True >>> np.can_cast('<i8', '>u4', 'same_kind') False >>> np.can_cast('<i8', '>u4', 'unsafe') True """ return (from_,) @array_function_from_c_func_and_dispatcher(_multiarray_umath.min_scalar_type) def min_scalar_type(a): """ min_scalar_type(a) For scalar ``a``, returns the data type with the smallest size and smallest scalar kind which can hold its value. For non-scalar array ``a``, returns the vector's dtype unmodified. Floating point values are not demoted to integers, and complex values are not demoted to floats. Parameters ---------- a : scalar or array_like The value whose minimal data type is to be found. Returns ------- out : dtype The minimal data type. Notes ----- .. versionadded:: 1.6.0 See Also -------- result_type, promote_types, dtype, can_cast Examples -------- >>> np.min_scalar_type(10) dtype('uint8') >>> np.min_scalar_type(-260) dtype('int16') >>> np.min_scalar_type(3.1) dtype('float16') >>> np.min_scalar_type(1e50) dtype('float64') >>> np.min_scalar_type(np.arange(4,dtype='f8')) dtype('float64') """ return (a,) @array_function_from_c_func_and_dispatcher(_multiarray_umath.result_type) def result_type(*arrays_and_dtypes): """ result_type(*arrays_and_dtypes) Returns the type that results from applying the NumPy type promotion rules to the arguments. Type promotion in NumPy works similarly to the rules in languages like C++, with some slight differences. When both scalars and arrays are used, the array's type takes precedence and the actual value of the scalar is taken into account. For example, calculating 3*a, where a is an array of 32-bit floats, intuitively should result in a 32-bit float output. If the 3 is a 32-bit integer, the NumPy rules indicate it can't convert losslessly into a 32-bit float, so a 64-bit float should be the result type. By examining the value of the constant, '3', we see that it fits in an 8-bit integer, which can be cast losslessly into the 32-bit float. Parameters ---------- arrays_and_dtypes : list of arrays and dtypes The operands of some operation whose result type is needed. Returns ------- out : dtype The result type. See also -------- dtype, promote_types, min_scalar_type, can_cast Notes ----- .. versionadded:: 1.6.0 The specific algorithm used is as follows. Categories are determined by first checking which of boolean, integer (int/uint), or floating point (float/complex) the maximum kind of all the arrays and the scalars are. If there are only scalars or the maximum category of the scalars is higher than the maximum category of the arrays, the data types are combined with :func:`promote_types` to produce the return value. Otherwise, `min_scalar_type` is called on each array, and the resulting data types are all combined with :func:`promote_types` to produce the return value. The set of int values is not a subset of the uint values for types with the same number of bits, something not reflected in :func:`min_scalar_type`, but handled as a special case in `result_type`. Examples -------- >>> np.result_type(3, np.arange(7, dtype='i1')) dtype('int8') >>> np.result_type('i4', 'c8') dtype('complex128') >>> np.result_type(3.0, -2) dtype('float64') """ return arrays_and_dtypes @array_function_from_c_func_and_dispatcher(_multiarray_umath.dot) def dot(a, b, out=None): """ dot(a, b, out=None) Dot product of two arrays. Specifically, - If both `a` and `b` are 1-D arrays, it is inner product of vectors (without complex conjugation). - If both `a` and `b` are 2-D arrays, it is matrix multiplication, but using :func:`matmul` or ``a @ b`` is preferred. - If either `a` or `b` is 0-D (scalar), it is equivalent to :func:`multiply` and using ``numpy.multiply(a, b)`` or ``a * b`` is preferred. - If `a` is an N-D array and `b` is a 1-D array, it is a sum product over the last axis of `a` and `b`. - If `a` is an N-D array and `b` is an M-D array (where ``M>=2``), it is a sum product over the last axis of `a` and the second-to-last axis of `b`:: dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m]) Parameters ---------- a : array_like First argument. b : array_like Second argument. out : ndarray, optional Output argument. This must have the exact kind that would be returned if it was not used. In particular, it must have the right type, must be C-contiguous, and its dtype must be the dtype that would be returned for `dot(a,b)`. This is a performance feature. Therefore, if these conditions are not met, an exception is raised, instead of attempting to be flexible. Returns ------- output : ndarray Returns the dot product of `a` and `b`. If `a` and `b` are both scalars or both 1-D arrays then a scalar is returned; otherwise an array is returned. If `out` is given, then it is returned. Raises ------ ValueError If the last dimension of `a` is not the same size as the second-to-last dimension of `b`. See Also -------- vdot : Complex-conjugating dot product. tensordot : Sum products over arbitrary axes. einsum : Einstein summation convention. matmul : '@' operator as method with out parameter. Examples -------- >>> np.dot(3, 4) 12 Neither argument is complex-conjugated: >>> np.dot([2j, 3j], [2j, 3j]) (-13+0j) For 2-D arrays it is the matrix product: >>> a = [[1, 0], [0, 1]] >>> b = [[4, 1], [2, 2]] >>> np.dot(a, b) array([[4, 1], [2, 2]]) >>> a = np.arange(3*4*5*6).reshape((3,4,5,6)) >>> b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3)) >>> np.dot(a, b)[2,3,2,1,2,2] 499128 >>> sum(a[2,3,2,:] * b[1,2,:,2]) 499128 """ return (a, b, out) @array_function_from_c_func_and_dispatcher(_multiarray_umath.vdot) def vdot(a, b): """ vdot(a, b) Return the dot product of two vectors. The vdot(`a`, `b`) function handles complex numbers differently than dot(`a`, `b`). If the first argument is complex the complex conjugate of the first argument is used for the calculation of the dot product. Note that `vdot` handles multidimensional arrays differently than `dot`: it does *not* perform a matrix product, but flattens input arguments to 1-D vectors first. Consequently, it should only be used for vectors. Parameters ---------- a : array_like If `a` is complex the complex conjugate is taken before calculation of the dot product. b : array_like Second argument to the dot product. Returns ------- output : ndarray Dot product of `a` and `b`. Can be an int, float, or complex depending on the types of `a` and `b`. See Also -------- dot : Return the dot product without using the complex conjugate of the first argument. Examples -------- >>> a = np.array([1+2j,3+4j]) >>> b = np.array([5+6j,7+8j]) >>> np.vdot(a, b) (70-8j) >>> np.vdot(b, a) (70+8j) Note that higher-dimensional arrays are flattened! >>> a = np.array([[1, 4], [5, 6]]) >>> b = np.array([[4, 1], [2, 2]]) >>> np.vdot(a, b) 30 >>> np.vdot(b, a) 30 >>> 1*4 + 4*1 + 5*2 + 6*2 30 """ return (a, b) @array_function_from_c_func_and_dispatcher(_multiarray_umath.bincount) def bincount(x, weights=None, minlength=None): """ bincount(x, weights=None, minlength=0) Count number of occurrences of each value in array of non-negative ints. The number of bins (of size 1) is one larger than the largest value in `x`. If `minlength` is specified, there will be at least this number of bins in the output array (though it will be longer if necessary, depending on the contents of `x`). Each bin gives the number of occurrences of its index value in `x`. If `weights` is specified the input array is weighted by it, i.e. if a value ``n`` is found at position ``i``, ``out[n] += weight[i]`` instead of ``out[n] += 1``. Parameters ---------- x : array_like, 1 dimension, nonnegative ints Input array. weights : array_like, optional Weights, array of the same shape as `x`. minlength : int, optional A minimum number of bins for the output array. .. versionadded:: 1.6.0 Returns ------- out : ndarray of ints The result of binning the input array. The length of `out` is equal to ``np.amax(x)+1``. Raises ------ ValueError If the input is not 1-dimensional, or contains elements with negative values, or if `minlength` is negative. TypeError If the type of the input is float or complex. See Also -------- histogram, digitize, unique Examples -------- >>> np.bincount(np.arange(5)) array([1, 1, 1, 1, 1]) >>> np.bincount(np.array([0, 1, 1, 3, 2, 1, 7])) array([1, 3, 1, 1, 0, 0, 0, 1]) >>> x = np.array([0, 1, 1, 3, 2, 1, 7, 23]) >>> np.bincount(x).size == np.amax(x)+1 True The input array needs to be of integer dtype, otherwise a TypeError is raised: >>> np.bincount(np.arange(5, dtype=float)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: array cannot be safely cast to required type A possible use of ``bincount`` is to perform sums over variable-size chunks of an array, using the ``weights`` keyword. >>> w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights >>> x = np.array([0, 1, 1, 2, 2, 2]) >>> np.bincount(x, weights=w) array([ 0.3, 0.7, 1.1]) """ return (x, weights) @array_function_from_c_func_and_dispatcher(_multiarray_umath.ravel_multi_index) def ravel_multi_index(multi_index, dims, mode=None, order=None): """ ravel_multi_index(multi_index, dims, mode='raise', order='C') Converts a tuple of index arrays into an array of flat indices, applying boundary modes to the multi-index. Parameters ---------- multi_index : tuple of array_like A tuple of integer arrays, one array for each dimension. dims : tuple of ints The shape of array into which the indices from ``multi_index`` apply. mode : {'raise', 'wrap', 'clip'}, optional Specifies how out-of-bounds indices are handled. Can specify either one mode or a tuple of modes, one mode per index. * 'raise' -- raise an error (default) * 'wrap' -- wrap around * 'clip' -- clip to the range In 'clip' mode, a negative index which would normally wrap will clip to 0 instead. order : {'C', 'F'}, optional Determines whether the multi-index should be viewed as indexing in row-major (C-style) or column-major (Fortran-style) order. Returns ------- raveled_indices : ndarray An array of indices into the flattened version of an array of dimensions ``dims``. See Also -------- unravel_index Notes ----- .. versionadded:: 1.6.0 Examples -------- >>> arr = np.array([[3,6,6],[4,5,1]]) >>> np.ravel_multi_index(arr, (7,6)) array([22, 41, 37]) >>> np.ravel_multi_index(arr, (7,6), order='F') array([31, 41, 13]) >>> np.ravel_multi_index(arr, (4,6), mode='clip') array([22, 23, 19]) >>> np.ravel_multi_index(arr, (4,4), mode=('clip','wrap')) array([12, 13, 13]) >>> np.ravel_multi_index((3,1,4,1), (6,7,8,9)) 1621 """ return multi_index @array_function_from_c_func_and_dispatcher(_multiarray_umath.unravel_index) def unravel_index(indices, shape=None, order=None, dims=None): """ unravel_index(indices, shape, order='C') Converts a flat index or array of flat indices into a tuple of coordinate arrays. Parameters ---------- indices : array_like An integer array whose elements are indices into the flattened version of an array of dimensions ``shape``. Before version 1.6.0, this function accepted just one index value. shape : tuple of ints The shape of the array to use for unraveling ``indices``. .. versionchanged:: 1.16.0 Renamed from ``dims`` to ``shape``. order : {'C', 'F'}, optional Determines whether the indices should be viewed as indexing in row-major (C-style) or column-major (Fortran-style) order. .. versionadded:: 1.6.0 Returns ------- unraveled_coords : tuple of ndarray Each array in the tuple has the same shape as the ``indices`` array. See Also -------- ravel_multi_index Examples -------- >>> np.unravel_index([22, 41, 37], (7,6)) (array([3, 6, 6]), array([4, 5, 1])) >>> np.unravel_index([31, 41, 13], (7,6), order='F') (array([3, 6, 6]), array([4, 5, 1])) >>> np.unravel_index(1621, (6,7,8,9)) (3, 1, 4, 1) """ if dims is not None: warnings.warn("'shape' argument should be used instead of 'dims'", DeprecationWarning, stacklevel=3) return (indices,) @array_function_from_c_func_and_dispatcher(_multiarray_umath.copyto) def copyto(dst, src, casting=None, where=None): """ copyto(dst, src, casting='same_kind', where=True) Copies values from one array to another, broadcasting as necessary. Raises a TypeError if the `casting` rule is violated, and if `where` is provided, it selects which elements to copy. .. versionadded:: 1.7.0 Parameters ---------- dst : ndarray The array into which values are copied. src : array_like The array from which values are copied. casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional Controls what kind of data casting may occur when copying. * 'no' means the data types should not be cast at all. * 'equiv' means only byte-order changes are allowed. * 'safe' means only casts which can preserve values are allowed. * 'same_kind' means only safe casts or casts within a kind, like float64 to float32, are allowed. * 'unsafe' means any data conversions may be done. where : array_like of bool, optional A boolean array which is broadcasted to match the dimensions of `dst`, and selects elements to copy from `src` to `dst` wherever it contains the value True. """ return (dst, src, where) @array_function_from_c_func_and_dispatcher(_multiarray_umath.putmask) def putmask(a, mask, values): """ putmask(a, mask, values) Changes elements of an array based on conditional and input values. Sets ``a.flat[n] = values[n]`` for each n where ``mask.flat[n]==True``. If `values` is not the same size as `a` and `mask` then it will repeat. This gives behavior different from ``a[mask] = values``. Parameters ---------- a : array_like Target array. mask : array_like Boolean mask array. It has to be the same shape as `a`. values : array_like Values to put into `a` where `mask` is True. If `values` is smaller than `a` it will be repeated. See Also -------- place, put, take, copyto Examples -------- >>> x = np.arange(6).reshape(2, 3) >>> np.putmask(x, x>2, x**2) >>> x array([[ 0, 1, 2], [ 9, 16, 25]]) If `values` is smaller than `a` it is repeated: >>> x = np.arange(5) >>> np.putmask(x, x>1, [-33, -44]) >>> x array([ 0, 1, -33, -44, -33]) """ return (a, mask, values) @array_function_from_c_func_and_dispatcher(_multiarray_umath.packbits) def packbits(myarray, axis=None): """ packbits(myarray, axis=None) Packs the elements of a binary-valued array into bits in a uint8 array. The result is padded to full bytes by inserting zero bits at the end. Parameters ---------- myarray : array_like An array of integers or booleans whose elements should be packed to bits. axis : int, optional The dimension over which bit-packing is done. ``None`` implies packing the flattened array. Returns ------- packed : ndarray Array of type uint8 whose elements represent bits corresponding to the logical (0 or nonzero) value of the input elements. The shape of `packed` has the same number of dimensions as the input (unless `axis` is None, in which case the output is 1-D). See Also -------- unpackbits: Unpacks elements of a uint8 array into a binary-valued output array. Examples -------- >>> a = np.array([[[1,0,1], ... [0,1,0]], ... [[1,1,0], ... [0,0,1]]]) >>> b = np.packbits(a, axis=-1) >>> b array([[[160],[64]],[[192],[32]]], dtype=uint8) Note that in binary 160 = 1010 0000, 64 = 0100 0000, 192 = 1100 0000, and 32 = 0010 0000. """ return (myarray,) @array_function_from_c_func_and_dispatcher(_multiarray_umath.unpackbits) def unpackbits(myarray, axis=None): """ unpackbits(myarray, axis=None) Unpacks elements of a uint8 array into a binary-valued output array. Each element of `myarray` represents a bit-field that should be unpacked into a binary-valued output array. The shape of the output array is either 1-D (if `axis` is None) or the same shape as the input array with unpacking done along the axis specified. Parameters ---------- myarray : ndarray, uint8 type Input array. axis : int, optional The dimension over which bit-unpacking is done. ``None`` implies unpacking the flattened array. Returns ------- unpacked : ndarray, uint8 type The elements are binary-valued (0 or 1). See Also -------- packbits : Packs the elements of a binary-valued array into bits in a uint8 array. Examples -------- >>> a = np.array([[2], [7], [23]], dtype=np.uint8) >>> a array([[ 2], [ 7], [23]], dtype=uint8) >>> b = np.unpackbits(a, axis=1) >>> b array([[0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 0, 1, 1, 1]], dtype=uint8) """ return (myarray,) @array_function_from_c_func_and_dispatcher(_multiarray_umath.shares_memory) def shares_memory(a, b, max_work=None): """ shares_memory(a, b, max_work=None) Determine if two arrays share memory Parameters ---------- a, b : ndarray Input arrays max_work : int, optional Effort to spend on solving the overlap problem (maximum number of candidate solutions to consider). The following special values are recognized: max_work=MAY_SHARE_EXACT (default) The problem is solved exactly. In this case, the function returns True only if there is an element shared between the arrays. max_work=MAY_SHARE_BOUNDS Only the memory bounds of a and b are checked. Raises ------ numpy.TooHardError Exceeded max_work. Returns ------- out : bool See Also -------- may_share_memory Examples -------- >>> np.may_share_memory(np.array([1,2]), np.array([5,8,9])) False """ return (a, b) @array_function_from_c_func_and_dispatcher(_multiarray_umath.may_share_memory) def may_share_memory(a, b, max_work=None): """ may_share_memory(a, b, max_work=None) Determine if two arrays might share memory A return of True does not necessarily mean that the two arrays share any element. It just means that they *might*. Only the memory bounds of a and b are checked by default. Parameters ---------- a, b : ndarray Input arrays max_work : int, optional Effort to spend on solving the overlap problem. See `shares_memory` for details. Default for ``may_share_memory`` is to do a bounds check. Returns ------- out : bool See Also -------- shares_memory Examples -------- >>> np.may_share_memory(np.array([1,2]), np.array([5,8,9])) False >>> x = np.zeros([3, 4]) >>> np.may_share_memory(x[:,0], x[:,1]) True """ return (a, b) @array_function_from_c_func_and_dispatcher(_multiarray_umath.is_busday) def is_busday(dates, weekmask=None, holidays=None, busdaycal=None, out=None): """ is_busday(dates, weekmask='1111100', holidays=None, busdaycal=None, out=None) Calculates which of the given dates are valid days, and which are not. .. versionadded:: 1.7.0 Parameters ---------- dates : array_like of datetime64[D] The array of dates to process. weekmask : str or array_like of bool, optional A seven-element array indicating which of Monday through Sunday are valid days. May be specified as a length-seven list or array, like [1,1,1,1,1,0,0]; a length-seven string, like '1111100'; or a string like "Mon Tue Wed Thu Fri", made up of 3-character abbreviations for weekdays, optionally separated by white space. Valid abbreviations are: Mon Tue Wed Thu Fri Sat Sun holidays : array_like of datetime64[D], optional An array of dates to consider as invalid dates. They may be specified in any order, and NaT (not-a-time) dates are ignored. This list is saved in a normalized form that is suited for fast calculations of valid days. busdaycal : busdaycalendar, optional A `busdaycalendar` object which specifies the valid days. If this parameter is provided, neither weekmask nor holidays may be provided. out : array of bool, optional If provided, this array is filled with the result. Returns ------- out : array of bool An array with the same shape as ``dates``, containing True for each valid day, and False for each invalid day. See Also -------- busdaycalendar: An object that specifies a custom set of valid days. busday_offset : Applies an offset counted in valid days. busday_count : Counts how many valid days are in a half-open date range. Examples -------- >>> # The weekdays are Friday, Saturday, and Monday ... np.is_busday(['2011-07-01', '2011-07-02', '2011-07-18'], ... holidays=['2011-07-01', '2011-07-04', '2011-07-17']) array([False, False, True], dtype='bool') """ return (dates, weekmask, holidays, out) @array_function_from_c_func_and_dispatcher(_multiarray_umath.busday_offset) def busday_offset(dates, offsets, roll=None, weekmask=None, holidays=None, busdaycal=None, out=None): """ busday_offset(dates, offsets, roll='raise', weekmask='1111100', holidays=None, busdaycal=None, out=None) First adjusts the date to fall on a valid day according to the ``roll`` rule, then applies offsets to the given dates counted in valid days. .. versionadded:: 1.7.0 Parameters ---------- dates : array_like of datetime64[D] The array of dates to process. offsets : array_like of int The array of offsets, which is broadcast with ``dates``. roll : {'raise', 'nat', 'forward', 'following', 'backward', 'preceding', 'modifiedfollowing', 'modifiedpreceding'}, optional How to treat dates that do not fall on a valid day. The default is 'raise'. * 'raise' means to raise an exception for an invalid day. * 'nat' means to return a NaT (not-a-time) for an invalid day. * 'forward' and 'following' mean to take the first valid day later in time. * 'backward' and 'preceding' mean to take the first valid day earlier in time. * 'modifiedfollowing' means to take the first valid day later in time unless it is across a Month boundary, in which case to take the first valid day earlier in time. * 'modifiedpreceding' means to take the first valid day earlier in time unless it is across a Month boundary, in which case to take the first valid day later in time. weekmask : str or array_like of bool, optional A seven-element array indicating which of Monday through Sunday are valid days. May be specified as a length-seven list or array, like [1,1,1,1,1,0,0]; a length-seven string, like '1111100'; or a string like "Mon Tue Wed Thu Fri", made up of 3-character abbreviations for weekdays, optionally separated by white space. Valid abbreviations are: Mon Tue Wed Thu Fri Sat Sun holidays : array_like of datetime64[D], optional An array of dates to consider as invalid dates. They may be specified in any order, and NaT (not-a-time) dates are ignored. This list is saved in a normalized form that is suited for fast calculations of valid days. busdaycal : busdaycalendar, optional A `busdaycalendar` object which specifies the valid days. If this parameter is provided, neither weekmask nor holidays may be provided. out : array of datetime64[D], optional If provided, this array is filled with the result. Returns ------- out : array of datetime64[D] An array with a shape from broadcasting ``dates`` and ``offsets`` together, containing the dates with offsets applied. See Also -------- busdaycalendar: An object that specifies a custom set of valid days. is_busday : Returns a boolean array indicating valid days. busday_count : Counts how many valid days are in a half-open date range. Examples -------- >>> # First business day in October 2011 (not accounting for holidays) ... np.busday_offset('2011-10', 0, roll='forward') numpy.datetime64('2011-10-03','D') >>> # Last business day in February 2012 (not accounting for holidays) ... np.busday_offset('2012-03', -1, roll='forward') numpy.datetime64('2012-02-29','D') >>> # Third Wednesday in January 2011 ... np.busday_offset('2011-01', 2, roll='forward', weekmask='Wed') numpy.datetime64('2011-01-19','D') >>> # 2012 Mother's Day in Canada and the U.S. ... np.busday_offset('2012-05', 1, roll='forward', weekmask='Sun') numpy.datetime64('2012-05-13','D') >>> # First business day on or after a date ... np.busday_offset('2011-03-20', 0, roll='forward') numpy.datetime64('2011-03-21','D') >>> np.busday_offset('2011-03-22', 0, roll='forward') numpy.datetime64('2011-03-22','D') >>> # First business day after a date ... np.busday_offset('2011-03-20', 1, roll='backward') numpy.datetime64('2011-03-21','D') >>> np.busday_offset('2011-03-22', 1, roll='backward') numpy.datetime64('2011-03-23','D') """ return (dates, offsets, weekmask, holidays, out) @array_function_from_c_func_and_dispatcher(_multiarray_umath.busday_count) def busday_count(begindates, enddates, weekmask=None, holidays=None, busdaycal=None, out=None): """ busday_count(begindates, enddates, weekmask='1111100', holidays=[], busdaycal=None, out=None) Counts the number of valid days between `begindates` and `enddates`, not including the day of `enddates`. If ``enddates`` specifies a date value that is earlier than the corresponding ``begindates`` date value, the count will be negative. .. versionadded:: 1.7.0 Parameters ---------- begindates : array_like of datetime64[D] The array of the first dates for counting. enddates : array_like of datetime64[D] The array of the end dates for counting, which are excluded from the count themselves. weekmask : str or array_like of bool, optional A seven-element array indicating which of Monday through Sunday are valid days. May be specified as a length-seven list or array, like [1,1,1,1,1,0,0]; a length-seven string, like '1111100'; or a string like "Mon Tue Wed Thu Fri", made up of 3-character abbreviations for weekdays, optionally separated by white space. Valid abbreviations are: Mon Tue Wed Thu Fri Sat Sun holidays : array_like of datetime64[D], optional An array of dates to consider as invalid dates. They may be specified in any order, and NaT (not-a-time) dates are ignored. This list is saved in a normalized form that is suited for fast calculations of valid days. busdaycal : busdaycalendar, optional A `busdaycalendar` object which specifies the valid days. If this parameter is provided, neither weekmask nor holidays may be provided. out : array of int, optional If provided, this array is filled with the result. Returns ------- out : array of int An array with a shape from broadcasting ``begindates`` and ``enddates`` together, containing the number of valid days between the begin and end dates. See Also -------- busdaycalendar: An object that specifies a custom set of valid days. is_busday : Returns a boolean array indicating valid days. busday_offset : Applies an offset counted in valid days. Examples -------- >>> # Number of weekdays in January 2011 ... np.busday_count('2011-01', '2011-02') 21 >>> # Number of weekdays in 2011 ... np.busday_count('2011', '2012') 260 >>> # Number of Saturdays in 2011 ... np.busday_count('2011', '2012', weekmask='Sat') 53 """ return (begindates, enddates, weekmask, holidays, out) @array_function_from_c_func_and_dispatcher( _multiarray_umath.datetime_as_string) def datetime_as_string(arr, unit=None, timezone=None, casting=None): """ datetime_as_string(arr, unit=None, timezone='naive', casting='same_kind') Convert an array of datetimes into an array of strings. Parameters ---------- arr : array_like of datetime64 The array of UTC timestamps to format. unit : str One of None, 'auto', or a :ref:`datetime unit <arrays.dtypes.dateunits>`. timezone : {'naive', 'UTC', 'local'} or tzinfo Timezone information to use when displaying the datetime. If 'UTC', end with a Z to indicate UTC time. If 'local', convert to the local timezone first, and suffix with a +-#### timezone offset. If a tzinfo object, then do as with 'local', but use the specified timezone. casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'} Casting to allow when changing between datetime units. Returns ------- str_arr : ndarray An array of strings the same shape as `arr`. Examples -------- >>> d = np.arange('2002-10-27T04:30', 4*60, 60, dtype='M8[m]') >>> d array(['2002-10-27T04:30', '2002-10-27T05:30', '2002-10-27T06:30', '2002-10-27T07:30'], dtype='datetime64[m]') Setting the timezone to UTC shows the same information, but with a Z suffix >>> np.datetime_as_string(d, timezone='UTC') array(['2002-10-27T04:30Z', '2002-10-27T05:30Z', '2002-10-27T06:30Z', '2002-10-27T07:30Z'], dtype='<U35') Note that we picked datetimes that cross a DST boundary. Passing in a ``pytz`` timezone object will print the appropriate offset >>> np.datetime_as_string(d, timezone=pytz.timezone('US/Eastern')) array(['2002-10-27T00:30-0400', '2002-10-27T01:30-0400', '2002-10-27T01:30-0500', '2002-10-27T02:30-0500'], dtype='<U39') Passing in a unit will change the precision >>> np.datetime_as_string(d, unit='h') array(['2002-10-27T04', '2002-10-27T05', '2002-10-27T06', '2002-10-27T07'], dtype='<U32') >>> np.datetime_as_string(d, unit='s') array(['2002-10-27T04:30:00', '2002-10-27T05:30:00', '2002-10-27T06:30:00', '2002-10-27T07:30:00'], dtype='<U38') 'casting' can be used to specify whether precision can be changed >>> np.datetime_as_string(d, unit='h', casting='safe') TypeError: Cannot create a datetime string as units 'h' from a NumPy datetime with units 'm' according to the rule 'safe' """ return (arr,)
venv/lib/python3.7/site-packages/numpy/core/multiarray.py
50,606
bincount(x, weights=None, minlength=0) Count number of occurrences of each value in array of non-negative ints. The number of bins (of size 1) is one larger than the largest value in `x`. If `minlength` is specified, there will be at least this number of bins in the output array (though it will be longer if necessary, depending on the contents of `x`). Each bin gives the number of occurrences of its index value in `x`. If `weights` is specified the input array is weighted by it, i.e. if a value ``n`` is found at position ``i``, ``out[n] += weight[i]`` instead of ``out[n] += 1``. Parameters ---------- x : array_like, 1 dimension, nonnegative ints Input array. weights : array_like, optional Weights, array of the same shape as `x`. minlength : int, optional A minimum number of bins for the output array. .. versionadded:: 1.6.0 Returns ------- out : ndarray of ints The result of binning the input array. The length of `out` is equal to ``np.amax(x)+1``. Raises ------ ValueError If the input is not 1-dimensional, or contains elements with negative values, or if `minlength` is negative. TypeError If the type of the input is float or complex. See Also -------- histogram, digitize, unique Examples -------- >>> np.bincount(np.arange(5)) array([1, 1, 1, 1, 1]) >>> np.bincount(np.array([0, 1, 1, 3, 2, 1, 7])) array([1, 3, 1, 1, 0, 0, 0, 1]) >>> x = np.array([0, 1, 1, 3, 2, 1, 7, 23]) >>> np.bincount(x).size == np.amax(x)+1 True The input array needs to be of integer dtype, otherwise a TypeError is raised: >>> np.bincount(np.arange(5, dtype=float)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: array cannot be safely cast to required type A possible use of ``bincount`` is to perform sums over variable-size chunks of an array, using the ``weights`` keyword. >>> w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights >>> x = np.array([0, 1, 1, 2, 2, 2]) >>> np.bincount(x, weights=w) array([ 0.3, 0.7, 1.1]) busday_count(begindates, enddates, weekmask='1111100', holidays=[], busdaycal=None, out=None) Counts the number of valid days between `begindates` and `enddates`, not including the day of `enddates`. If ``enddates`` specifies a date value that is earlier than the corresponding ``begindates`` date value, the count will be negative. .. versionadded:: 1.7.0 Parameters ---------- begindates : array_like of datetime64[D] The array of the first dates for counting. enddates : array_like of datetime64[D] The array of the end dates for counting, which are excluded from the count themselves. weekmask : str or array_like of bool, optional A seven-element array indicating which of Monday through Sunday are valid days. May be specified as a length-seven list or array, like [1,1,1,1,1,0,0]; a length-seven string, like '1111100'; or a string like "Mon Tue Wed Thu Fri", made up of 3-character abbreviations for weekdays, optionally separated by white space. Valid abbreviations are: Mon Tue Wed Thu Fri Sat Sun holidays : array_like of datetime64[D], optional An array of dates to consider as invalid dates. They may be specified in any order, and NaT (not-a-time) dates are ignored. This list is saved in a normalized form that is suited for fast calculations of valid days. busdaycal : busdaycalendar, optional A `busdaycalendar` object which specifies the valid days. If this parameter is provided, neither weekmask nor holidays may be provided. out : array of int, optional If provided, this array is filled with the result. Returns ------- out : array of int An array with a shape from broadcasting ``begindates`` and ``enddates`` together, containing the number of valid days between the begin and end dates. See Also -------- busdaycalendar: An object that specifies a custom set of valid days. is_busday : Returns a boolean array indicating valid days. busday_offset : Applies an offset counted in valid days. Examples -------- >>> # Number of weekdays in January 2011 ... np.busday_count('2011-01', '2011-02') 21 >>> # Number of weekdays in 2011 ... np.busday_count('2011', '2012') 260 >>> # Number of Saturdays in 2011 ... np.busday_count('2011', '2012', weekmask='Sat') 53 busday_offset(dates, offsets, roll='raise', weekmask='1111100', holidays=None, busdaycal=None, out=None) First adjusts the date to fall on a valid day according to the ``roll`` rule, then applies offsets to the given dates counted in valid days. .. versionadded:: 1.7.0 Parameters ---------- dates : array_like of datetime64[D] The array of dates to process. offsets : array_like of int The array of offsets, which is broadcast with ``dates``. roll : {'raise', 'nat', 'forward', 'following', 'backward', 'preceding', 'modifiedfollowing', 'modifiedpreceding'}, optional How to treat dates that do not fall on a valid day. The default is 'raise'. * 'raise' means to raise an exception for an invalid day. * 'nat' means to return a NaT (not-a-time) for an invalid day. * 'forward' and 'following' mean to take the first valid day later in time. * 'backward' and 'preceding' mean to take the first valid day earlier in time. * 'modifiedfollowing' means to take the first valid day later in time unless it is across a Month boundary, in which case to take the first valid day earlier in time. * 'modifiedpreceding' means to take the first valid day earlier in time unless it is across a Month boundary, in which case to take the first valid day later in time. weekmask : str or array_like of bool, optional A seven-element array indicating which of Monday through Sunday are valid days. May be specified as a length-seven list or array, like [1,1,1,1,1,0,0]; a length-seven string, like '1111100'; or a string like "Mon Tue Wed Thu Fri", made up of 3-character abbreviations for weekdays, optionally separated by white space. Valid abbreviations are: Mon Tue Wed Thu Fri Sat Sun holidays : array_like of datetime64[D], optional An array of dates to consider as invalid dates. They may be specified in any order, and NaT (not-a-time) dates are ignored. This list is saved in a normalized form that is suited for fast calculations of valid days. busdaycal : busdaycalendar, optional A `busdaycalendar` object which specifies the valid days. If this parameter is provided, neither weekmask nor holidays may be provided. out : array of datetime64[D], optional If provided, this array is filled with the result. Returns ------- out : array of datetime64[D] An array with a shape from broadcasting ``dates`` and ``offsets`` together, containing the dates with offsets applied. See Also -------- busdaycalendar: An object that specifies a custom set of valid days. is_busday : Returns a boolean array indicating valid days. busday_count : Counts how many valid days are in a half-open date range. Examples -------- >>> # First business day in October 2011 (not accounting for holidays) ... np.busday_offset('2011-10', 0, roll='forward') numpy.datetime64('2011-10-03','D') >>> # Last business day in February 2012 (not accounting for holidays) ... np.busday_offset('2012-03', -1, roll='forward') numpy.datetime64('2012-02-29','D') >>> # Third Wednesday in January 2011 ... np.busday_offset('2011-01', 2, roll='forward', weekmask='Wed') numpy.datetime64('2011-01-19','D') >>> # 2012 Mother's Day in Canada and the U.S. ... np.busday_offset('2012-05', 1, roll='forward', weekmask='Sun') numpy.datetime64('2012-05-13','D') >>> # First business day on or after a date ... np.busday_offset('2011-03-20', 0, roll='forward') numpy.datetime64('2011-03-21','D') >>> np.busday_offset('2011-03-22', 0, roll='forward') numpy.datetime64('2011-03-22','D') >>> # First business day after a date ... np.busday_offset('2011-03-20', 1, roll='backward') numpy.datetime64('2011-03-21','D') >>> np.busday_offset('2011-03-22', 1, roll='backward') numpy.datetime64('2011-03-23','D') can_cast(from_, to, casting='safe') Returns True if cast between data types can occur according to the casting rule. If from is a scalar or array scalar, also returns True if the scalar value can be cast without overflow or truncation to an integer. Parameters ---------- from_ : dtype, dtype specifier, scalar, or array Data type, scalar, or array to cast from. to : dtype or dtype specifier Data type to cast to. casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional Controls what kind of data casting may occur. * 'no' means the data types should not be cast at all. * 'equiv' means only byte-order changes are allowed. * 'safe' means only casts which can preserve values are allowed. * 'same_kind' means only safe casts or casts within a kind, like float64 to float32, are allowed. * 'unsafe' means any data conversions may be done. Returns ------- out : bool True if cast can occur according to the casting rule. Notes ----- Starting in NumPy 1.9, can_cast function now returns False in 'safe' casting mode for integer/float dtype and string dtype if the string dtype length is not long enough to store the max integer/float value converted to a string. Previously can_cast in 'safe' mode returned True for integer/float dtype and a string dtype of any length. See also -------- dtype, result_type Examples -------- Basic examples >>> np.can_cast(np.int32, np.int64) True >>> np.can_cast(np.float64, complex) True >>> np.can_cast(complex, float) False >>> np.can_cast('i8', 'f8') True >>> np.can_cast('i8', 'f4') False >>> np.can_cast('i4', 'S4') False Casting scalars >>> np.can_cast(100, 'i1') True >>> np.can_cast(150, 'i1') False >>> np.can_cast(150, 'u1') True >>> np.can_cast(3.5e100, np.float32) False >>> np.can_cast(1000.0, np.float32) True Array scalar checks the value, array does not >>> np.can_cast(np.array(1000.0), np.float32) True >>> np.can_cast(np.array([1000.0]), np.float32) False Using the casting rules >>> np.can_cast('i8', 'i8', 'no') True >>> np.can_cast('<i8', '>i8', 'no') False >>> np.can_cast('<i8', '>i8', 'equiv') True >>> np.can_cast('<i4', '>i8', 'equiv') False >>> np.can_cast('<i4', '>i8', 'safe') True >>> np.can_cast('<i8', '>i4', 'safe') False >>> np.can_cast('<i8', '>i4', 'same_kind') True >>> np.can_cast('<i8', '>u4', 'same_kind') False >>> np.can_cast('<i8', '>u4', 'unsafe') True concatenate((a1, a2, ...), axis=0, out=None) Join a sequence of arrays along an existing axis. Parameters ---------- a1, a2, ... : sequence of array_like The arrays must have the same shape, except in the dimension corresponding to `axis` (the first, by default). axis : int, optional The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0. out : ndarray, optional If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified. Returns ------- res : ndarray The concatenated array. See Also -------- ma.concatenate : Concatenate function that preserves input masks. array_split : Split an array into multiple sub-arrays of equal or near-equal size. split : Split array into a list of multiple sub-arrays of equal size. hsplit : Split array into multiple sub-arrays horizontally (column wise) vsplit : Split array into multiple sub-arrays vertically (row wise) dsplit : Split array into multiple sub-arrays along the 3rd axis (depth). stack : Stack a sequence of arrays along a new axis. hstack : Stack arrays in sequence horizontally (column wise) vstack : Stack arrays in sequence vertically (row wise) dstack : Stack arrays in sequence depth wise (along third dimension) block : Assemble arrays from blocks. Notes ----- When one or more of the arrays to be concatenated is a MaskedArray, this function will return a MaskedArray object instead of an ndarray, but the input masks are *not* preserved. In cases where a MaskedArray is expected as input, use the ma.concatenate function from the masked array module instead. Examples -------- >>> a = np.array([[1, 2], [3, 4]]) >>> b = np.array([[5, 6]]) >>> np.concatenate((a, b), axis=0) array([[1, 2], [3, 4], [5, 6]]) >>> np.concatenate((a, b.T), axis=1) array([[1, 2, 5], [3, 4, 6]]) >>> np.concatenate((a, b), axis=None) array([1, 2, 3, 4, 5, 6]) This function will not preserve masking of MaskedArray inputs. >>> a = np.ma.arange(3) >>> a[1] = np.ma.masked >>> b = np.arange(2, 5) >>> a masked_array(data=[0, --, 2], mask=[False, True, False], fill_value=999999) >>> b array([2, 3, 4]) >>> np.concatenate([a, b]) masked_array(data=[0, 1, 2, 2, 3, 4], mask=False, fill_value=999999) >>> np.ma.concatenate([a, b]) masked_array(data=[0, --, 2, 2, 3, 4], mask=[False, True, False, False, False, False], fill_value=999999) copyto(dst, src, casting='same_kind', where=True) Copies values from one array to another, broadcasting as necessary. Raises a TypeError if the `casting` rule is violated, and if `where` is provided, it selects which elements to copy. .. versionadded:: 1.7.0 Parameters ---------- dst : ndarray The array into which values are copied. src : array_like The array from which values are copied. casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional Controls what kind of data casting may occur when copying. * 'no' means the data types should not be cast at all. * 'equiv' means only byte-order changes are allowed. * 'safe' means only casts which can preserve values are allowed. * 'same_kind' means only safe casts or casts within a kind, like float64 to float32, are allowed. * 'unsafe' means any data conversions may be done. where : array_like of bool, optional A boolean array which is broadcasted to match the dimensions of `dst`, and selects elements to copy from `src` to `dst` wherever it contains the value True. datetime_as_string(arr, unit=None, timezone='naive', casting='same_kind') Convert an array of datetimes into an array of strings. Parameters ---------- arr : array_like of datetime64 The array of UTC timestamps to format. unit : str One of None, 'auto', or a :ref:`datetime unit <arrays.dtypes.dateunits>`. timezone : {'naive', 'UTC', 'local'} or tzinfo Timezone information to use when displaying the datetime. If 'UTC', end with a Z to indicate UTC time. If 'local', convert to the local timezone first, and suffix with a +-#### timezone offset. If a tzinfo object, then do as with 'local', but use the specified timezone. casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'} Casting to allow when changing between datetime units. Returns ------- str_arr : ndarray An array of strings the same shape as `arr`. Examples -------- >>> d = np.arange('2002-10-27T04:30', 4*60, 60, dtype='M8[m]') >>> d array(['2002-10-27T04:30', '2002-10-27T05:30', '2002-10-27T06:30', '2002-10-27T07:30'], dtype='datetime64[m]') Setting the timezone to UTC shows the same information, but with a Z suffix >>> np.datetime_as_string(d, timezone='UTC') array(['2002-10-27T04:30Z', '2002-10-27T05:30Z', '2002-10-27T06:30Z', '2002-10-27T07:30Z'], dtype='<U35') Note that we picked datetimes that cross a DST boundary. Passing in a ``pytz`` timezone object will print the appropriate offset >>> np.datetime_as_string(d, timezone=pytz.timezone('US/Eastern')) array(['2002-10-27T00:30-0400', '2002-10-27T01:30-0400', '2002-10-27T01:30-0500', '2002-10-27T02:30-0500'], dtype='<U39') Passing in a unit will change the precision >>> np.datetime_as_string(d, unit='h') array(['2002-10-27T04', '2002-10-27T05', '2002-10-27T06', '2002-10-27T07'], dtype='<U32') >>> np.datetime_as_string(d, unit='s') array(['2002-10-27T04:30:00', '2002-10-27T05:30:00', '2002-10-27T06:30:00', '2002-10-27T07:30:00'], dtype='<U38') 'casting' can be used to specify whether precision can be changed >>> np.datetime_as_string(d, unit='h', casting='safe') TypeError: Cannot create a datetime string as units 'h' from a NumPy datetime with units 'm' according to the rule 'safe' dot(a, b, out=None) Dot product of two arrays. Specifically, - If both `a` and `b` are 1-D arrays, it is inner product of vectors (without complex conjugation). - If both `a` and `b` are 2-D arrays, it is matrix multiplication, but using :func:`matmul` or ``a @ b`` is preferred. - If either `a` or `b` is 0-D (scalar), it is equivalent to :func:`multiply` and using ``numpy.multiply(a, b)`` or ``a * b`` is preferred. - If `a` is an N-D array and `b` is a 1-D array, it is a sum product over the last axis of `a` and `b`. - If `a` is an N-D array and `b` is an M-D array (where ``M>=2``), it is a sum product over the last axis of `a` and the second-to-last axis of `b`:: dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m]) Parameters ---------- a : array_like First argument. b : array_like Second argument. out : ndarray, optional Output argument. This must have the exact kind that would be returned if it was not used. In particular, it must have the right type, must be C-contiguous, and its dtype must be the dtype that would be returned for `dot(a,b)`. This is a performance feature. Therefore, if these conditions are not met, an exception is raised, instead of attempting to be flexible. Returns ------- output : ndarray Returns the dot product of `a` and `b`. If `a` and `b` are both scalars or both 1-D arrays then a scalar is returned; otherwise an array is returned. If `out` is given, then it is returned. Raises ------ ValueError If the last dimension of `a` is not the same size as the second-to-last dimension of `b`. See Also -------- vdot : Complex-conjugating dot product. tensordot : Sum products over arbitrary axes. einsum : Einstein summation convention. matmul : '@' operator as method with out parameter. Examples -------- >>> np.dot(3, 4) 12 Neither argument is complex-conjugated: >>> np.dot([2j, 3j], [2j, 3j]) (-13+0j) For 2-D arrays it is the matrix product: >>> a = [[1, 0], [0, 1]] >>> b = [[4, 1], [2, 2]] >>> np.dot(a, b) array([[4, 1], [2, 2]]) >>> a = np.arange(3*4*5*6).reshape((3,4,5,6)) >>> b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3)) >>> np.dot(a, b)[2,3,2,1,2,2] 499128 >>> sum(a[2,3,2,:] * b[1,2,:,2]) 499128 empty_like(prototype, dtype=None, order='K', subok=True) Return a new array with the same shape and type as a given array. Parameters ---------- prototype : array_like The shape and data-type of `prototype` define these same attributes of the returned array. dtype : data-type, optional Overrides the data type of the result. .. versionadded:: 1.6.0 order : {'C', 'F', 'A', or 'K'}, optional Overrides the memory layout of the result. 'C' means C-order, 'F' means F-order, 'A' means 'F' if ``prototype`` is Fortran contiguous, 'C' otherwise. 'K' means match the layout of ``prototype`` as closely as possible. .. versionadded:: 1.6.0 subok : bool, optional. If True, then the newly created array will use the sub-class type of 'a', otherwise it will be a base-class array. Defaults to True. Returns ------- out : ndarray Array of uninitialized (arbitrary) data with the same shape and type as `prototype`. See Also -------- ones_like : Return an array of ones with shape and type of input. zeros_like : Return an array of zeros with shape and type of input. full_like : Return a new array with shape of input filled with value. empty : Return a new uninitialized array. Notes ----- This function does *not* initialize the returned array; to do that use `zeros_like` or `ones_like` instead. It may be marginally faster than the functions that do set the array values. Examples -------- >>> a = ([1,2,3], [4,5,6]) # a is array-like >>> np.empty_like(a) array([[-1073741821, -1073741821, 3], #random [ 0, 0, -1073741821]]) >>> a = np.array([[1., 2., 3.],[4.,5.,6.]]) >>> np.empty_like(a) array([[ -2.00000715e+000, 1.48219694e-323, -2.00000572e+000],#random [ 4.38791518e-305, -2.00000715e+000, 4.17269252e-309]]) inner(a, b) Inner product of two arrays. Ordinary inner product of vectors for 1-D arrays (without complex conjugation), in higher dimensions a sum product over the last axes. Parameters ---------- a, b : array_like If `a` and `b` are nonscalar, their last dimensions must match. Returns ------- out : ndarray `out.shape = a.shape[:-1] + b.shape[:-1]` Raises ------ ValueError If the last dimension of `a` and `b` has different size. See Also -------- tensordot : Sum products over arbitrary axes. dot : Generalised matrix product, using second last dimension of `b`. einsum : Einstein summation convention. Notes ----- For vectors (1-D arrays) it computes the ordinary inner-product:: np.inner(a, b) = sum(a[:]*b[:]) More generally, if `ndim(a) = r > 0` and `ndim(b) = s > 0`:: np.inner(a, b) = np.tensordot(a, b, axes=(-1,-1)) or explicitly:: np.inner(a, b)[i0,...,ir-1,j0,...,js-1] = sum(a[i0,...,ir-1,:]*b[j0,...,js-1,:]) In addition `a` or `b` may be scalars, in which case:: np.inner(a,b) = a*b Examples -------- Ordinary inner product for vectors: >>> a = np.array([1,2,3]) >>> b = np.array([0,1,0]) >>> np.inner(a, b) 2 A multidimensional example: >>> a = np.arange(24).reshape((2,3,4)) >>> b = np.arange(4) >>> np.inner(a, b) array([[ 14, 38, 62], [ 86, 110, 134]]) An example where `b` is a scalar: >>> np.inner(np.eye(2), 7) array([[ 7., 0.], [ 0., 7.]]) is_busday(dates, weekmask='1111100', holidays=None, busdaycal=None, out=None) Calculates which of the given dates are valid days, and which are not. .. versionadded:: 1.7.0 Parameters ---------- dates : array_like of datetime64[D] The array of dates to process. weekmask : str or array_like of bool, optional A seven-element array indicating which of Monday through Sunday are valid days. May be specified as a length-seven list or array, like [1,1,1,1,1,0,0]; a length-seven string, like '1111100'; or a string like "Mon Tue Wed Thu Fri", made up of 3-character abbreviations for weekdays, optionally separated by white space. Valid abbreviations are: Mon Tue Wed Thu Fri Sat Sun holidays : array_like of datetime64[D], optional An array of dates to consider as invalid dates. They may be specified in any order, and NaT (not-a-time) dates are ignored. This list is saved in a normalized form that is suited for fast calculations of valid days. busdaycal : busdaycalendar, optional A `busdaycalendar` object which specifies the valid days. If this parameter is provided, neither weekmask nor holidays may be provided. out : array of bool, optional If provided, this array is filled with the result. Returns ------- out : array of bool An array with the same shape as ``dates``, containing True for each valid day, and False for each invalid day. See Also -------- busdaycalendar: An object that specifies a custom set of valid days. busday_offset : Applies an offset counted in valid days. busday_count : Counts how many valid days are in a half-open date range. Examples -------- >>> # The weekdays are Friday, Saturday, and Monday ... np.is_busday(['2011-07-01', '2011-07-02', '2011-07-18'], ... holidays=['2011-07-01', '2011-07-04', '2011-07-17']) array([False, False, True], dtype='bool') lexsort(keys, axis=-1) Perform an indirect stable sort using a sequence of keys. Given multiple sorting keys, which can be interpreted as columns in a spreadsheet, lexsort returns an array of integer indices that describes the sort order by multiple columns. The last key in the sequence is used for the primary sort order, the second-to-last key for the secondary sort order, and so on. The keys argument must be a sequence of objects that can be converted to arrays of the same shape. If a 2D array is provided for the keys argument, it's rows are interpreted as the sorting keys and sorting is according to the last row, second last row etc. Parameters ---------- keys : (k, N) array or tuple containing k (N,)-shaped sequences The `k` different "columns" to be sorted. The last column (or row if `keys` is a 2D array) is the primary sort key. axis : int, optional Axis to be indirectly sorted. By default, sort over the last axis. Returns ------- indices : (N,) ndarray of ints Array of indices that sort the keys along the specified axis. See Also -------- argsort : Indirect sort. ndarray.sort : In-place sort. sort : Return a sorted copy of an array. Examples -------- Sort names: first by surname, then by name. >>> surnames = ('Hertz', 'Galilei', 'Hertz') >>> first_names = ('Heinrich', 'Galileo', 'Gustav') >>> ind = np.lexsort((first_names, surnames)) >>> ind array([1, 2, 0]) >>> [surnames[i] + ", " + first_names[i] for i in ind] ['Galilei, Galileo', 'Hertz, Gustav', 'Hertz, Heinrich'] Sort two columns of numbers: >>> a = [1,5,1,4,3,4,4] # First column >>> b = [9,4,0,4,0,2,1] # Second column >>> ind = np.lexsort((b,a)) # Sort by a, then by b >>> print(ind) [2 0 4 6 5 3 1] >>> [(a[i],b[i]) for i in ind] [(1, 0), (1, 9), (3, 0), (4, 1), (4, 2), (4, 4), (5, 4)] Note that sorting is first according to the elements of ``a``. Secondary sorting is according to the elements of ``b``. A normal ``argsort`` would have yielded: >>> [(a[i],b[i]) for i in np.argsort(a)] [(1, 9), (1, 0), (3, 0), (4, 4), (4, 2), (4, 1), (5, 4)] Structured arrays are sorted lexically by ``argsort``: >>> x = np.array([(1,9), (5,4), (1,0), (4,4), (3,0), (4,2), (4,1)], ... dtype=np.dtype([('x', int), ('y', int)])) >>> np.argsort(x) # or np.argsort(x, order=('x', 'y')) array([2, 0, 4, 6, 5, 3, 1]) may_share_memory(a, b, max_work=None) Determine if two arrays might share memory A return of True does not necessarily mean that the two arrays share any element. It just means that they *might*. Only the memory bounds of a and b are checked by default. Parameters ---------- a, b : ndarray Input arrays max_work : int, optional Effort to spend on solving the overlap problem. See `shares_memory` for details. Default for ``may_share_memory`` is to do a bounds check. Returns ------- out : bool See Also -------- shares_memory Examples -------- >>> np.may_share_memory(np.array([1,2]), np.array([5,8,9])) False >>> x = np.zeros([3, 4]) >>> np.may_share_memory(x[:,0], x[:,1]) True min_scalar_type(a) For scalar ``a``, returns the data type with the smallest size and smallest scalar kind which can hold its value. For non-scalar array ``a``, returns the vector's dtype unmodified. Floating point values are not demoted to integers, and complex values are not demoted to floats. Parameters ---------- a : scalar or array_like The value whose minimal data type is to be found. Returns ------- out : dtype The minimal data type. Notes ----- .. versionadded:: 1.6.0 See Also -------- result_type, promote_types, dtype, can_cast Examples -------- >>> np.min_scalar_type(10) dtype('uint8') >>> np.min_scalar_type(-260) dtype('int16') >>> np.min_scalar_type(3.1) dtype('float16') >>> np.min_scalar_type(1e50) dtype('float64') >>> np.min_scalar_type(np.arange(4,dtype='f8')) dtype('float64') packbits(myarray, axis=None) Packs the elements of a binary-valued array into bits in a uint8 array. The result is padded to full bytes by inserting zero bits at the end. Parameters ---------- myarray : array_like An array of integers or booleans whose elements should be packed to bits. axis : int, optional The dimension over which bit-packing is done. ``None`` implies packing the flattened array. Returns ------- packed : ndarray Array of type uint8 whose elements represent bits corresponding to the logical (0 or nonzero) value of the input elements. The shape of `packed` has the same number of dimensions as the input (unless `axis` is None, in which case the output is 1-D). See Also -------- unpackbits: Unpacks elements of a uint8 array into a binary-valued output array. Examples -------- >>> a = np.array([[[1,0,1], ... [0,1,0]], ... [[1,1,0], ... [0,0,1]]]) >>> b = np.packbits(a, axis=-1) >>> b array([[[160],[64]],[[192],[32]]], dtype=uint8) Note that in binary 160 = 1010 0000, 64 = 0100 0000, 192 = 1100 0000, and 32 = 0010 0000. putmask(a, mask, values) Changes elements of an array based on conditional and input values. Sets ``a.flat[n] = values[n]`` for each n where ``mask.flat[n]==True``. If `values` is not the same size as `a` and `mask` then it will repeat. This gives behavior different from ``a[mask] = values``. Parameters ---------- a : array_like Target array. mask : array_like Boolean mask array. It has to be the same shape as `a`. values : array_like Values to put into `a` where `mask` is True. If `values` is smaller than `a` it will be repeated. See Also -------- place, put, take, copyto Examples -------- >>> x = np.arange(6).reshape(2, 3) >>> np.putmask(x, x>2, x**2) >>> x array([[ 0, 1, 2], [ 9, 16, 25]]) If `values` is smaller than `a` it is repeated: >>> x = np.arange(5) >>> np.putmask(x, x>1, [-33, -44]) >>> x array([ 0, 1, -33, -44, -33]) ravel_multi_index(multi_index, dims, mode='raise', order='C') Converts a tuple of index arrays into an array of flat indices, applying boundary modes to the multi-index. Parameters ---------- multi_index : tuple of array_like A tuple of integer arrays, one array for each dimension. dims : tuple of ints The shape of array into which the indices from ``multi_index`` apply. mode : {'raise', 'wrap', 'clip'}, optional Specifies how out-of-bounds indices are handled. Can specify either one mode or a tuple of modes, one mode per index. * 'raise' -- raise an error (default) * 'wrap' -- wrap around * 'clip' -- clip to the range In 'clip' mode, a negative index which would normally wrap will clip to 0 instead. order : {'C', 'F'}, optional Determines whether the multi-index should be viewed as indexing in row-major (C-style) or column-major (Fortran-style) order. Returns ------- raveled_indices : ndarray An array of indices into the flattened version of an array of dimensions ``dims``. See Also -------- unravel_index Notes ----- .. versionadded:: 1.6.0 Examples -------- >>> arr = np.array([[3,6,6],[4,5,1]]) >>> np.ravel_multi_index(arr, (7,6)) array([22, 41, 37]) >>> np.ravel_multi_index(arr, (7,6), order='F') array([31, 41, 13]) >>> np.ravel_multi_index(arr, (4,6), mode='clip') array([22, 23, 19]) >>> np.ravel_multi_index(arr, (4,4), mode=('clip','wrap')) array([12, 13, 13]) >>> np.ravel_multi_index((3,1,4,1), (6,7,8,9)) 1621 result_type(*arrays_and_dtypes) Returns the type that results from applying the NumPy type promotion rules to the arguments. Type promotion in NumPy works similarly to the rules in languages like C++, with some slight differences. When both scalars and arrays are used, the array's type takes precedence and the actual value of the scalar is taken into account. For example, calculating 3*a, where a is an array of 32-bit floats, intuitively should result in a 32-bit float output. If the 3 is a 32-bit integer, the NumPy rules indicate it can't convert losslessly into a 32-bit float, so a 64-bit float should be the result type. By examining the value of the constant, '3', we see that it fits in an 8-bit integer, which can be cast losslessly into the 32-bit float. Parameters ---------- arrays_and_dtypes : list of arrays and dtypes The operands of some operation whose result type is needed. Returns ------- out : dtype The result type. See also -------- dtype, promote_types, min_scalar_type, can_cast Notes ----- .. versionadded:: 1.6.0 The specific algorithm used is as follows. Categories are determined by first checking which of boolean, integer (int/uint), or floating point (float/complex) the maximum kind of all the arrays and the scalars are. If there are only scalars or the maximum category of the scalars is higher than the maximum category of the arrays, the data types are combined with :func:`promote_types` to produce the return value. Otherwise, `min_scalar_type` is called on each array, and the resulting data types are all combined with :func:`promote_types` to produce the return value. The set of int values is not a subset of the uint values for types with the same number of bits, something not reflected in :func:`min_scalar_type`, but handled as a special case in `result_type`. Examples -------- >>> np.result_type(3, np.arange(7, dtype='i1')) dtype('int8') >>> np.result_type('i4', 'c8') dtype('complex128') >>> np.result_type(3.0, -2) dtype('float64') shares_memory(a, b, max_work=None) Determine if two arrays share memory Parameters ---------- a, b : ndarray Input arrays max_work : int, optional Effort to spend on solving the overlap problem (maximum number of candidate solutions to consider). The following special values are recognized: max_work=MAY_SHARE_EXACT (default) The problem is solved exactly. In this case, the function returns True only if there is an element shared between the arrays. max_work=MAY_SHARE_BOUNDS Only the memory bounds of a and b are checked. Raises ------ numpy.TooHardError Exceeded max_work. Returns ------- out : bool See Also -------- may_share_memory Examples -------- >>> np.may_share_memory(np.array([1,2]), np.array([5,8,9])) False unpackbits(myarray, axis=None) Unpacks elements of a uint8 array into a binary-valued output array. Each element of `myarray` represents a bit-field that should be unpacked into a binary-valued output array. The shape of the output array is either 1-D (if `axis` is None) or the same shape as the input array with unpacking done along the axis specified. Parameters ---------- myarray : ndarray, uint8 type Input array. axis : int, optional The dimension over which bit-unpacking is done. ``None`` implies unpacking the flattened array. Returns ------- unpacked : ndarray, uint8 type The elements are binary-valued (0 or 1). See Also -------- packbits : Packs the elements of a binary-valued array into bits in a uint8 array. Examples -------- >>> a = np.array([[2], [7], [23]], dtype=np.uint8) >>> a array([[ 2], [ 7], [23]], dtype=uint8) >>> b = np.unpackbits(a, axis=1) >>> b array([[0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 0, 1, 1, 1]], dtype=uint8) unravel_index(indices, shape, order='C') Converts a flat index or array of flat indices into a tuple of coordinate arrays. Parameters ---------- indices : array_like An integer array whose elements are indices into the flattened version of an array of dimensions ``shape``. Before version 1.6.0, this function accepted just one index value. shape : tuple of ints The shape of the array to use for unraveling ``indices``. .. versionchanged:: 1.16.0 Renamed from ``dims`` to ``shape``. order : {'C', 'F'}, optional Determines whether the indices should be viewed as indexing in row-major (C-style) or column-major (Fortran-style) order. .. versionadded:: 1.6.0 Returns ------- unraveled_coords : tuple of ndarray Each array in the tuple has the same shape as the ``indices`` array. See Also -------- ravel_multi_index Examples -------- >>> np.unravel_index([22, 41, 37], (7,6)) (array([3, 6, 6]), array([4, 5, 1])) >>> np.unravel_index([31, 41, 13], (7,6), order='F') (array([3, 6, 6]), array([4, 5, 1])) >>> np.unravel_index(1621, (6,7,8,9)) (3, 1, 4, 1) vdot(a, b) Return the dot product of two vectors. The vdot(`a`, `b`) function handles complex numbers differently than dot(`a`, `b`). If the first argument is complex the complex conjugate of the first argument is used for the calculation of the dot product. Note that `vdot` handles multidimensional arrays differently than `dot`: it does *not* perform a matrix product, but flattens input arguments to 1-D vectors first. Consequently, it should only be used for vectors. Parameters ---------- a : array_like If `a` is complex the complex conjugate is taken before calculation of the dot product. b : array_like Second argument to the dot product. Returns ------- output : ndarray Dot product of `a` and `b`. Can be an int, float, or complex depending on the types of `a` and `b`. See Also -------- dot : Return the dot product without using the complex conjugate of the first argument. Examples -------- >>> a = np.array([1+2j,3+4j]) >>> b = np.array([5+6j,7+8j]) >>> np.vdot(a, b) (70-8j) >>> np.vdot(b, a) (70+8j) Note that higher-dimensional arrays are flattened! >>> a = np.array([[1, 4], [5, 6]]) >>> b = np.array([[4, 1], [2, 2]]) >>> np.vdot(a, b) 30 >>> np.vdot(b, a) 30 >>> 1*4 + 4*1 + 5*2 + 6*2 30 where(condition, [x, y]) Return elements chosen from `x` or `y` depending on `condition`. .. note:: When only `condition` is provided, this function is a shorthand for ``np.asarray(condition).nonzero()``. Using `nonzero` directly should be preferred, as it behaves correctly for subclasses. The rest of this documentation covers only the case where all three arguments are provided. Parameters ---------- condition : array_like, bool Where True, yield `x`, otherwise yield `y`. x, y : array_like Values from which to choose. `x`, `y` and `condition` need to be broadcastable to some shape. Returns ------- out : ndarray An array with elements from `x` where `condition` is True, and elements from `y` elsewhere. See Also -------- choose nonzero : The function that is called when x and y are omitted Notes ----- If all the arrays are 1-D, `where` is equivalent to:: [xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples -------- >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ... [[1, 2], [3, 4]], ... [[9, 8], [7, 6]]) array([[1, 8], [3, 4]]) The shapes of x, y, and the condition are broadcast together: >>> x, y = np.ogrid[:3, :4] >>> np.where(x < y, x, 10 + y) # both x and 10+y are broadcast array([[10, 0, 0, 0], [10, 11, 1, 1], [10, 11, 12, 2]]) >>> a = np.array([[0, 1, 2], ... [0, 2, 4], ... [0, 3, 6]]) >>> np.where(a < 4, a, -1) # -1 is broadcast array([[ 0, 1, 2], [ 0, 2, -1], [ 0, 3, -1]]) Create the numpy.core.multiarray namespace for backward compatibility. In v1.16 the multiarray and umath c-extension modules were merged into a single _multiarray_umath extension module. So we replicate the old namespace by importing from the extension module. For backward compatibility, make sure pickle imports these functions from here We can't verify dispatcher signatures because NumPy's C functions don't support introspection. optimize for the typical case where only arrays is provided
39,670
en
0.65637
# Copyright (c) 2021 PaddlePaddle Authors. 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. import os import csv import math import json import time import random import logging import functools import traceback from collections import defaultdict from _thread import start_new_thread from multiprocessing import Queue, Process import numpy as np from tqdm import tqdm import paddle import paddle.distributed as dist def set_seed(seed): """Set seed for reproduction. """ seed = seed + dist.get_rank() random.seed(seed) np.random.seed(seed) paddle.seed(seed) os.environ['PYTHONHASHSEED'] = str(seed) def set_logger(args): """Write logs to console and log file. """ log_file = os.path.join(args.save_path, 'train.log') logging.basicConfig( format='%(asctime)s %(levelname)-8s %(message)s', level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S', filename=log_file, filemode='a+') if args.print_on_screen: console = logging.StreamHandler() console.setLevel(logging.INFO) formatter = logging.Formatter( '%(asctime)s %(levelname)-8s %(message)s') console.setFormatter(formatter) logging.getLogger('').addHandler(console) for arg in vars(args): logging.info('{:20}:{}'.format(arg, getattr(args, arg))) def print_log(step, interval, log, timer, time_sum): """Print log to logger. """ logging.info( '[GPU %d] step: %d, loss: %.5f, reg: %.4e, speed: %.2f steps/s, time: %.2f s' % (dist.get_rank(), step, log['loss'] / interval, log['reg'] / interval, interval / time_sum, time_sum)) logging.info('sample: %f, forward: %f, backward: %f, update: %f' % ( timer['sample'], timer['forward'], timer['backward'], timer['update'])) def uniform(low, high, size, dtype=np.float32, seed=0): """Memory efficient uniform implementation. """ rng = np.random.default_rng(seed) out = (high - low) * rng.random(size, dtype=dtype) + low return out def timer_wrapper(name): """Time counter wrapper. """ def decorate(func): """decorate func """ @functools.wraps(func) def wrapper(*args, **kwargs): """wrapper func """ logging.info(f'[{name}] start...') ts = time.time() result = func(*args, **kwargs) te = time.time() costs = te - ts if costs < 1e-4: cost_str = '%f sec' % costs elif costs > 3600: cost_str = '%.4f sec (%.4f hours)' % (costs, costs / 3600.) else: cost_str = '%.4f sec' % costs logging.info(f'[{name}] finished! It takes {cost_str} s') return result return wrapper return decorate def calculate_metrics(scores, corr_idxs, filter_list): """Calculate metrics according to scores. """ logs = [] for i in range(scores.shape[0]): rank = (scores[i] > scores[i][corr_idxs[i]]).astype('float32') if filter_list is not None: mask = paddle.ones(rank.shape, dtype='float32') mask[filter_list[i]] = 0. rank = rank * mask rank = paddle.sum(rank) + 1 logs.append({ 'MRR': 1.0 / rank, 'MR': float(rank), 'HITS@1': 1.0 if rank <= 1 else 0.0, 'HITS@3': 1.0 if rank <= 3 else 0.0, 'HITS@10': 1.0 if rank <= 10 else 0.0, }) return logs def evaluate_wikikg2(model, loader, mode, save_path): from ogb.linkproppred import Evaluator evaluator = Evaluator(name='ogbl-wikikg2') model.eval() with paddle.no_grad(): y_pred_pos = [] y_pred_neg = [] for h, r, t, neg_h, neg_t in tqdm(loader): pos_h = model._get_ent_embedding(h) pos_r = model._get_rel_embedding(r) pos_t = model._get_ent_embedding(t) y_pred_pos.append(model(pos_h, pos_r, pos_t).numpy()) y_neg_head = model.predict(t, r, neg_h, mode='head').numpy() y_neg_tail = model.predict(h, r, neg_t, mode='tail').numpy() y_pred_neg.append(np.concatenate([y_neg_head, y_neg_tail], axis=1)) y_pred_pos = np.concatenate(y_pred_pos, axis=0) y_pred_neg = np.concatenate(y_pred_neg, axis=0) input_dict = {'y_pred_pos': y_pred_pos, 'y_pred_neg': y_pred_neg} result = evaluator.eval(input_dict) logging.info('-- %s results ------------' % mode) logging.info(' ' + ' '.join( ['{}: {}'.format(k, v.mean()) for k, v in result.items()])) def evaluate_wikikg90m(model, loader, mode, save_path): from ogb.lsc import WikiKG90MEvaluator evaluator = WikiKG90MEvaluator() model.eval() with paddle.no_grad(): top_tens = [] corr_idx = [] for h, r, t_idx, cand_t in tqdm(loader): score = model.predict(h, r, cand_t) rank = paddle.argsort(score, axis=1, descending=True) top_tens.append(rank[:, :10].numpy()) corr_idx.append(t_idx.numpy()) t_pred_top10 = np.concatenate(top_tens, axis=0) t_correct_index = np.concatenate(corr_idx, axis=0) input_dict = {} if mode == 'valid': input_dict['h,r->t'] = { 't_pred_top10': t_pred_top10, 't_correct_index': t_correct_index } result = evaluator.eval(input_dict) logging.info('-- %s results -------------' % mode) logging.info(' '.join( ['{}: {}'.format(k, v) for k, v in result.items()])) else: input_dict['h,r->t'] = {'t_pred_top10': t_pred_top10} evaluator.save_test_submission( input_dict=input_dict, dir_path=save_path) @timer_wrapper('evaluation') def evaluate(model, loader, evaluate_mode='test', filter_dict=None, save_path='./tmp/', data_mode='hrt'): """Evaluate given KGE model. """ if data_mode == 'wikikg2': evaluate_wikikg2(model, loader, evaluate_mode, save_path) elif data_mode == 'wikikg90m': evaluate_wikikg90m(model, loader, evaluate_mode, save_path) else: model.eval() with paddle.no_grad(): h_metrics = [] t_metrics = [] output = {'h,r->t': {}, 't,r->h': {}, 'average': {}} for h, r, t in tqdm(loader): t_score = model.predict(h, r, mode='tail') h_score = model.predict(t, r, mode='head') if filter_dict is not None: h_filter_list = [ filter_dict['head'][(ti, ri)] for ti, ri in zip(t.numpy(), r.numpy()) ] t_filter_list = [ filter_dict['tail'][(hi, ri)] for hi, ri in zip(h.numpy(), r.numpy()) ] else: h_filter_list = None t_filter_list = None h_metrics += calculate_metrics(h_score, h, h_filter_list) t_metrics += calculate_metrics(t_score, t, t_filter_list) for metric in h_metrics[0].keys(): output['t,r->h'][metric] = np.mean( [x[metric] for x in h_metrics]) output['h,r->t'][metric] = np.mean( [x[metric] for x in t_metrics]) output['average'][metric] = ( output['t,r->h'][metric] + output['h,r->t'][metric]) / 2 logging.info('-------------- %s result --------------' % evaluate_mode) logging.info('t,r->h |' + ' '.join( ['{}: {}'.format(k, v) for k, v in output['t,r->h'].items()])) logging.info('h,r->t |' + ' '.join( ['{}: {}'.format(k, v) for k, v in output['h,r->t'].items()])) logging.info('average |' + ' '.join( ['{}: {}'.format(k, v) for k, v in output['average'].items()])) logging.info('-----------------------------------------') def gram_schimidt_process(embeds, num_elem, use_scale): """ Orthogonalize embeddings. """ num_embed = embeds.shape[0] assert embeds.shape[1] == num_elem assert embeds.shape[2] == (num_elem + int(use_scale)) if use_scale: scales = embeds[:, :, -1] embeds = embeds[:, :, :num_elem] u = [embeds[:, 0]] uu = [0] * num_elem uu[0] = (u[0] * u[0]).sum(axis=-1) u_d = embeds[:, 1:] ushape = (num_embed, 1, -1) for i in range(1, num_elem): tmp_a = (embeds[:, i:] * u[i - 1].reshape(ushape)).sum(axis=-1) tmp_b = uu[i - 1].reshape((num_embed, -1)) tmp_u = (tmp_a / tmp_b).reshape((num_embed, -1, 1)) u_d = u_d - u[-1].reshape(ushape) * tmp_u u_i = u_d[:, 0] if u_d.shape[1] > 1: u_d = u_d[:, 1:] uu[i] = (u_i * u_i).sum(axis=-1) u.append(u_i) u = np.stack(u, axis=1) u_norm = np.linalg.norm(u, axis=-1, keepdims=True) u = u / u_norm if use_scale: u = np.concatenate([u, scales.reshape((num_embed, -1, 1))], axis=-1) return u
apps/Graph4KG/utils.py
9,832
Calculate metrics according to scores. decorate func Evaluate given KGE model. Orthogonalize embeddings. Print log to logger. Write logs to console and log file. Set seed for reproduction. Time counter wrapper. Memory efficient uniform implementation. wrapper func Copyright (c) 2021 PaddlePaddle Authors. 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.
912
en
0.817816
# Copyright 2018 Contributors to Hyperledger Sawtooth # # 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. # ----------------------------------------------------------------------------- """Test User Addresser""" import logging import pytest from rbac.common import addresser from tests.rbac.common.assertions import TestAssertions LOGGER = logging.getLogger(__name__) @pytest.mark.addressing @pytest.mark.library class TestUserAddresser(TestAssertions): """Test User Addresser""" def test_address(self): """Tests address makes an address that identifies as the correct AddressSpace""" user_id = addresser.user.unique_id() user_address = addresser.user.address(user_id) self.assertIsAddress(user_address) self.assertEqual( addresser.get_address_type(user_address), addresser.AddressSpace.USER ) def test_unique_id(self): """Tests that unique_id generates a unique identifier and is unique""" id1 = addresser.user.unique_id() id2 = addresser.user.unique_id() self.assertIsIdentifier(id1) self.assertIsIdentifier(id2) self.assertNotEqual(id1, id2) def test_get_address_type(self): """Tests that get_address_type returns AddressSpace.USER if it is a user address, and None if it is of another address type""" user_address = addresser.user.address(addresser.user.unique_id()) role_address = addresser.role.address(addresser.role.unique_id()) self.assertEqual( addresser.get_address_type(user_address), addresser.AddressSpace.USER ) self.assertEqual( addresser.user.get_address_type(user_address), addresser.AddressSpace.USER ) self.assertIsNone(addresser.user.get_address_type(role_address)) self.assertEqual( addresser.get_address_type(role_address), addresser.AddressSpace.ROLES_ATTRIBUTES, ) def test_get_addresser(self): """Test that get_addresser returns the addresser class if it is a user address, and None if it is of another address type""" user_address = addresser.user.address(addresser.user.unique_id()) other_address = addresser.role.address(addresser.role.unique_id()) self.assertIsInstance( addresser.get_addresser(user_address), type(addresser.user) ) self.assertIsInstance( addresser.user.get_addresser(user_address), type(addresser.user) ) self.assertIsNone(addresser.user.get_addresser(other_address)) def test_user_parse(self): """Test addresser.user.parse returns a parsed address if it is a user address""" user_id = addresser.user.unique_id() user_address = addresser.user.address(user_id) parsed = addresser.user.parse(user_address) self.assertEqual(parsed.object_type, addresser.ObjectType.USER) self.assertEqual(parsed.related_type, addresser.ObjectType.NONE) self.assertEqual( parsed.relationship_type, addresser.RelationshipType.ATTRIBUTES ) self.assertEqual(parsed.address_type, addresser.AddressSpace.USER) self.assertEqual(parsed.object_id, user_id) self.assertEqual(parsed.related_id, None) def test_addresser_parse(self): """Test addresser.parse returns a parsed address""" user_id = addresser.user.unique_id() user_address = addresser.user.address(user_id) parsed = addresser.parse(user_address) self.assertEqual(parsed.object_type, addresser.ObjectType.USER) self.assertEqual(parsed.related_type, addresser.ObjectType.NONE) self.assertEqual( parsed.relationship_type, addresser.RelationshipType.ATTRIBUTES ) self.assertEqual(parsed.address_type, addresser.AddressSpace.USER) self.assertEqual(parsed.object_id, user_id) self.assertEqual(parsed.related_id, None) def test_parse_other(self): """Test that parse returns None if it is not a user address""" other_address = addresser.role.address(addresser.role.unique_id()) self.assertIsNone(addresser.user.parse(other_address)) def test_addresses_are(self): """Test that addresses_are returns True if all addresses are a user addresses, and False if any addresses are if a different address type""" user_address1 = addresser.user.address(addresser.user.unique_id()) user_address2 = addresser.user.address(addresser.user.unique_id()) other_address = addresser.role.address(addresser.role.unique_id()) self.assertTrue(addresser.user.addresses_are([user_address1])) self.assertTrue(addresser.user.addresses_are([user_address1, user_address2])) self.assertFalse(addresser.user.addresses_are([other_address])) self.assertFalse(addresser.user.addresses_are([user_address1, other_address])) self.assertFalse(addresser.user.addresses_are([other_address, user_address1])) self.assertTrue(addresser.user.addresses_are([])) def test_address_deterministic(self): """Tests address makes an address that identifies as the correct AddressSpace""" user_id1 = addresser.user.unique_id() user_address1 = addresser.user.address(user_id1) user_address2 = addresser.user.address(user_id1) self.assertIsAddress(user_address1) self.assertIsAddress(user_address2) self.assertEqual(user_address1, user_address2) self.assertEqual( addresser.get_address_type(user_address1), addresser.AddressSpace.USER ) def test_address_random(self): """Tests address makes a unique address given different inputs""" user_id1 = addresser.user.unique_id() user_id2 = addresser.user.unique_id() user_address1 = addresser.user.address(user_id1) user_address2 = addresser.user.address(user_id2) self.assertIsAddress(user_address1) self.assertIsAddress(user_address2) self.assertNotEqual(user_address1, user_address2) self.assertEqual( addresser.get_address_type(user_address1), addresser.AddressSpace.USER ) self.assertEqual( addresser.get_address_type(user_address2), addresser.AddressSpace.USER )
tests/rbac/common/addresser/user_test.py
6,839
Test User Addresser Tests address makes an address that identifies as the correct AddressSpace Tests address makes an address that identifies as the correct AddressSpace Tests address makes a unique address given different inputs Test addresser.parse returns a parsed address Test that addresses_are returns True if all addresses are a user addresses, and False if any addresses are if a different address type Tests that get_address_type returns AddressSpace.USER if it is a user address, and None if it is of another address type Test that get_addresser returns the addresser class if it is a user address, and None if it is of another address type Test that parse returns None if it is not a user address Tests that unique_id generates a unique identifier and is unique Test addresser.user.parse returns a parsed address if it is a user address Test User Addresser Copyright 2018 Contributors to Hyperledger Sawtooth 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. -----------------------------------------------------------------------------
1,521
en
0.824402
# List the type colors for the editor AIR = (0, 0, 0) GRASS = (100, 200, 40) ROCK = (106, 106, 106) LAVA = (252, 144, 3) WATER = (0, 0, 255) PLAYER = (155, 191, 250) PLAYER_END = (40, 30, 100) SPIKE_UP = (204, 24, 24) SPIKE_DOWN = (166, 8, 8) # List all the used types types = ['GRASS', 'ROCK', 'LAVA', 'WATER', 'PLAYER', 'SPIKE_UP', 'SPIKE_DOWN', 'PLAYER_END', 'AIR'] colorTypes = [GRASS, ROCK, LAVA, WATER, PLAYER, SPIKE_UP, SPIKE_DOWN, PLAYER_END, AIR] # Set default type select = 'GRASS' colorSelect = GRASS
tools/LevelCreator/ezTypes.py
514
List the type colors for the editor List all the used types Set default type
76
en
0.49178
""" Matrix operations for neuroswarms models. Author: Joseph Monaco (jmonaco@jhu.edu) Affiliation: Johns Hopkins University Created: 2019-05-12 Updated: 2020-11-16 Related paper: Monaco, J.D., Hwang, G.M., Schultz, K.M. et al. Cognitive swarming in complex environments with attractor dynamics and oscillatory computing. Biol Cybern 114, 269–284 (2020). https://doi.org/10.1007/s00422-020-00823-z This software is provided AS IS under the terms of the Open Source MIT License. See http://www.opensource.org/licenses/mit-license.php """ __all__ = ('tile_index', 'pairwise_tile_index', 'pairwise_distances', 'distances', 'pairwise_phasediffs', 'pairwise_unit_diffs', 'somatic_motion_update', 'reward_motion_update') from numpy import (empty, zeros, newaxis as AX, swapaxes, hypot, sin, inf, broadcast_arrays, broadcast_to) from .utils.types import * DEBUGGING = False def _check_ndim(Mstr, M, ndim): assert M.ndim == ndim, f'{Mstr}.ndim != {ndim}' def _check_shape(Mstr, M, shape, axis=None): if axis is None: assert M.shape == shape, f'{Mstr}.shape != {shape}' else: assert M.shape[axis] == shape, f'{Mstr}.shape[{axis}] != {shape}' def tile_index(A, B): """ Entrywise comparison index of tile index (column) vectors. """ AA, BB = broadcast_arrays(A, B) if DEBUGGING: shape = (max(A.shape[0], B.shape[0]), 1) _check_shape('AA', AA, shape) _check_shape('BB', BB, shape) return (AA, BB) def pairwise_tile_index(A, B): """ Pairwise comparison index of tile index (column) vectors. """ AA, BB = broadcast_arrays(A, B.T) if DEBUGGING: shape = (len(A), len(B)) _check_shape('AA', AA, shape) _check_shape('BB', BB, shape) return (AA, BB) def pairwise_phasediffs(A, B): """ Compute synchronizing phase differences between phase pairs. """ N_A = len(A) N_B = len(B) DD_shape = (N_A, N_B) if DEBUGGING: _check_ndim('A', A, 2) _check_ndim('B', B, 2) _check_shape('A', A, 1, axis=1) _check_shape('B', B, 1, axis=1) return B.T - A def distances(A, B): """ Compute distances between points in entrywise order. """ AA, BB = broadcast_arrays(A, B) shape = AA.shape if DEBUGGING: _check_ndim('AA', AA, 2) _check_ndim('BB', BB, 2) _check_shape('AA', AA, 2, axis=1) _check_shape('BB', BB, 2, axis=1) return hypot(AA[:,0] - BB[:,0], AA[:,1] - BB[:,1])[:,AX] def pairwise_unit_diffs(A, B): """ Compute attracting unit-vector differences between pairs of points. """ DD = pairwise_position_deltas(A, B) D_norm = hypot(DD[...,0], DD[...,1]) nz = D_norm.nonzero() DD[nz] /= D_norm[nz][...,AX] return DD def pairwise_distances(A, B): """ Compute distances between pairs of points. """ DD = pairwise_position_deltas(A, B) return hypot(DD[...,0], DD[...,1]) def pairwise_position_deltas(A, B): """ Compute attracting component deltas between pairs of points. """ N_A = len(A) N_B = len(B) if DEBUGGING: _check_ndim('A', A, 2) _check_ndim('B', B, 2) _check_shape('A', A, 2, axis=1) _check_shape('B', B, 2, axis=1) # Broadcast the first position matrix AA = empty((N_A,N_B,2), DISTANCE_DTYPE) AA[:] = A[:,AX,:] return B[AX,...] - AA def somatic_motion_update(D_up, D_cur, X, V): """ Compute updated positions by averaging pairwise difference vectors for mutually visible pairs with equal bidirectional adjustments within each pair. The updated distance matrix does not need to be symmetric; it represents 'desired' updates based on recurrent learning. :D_up: R(N,N)-matrix of updated distances :D_cur: R(N,N)-matrix of current distances :X: R(N,2)-matrix of current positions :V: {0,1}(N,2)-matrix of current agent visibility :returns: R(N,2)-matrix of updated positions """ N = len(X) D_shape = (N, N) if DEBUGGING: _check_ndim('X', X, 2) _check_shape('X', X, 2, axis=1) _check_shape('D_up', D_up, D_shape) _check_shape('D_cur', D_cur, D_shape) _check_shape('V', V, D_shape) # Broadcast field position matrix and its transpose XX = empty((N,N,2)) XX[:] = X[:,AX,:] XT = swapaxes(XX, 0, 1) # Find visible & valid values (i.e., corresponding to non-zero weights) # # NOTE: The normalizing factor is divided by 2 because the somatic update # represents one half of the change in distance between a pair of units. D_inf = D_up == inf norm = V * ~D_inf N = norm.sum(axis=1) valid = N.nonzero()[0] norm[valid] /= 2*N[valid,AX] # Zero out the inf elements of the updated distance matrix and corresponding # elements in the current distance matrix D_up[D_inf] = D_cur[D_inf] = 0.0 # Construct the agent-agent avoidant unit vectors DX = XX - XT DX_norm = hypot(DX[...,0], DX[...,1]) valid = DX_norm.nonzero() DX[valid] /= DX_norm[valid][:,AX] return (norm[...,AX]*(D_up - D_cur)[...,AX]*DX).sum(axis=1) def reward_motion_update(D_up, D_cur, X, R, V): """ Compute updated positions by averaging reward-based unit vectors for adjustments of the point only. The updated distance matrix represents 'desired' updates based on reward learning. :D_up: R(N,N_R)-matrix of updated distances between points and rewards :D_cur: R(N,N_R)-matrix of current distances between points and rewards :X: R(N,2)-matrix of current point positions :R: R(N_R,2)-matrix of current reward positions :V: {0,1}(N_R,2)-matrix of current agent-reward visibility :returns: R(N,2)-matrix of updated positions """ N = len(X) N_R = len(R) D_shape = (N, N_R) if DEBUGGING: _check_ndim('X', X, 2) _check_ndim('R', R, 2) _check_shape('X', X, 2, axis=1) _check_shape('R', R, 2, axis=1) _check_shape('D_up', D_up, D_shape) _check_shape('D_cur', D_cur, D_shape) _check_shape('V', V, D_shape) # Broadcast field position matrix XX = empty((N,N_R,2)) XX[:] = X[:,AX,:] # Find valid values (i.e., corresponding to non-zero weights) D_inf = D_up == inf norm = V * ~D_inf N = norm.sum(axis=1) valid = N.nonzero()[0] norm[valid] /= N[valid,AX] # Zero out the inf elements of the updated distance matrix and corresponding # elements in the current distance matrix D_up[D_inf] = D_cur[D_inf] = 0.0 # Construct the agent-reward avoidant unit vectors DR = XX - R[AX] DR_norm = hypot(DR[...,0], DR[...,1]) valid = DR_norm.nonzero() DR[valid] /= DR_norm[valid][:,AX] return (norm[...,AX]*(D_up - D_cur)[...,AX]*DR).sum(axis=1)
neuroswarms/matrix.py
6,867
Compute distances between points in entrywise order. Compute distances between pairs of points. Compute synchronizing phase differences between phase pairs. Compute attracting component deltas between pairs of points. Pairwise comparison index of tile index (column) vectors. Compute attracting unit-vector differences between pairs of points. Compute updated positions by averaging reward-based unit vectors for adjustments of the point only. The updated distance matrix represents 'desired' updates based on reward learning. :D_up: R(N,N_R)-matrix of updated distances between points and rewards :D_cur: R(N,N_R)-matrix of current distances between points and rewards :X: R(N,2)-matrix of current point positions :R: R(N_R,2)-matrix of current reward positions :V: {0,1}(N_R,2)-matrix of current agent-reward visibility :returns: R(N,2)-matrix of updated positions Compute updated positions by averaging pairwise difference vectors for mutually visible pairs with equal bidirectional adjustments within each pair. The updated distance matrix does not need to be symmetric; it represents 'desired' updates based on recurrent learning. :D_up: R(N,N)-matrix of updated distances :D_cur: R(N,N)-matrix of current distances :X: R(N,2)-matrix of current positions :V: {0,1}(N,2)-matrix of current agent visibility :returns: R(N,2)-matrix of updated positions Entrywise comparison index of tile index (column) vectors. Matrix operations for neuroswarms models. Author: Joseph Monaco (jmonaco@jhu.edu) Affiliation: Johns Hopkins University Created: 2019-05-12 Updated: 2020-11-16 Related paper: Monaco, J.D., Hwang, G.M., Schultz, K.M. et al. Cognitive swarming in complex environments with attractor dynamics and oscillatory computing. Biol Cybern 114, 269–284 (2020). https://doi.org/10.1007/s00422-020-00823-z This software is provided AS IS under the terms of the Open Source MIT License. See http://www.opensource.org/licenses/mit-license.php Broadcast the first position matrix Broadcast field position matrix and its transpose Find visible & valid values (i.e., corresponding to non-zero weights) NOTE: The normalizing factor is divided by 2 because the somatic update represents one half of the change in distance between a pair of units. Zero out the inf elements of the updated distance matrix and corresponding elements in the current distance matrix Construct the agent-agent avoidant unit vectors Broadcast field position matrix Find valid values (i.e., corresponding to non-zero weights) Zero out the inf elements of the updated distance matrix and corresponding elements in the current distance matrix Construct the agent-reward avoidant unit vectors
2,681
en
0.800056
# Copyright 2018-2021 Xanadu Quantum Technologies Inc. # 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. """ This submodule contains the discrete-variable quantum operations that perform arithmetic operations on their input states. """ # pylint:disable=abstract-method,arguments-differ,protected-access import numpy as np import pennylane as qml from pennylane.operation import Operation class QubitCarry(Operation): r"""QubitCarry(wires) Apply the ``QubitCarry`` operation to four input wires. This operation performs the transformation: .. math:: |a\rangle |b\rangle |c\rangle |d\rangle \rightarrow |a\rangle |b\rangle |b\oplus c\rangle |bc \oplus d\oplus (b\oplus c)a\rangle .. figure:: ../../_static/ops/QubitCarry.svg :align: center :width: 60% :target: javascript:void(0); See `here <https://arxiv.org/abs/quant-ph/0008033v1>`__ for more information. .. note:: The first wire should be used to input a carry bit from previous operations. The final wire holds the carry bit of this operation and the input state on this wire should be :math:`|0\rangle`. **Details:** * Number of wires: 4 * Number of parameters: 0 Args: wires (Sequence[int]): the wires the operation acts on **Example** The ``QubitCarry`` operation maps the state :math:`|0110\rangle` to :math:`|0101\rangle`, where the last qubit denotes the carry value: .. code-block:: input_bitstring = (0, 1, 1, 0) @qml.qnode(dev) def circuit(basis_state): qml.BasisState(basis_state, wires=[0, 1, 2, 3]) qml.QubitCarry(wires=[0, 1, 2, 3]) return qml.probs(wires=[0, 1, 2, 3]) probs = circuit(input_bitstring) probs_indx = np.argwhere(probs == 1).flatten()[0] bitstrings = list(itertools.product(range(2), repeat=4)) output_bitstring = bitstrings[probs_indx] The output bitstring is >>> output_bitstring (0, 1, 0, 1) The action of ``QubitCarry`` is to add wires ``1`` and ``2``. The modulo-two result is output in wire ``2`` with a carry value output in wire ``3``. In this case, :math:`1 \oplus 1 = 0` with a carry, so we have: >>> bc_sum = output_bitstring[2] >>> bc_sum 0 >>> carry = output_bitstring[3] >>> carry 1 """ num_wires = 4 num_params = 0 _mat = np.array( [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], ] ) @classmethod def _matrix(cls, *params): return QubitCarry._mat @staticmethod def decomposition(wires): decomp_ops = [ qml.Toffoli(wires=wires[1:]), qml.CNOT(wires=[wires[1], wires[2]]), qml.Toffoli(wires=[wires[0], wires[2], wires[3]]), ] return decomp_ops class QubitSum(Operation): r"""QubitSum(wires) Apply a ``QubitSum`` operation on three input wires. This operation performs the transformation: .. math:: |a\rangle |b\rangle |c\rangle \rightarrow |a\rangle |b\rangle |a\oplus b\oplus c\rangle .. figure:: ../../_static/ops/QubitSum.svg :align: center :width: 40% :target: javascript:void(0); See `here <https://arxiv.org/abs/quant-ph/0008033v1>`__ for more information. **Details:** * Number of wires: 3 * Number of parameters: 0 Args: wires (Sequence[int]): the wires the operation acts on **Example** The ``QubitSum`` operation maps the state :math:`|010\rangle` to :math:`|011\rangle`, with the final wire holding the modulo-two sum of the first two wires: .. code-block:: input_bitstring = (0, 1, 0) @qml.qnode(dev) def circuit(basis_state): qml.BasisState(basis_state, wires = [0, 1, 2]) qml.QubitSum(wires=[0, 1, 2]) return qml.probs(wires=[0, 1, 2]) probs = circuit(input_bitstring) probs_indx = np.argwhere(probs == 1).flatten()[0] bitstrings = list(itertools.product(range(2), repeat=3)) output_bitstring = bitstrings[probs_indx] The output bitstring is >>> output_bitstring (0, 1, 1) The action of ``QubitSum`` is to add wires ``0``, ``1``, and ``2``. The modulo-two result is output in wire ``2``. In this case, :math:`0 \oplus 1 \oplus 0 = 1`, so we have: >>> abc_sum = output_bitstring[2] >>> abc_sum 1 """ num_wires = 3 num_params = 0 _mat = np.array( [ [1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1], ] ) def label(self, decimals=None, base_label=None): return super().label(decimals=decimals, base_label=base_label or "Σ") @classmethod def _matrix(cls, *params): return QubitSum._mat @staticmethod def decomposition(wires): decomp_ops = [ qml.CNOT(wires=[wires[1], wires[2]]), qml.CNOT(wires=[wires[0], wires[2]]), ] return decomp_ops def adjoint(self): return QubitSum(wires=self.wires)
pennylane/ops/qubit/arithmetic_ops.py
6,771
QubitCarry(wires) Apply the ``QubitCarry`` operation to four input wires. This operation performs the transformation: .. math:: |a\rangle |b\rangle |c\rangle |d\rangle \rightarrow |a\rangle |b\rangle |b\oplus c\rangle |bc \oplus d\oplus (b\oplus c)a\rangle .. figure:: ../../_static/ops/QubitCarry.svg :align: center :width: 60% :target: javascript:void(0); See `here <https://arxiv.org/abs/quant-ph/0008033v1>`__ for more information. .. note:: The first wire should be used to input a carry bit from previous operations. The final wire holds the carry bit of this operation and the input state on this wire should be :math:`|0\rangle`. **Details:** * Number of wires: 4 * Number of parameters: 0 Args: wires (Sequence[int]): the wires the operation acts on **Example** The ``QubitCarry`` operation maps the state :math:`|0110\rangle` to :math:`|0101\rangle`, where the last qubit denotes the carry value: .. code-block:: input_bitstring = (0, 1, 1, 0) @qml.qnode(dev) def circuit(basis_state): qml.BasisState(basis_state, wires=[0, 1, 2, 3]) qml.QubitCarry(wires=[0, 1, 2, 3]) return qml.probs(wires=[0, 1, 2, 3]) probs = circuit(input_bitstring) probs_indx = np.argwhere(probs == 1).flatten()[0] bitstrings = list(itertools.product(range(2), repeat=4)) output_bitstring = bitstrings[probs_indx] The output bitstring is >>> output_bitstring (0, 1, 0, 1) The action of ``QubitCarry`` is to add wires ``1`` and ``2``. The modulo-two result is output in wire ``2`` with a carry value output in wire ``3``. In this case, :math:`1 \oplus 1 = 0` with a carry, so we have: >>> bc_sum = output_bitstring[2] >>> bc_sum 0 >>> carry = output_bitstring[3] >>> carry 1 QubitSum(wires) Apply a ``QubitSum`` operation on three input wires. This operation performs the transformation: .. math:: |a\rangle |b\rangle |c\rangle \rightarrow |a\rangle |b\rangle |a\oplus b\oplus c\rangle .. figure:: ../../_static/ops/QubitSum.svg :align: center :width: 40% :target: javascript:void(0); See `here <https://arxiv.org/abs/quant-ph/0008033v1>`__ for more information. **Details:** * Number of wires: 3 * Number of parameters: 0 Args: wires (Sequence[int]): the wires the operation acts on **Example** The ``QubitSum`` operation maps the state :math:`|010\rangle` to :math:`|011\rangle`, with the final wire holding the modulo-two sum of the first two wires: .. code-block:: input_bitstring = (0, 1, 0) @qml.qnode(dev) def circuit(basis_state): qml.BasisState(basis_state, wires = [0, 1, 2]) qml.QubitSum(wires=[0, 1, 2]) return qml.probs(wires=[0, 1, 2]) probs = circuit(input_bitstring) probs_indx = np.argwhere(probs == 1).flatten()[0] bitstrings = list(itertools.product(range(2), repeat=3)) output_bitstring = bitstrings[probs_indx] The output bitstring is >>> output_bitstring (0, 1, 1) The action of ``QubitSum`` is to add wires ``0``, ``1``, and ``2``. The modulo-two result is output in wire ``2``. In this case, :math:`0 \oplus 1 \oplus 0 = 1`, so we have: >>> abc_sum = output_bitstring[2] >>> abc_sum 1 This submodule contains the discrete-variable quantum operations that perform arithmetic operations on their input states. Copyright 2018-2021 Xanadu Quantum Technologies Inc. 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. pylint:disable=abstract-method,arguments-differ,protected-access
3,949
en
0.574216
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- from typing import Any, TYPE_CHECKING from azure.core.configuration import Configuration from azure.core.pipeline import policies from azure.mgmt.core.policies import ARMHttpLoggingPolicy from .._version import VERSION if TYPE_CHECKING: # pylint: disable=unused-import,ungrouped-imports from azure.core.credentials_async import AsyncTokenCredential class SiteRecoveryManagementClientConfiguration(Configuration): """Configuration for SiteRecoveryManagementClient. Note that all parameters used to create this instance are saved as instance attributes. :param credential: Credential needed for the client to connect to Azure. :type credential: ~azure.core.credentials_async.AsyncTokenCredential :param subscription_id: The subscription Id. :type subscription_id: str :param resource_group_name: The name of the resource group where the recovery services vault is present. :type resource_group_name: str :param resource_name: The name of the recovery services vault. :type resource_name: str """ def __init__( self, credential: "AsyncTokenCredential", subscription_id: str, resource_group_name: str, resource_name: str, **kwargs: Any ) -> None: if credential is None: raise ValueError("Parameter 'credential' must not be None.") if subscription_id is None: raise ValueError("Parameter 'subscription_id' must not be None.") if resource_group_name is None: raise ValueError("Parameter 'resource_group_name' must not be None.") if resource_name is None: raise ValueError("Parameter 'resource_name' must not be None.") super(SiteRecoveryManagementClientConfiguration, self).__init__(**kwargs) self.credential = credential self.subscription_id = subscription_id self.resource_group_name = resource_group_name self.resource_name = resource_name self.api_version = "2021-06-01" self.credential_scopes = kwargs.pop('credential_scopes', ['https://management.azure.com/.default']) kwargs.setdefault('sdk_moniker', 'mgmt-recoveryservicessiterecovery/{}'.format(VERSION)) self._configure(**kwargs) def _configure( self, **kwargs: Any ) -> None: self.user_agent_policy = kwargs.get('user_agent_policy') or policies.UserAgentPolicy(**kwargs) self.headers_policy = kwargs.get('headers_policy') or policies.HeadersPolicy(**kwargs) self.proxy_policy = kwargs.get('proxy_policy') or policies.ProxyPolicy(**kwargs) self.logging_policy = kwargs.get('logging_policy') or policies.NetworkTraceLoggingPolicy(**kwargs) self.http_logging_policy = kwargs.get('http_logging_policy') or ARMHttpLoggingPolicy(**kwargs) self.retry_policy = kwargs.get('retry_policy') or policies.AsyncRetryPolicy(**kwargs) self.custom_hook_policy = kwargs.get('custom_hook_policy') or policies.CustomHookPolicy(**kwargs) self.redirect_policy = kwargs.get('redirect_policy') or policies.AsyncRedirectPolicy(**kwargs) self.authentication_policy = kwargs.get('authentication_policy') if self.credential and not self.authentication_policy: self.authentication_policy = policies.AsyncBearerTokenCredentialPolicy(self.credential, *self.credential_scopes, **kwargs)
sdk/recoveryservices/azure-mgmt-recoveryservicessiterecovery/azure/mgmt/recoveryservicessiterecovery/aio/_configuration.py
3,868
Configuration for SiteRecoveryManagementClient. Note that all parameters used to create this instance are saved as instance attributes. :param credential: Credential needed for the client to connect to Azure. :type credential: ~azure.core.credentials_async.AsyncTokenCredential :param subscription_id: The subscription Id. :type subscription_id: str :param resource_group_name: The name of the resource group where the recovery services vault is present. :type resource_group_name: str :param resource_name: The name of the recovery services vault. :type resource_name: str coding=utf-8 -------------------------------------------------------------------------- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See License.txt in the project root for license information. Code generated by Microsoft (R) AutoRest Code Generator. Changes may cause incorrect behavior and will be lost if the code is regenerated. -------------------------------------------------------------------------- pylint: disable=unused-import,ungrouped-imports
1,078
en
0.673979
from talon import Module, Context import appscript mod = Module() ctx = Context() ctx.matches = r""" os: mac """ @mod.action_class class Actions: def run_shortcut(name: str): """Runs a shortcut on macOS""" pass @ctx.action_class("user") class UserActions: def run_shortcut(name: str): appscript.app(id='com.apple.shortcuts.events').shortcuts[name].run_()
code/platforms/mac/user.py
401
Runs a shortcut on macOS
24
en
0.521713
# Definition for binary tree with next pointer. # class TreeLinkNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # self.next = None class Solution: # @param root, a tree link node # @return nothing def connect(self, root): if root and root.left and root.right: root.left.next = root.right root.right.next = root.next and root.next.left return self.connect(root.left) or self.connect(root.right)
LeetcodeAlgorithms/116. Populating Next Right Pointers in Each Node/populating-next-right-pointers-in-each-node.py
538
Definition for binary tree with next pointer. class TreeLinkNode: def __init__(self, x): self.val = x self.left = None self.right = None self.next = None @param root, a tree link node @return nothing
235
en
0.587817
import datetime from platform import python_version from six import integer_types, string_types, text_type class _NO_VALUE(object): pass # we don't use NOTHING because it might be returned from various APIs NO_VALUE = _NO_VALUE() _SUPPORTED_TYPES = (float, bool, str, datetime.datetime, type(None)) + \ string_types + integer_types + (text_type, bytes) + (type,) if python_version() < '3.0': dict_type = dict else: from collections import abc dict_type = abc.Mapping def diff(a, b, path=None): path = _make_path(path) if isinstance(a, (list, tuple)): return _diff_sequences(a, b, path) if type(a).__name__ == 'SON': a = dict(a) if type(b).__name__ == 'SON': b = dict(b) if isinstance(a, dict_type): return _diff_dicts(a, b, path) if type(a).__name__ == 'ObjectId': a = str(a) if type(b).__name__ == 'ObjectId': b = str(b) if type(a).__name__ == 'Int64': a = int(a) if type(b).__name__ == 'Int64': b = int(b) if not isinstance(a, _SUPPORTED_TYPES): raise NotImplementedError( 'Unsupported diff type: {0}'.format(type(a))) # pragma: no cover if not isinstance(b, _SUPPORTED_TYPES): raise NotImplementedError( 'Unsupported diff type: {0}'.format(type(b))) # pragma: no cover if a != b: return [(path[:], a, b)] return [] def _diff_dicts(a, b, path): if not isinstance(a, type(b)): return [(path[:], type(a), type(b))] returned = [] for key in set(a) | set(b): a_value = a.get(key, NO_VALUE) b_value = b.get(key, NO_VALUE) path.append(key) if a_value is NO_VALUE or b_value is NO_VALUE: returned.append((path[:], a_value, b_value)) else: returned.extend(diff(a_value, b_value, path)) path.pop() return returned def _diff_sequences(a, b, path): if len(a) != len(b): return [(path[:], a, b)] returned = [] for i, a_i in enumerate(a): path.append(i) returned.extend(diff(a_i, b[i], path)) path.pop() return returned def _make_path(path): if path is None: return [] return path
tests/diff.py
2,230
we don't use NOTHING because it might be returned from various APIs pragma: no cover pragma: no cover
101
en
0.910391
from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import QColorDialog, QDialogButtonBox BB = QDialogButtonBox class ColorDialog(QColorDialog): def __init__(self, parent=None): super(ColorDialog, self).__init__(parent) self.setOption(QColorDialog.ShowAlphaChannel) # The Mac native dialog does not support our restore button. self.setOption(QColorDialog.DontUseNativeDialog) # Add a restore defaults button. # The default is set at invocation time, so that it # works across dialogs for different elements. self.default = None self.bb = self.layout().itemAt(1).widget() self.bb.addButton(BB.RestoreDefaults) self.bb.clicked.connect(self.checkRestore) def getColor(self, value=None, title=None, default=None): self.default = default if title: self.setWindowTitle(title) if value: self.setCurrentColor(value) return self.currentColor() if self.exec_() else None def checkRestore(self, button): if self.bb.buttonRole(button) & BB.ResetRole and self.default: self.setCurrentColor(self.default)
view/libs/colorDialog.py
1,195
The Mac native dialog does not support our restore button. Add a restore defaults button. The default is set at invocation time, so that it works across dialogs for different elements.
184
en
0.83133
# Copyright (C) 2019 Google Inc. # Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file> """Provides an HTML cleaner function with sqalchemy compatible API""" import re import HTMLParser import bleach # Set up custom tags/attributes for bleach BLEACH_TAGS = [ 'caption', 'strong', 'em', 'b', 'i', 'p', 'code', 'pre', 'tt', 'samp', 'kbd', 'var', 'sub', 'sup', 'dfn', 'cite', 'big', 'small', 'address', 'hr', 'br', 'div', 'span', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'abbr', 'acronym', 'a', 'img', 'blockquote', 'del', 'ins', 'table', 'tbody', 'tr', 'td', 'th', ] + bleach.ALLOWED_TAGS BLEACH_ATTRS = {} ATTRS = [ 'href', 'src', 'width', 'height', 'alt', 'cite', 'datetime', 'title', 'class', 'name', 'xml:lang', 'abbr' ] BUGGY_STRINGS_PATTERN = "&.{2,3};" for tag in BLEACH_TAGS: BLEACH_ATTRS[tag] = ATTRS CLEANER = bleach.sanitizer.Cleaner( tags=BLEACH_TAGS, attributes=BLEACH_ATTRS, strip=True ) PARSER = HTMLParser.HTMLParser() def cleaner(dummy, value, *_): """Cleans out unsafe HTML tags. Uses bleach and unescape until it reaches a fix point. Args: dummy: unused, sqalchemy will pass in the model class value: html (string) to be cleaned Returns: Html (string) without unsafe tags. """ if value is None: # No point in sanitizing None values return value if not isinstance(value, basestring): # No point in sanitizing non-strings return value value = unicode(value) buggy_strings = re.finditer(BUGGY_STRINGS_PATTERN, PARSER.unescape(value)) while True: lastvalue = value value = PARSER.unescape(CLEANER.clean(value)) if value == lastvalue: break # for some reason clean() function converts strings like "&*!;" to "&*;;". # if we have such string we are replacing new incorrect values to old ones if buggy_strings: backup_value = value updated_buggy_strings = re.finditer(BUGGY_STRINGS_PATTERN, value) for match in updated_buggy_strings: try: old_value = buggy_strings.next().group() start, finish = match.span() value = value[:start] + old_value + value[finish:] except StopIteration: # If we have different number of string after clean function # we should skip replacing return backup_value return value
src/ggrc/utils/html_cleaner.py
2,370
Cleans out unsafe HTML tags. Uses bleach and unescape until it reaches a fix point. Args: dummy: unused, sqalchemy will pass in the model class value: html (string) to be cleaned Returns: Html (string) without unsafe tags. Provides an HTML cleaner function with sqalchemy compatible API Copyright (C) 2019 Google Inc. Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file> Set up custom tags/attributes for bleach No point in sanitizing None values No point in sanitizing non-strings for some reason clean() function converts strings like "&*!;" to "&*;;". if we have such string we are replacing new incorrect values to old ones If we have different number of string after clean function we should skip replacing
745
en
0.667298
''' This script includes: 1. ClassifierOfflineTrain This is for offline training. The input data are the processed features. 2. class ClassifierOnlineTest(object) This is for online testing. The input data are the raw skeletons. It uses FeatureGenerator to extract features, and then use ClassifierOfflineTrain to recognize the action. Notice, this model is only for recognizing the action of one person. TODO: Add more comments to this function. ''' import numpy as np import sys import os import pickle import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap from collections import deque import cv2 from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.datasets import make_moons, make_circles, make_classification from sklearn.neural_network import MLPClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from sklearn.gaussian_process import GaussianProcessClassifier from sklearn.gaussian_process.kernels import RBF from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier from sklearn.naive_bayes import GaussianNB from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis from sklearn.decomposition import PCA if True: import sys import os ROOT = os.path.dirname(os.path.abspath(__file__))+"/../" sys.path.append(ROOT) from utils.lib_feature_proc import FeatureGenerator # -- Settings NUM_FEATURES_FROM_PCA = 50 # -- Classes class ClassifierOfflineTrain(object): ''' The classifer for offline training. The input features to this classifier are already processed by `class FeatureGenerator`. ''' def __init__(self): self._init_all_models() # self.clf = self._choose_model("Nearest Neighbors") # self.clf = self._choose_model("Linear SVM") # self.clf = self._choose_model("RBF SVM") # self.clf = self._choose_model("Gaussian Process") # self.clf = self._choose_model("Decision Tree") # self.clf = self._choose_model("Random Forest") self.clf = self._choose_model("Neural Net") def predict(self, X): ''' Predict the class index of the feature X ''' Y_predict = self.clf.predict(self.pca.transform(X)) return Y_predict def predict_and_evaluate(self, te_X, te_Y): ''' Test model on test set and obtain accuracy ''' te_Y_predict = self.predict(te_X) N = len(te_Y) n = sum(te_Y_predict == te_Y) accu = n / N return accu, te_Y_predict def train(self, X, Y): ''' Train model. The result is saved into self.clf ''' n_components = min(NUM_FEATURES_FROM_PCA, X.shape[1]) self.pca = PCA(n_components=n_components, whiten=True) self.pca.fit(X) # print("Sum eig values:", np.sum(self.pca.singular_values_)) print("Sum eig values:", np.sum(self.pca.explained_variance_ratio_)) X_new = self.pca.transform(X) print("After PCA, X.shape = ", X_new.shape) self.clf.fit(X_new, Y) def _choose_model(self, name): self.model_name = name idx = self.names.index(name) return self.classifiers[idx] def _init_all_models(self): self.names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Gaussian Process", "Decision Tree", "Random Forest", "Neural Net", "AdaBoost", "Naive Bayes", "QDA"] self.model_name = None self.classifiers = [ KNeighborsClassifier(5), SVC(kernel="linear", C=10.0), SVC(gamma=0.01, C=1.0, verbose=True), GaussianProcessClassifier(1.0 * RBF(1.0)), DecisionTreeClassifier(max_depth=5), RandomForestClassifier( max_depth=30, n_estimators=100, max_features="auto"), MLPClassifier((20, 30, 40)), # Neural Net AdaBoostClassifier(), GaussianNB(), QuadraticDiscriminantAnalysis()] def _predict_proba(self, X): ''' Predict the probability of feature X belonging to each of the class Y[i] ''' Y_probs = self.clf.predict_proba(self.pca.transform(X)) return Y_probs # np.array with a length of len(classes) class ClassifierOnlineTest(object): ''' Classifier for online inference. The input data to this classifier is the raw skeleton data, so they are processed by `class FeatureGenerator` before sending to the self.model trained by `class ClassifierOfflineTrain`. ''' def __init__(self, model_path, action_labels, window_size, human_id=0): # -- Settings self.human_id = human_id with open(model_path, 'rb') as f: self.model = pickle.load(f) if self.model is None: print("my Error: failed to load model") assert False self.action_labels = action_labels self.THRESHOLD_SCORE_FOR_DISP = 0.5 # -- Time serials storage self.feature_generator = FeatureGenerator(window_size) self.reset() def reset(self): self.feature_generator.reset() self.scores_hist = deque() self.scores = None def predict(self, skeleton): ''' Predict the class (string) of the input raw skeleton ''' LABEL_UNKNOWN = "" is_features_good, features = self.feature_generator.add_cur_skeleton( skeleton) if is_features_good: # convert to 2d array features = features.reshape(-1, features.shape[0]) curr_scores = self.model._predict_proba(features)[0] self.scores = self.smooth_scores(curr_scores) if self.scores.max() < self.THRESHOLD_SCORE_FOR_DISP: # If lower than threshold, bad prediced_label = LABEL_UNKNOWN else: predicted_idx = self.scores.argmax() prediced_label = self.action_labels[predicted_idx] else: prediced_label = LABEL_UNKNOWN return prediced_label def smooth_scores(self, curr_scores): ''' Smooth the current prediction score by taking the average with previous scores ''' self.scores_hist.append(curr_scores) DEQUE_MAX_SIZE = 2 if len(self.scores_hist) > DEQUE_MAX_SIZE: self.scores_hist.popleft() if 1: # Use sum score_sums = np.zeros((len(self.action_labels),)) for score in self.scores_hist: score_sums += score score_sums /= len(self.scores_hist) print("\nMean score:\n", score_sums) return score_sums else: # Use multiply score_mul = np.ones((len(self.action_labels),)) for score in self.scores_hist: score_mul *= score return score_mul def draw_scores_onto_image(self, img_disp): if self.scores is None: return for i in range(len(self.action_labels)): FONT_SIZE = 0.6 TXT_X = 20 TXT_Y = 150 + i*30 COLOR_INTENSITY = 255 #if i == -1: # s = "{}".format(self.human_id) #else: #label = self.action_labels[i] #s = "{:<5}: {:.2f}".format(label, self.scores[i]) #COLOR_INTENSITY *= (0.0 + 1.0 * self.scores[i])**0.5 if i!=-1: label = self.action_labels[i] s = "{:<5}: {:.2f}".format(label, self.scores[i]) COLOR_INTENSITY *= (0.0 + 1.0 * self.scores[i])**0.5 cv2.putText(img_disp, text=s, org=(TXT_X, TXT_Y), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=FONT_SIZE, color=(0, 0, int(COLOR_INTENSITY)), thickness=2)
utils/lib_classifier.py
7,960
The classifer for offline training. The input features to this classifier are already processed by `class FeatureGenerator`. Classifier for online inference. The input data to this classifier is the raw skeleton data, so they are processed by `class FeatureGenerator` before sending to the self.model trained by `class ClassifierOfflineTrain`. Predict the probability of feature X belonging to each of the class Y[i] Predict the class index of the feature X Predict the class (string) of the input raw skeleton Test model on test set and obtain accuracy Smooth the current prediction score by taking the average with previous scores Train model. The result is saved into self.clf This script includes: 1. ClassifierOfflineTrain This is for offline training. The input data are the processed features. 2. class ClassifierOnlineTest(object) This is for online testing. The input data are the raw skeletons. It uses FeatureGenerator to extract features, and then use ClassifierOfflineTrain to recognize the action. Notice, this model is only for recognizing the action of one person. TODO: Add more comments to this function. -- Settings -- Classes self.clf = self._choose_model("Nearest Neighbors") self.clf = self._choose_model("Linear SVM") self.clf = self._choose_model("RBF SVM") self.clf = self._choose_model("Gaussian Process") self.clf = self._choose_model("Decision Tree") self.clf = self._choose_model("Random Forest") print("Sum eig values:", np.sum(self.pca.singular_values_)) Neural Net np.array with a length of len(classes) -- Settings -- Time serials storage convert to 2d array If lower than threshold, bad Use sum Use multiplyif i == -1: s = "{}".format(self.human_id)else:label = self.action_labels[i]s = "{:<5}: {:.2f}".format(label, self.scores[i])COLOR_INTENSITY *= (0.0 + 1.0 * self.scores[i])**0.5
1,865
en
0.689466
#------------------------------------------------------------------------------ # Copyright (c) 2013, Nucleic Development Team. # # Distributed under the terms of the Modified BSD License. # # The full license is in the file COPYING.txt, distributed with this software. #------------------------------------------------------------------------------ from atom.api import Typed, ForwardTyped from .bounded_date import BoundedDate, ProxyBoundedDate class ProxyCalendar(ProxyBoundedDate): """ The abstract defintion of a proxy Calendar object. """ #: A reference to the Calendar declaration. declaration = ForwardTyped(lambda: Calendar) class Calendar(BoundedDate): """ A bounded date control which edits a Python datetime.date using a widget which resembles a calendar. """ #: A reference to the ProxyCalendar object. proxy = Typed(ProxyCalendar)
enaml/enaml/widgets/calendar.py
888
A bounded date control which edits a Python datetime.date using a widget which resembles a calendar. The abstract defintion of a proxy Calendar object. ------------------------------------------------------------------------------ Copyright (c) 2013, Nucleic Development Team. Distributed under the terms of the Modified BSD License. The full license is in the file COPYING.txt, distributed with this software.------------------------------------------------------------------------------: A reference to the Calendar declaration.: A reference to the ProxyCalendar object.
579
en
0.664671
# -*- coding: utf-8 -*- """ WOLFEYES'S FRAMEWORK Python 3 / OpenCV 3 This file describes some TypeCheking decorators. Might be useless, but allows for almost very precise type checking, especially on keyworded args, which might help. """ # 'kargs' get the arguments and passes the decorator def args(*types, **ktypes): """Allow testing of input types: argkey=(types) or argkey=type""" # The decorator modifies the function def decorator(func): def modified(*args, **kargs): # The modified fonction takes some args and kargs, # Which we need to check before passing it to func. # Works much like a proxy/firewall. # Check args: position = 1 for arg, T in zip(args, types): if not isinstance(arg, T): raise TypeError("Positional arg (%d) should be of type(s) %s, got %s" % (position, T, type(arg))) position += 1 # Check kargs: for key, arg in kargs.items(): if key in ktypes: T = ktypes[key] if not isinstance(arg, T): raise TypeError("Keyworded arg '%s' should be of type(s) %s, got %s" % (key, T, type(arg))) # Actual result after check return func(*args, **kargs) # The decorator has decorated 'func' in 'modified' return modified # We pass the actual decorator right after getting the kargs return decorator # 'ret' gets the possible output types def ret(*types, **kargs): # Garde-fou if len(types) is 1 and not isinstance(types[0], type) and callable(types[0]): raise ValueError("You should not pass a function to TypeError.ret, maybe you did not write as '@TypeError.ret()...'") # This decorator will modify the function 'func' def decorator(func): def modified(*args, **kargs): # This is the modified function, 'modified' Works # like a proxy, just passes the arguments and # checks the type of the return's value ret = func(*args, **kargs) if not isinstance(ret, types): raise TypeError("The function %s is returning an abnormal value, expected %s but got %s" % (func, types, type(ret))) # func's return return ret # Modified function return modified # The actual decorator return decorator
WolfEyes/Utils/TypeChecker.py
2,472
Allow testing of input types: argkey=(types) or argkey=type WOLFEYES'S FRAMEWORK Python 3 / OpenCV 3 This file describes some TypeCheking decorators. Might be useless, but allows for almost very precise type checking, especially on keyworded args, which might help. -*- coding: utf-8 -*- 'kargs' get the arguments and passes the decorator The decorator modifies the function The modified fonction takes some args and kargs, Which we need to check before passing it to func. Works much like a proxy/firewall. Check args: Check kargs: Actual result after check The decorator has decorated 'func' in 'modified' We pass the actual decorator right after getting the kargs 'ret' gets the possible output types Garde-fou This decorator will modify the function 'func' This is the modified function, 'modified' Works like a proxy, just passes the arguments and checks the type of the return's value func's return Modified function The actual decorator
947
en
0.693623
import requests def run(event, context): #return event.get('url') + 'aaa' r = requests.get(event.get('url')) return r.text #return '...**^^.This is a request test for url: {0}'.format(event.get('url'))
_from_pydot/dev/request_test.py
219
return event.get('url') + 'aaa'return '...**^^.This is a request test for url: {0}'.format(event.get('url'))
108
en
0.333137
import configparser import os import re import subprocess import sys import time import utilities_common.cli as clicommon from urllib.request import urlopen, urlretrieve import click from sonic_py_common import logger from swsscommon.swsscommon import SonicV2Connector from .bootloader import get_bootloader from .common import ( run_command, run_command_or_raise, IMAGE_PREFIX, UPPERDIR_NAME, WORKDIR_NAME, DOCKERDIR_NAME, ) from .exception import SonicRuntimeException SYSLOG_IDENTIFIER = "sonic-installer" LOG_ERR = logger.Logger.LOG_PRIORITY_ERROR LOG_WARN = logger.Logger.LOG_PRIORITY_WARNING LOG_NOTICE = logger.Logger.LOG_PRIORITY_NOTICE # Global Config object _config = None # Global logger instance log = logger.Logger(SYSLOG_IDENTIFIER) # This is from the aliases example: # https://github.com/pallets/click/blob/57c6f09611fc47ca80db0bd010f05998b3c0aa95/examples/aliases/aliases.py class Config(object): """Object to hold CLI config""" def __init__(self): self.path = os.getcwd() self.aliases = {} def read_config(self, filename): parser = configparser.RawConfigParser() parser.read([filename]) try: self.aliases.update(parser.items('aliases')) except configparser.NoSectionError: pass class AliasedGroup(click.Group): """This subclass of click.Group supports abbreviations and looking up aliases in a config file with a bit of magic. """ def get_command(self, ctx, cmd_name): global _config # If we haven't instantiated our global config, do it now and load current config if _config is None: _config = Config() # Load our config file cfg_file = os.path.join(os.path.dirname(__file__), 'aliases.ini') _config.read_config(cfg_file) # Try to get builtin commands as normal rv = click.Group.get_command(self, ctx, cmd_name) if rv is not None: return rv # No builtin found. Look up an explicit command alias in the config if cmd_name in _config.aliases: actual_cmd = _config.aliases[cmd_name] return click.Group.get_command(self, ctx, actual_cmd) # Alternative option: if we did not find an explicit alias we # allow automatic abbreviation of the command. "status" for # instance will match "st". We only allow that however if # there is only one command. matches = [x for x in self.list_commands(ctx) if x.lower().startswith(cmd_name.lower())] if not matches: return None elif len(matches) == 1: return click.Group.get_command(self, ctx, matches[0]) ctx.fail('Too many matches: %s' % ', '.join(sorted(matches))) # # Helper functions # _start_time = None _last_time = None def reporthook(count, block_size, total_size): global _start_time, _last_time cur_time = int(time.time()) if count == 0: _start_time = cur_time _last_time = cur_time return if cur_time == _last_time: return _last_time = cur_time duration = cur_time - _start_time progress_size = int(count * block_size) speed = int(progress_size / (1024 * duration)) percent = int(count * block_size * 100 / total_size) time_left = (total_size - progress_size) / speed / 1024 sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds left... " % (percent, progress_size / (1024 * 1024), speed, time_left)) sys.stdout.flush() # TODO: Embed tag name info into docker image meta data at build time, # and extract tag name from docker image file. def get_docker_tag_name(image): # Try to get tag name from label metadata cmd = "docker inspect --format '{{.ContainerConfig.Labels.Tag}}' " + image proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) (out, _) = proc.communicate() if proc.returncode != 0: return "unknown" tag = out.rstrip() if tag == "<no value>": return "unknown" return tag def echo_and_log(msg, priority=LOG_NOTICE, fg=None): if priority >= LOG_ERR: # Print to stderr if priority is error click.secho(msg, fg=fg, err=True) else: click.secho(msg, fg=fg) log.log(priority, msg, False) # Function which validates whether a given URL specifies an existent file # on a reachable remote machine. Will abort the current operation if not def validate_url_or_abort(url): # Attempt to retrieve HTTP response code try: urlfile = urlopen(url) response_code = urlfile.getcode() urlfile.close() except IOError: response_code = None if not response_code: echo_and_log("Did not receive a response from remote machine. Aborting...", LOG_ERR) raise click.Abort() else: # Check for a 4xx response code which indicates a nonexistent URL if response_code / 100 == 4: echo_and_log("Image file not found on remote machine. Aborting...", LOG_ERR) raise click.Abort() # Callback for confirmation prompt. Aborts if user enters "n" def abort_if_false(ctx, param, value): if not value: ctx.abort() def get_container_image_name(container_name): # example image: docker-lldp-sv2:latest cmd = "docker inspect --format '{{.Config.Image}}' " + container_name proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) (out, _) = proc.communicate() if proc.returncode != 0: sys.exit(proc.returncode) image_latest = out.rstrip() # example image_name: docker-lldp-sv2 cmd = "echo " + image_latest + " | cut -d ':' -f 1" proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) image_name = proc.stdout.read().rstrip() return image_name def get_container_image_id(image_tag): # TODO: extract commond docker info fetching functions # this is image_id for image with tag, like 'docker-teamd:latest' cmd = "docker images --format '{{.ID}}' " + image_tag proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) image_id = proc.stdout.read().rstrip() return image_id def get_container_image_id_all(image_name): # All images id under the image name like 'docker-teamd' cmd = "docker images --format '{{.ID}}' " + image_name proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) image_id_all = proc.stdout.read() image_id_all = image_id_all.splitlines() image_id_all = set(image_id_all) return image_id_all def hget_warm_restart_table(db_name, table_name, warm_app_name, key): db = SonicV2Connector() db.connect(db_name, False) _hash = table_name + db.get_db_separator(db_name) + warm_app_name client = db.get_redis_client(db_name) return client.hget(_hash, key) def hdel_warm_restart_table(db_name, table_name, warm_app_name, key): db = SonicV2Connector() db.connect(db_name, False) _hash = table_name + db.get_db_separator(db_name) + warm_app_name client = db.get_redis_client(db_name) return client.hdel(_hash, key) def print_deprecation_warning(deprecated_cmd_or_subcmd, new_cmd_or_subcmd): click.secho("Warning: '{}' {}command is deprecated and will be removed in the future" .format(deprecated_cmd_or_subcmd, "" if deprecated_cmd_or_subcmd == "sonic_installer" else "sub"), fg="red", err=True) click.secho("Please use '{}' instead".format(new_cmd_or_subcmd), fg="red", err=True) def mount_squash_fs(squashfs_path, mount_point): run_command_or_raise(["mkdir", "-p", mount_point]) run_command_or_raise(["mount", "-t", "squashfs", squashfs_path, mount_point]) def umount(mount_point, read_only=True, recursive=False, force=True, remove_dir=True, raise_exception=True): flags = [] if read_only: flags.append("-r") if force: flags.append("-f") if recursive: flags.append("-R") run_command_or_raise(["umount", *flags, mount_point], raise_exception=raise_exception) if remove_dir: run_command_or_raise(["rm", "-rf", mount_point], raise_exception=raise_exception) def mount_overlay_fs(lowerdir, upperdir, workdir, mount_point): run_command_or_raise(["mkdir", "-p", mount_point]) overlay_options = "rw,relatime,lowerdir={},upperdir={},workdir={}".format(lowerdir, upperdir, workdir) run_command_or_raise(["mount", "overlay", "-t", "overlay", "-o", overlay_options, mount_point]) def mount_bind(source, mount_point): run_command_or_raise(["mkdir", "-p", mount_point]) run_command_or_raise(["mount", "--bind", source, mount_point]) def mount_procfs_chroot(root): run_command_or_raise(["chroot", root, "mount", "proc", "/proc", "-t", "proc"]) def mount_sysfs_chroot(root): run_command_or_raise(["chroot", root, "mount", "sysfs", "/sys", "-t", "sysfs"]) def update_sonic_environment(bootloader, binary_image_version): """Prepare sonic environment variable using incoming image template file. If incoming image template does not exist use current image template file. """ SONIC_ENV_TEMPLATE_FILE = os.path.join("usr", "share", "sonic", "templates", "sonic-environment.j2") SONIC_VERSION_YML_FILE = os.path.join("etc", "sonic", "sonic_version.yml") sonic_version = re.sub(IMAGE_PREFIX, '', binary_image_version) new_image_dir = bootloader.get_image_path(binary_image_version) new_image_mount = os.path.join('/', "tmp", "image-{0}-fs".format(sonic_version)) env_dir = os.path.join(new_image_dir, "sonic-config") env_file = os.path.join(env_dir, "sonic-environment") with bootloader.get_rootfs_path(new_image_dir) as new_image_squashfs_path: try: mount_squash_fs(new_image_squashfs_path, new_image_mount) next_sonic_env_template_file = os.path.join(new_image_mount, SONIC_ENV_TEMPLATE_FILE) next_sonic_version_yml_file = os.path.join(new_image_mount, SONIC_VERSION_YML_FILE) sonic_env = run_command_or_raise([ "sonic-cfggen", "-d", "-y", next_sonic_version_yml_file, "-t", next_sonic_env_template_file, ]) os.mkdir(env_dir, 0o755) with open(env_file, "w+") as ef: print(sonic_env, file=ef) os.chmod(env_file, 0o644) except SonicRuntimeException as ex: echo_and_log("Warning: SONiC environment variables are not supported for this image: {0}".format(str(ex)), LOG_ERR, fg="red") if os.path.exists(env_file): os.remove(env_file) os.rmdir(env_dir) finally: umount(new_image_mount) def migrate_sonic_packages(bootloader, binary_image_version): """ Migrate SONiC packages to new SONiC image. """ SONIC_PACKAGE_MANAGER = "sonic-package-manager" PACKAGE_MANAGER_DIR = "/var/lib/sonic-package-manager/" DOCKER_CTL_SCRIPT = "/usr/lib/docker/docker.sh" DOCKERD_SOCK = "docker.sock" VAR_RUN_PATH = "/var/run/" tmp_dir = "tmp" packages_file = "packages.json" packages_path = os.path.join(PACKAGE_MANAGER_DIR, packages_file) sonic_version = re.sub(IMAGE_PREFIX, '', binary_image_version) new_image_dir = bootloader.get_image_path(binary_image_version) new_image_upper_dir = os.path.join(new_image_dir, UPPERDIR_NAME) new_image_work_dir = os.path.join(new_image_dir, WORKDIR_NAME) new_image_docker_dir = os.path.join(new_image_dir, DOCKERDIR_NAME) new_image_mount = os.path.join("/", tmp_dir, "image-{0}-fs".format(sonic_version)) new_image_docker_mount = os.path.join(new_image_mount, "var", "lib", "docker") if not os.path.isdir(new_image_docker_dir): # NOTE: This codepath can be reached if the installation process did not # extract the default dockerfs. This can happen with docker_inram # though the bootloader class should have disabled the package # migration which is why this message is a non fatal error message. echo_and_log("Error: SONiC package migration cannot proceed due to missing docker folder", LOG_ERR, fg="red") return docker_started = False with bootloader.get_rootfs_path(new_image_dir) as new_image_squashfs_path: try: mount_squash_fs(new_image_squashfs_path, new_image_mount) # make sure upper dir and work dir exist run_command_or_raise(["mkdir", "-p", new_image_upper_dir]) run_command_or_raise(["mkdir", "-p", new_image_work_dir]) mount_overlay_fs(new_image_mount, new_image_upper_dir, new_image_work_dir, new_image_mount) mount_bind(new_image_docker_dir, new_image_docker_mount) mount_procfs_chroot(new_image_mount) mount_sysfs_chroot(new_image_mount) # Assume if docker.sh script exists we are installing Application Extension compatible image. if not os.path.exists(os.path.join(new_image_mount, os.path.relpath(DOCKER_CTL_SCRIPT, os.path.abspath(os.sep)))): echo_and_log("Warning: SONiC Application Extension is not supported in this image", LOG_WARN, fg="yellow") return run_command_or_raise(["chroot", new_image_mount, DOCKER_CTL_SCRIPT, "start"]) docker_started = True run_command_or_raise(["cp", packages_path, os.path.join(new_image_mount, tmp_dir, packages_file)]) run_command_or_raise(["touch", os.path.join(new_image_mount, "tmp", DOCKERD_SOCK)]) run_command_or_raise(["mount", "--bind", os.path.join(VAR_RUN_PATH, DOCKERD_SOCK), os.path.join(new_image_mount, "tmp", DOCKERD_SOCK)]) run_command_or_raise(["chroot", new_image_mount, "sh", "-c", "command -v {}".format(SONIC_PACKAGE_MANAGER)]) run_command_or_raise(["chroot", new_image_mount, SONIC_PACKAGE_MANAGER, "migrate", os.path.join("/", tmp_dir, packages_file), "--dockerd-socket", os.path.join("/", tmp_dir, DOCKERD_SOCK), "-y"]) finally: if docker_started: run_command_or_raise(["chroot", new_image_mount, DOCKER_CTL_SCRIPT, "stop"], raise_exception=False) umount(new_image_mount, recursive=True, read_only=False, remove_dir=False, raise_exception=False) umount(new_image_mount, raise_exception=False) class SWAPAllocator(object): """Context class to allocate SWAP memory.""" SWAP_MEM_SIZE = 1024 DISK_FREESPACE_THRESHOLD = 4 * 1024 TOTAL_MEM_THRESHOLD = 2048 AVAILABLE_MEM_THRESHOLD = 1200 SWAP_FILE_PATH = '/host/swapfile' KiB_TO_BYTES_FACTOR = 1024 MiB_TO_BYTES_FACTOR = 1024 * 1024 def __init__(self, allocate, swap_mem_size=None, total_mem_threshold=None, available_mem_threshold=None): """ Initialize the SWAP memory allocator. The allocator will try to setup SWAP memory only if all the below conditions are met: - allocate evaluates to True - disk has enough space(> DISK_MEM_THRESHOLD) - either system total memory < total_mem_threshold or system available memory < available_mem_threshold @param allocate: True to allocate SWAP memory if necessarry @param swap_mem_size: the size of SWAP memory to allocate(in MiB) @param total_mem_threshold: the system totla memory threshold(in MiB) @param available_mem_threshold: the system available memory threshold(in MiB) """ self.allocate = allocate self.swap_mem_size = SWAPAllocator.SWAP_MEM_SIZE if swap_mem_size is None else swap_mem_size self.total_mem_threshold = SWAPAllocator.TOTAL_MEM_THRESHOLD if total_mem_threshold is None else total_mem_threshold self.available_mem_threshold = SWAPAllocator.AVAILABLE_MEM_THRESHOLD if available_mem_threshold is None else available_mem_threshold self.is_allocated = False @staticmethod def get_disk_freespace(path): """Return free disk space in bytes.""" fs_stats = os.statvfs(path) return fs_stats.f_bsize * fs_stats.f_bavail @staticmethod def read_from_meminfo(): """Read information from /proc/meminfo.""" meminfo = {} with open("/proc/meminfo") as fd: for line in fd.readlines(): if line: fields = line.split() if len(fields) >= 2 and fields[1].isdigit(): meminfo[fields[0].rstrip(":")] = int(fields[1]) return meminfo def setup_swapmem(self): swapfile = SWAPAllocator.SWAP_FILE_PATH with open(swapfile, 'wb') as fd: os.posix_fallocate(fd.fileno(), 0, self.swap_mem_size * SWAPAllocator.MiB_TO_BYTES_FACTOR) os.chmod(swapfile, 0o600) run_command(f'mkswap {swapfile}; swapon {swapfile}') def remove_swapmem(self): swapfile = SWAPAllocator.SWAP_FILE_PATH run_command_or_raise(['swapoff', swapfile], raise_exception=False) try: os.unlink(swapfile) finally: pass def __enter__(self): if self.allocate: if self.get_disk_freespace('/host') < max(SWAPAllocator.DISK_FREESPACE_THRESHOLD, self.swap_mem_size) * SWAPAllocator.MiB_TO_BYTES_FACTOR: echo_and_log("Failed to setup SWAP memory due to insufficient disk free space...", LOG_ERR) return meminfo = self.read_from_meminfo() mem_total_in_bytes = meminfo["MemTotal"] * SWAPAllocator.KiB_TO_BYTES_FACTOR mem_avail_in_bytes = meminfo["MemAvailable"] * SWAPAllocator.KiB_TO_BYTES_FACTOR if (mem_total_in_bytes < self.total_mem_threshold * SWAPAllocator.MiB_TO_BYTES_FACTOR or mem_avail_in_bytes < self.available_mem_threshold * SWAPAllocator.MiB_TO_BYTES_FACTOR): echo_and_log("Setup SWAP memory") swapfile = SWAPAllocator.SWAP_FILE_PATH if os.path.exists(swapfile): self.remove_swapmem() try: self.setup_swapmem() except Exception: self.remove_swapmem() raise self.is_allocated = True def __exit__(self, *exc_info): if self.is_allocated: self.remove_swapmem() def validate_positive_int(ctx, param, value): """Callback to validate param passed is a positive integer.""" if isinstance(value, int) and value > 0: return value raise click.BadParameter("Must be a positive integer") # Main entrypoint @click.group(cls=AliasedGroup) def sonic_installer(): """ SONiC image installation manager """ if os.geteuid() != 0: exit("Root privileges required for this operation") # Warn the user if they are calling the deprecated version of the command (with an underscore instead of a hyphen) if os.path.basename(sys.argv[0]) == "sonic_installer": print_deprecation_warning("sonic_installer", "sonic-installer") # Install image @sonic_installer.command('install') @click.option('-y', '--yes', is_flag=True, callback=abort_if_false, expose_value=False, prompt='New image will be installed, continue?') @click.option('-f', '--force', is_flag=True, help="Force installation of an image of a type which differs from that of the current running image") @click.option('--skip_migration', is_flag=True, help="Do not migrate current configuration to the newly installed image") @click.option('--skip-package-migration', is_flag=True, help="Do not migrate current packages to the newly installed image") @click.option('--skip-setup-swap', is_flag=True, help='Skip setup temporary SWAP memory used for installation') @click.option('--swap-mem-size', default=1024, type=int, show_default='1024 MiB', help='SWAP memory space size', callback=validate_positive_int, cls=clicommon.MutuallyExclusiveOption, mutually_exclusive=['skip_setup_swap']) @click.option('--total-mem-threshold', default=2048, type=int, show_default='2048 MiB', help='If system total memory is lower than threshold, setup SWAP memory', cls=clicommon.MutuallyExclusiveOption, mutually_exclusive=['skip_setup_swap'], callback=validate_positive_int) @click.option('--available-mem-threshold', default=1200, type=int, show_default='1200 MiB', help='If system available memory is lower than threhold, setup SWAP memory', cls=clicommon.MutuallyExclusiveOption, mutually_exclusive=['skip_setup_swap'], callback=validate_positive_int) @click.argument('url') def install(url, force, skip_migration=False, skip_package_migration=False, skip_setup_swap=False, swap_mem_size=None, total_mem_threshold=None, available_mem_threshold=None): """ Install image from local binary or URL""" bootloader = get_bootloader() if url.startswith('http://') or url.startswith('https://'): echo_and_log('Downloading image...') validate_url_or_abort(url) try: urlretrieve(url, bootloader.DEFAULT_IMAGE_PATH, reporthook) click.echo('') except Exception as e: echo_and_log("Download error", e) raise click.Abort() image_path = bootloader.DEFAULT_IMAGE_PATH else: image_path = os.path.join("./", url) binary_image_version = bootloader.get_binary_image_version(image_path) if not binary_image_version: echo_and_log("Image file does not exist or is not a valid SONiC image file", LOG_ERR) raise click.Abort() # Is this version already installed? if binary_image_version in bootloader.get_installed_images(): echo_and_log("Image {} is already installed. Setting it as default...".format(binary_image_version)) if not bootloader.set_default_image(binary_image_version): echo_and_log('Error: Failed to set image as default', LOG_ERR) raise click.Abort() else: # Verify that the binary image is of the same type as the running image if not bootloader.verify_binary_image(image_path) and not force: echo_and_log("Image file '{}' is of a different type than running image.\n".format(url) + "If you are sure you want to install this image, use -f|--force.\n" + "Aborting...", LOG_ERR) raise click.Abort() echo_and_log("Installing image {} and setting it as default...".format(binary_image_version)) with SWAPAllocator(not skip_setup_swap, swap_mem_size, total_mem_threshold, available_mem_threshold): bootloader.install_image(image_path) # Take a backup of current configuration if skip_migration: echo_and_log("Skipping configuration migration as requested in the command option.") else: run_command('config-setup backup') update_sonic_environment(bootloader, binary_image_version) if not bootloader.supports_package_migration(binary_image_version) and not skip_package_migration: echo_and_log("Warning: SONiC package migration is not supported for this bootloader/image", fg="yellow") skip_package_migration = True if not skip_package_migration: migrate_sonic_packages(bootloader, binary_image_version) # Finally, sync filesystem run_command("sync;sync;sync") run_command("sleep 3") # wait 3 seconds after sync echo_and_log('Done') # List installed images @sonic_installer.command('list') def list_command(): """ Print installed images """ bootloader = get_bootloader() images = bootloader.get_installed_images() curimage = bootloader.get_current_image() nextimage = bootloader.get_next_image() click.echo("Current: " + curimage) click.echo("Next: " + nextimage) click.echo("Available: ") for image in images: click.echo(image) # Set default image for boot @sonic_installer.command('set-default') @click.argument('image') def set_default(image): """ Choose image to boot from by default """ # Warn the user if they are calling the deprecated version of the subcommand (with an underscore instead of a hyphen) if "set_default" in sys.argv: print_deprecation_warning("set_default", "set-default") bootloader = get_bootloader() if image not in bootloader.get_installed_images(): echo_and_log('Error: Image does not exist', LOG_ERR) raise click.Abort() bootloader.set_default_image(image) # Set image for next boot @sonic_installer.command('set-next-boot') @click.argument('image') def set_next_boot(image): """ Choose image for next reboot (one time action) """ # Warn the user if they are calling the deprecated version of the subcommand (with underscores instead of hyphens) if "set_next_boot" in sys.argv: print_deprecation_warning("set_next_boot", "set-next-boot") bootloader = get_bootloader() if image not in bootloader.get_installed_images(): echo_and_log('Error: Image does not exist', LOG_ERR) sys.exit(1) bootloader.set_next_image(image) # Uninstall image @sonic_installer.command('remove') @click.option('-y', '--yes', is_flag=True, callback=abort_if_false, expose_value=False, prompt='Image will be removed, continue?') @click.argument('image') def remove(image): """ Uninstall image """ bootloader = get_bootloader() images = bootloader.get_installed_images() current = bootloader.get_current_image() if image not in images: echo_and_log('Image does not exist', LOG_ERR) sys.exit(1) if image == current: echo_and_log('Cannot remove current image', LOG_ERR) sys.exit(1) # TODO: check if image is next boot or default boot and fix these bootloader.remove_image(image) # Retrieve version from binary image file and print to screen @sonic_installer.command('binary-version') @click.argument('binary_image_path') def binary_version(binary_image_path): """ Get version from local binary image file """ # Warn the user if they are calling the deprecated version of the subcommand (with an underscore instead of a hyphen) if "binary_version" in sys.argv: print_deprecation_warning("binary_version", "binary-version") bootloader = get_bootloader() version = bootloader.get_binary_image_version(binary_image_path) if not version: click.echo("Image file does not exist or is not a valid SONiC image file") sys.exit(1) else: click.echo(version) # Remove installed images which are not current and next @sonic_installer.command('cleanup') @click.option('-y', '--yes', is_flag=True, callback=abort_if_false, expose_value=False, prompt='Remove images which are not current and next, continue?') def cleanup(): """ Remove installed images which are not current and next """ bootloader = get_bootloader() images = bootloader.get_installed_images() curimage = bootloader.get_current_image() nextimage = bootloader.get_next_image() image_removed = 0 for image in images: if image != curimage and image != nextimage: echo_and_log("Removing image %s" % image) bootloader.remove_image(image) image_removed += 1 if image_removed == 0: echo_and_log("No image(s) to remove") DOCKER_CONTAINER_LIST = [ "bgp", "dhcp_relay", "lldp", "macsec", "nat", "pmon", "radv", "restapi", "sflow", "snmp", "swss", "syncd", "teamd", "telemetry" ] # Upgrade docker image @sonic_installer.command('upgrade-docker') @click.option('-y', '--yes', is_flag=True, callback=abort_if_false, expose_value=False, prompt='New docker image will be installed, continue?') @click.option('--cleanup_image', is_flag=True, help="Clean up old docker image") @click.option('--skip_check', is_flag=True, help="Skip task check for docker upgrade") @click.option('--tag', type=str, help="Tag for the new docker image") @click.option('--warm', is_flag=True, help="Perform warm upgrade") @click.argument('container_name', metavar='<container_name>', required=True, type=click.Choice(DOCKER_CONTAINER_LIST)) @click.argument('url') def upgrade_docker(container_name, url, cleanup_image, skip_check, tag, warm): """ Upgrade docker image from local binary or URL""" # Warn the user if they are calling the deprecated version of the subcommand (with an underscore instead of a hyphen) if "upgrade_docker" in sys.argv: print_deprecation_warning("upgrade_docker", "upgrade-docker") image_name = get_container_image_name(container_name) image_latest = image_name + ":latest" image_id_previous = get_container_image_id(image_latest) DEFAULT_IMAGE_PATH = os.path.join("/tmp/", image_name) if url.startswith('http://') or url.startswith('https://'): echo_and_log('Downloading image...') validate_url_or_abort(url) try: urlretrieve(url, DEFAULT_IMAGE_PATH, reporthook) except Exception as e: echo_and_log("Download error: {}".format(e), LOG_ERR) raise click.Abort() image_path = DEFAULT_IMAGE_PATH else: image_path = os.path.join("./", url) # Verify that the local file exists and is a regular file # TODO: Verify the file is a *proper Docker image file* if not os.path.isfile(image_path): echo_and_log("Image file '{}' does not exist or is not a regular file. Aborting...".format(image_path), LOG_ERR) raise click.Abort() warm_configured = False # warm restart enable/disable config is put in stateDB, not persistent across cold reboot, not saved to config_DB.json file state_db = SonicV2Connector(host='127.0.0.1') state_db.connect(state_db.STATE_DB, False) TABLE_NAME_SEPARATOR = '|' prefix = 'WARM_RESTART_ENABLE_TABLE' + TABLE_NAME_SEPARATOR _hash = '{}{}'.format(prefix, container_name) if state_db.get(state_db.STATE_DB, _hash, "enable") == "true": warm_configured = True state_db.close(state_db.STATE_DB) if container_name == "swss" or container_name == "bgp" or container_name == "teamd": if warm_configured is False and warm: run_command("config warm_restart enable %s" % container_name) # Fetch tag of current running image tag_previous = get_docker_tag_name(image_latest) # Load the new image beforehand to shorten disruption time run_command("docker load < %s" % image_path) warm_app_names = [] # warm restart specific procssing for swss, bgp and teamd dockers. if warm_configured is True or warm: # make sure orchagent is in clean state if swss is to be upgraded if container_name == "swss": skipPendingTaskCheck = "" if skip_check: skipPendingTaskCheck = " -s" cmd = "docker exec -i swss orchagent_restart_check -w 2000 -r 5 " + skipPendingTaskCheck proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True) (out, err) = proc.communicate() if proc.returncode != 0: if not skip_check: echo_and_log("Orchagent is not in clean state, RESTARTCHECK failed", LOG_ERR) # Restore orignal config before exit if warm_configured is False and warm: run_command("config warm_restart disable %s" % container_name) # Clean the image loaded earlier image_id_latest = get_container_image_id(image_latest) run_command("docker rmi -f %s" % image_id_latest) # Re-point latest tag to previous tag run_command("docker tag %s:%s %s" % (image_name, tag_previous, image_latest)) sys.exit(proc.returncode) else: echo_and_log("Orchagent is not in clean state, upgrading it anyway") else: echo_and_log("Orchagent is in clean state and frozen for warm upgrade") warm_app_names = ["orchagent", "neighsyncd"] elif container_name == "bgp": # Kill bgpd to restart the bgp graceful restart procedure echo_and_log("Stopping bgp ...") run_command("docker exec -i bgp pkill -9 zebra") run_command("docker exec -i bgp pkill -9 bgpd") warm_app_names = ["bgp"] echo_and_log("Stopped bgp ...") elif container_name == "teamd": echo_and_log("Stopping teamd ...") # Send USR1 signal to all teamd instances to stop them # It will prepare teamd for warm-reboot run_command("docker exec -i teamd pkill -USR1 teamd > /dev/null") warm_app_names = ["teamsyncd"] echo_and_log("Stopped teamd ...") # clean app reconcilation state from last warm start if exists for warm_app_name in warm_app_names: hdel_warm_restart_table("STATE_DB", "WARM_RESTART_TABLE", warm_app_name, "state") run_command("docker kill %s > /dev/null" % container_name) run_command("docker rm %s " % container_name) if tag is None: # example image: docker-lldp-sv2:latest tag = get_docker_tag_name(image_latest) run_command("docker tag %s:latest %s:%s" % (image_name, image_name, tag)) run_command("systemctl restart %s" % container_name) # All images id under the image name image_id_all = get_container_image_id_all(image_name) # this is image_id for image with "latest" tag image_id_latest = get_container_image_id(image_latest) for id in image_id_all: if id != image_id_latest: # Unless requested, the previoud docker image will be preserved if not cleanup_image and id == image_id_previous: continue run_command("docker rmi -f %s" % id) exp_state = "reconciled" state = "" # post warm restart specific procssing for swss, bgp and teamd dockers, wait for reconciliation state. if warm_configured is True or warm: count = 0 for warm_app_name in warm_app_names: state = "" # Wait up to 180 seconds for reconciled state while state != exp_state and count < 90: sys.stdout.write("\r {}: ".format(warm_app_name)) sys.stdout.write("[%-s" % ('='*count)) sys.stdout.flush() count += 1 time.sleep(2) state = hget_warm_restart_table("STATE_DB", "WARM_RESTART_TABLE", warm_app_name, "state") log.log_notice("%s reached %s state" % (warm_app_name, state)) sys.stdout.write("]\n\r") if state != exp_state: echo_and_log("%s failed to reach %s state" % (warm_app_name, exp_state), LOG_ERR) else: exp_state = "" # this is cold upgrade # Restore to previous cold restart setting if warm_configured is False and warm: if container_name == "swss" or container_name == "bgp" or container_name == "teamd": run_command("config warm_restart disable %s" % container_name) if state == exp_state: echo_and_log('Done') else: echo_and_log('Failed', LOG_ERR) sys.exit(1) # rollback docker image @sonic_installer.command('rollback-docker') @click.option('-y', '--yes', is_flag=True, callback=abort_if_false, expose_value=False, prompt='Docker image will be rolled back, continue?') @click.argument('container_name', metavar='<container_name>', required=True, type=click.Choice(DOCKER_CONTAINER_LIST)) def rollback_docker(container_name): """ Rollback docker image to previous version""" # Warn the user if they are calling the deprecated version of the subcommand (with an underscore instead of a hyphen) if "rollback_docker" in sys.argv: print_deprecation_warning("rollback_docker", "rollback-docker") image_name = get_container_image_name(container_name) # All images id under the image name image_id_all = get_container_image_id_all(image_name) if len(image_id_all) != 2: echo_and_log("Two images required, but there are '{}' images for '{}'. Aborting...".format(len(image_id_all), image_name), LOG_ERR) raise click.Abort() image_latest = image_name + ":latest" image_id_previous = get_container_image_id(image_latest) version_tag = "" for id in image_id_all: if id != image_id_previous: version_tag = get_docker_tag_name(id) # make previous image as latest run_command("docker tag %s:%s %s:latest" % (image_name, version_tag, image_name)) if container_name == "swss" or container_name == "bgp" or container_name == "teamd": echo_and_log("Cold reboot is required to restore system state after '{}' rollback !!".format(container_name), LOG_ERR) else: run_command("systemctl restart %s" % container_name) echo_and_log('Done') # verify the next image @sonic_installer.command('verify-next-image') def verify_next_image(): """ Verify the next image for reboot""" bootloader = get_bootloader() if not bootloader.verify_next_image(): echo_and_log('Image verification failed', LOG_ERR) sys.exit(1) click.echo('Image successfully verified') if __name__ == '__main__': sonic_installer()
sonic_installer/main.py
38,012
This subclass of click.Group supports abbreviations and looking up aliases in a config file with a bit of magic. Object to hold CLI config Context class to allocate SWAP memory. Initialize the SWAP memory allocator. The allocator will try to setup SWAP memory only if all the below conditions are met: - allocate evaluates to True - disk has enough space(> DISK_MEM_THRESHOLD) - either system total memory < total_mem_threshold or system available memory < available_mem_threshold @param allocate: True to allocate SWAP memory if necessarry @param swap_mem_size: the size of SWAP memory to allocate(in MiB) @param total_mem_threshold: the system totla memory threshold(in MiB) @param available_mem_threshold: the system available memory threshold(in MiB) Get version from local binary image file Remove installed images which are not current and next Return free disk space in bytes. Install image from local binary or URL Print installed images Migrate SONiC packages to new SONiC image. Read information from /proc/meminfo. Uninstall image Rollback docker image to previous version Choose image to boot from by default Choose image for next reboot (one time action) SONiC image installation manager Prepare sonic environment variable using incoming image template file. If incoming image template does not exist use current image template file. Upgrade docker image from local binary or URL Callback to validate param passed is a positive integer. Verify the next image for reboot Global Config object Global logger instance This is from the aliases example: https://github.com/pallets/click/blob/57c6f09611fc47ca80db0bd010f05998b3c0aa95/examples/aliases/aliases.py If we haven't instantiated our global config, do it now and load current config Load our config file Try to get builtin commands as normal No builtin found. Look up an explicit command alias in the config Alternative option: if we did not find an explicit alias we allow automatic abbreviation of the command. "status" for instance will match "st". We only allow that however if there is only one command. Helper functions TODO: Embed tag name info into docker image meta data at build time, and extract tag name from docker image file. Try to get tag name from label metadata Print to stderr if priority is error Function which validates whether a given URL specifies an existent file on a reachable remote machine. Will abort the current operation if not Attempt to retrieve HTTP response code Check for a 4xx response code which indicates a nonexistent URL Callback for confirmation prompt. Aborts if user enters "n" example image: docker-lldp-sv2:latest example image_name: docker-lldp-sv2 TODO: extract commond docker info fetching functions this is image_id for image with tag, like 'docker-teamd:latest' All images id under the image name like 'docker-teamd' NOTE: This codepath can be reached if the installation process did not extract the default dockerfs. This can happen with docker_inram though the bootloader class should have disabled the package migration which is why this message is a non fatal error message. make sure upper dir and work dir exist Assume if docker.sh script exists we are installing Application Extension compatible image. Main entrypoint Warn the user if they are calling the deprecated version of the command (with an underscore instead of a hyphen) Install image Is this version already installed? Verify that the binary image is of the same type as the running image Take a backup of current configuration Finally, sync filesystem wait 3 seconds after sync List installed images Set default image for boot Warn the user if they are calling the deprecated version of the subcommand (with an underscore instead of a hyphen) Set image for next boot Warn the user if they are calling the deprecated version of the subcommand (with underscores instead of hyphens) Uninstall image TODO: check if image is next boot or default boot and fix these Retrieve version from binary image file and print to screen Warn the user if they are calling the deprecated version of the subcommand (with an underscore instead of a hyphen) Remove installed images which are not current and next Upgrade docker image Warn the user if they are calling the deprecated version of the subcommand (with an underscore instead of a hyphen) Verify that the local file exists and is a regular file TODO: Verify the file is a *proper Docker image file* warm restart enable/disable config is put in stateDB, not persistent across cold reboot, not saved to config_DB.json file Fetch tag of current running image Load the new image beforehand to shorten disruption time warm restart specific procssing for swss, bgp and teamd dockers. make sure orchagent is in clean state if swss is to be upgraded Restore orignal config before exit Clean the image loaded earlier Re-point latest tag to previous tag Kill bgpd to restart the bgp graceful restart procedure Send USR1 signal to all teamd instances to stop them It will prepare teamd for warm-reboot clean app reconcilation state from last warm start if exists example image: docker-lldp-sv2:latest All images id under the image name this is image_id for image with "latest" tag Unless requested, the previoud docker image will be preserved post warm restart specific procssing for swss, bgp and teamd dockers, wait for reconciliation state. Wait up to 180 seconds for reconciled state this is cold upgrade Restore to previous cold restart setting rollback docker image Warn the user if they are calling the deprecated version of the subcommand (with an underscore instead of a hyphen) All images id under the image name make previous image as latest verify the next image
5,732
en
0.795397
#!/usr/bin/python3 # encoding: utf-8 # Setup file for dulwich # Copyright (C) 2008-2016 Jelmer Vernooij <jelmer@jelmer.uk> try: from setuptools import setup, Extension except ImportError: from distutils.core import setup, Extension has_setuptools = False else: has_setuptools = True from distutils.core import Distribution import io import os import sys from typing import Dict, Any if sys.version_info < (3, 6): raise Exception( 'Dulwich only supports Python 3.6 and later. ' 'For 2.7 support, please install a version prior to 0.20') dulwich_version_string = '0.20.32' class DulwichDistribution(Distribution): def is_pure(self): if self.pure: return True def has_ext_modules(self): return not self.pure global_options = Distribution.global_options + [ ('pure', None, "use pure Python code instead of C " "extensions (slower on CPython)")] pure = False if sys.platform == 'darwin' and os.path.exists('/usr/bin/xcodebuild'): # XCode 4.0 dropped support for ppc architecture, which is hardcoded in # distutils.sysconfig import subprocess p = subprocess.Popen( ['/usr/bin/xcodebuild', '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env={}) out, err = p.communicate() for line in out.splitlines(): line = line.decode("utf8") # Also parse only first digit, because 3.2.1 can't be parsed nicely if (line.startswith('Xcode') and int(line.split()[1].split('.')[0]) >= 4): os.environ['ARCHFLAGS'] = '' tests_require = ['fastimport'] if '__pypy__' not in sys.modules and sys.platform != 'win32': tests_require.extend([ 'gevent', 'geventhttpclient', 'setuptools>=17.1']) ext_modules = [ Extension('dulwich._objects', ['dulwich/_objects.c']), Extension('dulwich._pack', ['dulwich/_pack.c']), Extension('dulwich._diff_tree', ['dulwich/_diff_tree.c']), ] setup_kwargs = {} # type: Dict[str, Any] scripts = ['bin/dul-receive-pack', 'bin/dul-upload-pack'] if has_setuptools: setup_kwargs['extras_require'] = { 'fastimport': ['fastimport'], 'https': ['urllib3[secure]>=1.24.1'], 'pgp': ['gpg'], 'watch': ['pyinotify'], } setup_kwargs['install_requires'] = ['urllib3>=1.24.1', 'certifi'] setup_kwargs['include_package_data'] = True setup_kwargs['test_suite'] = 'dulwich.tests.test_suite' setup_kwargs['tests_require'] = tests_require setup_kwargs['entry_points'] = { "console_scripts": [ "dulwich=dulwich.cli:main", ]} setup_kwargs['python_requires'] = '>=3.6' else: scripts.append('bin/dulwich') with io.open(os.path.join(os.path.dirname(__file__), "README.rst"), encoding="utf-8") as f: description = f.read() setup(name='dulwich', author="Jelmer Vernooij", author_email="jelmer@jelmer.uk", url="https://www.dulwich.io/", long_description=description, description="Python Git Library", version=dulwich_version_string, license='Apachev2 or later or GPLv2', project_urls={ "Bug Tracker": "https://github.com/dulwich/dulwich/issues", "Repository": "https://www.dulwich.io/code/", "GitHub": "https://github.com/dulwich/dulwich", }, keywords="git vcs", packages=['dulwich', 'dulwich.tests', 'dulwich.tests.compat', 'dulwich.contrib'], package_data={'': ['../docs/tutorial/*.txt', 'py.typed']}, scripts=scripts, ext_modules=ext_modules, zip_safe=False, distclass=DulwichDistribution, classifiers=[ 'Development Status :: 4 - Beta', 'License :: OSI Approved :: Apache Software License', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: 3.10', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Operating System :: POSIX', 'Operating System :: Microsoft :: Windows', 'Topic :: Software Development :: Version Control', ], **setup_kwargs )
setup.py
4,406
!/usr/bin/python3 encoding: utf-8 Setup file for dulwich Copyright (C) 2008-2016 Jelmer Vernooij <jelmer@jelmer.uk> XCode 4.0 dropped support for ppc architecture, which is hardcoded in distutils.sysconfig Also parse only first digit, because 3.2.1 can't be parsed nicely type: Dict[str, Any]
291
en
0.723127
import ctypes import logging import threading import time from contextlib import contextmanager from queue import Queue from typing import Any, Callable, Dict, Generator, Iterable, List, Optional, Tuple, Union, cast import attr import hypothesis import hypothesis.errors import requests from _pytest.logging import LogCaptureHandler, catching_logs from requests.auth import HTTPDigestAuth, _basic_auth_str from .._hypothesis import make_test_or_exception from ..checks import DEFAULT_CHECKS from ..constants import USER_AGENT from ..exceptions import InvalidSchema from ..loaders import from_uri from ..models import Case, Endpoint, Status, TestResult, TestResultSet from ..schemas import BaseSchema from ..utils import WSGIResponse, capture_hypothesis_output, get_base_url from . import events DEFAULT_DEADLINE = 500 # pragma: no mutate RawAuth = Tuple[str, str] # pragma: no mutate def get_hypothesis_settings(hypothesis_options: Optional[Dict[str, Any]] = None) -> hypothesis.settings: # Default settings, used as a parent settings object below settings = hypothesis.settings(deadline=DEFAULT_DEADLINE) if hypothesis_options is not None: settings = hypothesis.settings(settings, **hypothesis_options) return settings # pylint: disable=too-many-instance-attributes @attr.s class BaseRunner: schema: BaseSchema = attr.ib() checks: Iterable[Callable] = attr.ib() hypothesis_settings: hypothesis.settings = attr.ib(converter=get_hypothesis_settings) auth: Optional[RawAuth] = attr.ib(default=None) auth_type: Optional[str] = attr.ib(default=None) headers: Optional[Dict[str, Any]] = attr.ib(default=None) request_timeout: Optional[int] = attr.ib(default=None) seed: Optional[int] = attr.ib(default=None) def execute(self,) -> Generator[events.ExecutionEvent, None, None]: """Common logic for all runners.""" results = TestResultSet() initialized = events.Initialized( results=results, schema=self.schema, checks=self.checks, hypothesis_settings=self.hypothesis_settings ) yield initialized yield from self._execute(results) yield events.Finished(results=results, schema=self.schema, running_time=time.time() - initialized.start_time) def _execute(self, results: TestResultSet) -> Generator[events.ExecutionEvent, None, None]: raise NotImplementedError @attr.s(slots=True) class SingleThreadRunner(BaseRunner): """Fast runner that runs tests sequentially in the main thread.""" def _execute(self, results: TestResultSet) -> Generator[events.ExecutionEvent, None, None]: auth = get_requests_auth(self.auth, self.auth_type) with get_session(auth, self.headers) as session: for endpoint, test in self.schema.get_all_tests(network_test, self.hypothesis_settings, self.seed): for event in run_test( self.schema, endpoint, test, self.checks, results, session=session, request_timeout=self.request_timeout, ): yield event if isinstance(event, events.Interrupted): return @attr.s(slots=True) class SingleThreadWSGIRunner(SingleThreadRunner): def _execute(self, results: TestResultSet) -> Generator[events.ExecutionEvent, None, None]: for endpoint, test in self.schema.get_all_tests(wsgi_test, self.hypothesis_settings, self.seed): for event in run_test( self.schema, endpoint, test, self.checks, results, auth=self.auth, auth_type=self.auth_type, headers=self.headers, ): yield event if isinstance(event, events.Interrupted): return def _run_task( test_template: Callable, tasks_queue: Queue, events_queue: Queue, schema: BaseSchema, checks: Iterable[Callable], settings: hypothesis.settings, seed: Optional[int], results: TestResultSet, **kwargs: Any, ) -> None: # pylint: disable=too-many-arguments with capture_hypothesis_output(): while not tasks_queue.empty(): endpoint = tasks_queue.get() test = make_test_or_exception(endpoint, test_template, settings, seed) for event in run_test(schema, endpoint, test, checks, results, **kwargs): events_queue.put(event) def thread_task( tasks_queue: Queue, events_queue: Queue, schema: BaseSchema, checks: Iterable[Callable], settings: hypothesis.settings, auth: Optional[RawAuth], auth_type: Optional[str], headers: Optional[Dict[str, Any]], seed: Optional[int], results: TestResultSet, kwargs: Any, ) -> None: """A single task, that threads do. Pretty similar to the default one-thread flow, but includes communication with the main thread via the events queue. """ # pylint: disable=too-many-arguments prepared_auth = get_requests_auth(auth, auth_type) with get_session(prepared_auth, headers) as session: _run_task( network_test, tasks_queue, events_queue, schema, checks, settings, seed, results, session=session, **kwargs ) def wsgi_thread_task( tasks_queue: Queue, events_queue: Queue, schema: BaseSchema, checks: Iterable[Callable], settings: hypothesis.settings, seed: Optional[int], results: TestResultSet, kwargs: Any, ) -> None: # pylint: disable=too-many-arguments _run_task(wsgi_test, tasks_queue, events_queue, schema, checks, settings, seed, results, **kwargs) def stop_worker(thread_id: int) -> None: """Raise an error in a thread so it is possible to asynchronously stop thread execution.""" ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(thread_id), ctypes.py_object(SystemExit)) class ThreadInterrupted(Exception): """Special exception when worker thread received SIGINT.""" @attr.s(slots=True) class ThreadPoolRunner(BaseRunner): """Spread different tests among multiple worker threads.""" workers_num: int = attr.ib(default=2) def _execute(self, results: TestResultSet) -> Generator[events.ExecutionEvent, None, None]: """All events come from a queue where different workers push their events.""" tasks_queue = self._get_tasks_queue() # Events are pushed by workers via a separate queue events_queue: Queue = Queue() workers = self._init_workers(tasks_queue, events_queue, results) def stop_workers() -> None: for worker in workers: # workers are initialized at this point and `worker.ident` is set with an integer value ident = cast(int, worker.ident) stop_worker(ident) worker.join() is_finished = False try: while not is_finished: # Sleep is needed for performance reasons # each call to `is_alive` of an alive worker waits for a lock # iterations without waiting are too frequent and a lot of time will be spent on waiting for this locks time.sleep(0.001) is_finished = all(not worker.is_alive() for worker in workers) while not events_queue.empty(): event = events_queue.get() yield event if isinstance(event, events.Interrupted): # Thread received SIGINT # We could still have events in the queue, but ignore them to keep the logic simple # for now, could be improved in the future to show more info in such corner cases raise ThreadInterrupted except ThreadInterrupted: stop_workers() except KeyboardInterrupt: stop_workers() yield events.Interrupted(results=results, schema=self.schema) def _get_tasks_queue(self) -> Queue: """All endpoints are distributed among all workers via a queue.""" tasks_queue: Queue = Queue() tasks_queue.queue.extend(self.schema.get_all_endpoints()) return tasks_queue def _init_workers(self, tasks_queue: Queue, events_queue: Queue, results: TestResultSet) -> List[threading.Thread]: """Initialize & start workers that will execute tests.""" workers = [ threading.Thread( target=self._get_task(), kwargs=self._get_worker_kwargs(tasks_queue, events_queue, results) ) for _ in range(self.workers_num) ] for worker in workers: worker.start() return workers def _get_task(self) -> Callable: return thread_task def _get_worker_kwargs(self, tasks_queue: Queue, events_queue: Queue, results: TestResultSet) -> Dict[str, Any]: return { "tasks_queue": tasks_queue, "events_queue": events_queue, "schema": self.schema, "checks": self.checks, "settings": self.hypothesis_settings, "auth": self.auth, "auth_type": self.auth_type, "headers": self.headers, "seed": self.seed, "results": results, "kwargs": {"request_timeout": self.request_timeout}, } class ThreadPoolWSGIRunner(ThreadPoolRunner): def _get_task(self) -> Callable: return wsgi_thread_task def _get_worker_kwargs(self, tasks_queue: Queue, events_queue: Queue, results: TestResultSet) -> Dict[str, Any]: return { "tasks_queue": tasks_queue, "events_queue": events_queue, "schema": self.schema, "checks": self.checks, "settings": self.hypothesis_settings, "seed": self.seed, "results": results, "kwargs": {"auth": self.auth, "auth_type": self.auth_type, "headers": self.headers}, } def execute_from_schema( schema: BaseSchema, checks: Iterable[Callable], *, workers_num: int = 1, hypothesis_options: Optional[Dict[str, Any]] = None, auth: Optional[RawAuth] = None, auth_type: Optional[str] = None, headers: Optional[Dict[str, Any]] = None, request_timeout: Optional[int] = None, seed: Optional[int] = None, ) -> Generator[events.ExecutionEvent, None, None]: """Execute tests for the given schema. Provides the main testing loop and preparation step. """ runner: BaseRunner if workers_num > 1: if schema.app: runner = ThreadPoolWSGIRunner( schema=schema, checks=checks, hypothesis_settings=hypothesis_options, auth=auth, auth_type=auth_type, headers=headers, seed=seed, workers_num=workers_num, ) else: runner = ThreadPoolRunner( schema=schema, checks=checks, hypothesis_settings=hypothesis_options, auth=auth, auth_type=auth_type, headers=headers, seed=seed, request_timeout=request_timeout, ) else: if schema.app: runner = SingleThreadWSGIRunner( schema=schema, checks=checks, hypothesis_settings=hypothesis_options, auth=auth, auth_type=auth_type, headers=headers, seed=seed, ) else: runner = SingleThreadRunner( schema=schema, checks=checks, hypothesis_settings=hypothesis_options, auth=auth, auth_type=auth_type, headers=headers, seed=seed, request_timeout=request_timeout, ) yield from runner.execute() def run_test( schema: BaseSchema, endpoint: Endpoint, test: Union[Callable, InvalidSchema], checks: Iterable[Callable], results: TestResultSet, **kwargs: Any, ) -> Generator[events.ExecutionEvent, None, None]: """A single test run with all error handling needed.""" # pylint: disable=too-many-arguments result = TestResult(endpoint=endpoint) yield events.BeforeExecution(results=results, schema=schema, endpoint=endpoint) hypothesis_output: List[str] = [] try: if isinstance(test, InvalidSchema): status = Status.error result.add_error(test) else: with capture_hypothesis_output() as hypothesis_output: test(checks, result, **kwargs) status = Status.success except AssertionError: status = Status.failure except hypothesis.errors.Flaky: status = Status.error result.mark_errored() # Sometimes Hypothesis detects inconsistent test results and checks are not available if result.checks: flaky_example = result.checks[-1].example else: flaky_example = None result.add_error( hypothesis.errors.Flaky( "Tests on this endpoint produce unreliable results: \n" "Falsified on the first call but did not on a subsequent one" ), flaky_example, ) except hypothesis.errors.Unsatisfiable: # We need more clear error message here status = Status.error result.add_error(hypothesis.errors.Unsatisfiable("Unable to satisfy schema parameters for this endpoint")) except KeyboardInterrupt: yield events.Interrupted(results=results, schema=schema) return except Exception as error: status = Status.error result.add_error(error) # Fetch seed value, hypothesis generates it during test execution result.seed = getattr(test, "_hypothesis_internal_use_seed", None) or getattr( test, "_hypothesis_internal_use_generated_seed", None ) results.append(result) yield events.AfterExecution( results=results, schema=schema, endpoint=endpoint, status=status, hypothesis_output=hypothesis_output ) def execute( # pylint: disable=too-many-arguments schema_uri: str, checks: Iterable[Callable] = DEFAULT_CHECKS, api_options: Optional[Dict[str, Any]] = None, loader_options: Optional[Dict[str, Any]] = None, hypothesis_options: Optional[Dict[str, Any]] = None, loader: Callable = from_uri, ) -> TestResultSet: generator = prepare( schema_uri=schema_uri, checks=checks, api_options=api_options, loader_options=loader_options, hypothesis_options=hypothesis_options, loader=loader, ) all_events = list(generator) finished = all_events[-1] return finished.results def prepare( # pylint: disable=too-many-arguments schema_uri: str, checks: Iterable[Callable] = DEFAULT_CHECKS, workers_num: int = 1, api_options: Optional[Dict[str, Any]] = None, loader_options: Optional[Dict[str, Any]] = None, hypothesis_options: Optional[Dict[str, Any]] = None, loader: Callable = from_uri, seed: Optional[int] = None, ) -> Generator[events.ExecutionEvent, None, None]: """Prepare a generator that will run test cases against the given API definition.""" api_options = api_options or {} loader_options = loader_options or {} if "base_url" not in loader_options: loader_options["base_url"] = get_base_url(schema_uri) schema = loader(schema_uri, **loader_options) return execute_from_schema( schema, checks, hypothesis_options=hypothesis_options, seed=seed, workers_num=workers_num, **api_options ) def network_test( case: Case, checks: Iterable[Callable], result: TestResult, session: requests.Session, request_timeout: Optional[int], ) -> None: """A single test body that will be executed against the target.""" # pylint: disable=too-many-arguments timeout = prepare_timeout(request_timeout) response = case.call(session=session, timeout=timeout) _run_checks(case, checks, result, response) def wsgi_test( case: Case, checks: Iterable[Callable], result: TestResult, auth: Optional[RawAuth], auth_type: Optional[str], headers: Optional[Dict[str, Any]], ) -> None: # pylint: disable=too-many-arguments headers = _prepare_wsgi_headers(headers, auth, auth_type) with catching_logs(LogCaptureHandler(), level=logging.DEBUG) as recorded: response = case.call_wsgi(headers=headers) result.logs.extend(recorded.records) _run_checks(case, checks, result, response) def _prepare_wsgi_headers( headers: Optional[Dict[str, Any]], auth: Optional[RawAuth], auth_type: Optional[str] ) -> Dict[str, Any]: headers = headers or {} headers.setdefault("User-agent", USER_AGENT) wsgi_auth = get_wsgi_auth(auth, auth_type) if wsgi_auth: headers["Authorization"] = wsgi_auth return headers def _run_checks( case: Case, checks: Iterable[Callable], result: TestResult, response: Union[requests.Response, WSGIResponse] ) -> None: errors = None for check in checks: check_name = check.__name__ try: check(response, result) result.add_success(check_name, case) except AssertionError as exc: errors = True # pragma: no mutate result.add_failure(check_name, case, str(exc)) if errors is not None: # An exception needed to trigger Hypothesis shrinking & flaky tests detection logic # The message doesn't matter raise AssertionError def prepare_timeout(timeout: Optional[int]) -> Optional[float]: """Request timeout is in milliseconds, but `requests` uses seconds""" output: Optional[Union[int, float]] = timeout if timeout is not None: output = timeout / 1000 return output @contextmanager def get_session( auth: Optional[Union[HTTPDigestAuth, RawAuth]] = None, headers: Optional[Dict[str, Any]] = None ) -> Generator[requests.Session, None, None]: with requests.Session() as session: if auth is not None: session.auth = auth session.headers["User-agent"] = USER_AGENT if headers is not None: session.headers.update(**headers) yield session def get_requests_auth(auth: Optional[RawAuth], auth_type: Optional[str]) -> Optional[Union[HTTPDigestAuth, RawAuth]]: if auth and auth_type == "digest": return HTTPDigestAuth(*auth) return auth def get_wsgi_auth(auth: Optional[RawAuth], auth_type: Optional[str]) -> Optional[str]: if auth: if auth_type == "digest": raise ValueError("Digest auth is not supported for WSGI apps") return _basic_auth_str(*auth) return None
src/schemathesis/runner/__init__.py
19,128
Fast runner that runs tests sequentially in the main thread. Special exception when worker thread received SIGINT. Spread different tests among multiple worker threads. All events come from a queue where different workers push their events. All endpoints are distributed among all workers via a queue. Initialize & start workers that will execute tests. Common logic for all runners. Execute tests for the given schema. Provides the main testing loop and preparation step. A single test body that will be executed against the target. Prepare a generator that will run test cases against the given API definition. Request timeout is in milliseconds, but `requests` uses seconds A single test run with all error handling needed. Raise an error in a thread so it is possible to asynchronously stop thread execution. A single task, that threads do. Pretty similar to the default one-thread flow, but includes communication with the main thread via the events queue. pragma: no mutate pragma: no mutate Default settings, used as a parent settings object below pylint: disable=too-many-instance-attributes pylint: disable=too-many-arguments pylint: disable=too-many-arguments pylint: disable=too-many-arguments Events are pushed by workers via a separate queue workers are initialized at this point and `worker.ident` is set with an integer value Sleep is needed for performance reasons each call to `is_alive` of an alive worker waits for a lock iterations without waiting are too frequent and a lot of time will be spent on waiting for this locks Thread received SIGINT We could still have events in the queue, but ignore them to keep the logic simple for now, could be improved in the future to show more info in such corner cases pylint: disable=too-many-arguments Sometimes Hypothesis detects inconsistent test results and checks are not available We need more clear error message here Fetch seed value, hypothesis generates it during test execution pylint: disable=too-many-arguments pylint: disable=too-many-arguments pylint: disable=too-many-arguments pylint: disable=too-many-arguments pragma: no mutate An exception needed to trigger Hypothesis shrinking & flaky tests detection logic The message doesn't matter
2,219
en
0.896831
# coding: utf-8 from __future__ import unicode_literals import json import re import socket from .common import InfoExtractor from ..compat import ( compat_etree_fromstring, compat_http_client, compat_str, compat_urllib_error, compat_urllib_parse_unquote, compat_urllib_parse_unquote_plus, ) from ..utils import ( clean_html, error_to_compat_str, ExtractorError, float_or_none, get_element_by_id, int_or_none, js_to_json, limit_length, parse_count, qualities, sanitized_Request, try_get, urlencode_postdata, urljoin, ) class FacebookIE(InfoExtractor): _VALID_URL = r'''(?x) (?: https?:// (?:[\w-]+\.)?(?:facebook\.com|facebookcorewwwi\.onion)/ (?:[^#]*?\#!/)? (?: (?: video/video\.php| photo\.php| video\.php| video/embed| story\.php| watch(?:/live)?/? )\?(?:.*?)(?:v|video_id|story_fbid)=| [^/]+/videos/(?:[^/]+/)?| [^/]+/posts/| groups/[^/]+/permalink/| watchparty/ )| facebook: ) (?P<id>[0-9]+) ''' _LOGIN_URL = 'https://www.facebook.com/login.php?next=http%3A%2F%2Ffacebook.com%2Fhome.php&login_attempt=1' _CHECKPOINT_URL = 'https://www.facebook.com/checkpoint/?next=http%3A%2F%2Ffacebook.com%2Fhome.php&_fb_noscript=1' _NETRC_MACHINE = 'facebook' IE_NAME = 'facebook' _VIDEO_PAGE_TEMPLATE = 'https://www.facebook.com/video/video.php?v=%s' _VIDEO_PAGE_TAHOE_TEMPLATE = 'https://www.facebook.com/video/tahoe/async/%s/?chain=true&isvideo=true&payloadtype=primary' _TESTS = [{ 'url': 'https://www.facebook.com/video.php?v=637842556329505&fref=nf', 'md5': '6a40d33c0eccbb1af76cf0485a052659', 'info_dict': { 'id': '637842556329505', 'ext': 'mp4', 'title': 're:Did you know Kei Nishikori is the first Asian man to ever reach a Grand Slam', 'uploader': 'Tennis on Facebook', 'upload_date': '20140908', 'timestamp': 1410199200, }, 'skip': 'Requires logging in', }, { # data.video 'url': 'https://www.facebook.com/video.php?v=274175099429670', 'info_dict': { 'id': '274175099429670', 'ext': 'mp4', 'title': 're:^Asif Nawab Butt posted a video', 'uploader': 'Asif Nawab Butt', 'upload_date': '20140506', 'timestamp': 1399398998, 'thumbnail': r're:^https?://.*', }, 'expected_warnings': [ 'title' ] }, { 'note': 'Video with DASH manifest', 'url': 'https://www.facebook.com/video.php?v=957955867617029', 'md5': 'b2c28d528273b323abe5c6ab59f0f030', 'info_dict': { 'id': '957955867617029', 'ext': 'mp4', 'title': 'When you post epic content on instagram.com/433 8 million followers, this is ...', 'uploader': 'Demy de Zeeuw', 'upload_date': '20160110', 'timestamp': 1452431627, }, 'skip': 'Requires logging in', }, { 'url': 'https://www.facebook.com/maxlayn/posts/10153807558977570', 'md5': '037b1fa7f3c2d02b7a0d7bc16031ecc6', 'info_dict': { 'id': '544765982287235', 'ext': 'mp4', 'title': '"What are you doing running in the snow?"', 'uploader': 'FailArmy', }, 'skip': 'Video gone', }, { 'url': 'https://m.facebook.com/story.php?story_fbid=1035862816472149&id=116132035111903', 'md5': '1deb90b6ac27f7efcf6d747c8a27f5e3', 'info_dict': { 'id': '1035862816472149', 'ext': 'mp4', 'title': 'What the Flock Is Going On In New Zealand Credit: ViralHog', 'uploader': 'S. Saint', }, 'skip': 'Video gone', }, { 'note': 'swf params escaped', 'url': 'https://www.facebook.com/barackobama/posts/10153664894881749', 'md5': '97ba073838964d12c70566e0085c2b91', 'info_dict': { 'id': '10153664894881749', 'ext': 'mp4', 'title': 'Average time to confirm recent Supreme Court nominees: 67 days Longest it\'s t...', 'thumbnail': r're:^https?://.*', 'timestamp': 1456259628, 'upload_date': '20160223', 'uploader': 'Barack Obama', }, }, { # have 1080P, but only up to 720p in swf params # data.video.story.attachments[].media 'url': 'https://www.facebook.com/cnn/videos/10155529876156509/', 'md5': '9571fae53d4165bbbadb17a94651dcdc', 'info_dict': { 'id': '10155529876156509', 'ext': 'mp4', 'title': 'She survived the holocaust — and years later, she’s getting her citizenship s...', 'timestamp': 1477818095, 'upload_date': '20161030', 'uploader': 'CNN', 'thumbnail': r're:^https?://.*', 'view_count': int, }, }, { # bigPipe.onPageletArrive ... onPageletArrive pagelet_group_mall # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media 'url': 'https://www.facebook.com/yaroslav.korpan/videos/1417995061575415/', 'info_dict': { 'id': '1417995061575415', 'ext': 'mp4', 'title': 'md5:1db063d6a8c13faa8da727817339c857', 'timestamp': 1486648217, 'upload_date': '20170209', 'uploader': 'Yaroslav Korpan', }, 'params': { 'skip_download': True, }, }, { 'url': 'https://www.facebook.com/LaGuiaDelVaron/posts/1072691702860471', 'info_dict': { 'id': '1072691702860471', 'ext': 'mp4', 'title': 'md5:ae2d22a93fbb12dad20dc393a869739d', 'timestamp': 1477305000, 'upload_date': '20161024', 'uploader': 'La Guía Del Varón', 'thumbnail': r're:^https?://.*', }, 'params': { 'skip_download': True, }, }, { # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media 'url': 'https://www.facebook.com/groups/1024490957622648/permalink/1396382447100162/', 'info_dict': { 'id': '1396382447100162', 'ext': 'mp4', 'title': 'md5:19a428bbde91364e3de815383b54a235', 'timestamp': 1486035494, 'upload_date': '20170202', 'uploader': 'Elisabeth Ahtn', }, 'params': { 'skip_download': True, }, }, { 'url': 'https://www.facebook.com/video.php?v=10204634152394104', 'only_matching': True, }, { 'url': 'https://www.facebook.com/amogood/videos/1618742068337349/?fref=nf', 'only_matching': True, }, { # data.mediaset.currMedia.edges 'url': 'https://www.facebook.com/ChristyClarkForBC/videos/vb.22819070941/10153870694020942/?type=2&theater', 'only_matching': True, }, { # data.video.story.attachments[].media 'url': 'facebook:544765982287235', 'only_matching': True, }, { # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media 'url': 'https://www.facebook.com/groups/164828000315060/permalink/764967300301124/', 'only_matching': True, }, { # data.video.creation_story.attachments[].media 'url': 'https://zh-hk.facebook.com/peoplespower/videos/1135894589806027/', 'only_matching': True, }, { # data.video 'url': 'https://www.facebookcorewwwi.onion/video.php?v=274175099429670', 'only_matching': True, }, { # no title 'url': 'https://www.facebook.com/onlycleverentertainment/videos/1947995502095005/', 'only_matching': True, }, { # data.video 'url': 'https://www.facebook.com/WatchESLOne/videos/359649331226507/', 'info_dict': { 'id': '359649331226507', 'ext': 'mp4', 'title': '#ESLOne VoD - Birmingham Finals Day#1 Fnatic vs. @Evil Geniuses', 'uploader': 'ESL One Dota 2', }, 'params': { 'skip_download': True, }, }, { # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.all_subattachments.nodes[].media 'url': 'https://www.facebook.com/100033620354545/videos/106560053808006/', 'info_dict': { 'id': '106560053808006', }, 'playlist_count': 2, }, { # data.video.story.attachments[].media 'url': 'https://www.facebook.com/watch/?v=647537299265662', 'only_matching': True, }, { # data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.all_subattachments.nodes[].media 'url': 'https://www.facebook.com/PankajShahLondon/posts/10157667649866271', 'info_dict': { 'id': '10157667649866271', }, 'playlist_count': 3, }, { # data.nodes[].comet_sections.content.story.attachments[].style_type_renderer.attachment.media 'url': 'https://m.facebook.com/Alliance.Police.Department/posts/4048563708499330', 'info_dict': { 'id': '117576630041613', 'ext': 'mp4', # TODO: title can be extracted from video page 'title': 'Facebook video #117576630041613', 'uploader_id': '189393014416438', 'upload_date': '20201123', 'timestamp': 1606162592, }, 'skip': 'Requires logging in', }, { # node.comet_sections.content.story.attached_story.attachments.style_type_renderer.attachment.media 'url': 'https://www.facebook.com/groups/ateistiskselskab/permalink/10154930137678856/', 'info_dict': { 'id': '211567722618337', 'ext': 'mp4', 'title': 'Facebook video #211567722618337', 'uploader_id': '127875227654254', 'upload_date': '20161122', 'timestamp': 1479793574, }, }, { # data.video.creation_story.attachments[].media 'url': 'https://www.facebook.com/watch/live/?v=1823658634322275', 'only_matching': True, }, { 'url': 'https://www.facebook.com/watchparty/211641140192478', 'info_dict': { 'id': '211641140192478', }, 'playlist_count': 1, 'skip': 'Requires logging in', }] _SUPPORTED_PAGLETS_REGEX = r'(?:pagelet_group_mall|permalink_video_pagelet|hyperfeed_story_id_[0-9a-f]+)' _api_config = { 'graphURI': '/api/graphql/' } @staticmethod def _extract_urls(webpage): urls = [] for mobj in re.finditer( r'<iframe[^>]+?src=(["\'])(?P<url>https?://www\.facebook\.com/(?:video/embed|plugins/video\.php).+?)\1', webpage): urls.append(mobj.group('url')) # Facebook API embed # see https://developers.facebook.com/docs/plugins/embedded-video-player for mobj in re.finditer(r'''(?x)<div[^>]+ class=(?P<q1>[\'"])[^\'"]*\bfb-(?:video|post)\b[^\'"]*(?P=q1)[^>]+ data-href=(?P<q2>[\'"])(?P<url>(?:https?:)?//(?:www\.)?facebook.com/.+?)(?P=q2)''', webpage): urls.append(mobj.group('url')) return urls def _login(self): useremail, password = self._get_login_info() if useremail is None: return login_page_req = sanitized_Request(self._LOGIN_URL) self._set_cookie('facebook.com', 'locale', 'en_US') login_page = self._download_webpage(login_page_req, None, note='Downloading login page', errnote='Unable to download login page') lsd = self._search_regex( r'<input type="hidden" name="lsd" value="([^"]*)"', login_page, 'lsd') lgnrnd = self._search_regex(r'name="lgnrnd" value="([^"]*?)"', login_page, 'lgnrnd') login_form = { 'email': useremail, 'pass': password, 'lsd': lsd, 'lgnrnd': lgnrnd, 'next': 'http://facebook.com/home.php', 'default_persistent': '0', 'legacy_return': '1', 'timezone': '-60', 'trynum': '1', } request = sanitized_Request(self._LOGIN_URL, urlencode_postdata(login_form)) request.add_header('Content-Type', 'application/x-www-form-urlencoded') try: login_results = self._download_webpage(request, None, note='Logging in', errnote='unable to fetch login page') if re.search(r'<form(.*)name="login"(.*)</form>', login_results) is not None: error = self._html_search_regex( r'(?s)<div[^>]+class=(["\']).*?login_error_box.*?\1[^>]*><div[^>]*>.*?</div><div[^>]*>(?P<error>.+?)</div>', login_results, 'login error', default=None, group='error') if error: raise ExtractorError('Unable to login: %s' % error, expected=True) self._downloader.report_warning('unable to log in: bad username/password, or exceeded login rate limit (~3/min). Check credentials or wait.') return fb_dtsg = self._search_regex( r'name="fb_dtsg" value="(.+?)"', login_results, 'fb_dtsg', default=None) h = self._search_regex( r'name="h"\s+(?:\w+="[^"]+"\s+)*?value="([^"]+)"', login_results, 'h', default=None) if not fb_dtsg or not h: return check_form = { 'fb_dtsg': fb_dtsg, 'h': h, 'name_action_selected': 'dont_save', } check_req = sanitized_Request(self._CHECKPOINT_URL, urlencode_postdata(check_form)) check_req.add_header('Content-Type', 'application/x-www-form-urlencoded') check_response = self._download_webpage(check_req, None, note='Confirming login') if re.search(r'id="checkpointSubmitButton"', check_response) is not None: self._downloader.report_warning('Unable to confirm login, you have to login in your browser and authorize the login.') except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err: self._downloader.report_warning('unable to log in: %s' % error_to_compat_str(err)) return def _real_initialize(self): self._login() def _extract_from_url(self, url, video_id): webpage = self._download_webpage( url.replace('://m.facebook.com/', '://www.facebook.com/'), video_id) video_data = None def extract_video_data(instances): video_data = [] for item in instances: if try_get(item, lambda x: x[1][0]) == 'VideoConfig': video_item = item[2][0] if video_item.get('video_id'): video_data.append(video_item['videoData']) return video_data server_js_data = self._parse_json(self._search_regex( [r'handleServerJS\(({.+})(?:\);|,")', r'\bs\.handle\(({.+?})\);'], webpage, 'server js data', default='{}'), video_id, fatal=False) if server_js_data: video_data = extract_video_data(server_js_data.get('instances', [])) def extract_from_jsmods_instances(js_data): if js_data: return extract_video_data(try_get( js_data, lambda x: x['jsmods']['instances'], list) or []) def extract_dash_manifest(video, formats): dash_manifest = video.get('dash_manifest') if dash_manifest: formats.extend(self._parse_mpd_formats( compat_etree_fromstring(compat_urllib_parse_unquote_plus(dash_manifest)))) def process_formats(formats): # Downloads with browser's User-Agent are rate limited. Working around # with non-browser User-Agent. for f in formats: f.setdefault('http_headers', {})['User-Agent'] = 'facebookexternalhit/1.1' self._sort_formats(formats) def extract_relay_data(_filter): return self._parse_json(self._search_regex( r'handleWithCustomApplyEach\([^,]+,\s*({.*?%s.*?})\);' % _filter, webpage, 'replay data', default='{}'), video_id, fatal=False) or {} def extract_relay_prefetched_data(_filter): replay_data = extract_relay_data(_filter) for require in (replay_data.get('require') or []): if require[0] == 'RelayPrefetchedStreamCache': return try_get(require, lambda x: x[3][1]['__bbox']['result']['data'], dict) or {} if not video_data: server_js_data = self._parse_json(self._search_regex([ r'bigPipe\.onPageletArrive\(({.+?})\)\s*;\s*}\s*\)\s*,\s*["\']onPageletArrive\s+' + self._SUPPORTED_PAGLETS_REGEX, r'bigPipe\.onPageletArrive\(({.*?id\s*:\s*"%s".*?})\);' % self._SUPPORTED_PAGLETS_REGEX ], webpage, 'js data', default='{}'), video_id, js_to_json, False) video_data = extract_from_jsmods_instances(server_js_data) if not video_data: data = extract_relay_prefetched_data( r'"(?:dash_manifest|playable_url(?:_quality_hd)?)"\s*:\s*"[^"]+"') if data: entries = [] def parse_graphql_video(video): formats = [] q = qualities(['sd', 'hd']) for (suffix, format_id) in [('', 'sd'), ('_quality_hd', 'hd')]: playable_url = video.get('playable_url' + suffix) if not playable_url: continue formats.append({ 'format_id': format_id, 'quality': q(format_id), 'url': playable_url, }) extract_dash_manifest(video, formats) process_formats(formats) v_id = video.get('videoId') or video.get('id') or video_id info = { 'id': v_id, 'formats': formats, 'thumbnail': try_get(video, lambda x: x['thumbnailImage']['uri']), 'uploader_id': try_get(video, lambda x: x['owner']['id']), 'timestamp': int_or_none(video.get('publish_time')), 'duration': float_or_none(video.get('playable_duration_in_ms'), 1000), } description = try_get(video, lambda x: x['savable_description']['text']) title = video.get('name') if title: info.update({ 'title': title, 'description': description, }) else: info['title'] = description or 'Facebook video #%s' % v_id entries.append(info) def parse_attachment(attachment, key='media'): media = attachment.get(key) or {} if media.get('__typename') == 'Video': return parse_graphql_video(media) nodes = data.get('nodes') or [] node = data.get('node') or {} if not nodes and node: nodes.append(node) for node in nodes: story = try_get(node, lambda x: x['comet_sections']['content']['story'], dict) or {} attachments = try_get(story, [ lambda x: x['attached_story']['attachments'], lambda x: x['attachments'] ], list) or [] for attachment in attachments: attachment = try_get(attachment, lambda x: x['style_type_renderer']['attachment'], dict) ns = try_get(attachment, lambda x: x['all_subattachments']['nodes'], list) or [] for n in ns: parse_attachment(n) parse_attachment(attachment) edges = try_get(data, lambda x: x['mediaset']['currMedia']['edges'], list) or [] for edge in edges: parse_attachment(edge, key='node') video = data.get('video') or {} if video: attachments = try_get(video, [ lambda x: x['story']['attachments'], lambda x: x['creation_story']['attachments'] ], list) or [] for attachment in attachments: parse_attachment(attachment) if not entries: parse_graphql_video(video) return self.playlist_result(entries, video_id) if not video_data: m_msg = re.search(r'class="[^"]*uiInterstitialContent[^"]*"><div>(.*?)</div>', webpage) if m_msg is not None: raise ExtractorError( 'The video is not available, Facebook said: "%s"' % m_msg.group(1), expected=True) elif '>You must log in to continue' in webpage: self.raise_login_required() if not video_data and '/watchparty/' in url: post_data = { 'doc_id': 3731964053542869, 'variables': json.dumps({ 'livingRoomID': video_id, }), } prefetched_data = extract_relay_prefetched_data(r'"login_data"\s*:\s*{') if prefetched_data: lsd = try_get(prefetched_data, lambda x: x['login_data']['lsd'], dict) if lsd: post_data[lsd['name']] = lsd['value'] relay_data = extract_relay_data(r'\[\s*"RelayAPIConfigDefaults"\s*,') for define in (relay_data.get('define') or []): if define[0] == 'RelayAPIConfigDefaults': self._api_config = define[2] living_room = self._download_json( urljoin(url, self._api_config['graphURI']), video_id, data=urlencode_postdata(post_data))['data']['living_room'] entries = [] for edge in (try_get(living_room, lambda x: x['recap']['watched_content']['edges']) or []): video = try_get(edge, lambda x: x['node']['video']) or {} v_id = video.get('id') if not v_id: continue v_id = compat_str(v_id) entries.append(self.url_result( self._VIDEO_PAGE_TEMPLATE % v_id, self.ie_key(), v_id, video.get('name'))) return self.playlist_result(entries, video_id) if not video_data: # Video info not in first request, do a secondary request using # tahoe player specific URL tahoe_data = self._download_webpage( self._VIDEO_PAGE_TAHOE_TEMPLATE % video_id, video_id, data=urlencode_postdata({ '__a': 1, '__pc': self._search_regex( r'pkg_cohort["\']\s*:\s*["\'](.+?)["\']', webpage, 'pkg cohort', default='PHASED:DEFAULT'), '__rev': self._search_regex( r'client_revision["\']\s*:\s*(\d+),', webpage, 'client revision', default='3944515'), 'fb_dtsg': self._search_regex( r'"DTSGInitialData"\s*,\s*\[\]\s*,\s*{\s*"token"\s*:\s*"([^"]+)"', webpage, 'dtsg token', default=''), }), headers={ 'Content-Type': 'application/x-www-form-urlencoded', }) tahoe_js_data = self._parse_json( self._search_regex( r'for\s+\(\s*;\s*;\s*\)\s*;(.+)', tahoe_data, 'tahoe js data', default='{}'), video_id, fatal=False) video_data = extract_from_jsmods_instances(tahoe_js_data) if not video_data: raise ExtractorError('Cannot parse data') if len(video_data) > 1: entries = [] for v in video_data: video_url = v[0].get('video_url') if not video_url: continue entries.append(self.url_result(urljoin( url, video_url), self.ie_key(), v[0].get('video_id'))) return self.playlist_result(entries, video_id) video_data = video_data[0] formats = [] subtitles = {} for f in video_data: format_id = f['stream_type'] if f and isinstance(f, dict): f = [f] if not f or not isinstance(f, list): continue for quality in ('sd', 'hd'): for src_type in ('src', 'src_no_ratelimit'): src = f[0].get('%s_%s' % (quality, src_type)) if src: preference = -10 if format_id == 'progressive' else 0 if quality == 'hd': preference += 5 formats.append({ 'format_id': '%s_%s_%s' % (format_id, quality, src_type), 'url': src, 'quality': preference, }) extract_dash_manifest(f[0], formats) subtitles_src = f[0].get('subtitles_src') if subtitles_src: subtitles.setdefault('en', []).append({'url': subtitles_src}) if not formats: raise ExtractorError('Cannot find video formats') process_formats(formats) video_title = self._html_search_regex( r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>([^<]*)</h2>', webpage, 'title', default=None) if not video_title: video_title = self._html_search_regex( r'(?s)<span class="fbPhotosPhotoCaption".*?id="fbPhotoPageCaption"><span class="hasCaption">(.*?)</span>', webpage, 'alternative title', default=None) if not video_title: video_title = self._html_search_meta( 'description', webpage, 'title', default=None) if video_title: video_title = limit_length(video_title, 80) else: video_title = 'Facebook video #%s' % video_id uploader = clean_html(get_element_by_id( 'fbPhotoPageAuthorName', webpage)) or self._search_regex( r'ownerName\s*:\s*"([^"]+)"', webpage, 'uploader', default=None) or self._og_search_title(webpage, fatal=False) timestamp = int_or_none(self._search_regex( r'<abbr[^>]+data-utime=["\'](\d+)', webpage, 'timestamp', default=None)) thumbnail = self._html_search_meta(['og:image', 'twitter:image'], webpage) view_count = parse_count(self._search_regex( r'\bviewCount\s*:\s*["\']([\d,.]+)', webpage, 'view count', default=None)) info_dict = { 'id': video_id, 'title': video_title, 'formats': formats, 'uploader': uploader, 'timestamp': timestamp, 'thumbnail': thumbnail, 'view_count': view_count, 'subtitles': subtitles, } return info_dict def _real_extract(self, url): video_id = self._match_id(url) real_url = self._VIDEO_PAGE_TEMPLATE % video_id if url.startswith('facebook:') else url return self._extract_from_url(real_url, video_id) class FacebookPluginsVideoIE(InfoExtractor): _VALID_URL = r'https?://(?:[\w-]+\.)?facebook\.com/plugins/video\.php\?.*?\bhref=(?P<id>https.+)' _TESTS = [{ 'url': 'https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fgov.sg%2Fvideos%2F10154383743583686%2F&show_text=0&width=560', 'md5': '5954e92cdfe51fe5782ae9bda7058a07', 'info_dict': { 'id': '10154383743583686', 'ext': 'mp4', 'title': 'What to do during the haze?', 'uploader': 'Gov.sg', 'upload_date': '20160826', 'timestamp': 1472184808, }, 'add_ie': [FacebookIE.ie_key()], }, { 'url': 'https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fvideo.php%3Fv%3D10204634152394104', 'only_matching': True, }, { 'url': 'https://www.facebook.com/plugins/video.php?href=https://www.facebook.com/gov.sg/videos/10154383743583686/&show_text=0&width=560', 'only_matching': True, }] def _real_extract(self, url): return self.url_result( compat_urllib_parse_unquote(self._match_id(url)), FacebookIE.ie_key())
yt_dlp/extractor/facebook.py
30,062
coding: utf-8 data.video have 1080P, but only up to 720p in swf params data.video.story.attachments[].media bigPipe.onPageletArrive ... onPageletArrive pagelet_group_mall data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media data.mediaset.currMedia.edges data.video.story.attachments[].media data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.media data.video.creation_story.attachments[].media data.video no title data.video data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.all_subattachments.nodes[].media data.video.story.attachments[].media data.node.comet_sections.content.story.attachments[].style_type_renderer.attachment.all_subattachments.nodes[].media data.nodes[].comet_sections.content.story.attachments[].style_type_renderer.attachment.media TODO: title can be extracted from video page node.comet_sections.content.story.attached_story.attachments.style_type_renderer.attachment.media data.video.creation_story.attachments[].media Facebook API embed see https://developers.facebook.com/docs/plugins/embedded-video-player Downloads with browser's User-Agent are rate limited. Working around with non-browser User-Agent. Video info not in first request, do a secondary request using tahoe player specific URL
1,413
en
0.381764
""" The patch module allows for a grid to be created and for data to be defined on that grid. Typical usage: -- create the grid grid = Grid1d(nx) -- create the data that lives on that grid data = CellCenterData1d(grid) bcObj = bcObject(xlb="reflect", xrb="reflect"_ data.registerVar("density", bcObj) ... data.create() -- initialize some data dens = data.get_var("density") dens[:,:] = ... -- fill the ghost cells data.fil_lBC("density") """ from __future__ import print_function import sys import numpy valid = ["outflow", "periodic", "reflect", "reflect-even", "reflect-odd", "dirichlet", "neumann"] class BCObject(object): """ Boundary condition container -- hold the BCs on each boundary for a single variable """ def __init__(self, xlb="outflow", xrb="outflow", odd_reflect_dir=""): # note: "reflect" is ambiguous and will be converted into # either reflect-even (the default) or reflect-odd if xlb not in valid or xrb not in valid: sys.exit("ERROR: invalid BC") # -x boundary self.xlb = xlb if self.xlb == "reflect": self.xlb = numpy.where(odd_reflect_dir == "x", "reflect-odd", "reflect-even") # +x boundary self.xrb = xrb if self.xrb == "reflect": self.xrb = numpy.where(odd_reflect_dir == "x", "reflect-odd", "reflect-even") # periodic checks if ((xlb == "periodic" and xrb != "periodic") or (xrb == "periodic" and xlb != "periodic")): sys.exit("ERROR: both xlb and xrb must be periodic") def __str__(self): """ print out some basic information about the BC object """ string = "BCs: -x: %s +x: %s " % \ (self.xlb, self.xrb) return string class Grid1d(object): """ the 1-d grid class. The grid object will contain the coordinate information (at various centerings). A basic (1-d) representation of the layout is: | | | X | | | | X | | | +--*--+- // -+--*--X--*--+--*--+- // -+--*--+--*--X--*--+- // -+--*--+ 0 ng-1 ng ng+1 ... ng+nx-1 ng+nx 2ng+nx-1 ilo ihi |<- ng ghostcells->|<---- nx interior zones ----->|<- ng ghostcells->| The '*' marks the data locations. """ def __init__(self, nx, ng=1, xmin=0.0, xmax=1.0): """ The class constructor function. The only data that we require is the number of points that make up the mesh. We optionally take the extrema of the domain, number of ghost cells (assume 1) """ # size of grid self.nx = nx self.ng = ng self.qx = 2*ng+nx # domain extrema self.xmin = xmin self.xmax = xmax # compute the indices of the block interior (excluding guardcells) self.ilo = ng self.ihi = ng+nx-1 # define the coordinate information at the left, center, and right # zone coordinates self.dx = (xmax - xmin)/nx self.xl = (numpy.arange(nx+2*ng) - ng)*self.dx + xmin self.xr = (numpy.arange(nx+2*ng) + 1.0 - ng)*self.dx + xmin self.x = 0.5*(self.xl + self.xr) def scratch_array(self): return numpy.zeros((self.qx), dtype=numpy.float64) def __str__(self): """ print out some basic information about the grid object """ return "1-d grid: nx = {}, ng = {}".format(self.nx, self.ng) class CellCenterData1d(object): """ the cell-centered data that lives on a grid. a CellCenterData1d object is built in a multi-step process before it can be used. We pass in a grid object to describe where the data lives: my_data = patch.CellCenterData1d(myGrid) register any variables that we expect to live on this patch. Here bcObject describes the boundary conditions for that variable. my_data.registerVar('density', bcObject) my_data.registerVar('x-momentum', bcObject) ... finally, finish the initialization of the patch my_data.create() This last step actually allocates the storage for the state variables. Once this is done, the patch is considered to be locked. New variables cannot be added. """ def __init__(self, grid, dtype=numpy.float64): self.grid = grid self.dtype = dtype self.data = None self.vars = [] self.nvar = 0 self.BCs = {} # time self.t = -1 self.initialized = 0 def register_var(self, name, bc_object): """ register a variable with CellCenterData1d object. Here we pass in a BCObject that describes the boundary conditions for that variable. """ if self.initialized == 1: sys.exit("ERROR: grid already initialized") self.vars.append(name) self.nvar += 1 self.BCs[name] = bc_object def create(self): """ called after all the variables are registered and allocates the storage for the state data """ if self.initialized == 1: sys.exit("ERROR: grid already initialized") self.data = numpy.zeros((self.nvar, self.grid.qx), dtype=self.dtype) self.initialized = 1 def __str__(self): """ print out some basic information about the ccData2d object """ if self.initialized == 0: mystr = "CellCenterData1d object not yet initialized" return mystr mystr = "cc data: nx = {}, ng = {}\n".format(self.grid.nx, self.grid.ng) + \ " nvars = {}\n".format(self.nvar) + \ "variables: \n" ilo = self.grid.ilo ihi = self.grid.ihi for n in range(self.nvar): mystr += "%16s: min: %15.10f max: %15.10f\n" % \ (self.vars[n], numpy.min(self.data[n, ilo:ihi+1]), numpy.max(self.data[n, ilo:ihi+1])) mystr += "%16s BCs: -x: %-12s +x: %-12s \n" %\ (" ", self.BCs[self.vars[n]].xlb, self.BCs[self.vars[n]].xrb) return mystr def get_var(self, name): """ return a data array the variable described by name. Any changes made to this are automatically reflected in the CellCenterData1d object. """ n = self.vars.index(name) return self.data[n, :] def zero(self, name): n = self.vars.index(name) self.data[n, :] = 0.0 def fill_BC_all(self): """ fill boundary conditions on all variables """ for name in self.vars: self.fill_BC(name) def fill_BC(self, name): """ fill the boundary conditions. This operates on a single state variable at a time, to allow for maximum flexibility we do periodic, reflect-even, reflect-odd, and outflow each variable name has a corresponding bc_object stored in the ccData2d object -- we refer to this to figure out the action to take at each boundary. """ # there is only a single grid, so every boundary is on # a physical boundary (except if we are periodic) # Note: we piggy-back on outflow and reflect-odd for # Neumann and Dirichlet homogeneous BCs respectively, but # this only works for a single ghost cell n = self.vars.index(name) # -x boundary if self.BCs[name].xlb == "outflow" or self.BCs[name].xlb == "neumann": for i in range(0, self.grid.ilo): self.data[n, i] = self.data[n, self.grid.ilo] elif self.BCs[name].xlb == "reflect-even": for i in range(0, self.grid.ilo): self.data[n, i] = self.data[n, 2*self.grid.ng-i-1] elif self.BCs[name].xlb in ["reflect-odd", "dirichlet"]: for i in range(0, self.grid.ilo): self.data[n, i] = -self.data[n, 2*self.grid.ng-i-1] elif self.BCs[name].xlb == "periodic": for i in range(0, self.grid.ilo): self.data[n, i] = self.data[n, self.grid.ihi-self.grid.ng+i+1] # +x boundary if self.BCs[name].xrb == "outflow" or self.BCs[name].xrb == "neumann": for i in range(self.grid.ihi+1, self.grid.nx+2*self.grid.ng): self.data[n, i] = self.data[n, self.grid.ihi] elif self.BCs[name].xrb == "reflect-even": for i in range(0, self.grid.ng): i_bnd = self.grid.ihi+1+i i_src = self.grid.ihi-i self.data[n, i_bnd] = self.data[n, i_src] elif self.BCs[name].xrb in ["reflect-odd", "dirichlet"]: for i in range(0, self.grid.ng): i_bnd = self.grid.ihi+1+i i_src = self.grid.ihi-i self.data[n, i_bnd] = -self.data[n, i_src] elif self.BCs[name].xrb == "periodic": for i in range(self.grid.ihi+1, 2*self.grid.ng + self.grid.nx): self.data[n, i] = self.data[n, i-self.grid.ihi-1+self.grid.ng] def restrict(self, varname): """ restrict the variable varname to a coarser grid (factor of 2 coarser) and return an array with the resulting data (and same number of ghostcells) """ fG = self.grid fData = self.get_var(varname) # allocate an array for the coarsely gridded data ng_c = fG.ng nx_c = fG.nx//2 cData = numpy.zeros((2*ng_c+nx_c), dtype=self.dtype) ilo_c = ng_c ihi_c = ng_c+nx_c-1 # fill the coarse array with the restricted data -- just # average the 2 fine cells into the corresponding coarse cell # that encompasses them. # This is done by shifting our view into the fData array and # using a stride of 2 in the indexing. cData[ilo_c:ihi_c+1] = \ 0.5*(fData[fG.ilo :fG.ihi+1:2] + fData[fG.ilo+1:fG.ihi+1:2]) return cData def prolong(self, varname): """ prolong the data in the current (coarse) grid to a finer (factor of 2 finer) grid. Return an array with the resulting data (and same number of ghostcells). We will reconstruct the data in the zone from the zone-averaged variables using the centered-difference slopes (x) f(x,y) = m x/dx + <f> When averaged over the parent cell, this reproduces <f>. Each zone's reconstrution will be averaged over 2 children. | | | | | | <f> | --> | | | | | | 1 | 2 | +-----------+ +-----+-----+ We will fill each of the finer resolution zones by filling all the 1's together, using a stride 2 into the fine array. Then the 2's, this allows us to operate in a vector fashion. All operations will use the same slopes for their respective parents. """ cG = self.grid cData = self.get_var(varname) # allocate an array for the coarsely gridded data ng_f = cG.ng nx_f = cG.nx*2 fData = numpy.zeros((2*ng_f+nx_f), dtype=self.dtype) ilo_f = ng_f ihi_f = ng_f+nx_f-1 # slopes for the coarse data m_x = cG.scratch_array() m_x[cG.ilo:cG.ihi+1] = \ 0.5*(cData[cG.ilo+1:cG.ihi+2] - cData[cG.ilo-1:cG.ihi]) # fill the '1' children fData[ilo_f:ihi_f+1:2] = \ cData[cG.ilo:cG.ihi+1] - 0.25*m_x[cG.ilo:cG.ihi+1] # fill the '2' children fData[ilo_f+1:ihi_f+1:2] = \ cData[cG.ilo:cG.ihi+1] + 0.25*m_x[cG.ilo:cG.ihi+1] return fData if __name__ == "__main__": # illustrate basic mesh operations myg = Grid1d(16, xmax=1.0) mydata = CellCenterData1d(myg) bc = BCObject() mydata.register_var("a", bc) mydata.create() a = mydata.get_var("a") a[:] = numpy.exp(-(myg.x - 0.5)**2/0.1**2) print(mydata)
multigrid/patch1d.py
12,391
Boundary condition container -- hold the BCs on each boundary for a single variable the cell-centered data that lives on a grid. a CellCenterData1d object is built in a multi-step process before it can be used. We pass in a grid object to describe where the data lives: my_data = patch.CellCenterData1d(myGrid) register any variables that we expect to live on this patch. Here bcObject describes the boundary conditions for that variable. my_data.registerVar('density', bcObject) my_data.registerVar('x-momentum', bcObject) ... finally, finish the initialization of the patch my_data.create() This last step actually allocates the storage for the state variables. Once this is done, the patch is considered to be locked. New variables cannot be added. the 1-d grid class. The grid object will contain the coordinate information (at various centerings). A basic (1-d) representation of the layout is: | | | X | | | | X | | | +--*--+- // -+--*--X--*--+--*--+- // -+--*--+--*--X--*--+- // -+--*--+ 0 ng-1 ng ng+1 ... ng+nx-1 ng+nx 2ng+nx-1 ilo ihi |<- ng ghostcells->|<---- nx interior zones ----->|<- ng ghostcells->| The '*' marks the data locations. The class constructor function. The only data that we require is the number of points that make up the mesh. We optionally take the extrema of the domain, number of ghost cells (assume 1) print out some basic information about the BC object print out some basic information about the grid object print out some basic information about the ccData2d object called after all the variables are registered and allocates the storage for the state data fill the boundary conditions. This operates on a single state variable at a time, to allow for maximum flexibility we do periodic, reflect-even, reflect-odd, and outflow each variable name has a corresponding bc_object stored in the ccData2d object -- we refer to this to figure out the action to take at each boundary. fill boundary conditions on all variables return a data array the variable described by name. Any changes made to this are automatically reflected in the CellCenterData1d object. prolong the data in the current (coarse) grid to a finer (factor of 2 finer) grid. Return an array with the resulting data (and same number of ghostcells). We will reconstruct the data in the zone from the zone-averaged variables using the centered-difference slopes (x) f(x,y) = m x/dx + <f> When averaged over the parent cell, this reproduces <f>. Each zone's reconstrution will be averaged over 2 children. | | | | | | <f> | --> | | | | | | 1 | 2 | +-----------+ +-----+-----+ We will fill each of the finer resolution zones by filling all the 1's together, using a stride 2 into the fine array. Then the 2's, this allows us to operate in a vector fashion. All operations will use the same slopes for their respective parents. register a variable with CellCenterData1d object. Here we pass in a BCObject that describes the boundary conditions for that variable. restrict the variable varname to a coarser grid (factor of 2 coarser) and return an array with the resulting data (and same number of ghostcells) The patch module allows for a grid to be created and for data to be defined on that grid. Typical usage: -- create the grid grid = Grid1d(nx) -- create the data that lives on that grid data = CellCenterData1d(grid) bcObj = bcObject(xlb="reflect", xrb="reflect"_ data.registerVar("density", bcObj) ... data.create() -- initialize some data dens = data.get_var("density") dens[:,:] = ... -- fill the ghost cells data.fil_lBC("density") note: "reflect" is ambiguous and will be converted into either reflect-even (the default) or reflect-odd -x boundary +x boundary periodic checks size of grid domain extrema compute the indices of the block interior (excluding guardcells) define the coordinate information at the left, center, and right zone coordinates time there is only a single grid, so every boundary is on a physical boundary (except if we are periodic) Note: we piggy-back on outflow and reflect-odd for Neumann and Dirichlet homogeneous BCs respectively, but this only works for a single ghost cell -x boundary +x boundary allocate an array for the coarsely gridded data fill the coarse array with the restricted data -- just average the 2 fine cells into the corresponding coarse cell that encompasses them. This is done by shifting our view into the fData array and using a stride of 2 in the indexing. allocate an array for the coarsely gridded data slopes for the coarse data fill the '1' children fill the '2' children illustrate basic mesh operations
4,885
en
0.783566
#!/usr/bin/python # coding: utf-8 # This file is execfile()d with the current directory set to its containing # dir. Note that not all possible configuration values are present in this # autogenerated file. All configuration values have a default; values that are # commented out serve to show the default. import sys import os import sphinx_rtd_theme # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. sys.path.insert(0, os.path.abspath(os.path.join('..'))) import pageit # noqa # -- General configuration ---------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. # needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = ['sphinx.ext.doctest', 'sphinx.ext.autodoc', 'sphinxcontrib.napoleon'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. # source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. project = u'pageit' copyright = u'2013, Metaist' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. version = pageit.__version__ # The full version, including alpha/beta/rc tags. release = version # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. # language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: # today = '' # Else, today_fmt is used as the format for a strftime call. # today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ['_build'] # The reST default role (used for this markup: `text`) to use for all # documents. # default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. # add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). # add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. # show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. # modindex_common_prefix = [] # -- Options for HTML output -------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # html_theme = 'default' html_theme = "sphinx_rtd_theme" # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. # html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. # html_theme_path = [] html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] # The name for this set of Sphinx documents. If None, it defaults to # "<project> v<release> documentation". # html_title = None # A shorter title for the navigation bar. Default is the same as html_title. # html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. # html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. # html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. # html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. # html_use_smartypants = True # Custom sidebar templates, maps document names to template names. # html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. # html_additional_pages = {} # If false, no module index is generated. # html_domain_indices = True # If false, no index is generated. # html_use_index = True # If true, the index is split into individual pages for each letter. # html_split_index = False # If true, links to the reST sources are added to the pages. # html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. # html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. # html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a <link> tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. # html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). # html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = 'pageitdoc' # -- Options for LaTeX output ------------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). # 'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). # 'pointsize': '10pt', # Additional stuff for the LaTeX preamble. # 'preamble': '', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass # [howto/manual]). latex_documents = [ ('index', 'pageit.tex', u'pageit Documentation', u'The Metaist', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. # latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. # latex_use_parts = False # If true, show page references after internal links. # latex_show_pagerefs = False # If true, show URL addresses after external links. # latex_show_urls = False # Documents to append as an appendix to all manuals. # latex_appendices = [] # If false, no module index is generated. # latex_domain_indices = True # -- Options for manual page output ------------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ ('index', 'pageit', u'pageit Documentation', [u'The Metaist'], 1) ] # If true, show URL addresses after external links. # man_show_urls = False # -- Options for Texinfo output ----------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ ('index', 'pageit', u'pageit Documentation', u'The Metaist', 'pageit', pageit.__doc__.split('\n')[0], 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. # texinfo_appendices = [] # If false, no module index is generated. # texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. # texinfo_show_urls = 'footnote'
docs/conf.py
7,922
!/usr/bin/python coding: utf-8 This file is execfile()d with the current directory set to its containing dir. Note that not all possible configuration values are present in this autogenerated file. All configuration values have a default; values that are commented out serve to show the default. If extensions (or modules to document with autodoc) are in another directory, add these directories to sys.path here. If the directory is relative to the documentation root, use os.path.abspath to make it absolute, like shown here. noqa -- General configuration ---------------------------------------------------- If your documentation needs a minimal Sphinx version, state it here. needs_sphinx = '1.0' Add any Sphinx extension module names here, as strings. They can be extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. Add any paths that contain templates here, relative to this directory. The suffix of source filenames. The encoding of source files. source_encoding = 'utf-8-sig' The master toctree document. General information about the project. The version info for the project you're documenting, acts as replacement for |version| and |release|, also used in various other places throughout the built documents. The short X.Y version. The full version, including alpha/beta/rc tags. The language for content autogenerated by Sphinx. Refer to documentation for a list of supported languages. language = None There are two options for replacing |today|: either, you set today to some non-false value, then it is used: today = '' Else, today_fmt is used as the format for a strftime call. today_fmt = '%B %d, %Y' List of patterns, relative to source directory, that match files and directories to ignore when looking for source files. The reST default role (used for this markup: `text`) to use for all documents. default_role = None If true, '()' will be appended to :func: etc. cross-reference text. add_function_parentheses = True If true, the current module name will be prepended to all description unit titles (such as .. function::). add_module_names = True If true, sectionauthor and moduleauthor directives will be shown in the output. They are ignored by default. show_authors = False The name of the Pygments (syntax highlighting) style to use. A list of ignored prefixes for module index sorting. modindex_common_prefix = [] -- Options for HTML output -------------------------------------------------- The theme to use for HTML and HTML Help pages. See the documentation for a list of builtin themes. html_theme = 'default' Theme options are theme-specific and customize the look and feel of a theme further. For a list of options available for each theme, see the documentation. html_theme_options = {} Add any paths that contain custom themes here, relative to this directory. html_theme_path = [] The name for this set of Sphinx documents. If None, it defaults to "<project> v<release> documentation". html_title = None A shorter title for the navigation bar. Default is the same as html_title. html_short_title = None The name of an image file (relative to this directory) to place at the top of the sidebar. html_logo = None The name of an image file (within the static path) to use as favicon of the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 pixels large. html_favicon = None Add any paths that contain custom static files (such as style sheets) here, relative to this directory. They are copied after the builtin static files, so a file named "default.css" will overwrite the builtin "default.css". If not '', a 'Last updated on:' timestamp is inserted at every page bottom, using the given strftime format. html_last_updated_fmt = '%b %d, %Y' If true, SmartyPants will be used to convert quotes and dashes to typographically correct entities. html_use_smartypants = True Custom sidebar templates, maps document names to template names. html_sidebars = {} Additional templates that should be rendered to pages, maps page names to template names. html_additional_pages = {} If false, no module index is generated. html_domain_indices = True If false, no index is generated. html_use_index = True If true, the index is split into individual pages for each letter. html_split_index = False If true, links to the reST sources are added to the pages. html_show_sourcelink = True If true, "Created using Sphinx" is shown in the HTML footer. Default is True. html_show_sphinx = True If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. html_show_copyright = True If true, an OpenSearch description file will be output, and all pages will contain a <link> tag referring to it. The value of this option must be the base URL from which the finished HTML is served. html_use_opensearch = '' This is the file name suffix for HTML files (e.g. ".xhtml"). html_file_suffix = None Output file base name for HTML help builder. -- Options for LaTeX output ------------------------------------------------- The paper size ('letterpaper' or 'a4paper'). 'papersize': 'letterpaper', The font size ('10pt', '11pt' or '12pt'). 'pointsize': '10pt', Additional stuff for the LaTeX preamble. 'preamble': '', Grouping the document tree into LaTeX files. List of tuples (source start file, target name, title, author, documentclass [howto/manual]). The name of an image file (relative to this directory) to place at the top of the title page. latex_logo = None For "manual" documents, if this is true, then toplevel headings are parts, not chapters. latex_use_parts = False If true, show page references after internal links. latex_show_pagerefs = False If true, show URL addresses after external links. latex_show_urls = False Documents to append as an appendix to all manuals. latex_appendices = [] If false, no module index is generated. latex_domain_indices = True -- Options for manual page output ------------------------------------------- One entry per manual page. List of tuples (source start file, name, description, authors, manual section). If true, show URL addresses after external links. man_show_urls = False -- Options for Texinfo output ----------------------------------------------- Grouping the document tree into Texinfo files. List of tuples (source start file, target name, title, author, dir menu entry, description, category) Documents to append as an appendix to all manuals. texinfo_appendices = [] If false, no module index is generated. texinfo_domain_indices = True How to display URL addresses: 'footnote', 'no', or 'inline'. texinfo_show_urls = 'footnote'
6,568
en
0.678018
#!/usr/bin/python3 """ An example script that cleans up failed experiments by moving them to the archive """ import argparse from datetime import datetime from clearml_agent import APIClient parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--project", "-P", help="Project ID. Only clean up experiments from this project") parser.add_argument("--user", "-U", help="User ID. Only clean up experiments assigned to this user") parser.add_argument( "--status", "-S", default="failed", help="Experiment status. Only clean up experiments with this status (default %(default)s)" ) parser.add_argument( "--iterations", "-I", type=int, help="Number of iterations. Only clean up experiments with less or equal number of iterations" ) parser.add_argument( "--sec-from-start", "-T", type=int, help="Seconds from start time. " "Only clean up experiments if less or equal number of seconds have elapsed since started" ) args = parser.parse_args() client = APIClient() tasks = client.tasks.get_all( project=[args.project] if args.project else None, user=[args.user] if args.user else None, status=[args.status] if args.status else None, system_tags=["-archived"] ) count = 0 for task in tasks: if args.iterations and (task.last_iteration or 0) > args.iterations: continue if args.sec_from_start: if not task.started: continue if (datetime.utcnow() - task.started.replace(tzinfo=None)).total_seconds() > args.sec_from_start: continue try: client.tasks.edit( task=task.id, system_tags=(task.system_tags or []) + ["archived"], force=True ) count += 1 except Exception as ex: print("Failed editing experiment: {}".format(ex)) print("Cleaned up {} experiments".format(count))
examples/archive_experiments.py
1,871
An example script that cleans up failed experiments by moving them to the archive !/usr/bin/python3
100
en
0.905745
# Token type identifiers. AUT_INVALID = 0x00 AUT_OTHER_FILE32 = 0x11 AUT_OHEADER = 0x12 AUT_TRAILER = 0x13 AUT_HEADER32 = 0x14 AUT_HEADER32_EX = 0x15 AUT_DATA = 0x21 AUT_IPC = 0x22 AUT_PATH = 0x23 AUT_SUBJECT32 = 0x24 AUT_XATPATH = 0x25 AUT_PROCESS32 = 0x26 AUT_RETURN32 = 0x27 AUT_TEXT = 0x28 AUT_OPAQUE = 0x29 AUT_IN_ADDR = 0x2A AUT_IP = 0x2B AUT_IPORT = 0x2C AUT_ARG32 = 0x2D AUT_SOCKET = 0x2E AUT_SEQ = 0x2F AUT_ACL = 0x30 AUT_ATTR = 0x31 AUT_IPC_PERM = 0x32 AUT_LABEL = 0x33 AUT_GROUPS = 0x34 AUT_ACE = 0x35 AUT_PRIV = 0x38 AUT_UPRIV = 0x39 AUT_LIAISON = 0x3A AUT_NEWGROUPS = 0x3B AUT_EXEC_ARGS = 0x3C AUT_EXEC_ENV = 0x3D AUT_ATTR32 = 0x3E AUT_UNAUTH = 0x3F AUT_XATOM = 0x40 AUT_XOBJ = 0x41 AUT_XPROTO = 0x42 AUT_XSELECT = 0x43 AUT_XCOLORMAP = 0x44 AUT_XCURSOR = 0x45 AUT_XFONT = 0x46 AUT_XGC = 0x47 AUT_XPIXMAP = 0x48 AUT_XPROPERTY = 0x49 AUT_XWINDOW = 0x4A AUT_XCLIENT = 0x4B AUT_CMD = 0x51 AUT_EXIT = 0x52 AUT_ZONENAME = 0x60 AUT_HOST = 0x70 AUT_ARG64 = 0x71 AUT_RETURN64 = 0x72 AUT_ATTR64 = 0x73 AUT_HEADER64 = 0x74 AUT_SUBJECT64 = 0x75 AUT_PROCESS64 = 0x77 AUT_OTHER_FILE64 = 0x78 AUT_HEADER64_EX = 0x79 AUT_SUBJECT32_EX = 0x7A AUT_PROCESS32_EX = 0x7B AUT_SUBJECT64_EX = 0x7C AUT_PROCESS64_EX = 0x7D AUT_IN_ADDR_EX = 0x7E AUT_SOCKET_EX = 0x7F # # Pre-64-bit BSM, 32-bit tokens weren't explicitly named as '32'. We have # compatibility defines. AUT_HEADER = AUT_HEADER32 AUT_ARG = AUT_ARG32 AUT_RETURN = AUT_RETURN32 AUT_SUBJECT = AUT_SUBJECT32 AUT_PROCESS = AUT_PROCESS32 AUT_OTHER_FILE = AUT_OTHER_FILE32 # # * # The values for the following token ids are not defined by BSM. # # XXXRW: Not sure how to handle these in OpenBSM yet, but I'll give them # names more consistent with Sun's BSM. These originally came from Apple's # BSM. AUT_SOCKINET32 = 0x80 # XXX AUT_SOCKINET128 = 0x81 # XXX AUT_SOCKUNIX = 0x82 # XXX _AUT_RIGHTS = 0x83 # XXX FreeBSD AUT_ARG_UUID = 0x84 # UUID of argument object AUT_RETURN_UUID = 0x85 # UUID of returned object # # Apple specific tokens AUT_IDENTITY = 0xED AUT_KRB5_PRINCIPA = 0xEE AUT_CERT_HAHSH = 0xEF # print values for the arbitrary token AUP_BINARY = 0 AUP_OCTAL = 1 AUP_DECIMAL = 2 AUP_HEX = 3 AUP_STRING = 4 # # data-types for the arbitrary token AUR_BYTE = 0 AUR_CHAR = AUR_BYTE AUR_SHORT = 1 AUR_INT32 = 2 AUR_INT = AUR_INT32 AUR_INT64 = 3 # # ... and their sizes AUR_BYTE_SIZE = 1 # sizeof(u_char) AUR_CHAR_SIZE = AUR_BYTE_SIZE AUR_SHORT_SIZE = 2 # sizeof(uint16_t) AUR_INT32_SIZE = 4 # sizeof(uint32_t) AUR_INT_SIZE = AUR_INT32_SIZE AUR_INT64_SIZE = 8 # sizeof(uint64_t) AUR_BYTE_FORMAT = "B" AUR_CHAR_FORMAT = "c" AUR_SHORT_FORMAT = "H" AUR_INT32_FORMAT = "I" AUR_INT_FORMAT = AUR_INT32_FORMAT AUR_INT64_FORMAT = "Q" # # Modifiers for the header token PAD_NOTATTR = 0x4000 # nonattributable event PAD_FAILURE = 0x8000 # fail audit event # AUDIT_MAX_GROUPS = 16 # # * # A number of BSM versions are floating around and defined. Here are # constants for them. OpenBSM uses the same token types, etc, used in the # Solaris BSM version, but has a separate version number in order to # identify a potentially different event identifier name space. AUDIT_HEADER_VERSION_OLDDARWIN = 1 # In = retrospect, a mistake. AUDIT_HEADER_VERSION_SOLARIS = 2 AUDIT_HEADER_VERSION_TSOL25 = 3 AUDIT_HEADER_VERSION_TSOL = 4 AUDIT_HEADER_VERSION_OPENBSM10 = 10 AUDIT_HEADER_VERSION_OPENBSM11 = 11 AUDIT_HEADER_VERSION_OPENBSM = AUDIT_HEADER_VERSION_OPENBSM11 # AUT_TRAILER_MAGIC = 0xB105 # AUT_HEADERS = [AUT_HEADER32, AUT_HEADER32_EX, AUT_HEADER64, AUT_HEADER64_EX]
bsm/audit_record.py
3,550
Token type identifiers. Pre-64-bit BSM, 32-bit tokens weren't explicitly named as '32'. We have compatibility defines. * The values for the following token ids are not defined by BSM. XXXRW: Not sure how to handle these in OpenBSM yet, but I'll give them names more consistent with Sun's BSM. These originally came from Apple's BSM. XXX XXX XXX XXX FreeBSD UUID of argument object UUID of returned object Apple specific tokens print values for the arbitrary token data-types for the arbitrary token ... and their sizes sizeof(u_char) sizeof(uint16_t) sizeof(uint32_t) sizeof(uint64_t) Modifiers for the header token nonattributable event fail audit event * A number of BSM versions are floating around and defined. Here are constants for them. OpenBSM uses the same token types, etc, used in the Solaris BSM version, but has a separate version number in order to identify a potentially different event identifier name space. In = retrospect, a mistake.
957
en
0.878153
# # Copyright 2016 The BigDL 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. # from argparse import ArgumentParser from pyspark.sql import SparkSession from pyspark.sql.types import * LABEL_COL = 0 INT_COLS = list(range(1, 14)) CAT_COLS = list(range(14, 40)) if __name__ == '__main__': parser = ArgumentParser() parser.add_argument('--input', type=str, required=True, help="The path to the csv file to be processed.") parser.add_argument('--output', type=str, default=".", help="The path to the folder to save the parquet data.") args = parser.parse_args() spark = SparkSession.builder.getOrCreate() input = args.input output = args.output label_fields = [StructField('_c%d' % LABEL_COL, IntegerType())] int_fields = [StructField('_c%d' % i, IntegerType()) for i in INT_COLS] str_fields = [StructField('_c%d' % i, StringType()) for i in CAT_COLS] schema = StructType(label_fields + int_fields + str_fields) df = spark.read.schema(schema).option('sep', '\t').csv(input) df.write.parquet(output, mode="overwrite")
python/friesian/example/dlrm/csv_to_parquet.py
1,565
Copyright 2016 The BigDL 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.
555
en
0.863056
import numpy as np import scipy from scipy.stats import qmc from scipy.stats import special_ortho_group import matplotlib.pyplot as plt from scipy.optimize import minimize import warnings from .ssp import SSP class SSPSpace: def __init__(self, domain_dim: int, ssp_dim: int, axis_matrix=None, phase_matrix=None, domain_bounds=None, length_scale=1): self.sample_points = None self.sample_ssps = None self.domain_dim = domain_dim self.ssp_dim = ssp_dim if not isinstance(length_scale, np.ndarray) or length_scale.size == 1: self.length_scale = length_scale * np.ones((self.domain_dim,)) if domain_bounds is not None: assert domain_bounds.shape[0] == domain_dim self.domain_bounds = domain_bounds if (axis_matrix is None) & (phase_matrix is None): raise RuntimeError("SSP spaces must be defined by either a axis matrix or phase matrix. Use subclasses to construct spaces with predefined axes.") elif (phase_matrix is None): assert axis_matrix.shape[0] == ssp_dim, f'Expected ssp_dim {axis_matrix.shape[0]}, got {ssp_dim}.' assert axis_matrix.shape[1] == domain_dim self.axis_matrix = axis_matrix self.phase_matrix = (-1.j*np.log(np.fft.fft(axis_matrix,axis=0))).real elif (axis_matrix is None): assert phase_matrix.shape[0] == ssp_dim assert phase_matrix.shape[1] == domain_dim self.phase_matrix = phase_matrix self.axis_matrix = np.fft.ifft(np.exp(1.j*phase_matrix), axis=0).real def update_lengthscale(self, scale): if not isinstance(scale, np.ndarray) or scale.size == 1: self.length_scale = scale * np.ones((self.domain_dim,)) else: assert scale.size == self.domain_dim self.length_scale = scale assert self.length_scale.size == self.domain_dim def encode(self,x): assert x.shape[0] == self.domain_dim ls_mat = np.atleast_2d(np.diag(1/self.length_scale.flatten())) scaled_x = ls_mat @ x data = np.fft.ifft( np.exp( 1.j * self.phase_matrix @ scaled_x ), axis=0 ).real return data def encode_and_deriv(self,x): ls_mat = np.atleast_2d(np.diag(1 / self.length_scale)) scaled_x = x @ ls_mat fdata = np.exp( 1.j * self.phase_matrix @ scaled_x.T ) data = np.fft.ifft( fdata, axis=0 ).real ddata = np.fft.ifft( 1.j * np.stack([np.diag(fdata[:,j]) for j in range(x.shape[0])]) @ self.phase_matrix @ ls_mat, axis=0 ).real return data.T, ddata.T def encode_fourier(self,x): assert x.shape[0] == self.domain_dim ls_mat = np.atleast_2d(np.diag(1/self.length_scale.flatten())) scaled_x = ls_mat @ x data = np.exp( 1.j * self.phase_matrix @ scaled_x ) return data def encode_as_SSP(self,x): assert x.shape[0] == self.domain_dim return SSP(self.encode(x),self) def decode(self,ssp,method='from-set', num_sample_pts=10000,from_set_method='grid',num_init_pts =10): if method=='least-squares': # problems duw to complex log x = np.linalg.lstsq(self.phase_matrix, (1.j*np.log(np.fft.fft(ssp,axis=0))).real)[0] #raise NotImplementedError() #fssp = np.fft.fft(ssp,axis=0) #x = np.linalg.lstsq(np.tile(self.phase_matrix,(2,1)), np.hstack([np.arccos(fssp.real), np.arcsin(fssp.imag)])) return x elif method=='from-set': sample_ssps, sample_points = self.get_sample_ssps(num_sample_pts,method=from_set_method) sims = sample_ssps.T @ ssp return sample_points[:,np.argmax(sims)] elif method=='direct-optim': x0 = self.decode(ssp, method='from-set',num_sample_pts=num_init_pts) def min_func(x,target=ssp): x_ssp = self.encode(np.atleast_2d(x)) return -np.inner(x_ssp, target).flatten() soln = minimize(min_func, x0, method='L-BFGS-B') return soln.x elif method=='grad_descent': x = self.decode(ssp, method='from-set',num_sample_pts=num_init_pts) fssp = np.fft.fft(ssp,axis=0) ls_mat = np.diag(1/self.length_scale.flatten()) for j in range(10): scaled_x = ls_mat @ x x_enc = np.exp(1.j * self.phase_matrix @ scaled_x) grad_mat = (1.j * (self.phase_matrix @ ls_mat).T * x_enc) grad = (grad_mat @ fssp.T).flatten() x = x - 0.1*grad.real return x elif method=='nonlin-reg': x = self.decode(ssp, method='from-set',num_sample_pts=num_init_pts) fssp = np.fft.fft(ssp,axis=0) dy = np.hstack([fssp.real, fssp.imag]) ls_mat = np.diag(1/self.length_scale.flatten()) for j in range(10): J = np.vstack([self.phase_matrix * np.sin(self.phase_matrix @ x @ ls_mat).reshape(1,-1), -self.phase_matrix * np.cos(self.phase_matrix @ x @ ls_mat).reshape(1,-1)]) soln = np.linalg.pinv(J.T @ J) @ J.T @ dy x = x + soln return x else: raise NotImplementedError() def clean_up(self,ssp,**kwargs): x = self.decode(ssp,**kwargs) return self.encode(x) def get_sample_points(self,num_points,method='grid'): if self.domain_bounds is None: bounds = np.vstack([-10*np.ones(self.domain_dim), 10*np.ones(self.domain_dim)]).T else: bounds = self.domain_bounds if method=='grid': n_per_dim = int(num_points**(1/self.domain_dim)) if n_per_dim**self.domain_dim != num_points: warnings.warn((f'Evenly distributing points over a ' f'{self.domain_dim} grid requires numbers ' f'of samples to be powers of {self.domain_dim}.' f'Requested {num_points} samples, returning ' f'{n_per_dim**self.domain_dim}'), RuntimeWarning) ### end if xs = np.linspace(bounds[:,0],bounds[:,1],n_per_dim) xxs = np.meshgrid(*[xs[:,i] for i in range(self.domain_dim)]) sample_points = np.array([x.reshape(-1) for x in xxs]) return sample_points elif method=='sobol': sampler = qmc.Sobol(d=self.domain_dim) lbounds = bounds[:,0] ubounds = bounds[:,1] u_sample_points = sampler.random(num_points) sample_points = qmc.scale(u_sample_points, lbounds, ubounds) return sample_points.T else: raise NotImplementedError() def get_sample_ssps(self,num_points,**kwargs): # make new if num_pts different than whats stored? sample_points = self.get_sample_points(num_points,**kwargs) sample_ssps = self.encode(sample_points) return sample_ssps, sample_points def identity(self): s = np.zeros(self.ssp_dim) s[0] = 1 return s def bind(self,a,b): return np.fft.ifft(np.fft.fft(a) * np.fft.fft(b)).real def invert(self,a): return a[-np.arange(len(a))] def normalize(self,ssp): return ssp/np.max([1e-6,np.sqrt(np.sum(ssp**2))]) def unitary(self,ssp): fssp = np.fft.fft(ssp) fssp = fssp/np.sqrt(fssp.real**2 + fssp.imag**2) return np.fft.ifft(fssp).real def unitary_fourier(self,fssp): fssp = fssp/np.sqrt(fssp.real**2 + fssp.imag**2) return fssp def decode_path(self, ssp_path, N_ma=None, n_samples = 10000): sample_ssps, sample_points = self.get_sample_ssps(n_samples) path = np.zeros((ssp_path.shape[0], self.domain_dim)) max_sims = np.zeros(ssp_path.shape[0]) for i in range(ssp_path.shape[0]): sims = sample_ssps.T @ ssp_path[i,:] max_sims[i] = np.max(sims) path[i,:] = sample_points[:,np.argmax(sims)] return path, max_sims def similarity_plot(self,ssp,n_grid=100,plot_type='heatmap',cmap="YlGnBu",ax=None,**kwargs): if ax is None: fig = plt.figure() ax = fig.add_subplot(111) if self.domain_dim == 1: xs = np.linspace(self.domain_bounds[0,0],self.domain_bounds[0,1], n_grid) im=ax.plot(xs, self.encode(xs.reshape(1,-1)).T @ self.data) ax.set_xlim(self.domain_bounds[0,0],self.domain_bounds[0,1]) elif self.domain_dim == 2: xs = np.linspace(self.domain_bounds[0,0],self.domain_bounds[0,1], n_grid) ys = np.linspace(self.domain_bounds[1,0],self.domain_bounds[1,1], n_grid) X,Y = np.meshgrid(xs,ys) sims = self.encode(np.vstack([X.reshape(-1),Y.reshape(-1)])).T @ ssp if plot_type=='heatmap': im=ax.pcolormesh(X,Y,sims.reshape(X.shape),cmap=cmap,**kwargs) elif plot_type=='contour': im=ax.contour(X,Y,sims.reshape(X.shape),cmap=cmap,**kwargs) elif plot_type=='contourf': im=ax.contourf(X,Y,sims.reshape(X.shape),cmap=cmap,**kwargs) ax.set_xlim(self.domain_bounds[0,0],self.domain_bounds[0,1]) ax.set_ylim(self.domain_bounds[1,0],self.domain_bounds[1,1]) else: raise NotImplementedError() return im class RandomSSPSpace(SSPSpace): def __init__(self, domain_dim: int, ssp_dim: int, domain_bounds=None, length_scale=1, rng=np.random.default_rng()): partial_phases = rng.random.rand(ssp_dim//2,domain_dim)*2*np.pi - np.pi axis_matrix = _constructaxisfromphases(partial_phases) super().__init__(domain_dim,ssp_dim,axis_matrix=axis_matrix, domain_bounds=domain_bounds,length_scale=length_scale) class HexagonalSSPSpace(SSPSpace): def __init__(self, domain_dim:int,ssp_dim: int=151, n_rotates:int=5, n_scales:int=5, scale_min=2*np.pi/np.sqrt(6) - 0.5, scale_max=2*np.pi/np.sqrt(6) + 0.5, domain_bounds=None, length_scale=1): if (n_rotates==5) & (n_scales==5) & (ssp_dim != 151): n_rotates = int(np.max([1,np.sqrt((ssp_dim-1)/(2*(domain_dim+1)))])) n_scales = n_rotates phases_hex = np.hstack([np.sqrt(1+ 1/domain_dim)*np.identity(domain_dim) - (domain_dim**(-3/2))*(np.sqrt(domain_dim+1) + 1), (domain_dim**(-1/2))*np.ones((domain_dim,1))]).T self.grid_basis_dim = domain_dim + 1 self.num_grids = n_rotates*n_scales scales = np.linspace(scale_min,scale_max,n_scales) phases_scaled = np.vstack([phases_hex*i for i in scales]) if (n_rotates==1): phases_scaled_rotated = phases_scaled elif (domain_dim==1): scales = np.linspace(scale_min,scale_max,n_scales+n_rotates) phases_scaled_rotated = np.vstack([phases_hex*i for i in scales]) elif (domain_dim == 2): angles = np.linspace(0,2*np.pi/3,n_rotates) R_mats = np.stack([np.stack([np.cos(angles), -np.sin(angles)],axis=1), np.stack([np.sin(angles), np.cos(angles)], axis=1)], axis=1) phases_scaled_rotated = (R_mats @ phases_scaled.T).transpose(0,2,1).reshape(-1,domain_dim) else: R_mats = special_ortho_group.rvs(domain_dim, size=n_rotates) phases_scaled_rotated = (R_mats @ phases_scaled.T).transpose(0,2,1).reshape(-1,domain_dim) axis_matrix = _constructaxisfromphases(phases_scaled_rotated) ssp_dim = axis_matrix.shape[0] super().__init__(domain_dim,ssp_dim,axis_matrix=axis_matrix, domain_bounds=domain_bounds,length_scale=length_scale) def sample_grid_encoders(self, n): sample_pts = self.get_sample_points(n,method='sobol') N = self.num_grids if N < n: sorts = np.hstack([np.arange(N), np.random.randint(0, N - 1, size = n - N)]) else: sorts = np.arange(n) encoders = np.zeros((self.ssp_dim,n)) for i in range(n): sub_mat = _get_sub_SSP(sorts[i],N,sublen=self.grid_basis_dim) proj_mat = _proj_sub_SSP(sorts[i],N,sublen=self.grid_basis_dim) sub_space = SSPSpace(self.domain_dim,2*self.grid_basis_dim + 1, axis_matrix= sub_mat @ self.axis_matrix) encoders[:,i] = N * proj_mat @ sub_space.encode(sample_pts[:,i]) return encoders def _constructaxisfromphases(K): d = K.shape[0] n = K.shape[1] axes = np.ones((d*2 + 1,n)) for i in range(n): F = np.ones((d*2 + 1,), dtype="complex") F[0:d] = np.exp(1.j*K[:,i]) F[-d:] = np.flip(np.conj(F[0:d])) F = np.fft.ifftshift(F) axes[:,i] = np.fft.ifft(F).real return axes def _get_sub_FourierSSP(n, N, sublen=3): # Return a matrix, \bar{A}_n # Consider the multi scale representation (S_{total}) and sub vectors (S_n) described in the paper # Then # \bar{A}_n F{S_{total}} = F{S_n} # i.e. pick out the sub vector in the Fourier domain tot_len = 2*sublen*N + 1 FA = np.zeros((2*sublen + 1, tot_len)) FA[0:sublen, sublen*n:sublen*(n+1)] = np.eye(sublen) FA[sublen, sublen*N] = 1 FA[sublen+1:, tot_len - np.arange(sublen*(n+1),sublen*n,-1)] = np.eye(sublen) return FA def _get_sub_SSP(n,N,sublen=3): # Return a matrix, A_n # Consider the multi scale representation (S_{total}) and sub vectors (S_n) described in the paper # Then # A_n S_{total} = S_n # i.e. pick out the sub vector in the time domain tot_len = 2*sublen*N + 1 FA = _get_sub_FourierSSP(n,N,sublen=sublen) W = np.fft.fft(np.eye(tot_len)) invW = np.fft.ifft(np.eye(2*sublen + 1)) A = invW @ np.fft.ifftshift(FA) @ W return A.real def _proj_sub_FourierSSP(n,N,sublen=3): # Return a matrix, \bar{B}_n # Consider the multi scale representation (S_{total}) and sub vectors (S_n) described in the paper # Then # \sum_n \bar{B}_n F{S_{n}} = F{S_{total}} # i.e. project the sub vector in the Fourier domain such that summing all such projections gives the full vector in Fourier domain tot_len = 2*sublen*N + 1 FB = np.zeros((2*sublen + 1, tot_len)) FB[0:sublen, sublen*n:sublen*(n+1)] = np.eye(sublen) FB[sublen, sublen*N] = 1/N # all sub vectors have a "1" zero freq term so scale it so full vector will have 1 FB[sublen+1:, tot_len - np.arange(sublen*(n+1),sublen*n,-1)] = np.eye(sublen) return FB.T def _proj_sub_SSP(n,N,sublen=3): # Return a matrix, B_n # Consider the multi scale representation (S_{total}) and sub vectors (S_n) described in the paper # Then # \sum_n B_n S_{n} = S_{total} # i.e. project the sub vector in the time domain such that summing all such projections gives the full vector tot_len = 2*sublen*N + 1 FB = _proj_sub_FourierSSP(n,N,sublen=sublen) invW = np.fft.ifft(np.eye(tot_len)) W = np.fft.fft(np.eye(2*sublen + 1)) B = invW @ np.fft.ifftshift(FB) @ W return B.real
semanticmapping/sspspace.py
15,554
problems duw to complex lograise NotImplementedError()fssp = np.fft.fft(ssp,axis=0)x = np.linalg.lstsq(np.tile(self.phase_matrix,(2,1)), np.hstack([np.arccos(fssp.real), np.arcsin(fssp.imag)])) end if make new if num_pts different than whats stored? Return a matrix, \bar{A}_n Consider the multi scale representation (S_{total}) and sub vectors (S_n) described in the paper Then \bar{A}_n F{S_{total}} = F{S_n} i.e. pick out the sub vector in the Fourier domain Return a matrix, A_n Consider the multi scale representation (S_{total}) and sub vectors (S_n) described in the paper Then A_n S_{total} = S_n i.e. pick out the sub vector in the time domain Return a matrix, \bar{B}_n Consider the multi scale representation (S_{total}) and sub vectors (S_n) described in the paper Then \sum_n \bar{B}_n F{S_{n}} = F{S_{total}} i.e. project the sub vector in the Fourier domain such that summing all such projections gives the full vector in Fourier domain all sub vectors have a "1" zero freq term so scale it so full vector will have 1 Return a matrix, B_n Consider the multi scale representation (S_{total}) and sub vectors (S_n) described in the paper Then \sum_n B_n S_{n} = S_{total} i.e. project the sub vector in the time domain such that summing all such projections gives the full vector
1,297
en
0.761714
# -*- coding: utf-8 -*- # Copyright 2020 Google LLC # # 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. # import warnings from typing import Callable, Dict, Optional, Sequence, Tuple, Union from google.api_core import grpc_helpers from google.api_core import gapic_v1 import google.auth # type: ignore from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore import grpc from google.cloud.datastore_v1.types import datastore from .base import DatastoreTransport, DEFAULT_CLIENT_INFO class DatastoreGrpcTransport(DatastoreTransport): """gRPC backend transport for Datastore. Each RPC normalizes the partition IDs of the keys in its input entities, and always returns entities with keys with normalized partition IDs. This applies to all keys and entities, including those in values, except keys with both an empty path and an empty or unset partition ID. Normalization of input keys sets the project ID (if not already set) to the project ID from the request. This class defines the same methods as the primary client, so the primary client can load the underlying transport implementation and call it. It sends protocol buffers over the wire using gRPC (which is built on top of HTTP/2); the ``grpcio`` package must be installed. """ _stubs: Dict[str, Callable] def __init__( self, *, host: str = "datastore.googleapis.com", credentials: ga_credentials.Credentials = None, credentials_file: str = None, scopes: Sequence[str] = None, channel: grpc.Channel = None, api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, ssl_channel_credentials: grpc.ChannelCredentials = None, client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None, quota_project_id: Optional[str] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, always_use_jwt_access: Optional[bool] = False, ) -> None: """Instantiate the transport. Args: host (Optional[str]): The hostname to connect to. credentials (Optional[google.auth.credentials.Credentials]): The authorization credentials to attach to requests. These credentials identify the application to the service; if none are specified, the client will attempt to ascertain the credentials from the environment. This argument is ignored if ``channel`` is provided. credentials_file (Optional[str]): A file with credentials that can be loaded with :func:`google.auth.load_credentials_from_file`. This argument is ignored if ``channel`` is provided. scopes (Optional(Sequence[str])): A list of scopes. This argument is ignored if ``channel`` is provided. channel (Optional[grpc.Channel]): A ``Channel`` instance through which to make calls. api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or application default SSL credentials. client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): Deprecated. A callback to provide client SSL certificate bytes and private key bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` is None. ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials for the grpc channel. It is ignored if ``channel`` is provided. client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): A callback to provide client certificate bytes and private key bytes, both in PEM format. It is used to configure a mutual TLS channel. It is ignored if ``channel`` or ``ssl_channel_credentials`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. client_info (google.api_core.gapic_v1.client_info.ClientInfo): The client info used to send a user-agent string along with API requests. If ``None``, then default info will be used. Generally, you only need to set this if you're developing your own client library. always_use_jwt_access (Optional[bool]): Whether self signed JWT should be used for service account credentials. Raises: google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport creation failed for any reason. google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ self._grpc_channel = None self._ssl_channel_credentials = ssl_channel_credentials self._stubs: Dict[str, Callable] = {} if api_mtls_endpoint: warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) if client_cert_source: warnings.warn("client_cert_source is deprecated", DeprecationWarning) if channel: # Ignore credentials if a channel was passed. credentials = False # If a channel was explicitly provided, set it. self._grpc_channel = channel self._ssl_channel_credentials = None else: if api_mtls_endpoint: host = api_mtls_endpoint # Create SSL credentials with client_cert_source or application # default SSL credentials. if client_cert_source: cert, key = client_cert_source() self._ssl_channel_credentials = grpc.ssl_channel_credentials( certificate_chain=cert, private_key=key ) else: self._ssl_channel_credentials = SslCredentials().ssl_credentials else: if client_cert_source_for_mtls and not ssl_channel_credentials: cert, key = client_cert_source_for_mtls() self._ssl_channel_credentials = grpc.ssl_channel_credentials( certificate_chain=cert, private_key=key ) # The base transport sets the host, credentials and scopes super().__init__( host=host, credentials=credentials, credentials_file=credentials_file, scopes=scopes, quota_project_id=quota_project_id, client_info=client_info, always_use_jwt_access=always_use_jwt_access, ) if not self._grpc_channel: self._grpc_channel = type(self).create_channel( self._host, credentials=self._credentials, credentials_file=credentials_file, scopes=self._scopes, ssl_credentials=self._ssl_channel_credentials, quota_project_id=quota_project_id, options=[ ("grpc.max_send_message_length", -1), ("grpc.max_receive_message_length", -1), ], ) # Wrap messages. This must be done after self._grpc_channel exists self._prep_wrapped_messages(client_info) @classmethod def create_channel( cls, host: str = "datastore.googleapis.com", credentials: ga_credentials.Credentials = None, credentials_file: str = None, scopes: Optional[Sequence[str]] = None, quota_project_id: Optional[str] = None, **kwargs, ) -> grpc.Channel: """Create and return a gRPC channel object. Args: host (Optional[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If none are specified, the client will attempt to ascertain the credentials from the environment. credentials_file (Optional[str]): A file with credentials that can be loaded with :func:`google.auth.load_credentials_from_file`. This argument is mutually exclusive with credentials. scopes (Optional[Sequence[str]]): A optional list of scopes needed for this service. These are only used when credentials are not specified and are passed to :func:`google.auth.default`. quota_project_id (Optional[str]): An optional project to use for billing and quota. kwargs (Optional[dict]): Keyword arguments, which are passed to the channel creation. Returns: grpc.Channel: A gRPC channel object. Raises: google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ return grpc_helpers.create_channel( host, credentials=credentials, credentials_file=credentials_file, quota_project_id=quota_project_id, default_scopes=cls.AUTH_SCOPES, scopes=scopes, default_host=cls.DEFAULT_HOST, **kwargs, ) @property def grpc_channel(self) -> grpc.Channel: """Return the channel designed to connect to this service. """ return self._grpc_channel @property def lookup(self) -> Callable[[datastore.LookupRequest], datastore.LookupResponse]: r"""Return a callable for the lookup method over gRPC. Looks up entities by key. Returns: Callable[[~.LookupRequest], ~.LookupResponse]: A function that, when called, will call the underlying RPC on the server. """ # Generate a "stub function" on-the-fly which will actually make # the request. # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "lookup" not in self._stubs: self._stubs["lookup"] = self.grpc_channel.unary_unary( "/google.datastore.v1.Datastore/Lookup", request_serializer=datastore.LookupRequest.serialize, response_deserializer=datastore.LookupResponse.deserialize, ) return self._stubs["lookup"] @property def run_query( self, ) -> Callable[[datastore.RunQueryRequest], datastore.RunQueryResponse]: r"""Return a callable for the run query method over gRPC. Queries for entities. Returns: Callable[[~.RunQueryRequest], ~.RunQueryResponse]: A function that, when called, will call the underlying RPC on the server. """ # Generate a "stub function" on-the-fly which will actually make # the request. # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "run_query" not in self._stubs: self._stubs["run_query"] = self.grpc_channel.unary_unary( "/google.datastore.v1.Datastore/RunQuery", request_serializer=datastore.RunQueryRequest.serialize, response_deserializer=datastore.RunQueryResponse.deserialize, ) return self._stubs["run_query"] @property def begin_transaction( self, ) -> Callable[ [datastore.BeginTransactionRequest], datastore.BeginTransactionResponse ]: r"""Return a callable for the begin transaction method over gRPC. Begins a new transaction. Returns: Callable[[~.BeginTransactionRequest], ~.BeginTransactionResponse]: A function that, when called, will call the underlying RPC on the server. """ # Generate a "stub function" on-the-fly which will actually make # the request. # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "begin_transaction" not in self._stubs: self._stubs["begin_transaction"] = self.grpc_channel.unary_unary( "/google.datastore.v1.Datastore/BeginTransaction", request_serializer=datastore.BeginTransactionRequest.serialize, response_deserializer=datastore.BeginTransactionResponse.deserialize, ) return self._stubs["begin_transaction"] @property def commit(self) -> Callable[[datastore.CommitRequest], datastore.CommitResponse]: r"""Return a callable for the commit method over gRPC. Commits a transaction, optionally creating, deleting or modifying some entities. Returns: Callable[[~.CommitRequest], ~.CommitResponse]: A function that, when called, will call the underlying RPC on the server. """ # Generate a "stub function" on-the-fly which will actually make # the request. # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "commit" not in self._stubs: self._stubs["commit"] = self.grpc_channel.unary_unary( "/google.datastore.v1.Datastore/Commit", request_serializer=datastore.CommitRequest.serialize, response_deserializer=datastore.CommitResponse.deserialize, ) return self._stubs["commit"] @property def rollback( self, ) -> Callable[[datastore.RollbackRequest], datastore.RollbackResponse]: r"""Return a callable for the rollback method over gRPC. Rolls back a transaction. Returns: Callable[[~.RollbackRequest], ~.RollbackResponse]: A function that, when called, will call the underlying RPC on the server. """ # Generate a "stub function" on-the-fly which will actually make # the request. # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "rollback" not in self._stubs: self._stubs["rollback"] = self.grpc_channel.unary_unary( "/google.datastore.v1.Datastore/Rollback", request_serializer=datastore.RollbackRequest.serialize, response_deserializer=datastore.RollbackResponse.deserialize, ) return self._stubs["rollback"] @property def allocate_ids( self, ) -> Callable[[datastore.AllocateIdsRequest], datastore.AllocateIdsResponse]: r"""Return a callable for the allocate ids method over gRPC. Allocates IDs for the given keys, which is useful for referencing an entity before it is inserted. Returns: Callable[[~.AllocateIdsRequest], ~.AllocateIdsResponse]: A function that, when called, will call the underlying RPC on the server. """ # Generate a "stub function" on-the-fly which will actually make # the request. # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "allocate_ids" not in self._stubs: self._stubs["allocate_ids"] = self.grpc_channel.unary_unary( "/google.datastore.v1.Datastore/AllocateIds", request_serializer=datastore.AllocateIdsRequest.serialize, response_deserializer=datastore.AllocateIdsResponse.deserialize, ) return self._stubs["allocate_ids"] @property def reserve_ids( self, ) -> Callable[[datastore.ReserveIdsRequest], datastore.ReserveIdsResponse]: r"""Return a callable for the reserve ids method over gRPC. Prevents the supplied keys' IDs from being auto- llocated by Cloud Datastore. Returns: Callable[[~.ReserveIdsRequest], ~.ReserveIdsResponse]: A function that, when called, will call the underlying RPC on the server. """ # Generate a "stub function" on-the-fly which will actually make # the request. # gRPC handles serialization and deserialization, so we just need # to pass in the functions for each. if "reserve_ids" not in self._stubs: self._stubs["reserve_ids"] = self.grpc_channel.unary_unary( "/google.datastore.v1.Datastore/ReserveIds", request_serializer=datastore.ReserveIdsRequest.serialize, response_deserializer=datastore.ReserveIdsResponse.deserialize, ) return self._stubs["reserve_ids"] def close(self): self.grpc_channel.close() __all__ = ("DatastoreGrpcTransport",)
google/cloud/datastore_v1/services/datastore/transports/grpc.py
18,062
gRPC backend transport for Datastore. Each RPC normalizes the partition IDs of the keys in its input entities, and always returns entities with keys with normalized partition IDs. This applies to all keys and entities, including those in values, except keys with both an empty path and an empty or unset partition ID. Normalization of input keys sets the project ID (if not already set) to the project ID from the request. This class defines the same methods as the primary client, so the primary client can load the underlying transport implementation and call it. It sends protocol buffers over the wire using gRPC (which is built on top of HTTP/2); the ``grpcio`` package must be installed. Instantiate the transport. Args: host (Optional[str]): The hostname to connect to. credentials (Optional[google.auth.credentials.Credentials]): The authorization credentials to attach to requests. These credentials identify the application to the service; if none are specified, the client will attempt to ascertain the credentials from the environment. This argument is ignored if ``channel`` is provided. credentials_file (Optional[str]): A file with credentials that can be loaded with :func:`google.auth.load_credentials_from_file`. This argument is ignored if ``channel`` is provided. scopes (Optional(Sequence[str])): A list of scopes. This argument is ignored if ``channel`` is provided. channel (Optional[grpc.Channel]): A ``Channel`` instance through which to make calls. api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or application default SSL credentials. client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): Deprecated. A callback to provide client SSL certificate bytes and private key bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` is None. ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials for the grpc channel. It is ignored if ``channel`` is provided. client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): A callback to provide client certificate bytes and private key bytes, both in PEM format. It is used to configure a mutual TLS channel. It is ignored if ``channel`` or ``ssl_channel_credentials`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. client_info (google.api_core.gapic_v1.client_info.ClientInfo): The client info used to send a user-agent string along with API requests. If ``None``, then default info will be used. Generally, you only need to set this if you're developing your own client library. always_use_jwt_access (Optional[bool]): Whether self signed JWT should be used for service account credentials. Raises: google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport creation failed for any reason. google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. Return a callable for the allocate ids method over gRPC. Allocates IDs for the given keys, which is useful for referencing an entity before it is inserted. Returns: Callable[[~.AllocateIdsRequest], ~.AllocateIdsResponse]: A function that, when called, will call the underlying RPC on the server. Return a callable for the begin transaction method over gRPC. Begins a new transaction. Returns: Callable[[~.BeginTransactionRequest], ~.BeginTransactionResponse]: A function that, when called, will call the underlying RPC on the server. Return a callable for the commit method over gRPC. Commits a transaction, optionally creating, deleting or modifying some entities. Returns: Callable[[~.CommitRequest], ~.CommitResponse]: A function that, when called, will call the underlying RPC on the server. Create and return a gRPC channel object. Args: host (Optional[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If none are specified, the client will attempt to ascertain the credentials from the environment. credentials_file (Optional[str]): A file with credentials that can be loaded with :func:`google.auth.load_credentials_from_file`. This argument is mutually exclusive with credentials. scopes (Optional[Sequence[str]]): A optional list of scopes needed for this service. These are only used when credentials are not specified and are passed to :func:`google.auth.default`. quota_project_id (Optional[str]): An optional project to use for billing and quota. kwargs (Optional[dict]): Keyword arguments, which are passed to the channel creation. Returns: grpc.Channel: A gRPC channel object. Raises: google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. Return the channel designed to connect to this service. Return a callable for the lookup method over gRPC. Looks up entities by key. Returns: Callable[[~.LookupRequest], ~.LookupResponse]: A function that, when called, will call the underlying RPC on the server. Return a callable for the reserve ids method over gRPC. Prevents the supplied keys' IDs from being auto- llocated by Cloud Datastore. Returns: Callable[[~.ReserveIdsRequest], ~.ReserveIdsResponse]: A function that, when called, will call the underlying RPC on the server. Return a callable for the rollback method over gRPC. Rolls back a transaction. Returns: Callable[[~.RollbackRequest], ~.RollbackResponse]: A function that, when called, will call the underlying RPC on the server. Return a callable for the run query method over gRPC. Queries for entities. Returns: Callable[[~.RunQueryRequest], ~.RunQueryResponse]: A function that, when called, will call the underlying RPC on the server. -*- coding: utf-8 -*- Copyright 2020 Google LLC 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. type: ignore type: ignore type: ignore Ignore credentials if a channel was passed. If a channel was explicitly provided, set it. Create SSL credentials with client_cert_source or application default SSL credentials. The base transport sets the host, credentials and scopes Wrap messages. This must be done after self._grpc_channel exists Generate a "stub function" on-the-fly which will actually make the request. gRPC handles serialization and deserialization, so we just need to pass in the functions for each. Generate a "stub function" on-the-fly which will actually make the request. gRPC handles serialization and deserialization, so we just need to pass in the functions for each. Generate a "stub function" on-the-fly which will actually make the request. gRPC handles serialization and deserialization, so we just need to pass in the functions for each. Generate a "stub function" on-the-fly which will actually make the request. gRPC handles serialization and deserialization, so we just need to pass in the functions for each. Generate a "stub function" on-the-fly which will actually make the request. gRPC handles serialization and deserialization, so we just need to pass in the functions for each. Generate a "stub function" on-the-fly which will actually make the request. gRPC handles serialization and deserialization, so we just need to pass in the functions for each. Generate a "stub function" on-the-fly which will actually make the request. gRPC handles serialization and deserialization, so we just need to pass in the functions for each.
8,643
en
0.811788
from src.utils.config import config import json # import uuid import requests _NAMESPACE = "WS" _VER_NAMESPACE = "WSVER" _SAMPLE_NAMESPACE = "SMP" # versioned and non-versioned index have same version _SAMPLE_SET_INDEX_VERSION = 1 _SAMPLE_SET_INDEX_NAME = 'sample_set_' + str(_SAMPLE_SET_INDEX_VERSION) _VER_SAMPLE_SET_INDEX_NAME = 'sample_set_version_' + str(_SAMPLE_SET_INDEX_VERSION) # versioned and non-versioned index have same version _SAMPLE_INDEX_VERSION = 1 _SAMPLE_INDEX_NAME = 'sample_' + str(_SAMPLE_INDEX_VERSION) # _VER_SAMPLE_INDEX_NAME = 'sample_version_' + str(_SAMPLE_INDEX_VERSION) def _get_sample(sample_info): """ Get sample from SampleService sample_info - dict containing 'id' and 'version' of a sample """ headers = {"Authorization": config()['ws_token']} params = { "id": sample_info['id'] } if sample_info.get('version'): params['version'] = sample_info['version'] payload = { "method": "SampleService.get_sample", "id": "", # str(uuid.uuid4()), "params": [params], "version": "1.1" } resp = requests.post(url=config()['sample_service_url'], headers=headers, data=json.dumps(payload)) if not resp.ok: raise RuntimeError(f"Returned from sample service with status {resp.status_code} - {resp.text}") resp_json = resp.json() if resp_json.get('error'): raise RuntimeError(f"Error from SampleService - {resp_json['error']}") sample = resp_json['result'][0] return sample def _flatten_meta(meta, prefix=None): """ Flattens metadata fields in a Sample object. Fields are concatenated into a single string field to save into an Elasticsearch index meta - Sample Metadata to be flattened prefix - (optional) prefix for the metadata values. default=None """ new_meta = {} for key in meta: if prefix: val = prefix + ":" else: val = "" if "value" in meta[key]: val += str(meta[key]['value']) if "units" in meta[key]: val += ";" + str(meta[key]['units']) new_meta[key] = val return new_meta def _combine_meta(meta, flattened_meta, idx): """ Combine newly flattened metadata with existing metadata. This Function is designed to keep the indexing of the different metadata fields consistent for each node within the sample node tree s.t. all the fields in index (idx) 0 will be from item 0 in the node tree. Empty string ("") entries are Empty and added simply so that the indexing of all fields line up. meta - existing metadata. flattened_meta - newly flattened metadata. idx - current index of ndoe_tree. """ for key in flattened_meta: if key in meta: meta[key] += ["" for _ in range(idx - len(meta[key]))] + [flattened_meta[key]] else: meta[key] = ["" for _ in range(idx)] + [flattened_meta[key]] return meta def index_sample_set(obj_data, ws_info, obj_data_v1): """Indexer for KBaseSets.SampleSet object type""" info = obj_data['info'] if not obj_data.get('data'): raise Exception("no data in object") data = obj_data['data'] workspace_id = info[6] object_id = info[0] version = info[4] sample_set_id = f"{_NAMESPACE}::{workspace_id}:{object_id}" ver_sample_set_id = f"{_VER_NAMESPACE}::{workspace_id}:{object_id}:{version}" sample_set_index = { "_action": "index", "doc": { "description": data["description"], "sample_ids": [s['id'] for s in data['samples']], "sample_names": [s['name'] for s in data['samples']], "sample_versions": [s['version'] for s in data['samples']] }, "index": _SAMPLE_SET_INDEX_NAME, "id": sample_set_id } yield sample_set_index ver_sample_set_index = dict(sample_set_index) ver_sample_set_index['index'] = _VER_SAMPLE_SET_INDEX_NAME ver_sample_set_index['id'] = ver_sample_set_id yield ver_sample_set_index for samp in data["samples"]: # query the sample service for sample sample = _get_sample(samp) sample_id = f"{_SAMPLE_NAMESPACE}::{sample['id']}:{sample['version']}" # not sure on how we need to handle more than 1 node. if len(sample['node_tree']) == 1: meta_controlled = _flatten_meta( sample['node_tree'][0]['meta_controlled'] ) meta_user = _flatten_meta( sample['node_tree'][0]['meta_user'] ) meta_controlled['node_id'] = sample['node_tree'][0]['id'] else: meta_controlled, meta_user = {}, {} for idx, node in enumerate(sample['node_tree']): meta_controlled = _combine_meta( meta_controlled, _flatten_meta( node['meta_controlled'] ), idx ) meta_user = _combine_meta( meta_user, _flatten_meta( node['meta_user'] ), idx ) meta_controlled['node_id'] = node['id'] sample_index = { "_action": "index", "doc": { "save_date": sample['save_date'], "sample_version": sample['version'], "name": sample['name'], "parent_id": sample_set_id, **meta_user, **meta_controlled }, "index": _SAMPLE_INDEX_NAME, "id": sample_id } yield sample_index
src/index_runner/es_indexers/sample_set.py
5,754
Combine newly flattened metadata with existing metadata. This Function is designed to keep the indexing of the different metadata fields consistent for each node within the sample node tree s.t. all the fields in index (idx) 0 will be from item 0 in the node tree. Empty string ("") entries are Empty and added simply so that the indexing of all fields line up. meta - existing metadata. flattened_meta - newly flattened metadata. idx - current index of ndoe_tree. Flattens metadata fields in a Sample object. Fields are concatenated into a single string field to save into an Elasticsearch index meta - Sample Metadata to be flattened prefix - (optional) prefix for the metadata values. default=None Get sample from SampleService sample_info - dict containing 'id' and 'version' of a sample Indexer for KBaseSets.SampleSet object type import uuid versioned and non-versioned index have same version versioned and non-versioned index have same version _VER_SAMPLE_INDEX_NAME = 'sample_version_' + str(_SAMPLE_INDEX_VERSION) str(uuid.uuid4()), query the sample service for sample not sure on how we need to handle more than 1 node.
1,171
en
0.733669
from alipay import AliPay from django.core.paginator import Paginator from django.http import HttpResponseRedirect from django.http import JsonResponse from django.shortcuts import render from django.utils.http import urlquote from Qshop.settings import alipay_private_key_string, alipay_public_key_string from Seller.views import getPassword # Create your views here. def loginValid(func): """ :desc 闭包函数校验是否登录 :param func: :return: """ def inner(request, *args, **kwargs): email = request.COOKIES.get("user") s_email = request.session.get("user") if email and s_email and email == s_email: user = LoginUser.objects.filter(email=email).first() if user: return func(request, *args, **kwargs) return HttpResponseRedirect("/Buyer/login/") return inner def login(request): if request.method == "POST": erremail = "" email = request.POST.get("email") pwd = request.POST.get("pwd") user = LoginUser.objects.filter(email=email).first() if user: db_password = user.password pwd = getPassword(pwd) if db_password == pwd: response = HttpResponseRedirect("/Buyer/index/", locals()) response.set_cookie("user", user.email) response.set_cookie("user_id", user.id) response.set_cookie("username", urlquote(user.username)) request.session["user"] = user.email return response else: errpwd = "密码不匹配" else: erremail = "该邮箱未注册" return render(request, "buyer/login.html", locals()) def register(request): errmsg = "" if request.method == "POST": username = request.POST.get("user_name") pwd = request.POST.get("pwd") email = request.POST.get("email") db_email = LoginUser.objects.filter(email=email).first() db_username = LoginUser.objects.filter(username=username).first() if not db_email: if not db_username: user = LoginUser() user.username = username user.password = getPassword(pwd) user.email = email user.save() return HttpResponseRedirect("/Buyer/login/", locals()) else: errmsg = "用户名已存在" else: errmsg = "邮箱已注册" return render(request, "buyer/register.html", {"errmsg": errmsg}) def index(request): types = GoodsType.objects.all() goods_result = [] for type in types: goods = type.goods_set.order_by("goods_pro_date")[0:4] if len(goods) >= 4: goods_result.append({type: goods}) return render(request, "buyer/index.html", locals()) def logout(request): url = request.META.get("HTTP_REFERER", "/Buyer/index/") response = HttpResponseRedirect(url) cookies = request.COOKIES.keys() for cookie in cookies: response.delete_cookie(cookie) if request.session.get("user"): del request.session['user'] return response @loginValid def user_info(request): id = request.COOKIES.get("user_id") if id: user = LoginUser.objects.filter(id=id).first() return render(request, "buyer/user_center_info.html", locals()) @loginValid def user_site(request): id = request.COOKIES.get("user_id") if id: user = LoginUser.objects.filter(id=id).first() return render(request, "buyer/user_center_site.html", locals()) @loginValid def user_order(request): id = request.COOKIES.get("user_id") if id: user = LoginUser.objects.filter(id=id).first() order_lists = PayOrder.objects.filter(order_user=user).order_by("-order_date") return render(request, "buyer/user_center_order.html", locals()) """ def good_list(request): type = request.GET.get("type") keyword = request.GET.get("keyword") goods_list = [] if type == 'byid': if keyword: types = GoodsType.objects.get(id=keyword) goods_list = types.goods_set.order_by("goods_pro_date") elif type == 'bykey': if keyword: goods_list = Goods.objects.filter(goods_name__contains=keyword).order_by("goods_pro_date") if goods_list: nums = goods_list.count() nums = int(math.ceil(nums / 5)) recommon_list = goods_list[:nums] return render(request, "buyer/goods_list.html", locals()) """ def good_list(request, page): page = int(page) type = request.GET.get("type") keyword = request.GET.get("keyword") goods_list = [] if type == 'byid': # 按照商品id查 if keyword: types = GoodsType.objects.get(id=int(keyword)) goods_list = types.goods_set.order_by("goods_pro_date") elif type == 'bykey': # 按商品名字查 if keyword: goods_list = Goods.objects.filter(goods_name__contains=keyword).order_by("goods_pro_date") if goods_list: # 分页 page_list = Paginator(goods_list, 15) goods_list = page_list.page(page) pages = page_list.page_range # 推荐商品 nums = len(goods_list) nums = int(math.ceil(nums / 5)) recommon_list = goods_list[:nums] return render(request, "buyer/goods_list.html", locals()) def good_detail(request, id): good = Goods.objects.filter(id=int(id)).first() return render(request, "buyer/detail.html", locals()) import math import time import datetime from Buyer.models import * @loginValid def pay_order(request): """ get请求 商品详情页购买单个商品。传入商品id,数量。 post请求 购物车购买多个商品。 """ if request.method == "GET": num = request.GET.get("num") id = request.GET.get("id") if num and id: num = int(num) id = int(id) order = PayOrder() # 订单 order.order_number = str(time.time()).replace(".", "") order.order_date = datetime.datetime.now() order.order_user = LoginUser.objects.get(id=int(request.COOKIES.get("user_id"))) order.save() good = Goods.objects.get(id=id) order_info = OrderInfo() # 订单详情 order_info.order_id = order order_info.goods_id = good.id order_info.goods_picture = good.goods_picture order_info.goods_name = good.goods_name order_info.goods_count = num order_info.goods_price = good.goods_price order_info.goods_total_price = round(good.goods_price * num, 3) order_info.store_id = good.goods_store order_info.order_status = 0 # 状态 order_info.save() order.order_total = order_info.goods_total_price order.save() elif request.method == "POST": request_data = [] data = request.POST data_item = request.POST.items() for key, value in data_item: if key.startswith("check_"): id = int(key.split("_", 1)[1]) num = int(data.get("count_" + str(id))) request_data.append((id, num)) if request_data: order = PayOrder() # 创建订单 order.order_number = str(time.time()).replace(".", "") order.order_date = datetime.datetime.now() order.order_user = LoginUser.objects.get(id=int(request.COOKIES.get("user_id"))) order.order_total = 0.0 order.goods_number = 0 order.save() for id, num in request_data: good = Goods.objects.get(id=id) order_info = OrderInfo() # 订单详情 order_info.order_id = order order_info.goods_id = good.id order_info.goods_picture = good.goods_picture order_info.goods_name = good.goods_name order_info.goods_count = num order_info.goods_price = good.goods_price order_info.goods_total_price = round(good.goods_price * num, 3) order_info.store_id = good.goods_store order_info.order_status = 0 order_info.save() order.order_total += order_info.goods_total_price # 订单总价 order.goods_number += 1 # 商品种类个数 order.save() return render(request, "buyer/place_order.html", locals()) @loginValid def alipayOrder(request): """ 阿里支付,传入交易订单号,总金额 """ order_number = request.GET.get("order_number") total = request.GET.get("total") # 实例化支付 alipay = AliPay( appid="2016101200667714", app_notify_url=None, app_private_key_string=alipay_private_key_string, alipay_public_key_string=alipay_public_key_string, sign_type="RSA2" ) order_string = alipay.api_alipay_trade_page_pay( out_trade_no=order_number, # 订单编号 total_amount=str(total), # 金额 字符串类型 subject="生鲜交易", return_url="http://127.0.0.1:8000/Buyer/pay_result/", # 支付跳转页面 notify_url="http://127.0.0.1:8000/Buyer/pay_result/", ) result = "https://openapi.alipaydev.com/gateway.do?" + order_string return HttpResponseRedirect(result) @loginValid def pay_result(request): """ 支付结果页 如果有out_trade_no,支付成功,修改订单状态 """ out_trade_no = request.GET.get("out_trade_no") if out_trade_no: payorder = PayOrder.objects.get(order_number=out_trade_no) payorder.orderinfo_set.all().update(order_status=1) return render(request, "buyer/pay_result.html", locals()) @loginValid def delgood(request): sendData = { "code": 200, "data": "" } id = request.GET.get("id") if id: cart = Cart.objects.get(id=id) cart.delete() sendData["data"] = "删除编号%s成功"%id return JsonResponse(sendData) @loginValid def add_cart(request): """ 处理ajax 请求,添加商品到购物车 ,成功保存到数据库。 传入商品id,数量 """ sendData = { "code": 200, "data": "" } if request.method == "POST": id = int(request.POST.get("goods_id")) count = int(request.POST.get("count", 1)) goods = Goods.objects.get(id=id) cart = Cart() cart.goods_name = goods.goods_name cart.goods_num = count cart.goods_price = goods.goods_price cart.goods_picture = goods.goods_picture cart.goods_total = round(goods.goods_price * count, 3) cart.goods_id = goods.id cart.cart_user = request.COOKIES.get("user_id") cart.save() sendData['data'] = "加入购物车成功" else: sendData["code"] = 500 sendData["data"] = "请求方式错误" return JsonResponse(sendData) @loginValid def mycart(request): id = request.COOKIES.get("user_id") carts = Cart.objects.filter(cart_user=id).order_by("-id") number = carts.count() return render(request, "buyer/cart.html", locals())
Qshop/Buyer/views.py
11,385
处理ajax 请求,添加商品到购物车 ,成功保存到数据库。 传入商品id,数量 阿里支付,传入交易订单号,总金额 :desc 闭包函数校验是否登录 :param func: :return: get请求 商品详情页购买单个商品。传入商品id,数量。 post请求 购物车购买多个商品。 支付结果页 如果有out_trade_no,支付成功,修改订单状态 Create your views here. 按照商品id查 按商品名字查 分页 推荐商品 订单 订单详情 状态 创建订单 订单详情 订单总价 商品种类个数 实例化支付 订单编号 金额 字符串类型 支付跳转页面
285
zh
0.961396
#Import Library import warnings import numpy as np import datetime from extract_data import * from word_encoder import * from sklearn import svm from sklearn.ensemble import RandomForestClassifier from sklearn.naive_bayes import GaussianNB from sklearn import tree # send the extracted data availble from extract_data to the encode function # this function vectorizes the text based data into ASCII format for use by # the algorithms encoded_data = encode(data) scores = [] # convert the float scores to int. Multiplying by 10 helps us keep the decimal # level precision which would otherwise be lost in typecasting i = 0 while i < len(label): scores.append(int (float(label[i]) * 10)) i += 1; # ignore depricated warning def warn(*args, **kwargs): pass warnings.warn = warn # SVM classifier svm_clf = svm.SVC(kernel = 'linear') #svm_clf.fit(encoded_data, scores) # Gaussian Naive Bayes gnb_clf = GaussianNB() gnb_clf.fit(encoded_data, scores) # Random Forest rf_clf = RandomForestClassifier(n_estimators=10) rf_clf.fit(encoded_data, scores) # Decision Tree dt_clf = tree.DecisionTreeClassifier() dt_clf.fit(encoded_data, scores) #print("SVM:") #print(svm_clf.predict ([1403, 2752, 3263, 4200, 4309, 4417, 4518, 4675, 5909, 6102, 6500, 8459, 8672, 8882, 9712, 9810, 10524, 10757, 11096, 11299, 11461, 11617, 11775])) print("Gaussian Naive Bayes:") print(gnb_clf.predict ([1403, 2752, 3263, 4200, 4309, 4417, 4518, 4675, 5909, 6102, 6500, 8459, 8672, 8882, 9712, 9810, 10524, 10757, 11096, 11299, 11461, 11617, 11775])) print("Random Forest:") print(rf_clf.predict ([1403, 2752, 3263, 4200, 4309, 4417, 4518, 4675, 5909, 6102, 6500, 8459, 8672, 8882, 9712, 9810, 10524, 10757, 11096, 11299, 11461, 11617, 11775])) print("Decision Tree:") print(dt_clf.predict ([1403, 2752, 3263, 4200, 4309, 4417, 4518, 4675, 5909, 6102, 6500, 8459, 8672, 8882, 9712, 9810, 10524, 10757, 11096, 11299, 11461, 11617, 11775])) print("End time: " + str(datetime.datetime.now()).split('.')[0])
Project/algorithm.old.py
2,002
Import Library send the extracted data availble from extract_data to the encode function this function vectorizes the text based data into ASCII format for use by the algorithms convert the float scores to int. Multiplying by 10 helps us keep the decimal level precision which would otherwise be lost in typecasting ignore depricated warning SVM classifiersvm_clf.fit(encoded_data, scores) Gaussian Naive Bayes Random Forest Decision Treeprint("SVM:")print(svm_clf.predict ([1403, 2752, 3263, 4200, 4309, 4417, 4518, 4675, 5909, 6102, 6500, 8459, 8672, 8882, 9712, 9810, 10524, 10757, 11096, 11299, 11461, 11617, 11775]))
621
en
0.625865
from __future__ import division import databench import math import random class Dummypi(databench.Analysis): """A dummy analysis.""" @databench.on def connected(self): yield self.data.init({'samples': 100000}) @databench.on def run(self): """Run when button is pressed.""" inside = 0 for draws in range(1, self.data['samples']): # generate points and check whether they are inside the unit circle r1 = random.random() r2 = random.random() if r1 ** 2 + r2 ** 2 < 1.0: inside += 1 # every 1000 iterations, update status if draws % 1000 != 0: continue # debug yield self.emit('log', {'draws': draws, 'inside': inside}) # calculate pi and its uncertainty given the current draws p = inside / draws pi = { 'estimate': 4.0 * p, 'uncertainty': 4.0 * math.sqrt(draws * p * (1.0 - p)) / draws, } # send status to frontend yield self.set_state(pi=pi) yield self.emit('log', {'action': 'done'}) @databench.on def samples(self, value): yield self.set_state(samples=value)
databench/analyses_packaged/dummypi/analysis.py
1,281
A dummy analysis. Run when button is pressed. generate points and check whether they are inside the unit circle every 1000 iterations, update status debug calculate pi and its uncertainty given the current draws send status to frontend
237
en
0.779092
# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. """ This script will be executed whenever a change is pushed to the master branch. It will schedule multiple child tasks that build the app, run tests and execute code quality tools: """ import datetime import json import taskcluster import os from lib.tasks import schedule_task TASK_ID = os.environ.get('TASK_ID') REPO_URL = os.environ.get('MOBILE_HEAD_REPOSITORY') BRANCH = os.environ.get('MOBILE_HEAD_BRANCH') COMMIT = os.environ.get('MOBILE_HEAD_REV') OWNER = "skaspari@mozilla.com" SOURCE = "https://github.com/mozilla-mobile/focus-android/tree/master/tools/taskcluster" def generate_build_task(): return taskcluster.slugId(), generate_task( name="(Focus for Android) Build", description="Build Focus/Klar for Android from source code.", command=('echo "--" > .adjust_token' ' && python tools/l10n/check_translations.py' ' && ./gradlew --no-daemon clean assemble')) def generate_unit_test_task(buildTaskId): return taskcluster.slugId(), generate_task( name="(Focus for Android) Unit tests", description="Run unit tests for Focus/Klar for Android.", command='echo "--" > .adjust_token && ./gradlew --no-daemon clean test', dependencies=[buildTaskId]) def generate_code_quality_task(buildTaskId): return taskcluster.slugId(), generate_task( name="(Focus for Android) Code quality", description="Run code quality tools on Focus/Klar for Android code base.", command='echo "--" > .adjust_token && ./gradlew --no-daemon clean detektCheck ktlint lint pmd checkstyle spotbugs', dependencies=[buildTaskId]) def generate_compare_locales_task(): return taskcluster.slugId(), generate_task( name="(Focus for Android) String validation", description="Check Focus/Klar for Android for errors in en-US and l10n.", command=('pip install "compare-locales>=5.0.2,<6.0"' ' && mkdir -p /opt/focus-android/test_artifacts' ' && compare-locales --validate l10n.toml .' ' && compare-locales --json=/opt/focus-android/test_artifacts/data.json l10n.toml .'), artifacts={ "public": { "type": "directory", "path": "/opt/focus-android/test_artifacts", "expires": taskcluster.stringDate(taskcluster.fromNow('1 week')) } }) def generate_ui_test_task(dependencies, engine="Klar", device="ARM"): ''' :param str engine: Klar, Webview :param str device: ARM, X86 :return: uiWebviewARMTestTaskId, uiWebviewARMTestTask ''' if engine is "Klar": engine = "geckoview" assemble_engine = engine elif engine is "Webview": engine = "webview" assemble_engine = "Focus" else: raise Exception("ERROR: unknown engine type --> Aborting!") task_name = "(Focus for Android) UI tests - {0} {1}".format(engine, device) task_description = "Run UI tests for {0} {1} build for Android.".format(engine, device) build_dir = "assemble{0}{1}Debug".format(assemble_engine, device.capitalize()) build_dir_test = "assemble{0}{1}DebugAndroidTest".format(assemble_engine, device.capitalize()) print('BUILD_DIR: {0}'.format(build_dir)) print('BUILD_DIR_TEST: {0}'.format(build_dir_test)) device = device.lower() return taskcluster.slugId(), generate_task( name=task_name, description=task_description, command=('echo "--" > .adjust_token' ' && ./gradlew --no-daemon clean ' + build_dir + ' ' + build_dir_test + ' ' ' && ./tools/taskcluster/google-firebase-testlab-login.sh' ' && tools/taskcluster/execute-firebase-tests.sh ' + device + ' ' + engine), dependencies=dependencies, scopes=['secrets:get:project/focus/firebase'], routes=['notify.irc-channel.#android-ci.on-any'], artifacts={ "public": { "type": "directory", "path": "/opt/focus-android/test_artifacts", "expires": taskcluster.stringDate(taskcluster.fromNow('1 week')) } }) # For GeckoView, upload nightly (it has release config) by default, all Release builds have WV def upload_apk_nimbledroid_task(dependencies): return taskcluster.slugId(), generate_task( name="(Focus for Android) Upload Debug APK to Nimbledroid", description="Upload APKs to Nimbledroid for performance measurement and tracking.", command=('echo "--" > .adjust_token' ' && ./gradlew --no-daemon clean assembleKlarArmNightly' ' && python tools/taskcluster/upload_apk_nimbledroid.py'), dependencies=dependencies, scopes=['secrets:get:project/focus/nimbledroid'], ) def generate_task(name, description, command, dependencies=[], artifacts={}, scopes=[], routes=[]): created = datetime.datetime.now() expires = taskcluster.fromNow('1 month') deadline = taskcluster.fromNow('1 day') return { "workerType": "github-worker", "taskGroupId": TASK_ID, "expires": taskcluster.stringDate(expires), "retries": 5, "created": taskcluster.stringDate(created), "tags": {}, "priority": "lowest", "schedulerId": "taskcluster-github", "deadline": taskcluster.stringDate(deadline), "dependencies": [TASK_ID] + dependencies, "routes": routes, "scopes": scopes, "requires": "all-completed", "payload": { "features": { "taskclusterProxy": True }, "maxRunTime": 7200, "image": "mozillamobile/focus-android:1.4", "command": [ "/bin/bash", "--login", "-c", "git fetch %s %s && git config advice.detachedHead false && git checkout %s && %s" % (REPO_URL, BRANCH, COMMIT, command) ], "artifacts": artifacts, "deadline": taskcluster.stringDate(deadline) }, "provisionerId": "aws-provisioner-v1", "metadata": { "name": name, "description": description, "owner": OWNER, "source": SOURCE } } if __name__ == "__main__": queue = taskcluster.Queue({'baseUrl': 'http://taskcluster/queue/v1'}) buildTaskId, buildTask = generate_build_task() schedule_task(queue, buildTaskId, buildTask) unitTestTaskId, unitTestTask = generate_unit_test_task(buildTaskId) schedule_task(queue, unitTestTaskId, unitTestTask) codeQualityTaskId, codeQualityTask = generate_code_quality_task(buildTaskId) schedule_task(queue, codeQualityTaskId, codeQualityTask) clTaskId, clTask = generate_compare_locales_task() schedule_task(queue, clTaskId, clTask) uiWebviewARMTestTaskId, uiWebviewARMTestTask = generate_ui_test_task( [unitTestTaskId, codeQualityTaskId], "Webview", "ARM") schedule_task(queue, uiWebviewARMTestTaskId, uiWebviewARMTestTask) # uiWebviewX86TestTaskId, uiWebviewX86TestTask = generate_ui_test_task([unitTestTaskId, codeQualityTaskId], "Webview", "X86") # schedule_task(queue, uiWebviewX86TestTaskId, uiWebviewX86TestTask) uploadNDTaskId, uploadNDTask = upload_apk_nimbledroid_task([unitTestTaskId, codeQualityTaskId]) schedule_task(queue, uploadNDTaskId, uploadNDTask)
tools/taskcluster/schedule-master-build.py
7,652
:param str engine: Klar, Webview :param str device: ARM, X86 :return: uiWebviewARMTestTaskId, uiWebviewARMTestTask This script will be executed whenever a change is pushed to the master branch. It will schedule multiple child tasks that build the app, run tests and execute code quality tools: This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. For GeckoView, upload nightly (it has release config) by default, all Release builds have WV uiWebviewX86TestTaskId, uiWebviewX86TestTask = generate_ui_test_task([unitTestTaskId, codeQualityTaskId], "Webview", "X86") schedule_task(queue, uiWebviewX86TestTaskId, uiWebviewX86TestTask)
772
en
0.758879
# Copyright 2018 Xanadu Quantum Technologies Inc. # 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. """Tests that exectation values are correctly computed in the plugin devices""" import pytest import numpy as np import pennylane as qml from contextlib import contextmanager from conftest import U, U2, A, B np.random.seed(42) @contextmanager def mimic_execution_for_expval(device): device.reset() with device.execution_context(): yield if not device.shots is None: device._samples = device.generate_samples() @pytest.mark.parametrize("shots", [None, 8192]) class TestExpval: """Test expectation values""" def test_identity_expectation(self, device, shots, tol): """Test that identity expectation value (i.e. the trace) is 1""" theta = 0.432 phi = 0.123 dev = device(2) with mimic_execution_for_expval(dev): dev.apply( [qml.RX(theta, wires=[0]), qml.RX(phi, wires=[1]), qml.CNOT(wires=[0, 1]),] ) O = qml.Identity name = "Identity" dev._obs_queue = [O(wires=[0], do_queue=False), O(wires=[1], do_queue=False)] res = np.array( [dev.expval(O(wires=[0], do_queue=False)), dev.expval(O(wires=[1], do_queue=False)),] ) assert np.allclose(res, np.array([1, 1]), **tol) def test_pauliz_expectation(self, device, shots, tol): """Test that PauliZ expectation value is correct""" theta = 0.432 phi = 0.123 dev = device(2) with mimic_execution_for_expval(dev): dev.apply( [qml.RX(theta, wires=[0]), qml.RX(phi, wires=[1]), qml.CNOT(wires=[0, 1]),] ) O = qml.PauliZ name = "PauliZ" dev._obs_queue = [O(wires=[0], do_queue=False), O(wires=[1], do_queue=False)] res = np.array( [dev.expval(O(wires=[0], do_queue=False)), dev.expval(O(wires=[1], do_queue=False)),] ) assert np.allclose(res, np.array([np.cos(theta), np.cos(theta) * np.cos(phi)]), **tol) def test_paulix_expectation(self, device, shots, tol): """Test that PauliX expectation value is correct""" theta = 0.432 phi = 0.123 dev = device(2) O = qml.PauliX with mimic_execution_for_expval(dev): dev.apply( [qml.RY(theta, wires=[0]), qml.RY(phi, wires=[1]), qml.CNOT(wires=[0, 1]),], rotations=O(wires=[0], do_queue=False).diagonalizing_gates() + O(wires=[1], do_queue=False).diagonalizing_gates(), ) dev._obs_queue = [O(wires=[0], do_queue=False), O(wires=[1], do_queue=False)] res = np.array( [dev.expval(O(wires=[0], do_queue=False)), dev.expval(O(wires=[1], do_queue=False)),] ) assert np.allclose(res, np.array([np.sin(theta) * np.sin(phi), np.sin(phi)]), **tol) def test_pauliy_expectation(self, device, shots, tol): """Test that PauliY expectation value is correct""" theta = 0.432 phi = 0.123 dev = device(2) O = qml.PauliY with mimic_execution_for_expval(dev): dev.apply( [qml.RX(theta, wires=[0]), qml.RX(phi, wires=[1]), qml.CNOT(wires=[0, 1]),], rotations=O(wires=[0], do_queue=False).diagonalizing_gates() + O(wires=[1], do_queue=False).diagonalizing_gates(), ) dev._obs_queue = [O(wires=[0], do_queue=False), O(wires=[1], do_queue=False)] res = np.array( [dev.expval(O(wires=[0], do_queue=False)), dev.expval(O(wires=[1], do_queue=False)),] ) assert np.allclose(res, np.array([0, -(np.cos(theta)) * np.sin(phi)]), **tol) def test_hadamard_expectation(self, device, shots, tol): """Test that Hadamard expectation value is correct""" theta = 0.432 phi = 0.123 dev = device(2) O = qml.Hadamard with mimic_execution_for_expval(dev): dev.apply( [qml.RY(theta, wires=[0]), qml.RY(phi, wires=[1]), qml.CNOT(wires=[0, 1]),], rotations=O(wires=[0], do_queue=False).diagonalizing_gates() + O(wires=[1], do_queue=False).diagonalizing_gates(), ) dev._obs_queue = [O(wires=[0], do_queue=False), O(wires=[1], do_queue=False)] res = np.array( [dev.expval(O(wires=[0], do_queue=False)), dev.expval(O(wires=[1], do_queue=False)),] ) expected = np.array( [ np.sin(theta) * np.sin(phi) + np.cos(theta), np.cos(theta) * np.cos(phi) + np.sin(phi), ] ) / np.sqrt(2) assert np.allclose(res, expected, **tol) def test_hermitian_expectation(self, device, shots, tol): """Test that arbitrary Hermitian expectation values are correct""" theta = 0.432 phi = 0.123 dev = device(2) O = qml.Hermitian with mimic_execution_for_expval(dev): dev.apply( [qml.RY(theta, wires=[0]), qml.RY(phi, wires=[1]), qml.CNOT(wires=[0, 1]),], rotations=O(A, wires=[0], do_queue=False).diagonalizing_gates() + O(A, wires=[1], do_queue=False).diagonalizing_gates(), ) dev._obs_queue = [ O(A, wires=[0], do_queue=False), O(A, wires=[1], do_queue=False), ] res = np.array( [ dev.expval(O(A, wires=[0], do_queue=False)), dev.expval(O(A, wires=[1], do_queue=False)), ] ) a = A[0, 0] re_b = A[0, 1].real d = A[1, 1] ev1 = ((a - d) * np.cos(theta) + 2 * re_b * np.sin(theta) * np.sin(phi) + a + d) / 2 ev2 = ((a - d) * np.cos(theta) * np.cos(phi) + 2 * re_b * np.sin(phi) + a + d) / 2 expected = np.array([ev1, ev2]) assert np.allclose(res, expected, **tol) def test_multi_mode_hermitian_expectation(self, device, shots, tol): """Test that arbitrary multi-mode Hermitian expectation values are correct""" theta = 0.432 phi = 0.123 dev = device(2) O = qml.Hermitian with mimic_execution_for_expval(dev): dev.apply( [qml.RY(theta, wires=[0]), qml.RY(phi, wires=[1]), qml.CNOT(wires=[0, 1]),], rotations=O(B, wires=[0, 1], do_queue=False).diagonalizing_gates(), ) dev._obs_queue = [O(B, wires=[0, 1], do_queue=False)] res = np.array([dev.expval(O(B, wires=[0, 1], do_queue=False))]) # below is the analytic expectation value for this circuit with arbitrary # Hermitian observable B expected = 0.5 * ( 6 * np.cos(theta) * np.sin(phi) - np.sin(theta) * (8 * np.sin(phi) + 7 * np.cos(phi) + 3) - 2 * np.sin(phi) - 6 * np.cos(phi) - 6 ) assert np.allclose(res, expected, **tol) @pytest.mark.parametrize("shots", [None, 8192]) class TestTensorExpval: """Test tensor expectation values""" def test_paulix_pauliy(self, device, shots, tol): """Test that a tensor product involving PauliX and PauliY works correctly""" theta = 0.432 phi = 0.123 varphi = -0.543 dev = device(3) obs = qml.PauliX(wires=[0], do_queue=False) @ qml.PauliY(wires=[2], do_queue=False) with mimic_execution_for_expval(dev): dev.apply( [ qml.RX(theta, wires=[0]), qml.RX(phi, wires=[1]), qml.RX(varphi, wires=[2]), qml.CNOT(wires=[0, 1]), qml.CNOT(wires=[1, 2]), ], rotations=obs.diagonalizing_gates(), ) res = dev.expval(obs) expected = np.sin(theta) * np.sin(phi) * np.sin(varphi) assert np.allclose(res, expected, **tol) def test_pauliz_hadamard(self, device, shots, tol): """Test that a tensor product involving PauliZ and PauliY and hadamard works correctly""" theta = 0.432 phi = 0.123 varphi = -0.543 dev = device(3) obs = ( qml.PauliZ(wires=[0], do_queue=False) @ qml.Hadamard(wires=[1], do_queue=False) @ qml.PauliY(wires=[2], do_queue=False) ) with mimic_execution_for_expval(dev): dev.apply( [ qml.RX(theta, wires=[0]), qml.RX(phi, wires=[1]), qml.RX(varphi, wires=[2]), qml.CNOT(wires=[0, 1]), qml.CNOT(wires=[1, 2]), ], rotations=obs.diagonalizing_gates(), ) res = dev.expval(obs) expected = -(np.cos(varphi) * np.sin(phi) + np.sin(varphi) * np.cos(theta)) / np.sqrt(2) assert np.allclose(res, expected, **tol) def test_hermitian(self, device, shots, tol): """Test that a tensor product involving qml.Hermitian works correctly""" theta = 0.432 phi = 0.123 varphi = -0.543 dev = device(3) A = np.array( [ [-6, 2 + 1j, -3, -5 + 2j], [2 - 1j, 0, 2 - 1j, -5 + 4j], [-3, 2 + 1j, 0, -4 + 3j], [-5 - 2j, -5 - 4j, -4 - 3j, -6], ] ) obs = qml.PauliZ(wires=[0], do_queue=False) @ qml.Hermitian(A, wires=[1, 2], do_queue=False) with mimic_execution_for_expval(dev): dev.apply( [ qml.RX(theta, wires=[0]), qml.RX(phi, wires=[1]), qml.RX(varphi, wires=[2]), qml.CNOT(wires=[0, 1]), qml.CNOT(wires=[1, 2]), ], rotations=obs.diagonalizing_gates(), ) res = dev.expval(obs) expected = 0.5 * ( -6 * np.cos(theta) * (np.cos(varphi) + 1) - 2 * np.sin(varphi) * (np.cos(theta) + np.sin(phi) - 2 * np.cos(phi)) + 3 * np.cos(varphi) * np.sin(phi) + np.sin(phi) ) assert np.allclose(res, expected, **tol)
tests/test_expval.py
10,823
Test expectation values Test tensor expectation values Test that Hadamard expectation value is correct Test that a tensor product involving qml.Hermitian works correctly Test that arbitrary Hermitian expectation values are correct Test that identity expectation value (i.e. the trace) is 1 Test that arbitrary multi-mode Hermitian expectation values are correct Test that PauliX expectation value is correct Test that a tensor product involving PauliX and PauliY works correctly Test that PauliY expectation value is correct Test that PauliZ expectation value is correct Test that a tensor product involving PauliZ and PauliY and hadamard works correctly Tests that exectation values are correctly computed in the plugin devices Copyright 2018 Xanadu Quantum Technologies Inc. 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. below is the analytic expectation value for this circuit with arbitrary Hermitian observable B
1,395
en
0.832883
## # @filename : epd4in2b.py # @brief : Implements for Dual-color e-paper library # @author : Yehui from Waveshare # # Copyright (C) Waveshare August 15 2017 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documnetation 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 # furished 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 OR 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. # from . import epdif from PIL import Image import RPi.GPIO as GPIO # Display resolution EPD_WIDTH = 400 EPD_HEIGHT = 300 # EPD4IN2B commands PANEL_SETTING = 0x00 POWER_SETTING = 0x01 POWER_OFF = 0x02 POWER_OFF_SEQUENCE_SETTING = 0x03 POWER_ON = 0x04 POWER_ON_MEASURE = 0x05 BOOSTER_SOFT_START = 0x06 DEEP_SLEEP = 0x07 DATA_START_TRANSMISSION_1 = 0x10 DATA_STOP = 0x11 DISPLAY_REFRESH = 0x12 DATA_START_TRANSMISSION_2 = 0x13 VCOM_LUT = 0x20 W2W_LUT = 0x21 B2W_LUT = 0x22 W2B_LUT = 0x23 B2B_LUT = 0x24 PLL_CONTROL = 0x30 TEMPERATURE_SENSOR_CALIBRATION = 0x40 TEMPERATURE_SENSOR_SELECTION = 0x41 TEMPERATURE_SENSOR_WRITE = 0x42 TEMPERATURE_SENSOR_READ = 0x43 VCOM_AND_DATA_INTERVAL_SETTING = 0x50 LOW_POWER_DETECTION = 0x51 TCON_SETTING = 0x60 RESOLUTION_SETTING = 0x61 GSST_SETTING = 0x65 GET_STATUS = 0x71 AUTO_MEASURE_VCOM = 0x80 VCOM_VALUE = 0x81 VCM_DC_SETTING = 0x82 PARTIAL_WINDOW = 0x90 PARTIAL_IN = 0x91 PARTIAL_OUT = 0x92 PROGRAM_MODE = 0xA0 ACTIVE_PROGRAM = 0xA1 READ_OTP_DATA = 0xA2 POWER_SAVING = 0xE3 class EPD: def __init__(self): self.reset_pin = epdif.RST_PIN self.dc_pin = epdif.DC_PIN self.busy_pin = epdif.BUSY_PIN self.width = EPD_WIDTH self.height = EPD_HEIGHT def digital_write(self, pin, value): epdif.epd_digital_write(pin, value) def digital_read(self, pin): return epdif.epd_digital_read(pin) def delay_ms(self, delaytime): epdif.epd_delay_ms(delaytime) def send_command(self, command): self.digital_write(self.dc_pin, GPIO.LOW) # the parameter type is list but not int # so use [command] instead of command epdif.spi_transfer([command]) def send_data(self, data): self.digital_write(self.dc_pin, GPIO.HIGH) # the parameter type is list but not int # so use [data] instead of data epdif.spi_transfer([data]) def init(self): if (epdif.epd_init() != 0): return -1 self.reset() self.send_command(BOOSTER_SOFT_START) self.send_data (0x17) self.send_data (0x17) self.send_data (0x17) # 07 0f 17 1f 27 2F 37 2f self.send_command(POWER_ON) self.wait_until_idle() self.send_command(PANEL_SETTING) self.send_data(0x0F) # LUT from OTP def wait_until_idle(self): while(self.digital_read(self.busy_pin) == 0): # 0: busy, 1: idle self.delay_ms(100) def reset(self): self.digital_write(self.reset_pin, GPIO.LOW) # module reset self.delay_ms(200) self.digital_write(self.reset_pin, GPIO.HIGH) self.delay_ms(200) def get_frame_buffer(self, image): buf = [0xFF] * int(self.width * self.height / 8) # Set buffer to value of Python Imaging Library image. # Image must be in mode 1. image_monocolor = image.convert('1') imwidth, imheight = image_monocolor.size if imwidth != self.width or imheight != self.height: raise ValueError('Image must be same dimensions as display \ ({0}x{1}).' .format(self.width, self.height)) pixels = image_monocolor.load() for y in range(self.height): for x in range(self.width): # Set the bits for the column of pixels at the current position. if pixels[x, y] == 0: buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8)) return buf def display_frame(self, frame_buffer_black, frame_buffer_red): if (frame_buffer_black != None): self.send_command(DATA_START_TRANSMISSION_1) self.delay_ms(2) for i in range(0, int(self.width * self.height / 8)): self.send_data(frame_buffer_black[i]) self.delay_ms(2) if (frame_buffer_red != None): self.send_command(DATA_START_TRANSMISSION_2) self.delay_ms(2) for i in range(0, int(self.width * self.height / 8)): self.send_data(frame_buffer_red[i]) self.delay_ms(2) self.send_command(DISPLAY_REFRESH) self.wait_until_idle() # after this, call epd.init() to awaken the module def sleep(self): self.send_command(VCOM_AND_DATA_INTERVAL_SETTING) self.send_data(0xF7) # border floating self.send_command(POWER_OFF) self.wait_until_idle() self.send_command(DEEP_SLEEP) self.send_data(0xA5) # check code ### END OF FILE ###
display/epd4in2b.py
7,150
@filename : epd4in2b.py @brief : Implements for Dual-color e-paper library @author : Yehui from Waveshare Copyright (C) Waveshare August 15 2017 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documnetation 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 furished 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 OR 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. Display resolution EPD4IN2B commands the parameter type is list but not int so use [command] instead of command the parameter type is list but not int so use [data] instead of data 07 0f 17 1f 27 2F 37 2f LUT from OTP 0: busy, 1: idle module reset Set buffer to value of Python Imaging Library image. Image must be in mode 1. Set the bits for the column of pixels at the current position. after this, call epd.init() to awaken the module border floating check code END OF FILE
1,664
en
0.808495
# -*- coding: utf-8 -*- """This module is a stub for classes related to vulnerability exposure scores. Copyright: (c) 2022 Illumio License: Apache2, see LICENSE for more details. """ from dataclasses import dataclass from illumio.util import MutableObject @dataclass class Vulnerability(MutableObject): score: int = None
illumio/vulnerabilities/vulnerability.py
339
This module is a stub for classes related to vulnerability exposure scores. Copyright: (c) 2022 Illumio License: Apache2, see LICENSE for more details. -*- coding: utf-8 -*-
185
en
0.782859