content
stringlengths
0
894k
type
stringclasses
2 values
from distutils.core import setup setup( name = 'wthen', packages = ['wthen'], # this must be the same as the name above version = '0.1.2', description = 'A simple rule engine with YAML format', author = 'Alex Yu', author_email = '[email protected]', url = 'https://github.com/sevenbigcat/wthen', # use the URL to the github repo download_url = 'https://github.com/sevenbigcat/wtehn/archive/0.1.2.tar.gz', # I'll explain this in a second keywords = ['rule engine', 'ECA', 'YAML'], # arbitrary keywords classifiers = [], )
python
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Sep 26 22:03:23 2018 @author: sameermac """ #Computing Tanimoto Distance and uniquenesses of 50 molecules from QM9 Database #from __future__ import print_function from rdkit import Chem from rdkit.Chem.Fingerprints import FingerprintMols from rdkit import DataStructs import random from rdkit import Chem, RDConfig from rdkit.Chem import AllChem, rdMolAlign, rdShapeHelpers from rdkit.Chem import Draw #from rdkit.Chem import Descriptors - May need later #Reading molecules from SDF file MOL_open = open( '/Users/sameermac/Desktop/Thesis/gdb9.sdf','rb') MOL_LIST = Chem.ForwardSDMolSupplier(MOL_open) mol = [x for x in MOL_LIST if x is not None] #Some elements empty because there are invalid molecules =/> SMILES Format #Picking 50 random molecules mol_50 = random.sample(mol, 50) #Converting to SMILES format Smile_mol = [Chem.MolToSmiles(m) for m in mol_50] Mol_From_Smile = [Chem.MolFromSmiles(m) for m in Smile_mol] #Computing number of Unique Chemicals UniquenessIndex = len(set(Smile_mol)) / len(Smile_mol) #Computing Tanimoto Distance (using RDKIT Fingerprint) finTan = [FingerprintMols.FingerprintMol(x) for x in Mol_From_Smile] TanimotoSimilarity = DataStructs.FingerprintSimilarity(finTan[1], finTan[2]) TanimotoDistance = 1 - TanimotoSimilarity #Note Default measure is Tanimoto in FingerprintSimilarity #Draw.MolToImage(mol_50[0]) - see first molecule in viewer #Error Bad Conformer ID #Erased Code #TanimotoDistance = rdShapeHelpers.ShapeTanimotoDist(Chem.MolFromSmiles(Smile_mol[1]), Chem.MolFromSmiles(Smile_mol[2])) #TanimotoDistance = rdShapeHelpers.ShapeTanimotoDist(Smile_mol[1], Smile_mol[2]) #SmileMOLs = Chem.MolToSmiles(mol) #def Smile_Conversion(MOL_LIST): #for i in mol: #smileMOLs = Chem.MolToSmiles(mol) #return MOL_LIST #DataStructs.DiceSimilarity(pairFps[0],pairFps[1]) #fig, maxweight = SimilarityMaps.GetSimilarityMapForFingerprint(refmol, mol, lambda m,idx: SimilarityMaps.GetMorganFingerprint(m, atomId=idx, radius=1, fpType='count'), metric=DataStructs.TanimotoSimilarity) #metric=DataStructs.TanimotoSimilarity
python
"""Data preprocessing script for Danish Foundation Models """ from typing import Union from functools import partial from datasets.arrow_dataset import Dataset from transformers import AutoTokenizer, BatchEncoding from transformers.tokenization_utils_base import PreTrainedTokenizerBase from transformers.tokenization_utils_fast import PreTrainedTokenizerFast from dfm.data.load import dfm_load_dataset from datasets import DatasetDict def main(): """Main method for running the preprocessing script.""" tokenizer = AutoTokenizer.from_pretrained("Maltehb/danish-bert-botxo") ds = dfm_load_dataset("DDSC/reddit-da") ds["train"] = ds["train"].select(range(1000)) ds["test"] = ds["test"].select(range(1000)) ds["val"] = ds["val"].select(range(1000)) ds = preprocess_dataset(ds, tokenizer) def preprocess_dataset( dataset: DatasetDict, tokenizer: Union[PreTrainedTokenizerFast, PreTrainedTokenizerBase], num_proc: int = 4, block_size: int = 512, ) -> Dataset: """Preprocesses a dataset for training. Args: dataset (DatasetDict): Dataset to be preprocessed. tokenizer (Union[PreTrainedTokenizerFast, PreTrainedTokenizerBase]): A Hugging Face tokenizer. num_proc (int, optional): Number of cores to use for preprocessing. Defaults to 4. block_size (int, optional): Block size of how long the grouped texts can maximally be. Defaults to 512. Returns: Dataset: A preprocessed dataset. """ # Only use text columns for key in dataset.keys(): cols = dataset[key].column_names cols.remove("text") dataset[key] = dataset[key].remove_columns(cols) # Tokenize texts tokenize_func_ = partial(tokenize_func, tokenizer=tokenizer) dataset = dataset.map( tokenize_func_, batched=True, num_proc=num_proc, remove_columns=["text"] ) # Group texts into blocks of `block_size`. group_texts_ = partial(group_texts, block_size=block_size) dataset = dataset.map( group_texts_, batched=True, batch_size=1000, num_proc=num_proc, ) # Shuffle dataset # Should probably be done prior to this stage. dataset.shuffle() return dataset def tokenize_func( examples: dict, tokenizer: Union[PreTrainedTokenizerFast, PreTrainedTokenizerBase] ) -> BatchEncoding: """Wrapper for tokenization. Args: examples (dict): A dictionary containing a "text" key and the text value. tokenizer (Union[PreTrainedTokenizerFast, PreTrainedTokenizerBase]): A Hugging Face tokenizer. Returns: BatchEncoding: A batch encoding with input ids, token type ids and attention masks. """ return tokenizer(examples["text"]) def group_texts(examples: dict, block_size: int) -> dict: """Groups texts into blocks of `block_size Args: examples (dict): A dictionary containing a "text" key and the text value. block_size (int): The block size. Returns: dict: A dict containing input ids, token type ids and attention masks with sizes corresponding to the `block_size`. """ # Concatenate all texts. concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()} total_length = len(concatenated_examples[list(examples.keys())[0]]) # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can # customize this part to your needs. total_length = (total_length // block_size) * block_size # Split by chunks of max_len. result = { k: [t[i : i + block_size] for i in range(0, total_length, block_size)] for k, t in concatenated_examples.items() } result["labels"] = result["input_ids"].copy() return result if __name__ == "__main__": main()
python
import glob import torch import tensorflow as tf from pathlib import Path from tqdm import tqdm from itertools import cycle, islice, chain from einops import rearrange, repeat import torch.nn.functional as F class PairTextSpectrogramTFRecords(object): def __init__( self, local_or_gcs_path, batch_size, prefetch_size=0, mel_bins=80, max_audio_len=2048, max_text_len=256, ): self.mel_bins = mel_bins self.max_audio_len = max_audio_len self.max_text_len = max_text_len self.path = local_or_gcs_path self.batch_size = batch_size self.prefetch_size = prefetch_size self.mel_bins = mel_bins self.max_audio_len = max_audio_len self.max_text_len = max_text_len def files(self): return self.files def __iter__(self): files = tf.data.TFRecordDataset.list_files( self.path + "/*.tfrecord", shuffle=False ) dataset = tf.data.TFRecordDataset(files) dataset = dataset.map(self.deserialize_tf_record) dataset = dataset.padded_batch( self.batch_size, padded_shapes={ "audio": (self.max_audio_len, self.mel_bins), "text": (self.max_text_len), }, ) dataset = dataset.map(self.unsqueeze_trailing) dataset = dataset.prefetch(self.prefetch_size) dataset = dataset.as_numpy_iterator() return dataset def deserialize_tf_record(self, record): tfrecord_format = { "audio": tf.io.FixedLenSequenceFeature( (self.mel_bins,), dtype=tf.float32, allow_missing=True ), "text": tf.io.FixedLenSequenceFeature( [], dtype=tf.int64, allow_missing=True ), } features_tensor = tf.io.parse_single_example(record, tfrecord_format) return features_tensor def unsqueeze_trailing(self, record): record = { "audio": repeat(record["audio"], "... -> ... ()"), "text": record["text"], } return record @staticmethod def write(spectrograms, captions, fname="data.tfrecord"): tfrecord_writer = tf.io.TFRecordWriter(fname) for (spectrogram, caption) in tqdm(zip(spectrograms, captions)): example = tf.train.Example( features=tf.train.Features( feature={ "audio": tf.train.Feature( float_list=tf.train.FloatList(value=spectrogram.flatten()) ), "text": tf.train.Feature( int64_list=tf.train.Int64List( value=[*caption.encode("utf-8")] ) ), } ) ) tfrecord_writer.write(example.SerializeToString()) tfrecord_writer.close() def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis num_active = len(iterables) nexts = cycle(iter(it).__next__ for it in iterables) while num_active: try: for next in nexts: yield next() except StopIteration: # Remove the iterator we just exhausted from the cycle. num_active -= 1 nexts = cycle(islice(nexts, num_active))
python
# How to work with lists students = [ "Monika", "Fritz", "Luise", "Andi" ] print(students) # Add item to list students.append("Ronald") print(students) # Get the length of the list print(len(students)) # Get a specific student print(students[2])
python
import numpy as np from metagraph import translator from metagraph.plugins import has_grblas, has_scipy from ..numpy.types import NumpyVector, NumpyNodeMap from ..python.types import PythonNodeSet if has_grblas: import grblas from .types import ( GrblasEdgeMap, GrblasEdgeSet, GrblasGraph, GrblasMatrixType, GrblasVectorType, GrblasNodeSet, GrblasNodeMap, dtype_mg_to_grblas, ) @translator def nodemap_to_nodeset(x: GrblasNodeMap, **props) -> GrblasNodeSet: data = x.value.dup() # Force all values to be 1's to indicate no weights data[:](data.S) << 1 return GrblasNodeSet(data) @translator def edgemap_to_edgeset(x: GrblasEdgeMap, **props) -> GrblasEdgeSet: data = x.value.dup() # Force all values to be 1's to indicate no weights data[:, :](data.S) << 1 return GrblasEdgeSet(data, transposed=x.transposed) @translator def vector_from_numpy(x: NumpyVector, **props) -> GrblasVectorType: idx = np.arange(len(x)) if x.mask is not None: idx = idx[x.mask] vals = x.value[idx] vec = grblas.Vector.from_values( idx, vals, size=len(x), dtype=dtype_mg_to_grblas[x.value.dtype] ) return vec @translator def nodeset_from_python(x: PythonNodeSet, **props) -> GrblasNodeSet: nodes = list(sorted(x.value)) size = nodes[-1] + 1 vec = grblas.Vector.from_values(nodes, [1] * len(nodes), size=size) return GrblasNodeSet(vec) @translator def nodemap_from_numpy(x: NumpyNodeMap, **props) -> GrblasNodeMap: if x.mask is not None: idx = np.flatnonzero(x.mask) vals = x.value[idx] elif x.id2pos is not None: idx = x.pos2id vals = x.value else: idx = np.arange(len(x.value)) vals = x.value size = idx[-1] + 1 vec = grblas.Vector.from_values( idx, vals, size=size, dtype=dtype_mg_to_grblas[x.value.dtype] ) return GrblasNodeMap(vec) if has_grblas and has_scipy: from ..scipy.types import ScipyEdgeSet, ScipyEdgeMap, ScipyGraph, ScipyMatrixType from .types import dtype_mg_to_grblas @translator def edgeset_from_scipy(x: ScipyEdgeSet, **props) -> GrblasEdgeSet: m = x.value.tocoo() node_list = x.node_list size = max(node_list) + 1 out = grblas.Matrix.from_values( node_list[m.row], node_list[m.col], np.ones_like(m.data), nrows=size, ncols=size, ) return GrblasEdgeSet(out, transposed=x.transposed) @translator def edgemap_from_scipy(x: ScipyEdgeMap, **props) -> GrblasEdgeMap: m = x.value.tocoo() node_list = x.node_list size = max(node_list) + 1 dtype = dtype_mg_to_grblas[x.value.dtype] out = grblas.Matrix.from_values( node_list[m.row], node_list[m.col], m.data, nrows=size, ncols=size, dtype=dtype, ) return GrblasEdgeMap(out, transposed=x.transposed) @translator def graph_from_scipy(x: ScipyGraph, **props) -> GrblasGraph: aprops = ScipyGraph.Type.compute_abstract_properties( x, {"node_type", "edge_type"} ) nodes = None if aprops["node_type"] == "map": nodes = nodemap_from_numpy(x.nodes) elif aprops["node_type"] == "set": if x.nodes is not None: nodes = nodeset_from_python(x.nodes) if aprops["edge_type"] == "map": edges = edgemap_from_scipy(x.edges) elif aprops["edge_type"] == "set": edges = edgeset_from_scipy(x.edges) else: raise TypeError(f"Cannot translate with edge_type={aprops['edge_type']}") return GrblasGraph(edges=edges, nodes=nodes) @translator def matrix_from_scipy(x: ScipyMatrixType, **props) -> GrblasMatrixType: x = x.tocoo() nrows, ncols = x.shape dtype = dtype_mg_to_grblas[x.dtype] vec = grblas.Matrix.from_values( x.row, x.col, x.data, nrows=nrows, ncols=ncols, dtype=dtype ) return vec
python
from deeplodocus.utils.version import get_version name = "deeplodocus" VERSION = (0, 1, 0, 'alpha', 1) #__version__ = get_version(VERSION) __version__ = "0.3.0"
python
import time import logging.config from scapy.all import get_if_hwaddr, sendp, sniff, UDP, BOOTP, IP, DHCP, Ether logging.getLogger("scapy.runtime").setLevel(logging.ERROR) logger = logging.getLogger(name="elchicodepython.honeycheck") def apply_controls(control_modules, **kwargs): for control_object in control_modules: control_object.apply_actions(**kwargs) class DHCPServer: def __init__(self, ip, hw): self.ip = ip self.hw = hw def __repr__(self): return "<DHCPServer Object (ip = %s, hw = %s)>" % (self.ip, self.hw) def __str__(self): return "<DHCPServer Object (ip = %s, hw = %s)>" % (self.ip, self.hw) class Status: OK = 1 ROGUE_DETECTED = 2 class DHCPWatchmen: def __init__(self, iface, fail_test, pass_test, final_exec, whitelist): """ :param iface: interface to watch :param fail_test: action to trigger if a rogue dhcp server is detected :param pass_test: action to trigger if there are no rogue dhcp servers detected :param final_exec: action to trigger always after fail_test or pass_test :param whitelist: list of IPs of verified DHCP servers to ignore. """ self.iface = iface self.hw = get_if_hwaddr(iface) self.fail_test = fail_test self.pass_test = pass_test self.final_exec = final_exec self.whitelist = whitelist self.dhcp_servers = {} self.last_status = Status.OK def check_dhcp_servers(self, number_allowed): """ Check if the number of DHCP Servers detected is allowed and trigger the corresponding action to each situation :param number_allowed: number of dhcp_servers allowed """ if len(self.dhcp_servers) > number_allowed: if self.last_status != Status.ROGUE_DETECTED: logger.warning("MORE DHCP SERVERS THAN ALLOWED: ") self.last_status = Status.ROGUE_DETECTED apply_controls(self.fail_test, watchmen=self) self.dhcp_servers = {} else: if self.last_status != Status.OK: logger.info("All seems right") self.last_status = Status.OK apply_controls(self.pass_test, watchmen=self) apply_controls(self.final_exec, watchmen=self) def check_packet(self, packet): if packet.payload.op == 2: if self.whitelist: if packet.payload.src not in self.whitelist: self.dhcp_servers[packet.payload.src] = DHCPServer( packet.payload.src, packet.src ) else: self.dhcp_servers[packet.payload.src] = DHCPServer( packet.payload.src, packet.src ) def send_dhcp_discovery(self): dhcp_discover = ( Ether(dst="ff:ff:ff:ff:ff:ff") / IP(src="0.0.0.0", dst="255.255.255.255") / UDP(sport=68, dport=67) / BOOTP(chaddr=self.hw, flags=0x8000) / DHCP(options=[("message-type", "discover"), "end"]) ) sendp(dhcp_discover, verbose=0) logger.debug("DHCP DISCOVER SEND") def dhcp_discovery_daemon(self, timeout): if self.whitelist: # There are not supposed to be any DHCP server that does # not belongs to the whitelist logger.info("Whitelist enabled for " + self.iface) max_servers_allowed = 0 else: # It is suppose to be at least one DHCP Server in the network logger.info( "Executing HoneyCheck in %s without Whitelist" % self.iface ) max_servers_allowed = 1 while True: self.send_dhcp_discovery() time.sleep(timeout) self.check_dhcp_servers(max_servers_allowed) def sniff_dhcp(self): sniff(iface=self.iface, filter="udp port 68", prn=self.check_packet) def __repr__(self): return "<DHCPSWatchmen Object (iface = %s)>" % (self.iface) def __str__(self): return "<DHCPSWatchmen Object (iface = %s)>" % (self.iface)
python
import re banner = """ _____ __ __ _____ _____ | __ \\\ \ / / /\ | __ \ | __ \ [Author : Imad Hsissou] | |__) |\ \_/ / / \ | |__) || |__) | [Author email : [email protected]] | ___/ \ / / /\ \ | _ / | ___/ [https://github.com/imadhsissou] | | | | / ____ \ | | \ \ | | [version 0.1.1] |_| |_|/_/ \_\|_| \_\|_| \"Usage of Pyarp for attacking targets without prior mutual consent is illegal.\" Type "help", "?" or "license" for more information. Type "man" for full documentation. """ license = """ MIT License Copyright (c) 2016 Imad Hsissou 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. """ manual = """ _____MAN PAGE_____ NAME pyarp - a friendly command-line spoofing tool wrriten in python. COMMANDS iface - iface <interface name> Manually set your network interface 'eth0' is set as default. inet - inet [option] options : AF_INET : IP information AF_LINK : MAC information Type "inet" for both. getmac - getmac <host ip address> Get host MAC address. scan - scan <network>, eg {scan 192.168.1.0/24} Map your network. spoof - spoof <target> <target> Launch MITM/ARP spoofing attack. press Ctrl+C to stop. help - help [command] display information about a command. clear - clear screen exit - close pyarp, press Ctrl+D for clean exit press Ctrl+C for forced exit. NOTICE IPv6 is not supported Yet. """ regex_net = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}\ ([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$" regex_ip = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" regex_mac = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$" def pyarp_banner(): return banner def pyarp_license(): return license def pyarp_manual(): return manual def regex(exp, regexp): if regexp == 'net': check = re.compile(regex_net) match = check.match(exp) return match elif regexp == 'ip': check = re.compile(regex_ip) match = check.match(exp) return match elif regexp == 'mac': check = re.compile(regex_mac) match = check.match(exp) return match else: return False
python
"""This module tests the parsing logic in card module.""" import unittest from poker.model import card class CardTests(unittest.TestCase): """Test the card class.""" def test_from_string(self): """Test from_string constructs cards correctly.""" test_cases = [ { "string": "J♠", "card": card.Card(value=card.Values.JACK, suit=card.Suits.SPADES), }, { "string": "Q♦", "card": card.Card(value=card.Values.QUEEN, suit=card.Suits.DIAMONDS), }, { "string": "K♥", "card": card.Card(value=card.Values.KING, suit=card.Suits.HEARTS), }, { "string": "A♣", "card": card.Card(value=card.Values.ACE, suit=card.Suits.CLUBS), }, { "string": "10♠", "card": card.Card(value=card.Values.TEN, suit=card.Suits.SPADES), }, # Alternate Suit Bindings { "string": "JS", "card": card.Card(value=card.Values.JACK, suit=card.Suits.SPADES), }, { "string": "QD", "card": card.Card(value=card.Values.QUEEN, suit=card.Suits.DIAMONDS), }, { "string": "KH", "card": card.Card(value=card.Values.KING, suit=card.Suits.HEARTS), }, { "string": "AC", "card": card.Card(value=card.Values.ACE, suit=card.Suits.CLUBS), }, ] for test_case in test_cases: with self.subTest(string=test_case["string"], expected=test_case["card"]): string = test_case["string"] expected = test_case["card"] actual = card.Card.from_string(string) self.assertEqual(actual, expected) with self.assertRaisesRegex( ValueError, "cannot parse card from string (.*)? invalid characters" ): _ = card.Card.from_string("K♥1") def test_str(self): """Test that str produces the correct string.""" test_cases = [ { "string": "JS", "card": card.Card(value=card.Values.JACK, suit=card.Suits.SPADES), }, { "string": "QD", "card": card.Card(value=card.Values.QUEEN, suit=card.Suits.DIAMONDS), }, { "string": "KH", "card": card.Card(value=card.Values.KING, suit=card.Suits.HEARTS), }, { "string": "AC", "card": card.Card(value=card.Values.ACE, suit=card.Suits.CLUBS), }, ] for test_case in test_cases: with self.subTest(expected=test_case["string"], card=test_case["card"]): expected = test_case["string"] input_card = test_case["card"] actual = str(input_card) self.assertEqual(actual, expected) def test_is_sortable(self): """Test that cards are sortable.""" cards = [ card.Card(suit=card.Suits.CLUBS, value=card.Values.KING), card.Card(suit=card.Suits.CLUBS, value=card.Values.ACE), card.Card(suit=card.Suits.CLUBS, value=card.Values.SIX), card.Card(suit=card.Suits.CLUBS, value=card.Values.SEVEN), card.Card(suit=card.Suits.CLUBS, value=card.Values.QUEEN), card.Card(suit=card.Suits.CLUBS, value=card.Values.THREE), card.Card(suit=card.Suits.CLUBS, value=card.Values.TEN), card.Card(suit=card.Suits.CLUBS, value=card.Values.EIGHT), card.Card(suit=card.Suits.CLUBS, value=card.Values.FOUR), card.Card(suit=card.Suits.CLUBS, value=card.Values.FIVE), card.Card(suit=card.Suits.CLUBS, value=card.Values.TWO), card.Card(suit=card.Suits.CLUBS, value=card.Values.JACK), card.Card(suit=card.Suits.CLUBS, value=card.Values.NINE), ] expected = [ card.Card(value=card.Values.TWO, suit=card.Suits.CLUBS), card.Card(value=card.Values.THREE, suit=card.Suits.CLUBS), card.Card(value=card.Values.FOUR, suit=card.Suits.CLUBS), card.Card(value=card.Values.FIVE, suit=card.Suits.CLUBS), card.Card(value=card.Values.SIX, suit=card.Suits.CLUBS), card.Card(value=card.Values.SEVEN, suit=card.Suits.CLUBS), card.Card(value=card.Values.EIGHT, suit=card.Suits.CLUBS), card.Card(value=card.Values.NINE, suit=card.Suits.CLUBS), card.Card(value=card.Values.TEN, suit=card.Suits.CLUBS), card.Card(value=card.Values.JACK, suit=card.Suits.CLUBS), card.Card(value=card.Values.QUEEN, suit=card.Suits.CLUBS), card.Card(value=card.Values.KING, suit=card.Suits.CLUBS), card.Card(value=card.Values.ACE, suit=card.Suits.CLUBS), ] actual = sorted(cards) self.assertEqual(actual, expected) def test_lt(self): """Test the less than method functions correctly.""" ace_of_clubs = card.Card(value=card.Values.ACE, suit=card.Suits.CLUBS) ace_of_spades = card.Card(value=card.Values.ACE, suit=card.Suits.SPADES) two_of_clubs = card.Card(value=card.Values.TWO, suit=card.Suits.CLUBS) self.assertTrue(two_of_clubs < ace_of_clubs) self.assertTrue(two_of_clubs < ace_of_spades) self.assertFalse(ace_of_spades < ace_of_clubs) self.assertFalse(ace_of_clubs < ace_of_spades)
python
import IMLearn.learners.regressors.linear_regression from IMLearn.learners.regressors import PolynomialFitting from IMLearn.utils import split_train_test import numpy as np import pandas as pd import plotly.express as px import plotly.io as pio import matplotlib.pyplot as plt pio.templates.default = "simple_white" plt.interactive(False) def load_data(filename: str) -> pd.DataFrame: """ Load city daily temperature dataset and preprocess data_X. Parameters ---------- filename: str Path to house prices dataset Returns ------- Design matrix and response vector (Temp) """ df = pd.read_csv(filename, index_col=0, parse_dates=['Date']).dropna().reset_index() df['DayOfYear'] = df['Date'].dt.dayofyear df = df.drop(df[df.Temp <= -50].index) return df if __name__ == '__main__': np.random.seed(0) # Question 1 - Load and preprocessing of city temperature dataset data_X = load_data('../datasets/City_Temperature.csv') # Question 2 - Exploring data_X for specific country israel_X = data_X.loc[data_X['Country'] == 'Israel'] plt.figure() plt.scatter(x=israel_X.DayOfYear, y=israel_X.Temp, c=israel_X.Year, s=3, cmap="summer") plt.colorbar(label="Avg temp' in Israel: 1995-2007", orientation="horizontal") plt.show() months = israel_X.groupby('Month').agg({'Temp': 'std'}) plt.bar(months.index, months['Temp']) plt.xticks(months.index) plt.title('Standard deviation of the daily temperatures with respect to months', y=1.03) plt.xlabel('Month') plt.ylabel('Standard Deviation') plt.show() # Question 3 - Exploring differences between countries country_month = data_X.groupby(['Country', 'Month']).agg({'Temp': {'mean', 'std'}}) for country, data in country_month.groupby(level=0): data.index.get_level_values('Month') plt.errorbar(data.index.get_level_values('Month'), data.Temp['mean'], yerr=data.Temp['std'], label=country) plt.legend(fontsize=6) plt.title('avg. monthly temp\', with error bars (standard deviation) by contries', y=1.03) plt.xlabel('Month') plt.ylabel('Avg. Temp\'') plt.xticks(country_month.index.get_level_values('Month')) plt.show() # Question 4 - Fitting model for different values of `k` X = israel_X.DayOfYear y = israel_X.Temp train_X, train_y, test_X, test_y = split_train_test(X, y, 0.75) ks = np.linspace(1, 10, 10) losses = [] for k in ks: p_model = PolynomialFitting(int(k)) p_model.fit(train_X, train_y) loss = p_model.loss(test_X, test_y) losses.append(loss) plt.title('Loss of PolyModel for different k\'s', y=1.03) plt.xticks(ks) plt.xlabel('k') plt.ylabel('Loss') plt.bar(ks, losses) plt.show() # Question 5 - Evaluating fitted model on different countries k = 5 p_model = PolynomialFitting(k) X = israel_X.DayOfYear y = israel_X.Temp p_model.fit(X, y) losses = [] countries = [] for country in data_X['Country'].unique(): if country == 'Israel': continue countries.append(country) country_data = data_X[data_X['Country'] == country] country_X, country_y = country_data.DayOfYear, country_data.Temp losses.append(p_model.loss(country_X, country_y)) plt.figure() plt.bar(countries, losses) plt.title('Loss of Israel-trained PolyModel against other contries') plt.xlabel('Country') plt.ylabel('Loss') plt.show()
python
""" batch_size, input_height, input_width, in_channels, out_channels, kernel_height, kernel_width, ClassVector=None, bias=None, dilation=1, stride=1, padding=0 """ gated_pixelcnn_shape = [ (1, 256, 256, 3, 256, 3, None, None, 1, 1, 0) ]
python
""" This program print the matrix in spiral form. This problem has been solved through recursive way. Matrix must satisfy below conditions i) matrix should be only one or two dimensional ii) number of column of all rows should be equal """ def check_matrix(matrix: list[list]) -> bool: # must be matrix = list(list(row) for row in matrix) if matrix and isinstance(matrix, list): if isinstance(matrix[0], list): prev_len = 0 for row in matrix: if prev_len == 0: prev_len = len(row) result = True else: result = prev_len == len(row) else: result = True else: result = False return result def spiral_print_clockwise(a: list[list]) -> None: """ >>> spiral_print_clockwise([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) 1 2 3 4 8 12 11 10 9 5 6 7 """ if check_matrix(a) and len(a) > 0: a = list(list(row) for row in a) mat_row = len(a) if isinstance(a[0], list): mat_col = len(a[0]) else: for dat in a: print(dat) return # horizotal printing increasing for i in range(0, mat_col): print(a[0][i]) # vertical printing down for i in range(1, mat_row): print(a[i][mat_col - 1]) # horizotal printing decreasing if mat_row > 1: for i in range(mat_col - 2, -1, -1): print(a[mat_row - 1][i]) # vertical printing up for i in range(mat_row - 2, 0, -1): print(a[i][0]) remain_mat = [row[1 : mat_col - 1] for row in a[1 : mat_row - 1]] if len(remain_mat) > 0: spiral_print_clockwise(remain_mat) else: return else: print("Not a valid matrix") return # driver code if __name__ == "__main__": a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] spiral_print_clockwise(a)
python
from datetime import datetime import os import random import sys import traceback import types import gin import numpy as np import tensorflow as tf from stackrl import agents from stackrl import envs from stackrl import metrics from stackrl import nets @gin.configurable(module='stackrl') class Training(object): """Implements the DQN training routine""" def __init__( self, env=envs.make, eval_env=None, net=nets.PseudoSiamFCN, agent=agents.DQN, train_reward_buffer_length=10, eval_reward_buffer_length=10, directory='.', save_evaluated_policies=False, log_to_file=True, log_interval=100, eval_interval=10000, checkpoint_interval=10000, goal_check_interval=1000, memory_growth=True, seed=None, eval_seed=None, ): """ Args: env: constructor for the training environment. Must accept a a 'seed' kwarg and return an instance of the environment or a generator to be used as curriculum. In the later case, the generator must yield tuples with env instance and the corresponding goal return. eval_env: constructor for the evaluation environment. If not provided, the evaluation environment is instantiated from env. net: constructor for the Q network. Receives a (possibly nested) tuple with input shape as argument. agent: constructor for the agent. Receives the Q network as argument. train_reward_buffer_length: train reward logged is the average of the rewards from this number of most recent episodes. eval_reward_buffer_length: number of episodes to run on policy evaluation. directory: path to the directory where checkpoints, models and logs are to be saved save_evaluated_policies: whether to save the agent's net weights after each evaluation. log_to_file: whether verbose is to be printed to a file or to stdout. log_interval: number of iterations between logs. eval_interval: number of iterations between policy evaluation. checkpoint_interval: number of iterations between checkpoints. goal_check_interval: number of iterations between checks of goal completion (to move on to the next one on the curriculum). Only used if env is a list with the curriculum. seed: for the random sequence of integers used to seed all of the components (env, net, agent). (Note: if not provided, None is explicitly passed as seed to the components, overriding any default/configuration.) eval_seed: the evaluation environment is seeded with this at the beginning of each evaluation. If not provided, a number is taken from the random sequence of integers given by seed. """ # Set log directory and file if not os.path.isdir(directory): os.makedirs(directory) self._log_file = os.path.join(directory, 'train.log') if \ log_to_file else None try: devices = tf.config.list_physical_devices('GPU') except AttributeError: # list_physical_devices is under experimental for tensorflow-2.0.0 devices = tf.config.experimental.list_physical_devices('GPU') for device in devices: try: tf.config.experimental.set_memory_growth(device, memory_growth) except RuntimeError: self.log("Couldn't set memory growth to {} for device {}. Already initialized.".format(memory_growth, device)) # Set seeder. if seed is None: # If seed was not provided, use the current timestamp and write it # in log, to allow reproducing results of this train. seed = int(datetime.now().timestamp() % 2**32) self.log("Using {} as seed.".format(seed)) _random = random.Random(seed) seed = lambda: _random.randint(0,2**32-1) # Set global seeds. tf.random.set_seed(seed()) np.random.seed(seed()) # Set environment self._env = env(seed=seed()) if eval_env is not None: self._eval_env = eval_env() if isinstance(self._eval_env, types.GeneratorType): raise TypeError("eval_env can't return a generator") else: self._eval_env = env() if eval_seed is None: self._eval_seed = seed() else: self._eval_seed = eval_seed # Call the seeder anyway so that the rest of the seeds from the # sequence are the same regardless of eval_seed being provided. _=seed() # If env returns a generator, use it as the curriculum if isinstance(self._env, types.GeneratorType): self._curriculum = self._env self._env, self._current_goal = next(self._curriculum) if self._current_goal is None: raise ValueError("generator returned by env argument must yield tuples with env instance and goal") if eval_env is None: self._eval_curriculum = self._eval_env self._eval_env,_ = next(self._eval_curriculum) self._curriculum_file = os.path.join(directory, 'curriculum.csv') if os.path.isfile(self._curriculum_file): # Check previously achieved goals in this train directory _, achieved_goals = np.loadtxt( self._curriculum_file, delimiter=',', skiprows=1, unpack=True ) achieved_goals = np.atleast_1d(achieved_goals) for g in achieved_goals: # Skip environments already solved if g == self._current_goal: try: self._env, self._current_goal = next(self._curriculum) if hasattr(self, '_eval_curriculum'): self._eval_env,_ = next(self._eval_curriculum) except StopIteration: self._complete = True break else: break self._goal_check_interval = int(goal_check_interval) else: self._goal_check_interval = None # Agent self._agent = agent( net( self._env.observation_spec, seed=seed() ), collect_batch_size=self._env.batch_size, seed=seed() ) # Train log self._log_interval = int(log_interval) self._train_file = os.path.join(directory, 'train.csv') # Evaluation log self._eval_interval = int(eval_interval) self._eval_file = os.path.join(directory, 'eval.csv') # Metrics self._reward = metrics.AverageReward( self._env.batch_size, length=train_reward_buffer_length) self._eval_reward = metrics.AverageReward( self._eval_env.batch_size, length=eval_reward_buffer_length ) self._loss = metrics.AverageMetric(length=log_interval) self._mean_error = metrics.AverageMetric(length=log_interval) self._collect_timer = metrics.Timer() self._train_timer = metrics.Timer() # Save policy weights self._save_weights = save_evaluated_policies self._save_filepath = lambda i: os.path.join( directory, 'saved_weights', str(i), 'weights' ) # Train checkpoints self._checkpoint_interval = int(checkpoint_interval) self._checkpoint = tf.train.Checkpoint( agent=self._agent, reward=self._reward ) self._checkpoint_manager = tf.train.CheckpointManager( self._checkpoint, directory=os.path.join(directory, 'checkpoint'), max_to_keep=1 ) # Internal variables to avoid repeated operations self._last_checkpoint_iter = None self._last_save_iter = None # Flag to assert initialize method is called before run self._initialized = False @property def iterations(self): return self._agent.iterations.numpy() @property def reset_env(self): """Set self._reset_env to trigger an environment reset on the training loop.""" if hasattr(self, '_reset_env') and self._reset_env: # pylint: disable=access-member-before-definition self._reset_env = False return True else: return False @gin.configurable(module='stackrl.Training') def initialize( self, num_steps=None, policy=None ): """Checks if a checkpoint exists and if it doesn't performs initial evaluation and collect. Args: num_steps: Number of steps for the initial experience collect. If None, the agent's replay memory is filled to its max capacity. policy: policy to use on the initial collect. If None, a random collect is run. """ try: self._checkpoint.restore(self._checkpoint_manager.latest_checkpoint) if self._checkpoint_manager.latest_checkpoint: self.log('Starting from checkpoint.') else: self.log('Starting from scratch.') # Evaluate the agent's policy once before training. self.eval() # Set collect policy and number of steps. num_steps = num_steps or self._agent.replay_memory_size if policy is None: policy = lambda o: self._env.sample() elif callable(policy): if not isinstance(policy, tf.Module): policy = agents.TFWrapper(policy) else: raise TypeError( "Invalid type {} for argument policy. Must be callable.".format(type(policy)) ) # Run initial collect self.log('Running initial collect ({} steps)...'.format(num_steps)) step = self._env.reset() for i in range(num_steps-1): if callable(step): step = step() a = policy(step[0]) self._agent.observe(*step, a) step = self._env.step(a) if i % self._log_interval == 0: self.log('Collected {}/{}'.format(i, num_steps)) if callable(step): o,r,_=step() else: o,r,_=step self._agent.observe( o, r, # Set last step as terminal. tf.ones((self._env.batch_size,), dtype=tf.bool), # last action is repeated here but it doesn't matter as an # action from a terminal state is never used. a ) self.log('Done.') self._initialized = True except Exception as e: # Log and reraise expception self.log_exception() raise e @gin.configurable(module='stackrl.Training') def run( self, max_num_iters=sys.maxsize, stop_when_complete=False, tensorboard_log=False, ): """ Args: max_num_iterations: training stops after this number of iterations. stop_when_complete: only used if training with curriculum. Whether to stop training when last goal is achieved. If false, training will continue on last environment until max_num_iterations is reached. tensorboard_log: whether to make logs to be vizualized in tensorboard. """ self._stop_when_complete = stop_when_complete if not self._initialized: self.initialize() if tensorboard_log: # Set writer logdir = os.path.join( os.path.dirname(self._train_file), 'logdir', datetime.now().strftime('%Y%m%d-%H%M%S'), ) writer = tf.summary.create_file_writer(logdir) # Set agent's iterations as default step tf.summary.experimental.set_step(self._agent.iterations) # Log first evaluation with writer.as_default(): # pylint: disable=not-context-manager tf.summary.scalar('eval', self._eval_reward.result) # Check if tf.profiler exists profiler = hasattr(tf, 'profiler') try: step = self._env.reset() self._agent.acknowledge_reset() for i in range(max_num_iters): # Colect experience with self._collect_timer: if callable(step): step = step() # pylint: disable=not-callable self._reward += step if tensorboard_log and i == 1: profiler_outdir=os.path.join(logdir, 'collect') if profiler: tf.profiler.experimental.start(profiler_outdir) tf.summary.trace_on(graph=True, profiler=not profiler) action = self._agent.collect(*step) if tensorboard_log and i == 1: if profiler: tf.profiler.experimental.stop() profiler_outdir=None with writer.as_default(): # pylint: disable=not-context-manager tf.summary.trace_export( 'collect', profiler_outdir=profiler_outdir, ) step = self._env.step(action) # Train on the sampled batch with self._train_timer: if tensorboard_log and i == 1: profiler_outdir = os.path.join(logdir, 'train') if profiler: tf.profiler.experimental.start(profiler_outdir) tf.summary.trace_on(graph=True, profiler=not profiler) loss, merr = self._agent.train() if tensorboard_log and i == 1: if profiler: tf.profiler.experimental.stop() profiler_outdir=None with writer.as_default(): # pylint: disable=not-context-manager tf.summary.trace_export( 'train', profiler_outdir=profiler_outdir, ) self._loss += loss self._mean_error += merr iters = self.iterations if iters % self._log_interval == 0: if tensorboard_log: with writer.as_default(): # pylint: disable=not-context-manager tf.summary.scalar('reward', self._reward.result) tf.summary.scalar('loss', self._loss.result) tf.summary.scalar('mean_error', self._mean_error.result) self.log_train() if iters % self._eval_interval == 0: self.eval() if tensorboard_log: with writer.as_default(): # pylint: disable=not-context-manager tf.summary.scalar('eval', self._eval_reward.result) if self._save_weights: self.save() if self._goal_check_interval and iters % self._goal_check_interval == 0: self.check_goal() if self.reset_env: step = self._env.reset() self._agent.acknowledge_reset() if iters % self._checkpoint_interval == 0: self.checkpoint() except: self.log_exception() finally: self.checkpoint() def eval(self): """Evaluates the current policy and writes the results.""" self.log('Running evaluation...') # Reset evaluation reward and environment self._eval_reward.reset(full=True) self._eval_env.seed(self._eval_seed) step = self._eval_env.reset() if callable(step): step = step() values = [] while not self._eval_reward.full: a, value = self._agent.policy(step[0], values=True) step = self._eval_env.step(a) if callable(step): step = step() self._eval_reward += step values.append(value) values = tf.stack(values) mean_max_value = tf.reduce_mean(tf.reduce_max(values, axis=-1)) mean_value = tf.reduce_mean(values) std_value = tf.math.reduce_std(values) min_value = tf.reduce_min(values) max_value = tf.reduce_max(values) # If eval file is to be created, add header if not os.path.isfile(self._eval_file): line = 'Iter,Return,Value,MeanValue,StdValue,MinValue,MaxValue\n' else: line = '' # Add iteration number and results line += '{},{},{},{},{},{},{}\n'.format( self.iterations, self._eval_reward.result.numpy(), mean_max_value.numpy(), mean_value.numpy(), std_value.numpy(), min_value.numpy(), max_value.numpy(), ) # Write to file with open(self._eval_file, 'a') as f: f.write(line) self.log('Done.') def save(self): """Saves the weights of the current Q network""" iters = self.iterations if iters != self._last_save_iter: self.log("Saving Q network's weights...") self._agent.save_weights(self._save_filepath(iters)) self._last_save_iter = iters self.log('Done.') def checkpoint(self): """Makes a checkpoint of the current training state""" iters = self.iterations if iters != self._last_checkpoint_iter: self.log('Saving checkpoint...') self._checkpoint_manager.save() self._last_checkpoint_iter = iters self.log('Done.') def log(self, line): """Logs line with a time stamp.""" line = datetime.now().strftime( '%Y-%m-%d %H:%M:%S.%f: {}\n'.format(line) ) if self._log_file is not None: with open(self._log_file, 'a') as f: f.write(line) else: sys.stdout.write(line) def log_train(self): """Logs current step's results.""" iters = self.iterations reward = self._reward.result.numpy() loss = self._loss.result.numpy() merr = self._mean_error.result.numpy() # If file doesn't exist, write header if not os.path.isfile(self._train_file): line = 'Iter,Return,Loss,MeanError,CollectTime,TrainTime\n' else: line = '' line += '{},{},{},{},{},{}\n'.format( iters, reward, loss, merr, self._collect_timer(), self._train_timer() ) with open(self._train_file, 'a') as f: f.write(line) self.log('Iter {:8} Return {:<11.6} Loss {:<11.6}'.format(iters,reward,loss)) def log_exception(self): """Logs the last exception's traceback with a timestamp""" error = str(datetime.now())+': Exception.\n' + \ traceback.format_exc() if self._log_file is not None: with open(self._log_file, 'a') as f: f.write(error) else: sys.stderr.write(error) def check_goal(self): if not self._complete and \ self._reward > self._current_goal*(1-self._agent.epsilon): self.log('Goal reward achieved.') if not os.path.isfile(self._curriculum_file): line = 'EndIter,Goal\n' else: line = '' line += '{},{}\n'.format(self.iterations, self._current_goal) with open(self._curriculum_file, 'a') as f: f.write(line) if not self._update_environment(): # If there is no environment left, set complete flag. self._complete = True if self._complete and self._stop_when_complete: raise StopIteration('Training goal achieved.') def _update_environment(self): """Replaces the environments with the next one in the curriculum. Raises: StopIteration: when curriculum is finished. """ if hasattr(self, '_curriculum'): try: new_env, self._current_goal = next(self._curriculum) except StopIteration: return False else: return False self.log('Updating environment...') assert ( new_env.observation_spec == self._env.observation_spec and new_env.action_spec == self._env.action_spec ), "All envs in curriculum must have same observation and action specs." del(self._env) self._env = new_env if hasattr(self, '_eval_curriculum'): new_env,_ = next(self._eval_curriculum) assert ( new_env.observation_spec == self._eval_env.observation_spec and new_env.action_spec == self._eval_env.action_spec ), "All envs in curriculum must have same observation and action specs." del(self._eval_env) self._eval_env = new_env self.log('Done.') # Set flag to trigger environment reset on the training loop self._reset_env = True return True
python
import os import re import nltk import numpy as np from sklearn import feature_extraction from tqdm import tqdm import codecs #from embeddings import get_similarity_vector from sklearn.feature_extraction.text import TfidfVectorizer from scipy.spatial import distance _wnl = nltk.WordNetLemmatizer() import pickle from utils.score import LABELS from utils.system import parse_params, check_version from utils.dataset import DataSet def normalize_word(w): return _wnl.lemmatize(w).lower() def get_tokenized_lemmas(s): return [normalize_word(t) for t in nltk.word_tokenize(s)] def clean(s): # Cleans a string: Lowercasing, trimming, removing non-alphanumeric return " ".join(re.findall(r'\w+', s, flags=re.UNICODE)).lower() def remove_stopwords(l): # Removes stopwords from a list of tokens return [w for w in l if w not in feature_extraction.text.ENGLISH_STOP_WORDS] def generate_baseline_feats(feat_fn, headlines, bodies, feature_file): if feature_file =="": # this means that this request is for the deployed model to rpedict one instance from a user, therefore, we do not need to save the extracted features feats = feat_fn(headlines, bodies) return feats else: # this is for training a model on a dataset if not os.path.isfile(feature_file): #if features are not stored as files, calculate them and store them in files then load the files feats = feat_fn(headlines, bodies) np.save(feature_file, feats) return np.load(feature_file) def generate_additional_features(lexicon_file, headlines, bodies, feature_file): if feature_file == "" and not lexicon_file =="tfidf" : # this means that this request is for the deployed model to rpedict one instance from a user, therefore, we do not need to save the extracted features feats = lexical_features(headlines, bodies,lexicon_file ) return feats elif feature_file == "" and lexicon_file =="tfidf": feats = tfidf_features(headlines, bodies ) return feats else: # this is for training a model on a dataset if not os.path.isfile(feature_file): #if features are not stored as files, calculate them and store them in files then load the files #feats = feat_fn(headlines, bodies) if lexicon_file !="" and lexicon_file !="embeddings" and lexicon_file !="tfidf": feats = lexical_features(headlines, bodies,lexicon_file ) np.save(feature_file, feats) # if lexicon_file =="embeddings": # feats = embeddings_features(headlines, bodies ) # np.save(feature_file, feats) if lexicon_file =="tfidf": feats = tfidf_features(headlines, bodies ) np.save(feature_file, feats) return np.load(feature_file) # def embeddings_features(h,b): # X = get_similarity_vector(h,b) # return X def get_corpus(): check_version() parse_params() # Load the training dataset and generate folds d = DataSet() # Load the competition dataset competition_dataset = DataSet("competition_test") # Merging the train, test to train the chosen model on the full dataset d.articles.update(competition_dataset.articles) d.stances.extend(competition_dataset.stances) h, b, y = [], [], [] for stance in d.stances: y.append(LABELS.index(stance['Stance'])) h.append(stance['Headline']) b.append(d.articles[stance['Body ID']]) corpus = [] corpus.extend(b) corpus.extend(h) return corpus def tfidf_features(headlines,bodies): print(len(headlines)) print(len(bodies)) f = open("tfidf_vectorizer.pickle", "rb") vectorizer = pickle.load(f) clean_bodies = [clean(body) for body in bodies] clean_headlines = [clean(headline) for headline in headlines] X_b = vectorizer.transform(clean_bodies) X_h = vectorizer.transform(clean_headlines) similarities=[] shape = X_h.get_shape() num_of_rows = shape[0] #iterating over the rows of the two sparse matrices and calculating their similariy for i in range(0,num_of_rows): similarities.append(1 - (distance.cosine(X_b[i].toarray(), X_h[i].toarray()))) return similarities def word_overlap_features(headlines, bodies): X = [] for i, (headline, body) in tqdm(enumerate(zip(headlines, bodies))): clean_headline = clean(headline) clean_body = clean(body) clean_headline = get_tokenized_lemmas(clean_headline) clean_body = get_tokenized_lemmas(clean_body) features = [ len(set(clean_headline).intersection(clean_body)) / float(len(set(clean_headline).union(clean_body)))] X.append(features) return X def refuting_features(headlines, bodies): _refuting_words = [ 'fake', 'fraud', 'hoax', 'false', 'deny', 'denies', # 'refute', 'not', 'despite', 'nope', 'doubt', 'doubts', 'bogus', 'debunk', 'pranks', 'retract' ] X = [] for i, (headline, body) in tqdm(enumerate(zip(headlines, bodies))): clean_headline = clean(headline) clean_headline = get_tokenized_lemmas(clean_headline) features = [1 if word in clean_headline else 0 for word in _refuting_words] X.append(features) return X def lexical_features(headlines, bodies,lexicon_file): _cue_words = [] with codecs.open(lexicon_file,'r',encoding='utf') as f: lines = f.readlines() for line in lines: line= line.replace('\n','') cue_word = line.replace('\r','') _cue_words.append(cue_word) f.close() X = [] for i, (headline, body) in tqdm(enumerate(zip(headlines, bodies))): clean_body = clean(body) clean_body = get_tokenized_lemmas(clean_body) features = [1 if word in clean_body else 0 for word in _cue_words] X.append(features) return X def polarity_features(headlines, bodies): _refuting_words = [ 'fake', 'fraud', 'hoax', 'false', 'deny', 'denies', 'not', 'despite', 'nope', 'doubt', 'doubts', 'bogus', 'debunk', 'pranks', 'retract' ] def calculate_polarity(text): tokens = get_tokenized_lemmas(text) return sum([t in _refuting_words for t in tokens]) % 2 X = [] for i, (headline, body) in tqdm(enumerate(zip(headlines, bodies))): clean_headline = clean(headline) clean_body = clean(body) features = [] features.append(calculate_polarity(clean_headline)) features.append(calculate_polarity(clean_body)) X.append(features) return np.array(X) def ngrams(input, n): input = input.split(' ') output = [] for i in range(len(input) - n + 1): output.append(input[i:i + n]) return output def chargrams(input, n): output = [] for i in range(len(input) - n + 1): output.append(input[i:i + n]) return output def append_chargrams(features, text_headline, text_body, size): grams = [' '.join(x) for x in chargrams(" ".join(remove_stopwords(text_headline.split())), size)] grams_hits = 0 grams_early_hits = 0 grams_first_hits = 0 for gram in grams: if gram in text_body: grams_hits += 1 if gram in text_body[:255]: grams_early_hits += 1 if gram in text_body[:100]: grams_first_hits += 1 features.append(grams_hits) features.append(grams_early_hits) features.append(grams_first_hits) return features def append_ngrams(features, text_headline, text_body, size): grams = [' '.join(x) for x in ngrams(text_headline, size)] grams_hits = 0 grams_early_hits = 0 for gram in grams: if gram in text_body: grams_hits += 1 if gram in text_body[:255]: grams_early_hits += 1 features.append(grams_hits) features.append(grams_early_hits) return features def hand_features(headlines, bodies): def binary_co_occurence(headline, body): # Count how many times a token in the title # appears in the body text. bin_count = 0 bin_count_early = 0 for headline_token in clean(headline).split(" "): if headline_token in clean(body): bin_count += 1 if headline_token in clean(body)[:255]: bin_count_early += 1 return [bin_count, bin_count_early] def binary_co_occurence_stops(headline, body): # Count how many times a token in the title # appears in the body text. Stopwords in the title # are ignored. bin_count = 0 bin_count_early = 0 for headline_token in remove_stopwords(clean(headline).split(" ")): if headline_token in clean(body): bin_count += 1 bin_count_early += 1 return [bin_count, bin_count_early] def count_grams(headline, body): # Count how many times an n-gram of the title # appears in the entire body, and intro paragraph clean_body = clean(body) clean_headline = clean(headline) features = [] features = append_chargrams(features, clean_headline, clean_body, 2) features = append_chargrams(features, clean_headline, clean_body, 8) features = append_chargrams(features, clean_headline, clean_body, 4) features = append_chargrams(features, clean_headline, clean_body, 16) features = append_ngrams(features, clean_headline, clean_body, 2) features = append_ngrams(features, clean_headline, clean_body, 3) features = append_ngrams(features, clean_headline, clean_body, 4) features = append_ngrams(features, clean_headline, clean_body, 5) features = append_ngrams(features, clean_headline, clean_body, 6) return features X = [] for i, (headline, body) in tqdm(enumerate(zip(headlines, bodies))): X.append(binary_co_occurence(headline, body) + binary_co_occurence_stops(headline, body) + count_grams(headline, body)) return X
python
from django import forms from .models import Artist, Festival class ArtistAddForm(forms.ModelForm): class Meta: model = Artist fields = ('stage_name', 'first_name', 'description', 'born', 'contry_origin', 'died', 'is_active', 'last_name') class FestivalAddForm(forms.ModelForm): class Meta: model = Festival fields = ('name', 'description', 'last_year', 'contry', 'first_year', 'headliners', 'location_lat', 'location_lng', 'location_name', 'youtube_chanel')
python
import sqlalchemy as sa from aiopg.sa import Engine from sqlalchemy import Table from app.database.common import resultproxy_to_dict from app.database.models.category import Category from app.database.models.entity import Entity async def get_all_categories( engine: Engine, ): table: Table = Category.__table__ async with engine.acquire() as conn: async with conn.begin(): result = await conn.execute(table.select()) output = resultproxy_to_dict(result) return output async def get_all_entities( engine: Engine, ): table: Table = Entity.__table__ async with engine.acquire() as conn: async with conn.begin(): result = await conn.execute(table.select()) output = resultproxy_to_dict(result) return output async def get_entity_by_category_name( engine: Engine, category_title: str ): entity_table: Table = Entity.__table__ category_table: Table = Category.__table__ join = sa.join(entity_table, category_table, entity_table.c.category_id == category_table.c.id) query = (sa.select([entity_table], use_labels=False) .select_from(join).where(category_table.c.title == category_title)) async with engine.acquire() as conn: async with conn.begin(): result = await conn.execute(query) output = resultproxy_to_dict(result) return output async def get_entity_by_id( engine: Engine, entity_id: int ): table: Table = Entity.__table__ async with engine.acquire() as conn: async with conn.begin(): result = await conn.execute(table.select().where(table.c.id == entity_id)) output = resultproxy_to_dict(result) if len(output) == 0: return None return output[0]
python
#!/usr/bin/env python import numpy as np import theano import theano.tensor as T from scipy.sparse import lil_matrix from stochastic_bb import svrg_bb, sgd_bb """ An example showing how to use svrg_bb and sgd_bb The problem here is the regularized logistic regression """ __license__ = 'MIT' __author__ = 'Conghui Tan' __email__ = '[email protected]' if __name__ == '__main__': # problem size n, d = 1000, 100 # randomly generate training data A = np.random.randn(n, d) x_true = np.random.randn(d) y = np.sign(np.dot(A, x_true) + 0.1 * np.random.randn(n)) # generate test data A_test = np.random.randn(n, d) y_test = np.sign(np.dot(A_test, x_true)) # preprocess data tmp = lil_matrix((n, n)) tmp.setdiag(y) data = theano.shared(tmp * A) # define objective function and gradient via Theano l2 = 1e-2 par = T.vector() loss = T.log(1 + T.exp(-T.dot(data, par))).mean() + l2 / 2 * (par ** 2).sum() func = theano.function(inputs=[par], outputs=loss) idx = T.ivector() grad = theano.function(inputs=[par, idx], outputs=T.grad(loss, wrt=par), givens={data: data[idx, :]}) # test SVRG-BB x0 = np.random.rand(d) print('Begin to run SVRG-BB:') x = svrg_bb(grad, 1e-3, n, d, func=func, max_epoch=50) y_predict = np.sign(np.dot(A_test, x)) print('Test accuracy: %f' % (np.count_nonzero(y_test == y_predict)*1.0 / n)) # test SGD-BB print('\nBegin to run SGD-BB:') x = sgd_bb(grad, 1e-3, n, d, phi=lambda k: k, func=func, max_epoch=50) y_predict = np.sign(np.dot(A_test, x)) print('Test accuracy: %f' % (np.count_nonzero(y_test == y_predict)*1.0 / n))
python
class MinimapCatalog(): single_map = {'Grass': (1, 0), 'House': (2, 0), 'Shop': (3, 0), 'Switch': (4, 0), 'Fort': (5, 0), 'Ruins': (6, 0), 'Forest': (8, 0), 'Thicket': (9, 0), 'Hill': (11, 0), 'Floor': (12, 0), 'Pillar': (13, 0), 'Throne': (14, 0), 'Chest': (15, 0), 'Mountain': (4, 1), 'Desert': (10, 0), 'Snow': (12, 1), 'Dark_Snow': (13, 1), 'Pier': (14, 1)} complex_map = {'Wall': (0, 2), 'River': (0, 3), 'Sand': (0, 4), 'Sea': (0, 5)} other_map = {'Cliff': (8, 6), 'Desert_Cliff': (12, 6), 'Snow_Cliff': (4, 7), 'Door': (7, 1), 'Bridge': (0, 1), 'Coast': (0, 7)} def get_minimap_types(self): r = list(self.single_map.items()) + list(self.complex_map.items()) + list(self.other_map.items()) return r
python
""" 24hourvideo ----------- A copy of `24 Hour Psycho`_ by Douglas Gordon written in Python. .. _24 Hour Psycho: https://en.wikipedia.org/wiki/24_Hour_Psycho """ from setuptools import setup import ast import re _version_re = re.compile(r'__version__\s+=\s+(.*)') with open('twentyfourhourvideo/__init__.py', 'rb') as f: version = str(ast.literal_eval(_version_re.search( f.read().decode('utf-8')).group(1))) setup( name='24hourvideo', version=version, url='https://github.com/xsteadfastx/24hourvideo', license='MIT', author='Marvin Steadfast', author_email='[email protected]', description='Play videos 24 hour long', long_description=__doc__, packages=['twentyfourhourvideo'], install_requires=['click'], entry_points={ 'console_scripts': [ '24hourvideo = twentyfourhourvideo.cli:main' ] } )
python
from conans.model import Generator from conans.paths import BUILD_INFO class DepsCppTXT(object): def __init__(self, deps_cpp_info): self.include_paths = "\n".join(p.replace("\\", "/") for p in deps_cpp_info.include_paths) self.lib_paths = "\n".join(p.replace("\\", "/") for p in deps_cpp_info.lib_paths) self.libs = "\n".join(deps_cpp_info.libs) self.defines = "\n".join(deps_cpp_info.defines) self.cppflags = "\n".join(deps_cpp_info.cppflags) self.cflags = "\n".join(deps_cpp_info.cflags) self.sharedlinkflags = "\n".join(deps_cpp_info.sharedlinkflags) self.exelinkflags = "\n".join(deps_cpp_info.exelinkflags) self.bin_paths = "\n".join(p.replace("\\", "/") for p in deps_cpp_info.bin_paths) self.rootpath = "%s" % deps_cpp_info.rootpath.replace("\\", "/") class TXTGenerator(Generator): @property def filename(self): return BUILD_INFO @property def content(self): deps = DepsCppTXT(self.deps_build_info) template = ('[includedirs{dep}]\n{deps.include_paths}\n\n' '[libdirs{dep}]\n{deps.lib_paths}\n\n' '[bindirs{dep}]\n{deps.bin_paths}\n\n' '[libs{dep}]\n{deps.libs}\n\n' '[defines{dep}]\n{deps.defines}\n\n' '[cppflags{dep}]\n{deps.cppflags}\n\n' '[cflags{dep}]\n{deps.cflags}\n\n' '[sharedlinkflags{dep}]\n{deps.sharedlinkflags}\n\n' '[exelinkflags{dep}]\n{deps.exelinkflags}\n\n') sections = [] all_flags = template.format(dep="", deps=deps) sections.append(all_flags) template_deps = template + '[rootpath{dep}]\n{deps.rootpath}\n\n' for dep_name, dep_cpp_info in self.deps_build_info.dependencies: deps = DepsCppTXT(dep_cpp_info) dep_flags = template_deps.format(dep="_" + dep_name, deps=deps) sections.append(dep_flags) return "\n".join(sections)
python
from fabric.contrib.files import exists from fabric.operations import put, get from fabric.colors import green, red from fabric.api import env, local, sudo, run, cd, prefix, task, settings # Server hosts STAGING_USER = 'ubuntu' PRODUCTION_USER = 'ubuntu' STAGING_SERVER = '%[email protected]' % STAGING_USER PRODUCTION_SERVER = '%[email protected]' % PRODUCTION_USER APP_NAME = 'example' DIR_WEBAPP = '/var/webapps' REPO_NAME = 'https://github.com/vuchauthanh/chef-django-sample.git' @task def staging(): """ Use staging server settings """ global env print(green('Deploy for staging server.')) env.hosts = [STAGING_SERVER] env.key_filename = '/Volumes/Data/Keys/EC2/private/vuchau_ec2.pem' env['dir_app'] = '%s/%s' % (DIR_WEBAPP, APP_NAME) env['branch'] = 'develop' env['environment'] = 'staging', env['user'] = STAGING_USER @task def production(): """ Use prod server settings """ print(green('Deploy for production server.')) env.hosts = [PRODUCTION_SERVER] env.key_filename = '/Volumes/Data/Keys/EC2/private/vuchau_ec2.pem' env['dir_app'] = '%s/%s' % (DIR_WEBAPP, APP_NAME) env['branch'] = 'develop' env['environment'] = 'production' env['user'] = PRODUCTION_USER def package_installed(pkg_name): """ref: http:superuser.com/questions/427318/#comment490784_427339""" cmd_f = 'dpkg-query -l "%s" | grep -q ^.i' cmd = cmd_f % (pkg_name) with settings(warn_only=True): result = run(cmd) return result.succeeded @task def install_chef(latest=True): """ Install chef-solo on the server """ sudo('apt-get update', pty=True) sudo('apt-get install -y git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties', pty=True) if not package_installed('ruby'): run('cd ~/ && wget http://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.gz') run('tar -xzvf ruby-2.1.5.tar.gz') run('cd ~/ruby-2.1.5/ && ./configure && make && sudo make install') if latest: sudo('gem install chef --no-ri --no-rdoc', pty=True) else: sudo('gem install chef --no-ri --no-rdoc', pty=True) sudo('gem install json') @task def bootstrap(): """ Bootstrap the specified server. Install chef then run chef solo. :param name: The name of the node to be bootstrapped :param no_install: Optionally skip the Chef installation since it takes time and is unneccesary after the first run :return: """ print(green('Bootstrapping ...')) if not package_installed('chef'): install_chef() # Make root folder if not exists(env['dir_app']): sudo('mkdir -p %s' % DIR_WEBAPP) sudo('chown -R %s %s' % (env['user'], DIR_WEBAPP)) with cd(DIR_WEBAPP): print(green('Cloning repo from GitHub...')) if not exists('%s' % APP_NAME): run('git clone %s %s' % (REPO_NAME, APP_NAME)) @task def deploy(): """ Deploy to server """ print(green('Deploying ...')) with cd(env['dir_app']): pass def start(): print(green('Restarting supervisor service ...')) sudo('supervisorctl reload') print(green('Restarting nginx service ...')) sudo('service nginx restart') def stop(): print(green('Stop supervisor service ...')) sudo('supervisorctl stop backend') print(green('Stop nginx service ...')) sudo('service nginx stop') def restart(): print(green('Restarting supervisor service ...')) run('sudo supervisorctl reload') print(green('Restarting nginx service ...')) sudo('service nginx restart') def tail_log(log='access'): """ Tail log file. """ with cd(env['dir_logs']): sudo('tail -f %s' % env['log_file_name']) def get_log(log='access'): """ Tail log file. """ with cd(env['dir_logs']): get('%s' % env['log_file_name'])
python
""" Implements the Temoral Difference Learning algorithm. This solver contains TD-Lambda methods based on Prof.David Silver Lecture slides. Note that TD-Lambda can be used as other solver by setting the n-step return and \gamma value accordingly (c) copyright Kiran Vaddi 02-2020 """ import numpy as np import pdb from collections import defaultdict class TD: def __init__(self,env,policy): self.env = env self.policy = policy self.num_episodes = num_episodes self.gamma = gamma self.l = l self.alpha = alpha def _collect_samples(self): """ collects multiple samples of experiences from the environment. """ samples = {} for e in range(self.num_episodes): self.env.reset() episode = [] is_done = False while not is_done: state = self.env.s action = self.policy(state) sp,r,is_done = self.env.step(action) episode.append((state,r,sp)) samples[e] = episode self.samples = samples return samples def _sample_episode(self): """ collects a samples of experiences from the environment. requires the self to contain a policy the self class should contain env, policy """ self.env.reset() episode = [] is_done = False while not is_done: state = self.env.s action = self.policy(state) sp,r,is_done = self.env.step(action) episode.append((state,action,r,sp)) return episode def _compute_lambda_return(self,episode,V): """ Computes lamda return according to the following: lamda-return using: \[G_{t}^{\lambda} = (1-\lambda)*\Bigsum_{n=1}^{n=inf}\lambda^{n-1}G_t^n\] """ n = len(episode) Gtlambda = defaultdict(float) for step in range(n): Gtn = self._compute_nstep_return(episode,V,n=step) for time in Gtn: Gtlambda[time] += (1-self.l)*(self.l**step)*Gtn[time] return Gtlambda def _compute_nstep_return(self,episode,V, n = None): """ Computes n-step return according to the following: n-step return using: \[G_t^n = R_t+1 + \gamma*R_t+2 + ... +\gamma^{n-1}+\gamma^n*V(S_t+n)\] """ if n is None: n = len(episode) E = [] for state, reward,next_state in episode: E.append((state,reward,next_state)) Gn = defaultdict(float) for ind in range(len(E)): nsteps = E[ind:ind+n+1] # We use a step morethan what is asked but it is a hack Gtn = 0 for i,(state,reward,next_state) in enumerate(nsteps): Gtn += (self.gamma**i)*reward tostate = nsteps[-1][2] Gn[ind] = Gtn + (self.gamma**n)*V[tostate] return Gn def _compute_eligibility_trace(self,episode): """ Computes eligibility trace of any state using the following: \[E_t(s) = \gamma*\lambda*E_{t-1}(s) + \delta_{S_t,s}\] Inputs: ------ episode : An episode from the environment experience Outputs: ------- E : Eligibility trace. A dictornary with E[time,state] keys """ E = defaultdict(float) states = [i[0] for i in episode] for ind,(state,_,_) in enumerate(episode): E[ind,state] = self.gamma*self.l*E[ind-1,state] + 1 return E class tabular(TD): def __init__(self,env,policy,gamma=1.0, l = 0.0, alpha=1.0, verbose = False): self.gamma = gamma self.l = l self.alpha = alpha self.verbose = verbose super(tabular, self).__init__(env,policy) def forward_view(self): """ Returns a state value function approximation using Forward view TD-lambda update. Outputs: -------- Vpi : State value function under policy \pi """ samples = self._collect_samples() V = defaultdict(float) for e in samples: episode = samples[e] states = [i[0] for i in episode] Gtlambda = self._compute_lambda_return(episode,V) for time,state in enumerate(states): V[state] = V[state] + self.alpha*(Gtlambda[time]-V[state]) return V def tdn(self,n=0): """ Perform a TD(n) updates using the following: Computes TD-error using n-step return: \[ \delta_t = G_t^n - V(S_t)\] Update the state-value function using the following: \[V(S_t) = V(S_t) + \alpha*(\delta_t)\] Inputs: ------- n : n-step return to be calulcated (default, n=0) Outputs: ------- Vpi : State-value function under policy \(\pi\) a dictonary """ samples = self._collect_samples() V = defaultdict(float) for e in samples: episode = samples[e] states = [i[0] for i in episode] Gtn = self._compute_nstep_return(episode,V,n=n) for time,state in enumerate(states): V[state] = V[state] + self.alpha*(Gtn[time]-V[state]) return V def backward_view(self, n=0): """ Performs backward view TD-lambda using the following: Compute eligibility trace: \[E_t(S) = \gamma*\lambda*E_{t-1}(s) + \delta_{S_t,s}\] TD Error: \[\delta_t = R_{t+1} + \gamma*V(S_{t+1}) - V(S_t)\] Make the update using: \[V(s) = V(s) + \alpha*\delta_t*E_t(s)\] """ samples = self._collect_samples() V = defaultdict(float) for e in samples: episode = samples[e] T = len(episode) E = self._compute_eligibility_trace(episode) states = [i[0] for i in episode] Gtn = self._compute_nstep_return(episode,V,n=n) for t in range(T): current_state,_,_ = episode[t] delta_t = Gtn[t]-V[current_state] for state in V: V[state] = V[state] + self.alpha*delta_t*E[t,state] return V class svfa(TD): """ A TD class for State Value Function Approximation (SVFA). fa which is a function-approximator, should have the following methods: predict : Given a state, return state-value function approximation update : Given set of training data update function approximator. Inputs: ------- env : Environment class fa. : Function approximator policy : policy under which to sample experience from num_episodes. : Number of episodes (default, 100) gamma : Discount factor (default, 1.0) verbose : To print updates regularly (default, False) Attributes: ----------- Methods: -------- solve : Solves MC value function updates using function approximator 'method' : Two methods of TD solutions available: 'TD0' : Updates the target as R+\gamma*\hat{V} 'TDlambda' : Updates to target as G_{t}^{\lambda} """ def __init__(self,env,policy,fa,num_episodes = 100,\ gamma=1.0, l = 0.0, alpha=1.0, verbose = False): self.num_episodes = num_episodes self.gamma = gamma self.l = l self.alpha = alpha self.verbose = verbose self.fa = fa super(svfa, self).__init__(env,policy) self.V = defaultdict(float) self.N = defaultdict(int) self.S = defaultdict(int) self.trace = namedtuple("trace",["lengths", "rewards","epsilon"]) self.trace = self.trace(lengths=[],rewards=[],epsilon = []) def solve(self, method = 'TD0'): for e in self.num_episodes: episode = self._sample_episode() states = [] targets = [] for time,(st,at,rt,sp) in enumerate(episode): states.append(st) if method is 'TD0': target = rt + self.gamma*self.fa.predict(st) elif method is 'TDlambda': self.V[st] = self.fa.predict(st) Gtlambda = self._compute_lambda_return(episode,self.V) target = Gtlambda[st] targets.append(target) # Update our function approximator with # training data as {<St,target_t>}_{t=1..T} fa.update(state,target) self.trace.rewards.append(np.sum(targets)) self.trace.lengths.append(len(episode)) if self.verbose: if e%100==0: print('{}/{} episodes finished'.format(e,self.num_episodes)) return self.fa
python
""" This script shows the for code block """ NAME = input("Please enter your name: ") AGE = int(input("How old are you, {0}? ".format(NAME))) print(AGE) # if AGE >= 18: # print("You are old enough to vote") # print("Please put an X in the box") # else: # print("Please come back in {0} years".format(18 - AGE)) if AGE < 18: print("Please come back in {0} years".format(18 - AGE)) elif AGE == 900: print("Sorry, Yoda, you die in Return of the Jedi") else: print("You are old enough to vote") print("Please put an X in the box")
python
from django.contrib import admin from .models import Blog, BlogType # Register your models here. class BlogAdmin(admin.ModelAdmin): list_display = ('title', 'author', 'blog_type', 'created_time') @admin.register(BlogType) class BlogTypeAdmin(admin.ModelAdmin): list_display = ('id', 'type_name') admin.site.register(Blog, BlogAdmin)
python
from django.conf.urls import url from .views import ( CheckoutView, CheckoutUpdateView, OrderDeleteView, CheckoutOrderView, OrdersView, AcceptedOrdersView, RejectedOrdersView, BuyOrdersView, BuyThankView, ) urlpatterns = [ url(r'^order/(?P<id>\d+)/$', CheckoutView.as_view(), name='order'), url(r'^order/(?P<pk>\d+)/update/$', CheckoutUpdateView.as_view(), name='update'), url(r'^order/(?P<id>\d+)/delete/$', OrderDeleteView.as_view(), name='delete'), url(r'^cart/$', CheckoutOrderView.as_view(), name='checkout'), url(r'^pending/$', OrdersView.as_view(), name='pending'), url(r'^rejected/$', RejectedOrdersView.as_view(), name='rejected'), url(r'^accepted/$', AcceptedOrdersView.as_view(), name='accepted'), url(r'^thank_you/$', BuyThankView.as_view(), name='thank'), url(r'^buy/$', BuyOrdersView.as_view(), name='buy'), ]
python
from django import forms from captcha.fields import CaptchaField class UserForm(forms.Form): username = forms.CharField(label="用户",max_length=128,widget=forms.TextInput(attrs={'class':'form-contro','placeholder':'用户'})) password = forms.CharField(label="密码",max_length=128,widget=forms.PasswordInput(attrs={'class':'form-contro','placeholder':'密码'})) captcha = CaptchaField(label="验证码") class RegisterForm(forms.Form): sex = ( ('male','男'), ('female','女') ) username = forms.CharField(label="用户", max_length=128, widget=forms.TextInput(attrs={'class': 'form-contro', 'placeholder': '用户'})) password = forms.CharField(label="密码", max_length=128, widget=forms.PasswordInput(attrs={'class': 'form-contro', 'placeholder': '密码'})) password_sure = forms.CharField(label="确认密码", max_length=128, widget=forms.PasswordInput(attrs={'class': 'form-contro', 'placeholder': '密码'})) email = forms.EmailField(label="注册邮箱",widget=forms.EmailInput(attrs={'class': 'form-contro', 'placeholder': '邮箱'})) gender = forms.ChoiceField(label="性别",choices=sex) captcha = CaptchaField(label="验证码")
python
import json import os from datetime import datetime from io import StringIO from itertools import product import pytest from peewee import Model, SqliteDatabase from orcid_hub import JSONEncoder from orcid_hub.models import ( Affiliation, AffiliationRecord, AffiliationExternalId, BaseModel, BooleanField, ExternalId, File, ForeignKeyField, FundingContributor, FundingInvitee, FundingRecord, Log, ModelException, NestedDict, OrcidToken, Organisation, OrgInfo, OrcidApiCall, PartialDate, PartialDateField, PropertyRecord, PeerReviewExternalId, PeerReviewInvitee, PeerReviewRecord, ResourceRecord, Role, Task, TaskType, TaskTypeField, TextField, User, UserInvitation, UserOrg, UserOrgAffiliation, WorkContributor, WorkExternalId, WorkInvitee, WorkRecord, app, create_tables, drop_tables, load_yaml_json, validate_orcid_id) from utils import readup_test_data @pytest.fixture def models(testdb): Organisation.insert_many((dict( name="Organisation #%d" % i, tuakiri_name="Organisation #%d" % i, orcid_client_id="client-%d" % i, orcid_secret="secret-%d" % i, confirmed=(i % 2 == 0)) for i in range(10))).execute() User.insert_many((dict( name="Test User #%d" % i, first_name="Test_%d" % i, last_name="User_%d" % i, email="user%d@org%d.org.nz" % (i, i * 4 % 10), confirmed=(i % 3 != 0), roles=Role.SUPERUSER if i % 42 == 0 else Role.ADMIN if i % 13 == 0 else Role.RESEARCHER) for i in range(60))).execute() User.insert_many((dict( name="Test User with ORCID ID 'ABC-123' #%d" % i, orcid="ABC-123", first_name="Test_%d" % i, last_name="User_%d" % i, email="user_the_same_id_%d@org%d.org.nz" % (i, i), confirmed=True, organisation=(i + 1), roles=Role.RESEARCHER) for i in range(3))).execute() UserOrg.insert_many( dict(user=u.id, org=u.organisation_id) for u in User.select().where(User.orcid == "ABC-123")).execute() UserOrg.insert_many( dict(is_admin=((u + o) % 23 == 0), user=u, org=o) for (u, o) in product(range(2, 60, 4), range(2, 10)) if not UserOrg.select().where(UserOrg.user == u, UserOrg.org == o).exists()).execute() UserOrg.insert_many((dict(is_admin=True, user=43, org=o) for o in range(1, 11))).execute() OrcidToken.insert_many((dict( user=User.get(id=1), org=Organisation.get(id=1), scopes="/read-limited", access_token="Test_%d" % i) for i in range(60))).execute() UserOrgAffiliation.insert_many((dict( user=User.get(id=1), organisation=Organisation.get(id=1), department_name="Test_%d" % i, department_city="Test_%d" % i, role_title="Test_%d" % i, path="Test_%d" % i, put_code="%d" % i) for i in range(30))).execute() Task.insert_many((dict( org=Organisation.get(id=1), created_by=User.get(id=1), updated_by=User.get(id=1), filename="Test_%d" % i, task_type=0) for i in range(30))).execute() AffiliationRecord.insert_many((dict( is_active=False, task=Task.get(id=1), put_code=90, local_id="Test_%d" % i, status="Test_%d" % i, first_name="Test_%d" % i, last_name="Test_%d" % i, email="Test_%d" % i, orcid="123112311231%d" % i, organisation="Test_%d" % i, affiliation_type="Test_%d" % i, role="Test_%d" % i, department="Test_%d" % i, city="Test_%d" % i, region="Test_%d" % i, country="Test_%d" % i, disambiguated_id="Test_%d" % i, disambiguation_source="Test_%d" % i) for i in range(10))).execute() record = AffiliationRecord.get() AffiliationExternalId.insert_many((dict( record=record, type="Test1_%d" % i, value="Test1_%d" % i, url="Test1_%d" % i, relationship="Test1_%d" % i) for i in range(10))).execute() PropertyRecord.insert_many((dict( type="URL", is_active=False, task=Task.get(id=1), put_code=90, status="Test_%d" % i, first_name="Test_%d" % i, last_name="Test_%d" % i, email="Test_%d" % i, orcid="123112311231%d" % i, name="Test_%d" % i, value="Test_%d" % i, visibility="Test_%d" % i, display_index=i) for i in range(10))).execute() PropertyRecord.insert_many((dict( type="NAME", is_active=False, task=Task.get(id=1), put_code=90, status="Test_%d" % i, first_name="Test_%d" % i, last_name="Test_%d" % i, email="Test_%d" % i, orcid="123112311231%d" % i, value="Test_%d" % i, visibility="Test_%d" % i, display_index=i) for i in range(10))).execute() PropertyRecord.insert_many((dict( type="KEYWORD", is_active=False, task=Task.get(id=1), put_code=90, status="Test_%d" % i, first_name="Test_%d" % i, last_name="Test_%d" % i, email="Test_%d" % i, orcid="123112311231%d" % i, value="Test_%d" % i, visibility="Test_%d" % i, display_index=i) for i in range(10))).execute() FundingRecord.insert_many((dict( task=Task.get(id=1), title="Test_%d" % i, translated_title="Test_%d" % i, translated_title_language_code="Test_%d" % i, type="Test_%d" % i, organization_defined_type="Test_%d" % i, short_description="Test_%d" % i, amount="Test_%d" % i, currency="Test_%d" % i, org_name="Test_%d" % i, city="Test_%d" % i, region="Test_%d" % i, country="Test_%d" % i, disambiguated_id="Test_%d" % i, disambiguation_source="Test_%d" % i, is_active=False, status="Test_%d" % i) for i in range(10))).execute() record = FundingRecord.get() FundingContributor.insert_many((dict( record=record, orcid="123112311231%d" % i, name="Test_%d" % i, role="Test_%d" % i) for i in range(10))).execute() FundingInvitee.insert_many((dict( record=record, orcid="123112311231%d" % i, first_name="Test_%d" % i, last_name="Test_%d" % i, put_code=i, status="Test_%d" % i, identifier="%d" % i, visibility="Test_%d" % i, email="Test_%d" % i) for i in range(10))).execute() ExternalId.insert_many((dict( record=record, type="Test_%d" % i, value="Test_%d" % i, url="Test_%d" % i, relationship="Test_%d" % i) for i in range(10))).execute() task = Task.get() PeerReviewRecord.insert_many((dict( task=task, review_group_id="issn:1212_%d" % i, reviewer_role="reviewer_%d" % i, review_url="xyz_%d" % i, review_type="REVIEW_%d" % i, subject_external_id_type="doi_%d" % i, subject_external_id_value="1212_%d" % i, subject_external_id_url="url/SELF_%d" % i, subject_external_id_relationship="SELF_%d" % i, subject_container_name="Journal title_%d" % i, subject_type="JOURNAL_ARTICLE_%d" % i, subject_name_title="name_%d" % i, subject_name_subtitle="subtitle_%d" % i, subject_name_translated_title_lang_code="en", subject_name_translated_title="sdsd_%d" % i, subject_url="url_%d" % i, convening_org_name="THE ORGANISATION_%d" % i, convening_org_city="auckland_%d" % i, convening_org_region="auckland_%d" % i, convening_org_country="nz_%d" % i, convening_org_disambiguated_identifier="123_%d" % i, convening_org_disambiguation_source="1212_%d" % i, is_active=False) for i in range(10))).execute() record = PeerReviewRecord.get() PeerReviewExternalId.insert_many((dict( record=record, type="Test1_%d" % i, value="Test1_%d" % i, url="Test1_%d" % i, relationship="Test1_%d" % i) for i in range(10))).execute() PeerReviewInvitee.insert_many((dict( record=record, orcid="1231123112311%d" % i, first_name="Test1_%d" % i, last_name="Test1_%d" % i, put_code=i, status="Test1_%d" % i, identifier="1%d" % i, visibility = "PUBLIC", email="Test1_%d" % i) for i in range(10))).execute() WorkRecord.insert_many((dict( task=task, title="Test_%d" % i, subtitle="Test_%d" % i, translated_title="Test_%d" % i, translated_title_language_code="Test_%d" % i, journal_title="Test_%d" % i, short_description="Test_%d" % i, citation_type="Test_%d" % i, citation_value="Test_%d" % i, type="Test_%d" % i, url="Test_%d" % i, language_code="Test_%d" % i, country="Test_%d" % i, is_active=False, status="Test_%d" % i) for i in range(10))).execute() record = WorkRecord.get() WorkContributor.insert_many((dict( record=record, orcid="123112311231%d" % i, name="Test_%d" % i, contributor_sequence="%d" % i, role="Test_%d" % i) for i in range(10))).execute() WorkExternalId.insert_many((dict( record=record, type="Test_%d" % i, value="Test_%d" % i, url="Test_%d" % i, relationship="Test_%d" % i) for i in range(10))).execute() WorkInvitee.insert_many((dict( record=record, orcid="123112311231%d" % i, first_name="Test_%d" % i, last_name="Test_%d" % i, put_code=i, status="Test_%d" % i, identifier="%d" % i, visibility="Test_%d" % i, email="Test_%d" % i) for i in range(10))).execute() yield testdb def test_user_uuid(): u = User(email="[email protected]") assert str(u.uuid) == "8428e5f6-38c6-530f-8339-9aeffb99e022" def test_user_org_link_user_constraint(models): org = Organisation.get(id=1) uo = UserOrg(user_id=999999, org=org) with pytest.raises(User.DoesNotExist): uo.save() def test_user_org_link_org_constraint(models): user = User.select().limit(1).first() from peewee import IntegrityError with pytest.raises(IntegrityError): UserOrg.create(user=user, org_id=999999) def test_test_database(models): """Test of the consitency of the test database.""" assert Organisation.select().count() == 14 assert User.select().count() == 95 assert OrcidToken.select().count() == 76 assert AffiliationRecord.select().count() == 10 assert AffiliationExternalId.select().count() == 10 assert FundingRecord.select().count() == 10 assert FundingContributor.select().count() == 10 assert FundingInvitee.select().count() == 10 assert ExternalId.select().count() == 10 assert WorkRecord.select().count() == 10 assert WorkContributor.select().count() == 10 assert WorkExternalId.select().count() == 10 assert WorkInvitee.select().count() == 10 assert PeerReviewRecord.select().count() == 10 assert PeerReviewExternalId.select().count() == 10 assert PeerReviewInvitee.select().count() == 10 assert PropertyRecord.select().where(PropertyRecord.type == "URL").count() == 10 assert PropertyRecord.select().where(PropertyRecord.type == "NAME").count() == 10 assert PropertyRecord.select().where(PropertyRecord.type == "KEYWORD").count() == 10 assert Task.select().count() == 30 assert UserOrgAffiliation.select().count() == 30 assert User.get(id=43).admin_for.count() == 10 assert User.get(id=1).admin_for.count() == 0 assert User.get(id=42).admin_for.count() > 0 assert User.get(id=2).organisations.count() > 0 assert Organisation.get(id=1).admins.count() == 2 assert Organisation.get(id=5).users.count() > 0 assert Organisation.get(id=5).admins.count() > 0 assert User.select().where(User.orcid == User.get( email="[email protected]").orcid).count() == 3 assert len(User.get(email="[email protected]").org_links) == 3 user = User.get(email="[email protected]") available_organisations = user.available_organisations assert available_organisations.count() == 14 admin = User.create(email="[email protected]", organisation=user.organisation, confirmed=True, first_name="TEST", last_name="ADMIN", roles=Role.ADMIN) ui = UserInvitation.create(email=user.email, invitee=user, inviter=admin, token="TOKEN-123") admin.delete_instance() ui = UserInvitation.get(ui.id) assert ui.inviter_id is None user.delete_instance() assert not UserInvitation.select().where(UserInvitation.id == ui.id).exists() org = Organisation.select().limit(1).first() user = User.select().limit(1).first() ot = OrcidToken.create(user=user, org=org, scopes="S1,S2,S3") def test_roles(): assert Role.RESEARCHER == "RESEARCHER" assert Role.RESEARCHER == Role["RESEARCHER"] assert Role.RESEARCHER != "ADMIN" assert Role.RESEARCHER != Role["ADMIN"] assert hash(Role.RESEARCHER) == hash("RESEARCHER") def test_user_roles(models): user = User( name="Test User ABC123", first_name="ABC", last_name="123", email="[email protected]", confirmed=True, roles=Role.ADMIN | Role.RESEARCHER) assert user.has_role(Role.ADMIN) assert user.has_role("ADMIN") assert user.has_role(Role.RESEARCHER) assert user.has_role("RESEARCHER") assert user.has_role(Role.RESEARCHER | Role.ADMIN) assert user.has_role(4) assert user.has_role(2) assert not user.has_role(Role.SUPERUSER) assert not user.has_role("SUPERUSER") assert not user.has_role(1) assert not user.has_role("NOT A ROLE") assert not user.has_role(~(1 | 2 | 4 | 8 | 16)) assert not user.has_role(1.1234) def test_admin_is_admin(models): user = User( name="Test User ABC123", first_name="ABC", last_name="123", email="[email protected]", confirmed=True, roles=Role.ADMIN | Role.RESEARCHER) assert user.is_admin def test_drop_tables(models): drop_tables() assert not User.table_exists() # assert not Organisation.table_exists() assert not UserOrg.table_exists() def test_create_tables(models): drop_tables() create_tables() assert User.table_exists() assert Organisation.table_exists() assert UserOrg.table_exists() def test_partial_date(): pd = PartialDate.create({"year": {"value": "2003"}}) with pytest.raises(TypeError): pd.as_datetime() assert pd.as_orcid_dict() == {'year': {'value': '2003'}, 'month': None, 'day': None} assert pd.year == 2003 pd = PartialDate.create({ "year": { "value": "2003" }, "month": { "value": '07' }, "day": { "value": '31' } }) assert pd.as_orcid_dict() == { 'year': { 'value': '2003' }, 'month': { "value": '07' }, 'day': { "value": '31' } } assert pd.year == 2003 and pd.month == 7 and pd.day == 31 pd = PartialDate.create({ "year": { "value": "2003" }, "month": { "value": "11" }, "day": { "value": None } }) assert pd.year == 2003 and pd.month == 11 and pd.day is None pd = PartialDate.create({ "year": { "value": "2003" }, "month": { "value": None }, "day": { "value": None } }) assert pd.year == 2003 and pd.month is None and pd.day is None assert PartialDate().as_orcid_dict() is None assert PartialDate.create(None) is None assert PartialDate.create({}) is None assert PartialDate.create("1997") == PartialDate(year=1997, month=None, day=None) assert PartialDate.create("1997-12") == PartialDate(year=1997, month=12, day=None) assert PartialDate.create("1997-12-31") == PartialDate(year=1997, month=12, day=31) assert PartialDate.create("1997/12") == PartialDate(year=1997, month=12, day=None) assert PartialDate.create("1997/12/31") == PartialDate(year=1997, month=12, day=31) assert PartialDate.create("12/1997") == PartialDate(year=1997, month=12, day=None) assert PartialDate.create("31/12/1997") == PartialDate(year=1997, month=12, day=31) assert PartialDate.create("1997.12") == PartialDate(year=1997, month=12, day=None) assert PartialDate.create("1997.12.31") == PartialDate(year=1997, month=12, day=31) assert PartialDate.create("12.1997") == PartialDate(year=1997, month=12, day=None) assert PartialDate.create("31.12.1997") == PartialDate(year=1997, month=12, day=31) assert PartialDate.create("5.03.2018") == PartialDate(year=2018, month=3, day=5) assert PartialDate.create("1997 12:00:00 PM") == PartialDate(year=1997, month=None, day=None) assert PartialDate.create("1997-12 12:00:00 PM") == PartialDate(year=1997, month=12, day=None) assert PartialDate.create("1997-12-31 12:00:00 PM") == PartialDate(year=1997, month=12, day=31) assert PartialDate.create("1997/12 12:00:00 PM") == PartialDate(year=1997, month=12, day=None) assert PartialDate.create("1997/12/31 12:00:00 PM") == PartialDate(year=1997, month=12, day=31) assert PartialDate.create("12/1997 12:00:00 PM") == PartialDate(year=1997, month=12, day=None) assert PartialDate.create("31/12/1997 12:00:00 PM") == PartialDate(year=1997, month=12, day=31) assert PartialDate.create("6/08/2017 12:00:00 PM") == PartialDate(year=2017, month=8, day=6) assert PartialDate.create("1997.12 12:00:00 PM") == PartialDate(year=1997, month=12, day=None) assert PartialDate.create("1997.12.31 12:00:00 PM") == PartialDate(year=1997, month=12, day=31) assert PartialDate.create("12.1997 12:00:00 PM") == PartialDate(year=1997, month=12, day=None) assert PartialDate.create("31.12.1997 12:00:00 PM") == PartialDate(year=1997, month=12, day=31) assert PartialDate.create("6.08.2017 12:00:00 PM") == PartialDate(year=2017, month=8, day=6) with pytest.raises(ModelException): PartialDate.create("ABC") pd = PartialDate(2003, 12, 31) assert pd.as_datetime() == datetime(2003, 12, 31) pd = PartialDate() assert str(pd) == "" def test_task_type_field(mocker): db = SqliteDatabase(":memory:") class TestModel(Model): tt = TaskTypeField(null=True) class Meta: database = db exception = mocker.patch.object(app.logger, "exception") TestModel.create_table() TestModel.create(tt=None) for v in TaskType: TestModel.create(tt=v) TestModel.create(tt=str(v.value)) TestModel.create(tt=v.value) TestModel.create(tt=v.name) TestModel.create(tt=dict()) exception.assert_called_once() res = {r[0]:r[1] for r in db.execute_sql( "SELECT tt, count(*) AS rc FROM testmodel GROUP BY tt ORDER BY 1").fetchall()} assert all(res[v.value] == 4 for v in TaskType) assert res[None] == 2 def test_pd_field(): db = SqliteDatabase(":memory:") class TestModel(Model): pf = PartialDateField(null=True) class Meta: database = db TestModel.create_table() TestModel(pf=PartialDate()).save() TestModel(pf=None).save() res = [r[0] for r in db.execute_sql("SELECT pf FROM testmodel").fetchall()] assert res[0] is None and res[1] is None TestModel(pf=PartialDate(1997)).save() TestModel(pf=PartialDate(1996, 4)).save() TestModel(pf=PartialDate(1995, 5, 13)).save() res = [r[0] for r in db.execute_sql("SELECT pf FROM testmodel").fetchall()] assert '1995-05-13' in res assert '1996-04-**' in res assert '1997-**-**' in res res = [r.pf for r in TestModel.select().order_by(TestModel.pf)] assert res[0] is None assert res[1] is None assert res[2] == PartialDate(1995, 5, 13) assert res[3] == PartialDate(1996, 4) assert res[4] == PartialDate(1997) def test_load_org_info_from_csv(models): # flake8: noqa OrgInfo.load_from_csv( """Organisation,Title,First Name,Last Name,Role,Email,Phone,Permission to post to web,Country Code,City of home campus,common:disambiguated-organization-identifier,common:disambiguation-source Organisation_0,Title_0,First Name_0,Last Name_0,Role_0,Email_0,Phone_0,Permission to post to web_0,Country Code_0,City of home campus_0,common:disambiguated-organization-identifier_0,common:disambiguation-source Organisation_1,Title_1,First Name_1,Last Name_1,Role_1,Email_1,Phone_1,yes,Country Code_1,City of home campus_1,common:disambiguated-organization-identifier_1,common:disambiguation-source """) assert OrgInfo.select().count() == 2 oi = OrgInfo.get(name="Organisation_1") assert oi.is_public OrgInfo.load_from_csv( StringIO("""Name,Disambiguated Id,Disambiguation Source AgResearch Ltd,3713,RINGGOLD Aqualinc Research Ltd,9429035717133,NZBN Ara Institute of Canterbury,6006,Education Organisation Number Auckland District Health Board,1387,RINGGOLD Auckland University of Technology,1410,RINGGOLD Bay of Plenty District Health Board,7854,RINGGOLD Capital and Coast District Health Board,8458,RINGGOLD Cawthron Institute,5732,RINGGOLD CRL Energy Ltd,9429038654381,NZBN Health Research Council,http://dx.doi.org/10.13039/501100001505,FUNDREF Hutt Valley District Health Board,161292,RINGGOLD Institute of Environmental Science and Research,8480,RINGGOLD Institute of Geological & Nuclear Sciences Ltd,5180,RINGGOLD """)) assert OrgInfo.select().count() == 15 def test_affiliations(models): assert Affiliation.EDU == "EDU" assert Affiliation.EMP == "EMP" assert Affiliation.EMP == Affiliation["EMP"] assert hash(Affiliation.EMP) == hash("EMP") assert str(Affiliation.EDU | Affiliation.EMP) == "Education, Employment" def test_field_is_updated(testdb): u = User.create(email="[email protected]", name="TESTER") u.save() u.save() assert not u.field_is_updated("name") u.name = "NEW VALUE" assert u.field_is_updated("name") def test_load_task_from_csv(models): org, _ = Organisation.get_or_create(name="TEST0") # flake8: noqa test = AffiliationRecord.load_from_csv( """First name Last name email address Organisation Campus/Department City Course or Job title Start date End date Student/Staff FNA LBA [email protected] TEST1 Research Funding Wellington Programme Manager - ORCID 2016-09 Staff FNA LBA [email protected] TEST1 Research Funding Wellington Programme Manager - Insights and Evaluation 2014 Staff FNA LBA [email protected] TEST0 External Affairs Wellington Senior Evaluation Officer 2011 2014 Staff FNA LBA [email protected] TEST0 Policy and Evaluation Wellington Evaluation Officer 2005 2011 Staff FNA LBA [email protected] TEST0 Marsden Fund Wellington Research Assessor 2001 2004 Staff FNB LNB [email protected] TEST1 Communications and Outreach Wellington Projects and Events Coordinator 2013 Staff FNB LNB [email protected] TEST0 Science and Education Group Wellington School Programmes Manager 2008 2013 Staff FNB LNB TEST_FN TEST_LN <[email protected]> TEST0 Science and Education Group Wellington Project Manager 2000 2004 Staff FNB LNB [email protected] TEST0 Science and Education Group Wellington Manager Special Programmes 2004 2008 Staff """, filename="TEST.tsv", org=org) assert test.record_count == 9 assert AffiliationRecord.select().count( ) == test.record_count + 10 # The 10 value is from already inserted entries. def test_work_task(models): org = Organisation.select().first() raw_data0 = readup_test_data("example_works.json", "r") data0 = load_yaml_json("test0001.json", raw_data0) assert isinstance(data0, list) and isinstance(data0[0], NestedDict) data0 = load_yaml_json(None, source=raw_data0, content_type="json") assert isinstance(data0, list) and isinstance(data0[0], NestedDict) data0 = load_yaml_json(None, source=raw_data0) assert isinstance(data0, list) and isinstance(data0[0], NestedDict) task0 = WorkRecord.load_from_json(filename="work0042.json", source=raw_data0, org=org) data = task0.to_export_dict() raw_data = json.dumps(data, cls=JSONEncoder) task = WorkRecord.load_from_json(filename="work0001.json", source=raw_data, org=org) export = task.to_export_dict() for a in ["id", "filename", "created-at", "updated-at"]: del(export[a]) del(data[a]) assert data == export def test_is_superuser(): su = User(roles=Role.SUPERUSER) assert su.is_superuser su.is_superuser = False assert not su.has_role(Role.SUPERUSER) u = User() assert not u.is_superuser assert not u.has_role(Role.SUPERUSER) u.is_superuser = True assert u.is_superuser assert u.has_role(Role.SUPERUSER) def test_validate_orcid_id(): assert validate_orcid_id(None) is None assert validate_orcid_id(0) is None assert validate_orcid_id("") is None assert validate_orcid_id("0000-0000-0000-00X3") == "0000-0000-0000-00X3" with pytest.raises(ValueError): validate_orcid_id("123") with pytest.raises(ValueError): validate_orcid_id("0000-0000-0000-00X4") def test_boolean_field(): class TestTableWithBooleanField(BaseModel): test_field = BooleanField() class Meta: database = SqliteDatabase(":memory:") TestTableWithBooleanField.create_table() TestTableWithBooleanField.create(test_field=True) assert TestTableWithBooleanField.select().where( TestTableWithBooleanField.test_field.NOT()).count() == 0 def test_base_model_to_dict(): """Test base model features.""" db = SqliteDatabase(":memory:") class TestTable(BaseModel): test_field = TextField() class Meta: database = db class Child(BaseModel): parent = ForeignKeyField(TestTable) class Meta: database = db TestTable.create_table() Child.create_table() parent = TestTable.create(test_field="ABC123") assert parent.to_dict() == {"id": 1, "test_field": "ABC123"} child = Child.create(parent=parent) parent = TestTable.get(parent.id) assert parent.to_dict(backrefs=True) == {"id": 1, "test_field": "ABC123", "child_set": [{"id": 1}]} rec = TestTable.get(1) assert rec.test_field == "ABC123" rec = TestTable.get() assert rec.test_field == "ABC123" def test_other_names(models): org = Organisation.get() raw_data0 = readup_test_data("othernames.json", "r") data0 = load_yaml_json("othernames000.json", raw_data0) assert isinstance(data0, list) and isinstance(data0[0], NestedDict) data0 = load_yaml_json(None, source=raw_data0, content_type="json") assert isinstance(data0, list) and isinstance(data0[0], NestedDict) data0 = load_yaml_json(None, source=raw_data0) assert isinstance(data0, list) and isinstance(data0[0], NestedDict) task0 = PropertyRecord.load_from_json(filename="othernames000.json", source=raw_data0, org=org, file_property_type="NAME") data = task0.to_dict(recurse=True) raw_data = json.dumps(data, cls=JSONEncoder) task = PropertyRecord.load_from_json(filename="othernames001.json", source=raw_data, org=org, file_property_type="NAME") assert len(data0) == len(task.to_dict(recurse=True)["records"]) def test_researcher_urls(models): org = Organisation.get() raw_data0 = readup_test_data("researchurls.json", "r") data0 = load_yaml_json("researchurls.json", raw_data0) assert isinstance(data0, list) and isinstance(data0[0], NestedDict) task0 = PropertyRecord.load_from_json(filename="researchurls000.json", source=raw_data0, org=org, file_property_type="URL") data = task0.to_dict(recurse=True) raw_data = json.dumps(data, cls=JSONEncoder) task = PropertyRecord.load_from_json(filename="researchurls001.json", source=raw_data, org=org, file_property_type="URL") assert len(data0) == len(task.to_dict(recurse=True)["records"]) def test_load_resources_from_csv(models): org = Organisation.get() raw_data = readup_test_data("resources.tsv", "r") task = ResourceRecord.load_from_csv(raw_data, filename="resources.tsv", org=org) assert task assert task.records.count() == 2
python
import striga.server.service import sqlobject ### class SQLObjectFactory(striga.server.service.ServiceFactory): def __init__(self, parent, name = 'SQLObjectFactory', startstoppriority = 50): striga.server.service.ServiceFactory.__init__(self, SQLObjectService, 'SQLObject', 'sqlobject', parent, name, startstoppriority) ### class SQLObjectService(striga.server.service.Service): def __init__(self, parent, name = 'SQLObject', startstoppriority = 50): striga.server.service.Service.__init__(self, parent, name, startstoppriority) self.Connection = None def _DoStart(self): self.Connection = sqlobject.connectionForURI(self.DBURI) if self.ToHub: sqlobject.sqlhub.processConnection = self.Connection def _DoStop(self): if self.ToHub: sqlobject.sqlhub.processConnection = None self.Connection = None def _configure(self, conffilename, dburi, tohub="1", model=None): tohub = int(tohub) self.DBURI = dburi self.ToHub = (tohub != 0) self._ChangeServiceStateToConfigured()
python
# Webhooks for external integrations. from zerver.lib.actions import check_send_stream_message from zerver.lib.response import json_success from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view from zerver.models import Client, UserProfile from django.http import HttpRequest, HttpResponse import pprint import ujson from typing import Dict, Any, Iterable, Optional, Text PAGER_DUTY_EVENT_NAMES = { 'incident.trigger': 'triggered', 'incident.acknowledge': 'acknowledged', 'incident.unacknowledge': 'unacknowledged', 'incident.resolve': 'resolved', 'incident.assign': 'assigned', 'incident.escalate': 'escalated', 'incident.delegate': 'delineated', } def build_pagerduty_formatdict(message): # type: (Dict[str, Any]) -> Dict[str, Any] # Normalize the message dict, after this all keys will exist. I would # rather some strange looking messages than dropping pages. format_dict = {} # type: Dict[str, Any] format_dict['action'] = PAGER_DUTY_EVENT_NAMES[message['type']] format_dict['incident_id'] = message['data']['incident']['id'] format_dict['incident_num'] = message['data']['incident']['incident_number'] format_dict['incident_url'] = message['data']['incident']['html_url'] format_dict['service_name'] = message['data']['incident']['service']['name'] format_dict['service_url'] = message['data']['incident']['service']['html_url'] # This key can be missing on null if message['data']['incident'].get('assigned_to_user', None): format_dict['assigned_to_email'] = message['data']['incident']['assigned_to_user']['email'] format_dict['assigned_to_username'] = message['data']['incident']['assigned_to_user']['email'].split('@')[0] format_dict['assigned_to_url'] = message['data']['incident']['assigned_to_user']['html_url'] else: format_dict['assigned_to_email'] = 'nobody' format_dict['assigned_to_username'] = 'nobody' format_dict['assigned_to_url'] = '' # This key can be missing on null if message['data']['incident'].get('resolved_by_user', None): format_dict['resolved_by_email'] = message['data']['incident']['resolved_by_user']['email'] format_dict['resolved_by_username'] = message['data']['incident']['resolved_by_user']['email'].split('@')[0] format_dict['resolved_by_url'] = message['data']['incident']['resolved_by_user']['html_url'] else: format_dict['resolved_by_email'] = 'nobody' format_dict['resolved_by_username'] = 'nobody' format_dict['resolved_by_url'] = '' trigger_message = [] trigger_subject = message['data']['incident']['trigger_summary_data'].get('subject', '') if trigger_subject: trigger_message.append(trigger_subject) trigger_description = message['data']['incident']['trigger_summary_data'].get('description', '') if trigger_description: trigger_message.append(trigger_description) format_dict['trigger_message'] = u'\n'.join(trigger_message) return format_dict def send_raw_pagerduty_json(user_profile, client, stream, message, topic): # type: (UserProfile, Client, Text, Dict[str, Any], Optional[Text]) -> None subject = topic or 'pagerduty' body = ( u'Unknown pagerduty message\n' u'```\n' u'%s\n' u'```') % (ujson.dumps(message, indent=2),) check_send_stream_message(user_profile, client, stream, subject, body) def send_formated_pagerduty(user_profile, client, stream, message_type, format_dict, topic): # type: (UserProfile, Client, Text, Text, Dict[str, Any], Optional[Text]) -> None if message_type in ('incident.trigger', 'incident.unacknowledge'): template = (u':imp: Incident ' u'[{incident_num}]({incident_url}) {action} by ' u'[{service_name}]({service_url}) and assigned to ' u'[{assigned_to_username}@]({assigned_to_url})\n\n>{trigger_message}') elif message_type == 'incident.resolve' and format_dict['resolved_by_url']: template = (u':grinning: Incident ' u'[{incident_num}]({incident_url}) resolved by ' u'[{resolved_by_username}@]({resolved_by_url})\n\n>{trigger_message}') elif message_type == 'incident.resolve' and not format_dict['resolved_by_url']: template = (u':grinning: Incident ' u'[{incident_num}]({incident_url}) resolved\n\n>{trigger_message}') else: template = (u':no_good: Incident [{incident_num}]({incident_url}) ' u'{action} by [{assigned_to_username}@]({assigned_to_url})\n\n>{trigger_message}') subject = topic or u'incident {incident_num}'.format(**format_dict) body = template.format(**format_dict) check_send_stream_message(user_profile, client, stream, subject, body) @api_key_only_webhook_view('PagerDuty') @has_request_variables def api_pagerduty_webhook(request, user_profile, payload=REQ(argument_type='body'), stream=REQ(default='pagerduty'), topic=REQ(default=None)): # type: (HttpRequest, UserProfile, Dict[str, Iterable[Dict[str, Any]]], Text, Optional[Text]) -> HttpResponse for message in payload['messages']: message_type = message['type'] if message_type not in PAGER_DUTY_EVENT_NAMES: send_raw_pagerduty_json(user_profile, request.client, stream, message, topic) try: format_dict = build_pagerduty_formatdict(message) except Exception: send_raw_pagerduty_json(user_profile, request.client, stream, message, topic) else: send_formated_pagerduty(user_profile, request.client, stream, message_type, format_dict, topic) return json_success()
python
from __future__ import division, print_function, absolute_import import imageio import numpy as np from tqdm import tqdm import warnings import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from matplotlib.image import imsave import matplotlib.patheffects as path_effects from matplotlib.colors import NoNorm from astropy import log from astropy import visualization from astropy.wcs import WCS from .surveyquery import getSVImg # Figure class class K2Fig(object): """Figure showing K2 target pixel stamp and sky survey image.""" def __init__(self,TPF): self.TPF = TPF self.verbose = self.TPF.verbose def cut_levels(self, min_percent=1., max_percent=95., data_col='FLUX'): """Determine the cut levels for contrast stretching. Returns ------- vmin, vmax : float, float Min and max cut levels. """ # Get co-added flux # update to use TPF sample = self.TPF.flux_binned() # Scale image with warnings.catch_warnings(): warnings.filterwarnings('ignore', message="(.*)invalid value(.*)") vmin, vmax = np.percentile(sample[sample > 0], [min_percent, max_percent]) return vmin, vmax # Set up the figure and axes using astropy WCS def create_figure(self, output_filename, survey, stretch='log', vmin=1, vmax=None, min_percent=1, max_percent=95, cmap='gray', contour_color='red', data_col='FLUX'): """Returns a matplotlib Figure object that visualizes a frame. Parameters ---------- vmin : float, optional Minimum cut level (default: 0). vmax : float, optional Maximum cut level (default: 5000). cmap : str, optional The matplotlib color map name. The default is 'gray', can also be e.g. 'gist_heat'. raw : boolean, optional If `True`, show the raw pixel counts rather than the calibrated flux. Default: `False`. Returns ------- image : array An array of unisgned integers of shape (x, y, 3), representing an RBG colour image x px wide and y px high. """ # Get the flux data to visualize # Update to use TPF flx = self.TPF.flux_binned() # print(np.shape(flx)) # calculate cut_levels if vmax is None: vmin, vmax = self.cut_levels(min_percent,max_percent,data_col) # Determine the figsize shape = list(flx.shape) # print(shape) # Create the figure and display the flux image using matshow fig = plt.figure(figsize=shape) # Display the image using matshow # Update to generate axes using WCS axes instead of plain axes ax = plt.subplot(projection=self.TPF.wcs) ax.set_xlabel('RA') ax.set_ylabel('Dec') if self.verbose: print('{} vmin/vmax = {}/{} (median={})'.format(data_col, vmin, vmax, np.nanmedian(flx))) if stretch == 'linear': stretch_fn = visualization.LinearStretch() elif stretch == 'sqrt': stretch_fn = visualization.SqrtStretch() elif stretch == 'power': stretch_fn = visualization.PowerStretch(1.0) elif stretch == 'log': stretch_fn = visualization.LogStretch() elif stretch == 'asinh': stretch_fn = visualization.AsinhStretch(0.1) else: raise ValueError('Unknown stretch: {0}.'.format(stretch)) transform = (stretch_fn + visualization.ManualInterval(vmin=vmin, vmax=vmax)) ax.imshow((255*transform(flx)).astype(int), aspect='auto', origin='lower', interpolation='nearest', cmap=cmap, norm=NoNorm()) ax.set_xticks([]) ax.set_yticks([]) current_ylims = ax.get_ylim() current_xlims = ax.get_xlim() pixels, header = getSVImg(self.TPF.position, survey) levels = np.linspace(np.min(pixels),np.percentile(pixels,95),10) ax.contour(pixels,transform=ax.get_transform(WCS(header)), levels=levels,colors=contour_color) ax.set_xlim(current_xlims) ax.set_ylim(current_ylims) fig.canvas.draw() plt.savefig(output_filename, bbox_inches='tight', dpi=300) return fig
python
import threading import time import pandas as pd import pandas.testing as tm class ParallelExperimentBase(object): @property def backend(self): raise NotImplementedError def test_serial(self, ex): a = ex.parameter('a') @ex.result def id(a): ex.save_metric(metric_key='a', epoch=0, value=a) return a res = id(a) def compute(x): ex.set_parameters(a=x) assert res.compute() == x for i in range(100): compute(i) hist = ex.get_history() exp = pd.DataFrame({'a': range(100), 'Result': range(100)}, index=pd.Index(range(1, 101), name='Trial ID'), columns=['a', 'Result']) tm.assert_frame_equal(hist[['a', 'Result']], exp) def test_task_parallel(self, ex): a = ex.parameter('a') @ex def long_task(a): time.sleep(0.5) return a @ex.result def result(a, b, c, d, e, f): return a + b + c + d + e + f res = result(long_task(a), long_task(a), long_task(a), long_task(a), long_task(a), long_task(a)) ex.set_parameters(a=1) # parallel start = time.time() assert res.compute() == 6 assert time.time() - start <= 2.5 def test_task_serial(self): # for comparison def long_task(a): time.sleep(0.5) return a def result(a, b, c, d, e, f): return a + b + c + d + e + f start = time.time() res = result(long_task(1), long_task(1), long_task(1), long_task(1), long_task(1), long_task(1)) assert res == 6 assert time.time() - start >= 3 def test_threading_lock(self, ex): a = ex.parameter('a') @ex.result def id(a): ex.save_metric(metric_key='a', epoch=0, value=a) return a res = id(a) lock = threading.Lock() def compute(x): with lock: # without lock, parameters may be updated # between set and compute # this test actually parallelize nothing ex.set_parameters(a=x) assert res.compute() == x threads = [] for i in range(100): thread = threading.Thread(target=compute, args=([i])) threads.append(thread) for t in threads: t.start() for t in threads: t.join() hist = ex.get_history() hist = ex.get_history() exp = pd.DataFrame({'a': range(100), 'Result': range(100)}, index=pd.Index(range(1, 101), name='Trial ID'), columns=['a', 'Result']) tm.assert_frame_equal(hist[['a', 'Result']], exp)
python
import unittest from AdvPythonTraining.Eight_Day.P1 import Person as PersonClass class POneTest(unittest.TestCase): persone = PersonClass() user_id = [] user_name =[] def test_set_name(self): for i in range(4): name = 'name' +str(i) self.user_name.append(name) user_id = self.persone.set_name(name) self.assertIsNotNone(user_id) self.user_id.append(user_id) print("finish test case") def test_get_name(self): length =len(self.user_id) for i in range(6): if i<length: self.assertEqual(self.user_name[i], self.persone.get_name(self.user_id[i])) else: self.assertEqual(' there is no such user ', self.persone.get_name[i]) if __name__ == "__main__": if __name__ == '__main__': unittest.main()
python
#!/usr/bin/env python # Copyright 2008 Rene Rivera # Distributed under the Boost Software License, Version 1.0. # (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) import re import optparse import time import xml.dom.minidom import xml.dom.pulldom from xml.sax.saxutils import unescape, escape import os.path from pprint import pprint from __builtin__ import exit class BuildOutputXMLParsing(object): ''' XML parsing utilities for dealing with the Boost Build output XML format. ''' def get_child_data( self, root, tag = None, id = None, name = None, strip = False, default = None ): return self.get_data(self.get_child(root,tag=tag,id=id,name=name),strip=strip,default=default) def get_data( self, node, strip = False, default = None ): data = None if node: data_node = None if not data_node: data_node = self.get_child(node,tag='#text') if not data_node: data_node = self.get_child(node,tag='#cdata-section') data = "" while data_node: data += data_node.data data_node = data_node.nextSibling if data_node: if data_node.nodeName != '#text' \ and data_node.nodeName != '#cdata-section': data_node = None if not data: data = default else: if strip: data = data.strip() return data def get_child( self, root, tag = None, id = None, name = None, type = None ): return self.get_sibling(root.firstChild,tag=tag,id=id,name=name,type=type) def get_sibling( self, sibling, tag = None, id = None, name = None, type = None ): n = sibling while n: found = True if type and found: found = found and type == n.nodeType if tag and found: found = found and tag == n.nodeName if (id or name) and found: found = found and n.nodeType == xml.dom.Node.ELEMENT_NODE if id and found: if n.hasAttribute('id'): found = found and n.getAttribute('id') == id else: found = found and n.hasAttribute('id') and n.getAttribute('id') == id if name and found: found = found and n.hasAttribute('name') and n.getAttribute('name') == name if found: return n n = n.nextSibling return None class BuildOutputProcessor(BuildOutputXMLParsing): def __init__(self, inputs): self.test = {} self.target_to_test = {} self.target = {} self.parent = {} self.timestamps = [] for input in inputs: self.add_input(input) def add_input(self, input): ''' Add a single build XML output file to our data. ''' events = xml.dom.pulldom.parse(input) context = [] for (event,node) in events: if event == xml.dom.pulldom.START_ELEMENT: context.append(node) if node.nodeType == xml.dom.Node.ELEMENT_NODE: x_f = self.x_name_(*context) if x_f: events.expandNode(node) # expanding eats the end element, hence walking us out one level context.pop() # call handler (x_f[1])(node) elif event == xml.dom.pulldom.END_ELEMENT: context.pop() def x_name_(self, *context, **kwargs): node = None names = [ ] for c in context: if c: if not isinstance(c,xml.dom.Node): suffix = '_'+c.replace('-','_').replace('#','_') else: suffix = '_'+c.nodeName.replace('-','_').replace('#','_') node = c names.append('x') names = map(lambda x: x+suffix,names) if node: for name in names: if hasattr(self,name): return (name,getattr(self,name)) return None def x_build_test(self, node): ''' Records the initial test information that will eventually get expanded as we process the rest of the results. ''' test_node = node test_name = test_node.getAttribute('name') test_target = self.get_child_data(test_node,tag='target',strip=True) ## print ">>> %s %s" %(test_name,test_target) self.test[test_name] = { 'library' : "/".join(test_name.split('/')[0:-1]), 'test-name' : test_name.split('/')[-1], 'test-type' : test_node.getAttribute('type').lower(), 'test-program' : self.get_child_data(test_node,tag='source',strip=True), 'target' : test_target, 'info' : self.get_child_data(test_node,tag='info',strip=True), 'dependencies' : [], 'actions' : [], } # Add a lookup for the test given the test target. self.target_to_test[self.test[test_name]['target']] = test_name return None def x_build_targets_target( self, node ): ''' Process the target dependency DAG into an ancestry tree so we can look up which top-level library and test targets specific build actions correspond to. ''' target_node = node name = self.get_child_data(target_node,tag='name',strip=True) path = self.get_child_data(target_node,tag='path',strip=True) jam_target = self.get_child_data(target_node,tag='jam-target',strip=True) #~ Map for jam targets to virtual targets. self.target[jam_target] = { 'name' : name, 'path' : path } #~ Create the ancestry. dep_node = self.get_child(self.get_child(target_node,tag='dependencies'),tag='dependency') while dep_node: child = self.get_data(dep_node,strip=True) child_jam_target = '<p%s>%s' % (path,child.split('//',1)[1]) self.parent[child_jam_target] = jam_target dep_node = self.get_sibling(dep_node.nextSibling,tag='dependency') return None def x_build_action( self, node ): ''' Given a build action log, process into the corresponding test log and specific test log sub-part. ''' action_node = node name = self.get_child(action_node,tag='name') if name: name = self.get_data(name) #~ Based on the action, we decide what sub-section the log #~ should go into. action_type = None if re.match('[^%]+%[^.]+[.](compile)',name): action_type = 'compile' elif re.match('[^%]+%[^.]+[.](link|archive)',name): action_type = 'link' elif re.match('[^%]+%testing[.](capture-output)',name): action_type = 'run' elif re.match('[^%]+%testing[.](expect-failure|expect-success)',name): action_type = 'result' else: # TODO: Enable to see what other actions can be included in the test results. # action_type = None action_type = 'other' #~ print "+ [%s] %s %s :: %s" %(action_type,name,'','') if action_type: #~ Get the corresponding test. (target,test) = self.get_test(action_node,type=action_type) #~ Skip action that have no corresponding test as they are #~ regular build actions and don't need to show up in the #~ regression results. if not test: ##print "??? [%s] %s %s :: %s" %(action_type,name,target,test) return None ##print "+++ [%s] %s %s :: %s" %(action_type,name,target,test) #~ Collect some basic info about the action. action = { 'command' : self.get_action_command(action_node,action_type), 'output' : self.get_action_output(action_node,action_type), 'info' : self.get_action_info(action_node,action_type) } #~ For the test result status we find the appropriate node #~ based on the type of test. Then adjust the result status #~ accordingly. This makes the result status reflect the #~ expectation as the result pages post processing does not #~ account for this inversion. action['type'] = action_type if action_type == 'result': if re.match(r'^compile',test['test-type']): action['type'] = 'compile' elif re.match(r'^link',test['test-type']): action['type'] = 'link' elif re.match(r'^run',test['test-type']): action['type'] = 'run' #~ The result sub-part we will add this result to. if action_node.getAttribute('status') == '0': action['result'] = 'succeed' else: action['result'] = 'fail' # Add the action to the test. test['actions'].append(action) # Set the test result if this is the result action for the test. if action_type == 'result': test['result'] = action['result'] return None def x_build_timestamp( self, node ): ''' The time-stamp goes to the corresponding attribute in the result. ''' self.timestamps.append(self.get_data(node).strip()) return None def get_test( self, node, type = None ): ''' Find the test corresponding to an action. For testing targets these are the ones pre-declared in the --dump-test option. For libraries we create a dummy test as needed. ''' jam_target = self.get_child_data(node,tag='jam-target') base = self.target[jam_target]['name'] target = jam_target while target in self.parent: target = self.parent[target] #~ print "--- TEST: %s ==> %s" %(jam_target,target) #~ main-target-type is a precise indicator of what the build target is #~ originally meant to be. #main_type = self.get_child_data(self.get_child(node,tag='properties'), # name='main-target-type',strip=True) main_type = None if main_type == 'LIB' and type: lib = self.target[target]['name'] if not lib in self.test: self.test[lib] = { 'library' : re.search(r'libs/([^/]+)',lib).group(1), 'test-name' : os.path.basename(lib), 'test-type' : 'lib', 'test-program' : os.path.basename(lib), 'target' : lib } test = self.test[lib] else: target_name_ = self.target[target]['name'] if self.target_to_test.has_key(target_name_): test = self.test[self.target_to_test[target_name_]] else: test = None return (base,test) #~ The command executed for the action. For run actions we omit the command #~ as it's just noise. def get_action_command( self, action_node, action_type ): if action_type != 'run': return self.get_child_data(action_node,tag='command') else: return '' #~ The command output. def get_action_output( self, action_node, action_type ): return self.get_child_data(action_node,tag='output',default='') #~ Some basic info about the action. def get_action_info( self, action_node, action_type ): info = {} #~ The jam action and target. info['name'] = self.get_child_data(action_node,tag='name') info['path'] = self.get_child_data(action_node,tag='path') #~ The timing of the action. info['time-start'] = action_node.getAttribute('start') info['time-end'] = action_node.getAttribute('end') info['time-user'] = action_node.getAttribute('user') info['time-system'] = action_node.getAttribute('system') #~ Testing properties. test_info_prop = self.get_child_data(self.get_child(action_node,tag='properties'),name='test-info') info['always_show_run_output'] = test_info_prop == 'always_show_run_output' #~ And for compiles some context that may be hidden if using response files. if action_type == 'compile': info['define'] = [] define = self.get_child(self.get_child(action_node,tag='properties'),name='define') while define: info['define'].append(self.get_data(define,strip=True)) define = self.get_sibling(define.nextSibling,name='define') return info class BuildConsoleSummaryReport(object): HEADER = '\033[35m\033[1m' INFO = '\033[34m' OK = '\033[32m' WARNING = '\033[33m' FAIL = '\033[31m' ENDC = '\033[0m' def __init__(self, bop, opt): self.bop = bop def generate(self): self.summary_info = { 'total' : 0, 'success' : 0, 'failed' : [], } self.header_print("======================================================================") self.print_test_log() self.print_summary() self.header_print("======================================================================") @property def failed(self): return len(self.summary_info['failed']) > 0 def print_test_log(self): self.header_print("Tests run..") self.header_print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") for k in sorted(self.bop.test.keys()): test = self.bop.test[k] if len(test['actions']) > 0: self.summary_info['total'] += 1 ##print ">>>> {0}".format(test['test-name']) if 'result' in test: succeed = test['result'] == 'succeed' else: succeed = test['actions'][-1]['result'] == 'succeed' if succeed: self.summary_info['success'] += 1 else: self.summary_info['failed'].append(test) if succeed: self.ok_print("[PASS] {0}",k) else: self.fail_print("[FAIL] {0}",k) for action in test['actions']: self.print_action(succeed, action) def print_action(self, test_succeed, action): ''' Print the detailed info of failed or always print tests. ''' #self.info_print(">>> {0}",action.keys()) if not test_succeed or action['info']['always_show_run_output']: output = action['output'].strip() if output != "": p = self.fail_print if action['result'] == 'fail' else self.p_print self.info_print("") self.info_print("({0}) {1}",action['info']['name'],action['info']['path']) p("") p("{0}",action['command'].strip()) p("") for line in output.splitlines(): p("{0}",line.encode('utf-8')) def print_summary(self): self.header_print("") self.header_print("Testing summary..") self.header_print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") self.p_print("Total: {0}",self.summary_info['total']) self.p_print("Success: {0}",self.summary_info['success']) if self.failed: self.fail_print("Failed: {0}",len(self.summary_info['failed'])) for test in self.summary_info['failed']: self.fail_print(" {0}/{1}",test['library'],test['test-name']) def p_print(self, format, *args, **kargs): print format.format(*args,**kargs) def info_print(self, format, *args, **kargs): print self.INFO+format.format(*args,**kargs)+self.ENDC def header_print(self, format, *args, **kargs): print self.HEADER+format.format(*args,**kargs)+self.ENDC def ok_print(self, format, *args, **kargs): print self.OK+format.format(*args,**kargs)+self.ENDC def warn_print(self, format, *args, **kargs): print self.WARNING+format.format(*args,**kargs)+self.ENDC def fail_print(self, format, *args, **kargs): print self.FAIL+format.format(*args,**kargs)+self.ENDC class Main(object): def __init__(self,args=None): op = optparse.OptionParser( usage="%prog [options] input+") op.add_option( '--output', help="type of output to generate" ) ( opt, inputs ) = op.parse_args(args) bop = BuildOutputProcessor(inputs) output = None if opt.output == 'console': output = BuildConsoleSummaryReport(bop, opt) if output: output.generate() self.failed = output.failed if __name__ == '__main__': m = Main() if m.failed: exit(-1)
python
# Copyright 2015 Sanghack Lee # # 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 logging import shlee.RCDLight import shlee.RCDLight from causality.citest.CITest import Oracle from causality.dseparation.AbstractGroundGraph import AbstractGroundGraph from causality.learning import ModelEvaluation from causality.learning.RCD import RCD, SchemaDependencyWrapper from causality.model.RelationalDependency import RelationalVariable logger = logging.getLogger(__name__) logging.basicConfig(level=logging.ERROR) # Parameters schema, model = shlee.RCDLight.incompleteness_example() logger.info('Model: %s', model.dependencies) hopThreshold = max(len(d.relVar1.path) + 1 for d in model.dependencies) oracle = Oracle(model, 3 * hopThreshold) rcd = RCD(schema, oracle, hopThreshold, depth=2) rcd.identifyUndirectedDependencies() rcd.orientDependencies() print('Skeleton precision:', ModelEvaluation.skeletonPrecision(model, rcd.undirectedDependencies)) print('Skeleton recall:', ModelEvaluation.skeletonRecall(model, rcd.undirectedDependencies)) precision = ModelEvaluation.orientedPrecision(model, rcd.orientedDependencies) print('Oriented precision:', precision) print('Oriented recall:', ModelEvaluation.orientedRecall(model, rcd.orientedDependencies)) rcdl = shlee.RCDLight.RCDLight(schema, oracle, hopThreshold) rcdl.identifyUndirectedDependencies() rcdl.orientDependencies() print('Skeleton precision:', ModelEvaluation.skeletonPrecision(model, rcdl.undirectedDependencies)) print('Skeleton recall:', ModelEvaluation.skeletonRecall(model, rcdl.undirectedDependencies)) precision = ModelEvaluation.orientedPrecision(model, rcdl.orientedDependencies) print('Oriented precision:', precision) print('Oriented recall:', ModelEvaluation.orientedRecall(model, rcdl.orientedDependencies)) assert ModelEvaluation.orientedRecall(model, rcdl.orientedDependencies) == \ ModelEvaluation.orientedRecall(model, rcd.orientedDependencies) == \ 0.0 # Demonstrate that there is no 'unshielded triple' in AGGs for the counter-example. schema, model = shlee.RCDLight.incompleteness_example() hopThreshold = max(len(d.relVar1.path) + 1 for d in model.dependencies) oracle = Oracle(model, 3 * hopThreshold) schemaDepWrapper = SchemaDependencyWrapper(schema, model.dependencies) perspectives = [si.name for si in schema.getSchemaItems()] perspectiveToAgg = {perspective: AbstractGroundGraph(schemaDepWrapper, perspective, 3 * hopThreshold) for perspective in perspectives} for agg in perspectiveToAgg.values(): for node1 in agg.nodes(): neighbors1 = set(agg.predecessors(node1) + agg.successors(node1)) for node2 in neighbors1: neighbors2 = set(agg.predecessors(node2) + agg.successors(node2)) - {node1} for node3 in neighbors2: if node3 not in neighbors1: if not isinstance(node1, RelationalVariable) or not isinstance(node2, RelationalVariable) or \ not isinstance(node3, RelationalVariable): continue print(node1, node2, node3) assert False # There is no 'unshielded triple' in AGGs
python
#!/usr/bin/env python # Copyright (C) 2010 # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. ########## import optparse import os import sys try: import socks NO_SOCKS = False except ImportError: NO_SOCKS = True import socket try: from bs4 import BeautifulSoup HAVE_SOUP = True except ImportError: HAVE_SOUP = False ########## from parsers.thread import SiteParserThread from util import fixFormatting, isImageLibAvailable from xmlparser import MangaXmlParser from outputManager.progressBarManager import progressBarManager ########## VERSION = 'v0.8.8' siteDict = { '' : '[mf]', '1' : '[mf]', '2' : '[mr]', '3' : '[mp]', '4' : '[mh]', '5' : '[em]', } if HAVE_SOUP: siteDict['6'] = '[bt]' ########## class InvalidSite(Exception): pass def printLicenseInfo(): print( "\nProgram: Copyright (c) 2010. GPL v3 (http://www.gnu.org/licenses/gpl.html)." ) print( "Icon: Copyright (c) 2006. GNU Free Document License v1.2 (Author:Kasuga)." ) print( " http://ja.wikipedia.org/wiki/%E5%88%A9%E7%94%A8%E8%80%85:Kasuga\n" ) ########## def main(): printLicenseInfo() # for easier parsing, adds free --help and --version # optparse (v2.3-v2.7) was chosen over argparse (v2.7+) for compatibility (and relative similarity) reasons # and over getopt(v?) for additional functionality parser = optparse.OptionParser( usage='usage: %prog [options] <manga name>', version=('Manga Downloader %s' % VERSION) ) parser.set_defaults( all_chapters_FLAG = False, auto = False, conversion_FLAG = False, convert_Directory = False, device = 'Kindle 3', downloadFormat = '.cbz', downloadPath = 'DEFAULT_VALUE', inputDir = None, outputDir = 'DEFAULT_VALUE', overwrite_FLAG = False, verbose_FLAG = False, timeLogging_FLAG = False, maxChapterThreads = 3, useShortName = False, spaceToken = '.', proxy = None ) parser.add_option( '--all', action = 'store_true', dest = 'all_chapters_FLAG', help = 'Download all available chapters.' ) parser.add_option( '-d', '--directory', dest = 'downloadPath', help = 'The destination download directory. Defaults to the directory of the script.' ) parser.add_option( '--overwrite', action = 'store_true', dest = 'overwrite_FLAG', help = 'Overwrites previous copies of downloaded chapters.' ) parser.add_option( '--verbose', action = 'store_true', dest = 'verbose_FLAG', help = 'Verbose Output.' ) parser.add_option( '-x','--xml', dest = 'xmlfile_path', help = 'Parses the .xml file and downloads all chapters newer than the last chapter downloaded for the listed mangas.' ) parser.add_option( '-c', '--convertFiles', action = 'store_true', dest = 'conversion_FLAG', help = 'Converts downloaded files to a Format/Size acceptable to the device specified by the --device parameter.' ) parser.add_option( '--device', dest = 'device', help = 'Specifies the conversion device. Omitting this option default to %default.' ) parser.add_option( '--convertDirectory', action = 'store_true', dest = 'convert_Directory', help = 'Converts the image files stored in the directory specified by --inputDirectory. Stores the converted images in the directory specified by --outputDirectory' ) parser.add_option( '--inputDirectory', dest = 'inputDir', help = 'The directory containing the images to convert when --convertDirectory is specified.' ) parser.add_option( '--outputDirectory', dest = 'outputDir', help = 'The directory to store the images when --convertDirectory is specified.' ) parser.add_option( '-z', '--zip', action = 'store_const', dest = 'downloadFormat', const = '.zip', help = 'Downloads using .zip compression. Omitting this option defaults to %default.' ) parser.add_option( '-t', '--threads', dest = 'maxChapterThreads', help = 'Limits the number of chapter threads to the value specified.' ) parser.add_option( '--timeLogging', action = 'store_true', dest = 'timeLogging_FLAG', help = 'Output time logging.' ) parser.add_option( '--useShortName', action = 'store_true', dest = 'useShortName_FLAG', help = 'To support devices that limit the size of the filename, this parameter uses a short name' ) parser.add_option( '--spaceToken', dest = 'spaceToken', help = 'Specifies the character used to replace spaces in the manga name.' ) parser.add_option( '--proxy', dest = 'proxy', help = 'Specifies the proxy.' ) (options, args) = parser.parse_args() try: options.maxChapterThreads = int(options.maxChapterThreads) except: options.maxChapterThreads = 2 if (options.maxChapterThreads <= 0): options.maxChapterThreads = 2; if(len(args) == 0 and ( not (options.convert_Directory or options.xmlfile_path != None) )): parser.error('Manga not specified.') #if(len(args) > 1): # parser.error('Possible multiple mangas specified, please select one. (Did you forget to put quotes around a multi-word manga?)') SetDownloadPathToName_Flag = False SetOutputPathToDefault_Flag = False if(len(args) > 0): # Default Directory is the ./MangaName if (options.downloadPath == 'DEFAULT_VALUE'): SetDownloadPathToName_Flag = True # Default outputDir is the ./MangaName if (options.outputDir == 'DEFAULT_VALUE'): SetOutputPathToDefault_Flag = True PILAvailable = isImageLibAvailable() # Check if PIL Library is available if either of convert Flags are set if ((not PILAvailable) and (options.convert_Directory or options.conversion_FLAG)): print ("\nConversion Functionality Not available.\nMust install the PIL (Python Image Library)") sys.exit() else: if (PILAvailable): from ConvertPackage.ConvertFile import convertFile if (options.convert_Directory): options.inputDir = os.path.abspath(options.inputDir) # Changes the working directory to the script location if (os.path.dirname(sys.argv[0]) != ""): os.chdir(os.path.dirname(sys.argv[0])) options.outputMgr = progressBarManager() options.outputMgr.start() try: if (options.convert_Directory): if ( options.outputDir == 'DEFAULT_VALUE' ): options.outputDir = '.' print("Converting Files: %s" % options.inputDir) convertFile.convert(options.outputMgr, options.inputDir, options.outputDir, options.device, options.verbose_FLAG) elif options.xmlfile_path != None: xmlParser = MangaXmlParser(options) xmlParser.downloadManga() else: threadPool = [] for manga in args: print( manga ) options.manga = manga if SetDownloadPathToName_Flag: options.downloadPath = ('./' + fixFormatting(options.manga, options.spaceToken)) if SetOutputPathToDefault_Flag: options.outputDir = options.downloadPath options.downloadPath = os.path.realpath(options.downloadPath) + os.sep # site selection if HAVE_SOUP: print('\nWhich site?\n(1) MangaFox\n(2) MangaReader\n(3) MangaPanda\n(4) MangaHere\n(5) EatManga\n(6) Batoto\n') else: print('\nWhich site?\n(1) MangaFox\n(2) MangaReader\n(3) MangaPanda\n(4) MangaHere\n(5) EatManga\n') # Python3 fix - removal of raw_input() try: site = raw_input() except NameError: site = input() try: options.site = siteDict[site] except KeyError: raise InvalidSite('Site selection invalid.') threadPool.append(SiteParserThread(options, None, None)) for thread in threadPool: thread.start() thread.join() finally: # Must always stop the manager options.outputMgr.stop() if __name__ == '__main__': main()
python
#!/usr/bin/env python3 """ Copyright 2020 The Magma Authors. This source code is licensed under the BSD-style license found in the LICENSE file in the root directory of this source tree. 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 textwrap from datetime import datetime from dateutil import tz class RegistrationSuccessRate: def __init__(self, attach_requests, attach_accepts): self.attach_requests = attach_requests self.attach_accepts = attach_accepts @property def rate(self): if self.attach_requests == 0: return float('Inf') return 100. * self.attach_accepts / self.attach_requests def __str__(self): rate = str(self.rate) + '%' if self.attach_requests != 0 \ else 'No Attach requests' return '{} ({} requests, {} accepted)'.format( rate, self.attach_requests, self.attach_accepts, ) class CoreDumps: def __init__(self, core_dump_files): self.core_dump_files = core_dump_files @property def earliest(self): timestamps = [int(f.split('-')[1]) for f in self.core_dump_files] if not timestamps: return '-' return datetime.utcfromtimestamp(min(timestamps))\ .replace(tzinfo=tz.tzutc())\ .astimezone(tz=tz.tzlocal())\ .strftime('%Y-%m-%d %H:%M:%S') @property def latest(self): timestamps = [int(f.split('-')[1]) for f in self.core_dump_files] if not timestamps: return None return datetime.utcfromtimestamp(max(timestamps))\ .replace(tzinfo=tz.tzutc())\ .astimezone(tz=tz.tzlocal())\ .strftime('%Y-%m-%d %H:%M:%S') def __len__(self): return len(self.core_dump_files) def __str__(self): return '#Core dumps: {} from: {} to: {}'.format( len(self.core_dump_files), self.earliest, self.latest, ) class AGWHealthSummary: def __init__(self, hss_relay_enabled, nb_enbs_connected, allocated_ips, subscriber_table, core_dumps, registration_success_rate): self.hss_relay_enabled = hss_relay_enabled self.nb_enbs_connected = nb_enbs_connected self.allocated_ips = allocated_ips self.subscriber_table = subscriber_table self.core_dumps = core_dumps self.registration_success_rate = registration_success_rate def __str__(self): return textwrap.dedent(""" {} #eNBs connected: {} \t (run `enodebd_cli.py get_all_status` for more details) #IPs allocated: {} \t (run `mobility_cli.py list_allocated_ips` for more details) #UEs connected: {} \t (run `mobility_cli.py get_subscriber_table` for more details) #Core dumps: {} \t (run `ls /tmp/` to see core dumps) Earliest core-dump: {}, Latest core-dump: {} Registration success rate: {} """).format( 'Using Feg' if self.hss_relay_enabled else 'Using subscriberdb', self.nb_enbs_connected, len(self.allocated_ips), len(self.subscriber_table), len(self.core_dumps), self.core_dumps.earliest, self.core_dumps.latest, self.registration_success_rate, )
python
# Generated by Django 2.2.3 on 2019-07-22 11:45 import core.model_fields import core.validators from django.db import migrations, models import django.db.models.deletion import great_international.panels.great_international import modelcluster.fields class Migration(migrations.Migration): dependencies = [ ('wagtailcore', '0041_group_collection_permissions_verbose_name_plural'), ('wagtailimages', '0001_squashed_0021'), ('great_international', '0050_investhighpotentialopportunitiespage'), ] operations = [ migrations.CreateModel( name='AboutUkWhyChooseTheUkPage', fields=[ ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), ('service_name', models.CharField(choices=[('FIND_A_SUPPLIER', 'Find a Supplier'), ('EXPORT_READINESS', 'Export Readiness'), ('INVEST', 'Invest'), ('COMPONENTS', 'Components'), ('GREAT_INTERNATIONAL', 'Great International')], db_index=True, max_length=100, null=True)), ('uses_tree_based_routing', models.BooleanField(default=False, help_text="Allow this page's URL to be determined by its slug, and the slugs of its ancestors in the page tree.", verbose_name='tree-based routing enabled')), ('breadcrumbs_label', models.CharField(max_length=255)), ('breadcrumbs_label_en_gb', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_de', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_ja', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_zh_hans', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_fr', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_es', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_pt', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_ar', models.CharField(max_length=255, null=True)), ('hero_title', models.CharField(max_length=255)), ('hero_title_en_gb', models.CharField(max_length=255, null=True)), ('hero_title_de', models.CharField(max_length=255, null=True)), ('hero_title_ja', models.CharField(max_length=255, null=True)), ('hero_title_zh_hans', models.CharField(max_length=255, null=True)), ('hero_title_fr', models.CharField(max_length=255, null=True)), ('hero_title_es', models.CharField(max_length=255, null=True)), ('hero_title_pt', models.CharField(max_length=255, null=True)), ('hero_title_ar', models.CharField(max_length=255, null=True)), ('teaser', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='')), ('teaser_en_gb', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='')), ('teaser_de', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='')), ('teaser_ja', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='')), ('teaser_zh_hans', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='')), ('teaser_fr', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='')), ('teaser_es', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='')), ('teaser_pt', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='')), ('teaser_ar', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='')), ('section_one_body', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks])), ('section_one_body_en_gb', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks])), ('section_one_body_de', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks])), ('section_one_body_ja', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks])), ('section_one_body_zh_hans', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks])), ('section_one_body_fr', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks])), ('section_one_body_es', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks])), ('section_one_body_pt', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks])), ('section_one_body_ar', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks])), ('statistic_1_number', models.CharField(blank=True, max_length=255)), ('statistic_1_number_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_number_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_number_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_number_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_number_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_number_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_number_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_number_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_heading', models.CharField(blank=True, max_length=255)), ('statistic_1_heading_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_heading_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_heading_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_heading_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_heading_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_heading_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_heading_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_heading_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_smallprint', models.CharField(blank=True, max_length=255)), ('statistic_1_smallprint_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_smallprint_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_smallprint_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_smallprint_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_smallprint_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_smallprint_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_smallprint_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_1_smallprint_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_number', models.CharField(blank=True, max_length=255)), ('statistic_2_number_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_number_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_number_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_number_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_number_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_number_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_number_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_number_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_heading', models.CharField(blank=True, max_length=255)), ('statistic_2_heading_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_heading_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_heading_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_heading_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_heading_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_heading_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_heading_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_heading_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_smallprint', models.CharField(blank=True, max_length=255)), ('statistic_2_smallprint_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_smallprint_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_smallprint_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_smallprint_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_smallprint_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_smallprint_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_smallprint_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_2_smallprint_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_number', models.CharField(blank=True, max_length=255)), ('statistic_3_number_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_number_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_number_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_number_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_number_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_number_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_number_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_number_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_heading', models.CharField(blank=True, max_length=255)), ('statistic_3_heading_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_heading_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_heading_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_heading_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_heading_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_heading_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_heading_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_heading_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_smallprint', models.CharField(blank=True, max_length=255)), ('statistic_3_smallprint_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_smallprint_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_smallprint_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_smallprint_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_smallprint_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_smallprint_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_smallprint_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_3_smallprint_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_number', models.CharField(blank=True, max_length=255)), ('statistic_4_number_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_number_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_number_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_number_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_number_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_number_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_number_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_number_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_heading', models.CharField(blank=True, max_length=255)), ('statistic_4_heading_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_heading_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_heading_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_heading_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_heading_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_heading_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_heading_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_heading_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_smallprint', models.CharField(blank=True, max_length=255)), ('statistic_4_smallprint_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_smallprint_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_smallprint_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_smallprint_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_smallprint_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_smallprint_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_smallprint_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_4_smallprint_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_number', models.CharField(blank=True, max_length=255)), ('statistic_5_number_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_number_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_number_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_number_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_number_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_number_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_number_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_number_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_heading', models.CharField(blank=True, max_length=255)), ('statistic_5_heading_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_heading_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_heading_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_heading_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_heading_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_heading_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_heading_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_heading_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_smallprint', models.CharField(blank=True, max_length=255)), ('statistic_5_smallprint_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_smallprint_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_smallprint_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_smallprint_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_smallprint_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_smallprint_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_smallprint_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_5_smallprint_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_number', models.CharField(blank=True, max_length=255)), ('statistic_6_number_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_number_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_number_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_number_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_number_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_number_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_number_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_number_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_heading', models.CharField(blank=True, max_length=255)), ('statistic_6_heading_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_heading_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_heading_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_heading_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_heading_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_heading_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_heading_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_heading_ar', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_smallprint', models.CharField(blank=True, max_length=255)), ('statistic_6_smallprint_en_gb', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_smallprint_de', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_smallprint_ja', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_smallprint_zh_hans', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_smallprint_fr', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_smallprint_es', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_smallprint_pt', models.CharField(blank=True, max_length=255, null=True)), ('statistic_6_smallprint_ar', models.CharField(blank=True, max_length=255, null=True)), ('contact_us_section_title', models.CharField(blank=True, max_length=255, verbose_name='Title')), ('contact_us_section_title_en_gb', models.CharField(blank=True, max_length=255, null=True, verbose_name='Title')), ('contact_us_section_title_de', models.CharField(blank=True, max_length=255, null=True, verbose_name='Title')), ('contact_us_section_title_ja', models.CharField(blank=True, max_length=255, null=True, verbose_name='Title')), ('contact_us_section_title_zh_hans', models.CharField(blank=True, max_length=255, null=True, verbose_name='Title')), ('contact_us_section_title_fr', models.CharField(blank=True, max_length=255, null=True, verbose_name='Title')), ('contact_us_section_title_es', models.CharField(blank=True, max_length=255, null=True, verbose_name='Title')), ('contact_us_section_title_pt', models.CharField(blank=True, max_length=255, null=True, verbose_name='Title')), ('contact_us_section_title_ar', models.CharField(blank=True, max_length=255, null=True, verbose_name='Title')), ('contact_us_section_summary', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='Summary')), ('contact_us_section_summary_en_gb', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='Summary')), ('contact_us_section_summary_de', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='Summary')), ('contact_us_section_summary_ja', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='Summary')), ('contact_us_section_summary_zh_hans', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='Summary')), ('contact_us_section_summary_fr', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='Summary')), ('contact_us_section_summary_es', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='Summary')), ('contact_us_section_summary_pt', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='Summary')), ('contact_us_section_summary_ar', core.model_fields.MarkdownField(blank=True, null=True, validators=[core.validators.slug_hyperlinks], verbose_name='Summary')), ('contact_us_section_cta_text', models.CharField(blank=True, max_length=255, verbose_name='CTA text')), ('contact_us_section_cta_text_en_gb', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA text')), ('contact_us_section_cta_text_de', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA text')), ('contact_us_section_cta_text_ja', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA text')), ('contact_us_section_cta_text_zh_hans', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA text')), ('contact_us_section_cta_text_fr', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA text')), ('contact_us_section_cta_text_es', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA text')), ('contact_us_section_cta_text_pt', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA text')), ('contact_us_section_cta_text_ar', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA text')), ('contact_us_section_cta_link', models.CharField(blank=True, max_length=255, verbose_name='CTA URL')), ('contact_us_section_cta_link_en_gb', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA URL')), ('contact_us_section_cta_link_de', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA URL')), ('contact_us_section_cta_link_ja', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA URL')), ('contact_us_section_cta_link_zh_hans', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA URL')), ('contact_us_section_cta_link_fr', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA URL')), ('contact_us_section_cta_link_es', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA URL')), ('contact_us_section_cta_link_pt', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA URL')), ('contact_us_section_cta_link_ar', models.CharField(blank=True, max_length=255, null=True, verbose_name='CTA URL')), ('hero_image', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_ar', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_de', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_en_gb', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_es', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_fr', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_ja', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_pt', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_zh_hans', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('section_one_image', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('section_one_image_ar', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('section_one_image_de', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('section_one_image_en_gb', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('section_one_image_es', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('section_one_image_fr', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('section_one_image_ja', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('section_one_image_pt', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('section_one_image_zh_hans', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ], options={ 'abstract': False, }, bases=('wagtailcore.page', great_international.panels.great_international.AboutUkWhyChooseTheUkPagePanels), ), migrations.CreateModel( name='AboutUkLandingPage', fields=[ ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), ('service_name', models.CharField(choices=[('FIND_A_SUPPLIER', 'Find a Supplier'), ('EXPORT_READINESS', 'Export Readiness'), ('INVEST', 'Invest'), ('COMPONENTS', 'Components'), ('GREAT_INTERNATIONAL', 'Great International')], db_index=True, max_length=100, null=True)), ('uses_tree_based_routing', models.BooleanField(default=False, help_text="Allow this page's URL to be determined by its slug, and the slugs of its ancestors in the page tree.", verbose_name='tree-based routing enabled')), ('breadcrumbs_label', models.CharField(max_length=255)), ('breadcrumbs_label_en_gb', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_de', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_ja', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_zh_hans', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_fr', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_es', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_pt', models.CharField(max_length=255, null=True)), ('breadcrumbs_label_ar', models.CharField(max_length=255, null=True)), ('hero_title', models.CharField(max_length=255)), ('hero_title_en_gb', models.CharField(max_length=255, null=True)), ('hero_title_de', models.CharField(max_length=255, null=True)), ('hero_title_ja', models.CharField(max_length=255, null=True)), ('hero_title_zh_hans', models.CharField(max_length=255, null=True)), ('hero_title_fr', models.CharField(max_length=255, null=True)), ('hero_title_es', models.CharField(max_length=255, null=True)), ('hero_title_pt', models.CharField(max_length=255, null=True)), ('hero_title_ar', models.CharField(max_length=255, null=True)), ('hero_image', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_ar', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_de', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_en_gb', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_es', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_fr', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_ja', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_pt', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('hero_image_zh_hans', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ], options={ 'abstract': False, }, bases=('wagtailcore.page', great_international.panels.great_international.AboutUkLandingPagePanels), ), migrations.CreateModel( name='AboutUkArticlesFields', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('sort_order', models.IntegerField(blank=True, editable=False, null=True)), ('title', models.CharField(blank=True, max_length=255)), ('summary', models.TextField(blank=True)), ('link_text', models.CharField(blank=True, max_length=255, verbose_name='Link text')), ('link_url', models.CharField(blank=True, max_length=255, verbose_name='Link URL')), ('image', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ('page', modelcluster.fields.ParentalKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='about_uk_articles_fields', to='great_international.AboutUkWhyChooseTheUkPage')), ('page_ar', modelcluster.fields.ParentalKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='about_uk_articles_fields', to='great_international.AboutUkWhyChooseTheUkPage')), ('page_de', modelcluster.fields.ParentalKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='about_uk_articles_fields', to='great_international.AboutUkWhyChooseTheUkPage')), ('page_en_gb', modelcluster.fields.ParentalKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='about_uk_articles_fields', to='great_international.AboutUkWhyChooseTheUkPage')), ('page_es', modelcluster.fields.ParentalKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='about_uk_articles_fields', to='great_international.AboutUkWhyChooseTheUkPage')), ('page_fr', modelcluster.fields.ParentalKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='about_uk_articles_fields', to='great_international.AboutUkWhyChooseTheUkPage')), ('page_ja', modelcluster.fields.ParentalKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='about_uk_articles_fields', to='great_international.AboutUkWhyChooseTheUkPage')), ('page_pt', modelcluster.fields.ParentalKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='about_uk_articles_fields', to='great_international.AboutUkWhyChooseTheUkPage')), ('page_zh_hans', modelcluster.fields.ParentalKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='about_uk_articles_fields', to='great_international.AboutUkWhyChooseTheUkPage')), ], options={ 'ordering': ['sort_order'], 'abstract': False, }, bases=(models.Model, great_international.panels.great_international.AboutUkArticleFieldPanels), ), ]
python
# Generated by Django 3.0.10 on 2020-10-23 10:16 from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ ('project_core', '0142_fix_physicalperson_plural'), ('reporting', '0003_funding_instrument_description'), ] operations = [ migrations.AddField( model_name='fundinginstrumentyearmissingdata', name='missing_data_type', field=models.CharField(default='', max_length=32), preserve_default=False, ), migrations.AlterField( model_name='fundinginstrumentyearmissingdata', name='finance_year', field=models.IntegerField(blank=True, null=True), ), migrations.AlterField( model_name='fundinginstrumentyearmissingdata', name='funding_instrument', field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='project_core.FundingInstrument'), ), ]
python
import os import numpy as np from utils import map_to_full class VideoSaver: def __init__(self, savedir): try: os.makedirs(savedir) except: pass self.savedir = savedir self.id = 0 def save_mp4_from_vid_and_audio(self, video_tensor, audio_wav=None, fps=25, sr=16000, outname=None, extract_frames_hop=None): """ :param video_tensor: tchw :param sr: :return: """ from moviepy.audio.AudioClip import AudioArrayClip from moviepy.video.VideoClip import VideoClip video_tensor = video_tensor.transpose([0, 2, 3, 1]) # thwc # that's to avoid error due to float precision vid_dur = len(video_tensor) * (1. / fps) - 1e-6 v_clip = VideoClip(lambda t: video_tensor[int(np.round(t * 25))], duration=vid_dur) import tempfile if outname: outfile = os.path.join(self.savedir, outname) if not outfile.endswith('.mp4'): outfile += '.mp4' else: outfile = os.path.join(self.savedir, '%03d.mp4' % self.id) if audio_wav is not None: _, temp_audiofile = tempfile.mkstemp(dir='/dev/shm', suffix='.wav') import torch if isinstance(audio_wav, torch.Tensor): audio_wav = audio_wav.numpy() import scipy.io scipy.io.wavfile.write(temp_audiofile, 16000, audio_wav) self.id += 1 try: os.makedirs(os.path.dirname(outfile)) except: pass _, temp_videofile = tempfile.mkstemp(dir='/dev/shm', suffix='.mp4') v_clip.write_videofile(temp_videofile, fps=25, verbose=False) if audio_wav is not None: command = ("ffmpeg -threads 1 -loglevel error -y -i {} -i {} " "-c:v copy -map 0:v:0 -map 1:a:0 -pix_fmt yuv420p " "-shortest {}").format(temp_videofile, temp_audiofile, outfile) from subprocess import call cmd = command.split(' ') call(cmd) else: import shutil shutil.move(temp_videofile, outfile) v_clip.close() import imageio if extract_frames_hop: # extract the video as frames for paper frames_dir = os.path.join( os.path.dirname(outfile), 'frames_' + os.path.basename(outfile).replace('.mp4', '')) os.makedirs(frames_dir, exist_ok=True) import scipy.misc for fr_id, frame in enumerate(video_tensor[::extract_frames_hop]): scipy.misc.imsave(frames_dir + '/%04d.png' % fr_id, frame[:, :-5, :]) pass def normalize_img(value, vmax=None, vmin=None): vmin = value.min() if vmin is None else vmin vmax = value.max() if vmax is None else vmax if not (vmax - vmin) == 0: value = (value - vmin) / (vmax - vmin) # vmin..vmax return value # @profile def show_cam_on_vid(vid, cam, offset=0): """ :param vid: t x h x w x c :param cam: h_att x w_att :return: """ assert len(cam) == len(vid) vids = {} vmin = cam.min() vmax = cam.max() vmin = vmax = None vid_with_cam = np.array([ show_cam_on_image(frame, msk, offset, vmin, vmax) for frame, msk in zip(vid, cam) ]) return vid_with_cam def viz_boxes_with_scores(video, box_centers, scores=None, const_box_size=None, colors = None, asd_thresh=None): """ video: np array -> t h w c """ import aolib_p3.util as ut import aolib_p3.img as ig if colors is None: colors = ut.distinct_colors(len(box_centers)) peaks_on_vid_viz = [] def add_cont_bb_size_to_traj(box_centers, const_box_size): const_box_size = np.array([const_box_size, const_box_size]) const_box_size = np.tile(const_box_size[None, None], box_centers.shape[:2] + (1,)) box_centers = np.concatenate( [box_centers, const_box_size], -1) return box_centers if box_centers.shape[-1] == 2: # no box size, need to pad it box_centers = add_cont_bb_size_to_traj(box_centers, const_box_size) bb_sizes = box_centers[..., 2:] box_centers = box_centers[..., :2] if scores is not None: padlen = box_centers.shape[1] - scores.shape[-1] scores = np.pad(scores, [[0, 0], [padlen // 2, padlen // 2]], mode='edge') for tt in range(len(video)): border_width = 3 track_vis = video[tt] def make_text(track_vis, scores, const_off=40, relative_off=1, fmt='{:.2f}', font_size=30): texts = list(map(lambda xx: fmt.format(xx), scores)) if relative_off: txt_off = const_off + border_width text_loc = box_centers[:, tt] + \ np.array([-txt_off, txt_off]) else: text_loc = np.array([const_off, const_off ])[None].repeat(box_centers.shape[0], 0) track_vis = ig.draw_text(track_vis, texts, text_loc, colors, font_size=font_size) return track_vis if scores is not None: asd_scores = scores[:, tt] track_vis = make_text(track_vis, asd_scores) pnt_locs = [] cols = [] wds = int(bb_sizes.mean()) for ii, asd_sc in enumerate(asd_scores): if asd_sc > asd_thresh: pnt_locs.append(box_centers[ii, tt]) cols.append(colors[ii]) track_vis = draw_hollow_rects(track_vis, np.array(pnt_locs), cols, width=wds, border_width=border_width) else: track_vis = draw_hollow_rects(track_vis, box_centers[:, tt], colors, width=bb_sizes[:, tt], border_width=border_width) peaks_on_vid_viz.append(track_vis) peaks_on_vid_viz = np.array(peaks_on_vid_viz) vid_top_trajectories_viz = peaks_on_vid_viz.transpose([0, 3, 1, 2]) return vid_top_trajectories_viz def draw_hollow_rects(im, points, colors=None, width=1, border_width=None, texts=None): import aolib_p3.img as ig points = list(points) colors = ig.colors_from_input(colors, (255, 0, 0), len(points)) if isinstance(width, int): heights = widths = [width] * len(points) else: assert len(width) == len(points) widths, heights = np.array(width).T rects = [(p[0] - width / 2, p[1] - height / 2, width, height) for p, width, height in zip(points, widths, heights)] line_widths = None if border_width is not None: line_widths = [border_width] * len(points) return ig.draw_rects(im, rects, fills=[None] * len(points), outlines=colors, texts=texts, line_widths=line_widths) def show_cam_on_image(frame, cam, offset, vmin=None, vmax=None): """ :param frame: c x h x w :param cam: h_att x w_att :return: """ # frame = frame.transpose([1, 2, 0]) # chw --> hwc frame = np.float32(frame) / 255 import cv2 if vmin is not None: vmax = -vmin vmin = -vmax cam = normalize_img(-cam, vmin=vmin, vmax=vmax) heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET) h_frame, w_frame = frame.shape[:2] heatmap, offset = map_to_full(heatmap, w_frame, h_frame, offset, w_map=heatmap.shape[1]) heatmap = np.float32(heatmap) / 255 heatmap_frame = np.zeros_like(frame) heatmap_frame[offset:h_frame - offset, offset:w_frame - offset] = heatmap cam = heatmap_frame + frame cam = cam / np.max(cam) new_img = np.uint8(255 * cam) new_img = new_img.transpose([2, 0, 1]) # hwc --> chw return new_img def viz_avobjects( video, audio, att_map, avobject_traj, model_start_offset, video_saver, const_box_size, step, asd_thresh=None, vids_name='avobject_viz'): """ video: c T H W att_map: t h w """ print('Vizualizaing av att and avobject trajectories') video = video.permute([1,2,3,0]).numpy().astype('uint8') # C T H W -> T H W C # ----------- make cam_vid showing AV-att map and peaks --------------- vid_with_cam = show_cam_on_vid(video, att_map.detach().cpu(), offset=model_start_offset) vid_avobject = viz_boxes_with_scores( video, avobject_traj[..., [1, 0]], # switch x and y coords const_box_size=const_box_size ) # remove padding equal to the model's conv offset pad_len = model_start_offset vid_with_cam = vid_with_cam[..., pad_len:-pad_len, pad_len:-pad_len] vid_avobject = vid_avobject[..., pad_len:-pad_len, pad_len:-pad_len] video_saver.save_mp4_from_vid_and_audio( np.concatenate([vid_with_cam, vid_avobject], axis=3), audio / 32768, outname='{}/{}'.format(vids_name, step), ) def viz_source_separation(video, enh_audio, avobject_traj, model_start_offset, const_box_size, video_saver, step): video = video.permute([1,2,3,0]).numpy().astype('uint8') # C T H W -> T H W C assert avobject_traj.shape[0] == enh_audio.shape[0] n_objects = avobject_traj.shape[0] import aolib_p3.util as ut colors = ut.distinct_colors(n_objects) for ii in range(n_objects): vid_avobject = viz_boxes_with_scores( video, avobject_traj[ ii:ii+1, :, [1, 0]], # switch x and y coords const_box_size=const_box_size, colors = [colors[ii]] ) # remove padding equal to the model's conv offset pad_len = model_start_offset vid_avobject = vid_avobject[..., pad_len:-pad_len, pad_len:-pad_len] # vid_sep = video[0:1, ii].numpy().astype('uint8') # vid_sep = vid_sep.transpose([0, 2, 1, 3, 4]) video_saver.save_mp4_from_vid_and_audio( vid_avobject, enh_audio[ii], outname='sep_vid/{}/enh_{}'.format(step, ii))
python
# -*- coding: utf-8 -*- from multiprocessing import Pool import os import time start = time.time() def f(x): time.sleep(1) value = x * x print('{}s passed...\t{}\t(pid:{})'.format(int(time.time() - start), value, os.getpid())) return value timeout = time.time() + 10 # sec while True: with Pool(processes=2) as p: if time.time() > timeout: p.close() break print(p.map(f, [1, 2])) p.close() # コンテキストマネージャを使わずに以下のように書いても良い # Pool(3).map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
python
from turtle import * import random import threading from tkinter import * # generate random seed num = random.randint(1897348294, 18495729473285739) print("\n\nUsing Seed: " + str(num)) # set the seed for all randomization random.seed(num) # save the current seed to a text file with open('current_seed.txt', 'w') as f: f.write(str(num)) # colors colors = ['blue', 'red', 'purple', 'yellow', 'green', 'orange', 'hot_colors'] # create the turtle turtle1 = Turtle() # make it so there is no arrow drawing the lines turtle1.ht() # get window size screen = Screen() # set the background color screen.bgcolor('white') # set the screen size screen.screensize(canvwidth=512, canvheight=512) # get the screen height and width w = screen.window_width() h = screen.window_height() # printing just for reference print(screen.screensize()) # enable the following line to have a more precise image # w, h = w // 2, h // 2 # set the turtle speed turtle1.speed(0) # max speed is 0 def chooseColor(): """ chooses random color then opens that color's respective shade text file. then it randomly chooses a shade for the previous chosen color. """ color = random.choice(colors) with open("colors/" + color + '.txt', 'r') as f: shades = f.read().splitlines() rgb = random.choice(shades) print("Using " + color + " with rgb " + rgb) return rgb def draw_background(a_turtle): """ Draw a background rectangle. """ ts = a_turtle.getscreen() canvas = ts.getcanvas() height = ts.getcanvas()._canvas.winfo_height() width = ts.getcanvas()._canvas.winfo_width() turtleheading = turtle1.heading() turtlespeed = turtle1.speed() penposn = turtle1.position() penstate = turtle1.pen() turtle1.penup() turtle1.speed(0) # fastest turtle1.goto(-width / 2 - 2, -height / 2 + 3) turtle1.fillcolor(Screen().bgcolor()) turtle1.begin_fill() turtle1.setheading(0) turtle1.forward(width) turtle1.setheading(90) turtle1.forward(height) turtle1.setheading(180) turtle1.forward(width) turtle1.setheading(270) turtle1.forward(height) turtle1.end_fill() turtle1.penup() turtle1.setposition(*penposn) turtle1.pen(penstate) turtle1.setheading(turtleheading) turtle1.speed(turtlespeed) draw_background(turtle1) def square(): """ Draws square with angles of 70 to 91 degrees, with side lengths of 100 to 201 Guess you can't call it a square anymore """ m = random.randint(70, 91) d = random.randint(100, 201) for i in range(4): rgb = chooseColor() turtle1.pencolor(rgb) turtle1.right(m) turtle1.forward(d) def hexagon(): """ Draws hexagon with angles of 70 to 91 degrees, with side lengths of 100 to 201 Guess you can't call it a hexagon anymore """ m = random.randint(70, 91) d = random.randint(100, 201) turtle1.right(90) for i in range(4): rgb = chooseColor() turtle1.pencolor(rgb) turtle1.forward(m) turtle1.right(d) turtle1.forward(d) def triangle(): """ Draws triangle with angles of 70 to 91 degrees, with side lengths of 100 to 201 Guess you can't call it a triangle anymore """ m = random.randint(70, 91) d = random.randint(100, 201) for i in range(3): rgb = chooseColor() turtle1.pencolor(rgb) turtle1.forward(m) turtle1.right(-d) # set variables for counting j = 0 m = 50 while True: x, y = turtle1.pos() # Get x, y positions. if abs(x) > w or abs(y) > h: # Check if pen is outside of frame # reset pen to random position on X and Y between 0 and the frame border theX = random.randint(0, w - 100) theY = random.randint(0, h - 100) turtle1.setx(theX) turtle1.sety(theY) # draw a triangle, a hexagon and a square triangle() hexagon() square() j += 1 # if program has run the above 50 times, its time for another save if j == m: print("\n\nSAVING!!!!!!!!!\n\n") # get the current screen ts = turtle1.getscreen() # save the drawing to a post script ts.getcanvas().postscript(file="art_save.eps") m += 50
python
"""Tests for the models in the ``core`` app of the Marsha project.""" from django.db import transaction from django.db.utils import IntegrityError from django.test import TestCase from safedelete.models import SOFT_DELETE_CASCADE from ..factories import VideoFactory class VideoModelsTestCase(TestCase): """Test our intentions about the Video model.""" def test_models_video_str(self): """The str method should display the title of the video and its eventual soft deletion.""" video = VideoFactory(title="j'espère") self.assertEqual(str(video), "j'espère") video.delete() self.assertEqual(str(video), "j'espère [deleted]") def test_models_video_fields_lti_id_unique(self): """Videos should be unique for a given duo lti_id/playlist (see LTI specification).""" video = VideoFactory() # A video with a different lti_id and the same playlist can still be created VideoFactory(playlist=video.playlist) # A video for a different playlist and the same lti_id can still be created VideoFactory(lti_id=video.lti_id) # Trying to create a video with the same duo lti_id/playlist should raise a # database error with self.assertRaises(IntegrityError): with transaction.atomic(): VideoFactory(lti_id=video.lti_id, playlist=video.playlist) # Soft deleted videos should not count for unicity video.delete(force_policy=SOFT_DELETE_CASCADE) VideoFactory(lti_id=video.lti_id, playlist=video.playlist)
python
__copyright__ = 'Copyright(c) Gordon Elliott 2017' """ """ from datetime import datetime from decimal import Decimal from itertools import product from a_tuin.metadata.field_group import ( TupleFieldGroup, ListFieldGroup, DictFieldGroup, ObjectFieldGroup, ) from a_tuin.metadata.field import ( StringField, IntField, FloatField, DecimalField, DateTimeField, ComputedStringField, INVALID_FIELD_COMBINATIONS ) class _ObjectFieldGroupInstanceFixture(object): def __init__(self, name, count, rate, amount, timestamp, dunder="__computed__"): self.name = name self.count = count self.rate = rate self.amount = amount self.timestamp = timestamp self.dunder = dunder class _ObjectFieldGroupFixture(ObjectFieldGroup): def __init__(self, fields): super().__init__(fields, _ObjectFieldGroupInstanceFixture) MUTABLE_FIELD_GROUP_CLASSES = (ListFieldGroup, DictFieldGroup, _ObjectFieldGroupFixture) FIELD_GROUP_CLASSES = (TupleFieldGroup,) + MUTABLE_FIELD_GROUP_CLASSES FIELDS = ( StringField('name', 'Name of this entity'), IntField('count'), FloatField('rate'), DecimalField('amount'), DateTimeField('timestamp'), ComputedStringField('dunder', lambda field_group, instance: "__computed__") ) DATETIME_FIXTURE = datetime.now() INITIAL_VALUES = { 'name': 'initial name', 'count': 4, 'rate': 1.243, 'amount': Decimal('3.22'), 'timestamp': DATETIME_FIXTURE } EXPECTED_VALUES = INITIAL_VALUES.copy() EXPECTED_VALUES['dunder'] = "__computed__" FIELD_COMBINATIONS = ( (src, dest) for src, dest in product(FIELDS, FIELDS) if (type(src), type(dest)) not in INVALID_FIELD_COMBINATIONS ) def field_group_fixtures(fields=None, field_group_classes=None): fields = fields if fields else FIELDS field_group_classes = field_group_classes if field_group_classes else FIELD_GROUP_CLASSES def dict_as_sequence(d, sequence_type): return sequence_type( d[field.name] for field in fields if field.name in d ) fixture_constructors = { DictFieldGroup: lambda iv: iv.copy(), ListFieldGroup: lambda iv: dict_as_sequence(iv, list), TupleFieldGroup: lambda iv: dict_as_sequence(iv, tuple), _ObjectFieldGroupFixture: lambda iv: _ObjectFieldGroupInstanceFixture(**iv), } for field_group_class in field_group_classes: yield ( field_group_class, field_group_class(fields), fixture_constructors[field_group_class] ) def field_group_combinations(source_fields=None, destination_fields=None): return product( field_group_fixtures(source_fields), field_group_fixtures(destination_fields) ) def inplace_field_group_combinations(): return product( field_group_fixtures(), field_group_fixtures( field_group_classes=MUTABLE_FIELD_GROUP_CLASSES ) )
python
# -*- coding: utf-8 -*- try: # pragma: no cover from Cryptodome.Cipher import AES from Cryptodome import Random except ImportError: # pragma: no cover try: from Crypto.Cipher import AES from Crypto import Random except ImportError: raise ImportError("Missing dependency: pyCryptodome") import hashlib import base64 class AESCipher(object): """ A classical AES Cipher. Can use any size of data and any size of password thanks to padding. Also ensure the coherence and the type of the data with a unicode to byte converter. """ def __init__(self, key): self.bs = 32 self.key = hashlib.sha256(AESCipher.str_to_bytes(key)).digest() @staticmethod def str_to_bytes(data): u_type = type(b"".decode("utf8")) if isinstance(data, u_type): return data.encode("utf8") return data def _pad(self, s): return s + (self.bs - len(s) % self.bs) * AESCipher.str_to_bytes( chr(self.bs - len(s) % self.bs) ) @staticmethod def _unpad(s): return s[: -ord(s[len(s) - 1 :])] def encrypt(self, raw): raw = self._pad(AESCipher.str_to_bytes(raw)) iv = Random.new().read(AES.block_size) cipher = AES.new(self.key, AES.MODE_CBC, iv) return base64.b64encode(iv + cipher.encrypt(raw)).decode("utf-8") def decrypt(self, enc): enc = base64.b64decode(enc) iv = enc[: AES.block_size] cipher = AES.new(self.key, AES.MODE_CBC, iv) return self._unpad(cipher.decrypt(enc[AES.block_size :])).decode("utf-8")
python
def count_safe(input, part = 1): previous_row = list(input) safe_count = previous_row.count(".") rows = 400000 if part == 2 else 40 for i in range(1, rows): current_row = [] for j in range(len(input)): l = previous_row[j - 1] if j > 0 else "." c = previous_row[j] r = previous_row[j + 1] if j < 99 else "." if l == r: current_row.append(".") else: current_row.append("^") safe_count += current_row.count(".") previous_row = current_row[:] return safe_count if __name__ == "__main__": with open("day_18_input.txt") as f: input = f.read() print "Part 1 answer: " + str(count_safe(input)) print "Part 2 answer: " + str(count_safe(input, 2))
python
import logging logger = logging.getLogger(__name__) import struct from Crypto.Random import get_random_bytes from Crypto.Hash import HMAC from Crypto.Cipher import AES from jose.exceptions import AuthenticationError from jose.utils import pad_pkcs7, unpad_pkcs7, sha def _jwe_hash_str(ciphertext, iv, adata=b''): # http://tools.ietf.org/html/ # draft-ietf-jose-json-web-algorithms-24#section-5.2.2.1 return b''.join((adata, iv, ciphertext, struct.pack("!Q", len(adata) * 8))) class ContentEncryptionAlgorithm(object): def __init__(self, key): self.key = key @classmethod def generate_key(cls, rng=None): raise NotImplementedError() @classmethod def generate_iv(cls, rng=None): raise NotImplementedError() def encrypt(self, plaintext, adata, iv): raise NotImplementedError() def decrypt(self, ciphertext, adata, iv): raise NotImplementedError() class AES_CBC_HMAC_SHA2_Base(ContentEncryptionAlgorithm): def __init__(self, key): if len(key) != self.enc_key_size + self.mac_key_size: raise ValueError("key is wrong size") self.key = key @classmethod def generate_key(cls, rng=None): if rng is None: rng = get_random_bytes return rng(cls.enc_key_size + cls.mac_key_size) @classmethod def generate_iv(cls, rng=None): if rng is None: rng = get_random_bytes return rng(16) def _sign(self, key, ciphertext, iv, adata): # TODO this is completely the wrong way to select the hash function hmac = HMAC.new(key, digestmod=sha(16 * self.mac_key_size)) hmac.update(_jwe_hash_str(ciphertext, iv, adata)) signature = hmac.digest() # http://tools.ietf.org/html/ # draft-ietf-oauth-json-web-token-19#section-4.1.4 return signature[:self.token_size] def encrypt(self, plaintext, iv, adata=None): if adata is None: adata = b'' signature_key = self.key[:self.mac_key_size] encryption_key = self.key[self.mac_key_size:] padded_plaintext = pad_pkcs7(plaintext, block_size=AES.block_size) enc_algorithm = AES.new(encryption_key, AES.MODE_CBC, iv) ciphertext = enc_algorithm.encrypt(padded_plaintext) auth_digest = self._sign(signature_key, ciphertext, iv, adata) auth_token = auth_digest[:self.token_size] return ciphertext, auth_token def decrypt(self, ciphertext, auth_token, iv, adata=None): if adata is None: adata = b'' signature_key = self.key[:self.mac_key_size] encryption_key = self.key[self.mac_key_size:] enc_algorithm = AES.new(encryption_key, AES.MODE_CBC, iv) padded_plaintext = enc_algorithm.decrypt(ciphertext) plaintext = unpad_pkcs7(padded_plaintext) auth_digest = self._sign(signature_key, ciphertext, iv, adata) calculated_auth_token = auth_digest[:self.token_size] if calculated_auth_token != auth_token: raise AuthenticationError("Mismatched authentication tag") return plaintext class A128CBC_HS256(AES_CBC_HMAC_SHA2_Base): enc_key_size = 16 mac_key_size = 16 token_size = 16 class A192CBC_HS384(AES_CBC_HMAC_SHA2_Base): enc_key_size = 24 mac_key_size = 24 token_size = 24 class A256CBC_HS512(AES_CBC_HMAC_SHA2_Base): enc_key_size = 32 mac_key_size = 32 token_size = 32 _CONTENT_ENCRYPTION_ALGORITHMS = { 'A128CBC-HS256': A128CBC_HS256, 'A192CBC-HS384': A192CBC_HS384, 'A256CBC-HS512': A256CBC_HS512, } def from_name(name): return _CONTENT_ENCRYPTION_ALGORITHMS[name]
python
import os BASE_DIR = os.getcwd() TARGET_DIR = os.path.join(BASE_DIR, "target")
python
idadetotal = 0 idademedia = 0 contadormulher = 0 homemvelho = 0 lista = [] nomevelho = '' for p in range(1, 5): print('=-'*20, f'{p}ª PESSOA', '=-'*20) nome = str(input('Nome: ')) idade = int(input('Idade: ')) sexo = str(input('M/F: ')) idadetotal += idade idademedia = idadetotal/4 if sexo in 'Ff': if idade < 20: contadormulher += 1 if sexo in 'Mm': if p == 1: homemvelho = idade nomevelho = nome if idade > homemvelho: homemvelho = idade nomevelho = nome print(f'A média de idade do grupo é de {idademedia:.0f} anos.\nAlém disso, há, no total, {contadormulher} mulhere(s) com menos de 20 anos.') print(f'O homem mais velho tem {homemvelho} anos, e seu nome é {nomevelho}.')
python
import matplotlib.pyplot as plt import numpy as np np.random.seed(0) data = np.random.randn(10_000) #plt.hist(data, bins=30, alpha=.5, histtype="stepfilled", color="steelblue") #plt.show() counts, bin_edges = np.histogram(data, bins=5) print(counts) print(bin_edges) x1 = np.random.normal(0, 0.8, 1000) x2 = np.random.normal(-2, 1, 1000) x3 = np.random.normal(3, 2, 1000) kwargs = dict( histtype='stepfilled', alpha=0.3, bins=40 ) #plt.hist(x1, **kwargs) #plt.hist(x2, **kwargs) #plt.hist(x3, **kwargs) #plt.show() mean = [0, 0] cov = [[1, 1], [1, 2]] x, y = np.random.multivariate_normal(mean=mean, cov=cov, size=10000).T plt.hist2d(x, y, bins=30, cmap="Blues") plt.colorbar() plt.show()
python
from django.core import mail from django.core.mail import send_mail from django.conf import settings from django.template.loader import render_to_string from django.utils.html import strip_tags def send_email_order(sale, sender, receiver): # body = f"Gunakan nomer {sale.sale_number} untuk mengecek pesanan kamu di " \ # f"'{settings.LINK_CHECK_ORDER}'. Terimakasih sudah berbelanja di store kami." # # send_mail( # 'Hai, {}'.format(sale.customer.name), # body, # sender, # [receiver] # ) subject = f'Order {sale.customer.name}' html_message = render_to_string('cores/send_email_order.html', {'sale': sale}) plain_message = strip_tags(html_message) from_email = sale.user.email to = sale.customer.email mail.send_mail(subject, plain_message, from_email, [to], html_message=html_message)
python
import numpy as np import numpy.testing as npt import py.test from hypothesis import assume from hypothesis import given import arlunio.testing as T from arlunio.math import X from arlunio.math import Y @given(width=T.dimension, height=T.dimension) def test_X_matches_dimension(width, height): """Ensure that the output shape matches the width and height of the image""" x = X() assert x(width=width, height=height).shape == (height, width) @given(width=T.dimension, height=T.dimension) def test_X_uniform_vertically(width, height): """Ensure that the output only varies in the x-direction""" x = X() xs = x(width=width, height=height) assert np.unique(xs, axis=0).shape == (1, width) @given(width=T.dimension, height=T.dimension, scale=T.pve_num) def test_X_adapts_to_image_ratio_by_default(width, height, scale): """Ensure that the output adapts to the aspect ratio of the image.""" assume(width / height > 1) x = X(scale=scale) xs = x(width=width, height=height) assert width / height == py.test.approx(np.max(xs) / scale) @given(width=T.dimension, height=T.dimension, scale=T.pve_num) def test_X_fits_to_image_size_when_told(width, height, scale): """Ensure that the output fits to the size of the image when :code:`fit` property is set.""" x = X(scale=scale, stretch=True) xs = x(width=width, height=height) assert np.max(xs) == scale @given(width=T.dimension, height=T.dimension, offset=T.real_num) def test_X_shifts_origin_accordingly(width, height, offset): """Ensure that the output has shifted the output according to the :code:`x0` property.""" x1 = X() x2 = X(x0=offset) x1s = x1(width=width, height=height) x2s = x2(width=width, height=height) npt.assert_almost_equal(x1s - x2s, offset) @given(width=T.dimension, height=T.dimension) def test_Y_matches_dimension(width, height): """Ensure that the output shape matches the width and height of the image.""" y = Y() ys = y(width=width, height=height) assert ys.shape == (height, width) @given(width=T.dimension, height=T.dimension) def test_Y_uniform_horizontally(width, height): """Ensure that the output only varies in the y direction""" y = Y() ys = y(width=width, height=height) assert np.unique(ys, axis=1).shape == (height, 1) @given(width=T.dimension, height=T.dimension, scale=T.pve_num) def test_Y_adapts_to_image_ratio_by_default(width, height, scale): """Ensure that the output adapts to the aspect ratio of the image""" assume(height / width > 1) y = Y(scale=scale) ys = y(width=width, height=height) assert height / width == py.test.approx(np.max(ys) / scale) @given(width=T.dimension, height=T.dimension, scale=T.pve_num) def test_Y_fits_to_image_size_when_told(width, height, scale): """Ensure that the output fits to the size of the image when :code:`fit` property is set.""" y = Y(scale=scale, stretch=True) ys = y(width=width, height=height) assert np.max(ys) == scale @given(width=T.dimension, height=T.dimension, offset=T.real_num) def test_Y_shifts_origin_accordingly(width, height, offset): """Ensure that the output has shifted the output according to the :code:`y0` property.""" y1 = Y() y2 = Y(y0=offset) y1s = y1(width=width, height=height) y2s = y2(width=width, height=height) npt.assert_almost_equal(y1s - y2s, offset)
python
from boa3.builtin import public @public def Main(value: int) -> int: a = 0 condition = a < value while condition: a = a + 2 condition = a < value * 2 return a
python
from random import randint numeros = (randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100)) print(sorted(numeros)) print(f'O maior valor sorteado foi {max(numeros)}') print(f'O menor valor sorteado foi {min(numeros)}')
python
from src2docx import * import tkinter.filedialog import tkinter.messagebox import tkinter.ttk class MainForm(tkinter.Tk): def __init__(self): super().__init__() self.title("src2docx") self.geometry("220x180") self.resizable(0, 0) self.directoryLabel = tkinter.ttk.Label(self, text="소스 코드가 있는 폴더") self.directoryEntry = tkinter.ttk.Entry(self) self.directoryBrowseButton = tkinter.ttk.Button(self, text="찾아보기", command=self.onDirectoryBrowseButtonClicked) self.outputLabel = tkinter.ttk.Label(self, text="Word 문서 파일의 이름") self.outputEntry = tkinter.ttk.Entry(self) self.outputBrowseButton = tkinter.ttk.Button(self, text="찾아보기", command=self.onOutputBrowseButtonClicked) self.src2DocxButton = tkinter.ttk.Button(self, text="src2docx", command=self.onSrc2DocxButtonClicked) self.directoryLabel.pack() self.directoryEntry.pack() self.directoryBrowseButton.pack() tkinter.ttk.Label(self, text="↓").pack() self.outputLabel.pack() self.outputEntry.pack() self.outputBrowseButton.pack() self.src2DocxButton.pack(side="bottom") tkinter.ttk.Separator(self).pack(side="bottom", fill="x") self.mainloop() def onDirectoryBrowseButtonClicked(self): directory = tkinter.filedialog.askdirectory() self.directoryEntry.delete(0, tkinter.END) self.directoryEntry.insert(0, directory) def onOutputBrowseButtonClicked(self): filename = tkinter.filedialog.asksaveasfilename(filetypes=(("Word 문서", "*.docx"), ("모든 파일", "*.*"))) self.outputEntry.delete(0, tkinter.END) self.outputEntry.insert(0, filename) def onSrc2DocxButtonClicked(self): directory = self.directoryEntry.get().strip() output = self.outputEntry.get().strip() if directory == "" or output == "": tkinter.messagebox.showerror(title="src2docx", message="값을 입력해주세요.") return src2Docx = Src2Docx(directory, output) src2Docx.run() tkinter.messagebox.showinfo(title="src2docx", message="완료되었습니다.") if __name__ == "__main__": mainForm = MainForm()
python
#!/usr/bin/env python """ encoding.py encoding.py (c) 2016 by Paul A. Lambert licensed under a Creative Commons Attribution 4.0 International License. """ if __name__ == '__main__' and __package__ is None: from os import sys, path p = path.abspath(__file__) # ./cryptopy/persona/test/test_cipher_suite.py for i in range(4): p = path.dirname( p ) # four levels down to project '.' sys.path.append( p ) from cryptopy.cipher.encoding import b27encode, b27decode, b85encode, b85decode from cryptopy.cipher.encoding import b94encode, b94decode if __name__ == '__main__': """ Examples of text encodings for 128 bit and 48 bits (USID and SID) """ # calculate a USID and SID and use to demonstrate encodings service_name = 'service.name.example' from hashlib import sha256 hash_value = sha256( service_name ).digest() usid = hash_value[0:16] # USIDs are 16 octets of the hash value service_id = hash_value[0:6] # SIDs are 6 octets of the hash value print 'service name: ', service_name print 'hash value: ', hash_value.encode('hex') print 'usid: ', usid.encode('hex') print 'usid b27 ', b27encode(usid) assert b27decode(b27encode(usid)) == usid # test decode b27 print 'usid b85 ', b85encode(usid) print 'usid b94: ', b94encode(usid) print 'service id: ', service_id.encode('hex') print 'service id b27: ', b27encode(service_id) print 'service id b85: ', b85encode(service_id) print 'service id b94: ', b94encode(service_id) service_name = 'service.name.example' hash_value = sha256( service_name ).digest() usid = hash_value[0:16] usib_b27 = b27encode(usid) print 'usid b27', usib_b27 service_name = usib_b27 hash_value = sha256( service_name ).digest() usid = hash_value[0:16] usib_b27 = b27encode(usid) print 'usid b27', usib_b27 service_name = usib_b27 hash_value = sha256( service_name ).digest() usid = hash_value[0:16] usib_b27 = b27encode(usid) print 'usid b27', usib_b27 service_name = usib_b27 hash_value = sha256( service_name ).digest() usid = hash_value[0:16] usib_b27 = b27encode(usid) print 'usid b27', usib_b27 service_name = usib_b27 hash_value = sha256( service_name ).digest() usid = hash_value[0:16] usib_b27 = b27encode(usid) print 'usid b27', usib_b27 service_name = usib_b27 hash_value = sha256( service_name ).digest() usid = hash_value[0:16] usib_b27 = b27encode(usid) print 'usid b27', usib_b27
python
""" Copyright 2022 Objectiv B.V. """ import bach import pandas as pd import pytest from modelhub.stack.util import get_supported_dtypes_per_objectiv_column, check_objectiv_dataframe from tests_modelhub.data_and_utils.utils import create_engine_from_db_params def test_get_supported_types_per_objectiv_column() -> None: result = get_supported_dtypes_per_objectiv_column() expected = { 'event_id': 'uuid', 'day': 'date', 'moment': 'timestamp', 'user_id': 'uuid', 'global_contexts': 'json', 'location_stack': 'json', 'event_type': 'string', 'stack_event_types': 'json', 'session_id': 'int64', 'session_hit_number': 'int64' } assert expected == result result = get_supported_dtypes_per_objectiv_column(with_md_dtypes=True) assert result['global_contexts'] == 'objectiv_global_context' assert result['location_stack'] == 'objectiv_location_stack' def test_check_objectiv_dataframe(db_params) -> None: fake_objectiv_pdf = pd.DataFrame( { 'event_id': ['1'], 'day': ['2022-01-01'], 'moment': ['2022-01-01 01:01:01'], 'user_id': ['1'], 'global_contexts': [[]], 'location_stack': [[]], 'event_type': ['event'], 'stack_event_types': [[]], 'session_id': ['1'], 'session_hit_number': ['1'] }, ) fake_objectiv_df = bach.DataFrame.from_pandas( engine=create_engine_from_db_params(db_params), df=fake_objectiv_pdf, convert_objects=True, ) # should be ok check_objectiv_dataframe(columns_to_check=['event_id'], df=fake_objectiv_df[['event_id', 'day']]) # checks all objectiv columns with pytest.raises(ValueError, match=r'is not present in DataFrame.'): check_objectiv_dataframe(df=fake_objectiv_df[['event_id', 'day']]) # will check if event_id is in df index check_objectiv_dataframe(check_index=True, df=fake_objectiv_df.set_index('event_id')) with pytest.raises(ValueError, match=r'is not present in DataFrame index.'): check_objectiv_dataframe(check_index=True, df=fake_objectiv_df) check_objectiv_dataframe( columns_to_check=['session_id'], df=fake_objectiv_df[['session_id']].astype('int64'), check_dtypes=True, ) with pytest.raises(ValueError, match=r'must be int64 dtype'): check_objectiv_dataframe(columns_to_check=['session_id'], df=fake_objectiv_df, check_dtypes=True) gc_series = fake_objectiv_df['global_contexts'].copy_override_dtype('json') check_objectiv_dataframe( columns_to_check=['global_contexts'], df=gc_series.to_frame(), check_dtypes=True, ) with pytest.raises(ValueError, match=r'must be objectiv_global_context'): check_objectiv_dataframe( columns_to_check=['global_contexts'], df=gc_series.to_frame(), check_dtypes=True, with_md_dtypes=True, )
python
import csv import cv2 import numpy as np from matplotlib import pyplot as plt lines=[] with open("./data/driving_log.csv") as csvfile: reader=csv.reader(csvfile) for line in reader: lines.append(line) images=[] measurements=[] for line in lines: source_path=line[0] filename=source_path.split('/')[-1] curr_path="./data/IMG/" + filename #print("curr_path: ", curr_path) image = cv2.imread(curr_path) images.append(image) measurement = float(line[3]) measurements.append(measurement) augmented_images, augmented_measurements = [],[] for image, measurement in zip(images, measurements): augmented_images.append(image) ## Horizontal flip image augmented_images.append(cv2.flip(image,1)) augmented_measurements.append(measurement) augmented_measurements.append(measurement* -1.0) X_train=np.array(augmented_images) y_train=np.array(augmented_measurements) print("X train shape", X_train.shape) print("y train shape", y_train.shape) from keras.models import Sequential from keras.layers import Flatten, Dense, Conv2D, Lambda, MaxPooling2D, Cropping2D input_shape = (160,320,3) model = Sequential() ##cropping image model.add(Cropping2D(cropping=((50,20),(0,0)), input_shape=input_shape)) ## normalized image model.add(Lambda(lambda x: x / 255.0 - 0.5)) ##add conv2D 5*5, 6 channel model.add(Conv2D(6, kernel_size=(5,5), padding='valid', activation='relu')) ## add MaxPooling2D layer, 2*2s model.add(MaxPooling2D(pool_size=(2,2), padding='valid')) ##add conv2D 5*5, 6 channel model.add(Conv2D(6, kernel_size=(5,5), padding='valid', activation='relu')) ## add MaxPooling2D layer, 2*2s model.add(MaxPooling2D(pool_size=(2,2), padding='valid')) model.add(Flatten()) model.add(Dense(120)) model.add(Dense(84)) model.add(Dense(1)) model.compile(optimizer="Adam", loss="mse") model.fit(X_train, y_train, validation_split=0.2, epochs=2,shuffle=True) model.save("model.h5")
python
#!/bin/python3 # Complete the 'plusMinus' function below. # # The function accepts INTEGER_ARRAY arr as parameter. def plusMinus(arr): n = len(arr) neg, zero, pos = 0, 0, 0 for num in arr: if num < 0: neg += 1 elif num == 0: zero += 1 else: pos += 1 print(pos/n, neg/n, zero/n, sep='\n') if __name__ == '__main__': n = int(input().strip()) arr = list(map(int, input().rstrip().split())) plusMinus(arr)
python
#!/usr/bin/python3 import datetime window = 15 sourcefile = '/home/lunpin/anom/unsw_nb15/csv/NUSW-NB15_GT.csv' for count, line in enumerate (open (sourcefile, 'rt')): if count == 0: continue try: ts = int (line [: line.find (',')]) dt = datetime.datetime.fromtimestamp (ts) addon = '/'.join ([str (dt.day), str (dt.hour), str (dt.minute//window)]) line = line.strip () + ',' + addon print (line) except: pass
python
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def plusOne(self, head): """ :type head: ListNode :rtype: ListNode """ non_nine, cur = None, head while cur: if cur.val != 9: non_nine = cur cur = cur.next if non_nine is None: dummy = ListNode(1) dummy.next = head non_nine = head else: dummy = head non_nine.val += 1 non_nine = non_nine.next while non_nine: non_nine.val = 0 non_nine = non_nine.next return dummy
python
from django.http import HttpResponse, HttpResponseRedirect, HttpRequest from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.shortcuts import render, get_object_or_404 from django.core.urlresolvers import reverse from django.views import generic from django.contrib.auth import authenticate, login import os import sys os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chemdb.settings") from sets import Set import django django.setup() from django.contrib.sessions.models import Session import pickle sys.path.insert(0, "/user/m27/pkg/openbabel/2.3.2/lib") import openbabel import pybel from chembddb.models import MolGraph, Publication, MolProp, Data, Method from chembddb.forms import * from django.contrib.auth import logout from django.views.decorators.csrf import csrf_protect from django.core.exceptions import ObjectDoesNotExist from django.shortcuts import render from django.conf import settings from django.core.files.storage import FileSystemStorage import matplotlib.pyplot as plt import numpy as np import subprocess @csrf_protect def register(request): if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): user = User.objects.create_user( username=form.cleaned_data['username'], password=form.cleaned_data['password1'], email=form.cleaned_data['email'], first_name=form.cleaned_data['first_name'], last_name=form.cleaned_data['last_name'] ) return HttpResponseRedirect('/chembddb/register/success/') else: form = RegistrationForm() # until the submit button is not pressed, the first if loop will not be entered. return render(request, 'registration/register.html', {'form': form}) def login(request): username = request.POST['username'], password = request.POST['password'], user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return render(request, '/chembddb/register/success/') else: return render(request, 'registration/login.html') def register_success(request): print "Here in Successful registration\n" return render(request, 'registration/success.html', ) def logout_page(request): logout(request) return HttpResponseRedirect('/chembddb/') def reviewRequest(request): check = MolGraph.objects.filter( verification=False) # check - gets all the objects that are to be reviewed by the admin. info = [] name_list = [] cids = map(lambda x: x.id, check) # returns the id's of check count = 0 # if request.method == 'GET': x = 0 for c in cids: count += 1 name = MolGraph.objects.get(pk=c).compound_str temp = name temp = temp + ";" + MolGraph.objects.get(pk=c).SMILES_str dataset = Data.objects.filter(mol_graph_id=c) for val in dataset: p = MolProp.objects.get(pk=val.property_id) temp = temp + ";" + str(p.prop) data = " " if (val.value != 0.0): data = str(val.value) + " " + p.unit temp = temp + ";" + data try: m = Method.objects.get(pk=val.met_id).method temp = temp + ";" + m except ObjectDoesNotExist: m = " " temp = temp + ";" + val.credit info.append(temp) name_list.append(name) if "approve_" + name in request.POST: ob = MolGraph.objects.get(pk=c) ob.verification = True ob.save() return HttpResponseRedirect('/chembddb/reviewrequest') elif "reject_" + name in request.POST: MolGraph.objects.get(id=c).delete() return HttpResponseRedirect('/chembddb/reviewrequest') # Do something lis = zip(info, name_list) return render(request, 'chembddb/reviewpage.html', {'lis': lis}) def submitRequest(request): err_list = [] prop_list = MolProp.objects.all() prop_strings = [] context = [] if request.method == 'POST': if request.method == 'POST' and request.FILES['myfile']: myfile = request.FILES['myfile'] fs = FileSystemStorage(location='/projects/academic/hachmann/shirish/python/mysite/chembddb/media') filename = fs.save(myfile.name, myfile) # print myfile.name uploaded_file_url = fs.url(filename) subprocess.Popen(['python','/projects/academic/hachmann/shirish/python/mysite/chembddb/feeder.py', filename]) # execfile("bulktry.py") return render(request, 'chembddb/requestpage.html', {'uploaded_file_url': uploaded_file_url}) elif (request.POST['molecule_string'] and request.POST['smiles_string']): mol_str = request.POST['molecule_string'] smiles_str = request.POST['smiles_string'] property_str = "" cand = MolGraph(compound_str=mol_str, SMILES_str=smiles_str, verification=False) cand.save() c = MolGraph.objects.filter(SMILES_str=smiles_str) cid = map(lambda x: x.id, c) pn = map(lambda x: x.prop, prop_list) pid = map(lambda x: x.id, prop_list) pdic = dict(zip(pn, pid)) for prop in prop_list: val = request.POST[prop.prop + "_val"] unit = request.POST[prop.prop + "_unit"] pub = request.POST[prop.prop + "_pub"] met = request.POST[prop.prop + "_met"] cm = request.POST[prop.prop + "_metcom"] if (val): if (pub): p = Publication(publ_str=pub) p.save() p1 = Publication.objects.filter(publ_str=pub) p1id = map(lambda x: x.id, p1) if (met): m = Method(method=met, comment=cm) m.save() m1 = Method.objects.filter(method=met) mid = map(lambda x: x.id, m1) n = Data(mol_graph_id=cid[0], publication_id=p1id[0], met_id=mid[0], property_id=pdic[prop.prop], value=float(val), credit=request.user.username) else: n = Data(mol_graph_id=cid[0], publication_id=p1id[0], property_id=pdic[prop.prop], value=float(val), credit=request.user.username) n.save() else: p = Publication(publ_str=" ") p.save() p1 = Publication.objects.filter(publ_str=" ") p1id = map(lambda x: x.id, p1) n = Data(mol_graph_id=cid[0], property_id=pdic[prop.prop], value=float(val), credit=request.user.username) n.save() return HttpResponseRedirect('/chembddb/') else: if (not request.POST['molecule_string']): err_list.append("Please provide a name to the molecule") if (not request.POST['smiles_string']): err_list.append("Please provide SMILES string for the molecule") return render(request, 'chembddb/requestpage.html', {'property_list': prop_list, 'error_list': err_list}) ''' This function is used to extract data from the database for chemical candidate compounds. To extract data, simple and advanced search is involved. Simple search is based on SMILES of a chemical compound. Whereas, the advanced search is based on the combination of desired properties. Also, Django Pagination is performed on the output pages of the search results. ''' def index(request): prop_list = MolProp.objects.all() mol_objects = [] mol_objects_smi = [] data_prop_search = [] mol_graph_id_set = Set() mol_graph_list = [] query_text = [] prop_names = map(lambda x: x.prop, prop_list) prop_id = map(lambda x: x.id, prop_list) context = {} key = 'my_mol_prob' if('queried_text' in request.POST.keys() and request.POST['queried_text']): # check if queried_text is entered at all and also check if the entered text is not empty query_text = request.POST['queried_text'] if request.POST['verified'] == "1": # POST is a dictionary which can be accessed through its key mol_objects_smi = MolGraph.objects.get(SMILES_str__exact=query_text,verification=True) # matches exactly else: mol_objects_smi = MolGraph.objects.get(SMILES_str__exact=query_text) # matches exactly mol_objects = Data.objects.filter(mol_graph_id=mol_objects_smi.id) elif (request.method == 'POST'): print "Its inside this loop" prop_from = [] prop_to = [] context['propname'] = [] context['from_field'] = [] context['to_field'] = [] for prop in prop_names: prop_from = "" # the value entered in MIN text area by user prop_to = "" # the value entered in MAX text area by user if (prop in request.POST.keys()): prop_from_text_field = prop + "_from_val" prop_to_text_field = prop + "_to_val" prop_unit_name = prop + "_unit" # This is to modify the unit of Bohr3 to C2m2J-1 if (prop_unit_name in request.POST.keys() and request.POST[prop_unit_name] == "coulomb"): prop_from = float(request.POST[prop_from_text_field]) prop_from = float(prop_from/(1.3305)) prop_to = float(request.POST[prop_to_text_field]) prop_to = float(prop_to/(1.3305)) # prop_from_text_field = prop_from_text_field/(1.3305*1e-9) # prop_to_text_field = prop_to_text_field/(1.3305*1e-9) # This is to modify the unit of Bohr3 to cm3 elif (prop_unit_name in request.POST.keys() and request.POST[prop_unit_name] == "cubiccm"): prop_from = float(request.POST[prop_from_text_field]) prop_from = float((prop_from)/(0.148)) prop_to = float(request.POST[prop_to_text_field]) prop_to = float((prop_to)/(0.148)) # This is to modify the unit for Hartree into Calories: elif (prop_unit_name in request.POST.keys() and request.POST[prop_unit_name] =="g/cm3"): prop_from = float(request.POST[prop_from_text_field]) prop_to = float(request.POST[prop_to_text_field]) prop_from = float(prop_from/0.001) prop_to = float(prop_to/0.001) # This is to modify the unit for Hartree into eV # elif (prop_unit_name in request.POST.keys() and request.POST[prop_unit_name] =="electrovolt"): # prop_from = float(request.POST[prop_from_text_field]) # prop_to = float(request.POST[prop_to_text_field]) # prop_from = float(prop_from/27.2114) # prop_to = float(prop_to/27.2114) else: if (prop_from_text_field in request.POST.keys() and request.POST[prop_from_text_field]): prop_from = request.POST[prop_from_text_field] if (prop_to_text_field in request.POST.keys() and request.POST[prop_to_text_field]): prop_to = request.POST[prop_to_text_field] if (prop_from or prop_to): data_prop_search = Data.objects.filter(property_id=request.POST[prop]) if (prop_to): data_prop_search = data_prop_search.filter(value__lte=prop_to) if (prop_from): data_prop_search = data_prop_search.filter(value__gte=prop_from) mol_id_set = Set(map(lambda x: x.mol_graph_id, data_prop_search)) if (len(mol_graph_id_set) > 0): mol_graph_id_set = mol_graph_id_set.intersection(mol_id_set) # When new set is added to the existing set this command helps to add only new set of data. else: mol_graph_id_set = mol_id_set context['from_field'].append(prop_from) context['to_field'].append(prop_to) mol_graph_list = mol_graph_id_set request.session[key] = pickle.dumps(mol_graph_list) mol_objects1 = pickle.loads(request.session[key]) mol_objects_smiles = Data.objects.all() mol_objects = mol_objects_smiles.filter(mol_graph_id__in=mol_objects1, property_id__in=prop_id).select_related('mol_graph').defer('met', 'publication', 'credit') query_text = request.GET.get('queried_text') elif ('page' in request.GET.keys()): # Page refers to other pages except Page 1 during adavnced search. mol_objects1 = pickle.loads(request.session[key]) mol_objects_smiles = Data.objects.all() mol_objects = mol_objects_smiles.filter(mol_graph_id__in=mol_objects1, property_id__in=prop_id).select_related('mol_graph').defer('met', 'publication', 'credit') elif ('page' not in request.GET.keys()): # For homepage request.session.modified = True key = 'my_mol_prob' mol_graph_list = [] context ={'request':request, 'query_text':query_text, 'property_list':prop_list} return render(request, 'chembddb/index.html', context) #For plotting of graph between two properties. page = request.GET.get('page') paginator = Paginator(mol_objects, 80) try: compounds = paginator.page(page) except PageNotAnInteger: compounds = paginator.page(1) except EmptyPage: compounds = paginator.page(paginator.num_pages) # Here the values from mol_objects is extracted to get only polymer property values. mol_obj = mol_objects.values_list('value', flat=True) paginator = Paginator(mol_obj, 80) try: compound2 = paginator.page(page) except PageNotAnInteger: compound2 = paginator.page(1) except EmptyPage: compound2 = paginator.page(paginator.num_pages) context['compound_list'] = mol_objects context['comp'] = compound2 # context['compound_count'] = compounds.count() context['property_list'] = prop_list context['pcount'] = prop_list.count() context['query_text'] = query_text context['compounds'] = compounds context['request'] = request #context['unit_list']=unit_list # context = {'compound_list': compound_list_query, 'property_list': prop_list, 'query_text': query_text,} return render(request, 'chembddb/index.html', context) # class DetailView(generic.DetailView): # model = MolGraph # template_name = 'chembddb/detail.html' def mol_detail(request, mol_graph_id): dataset = Data.objects.filter(mol_graph_id=mol_graph_id) prop_list = [] val_list = [] credit_list = [] ver_list = [] pub_list = [] met_list = [] info_list = [] info = " " unit_list = [] for val in dataset: p = MolProp.objects.get(pk=val.property_id) data = " " if (val.value != 0.0): data = str("%.2f" % val.value) prop_list.append(p.prop) if val.met_id: m = Method.objects.get(pk=val.met_id) met_list.append(m.method) else: met_list.append("") if MolGraph.objects.get(pk=mol_graph_id).verification == True: ver_list.append("Verified") else: ver_list.append("Not Verified") credit_list.append(val.credit) # if(p.unit_set.count()): # check if this property has unit # data = data + " " + p.unit data = data + " " val_list.append(data) # val_list.append(data) else: data = str("%.2f" % val.value) prop_list.append(p.prop) if val.met_id: m = Method.objects.get(pk=val.met_id) met_list.append(m.method) else: met_list.append("") if MolGraph.objects.get(pk=mol_graph_id).verification == True: ver_list.append("Verified") else: ver_list.append("Not Verified") credit_list.append(val.credit) # if(p.unit_set.count()): # check if this property has unit # data = data + " " + p.unit data = data + " " val_list.append(data) # val_list.append(data) # pub1=Publication.objects.get(pk = val.publication_id) pub_list.append(" ") smiles_str = str(MolGraph.objects.get(pk=mol_graph_id).SMILES_str) # obConversion = openbabel.OBConversion() # obConversion.SetInFormat("smi") # mol = openbabel.OBMol() # obConversion.ReadString(mol, smiles_str) '''Creating an XYZ file from SMILES String using Openbabel and Pybel function. This XYZ is used for 3D visualization of polymer molecule structure''' mol = pybel.readstring("smi", smiles_str) mol.make3D(forcefield="mmff94", steps= 100) mol.write("xyz", "./chembddb/static/xyz/mol.xyz", overwrite = True) smiles_string = str(MolGraph.objects.get(pk=mol_graph_id).SMILES_str) # mol = pybel.readstring("smi", smiles_str) info = str(mol.molwt) info_list.append(info) info = str(mol.formula) info_list.append(mol.formula) info = str(mol.OBMol.NumAtoms()) info_list.append(mol.OBMol.NumAtoms()) info = str(mol.OBMol.NumBonds()) info_list.append(info) context = {'smiles_str': smiles_string, 'detail_list': zip(prop_list, val_list, pub_list, met_list, ver_list, credit_list),'mol_id': mol_graph_id, 'info_list': info_list,} return render(request, 'chembddb/detail.html', context)
python
from mitmproxy.test import tutils from mitmproxy import tcp from mitmproxy import controller from mitmproxy import http from mitmproxy import connections from mitmproxy import flow def ttcpflow(client_conn=True, server_conn=True, messages=True, err=None): if client_conn is True: client_conn = tclient_conn() if server_conn is True: server_conn = tserver_conn() if messages is True: messages = [ tcp.TCPMessage(True, b"hello"), tcp.TCPMessage(False, b"it's me"), ] if err is True: err = terr() f = tcp.TCPFlow(client_conn, server_conn) f.messages = messages f.error = err f.reply = controller.DummyReply() return f def tflow(client_conn=True, server_conn=True, req=True, resp=None, err=None): """ @type client_conn: bool | None | mitmproxy.proxy.connection.ClientConnection @type server_conn: bool | None | mitmproxy.proxy.connection.ServerConnection @type req: bool | None | mitmproxy.proxy.protocol.http.HTTPRequest @type resp: bool | None | mitmproxy.proxy.protocol.http.HTTPResponse @type err: bool | None | mitmproxy.proxy.protocol.primitives.Error @return: mitmproxy.proxy.protocol.http.HTTPFlow """ if client_conn is True: client_conn = tclient_conn() if server_conn is True: server_conn = tserver_conn() if req is True: req = tutils.treq() if resp is True: resp = tutils.tresp() if err is True: err = terr() if req: req = http.HTTPRequest.wrap(req) if resp: resp = http.HTTPResponse.wrap(resp) f = http.HTTPFlow(client_conn, server_conn) f.request = req f.response = resp f.error = err f.reply = controller.DummyReply() return f def tclient_conn(): """ @return: mitmproxy.proxy.connection.ClientConnection """ c = connections.ClientConnection.from_state(dict( address=dict(address=("address", 22), use_ipv6=True), clientcert=None, ssl_established=False, timestamp_start=1, timestamp_ssl_setup=2, timestamp_end=3, sni="address", cipher_name="cipher", tls_version="TLSv1.2", )) c.reply = controller.DummyReply() return c def tserver_conn(): """ @return: mitmproxy.proxy.connection.ServerConnection """ c = connections.ServerConnection.from_state(dict( address=dict(address=("address", 22), use_ipv6=True), source_address=dict(address=("address", 22), use_ipv6=True), ip_address=None, cert=None, timestamp_start=1, timestamp_tcp_setup=2, timestamp_ssl_setup=3, timestamp_end=4, ssl_established=False, sni="address", via=None )) c.reply = controller.DummyReply() return c def terr(content="error"): """ @return: mitmproxy.proxy.protocol.primitives.Error """ err = flow.Error(content) return err
python
from .imports import * from .utils.core import * from .utils.extras import * def optimizer_params(params, lr, wd): return {'params': chain_params(params), 'lr': lr, 'wd': wd} class LayerOptimizer(object): def __init__(self, optimizer, layer_groups, lrs, wds=None): if not isinstance(layer_groups, (list, tuple)): layer_groups = [layer_groups] lrs = listify(lrs, layer_groups) if wds is None: wds = 0. wds = listify(wds, layer_groups) self.layer_groups = layer_groups self.lrs = lrs self.wds = wds self.opt = optimizer(self.optimizer_params()) def optimizer_params(self): assert len(self.layer_groups) == len(self.lrs), (f'size mismatch, ' f'expected {len(self.layer_groups)} lrs, but got {len(self.lrs)}') assert len(self.layer_groups) == len(self.wds), (f'size mismatch, ' f'expected {len(self.layer_groups)} wds, but got {len(self.wds)}') params = list(zip(self.layer_groups, self.lrs, self.wds)) return [optimizer_params(*p) for p in params] @property def lr(self): return self.lrs[-1] @property def momentum(self): if 'betas' in self.opt.param_groups[0]: return self.opt.param_groups[0]['betas'][0] else: return self.opt.param_groups[0]['momentum'] def set_lrs(self, lrs): lrs = listify(lrs, self.layer_groups) set_lrs(self.opt, lrs) self.lrs = lrs def set_wds_out(self, wds): wds = listify(wds, self.layer_groups) set_wds_out(self.opt, wds) set_wds(self.opt, [0] * len(self.layer_groups)) self.wds = wds def set_wds(self, wds): wds = listify(wds, self.layer_groups) set_wds(self.opt, wds) set_wds_out(self.opt, [0] * len(self.layer_groups)) self.wds = wds def set_momentum(self, momentum): if 'betas' in self.opt.param_groups[0]: for pg in self.opt.param_groups: pg['betas'] = (momentum, pg['betas'][1]) else: for pg in self.opt.param_groups: pg['momentum'] = momentum def set_beta(self, beta): if 'betas' in self.opt.param_groups[0]: for pg in self.opt.param_groups: pg['betas'] = (pg['betas'][0],beta) elif 'alpha' in self.opt.param_groups[0]: for pg in self.opt.param_groups: pg['alpha'] = beta def set_optimizer(self, optimizer): if type(self.opt) != type(optimizer(self.optimizer_params())): self.opt = optimizer(self.optimizer_params()) def _strict_zip(x, y): assert len(x) == len(y), \ f'size mismatch, expected size {len(x)}, but got {len(x)} and {len(y)}' return zip(x, y) def set_lrs(opt, lrs): lrs = listify(lrs, opt.param_groups) for pg, lr in _strict_zip(opt.param_groups, lrs): pg['lr'] = lr def set_wds_out(opt, wds): wds = listify(wds, opt.param_groups) for pg, wd in _strict_zip(opt.param_groups, wds): pg['wd'] = wd # why is this different? def set_wds(opt, wds): wds = listify(wds, opt.param_groups) for pg, wd in _strict_zip(opt.param_groups, wds): pg['weight_decay'] = wd
python
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __version__ 1.0.0 """ #import operator import random #import matplotlib.pyplot import time def distance_between(a, b): """ A function to calculate the distance between agent a and agent b. Args: a: A list of two coordinates for orthoganol axes. b: A list of two coordinates for the same orthoganol axes as a. Returns: The straight line distance between the a and b in the an plane given by two orthoganol axes. """ distance = ((a[1] - b[1])**2 + (a[0] - b[0])**2)**0.5 ##print("distance =", str(distance)) return distance ''' Step 1: Initialise parameters ''' print("Step 1: Initialise parameters") num_of_agents = 1000 num_of_iterations = 1000 rangey = 100 rangex = 50 deltarange = 10 random_seed = 0 # Try varying this to get different results. print("num_of_agents", num_of_agents) print("num_of_iterations", num_of_iterations) print("rangey", rangey) print("rangex", rangex) print("deltarange", deltarange) print("random_seed", random_seed) random.seed(random_seed) ''' Step 2: Initialise agents. ''' print("Step 2: Initialise agents.") agents = [] # Create a new empty list for coordinates. # Populate agents adding agents with random locations for i in range(num_of_agents): agents.append([random.randint(0,rangey),random.randint(0,rangex)]) ## Print x, y locations of agents #for i in range(num_of_agents): # print("agents[" + str(i) + "] y =", agents[i][0], "x =", agents[i][1]) ''' Step 3: Move each agent up to a small (deltarange) random amount in x and y directions num_of_iterations times. This implements a torus where agents moving off the bottom move onto the top and those moving off the left move onto the right and vice versa. ''' start = time.clock() print("Step 3: Move each agent up to a small (deltarange) random amount in", "x and y directions num_of_iterations times. This implements a torus", "where agents moving off the bottom move onto the top and those moving", "off the left move onto the right and vice versa.") for j in range(num_of_iterations): for i in range(num_of_agents): # Move y deltay = random.randint(-deltarange, deltarange) #print("deltay ", deltay) agents[i][0] = (agents[i][0] + deltay) % rangey # Move x deltax = random.randint(-deltarange, deltarange) #print("deltax ", deltax) agents[i][1] = (agents[i][1] + deltax) % rangex ## Print x, y locations #for i in range(num_of_agents): # #print(str(i), agents[i][0]) # # str(i) is used to force i to be regarded as a string. # print("agents[" + str(i) + "] y =", agents[i][0], "x =", agents[i][1]) end = time.clock() print("time = " + str(end - start)) ''' Step 4: Calculate maximum and minimum distance between agents. ''' print("Step 4: Calculate maximum and minimum distance between agents.") # Time how long this takes to calculate start = end maxdistance = distance_between(agents[0], agents[1]) mindistance = maxdistance for i in range(num_of_agents): #for j in range(num_of_agents): # Timed with and without this optimisation for j in range(i, num_of_agents): #for j in range(num_of_agents): #if (i != j): # Faster without this if statement! #if (i > j): # print("i=", i,"j=", j) distance = distance_between(agents[i], agents[j]) maxdistance = max(maxdistance, distance) mindistance = min(mindistance, distance) #print("maxdistance=", maxdistance) #print("mindistance=", mindistance) print("maxdistance=", maxdistance) print("mindistance=", mindistance) end = time.clock() print("time = " + str(end - start)) """ This code is commented out as this program was all about testing timings. ''' Step 4: Calculate, store and print out the element of agents with the largest and smallest first and second elements. ''' print("Step 5: Calculate, store and print out the element of agents with the", "largest and smallest first and second elements.") maxy = max(agents, key=operator.itemgetter(0)) print("Element of agents with the largest first element", maxy) miny = min(agents, key=operator.itemgetter(0)) print("Element of agents with the smallest first element", miny) maxx = max(agents, key=operator.itemgetter(1)) print("Element of agents with the largest second element", maxx) minx = min(agents, key=operator.itemgetter(1)) print("Element of agents with the smallest second element", minx) ''' Step 5: Plot agents. ''' print("Step 6: Plot agents.") matplotlib.pyplot.ylim(0, rangex) # This is why I think it is odd axis order! matplotlib.pyplot.xlim(0, rangey) # Plot all agents print("Plot all agents black.") for i in range(num_of_agents): matplotlib.pyplot.scatter(agents[i][0],agents[i][1], color='black') # Plot agent with the maxy blue. print("Plot agent with the maxy blue.") matplotlib.pyplot.scatter(maxy[0], maxy[1], color='blue') # Plot agent with the miny red. print("Plot agent with the miny red.") matplotlib.pyplot.scatter(miny[0], miny[1], color='red') # Plot agent with the maxy blue. print("Plot agent with the maxx pink.") matplotlib.pyplot.scatter(maxx[0], maxx[1], color='pink') # Plot agent with the miny red. print("Plot agent with the minx green.") matplotlib.pyplot.scatter(minx[0], minx[1], color='green') matplotlib.pyplot.show() """
python
import requests import requests_cache import os import argparse import json requests_cache.install_cache("route_cache") def main(): parser = argparse.ArgumentParser() parser.add_argument("trip_file", help="Path to a file containing lines with six comma-separated values: src_id, src_lat, src_lng, dest_id, dest_lat, dest_lng") parser.add_argument("--geojson_file", default="trips.geojson", help="Path at which to write the resulting geojson data") args = parser.parse_args() geojson_data = {"type" : "FeatureCollection", "features" : []} with open(args.trip_file, "r") as handle: for line in handle: src_id, src_lat, src_lng, dest_id, dest_lat, dest_lng = line.strip().split(",") src_lat, src_lng, dest_lat, dest_lng = [float(v) for v in [src_lat, src_lng, dest_lat, dest_lng]] route_info = get_route(src_lat, src_lng, dest_lat, dest_lng, os.environ["BING_MAPS_API_KEY"]) json_route_info = format_geojson(src_id, dest_id, route_info) geojson_data["features"].append(json_route_info) with open(args.geojson_file, "w+") as out_handle: json.dump(geojson_data, out_handle, indent=4) def format_geojson(src_id, dest_id, route_info): route = route_info["resourceSets"][0]["resources"][0] #print(json.dumps(route, indent=4)) return { "type" : "Feature", "properties" : { "src_id" : src_id, "dest_id" : dest_id, "distanceUnit" : route["distanceUnit"], "durationUnit" : route["durationUnit"], "traveDistance" : route["travelDistance"], "travelDuration" : route["travelDuration"], "actualStart" : route["routeLegs"][0]["actualStart"], "actualEnd" : route["routeLegs"][-1]["actualEnd"], }, "geometry" : { "type" : "LineString", "coordinates" : [[c[1], c[0]] for c in route["routePath"]["line"]["coordinates"]] } } def get_route(src_lat, src_lng, dest_lat, dest_lng, api_key): route_response = requests.get("http://dev.virtualearth.net/REST/v1/Routes?wayPoint.1={0},{1}&wayPoint.2={2},{3}&routeAttributes=routePath&key={4}" .format(src_lat, src_lng, dest_lat, dest_lng, api_key)) return route_response.json() if __name__ == "__main__": main()
python
from __future__ import print_function, division, absolute_import import logging from ..utils import infer_storage_options from s3fs import S3FileSystem from . import core logger = logging.getLogger(__name__) class DaskS3FileSystem(S3FileSystem, core.FileSystem): sep = '/' def __init__(self, key=None, username=None, secret=None, password=None, path=None, host=None, s3=None, **kwargs): if username is not None: if key is not None: raise KeyError("S3 storage options got secrets argument " "collision. Please, use either `key` " "storage option or password field in URLpath, " "not both options together.") key = username if key is not None: kwargs['key'] = key if password is not None: if secret is not None: raise KeyError("S3 storage options got secrets argument " "collision. Please, use either `secret` " "storage option or password field in URLpath, " "not both options together.") secret = password if secret is not None: kwargs['secret'] = secret # S3FileSystem.__init__(self, kwargs) # not sure what do do here S3FileSystem.__init__(self, **kwargs) def _trim_filename(self, fn): so = infer_storage_options(fn) return so.get('host', '') + so['path'] def open(self, path, mode='rb'): s3_path = self._trim_filename(path) f = S3FileSystem.open(self, s3_path, mode=mode) return f def glob(self, path): s3_path = self._trim_filename(path) return ['s3://%s' % s for s in S3FileSystem.glob(self, s3_path)] def mkdirs(self, path): pass # no need to pre-make paths on S3 def ukey(self, path): s3_path = self._trim_filename(path) return self.info(s3_path)['ETag'] def size(self, path): s3_path = self._trim_filename(path) return self.info(s3_path)['Size'] core._filesystems['s3'] = DaskS3FileSystem
python
import numpy as np def neuron_sparse_ratio(x): return np.sum(x == 0.0) / float(np.prod(x.shape)) def feature_sparse_ratio(x): assert np.ndim(x) == 2 return np.sum(np.linalg.norm(x, ord=2, axis=1) == 0.0) / float(x.shape[0]) def deepint_stat(estimator): # Init stat = {} embedding_stat = {} pin_stat = {} # Computing for each in estimator.get_variable_names(): if 'FTRL' not in each.upper() and 'ADAM' not in each.upper(): var = estimator.get_variable_value(each) if 'Embedding' in each: embedding_stat[each] = { 'shape': var.shape, 'sparse_ratio': neuron_sparse_ratio(var), 'feature_sparse_ratio': feature_sparse_ratio(var) } elif 'PIN' in each: pin_stat[each] = { 'shape': var.shape, 'sparse_ratio': neuron_sparse_ratio(var) } # Embedding Analysis num_neuron = 0 num_zero_neuron = 0 num_feature = 0 num_zero_feature = 0 for each in embedding_stat: num_neuron += np.prod(embedding_stat[each]['shape']) num_zero_neuron += np.prod(embedding_stat[each]['shape']) * embedding_stat[each]['sparse_ratio'] num_feature += embedding_stat[each]['shape'][0] num_zero_feature += embedding_stat[each]['shape'][0] * embedding_stat[each]['feature_sparse_ratio'] stat['Embedding_Weights'] = num_neuron stat['Embedding_Sparse_Ratio'] = num_zero_neuron / num_neuron stat['Embedding_Feature_Sparse_Ratio'] = num_zero_feature / num_feature # PIN Analysis num_neuron = 0 num_zero_neuron = 0 for each in pin_stat: num_neuron += np.prod(pin_stat[each]['shape']) num_zero_neuron += np.prod(pin_stat[each]['shape']) * pin_stat[each]['sparse_ratio'] stat['PIN_Weights'] = num_neuron stat['PIN_Sparse_Ratio'] = num_zero_neuron / num_neuron # Total Analysis stat['Total_Weights'] = stat['Embedding_Weights'] + stat['PIN_Weights'] stat['Total_Sparse_Ratio'] = ( stat['Embedding_Weights'] * stat['Embedding_Sparse_Ratio'] + stat['PIN_Weights'] * stat['PIN_Sparse_Ratio'] ) / stat['Total_Weights'] # Return return (embedding_stat, pin_stat, stat)
python
#!/usr/bin/env python # -*- coding: utf-8 -*- r"""Provide the Cartesian acceleration task. The Cartesian acceleration task tries to impose a desired pose, velocity and acceleration profiles for a distal link with respect to a base link, or world frame. Before presenting the optimization problem, here is a small reminder. The acceleration is the time derivative of the velocity, i.e. :math:`a = \frac{dv}{dt}` where the cartesian velocities are related to joint velocities by :math:`v = J(q) \dot{q}` where :math:`J(q)` is the Jacobian, thus deriving that expression wrt time gives us: .. math:: a = \frac{d}{dt} v = \frac{d}{dt} J(q) \dot{q} = J(q) \ddot{q} + \dot{J}(q) \dot{q}. Now, we can formulate our minimization problem as: .. math:: || J(q) \ddot{q} + \dot{J} \dot{q} - (a_d + K_d (v_d - v) + K_p e) ||^2, where :math:`\ddot{q}` are the joint accelerations being optimized, :math:`a_d` are the desired cartesian accelerations, :math:`v_d = [v_d^\top, \omega_d^\top]^\top` are the desired cartesian velocities, :math:`v` are the current cartesian velocities of the distal link wrt the base, :math:`J(q) \in \mathbb{R}^{6 \times N}` is the Jacobian taken from the base to the distal link, :math:`K_p` and :math:`K_d` are the stiffness and damping gains respectively, :math:`e \in \mathbb{R}^{6}` is the error which is the concatenation of the position error given by :math:`e_{p} = (x_d - x)` (with :math:`x_d` being the desired position, and :math:`x` the current position), and the orientation error given by (if expressed as quaternions :math:`o = {s, v}` where :math:`s` is the real scalar part, and :math:`v` is the vector part) :math:`e_{o} = s v_d - s_d v - v_d \cross v`, and :math:`\dot{x}_d` is the desired cartesian velocity for the distal link with respect to the base link. The above formulation is equivalent to the QP objective function :math:`||Ax - b||^2`, by setting :math:`A = J(q)`, :math:`x = \ddot{q}`, and :math:`b = - \dot{J} \dot{q} + (a_d + K_d (v_d - v) + K_p e)`. This task can, for instance, be used for foot pose tracking when this one is not in contact with the ground. If the foot is in contact, we switch to a foot damping task which can be achieved by setting :math:`a_d = v_d = e = 0` and thus we are trying to solve :math:`||J(q) \ddot{q} - \dot{J} \dot{q} - K_d v_d||^2`. Inverse dynamics ---------------- Once the optimal joint accelerations :math:`\ddot{q}^*` have been computed, we can use inverse dynamics to compute the corresponding torques to apply on the joints. This is given by: .. math:: \tau = H(q) \ddot{q} + N(q,\dot{q)} where :math:`H(q)` is the inertia joint matrix, and N(q, \dot{q}) is a vector force that accounts for all the other non-linear forces acting on the system (Coriolis, centrifugal, gravity, external forces, friction, etc.). Important notes: - You don't have to specify the whole pose, you can also only specify the position or orientation. - You can also only specify the desired cartesian accelerations by setting `kp` and `kd` to zero; you don't have neither to provide the desired cartesian velocities, position or orientation. .. seealso:: `tasks/velocity/cartesian.py` and `tasks/torque/cartesian_impedance_control.py` The implementation of this class is inspired by [1] (which is licensed under the LGPLv2). References: - [1] "OpenSoT: A whole-body control library for the compliant humanoid robot COMAN", Rocchi et al., 2015 """ import numpy as np from pyrobolearn.priorities.tasks import JointAccelerationTask from pyrobolearn.utils.transformation import quaternion_error __author__ = "Brian Delhaisse" __copyright__ = "Copyright 2019, PyRoboLearn" __credits__ = ["Arturo Laurenzi (C++)", "Songyan Xin (insight)", "Brian Delhaisse (Python + doc)"] __license__ = "GNU GPLv3" __version__ = "1.0.0" __maintainer__ = "Brian Delhaisse" __email__ = "[email protected]" __status__ = "Development" class CartesianAccelerationTask(JointAccelerationTask): r"""Cartesian Acceleration Task The Cartesian acceleration task tries to impose a desired pose, velocity and acceleration profiles for a distal link with respect to a base link, or world frame. Before presenting the optimization problem, here is a small reminder. The acceleration is the time derivative of the velocity, i.e. :math:`a = \frac{dv}{dt}` where the cartesian velocities are related to joint velocities by :math:`v = J(q) \dot{q}` where :math:`J(q)` is the Jacobian, thus deriving that expression wrt time gives us: .. math:: a = \frac{d}{dt} v = \frac{d}{dt} J(q) \dot{q} = J(q) \ddot{q} + \dot{J}(q) \dot{q}. Now, we can formulate our minimization problem as: .. math:: || J(q) \ddot{q} + \dot{J} \dot{q} - (a_d + K_d (v_d - v) + K_p e) ||^2, where :math:`\ddot{q}` are the joint accelerations being optimized, :math:`a_d` are the desired cartesian accelerations, :math:`v_d = [v_d^\top, \omega_d^\top]^\top` are the desired cartesian velocities, :math:`v` are the current cartesian velocities of the distal link wrt the base, :math:`J(q) \in \mathbb{R}^{6 \times N}` is the Jacobian taken from the base to the distal link, :math:`K_p` and :math:`K_d` are the stiffness and damping gains respectively, :math:`e \in \mathbb{R}^{6}` is the error which is the concatenation of the position error given by :math:`e_{p} = (x_d - x)` (with :math:`x_d` being the desired pose, and :math:`x` the current pose), and the orientation error given by (if expressed as quaternions :math:`o = {s, v}` where :math:`s` is the real scalar part, and :math:`v` is the vector part) :math:`e_{o} = s v_d - s_d v - v_d \cross v`, and :math:`\dot{x}_d` is the desired cartesian velocity for the distal link with respect to the base link. The above formulation is equivalent to the QP objective function :math:`||Ax - b||^2`, by setting :math:`A = J(q)`, :math:`x = \ddot{q}`, and :math:`b = - \dot{J} \dot{q} + (a_d + K_d (v_d - v) + K_p e)`. This task can, for instance, be used for foot pose tracking when this one is not in contact with the ground. If the foot is in contact, we switch to a foot damping task which can be achieved by setting :math:`a_d = v_d = e = 0` and thus we are trying to solve :math:`||J(q) \ddot{q} - \dot{J} \dot{q} - K_d v_d||^2`. Inverse dynamics ---------------- Once the optimal joint accelerations :math:`\ddot{q}^*` have been computed, we can use inverse dynamics to compute the corresponding torques to apply on the joints. This is given by: .. math:: \tau = H(q) \ddot{q} + N(q,\dot{q)} where :math:`H(q)` is the inertia joint matrix, and N(q, \dot{q}) is a vector force that accounts for all the other non-linear forces acting on the system (Coriolis, centrifugal, gravity, external forces, friction, etc.). .. seealso:: `tasks/velocity/cartesian.py` and `tasks/torque/cartesian_impedance_control.py` The implementation of this class is inspired by [1] (which is licensed under the LGPLv2). References: - [1] "OpenSoT: A whole-body control library for the compliant humanoid robot COMAN", Rocchi et al., 2015 """ def __init__(self, model, distal_link, base_link=None, local_position=(0, 0, 0), desired_position=None, desired_orientation=None, desired_linear_velocity=None, desired_angular_velocity=None, desired_linear_acceleration=None, desired_angular_acceleration=None, kp_position=1., kp_orientation=1., kd_linear=1., kd_angular=1., weight=1., constraints=[]): """ Initialize the task. Args: model (ModelInterface): model interface. distal_link (int, str): distal link id or name. base_link (int, str, None): base link id or name. If None, it will be the world. local_position (np.array[float[3]]): local position on the distal link. desired_position (np.array[float[3]], None): desired position of distal link wrt the base. If None, it will not be taken into account. desired_orientation (np.array[float[4]], None): desired orientation (expressed as quaternion [x,y,z,w]) of distal link wrt the base. If None, it will not be taken into account. desired_linear_velocity (np.array[float[3]], None): desired linear velocity of distal link wrt the base. If None, it will be set to zero. desired_angular_velocity (np.array[float[3]], None): desired angular velocity of distal link wrt the base. If None, it will be set to zero. desired_linear_acceleration (np.array[float[3]], None): desired linear acceleration of distal link wrt the base. If None, it will be set to zero. desired_angular_acceleration (np.array[float[3]], None): desired angular acceleration of distal link wrt the base. If None, it will be set to zero. kp_position (float, np.array[float[3,3]]): position stiffness gain. kp_orientation (float, np.array[float[3,3]]): orientation stiffness gain. kd_linear (float, np.array[float[3,3]]): linear velocity damping gain. kd_angular (float, np.array[float[3,3]]): angular velocity damping gain. weight (float, np.array[float[6,6]], np.array[float[3,3]]): weight scalar or matrix associated to the task. constraints (list[Constraint]): list of constraints associated with the task. """ super(CartesianAccelerationTask, self).__init__(model=model, weight=weight, constraints=constraints) # define variables self.distal_link = self.model.get_link_id(distal_link) self.base_link = self.model.get_link_id(base_link) if base_link is not None else base_link self.local_position = local_position if base_link is not None: raise NotImplementedError("Currently, the base_link can only be set to the world (None).") # gains self.kp_position = kp_position self.kp_orientation = kp_orientation self.kd_linear = kd_linear self.kd_angular = kd_angular # define desired references self.desired_position = desired_position self.desired_orientation = desired_orientation self.desired_linear_velocity = desired_linear_velocity self.desired_angular_velocity = desired_angular_velocity self.desired_linear_acceleration = desired_linear_acceleration self.desired_angular_acceleration = desired_angular_acceleration # first update self.update() ############## # Properties # ############## @property def desired_position(self): """Get the desired cartesian position for the distal link wrt the base.""" return self._des_pos @desired_position.setter def desired_position(self, position): """Set the desired cartesian position for the distal link wrt the base.""" if position is not None: if not isinstance(position, (np.ndarray, list, tuple)): raise TypeError("Expecting the given desired position to be a np.array, instead got: " "{}".format(type(position))) position = np.asarray(position) if len(position) != 3: raise ValueError("Expecting the given desired position array to be of length 3, but instead got: " "{}".format(len(position))) self._des_pos = position @property def desired_orientation(self): """Get the desired cartesian orientation (expressed as a quaternion [x,y,z,w]) for the distal link wrt the base.""" return self._des_quat @desired_orientation.setter def desired_orientation(self, orientation): """Set the desired cartesian orientation (expressed as a quaternion [x,y,z,w]) for the distal link wrt the base.""" if orientation is not None: if not isinstance(orientation, (np.ndarray, list, tuple)): raise TypeError("Expecting the given desired orientation to be a np.array, instead got: " "{}".format(type(orientation))) orientation = np.asarray(orientation) if len(orientation) != 4: raise ValueError( "Expecting the given desired orientation array to be of length 4, but instead got: " "{}".format(len(orientation))) self._des_quat = orientation @property def desired_linear_velocity(self): """Get the desired cartesian linear velocity of the distal link wrt the base.""" return self._des_lin_vel @desired_linear_velocity.setter def desired_linear_velocity(self, velocity): """Set the desired cartesian linear velocity of the distal link wrt the base.""" if velocity is None: velocity = np.zeros(3) elif not isinstance(velocity, (np.ndarray, list, tuple)): raise TypeError("Expecting the given desired linear velocity to be a np.array, instead got: " "{}".format(type(velocity))) velocity = np.asarray(velocity) if len(velocity) != 3: raise ValueError("Expecting the given desired linear velocity array to be of length 3, but instead " "got: {}".format(len(velocity))) self._des_lin_vel = velocity @property def desired_angular_velocity(self): """Get the desired cartesian angular velocity of the distal link wrt the base.""" return self._des_ang_vel @desired_angular_velocity.setter def desired_angular_velocity(self, velocity): """Set the desired cartesian angular velocity of the distal link wrt the base.""" if velocity is None: velocity = np.zeros(3) elif not isinstance(velocity, (np.ndarray, list, tuple)): raise TypeError("Expecting the given desired angular velocity to be a np.array, instead got: " "{}".format(type(velocity))) velocity = np.asarray(velocity) if len(velocity) != 3: raise ValueError("Expecting the given desired angular velocity array to be of length 3, but instead " "got: {}".format(len(velocity))) self._des_ang_vel = velocity @property def desired_velocity(self): """Return the linear and angular velocity.""" return np.concatenate((self._des_lin_vel, self._des_ang_vel)) @property def desired_linear_acceleration(self): """Get the desired cartesian linear acceleration of the distal link wrt the base.""" return self._des_lin_acc @desired_linear_acceleration.setter def desired_linear_acceleration(self, acceleration): """Set the desired cartesian linear acceleration of the distal link wrt the base.""" if acceleration is None: acceleration = np.zeros(3) elif not isinstance(acceleration, (np.ndarray, list, tuple)): raise TypeError("Expecting the given desired linear acceleration to be a np.array, instead got: " "{}".format(type(acceleration))) acceleration = np.asarray(acceleration) if len(acceleration) != 3: raise ValueError("Expecting the given desired linear acceleration array to be of length 3, but instead " "got: {}".format(len(acceleration))) self._des_lin_acc = acceleration @property def desired_angular_acceleration(self): """Get the desired cartesian angular acceleration of the distal link wrt the base.""" return self._des_ang_acc @desired_angular_acceleration.setter def desired_angular_acceleration(self, acceleration): """Set the desired cartesian angular acceleration of the distal link wrt the base.""" if acceleration is None: acceleration = np.zeros(3) elif not isinstance(acceleration, (np.ndarray, list, tuple)): raise TypeError("Expecting the given desired angular acceleration to be a np.array, instead got: " "{}".format(type(acceleration))) acceleration = np.asarray(acceleration) if len(acceleration) != 3: raise ValueError("Expecting the given desired angular acceleration array to be of length 3, but instead " "got: {}".format(len(acceleration))) self._des_ang_acc = acceleration @property def desired_acceleration(self): """Return the linear and angular acceleration.""" return np.concatenate((self._des_lin_acc, self._des_ang_acc)) @property def x_desired(self): """Get the desired cartesian pose for the distal link wrt to the base.""" position = self.desired_position orientation = self.desired_orientation if position is not None: if orientation is not None: return np.concatenate((position, orientation)) return position return orientation @x_desired.setter def x_desired(self, x_d): """Set the desired cartesian pose for the distal link wrt to the base.""" if x_d is not None: if not isinstance(x_d, (np.ndarray, list, tuple)): raise TypeError( "Expecting the given desired pose to be a np.array, instead got: {}".format(type(x_d))) x_d = np.asarray(x_d) if len(x_d) == 3: # only position is provided x_d = np.concatenate((x_d, np.array([0., 0., 0., 1.]))) elif len(x_d) == 4: # only orientation is provided x_d = np.concatenate((np.zeros(3), x_d)) if len(x_d) != 7: raise ValueError("Expecting the given desired pose array to be of length 7 (3 for the position, " "and 4 for the orientation expressed as a quaternion [x,y,z,w]), instead got a " "length of: {}".format(len(x_d))) self._des_pos = x_d[:3] self._des_quat = x_d[3:] @property def dx_desired(self): """Get the desired cartesian velocity for the distal link wrt to the base.""" return np.concatenate((self._des_lin_vel, self._des_ang_vel)) @dx_desired.setter def dx_desired(self, dx_d): """Set the desired cartesian velocity for the distal link wrt to the base.""" if dx_d is not None: if not isinstance(dx_d, (np.ndarray, list, tuple)): raise TypeError("Expecting the given desired velocity to be a np.array, instead got: " "{}".format(type(dx_d))) dx_d = np.asarray(dx_d) if len(dx_d) == 3: # assume that it is the linear velocity dx_d = np.concatenate((dx_d, np.zeros(3))) if len(dx_d) != 6: raise ValueError("Expecting the given desired velocity array to be of length 6 (3 for the linear " "and 3 for the angular part), instead got a length of: {}".format(len(dx_d))) self._des_lin_vel = dx_d[:3] self._des_ang_vel = dx_d[3:] @property def ddx_desired(self): """Get the desired cartesian acceleration for the distal link wrt to the base.""" return np.concatenate((self._des_lin_acc, self._des_ang_acc)) @ddx_desired.setter def ddx_desired(self, ddx_d): """Set the desired cartesian acceleration for the distal link wrt to the base.""" if ddx_d is not None: if not isinstance(ddx_d, (np.ndarray, list, tuple)): raise TypeError("Expecting the given desired acceleration to be a np.array, instead got: " "{}".format(type(ddx_d))) ddx_d = np.asarray(ddx_d) if len(ddx_d) == 3: # assume that it is the linear acceleration ddx_d = np.concatenate((ddx_d, np.zeros(3))) if len(ddx_d) != 6: raise ValueError("Expecting the given desired acceleration array to be of length 6 (3 for the linear " "and 3 for the angular part), instead got a length of: {}".format(len(ddx_d))) self._des_lin_acc = ddx_d[:3] self._des_ang_acc = ddx_d[3:] @property def kp_position(self): """Return the position stiffness gain.""" return self._kp_pos @kp_position.setter def kp_position(self, kp): """Set the position stiffness gain.""" if kp is None: kp = 1. if not isinstance(kp, (float, int, np.ndarray)): raise TypeError("Expecting the given position stiffness gain kp to be an int, float, np.array, instead " "got: {}".format(type(kp))) if isinstance(kp, np.ndarray) and kp.shape != (3, 3): raise ValueError("Expecting the given position stiffness gain matrix kp to be of shape {}, but instead " "got shape: {}".format((3, 3), kp.shape)) self._kp_pos = kp @property def kp_orientation(self): """Return the orientation stiffness gain.""" return self._kp_quat @kp_orientation.setter def kp_orientation(self, kp): """Set the orientation stiffness gain.""" if kp is None: kp = 1. if not isinstance(kp, (float, int, np.ndarray)): raise TypeError("Expecting the given orientation stiffness gain kp to be an int, float, np.array, " "instead got: {}".format(type(kp))) if isinstance(kp, np.ndarray) and kp.shape != (3, 3): raise ValueError("Expecting the given orientation stiffness gain matrix kp to be of shape {}, but " "instead got shape: {}".format((3, 3), kp.shape)) self._kp_quat = kp @property def kd_linear(self): """Return the linear velocity damping gain.""" return self._kd_lin @kd_linear.setter def kd_linear(self, kd): """Set the linear velocity damping gain.""" if kd is None: kd = 1. if not isinstance(kd, (float, int, np.ndarray)): raise TypeError("Expecting the given linear velocity damping gain kd to be an int, float, np.array, " "instead got: {}".format(type(kd))) if isinstance(kd, np.ndarray) and kd.shape != (3, 3): raise ValueError("Expecting the given linear velocity damping gain matrix kd to be of shape {}, but " "instead got shape: {}".format((3, 3), kd.shape)) self._kd_lin = kd @property def kd_angular(self): """Return the angular velocity damping gain.""" return self._kd_ang @kd_angular.setter def kd_angular(self, kd): """Set the angular velocity damping gain.""" if kd is None: kd = 1. if not isinstance(kd, (float, int, np.ndarray)): raise TypeError("Expecting the given angular velocity damping gain kd to be an int, float, np.array, " "instead got: {}".format(type(kd))) if isinstance(kd, np.ndarray) and kd.shape != (3, 3): raise ValueError("Expecting the given angular velocity damping gain matrix kd to be of shape {}, but " "instead got shape: {}".format((3, 3), kd.shape)) self._kd_ang = kd ########### # Methods # ########### def set_desired_references(self, x_des, dx_des=None, ddx_des=None, *args, **kwargs): """Set the desired references. Args: x_des (np.array[float[7]], None): desired cartesian pose (position and quaternion [x,y,z,w]) of distal link wrt the base. If None, it will let the initial desired pose unchanged. dx_des (np.array[float[6]], None): desired cartesian velocity of distal link wrt the base. If None, it will let the initial desired velocities unchanged. ddx_des (np.array[float[6]], None): desired cartesian acceleration of distal link wrt the base. If None, it will let the initial desired accelerations unchanged. """ self.x_desired = x_des self.dx_desired = dx_des self.ddx_desired = ddx_des def get_desired_references(self): """Return the desired references. Returns: np.array[float[7]]: desired cartesian pose (position and quaternion [x,y,z,w]) of distal link wrt the base. np.array[float[6]]: desired cartesian velocity of distal link wrt the base. np.array[float[6]]: desired cartesian acceleration of distal link wrt the base. """ return self.x_desired, self.dx_desired, self.ddx_desired def _update(self, x=None): """ Update the task by computing the A matrix and b vector that will be used by the task solver. """ x = self.model.get_pose(link=self.distal_link, wrt_link=self.base_link) self._A = self.model.get_jacobian(link=self.distal_link, wrt_link=self.base_link, point=self.local_position) # shape: (6,N) vel = self.model.get_velocity(link=self.distal_link, wrt_link=self.base_link) jdotqdot = self.model.compute_JdotQdot(link=self.distal_link) # b = - \dot{J} \dot{q} + (a_d + K_d (v_d - v) + K_p e) b = -jdotqdot + self.desired_acceleration if self._des_quat is None: # only position and/or velocities if self._des_pos is None: # only velocities self._b = b + np.concatenate((np.dot(self.kd_linear, (self._des_lin_vel - vel[:3])), np.dot(self.kd_angular, (self._des_ang_vel - vel[3:])))) else: # only position self._A = self._A[:3] # compute position error error = (self._des_pos - x[:3]) # compute b vector lin_vel = np.dot(self.kd_linear, (self._des_lin_vel - vel[:3])) self._b = b[:3] + np.dot(self.kp_position, error) + lin_vel elif self._des_pos is None: # only orientation self._A = self._A[3:] # compute orientation error error = quaternion_error(quat_des=self._des_quat, quat_cur=x[3:]) # compute b vector ang_vel = np.dot(self.kd_angular, (self._des_ang_vel - vel[3:])) self._b = b[3:] + np.dot(self.kp_orientation, error) + ang_vel else: # both # compute position/orientation error position_error = (self._des_pos - x[:3]) orientation_error = quaternion_error(quat_des=self._des_quat, quat_cur=x[3:]) # compute b vector lin_vel = np.dot(self.kd_linear, (self._des_lin_vel - vel[:3])) ang_vel = np.dot(self.kd_angular, (self._des_ang_vel - vel[3:])) b_lin = np.dot(self.kp_position, position_error) + lin_vel b_ang = np.dot(self.kp_orientation, orientation_error) + ang_vel self._b = b + np.concatenate((b_lin, b_ang))
python
from arabic import toArabic as a from os.path import abspath, dirname from datetime import date dirpath = dirname(abspath(__file__)) days_of_the_week_verbose = ["Sunday","Monday","Tuesday","Wednesday","Wenesday","Wendsday","Thursday","Friday","Saturday"] days_of_the_week_abbreviated = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] # range generates a list of numbers from 1 to 31 # map converts everthing in the list to unicode days_of_the_month_as_numbers = map(unicode, list(reversed(range(1,32)))) + map(lambda n : u"0"+unicode(n),range(0, 10)) + map(a, list(reversed(range(1,32)))) # ordinal is a function that converts a number to its ordinal # for example it converts 22 to 22nd # we start it with __ because we want to keep it private __ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(n/10%10!=1)*(n%10<4)*n%10::4]) days_of_the_month_as_ordinal = [__ordinal(n) for n in range(1,32)] months_verbose = ["January","Febuary","February","March","April","May","June","July","August","September","October","November","December"] #for language in ("arabic", "french", "kurdish", "turkish"): for language in ["arabic"]: with open(dirpath + "/data/months_verbose/" + language + ".txt") as f: months_verbose += [line.strip().split(">")[0].strip() for line in f.read().decode("utf-8").split("\n") if line and not line.startswith("#")] months_abbreviated = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"] # range generates a list of numbers from 1 to 12 # map converts everthing in the list to unicode months_as_numbers = map(unicode,range(1,13)) + map(lambda n : u"0"+unicode(n),range(0, 10)) + map(a,range(1,13)) month_to_number = { "Jan": 1, "January": 1, "Feb": 2, "Febuary": 2, "February": 2, "Mar": 3, "March": 3, "Apr": 4, "April": 4, "May": 5, "Jun": 6, "June": 6, "Jul": 7, "July": 7, "Aug": 8, "August": 8, "Sep": 9, "Sept": 9, "September": 9, "Oct": 10, "October": 10, "Nov": 11, "November": 11, "Dec": 12, "December": 12 } for language in ["arabic"]: with open(dirpath + "/data/months_verbose/" + language + ".txt") as f: for line in f.read().decode("utf-8").split("\n"): if line and not line.startswith("#"): split = line.strip().split(">") month_to_number[split[0].strip()] = split[1].strip() current_year = date.today().year curent_year_abbreviated = int(str(current_year)[-2:]) years = map(unicode,range(1990, current_year+1)) + [u"15",u"16"] + map(a,range(1990, current_year+1))
python
# Generated by the protocol buffer compiler. DO NOT EDIT! # source: POGOProtos/Networking/Responses/CollectDailyBonusResponse.proto import sys _b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) from google.protobuf import descriptor as _descriptor from google.protobuf import message as _message from google.protobuf import reflection as _reflection from google.protobuf import symbol_database as _symbol_database from google.protobuf import descriptor_pb2 # @@protoc_insertion_point(imports) _sym_db = _symbol_database.Default() DESCRIPTOR = _descriptor.FileDescriptor( name='POGOProtos/Networking/Responses/CollectDailyBonusResponse.proto', package='POGOProtos.Networking.Responses', syntax='proto3', serialized_pb=_b('\n?POGOProtos/Networking/Responses/CollectDailyBonusResponse.proto\x12\x1fPOGOProtos.Networking.Responses\"\xab\x01\n\x19\x43ollectDailyBonusResponse\x12Q\n\x06result\x18\x01 \x01(\x0e\x32\x41.POGOProtos.Networking.Responses.CollectDailyBonusResponse.Result\";\n\x06Result\x12\t\n\x05UNSET\x10\x00\x12\x0b\n\x07SUCCESS\x10\x01\x12\x0b\n\x07\x46\x41ILURE\x10\x02\x12\x0c\n\x08TOO_SOON\x10\x03\x62\x06proto3') ) _sym_db.RegisterFileDescriptor(DESCRIPTOR) _COLLECTDAILYBONUSRESPONSE_RESULT = _descriptor.EnumDescriptor( name='Result', full_name='POGOProtos.Networking.Responses.CollectDailyBonusResponse.Result', filename=None, file=DESCRIPTOR, values=[ _descriptor.EnumValueDescriptor( name='UNSET', index=0, number=0, options=None, type=None), _descriptor.EnumValueDescriptor( name='SUCCESS', index=1, number=1, options=None, type=None), _descriptor.EnumValueDescriptor( name='FAILURE', index=2, number=2, options=None, type=None), _descriptor.EnumValueDescriptor( name='TOO_SOON', index=3, number=3, options=None, type=None), ], containing_type=None, options=None, serialized_start=213, serialized_end=272, ) _sym_db.RegisterEnumDescriptor(_COLLECTDAILYBONUSRESPONSE_RESULT) _COLLECTDAILYBONUSRESPONSE = _descriptor.Descriptor( name='CollectDailyBonusResponse', full_name='POGOProtos.Networking.Responses.CollectDailyBonusResponse', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='result', full_name='POGOProtos.Networking.Responses.CollectDailyBonusResponse.result', index=0, number=1, type=14, cpp_type=8, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), ], extensions=[ ], nested_types=[], enum_types=[ _COLLECTDAILYBONUSRESPONSE_RESULT, ], options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=101, serialized_end=272, ) _COLLECTDAILYBONUSRESPONSE.fields_by_name['result'].enum_type = _COLLECTDAILYBONUSRESPONSE_RESULT _COLLECTDAILYBONUSRESPONSE_RESULT.containing_type = _COLLECTDAILYBONUSRESPONSE DESCRIPTOR.message_types_by_name['CollectDailyBonusResponse'] = _COLLECTDAILYBONUSRESPONSE CollectDailyBonusResponse = _reflection.GeneratedProtocolMessageType('CollectDailyBonusResponse', (_message.Message,), dict( DESCRIPTOR = _COLLECTDAILYBONUSRESPONSE, __module__ = 'POGOProtos.Networking.Responses.CollectDailyBonusResponse_pb2' # @@protoc_insertion_point(class_scope:POGOProtos.Networking.Responses.CollectDailyBonusResponse) )) _sym_db.RegisterMessage(CollectDailyBonusResponse) # @@protoc_insertion_point(module_scope)
python
# Day 3 puzzle: https://adventofcode.com/2020/day/3 # In broad terms, count the passwords validated by the policy in place when created. from functools import reduce from puzzles import Puzzle from supporting import trimmed class ZeroThree(Puzzle): TREE = '#' def __init__(self): Puzzle.__init__(self, "03") def __puzzle__(self): return __file__ # This feels... evil! And I like it! def use(self, data): # Confirm able to import data. # for line in data: # print(line) # The incoming data, by default, is 'linear', or literally as the lines from the file. # May include line-ending whitespace characters... so, we're gonna trim any trailing, # or leading whitespace characters. self.data = trimmed(data) def traverse(self, right, down): x, y = (0, 0) # We always start at the top-left of the map. columns = len(self.data[0]) trees = 0 # And the top-left is [supposedly] garaunteed to be lacking trees. while y < len(self.data): # The length of the data is the number of rows in the map. # print(f"map[{x},{y}] = {self.data[y][x]}") trees += 1 if self.data[y][x] == ZeroThree.TREE else 0 # Roll around to the beginning of the map (effectively copying it to the right). x = (x + right) % columns y += down return trees def one(self): # The first part of the puzzle counts the number of trees encounterd on the map, with # a slope of right 3, down 1. return self.traverse(3, 1) def two(self): # The second part of the puzzle counts the number of trees encounterd on the map, over # a collection of iterations slope of right and down; each iteration's result is then # multiplied to the other counts, to arrive at the final answer. iterations = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)] # result = reduce(lambda product, term: product * term, expenses) # Heh, reduce() comes in handy again! Migh'zzle get some more practice using it. return reduce(lambda product, term: product * term, [self.traverse(right, down) for right, down in iterations])
python
from django import template from ssdfrontend.models import Target from ssdfrontend.models import User from utils.configreader import ConfigReader from django.db.models import Sum register = template.Library() @register.simple_tag def get_usedquota(theuser): try: user = User.objects.get(username=theuser) usedsize = Target.objects.filter(owner=user).aggregate(used_size=Sum('sizeinGB'))['used_size'] numtargets = Target.objects.filter(owner=user).count() if 'None' in str(usedsize): usedsize = 0 return str(usedsize) + ' GB across ' + str(numtargets) + ' targets' except: return "Not defined"
python
from minio import Minio import requests import io from minio.error import S3Error import youran class Min: def __init__(self): self.minioClient = Minio(f'{youran.MINIOIP}:{youran.MINIOPort}', access_key='minioadmin', secret_key='minioadmin', secure=False) def make_bucket(self,name): try: self.minioClient.make_bucket(name) return 0,'success' except S3Error as err: return -1,repr(err) def save(self,bucket_name,name,content): # response = requests.get(url, headers=headers.mobile, stream=True,timeout=30,verify=False) result = self.minioClient.put_object( bucket_name, name, io.BytesIO(content), length=-1, part_size=10*1024*1024, ) if result.object_name==name: return 0 def save_weibo(self,name,content): return self.save('weibo1',name,content) def get_img(self,bucket_name,name): # Get data of an object. response=None # try: response = self.minioClient.get_object(bucket_name, name) # Read data from response. return response.data # finally: # response.close() # response.release_conn() def get_weibo_media(self,ttype,name): return self.get_img('weibo1',f'{ttype}/'+name) def exist(self,name,t='imgs'): try: self.minioClient.stat_object('weibo1', t+'/'+name) return True except Exception as e: return False if __name__=='__main__': m=Min() rr=m.make_bucket('weibo1') print(rr) # rr=m.get_weibo_img('473ed7c0gy1fvwjohh750j20qo1bg7aj.jpg') # print(rr) # result=rr.save('https://img-cdn-qiniu.dcloud.net.cn/uniapp/doc/create1.png','test','imgs/create1.png') # # print(result) # List all object paths in bucket that begin with my-prefixname. objects = m.minioClient.list_objects('weibo1', prefix='imgs/', recursive=True) # print(objects) for obj in objects: print(obj.bucket_name, obj.object_name.encode('utf-8'), obj.last_modified, obj.etag, obj.size, obj.content_type) break
python
"""This module handles the weight factors and scaling. An adaptive restraints weight factor calculator is implemented, whereby the weight factor is doubled if a sufficiently large bond-RMSD is observed. Conversely, if a sufficiently small bond-RMSD is observed, then the weight factor is halved. """ from __future__ import division import random, time from cctbx import xray from libtbx import adopt_init_args from scitbx.array_family import flex import cctbx.maptbx.real_space_refinement_simple import scitbx.lbfgs from libtbx import group_args import qr from mmtbx.validation.ramalyze import ramalyze from mmtbx.validation.cbetadev import cbetadev from mmtbx.validation.rotalyze import rotalyze from mmtbx.validation.clashscore import clashscore from libtbx.utils import null_out def get_bonds_rmsd(restraints_manager, xrs): hd_sel = xrs.hd_selection() energies_sites = \ restraints_manager.select(~hd_sel).energies_sites( sites_cart = xrs.sites_cart().select(~hd_sel), compute_gradients = False) return energies_sites.bond_deviations()[2] class weights(object): def __init__(self, shake_sites = True, restraints_weight = None, data_weight = None, restraints_weight_scale = 1.0): adopt_init_args(self, locals()) if(self.data_weight is not None): self.weight_was_provided = True else: self.weight_was_provided = False self.restraints_weight_scales = flex.double([self.restraints_weight_scale]) self.r_frees = [] self.r_works = [] def scale_restraints_weight(self): if(self.weight_was_provided): return self.restraints_weight_scale *= 4.0 def adjust_restraints_weight_scale( self, fmodel, geometry_rmsd_manager, max_bond_rmsd, scale): adjusted = None if(self.weight_was_provided): return adjusted rw = fmodel.r_work() rf = fmodel.r_free() cctbx_rm_bonds_rmsd = get_bonds_rmsd( restraints_manager = geometry_rmsd_manager.geometry, xrs = fmodel.xray_structure) #### adjusted = False if(cctbx_rm_bonds_rmsd>max_bond_rmsd): self.restraints_weight_scale *= scale adjusted = True if(not adjusted and rf<rw): self.restraints_weight_scale /= scale adjusted = True if(not adjusted and cctbx_rm_bonds_rmsd<max_bond_rmsd and rf>rw and abs(rf-rw)*100.<5.): self.restraints_weight_scale /= scale adjusted = True if(not adjusted and cctbx_rm_bonds_rmsd<max_bond_rmsd and rf>rw and abs(rf-rw)*100.>5.): self.restraints_weight_scale *= scale adjusted = True #### self.r_frees.append(round(rf,4)) self.r_works.append(round(rw,4)) return adjusted def add_restraints_weight_scale_to_restraints_weight_scales(self): if(self.weight_was_provided): return self.restraints_weight_scales.append(self.restraints_weight_scale) def compute_weight(self, fmodel, rm, verbose=False): if(self.weight_was_provided): return random.seed(1) flex.set_random_seed(1) # fmodel_dc = fmodel.deep_copy() xrs = fmodel_dc.xray_structure.deep_copy_scatterers() if(self.shake_sites): xrs.shake_sites_in_place(mean_distance=0.2) fmodel_dc.update_xray_structure(xray_structure=xrs, update_f_calc=True) x_target_functor = fmodel_dc.target_functor() tgx = x_target_functor(compute_gradients=True) gx = flex.vec3_double(tgx.\ gradients_wrt_atomic_parameters(site=True).packed()) tc, gc = rm.target_and_gradients(sites_cart=xrs.sites_cart()) x = gc.norm() y = gx.norm() if verbose: print '>>> gradient norms c,x %0.2f %0.2f' % (x, y) # filter out large contributions gx_d = flex.sqrt(gx.dot()) sel = gx_d>flex.mean(gx_d)*6 y = gx.select(~sel).norm() # gc_d = flex.sqrt(gc.dot()) sel = gc_d>flex.mean(gc_d)*6 x = gc.select(~sel).norm() ################ if(y != 0.0): self.data_weight = x/y else: self.data_weight = 1.0 # ad hoc default fallback if verbose: print '>>> data_weight %0.2f' % self.data_weight class calculator(object): def __init__(self, fmodel=None, xray_structure=None, restraints_weight_scale = 1.0): assert [fmodel, xray_structure].count(None)==1 self.fmodel=None self.xray_structure=None if(fmodel is not None): self.fmodel = fmodel if(xray_structure is not None): self.xray_structure = xray_structure self.restraints_weight_scale = restraints_weight_scale def update_fmodel(self): if(self.fmodel is not None): self.fmodel.xray_structure.tidy_us() self.fmodel.xray_structure.apply_symmetry_sites() self.fmodel.update_xray_structure( xray_structure = self.fmodel.xray_structure, update_f_calc = True, update_f_mask = True) self.fmodel.update_all_scales(remove_outliers=False) else: self.xray_structure.tidy_us() self.xray_structure.apply_symmetry_sites() class sites_opt(object): """ General calculator for model geometry optimization. For native CCTBX restraints, restraints_manager and model.restraints_manager are the same things. However, restraints_manager can be an external entity, such as coming from external packeges (eg., QM). Ideally, and this is probably a TODO item for the future, any restraints_manager should always be in the model. dump_gradients is used for debugging. """ def __init__(self, model, max_shift, restraints_manager=None, dump_gradients=False, convergence_threshold=1.e-3, convergence_reached_times=3): self.model = model self.restraints_manager = restraints_manager self.dump_gradients = dump_gradients self.convergence_threshold = convergence_threshold self.convergence_reached_times = convergence_reached_times self.meat_convergence_criteria = 0 self.x = flex.double(self.model.size()*3, 0) self.n = self.x.size() self.f = None self.g = None self.f_start = None self.max_shift_between_resets = 0 self.sites_cart = self.model.get_sites_cart() self.bound_flags = flex.int(self.n, 2) self.lower_bound = flex.double([-1*max_shift]*self.n) self.upper_bound = flex.double([ max_shift]*self.n) def target_and_gradients(self): sites_plus_x = self.sites_cart+flex.vec3_double(self.x) self.f, self.g = self.restraints_manager.target_and_gradients( sites_cart = sites_plus_x) self.g = self.g.as_double() # For tests if(self.dump_gradients): from libtbx import easy_pickle easy_pickle.dump(self.dump_gradients, self.g) STOP() # if(self.f_start is None): self.f_start = self.f self.max_shift_between_resets = flex.max(flex.sqrt(( self.sites_cart - sites_plus_x).dot())) return self.f, self.g def compute_functional_and_gradients(self): return self.target_and_gradients() def apply_x(self): self.f_start = self.f self.model.set_sites_cart( sites_cart = self.sites_cart+flex.vec3_double(self.x)) self.x = flex.double(self.model.size()*3, 0) self.sites_cart = self.model.get_sites_cart() if(self.max_shift_between_resets < self.convergence_threshold): self.meat_convergence_criteria += 1 def converged(self): if(self.meat_convergence_criteria >= self.convergence_reached_times): return True return False def __call__(self): f, g = self.target_and_gradients() return self.x, f, g class sites(calculator): def __init__(self, fmodel=None, restraints_manager=None, weights=None, dump_gradients=None): adopt_init_args(self, locals()) self.x = None self.x_target_functor = None self.not_hd_selection = None # XXX UGLY self.initialize(fmodel = self.fmodel) def initialize(self, fmodel=None): self.not_hd_selection = ~self.fmodel.xray_structure.hd_selection() # XXX UGLY assert fmodel is not None self.fmodel = fmodel self.fmodel.xray_structure.scatterers().flags_set_grads(state=False) xray.set_scatterer_grad_flags( scatterers = self.fmodel.xray_structure.scatterers(), site = True) self.x = self.fmodel.xray_structure.sites_cart().as_double() self.x_target_functor = self.fmodel.target_functor() def calculate_weight(self, verbose=False): self.weights.compute_weight( fmodel = self.fmodel, rm = self.restraints_manager, verbose = verbose) def reset_fmodel(self, fmodel=None): if(fmodel is not None): self.initialize(fmodel=fmodel) self.fmodel = fmodel self.update_fmodel() def update_restraints_weight_scale(self, restraints_weight_scale): self.weights.restraints_weight_scale = restraints_weight_scale def update(self, x): self.x = flex.vec3_double(x) self.fmodel.xray_structure.set_sites_cart(sites_cart = self.x) self.fmodel.update_xray_structure( xray_structure = self.fmodel.xray_structure, update_f_calc = True) def target_and_gradients(self, x): self.update(x = x) rt, rg = self.restraints_manager.target_and_gradients(sites_cart = self.x) tgx = self.x_target_functor(compute_gradients=True) dt = tgx.target_work() dg = flex.vec3_double(tgx.\ gradients_wrt_atomic_parameters(site=True).packed()) t = dt*self.weights.data_weight + \ self.weights.restraints_weight*rt*self.weights.restraints_weight_scale g = dg*self.weights.data_weight + \ self.weights.restraints_weight*rg*self.weights.restraints_weight_scale if(self.dump_gradients is not None): from libtbx import easy_pickle easy_pickle.dump(self.dump_gradients+"_dg", dg.as_double()) easy_pickle.dump(self.dump_gradients+"_rg", rg.as_double()) easy_pickle.dump(self.dump_gradients+"_g", g.as_double()) STOP() return t, g.as_double() class adp(calculator): def __init__(self, fmodel=None, restraints_manager=None, restraints_weight=None, data_weight=None, restraints_weight_scale=None): adopt_init_args(self, locals()) self.x = None self.x_target_functor = None self.initialize(fmodel = self.fmodel) def initialize(self, fmodel=None): assert fmodel is not None self.fmodel = fmodel self.fmodel.xray_structure.scatterers().flags_set_grads(state=False) assert self.fmodel.xray_structure.scatterers().size() == \ self.fmodel.xray_structure.use_u_iso().count(True) sel = flex.bool( self.fmodel.xray_structure.scatterers().size(), True).iselection() self.fmodel.xray_structure.scatterers().flags_set_grad_u_iso(iselection=sel) self.x = fmodel.xray_structure.extract_u_iso_or_u_equiv() self.x_target_functor = self.fmodel.target_functor() def calculate_weight(self): raise RuntimeError("Not implemented.") self.data_weight = compute_weight( fmodel = self.fmodel, rm = self.restraints_manager) def reset_fmodel(self, fmodel=None): if(fmodel is not None): self.initialize(fmodel=fmodel) self.fmodel = fmodel def update(self, x): self.x = x self.fmodel.xray_structure.set_u_iso(values = self.x) self.fmodel.update_xray_structure( xray_structure = self.fmodel.xray_structure, update_f_calc = True) def target_and_gradients(self, x): self.update(x = x) tgx = self.x_target_functor(compute_gradients=True) f = tgx.target_work() g = tgx.gradients_wrt_atomic_parameters(u_iso=True) return f, g class sites_real_space(object): def __init__(self, model, geometry_rmsd_manager, max_bond_rmsd, stpmax, gradient_only, line_search, data_weight, refine_cycles, skip_weight_search, log, map_data=None, restraints_manager=None, max_iterations=None): adopt_init_args(self, locals()) self.gradient_only = True self.max_iterations = 100 self.weight = data_weight self.sites_cart_start = self.model.get_xray_structure().sites_cart() self.show(model=self.model) # self.rama_fav_best = None self.cbeta_best = None self.rota_best = None self.clash_best = None # if(self.weight is None): self.weight = 1. self.refine_cycles = refine_cycles self.skip_weight_search = skip_weight_search self.lbfgs_termination_params = scitbx.lbfgs.termination_parameters( max_iterations = self.max_iterations) self.lbfgs_core_params = scitbx.lbfgs.core_parameters( stpmin = 1.e-9, stpmax = stpmax) self.lbfgs_exception_handling_params = scitbx.lbfgs.\ exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound = False, ignore_line_search_failed_step_at_upper_bound = False, ignore_line_search_failed_maxfev = False) self.sites_cart_refined = None self.cctbx_rm_bonds_rmsd = get_bonds_rmsd( restraints_manager = self.geometry_rmsd_manager.geometry, xrs = self.model.get_xray_structure()) def get_shift(self, other): s1 = self.sites_cart_start s2 = other.sites_cart() return flex.mean(flex.sqrt((s1 - s2).dot())) def get_scores(self, model): rama_fav = ramalyze( pdb_hierarchy = model.get_hierarchy(), outliers_only = False).percent_favored cbeta = cbetadev( pdb_hierarchy = model.get_hierarchy(), outliers_only = True, out = null_out()).get_outlier_percent() rota = rotalyze( pdb_hierarchy = model.get_hierarchy(), outliers_only = False).percent_outliers b_rmsd = get_bonds_rmsd( restraints_manager = self.geometry_rmsd_manager.geometry, xrs = model.get_xray_structure()) clash = clashscore( pdb_hierarchy = model.get_hierarchy(), keep_hydrogens = False, fast = True, condensed_probe = True).get_clashscore() print "DEV: b_rmsd= %7.4f clash= %6.4f rota= %6.4f rama_fav= %5.4f cbeta= %6.4f"%( b_rmsd, clash, rota, rama_fav, cbeta) return group_args( rama_fav = rama_fav, cbeta = cbeta, rota = rota, b_rmsd = b_rmsd, clash = clash) def ready_to_stop(self, sc): return (sc.rama_fav < self.rama_fav_best and abs(sc.rama_fav-self.rama_fav_best)>1.) or \ sc.cbeta > self.cbeta_best or \ sc.rota > self.rota_best or \ (sc.clash > self.clash_best and abs(sc.clash-self.clash_best)>1.) def macro_cycle(self, weights): print "RSR: weights to try:", weights weight_best = None i_best = None model_best = None models = [] for i, w in enumerate(weights): self.weight = w m = self.run_one() models.append(m.deep_copy()) sc = self.get_scores(model = m) if(i==0 and self.rama_fav_best is None): # we assume best Rama favored with smallest weight self.rama_fav_best = sc.rama_fav self.cbeta_best = sc.cbeta self.rota_best = sc.rota self.clash_best = sc.clash elif(i==0): # 2nd round: fine-tuning if(self.ready_to_stop(sc)): break if(sc.b_rmsd<self.max_bond_rmsd): weight_best = w i_best = i model_best = models[i_best] else: break # if(i>0): if(self.ready_to_stop(sc)): i_best = i-1 weight_best = weights[i_best] model_best = models[i_best] break # print "RSR: weight_best:", weight_best return model_best, weight_best, i_best def run(self): weights = [0.1, 1.0, 10, 20, 30, 40, 50, 200] model, weight, i = self.macro_cycle(weights = weights) # if(weight==50.): new_weights = [50,60,70,80,90,100,110,120,130,140,150,160,170,180,190] elif(weight>1 and i!=len(weights)-1): new_weights = [] w=weights[i] while w<weights[i+1]: w+=1 new_weights.append(w) elif(weight == 1.0): new_weights = [1,2,3,4,5,6,7,8,9] elif(weight == 0.1): new_weights = [0.1,0.2,0.5,0.7] elif(weight == 0.01): new_weights = [0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.09] else: print "RSR: FALED TO FIND BEST WEIGHT" STOP() print "RSR: new_weights:", new_weights # model_, weight_, i_ = self.macro_cycle(weights = new_weights) self.model = model self.weight = weight if(weight_ is not None): self.model = model_ self.weight = weight_ # rmsd = get_bonds_rmsd( restraints_manager = self.geometry_rmsd_manager.geometry, xrs = self.model.get_xray_structure()) self.show(model=self.model, prefix="(start macro-cycles)") # for mc in [1,2,3,4,5]: self.model = self.run_one() # return self.model def show(self, model, prefix=""): s = model.geometry_statistics(use_hydrogens=False).show_short() s = s.split() s = " ".join(s) dist = self.get_shift(other=model.get_xray_structure()) if(self.weight is not None): w = "%5.2f"%self.weight else: w = "%5s"%str(None) cc_mask = qr.show_cc( map_data=self.map_data, xray_structure=model.get_xray_structure()) print "RSR", prefix, "weight=%s"%w, s, "shift=%6.4f"%dist, \ "cc_mask=%6.4f"%cc_mask with open("weight_%s.pdb"%w.strip(), "w") as of: of.write(model.model_as_pdb()) def run_one(self): model = self.model.deep_copy() xrs = model.get_xray_structure() uc = xrs.crystal_symmetry().unit_cell() refined = cctbx.maptbx.real_space_refinement_simple.lbfgs( unit_cell = uc, gradients_method = "tricubic", sites_cart = xrs.sites_cart(), density_map = self.map_data, geometry_restraints_manager = self.restraints_manager, real_space_target_weight = self.weight, real_space_gradients_delta = 0.25, gradient_only = self.gradient_only, line_search = self.line_search, lbfgs_core_params = self.lbfgs_core_params, lbfgs_termination_params = self.lbfgs_termination_params, lbfgs_exception_handling_params = self.lbfgs_exception_handling_params) model.set_sites_cart(sites_cart=refined.sites_cart) #### #rmsd = get_bonds_rmsd( # restraints_manager = self.geometry_rmsd_manager.geometry, # xrs = model.get_xray_structure()) self.show(model = model) return model
python
""" Automatically generate a fairness report for a dataset. """ import logging from itertools import combinations from typing import Any, List, Mapping, Optional, Sequence, Tuple, Union import pandas as pd from . import utils from .metrics.statistics import sensitive_group_analysis from .metrics.unified import stat_distance from .plot.distr import mult_distr_plot from .sensitive.detection import detect_names_df logger = logging.getLogger(__name__) class FairnessScorer: """This class analyzes a given DataFrame, looks for biases and quantifies fairness.""" def __init__( self, df: pd.DataFrame, target_attr: str, sensitive_attrs: Optional[Sequence[str]] = None, detect_sensitive: bool = False, distr_type: Optional[str] = None, sensitive_distr_types: Optional[Mapping[str, str]] = None, ): """Fairness Scorer constructor Args: df (pd.DataFrame): Input DataFrame to be scored. target_attr (str): The target attribute name. sensitive_attrs (Optional[Sequence[str]], optional): The sensitive attribute names. Defaults to None. detect_sensitive (bool, optional): Whether to try to detect sensitive attributes from the column names. Defaults to False. distr_type (Optional[str], optional): The type of distribution of the target attribute. Can take values from ["categorical", "continuous", "binary", "datetime"]. If None, the type of distribution is inferred based on the data in the column. Defaults to None. sensitive_distr_types (Optional[Mapping[str, str]], optional): The type of distribution of the sensitive attributes. Passed as a mapping from sensitive attribute name to corresponding distribution type. Can take values from ["categorical", "continuous", "binary", "datetime"]. If None, the type of distribution of all sensitive attributes are inferred based on the data in the respective columns. Defaults to None. """ if sensitive_attrs is None: detect_sensitive = True sensitive_attrs = [] # Detect sensitive attributes if detect_sensitive: attr_dict = detect_names_df(df, deep_search=True).items() sensitive_attrs = list(set([k for (k, v) in attr_dict if v is not None]).union(sensitive_attrs)) if len(sensitive_attrs) == 0: logger.warning("No sensitive attributes detected. Fairness score will always be 0.") self.df = df self.target_attr = target_attr self.sensitive_attrs = sorted(list(sensitive_attrs)) # Infer the types of each distribution if distr_type is None: self.distr_type = utils.infer_distr_type(df[target_attr]) else: self.distr_type = utils.DistrType(distr_type) t = sensitive_distr_types or {} self.sensitive_distr_types = [ utils.DistrType(t[attr]) if attr in t else utils.infer_distr_type(df[attr]) for attr in self.sensitive_attrs ] def distribution_score( self, metric: str = "auto", method: str = "dist_to_all", p_value: bool = False, max_comb: Optional[int] = None, ) -> pd.DataFrame: """Returns a dataframe consisting of all unique sub-groups and their statistical distance to the rest of the population w.r.t. the target variable. Args: metric (str, optional): Choose a metric to use. Defaults to automatically chosen metric depending on the distribution of the target variable. method (str, optional): The method used to apply the metric to the sub-group. Can take values ["dist_to_all", dist_to_rest"] which correspond to measuring the distance between the subgroup distribution and the overall distribution, or the overall distribution without the subgroup, respectively. Defaults to "dist_to_all". p_value (bool, optional): Whether or not to compute a p-value for the distances. max_comb (Optional[int], optional): Max number of combinations of sensitive attributes to be considered. If None all combinations are considered. Defaults to 4. """ df = self.df[self.sensitive_attrs + [self.target_attr]].copy() sensitive_attrs = self.sensitive_attrs # Bin continuous sensitive attributes for attr, distr_type in zip(self.sensitive_attrs, self.sensitive_distr_types): if distr_type.is_continuous() or distr_type.is_datetime(): col = utils.infer_dtype(df[attr]) df.loc[:, attr] = utils._bin_as_string(col, distr_type.value, prefix=True) # Convert binary attributes to 0s and 1s if self.distr_type.is_binary(): df.loc[:, self.target_attr] = pd.factorize(df[self.target_attr])[0] if len(sensitive_attrs) == 0 or len(df) == 0 or len(df.dropna()) == 0: return 0.0, pd.DataFrame([], columns=["Group", "Distance", "Proportion", "Counts"]) max_comb = min(max_comb, len(sensitive_attrs)) if max_comb is not None else len(sensitive_attrs) df_dists = [] # Try all combinations of sensitive attributes for k in range(1, max_comb + 1): for sensitive_attr in combinations(sensitive_attrs, k): df_not_nan = df[~(df[list(sensitive_attr)] == "nan").any(axis=1)] if len(df_not_nan) == 0: continue df_dist = _calculate_distance(df, self.target_attr, list(sensitive_attr), metric, method, p_value) df_dists.append(df_dist) df_dist = pd.concat(df_dists, ignore_index=True) return df_dist.reset_index(drop=True) def plot_distributions( self, figsize: Optional[Tuple[int, int]] = None, max_width: int = 3, max_quantiles: int = 8, show_hist: Optional[bool] = None, show_curve: Optional[bool] = None, shade: bool = True, normalize: bool = False, cmap: Optional[Sequence[Tuple[float, float, float]]] = None, ): """Plot the distributions of the target variable with respect to all sensitive values. Args: figsize (Optional[Tuple[int, int]], optional): The size of each figure. Defaults to (6, 4). max_width (int, optional): The maximum amount of figures. Defaults to 3. max_quantiles (int, optional): The maximum amount of quantiles to use for continuous data. Defaults to 8. show_hist (Optional[bool], optional): Shows the histogram if True. Defaults to True if the data is categorical or binary. show_curve (Optional[bool], optional): Shows a KDE if True. Defaults to True if the data is continuous or a date. shade (bool, optional): Shades the curve if True. Defaults to True. normalize (bool, optional): Normalizes the counts so the sum of the bar heights is 1. Defaults to False. cmap (Optional[Sequence[Tuple[float, float, float]]], optional): A sequence of RGB tuples used to colour the histograms. If None seaborn's default pallete will be used. Defaults to None. """ mult_distr_plot( self.df, self.target_attr, self.sensitive_attrs, figsize=figsize, max_width=max_width, max_quantiles=max_quantiles, show_hist=show_hist, show_curve=show_curve, shade=shade, normalize=normalize, cmap=cmap, ) def demographic_report( self, metric: str = "auto", method: str = "dist_to_all", alpha: Optional[float] = 0.05, max_comb: Optional[int] = 4, min_count: Optional[int] = 100, max_rows: int = 10, hide_positive: bool = False, ): """Generate a report on the fairness of different groups of sensitive attributes. Args: metric (str, optional): Choose a custom metric to use. Defaults to automatically chosen metric depending on the distribution of the target variable. See method (str, optional): The method used to apply the metric to the sub-group. Can take values ["dist_to_all", "dist_to_rest"] which correspond to measuring the distance between the subgroup distribution and the overall distribution, or the overall distribution without the subgroup, respectively. Defaults to "dist_to_all". alpha (Optional[float], optional): The maximum p-value to accept a bias. Defaults to 0.05. max_comb (Optional[int], optional): Max number of combinations of sensitive attributes to be considered. If None all combinations are considered. Defaults to 4. min_count (Optional[int], optional): If set, sub-groups with less samples than min_count will be ignored. Defaults to 100. max_rows (int, optional): Maximum number of biased demographics to display. Defaults to 10. hide_positive (bool, optional): Hides positive distances if set to True. This may be useful when using metrics which can return negative distances (binomial distance), in order to inspect a skew in only one direction. Alternatively changing the method may yeild more significant results. Defaults to False. """ df_dist = self.distribution_score(metric=metric, method=method, p_value=(alpha is not None), max_comb=max_comb) if alpha is not None: df_dist = df_dist[df_dist["P-Value"] < alpha] if min_count is not None: df_dist = df_dist[df_dist["Counts"] > min_count] score = calculate_score(df_dist) if hide_positive: df_dist = df_dist[df_dist["Distance"] < 0] df_dist = df_dist.sort_values("P-Value", ascending=True, key=abs) df_dist["Distance"] = df_dist["Distance"].map("{:.3f}".format) df_dist["P-Value"] = df_dist["P-Value"].map("{:.2e}".format) print(f"Sensitive Attributes: {self.sensitive_attrs}\n") print(df_dist[:max_rows].to_string(index=False)) print(f"\nWeighted Mean Statistical Distance: {score}") def compare_group_statistics( self, group_mode: str = "auto", categorical_mode: str = "entropy", groups: List[Union[Mapping[str, List[Any]], pd.Series]] = None, max_comb: int = 4, ) -> pd.DataFrame: """Generate a report of statistical measures (mean variance) of the target distributions with respect to each combination of the sensitive attributes by default, or with respect to the groups passed as input if mode is set to "manual". The sensitive or input group combinations will have a maximum length of separate groups. Args: group_mode (str, optional): If set to "auto", the function will consider combinations of pre-detected sensitive attributes, similar to distribution_score. If set to "manual", the groups have to be provided by the user. Defaults to "auto". categorical_mode (str, optional): Decides which measures to be used if the target attribute is categorical. Defaults to "entropy". groups (List[Union[Mapping[str, List[Any]], pd.Series]], optional): List of groups to be compared, ignored if mode is set to "auto". Defaults to None. max_comb (int): The maximum depth of the group combinations for which the statistics are generated. Defaults to 4. Returns: pd.DataFrame: Dataframe containing data on the first two central moments of the target distributions, by group. """ df = self.df target_attr = self.target_attr group_all = pd.Series([True] * len(df)) if group_mode == "manual": if groups is None: raise ValueError('Input groups cannot be None when group mode is set to "manual"') else: groups.append(group_all) return sensitive_group_analysis(df, target_attr, groups, categorical_mode=categorical_mode) elif group_mode == "auto": sensitive_attrs = self.sensitive_attrs max_comb = min(max_comb, len(sensitive_attrs)) auto_groups = [] for k in range(1, max_comb + 1): for sensitive_attr in combinations(sensitive_attrs, k): unique = df[list(sensitive_attr)].drop_duplicates() for _, row in unique.iterrows(): sensitive_group = {attr: [value] for attr, value in row.to_dict().items()} auto_groups.append(sensitive_group) return sensitive_group_analysis(df, target_attr, auto_groups, categorical_mode=categorical_mode) else: raise ValueError('Invalid group mode chosen! Please choose "manual" or use the "auto" default.') def calculate_score(df_dist: pd.DataFrame) -> float: """Calculate the weighted mean pairwise statistical distance. Args: df_dist (pd.DataFrame): A dataframe of statistical distances produced by or `fairlens.FairnessScorer.distribution_score`. Returns: float: The weighted mean statistical distance. """ return (df_dist["Distance"].abs() * df_dist["Counts"]).sum() / df_dist["Counts"].sum() def _calculate_distance( df: pd.DataFrame, target_attr: str, sensitive_attrs: Sequence[str], metric: str = "auto", method: str = "dist_to_all", p_value: bool = False, ) -> pd.DataFrame: unique = df[sensitive_attrs].drop_duplicates() dist = [] for _, row in unique.iterrows(): sensitive_group = {attr: [value] for attr, value in row.to_dict().items()} pred = utils.get_predicates_mult(df, [sensitive_group])[0] if method == "dist_to_rest": pred_other = ~pred else: pred_other = pd.Series([True] * len(df)) dist_res = stat_distance(df, target_attr, pred, pred_other, mode=metric, p_value=p_value) distance = dist_res[0] p = dist_res[1] if p_value else 0 dist.append( { "Group": ", ".join(map(str, row.to_dict().values())), "Distance": distance, "Proportion": len(df[pred]) / len(df), "Counts": len(df[pred]), "P-Value": p, } ) df_dist = pd.DataFrame(dist) if not p_value: df_dist.drop(columns=["P-Value"], inplace=True) return df_dist
python
#!/usr/bin/env python # -*- coding: utf-8 -*- from . import constants from .utils.strings import version_tuple_to_str __title__ = 'ocwb' __description__ = 'A Python wrapper around OpenCWB web APIs' __url__ = 'https://github.com/tsunglung/OpenCWB' __version__ = version_tuple_to_str(constants.OCWB_VERSION) __author__ = 'Tsunglung Yang' __author_email__ = '[email protected]' __license__ = 'MIT'
python
import json from urllib3_mock import Responses from delairstack.core.resources.resource import Resource from .resource_test_base import ResourcesTestBase responses = Responses('requests.packages.urllib3') class TestFlights(ResourcesTestBase): @staticmethod def __create_post_response(): return json.dumps({ '_id':'flight-id' }) @staticmethod def __create_upload_status_response(): return 'OK' @responses.activate def test_search_without_error(self): responses.add('POST', '/dxpm/flights/search', body=self.__search_post_response(), status=200, content_type='application/json') calls = responses.calls self.sdk.flights.search(project='project-id') self.assertEqual(len(calls), 1) self.assertEqual(calls[0].request.url, '/dxpm/flights/search') self.assertEqual(calls[0].request.body, '{"project_id": "project-id"}') self.sdk.flights.search(mission='mission-id') self.assertEqual(len(calls), 2) self.assertEqual(calls[1].request.url, '/dxpm/flights/search') self.assertEqual(calls[1].request.body, '{"mission_id": "mission-id"}') @staticmethod def __search_post_response(): return json.dumps({ 'flights': [{'_id': 'flight-id'}] })
python
class Solution: def rebot(self, nums, c, index): if c == 0: return True if index == len(nums) - 1: return nums[index] == c res = self.rebot(nums, c, index+1) if c >= nums[index]: res = res or self.rebot(nums, c-nums[index], index+1) return res def canPartition_1(self, nums): """ :type nums: List[int] :rtype: bool """ nums_sum = 0 for ele in nums: nums_sum += ele if nums_sum & 1 == 1: return False c = int(nums_sum / 2) return self.rebot(nums, c, 0) def canPartition(self, nums): """ :type nums: List[int] :rtype: bool """ nums_sum = 0 for ele in nums: nums_sum += ele if nums_sum & 1 == 1: return False n = len(nums) c = int(nums_sum / 2) memo = [False] * (c+1) if c >= nums[-1]: memo[nums[-1]] = True for i in range(n-2, -1, -1): for j in range(c, nums[i]-1, -1): memo[j] = memo[j] or memo[j-nums[i]] return memo[c] if __name__ == "__main__": nums = [1, 5, 11, 5] # nums = [1, 2, 3, 5] print(Solution().canPartition(nums)) print(Solution().canPartition_1(nums))
python
import socket import constants import subprocess import uuid from getmac import get_mac_address try : import requests except ModuleNotFoundError : import pip pip.main(['install','requests']) import requests def gma() : mac1=get_mac_address() mac2=':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff) for ele in range(0,8*6,8)][::-1]) return mac1 if mac1 else mac2 def _url(endpoint) : return constants.SERVER_URL+endpoint def get_ip() : s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.connect(('8.8.8.8',80)) ip=s.getsockname()[0] s.close() return ip def ping(mac,ip,username): res=requests.post(_url('/ping'),json={'mac':mac,'ip':ip,'username':username}) def recieve_msg(conn,mmac=gma()) : msg_head=conn.recv(constants.HEADER).decode(constants.FORMAT) if msg_head: msg_length=int(msg_head.split(' ')[0]) user_name=msg_head.split(' ')[1] mac=msg_head.split(' ')[2] revr=msg_head.split(' ')[3] msg=conn.recv(msg_length) msg=msg.decode(constants.FORMAT) if revr==mmac : return (msg,user_name,mac) else : return '' else : return '' def db_to_json(dbval) : ans=[] #for i in range(len(ans)) : # ans[i]=list(ans[i][:-1]) # for j in range(len(ans[i])): # ans[i][j]=str(ans[i][j]) # ans[i]='°'.join(ans[i]) #ans='|'.join(ans) for i in dbval : a={'mac':i[0],'ip':i[1],'username':i[2]} ans.append(a) return ans def get_user_list(lst,conn=None,name=None): #if not conn: # conn=send_msg(constants.GET_USER_LIST,constants.SERVER_IP,name,gma(),constants.SERVER_MAC) #msg=recieve_msg(conn) #disconnect=constants.DISCONNECT_MESSAGE.encode(constants.FORMAT) #lengthd=str(len(disconnect)).encode(constants.FORMAT) #lengthd+=b' '*(constants.HEADER-len(lengthd)) #conn.send(lengthd) #conn.send(disconnect) #conn.close() #if msg[2]==constants.SERVER_MAC: res=requests.get(_url('/online_users')) json=res.json() json_to_lst(json,lst) def json_to_lst(json,last) : for i in json: lst=[i['ip'],i['username']] lst.append(0) lst.append([]) last[i['mac']]=lst def send_msg(msg,ip,username,my_mac,your_mac): conn=socket.socket(socket.AF_INET,socket.SOCK_STREAM) try : conn.connect((ip,constants.PORT)) except : print("user might be offline") return None message=msg.encode(constants.FORMAT) length=str(len(message)).encode(constants.FORMAT) length+=b' ' length+=username.encode(constants.FORMAT) length+=b' ' length+=my_mac.encode(constants.FORMAT) length+=b' ' length+=your_mac.encode(constants.FORMAT) length+=b' '*(constants.HEADER-len(length)) disconnect=constants.DISCONNECT_MESSAGE.encode(constants.FORMAT) lengthd=str(len(disconnect)).encode(constants.FORMAT) lengthd+=b' '*(constants.HEADER-len(lengthd)) conn.send(length) conn.send(message) conn.send(lengthd) conn.send(disconnect) conn.close() return None def sendmsg(msg,conn,username,my_mac,your_mac): message=msg.encode(constants.FORMAT) length=str(len(message)).encode(constants.FORMAT) length+=b' ' length+=username.encode(constants.FORMAT) length+=b' ' length+=my_mac.encode(constants.FORMAT) length+=b' ' length+=your_mac.encode(constants.FORMAT) length+=b' '*(constants.HEADER-len(length)) disconnect=constants.DISCONNECT_MESSAGE.encode(constants.FORMAT) lengthd=str(len(disconnect)).encode(constants.FORMAT) lengthd+=b' '*(constants.HEADER-len(lengthd)) conn.send(length) conn.send(message) return None
python
# Copyright (C) 2016-2018 Virgil Security Inc. # # Lead Maintainer: Virgil Security Inc. <[email protected]> # # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # (1) Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # (2) Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # # (3) Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. import unittest from base64 import b64decode from virgil_crypto_lib.foundation._c_bridge import VirgilCryptoFoundationError from virgil_crypto.keys import VirgilPublicKey from virgil_crypto.card_crypto import CardCrypto class CardCryptoTest(unittest.TestCase): def __init__(self, *args, **kwargs): super(CardCryptoTest, self).__init__(*args, **kwargs) self.card_crypto = CardCrypto() self.test_text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." self.test_data = bytearray(self.test_text.encode()) self.key_pair = self.card_crypto.crypto.generate_key_pair() self.public_key = self.key_pair.public_key self.private_key = self.key_pair.private_key def test_export_public_key(self): self.assertIsNotNone( self.card_crypto.export_public_key(self.public_key) ) def test_export_public_key_empty_key(self): self.assertRaises(ValueError, self.card_crypto.export_public_key, None) def test_export_public_key_wrong_key(self): invalid_pub_key = VirgilPublicKey(None, None, None) self.assertRaises(ValueError, self.card_crypto.export_public_key, invalid_pub_key) def test_generate_sha512(self): test_hash = self.card_crypto.generate_sha512(self.test_data) self.assertIsNotNone(test_hash) self.assertEqual( b64decode("UVRFAY8h/41lGy4Jm82uLcbhseXLS852XZ2rE7kH8wJvSneUkpu04NmFqwhtWuz78P+T63xMhxEW0wXP0B21dA=="), bytearray(test_hash) ) def test_generate_sha512_with_empty_data(self): self.assertRaises(ValueError, self.card_crypto.generate_sha512, None) def test_generate_signature(self): self.assertIsNotNone( self.card_crypto.generate_signature(self.test_data, self.private_key) ) def test_generate_signature_empty_data(self): self.assertRaises(ValueError, self.card_crypto.generate_signature, None, self.private_key) def test_generate_signature_wrong_key(self): self.assertRaises(ValueError, self.card_crypto.generate_signature, self.test_data, None) def test_import_public_key(self): exported_public_key = self.card_crypto.export_public_key(self.public_key) imported_public_key = self.card_crypto.import_public_key(exported_public_key) exported_after_import = self.card_crypto.export_public_key(imported_public_key) self.assertEqual(exported_public_key, exported_after_import) def test_import_public_key_with_empty_data(self): self.assertRaises(ValueError, self.card_crypto.import_public_key, None) def test_import_public_key_with_wrong_data(self): self.assertRaises(VirgilCryptoFoundationError, self.card_crypto.import_public_key, self.test_data) def test_verify_signature(self): test_signature = self.card_crypto.generate_signature(self.test_data, self.private_key) self.assertTrue(self.card_crypto.verify_signature(test_signature, self.test_data, self.public_key)) def test_verify_signature_with_empty_signature(self): self.assertRaises(ValueError, self.card_crypto.verify_signature, None, self.test_data, self.public_key) def test_verify_signature_with_empty_key(self): test_signature = self.card_crypto.generate_signature(self.test_data, self.private_key) self.assertRaises(ValueError, self.card_crypto.verify_signature, test_signature, self.test_data, None) def test_verify_signature_with_invalid_signature(self): test_signature = self.card_crypto.generate_signature(self.test_data, self.private_key) self.assertRaises( VirgilCryptoFoundationError, self.card_crypto.verify_signature, test_signature[:-2], self.test_data, self.public_key )
python
from django.conf.urls import url from django.contrib.auth.views import login from . import views urlpatterns = [ url(r'^login/$', login, {'template_name': 'login.html'}, name='login'), url(r'^logout/$', views.logout_view, name='logout'), url(r'^register/$', views.register, name='register'), url(r'^profile/$', views.profile, name='profile'), url(r'^profile/edit/$', views.edit_profile, name='edit_profile'), ]
python
import gpxpy import gpxpy.gpx import pandas as pd import geopandas as gpd from shapely.geometry import LineString tabular = pd.read_csv(r'C:\garrett_workspace\tableau\strava_dashboard\activities.csv') geo_dataframe = gpd.GeoDataFrame(tabular) geo_dataframe['geometry'] = None for index in range(len(geo_dataframe)): filepath = geo_dataframe['gpx_filepath'][index] file = open(filepath, 'r') gpx = gpxpy.parse(file) points = [] elev = [] for track in gpx.tracks: for segment in track.segments: for point in segment.points: points.append(tuple([point.longitude, point.latitude])) elev.append(int(point.elevation*3.28)) line = LineString(points) geo_dataframe.loc[index, 'geometry'] = line print(index+1,'files parsed.') geo_dataframe.to_file(r'C:\garrett_workspace\tableau\strava_dashboard\geo_dataframe.shp') crs_to_write = """GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]""" with open(r'C:\garrett_workspace\tableau\strava_dashboard\{}.prj'.format('geo_dataframe'), 'w') as file: file.write(crs_to_write)
python
import pyodbc import operator import getConnection as gc def datasetsToColumns(datasets, cnxn): cursor = cnxn.cursor() columns_dict = dict() for dataset in datasets: cursor.execute("SELECT c.* from datasets d INNER JOIN columns_datasets cd on cd.id_2=d.id INNER JOIN columns c on cd.id_1=c.id WHERE d.id=?",dataset) columns = cursor.fetchall() for column in columns: if column[0] in columns_dict: columns_dict[column[0]] = columns_dict[column[0]] + 1 else: columns_dict[column[0]] = 1 return columns_dict def columnsToDatasets(columns, cnxn): cursor = cnxn.cursor() datasets_dict = dict() for column in columns: cursor.execute("SELECT d.* from columns c INNER JOIN columns_datasets cd on cd.id_1=c.id INNER JOIN datasets d on d.id=cd.id_2 WHERE c.id=?",column) datasets = cursor.fetchall() for dataset in datasets: if dataset[0] in datasets_dict: datasets_dict[dataset[0]] = datasets_dict[dataset[0]] + 1 else: datasets_dict[dataset[0]] = 1 return datasets_dict def getColumnNamesFromIds(ids,cnxn): cursor = cnxn.cursor() names = {} for ID in ids: cursor.execute("SELECT name FROM columns WHERE id=?",ID[0]) name = cursor.fetchone() names[ID[0]] = name return names def getSimilarColumns(column): print("Datasets using " + column) cnxn = gc.getConnection() # coincident: two columns which are found in the same dataset, or two datasets containig the same column # get datasets which contain this column datasetsWithColumn = columnsToDatasets([column], cnxn) # get other columns which appear in the same datasets as "column" coincidentColumns = datasetsToColumns(datasetsWithColumn.keys(), cnxn) # Can probably replace previous two command with a single SQL statement. # remove self from list of columns if column in coincidentColumns: coincidentColumns.pop(column) # get all datasets which contain columns coincident with column coincidentDatasets = columnsToDatasets(coincidentColumns.keys(), cnxn) # remove all datasets which contain column for key in datasetsWithColumn.keys(): if key in coincidentDatasets: coincidentDatasets.pop(key) # Get all columns in datasets similar to the datasets with "column", but not containing column similarColumns = datasetsToColumns(coincidentDatasets.keys(), cnxn) # remove all columns that are coincident with "column" for key in coincidentColumns.keys(): if key in similarColumns: similarColumns.pop(key) sorted_columns = sorted(similarColumns.items(), key=operator.itemgetter(1)) nameDict = getColumnNamesFromIds(sorted_columns, cnxn) print("---------------------------") print("Similar columns:") for column in sorted_columns: if(column[1] > 1): print(str(nameDict[column[0]]) + ": " + str(column[1])) print("") print("") # getSimilarDatasets("training-data/CKAN/snnopendata20082009august.csv") # getSimilarDatasets("training-data/CKAN/table1d-fra-sk.csv") # getSimilarDatasets("training-data/CKAN/prvtbl1nu.csv") # getSimilarDatasets("training-data/CKAN/rainfallseptember2015.csv") # getSimilarColumns("Geographical classification") getSimilarColumns("550") # getSimilarDatasets("training-data/CKAN/nndrcurrentreliefaugust.csv") # getSimilarDatasets("training-data/CKAN/00010012-eng.csv") # getSimilarDatasets("training-data/CKAN/snnopendata20142015may.csv")
python
from django.shortcuts import render, redirect from firstapp.models import Ariticle, Comment from firstapp.form import CommentForm def index(request): queryset = request.GET.get('tag') if queryset: ariticle_list = Ariticle.objects.filter(tag=queryset) else: ariticle_list = Ariticle.objects.all() context = {} context['ariticle_list'] = ariticle_list index_page = render(request, 'first_web_2.html', context) return index_page def detail(request, page_num, error_form=None): article = Ariticle.objects.get(id=page_num) form = CommentForm # 仅用于生成表单 context = {} best_comment = Comment.objects.filter(best_comment=True, belong_to=article) if best_comment: context['best_comment'] = best_comment[0] context['article'] = article # 传输的东西包含表单 if error_form is not None: context['form'] = error_form else: context['form'] = form return render(request, 'article_detail.html', context) def detail_comment(request, page_num): article = Ariticle.objects.get(id=page_num) form = CommentForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] comment_str = Comment(name=name, comment=comment, belong_to=article) comment_str.save() else: return detail(request, page_num, error_form=form) return redirect(to='detail', page_num=page_num)
python
# Generated by the protocol buffer compiler. DO NOT EDIT! # source: finocial.proto import sys _b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) from google.protobuf import descriptor as _descriptor from google.protobuf import message as _message from google.protobuf import reflection as _reflection from google.protobuf import symbol_database as _symbol_database from google.protobuf import descriptor_pb2 # @@protoc_insertion_point(imports) _sym_db = _symbol_database.Default() DESCRIPTOR = _descriptor.FileDescriptor( name='finocial.proto', package='Finocial.Chain', syntax='proto3', serialized_pb=_b('\n\x0e\x66inocial.proto\x12\x0e\x46inocial.Chain\"\x1b\n\x0bRecordValue\x12\x0c\n\x04\x64\x61ta\x18\x01 \x01(\x0c\"R\n\x06Record\x12\x0b\n\x03key\x18\x01 \x01(\x0c\x12*\n\x05value\x18\x02 \x01(\x0b\x32\x1b.Finocial.Chain.RecordValue\x12\x0f\n\x07version\x18\x03 \x01(\x0c\"X\n\x08Mutation\x12\x11\n\tnamespace\x18\x01 \x01(\x0c\x12\'\n\x07records\x18\x02 \x03(\x0b\x32\x16.Finocial.Chain.Record\x12\x10\n\x08metadata\x18\x03 \x01(\x0c\"P\n\x0bTransaction\x12\x10\n\x08mutation\x18\x01 \x01(\x0c\x12\x11\n\ttimestamp\x18\x02 \x01(\x03\x12\x1c\n\x14transaction_metadata\x18\x03 \x01(\x0c\x62\x06proto3') ) _sym_db.RegisterFileDescriptor(DESCRIPTOR) _RECORDVALUE = _descriptor.Descriptor( name='RecordValue', full_name='Finocial.Chain.RecordValue', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='data', full_name='Finocial.Chain.RecordValue.data', index=0, number=1, type=12, cpp_type=9, label=1, has_default_value=False, default_value=_b(""), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), ], extensions=[ ], nested_types=[], enum_types=[ ], options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=34, serialized_end=61, ) _RECORD = _descriptor.Descriptor( name='Record', full_name='Finocial.Chain.Record', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='key', full_name='Finocial.Chain.Record.key', index=0, number=1, type=12, cpp_type=9, label=1, has_default_value=False, default_value=_b(""), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), _descriptor.FieldDescriptor( name='value', full_name='Finocial.Chain.Record.value', index=1, number=2, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), _descriptor.FieldDescriptor( name='version', full_name='Finocial.Chain.Record.version', index=2, number=3, type=12, cpp_type=9, label=1, has_default_value=False, default_value=_b(""), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), ], extensions=[ ], nested_types=[], enum_types=[ ], options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=63, serialized_end=145, ) _MUTATION = _descriptor.Descriptor( name='Mutation', full_name='Finocial.Chain.Mutation', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='namespace', full_name='Finocial.Chain.Mutation.namespace', index=0, number=1, type=12, cpp_type=9, label=1, has_default_value=False, default_value=_b(""), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), _descriptor.FieldDescriptor( name='records', full_name='Finocial.Chain.Mutation.records', index=1, number=2, type=11, cpp_type=10, label=3, has_default_value=False, default_value=[], message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), _descriptor.FieldDescriptor( name='metadata', full_name='Finocial.Chain.Mutation.metadata', index=2, number=3, type=12, cpp_type=9, label=1, has_default_value=False, default_value=_b(""), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), ], extensions=[ ], nested_types=[], enum_types=[ ], options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=147, serialized_end=235, ) _TRANSACTION = _descriptor.Descriptor( name='Transaction', full_name='Finocial.Chain.Transaction', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='mutation', full_name='Finocial.Chain.Transaction.mutation', index=0, number=1, type=12, cpp_type=9, label=1, has_default_value=False, default_value=_b(""), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), _descriptor.FieldDescriptor( name='timestamp', full_name='Finocial.Chain.Transaction.timestamp', index=1, number=2, type=3, cpp_type=2, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), _descriptor.FieldDescriptor( name='transaction_metadata', full_name='Finocial.Chain.Transaction.transaction_metadata', index=2, number=3, type=12, cpp_type=9, label=1, has_default_value=False, default_value=_b(""), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), ], extensions=[ ], nested_types=[], enum_types=[ ], options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=237, serialized_end=317, ) _RECORD.fields_by_name['value'].message_type = _RECORDVALUE _MUTATION.fields_by_name['records'].message_type = _RECORD DESCRIPTOR.message_types_by_name['RecordValue'] = _RECORDVALUE DESCRIPTOR.message_types_by_name['Record'] = _RECORD DESCRIPTOR.message_types_by_name['Mutation'] = _MUTATION DESCRIPTOR.message_types_by_name['Transaction'] = _TRANSACTION RecordValue = _reflection.GeneratedProtocolMessageType('RecordValue', (_message.Message,), dict( DESCRIPTOR = _RECORDVALUE, __module__ = 'finocial_pb2' # @@protoc_insertion_point(class_scope:Finocial.Chain.RecordValue) )) _sym_db.RegisterMessage(RecordValue) Record = _reflection.GeneratedProtocolMessageType('Record', (_message.Message,), dict( DESCRIPTOR = _RECORD, __module__ = 'finocial_pb2' # @@protoc_insertion_point(class_scope:Finocial.Chain.Record) )) _sym_db.RegisterMessage(Record) Mutation = _reflection.GeneratedProtocolMessageType('Mutation', (_message.Message,), dict( DESCRIPTOR = _MUTATION, __module__ = 'finocial_pb2' # @@protoc_insertion_point(class_scope:Finocial.Chain.Mutation) )) _sym_db.RegisterMessage(Mutation) Transaction = _reflection.GeneratedProtocolMessageType('Transaction', (_message.Message,), dict( DESCRIPTOR = _TRANSACTION, __module__ = 'finocial_pb2' # @@protoc_insertion_point(class_scope:Finocial.Chain.Transaction) )) _sym_db.RegisterMessage(Transaction) # @@protoc_insertion_point(module_scope)
python
from flask import Blueprint from flask_restful import Resource, Api from flask_jwt import JWT, jwt_required from datetime import datetime from json import dumps from users_rest_api import app, db from users_rest_api.secure_check import authenticate, identity from users_rest_api.model import User users_api = Blueprint('users_api', __name__) api = Api(app) # API AUTHENTICATION - to authenticate deletion requests jwt = JWT(app, authenticate, identity) def json_serial(obj): """JSON serializer for objects not serializable by default json code""" if isinstance(obj, datetime): return obj.isoformat() raise TypeError ("Type %s not serializable" % type(obj)) class NewUser(Resource): """ A class to represent a new user Methods ------- post(self, username, email, password) Creates a new user object and stores it in the database. Returns a dictionary (JSON format) with the new user's username """ def post(self, username, email, password): user = User(username, email, password) db.session.add(user) db.session.commit() return user.json() class ExistingUser(Resource): """ A class to represent an existing user Methods ------- get(self, email) Reads/retrieves an existing user object from the database. Returns a dictionary (JSON format) with the user's username, email, date and time created and state (active/inactive). If user does not exist, returns {'name': None} and status code 404. delete(self, email) Deletes a user object if it exists in the database (Requires authentication) Returns a dictionary (JSON format) note stating 'delete success'. """ def get(self, email): user = User.query.filter_by(email=email).first() date = dumps(user.created_at, default=json_serial) if user: return {'id':user.id, 'username':user.username, 'email':user.email, 'created_at':date, 'is_active':user.is_active} else: return {'name': None}, 404 @jwt_required()# this request now reqires authentication def delete(self, email): user = User.query.filter_by(email=email).first() db.session.delete(user) db.session.commit() return {'note':'delete success'} class AllUsers(Resource): """ A class to represent all users Methods ------- get(self) Reads/retrieves all user objects from the database. Returns a list of dictionary objects (JSON format) with all usernames. If there are no users in the database, it returns {'users': None} and status code 404. """ def get(self): users = User.query.all() if users: return [user.json() for user in users] else: return {'users': None}, 404 # CREATE ROUTES api.add_resource(NewUser, '/user/<string:username>/<string:email>/<string:password>') api.add_resource(ExistingUser, '/user/<string:email>') api.add_resource(AllUsers, '/users') # SET UP DB in terminal # MAC & UNIX: # export FLASK_APP=app.py # WINDOWS: # set FLASK_APP=app.py # flask db init # flask db migrate -m "first migration" # flask db upgrade # python app.py # TESTING # GET ALL # curl http://127.0.0.1:5000/users # GET 1 # curl http://127.0.0.1:5000/user/Chris # POST # curl -X POST http://127.0.0.1:5000/user/Sam/[email protected]/sam123 # AUTHENTICATE user to get access token: # curl -H "Content-Type: application/json" -X POST -d '{"username":"Chris","password":"chris123"}' http://127.0.0.1:5000/auth # -- this returns a token - copy including quotation marks # then export the provided token (obviously use given token, not this example token) # export ACCESS="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MzkwNTU0NjIsImlhdCI6MTYzOTA1NTE2MiwibmJmIjoxNjM5MDU1MTYyLCJpZGVudGl0eSI6MX0.qOWnu5WUmXrbAv86AWDvCXebPbydEnNxWPuoWxP8AZI" # then finally use this exported token in the delete request # chris$ curl -H "Authorization: JWT $ACCESS" -X DELETE http://127.0.0.1:5000/user/[email protected]
python
from sqlobject import * from sqlobject.tests.dbtest import * from sqlobject.views import * class PhoneNumber(SQLObject): number = StringCol() calls = SQLMultipleJoin('PhoneCall') incoming = SQLMultipleJoin('PhoneCall', joinColumn='toID') class PhoneCall(SQLObject): phoneNumber = ForeignKey('PhoneNumber') to = ForeignKey('PhoneNumber') minutes = IntCol() class ViewPhoneCall(ViewSQLObject): class sqlmeta: idName = PhoneCall.q.id clause = PhoneCall.q.phoneNumberID==PhoneNumber.q.id minutes = IntCol(dbName=PhoneCall.q.minutes) number = StringCol(dbName=PhoneNumber.q.number) phoneNumber = ForeignKey('PhoneNumber', dbName=PhoneNumber.q.id) call = ForeignKey('PhoneCall', dbName=PhoneCall.q.id) class ViewPhone(ViewSQLObject): class sqlmeta: idName = PhoneNumber.q.id clause = PhoneCall.q.phoneNumberID==PhoneNumber.q.id minutes = IntCol(dbName=func.SUM(PhoneCall.q.minutes)) numberOfCalls = IntCol(dbName=func.COUNT(PhoneCall.q.phoneNumberID)) number = StringCol(dbName=PhoneNumber.q.number) phoneNumber = ForeignKey('PhoneNumber', dbName=PhoneNumber.q.id) calls = SQLMultipleJoin('PhoneCall', joinColumn='phoneNumberID') vCalls = SQLMultipleJoin('ViewPhoneCall', joinColumn='phoneNumberID', orderBy='id') class ViewPhoneMore(ViewSQLObject): ''' View on top of view ''' class sqlmeta: idName = ViewPhone.q.id clause = ViewPhone.q.id == PhoneCall.q.toID number = StringCol(dbName=ViewPhone.q.number) timesCalled = IntCol(dbName=func.COUNT(PhoneCall.q.toID)) timesCalledLong = IntCol(dbName=func.COUNT(PhoneCall.q.toID)) timesCalledLong.aggregateClause = PhoneCall.q.minutes>10 minutesCalled = IntCol(dbName=func.SUM(PhoneCall.q.minutes)) class ViewPhoneMore2(ViewPhoneMore): class sqlmeta: table = 'vpm' class ViewPhoneInnerAggregate(ViewPhone): twiceMinutes = IntCol(dbName=func.SUM(PhoneCall.q.minutes)*2) def setup_module(mod): setupClass([mod.PhoneNumber, mod.PhoneCall]) mod.ViewPhoneCall._connection = mod.PhoneNumber._connection mod.ViewPhone._connection = mod.PhoneNumber._connection mod.ViewPhoneMore._connection = mod.PhoneNumber._connection phones = inserts(mod.PhoneNumber, [('1234567890',), ('1111111111',)], 'number') calls = inserts(mod.PhoneCall, [(phones[0], phones[1], 5), (phones[0], phones[1], 20), (phones[1], phones[0], 10), (phones[1], phones[0], 25)], 'phoneNumber to minutes') mod.phones = phones mod.calls = calls mod.sqlrepr = mod.PhoneNumber._connection.sqlrepr def testSimpleVPC(): assert hasattr(ViewPhoneCall, 'minutes') assert hasattr(ViewPhoneCall, 'number') assert hasattr(ViewPhoneCall, 'phoneNumberID') def testColumnSQLVPC(): assert str(sqlrepr(ViewPhoneCall.q.id)) == 'view_phone_call.id' assert str(sqlrepr(ViewPhoneCall.q.minutes)) == 'view_phone_call.minutes' q = sqlrepr(ViewPhoneCall.q) assert q.count('phone_call.minutes AS minutes') assert q.count('phone_number.number AS number') def testAliasOverride(): assert str(sqlrepr(ViewPhoneMore2.q.id)) == 'vpm.id' def checkAttr(cls, id, attr, value): assert getattr(cls.get(id), attr) == value def testGetVPC(): checkAttr(ViewPhoneCall, calls[0].id, 'number', calls[0].phoneNumber.number) checkAttr(ViewPhoneCall, calls[0].id, 'minutes', calls[0].minutes) checkAttr(ViewPhoneCall, calls[0].id, 'phoneNumber', calls[0].phoneNumber) checkAttr(ViewPhoneCall, calls[2].id, 'number', calls[2].phoneNumber.number) checkAttr(ViewPhoneCall, calls[2].id, 'minutes', calls[2].minutes) checkAttr(ViewPhoneCall, calls[2].id, 'phoneNumber', calls[2].phoneNumber) def testGetVP(): checkAttr(ViewPhone, phones[0].id, 'number', phones[0].number) checkAttr(ViewPhone, phones[0].id, 'minutes', phones[0].calls.sum(PhoneCall.q.minutes)) checkAttr(ViewPhone, phones[0].id, 'phoneNumber', phones[0]) def testGetVPM(): checkAttr(ViewPhoneMore, phones[0].id, 'number', phones[0].number) checkAttr(ViewPhoneMore, phones[0].id, 'minutesCalled', phones[0].incoming.sum(PhoneCall.q.minutes)) checkAttr(ViewPhoneMore, phones[0].id, 'timesCalled', phones[0].incoming.count()) checkAttr(ViewPhoneMore, phones[0].id, 'timesCalledLong', phones[0].incoming.filter(PhoneCall.q.minutes>10).count()) def testJoinView(): p = ViewPhone.get(phones[0].id) assert p.calls.count() == 2 assert p.vCalls.count() == 2 assert p.vCalls[0] == ViewPhoneCall.get(calls[0].id) def testInnerAggregate(): checkAttr(ViewPhoneInnerAggregate, phones[0].id, 'twiceMinutes', phones[0].calls.sum(PhoneCall.q.minutes)*2) def testSelect(): s = ViewPhone.select() assert s.count() == len(phones) s = ViewPhoneCall.select() assert s.count() == len(calls) def testSelect2(): s = ViewPhone.select(ViewPhone.q.number==phones[0].number) assert s.getOne().phoneNumber == phones[0] def testDistinctCount(): # This test is for SelectResults non-* based count when distinct # We're really just checking this doesn't raise anything due to lack of sqlrepr'ing assert ViewPhone.select(distinct=True).count() == 2
python
''' Module providing `WeaveCodeObject`. ''' from __future__ import absolute_import import os import sys import numpy from brian2.codegen.codeobject import check_compiler_kwds from functools import reduce try: from scipy import weave from scipy.weave.c_spec import num_to_c_types from scipy.weave.inline_tools import function_cache from scipy.weave.catalog import default_dir except ImportError: try: # weave as an independent package import weave from weave.c_spec import num_to_c_types from weave.inline_tools import function_cache from weave.catalog import default_dir except ImportError: # No weave for Python 3 weave = None from brian2.core.variables import (DynamicArrayVariable, ArrayVariable, AuxiliaryVariable, Subexpression) from brian2.core.preferences import prefs from brian2.core.functions import DEFAULT_FUNCTIONS, Function from brian2.devices.device import all_devices from brian2.utils.logger import std_silent, get_logger from brian2.utils.stringtools import get_identifiers from ...codeobject import CodeObject, constant_or_scalar, sys_info from ...templates import Templater from ...generators.cpp_generator import CPPCodeGenerator from ...targets import codegen_targets from ...cpp_prefs import get_compiler_and_args, update_for_cross_compilation __all__ = ['WeaveCodeObject', 'WeaveCodeGenerator'] logger = get_logger(__name__) def get_weave_cache_dir(): if weave is not None: return default_dir() else: return None def get_weave_extensions(): return {'compiled_catalog', 'compiled_catalog.dir', 'compiled_catalog.dat', 'cpp', '.so', '.pyd', '.dll', '.obj', '.exp', '.lib'} def weave_data_type(dtype): ''' Gives the C language specifier for numpy data types using weave. For example, ``numpy.int32`` maps to ``long`` in C. ''' # this handles the case where int is specified, it will be int32 or int64 # depending on platform if dtype is int: dtype = numpy.array([1]).dtype.type if dtype is float: dtype = numpy.array([1.]).dtype.type try: dtype = numpy.empty(0, dtype=dtype).dtype.char except TypeError: raise TypeError('Illegal dtype %r' % dtype) return num_to_c_types[dtype] class WeaveCodeGenerator(CPPCodeGenerator): def __init__(self, *args, **kwds): super(WeaveCodeGenerator, self).__init__(*args, **kwds) self.c_data_type = weave_data_type class WeaveCodeObject(CodeObject): ''' Weave code object The ``code`` should be a `~brian2.codegen.templates.MultiTemplate` object with two macros defined, ``main`` (for the main loop code) and ``support_code`` for any support code (e.g. function definitions). ''' templater = Templater('brian2.codegen.runtime.weave_rt', '.cpp', env_globals={'c_data_type': weave_data_type, 'dtype': numpy.dtype, 'constant_or_scalar': constant_or_scalar}) generator_class = WeaveCodeGenerator class_name = 'weave' def __init__(self, owner, code, variables, variable_indices, template_name, template_source, compiler_kwds, name='weave_code_object*'): from brian2.devices.device import get_device self.device = get_device() check_compiler_kwds(compiler_kwds, ['headers', 'sources', 'define_macros', 'libraries', 'include_dirs', 'library_dirs', 'runtime_library_dirs'], 'weave') self._done_first_run = False self.namespace = {'_owner': owner} super(WeaveCodeObject, self).__init__(owner, code, variables, variable_indices, template_name, template_source, compiler_kwds=compiler_kwds, name=name) self.compiler, self.extra_compile_args = get_compiler_and_args() self.define_macros = (list(prefs['codegen.cpp.define_macros']) + compiler_kwds.get('define_macros', [])) if self.compiler == 'msvc': self.define_macros.extend([ ('INFINITY', '(std::numeric_limits<double>::infinity())'), ('NAN', '(std::numeric_limits<double>::quiet_NaN())'), ('M_PI', '3.14159265358979323846') ]) self.extra_link_args = list(prefs['codegen.cpp.extra_link_args']) self.include_dirs = (list(prefs['codegen.cpp.include_dirs']) + compiler_kwds.get('include_dirs', [])) if sys.platform == 'win32': self.include_dirs += [os.path.join(sys.prefix, 'Library', 'include')] else: self.include_dirs += [os.path.join(sys.prefix, 'include')] # TODO: We should probably have a special folder just for header # files that are shared between different codegen targets import brian2.synapses as synapses synapses_dir = os.path.dirname(synapses.__file__) self.include_dirs.append(synapses_dir) self.library_dirs = (list(prefs['codegen.cpp.library_dirs']) + compiler_kwds.get('library_dirs', [])) if sys.platform == 'win32': self.library_dirs += [os.path.join(sys.prefix, 'Library', 'lib')] else: self.library_dirs += [os.path.join(sys.prefix, 'lib')] update_for_cross_compilation(self.library_dirs, self.extra_compile_args, self.extra_link_args, logger=logger) self.runtime_library_dirs = (list(prefs['codegen.cpp.runtime_library_dirs']), compiler_kwds.get('runtime_library_dirs', [])) self.libraries = (list(prefs['codegen.cpp.libraries']) + compiler_kwds.get('libraries', [])) self.headers = (['<math.h>','<algorithm>', '<limits>', '"stdint_compat.h"'] + prefs['codegen.cpp.headers'] + compiler_kwds.get('headers', [])) self.additional_sources = compiler_kwds.get('sources', []) self.numpy_version = '.'.join(numpy.__version__.split('.')[:2]) # Only use major.minor version self.annotated_code = self.code.main+''' /* The following code is just compiler options for the call to weave.inline. By including them here, we force a recompile if the compiler options change, which is a good thing (e.g. switching -ffast-math on and off). support_code: {self.code.support_code} compiler: {self.compiler} define_macros: {self.define_macros} extra_compile_args: {self.extra_compile_args} extra_link_args: {self.extra_link_args} include_dirs: {self.include_dirs} library_dirs: {self.library_dirs} runtime_library_dirs: {self.runtime_library_dirs} libraries: {self.libraries} numpy version: {self.numpy_version} */ '''.format(self=self) self.python_code_namespace = {'_owner': owner} self.variables_to_namespace() @classmethod def is_available(cls): try: with std_silent(False): compiler, extra_compile_args = get_compiler_and_args() extra_link_args = prefs['codegen.cpp.extra_link_args'] library_dirs = prefs['codegen.cpp.library_dirs'] update_for_cross_compilation(library_dirs, extra_compile_args, extra_link_args, logger=logger) weave.inline('int x=0;', [], compiler=compiler, headers=['<algorithm>', '<limits>'], extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, library_dirs=library_dirs, include_dirs=prefs['codegen.cpp.include_dirs'], verbose=0) return True except Exception as ex: logger.warn(('Cannot use weave, a test compilation ' 'failed: %s (%s)' % (str(ex), ex.__class__.__name__)) , 'failed_compile_test') return False def _insert_func_namespace(self, func): impl = func.implementations[self] func_namespace = impl.get_namespace(self.owner) if func_namespace is not None: self.namespace.update(func_namespace) if impl.dependencies is not None: for dep in impl.dependencies.values(): self._insert_func_namespace(dep) def variables_to_namespace(self): # Variables can refer to values that are either constant (e.g. dt) # or change every timestep (e.g. t). We add the values of the # constant variables here and add the names of non-constant variables # to a list # A list containing tuples of name and a function giving the value self.nonconstant_values = [] for name, var in self.variables.items(): if isinstance(var, Function): self._insert_func_namespace(var) continue # Everything else has already been dealt with in the # CodeGenerator (support code, renaming, etc.) elif isinstance(var, (AuxiliaryVariable, Subexpression)): continue try: value = var.get_value() except (TypeError, AttributeError): # A dummy Variable without value or a an object that is accessed # with Python's C API directly self.namespace[name] = var continue if isinstance(var, ArrayVariable): self.namespace[self.device.get_array_name(var, self.variables)] = value self.namespace['_num'+name] = var.get_len() # if var.scalar and var.constant: # self.namespace[name] = value.item() else: self.namespace[name] = value if isinstance(var, DynamicArrayVariable): dyn_array_name = self.generator_class.get_array_name(var, access_data=False) self.namespace[dyn_array_name] = self.device.get_value(var, access_data=False) # Also provide the Variable object itself in the namespace (can be # necessary for resize operations, for example) self.namespace['_var_'+name] = var # Get all identifiers in the code -- note that this is not a smart # function, it will get identifiers from strings, comments, etc. This # is not a problem here, since we only use this list to filter out # things. If we include something incorrectly, this only means that we # will pass something into the namespace unnecessarily. all_identifiers = reduce(lambda s, c: s | get_identifiers(c), list(self.code.values()), set()) # Filter out all unneeded objects self.namespace = {k: v for k, v in self.namespace.items() if k in all_identifiers} # There is one type of objects that we have to inject into the # namespace with their current value at each time step: dynamic # arrays that change in size during runs, where the size change is not # initiated by the template itself for name, var in self.variables.items(): if (isinstance(var, DynamicArrayVariable) and var.needs_reference_update): array_name = self.device.get_array_name(var, self.variables) if array_name in self.namespace: self.nonconstant_values.append((array_name, var.get_value)) if '_num'+name in self.namespace: self.nonconstant_values.append(('_num'+name, var.get_len)) def update_namespace(self): # update the values of the non-constant values in the namespace for name, func in self.nonconstant_values: self.namespace[name] = func() def compile(self): CodeObject.compile(self) if hasattr(self.code, 'python_pre'): self.compiled_python_pre = compile(self.code.python_pre, '(string)', 'exec') else: self.compiled_python_pre = None if hasattr(self.code, 'python_post'): self.compiled_python_post = compile(self.code.python_post, '(string)', 'exec') else: self.compiled_python_post = None def run(self): if self.compiled_python_pre is not None: exec(self.compiled_python_pre, self.python_code_namespace) if self._done_first_run: ret_val = self._compiled_func(self.namespace, {}) else: self._inline_args = (self.annotated_code, list(self.namespace.keys())) self._inline_kwds = dict( local_dict=self.namespace, support_code=self.code.support_code, compiler=self.compiler, headers=self.headers, define_macros=self.define_macros, libraries=self.libraries, extra_compile_args=self.extra_compile_args, extra_link_args=self.extra_link_args, include_dirs=self.include_dirs, library_dirs=self.library_dirs, sources=self.additional_sources, verbose=0) with std_silent(): ret_val = weave.inline(*self._inline_args, **self._inline_kwds) self._compiled_func = function_cache[self.annotated_code] self._done_first_run = True if self.compiled_python_post is not None: exec(self.compiled_python_post, self.python_code_namespace) return ret_val if weave is not None: codegen_targets.add(WeaveCodeObject) # Use a special implementation for the randn function that makes use of numpy's # randn # Give those functions access to a common buffer stored in the runtime device device = all_devices['runtime'] randn_code = {'support_code': ''' #define BUFFER_SIZE 20000 // A randn() function that returns a single random number. Internally // it asks numpy's randn function for BUFFER_SIZE // random numbers at a time and then returns one number from this // buffer. // It needs a reference to the numpy_randn object (the original numpy // function), because this is otherwise only available in // compiled_function (where is is automatically handled by weave). // double _randn(const int _vectorisation_idx) { // the _vectorisation_idx argument is unused for now, it could in // principle be used to get reproducible random numbers when using // OpenMP etc. double **buffer_pointer = (double **)_namespace_randn_buffer; double* buffer = *buffer_pointer; npy_int32* buffer_index = (npy_int32*)_namespace_randn_buffer_index; if(*buffer_index == 0) { if (buffer != 0) free(buffer); py::tuple args(1); args[0] = BUFFER_SIZE; PyArrayObject *new_randn = (PyArrayObject *)PyArray_FromAny(_namespace_numpy_randn.call(args), NULL, 1, 1, 0, NULL); buffer = *buffer_pointer = (double *)(new_randn->data); // This should garbage collect the array object but leave the buffer PyArray_CLEARFLAGS(new_randn, NPY_ARRAY_OWNDATA); Py_DECREF(new_randn); } double number = buffer[*buffer_index]; (*buffer_index)++; if (*buffer_index == BUFFER_SIZE) *buffer_index = 0; return number; } '''} DEFAULT_FUNCTIONS['randn'].implementations.add_implementation(WeaveCodeObject, code=randn_code, name='_randn', namespace={'_numpy_randn': numpy.random.randn, '_randn_buffer': device.randn_buffer, '_randn_buffer_index': device.randn_buffer_index}) # Also use numpy for rand rand_code = {'support_code': ''' #define BUFFER_SIZE 20000 // A rand() function that returns a single random number. Internally // it asks numpy's rand function for BUFFER_SIZE // random numbers at a time and then returns one number from this // buffer. // It needs a reference to the numpy_rand object (the original numpy // function), because this is otherwise only available in // compiled_function (where is is automatically handled by weave). // double _rand(const int _vectorisation_idx) { // the _vectorisation_idx argument is unused for now, it could in // principle be used to get reproducible random numbers when using // OpenMP etc. double **buffer_pointer = (double **)_namespace_rand_buffer; double* buffer = *buffer_pointer; npy_int32* buffer_index = (npy_int32*)_namespace_rand_buffer_index; if(*buffer_index == 0) { if (buffer != 0) free(buffer); py::tuple args(1); args[0] = BUFFER_SIZE; PyArrayObject *new_rand = (PyArrayObject *)PyArray_FromAny(_namespace_numpy_rand.call(args), NULL, 1, 1, 0, NULL); buffer = *buffer_pointer = (double *)(new_rand->data); // This should garbage collect the array object but leave the buffer PyArray_CLEARFLAGS(new_rand, NPY_ARRAY_OWNDATA); Py_DECREF(new_rand); } double number = buffer[*buffer_index]; (*buffer_index)++; if (*buffer_index == BUFFER_SIZE) *buffer_index = 0; return number; } '''} DEFAULT_FUNCTIONS['rand'].implementations.add_implementation(WeaveCodeObject, code=rand_code, namespace={'_numpy_rand': numpy.random.rand, '_rand_buffer': device.rand_buffer, '_rand_buffer_index': device.rand_buffer_index}, name='_rand')
python
from unittest import TestCase from glimslib import fenics_local as fenics from glimslib.simulation_helpers.helper_classes import FunctionSpace, TimeSeriesData class TestTimeSeriesData(TestCase): def setUp(self): # Domain nx = ny = nz = 10 mesh = fenics.RectangleMesh(fenics.Point(-2, -2), fenics.Point(2, 2), nx, ny) # function spaces displacement_element = fenics.VectorElement("Lagrange", mesh.ufl_cell(), 1) concentration_element = fenics.FiniteElement("Lagrange", mesh.ufl_cell(), 1) element = fenics.MixedElement([displacement_element, concentration_element]) subspace_names = {0: 'displacement', 1: 'concentration'} functionspace = FunctionSpace(mesh) functionspace.init_function_space(element, subspace_names) # build a 'solution' function u_0_conc_expr = fenics.Expression('sqrt(pow(x[0]-x0,2)+pow(x[1]-y0,2)) < 0.1 ? (1.0) : (0.0)', degree=1, x0=0.25, y0=0.5) u_0_disp_expr = fenics.Constant((0.0, 0.0)) self.U = functionspace.project_over_space(function_expr={0: u_0_disp_expr, 1: u_0_conc_expr}) self.tsd = TimeSeriesData(functionspace=functionspace, name='solution') def test_add_observation(self): self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=1) self.assertEqual(len(self.tsd.data), 1) self.assertEqual(self.tsd.data.get(1).get_time(),1) self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=1, replace=False) self.tsd.add_observation(field=self.U, time=1, time_step=2, recording_step=1, replace=True) self.assertEqual(self.tsd.data.get(1).get_time_step(), 2) self.assertEqual(len(self.tsd.data), 1) self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=2, replace=False) self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=3, replace=False) self.assertEqual(len(self.tsd.data), 3) def test_get_observation(self): self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=1, replace=False) self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=2, replace=False) self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=3, replace=False) res = self.tsd.get_observation(2) self.assertEqual(res.get_recording_step(),2) res = self.tsd.get_observation(5) self.assertTrue(res is None) def test_get_most_recent_observation(self): self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=1, replace=False) self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=2, replace=False) self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=3, replace=False) res = self.tsd.get_most_recent_observation() self.assertEqual(res.get_recording_step(), 3) def test_get_solution_function(self): self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=1, replace=False) self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=2, replace=False) self.tsd.add_observation(field=self.U, time=1, time_step=1, recording_step=3, replace=False) u = self.tsd.get_solution_function(subspace_id=None, recording_step=2) u1 = self.tsd.get_solution_function(subspace_id=1, recording_step=2) u0 = self.tsd.get_solution_function(subspace_id=0, recording_step=2) self.assertEqual(u.function_space(), self.U.function_space()) self.assertNotEqual(u, self.U)
python
import os from argparse import ArgumentParser import random def read_ner(path): data = [[]] with open(path, encoding='ISO-8859-1') as f: for line in f: line = line.strip() # New sentence if len(line) == 0: if len(data[-1]) > 0: data.append([]) continue if line == '-DOCSTART- -DOCSTART- O': continue # Add token to sentence tok, _, label = line.split() label = label[0] + label[1:].lower() data[-1].append((tok, label)) if len(data[-1]) == 0: del data[-1] return data def prepare_ner(conll_path): train_path = os.path.join(conll_path, 'ned.train') dev_path = os.path.join(conll_path, 'ned.testa') test_path = os.path.join(conll_path, 'ned.testb') train = read_ner(train_path) dev = read_ner(dev_path) test = read_ner(test_path) return train, dev, test def write_tsv(path, data): label_counts = {} with open(path, 'w') as f: for sent in data: for tok, label in sent: if label not in label_counts: label_counts[label] = 0 label_counts[label] += 1 f.write('{}\t{}\n'.format(tok, label)) f.write('\n') print('Labels in {} ({} labels):'.format(path, len(label_counts))) total = sum(label_counts.values()) for label in sorted(label_counts, key=label_counts.get, reverse=True): count = label_counts[label] print('{:10} {:>8} ({:.2f}%)'.format(label, count, count / total * 100)) print('') def save_data(data, out_path): if len(data) == 0: print('No data found') return os.makedirs(os.path.join(out_path, 'ner')) train, dev, test = data # Write to files write_tsv(os.path.join(out_path, 'ner', 'train.tsv'), train) write_tsv(os.path.join(out_path, 'ner', 'dev.tsv'), dev) write_tsv(os.path.join(out_path, 'ner', 'test.tsv'), test) total = len(train) + len(dev) + len(test) print('NER: Train={:.2f}, Dev={:.2f}, Test={:.2f}'.format(len(train) / total, len(dev) / total, len(test) / total)) def main(): parser = ArgumentParser(description='Process some integers.') parser.add_argument("-i", dest="in_path", required=True, help="Path to CoNLL-2002 NER data", metavar="FILE") parser.add_argument("-o", dest="out_path", default='conll2002', help="Target location", metavar="FILE") parser.add_argument("--seed", dest="seed", default=6544, help="Random seed") args = parser.parse_args() if not os.path.exists(args.in_path): print('provide a valid input path') return if os.path.exists(args.out_path): print('output path already exists') return random.seed(args.seed) print(' > Preparing NER data') save_data(prepare_ner(args.in_path), args.out_path) if __name__ == '__main__': main()
python
''' This file is part of Camarillo. Copyright (C) 2008 Frederic-Gerald Morcos <[email protected]> Camarillo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Camarillo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Camarillo. If not, see <http://www.gnu.org/licenses/>. ''' from gtk import AboutDialog, gdk from grafer.data import data_path from os.path import join class About (AboutDialog): def __init__(self): AboutDialog.__init__(self) self.set_skip_pager_hint(True) self.set_skip_taskbar_hint(True) program_name = 'Grafer' program_version = '0.2' website = 'http://grafer.googlecode.com/' logo = gdk.pixbuf_new_from_file(join(data_path, 'icons', 'grafer.svg')) authors = ['Fred Morcos <[email protected]>'] documenters = ['Fred Morcos <[email protected]>'] artists = ['Fred Morcos <[email protected]>', 'Laila Hassaballa <[email protected]>'] self.set_program_name(program_name) self.set_version(program_version) self.set_logo(logo) self.set_website(website) self.set_website_label(website) self.set_authors(authors) self.set_documenters(documenters) self.set_artists(artists) self.set_license( 'Licensed under the GPLv3. See COPYING for more information.')
python
import itertools def part1(data): data = sorted([int(x.strip()) for x in data if x]) pairs = itertools.combinations(data, 2) for (a, b) in pairs: if a + b == 2020: return a * b def part2(data): data = sorted([int(x.strip()) for x in data if x]) pairs = itertools.combinations(data, 3) for (a, b, c) in pairs: if a + b + c == 2020: return a * b * c
python
from elasticsearch import Elasticsearch import hashlib es = Elasticsearch(hosts=[{'host': "127.0.0.1", 'port': 9200}]) res = es.search(index="ssh", body={ "aggs": { "scripts": { "terms": { "field": "originalRequestString", "size": 10000011 } } } }) count = 0 data = {} for hit in res['aggregations']['scripts']['buckets']: #print(str(hit["key"]) + " Counter: " + str(hit["doc_count"])) count = count + 1 current = int(hit["doc_count"]) count = count + 1 if (current in data): #print("Counter " + str(current) + " already used") currentData = str(data[current]) data[current] = currentData + ":" + str(hit["key"]) else: data[current] = hit["key"] #print("Counter " + str(current) + " not used") breaker = 0 print("Often used scripts are: ") for x in range(count,1,-1): if (x in data): print(str(x) + ":" + " " + str(data[x])) breaker = breaker+1 if (breaker == 11): break print("Total scripts: " + str(count))
python
""" Machine shop example Covers: - Interrupts - Resources: PreemptiveResource Scenario: A workshop has *n* identical machines. A stream of jobs (enough to keep the machines busy) arrives. Each machine breaks down periodically. Repairs are carried out by one repairman. The repairman has other, less important tasks to perform, too. Broken machines preempt theses tasks. The repairman continues them when he is done with the machine repair. The workshop works continuously. """ import random import salabim as sim RANDOM_SEED = 42 PT_MEAN = 10.0 # Avg. processing time in minutes PT_SIGMA = 2.0 # Sigma of processing time MTTF = 300.0 # Mean time to failure in minutes BREAK_MEAN = 1 / MTTF # Param. for expovariate distribution REPAIR_TIME = 30.0 # Time it takes to repair a machine in minutes JOB_DURATION = 30.0 # Duration of other jobs in minutes NUM_MACHINES = 10 # Number of machines in the machine shop WEEKS = 4 # Simulation time in weeks SIM_TIME = WEEKS * 7 * 24 * 60 # Simulation time in minutes SCALE = 10 class MachineBarAnimate(sim.Animate): def __init__(self, machine): self.machine = machine sim.Animate.__init__(self, rectangle0=(0, 0, 0, 0), linewidth0=0) def rectangle(self, t): if self.machine.scheduled_time() == sim.inf: d = self.machine.left_to_do else: d = self.machine.scheduled_time() - t return( 100, 100 + self.machine.n * 30, 100 + d * SCALE, 100 + self.machine.n * 30 + 20) def fillcolor(self, t): if self.machine.mode() == 'work': return 'green' if self.machine.mode() == 'wait': return 'red' if self.machine.mode() == 'repair': return 'orange' return '' class MachineTextAnimate(sim.Animate): def __init__(self, machine): self.machine = machine sim.Animate.__init__(self, x0=10, y0=100 + self.machine.n * 30, text='', anchor='sw', font='narrow', fontsize0=15) def text(self, t): return '{} {:4d}'.format(self.machine.ident, self.machine.parts_made) class MachineBarJobAnimate(sim.Animate): def __init__(self, machine): self.machine = machine sim.Animate.__init__(self, rectangle0=(0, 0, 0, 0), linewidth0=0) def rectangle(self, t): d = self.machine.job_time return( 100, 100 + self.machine.n * 30, 100 + d * SCALE, 100 + self.machine.n * 30 + 20) def fillcolor(self, t): if self.machine.mode() == 'work': return ('green', 25) if self.machine.mode() == 'wait': return ('red', 25) if self.machine.mode() == 'repair': return ('orange', 25) return '' class RepairBlockAnimate(sim.Animate): def __init__(self, i): self.i = i sim.Animate.__init__(self, y0=10, rectangle0=(0, 0, 20, 20), linecolor0='white') def x(self, t): return xrepairman(self.i, t) def rectangle(self, t): if self.i == -1: if repairman.claimers()[0] is None: d = 0 else: d = repairman.claimers()[0].scheduled_time() - t else: if repairman.requesters()[self.i] is None: d = 0 else: if repairman.requesters()[self.i] == other: d = repairman.requesters()[self.i].left_to_do else: d = repairman.requesters()[self.i].repair_time return (0, 0, d * SCALE, 20) def fillcolor(self, t): if self.i == -1: if repairman.claimers()[0] is None: return '' else: return 'orange' else: if repairman.requesters()[self.i] is None: return '' else: return 'red' class RepairTextAnimate(sim.Animate): def __init__(self, i): self.i = i sim.Animate.__init__(self, y0=10 + 3, text='', textcolor0='white', font='narrow', fontsize0=15, anchor='sw') def x(self, t): return xrepairman(self.i, t) + 2 def text(self, t): if self.i == -1: if repairman.claimers()[0] is None: return '' else: return repairman.claimers()[0].ident else: if repairman.requesters()[self.i] is None: return '' else: return repairman.requesters()[self.i].ident def xrepairman(i, t): start = 0 if i != -1: start += (repairman.claimers()[0].scheduled_time() - t) for j in range(i): if repairman.requesters()[j] is not None: if repairman.requesters()[j] != other: start += repairman.requesters()[j].repair_time return 10 + start * SCALE def do_animation(): env.animation_parameters(modelname='Machine shop', speed=4) for machine in machines: MachineBarAnimate(machine) MachineTextAnimate(machine) MachineBarJobAnimate(machine) MachineBarAnimate(other) MachineTextAnimate(other) MachineBarJobAnimate(other) for i in range(-1, NUM_MACHINES): RepairBlockAnimate(i) RepairTextAnimate(i) def time_per_part(): """Return actual processing time for a concrete part.""" return random.normalvariate(PT_MEAN, PT_SIGMA) def time_to_failure(): """Return time until next failure for a machine.""" return random.expovariate(BREAK_MEAN) class Machine(sim.Component): """A machine produces parts and my get broken every now and then. If it breaks, it requests a *repairman* and continues the production after the it is repaired. A machine has a *name* and a numberof *parts_made* thus far. """ def setup(self, n): self.n = n self.ident = str(n) self.parts_made = 0 self.broken = False self.disturber = Disturber(machine=self) def process(self): while True: self.job_time = time_per_part() self.left_to_do = self.job_time while True: yield self.hold(self.left_to_do, mode='work') # might be updated by disturber if self.broken: if repairman.claimers()[0] == other: other.release() other.activate() self.repair_time = REPAIR_TIME yield self.request((repairman, 1, 0), mode='wait') yield self.hold(self.repair_time, mode='repair') self.release() self.broken = False else: break self.parts_made += 1 class Disturber(sim.Component): def setup(self, machine): self.machine = machine def process(self): while True: yield self.hold(time_to_failure()) if not self.machine.broken: self.machine.broken = True self.machine.left_to_do = self.machine.remaining_duration() self.machine.activate() class Other(sim.Component): def setup(self): self.n = -1 self.ident = 'X' self.parts_made = 0 def process(self): while True: self.job_time = JOB_DURATION self.left_to_do = self.job_time while self.left_to_do > 1e-8: yield self.request((repairman, 1, 1), mode='wait') yield self.hold(self.left_to_do, mode='work') other.release() self.parts_made += 1 # Setup and start the simulation print('Machine shop') env = sim.Environment() random.seed(RANDOM_SEED) # This helps reproducing the results repairman = sim.Resource('repairman') machines = [Machine(n=i) for i in range(NUM_MACHINES)] other = Other(name='$') # Execute! do_animation() env.run(till=SIM_TIME) # Analyis/results print('Machine shop results after %s weeks' % WEEKS) for machine in machines: print('%s made %d parts.' % (machine.name(), machine.parts_made)) repairman.print_statistics()
python
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models, migrations class Migration(migrations.Migration): dependencies = [ ('products', '0005_auto_20160119_1341'), ] operations = [ migrations.RemoveField( model_name='answer', name='example', ), migrations.RemoveField( model_name='concept', name='topic', ), migrations.RemoveField( model_name='example', name='concept', ), migrations.RemoveField( model_name='example', name='topic', ), migrations.AlterField( model_name='product', name='slug', field=models.SlugField(unique=True, blank=True), ), migrations.DeleteModel( name='Answer', ), migrations.DeleteModel( name='Concept', ), migrations.DeleteModel( name='Example', ), migrations.DeleteModel( name='Topic', ), ]
python
mandatory = \ { 'article' : ['ENTRYTYPE', 'ID', 'author', 'title', 'journal', 'year', 'volume'], 'book' : ['ENTRYTYPE', 'ID', 'title', 'publisher', 'year'], 'booklet' : ['ENTRYTYPE', 'ID', 'title', 'year'], 'conference' : ['ENTRYTYPE', 'ID', 'author', 'title', 'booktitle', 'publisher', 'year'], 'inbook' : ['ENTRYTYPE', 'ID', 'title', 'publisher', 'year'], 'incollection' : ['ENTRYTYPE', 'ID', 'author', 'title', 'booktitle', 'publisher', 'year'], 'inproceedings' : ['ENTRYTYPE', 'ID', 'author', 'title', 'booktitle', 'year'], 'manual' : ['ENTRYTYPE', 'ID', 'title', 'year'], 'mastersthesis' : ['ENTRYTYPE', 'ID', 'author', 'title', 'school', 'year'], 'misc' : ['ENTRYTYPE', 'ID', 'title', 'year'], 'phdthesis' : ['ENTRYTYPE', 'ID', 'author', 'title', 'school', 'year'], 'proceedings' : ['ENTRYTYPE', 'ID', 'title', 'year'], 'techreport' : ['ENTRYTYPE', 'ID', 'author', 'title', 'institution', 'year'], 'unpublished' : ['ENTRYTYPE', 'ID', 'author', 'title', 'note'] }
python
# 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 uuid from keystoneauth1.exceptions import http from keystoneclient import exceptions from keystoneclient.tests.functional import base from keystoneclient.tests.functional.v3 import client_fixtures as fixtures class RolesTestCase(base.V3ClientTestCase): def check_role(self, role, role_ref=None): self.assertIsNotNone(role.id) self.assertIn('self', role.links) self.assertIn('/roles/' + role.id, role.links['self']) if role_ref: self.assertEqual(role_ref['name'], role.name) # There is no guarantee domain is present in role if hasattr(role_ref, 'domain'): self.assertEqual(role_ref['domain'], role.domain_id) else: # Only check remaining mandatory attribute self.assertIsNotNone(role.name) def test_create_role(self): role_ref = {'name': fixtures.RESOURCE_NAME_PREFIX + uuid.uuid4().hex} role = self.client.roles.create(**role_ref) self.addCleanup(self.client.roles.delete, role) self.check_role(role, role_ref) def test_create_domain_role(self): role_ref = {'name': fixtures.RESOURCE_NAME_PREFIX + uuid.uuid4().hex, 'domain': self.project_domain_id} role = self.client.roles.create(**role_ref) self.addCleanup(self.client.roles.delete, role) self.check_role(role, role_ref) def test_get_role(self): role = fixtures.Role(self.client, domain=self.project_domain_id) self.useFixture(role) role_ret = self.client.roles.get(role.id) self.check_role(role_ret, role.ref) def test_update_role_name(self): role = fixtures.Role(self.client, domain=self.project_domain_id) self.useFixture(role) new_name = fixtures.RESOURCE_NAME_PREFIX + uuid.uuid4().hex role_ret = self.client.roles.update(role.id, name=new_name) role.ref.update({'name': new_name}) self.check_role(role_ret, role.ref) def test_update_role_domain(self): role = fixtures.Role(self.client) self.useFixture(role) domain = fixtures.Domain(self.client) self.useFixture(domain) new_domain = domain.id role_ret = self.client.roles.update(role.id, domain=new_domain) role.ref.update({'domain': new_domain}) self.check_role(role_ret, role.ref) def test_list_roles_invalid_params(self): user = fixtures.User(self.client, self.project_domain_id) self.useFixture(user) # Only filter in role grants for a user on a resource. # Domain or project should be specified. self.assertRaises(exceptions.ValidationError, self.client.roles.list, user=user.id) # Only filter in role grants for a group on a resource. # Domain or project should be specified. group = fixtures.Group(self.client, self.project_domain_id) self.useFixture(group) self.assertRaises(exceptions.ValidationError, self.client.roles.list, group=group.id) def test_list_roles(self): global_role = fixtures.Role(self.client) self.useFixture(global_role) domain = fixtures.Domain(self.client) self.useFixture(domain) domain_role = fixtures.Role(self.client, domain=domain.id) self.useFixture(domain_role) global_roles = self.client.roles.list() domain_roles = self.client.roles.list(domain_id=domain.id) roles = global_roles + domain_roles # All roles are valid for role in roles: self.check_role(role) self.assertIn(global_role.entity, global_roles) self.assertIn(domain_role.entity, domain_roles) def test_delete_role(self): role = self.client.roles.create(name=uuid.uuid4().hex, domain=self.project_domain_id) self.client.roles.delete(role.id) self.assertRaises(http.NotFound, self.client.roles.get, role.id) def test_grant_role_invalid_params(self): user = fixtures.User(self.client, self.project_domain_id) self.useFixture(user) role = fixtures.Role(self.client, domain=self.project_domain_id) self.useFixture(role) # Only grant role to a group on a resource. # Domain or project must be specified. self.assertRaises(exceptions.ValidationError, self.client.roles.grant, role.id, user=user.id) group = fixtures.Group(self.client, self.project_domain_id) self.useFixture(group) # Only grant role to a group on a resource. # Domain or project must be specified. self.assertRaises(exceptions.ValidationError, self.client.roles.grant, role.id, group=group.id) def test_user_domain_grant_and_revoke(self): user = fixtures.User(self.client, self.project_domain_id) self.useFixture(user) domain = fixtures.Domain(self.client) self.useFixture(domain) role = fixtures.Role(self.client, domain=self.project_domain_id) self.useFixture(role) self.client.roles.grant(role, user=user.id, domain=domain.id) roles_after_grant = self.client.roles.list(user=user.id, domain=domain.id) self.assertItemsEqual(roles_after_grant, [role.entity]) self.client.roles.revoke(role, user=user.id, domain=domain.id) roles_after_revoke = self.client.roles.list(user=user.id, domain=domain.id) self.assertEqual(roles_after_revoke, []) def test_user_project_grant_and_revoke(self): user = fixtures.User(self.client, self.project_domain_id) self.useFixture(user) project = fixtures.Project(self.client, self.project_domain_id) self.useFixture(project) role = fixtures.Role(self.client, domain=self.project_domain_id) self.useFixture(role) self.client.roles.grant(role, user=user.id, project=project.id) roles_after_grant = self.client.roles.list(user=user.id, project=project.id) self.assertItemsEqual(roles_after_grant, [role.entity]) self.client.roles.revoke(role, user=user.id, project=project.id) roles_after_revoke = self.client.roles.list(user=user.id, project=project.id) self.assertEqual(roles_after_revoke, []) def test_group_domain_grant_and_revoke(self): group = fixtures.Group(self.client, self.project_domain_id) self.useFixture(group) domain = fixtures.Domain(self.client) self.useFixture(domain) role = fixtures.Role(self.client, domain=self.project_domain_id) self.useFixture(role) self.client.roles.grant(role, group=group.id, domain=domain.id) roles_after_grant = self.client.roles.list(group=group.id, domain=domain.id) self.assertItemsEqual(roles_after_grant, [role.entity]) self.client.roles.revoke(role, group=group.id, domain=domain.id) roles_after_revoke = self.client.roles.list(group=group.id, domain=domain.id) self.assertEqual(roles_after_revoke, []) def test_group_project_grant_and_revoke(self): group = fixtures.Group(self.client, self.project_domain_id) self.useFixture(group) project = fixtures.Project(self.client, self.project_domain_id) self.useFixture(project) role = fixtures.Role(self.client, domain=self.project_domain_id) self.useFixture(role) self.client.roles.grant(role, group=group.id, project=project.id) roles_after_grant = self.client.roles.list(group=group.id, project=project.id) self.assertItemsEqual(roles_after_grant, [role.entity]) self.client.roles.revoke(role, group=group.id, project=project.id) roles_after_revoke = self.client.roles.list(group=group.id, project=project.id) self.assertEqual(roles_after_revoke, [])
python
""" set of functions for feature extraction """ # imports import numpy as np import cv2 from skimage.feature import hog def get_hog_features(img, orient=9, pix_per_cell=8, cell_per_block=2, vis=False, feature_vec=True): """ function to return HOG features Args: img: the image to extract features from orient, pix_per_cell, cell_per_block: parameters vis: whether to return an image with the features visualised feature_vec: whether to return a flattened feature set Returns: features: the features extracted hog_image: the visualisation if requested """ # Call with two outputs if vis==True if vis == True: features, hog_image = hog(img, orientations=orient, pixels_per_cell=(pix_per_cell, pix_per_cell), cells_per_block=(cell_per_block, cell_per_block), block_norm= 'L2-Hys', transform_sqrt=True, visualise=vis, feature_vector=feature_vec) return features, hog_image # Otherwise call with one output else: features = hog(img, orientations=orient, pixels_per_cell=(pix_per_cell, pix_per_cell), cells_per_block=(cell_per_block, cell_per_block), block_norm= 'L2-Hys', transform_sqrt=True, visualise=vis, feature_vector=feature_vec) return features def color_hist(img, nbins=32, bins_range=(0, 1)): """ function to return flattened histogram of color channels Args: img: the image to process nbins, bins_range: parameters Returns hist_features: the flattened channel histogram """ # Compute the histogram of the color channels separately channel1_hist = np.histogram(img[:,:,0], bins=nbins, range=bins_range) channel2_hist = np.histogram(img[:,:,1], bins=nbins, range=bins_range) channel3_hist = np.histogram(img[:,:,2], bins=nbins, range=bins_range) # Concatenate the histograms into a single feature vector hist_features = np.concatenate((channel1_hist[0], channel2_hist[0], channel3_hist[0])) # Return the individual histograms, bin_centers and feature vector return hist_features def bin_spatial(img, size=(32, 32)): """ function to return flattened array of image Args: img: image to process size: size to resize image to Returns: flattened feature set of resized image """ color1 = cv2.resize(img[:,:,0], size).ravel() color2 = cv2.resize(img[:,:,1], size).ravel() color3 = cv2.resize(img[:,:,2], size).ravel() return np.hstack((color1, color2, color3)) def extract_features(imgs, cspace='RGB', spatial_size=(32, 32), hist_bins=32, orient=9, pix_per_cell=8, cell_per_block=2, hog_channel=0, spatial_feat=True, hist_feat=True, hog_feat=True): """ function to extract combinations of features and to return a flattened array. we assume we will use 0..1 pixel range Args: imgs: the images to iterate through cspace: the color space to use spatial_size: the size of spatial binning hist_bins: number of bins for color histogram pix_per_cell, cell_per_block, hog_channel: parameters for hog features spatial_feat, hist_feat, hog_feat: which features to extract Returns a features array with features for each image """ # initalise a color conversion dictionary color_list = { 'RGB': cv2.COLOR_BGR2RGB, 'HSV': cv2.COLOR_BGR2HSV, 'LUV': cv2.COLOR_BGR2LUV, 'HLS': cv2.COLOR_BGR2HLS, 'YUV': cv2.COLOR_BGR2YUV, 'YCrCb': cv2.COLOR_BGR2YCrCb } # Create a list to append feature vectors to features = [] # Iterate through the list of images for file in imgs: # initalise a feature array for the image file_features = [] # Read in each one by one and rescale to 0..1 image = cv2.imread(file) image = image.astype(np.float32)/255 # apply color conversion if cspace in color_list: feature_image = cv2.cvtColor(image, color_list[cspace]) else: # bad color space passed, use RGB feature_image = cv2.cvtColor(image, color_list['RGB']) # extract features if flags are true if spatial_feat == True: spatial_features = bin_spatial(feature_image, size=spatial_size) file_features.append(spatial_features) if hist_feat == True: # Apply color_hist() hist_features = color_hist(feature_image, nbins=hist_bins) file_features.append(hist_features) if hog_feat == True: # Call get_hog_features() with vis=False, feature_vec=True if hog_channel == 'ALL': hog_features = [] for channel in range(feature_image.shape[2]): hog_features.append(get_hog_features(feature_image[:,:,channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) hog_features = np.ravel(hog_features) else: hog_features = get_hog_features(feature_image[:,:,hog_channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True) # Append the new feature vector to the features list file_features.append(hog_features) features.append(np.concatenate(file_features)) # Return list of feature vectors return features
python
import numpy as np import sys from schools3.config import base_config config = base_config.Config() config.categorical_columns = [ 'sixth_read_pl', 'sixth_math_pl', 'sixth_write_pl', 'sixth_ctz_pl', 'sixth_science_pl', 'seventh_read_pl', 'seventh_math_pl', 'seventh_write_pl', 'eighth_read_pl', 'eighth_math_pl', 'eighth_science_pl', 'eighth_socstudies_pl', ] sys.modules[__name__] = config
python