repo_name
stringlengths
5
92
path
stringlengths
4
221
copies
stringclasses
19 values
size
stringlengths
4
6
content
stringlengths
766
896k
license
stringclasses
15 values
hash
int64
-9,223,277,421,539,062,000
9,223,102,107B
line_mean
float64
6.51
99.9
line_max
int64
32
997
alpha_frac
float64
0.25
0.96
autogenerated
bool
1 class
ratio
float64
1.5
13.6
config_test
bool
2 classes
has_no_keywords
bool
2 classes
few_assignments
bool
1 class
czhengsci/veidt
veidt/utils/data_selection.py
1
4503
# coding: utf-8 # Copyright (c) Materials Virtual Lab # Distributed under the terms of the BSD License. from __future__ import division, print_function, unicode_literals, \ absolute_import import random import numpy as np import pandas as pd from copy import copy from pymatgen import Structure class MonteCarloSampler(object): """ Sample a subset from the dataset to achieve some criteria using simulated annealing. For example, one needs to subset the data so that a fraction of the data can already cover a large feature space, i.e., maximizing the distances. """ def __init__(self, datasets, num_samples, cost_function): """ Sample a subset with size num_samples from datasets to minimize the cost function. Args: datasets (numpy.array): The total datasets. num_samples (int): Number of samples from the data. cost_function (function): Function that takes into a subset of the data and calculate a cost. """ self.datasets = datasets self.num_samples = num_samples self.cost_function = cost_function self.num_total = len(datasets) self.num_remain = self.num_total - num_samples self.index_selected = list(np.random.choice( self.num_total, num_samples, replace=False)) self._get_remain_index() self.cost = self.compute_cost(self.datasets[self.index_selected, :]) self.accepted = 0 self.rejected = 0 self.cost_history = [] self.cost_history.append(self.cost) def _get_remain_index(self): self.index_remain = sorted(list(set(range(self.num_total)) - set(self.index_selected))) def compute_cost(self, data_subset): """ Compute the cost of data subsets. Args: data_subset (numpy.array): Data subset. """ return self.cost_function(data_subset) def sample(self, num_attempts, t_init, t_final): """ Metropolis sampler. For every sampling attempt, one data entry is swapped with the data reservior. Then the energy difference is evaluated. If dE < 0, the swapping is accepted. If dE > 0, then it is accepted with probability exp(-dE / T), where T is some artificial temperature. We can start with a relatively large T, and then reduce it with sampling process going on. Args: num_attempts (int): Number of sampling attempts. t_init (float): Initial temperature. t_final (float): Final temperature. """ temperatures = np.linspace(t_init, t_final, num_attempts) for i in range(num_attempts): temperature = temperatures[i] index = random.choice(self.index_selected) index_remain = random.choice(self.index_remain) self.update(index, index_remain, temperature) self.cost_history.append(self.cost) def update(self, index, index_remain, temperature): """ Implement the data swap, if it is accepted. Args: index (int): The index of selected feature matrix used for swapping. index_remain (int): The index of remaining feature matrix used for swapping. temperature (float): Artificial temperature. """ new_selected = copy(self.index_selected) new_selected.remove(index) new_selected.append(index_remain) cost_after_swap = self.compute_cost(self.datasets[new_selected, :]) d_cost = cost_after_swap - self.cost accept = self.decision(d_cost, temperature) if accept: self.index_selected = copy(new_selected) self._get_remain_index() self.cost = cost_after_swap else: pass def decision(self, d_cost, temperature): """ Decision on accepting the data swap. Args: d_cost (float): Difference between cost in proposed move. temperature (float): Temperature. """ if d_cost < 0: self.accepted += 1 return True else: p = np.exp(-d_cost / temperature) p2 = np.random.rand(1) if p2 < p: self.accepted += 1 return True else: self.rejected += 1 return False
bsd-3-clause
-7,386,989,554,089,515,000
34.464567
88
0.593826
false
4.304971
false
false
false
rolandgeider/wger
wger/manager/migrations/0010_auto_20210102_1446.py
1
1206
# Generated by Django 3.1.3 on 2021-01-02 13:46 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('manager', '0009_auto_20201202_1559'), ] operations = [ migrations.AlterField( model_name='setting', name='rir', field=models.CharField(blank=True, choices=[(None, '------'), ('0', 0), ('0.5', 0.5), ('1', 1), ('1.5', 1.5), ('2', 2), ('2.5', 2.5), ('3', 3), ('3.5', 3.5), ('4', 4)], max_length=3, null=True, verbose_name='RiR'), ), migrations.AlterField( model_name='workoutlog', name='rir', field=models.CharField(blank=True, choices=[(None, '------'), ('0', 0), ('0.5', 0.5), ('1', 1), ('1.5', 1.5), ('2', 2), ('2.5', 2.5), ('3', 3), ('3.5', 3.5), ('4', 4)], max_length=3, null=True, verbose_name='RiR'), ), ]
agpl-3.0
8,143,447,409,504,964,000
37.903226
95
0.360697
false
3.902913
false
false
false
vrsys/avangong
examples/sound/openal/openal-test.py
1
4058
# -*- Mode:Python -*- ########################################################################## # # # This file is part of AVANGO. # # # # Copyright 1997 - 2009 Fraunhofer-Gesellschaft zur Foerderung der # # angewandten Forschung (FhG), Munich, Germany. # # # # AVANGO is free software: you can redistribute it and/or modify # # it under the terms of the GNU Lesser General Public License as # # published by the Free Software Foundation, version 3. # # # # AVANGO 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 Lesser General Public # # License along with AVANGO. If not, see <http://www.gnu.org/licenses/>. # # # ########################################################################## import avango.osg.viewer import avango.moving import sys import avango.sound import avango.sound.openal if len(sys.argv) != 2: print "Usage '" + sys.argv[0] + " <modelname>" sys.exit(1) soundtraverser = avango.sound.nodes.SoundTraverser() openal_renderer = avango.sound.openal.nodes.OpenALSoundRenderer() openal_renderer.Device.value = "" soundtraverser.Renderers.value = [openal_renderer] # set up scene graph obj = avango.osg.nodes.LoadFile(Filename=sys.argv[1]) obj_trans = avango.osg.nodes.MatrixTransform() obj_trans.Children.value = [obj] #set up sound stereosound = avango.sound.nodes.SoundSource() obj_trans.Children.value.append(stereosound) stereosound.URL.value = "oggfile.ogg" stereosound.Loop.value = False monosound = avango.sound.nodes.SoundSource() obj_trans.Children.value.append(monosound) monosound.URL.value = "applause_mono.ogg" monosound.Loop.value = False root_group = avango.osg.nodes.Group() root_group.Children.value = [obj_trans] # set up viewing window = avango.osg.viewer.nodes.GraphicsWindow() camera = avango.osg.viewer.nodes.Camera() camera.Window.value = window viewer = avango.osg.viewer.nodes.Viewer() viewer.MasterCamera.value = camera viewer.Scene.value = root_group # set up event handling events = avango.osg.viewer.nodes.EventFields(View = viewer) window.ToggleFullScreen.connect_from(events.KeyAltReturn) window.DragEvent.connect_from(events.DragEvent) window.MoveEvent.connect_from(events.MoveEvent) soundtraverser.RootNode.value = root_group soundtraverser.Traverse.value = True # set up trackball mover trackball = avango.moving.nodes.Trackball() trackball.Direction.connect_from(window.MousePositionNorm) trackball.RotateTrigger.connect_from(events.MouseButtons_OnlyLeft) trackball.ZoomTrigger.connect_from(events.MouseButtons_LeftAndRight) trackball.PanTrigger.connect_from(events.MouseButtons_OnlyRight) trackball.Matrix.value = camera.ViewerTransform.value trackball.CenterTransform.value = \ avango.osg.make_scale_mat(0.1, 0.1, 0.1) * \ avango.osg.make_trans_mat(0, 0, -0.6) camera.ViewerTransform.connect_from(trackball.Matrix) openal_renderer.ListenerPosition.connect_from(camera.ViewerTransform) # render a frame to update bounding spheres and scale model to fit in window viewer.frame() scale = 0.08 / obj.get_bounding_sphere().radius() obj_trans.Matrix.value = avango.osg.make_scale_mat(scale, scale, scale) viewer.frame() # run evaluation and render loop stereosound.Play.value = True monosound.Play.value = True viewer.frame() viewer.run()
lgpl-3.0
-8,545,645,918,670,794,000
35.232143
76
0.62691
false
3.682396
false
false
false
fccoelho/jogos_vorazes
estrategias/LeoRodrigues.py
1
2061
# -*- coding: utf8 -*- from .jogadores import Jogador class MeuJogador(Jogador): def percent(self,data,percentil): data = sorted(data) n = len(data) if n == 0: pass #print ("Lista vazia") else: return int(round(percentil*n+1)) def escolha_de_cacada(self, rodada, comida_atual, reputacao_atual, m, reputacoes_dos_jogadores): """ Método principal que executa a cada rodada. você precisa criar uma lista de escolhas onde 'c' significa escolher caçar e 'd' representa descansar as decisãoes podem usar todas as informações disponíveis, por exemplo, as reputações dos outros jogadores. rodada: inteiro que é a rodada em que você está comida_atual: inteiro com a comida que você tem reputacao_atual: float representando sua reputação atual m: inteiro que é um limiarde cooperação/caçada desta rodada. reputacoes_dos_jogadores: lista de floats com as reputações dos outros jogadores """ reput_oder = sorted(reputacoes_dos_jogadores) if comida_atual > 50: if rodada < 10: #print(reput_oder) escolhas = ['c' if max(reput_oder) > reput_oder[self.percent(reput_oder,0.25)-1] and min(reput_oder) < reput_oder[self.percent(reput_oder,0.75)-1] else 'd' for i in reput_oder] return escolhas else: if float(sum(reput_oder))/float(len(reput_oder)) >= 0.55: escolhas = ['c' if max(reput_oder) > reput_oder[self.percent(reput_oder,0.20)-1] else 'd' for i in reputacoes_dos_jogadores] else: escolhas = ['c' if max(reput_oder) > reput_oder[self.percent(reput_oder,0.40)-1] and min(reput_oder) < reput_oder[self.percent(reput_oder,0.60)-1] else 'd' for i in reput_oder] return escolhas else: escolhas = ['d' for i in reput_oder] return escolhas
mit
8,636,253,855,674,421,000
47.571429
197
0.594608
false
2.869198
false
false
false
dwadler/QGIS
python/plugins/processing/algs/qgis/PointDistance.py
1
13351
# -*- coding: utf-8 -*- """ *************************************************************************** PointDistance.py --------------------- Date : August 2012 Copyright : (C) 2012 by Victor Olaya Email : volayaf at gmail dot com *************************************************************************** * * * 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 2 of the License, or * * (at your option) any later version. * * * *************************************************************************** """ __author__ = 'Victor Olaya' __date__ = 'August 2012' __copyright__ = '(C) 2012, Victor Olaya' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' import os import math from qgis.PyQt.QtGui import QIcon from qgis.PyQt.QtCore import QVariant from qgis.core import (QgsApplication, QgsFeatureRequest, QgsField, QgsFields, QgsProject, QgsFeature, QgsGeometry, QgsDistanceArea, QgsFeatureSink, QgsProcessingParameterFeatureSource, QgsProcessing, QgsProcessingException, QgsProcessingParameterEnum, QgsProcessingParameterField, QgsProcessingParameterNumber, QgsProcessingParameterFeatureSink, QgsSpatialIndex, QgsWkbTypes) from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] class PointDistance(QgisAlgorithm): INPUT = 'INPUT' INPUT_FIELD = 'INPUT_FIELD' TARGET = 'TARGET' TARGET_FIELD = 'TARGET_FIELD' MATRIX_TYPE = 'MATRIX_TYPE' NEAREST_POINTS = 'NEAREST_POINTS' OUTPUT = 'OUTPUT' def icon(self): return QgsApplication.getThemeIcon("/algorithms/mAlgorithmDistanceMatrix.svg") def svgIconPath(self): return QgsApplication.iconPath("/algorithms/mAlgorithmDistanceMatrix.svg") def group(self): return self.tr('Vector analysis') def groupId(self): return 'vectoranalysis' def __init__(self): super().__init__() def initAlgorithm(self, config=None): self.mat_types = [self.tr('Linear (N*k x 3) distance matrix'), self.tr('Standard (N x T) distance matrix'), self.tr('Summary distance matrix (mean, std. dev., min, max)')] self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Input point layer'), [QgsProcessing.TypeVectorPoint])) self.addParameter(QgsProcessingParameterField(self.INPUT_FIELD, self.tr('Input unique ID field'), parentLayerParameterName=self.INPUT, type=QgsProcessingParameterField.Any)) self.addParameter(QgsProcessingParameterFeatureSource(self.TARGET, self.tr('Target point layer'), [QgsProcessing.TypeVectorPoint])) self.addParameter(QgsProcessingParameterField(self.TARGET_FIELD, self.tr('Target unique ID field'), parentLayerParameterName=self.TARGET, type=QgsProcessingParameterField.Any)) self.addParameter(QgsProcessingParameterEnum(self.MATRIX_TYPE, self.tr('Output matrix type'), options=self.mat_types, defaultValue=0)) self.addParameter(QgsProcessingParameterNumber(self.NEAREST_POINTS, self.tr('Use only the nearest (k) target points'), type=QgsProcessingParameterNumber.Integer, minValue=0, defaultValue=0)) self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Distance matrix'), QgsProcessing.TypeVectorPoint)) def name(self): return 'distancematrix' def displayName(self): return self.tr('Distance matrix') def processAlgorithm(self, parameters, context, feedback): source = self.parameterAsSource(parameters, self.INPUT, context) if source is None: raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT)) source_field = self.parameterAsString(parameters, self.INPUT_FIELD, context) target_source = self.parameterAsSource(parameters, self.TARGET, context) if target_source is None: raise QgsProcessingException(self.invalidSourceError(parameters, self.TARGET)) target_field = self.parameterAsString(parameters, self.TARGET_FIELD, context) same_source_and_target = parameters[self.INPUT] == parameters[self.TARGET] matType = self.parameterAsEnum(parameters, self.MATRIX_TYPE, context) nPoints = self.parameterAsInt(parameters, self.NEAREST_POINTS, context) if nPoints < 1: nPoints = target_source.featureCount() if matType == 0: # Linear distance matrix return self.linearMatrix(parameters, context, source, source_field, target_source, target_field, same_source_and_target, matType, nPoints, feedback) elif matType == 1: # Standard distance matrix return self.regularMatrix(parameters, context, source, source_field, target_source, target_field, nPoints, feedback) elif matType == 2: # Summary distance matrix return self.linearMatrix(parameters, context, source, source_field, target_source, target_field, same_source_and_target, matType, nPoints, feedback) def linearMatrix(self, parameters, context, source, inField, target_source, targetField, same_source_and_target, matType, nPoints, feedback): if same_source_and_target: # need to fetch an extra point from the index, since the closest match will always be the same # as the input feature nPoints += 1 inIdx = source.fields().lookupField(inField) outIdx = target_source.fields().lookupField(targetField) fields = QgsFields() input_id_field = source.fields()[inIdx] input_id_field.setName('InputID') fields.append(input_id_field) if matType == 0: target_id_field = target_source.fields()[outIdx] target_id_field.setName('TargetID') fields.append(target_id_field) fields.append(QgsField('Distance', QVariant.Double)) else: fields.append(QgsField('MEAN', QVariant.Double)) fields.append(QgsField('STDDEV', QVariant.Double)) fields.append(QgsField('MIN', QVariant.Double)) fields.append(QgsField('MAX', QVariant.Double)) out_wkb = QgsWkbTypes.multiType(source.wkbType()) if matType == 0 else source.wkbType() (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context, fields, out_wkb, source.sourceCrs()) if sink is None: raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT)) index = QgsSpatialIndex(target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(source.sourceCrs(), context.transformContext())), feedback) distArea = QgsDistanceArea() distArea.setSourceCrs(source.sourceCrs(), context.transformContext()) distArea.setEllipsoid(context.project().ellipsoid()) features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([inIdx])) total = 100.0 / source.featureCount() if source.featureCount() else 0 for current, inFeat in enumerate(features): if feedback.isCanceled(): break inGeom = inFeat.geometry() inID = str(inFeat[inIdx]) featList = index.nearestNeighbor(inGeom.asPoint(), nPoints) distList = [] vari = 0.0 request = QgsFeatureRequest().setFilterFids(featList).setSubsetOfAttributes([outIdx]).setDestinationCrs(source.sourceCrs(), context.transformContext()) for outFeat in target_source.getFeatures(request): if feedback.isCanceled(): break if same_source_and_target and inFeat.id() == outFeat.id(): continue outID = outFeat[outIdx] outGeom = outFeat.geometry() dist = distArea.measureLine(inGeom.asPoint(), outGeom.asPoint()) if matType == 0: out_feature = QgsFeature() out_geom = QgsGeometry.unaryUnion([inFeat.geometry(), outFeat.geometry()]) out_feature.setGeometry(out_geom) out_feature.setAttributes([inID, outID, dist]) sink.addFeature(out_feature, QgsFeatureSink.FastInsert) else: distList.append(float(dist)) if matType != 0: mean = sum(distList) / len(distList) for i in distList: vari += (i - mean) * (i - mean) vari = math.sqrt(vari / len(distList)) out_feature = QgsFeature() out_feature.setGeometry(inFeat.geometry()) out_feature.setAttributes([inID, mean, vari, min(distList), max(distList)]) sink.addFeature(out_feature, QgsFeatureSink.FastInsert) feedback.setProgress(int(current * total)) return {self.OUTPUT: dest_id} def regularMatrix(self, parameters, context, source, inField, target_source, targetField, nPoints, feedback): distArea = QgsDistanceArea() distArea.setSourceCrs(source.sourceCrs(), context.transformContext()) distArea.setEllipsoid(context.project().ellipsoid()) inIdx = source.fields().lookupField(inField) targetIdx = target_source.fields().lookupField(targetField) index = QgsSpatialIndex(target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(source.sourceCrs(), context.transformContext())), feedback) first = True sink = None dest_id = None features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([inIdx])) total = 100.0 / source.featureCount() if source.featureCount() else 0 for current, inFeat in enumerate(features): if feedback.isCanceled(): break inGeom = inFeat.geometry() if first: featList = index.nearestNeighbor(inGeom.asPoint(), nPoints) first = False fields = QgsFields() input_id_field = source.fields()[inIdx] input_id_field.setName('ID') fields.append(input_id_field) for f in target_source.getFeatures(QgsFeatureRequest().setFilterFids(featList).setSubsetOfAttributes([targetIdx]).setDestinationCrs(source.sourceCrs(), context.transformContext())): fields.append(QgsField(str(f[targetField]), QVariant.Double)) (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context, fields, source.wkbType(), source.sourceCrs()) if sink is None: raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT)) data = [inFeat[inField]] for target in target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setFilterFids(featList).setDestinationCrs(source.sourceCrs(), context.transformContext())): if feedback.isCanceled(): break outGeom = target.geometry() dist = distArea.measureLine(inGeom.asPoint(), outGeom.asPoint()) data.append(dist) out_feature = QgsFeature() out_feature.setGeometry(inGeom) out_feature.setAttributes(data) sink.addFeature(out_feature, QgsFeatureSink.FastInsert) feedback.setProgress(int(current * total)) return {self.OUTPUT: dest_id}
gpl-2.0
4,954,009,398,125,861,000
46.176678
197
0.565126
false
4.837319
false
false
false
JohanComparat/pySU
spm/bin_spiders/spiders_last_burst_vs_radius.py
1
5578
import astropy.cosmology as co aa=co.Planck15 import astropy.io.fits as fits import astropy.units as u from astropy.coordinates import angles #import AngularSeparation from astropy import coordinates as coord import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as p import numpy as n import os import sys import ClusterScalingRelations as clsr from scipy.interpolate import interp1d import StellarMass as sm smhmr = sm.StellarMass() scl = clsr.ClusterScalingRelations_Mantz2016() cat = fits.open(os.path.join(os.environ['DATA_DIR'], 'spiders', 'cluster', 'validatedclusters_catalogue_2016-07-04-DR14_version_round1-v4_Xmass-v1.fits.gz'))[1].data spm = fits.open(os.path.join(os.environ['DATA_DIR'], 'spiders', 'cluster', 'validatedclusters_catalogue_2016-07-04-DR14_version_round1-v4_Xmass-v1_spm.fits'))[1].data volume_rough = aa.comoving_volume(0.5)*2200.*n.pi/129600 volume = volume_rough.value # get cluster center # distance to center # rescale to r200c_deg # get the latest min(ages) of the ssp # compute SFR # now looks at individual galaxies # and gets the highest SFR for each galaxy # youngest age highest_sfrs = [] youngest_ages = [] sep_r200c = [] for cc in cat: center = coord.ICRS(ra=cc['RA_OPT']*u.degree, dec=cc['DEC_OPT']*u.degree) gal = (spm['CLUS_ID']==cc['CLUS_ID']) #all_members = coord.ICRS() #separations = center.separation(all_members)/(cc['R200C_DEG']*u.degree)).value for id_cc, (pla, mjd, fib) in enumerate(zip(cc['ALLPLATE'][:len(gal.nonzero()[0])], cc['ALLMJD'][:len(gal.nonzero()[0])], cc['ALLFIBERID'][:len(gal.nonzero()[0])])): sel = (gal) & (spm['PLATE']==pla) & (spm['MJD']==mjd) & (spm['FIBERID']==fib) if len(sel.nonzero()[0])>0 : n_cp = spm['Chabrier_MILES_nComponentsSSP'][sel].astype('int')[0] if n_cp > 0 : all_ages = n.array([ spm['Chabrier_MILES_age_ssp_'+str(ii)][sel][0] for ii in n.arange(n_cp) ]) all_masses = n.array([ spm['Chabrier_MILES_stellar_mass_ssp_'+str(ii)][sel][0] for ii in n.arange(n_cp) ]) sfr_inst = all_masses / all_ages youngest_ages.append(n.min(all_ages)) highest_sfrs.append(n.max(sfr_inst)) position = coord.ICRS(cc['ALLRA'][id_cc]*u.degree, cc['ALLDEC'][id_cc]*u.degree) sep_r200c.append( (center.separation(position)/(cc['R200C_DEG']*u.degree)).value ) highest_sfrs = n.array(highest_sfrs) youngest_ages = n.array(youngest_ages) sep_r200c = n.array(sep_r200c) p.figure(1, (5,5)) p.title('SPIDERS') p.plot(sep_r200c, highest_sfrs, 'r+') p.xlabel('r/r200c') p.ylabel('SFR [Msun/yr]') #p.xscale('log') p.yscale('log') p.xlim((0.08,1.5)) p.grid() p.savefig(os.path.join(os.environ['DATA_DIR'], 'spiders', 'cluster', 'disteance-2-center-SFR.png')) p.clf() dx = ( n.max(sep_r200c) - n.min(sep_r200c) ) /3. r_b = n.arange(n.min(sep_r200c), n.max(sep_r200c) + dx, dx) p.figure(1, (5,5)) for ii,bb in enumerate(r_b[:-1]): sub = (sep_r200c>bb)&(sep_r200c<r_b[ii+1]) p.hist(highest_sfrs[sub], label=str(n.round(bb,3))+"<"+str(n.round(r_b[ii+1],3)), cumulative=True, normed=True, histtype='step') p.ylabel('normed cumulative distribution') p.xlabel('SFR [Msun/yr]') p.xscale('log') p.ylim((-0.01, 1.01)) p.grid() p.legend(frameon=False, loc=0) p.savefig(os.path.join(os.environ['DATA_DIR'], 'spiders', 'cluster', 'disteance-2-center-SFR-histograms.png')) p.clf() p.figure(1, (5,5)) p.title('SPIDERS') p.plot(sep_r200c, youngest_ages, 'r+') p.xlabel('r/r200c') p.ylabel('age [yr]') p.xscale('log') p.yscale('log') p.xlim((0.1,5)) p.grid() p.savefig(os.path.join(os.environ['DATA_DIR'], 'spiders', 'cluster', 'disteance-2-center-AGE.png')) p.clf() p.figure(1, (5,5)) p.title('SPIDERS DR14 galaxies') p.plot(spm['Z'], spm["Chabrier_MILES_stellar_mass"], 'b,', label='targets') p.plot(z, y, 'r,', label='cluster members') p.xlabel('redshift') p.ylabel('stellar mass [Msun]') #p.xscale('log') p.yscale('log') p.xlim((0,0.7)) p.ylim((1e9,1e12)) p.grid() p.legend(frameon=False, loc=0) p.savefig(os.path.join(os.environ['DATA_DIR'], 'spiders', 'cluster', 'redshift-mass.png')) p.clf() logm2x = n.hstack((m2x)) bins=n.arange(-7, 0.5, 0.1) basis = (n.isnan(logm2x)==False)&(logm2x != -n.inf)&(logm2x != n.inf) arbitrary_factor =5. p.figure(1, (5,5)) ok = (basis)&(x>1e44) out = n.log10(n.histogram(logm2x[ok], bins=bins)[0]) p.plot((bins[1:]+bins[:-1])/2., n.log10(out/arbitrary_factor), label='LX>44') ok = (basis)&(x>10**44.5) out = n.log10(n.histogram(logm2x[ok], bins=bins)[0]) p.plot((bins[1:]+bins[:-1])/2., n.log10(out/arbitrary_factor), label='LX>44.5') ok = (basis)&(x>1e45) out = n.log10(n.histogram(logm2x[ok], bins=bins)[0]) p.plot((bins[1:]+bins[:-1])/2., n.log10(out/arbitrary_factor), label='LX>45') ok = (basis)&(m200c>10**14) out = n.log10(n.histogram(logm2x[ok], bins=bins)[0]) p.plot((bins[1:]+bins[:-1])/2., n.log10(out/arbitrary_factor), label='M200c>14', ls='dashed') ok = (basis)&(m200c>10**15) out = n.log10(n.histogram(logm2x[ok], bins=bins)[0]) p.plot((bins[1:]+bins[:-1])/2., n.log10(out/arbitrary_factor), label='M200c>15', ls='dashed') xs = n.arange(-7, 0.01, 0.01) logfsat= lambda logxi, a, b, logN0, exponent : n.log10( 10**logN0 * (10**logxi)**a)# * n.e**(-b*(10**logxi)**exponent)) p.plot(xs, logfsat(xs, -0.81, 5.81, -2.25, -2.54), label='-0.81') p.plot(xs, logfsat(xs, -0.18, 5.81, -1.2, -.54), label='-0.18') p.xlabel('log10(SMHMR(stellar mass) / HaloMass(Lx ray))') p.ylabel('histogram') #p.xscale('log') #p.yscale('log') p.ylim((-1.5, 0.5)) p.xlim((-4,0)) p.grid() p.legend(frameon=False, loc=0) p.savefig(os.path.join(os.environ['DATA_DIR'], 'spiders', 'cluster', 'LX-mass-histogram.png')) p.clf()
cc0-1.0
-8,053,327,241,965,627,000
32.202381
166
0.661527
false
2.250101
false
false
false
OpenDataPolicingNC/Traffic-Stops
traffic_stops/base_views.py
1
3014
from django.core.exceptions import ImproperlyConfigured from django.shortcuts import redirect, Http404 from django.views.generic import DetailView, ListView, TemplateView from django.views.generic.edit import ProcessFormView, FormMixin from traffic_stops.utils import get_chunks from collections import defaultdict class Home(FormMixin, ProcessFormView, TemplateView): def get(self, request, *args, **kwargs): if request.GET: form = self.get_form_class()(request.GET) if form.is_valid(): success = self.get_success_url() return redirect(success, form.cleaned_data['agency'].pk) return super(Home, self).get(request, **kwargs) class AgencyList(FormMixin, ListView): def get_success_url(self, pk, **kwargs): success = super(AgencyList, self).get_success_url(self, **kwargs) return redirect(success, pk) def get(self, request, **kwargs): if request.GET: form = self.get_form_class()(request.GET) if form.is_valid(): return self.get_success_url(pk=form.cleaned_data['agency'].pk) return super(AgencyList, self).get(request, **kwargs) def get_context_data(self, **kwargs): context = super(AgencyList, self).get_context_data(**kwargs) # The following seems to be all ProcessFormView really gives us. # It causes collisions with ListView's get method. Hence # we just add it as a trivial context-modification snippet. form_class = self.get_form_class() form = self.get_form(form_class) context['form'] = form # Once we have the "letters present", we want to be able to iterate # over categorized, sorted lists of agencies. Therefore we create # a dict indexed by first letter. sorted_agencies = defaultdict(list) for agency in context['agency_list']: initial = agency.name[:1] sorted_agencies[initial].append(agency) for key in sorted_agencies: sorted_agencies[key].sort(key=lambda x: x.name.lower()) sorted_agencies[key] = get_chunks(sorted_agencies[key]) sorted_agencies = sorted(sorted_agencies.items()) context['sorted_agencies'] = sorted_agencies context['agency_form'] = form return context class AgencyDetail(DetailView): def get_stop_model(self): if self.stop_model: return self.stop_model else: raise ImproperlyConfigured("No stop model provided.") def get_context_data(self, **kwargs): context = super(AgencyDetail, self).get_context_data(**kwargs) agency = context['object'] officer_id = self.request.GET.get('officer_id') if officer_id: Stop = self.get_stop_model() if not Stop.objects.filter(agency=agency, officer_id=officer_id).exists(): raise Http404() context['officer_id'] = officer_id return context
mit
6,739,473,892,686,787,000
36.675
86
0.639681
false
3.904145
false
false
false
alfredodeza/execnet
execnet/gateway_bootstrap.py
1
3067
# -*- coding: utf-8 -*- """ code to initialize the remote side of a gateway once the io is created """ import inspect import os import execnet from execnet import gateway_base from execnet.gateway import Gateway importdir = os.path.dirname(os.path.dirname(execnet.__file__)) class HostNotFound(Exception): pass def bootstrap_import(io, spec): # only insert the importdir into the path if we must. This prevents # bugs where backports expect to be shadowed by the standard library on # newer versions of python but would instead shadow the standard library sendexec( io, "import sys", "if %r not in sys.path:" % importdir, " sys.path.insert(0, %r)" % importdir, "from execnet.gateway_base import serve, init_popen_io, get_execmodel", "sys.stdout.write('1')", "sys.stdout.flush()", "execmodel = get_execmodel(%r)" % spec.execmodel, "serve(init_popen_io(execmodel), id='%s-slave')" % spec.id, ) s = io.read(1) assert s == "1".encode("ascii"), repr(s) def bootstrap_exec(io, spec): try: sendexec( io, inspect.getsource(gateway_base), "execmodel = get_execmodel(%r)" % spec.execmodel, "io = init_popen_io(execmodel)", "io.write('1'.encode('ascii'))", "serve(io, id='%s-slave')" % spec.id, ) s = io.read(1) assert s == "1".encode("ascii") except EOFError: ret = io.wait() if ret == 255: raise HostNotFound(io.remoteaddress) def bootstrap_socket(io, id): # XXX: switch to spec from execnet.gateway_socket import SocketIO sendexec( io, inspect.getsource(gateway_base), "import socket", inspect.getsource(SocketIO), "try: execmodel", "except NameError:", " execmodel = get_execmodel('thread')", "io = SocketIO(clientsock, execmodel)", "io.write('1'.encode('ascii'))", "serve(io, id='%s-slave')" % id, ) s = io.read(1) assert s == "1".encode("ascii") def sendexec(io, *sources): source = "\n".join(sources) io.write((repr(source) + "\n").encode("ascii")) def fix_pid_for_jython_popen(gw): """ fix for jython 2.5.1 """ spec, io = gw.spec, gw._io if spec.popen and not spec.via: # XXX: handle the case of remote being jython # and not having the popen pid if io.popen.pid is None: io.popen.pid = gw.remote_exec( "import os; channel.send(os.getpid())" ).receive() def bootstrap(io, spec): if spec.popen: if spec.via or spec.python: bootstrap_exec(io, spec) else: bootstrap_import(io, spec) elif spec.ssh or spec.vagrant_ssh: bootstrap_exec(io, spec) elif spec.socket: bootstrap_socket(io, spec) else: raise ValueError("unknown gateway type, cant bootstrap") gw = Gateway(io, spec) fix_pid_for_jython_popen(gw) return gw
mit
8,197,248,749,936,590,000
27.137615
79
0.580698
false
3.537486
false
false
false
CaptainDesAstres/Blender-Render-Manager
TaskList/TaskLog/FrameLog.py
1
1368
#!/usr/bin/python3.4 # -*-coding:Utf-8 -* '''module to manage task Frame log''' import xml.etree.ElementTree as xmlMod import datetime from usefullFunctions import * class FrameLog: '''class to manage task frame log''' def __init__(self, xml = None, frame = None, date = None, computingTime = None): '''initialize task frame object''' if xml is None: self.defaultInit(frame, date, computingTime) else: self.fromXml(xml) def defaultInit(self, frame, date, computingTime): '''initialize Task frame log object''' self.frame = frame self.date = date self.computingTime = computingTime def fromXml(self, xml): '''initialize Task frame log object with saved log''' self.frame = int(xml.get('frame')) self.date = datetime.datetime.fromtimestamp(float(xml.get('date'))) self.computingTime = float(xml.get('computingTime')) def toXml(self): '''export task frame log into xml syntaxed string''' return '<frame frame="'+str(self.frame)\ +'" date="'+str(int(self.date.timestamp()))\ +'" computingTime="'+str(self.computingTime)+'" />' def print(self): '''A method to print task frame log''' print(' ╚═ '+columnLimit((str(self.frame)), 9, sep = '')\ +self.date.strftime('%d/%m/%Y at %H:%M')\ +' '+str(round(self.computingTime, 2)) )
mit
1,593,360,718,712,065,800
19.358209
69
0.629765
false
3.216981
false
false
false
ctberthiaume/keggannot
keggannot/annot.py
1
16296
import os, sys from decimal import Decimal import gzip from collections import OrderedDict import logging def blast_result_iterator(blast_file): _ = blast_file.readline() # burn header for line in blast_file: if not line.startswith("#"): yield BlastHit(line) class BlastHit(object): """ Parse ncbi blast tabular output, comments will be ignored. blastp produces the following columns: qseqid sseqid pident length mismatch gapopen qstart qend sstart send evalue bitscore """ col_sep = "\t" query_id_col = 0 subject_id_col = 1 evalue_col = 10 bitscore_col = 11 def __init__(self, line=None): self.line = line self.query_id = None self.subject_id = None self.evalue = None self.bitscore = None if self.line: cols = line.split(self.col_sep) self.query_id = cols[self.query_id_col].strip() self.subject_id = cols[self.subject_id_col].strip() self.evalue = Decimal(cols[self.evalue_col].strip()) self.bitscore = Decimal(cols[self.bitscore_col].strip()) def __repr__(self): vals = (self.__class__.__name__, self.query_id, self.subject_id, self.evalue, self.bitscore) return "%s(query_id=%s, subject_id=%s, evalue=%s, bitscore=%s)" % vals class Gene(object): def __init__(self, gene_id): self.id = gene_id self.best_hit = None # the best hit with KO was not used because it lacked a module # -m flag changed this result self.module_skip = False def add_blast_hit(self, hit): logging.debug("In %s, adding blast hit %s" % (self, hit)) if (not self.best_hit) or (hit.evalue < self.best_hit.evalue): logging.debug("New best hit") self.best_hit = hit def __repr__(self): vals = (self.__class__.__name__, self.id, self.best_hit, self.module_skip) return "%s(id=%s, best_hit=%s, module_skip=%s)" % vals class KEGGAnnotator(object): def __init__(self, ko_genes_list_file, ko_module_list_file, ko_pathway_list_file, ko_enzyme_list_file, ko_file, module_file, pathway_file, blast_file=None, require_module=False): # To be output, a hit must have a module definition. The first (best) # hit with a module definition is used for each query. if require_module: self.add_blast_hit = self.add_blast_hit_module else: self.add_blast_hit = self.add_blast_hit_ko # Load KO lookup files found in genes/ko/ko_*.list logging.info("Creating new %s object" % self.__class__.__name__) logging.info("Loading ko_genes.list file") self.ko2genes = self.dict_from_tabular_file(ko_genes_list_file, remove_key_prefix=True, remove_val_prefix=False) logging.info("Done loading ko_genes.list file, found %i KOs" % len(self.ko2genes)) logging.info("Loading ko_module.list file") self.ko2modules = self.dict_from_tabular_file(ko_module_list_file) logging.info("Done loading ko_module.list file, found %i KOs" % len(self.ko2modules)) logging.info("Loading ko_pathway.list file") self.ko2pathways = self.dict_from_tabular_file(ko_pathway_list_file) logging.info("Done loading ko_pathway.list file, found %i KOs" % len(self.ko2pathways)) logging.info("Loading ko_enzyme.list file") self.ko2enzymes = self.dict_from_tabular_file(ko_enzyme_list_file) logging.info("Done loading ko_enzyme.list file, found %i KOs" % len(self.ko2enzymes)) # Make genes to KO lookup logging.info("Creating gene to KOs lookup table") self.gene2kos = dict() for ko, gene_list in self.ko2genes.items(): for g in gene_list: klist = self.gene2kos.setdefault(g, []) klist.append(ko) logging.info("Done creating gene to KOs lookup table") # Load KO, module, pathway definition file logging.info("Loading ko file") self.kos = self.parse_ko_file(ko_file) logging.info("Done loading ko file") logging.info("Loading module file") self.modules = self.parse_module_file(module_file) logging.info("Done loading module file") logging.info("Loading pathway file") self.pathways = self.parse_pathway_file(pathway_file) logging.info("Done loading pathway file") # keep track of genes which had ko, useful to mark cases where -m changed # results. self.genes_with_ko = {} self.genes = {} if blast_file: logging.info("Adding new blast results file") self.add_blast_results(blast_file) logging.info("Done adding new blast results") def dict_from_tabular_file(self, f, key_column=0, value_column=1, remove_key_prefix=True, remove_val_prefix=True, comment_symbol=None): d = dict() max_column = max(key_column, value_column) line_num = 0 for line in f: line_num += 1 if comment_symbol and line[0] == comment_symbol: continue fields = line.split() if max_column > len(fields): logging.error("Highest column is greater than columns in line %i" % line_num) sys.exit(1) # Remove ko:, md:, etc. if required if remove_key_prefix: fields[key_column] = self.remove_prefix(fields[key_column]) if remove_val_prefix: fields[value_column] = self.remove_prefix(fields[value_column]) if fields[key_column] in d: #logging.debug("Duplicate key %s encountered" % fields[key_column]) d[fields[key_column]].append(fields[value_column]) else: d[fields[key_column]] = [fields[value_column]] return d def remove_prefix(self, kegg_id): """Remove ko:, md:, path:, ec: from ID""" return kegg_id.split(":")[1] def parse_ko_file(self, ko_file): """Parse genes/ko/ko file""" k = dict() cur_entry = "" cur_name = "" cur_def = "" line_no = 0 for line in ko_file: line_no += 1 if line.startswith("ENTRY"): cur_entry = line.split()[1].rstrip() elif line.startswith("NAME"): cur_name = line.split(None, 1)[1].rstrip() elif line.startswith("DEFINITION"): cur_def = line.split(None, 1)[1].rstrip() elif line.startswith("///"): if (not cur_entry) or (not cur_name): sys.stderr.write("Error parsing %s at %i\n" % (ko_file.name, line_no)) sys.exit(1) k[cur_entry] = {"name": cur_name, "def": cur_def} cur_entry = "" cur_name = "" cur_def = "" return k def parse_module_file(self, module_file): """Parse module/module file""" m = dict() cur_entry = "" cur_name = "" cur_class = "" line_no = 0 for line in module_file: line_no += 1 if line.startswith("ENTRY"): cur_entry = line.split()[1].rstrip() elif line.startswith("NAME"): cur_name = line.split(None, 1)[1].rstrip() elif line.startswith("CLASS"): cur_class = line.split(None, 1)[1].rstrip() elif line.startswith("///"): if (not cur_entry) or (not cur_name): sys.stderr.write("Error parsing %s at %i\n" % (module_file.name, line_no)) sys.exit(1) m[cur_entry] = {"name": cur_name, "class": cur_class} cur_entry = "" cur_name = "" cur_class = "" return m def parse_pathway_file(self, pathway_file): """Parse pathway/pathway file""" p = dict() cur_name = "" cur_desc = "" cur_class = "" cur_entry = "" line_no = 0 for line in pathway_file: line_no += 1 if line.startswith("ENTRY"): fields = line.split() if fields[1].startswith("k"): cur_entry = fields[1] elif cur_entry and line.startswith("NAME"): cur_name = line.split(None, 1)[1].rstrip() elif cur_entry and line.startswith("DESCRIPTION"): cur_desc = line.split(None, 1)[1].rstrip() elif cur_entry and line.startswith("CLASS"): cur_class = line.split(None, 1)[1].rstrip() elif line.startswith("///"): if cur_entry: if (not cur_name) and (not cur_desc) and (not cur_class): sys.stderr.write("Error parsing %s at %i\n" % (pathway_file.name, line_no)) sys.exit(1) p[cur_entry] = {"name": cur_name, "desc": cur_desc, "class": cur_class} cur_pathway = "" cur_name = "" cur_desc = "" cur_class = "" cur_entry = "" return p def add_blast_results(self, blast_file): cnt = 0 for hit in blast_result_iterator(blast_file): self.add_blast_hit(hit) cnt += 1 logging.info("Found %i blast results, %i passed filter" % (cnt, len(self.genes))) # Only add hit if it has KO annotation def add_blast_hit_ko(self, hit): logging.debug("Attempting to add hit: %s" % hit) qid = hit.query_id if (not qid in self.genes) and self.get_hit_ko_list(hit): self.genes[qid] = Gene(qid) self.genes[qid].add_blast_hit(hit) # Only add hit if it has module annotation def add_blast_hit_module(self, hit): logging.debug("Attempting to add hit: %s" % hit) qid = hit.query_id if (not qid in self.genes): kos = self.get_hit_ko_list(hit) if kos: if self.get_hit_module_list(kos): # This hit has module annotations so add it g = Gene(qid) self.genes[qid] = g if qid in self.genes_with_ko: g.module_skip = True # note that this results differs because of -m g.add_blast_hit(hit) # add the hit result to the Gene object else: # This hit didn't have module annotations, but had ko so add to # genes_with_ko dict self.genes_with_ko[qid] = True def get_hit_ko_list(self, hit): if not hit: return None try: return sorted(self.gene2kos[hit.subject_id]) except KeyError: logging.debug("KOs not found for gene %s" % hit.subject_id) return None def get_hit_module_list(self, kos): answer = set() if kos: for ko in kos: if ko in self.ko2modules: answer.update(self.ko2modules[ko]) if len(answer): return sorted(answer) else: if kos: logging.debug("Modules not found for KOs %s" % kos) return None def get_hit_pathway_list(self, kos): answer = set() if kos: for ko in kos: try: answer.update(self.ko2pathways[ko]) except KeyError: pass if len(answer): return sorted(answer) else: if kos: logging.debug("Pathways not found for KOs %s" % kos) return None def get_hit_enzyme_list(self, kos): answer = set() if kos: for ko in kos: try: answer.update(self.ko2enzymes[ko]) except KeyError: pass if len(answer): return sorted(answer) else: if kos: logging.debug("Enzymes not found for KOs %s" % kos) return None def make_basic_report_text(self): yield "\t".join(["query", "gene", "KO", "KO_names", "KO_descriptions", "modules", "module_names", "module_classes", "pathways", "pathway_names", "pathway_classes", "EC", "evalue", "score", "module_skip"]) annotations = self.get_gene_annotations() for gene_id, annot in annotations.iteritems(): yield self.tabify_annotations(gene_id, annot) def get_gene_annotations(self): out = dict() for gene_id, gene in self.genes.iteritems(): out[gene_id] = self.aggregate_hit_data(gene) return out def aggregate_hit_data(self, gene): hit = gene.best_hit out = {"gene": "", "kos": [], "ko_names": [], "ko_defs": [], "modules": [], "module_names": [], "module_classes": [], "pathways": [], "pathway_names": [], "pathway_classes": [], "enzymes": [], "evalue": "", "score": "", "module_skip": False } # Add hit gene if hit: out["gene"] = hit.subject_id out["evalue"] = hit.evalue out["score"] = hit.bitscore out["module_skip"] = gene.module_skip kos = self.get_hit_ko_list(hit) if len(kos) > 1: logging.info("More than one KO for %s" % gene.id) out.update(self.ko_annots(kos)) return out def ko_annots(self, kos): """Make a dictionary of KEGG annotations for a list of KOs""" out = {} if kos: out["kos"] = kos out["ko_names"] = [self.kos[k]["name"] for k in kos] out["ko_defs"] = [self.kos[k]["def"] for k in kos] # Build module list modules = self.get_hit_module_list(kos) if modules: out["modules"] = modules out["module_names"] = [self.modules[m]["name"] for m in modules] out["module_classes"] = [self.modules[m]["class"] for m in modules] # Build pathway list pathways = self.get_hit_pathway_list(kos) if pathways: out["pathways"] = pathways out["pathway_names"] = [self.pathways[p]["name"] for p in pathways] out["pathway_classes"] = [self.pathways[p]["class"] for p in pathways] # Build enzyme list enzymes = self.get_hit_enzyme_list(kos) if enzymes: out["enzymes"] = enzymes return out def tabify_annotations(self, gene_id, annot): """Create tab delimited string for hit data created by aggregate_hit_data""" out_text = gene_id + "\t" out_text += "\t".join([annot["gene"], self.tabify_ko_annotations(annot), str(annot["evalue"]), str(annot["score"]), str(annot["module_skip"])]) return out_text def tabify_ko_annotations(self, annot): """Tabify output from ko_annots""" return "\t".join(["; ".join(annot["kos"]), "; ".join(annot["ko_names"]), "; ".join(annot["ko_defs"]), "; ".join(annot["modules"]), "; ".join(annot["module_names"]), " ::: ".join(annot["module_classes"]), "; ".join(annot["pathways"]), "; ".join(annot["pathway_names"]), " ::: ".join(annot["pathway_classes"]), "; ".join(annot["enzymes"])])
apache-2.0
2,386,312,616,748,014,000
37.8
100
0.510371
false
3.819077
false
false
false
ryanpbrewster/SciVis-2015
examples/sdf_example.py
1
2689
""" The Example is from http://darksky.slac.stanford.edu/scivis2015/examples.html """ from sdfpy import load_sdf from thingking import loadtxt prefix = "../data/" # Load N-body particles from a = 1.0 dataset. Particles have positions with # units of proper kpc, and velocities with units of km/s. particles = load_sdf(prefix+"ds14_scivis_0128_e4_dt04_1.0000") # Load the a=1 Rockstar hlist file. The header of the file lists the useful # units/information. scale, id, desc_scale, desc_id, num_prog, pid, upid, desc_pid, phantom, \ sam_mvir, mvir, rvir, rs, vrms, mmp, scale_of_last_MM, vmax, x, y, z, \ vx, vy, vz, Jx, Jy, Jz, Spin, Breadth_first_ID, Depth_first_ID, \ Tree_root_ID, Orig_halo_ID, Snap_num, Next_coprogenitor_depthfirst_ID, \ Last_progenitor_depthfirst_ID, Rs_Klypin, M_all, M200b, M200c, M500c, \ M2500c, Xoff, Voff, Spin_Bullock, b_to_a, c_to_a, A_x, A_y, A_z, \ b_to_a_500c, c_to_a_500c, A_x_500c, A_y_500c, A_z_500c, T_over_U, \ M_pe_Behroozi, M_pe_Diemer, Macc, Mpeak, Vacc, Vpeak, Halfmass_Scale, \ Acc_Rate_Inst, Acc_Rate_100Myr, Acc_Rate_Tdyn = \ loadtxt(prefix+"rockstar/hlists/hlist_1.00000.list", unpack=True) # Now we want to convert the proper kpc of the particle position to comoving # Mpc/h, a common unit used in computational cosmology in general, but # specifically is used as the output unit in the merger tree halo list loaded # in above. First we get the Hubble parameter, here stored as 'h_100' in the # SDF parameters. Then we load the simulation width, L0, which is also in # proper kpc. Finally we load the scale factor, a, which for this particular # snapshot is equal to 1 since we are loading the final snapshot from the # simulation. h_100 = particles.parameters['h_100'] width = particles.parameters['L0'] cosmo_a = particles.parameters['a'] kpc_to_Mpc = 1./1000 sl = slice(0,None) # Define a simple function to convert proper to comoving Mpc/h. convert_to_cMpc = lambda proper: (proper + width/2.) * h_100 * kpc_to_Mpc / cosmo_a # Plot all the particles, adding a bit of alpha so that we see the density of # points. import matplotlib.pylab as pl pl.figure(figsize=[10,10]) pl.scatter(convert_to_cMpc(particles['x'][sl]), convert_to_cMpc(particles['y'][sl]), color='b', s=1.0, alpha=0.05) # Plot all the halos in red. pl.scatter(x, y, color='r', alpha=0.1) # Add some labels pl.xlabel('x [cMpc/h]') pl.ylabel('y [cMpc/h]') pl.savefig("halos_and_particles.png", bbox_inches='tight') # Could now consider coloring halos by any of the various quantities above. # Perhaps mvir would be nice to show the virial Mass of the halo, or we could # scale the points by the virial radius, rvir.
mit
9,184,363,794,019,417,000
43.081967
83
0.706582
false
2.716162
false
false
false
rhcarvalho/kombu
kombu/tests/async/http/test_curl.py
1
5102
# -*- coding: utf-8 -*- from __future__ import absolute_import from kombu.async.http.curl import READ, WRITE, CurlClient from kombu.tests.case import ( HubCase, Mock, call, patch, case_requires, set_module_symbol, ) @case_requires('pycurl') class test_CurlClient(HubCase): class Client(CurlClient): Curl = Mock(name='Curl') def test_when_pycurl_missing(self): with set_module_symbol('kombu.async.http.curl', 'pycurl', None): with self.assertRaises(ImportError): self.Client() def test_max_clients_set(self): x = self.Client(max_clients=303) self.assertEqual(x.max_clients, 303) def test_init(self): with patch('kombu.async.http.curl.pycurl') as _pycurl: x = self.Client() self.assertIsNotNone(x._multi) self.assertIsNotNone(x._pending) self.assertIsNotNone(x._free_list) self.assertIsNotNone(x._fds) self.assertEqual( x._socket_action, x._multi.socket_action, ) self.assertEqual(len(x._curls), x.max_clients) self.assertTrue(x._timeout_check_tref) x._multi.setopt.assert_has_calls([ call(_pycurl.M_TIMERFUNCTION, x._set_timeout), call(_pycurl.M_SOCKETFUNCTION, x._handle_socket), ]) def test_close(self): with patch('kombu.async.http.curl.pycurl'): x = self.Client() x._timeout_check_tref = Mock(name='timeout_check_tref') x.close() x._timeout_check_tref.cancel.assert_called_with() for _curl in x._curls: _curl.close.assert_called_with() x._multi.close.assert_called_with() def test_add_request(self): with patch('kombu.async.http.curl.pycurl'): x = self.Client() x._process_queue = Mock(name='_process_queue') x._set_timeout = Mock(name='_set_timeout') request = Mock(name='request') x.add_request(request) self.assertIn(request, x._pending) x._process_queue.assert_called_with() x._set_timeout.assert_called_with(0) def test_handle_socket(self): with patch('kombu.async.http.curl.pycurl') as _pycurl: hub = Mock(name='hub') x = self.Client(hub) fd = Mock(name='fd1') # POLL_REMOVE x._fds[fd] = fd x._handle_socket(_pycurl.POLL_REMOVE, fd, x._multi, None, _pycurl) hub.remove.assert_called_with(fd) self.assertNotIn(fd, x._fds) x._handle_socket(_pycurl.POLL_REMOVE, fd, x._multi, None, _pycurl) # POLL_IN hub = x.hub = Mock(name='hub') fds = [fd, Mock(name='fd2'), Mock(name='fd3')] x._fds = {f: f for f in fds} x._handle_socket(_pycurl.POLL_IN, fd, x._multi, None, _pycurl) hub.remove.assert_has_calls([call(fd)]) hub.add_reader.assert_called_with(fd, x.on_readable, fd) self.assertEqual(x._fds[fd], READ) # POLL_OUT hub = x.hub = Mock(name='hub') x._handle_socket(_pycurl.POLL_OUT, fd, x._multi, None, _pycurl) hub.add_writer.assert_called_with(fd, x.on_writable, fd) self.assertEqual(x._fds[fd], WRITE) # POLL_INOUT hub = x.hub = Mock(name='hub') x._handle_socket(_pycurl.POLL_INOUT, fd, x._multi, None, _pycurl) hub.add_reader.assert_called_with(fd, x.on_readable, fd) hub.add_writer.assert_called_with(fd, x.on_writable, fd) self.assertEqual(x._fds[fd], READ | WRITE) # UNKNOWN EVENT hub = x.hub = Mock(name='hub') x._handle_socket(0xff3f, fd, x._multi, None, _pycurl) # FD NOT IN FDS hub = x.hub = Mock(name='hub') x._fds.clear() x._handle_socket(0xff3f, fd, x._multi, None, _pycurl) self.assertFalse(hub.remove.called) def test_set_timeout(self): x = self.Client() x._set_timeout(100) def test_timeout_check(self): with patch('kombu.async.http.curl.pycurl') as _pycurl: x = self.Client() x._process_pending_requests = Mock(name='process_pending') x._multi.socket_all.return_value = 333, 1 _pycurl.error = KeyError x._timeout_check(_pycurl=_pycurl) x._multi.socket_all.return_value = None x._multi.socket_all.side_effect = _pycurl.error(333) x._timeout_check(_pycurl=_pycurl) def test_on_readable_on_writeable(self): with patch('kombu.async.http.curl.pycurl') as _pycurl: x = self.Client() x._on_event = Mock(name='on_event') fd = Mock(name='fd') x.on_readable(fd, _pycurl=_pycurl) x._on_event.assert_called_with(fd, _pycurl.CSELECT_IN) x.on_writable(fd, _pycurl=_pycurl) x._on_event.assert_called_with(fd, _pycurl.CSELECT_OUT)
bsd-3-clause
-1,647,547,606,994,934,000
37.074627
78
0.555076
false
3.396804
true
false
false
Azure/azure-sdk-for-python
sdk/graphrbac/azure-graphrbac/azure/graphrbac/models/application_create_parameters.py
1
8780
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. # # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is # regenerated. # -------------------------------------------------------------------------- from .application_base import ApplicationBase class ApplicationCreateParameters(ApplicationBase): """Request parameters for creating a new application. All required parameters must be populated in order to send to Azure. :param allow_guests_sign_in: A property on the application to indicate if the application accepts other IDPs or not or partially accepts. :type allow_guests_sign_in: bool :param allow_passthrough_users: Indicates that the application supports pass through users who have no presence in the resource tenant. :type allow_passthrough_users: bool :param app_logo_url: The url for the application logo image stored in a CDN. :type app_logo_url: str :param app_roles: The collection of application roles that an application may declare. These roles can be assigned to users, groups or service principals. :type app_roles: list[~azure.graphrbac.models.AppRole] :param app_permissions: The application permissions. :type app_permissions: list[str] :param available_to_other_tenants: Whether the application is available to other tenants. :type available_to_other_tenants: bool :param error_url: A URL provided by the author of the application to report errors when using the application. :type error_url: str :param group_membership_claims: Configures the groups claim issued in a user or OAuth 2.0 access token that the app expects. :type group_membership_claims: object :param homepage: The home page of the application. :type homepage: str :param informational_urls: URLs with more information about the application. :type informational_urls: ~azure.graphrbac.models.InformationalUrl :param is_device_only_auth_supported: Specifies whether this application supports device authentication without a user. The default is false. :type is_device_only_auth_supported: bool :param key_credentials: A collection of KeyCredential objects. :type key_credentials: list[~azure.graphrbac.models.KeyCredential] :param known_client_applications: Client applications that are tied to this resource application. Consent to any of the known client applications will result in implicit consent to the resource application through a combined consent dialog (showing the OAuth permission scopes required by the client and the resource). :type known_client_applications: list[str] :param logout_url: the url of the logout page :type logout_url: str :param oauth2_allow_implicit_flow: Whether to allow implicit grant flow for OAuth2 :type oauth2_allow_implicit_flow: bool :param oauth2_allow_url_path_matching: Specifies whether during a token Request Azure AD will allow path matching of the redirect URI against the applications collection of replyURLs. The default is false. :type oauth2_allow_url_path_matching: bool :param oauth2_permissions: The collection of OAuth 2.0 permission scopes that the web API (resource) application exposes to client applications. These permission scopes may be granted to client applications during consent. :type oauth2_permissions: list[~azure.graphrbac.models.OAuth2Permission] :param oauth2_require_post_response: Specifies whether, as part of OAuth 2.0 token requests, Azure AD will allow POST requests, as opposed to GET requests. The default is false, which specifies that only GET requests will be allowed. :type oauth2_require_post_response: bool :param org_restrictions: A list of tenants allowed to access application. :type org_restrictions: list[str] :param optional_claims: :type optional_claims: ~azure.graphrbac.models.OptionalClaims :param password_credentials: A collection of PasswordCredential objects :type password_credentials: list[~azure.graphrbac.models.PasswordCredential] :param pre_authorized_applications: list of pre-authorized applications. :type pre_authorized_applications: list[~azure.graphrbac.models.PreAuthorizedApplication] :param public_client: Specifies whether this application is a public client (such as an installed application running on a mobile device). Default is false. :type public_client: bool :param publisher_domain: Reliable domain which can be used to identify an application. :type publisher_domain: str :param reply_urls: A collection of reply URLs for the application. :type reply_urls: list[str] :param required_resource_access: Specifies resources that this application requires access to and the set of OAuth permission scopes and application roles that it needs under each of those resources. This pre-configuration of required resource access drives the consent experience. :type required_resource_access: list[~azure.graphrbac.models.RequiredResourceAccess] :param saml_metadata_url: The URL to the SAML metadata for the application. :type saml_metadata_url: str :param sign_in_audience: Audience for signing in to the application (AzureADMyOrganization, AzureADAllOrganizations, AzureADAndMicrosoftAccounts). :type sign_in_audience: str :param www_homepage: The primary Web page. :type www_homepage: str :param display_name: Required. The display name of the application. :type display_name: str :param identifier_uris: A collection of URIs for the application. :type identifier_uris: list[str] """ _validation = { 'display_name': {'required': True}, } _attribute_map = { 'allow_guests_sign_in': {'key': 'allowGuestsSignIn', 'type': 'bool'}, 'allow_passthrough_users': {'key': 'allowPassthroughUsers', 'type': 'bool'}, 'app_logo_url': {'key': 'appLogoUrl', 'type': 'str'}, 'app_roles': {'key': 'appRoles', 'type': '[AppRole]'}, 'app_permissions': {'key': 'appPermissions', 'type': '[str]'}, 'available_to_other_tenants': {'key': 'availableToOtherTenants', 'type': 'bool'}, 'error_url': {'key': 'errorUrl', 'type': 'str'}, 'group_membership_claims': {'key': 'groupMembershipClaims', 'type': 'object'}, 'homepage': {'key': 'homepage', 'type': 'str'}, 'informational_urls': {'key': 'informationalUrls', 'type': 'InformationalUrl'}, 'is_device_only_auth_supported': {'key': 'isDeviceOnlyAuthSupported', 'type': 'bool'}, 'key_credentials': {'key': 'keyCredentials', 'type': '[KeyCredential]'}, 'known_client_applications': {'key': 'knownClientApplications', 'type': '[str]'}, 'logout_url': {'key': 'logoutUrl', 'type': 'str'}, 'oauth2_allow_implicit_flow': {'key': 'oauth2AllowImplicitFlow', 'type': 'bool'}, 'oauth2_allow_url_path_matching': {'key': 'oauth2AllowUrlPathMatching', 'type': 'bool'}, 'oauth2_permissions': {'key': 'oauth2Permissions', 'type': '[OAuth2Permission]'}, 'oauth2_require_post_response': {'key': 'oauth2RequirePostResponse', 'type': 'bool'}, 'org_restrictions': {'key': 'orgRestrictions', 'type': '[str]'}, 'optional_claims': {'key': 'optionalClaims', 'type': 'OptionalClaims'}, 'password_credentials': {'key': 'passwordCredentials', 'type': '[PasswordCredential]'}, 'pre_authorized_applications': {'key': 'preAuthorizedApplications', 'type': '[PreAuthorizedApplication]'}, 'public_client': {'key': 'publicClient', 'type': 'bool'}, 'publisher_domain': {'key': 'publisherDomain', 'type': 'str'}, 'reply_urls': {'key': 'replyUrls', 'type': '[str]'}, 'required_resource_access': {'key': 'requiredResourceAccess', 'type': '[RequiredResourceAccess]'}, 'saml_metadata_url': {'key': 'samlMetadataUrl', 'type': 'str'}, 'sign_in_audience': {'key': 'signInAudience', 'type': 'str'}, 'www_homepage': {'key': 'wwwHomepage', 'type': 'str'}, 'display_name': {'key': 'displayName', 'type': 'str'}, 'identifier_uris': {'key': 'identifierUris', 'type': '[str]'}, } def __init__(self, **kwargs): super(ApplicationCreateParameters, self).__init__(**kwargs) self.display_name = kwargs.get('display_name', None) self.identifier_uris = kwargs.get('identifier_uris', None)
mit
-113,552,795,501,562,910
53.875
114
0.685877
false
4.116268
false
false
false
diamondman/pys3streamer
s3streamer/streamer.py
1
4416
from __future__ import unicode_literals from __future__ import print_function from boto.s3.connection import S3Connection class S3Streamer(object): #def __init__(self, bucket_name, *key_names, s3_connection=None, key_is_prefix=False): def __init__(self, bucket_name, *key_names, **kwargs): """Create a new S3Streamer. Automatically creates a Boto S3Connection if one is not provided. Args: bucket_name: String name of the Amazon S3 bucket. All keys read will be read from this bucket. key_names: List of key names to cat together and read, or list of key prefixes. s3_connection (optional): A Boto S3Connection object. One will be created from your env settings if not provided key_is_prefix (optional): Boolean determining if key_names should be interpreted as key prefixes (True means interpret as key_prefix). Note no deduplication is done for keys when multiple key prefixes are used. It is possible to get the same key multiple times if it matches multiple prefixes. """ if not len(key_names): raise ValueError("At least one key name must be provided") self._key_strs = key_names s3_connection = kwargs.pop('s3_connection', None) key_is_prefix = kwargs.pop('key_is_prefix', False) if kwargs.keys(): raise TypeError("unexpected keyword argument %s" % list(kwargs.keys())[0]) self._conn = s3_connection or S3Connection() self._bucket_name = bucket_name self._bucket = None self._current_key_str = iter(self._key_strs) self._key_is_prefix = key_is_prefix self._tmp_iter = None self._cur_key = None self._readline_buff = b'' self._key_names_accessed = [] self._read_buffer_size = 1*1024*1024 self._hit_eof = False @property def bucket(self): """Get the Amazon S3 boto bucket object.""" if not self._bucket: self._bucket = self._conn.get_bucket(self._bucket_name) return self._bucket def __repr__(self): return "<%s: %s>"%(self.__class__.__name__, self._bucket_name) @property def keys_read(self): """List the Amazon S3 keys that have been read so far""" return list(self._key_names_accessed) @property def _next_key(self): if self._key_is_prefix: if not self._tmp_iter: try: k_str = next(self._current_key_str) self._tmp_iter = iter(self.bucket.list(prefix=k_str)) except StopIteration as e: return None try: k = next(self._tmp_iter) self._key_names_accessed.append(k.name) return k except StopIteration as e: self._tmp_iter = None return self._next_key else: try: k_str = next(self._current_key_str) except StopIteration as e: return None return self.bucket.get_key(k_str) or self._next_key def _select_next_key(self): self._cur_key = self._next_key self._hit_eof = False return self._cur_key @property def _current_key(self): if not self._cur_key: self._select_next_key() return self._cur_key def read(self, size): if size is 0: raise ValueError("size 0 unsupported because it is dangerous.") d = self._current_key.read(size) if len(d) is not size and not self._hit_eof: d2 = self._current_key.read(size-len(d)) if not d2: #HIT EOF self._hit_eof = True d += b'\n' return d d += d2 if d: return d if not self._select_next_key(): return b'' return self.read(size) def readline(self): while b'\n' not in self._readline_buff: d = self.read(self._read_buffer_size) if not d: return b'' self._readline_buff += d line, _, self._readline_buff = self._readline_buff.partition(b'\n') return line+b'\n' def __iter__(self): while True: d = self.readline() if not d: break yield d def close(self): pass
mit
-8,990,907,671,535,923,000
34.328
305
0.55933
false
4.032877
false
false
false
Duroktar/cookbook
AIML Files/ALICE/ALICE_bot.py
1
1830
# - LINKS - ALICE Bot # # By: traBpUkciP (2016) import aiml # AI-Markup Language library import datetime import time import urllib # library for dealing with web stuff through Python import sys, os path = os.path.dirname(os.path.abspath(sys.argv[0])) BRAIN_FILE = path + "/bot_brain_ALICE.brn" k = aiml.Kernel() k.bootstrap(brainFile=BRAIN_FILE) def talk(text): # THIS URL NEEDS TO BE SET TO YOUR PORT AND KEY ACCORDINGLY # THIS PART ONLY WORK IF YOU HAVE LINKS WEB REQUEST SETTINGS ON DEFAULT url = 'http://127.0.0.1:54657/?action=[Speak("placeholder")]&key=1234ABC' #set default talk to jarvis address newurl = url.replace("placeholder", text) #fill in text to be spoken urllib.urlopen(newurl) def userInput(): f = open( path + "/dictation.txt") a = f.read() f.close() if a == "False": return False else: print "---------------------------------------------------------------------------------" print "Input from LINKS detected!" aliceSpeak(a) def clearInput(): f = open( path + '/dictation.txt', 'w') f.write("False") f.close() def writeHistory(i): f = open( path + '/history.txt', 'a') ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') a = st + ": " + i print a print "---------------------------------------------------------------------------------" f.write(a) f.write('\n') f.close() def aliceSpeak(feed): message = feed writeHistory(message) clearInput() bot_response = k.respond(message) talk(bot_response) main() def main(): while userInput() == False: time.sleep(.1) print "working" continue try: while True: main() except KeyboardInterrupt: pass
gpl-3.0
-182,710,782,391,264,220
18.0625
114
0.553005
false
3.465909
false
false
false
opadron/girder
girder/api/v1/resource.py
1
21286
#!/usr/bin/env python # -*- coding: utf-8 -*- ############################################################################### # Copyright 2013 Kitware Inc. # # Licensed under the Apache License, Version 2.0 ( the "License" ); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ############################################################################### import cherrypy import json from ..describe import Description, describeRoute from ..rest import Resource as BaseResource, RestException from girder.constants import AccessType from girder.api import access from girder.models.model_base import AccessControlledModel from girder.utility import acl_mixin from girder.utility import ziputil from girder.utility.progress import ProgressContext # Plugins can modify this set to allow other types to be searched allowedSearchTypes = {'collection', 'folder', 'group', 'item', 'user'} class Resource(BaseResource): """ API Endpoints that deal with operations across multiple resource types. """ def __init__(self): super(Resource, self).__init__() self.resourceName = 'resource' self.route('GET', ('search',), self.search) self.route('GET', ('lookup',), self.lookup) self.route('GET', (':id',), self.getResource) self.route('GET', ('download',), self.download) self.route('POST', ('download',), self.download) self.route('PUT', ('move',), self.moveResources) self.route('POST', ('copy',), self.copyResources) self.route('DELETE', (), self.delete) @access.public @describeRoute( Description('Search for resources in the system.') .param('q', 'The search query.') .param('mode', 'The search mode. Can use either a text search or a ' 'prefix-based search.', enum=('text', 'prefix'), required=False, default='text') .param('types', 'A JSON list of resource types to search for, e.g. ' "'user', 'folder', 'item'.") .param('level', 'Minimum required access level.', required=False, dataType='int', default=AccessType.READ) .pagingParams(defaultSort=None, defaultLimit=10) .errorResponse('Invalid type list format.') ) def search(self, params): self.requireParams(('q', 'types'), params) mode = params.get('mode', 'text') level = AccessType.validate(params.get('level', AccessType.READ)) user = self.getCurrentUser() limit = int(params.get('limit', 10)) offset = int(params.get('offset', 0)) if mode == 'text': method = 'textSearch' elif mode == 'prefix': method = 'prefixSearch' else: raise RestException( 'The search mode must be either "text" or "prefix".') try: types = json.loads(params['types']) except ValueError: raise RestException('The types parameter must be JSON.') results = {} for modelName in types: if modelName not in allowedSearchTypes: continue if '.' in modelName: name, plugin = modelName.rsplit('.', 1) model = self.model(name, plugin) else: model = self.model(modelName) results[modelName] = [ model.filter(d, user) for d in getattr(model, method)( query=params['q'], user=user, limit=limit, offset=offset, level=level) ] return results def _validateResourceSet(self, params, allowedModels=None): """ Validate a JSON string listing resources. The resources parameter is a JSON encoded dictionary with each key a model name and each value a list of ids that must be present in that model. :param params: a dictionary of parameters that must include 'resources' :param allowedModels: if present, an iterable of models that may be included in the resources. :returns: the JSON decoded resource dictionary. """ self.requireParams(('resources', ), params) try: resources = json.loads(params['resources']) except ValueError: raise RestException('The resources parameter must be JSON.') if type(resources) is not dict: raise RestException('Invalid resources format.') if allowedModels: for key in resources: if key not in allowedModels: raise RestException('Resource type not supported.') count = sum([len(resources[key]) for key in resources]) if not count: raise RestException('No resources specified.') return resources def _getResourceModel(self, kind, funcName=None): """ Load and return a model with a specific function or throw an exception. :param kind: the name of the model to load :param funcName: a function name to ensure that each model contains. :returns: the loaded model. """ try: model = self.model(kind) except ImportError: model = None if not model or (funcName and not hasattr(model, funcName)): raise RestException('Invalid resources format.') return model def _lookUpToken(self, token, parentType, parent): """ Find a particular child resource by name or throw an exception. :param token: the name of the child resource to find :param parentType: the type of the parent to search :param parent: the parent resource :returns: the child resource """ seekFolder = (parentType in ('user', 'collection', 'folder')) seekItem = (parentType == 'folder') seekFile = (parentType == 'item') # (model name, mask, search filter) searchTable = ( ('folder', seekFolder, {'name': token, 'parentId': parent['_id'], 'parentCollection': parentType}), ('item', seekItem, {'name': token, 'folderId': parent['_id']}), ('file', seekFile, {'name': token, 'itemId': parent['_id']}), ) for candidateModel, mask, filterObject in searchTable: if not mask: continue candidateChild = self.model(candidateModel).findOne(filterObject) if candidateChild is not None: return candidateChild, candidateModel # if no folder, item, or file matches, give up raise RestException('Child resource not found: %s(%s)->%s' % ( parentType, parent.get('name', parent.get('_id')), token)) def _lookUpPath(self, path, user): pathArray = [token for token in path.split('/') if token] model = pathArray[0] parent = None if model == 'user': username = pathArray[1] parent = self.model('user').findOne({'login': username}) if parent is None: raise RestException('User not found: %s' % username) elif model == 'collection': collectionName = pathArray[1] parent = self.model('collection').findOne({'name': collectionName}) if parent is None: raise RestException( 'Collection not found: %s' % collectionName) else: raise RestException('Invalid path format') try: document = parent self.model(model).requireAccess(document, user) for token in pathArray[2:]: document, model = self._lookUpToken(token, model, document) self.model(model).requireAccess(document, user) except RestException: raise RestException('Path not found: %s' % path) result = self.model(model).filter(document, user) return result @access.public def lookup(self, params): self.requireParams('path', params) return self._lookUpPath(params['path'], self.getCurrentUser()) lookup.description = ( Description('Look up a resource in the data hierarchy by path.') .param('path', 'The path of the resource. The path must be an absolute Unix ' 'path starting with either "/user/[user name]", for a user\'s ' 'resources or "/collection/[collection name]", for resources ' 'under a collection.') .errorResponse('Path is invalid.') .errorResponse('Path refers to a resource that does not exist.') .errorResponse('Read access was denied for the resource.', 403)) @access.cookie(force=True) @access.public def download(self, params): """ Returns a generator function that will be used to stream out a zip file containing the listed resource's contents, filtered by permissions. """ user = self.getCurrentUser() resources = self._validateResourceSet(params) # Check that all the resources are valid, so we don't download the zip # file if it would throw an error. for kind in resources: model = self._getResourceModel(kind, 'fileList') for id in resources[kind]: if not model.load(id=id, user=user, level=AccessType.READ): raise RestException('Resource %s %s not found.' % (kind, id)) metadata = self.boolParam('includeMetadata', params, default=False) cherrypy.response.headers['Content-Type'] = 'application/zip' cherrypy.response.headers['Content-Disposition'] = \ 'attachment; filename="Resources.zip"' def stream(): zip = ziputil.ZipGenerator() for kind in resources: model = self.model(kind) for id in resources[kind]: doc = model.load(id=id, user=user, level=AccessType.READ) for (path, file) in model.fileList( doc=doc, user=user, includeMetadata=metadata, subpath=True): for data in zip.addFile(file, path): yield data yield zip.footer() return stream download.description = ( Description('Download a set of items, folders, collections, and users ' 'as a zip archive.') .notes('This route is also exposed via the POST method because the ' 'request parameters can be quite long, and encoding them in the ' 'URL (as is standard when using the GET method) can cause the ' 'URL to become too long, which causes errors.') .param('resources', 'A JSON-encoded list of types to download. Each ' 'type is a list of ids. For example: {"item": [(item id 1), ' '(item id 2)], "folder": [(folder id 1)]}.') .param('includeMetadata', 'Include any metadata in JSON files in the ' 'archive.', required=False, dataType='boolean', default=False) .errorResponse('Unsupport or unknown resource type.') .errorResponse('Invalid resources format.') .errorResponse('No resources specified.') .errorResponse('Resource not found.') .errorResponse('Read access was denied for a resource.', 403)) @access.user def delete(self, params): """ Delete a set of resources. """ user = self.getCurrentUser() resources = self._validateResourceSet(params) total = sum([len(resources[key]) for key in resources]) progress = self.boolParam('progress', params, default=False) with ProgressContext(progress, user=user, title='Deleting resources', message='Calculating size...') as ctx: ctx.update(total=total) current = 0 for kind in resources: model = self._getResourceModel(kind, 'remove') for id in resources[kind]: if (isinstance(model, (acl_mixin.AccessControlMixin, AccessControlledModel))): doc = model.load(id=id, user=user, level=AccessType.ADMIN) else: doc = model.load(id=id) if not doc: raise RestException('Resource %s %s not found.' % (kind, id)) # Don't do a subtree count if we weren't asked for progress if progress: subtotal = model.subtreeCount(doc) if subtotal != 1: total += model.subtreeCount(doc)-1 ctx.update(total=total) model.remove(doc, progress=ctx) if progress: current += subtotal if ctx.progress['data']['current'] != current: ctx.update(current=current, message='Deleted ' + kind) delete.description = ( Description('Delete a set of items, folders, or other resources.') .param('resources', 'A JSON-encoded list of types to delete. Each ' 'type is a list of ids. For example: {"item": [(item id 1), ' '(item id2)], "folder": [(folder id 1)]}.') .param('progress', 'Whether to record progress on this task.', default=False, required=False, dataType='boolean') .errorResponse('Unsupport or unknown resource type.') .errorResponse('Invalid resources format.') .errorResponse('No resources specified.') .errorResponse('Resource not found.') .errorResponse('Admin access was denied for a resource.', 403)) @access.admin def getResource(self, id, params): model = self._getResourceModel(params['type']) if (isinstance(model, (acl_mixin.AccessControlMixin, AccessControlledModel))): user = self.getCurrentUser() return model.load(id=id, user=user, level=AccessType.READ) return model.load(id=id) getResource.description = ( Description('Get any resource by ID.') .param('id', 'The ID of the resource.', paramType='path') .param('type', 'The type of the resource (item, file, etc.).') .errorResponse('ID was invalid.') .errorResponse('Read access was denied for the resource.', 403)) def _prepareMoveOrCopy(self, params): user = self.getCurrentUser() resources = self._validateResourceSet(params, ('folder', 'item')) parentType = params['parentType'].lower() if parentType not in ('user', 'collection', 'folder'): raise RestException('Invalid parentType.') if ('item' in resources and len(resources['item']) > 0 and parentType != 'folder'): raise RestException('Invalid parentType.') parent = self.model(parentType).load( params['parentId'], level=AccessType.WRITE, user=user, exc=True) progress = self.boolParam('progress', params, default=False) return user, resources, parent, parentType, progress @access.user def moveResources(self, params): """ Move the specified resources to a new parent folder, user, or collection. Only folder and item resources can be moved with this function. """ user, resources, parent, parentType, progress = \ self._prepareMoveOrCopy(params) total = sum([len(resources[key]) for key in resources]) with ProgressContext(progress, user=user, title='Moving resources', message='Calculating requirements...', total=total) as ctx: for kind in resources: model = self._getResourceModel(kind, 'move') for id in resources[kind]: doc = model.load(id=id, user=user, level=AccessType.WRITE) if not doc: raise RestException('Resource %s %s not found.' % (kind, id)) ctx.update(message='Moving %s %s' % ( kind, doc.get('name', ''))) if kind == 'item': if parent['_id'] != doc['folderId']: model.move(doc, parent) elif kind == 'folder': if ((parentType, parent['_id']) != (doc['parentCollection'], doc['parentId'])): model.move(doc, parent, parentType) ctx.update(increment=1) moveResources.description = ( Description('Move a set of items and folders.') .param('resources', 'A JSON-encoded list of types to move. Each type ' 'is a list of ids. Only folders and items may be specified. ' 'For example: {"item": [(item id 1), (item id2)], "folder": ' '[(folder id 1)]}.') .param('parentType', 'Parent type for the new parent of these ' 'resources.') .param('parentId', 'Parent ID for the new parent of these resources.') .param('progress', 'Whether to record progress on this task. Default ' 'is false.', required=False, dataType='boolean') .errorResponse('Unsupport or unknown resource type.') .errorResponse('Invalid resources format.') .errorResponse('Resource type not supported.') .errorResponse('No resources specified.') .errorResponse('Resource not found.') .errorResponse('ID was invalid.')) @access.user def copyResources(self, params): """ Copy the specified resources to a new parent folder, user, or collection. Only folder and item resources can be copied with this function. """ user, resources, parent, parentType, progress = \ self._prepareMoveOrCopy(params) total = len(resources.get('item', [])) if 'folder' in resources: model = self._getResourceModel('folder') for id in resources['folder']: folder = model.load(id=id, user=user, level=AccessType.READ) if folder: total += model.subtreeCount(folder) with ProgressContext(progress, user=user, title='Copying resources', message='Calculating requirements...', total=total) as ctx: for kind in resources: model = self._getResourceModel(kind) for id in resources[kind]: doc = model.load(id=id, user=user, level=AccessType.READ) if not doc: raise RestException('Resource not found. No %s with ' 'id %s' % (kind, id)) ctx.update(message='Copying %s %s' % ( kind, doc.get('name', ''))) if kind == 'item': model.copyItem(doc, folder=parent, creator=user) ctx.update(increment=1) elif kind == 'folder': model.copyFolder( doc, parent=parent, parentType=parentType, creator=user, progress=ctx) copyResources.description = ( Description('Copy a set of items and folders.') .param('resources', 'A JSON-encoded list of types to copy. Each type ' 'is a list of ids. Only folders and items may be specified. ' 'For example: {"item": [(item id 1), (item id2)], "folder": ' '[(folder id 1)]}.') .param('parentType', 'Parent type for the new parent of these ' 'resources.') .param('parentId', 'Parent ID for the new parent of these resources.') .param('progress', 'Whether to record progress on this task. Default ' 'is false.', required=False, dataType='boolean') .errorResponse('Unsupport or unknown resource type.') .errorResponse('Invalid resources format.') .errorResponse('Resource type not supported.') .errorResponse('No resources specified.') .errorResponse('Resource not found.') .errorResponse('ID was invalid.'))
apache-2.0
4,512,090,057,241,301,500
44.482906
80
0.558677
false
4.741813
false
false
false
twiindan/selenium_lessons
01_Introducción/02_tools_and_environment.py
1
2414
#=============================================================================== # - CPython is the real name of default standard Python implementation # - Implemented in C # - There are Python implementations in other languages: # - Jython: Python 2.5 interpreter written in Java which runs bytecode in the JVM # - IronPython: Similar approach for .NET Common Language Runtime # - JS, C++, CIL... # - Stackless Python: CPython fork with microthreads concurrency # - PyPy: Python 2.7 interpreter implemented in Python. #=============================================================================== #=============================================================================== # - IPython: create a comprehensive environment for interactive and exploratory computing # - An enhanced interactive Python shell #=============================================================================== #=============================================================================== # - virtualenv: a tool to create isolated Python environments. # - It simply changes your PATH environment var to point to a different folder #=============================================================================== #=============================================================================== # - PyPi: The Python Package Index is a repository of software for the Python programming language. # - There are currently 96678 packages here. # - The packages are 'eggs' #=============================================================================== #=============================================================================== # - pip: A tool for installing and managing Python packages # - It installs packages from PyPi, local folders or Git and other repositories # - It can read a list of packages from a file or generate the list of installed packages #=============================================================================== #=============================================================================== # - IDE? # - PyCharm (we have licenses, ask IT) # - Eclipse + Pydev # - NetBeans # - NINJA IDE # - Python's IDLE (not recommendable at all) # - ... # - Emacs # - Vi # - http://wiki.python.org/moin/IntegratedDevelopmentEnvironments # - Lots of good IDEs, it's up to you! #===============================================================================
apache-2.0
-6,505,159,516,177,435,000
44.566038
99
0.42792
false
5.816867
false
false
false
wangyang59/tf_models
video_prediction/prediction_train_flo_learn_ip_sintel_test.py
1
30243
# # 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 # Copyright 2016 The TensorFlow Authors All Rights Reserved. # # 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. # ============================================================================== """Code for training the prediction model.""" import numpy as np import tensorflow as tf from tensorflow.python.platform import app from tensorflow.python.platform import flags from prediction_input_flo_sintel import build_tfrecord_input, DATA_DIR from prediction_model_flo_chair_ip import construct_model from visualize import plot_flo_learn_symm, plot_general from optical_flow_warp import transformer from optical_flow_warp_fwd import transformerFwd from optical_flow_warp_old import transformer_old import os # How often to record tensorboard summaries. SUMMARY_INTERVAL = 20 # How often to run a batch through the validation model. VAL_INTERVAL = 200 # How often to save a model checkpoint SAVE_INTERVAL = 500 FLAGS = flags.FLAGS flags.DEFINE_string('data_dir', DATA_DIR, 'directory containing data.') flags.DEFINE_string('output_dir', "", 'directory for model checkpoints.') flags.DEFINE_integer('num_iterations', 100000, 'number of training iterations.') flags.DEFINE_string('pretrained_model', '', 'filepath of a pretrained model to initialize from.') flags.DEFINE_float('train_val_split', 0.95, 'The percentage of files to use for the training set,' ' vs. the validation set.') flags.DEFINE_integer('batch_size', 32, 'batch size for training') flags.DEFINE_float('learning_rate', 0.001, 'the base learning rate of the generator') flags.DEFINE_integer('num_gpus', 1, 'the number of gpu to use') def get_black_list(clses): blacklist = [] for cls in clses: fname = "/home/wangyang59/Data/ILSVRC2016/ImageSets/VID/train_%s.txt" % cls with open(fname) as f: content = f.readlines() blacklist += [x.split(" ")[0].split("/")[-1] + ".tfrecord" for x in content] return blacklist ## Helper functions def peak_signal_to_noise_ratio(true, pred): """Image quality metric based on maximal signal power vs. power of the noise. Args: true: the ground truth image. pred: the predicted image. Returns: peak signal to noise ratio (PSNR) """ return 10.0 * tf.log(1.0 / mean_squared_error(true, pred)) / tf.log(10.0) def mean_squared_error(true, pred): """L2 distance between tensors true and pred. Args: true: the ground truth image. pred: the predicted image. Returns: mean squared error between ground truth and predicted image. """ return tf.reduce_sum(tf.square(true - pred)) / tf.to_float(tf.size(pred)) def mean_charb_error(true, pred, beta): return tf.reduce_sum(tf.sqrt((tf.square(beta*(true-pred)) + 0.001*0.001))) / tf.to_float(tf.size(pred)) def mean_charb_error_wmask(true, pred, mask, beta): return tf.reduce_sum(tf.sqrt((tf.square(beta*(true-pred)) + 0.001*0.001))*mask) / tf.to_float(tf.size(pred)) def weighted_mean_squared_error(true, pred, weight): """L2 distance between tensors true and pred. Args: true: the ground truth image. pred: the predicted image. Returns: mean squared error between ground truth and predicted image. """ tmp = tf.reduce_sum(weight*tf.square(true-pred), axis=[1,2], keep_dims=True) / tf.reduce_sum(weight, axis=[1, 2], keep_dims=True) return tf.reduce_mean(tmp) #return tf.reduce_sum(tf.square(true - pred)*weight) / tf.to_float(tf.size(pred)) #return tf.reduce_sum(tf.square(true - pred)*weight) / tf.reduce_sum(weight) def mean_L1_error(true, pred): """L2 distance between tensors true and pred. Args: true: the ground truth image. pred: the predicted image. Returns: mean squared error between ground truth and predicted image. """ return tf.reduce_sum(tf.abs(true - pred)) / tf.to_float(tf.size(pred)) def weighted_mean_L1_error(true, pred, weight): """L2 distance between tensors true and pred. Args: true: the ground truth image. pred: the predicted image. Returns: mean squared error between ground truth and predicted image. """ return tf.reduce_sum(tf.abs(true - pred)*weight) / tf.to_float(tf.size(pred)) def gradient_x(img): gx = img[:,:,:-1,:] - img[:,:,1:,:] return gx def gradient_y(img): gy = img[:,:-1,:,:] - img[:,1:,:,:] return gy def cal_grad_error(flo, image, beta): """Calculate the gradient of the given image by calculate the difference between nearby pixels """ error = 0.0 img_grad_x = gradient_x(image) img_grad_y = gradient_y(image) weights_x = tf.exp(-10.0*tf.reduce_mean(tf.abs(img_grad_x), 3, keep_dims=True)) weights_y = tf.exp(-10.0*tf.reduce_mean(tf.abs(img_grad_y), 3, keep_dims=True)) error += weighted_mean_L1_error(flo[:, 1:, :, :], flo[:, :-1, :, :], weights_y*beta) error += weighted_mean_L1_error(flo[:, :, 1:, :], flo[:, :, :-1, :], weights_x*beta) #error += mean_charb_error_wmask(flo[:, 1:, :, :], flo[:, :-1, :, :], weights_y, beta) #error += mean_charb_error_wmask(flo[:, :, 1:, :], flo[:, :, :-1, :], weights_x, beta) return error / 2.0 def img_grad_error(true, pred, mask, beta): error = 0.0 error += mean_charb_error_wmask(true[:, 1:, :, :] - true[:, :-1, :, :], pred[:, 1:, :, :] - pred[:, :-1, :, :], mask[:, 1:, :, :], beta) error += mean_charb_error_wmask(true[:, :, 1:, :] - true[:, :, :-1, :], pred[:, :, 1:, :] - pred[:, :, :-1, :], mask[:, :, 1:, :], beta) return error / 2.0 def cal_epe(flo1, flo2): return tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(flo1 - flo2), axis=3))) def blur(image): batch_size, img_height, img_width, color_channels = map(int, image.get_shape()[0:4]) kernel = np.array([1., 2., 1., 2., 4., 2., 1., 2., 1.], dtype=np.float32) / 16.0 kernel = kernel.reshape((3, 3, 1, 1)) kernel = tf.constant(kernel, shape=(3, 3, 1, 1), name='gaussian_kernel', verify_shape=True) blur_image = tf.nn.depthwise_conv2d(tf.pad(image, [[0,0], [1,1], [1,1],[0,0]], "SYMMETRIC"), tf.tile(kernel, [1, 1, color_channels, 1]), [1, 1, 1, 1], 'VALID') return blur_image def down_sample(image, to_blur=True): batch_size, img_height, img_width, color_channels = map(int, image.get_shape()[0:4]) if to_blur: image = blur(image) return tf.image.resize_bicubic(image, [img_height/2, img_width/2]) def get_pyrimad(image): image2 = down_sample(down_sample(image)) image3 = down_sample(image2) image4 = down_sample(image3) image5 = down_sample(image4) image6 = down_sample(image5) # image2 = tf.image.resize_area(image, [img_height/4, img_width/4]) # image3 = tf.image.resize_area(image, [img_height/8, img_width/8]) # image4 = tf.image.resize_area(image, [img_height/16, img_width/16]) # image5 = tf.image.resize_area(image, [img_height/32, img_width/32]) # image6 = tf.image.resize_area(image, [img_height/64, img_width/64]) return image2, image3, image4, image5, image6 def get_channel(image): zeros = tf.zeros_like(image) ones = tf.ones_like(image) #gray = 0.21*image[:, :, :, 0] + 0.72*image[:, :, :, 1] + 0.07*image[:, :, :, 2] channels = [] for i in range(10): channels.append(tf.where(tf.logical_and(image >= i/10.0, image < (i+1)/10.0), ones, zeros)) return tf.concat([image]+channels, axis=3) def average_gradients(tower_grads): """Calculate the average gradient for each shared variable across all towers. Note that this function provides a synchronization point across all towers. Args: tower_grads: List of lists of (gradient, variable) tuples. The outer list is over individual gradients. The inner list is over the gradient calculation for each tower. Returns: List of pairs of (gradient, variable) where the gradient has been averaged across all towers. """ average_grads = [] for grad_and_vars in zip(*tower_grads): # Note that each grad_and_vars looks like the following: # ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN)) grads = [] for g, _ in grad_and_vars: # Add 0 dimension to the gradients to represent the tower. expanded_g = tf.expand_dims(g, 0) # Append on a 'tower' dimension which we will average over below. grads.append(expanded_g) # Average over the 'tower' dimension. grad = tf.concat(axis=0, values=grads) grad = tf.reduce_mean(grad, 0) # Keep in mind that the Variables are redundant because they are shared # across towers. So .. we will just return the first tower's pointer to # the Variable. v = grad_and_vars[0][1] grad_and_var = (grad, v) average_grads.append(grad_and_var) return average_grads class Model(object): def __init__(self, image1=None, image2=None, true_flo=None, reuse_scope=False, scope=None, prefix="train"): #self.prefix = prefix = tf.placeholder(tf.string, []) self.iter_num = tf.placeholder(tf.float32, []) summaries = [] batch_size, H, W, color_channels = map(int, image1.get_shape()[0:4]) # if not reuse_scope: # image2_recon, feature2 = autoencoder(image2, trainable=False) # else: # If it's a validation or test model. # with tf.variable_scope(scope, reuse=True): # image2_recon, feature2 = autoencoder(image2, trainable=False) # # with tf.variable_scope(scope, reuse=True): # image1_recon, feature1 = autoencoder(image1, trainable=False) image1_pyrimad = get_pyrimad(get_channel(image1)) image2_pyrimad = get_pyrimad(get_channel(image2)) image1_2, image1_3, image1_4, image1_5, image1_6 = image1_pyrimad image2_2, image2_3, image2_4, image2_5, image2_6 = image2_pyrimad if not reuse_scope: flow2, flow3, flow4, flow5, flow6, image1_trans = construct_model(image1, image2, image1_pyrimad, image2_pyrimad) else: # If it's a validation or test model. with tf.variable_scope(scope, reuse=True): flow2, flow3, flow4, flow5, flow6, image1_trans = construct_model(image1, image2, image1_pyrimad, image2_pyrimad) with tf.variable_scope(scope, reuse=True): flow2r, flow3r, flow4r, flow5r, flow6r, _ = construct_model(image2, image1, image2_pyrimad, image1_pyrimad) occu_mask_6 = tf.clip_by_value(transformerFwd(tf.ones(shape=[batch_size, H/64, W/64, 1], dtype='float32'), 20*flow6r/64.0, [H/64, W/64]), clip_value_min=0.0, clip_value_max=1.0) occu_mask_5 = tf.clip_by_value(transformerFwd(tf.ones(shape=[batch_size, H/32, W/32, 1], dtype='float32'), 20*flow5r/32.0, [H/32, W/32]), clip_value_min=0.0, clip_value_max=1.0) occu_mask_4 = tf.clip_by_value(transformerFwd(tf.ones(shape=[batch_size, H/16, W/16, 1], dtype='float32'), 20*flow4r/16.0, [H/16, W/16]), clip_value_min=0.0, clip_value_max=1.0) occu_mask_3 = tf.clip_by_value(transformerFwd(tf.ones(shape=[batch_size, H/8, W/8, 1], dtype='float32'), 20*flow3r/8.0, [H/8, W/8]), clip_value_min=0.0, clip_value_max=1.0) occu_mask_2 = tf.clip_by_value(transformerFwd(tf.ones(shape=[batch_size, H/4, W/4, 1], dtype='float32'), 20*flow2r/4.0, [H/4, W/4]), clip_value_min=0.0, clip_value_max=1.0) image1_2p, image1_3p, image1_4p, image1_5p, image1_6p = image1_trans loss6 = mean_charb_error_wmask(image1_6, image1_6p, occu_mask_6, 1.0) loss5 = mean_charb_error_wmask(image1_5, image1_5p, occu_mask_5, 1.0) loss4 = mean_charb_error_wmask(image1_4, image1_4p, occu_mask_4, 1.0) loss3 = mean_charb_error_wmask(image1_3, image1_3p, occu_mask_3, 1.0) loss2 = mean_charb_error_wmask(image1_2, image1_2p, occu_mask_2, 1.0) grad_error6 = cal_grad_error(flow6, image1_6[:,:,:,0:3], 1.0/64.0) grad_error5 = cal_grad_error(flow5, image1_5[:,:,:,0:3], 1.0/32.0) grad_error4 = cal_grad_error(flow4, image1_4[:,:,:,0:3], 1.0/16.0) grad_error3 = cal_grad_error(flow3, image1_3[:,:,:,0:3], 1.0/8.0) grad_error2 = cal_grad_error(flow2, image1_2[:,:,:,0:3], 1.0/4.0) img_grad_error6 = img_grad_error(image1_6p, image1_6, occu_mask_6, 1.0) img_grad_error5 = img_grad_error(image1_5p, image1_5, occu_mask_5, 1.0) img_grad_error4 = img_grad_error(image1_4p, image1_4, occu_mask_4, 1.0) img_grad_error3 = img_grad_error(image1_3p, image1_3, occu_mask_3, 1.0) img_grad_error2 = img_grad_error(image1_2p, image1_2, occu_mask_2, 1.0) # feature1_6_norm = tf.nn.l2_normalize(feature1[4], dim=3) # feature1_6p = transformer(tf.nn.l2_normalize(feature2[4], dim=3), 20*flow6/64.0, [H/64, W/64], feature1_6_norm) # loss6f = mean_charb_error_wmask(feature1_6_norm, feature1_6p, occu_mask_6, 10.0) # # feature1_5_norm = tf.nn.l2_normalize(feature1[3], dim=3) # feature1_5p = transformer(tf.nn.l2_normalize(feature2[3], dim=3), 20*flow5/32.0, [H/32, W/32], feature1_5_norm) # loss5f = mean_charb_error_wmask(feature1_5_norm, feature1_5p, occu_mask_5, 10.0) # # #feature1_5p = transformer_old(feature2[3], 20*flow5/32.0, [H/32, W/32]) # # with tf.variable_scope(scope, reuse=True): # # image1_recon = decoder(feature1_6p, reuse_scope=True, trainable=True) # #image1_recon2 = decoder(feature1_5p, reuse_scope=True, trainable=TruH=e, level=5) # # loss_ae = mean_charb_error(image1_recon, image1, 1.0) + mean_charb_error(image2, image2_recon, 1.0) + loss5f + loss6f # # summaries.append(tf.summary.scalar(prefix + '_loss_ae', loss_ae)) # summaries.append(tf.summary.scalar(prefix + '_loss6f', loss6f)) # summaries.append(tf.summary.scalar(prefix + '_loss5f', loss5f)) # loss = 0.05*(loss2+img_grad_error2) + 0.1*(loss3+img_grad_error3) + \ # 0.2*(loss4+img_grad_error4) + 0.8*(loss5+img_grad_error5) + 3.2*(loss6+img_grad_error6) + \ # (0.05*grad_error2 + 0.1*grad_error3 + 0.2*grad_error4 + 0.0*grad_error5 + 0.0*grad_error6)*10.0 loss = 1.0*(loss2+img_grad_error2) + 1.0*(loss3+img_grad_error3) + \ 1.0*(loss4+img_grad_error4) + 1.0*(loss5+img_grad_error5) + 1.0*(loss6+img_grad_error6) + \ (1.0*grad_error2 + 1.0*grad_error3 + 1.0*grad_error4 + 1.0*grad_error5 + 1.0*grad_error6)*10.0 # loss = 3.2*(loss2+img_grad_error2) + 0.8*(loss3+img_grad_error3) + \ # 0.2*(loss4+img_grad_error4) + 0.1*(loss5+img_grad_error5) + 0.05*(loss6+img_grad_error6) + \ # (3.2*grad_error2 + 0.8*grad_error3 + 0.2*grad_error4 + 0.1*grad_error5 + 0.05*grad_error6)*10.0 self.loss = loss summaries.append(tf.summary.scalar(prefix + '_loss', self.loss)) summaries.append(tf.summary.scalar(prefix + '_loss2', loss2)) summaries.append(tf.summary.scalar(prefix + '_loss3', loss3)) summaries.append(tf.summary.scalar(prefix + '_loss4', loss4)) summaries.append(tf.summary.scalar(prefix + '_loss5', loss5)) summaries.append(tf.summary.scalar(prefix + '_loss6', loss6)) summaries.append(tf.summary.scalar(prefix + '_grad_loss2', grad_error2)) summaries.append(tf.summary.scalar(prefix + '_grad_loss3', grad_error3)) summaries.append(tf.summary.scalar(prefix + '_grad_loss4', grad_error4)) summaries.append(tf.summary.scalar(prefix + '_grad_loss5', grad_error5)) summaries.append(tf.summary.scalar(prefix + '_grad_loss6', grad_error6)) self.summ_op = tf.summary.merge(summaries) class Model_eval(object): def __init__(self, image1=None, image2=None, true_flo=None, true_occ_mask=None, scene=None, image_no=None, scope=None, prefix="eval"): #self.prefix = prefix = tf.placeholder(tf.string, []) self.iter_num = tf.placeholder(tf.float32, []) summaries = [] self.scene = scene self.image_no = image_no batch_size, H, W, color_channels = map(int, image1.get_shape()[0:4]) image1_pyrimad = get_pyrimad(get_channel(image1)) image2_pyrimad = get_pyrimad(get_channel(image2)) image1_2, image1_3, image1_4, image1_5, image1_6 = image1_pyrimad image2_2, image2_3, image2_4, image2_5, image2_6 = image2_pyrimad with tf.variable_scope(scope, reuse=True): flow2, flow3, flow4, flow5, flow6, image1_trans = construct_model(image1, image2, image1_pyrimad, image2_pyrimad) with tf.variable_scope(scope, reuse=True): flow2r, flow3r, flow4r, flow5r, flow6r, _ = construct_model(image2, image1, image2_pyrimad, image1_pyrimad) image1_2p, image1_3p, image1_4p, image1_5p, image1_6p = image1_trans occu_mask_2 = tf.clip_by_value(transformerFwd(tf.ones(shape=[batch_size, H/4, W/4, 1], dtype='float32'), 20*flow2r/4.0, [H/4, W/4]), clip_value_min=0.0, clip_value_max=1.0) # with tf.variable_scope(scope, reuse=True): # image2_recon, feature2 = autoencoder(image2, reuse_scope=True, trainable=False) # # feature1_6p = transformer_old(feature2[4], 20*flow6/64.0, [H/64, W/64]) # with tf.variable_scope(scope, reuse=True): # image1_recon = decoder(feature1_6p, reuse_scope=True, trainable=False) #feature1_5p = transformer_old(feature2[3], 20*flow5/32.0, [H/32, W/32]) #image1_recon2 = decoder(feature1_5p, reuse_scope=True, trainable=True, level=5) # loss_ae = mean_charb_error(image1, image1_recon, 1.0) + mean_charb_error(image2, image2_recon, 1.0) # self.image_ae = [image1, image2, image1_recon, image2_recon] # summaries.append(tf.summary.scalar(prefix + '_loss_ae', loss_ae)) true_flo_scale = tf.concat([true_flo[:,:,:,0:1], true_flo[:,:,:,1:2]/436.0*448.0], axis=3) self.orig_image1 = tf.image.resize_bicubic(image1, [436, 1024]) self.orig_image2 = tf.image.resize_bicubic(image2, [436, 1024]) self.true_flo = true_flo self.pred_flo = tf.image.resize_bicubic(20*tf.concat([flow2[:,:,:,0:1], flow2[:,:,:,1:2]/448.0*436.0], axis=3), [436, 1024]) self.true_warp = transformer_old(self.orig_image2, self.true_flo, [436, 1024]) self.pred_warp = transformer_old(self.orig_image2, self.pred_flo, [436, 1024]) self.pred_flo_r = tf.image.resize_bicubic(20*tf.concat([flow2r[:,:,:,0:1], flow2r[:,:,:,1:2]/448.0*436.0], axis=3), [436, 1024]) self.occu_mask = tf.image.resize_bicubic(occu_mask_2, [436, 1024]) self.occu_mask_test = 1.0 - true_occ_mask flow2_scale = tf.image.resize_bicubic(20*tf.concat([flow2[:,:,:,0:1], flow2[:,:,:,1:2]/448.0*436.0], axis=3), [436, 1024]) self.epe = cal_epe(true_flo, flow2_scale) self.epeInd = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(true_flo - flow2_scale), axis=3)), axis=[1, 2]) summaries.append(tf.summary.scalar(prefix + '_flo_loss', self.epe)) self.small_scales = [image1_4[:,:,:,0:3], image2_4[:,:,:,0:3], image1_4p[:,:,:,0:3], tf.image.resize_bicubic(true_flo_scale/16.0, [H/16, W/16]), 20*flow4/16.0, tf.image.resize_bicubic(true_flo_scale/16.0, [H/16, W/16])-20*flow4/16.0, image1_5[:,:,:,0:3], image2_5[:,:,:,0:3], image1_5p[:,:,:,0:3], tf.image.resize_bicubic(true_flo_scale/32.0, [H/32, W/32]), 20*flow5/32.0, tf.image.resize_bicubic(true_flo_scale/32.0, [H/32, W/32])-20*flow5/32.0, image1_6[:,:,:,0:3], image2_6[:,:,:,0:3], image1_6p[:,:,:,0:3], tf.image.resize_bicubic(true_flo_scale/64.0, [H/64, W/64]), 20*flow6/64.0, tf.image.resize_bicubic(true_flo_scale/64.0, [H/64, W/64])-20*flow6/64.0] self.occ_count = tf.reduce_mean(true_occ_mask) self.true_occ_mask = true_occ_mask self.occ_epe = cal_epe(true_flo*true_occ_mask, flow2_scale*true_occ_mask) self.nonocc_epe = cal_epe(true_flo*(1.0-true_occ_mask), flow2_scale*(1.0-true_occ_mask)) summaries.append(tf.summary.scalar(prefix + '_occ_count', self.occ_count)) summaries.append(tf.summary.scalar(prefix + '_occ_epe', self.occ_epe)) summaries.append(tf.summary.scalar(prefix + '_nonocc_epe', self.nonocc_epe)) self.summ_op = tf.summary.merge(summaries) def plot_all(model, itr, sess, feed_dict): orig_image1, true_flo, pred_flo, true_warp, pred_warp, pred_flo_r, occu_mask, occu_mask_test, small_scales = sess.run([model.orig_image1, model.true_flo, model.pred_flo, model.true_warp, model.pred_warp, model.pred_flo_r, model.occu_mask, model.occu_mask_test, model.small_scales], feed_dict) plot_flo_learn_symm(orig_image1, true_flo, pred_flo, true_warp, pred_warp, pred_flo_r, occu_mask, occu_mask_test, output_dir=FLAGS.output_dir, itr=itr) plot_general(small_scales, h=6, w=3, output_dir=FLAGS.output_dir, itr=itr, suffix="small") #plot_general(image_ae, h=2, w=2, output_dir=FLAGS.output_dir, itr=itr, suffix="ae") def main(unused_argv): if FLAGS.output_dir == "": raise Exception("OUT_DIR must be specified") if os.path.exists(FLAGS.output_dir): raise Exception("OUT_DIR already exist") print 'Constructing models and inputs.' with tf.Graph().as_default(), tf.device('/cpu:0'): train_op = tf.train.AdamOptimizer(FLAGS.learning_rate) tower_grads = [] itr_placeholders = [] image1, image2, flo, _= build_tfrecord_input(training=True) split_image1 = tf.split(axis=0, num_or_size_splits=FLAGS.num_gpus, value=image1) split_image2 = tf.split(axis=0, num_or_size_splits=FLAGS.num_gpus, value=image2) split_flo = tf.split(axis=0, num_or_size_splits=FLAGS.num_gpus, value=flo) eval_image1, eval_image2, eval_flo, eval_occ_mask, scenes, image_no = build_tfrecord_input(training=False, num_epochs=1) summaries_cpu = tf.get_collection(tf.GraphKeys.SUMMARIES, tf.get_variable_scope().name) with tf.variable_scope(tf.get_variable_scope()) as vs: for i in xrange(FLAGS.num_gpus): with tf.device('/gpu:%d' % i): if i == FLAGS.num_gpus - 1: scopename = "model" else: scopename = '%s_%d' % ("tower", i) with tf.name_scope(scopename) as ns: if i == 0: model = Model(split_image1[i], split_image2[i], split_flo[i], reuse_scope=False, scope=vs) else: model = Model(split_image1[i], split_image2[i], split_flo[i], reuse_scope=True, scope=vs) loss = model.loss # Retain the summaries from the final tower. if i == FLAGS.num_gpus - 1: summaries = tf.get_collection(tf.GraphKeys.SUMMARIES, ns) eval_model = Model_eval(eval_image1, eval_image2, eval_flo, eval_occ_mask, scenes, image_no, scope=vs) # Calculate the gradients for the batch of data on this CIFAR tower. grads = train_op.compute_gradients(loss) # Keep track of the gradients across all towers. tower_grads.append(grads) itr_placeholders.append(model.iter_num) # We must calculate the mean of each gradient. Note that this is the # synchronization point across all towers. grads = average_gradients(tower_grads) # Apply the gradients to adjust the shared variables. apply_gradient_op = train_op.apply_gradients(grads) # Create a saver. saver = tf.train.Saver( tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES), max_to_keep=5) # saver1 = tf.train.Saver( # list(set(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES))-set(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=".*ae.*"))), max_to_keep=5) # # saver2 = tf.train.Saver( # tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=".*ae.*"), max_to_keep=5) # Build the summary operation from the last tower summaries. summary_op = tf.summary.merge(summaries + summaries_cpu) # Make training session. sess = tf.Session(config=tf.ConfigProto( allow_soft_placement=True, log_device_placement=False)) summary_writer = tf.summary.FileWriter( FLAGS.output_dir, graph=sess.graph, flush_secs=10) if FLAGS.pretrained_model: saver.restore(sess, FLAGS.pretrained_model) #saver2.restore(sess, "./tmp/flow_exp/flow_learn_chair_copy_ae_bal/model65002") #sess.run(tf.initialize_variables(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=".*ae.*"))) #start_itr = int(FLAGS.pretrained_model.split("/")[-1][5:]) start_itr = 0 sess.run(tf.local_variables_initializer()) else: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) start_itr = 0 tf.train.start_queue_runners(sess) average_epe = tf.placeholder(tf.float32) average_epe_summary = tf.summary.scalar("model/eval_average_epe", average_epe) epes = [] average_occ_count = tf.placeholder(tf.float32) average_occ_count_summary = tf.summary.scalar("model/eval_average_occ_count", average_occ_count) occ_counts = [] average_epe_occ = tf.placeholder(tf.float32) average_epe_occ_summary = tf.summary.scalar("model/eval_average_epe_occ", average_epe_occ) epes_occ = [] average_epe_nonocc = tf.placeholder(tf.float32) average_epe_nonocc_summary = tf.summary.scalar("model/eval_average_epe_nonocc", average_epe_nonocc) epes_nonocc = [] # Run training. for itr in range(start_itr, FLAGS.num_iterations): # Generate new batch of data. feed_dict = {x:np.float32(itr) for x in itr_placeholders} eval_summary_str, epe, occ_count, occ_epe, nonocc_epe, \ orig_image1, orig_image2, true_flo, pred_flo, true_warp, pred_warp, \ pred_flo_r, occu_mask, occu_mask_test, small_scales, scene, image_no, epeInd, true_occ_mask = sess.run([eval_model.summ_op, eval_model.epe, eval_model.occ_count, eval_model.occ_epe, eval_model.nonocc_epe, eval_model.orig_image1, eval_model.orig_image2, eval_model.true_flo, eval_model.pred_flo, eval_model.true_warp, eval_model.pred_warp, eval_model.pred_flo_r, eval_model.occu_mask, eval_model.occu_mask_test, eval_model.small_scales, eval_model.scene, eval_model.image_no, eval_model.epeInd, eval_model.true_occ_mask]) idx = epeInd > 0.0 if np.sum(idx) > 0: ims1 = plot_general([orig_image1[idx], true_flo[idx], pred_flo[idx], occu_mask_test[idx], occu_mask[idx]], h=1, w=5, output_dir=FLAGS.output_dir, itr=itr, get_im=True) #ims2 = plot_general([tmp[idx] for tmp in small_scales], h=6, w=3, output_dir=FLAGS.output_dir, itr=itr, suffix="small", get_im=True) scene = scene[idx] image_no = image_no[idx] for i in range(np.sum(idx)): if not os.path.exists(os.path.join(FLAGS.output_dir, scene[i][0])): os.makedirs(os.path.join(FLAGS.output_dir, scene[i][0])) ims1[i].save(os.path.join(FLAGS.output_dir, scene[i][0], image_no[i][0] + ".jpeg")) #ims2[i].save(os.path.join(FLAGS.output_dir, scene[i][0], image_no[i][0] + "_small.jpeg")) epes.append(epe) occ_counts.append(occ_count) epes_occ.append(occ_epe) epes_nonocc.append(nonocc_epe) print(sum(epes)/len(epes)) feed = {average_epe: sum(epes)/len(epes)} epe_summary_str = sess.run(average_epe_summary, feed_dict=feed) feed = {average_occ_count: sum(occ_counts)/len(occ_counts)} epe_tier1_summary_str = sess.run(average_occ_count_summary, feed_dict=feed) feed = {average_epe_occ: sum(epes_occ)/len(epes_occ)} epe_tier2_summary_str = sess.run(average_epe_occ_summary, feed_dict=feed) feed = {average_epe_nonocc: sum(epes_nonocc)/len(epes_nonocc)} epe_tier3_summary_str = sess.run(average_epe_nonocc_summary, feed_dict=feed) summary_writer.add_summary(eval_summary_str, itr) summary_writer.add_summary(epe_summary_str, itr) summary_writer.add_summary(epe_tier1_summary_str, itr) summary_writer.add_summary(epe_tier2_summary_str, itr) summary_writer.add_summary(epe_tier3_summary_str, itr) if __name__ == '__main__': app.run()
apache-2.0
5,854,912,422,827,126,000
44.753404
173
0.606289
false
2.994653
false
false
false
andrewsomething/digitalocean-indicator
digitalocean_indicator/__init__.py
1
1487
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- ### BEGIN LICENSE # This program is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License version 3, as published # by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranties of # MERCHANTABILITY, SATISFACTORY QUALITY, 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/>. ### END LICENSE import optparse from locale import gettext as _ import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk # pylint: disable=E0611 from digitalocean_indicator import DoIndicator from digitalocean_indicator_lib import set_up_logging, get_version def parse_options(): """Support for command line options""" parser = optparse.OptionParser(version="%%prog %s" % get_version()) parser.add_option("-v", "--verbose", action="count", dest="verbose", help="Show debug messages") (options, args) = parser.parse_args() set_up_logging(options) def main(): 'constructor for your class instances' parse_options() # Run the application. do_indicator = DoIndicator.Indicator() Gtk.main()
gpl-3.0
8,986,540,471,270,268,000
31.326087
75
0.702757
false
3.923483
false
false
false
vandorjw/django-template-project
project/project_name/views.py
1
1288
from django.views.generic import TemplateView from django.views.generic.edit import FormView from blog.models import Article from django.core.mail import send_mail from {{project_name}}.forms import ContactForm class HomePageView(TemplateView): template_name="index.html" def get_context_data(self, **kwargs): context = super(HomePageView, self).get_context_data(**kwargs) context['top4articles'] = Article.objects.filter(is_active=True)[:4] return context class ContactPageView( FormView ): template_name="contact.html" form_class = ContactForm success_url = '/contact/' def form_valid(self, form): subject = form.cleaned_data['subject'] message = form.cleaned_data['message'] sender = form.cleaned_data['sender'] recipients = ('CHANGE_ME@{{ project_name }}.com',) send_mail(subject, message, sender, recipients) return super(ContactPageView, self).form_valid(self) class RobotPageView(TemplateView): template_name="robots.txt" content_type='text/plain' class HumanPageView(TemplateView): template_name="humans.txt" content_type='text/plain' #class GooglePageView(TemplateView): # template_name="googleXXXXXXXXXXX.html" # content_type='text/plain'
mit
2,950,621,827,330,681,000
32.025641
76
0.697205
false
3.722543
false
false
false
a25kk/aha
src/aha.sitecontent/aha/sitecontent/browser/pagesection.py
1
7060
# -*- coding: utf-8 -*- """Module providing views for a contentpage section""" from AccessControl import Unauthorized from Acquisition import aq_inner from Acquisition import aq_parent from plone import api from plone.api.exc import InvalidParameterError from plone.protect.utils import addTokenToUrl from Products.CMFPlone.utils import safe_unicode from Products.Five.browser import BrowserView from zope.component import getMultiAdapter from zope.component import getUtility from aha.sitecontent.mailer import create_plaintext_message from aha.sitecontent.mailer import prepare_email_message from aha.sitecontent.mailer import get_mail_template from aha.sitecontent.mailer import send_mail from aha.sitecontent.interfaces import IResponsiveImagesTool from aha.sitecontent import _ class PageSectionView(BrowserView): """ Page Section default view """ def __call__(self): return self.render() def render(self): return self.index() def parent_page(self): return aq_parent(aq_inner(self.context)) def rendered_page_snippet(self): context = aq_inner(self.context) snippet = context.restrictedTraverse('@@pagesection-snippet')() if context.displayInquiryForm: snippet = context.restrictedTraverse('@@page-section-form')() return snippet class PageSectionSnippet(BrowserView): """ Embeddable section content snippet """ def field_has_data(self, fieldname): """ Check wether a given schema key returns a value""" context = aq_inner(self.context) try: video_link = getattr(context, fieldname, None) except AttributeError: video_link = None if video_link is not None: return True return False def has_video_link(self): return self.field_has_data('videoLink') def has_external_image(self): return self.field_has_data('externalImage') def show_image(self): display = True if self.has_video_link() or self.has_external_image(): display = False return display def get_image_data(self, uuid): tool = getUtility(IResponsiveImagesTool) return tool.create(uuid) class PageSectionForm(BrowserView): """ Embeddable section content snippet including inquiry form """ def __call__(self): return self.render() def update(self): self.errors = {} unwanted = ('_authenticator', 'form.button.Submit') required = ['email'] required_boolean = ('privacy-policy-agreement', 'privacy-policy') if 'form.button.Submit' in self.request: authenticator = getMultiAdapter((self.context, self.request), name=u"authenticator") if not authenticator.verify(): raise Unauthorized form = self.request.form form_data = {} form_errors = {} error_idx = 0 if self.privacy_policy_enabled(): for field_name in required_boolean: if not field_name in form: form_errors[field_name] = self.required_field_error() error_idx += 1 for value in form: if value not in unwanted: form_data[value] = safe_unicode(form[value]) if not form[value] and value in required: form_errors[value] = self.required_field_error() error_idx += 1 else: error = { 'active': False, 'msg': form[value] } form_errors[value] = error if error_idx > 0: self.errors = form_errors else: self.send_inquiry(form) def render(self): self.update() return self.index() def default_value(self, error): value = '' if error['active'] is False: value = error['msg'] return value @staticmethod def required_field_error(): translation_service = api.portal.get_tool(name="translation_service") error = {} error_msg = _(u"This field is required") error['active'] = True error['msg'] = translation_service.translate( error_msg, 'aha.sitecontent', target_language=api.portal.get_default_language() ) return error @staticmethod def privacy_policy_enabled(): return True @staticmethod def privacy_policy_url(): portal = api.portal.get() portal_url = portal.absolute_url() url = '{0}/raum/datenschutzerklaerung'.format(portal_url) return url def send_inquiry(self, data): context = aq_inner(self.context) subject = _(u"Inquiry from website visitor") email_subject = api.portal.translate( "Inquiry from website visitor", 'aha.sitecontent', api.portal.get_current_language()) data['subject'] = email_subject mail_tpl = self._compose_message(data) mail_plain = create_plaintext_message(mail_tpl) msg = prepare_email_message(mail_tpl, mail_plain) recipients = ['[email protected]', ] send_mail( msg, recipients, email_subject ) context_parent = aq_parent(context) next_url = '{0}/@@inquiry-form-dispatched/'.format( context_parent.absolute_url() ) url = addTokenToUrl(next_url) return self.request.response.redirect(url) def _compose_message(self, data): portal = api.portal.get() portal_url = portal.absolute_url() template_vars = { 'email': data['email'], 'subject': str(data['subject']), 'fullname': data['fullname'], 'phone': data['phone'], 'message': data['comment'], 'url': portal_url } template_name = 'inquiry-mail.html' message = get_mail_template(template_name, template_vars) return message def field_has_data(self, fieldname): """ Check wether a given schema key returns a value""" context = aq_inner(self.context) try: video_link = getattr(context, fieldname, None) except AttributeError: video_link = None if video_link is not None: return True return False def has_video_link(self): return self.field_has_data('videoLink') def has_external_image(self): return self.field_has_data('externalImage') def show_image(self): display = True if self.has_video_link() or self.has_external_image(): display = False return display def get_image_data(self, uuid): tool = getUtility(IResponsiveImagesTool) return tool.create(uuid)
mit
-960,660,328,584,240,100
31.837209
77
0.585694
false
4.235153
false
false
false
dylanninin/schema
test_schema.py
1
18858
from __future__ import with_statement from collections import defaultdict, namedtuple from operator import methodcaller import os from pytest import raises from schema import Schema, Use, And, Or, Optional, SchemaError, JSONSchema try: basestring except NameError: basestring = str # Python 3 does not have basestring SE = raises(SchemaError) def ve(_): raise ValueError() def se(_): raise SchemaError('first auto', 'first error') def test_schema(): assert Schema(1).validate(1) == 1 with SE: Schema(1).validate(9) assert Schema(int).validate(1) == 1 with SE: Schema(int).validate('1') assert Schema(Use(int)).validate('1') == 1 with SE: Schema(int).validate(int) assert Schema(str).validate('hai') == 'hai' with SE: Schema(str).validate(1) assert Schema(Use(str)).validate(1) == '1' assert Schema(list).validate(['a', 1]) == ['a', 1] assert Schema(dict).validate({'a': 1}) == {'a': 1} with SE: Schema(dict).validate(['a', 1]) assert Schema(lambda n: 0 < n < 5).validate(3) == 3 with SE: Schema(lambda n: 0 < n < 5).validate(-1) def test_validate_file(): assert Schema( Use(open)).validate('LICENSE-MIT').read().startswith('Copyright') with SE: Schema(Use(open)).validate('NON-EXISTENT') assert Schema(os.path.exists).validate('.') == '.' with SE: Schema(os.path.exists).validate('./non-existent/') assert Schema(os.path.isfile).validate('LICENSE-MIT') == 'LICENSE-MIT' with SE: Schema(os.path.isfile).validate('NON-EXISTENT') def test_and(): assert And(int, lambda n: 0 < n < 5).validate(3) == 3 with SE: And(int, lambda n: 0 < n < 5).validate(3.33) assert And(Use(int), lambda n: 0 < n < 5).validate(3.33) == 3 with SE: And(Use(int), lambda n: 0 < n < 5).validate('3.33') def test_or(): assert Or(int, dict).validate(5) == 5 assert Or(int, dict).validate({}) == {} with SE: Or(int, dict).validate('hai') assert Or(int).validate(4) with SE: Or().validate(2) def test_validate_list(): assert Schema([1, 0]).validate([1, 0, 1, 1]) == [1, 0, 1, 1] assert Schema([1, 0]).validate([]) == [] with SE: Schema([1, 0]).validate(0) with SE: Schema([1, 0]).validate([2]) assert And([1, 0], lambda l: len(l) > 2).validate([0, 1, 0]) == [0, 1, 0] with SE: And([1, 0], lambda l: len(l) > 2).validate([0, 1]) def test_list_tuple_set_frozenset(): assert Schema([int]).validate([1, 2]) with SE: Schema([int]).validate(['1', 2]) assert Schema(set([int])).validate(set([1, 2])) == set([1, 2]) with SE: Schema(set([int])).validate([1, 2]) # not a set with SE: Schema(set([int])).validate(['1', 2]) assert Schema(tuple([int])).validate(tuple([1, 2])) == tuple([1, 2]) with SE: Schema(tuple([int])).validate([1, 2]) # not a set def test_strictly(): assert Schema(int).validate(1) == 1 with SE: Schema(int).validate('1') def test_dict(): assert Schema({'key': 5}).validate({'key': 5}) == {'key': 5} with SE: Schema({'key': 5}).validate({'key': 'x'}) with SE: Schema({'key': 5}).validate(['key', 5]) assert Schema({'key': int}).validate({'key': 5}) == {'key': 5} assert Schema({'n': int, 'f': float}).validate( {'n': 5, 'f': 3.14}) == {'n': 5, 'f': 3.14} with SE: Schema({'n': int, 'f': float}).validate( {'n': 3.14, 'f': 5}) with SE: try: Schema({}).validate({'abc': None, 1: None}) except SchemaError as e: assert e.args[0].startswith("Wrong keys 'abc', 1 in") raise with SE: try: Schema({'key': 5}).validate({}) except SchemaError as e: assert e.args[0] == "Missing keys: 'key'" raise with SE: try: Schema({'key': 5}).validate({'n': 5}) except SchemaError as e: assert e.args[0] == "Missing keys: 'key'" raise with SE: try: Schema({}).validate({'n': 5}) except SchemaError as e: assert e.args[0] == "Wrong keys 'n' in {'n': 5}" raise with SE: try: Schema({'key': 5}).validate({'key': 5, 'bad': 5}) except SchemaError as e: assert e.args[0] in ["Wrong keys 'bad' in {'key': 5, 'bad': 5}", "Wrong keys 'bad' in {'bad': 5, 'key': 5}"] raise with SE: try: Schema({}).validate({'a': 5, 'b': 5}) except SchemaError as e: assert e.args[0] in ["Wrong keys 'a', 'b' in {'a': 5, 'b': 5}", "Wrong keys 'a', 'b' in {'b': 5, 'a': 5}"] raise def test_dict_keys(): assert Schema({str: int}).validate( {'a': 1, 'b': 2}) == {'a': 1, 'b': 2} with SE: Schema({str: int}).validate({1: 1, 'b': 2}) assert Schema({Use(str): Use(int)}).validate( {1: 3.14, 3.14: 1}) == {'1': 3, '3.14': 1} def test_dict_optional_keys(): with SE: Schema({'a': 1, 'b': 2}).validate({'a': 1}) assert Schema({'a': 1, Optional('b'): 2}).validate({'a': 1}) == {'a': 1} assert Schema({'a': 1, Optional('b'): 2}).validate( {'a': 1, 'b': 2}) == {'a': 1, 'b': 2} # Make sure Optionals are favored over types: assert Schema({basestring: 1, Optional('b'): 2}).validate({'a': 1, 'b': 2}) == {'a': 1, 'b': 2} def test_dict_optional_defaults(): # Optionals fill out their defaults: assert Schema({Optional('a', default=1): 11, Optional('b', default=2): 22}).validate({'a': 11}) == {'a': 11, 'b': 2} # Optionals take precedence over types. Here, the "a" is served by the # Optional: assert Schema({Optional('a', default=1): 11, basestring: 22}).validate({'b': 22}) == {'a': 1, 'b': 22} with raises(TypeError): Optional(And(str, Use(int)), default=7) def test_dict_subtypes(): d = defaultdict(int, key=1) v = Schema({'key': 1}).validate(d) assert v == d assert isinstance(v, defaultdict) # Please add tests for Counter and OrderedDict once support for Python2.6 # is dropped! def test_complex(): s = Schema({'<file>': And([Use(open)], lambda l: len(l)), '<path>': os.path.exists, Optional('--count'): And(int, lambda n: 0 <= n <= 5)}) data = s.validate({'<file>': ['./LICENSE-MIT'], '<path>': './'}) assert len(data) == 2 assert len(data['<file>']) == 1 assert data['<file>'][0].read().startswith('Copyright') assert data['<path>'] == './' def test_nice_errors(): try: Schema(int, error='should be integer').validate('x') except SchemaError as e: assert e.errors == ['should be integer'] try: Schema(Use(float), error='should be a number').validate('x') except SchemaError as e: assert e.code == 'should be a number' try: Schema({Optional('i'): Use(int, error='should be a number')}).validate({'i': 'x'}) except SchemaError as e: assert e.code == 'should be a number' def test_use_error_handling(): try: Use(ve).validate('x') except SchemaError as e: assert e.autos == ["ve('x') raised ValueError()"] assert e.errors == [None] try: Use(ve, error='should not raise').validate('x') except SchemaError as e: assert e.autos == ["ve('x') raised ValueError()"] assert e.errors == ['should not raise'] try: Use(se).validate('x') except SchemaError as e: assert e.autos == [None, 'first auto'] assert e.errors == [None, 'first error'] try: Use(se, error='second error').validate('x') except SchemaError as e: assert e.autos == [None, 'first auto'] assert e.errors == ['second error', 'first error'] def test_or_error_handling(): try: Or(ve).validate('x') except SchemaError as e: assert e.autos[0].startswith('Or(') assert e.autos[0].endswith(") did not validate 'x'") assert e.autos[1] == "ve('x') raised ValueError()" assert len(e.autos) == 2 assert e.errors == [None, None] try: Or(ve, error='should not raise').validate('x') except SchemaError as e: assert e.autos[0].startswith('Or(') assert e.autos[0].endswith(") did not validate 'x'") assert e.autos[1] == "ve('x') raised ValueError()" assert len(e.autos) == 2 assert e.errors == ['should not raise', 'should not raise'] try: Or('o').validate('x') except SchemaError as e: assert e.autos == ["Or('o') did not validate 'x'", "'o' does not match 'x'"] assert e.errors == [None, None] try: Or('o', error='second error').validate('x') except SchemaError as e: assert e.autos == ["Or('o') did not validate 'x'", "'o' does not match 'x'"] assert e.errors == ['second error', 'second error'] def test_and_error_handling(): try: And(ve).validate('x') except SchemaError as e: assert e.autos == ["ve('x') raised ValueError()"] assert e.errors == [None] try: And(ve, error='should not raise').validate('x') except SchemaError as e: assert e.autos == ["ve('x') raised ValueError()"] assert e.errors == ['should not raise'] try: And(str, se).validate('x') except SchemaError as e: assert e.autos == [None, 'first auto'] assert e.errors == [None, 'first error'] try: And(str, se, error='second error').validate('x') except SchemaError as e: assert e.autos == [None, 'first auto'] assert e.errors == ['second error', 'first error'] def test_schema_error_handling(): try: Schema(Use(ve)).validate('x') except SchemaError as e: assert e.autos == [None, "ve('x') raised ValueError()"] assert e.errors == [None, None] try: Schema(Use(ve), error='should not raise').validate('x') except SchemaError as e: assert e.autos == [None, "ve('x') raised ValueError()"] assert e.errors == ['should not raise', None] try: Schema(Use(se)).validate('x') except SchemaError as e: assert e.autos == [None, None, 'first auto'] assert e.errors == [None, None, 'first error'] try: Schema(Use(se), error='second error').validate('x') except SchemaError as e: assert e.autos == [None, None, 'first auto'] assert e.errors == ['second error', None, 'first error'] def test_use_json(): import json gist_schema = Schema(And(Use(json.loads), # first convert from JSON {Optional('description'): basestring, 'public': bool, 'files': {basestring: {'content': basestring}}})) gist = '''{"description": "the description for this gist", "public": true, "files": { "file1.txt": {"content": "String file contents"}, "other.txt": {"content": "Another file contents"}}}''' assert gist_schema.validate(gist) def test_error_reporting(): s = Schema({'<files>': [Use(open, error='<files> should be readable')], '<path>': And(os.path.exists, error='<path> should exist'), '--count': Or(None, And(Use(int), lambda n: 0 < n < 5), error='--count should be integer 0 < n < 5')}, error='Error:') s.validate({'<files>': [], '<path>': './', '--count': 3}) try: s.validate({'<files>': [], '<path>': './', '--count': '10'}) except SchemaError as e: assert e.code == 'Error:\n--count should be integer 0 < n < 5' try: s.validate({'<files>': [], '<path>': './hai', '--count': '2'}) except SchemaError as e: assert e.code == 'Error:\n<path> should exist' try: s.validate({'<files>': ['hai'], '<path>': './', '--count': '2'}) except SchemaError as e: assert e.code == 'Error:\n<files> should be readable' def test_schema_repr(): # what about repr with `error`s? schema = Schema([Or(None, And(str, Use(float)))]) repr_ = "Schema([Or(None, And(<type 'str'>, Use(<type 'float'>)))])" # in Python 3 repr contains <class 'str'>, not <type 'str'> assert repr(schema).replace('class', 'type') == repr_ def test_validate_object(): schema = Schema({object: str}) assert schema.validate({42: 'str'}) == {42: 'str'} with SE: schema.validate({42: 777}) def test_issue_9_prioritized_key_comparison(): validate = Schema({'key': 42, object: 42}).validate assert validate({'key': 42, 777: 42}) == {'key': 42, 777: 42} def test_issue_9_prioritized_key_comparison_in_dicts(): # http://stackoverflow.com/questions/14588098/docopt-schema-validation s = Schema({'ID': Use(int, error='ID should be an int'), 'FILE': Or(None, Use(open, error='FILE should be readable')), Optional(str): object}) data = {'ID': 10, 'FILE': None, 'other': 'other', 'other2': 'other2'} assert s.validate(data) == data data = {'ID': 10, 'FILE': None} assert s.validate(data) == data def test_missing_keys_exception_with_non_str_dict_keys(): s = Schema({And(str, Use(str.lower), 'name'): And(str, len)}) with SE: s.validate(dict()) with SE: try: Schema({1: 'x'}).validate(dict()) except SchemaError as e: assert e.args[0] == "Missing keys: 1" raise def test_issue_56_cant_rely_on_callables_to_have_name(): s = Schema(methodcaller('endswith', '.csv')) assert s.validate('test.csv') == 'test.csv' with SE: try: s.validate('test.py') except SchemaError as e: assert "operator.methodcaller" in e.args[0] raise def test_exception_handling_with_bad_validators(): BadValidator = namedtuple("BadValidator", ["validate"]) s = Schema(BadValidator("haha")) with SE: try: s.validate("test") except SchemaError as e: assert "TypeError" in e.args[0] raise def test_issue_83_iterable_validation_return_type(): TestSetType = type("TestSetType", (set,), dict()) data = TestSetType(["test", "strings"]) s = Schema(set([str])) assert isinstance(s.validate(data), TestSetType) def test_optional_key_convert_failed_randomly_while_with_another_optional_object(): """ In this test, created_at string "2015-10-10 00:00:00" is expected to be converted to a datetime instance. - it works when the schema is s = Schema({ 'created_at': _datetime_validator, Optional(basestring): object, }) - but when wrapping the key 'created_at' with Optional, it fails randomly :return: """ import datetime fmt = '%Y-%m-%d %H:%M:%S' _datetime_validator = Or(None, Use(lambda i: datetime.datetime.strptime(i, fmt))) # FIXME given tests enough for i in range(1024): s = Schema({ Optional('created_at'): _datetime_validator, Optional('updated_at'): _datetime_validator, Optional('birth'): _datetime_validator, Optional(basestring): object, }) data = { 'created_at': '2015-10-10 00:00:00' } validated_data = s.validate(data) # is expected to be converted to a datetime instance, but fails randomly # (most of the time) assert isinstance(validated_data['created_at'], datetime.datetime) # assert isinstance(validated_data['created_at'], basestring) def test_json_schema(): assert JSONSchema(1, 1).validate().data == 1 assert JSONSchema(int, 1).validate().data == 1 assert JSONSchema(int, '1').validate().data is None assert JSONSchema(int, int).validate().data is None assert JSONSchema(str, 'hai').validate().data == 'hai' assert JSONSchema(str, 1).validate().data is None assert JSONSchema(Use(str), 1).validate().data == '1' assert JSONSchema(list, ['a', 1]).validate().data == ['a', 1] assert JSONSchema(dict, {'a': 1}).validate().data == {'a': 1} assert JSONSchema(dict, ['a', 1]).validate().data is None # TODO lambda # assert JSONSchema(lambda n: 0 < n < 5, 3).validate().data == 3 def test_json_schema_errors_with_int(): js = JSONSchema(int, 0.1).validate() assert js.data is None assert js.valid is False assert js.errors == '0.1 is not a valid int' js = JSONSchema(int, 'number').validate() assert js.data is None assert js.valid is False assert js.errors == 'number is not a valid int' def test_json_schema_errors_with_str(): js = JSONSchema(str, 1).validate() assert js.data is None assert js.valid is False assert js.errors == '1 is not a valid str' def test_json_schema_errors_with_bool(): js = JSONSchema(bool, 1).validate() assert js.data is None assert js.valid is False assert js.errors == '1 is not a valid bool' def test_json_schema_errors_with_dict(): js = JSONSchema(dict, 1).validate() assert js.data is None assert js.valid is False assert js.errors == '1 is not a valid dict' js = JSONSchema(dict, None).validate() assert js.data is None assert js.valid is False assert js.errors == 'None is not a valid dict' js = JSONSchema(dict, (0,)).validate() assert js.data is None assert js.valid is False assert js.errors == '(0,) is not a valid dict' js = JSONSchema(dict, dict).validate() assert js.data is None assert js.valid is False assert js.errors == "<type 'dict'> is not a valid json" js = JSONSchema(dict, object).validate() assert js.data is None assert js.valid is False assert js.errors == "<type 'object'> is not a valid json" js = JSONSchema(dict, {'1', '2'}).validate() assert js.data is None assert js.valid is False assert js.errors == "set(['1', '2']) is not a valid dict" js = JSONSchema({'name': str, 'age': lambda n: 18 <= n <= 99}, {'name': 'Sue', 'age': 100}).validate() assert js.data == {'name': 'Sue'} assert js.valid is False assert js.errors == {'age': '<lambda>(100) should evaluate to True'} # TODO object value # js = JSONSchema({str: int, int: None}, {'key1': 1, 'key2': 2, 10: None, 20: None}).validate() # assert js.data == {'key1': 1, 'key2': 2} # assert js.valid is False def test_json_schema_errors_with_list(): js = JSONSchema(list, 1).validate() assert js.data is None assert js.valid is False assert js.errors == '1 is not a valid list'
mit
-8,319,315,173,465,126,000
33.039711
99
0.561724
false
3.521569
true
false
false
arashn/senior-project
server/create_mission.py
1
2187
# This is a script to create a mission from a start # location to an end location and upload the mission # to the vehicle. The script uses the Google Maps # Directions API to obtain directions from the start # location to the end location, and uses the points # received as waypoints in the mission. import googlemaps from polyline.codec import PolylineCodec from dronekit import connect, VehicleMode, Command from pymavlink import mavutil gmaps = googlemaps.Client(key='AIzaSyBj8RNUHUSuk78N2Jim9yrMAKjWvh6gc_g') vehicle = connect('/dev/ttyUSB0', baud=57600, wait_ready=True) print "Drone is ready" def get_directions(start_location, end_location): directions_result = gmaps.directions(start_location, end_location, mode="walking") print directions_result print "Coordinates:" directions = [] start = directions_result[0]['legs'][0]['steps'][0]['start_location'] directions.append((start['lat'], start['lng'])) for step in directions_result[0]['legs'][0]['steps']: poly = PolylineCodec().decode(step['polyline']['points']) for point in poly: directions.append(point) end = step['end_location'] directions.append((end['lat'], end['lng'])) for x in directions: print x return directions def create_mission(directions): cmds = vehicle.commands cmds.clear() cmds.add(Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 0, 0, 0, 0, 0, 0, 0, 5)) for point in directions: lat = float(point[0]) lon = float(point[1]) cmds.add(Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 0, 1, 0, 0, 0, lat, lon, 5)) cmds.add(Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_LOITER_TIME, 0, 0, 10, 0, 0, 0, 0, 0, 5)) cmds.add(Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_LAND, 0, 0, 0, 0, 0, 0, 0, 0, 0)) cmds.upload() start_location = sys.argv[1] end_location = sys.argv[2] directions = get_directions(start_location, end_location) create_mission(directions)
gpl-3.0
8,425,306,509,460,994,000
39.5
150
0.692273
false
3.169565
false
false
false
j08lue/poppy
poppy/ts_flux_budget.py
1
6682
import numpy as np import warnings from oceanpy.fluxbudget import budget_over_region_2D from oceanpy.stats import central_differences def _fill0(a): return np.ma.filled(a,0.) def _warn_virtual_salt_flux_units(): warnings.warn('Output units are kg SALT s-1!',) warnings.filterwarnings("once") def fluxbudget_VVEL(ds,mask,varn,kza=0,kzo=None,S0=34.8,t=0): """Integrate horizontal flux using VVEL*SCALAR""" _warn_virtual_salt_flux_units() dsvar = ds.variables dxu = dsvar['DXU'][:] * 1e-2 dyu = dsvar['DYU'][:] * 1e-2 dz = dsvar['dz'][:] * 1e-2 if kzo is None: kzo = len(dz) fluxbudget = 0. for k in range(kza,kzo): uflux = _fill0(dsvar['UVEL'][t,k]) * 1e-2 uflux *= dyu uflux *= dz[k] vflux = _fill0(dsvar['VVEL'][t,k]) * 1e-2 vflux *= dxu vflux *= dz[k] if not varn: scalar = None elif varn == 'heat': scalar = _fill0(dsvar['TEMP'][t,k]) elif varn == 'salt': scalar = _fill0(dsvar['SALT'][t,k]) elif varn == 'freshwater': scalar = (S0 - _fill0(dsvar['SALT'][t,k])) / S0 fluxbudget += budget_over_region_2D(uflux,vflux,scalar=scalar,mask=mask,grid='ArakawaB') if varn == 'heat': fluxbudget *= (1e3 * 4e3 * 1e-15) # PW return fluxbudget def fluxbudget_UESVNS(ds,mask,varn='salt',kza=0,kzo=None,t=0): """Integrate horizontal flux using UES and VNS variables""" _warn_virtual_salt_flux_units() dsvar = ds.variables dz = dsvar['dz'][:] * 1e-2 tarea = dsvar['UAREA'][:] * 1e-4 if kzo is None: kzo = len(dz) fluxbudget = 0. for k in range(kza,kzo): uflux = _fill0(dsvar['UES'][t,k]) uflux *= tarea uflux *= dz[k] vflux = _fill0(dsvar['VNS'][t,k]) vflux *= tarea vflux *= dz[k] fluxbudget += budget_over_region_2D(uflux,vflux,scalar=None,mask=mask) return fluxbudget def fluxbudget_bolus_visop(ds,mask,varn,kza=0,kzo=None,S0=34.8,t=0): """Compute flux of `varn` into region `mask` due to eddy (bolus) velocity""" _warn_virtual_salt_flux_units() dsvar = ds.variables dxt = dsvar['DXT'][:] * 1e-2 dyt = dsvar['DYT'][:] * 1e-2 dz = dsvar['dz'][:] * 1e-2 if kzo is None: kzo = len(dz) fluxbudget = 0. for k in range(kza,kzo): # get bolus velocity uflux = _fill0(dsvar['UISOP'][t,k]) * 1e-2 # m s-1 vflux = _fill0(dsvar['VISOP'][t,k]) * 1e-2 # m s-1 # get scalar data if varn == 'heat': scalar = _fill0(dsvar['TEMP'][t,k]) elif varn == 'salt': scalar = _fill0(dsvar['SALT'][t,k]) elif varn == 'freshwater': scalar = (S0 - _fill0(dsvar['SALT'][t,k])) / S0 # multiply flux by scalar uflux *= scalar vflux *= scalar # multiply by horizontal grid spacing uflux *= dyt vflux *= dxt # multiply by vertical grid spacing uflux *= dz[k] vflux *= dz[k] # compute budget fluxbudget += budget_over_region_2D(uflux,vflux,scalar=None,mask=mask) if varn == 'heat': fluxbudget *= (1e3 * 4e3 * 1e-15) # PW return fluxbudget fluxbudget_bolus = fluxbudget_bolus_visop def fluxbudget_diffusion(ds,mask,varn,kza=0,kzo=None,S0=34.8,t=0): """Compute flux of `varn` into region `mask` due to diffusion""" _warn_virtual_salt_flux_units() dsvar = ds.variables dxt = dsvar['DXT'][:] * 1e-2 dyt = dsvar['DYT'][:] * 1e-2 dz = dsvar['dz'][:] * 1e-2 if kzo is None: kzo = len(dz) fluxbudget = 0. for k in range(kza,kzo): # get scalar data if varn == 'heat': scalar = _fill0(dsvar['TEMP'][t,k]) elif varn == 'salt': scalar = _fill0(dsvar['SALT'][t,k]) elif varn == 'freshwater': scalar = (S0 - _fill0(dsvar['SALT'][t,k])) / S0 # get gradient uflux = central_differences(scalar,dxt,axis=1) # [scalar] m-1 vflux = central_differences(scalar,dyt,axis=0) # [scalar] m-1 # multiply gradient by diffusion coefficient kappa = _fill0(dsvar['KAPPA_ISOP'][t,k] * 1e-4) # m2 s-1 uflux *= kappa vflux *= kappa # multiply by horizontal grid spacing uflux *= dyt vflux *= dxt # multiply by vertical grid spacing uflux *= dz[k] vflux *= dz[k] # compute budget fluxbudget += budget_over_region_2D(uflux,vflux,scalar=None,mask=mask) # convert to right units if varn == 'heat': fluxbudget *= (1e3 * 4e3 * 1e-15) # PW return fluxbudget def fluxbudget_bolus_advection_tendency(ds,mask,varn,t=0): _warn_virtual_salt_flux_units() dsvar = ds.variables if varn == 'heat': integrand = _fill0(dsvar['ADVT_ISOP'][t][mask]) * 1e-2 elif varn == 'salt': integrand = _fill0(dsvar['ADVS_ISOP'][t][mask]) * 1e-2 else: raise ValueError('This function only works for heat and salt transport.') integrand *= dsvar['TAREA'][:][mask] * 1e-4 integral = np.sum(integrand) return integral def transport_divergence(ds,mask,varn='salt',kza=0,kzo=None,t=0): _warn_virtual_salt_flux_units() if varn == 'heat': uvar,vvar = 'UET','VNT' elif varn == 'salt': uvar,vvar = 'UES','VNS' dsvar = ds.variables dxu = dsvar['DXU'][:] * 1e-2 dyu = dsvar['DYU'][:] * 1e-2 tarea = dsvar['TAREA'][:] * 1e-4 dz = dsvar['dz'][:] * 1e-2 if kzo is None: kzo = len(dz) transport_divergence = 0. for k in range(kza,kzo): uflux = _fill0(dsvar[uvar][t,k]) uflux *= dyu uflux *= dz[k] uflux *= mask vflux = _fill0(dsvar[vvar][t,k]) vflux *= dxu vflux *= dz[k] vflux *= mask divergence = central_differences(uflux,dxu,axis=1) + central_differences(vflux,dyu,axis=0) divergence *= mask transport_divergence += np.sum(divergence*tarea) if varn=='heat': warnings.warn('Units might be wrong for heat transport! Check!') return transport_divergence def transport_divergence_from_vertical(ds,mask,varn='salt',kza=0,kzo=None,t=0): _warn_virtual_salt_flux_units() if varn == 'heat': wvar = 'WTT' elif varn == 'salt': wvar = 'WTS' dsvar = ds.variables dz = dsvar['dz'][:] * 1e-2 if kzo is None: kzo = len(dz) transport_divergence = 0. for k in range(kza,kzo): wflux = _fill0(dsvar[wvar][t,k][mask]) wflux *= dz[k] wflux *= dsvar['TAREA'][:][mask] * 1e-4 transport_divergence += np.sum(wflux) return transport_divergence
gpl-2.0
-7,507,726,245,566,721,000
32.41
98
0.564801
false
2.856776
false
false
false
tkem/mopidy-podcast-itunes
mopidy_podcast_itunes/__init__.py
1
4677
import pathlib import pkg_resources from mopidy import config, ext, httpclient __version__ = pkg_resources.get_distribution("Mopidy-Podcast").version CHARTS = ["podcasts", "audioPodcasts", "videoPodcasts"] COUNTRIES = [ "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "ZA", "ZM", "ZW", ] EXPLICIT = ("Yes", "No") # since config.Boolean has no "optional" MAX_LIMIT = 200 # absolute limit specified by iTunes Store API class Extension(ext.Extension): dist_name = "Mopidy-Podcast-iTunes" ext_name = "podcast-itunes" version = __version__ def get_default_config(self): return config.read(pathlib.Path(__file__).parent / "ext.conf") def get_config_schema(self): schema = super().get_config_schema() schema.update( base_url=config.String(), country=config.String(choices=COUNTRIES), explicit=config.String(choices=EXPLICIT, optional=True), charts=config.String(choices=CHARTS), charts_limit=config.Integer( minimum=1, maximum=MAX_LIMIT, optional=True ), search_limit=config.Integer( minimum=1, maximum=MAX_LIMIT, optional=True ), timeout=config.Integer(minimum=1, optional=True), retries=config.Integer(minimum=0), # no longer used charts_format=config.Deprecated(), episode_format=config.Deprecated(), genre_format=config.Deprecated(), podcast_format=config.Deprecated(), root_genre_id=config.Deprecated(), root_name=config.Deprecated(), ) return schema def setup(self, registry): from .backend import iTunesPodcastBackend registry.add("backend", iTunesPodcastBackend) @classmethod def get_requests_session(cls, config): import requests proxy = httpclient.format_proxy(config["proxy"]) user_agent_string = f"{cls.dist_name}/{cls.version}" user_agent = httpclient.format_user_agent(user_agent_string) session = requests.Session() session.proxies.update({"http": proxy, "https": proxy}) session.headers.update({"user-agent": user_agent}) return session
apache-2.0
-5,865,417,700,428,691,000
13.707547
70
0.39876
false
2.963878
true
false
false
FlightGear/flightgear
utils/Modeller/yasim_import.py
1
30696
#!BPY # """ # Name: 'YASim (.xml)' # Blender: 245 # Group: 'Import' # Tooltip: 'Loads and visualizes a YASim FDM geometry' # """ __author__ = "Melchior FRANZ < mfranz # aon : at >" __url__ = ["http://www.flightgear.org/", "http://cvs.flightgear.org/viewvc/source/utils/Modeller/yasim_import.py"] __version__ = "0.2" __bpydoc__ = """\ yasim_import.py loads and visualizes a YASim FDM geometry ========================================================= It is recommended to load the model superimposed over a greyed out and immutable copy of the aircraft model: (0) put this script into ~/.blender/scripts/ (1) load or import aircraft model (menu -> "File" -> "Import" -> "AC3D (.ac) ...") (2) create new *empty* scene (menu -> arrow button left of "SCE:scene1" combobox -> "ADD NEW" -> "empty") (3) rename scene to yasim (not required) (4) link to scene1 (F10 -> "Output" tab in "Buttons Window" -> arrow button left of text entry "No Set Scene" -> "scene1") (5) now load the YASim config file (menu -> "File" -> "Import" -> "YASim (.xml) ...") This is good enough for simple checks. But if you are working on the YASim configuration, then you need a quick and convenient way to reload the file. In that case continue after (4): (5) switch the button area at the bottom of the blender screen to "Scripts Window" mode (green python snake icon) (6) load the YASim config file (menu -> "Scripts" -> "Import" -> "YASim (.xml) ...") (7) make the "Scripts Window" area as small as possible by dragging the area separator down (8) optionally split the "3D View" area and switch the right part to the "Outliner" (9) press the "Reload YASim" button in the script area to reload the file If the 3D model is displaced with respect to the FDM model, then the <offsets> values from the model animation XML file should be added as comment to the YASim config file, as a line all by itself, with no spaces surrounding the equal signs. Spaces elsewhere are allowed. For example: <offsets> <x-m>3.45</x-m> <z-m>-0.4</z-m> <pitch-deg>5</pitch-deg> </offsets> becomes: <!-- offsets: x=3.45 z=-0.4 p=5 --> Possible variables are: x ... <x-m> y ... <y-m> z ... <z-m> h ... <heading-deg> p ... <pitch-deg> r ... <roll-deg> Of course, absolute FDM coordinates can then no longer directly be read from Blender's 3D view. The cursor coordinates display in the script area, however, shows the coordinates in YASim space. Note that object names don't contain XML indices but element numbers. YASim_flap0#2 is the third flap0 in the whole file, not necessarily in its parent XML group. A floating point part in the object name (e.g. YASim_flap0#2.004) only means that the geometry has been reloaded that often. It's an unavoidable consequence of how Blender deals with meshes. Elements are displayed as follows: cockpit -> monkey head fuselage -> blue "tube" (with only 12 sides for less clutter); center at "a" vstab -> red with yellow control surfaces (flap0, flap1, slat, spoiler) wing/mstab/hstab -> green with yellow control surfaces (which are always 20 cm deep); symmetric surfaces are only displayed on the left side, unless the "Mirror" button is active thrusters (jet/propeller/thruster) -> dashed line from center to actionpt; arrow from actionpt along thrust vector (always 1 m long); propeller circle rotor -> radius and rel_len_blade_start circle, normal and forward vector, one blade at phi0 with direction arrow near blade tip gear -> contact point and compression vector (no arrow head) tank -> magenta cube (10 cm side length) weight -> inverted cyan cone ballast -> yellow cylinder hitch -> hexagon (10 cm diameter) hook -> dashed line for up angle, T-line for down angle launchbar -> dashed line for up angles, T-line for down angles (launchbar and holdback each) The Mirror button complements symmetrical surfaces (wing/hstab/mstab) and control surfaces (flap0/flap1/slat/spoiler). This is useful for asymmetrical aircraft, but has the disadvantage that it moves the surfaces' object centers from their usual place, yasim's [x, y, z] value, to [0, 0, 0]. Turning mirroring off restores the object center. Environment variable BLENDER_YASIM_IMPORT can be set to a space-separated list of options: $ BLENDER_YASIM_IMPORT="mirror verbose" blender whereby: verbose ... enables verbose logs mirror ... enables mirroring of symmetric surfaces """ #-------------------------------------------------------------------------------- # Copyright (C) 2009 Melchior FRANZ < mfranz # aon : at > # # 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 2 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, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #-------------------------------------------------------------------------------- import Blender, BPyMessages, string, math, os from Blender.Mathutils import * from xml.sax import handler, make_parser CONFIG = string.split(os.getenv("BLENDER_YASIM_IMPORT") or "") YASIM_MATRIX = Matrix([-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]) ORIGIN = Vector(0, 0, 0) X = Vector(1, 0, 0) Y = Vector(0, 1, 0) Z = Vector(0, 0, 1) DEG2RAD = math.pi / 180 RAD2DEG = 180 / math.pi NO_EVENT = 0 RELOAD_BUTTON = 1 CURSOR_BUTTON = 2 MIRROR_BUTTON = 3 class Global: verbose = "verbose" in CONFIG path = "" matrix = None data = None cursor = ORIGIN last_cursor = Vector(Blender.Window.GetCursorPos()) mirror_button = Blender.Draw.Create("mirror" in CONFIG) class Abort(Exception): def __init__(self, msg, term = None): self.msg = msg self.term = term def log(msg): if Global.verbose: print(msg) def draw_dashed_line(mesh, start, end): w = 0.04 step = w * (end - start).normalize() n = len(mesh.verts) for i in range(int(1 + 0.5 * (end - start).length / w)): a = start + 2 * i * step b = a + step if (b - end).length < step.length: b = end mesh.verts.extend([a, b]) mesh.edges.extend([n + 2 * i, n + 2 * i + 1]) def draw_arrow(mesh, start, end): v = end - start m = v.toTrackQuat('x', 'z').toMatrix().resize4x4() * TranslationMatrix(start) v = v.length * X n = len(mesh.verts) mesh.verts.extend([ORIGIN * m , v * m, (v - 0.05 * X + 0.05 * Y) * m, (v - 0.05 * X - 0.05 * Y) * m]) # head mesh.verts.extend([(ORIGIN + 0.05 * Y) * m, (ORIGIN - 0.05 * Y) * m]) # base mesh.edges.extend([[n, n + 1], [n + 1, n + 2], [n + 1, n + 3], [n + 4, n + 5]]) def draw_circle(mesh, numpoints, radius, matrix): n = len(mesh.verts) for i in range(numpoints): angle = 2.0 * math.pi * i / numpoints v = Vector(radius * math.cos(angle), radius * math.sin(angle), 0) mesh.verts.extend([v * matrix]) for i in range(numpoints): i1 = (i + 1) % numpoints mesh.edges.extend([[n + i, n + i1]]) class Item: scene = Blender.Scene.GetCurrent() def make_twosided(self, mesh): mesh.faceUV = True for f in mesh.faces: f.mode |= Blender.Mesh.FaceModes.TWOSIDE | Blender.Mesh.FaceModes.OBCOL def set_color(self, obj, color): mat = Blender.Material.New() mat.setRGBCol(color[0], color[1], color[2]) mat.setAlpha(color[3]) mat.mode |= Blender.Material.Modes.ZTRANSP | Blender.Material.Modes.TRANSPSHADOW obj.transp = True mesh = obj.getData(mesh = True) mesh.materials += [mat] for f in mesh.faces: f.smooth = True mesh.calcNormals() class Cockpit(Item): def __init__(self, center): mesh = Blender.Mesh.Primitives.Monkey() mesh.transform(ScaleMatrix(0.13, 4) * Euler(90, 0, 90).toMatrix().resize4x4() * TranslationMatrix(Vector(-0.1, 0, -0.032))) obj = self.scene.objects.new(mesh, "YASim_cockpit") obj.setMatrix(TranslationMatrix(center) * Global.matrix) class Tank(Item): def __init__(self, name, center): mesh = Blender.Mesh.Primitives.Cube() mesh.transform(ScaleMatrix(0.05, 4)) obj = self.scene.objects.new(mesh, name) obj.setMatrix(TranslationMatrix(center) * Global.matrix) self.set_color(obj, [1, 0, 1, 0.5]) class Ballast(Item): def __init__(self, name, center): mesh = Blender.Mesh.Primitives.Cylinder() mesh.transform(ScaleMatrix(0.05, 4)) obj = self.scene.objects.new(mesh, name) obj.setMatrix(TranslationMatrix(center) * Global.matrix) self.set_color(obj, [1, 1, 0, 0.5]) class Weight(Item): def __init__(self, name, center): mesh = Blender.Mesh.Primitives.Cone() mesh.transform(ScaleMatrix(0.05, 4)) obj = self.scene.objects.new(mesh, name) obj.setMatrix(TranslationMatrix(center) * Global.matrix) self.set_color(obj, [0, 1, 1, 0.5]) class Gear(Item): def __init__(self, name, center, compression): mesh = Blender.Mesh.New() mesh.verts.extend([ORIGIN, compression]) mesh.edges.extend([0, 1]) obj = self.scene.objects.new(mesh, name) obj.setMatrix(TranslationMatrix(center) * Global.matrix) class Hook(Item): def __init__(self, name, center, length, up_angle, dn_angle): mesh = Blender.Mesh.New() up = ORIGIN - length * math.cos(up_angle * DEG2RAD) * X - length * math.sin(up_angle * DEG2RAD) * Z dn = ORIGIN - length * math.cos(dn_angle * DEG2RAD) * X - length * math.sin(dn_angle * DEG2RAD) * Z mesh.verts.extend([ORIGIN, dn, dn + 0.05 * Y, dn - 0.05 * Y]) mesh.edges.extend([[0, 1], [2, 3]]) draw_dashed_line(mesh, ORIGIN, up) draw_dashed_line(mesh, ORIGIN, dn) obj = self.scene.objects.new(mesh, name) obj.setMatrix(TranslationMatrix(center) * Global.matrix) class Launchbar(Item): def __init__(self, name, lb, lb_length, hb, hb_length, up_angle, dn_angle): mesh = Blender.Mesh.New() hb = hb - lb lb_tip = ORIGIN + lb_length * math.cos(dn_angle * DEG2RAD) * X - lb_length * math.sin(dn_angle * DEG2RAD) * Z hb_tip = hb - hb_length * math.cos(dn_angle * DEG2RAD) * X - hb_length * math.sin(dn_angle * DEG2RAD) * Z mesh.verts.extend([lb_tip, ORIGIN, hb, hb_tip, lb_tip + 0.05 * Y, lb_tip - 0.05 * Y, hb_tip + 0.05 * Y, hb_tip - 0.05 * Y]) mesh.edges.extend([[0, 1], [1, 2], [2, 3], [4, 5], [6, 7]]) draw_dashed_line(mesh, ORIGIN, lb_length * math.cos(up_angle * DEG2RAD) * X - lb_length * math.sin(up_angle * DEG2RAD) * Z) draw_dashed_line(mesh, hb, hb - hb_length * math.cos(up_angle * DEG2RAD) * X - hb_length * math.sin(up_angle * DEG2RAD) * Z) obj = self.scene.objects.new(mesh, name) obj.setMatrix(TranslationMatrix(lb) * Global.matrix) class Hitch(Item): def __init__(self, name, center): mesh = Blender.Mesh.Primitives.Circle(6, 0.1) obj = self.scene.objects.new(mesh, name) obj.setMatrix(RotationMatrix(90, 4, "x") * TranslationMatrix(center) * Global.matrix) class Thrust: def set_actionpt(self, p): self.actionpt = p def set_dir(self, d): self.thrustvector = d class Thruster(Thrust, Item): def __init__(self, name, center, thrustvector): (self.name, self.center, self.actionpt, self.thrustvector) = (name, center, center, thrustvector) def __del__(self): a = self.actionpt - self.center mesh = Blender.Mesh.New() draw_dashed_line(mesh, ORIGIN, a) draw_arrow(mesh, a, a + self.thrustvector.normalize()) obj = self.scene.objects.new(mesh, self.name) obj.setMatrix(TranslationMatrix(self.center) * Global.matrix) class Propeller(Thrust, Item): def __init__(self, name, center, radius): (self.name, self.center, self.radius, self.actionpt, self.thrustvector) = (name, center, radius, center, -X) def __del__(self): a = self.actionpt - self.center matrix = self.thrustvector.toTrackQuat('z', 'x').toMatrix().resize4x4() * TranslationMatrix(a) mesh = Blender.Mesh.New() mesh.verts.extend([ORIGIN * matrix, (ORIGIN + self.radius * X) * matrix]) mesh.edges.extend([[0, 1]]) draw_dashed_line(mesh, ORIGIN, a) draw_arrow(mesh, a, a + self.thrustvector.normalize()) draw_circle(mesh, 128, self.radius, matrix) obj = self.scene.objects.new(mesh, self.name) obj.setMatrix(TranslationMatrix(self.center) * Global.matrix) class Jet(Thrust, Item): def __init__(self, name, center, rotate): (self.name, self.center, self.actionpt) = (name, center, center) self.thrustvector = -X * RotationMatrix(rotate, 4, "y") def __del__(self): a = self.actionpt - self.center mesh = Blender.Mesh.New() draw_dashed_line(mesh, ORIGIN, a) draw_arrow(mesh, a, a + self.thrustvector.normalize()) obj = self.scene.objects.new(mesh, self.name) obj.setMatrix(TranslationMatrix(self.center) * Global.matrix) class Fuselage(Item): def __init__(self, name, a, b, width, taper, midpoint): numvert = 12 angle = [] for i in range(numvert): alpha = i * 2 * math.pi / float(numvert) angle.append([math.cos(alpha), math.sin(alpha)]) axis = b - a length = axis.length mesh = Blender.Mesh.New() for i in range(numvert): mesh.verts.extend([[0, 0.5 * width * taper * angle[i][0], 0.5 * width * taper * angle[i][1]]]) for i in range(numvert): mesh.verts.extend([[midpoint * length, 0.5 * width * angle[i][0], 0.5 * width * angle[i][1]]]) for i in range(numvert): mesh.verts.extend([[length, 0.5 * width * taper * angle[i][0], 0.5 * width * taper * angle[i][1]]]) for i in range(numvert): i1 = (i + 1) % numvert mesh.faces.extend([[i, i1, i1 + numvert, i + numvert]]) mesh.faces.extend([[i + numvert, i1 + numvert, i1 + 2 * numvert, i + 2 * numvert]]) mesh.verts.extend([ORIGIN, length * X]) obj = self.scene.objects.new(mesh, name) obj.setMatrix(axis.toTrackQuat('x', 'y').toMatrix().resize4x4() * TranslationMatrix(a) * Global.matrix) self.set_color(obj, [0, 0, 0.5, 0.4]) class Rotor(Item): def __init__(self, name, center, up, fwd, numblades, radius, chord, twist, taper, rel_len_blade_start, phi0, ccw): matrix = RotationMatrix(phi0, 4, "z") * up.toTrackQuat('z', 'x').toMatrix().resize4x4() invert = matrix.copy().invert() direction = [-1, 1][ccw] twist *= DEG2RAD a = ORIGIN + rel_len_blade_start * radius * X b = ORIGIN + radius * X tw = 0.5 * chord * taper * math.cos(twist) * Y + 0.5 * direction * chord * taper * math.sin(twist) * Z mesh = Blender.Mesh.New() mesh.verts.extend([ORIGIN, a, b, a + 0.5 * chord * Y, a - 0.5 * chord * Y, b + tw, b - tw]) mesh.edges.extend([[0, 1], [1, 2], [1, 3], [1, 4], [3, 5], [4, 6], [5, 6]]) draw_circle(mesh, 64, rel_len_blade_start * radius, Matrix()) draw_circle(mesh, 128, radius, Matrix()) draw_arrow(mesh, ORIGIN, up * invert) draw_arrow(mesh, ORIGIN, fwd * invert) b += 0.1 * X + direction * chord * Y draw_arrow(mesh, b, b + min(0.5 * radius, 1) * direction * Y) obj = self.scene.objects.new(mesh, name) obj.setMatrix(matrix * TranslationMatrix(center) * Global.matrix) class Wing(Item): def __init__(self, name, root, length, chord, incidence, twist, taper, sweep, dihedral): # <1--0--2 # \ | / # 4-3-5 self.is_symmetric = not name.startswith("YASim_vstab#") mesh = Blender.Mesh.New() mesh.verts.extend([ORIGIN, ORIGIN + 0.5 * chord * X, ORIGIN - 0.5 * chord * X]) tip = ORIGIN + math.cos(sweep * DEG2RAD) * length * Y - math.sin(sweep * DEG2RAD) * length * X tipfore = tip + 0.5 * taper * chord * math.cos(twist * DEG2RAD) * X + 0.5 * taper * chord * math.sin(twist * DEG2RAD) * Z tipaft = tip + tip - tipfore mesh.verts.extend([tip, tipfore, tipaft]) mesh.faces.extend([[0, 1, 4, 3], [2, 0, 3, 5]]) self.make_twosided(mesh) obj = self.scene.objects.new(mesh, name) mesh.transform(Euler(dihedral, -incidence, 0).toMatrix().resize4x4()) self.set_color(obj, [[0.5, 0.0, 0, 0.5], [0.0, 0.5, 0, 0.5]][self.is_symmetric]) (self.obj, self.mesh) = (obj, mesh) if self.is_symmetric and Global.mirror_button.val: mod = obj.modifiers.append(Blender.Modifier.Type.MIRROR) mod[Blender.Modifier.Settings.AXIS_X] = False mod[Blender.Modifier.Settings.AXIS_Y] = True mod[Blender.Modifier.Settings.AXIS_Z] = False mesh.transform(TranslationMatrix(root)) # must move object center to x axis obj.setMatrix(Global.matrix) else: obj.setMatrix(TranslationMatrix(root) * Global.matrix) def add_flap(self, name, start, end): a = Vector(self.mesh.verts[2].co) b = Vector(self.mesh.verts[5].co) c = 0.2 * (Vector(self.mesh.verts[0].co - a)).normalize() m = self.obj.getMatrix() mesh = Blender.Mesh.New() i0 = a + start * (b - a) i1 = a + end * (b - a) mesh.verts.extend([i0, i1, i0 + c, i1 + c]) mesh.faces.extend([[0, 1, 3, 2]]) self.make_twosided(mesh) obj = self.scene.objects.new(mesh, name) obj.setMatrix(m) self.set_color(obj, [0.8, 0.8, 0, 0.9]) if self.is_symmetric and Global.mirror_button.val: mod = obj.modifiers.append(Blender.Modifier.Type.MIRROR) mod[Blender.Modifier.Settings.AXIS_X] = False mod[Blender.Modifier.Settings.AXIS_Y] = True mod[Blender.Modifier.Settings.AXIS_Z] = False class import_yasim(handler.ErrorHandler, handler.ContentHandler): ignored = ["cruise", "approach", "control-input", "control-output", "control-speed", \ "control-setting", "stall", "airplane", "piston-engine", "turbine-engine", \ "rotorgear", "tow", "winch", "solve-weight"] # err_handler def warning(self, exception): print((self.error_string("Warning", exception))) def error(self, exception): print((self.error_string("Error", exception))) def fatalError(self, exception): raise Abort(str(exception), self.error_string("Fatal", exception)) def error_string(self, tag, e): (column, line) = (e.getColumnNumber(), e.getLineNumber()) return "%s: %s\n%s%s^" % (tag, str(e), Global.data[line - 1], column * ' ') # doc_handler def setDocumentLocator(self, locator): self.locator = locator def startDocument(self): self.tags = [] self.counter = {} self.items = [None] def endDocument(self): for o in Item.scene.objects: o.sel = True def startElement(self, tag, attrs): if len(self.tags) == 0 and tag != "airplane": raise Abort("this isn't a YASim config file (bad root tag at line %d)" % self.locator.getLineNumber()) self.tags.append(tag) path = string.join(self.tags, '/') item = Item() parent = self.items[-1] if self.counter.has_key(tag): self.counter[tag] += 1 else: self.counter[tag] = 0 if tag == "cockpit": c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) log("\033[31mcockpit x=%f y=%f z=%f\033[m" % (c[0], c[1], c[2])) item = Cockpit(c) elif tag == "fuselage": a = Vector(float(attrs["ax"]), float(attrs["ay"]), float(attrs["az"])) b = Vector(float(attrs["bx"]), float(attrs["by"]), float(attrs["bz"])) width = float(attrs["width"]) taper = float(attrs.get("taper", 1)) midpoint = float(attrs.get("midpoint", 0.5)) log("\033[32mfuselage ax=%f ay=%f az=%f bx=%f by=%f bz=%f width=%f taper=%f midpoint=%f\033[m" % \ (a[0], a[1], a[2], b[0], b[1], b[2], width, taper, midpoint)) item = Fuselage("YASim_%s#%d" % (tag, self.counter[tag]), a, b, width, taper, midpoint) elif tag == "gear": c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) compression = float(attrs.get("compression", 1)) up = Z * compression if attrs.has_key("upx"): up = Vector(float(attrs["upx"]), float(attrs["upy"]), float(attrs["upz"])).normalize() * compression log("\033[35;1mgear x=%f y=%f z=%f compression=%f upx=%f upy=%f upz=%f\033[m" \ % (c[0], c[1], c[2], compression, up[0], up[1], up[2])) item = Gear("YASim_gear#%d" % self.counter[tag], c, up) elif tag == "jet": c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) rotate = float(attrs.get("rotate", 0)) log("\033[36;1mjet x=%f y=%f z=%f rotate=%f\033[m" % (c[0], c[1], c[2], rotate)) item = Jet("YASim_jet#%d" % self.counter[tag], c, rotate) elif tag == "propeller": c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) radius = float(attrs["radius"]) log("\033[36;1m%s x=%f y=%f z=%f radius=%f\033[m" % (tag, c[0], c[1], c[2], radius)) item = Propeller("YASim_propeller#%d" % self.counter[tag], c, radius) elif tag == "thruster": c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) v = Vector(float(attrs["vx"]), float(attrs["vy"]), float(attrs["vz"])) log("\033[36;1m%s x=%f y=%f z=%f vx=%f vy=%f vz=%f\033[m" % (tag, c[0], c[1], c[2], v[0], v[1], v[2])) item = Thruster("YASim_thruster#%d" % self.counter[tag], c, v) elif tag == "actionpt": if not isinstance(parent, Thrust): raise Abort("%s is not part of a thruster/propeller/jet at line %d" \ % (path, self.locator.getLineNumber())) c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) log("\t\033[36mactionpt x=%f y=%f z=%f\033[m" % (c[0], c[1], c[2])) parent.set_actionpt(c) elif tag == "dir": if not isinstance(parent, Thrust): raise Abort("%s is not part of a thruster/propeller/jet at line %d" \ % (path, self.locator.getLineNumber())) c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) log("\t\033[36mdir x=%f y=%f z=%f\033[m" % (c[0], c[1], c[2])) parent.set_dir(c) elif tag == "tank": c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) log("\033[34;1m%s x=%f y=%f z=%f\033[m" % (tag, c[0], c[1], c[2])) item = Tank("YASim_tank#%d" % self.counter[tag], c) elif tag == "ballast": c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) log("\033[34m%s x=%f y=%f z=%f\033[m" % (tag, c[0], c[1], c[2])) item = Ballast("YASim_ballast#%d" % self.counter[tag], c) elif tag == "weight": c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) log("\033[34m%s x=%f y=%f z=%f\033[m" % (tag, c[0], c[1], c[2])) item = Weight("YASim_weight#%d" % self.counter[tag], c) elif tag == "hook": c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) length = float(attrs.get("length", 1)) up_angle = float(attrs.get("up-angle", 0)) down_angle = float(attrs.get("down-angle", 70)) log("\033[35m%s x=%f y=%f z=%f length=%f up-angle=%f down-angle=%f\033[m" \ % (tag, c[0], c[1], c[2], length, up_angle, down_angle)) item = Hook("YASim_hook#%d" % self.counter[tag], c, length, up_angle, down_angle) elif tag == "hitch": c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) log("\033[35m%s x=%f y=%f z=%f\033[m" % (tag, c[0], c[1], c[2])) item = Hitch("YASim_hitch#%d" % self.counter[tag], c) elif tag == "launchbar": c = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) length = float(attrs.get("length", 1)) up_angle = float(attrs.get("up-angle", -45)) down_angle = float(attrs.get("down-angle", 45)) holdback = Vector(float(attrs.get("holdback-x", c[0])), float(attrs.get("holdback-y", c[1])), float(attrs.get("holdback-z", c[2]))) holdback_length = float(attrs.get("holdback-length", 2)) log("\033[35m%s x=%f y=%f z=%f length=%f down-angle=%f up-angle=%f holdback-x=%f holdback-y=%f holdback-z+%f holdback-length=%f\033[m" \ % (tag, c[0], c[1], c[2], length, down_angle, up_angle, \ holdback[0], holdback[1], holdback[2], holdback_length)) item = Launchbar("YASim_launchbar#%d" % self.counter[tag], c, length, holdback, holdback_length, up_angle, down_angle) elif tag == "wing" or tag == "hstab" or tag == "vstab" or tag == "mstab": root = Vector(float(attrs["x"]), float(attrs["y"]), float(attrs["z"])) length = float(attrs["length"]) chord = float(attrs["chord"]) incidence = float(attrs.get("incidence", 0)) twist = float(attrs.get("twist", 0)) taper = float(attrs.get("taper", 1)) sweep = float(attrs.get("sweep", 0)) dihedral = float(attrs.get("dihedral", [0, 90][tag == "vstab"])) log("\033[33;1m%s x=%f y=%f z=%f length=%f chord=%f incidence=%f twist=%f taper=%f sweep=%f dihedral=%f\033[m" \ % (tag, root[0], root[1], root[2], length, chord, incidence, twist, taper, sweep, dihedral)) item = Wing("YASim_%s#%d" % (tag, self.counter[tag]), root, length, chord, incidence, twist, taper, sweep, dihedral) elif tag == "flap0" or tag == "flap1" or tag == "slat" or tag == "spoiler": if not isinstance(parent, Wing): raise Abort("%s is not part of a wing or stab at line %d" \ % (path, self.locator.getLineNumber())) start = float(attrs["start"]) end = float(attrs["end"]) log("\t\033[33m%s start=%f end=%f\033[m" % (tag, start, end)) parent.add_flap("YASim_%s#%d" % (tag, self.counter[tag]), start, end) elif tag == "rotor": c = Vector(float(attrs.get("x", 0)), float(attrs.get("y", 0)), float(attrs.get("z", 0))) norm = Vector(float(attrs.get("nx", 0)), float(attrs.get("ny", 0)), float(attrs.get("nz", 1))) fwd = Vector(float(attrs.get("fx", 1)), float(attrs.get("fy", 0)), float(attrs.get("fz", 0))) diameter = float(attrs.get("diameter", 10.2)) numblades = int(attrs.get("numblades", 4)) chord = float(attrs.get("chord", 0.3)) twist = float(attrs.get("twist", 0)) taper = float(attrs.get("taper", 1)) rel_len_blade_start = float(attrs.get("rel-len-blade-start", 0)) phi0 = float(attrs.get("phi0", 0)) ccw = not not int(attrs.get("ccw", 0)) log(("\033[36;1mrotor x=%f y=%f z=%f nx=%f ny=%f nz=%f fx=%f fy=%f fz=%f numblades=%d diameter=%f " \ + "chord=%f twist=%f taper=%f rel_len_blade_start=%f phi0=%f ccw=%d\033[m") \ % (c[0], c[1], c[2], norm[0], norm[1], norm[2], fwd[0], fwd[1], fwd[2], numblades, \ diameter, chord, twist, taper, rel_len_blade_start, phi0, ccw)) item = Rotor("YASim_rotor#%d" % self.counter[tag], c, norm, fwd, numblades, 0.5 * diameter, chord, \ twist, taper, rel_len_blade_start, phi0, ccw) elif tag not in self.ignored: log("\033[30;1m%s\033[m" % path) self.items.append(item) def endElement(self, tag): self.tags.pop() self.items.pop() def extract_matrix(filedata, tag): v = { 'x': 0.0, 'y': 0.0, 'z': 0.0, 'h': 0.0, 'p': 0.0, 'r': 0.0 } has_offsets = False for line in filedata: line = string.strip(line) if not line.startswith("<!--") or not line.endswith("-->"): continue line = string.strip(line[4:-3]) if not string.lower(line).startswith("%s:" % tag): continue line = string.strip(line[len(tag) + 1:]) for assignment in string.split(line): (key, value) = string.split(assignment, '=', 2) v[string.strip(key)] = float(string.strip(value)) has_offsets = True if not has_offsets: return None print(("using offsets: x=%f y=%f z=%f h=%f p=%f r=%f" % (v['x'], v['y'], v['z'], v['h'], v['p'], v['r']))) return Euler(v['r'], v['p'], v['h']).toMatrix().resize4x4() * TranslationMatrix(Vector(v['x'], v['y'], v['z'])) def load_yasim_config(path): if BPyMessages.Error_NoFile(path): return Blender.Window.WaitCursor(1) Blender.Window.EditMode(0) print(("loading '%s'" % path)) try: for o in Item.scene.objects: if o.name.startswith("YASim_"): Item.scene.objects.unlink(o) try: f = open(path) Global.data = f.readlines() finally: f.close() Global.path = path Global.matrix = YASIM_MATRIX matrix = extract_matrix(Global.data, "offsets") if matrix: Global.matrix *= matrix.invert() Global.yasim.parse(path) Blender.Registry.SetKey("FGYASimImportExport", { "path": path }, False) Global.data = None except Abort, e: print(("%s\nAborting ..." % (e.term or e.msg))) Blender.Draw.PupMenu("Error%t|" + e.msg) Blender.Window.RedrawAll() Blender.Window.WaitCursor(0) def gui_draw(): from Blender import BGL, Draw (width, height) = Blender.Window.GetAreaSize() BGL.glClearColor(0.4, 0.4, 0.45, 1) BGL.glClear(BGL.GL_COLOR_BUFFER_BIT) BGL.glColor3f(1, 1, 1) BGL.glRasterPos2f(5, 55) Draw.Text("FlightGear YASim Import: '%s'" % Global.path) Draw.PushButton("Reload", RELOAD_BUTTON, 5, 5, 80, 32, "reload YASim config file") Global.mirror_button = Draw.Toggle("Mirror", MIRROR_BUTTON, 100, 5, 50, 16, Global.mirror_button.val, \ "show symmetric surfaces on both sides (reloads config)") Draw.PushButton("Update Cursor", CURSOR_BUTTON, width - 650, 5, 100, 32, "update cursor display (in YASim coordinate system)") BGL.glRasterPos2f(width - 530 + Blender.Draw.GetStringWidth("Vector from last") - Blender.Draw.GetStringWidth("Current"), 24) Draw.Text("Current cursor pos: x = %+.3f y = %+.3f z = %+.3f" % tuple(Global.cursor)) c = Global.cursor - Global.last_cursor BGL.glRasterPos2f(width - 530, 7) Draw.Text("Vector from last cursor pos: x = %+.3f y = %+.3f z = %+.3f length = %.3f m" % (c[0], c[1], c[2], c.length)) def gui_event(ev, value): if ev == Blender.Draw.ESCKEY: Blender.Draw.Exit() def gui_button(n): if n == NO_EVENT: return elif n == RELOAD_BUTTON: load_yasim_config(Global.path) elif n == CURSOR_BUTTON: Global.last_cursor = Global.cursor Global.cursor = Vector(Blender.Window.GetCursorPos()) * Global.matrix.invert() d = Global.cursor - Global.last_cursor print(("cursor: x=\"%f\" y=\"%f\" z=\"%f\" dx=%f dy=%f dz=%f length=%f" \ % (Global.cursor[0], Global.cursor[1], Global.cursor[2], d[0], d[1], d[2], d.length))) elif n == MIRROR_BUTTON: load_yasim_config(Global.path) Blender.Draw.Redraw(1) def main(): log(6 * "\n") registry = Blender.Registry.GetKey("FGYASimImportExport", False) if registry and "path" in registry and Blender.sys.exists(Blender.sys.expandpath(registry["path"])): path = registry["path"] else: path = "" xml_handler = import_yasim() Global.yasim = make_parser() Global.yasim.setContentHandler(xml_handler) Global.yasim.setErrorHandler(xml_handler) if Blender.Window.GetScreenInfo(Blender.Window.Types.SCRIPT): Blender.Draw.Register(gui_draw, gui_event, gui_button) Blender.Window.FileSelector(load_yasim_config, "Import YASim Configuration File", path) main()
gpl-2.0
1,149,373,334,458,743,400
36.117291
139
0.627769
false
2.725142
true
false
false
sahat/bokeh
examples/plotting/server/remote_image.py
1
1468
import numpy as np from bokeh.plotting import * from bokeh.objects import Range1d, ServerDataSource """ In order to run this example, you have to execute ./bokeh-server -D remotedata the remote data directory in the bokeh checkout has the sample data for this example In addition, you must install ArrayManagement from this branch (soon to be master) https://github.com/ContinuumIO/ArrayManagement """ N = 1000 x = np.linspace(0, 10, N) y = np.linspace(0, 10, N) xx, yy = np.meshgrid(x, y) d = np.sin(xx)*np.cos(yy) output_server("remote_image") source = ServerDataSource(data_url="/defaultuser/array.table/array", owner_username="defaultuser", data={'x': [0], 'y': [0], 'global_x_range' : [0, 10], 'global_y_range' : [0, 10], 'global_offset_x' : [0], 'global_offset_y' : [0], 'dw' : [10], 'dh' : [10], 'palette': ["Spectral-11"] } ) image( source=source, image="image", x="x", y="y", dw="dw", dh="dh", width=200, height=200, palette="palette", x_range=Range1d(start=0, end=10), y_range=Range1d(start=0, end=10), tools="pan,wheel_zoom,box_zoom,reset,previewsave" ) show()
bsd-3-clause
-9,210,181,709,916,321,000
27.784314
84
0.50545
false
3.598039
false
false
false
utkarsh-goswami/erpnext
erpnext/accounts/doctype/pricing_rule/pricing_rule.py
1
13027
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors # MIT License. See license.txt # For license information, please see license.txt from __future__ import unicode_literals import frappe import json import copy from frappe import throw, _ from frappe.utils import flt, cint from frappe.model.document import Document class MultiplePricingRuleConflict(frappe.ValidationError): pass class PricingRule(Document): def validate(self): self.validate_mandatory() self.validate_applicable_for_selling_or_buying() self.validate_min_max_qty() self.cleanup_fields_value() self.validate_price_or_discount() self.validate_max_discount() if self.price_or_discount != 'Price': self.currency = None if not self.margin_type: self.margin_rate_or_amount = 0.0 def validate_mandatory(self): for field in ["apply_on", "applicable_for"]: tocheck = frappe.scrub(self.get(field) or "") if tocheck and not self.get(tocheck): throw(_("{0} is required").format(self.meta.get_label(tocheck)), frappe.MandatoryError) def validate_applicable_for_selling_or_buying(self): if not self.selling and not self.buying: throw(_("Atleast one of the Selling or Buying must be selected")) if not self.selling and self.applicable_for in ["Customer", "Customer Group", "Territory", "Sales Partner", "Campaign"]: throw(_("Selling must be checked, if Applicable For is selected as {0}" .format(self.applicable_for))) if not self.buying and self.applicable_for in ["Supplier", "Supplier Type"]: throw(_("Buying must be checked, if Applicable For is selected as {0}" .format(self.applicable_for))) def validate_min_max_qty(self): if self.min_qty and self.max_qty and flt(self.min_qty) > flt(self.max_qty): throw(_("Min Qty can not be greater than Max Qty")) def cleanup_fields_value(self): for logic_field in ["apply_on", "applicable_for", "price_or_discount"]: fieldname = frappe.scrub(self.get(logic_field) or "") # reset all values except for the logic field options = (self.meta.get_options(logic_field) or "").split("\n") for f in options: if not f: continue f = frappe.scrub(f) if f!=fieldname: self.set(f, None) def validate_price_or_discount(self): for field in ["Price"]: if flt(self.get(frappe.scrub(field))) < 0: throw(_("{0} can not be negative").format(field)) def validate_max_discount(self): if self.price_or_discount == "Discount Percentage" and self.item_code: max_discount = frappe.db.get_value("Item", self.item_code, "max_discount") if max_discount and flt(self.discount_percentage) > flt(max_discount): throw(_("Max discount allowed for item: {0} is {1}%").format(self.item_code, max_discount)) #-------------------------------------------------------------------------------- @frappe.whitelist() def apply_pricing_rule(args): """ args = { "items": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...], "customer": "something", "customer_group": "something", "territory": "something", "supplier": "something", "supplier_type": "something", "currency": "something", "conversion_rate": "something", "price_list": "something", "plc_conversion_rate": "something", "company": "something", "transaction_date": "something", "campaign": "something", "sales_partner": "something", "ignore_pricing_rule": "something" } """ if isinstance(args, basestring): args = json.loads(args) args = frappe._dict(args) if not args.transaction_type: set_transaction_type(args) # list of dictionaries out = [] if args.get("doctype") == "Material Request": return out item_list = args.get("items") args.pop("items") set_serial_nos_based_on_fifo = frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo") for item in item_list: args_copy = copy.deepcopy(args) args_copy.update(item) out.append(get_pricing_rule_for_item(args_copy)) if set_serial_nos_based_on_fifo and not args.get('is_return'): out.append(get_serial_no_for_item(args_copy)) return out def get_serial_no_for_item(args): from erpnext.stock.get_item_details import get_serial_no item_details = frappe._dict({ "doctype": args.doctype, "name": args.name, "serial_no": args.serial_no }) if args.get("parenttype") in ("Sales Invoice", "Delivery Note") and args.stock_qty > 0: item_details.serial_no = get_serial_no(args) return item_details def get_pricing_rule_for_item(args): if args.get("parenttype") == "Material Request": return {} item_details = frappe._dict({ "doctype": args.doctype, "name": args.name, "pricing_rule": None }) if args.ignore_pricing_rule or not args.item_code: if frappe.db.exists(args.doctype, args.name) and args.get("pricing_rule"): item_details = remove_pricing_rule_for_item(args.get("pricing_rule"), item_details) return item_details if not (args.item_group and args.brand): try: args.item_group, args.brand = frappe.db.get_value("Item", args.item_code, ["item_group", "brand"]) except TypeError: # invalid item_code return item_details if not args.item_group: frappe.throw(_("Item Group not mentioned in item master for item {0}").format(args.item_code)) if args.transaction_type=="selling": if args.customer and not (args.customer_group and args.territory): customer = frappe.db.get_value("Customer", args.customer, ["customer_group", "territory"]) if customer: args.customer_group, args.territory = customer args.supplier = args.supplier_type = None elif args.supplier and not args.supplier_type: args.supplier_type = frappe.db.get_value("Supplier", args.supplier, "supplier_type") args.customer = args.customer_group = args.territory = None pricing_rules = get_pricing_rules(args) pricing_rule = filter_pricing_rules(args, pricing_rules) if pricing_rule: item_details.pricing_rule = pricing_rule.name item_details.pricing_rule_for = pricing_rule.price_or_discount item_details.margin_type = pricing_rule.margin_type item_details.margin_rate_or_amount = pricing_rule.margin_rate_or_amount if pricing_rule.price_or_discount == "Price": if pricing_rule.get('currency') and \ pricing_rule.currency == args.currency: price_list_rate = pricing_rule.price * (args.conversion_factor or 1.0) else: price_list_rate = (pricing_rule.price/flt(args.conversion_rate)) * args.conversion_factor or 1.0 \ if args.conversion_rate else 0.0 item_details.update({ "price_list_rate": price_list_rate, "discount_percentage": 0.0 }) else: item_details.discount_percentage = pricing_rule.discount_percentage elif args.get('pricing_rule'): item_details = remove_pricing_rule_for_item(args.get("pricing_rule"), item_details) return item_details def remove_pricing_rule_for_item(pricing_rule, item_details): pricing_rule = frappe.db.get_value('Pricing Rule', pricing_rule, ['price_or_discount', 'margin_type'], as_dict=1) if pricing_rule and pricing_rule.price_or_discount == 'Discount Percentage': item_details.discount_percentage = 0.0 if pricing_rule and pricing_rule.margin_type in ['Percentage', 'Amount']: item_details.margin_rate_or_amount = 0.0 item_details.margin_type = None if item_details.pricing_rule: item_details.pricing_rule = None return item_details @frappe.whitelist() def remove_pricing_rules(item_list): if isinstance(item_list, basestring): item_list = json.loads(item_list) out = [] for item in item_list: item = frappe._dict(item) out.append(remove_pricing_rule_for_item(item.get("pricing_rule"), item)) return out def get_pricing_rules(args): def _get_tree_conditions(parenttype, allow_blank=True): field = frappe.scrub(parenttype) condition = "" if args.get(field): try: lft, rgt = frappe.db.get_value(parenttype, args[field], ["lft", "rgt"]) except TypeError: frappe.throw(_("Invalid {0}").format(args[field])) parent_groups = frappe.db.sql_list("""select name from `tab%s` where lft<=%s and rgt>=%s""" % (parenttype, '%s', '%s'), (lft, rgt)) if parent_groups: if allow_blank: parent_groups.append('') condition = " ifnull("+field+", '') in ('" + \ "', '".join([frappe.db.escape(d) for d in parent_groups])+"')" return condition conditions = item_variant_condition = "" values = {"item_code": args.get("item_code"), "brand": args.get("brand")} for field in ["company", "customer", "supplier", "supplier_type", "campaign", "sales_partner", "currency"]: if args.get(field): conditions += " and ifnull("+field+", '') in (%("+field+")s, '')" values[field] = args.get(field) else: conditions += " and ifnull("+field+", '') = ''" for parenttype in ["Customer Group", "Territory"]: group_condition = _get_tree_conditions(parenttype) if group_condition: conditions += " and " + group_condition if not args.price_list: args.price_list = None conditions += " and ifnull(for_price_list, '') in (%(price_list)s, '')" values["price_list"] = args.get("price_list") if args.get("transaction_date"): conditions += """ and %(transaction_date)s between ifnull(valid_from, '2000-01-01') and ifnull(valid_upto, '2500-12-31')""" values['transaction_date'] = args.get('transaction_date') item_group_condition = _get_tree_conditions("Item Group", False) if item_group_condition: item_group_condition = " or " + item_group_condition # load variant of if not defined if "variant_of" not in args: args.variant_of = frappe.db.get_value("Item", args.item_code, "variant_of") if args.variant_of: item_variant_condition = ' or item_code=%(variant_of)s ' values['variant_of'] = args.variant_of return frappe.db.sql("""select * from `tabPricing Rule` where (item_code=%(item_code)s {item_variant_condition} {item_group_condition} or brand=%(brand)s) and docstatus < 2 and disable = 0 and {transaction_type} = 1 {conditions} order by priority desc, name desc""".format( item_group_condition = item_group_condition, item_variant_condition = item_variant_condition, transaction_type = args.transaction_type, conditions = conditions), values, as_dict=1) def filter_pricing_rules(args, pricing_rules): # filter for qty stock_qty = args.get('qty') * args.get('conversion_factor', 1) if pricing_rules: pricing_rules = filter(lambda x: (flt(stock_qty)>=flt(x.min_qty) and (flt(stock_qty)<=x.max_qty if x.max_qty else True)), pricing_rules) # add variant_of property in pricing rule for p in pricing_rules: if p.item_code and args.variant_of: p.variant_of = args.variant_of else: p.variant_of = None # find pricing rule with highest priority if pricing_rules: max_priority = max([cint(p.priority) for p in pricing_rules]) if max_priority: pricing_rules = filter(lambda x: cint(x.priority)==max_priority, pricing_rules) # apply internal priority all_fields = ["item_code", "item_group", "brand", "customer", "customer_group", "territory", "supplier", "supplier_type", "campaign", "sales_partner", "variant_of"] if len(pricing_rules) > 1: for field_set in [["item_code", "variant_of", "item_group", "brand"], ["customer", "customer_group", "territory"], ["supplier", "supplier_type"]]: remaining_fields = list(set(all_fields) - set(field_set)) if if_all_rules_same(pricing_rules, remaining_fields): pricing_rules = apply_internal_priority(pricing_rules, field_set, args) break if len(pricing_rules) > 1: price_or_discount = list(set([d.price_or_discount for d in pricing_rules])) if len(price_or_discount) == 1 and price_or_discount[0] == "Discount Percentage": pricing_rules = filter(lambda x: x.for_price_list==args.price_list, pricing_rules) \ or pricing_rules if len(pricing_rules) > 1 and not args.for_shopping_cart: frappe.throw(_("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: {0}") .format("\n".join([d.name for d in pricing_rules])), MultiplePricingRuleConflict) elif pricing_rules: return pricing_rules[0] def if_all_rules_same(pricing_rules, fields): all_rules_same = True val = [pricing_rules[0][k] for k in fields] for p in pricing_rules[1:]: if val != [p[k] for k in fields]: all_rules_same = False break return all_rules_same def apply_internal_priority(pricing_rules, field_set, args): filtered_rules = [] for field in field_set: if args.get(field): filtered_rules = filter(lambda x: x[field]==args[field], pricing_rules) if filtered_rules: break return filtered_rules or pricing_rules def set_transaction_type(args): if args.doctype in ("Opportunity", "Quotation", "Sales Order", "Delivery Note", "Sales Invoice"): args.transaction_type = "selling" elif args.doctype in ("Material Request", "Supplier Quotation", "Purchase Order", "Purchase Receipt", "Purchase Invoice"): args.transaction_type = "buying" elif args.customer: args.transaction_type = "selling" else: args.transaction_type = "buying"
gpl-3.0
7,289,619,787,295,705,000
34.693151
131
0.68834
false
3.033768
false
false
false
prataprc/tayra
tayra/test/stdttl/ref/useinterface.ttl.py
1
2558
import imp from io import StringIO from pluggdapps.plugin import Plugin, implements from tayra import BaseTTLPlugin def __traceback_decorator__( frames ): from copy import deepcopy from os.path import basename def _map2ttl( frame ): filename = frame.filename lineno = frame.lineno lines = open(filename).readlines()[:lineno] lines.reverse() rc = {} for l in lines : if l.strip().startswith('# lineno') : _, ttl_lineno = l.split(':', 1) ttl_lineno = int( ttl_lineno ) ttl_text = open( _ttlfile ).readlines()[ ttl_lineno-1 ] return ttl_lineno, ttl_text return None, None newframes = [] for frame in frames : newframes.append( frame ) frameadded = getattr( frame, '_ttlframeadded', False ) basen = basename( frame.filename ) if basen.endswith( '.ttl.py' ) and basen == (basename( _ttlfile ) + '.py') and frameadded == False : newframe = deepcopy( frame ) frame._ttlframeadded = True try : newframe.lineno, newframe.linetext = _map2ttl( newframe ) if newframe.lineno : newframe.filename = _ttlfile newframes.append( newframe ) except : raise continue return newframes from tayra.interfaces import ITayraTestInterface def body( *args, **kwargs ) : _m.pushbuf() _m.extend( ['<!DOCTYPE html>\n'] ) # lineno:4 obj = _compiler.query_plugin( ITayraTestInterface, 'tayra.XYZTestInterface' ) # lineno:6 _m.pushbuf() _m.extend( ['<html>'] ) _m.pushbuf() _m.extend( ['\n '] ) # lineno:7 _m.pushbuf() _m.extend( ['<head>'] ) _m.pushbuf() _m.extend( ['\n '] ) _m.handletag( _m.popbuftext(), _m.popbuftext(), **{'nl': '', 'oprune': False, 'indent': False, 'iprune': False} ) # lineno:8 _m.pushbuf() _m.extend( ['<body>'] ) _m.pushbuf() _m.extend( ['\n '] ) # lineno:9 _m.extend( [''] ) _m.append(_m.evalexprs( '', 'obj.render()', '', globals(), locals()) ) _m.extend( ['\n'] ) _m.handletag( _m.popbuftext(), _m.popbuftext(), **{'nl': '', 'oprune': False, 'indent': False, 'iprune': False} ) _m.handletag( _m.popbuftext(), _m.popbuftext(), **{'nl': '', 'oprune': False, 'indent': False, 'iprune': False} ) return _m.popbuftext() # ---- Global Functions # ---- Interface functions # ---- Footer
gpl-3.0
4,534,265,095,234,785,000
31.794872
134
0.538702
false
3.475543
false
false
false
asiersarasua/QGIS
python/plugins/db_manager/layer_preview.py
1
5023
# -*- coding: utf-8 -*- """ /*************************************************************************** Name : DB Manager Description : Database manager plugin for QGIS Date : May 23, 2011 copyright : (C) 2011 by Giuseppe Sucameli email : [email protected] ***************************************************************************/ /*************************************************************************** * * * 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 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ """ from qgis.PyQt.QtCore import Qt, QTimer from qgis.PyQt.QtGui import QColor, QCursor from qgis.PyQt.QtWidgets import QApplication from qgis.gui import QgsMapCanvas, QgsMessageBar from qgis.core import Qgis, QgsVectorLayer, QgsProject, QgsSettings from qgis.utils import OverrideCursor from .db_plugins.plugin import Table class LayerPreview(QgsMapCanvas): def __init__(self, parent=None): super(LayerPreview, self).__init__(parent) self.parent = parent self.setCanvasColor(QColor(255, 255, 255)) self.item = None self.dirty = False self.currentLayerId = None # reuse settings from QGIS settings = QgsSettings() self.enableAntiAliasing(settings.value("/qgis/enable_anti_aliasing", False, type=bool)) zoomFactor = settings.value("/qgis/zoom_factor", 2, type=float) self.setWheelFactor(zoomFactor) def refresh(self): self.setDirty(True) self.loadPreview(self.item) def loadPreview(self, item): if item == self.item and not self.dirty: return if item is None: return self._clear() if isinstance(item, Table) and item.type in [Table.VectorType, Table.RasterType]: # update the preview, but first let the manager chance to show the canvas def runPrev(): return self._loadTablePreview(item) QTimer.singleShot(50, runPrev) else: return self.item = item self.item.aboutToChange.connect(self.setDirty) def setDirty(self, val=True): self.dirty = val def _clear(self): """ remove any layers from preview canvas """ if self.item is not None: # skip exception on RuntimeError fixes #6892 try: self.item.aboutToChange.disconnect(self.setDirty) except RuntimeError: pass self.item = None self.dirty = False self._loadTablePreview(None) def _loadTablePreview(self, table, limit=False): """ if has geometry column load to map canvas """ with OverrideCursor(Qt.WaitCursor): self.freeze() vl = None if table and table.geomType: # limit the query result if required if limit and table.rowCount > 1000: uniqueField = table.getValidQgisUniqueFields(True) if uniqueField is None: self.parent.tabs.setCurrentWidget(self.parent.info) self.parent.infoBar.pushMessage( QApplication.translate("DBManagerPlugin", "Unable to find a valid unique field"), Qgis.Warning, self.parent.iface.messageTimeout()) return uri = table.database().uri() uri.setDataSource("", u"(SELECT * FROM %s LIMIT 1000)" % table.quotedName(), table.geomColumn, "", uniqueField.name) provider = table.database().dbplugin().providerName() vl = QgsVectorLayer(uri.uri(False), table.name, provider) else: vl = table.toMapLayer() if vl and not vl.isValid(): vl.deleteLater() vl = None # remove old layer (if any) and set new if self.currentLayerId: if not QgsProject.instance().layerTreeRoot().findLayer(self.currentLayerId): QgsProject.instance().removeMapLayers([self.currentLayerId]) if vl and vl.isValid(): self.setLayers([vl]) QgsProject.instance().addMapLayers([vl], False) self.zoomToFullExtent() else: self.setLayers([]) self.currentLayerId = vl.id() self.freeze(False) super().refresh()
gpl-2.0
151,934,034,634,910,370
36.207407
118
0.516624
false
4.895712
false
false
false
swayf/pyLoad
module/plugins/accounts/FilesonicCom.py
1
2796
# -*- coding: utf-8 -*- """ 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/>. @author: RaNaN """ from time import mktime, strptime from module.plugins.Account import Account from module.common.json_layer import json_loads class FilesonicCom(Account): __name__ = "FilesonicCom" __version__ = "0.31" __type__ = "account" __description__ = """filesonic.com account plugin""" __author_name__ = ("RaNaN", "Paul King") __author_mail__ = ("[email protected]", "") API_URL = "http://api.filesonic.com" def getDomain(self, req): xml = req.load(self.API_URL + "/utility?method=getFilesonicDomainForCurrentIp&format=json", decode=True) return json_loads(xml)["FSApi_Utility"]["getFilesonicDomainForCurrentIp"]["response"] def loadAccountInfo(self, req): xml = req.load(self.API_URL + "/user?method=getInfo&format=json", post={"u": self.loginname, "p": self.password}, decode=True) self.logDebug("account status retrieved from api %s" % xml) json = json_loads(xml) if json["FSApi_User"]["getInfo"]["status"] != "success": self.logError(_("Invalid login retrieving user details")) return {"validuntil": -1, "trafficleft": -1, "premium": False} premium = json["FSApi_User"]["getInfo"]["response"]["users"]["user"]["is_premium"] if premium: validuntil = json["FSApi_User"]["getInfo"]["response"]["users"]["user"]["premium_expiration"] validuntil = int(mktime(strptime(validuntil, "%Y-%m-%d %H:%M:%S"))) else: validuntil = -1 return {"validuntil": validuntil, "trafficleft": -1, "premium": premium} def login(self, req): domain = self.getDomain(req) post_vars = { "email": self.loginname, "password": self.password, "rememberMe": 1 } page = req.load("http://www%s/user/login" % domain, cookies=True, post=post_vars, decode=True) if "Provided password does not match." in page or "You must be logged in to view this page." in page: self.wrongPassword()
agpl-3.0
-295,136,709,200,270,100
38.380282
109
0.620529
false
3.85124
false
false
false
Imperat/SSU-Courses
ssu-formal-languages/pda/pda.py
1
1105
import pda_exceptions as e class PDA(object): def __init__(self, rules, input_alphabet, states, initial_state, terminate_states): self.rules = rules self.input_alphabet = input_alphabet self.states = states self.state = initial_state self.terminate_states = terminate_states self.crash = False def _crash(self): self.crash = True def input(self, symbol): # print symbol + "- - " + self.state try: if self.crash: raise e.PDACrashException( "Error by input. PDA is crashed!") self.state = self.rules[self.state][symbol] except KeyError: if symbol not in self.input_alphabet: self._crash() raise e.UnknownSymbolException( "Symbol isn't in input alphabet") else: self._crash() raise e.PDACrashException( "PDA is crashed") def in_terminate_state(self): return self.state in self.terminate_states
apache-2.0
1,192,358,845,135,127,600
30.571429
55
0.540271
false
4.437751
false
false
false
caedesvvv/pynoded
pynoded/graph.py
1
4291
""" Base graph objects """ from evh.base import EvHandler,EvStack from math import * class Drawable(object): """ Base class for drawable objects. """ def __init__(self,x,y,scale=1.0): self.x=x self.y=y self.scale=scale def Draw(self,ctx): ctx.save() ctx.scale(self.scale,self.scale) ctx.translate(self.x,self.y) self.Draw_(ctx) ctx.restore() def ToLocal(self,x,y): return ((x/self.scale)-self.x,(y/self.scale)-self.y) def FromLocal(self,x,y): return (self.x+x*self.scale,self.y+y*self.scale) def Draw_(self,ctx): """ Main method to do the cairo drawing of the object. This is the main function drawable objects have to override. @param ctx: cairo context """ pass class Collider(object): """ Base class for colliders. """ def Test(self,x,y): raise repr(self),"Not implemented!" class CircleCollider(Collider): """ A circle collider. """ def __init__(self,r): self.r=r def Test(self,x,y): return sqrt((x-self.x)**2+(y-self.y)**2)<=self.r class RectCollider(Collider): """ A rect collider. """ def __init__(self,w,h): self.w=w self.h=h def Test(self,x,y): return x>=self.x and x<=self.x+self.w and y>=self.y and y<=self.y+self.h class GraphObject(Drawable,Collider): """ Base class for graph objects. """ def __init__(self,parent,x,y,scale=1.0): if parent: self.parent=parent Drawable.__init__(self,x,y,scale) self.evstack=EvStack() def GetPointer(self): return self.ToLocal(*self.parent.GetPointer()) def Redraw(self): self.parent.Redraw() def ToParent(self,obj,x,y): if obj==self: return (x,y) else: return self.parent.ToParent(obj,*self.FromLocal(x,y)) def Root(self): return self.parent.Root() class Graph(GraphObject): """ A graph capable of containing connected objects. """ def __init__(self,parent,x,y,scale=1.0): GraphObject.__init__(self,parent,x,y,scale) self.evstack.append(PropagateEvH(self)) self.Clear() def Clear(self): self.objects=[[],[],[],[]] def Draw_(self,ctx): for prio in self.objects: for obj in prio: obj.Draw(ctx) def Propagate(self,x,y,event,*args): o=self.ObjectAt(x,y) return o and getattr(o.evstack,event,False) and getattr(o.evstack,event)(*args) def ObjectAt(self,x,y): for prio in reversed(self.objects): for o in reversed(prio): if o.Test(x,y): return o class MainGraph(Graph): """ Base class for main graphs. """ def __init__(self,*args): Graph.__init__(self,*args) self.objects[1]=[] def Clear(self): Graph.Clear(self) self.objects[1]=[] def Zoom(self,x,y,factor): pre_x,pre_y = self.ToLocal(x,y) self.scale *=factor post_x,post_y = self.ToLocal(x,y) self.x,self.y = (self.x+post_x-pre_x,self.y+post_y-pre_y) self.Redraw() def AddNode(self,obj): self.objects[0].append(obj) self.Redraw() def ToGlobal(self,x,y): return (x,y) def GetRawPointer(self): raise "Not implemented" def GetPointer(self): return self.ToLocal(*self.RawPointer) def Root(self): return self def Test(self,x,y): return True def CenteredBB(self,x,y,size): # not really needed, but useful in general.. obj_size = size bb = [x-(obj_size/2),y-(obj_size/2),obj_size,obj_size] return bb RawPointer = property(GetRawPointer) Pointer = property(GetPointer) class PropagateEvH(EvHandler): """ Event handler for propagating to children. """ def __init__(self,graph): """ PropagateEvH Constructor. @param graph: graph to which this event handler is attached. """ self.graph=graph def __getattr__(self,name): x,y=self.graph.GetPointer() return lambda *args: self.graph.Propagate(x,y,name,*args)
gpl-3.0
-4,142,430,700,984,500,700
23.66092
87
0.563505
false
3.349727
false
false
false
wadobo/congressus
congressus/tickets/templatetags/tickets.py
1
1531
import re from django import template from django.utils.translation import ugettext as _ from django.utils.html import mark_safe register = template.Library() @register.simple_tag def ticket_seat_class(session, layout, seat, row, col): row = str(row) col = str(col) if seat == 'R': return 'seat-R' elif seat == '_': return 'seat-_' holded_type = session.is_seat_holded(layout, row, col) if holded_type: return 'seat-' + re.sub('[CP]', 'H', holded_type) if session.is_seat_available(layout, row, col): return 'seat-L' return 'seat-R' @register.simple_tag(takes_context=True) def scene_span(context, session, map): flag = 'scenedraw-%s' % session.id if flag in context: return '' context.dicts[0][flag] = True rows = (map.scene_bottom - map.scene_top) + 1 cols = (map.scene_right - map.scene_left) + 1 html = '<td class="scene" rowspan="%s" colspan="%s"> %s </td>' % (rows, cols, _('scene')) return mark_safe(html) @register.simple_tag def key(data, key, prefix="", default=''): k = key if prefix: k = prefix + str(key) return data.get(k, default) @register.simple_tag def get_value(dic, key): if not isinstance(dic, dict): return return dic.get(key) @register.simple_tag def get_free_seats(dic, session_id, layout): if not isinstance(dic, dict): return free = dic.get((session_id, layout.id)) if free is None: free = layout.free() return free
agpl-3.0
2,939,241,391,327,671,000
21.514706
93
0.614631
false
3.150206
false
false
false
michaelkirk/QGIS
python/plugins/processing/algs/lidar/lastools/lasnoise.py
1
3645
# -*- coding: utf-8 -*- """ *************************************************************************** lasnoise.py --------------------- Date : September 2013 Copyright : (C) 2013 by Martin Isenburg Email : martin near rapidlasso point com *************************************************************************** * * * 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 2 of the License, or * * (at your option) any later version. * * * *************************************************************************** """ __author__ = 'Martin Isenburg' __date__ = 'September 2013' __copyright__ = '(C) 2013, Martin Isenburg' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' import os from LAStoolsUtils import LAStoolsUtils from LAStoolsAlgorithm import LAStoolsAlgorithm from processing.core.parameters import ParameterNumber from processing.core.parameters import ParameterSelection class lasnoise(LAStoolsAlgorithm): ISOLATED = "ISOLATED" STEP_XY = "STEP_XY" STEP_Z = "STEP_Z" OPERATION = "OPERATION" OPERATIONS = ["classify", "remove"] CLASSIFY_AS = "CLASSIFY_AS" def defineCharacteristics(self): self.name, self.i18n_name = self.trAlgorithm('lasnoise') self.group, self.i18n_group = self.trAlgorithm('LAStools') self.addParametersVerboseGUI() self.addParametersPointInputGUI() self.addParameter(ParameterNumber(lasnoise.ISOLATED, self.tr("isolated if surrounding cells have only"), 0, None, 5)) self.addParameter(ParameterNumber(lasnoise.STEP_XY, self.tr("resolution of isolation grid in xy"), 0, None, 4.0)) self.addParameter(ParameterNumber(lasnoise.STEP_Z, self.tr("resolution of isolation grid in z"), 0, None, 4.0)) self.addParameter(ParameterSelection(lasnoise.OPERATION, self.tr("what to do with isolated points"), lasnoise.OPERATIONS, 0)) self.addParameter(ParameterNumber(lasnoise.CLASSIFY_AS, self.tr("classify as"), 0, None, 7)) self.addParametersPointOutputGUI() self.addParametersAdditionalGUI() def processAlgorithm(self, progress): commands = [os.path.join(LAStoolsUtils.LAStoolsPath(), "bin", "lasnoise")] self.addParametersVerboseCommands(commands) self.addParametersPointInputCommands(commands) isolated = self.getParameterValue(lasnoise.ISOLATED) commands.append("-isolated") commands.append(str(isolated)) step_xy = self.getParameterValue(lasnoise.STEP_XY) commands.append("-step_xy") commands.append(str(step_xy)) step_z = self.getParameterValue(lasnoise.STEP_Z) commands.append("-step_z") commands.append(str(step_z)) operation = self.getParameterValue(lasnoise.OPERATION) if operation != 0: commands.append("-remove_noise") else: commands.append("-classify_as") classify_as = self.getParameterValue(lasnoise.CLASSIFY_AS) commands.append(str(classify_as)) self.addParametersPointOutputCommands(commands) self.addParametersAdditionalCommands(commands) LAStoolsUtils.runLAStools(commands, progress)
gpl-2.0
-8,790,487,615,896,949,000
42.915663
82
0.590672
false
4.298349
false
false
false
pirate/bookmark-archiver
archivebox/extractors/readability.py
1
4294
__package__ = 'archivebox.extractors' from pathlib import Path from tempfile import NamedTemporaryFile from typing import Optional import json from ..index.schema import Link, ArchiveResult, ArchiveError from ..system import run, atomic_write from ..util import ( enforce_types, download_url, is_static_file, ) from ..config import ( TIMEOUT, CURL_BINARY, SAVE_READABILITY, DEPENDENCIES, READABILITY_VERSION, ) from ..logging_util import TimedProgress @enforce_types def get_html(link: Link, path: Path) -> str: """ Try to find wget, singlefile and then dom files. If none is found, download the url again. """ canonical = link.canonical_outputs() abs_path = path.absolute() sources = [canonical["singlefile_path"], canonical["wget_path"], canonical["dom_path"]] document = None for source in sources: try: with open(abs_path / source, "r", encoding="utf-8") as f: document = f.read() break except (FileNotFoundError, TypeError): continue if document is None: return download_url(link.url) else: return document @enforce_types def should_save_readability(link: Link, out_dir: Optional[str]=None, overwrite: Optional[bool]=False) -> bool: if is_static_file(link.url): return False out_dir = out_dir or Path(link.link_dir) if not overwrite and (out_dir / 'readability').exists(): return False return SAVE_READABILITY @enforce_types def save_readability(link: Link, out_dir: Optional[str]=None, timeout: int=TIMEOUT) -> ArchiveResult: """download reader friendly version using @mozilla/readability""" out_dir = Path(out_dir or link.link_dir) output_folder = out_dir.absolute() / "readability" output = "readability" # Readability Docs: https://github.com/mozilla/readability status = 'succeeded' # fake command to show the user so they have something to try debugging if get_html fails cmd = [ CURL_BINARY, link.url ] readability_content = None timer = TimedProgress(timeout, prefix=' ') try: document = get_html(link, out_dir) temp_doc = NamedTemporaryFile(delete=False) temp_doc.write(document.encode("utf-8")) temp_doc.close() if not document or len(document) < 10: raise ArchiveError('Readability could not find HTML to parse for article text') cmd = [ DEPENDENCIES['READABILITY_BINARY']['path'], temp_doc.name, ] result = run(cmd, cwd=out_dir, timeout=timeout) try: result_json = json.loads(result.stdout) assert result_json and 'content' in result_json except json.JSONDecodeError: raise ArchiveError('Readability was not able to archive the page', result.stdout + result.stderr) output_folder.mkdir(exist_ok=True) readability_content = result_json.pop("textContent") atomic_write(str(output_folder / "content.html"), result_json.pop("content")) atomic_write(str(output_folder / "content.txt"), readability_content) atomic_write(str(output_folder / "article.json"), result_json) # parse out number of files downloaded from last line of stderr: # "Downloaded: 76 files, 4.0M in 1.6s (2.52 MB/s)" output_tail = [ line.strip() for line in (result.stdout + result.stderr).decode().rsplit('\n', 3)[-3:] if line.strip() ] hints = ( 'Got readability response code: {}.'.format(result.returncode), *output_tail, ) # Check for common failure cases if (result.returncode > 0): raise ArchiveError('Readability was not able to archive the page', hints) except (Exception, OSError) as err: status = 'failed' output = err cmd = [cmd[0], './{singlefile,dom}.html'] finally: timer.end() return ArchiveResult( cmd=cmd, pwd=str(out_dir), cmd_version=READABILITY_VERSION, output=output, status=status, index_texts=[readability_content] if readability_content else [], **timer.stats, )
mit
4,898,612,251,076,805,000
30.807407
110
0.622729
false
3.935839
false
false
false
tysonholub/twilio-python
twilio/rest/taskrouter/v1/workspace/workspace_statistics.py
1
10107
# coding=utf-8 r""" This code was generated by \ / _ _ _| _ _ | (_)\/(_)(_|\/| |(/_ v1.0.0 / / """ from twilio.base import serialize from twilio.base import values from twilio.base.instance_context import InstanceContext from twilio.base.instance_resource import InstanceResource from twilio.base.list_resource import ListResource from twilio.base.page import Page class WorkspaceStatisticsList(ListResource): """ """ def __init__(self, version, workspace_sid): """ Initialize the WorkspaceStatisticsList :param Version version: Version that contains the resource :param workspace_sid: The SID of the Workspace :returns: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsList :rtype: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsList """ super(WorkspaceStatisticsList, self).__init__(version) # Path Solution self._solution = {'workspace_sid': workspace_sid, } def get(self): """ Constructs a WorkspaceStatisticsContext :returns: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsContext :rtype: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsContext """ return WorkspaceStatisticsContext(self._version, workspace_sid=self._solution['workspace_sid'], ) def __call__(self): """ Constructs a WorkspaceStatisticsContext :returns: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsContext :rtype: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsContext """ return WorkspaceStatisticsContext(self._version, workspace_sid=self._solution['workspace_sid'], ) def __repr__(self): """ Provide a friendly representation :returns: Machine friendly representation :rtype: str """ return '<Twilio.Taskrouter.V1.WorkspaceStatisticsList>' class WorkspaceStatisticsPage(Page): """ """ def __init__(self, version, response, solution): """ Initialize the WorkspaceStatisticsPage :param Version version: Version that contains the resource :param Response response: Response from the API :param workspace_sid: The SID of the Workspace :returns: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsPage :rtype: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsPage """ super(WorkspaceStatisticsPage, self).__init__(version, response) # Path Solution self._solution = solution def get_instance(self, payload): """ Build an instance of WorkspaceStatisticsInstance :param dict payload: Payload response from the API :returns: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsInstance :rtype: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsInstance """ return WorkspaceStatisticsInstance( self._version, payload, workspace_sid=self._solution['workspace_sid'], ) def __repr__(self): """ Provide a friendly representation :returns: Machine friendly representation :rtype: str """ return '<Twilio.Taskrouter.V1.WorkspaceStatisticsPage>' class WorkspaceStatisticsContext(InstanceContext): """ """ def __init__(self, version, workspace_sid): """ Initialize the WorkspaceStatisticsContext :param Version version: Version that contains the resource :param workspace_sid: The SID of the Workspace to fetch :returns: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsContext :rtype: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsContext """ super(WorkspaceStatisticsContext, self).__init__(version) # Path Solution self._solution = {'workspace_sid': workspace_sid, } self._uri = '/Workspaces/{workspace_sid}/Statistics'.format(**self._solution) def fetch(self, minutes=values.unset, start_date=values.unset, end_date=values.unset, task_channel=values.unset, split_by_wait_time=values.unset): """ Fetch a WorkspaceStatisticsInstance :param unicode minutes: Only calculate statistics since this many minutes in the past :param datetime start_date: Only calculate statistics from on or after this date :param datetime end_date: Only calculate statistics from this date and time and earlier :param unicode task_channel: Only calculate statistics on this TaskChannel. :param unicode split_by_wait_time: A comma separated list of values that describes the thresholds to calculate statistics on :returns: Fetched WorkspaceStatisticsInstance :rtype: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsInstance """ params = values.of({ 'Minutes': minutes, 'StartDate': serialize.iso8601_datetime(start_date), 'EndDate': serialize.iso8601_datetime(end_date), 'TaskChannel': task_channel, 'SplitByWaitTime': split_by_wait_time, }) payload = self._version.fetch( 'GET', self._uri, params=params, ) return WorkspaceStatisticsInstance( self._version, payload, workspace_sid=self._solution['workspace_sid'], ) def __repr__(self): """ Provide a friendly representation :returns: Machine friendly representation :rtype: str """ context = ' '.join('{}={}'.format(k, v) for k, v in self._solution.items()) return '<Twilio.Taskrouter.V1.WorkspaceStatisticsContext {}>'.format(context) class WorkspaceStatisticsInstance(InstanceResource): """ """ def __init__(self, version, payload, workspace_sid): """ Initialize the WorkspaceStatisticsInstance :returns: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsInstance :rtype: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsInstance """ super(WorkspaceStatisticsInstance, self).__init__(version) # Marshaled Properties self._properties = { 'realtime': payload.get('realtime'), 'cumulative': payload.get('cumulative'), 'account_sid': payload.get('account_sid'), 'workspace_sid': payload.get('workspace_sid'), 'url': payload.get('url'), } # Context self._context = None self._solution = {'workspace_sid': workspace_sid, } @property def _proxy(self): """ Generate an instance context for the instance, the context is capable of performing various actions. All instance actions are proxied to the context :returns: WorkspaceStatisticsContext for this WorkspaceStatisticsInstance :rtype: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsContext """ if self._context is None: self._context = WorkspaceStatisticsContext( self._version, workspace_sid=self._solution['workspace_sid'], ) return self._context @property def realtime(self): """ :returns: n object that contains the real-time statistics for the Workspace :rtype: dict """ return self._properties['realtime'] @property def cumulative(self): """ :returns: An object that contains the cumulative statistics for the Workspace :rtype: dict """ return self._properties['cumulative'] @property def account_sid(self): """ :returns: The SID of the Account that created the resource :rtype: unicode """ return self._properties['account_sid'] @property def workspace_sid(self): """ :returns: The SID of the Workspace :rtype: unicode """ return self._properties['workspace_sid'] @property def url(self): """ :returns: The absolute URL of the Workspace statistics resource :rtype: unicode """ return self._properties['url'] def fetch(self, minutes=values.unset, start_date=values.unset, end_date=values.unset, task_channel=values.unset, split_by_wait_time=values.unset): """ Fetch a WorkspaceStatisticsInstance :param unicode minutes: Only calculate statistics since this many minutes in the past :param datetime start_date: Only calculate statistics from on or after this date :param datetime end_date: Only calculate statistics from this date and time and earlier :param unicode task_channel: Only calculate statistics on this TaskChannel. :param unicode split_by_wait_time: A comma separated list of values that describes the thresholds to calculate statistics on :returns: Fetched WorkspaceStatisticsInstance :rtype: twilio.rest.taskrouter.v1.workspace.workspace_statistics.WorkspaceStatisticsInstance """ return self._proxy.fetch( minutes=minutes, start_date=start_date, end_date=end_date, task_channel=task_channel, split_by_wait_time=split_by_wait_time, ) def __repr__(self): """ Provide a friendly representation :returns: Machine friendly representation :rtype: str """ context = ' '.join('{}={}'.format(k, v) for k, v in self._solution.items()) return '<Twilio.Taskrouter.V1.WorkspaceStatisticsInstance {}>'.format(context)
mit
-1,229,389,616,420,329,200
34.588028
132
0.646977
false
4.575373
false
false
false
awsdocs/aws-doc-sdk-examples
python/example_code/rekognition/rekognition_collections.py
1
13421
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Shows how to use the AWS SDK for Python (Boto3) with Amazon Rekognition to create a collection that contains faces indexed from a series of images. The collection is then searched for faces that match a reference face. The usage demo in this file uses images in the .media folder. If you run this code without cloning the GitHub repository, you must first download the image files from https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/python/example_code/rekognition/.media """ import logging from pprint import pprint import boto3 from botocore.exceptions import ClientError from rekognition_objects import RekognitionFace from rekognition_image_detection import RekognitionImage logger = logging.getLogger(__name__) class RekognitionCollection: """ Encapsulates an Amazon Rekognition collection. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, collection, rekognition_client): """ Initializes a collection object. :param collection: Collection data in the format returned by a call to create_collection. :param rekognition_client: A Boto3 Rekognition client. """ self.collection_id = collection['CollectionId'] self.collection_arn, self.face_count, self.created = self._unpack_collection( collection) self.rekognition_client = rekognition_client @staticmethod def _unpack_collection(collection): """ Unpacks optional parts of a collection that can be returned by describe_collection. :param collection: The collection data. :return: A tuple of the data in the collection. """ return ( collection.get('CollectionArn'), collection.get('FaceCount', 0), collection.get('CreationTimestamp')) def to_dict(self): """ Renders parts of the collection data to a dict. :return: The collection data as a dict. """ rendering = { 'collection_id': self.collection_id, 'collection_arn': self.collection_arn, 'face_count': self.face_count, 'created': self.created } return rendering def describe_collection(self): """ Gets data about the collection from the Amazon Rekognition service. :return: The collection rendered as a dict. """ try: response = self.rekognition_client.describe_collection( CollectionId=self.collection_id) # Work around capitalization of Arn vs. ARN response['CollectionArn'] = response.get('CollectionARN') (self.collection_arn, self.face_count, self.created) = self._unpack_collection(response) logger.info("Got data for collection %s.", self.collection_id) except ClientError: logger.exception("Couldn't get data for collection %s.", self.collection_id) raise else: return self.to_dict() def delete_collection(self): """ Deletes the collection. """ try: self.rekognition_client.delete_collection(CollectionId=self.collection_id) logger.info("Deleted collection %s.", self.collection_id) self.collection_id = None except ClientError: logger.exception("Couldn't delete collection %s.", self.collection_id) raise def index_faces(self, image, max_faces): """ Finds faces in the specified image, indexes them, and stores them in the collection. :param image: The image to index. :param max_faces: The maximum number of faces to index. :return: A tuple. The first element is a list of indexed faces. The second element is a list of faces that couldn't be indexed. """ try: response = self.rekognition_client.index_faces( CollectionId=self.collection_id, Image=image.image, ExternalImageId=image.image_name, MaxFaces=max_faces, DetectionAttributes=['ALL']) indexed_faces = [ RekognitionFace({**face['Face'], **face['FaceDetail']}) for face in response['FaceRecords']] unindexed_faces = [ RekognitionFace(face['FaceDetail']) for face in response['UnindexedFaces']] logger.info( "Indexed %s faces in %s. Could not index %s faces.", len(indexed_faces), image.image_name, len(unindexed_faces)) except ClientError: logger.exception("Couldn't index faces in image %s.", image.image_name) raise else: return indexed_faces, unindexed_faces def list_faces(self, max_results): """ Lists the faces currently indexed in the collection. :param max_results: The maximum number of faces to return. :return: The list of faces in the collection. """ try: response = self.rekognition_client.list_faces( CollectionId=self.collection_id, MaxResults=max_results) faces = [RekognitionFace(face) for face in response['Faces']] logger.info( "Found %s faces in collection %s.", len(faces), self.collection_id) except ClientError: logger.exception( "Couldn't list faces in collection %s.", self.collection_id) raise else: return faces def search_faces_by_image(self, image, threshold, max_faces): """ Searches for faces in the collection that match the largest face in the reference image. :param image: The image that contains the reference face to search for. :param threshold: The match confidence must be greater than this value for a face to be included in the results. :param max_faces: The maximum number of faces to return. :return: A tuple. The first element is the face found in the reference image. The second element is the list of matching faces found in the collection. """ try: response = self.rekognition_client.search_faces_by_image( CollectionId=self.collection_id, Image=image.image, FaceMatchThreshold=threshold, MaxFaces=max_faces) image_face = RekognitionFace({ 'BoundingBox': response['SearchedFaceBoundingBox'], 'Confidence': response['SearchedFaceConfidence'] }) collection_faces = [ RekognitionFace(face['Face']) for face in response['FaceMatches']] logger.info("Found %s faces in the collection that match the largest " "face in %s.", len(collection_faces), image.image_name) except ClientError: logger.exception( "Couldn't search for faces in %s that match %s.", self.collection_id, image.image_name) raise else: return image_face, collection_faces def search_faces(self, face_id, threshold, max_faces): """ Searches for faces in the collection that match another face from the collection. :param face_id: The ID of the face in the collection to search for. :param threshold: The match confidence must be greater than this value for a face to be included in the results. :param max_faces: The maximum number of faces to return. :return: The list of matching faces found in the collection. This list does not contain the face specified by `face_id`. """ try: response = self.rekognition_client.search_faces( CollectionId=self.collection_id, FaceId=face_id, FaceMatchThreshold=threshold, MaxFaces=max_faces) faces = [RekognitionFace(face['Face']) for face in response['FaceMatches']] logger.info( "Found %s faces in %s that match %s.", len(faces), self.collection_id, face_id) except ClientError: logger.exception( "Couldn't search for faces in %s that match %s.", self.collection_id, face_id) raise else: return faces def delete_faces(self, face_ids): """ Deletes faces from the collection. :param face_ids: The list of IDs of faces to delete. :return: The list of IDs of faces that were deleted. """ try: response = self.rekognition_client.delete_faces( CollectionId=self.collection_id, FaceIds=face_ids) deleted_ids = response['DeletedFaces'] logger.info( "Deleted %s faces from %s.", len(deleted_ids), self.collection_id) except ClientError: logger.exception("Couldn't delete faces from %s.", self.collection_id) raise else: return deleted_ids class RekognitionCollectionManager: """ Encapsulates Amazon Rekognition collection management functions. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, rekognition_client): """ Initializes the collection manager object. :param rekognition_client: A Boto3 Rekognition client. """ self.rekognition_client = rekognition_client def create_collection(self, collection_id): """ Creates an empty collection. :param collection_id: Text that identifies the collection. :return: The newly created collection. """ try: response = self.rekognition_client.create_collection( CollectionId=collection_id) response['CollectionId'] = collection_id collection = RekognitionCollection(response, self.rekognition_client) logger.info("Created collection %s.", collection_id) except ClientError: logger.exception("Couldn't create collection %s.", collection_id) raise else: return collection def list_collections(self, max_results): """ Lists collections for the current account. :param max_results: The maximum number of collections to return. :return: The list of collections for the current account. """ try: response = self.rekognition_client.list_collections(MaxResults=max_results) collections = [ RekognitionCollection({'CollectionId': col_id}, self.rekognition_client) for col_id in response['CollectionIds']] except ClientError: logger.exception("Couldn't list collections.") raise else: return collections def usage_demo(): print('-'*88) print("Welcome to the Amazon Rekognition face collection demo!") print('-'*88) logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') rekognition_client = boto3.client('rekognition') images = [ RekognitionImage.from_file( '.media/pexels-agung-pandit-wiguna-1128316.jpg', rekognition_client, image_name='sitting'), RekognitionImage.from_file( '.media/pexels-agung-pandit-wiguna-1128317.jpg', rekognition_client, image_name='hopping'), RekognitionImage.from_file( '.media/pexels-agung-pandit-wiguna-1128318.jpg', rekognition_client, image_name='biking')] collection_mgr = RekognitionCollectionManager(rekognition_client) collection = collection_mgr.create_collection('doc-example-collection-demo') print(f"Created collection {collection.collection_id}:") pprint(collection.describe_collection()) print("Indexing faces from three images:") for image in images: collection.index_faces(image, 10) print("Listing faces in collection:") faces = collection.list_faces(10) for face in faces: pprint(face.to_dict()) input("Press Enter to continue.") print(f"Searching for faces in the collection that match the first face in the " f"list (Face ID: {faces[0].face_id}.") found_faces = collection.search_faces(faces[0].face_id, 80, 10) print(f"Found {len(found_faces)} matching faces.") for face in found_faces: pprint(face.to_dict()) input("Press Enter to continue.") print(f"Searching for faces in the collection that match the largest face in " f"{images[0].image_name}.") image_face, match_faces = collection.search_faces_by_image(images[0], 80, 10) print(f"The largest face in {images[0].image_name} is:") pprint(image_face.to_dict()) print(f"Found {len(match_faces)} matching faces.") for face in match_faces: pprint(face.to_dict()) input("Press Enter to continue.") collection.delete_collection() print('Thanks for watching!') print('-'*88) if __name__ == '__main__': usage_demo()
apache-2.0
4,713,168,682,652,689,000
38.12828
102
0.617167
false
4.346179
false
false
false
atarax82/lotto-project
project/monitor.py
1
2948
import os import sys #import time import signal import threading import atexit import queue _interval = 1.0 _times = {} _files = [] _running = False _queue = queue.Queue() _lock = threading.Lock() def _restart(path): _queue.put(True) prefix = 'monitor (pid=%d):' % os.getpid() print('%s Change detected to \'%s\'.' % (prefix, path), file=sys.stderr) print('%s Triggering process restart.' % prefix, file=sys.stderr) os.kill(os.getpid(), signal.SIGINT) def _modified(path): try: # If path doesn't denote a file and were previously # tracking it, then it has been removed or the file type # has changed so force a restart. If not previously # tracking the file then we can ignore it as probably # pseudo reference such as when file extracted from a # collection of modules contained in a zip file. if not os.path.isfile(path): return path in _times # Check for when file last modified. mtime = os.stat(path).st_mtime if path not in _times: _times[path] = mtime # Force restart when modification time has changed, even # if time now older, as that could indicate older file # has been restored. if mtime != _times[path]: return True except: # If any exception occured, likely that file has been # been removed just before stat(), so force a restart. return True return False def _monitor(): while 1: # Check modification times on all files in sys.modules. for module in list(sys.modules.values()): if not hasattr(module, '__file__'): continue path = getattr(module, '__file__') if not path: continue if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']: path = path[:-1] if _modified(path): return _restart(path) # Check modification times on files which have # specifically been registered for monitoring. for path in _files: if _modified(path): return _restart(path) # Go to sleep for specified interval. try: return _queue.get(timeout=_interval) except: pass _thread = threading.Thread(target=_monitor) _thread.setDaemon(True) def _exiting(): try: _queue.put(True) except: pass _thread.join() atexit.register(_exiting) def track(path): if not path in _files: _files.append(path) def start(interval=1.0): global _interval if interval < _interval: _interval = interval global _running _lock.acquire() if not _running: prefix = 'monitor (pid=%d):' % os.getpid() print('%s Starting change monitor.' % prefix, file=sys.stderr) _running = True _thread.start() _lock.release()
gpl-3.0
-7,386,364,848,896,357,000
25.097345
76
0.585142
false
4.140449
false
false
false
ESSolutions/ESSArch_Core
ESSArch_Core/agents/documents.py
1
1455
from elasticsearch_dsl import Date, InnerDoc, Keyword, Nested, Text from ESSArch_Core.agents.models import Agent from ESSArch_Core.search.documents import DocumentBase from ESSArch_Core.tags.documents import autocomplete_analyzer class AgentNameDocument(InnerDoc): main = Text() part = Text() description = Text() start_date = Date() end_date = Date() @classmethod def from_obj(cls, obj): doc = AgentNameDocument( main=obj.main, part=obj.part, description=obj.description, start_date=obj.start_date, end_date=obj.end_date, ) return doc class AgentDocument(DocumentBase): id = Keyword() task_id = Keyword() names = Nested(AgentNameDocument) start_date = Date() end_date = Date() @classmethod def get_model(cls): return Agent @classmethod def from_obj(cls, obj): if obj.task is None: task_id = None else: task_id = str(obj.task.pk) doc = AgentDocument( _id=str(obj.pk), id=str(obj.pk), task_id=task_id, names=[ AgentNameDocument.from_obj(name) for name in obj.names.iterator() ], start_date=obj.start_date, end_date=obj.end_date, ) return doc class Index: name = 'agent' analyzers = [autocomplete_analyzer]
gpl-3.0
3,981,038,037,303,409,700
23.661017
81
0.57457
false
3.869681
false
false
false
MasterGowen/moonrain
moonrain/projects/views.py
1
3432
import json from django.shortcuts import render, redirect from django.views.generic.edit import UpdateView, DeleteView from .models import Project from ..videos.models import Video, VideosSequence from ..videos.views import new_sequence, get_sequence from django.contrib.auth.decorators import login_required from django.http import Http404, HttpResponse from django.core.exceptions import ObjectDoesNotExist from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.core.context_processors import csrf from .forms import ProjectForm def projects_list_all(request): projects = [] for project in Project.objects.all(): if project.permission == 'public': projects.append(project) @login_required def for_users(request, project, projects): if project.permission == 'for_users': projects.append(project) for_users(request, project, projects) @login_required def for_staff(request, project, projects): if project.permission == 'for_staff': if request.user == project.author or str(request.user) in str(project.users()): projects.append(project) for_staff(request, project, projects) projects = list(reversed(projects)) paginator = Paginator(projects, 10) page = request.GET.get('page') try: projects = paginator.page(page) except PageNotAnInteger: projects = paginator.page(1) except EmptyPage: projects = paginator.page(paginator.num_pages) return render(request, 'projects/index.html', {'projects': projects, 'pages': range(1, (paginator.num_pages + 1))}) def detail(request, project_id): try: project = Project.objects.get(id=project_id) jsonSequence = json.loads(get_sequence(request, project)) except ObjectDoesNotExist: raise Http404 videos_ids = jsonSequence['sequence'].split(',') videos = [] for video_id in videos_ids: if video_id != 'None': video = Video.objects.get(id=video_id) videos.append(video) if project.permission == 'public': return render(request, 'projects/project.html', {'project': project, 'videos': videos}) elif project.permission == 'for_users' \ and request.user: return render(request, 'projects/project.html', {'project': project, 'videos': videos}) elif project.permission == 'for_staff' \ and request.user == project.author \ or str(request.user) \ in str(project.users()): return render(request, 'projects/project.html', {'project': project, 'videos': videos}) else: return HttpResponse(status=403) def new_project(request): if request.method == 'POST': form = ProjectForm(request.POST) if form.is_valid(): project = form.save(commit=False) project.author_id = request.user.id project.save() new_sequence(request, project.id) return redirect(project) args = {} args.update(csrf(request)) args['form'] = ProjectForm() return render(request, 'projects/new.html', args) class ProjectDelete(DeleteView): model = Project fields = [] success_url = '/projects/' class ProjectUpdate(UpdateView): model = Project fields = ['name', 'comments', 'tags', 'permission']
gpl-2.0
8,194,564,417,942,022,000
32.656863
119
0.648893
false
4.180268
false
false
false
tbetcke/PyBEM2D
examples/circscatt.py
1
1272
import pybem2d.core.bases as pcb import pybem2d.core.segments as pcs import pybem2d.core.quadrules as pcq import pybem2d.core.kernels as pck import pybem2d.core.mesh as pcm import pybem2d.core.assembly as pca import pybem2d.core.evaluation as pce import pybem2d.core.visualization as pcv import numpy as np k=10 nelems=50 dirs=np.array([1.0,0]) # Define the mesh circle=pcs.Arc(3,0,0,2*np.pi,1.0) d=pcm.Domain([circle]) mesh=pcm.Mesh([d]) mesh.discretize(nelems) quadrule=pcq.GaussQuadrature() # A standard Gauss Quadrature with default parameters mToB=pcb.Legendre.legendreBasis(mesh,2) # A basis of Legendre polynomials of degree 2 kernel=pck.AcousticCombined(k,k) # The combined potential layer singleLayer=pck.AcousticSingleLayer(k) assembly=pca.Assembly(mToB,quadrule) rhsfun=lambda t,x,n: 2j*k*np.exp(1j*k*(dirs[0]*x[0]+dirs[1]*x[1]))*(dirs[0]*n[0]+dirs[1]*n[1]-1) rhs=assembly.projFun([rhsfun]) mKernel=assembly.getKernel(kernel) mIdentity=assembly.getIdentity() op=mIdentity+2*mKernel print op.shape coeffs=np.linalg.solve(op,rhs) #ev=pce.Evaluator(mToB,singleLayer,quadrule) #v=pcv.Visualizer(ev,[-3,5,-3,3],200,200,incWave=lambda x: np.exp(1j*k*(x[0]*dirs[0]+x[1]*dirs[1]))) #v.fullField(-coeffs[:,0]) x,f=pce.evalDensity(mToB,coeffs[:,0])
mit
7,395,326,172,782,290,000
22.127273
100
0.750786
false
2.359926
false
true
false
mjafin/bcbio-nextgen
bcbio/variation/bedutils.py
1
7691
"""Utilities for manipulating BED files. """ import os import shutil import sys import subprocess import toolz as tz from bcbio import utils from bcbio.bam import ref from bcbio.distributed.transaction import file_transaction from bcbio.pipeline import config_utils from bcbio.pipeline import datadict as dd from bcbio.provenance import do from bcbio.variation import vcfutils def get_sort_cmd(): """Retrieve GNU coreutils sort command, using version-sort if available. Recent versions of sort have alpha-numeric sorting, which provides more natural sorting of chromosomes (chr1, chr2) instead of (chr1, chr10). This also fixes versions of sort, like 8.22 in CentOS 7.1, that have broken sorting without version sorting specified. https://github.com/chapmanb/bcbio-nextgen/issues/624 https://github.com/chapmanb/bcbio-nextgen/issues/1017 """ has_versionsort = subprocess.check_output("sort --help | grep version-sort; exit 0", shell=True).strip() if has_versionsort: return "sort -V" else: return "sort" def check_bed_contigs(in_file, data): """Ensure BED file contigs match the reference genome. """ contigs = set([]) with utils.open_gzipsafe(in_file) as in_handle: for line in in_handle: if not line.startswith(("#", "track", "browser")) and line.strip(): contigs.add(line.split()[0]) ref_contigs = set([x.name for x in ref.file_contigs(dd.get_ref_file(data))]) if len(contigs - ref_contigs) / float(len(contigs)) > 0.25: raise ValueError("Contigs in BED file %s not in reference genome:\n %s\n" % (in_file, list(contigs - ref_contigs)) + "This is typically due to chr1 versus 1 differences in BED file and reference.") def clean_file(in_file, data, prefix="", bedprep_dir=None): """Prepare a clean sorted input BED file without headers """ if in_file: if not bedprep_dir: bedprep_dir = utils.safe_makedir(os.path.join(data["dirs"]["work"], "bedprep")) out_file = os.path.join(bedprep_dir, "%s%s" % (prefix, os.path.basename(in_file))).replace(".gz", "") if not utils.file_uptodate(out_file, in_file): check_bed_contigs(in_file, data) with file_transaction(data, out_file) as tx_out_file: py_cl = os.path.join(os.path.dirname(sys.executable), "py") cat_cmd = "zcat" if in_file.endswith(".gz") else "cat" sort_cmd = get_sort_cmd() cmd = ("{cat_cmd} {in_file} | grep -v ^track | grep -v ^browser | " "grep -v ^# | " "{py_cl} -x 'bcbio.variation.bedutils.remove_bad(x)' | " "{sort_cmd} -k1,1 -k2,2n > {tx_out_file}") do.run(cmd.format(**locals()), "Prepare cleaned BED file", data) vcfutils.bgzip_and_index(out_file, data.get("config", {}), remove_orig=False) return out_file def sort_merge(in_file, data): """Sort and merge a BED file, collapsing gene names. """ out_file = "%s-sort.bed" % os.path.splitext(in_file)[0] if not utils.file_uptodate(out_file, in_file): with file_transaction(data, out_file) as tx_out_file: cat_cmd = "zcat" if in_file.endswith(".gz") else "cat" sort_cmd = get_sort_cmd() cmd = ("{cat_cmd} {in_file} | {sort_cmd} -k1,1 -k2,2n | " "bedtools merge -i - -c 4 -o distinct > {tx_out_file}") do.run(cmd.format(**locals()), "Sort BED file", data) return out_file def remove_bad(line): """Remove non-increasing BED lines which will cause variant callers to choke. """ parts = line.strip().split("\t") if line.strip() and len(parts) > 2 and int(parts[2]) > int(parts[1]): return line else: return None def merge_overlaps(in_file, data, distance=None, out_dir=None): """Merge bed file intervals to avoid overlapping regions. Overlapping regions (1:1-100, 1:90-100) cause issues with callers like FreeBayes that don't collapse BEDs prior to using them. """ config = data["config"] if in_file: bedtools = config_utils.get_program("bedtools", config, default="bedtools") work_dir = tz.get_in(["dirs", "work"], data) if out_dir: bedprep_dir = out_dir elif work_dir: bedprep_dir = utils.safe_makedir(os.path.join(work_dir, "bedprep")) else: bedprep_dir = os.path.dirname(in_file) out_file = os.path.join(bedprep_dir, "%s-merged.bed" % (utils.splitext_plus(os.path.basename(in_file))[0])) if not utils.file_uptodate(out_file, in_file): with file_transaction(data, out_file) as tx_out_file: distance = "-d %s" % distance if distance else "" cmd = "{bedtools} merge {distance} -i {in_file} > {tx_out_file}" do.run(cmd.format(**locals()), "Prepare merged BED file", data) vcfutils.bgzip_and_index(out_file, data["config"], remove_orig=False) return out_file def population_variant_regions(items): """Retrieve the variant region BED file from a population of items. If tumor/normal, return the tumor BED file. If a population, return the BED file covering the most bases. """ import pybedtools if len(items) == 1: return dd.get_variant_regions(items[0]) else: paired = vcfutils.get_paired(items) if paired: return dd.get_variant_regions(paired.tumor_data) else: vrs = [] for data in items: vr_bed = dd.get_variant_regions(data) if vr_bed: vrs.append((pybedtools.BedTool(vr_bed).total_coverage(), vr_bed)) vrs.sort(reverse=True) if vrs: return vrs[0][1] def clean_inputs(data): """Clean BED input files to avoid overlapping segments that cause downstream issues. Per-merges inputs to avoid needing to call multiple times during later parallel steps. """ clean_vr = clean_file(utils.get_in(data, ("config", "algorithm", "variant_regions")), data) merged_vr = merge_overlaps(clean_vr, data) data["config"]["algorithm"]["variant_regions"] = clean_vr data["config"]["algorithm"]["variant_regions_merged"] = merged_vr return data def combine(in_files, out_file, config): """Combine multiple BED files into a single output. """ if not utils.file_exists(out_file): with file_transaction(config, out_file) as tx_out_file: with open(tx_out_file, "w") as out_handle: for in_file in in_files: with open(in_file) as in_handle: shutil.copyfileobj(in_handle, out_handle) return out_file def intersect_two(f1, f2, work_dir, data): """Intersect two regions, handling cases where either file is not present. """ f1_exists = f1 and utils.file_exists(f1) f2_exists = f2 and utils.file_exists(f2) if not f1_exists and not f2_exists: return None elif f1_exists and not f2_exists: return f1 elif f2_exists and not f1_exists: return f2 else: out_file = os.path.join(work_dir, "%s-merged.bed" % (utils.splitext_plus(os.path.basename(f1))[0])) if not utils.file_exists(out_file): with file_transaction(data, out_file) as tx_out_file: cmd = "bedtools intersect -a {f1} -b {f2} > {tx_out_file}" do.run(cmd.format(**locals()), "Intersect BED files", data) return out_file
mit
-970,185,428,665,828,700
41.727778
115
0.606683
false
3.415187
true
false
false
JeroenZegers/Nabu-MSSS
nabu/neuralnetworks/loss_computers/ms_loss.py
1
2597
"""@file ms_loss.py contains the MsLoss. Temporary naming of file and class""" import loss_computer import tensorflow as tf class MsLoss(loss_computer.LossComputer): """A loss computer that calculates the loss""" def __call__(self, targets, logits, seq_length): # target is actually only required for it's shape to derive the number of active speakers multi_targets = targets['multi_targets'] nr_act_spk = multi_targets.get_shape()[-1] # seq_length = seq_length['bin_est'] logits = logits['act_logit'] logits = tf.squeeze(logits, axis=-1) nr_spk = logits.get_shape()[1] batch_size = logits.get_shape()[0] if self.lossconf['activation'] == 'sigmoid': logits = tf.sigmoid(logits) else: raise BaseException('Other activations not yet implemented') if len(logits.get_shape()) != 3: raise BaseException('Hardcoded some stuff for 3 dimensions') second_dim = logits.get_shape()[1] seq_length = seq_length['features'] # have to do this better max_len = tf.shape(logits)[-1] tmp = [] for utt_ind in range(batch_size): tmp.append( tf.expand_dims( tf.concat( [tf.ones([second_dim, seq_length[utt_ind]]), tf.zeros([second_dim, max_len - seq_length[utt_ind]])], -1), 0)) # seq_length_mask[utt_ind, :seq_length[utt_ind]] = 1 seq_length_mask = tf.concat(tmp, 0) logits = logits * seq_length_mask if self.lossconf['av_time'] == 'True': logits = tf.reduce_sum(logits, 2) logits = tf.divide(logits, tf.expand_dims(tf.to_float(seq_length), -1)) targets = tf.concat([tf.ones([batch_size, nr_act_spk]), tf.zeros([batch_size, nr_spk-nr_act_spk])], -1) loss = tf.reduce_sum(tf.square(logits - targets)) norm = tf.to_float(batch_size * nr_spk) return loss, norm def oldcall(self, targets, logits, seq_length): # target is actually only required for it's shape to derive the number of active speakers multi_targets = targets['multi_targets'] nr_act_spk = multi_targets.get_shape()[-1] # seq_length = seq_length['bin_est'] logits = logits['act_logit'] logits = tf.squeeze(logits, axis=-1) nr_spk = logits.get_shape()[1] batch_size = logits.get_shape()[0] if self.lossconf['activation'] == 'sigmoid': logits = tf.sigmoid(logits) else: raise BaseException('Other activations not yet implemented') if self.lossconf['av_time'] == 'True': logits = tf.reduce_mean(logits, 2) targets = tf.concat([tf.ones([batch_size, nr_act_spk]), tf.zeros([batch_size, nr_spk-nr_act_spk])], -1) loss = tf.reduce_sum(tf.square(logits - targets)) norm = tf.to_float(batch_size * nr_spk) return loss, norm
mit
8,943,644,888,906,579,000
32.727273
115
0.672699
false
2.901676
false
false
false
ikosenn/cray-cray
fummy.py
1
2626
""" Author: ikosenn This is a program to eliminate stale git branches. It checks last commits and based on the staleness threshold eliminates all stale branches Another CL function is provided to eliminate all available branches. You can also remove all branches that have already been merged to the main branch """ import os from datetime import datetime import click from sarge import capture_stdout import pytz from dateutil.parser import parse DEFAULT_BRANCH = 'master' # helper functions def get_time_difference(time): """ Computes the difference with todays time """ timezone = "Africa/Nairobi" branch_time = parse(time) current_time = datetime.now(pytz.timezone(timezone)) diff_days = (current_time - branch_time) return diff_days.days def cwd(path): os.chdir(path) @click.command() @click.option( '--threshold', '-t', default=10, prompt='What number of days should the threshold be? [10 days]') @click.option( 'branches', '--branch', '-b', default=DEFAULT_BRANCH, prompt='What branches should be excluded? [master]', multiple=True) @click.option( '--path', '-p', prompt='File path to the git repo?', type=click.Path(exists=True)) def fummy(threshold, branches, path): cwd(path) all_branches = capture_stdout('git branch') # remove spaces and any blank spaces temp = all_branches.stdout.text.replace( '*', '').replace(' ', '').split('\n') for branch in temp: if branch and branch not in branches: click.echo('Processing branch: {}'.format(branch)) p = capture_stdout( 'git show {} --format="%cI" --no-patch'.format(branch)) diff_days = get_time_difference(p.stdout.text) if diff_days > threshold: click.echo('Deleting {}'.format(branch)) p = capture_stdout( 'git branch -D {}'.format(branch)) click.echo(p.stdout.text) @click.command() @click.option('--filename', type=click.Path(exists=True)) @click.option('--default', '-d', default=DEFAULT_BRANCH) def kill_merged(default): """ Start by checking out to the master branch and then finding out the branches already merged to master and eliminating the buggage """ # git branch --merged master pass @click.group() def cli(): """ Command Line Interface tools loader for ``fummy`` These utilities help with deleting git branches older than the specified period """ pass cli.add_command(fummy) if __name__ == '__main__': cli()
mit
-3,121,794,362,118,456,300
25
76
0.639756
false
3.978788
false
false
false
AutorestCI/azure-sdk-for-python
azure-mgmt-monitor/azure/mgmt/monitor/operations/action_groups_operations.py
1
19086
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. # # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is # regenerated. # -------------------------------------------------------------------------- import uuid from msrest.pipeline import ClientRawResponse from .. import models class ActionGroupsOperations(object): """ActionGroupsOperations operations. :param client: Client for service requests. :param config: Configuration of service client. :param serializer: An object model serializer. :param deserializer: An objec model deserializer. :ivar api_version: Client Api Version. Constant value: "2017-04-01". """ def __init__(self, client, config, serializer, deserializer): self._client = client self._serialize = serializer self._deserialize = deserializer self.api_version = "2017-04-01" self.config = config def create_or_update( self, resource_group_name, action_group_name, action_group, custom_headers=None, raw=False, **operation_config): """Create a new action group or update an existing one. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param action_group_name: The name of the action group. :type action_group_name: str :param action_group: The action group to create or use for the update. :type action_group: ~azure.mgmt.monitor.models.ActionGroupResource :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response :param operation_config: :ref:`Operation configuration overrides<msrest:optionsforoperations>`. :return: ActionGroupResource or ClientRawResponse if raw=true :rtype: ~azure.mgmt.monitor.models.ActionGroupResource or ~msrest.pipeline.ClientRawResponse :raises: :class:`ErrorResponseException<azure.mgmt.monitor.models.ErrorResponseException>` """ # Construct URL url = '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/actionGroups/{actionGroupName}' path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'actionGroupName': self._serialize.url("action_group_name", action_group_name, 'str'), 'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str') } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str') # Construct headers header_parameters = {} header_parameters['Content-Type'] = 'application/json; charset=utf-8' if self.config.generate_client_request_id: header_parameters['x-ms-client-request-id'] = str(uuid.uuid1()) if custom_headers: header_parameters.update(custom_headers) if self.config.accept_language is not None: header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str') # Construct body body_content = self._serialize.body(action_group, 'ActionGroupResource') # Construct and send request request = self._client.put(url, query_parameters) response = self._client.send( request, header_parameters, body_content, **operation_config) if response.status_code not in [200, 201]: raise models.ErrorResponseException(self._deserialize, response) deserialized = None if response.status_code == 200: deserialized = self._deserialize('ActionGroupResource', response) if response.status_code == 201: deserialized = self._deserialize('ActionGroupResource', response) if raw: client_raw_response = ClientRawResponse(deserialized, response) return client_raw_response return deserialized def get( self, resource_group_name, action_group_name, custom_headers=None, raw=False, **operation_config): """Get an action group. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param action_group_name: The name of the action group. :type action_group_name: str :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response :param operation_config: :ref:`Operation configuration overrides<msrest:optionsforoperations>`. :return: ActionGroupResource or ClientRawResponse if raw=true :rtype: ~azure.mgmt.monitor.models.ActionGroupResource or ~msrest.pipeline.ClientRawResponse :raises: :class:`ErrorResponseException<azure.mgmt.monitor.models.ErrorResponseException>` """ # Construct URL url = '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/actionGroups/{actionGroupName}' path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'actionGroupName': self._serialize.url("action_group_name", action_group_name, 'str'), 'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str') } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str') # Construct headers header_parameters = {} header_parameters['Content-Type'] = 'application/json; charset=utf-8' if self.config.generate_client_request_id: header_parameters['x-ms-client-request-id'] = str(uuid.uuid1()) if custom_headers: header_parameters.update(custom_headers) if self.config.accept_language is not None: header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str') # Construct and send request request = self._client.get(url, query_parameters) response = self._client.send(request, header_parameters, **operation_config) if response.status_code not in [200]: raise models.ErrorResponseException(self._deserialize, response) deserialized = None if response.status_code == 200: deserialized = self._deserialize('ActionGroupResource', response) if raw: client_raw_response = ClientRawResponse(deserialized, response) return client_raw_response return deserialized def delete( self, resource_group_name, action_group_name, custom_headers=None, raw=False, **operation_config): """Delete an action group. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param action_group_name: The name of the action group. :type action_group_name: str :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response :param operation_config: :ref:`Operation configuration overrides<msrest:optionsforoperations>`. :return: None or ClientRawResponse if raw=true :rtype: None or ~msrest.pipeline.ClientRawResponse :raises: :class:`ErrorResponseException<azure.mgmt.monitor.models.ErrorResponseException>` """ # Construct URL url = '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/actionGroups/{actionGroupName}' path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'actionGroupName': self._serialize.url("action_group_name", action_group_name, 'str'), 'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str') } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str') # Construct headers header_parameters = {} header_parameters['Content-Type'] = 'application/json; charset=utf-8' if self.config.generate_client_request_id: header_parameters['x-ms-client-request-id'] = str(uuid.uuid1()) if custom_headers: header_parameters.update(custom_headers) if self.config.accept_language is not None: header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str') # Construct and send request request = self._client.delete(url, query_parameters) response = self._client.send(request, header_parameters, **operation_config) if response.status_code not in [200, 204]: raise models.ErrorResponseException(self._deserialize, response) if raw: client_raw_response = ClientRawResponse(None, response) return client_raw_response def list_by_subscription_id( self, custom_headers=None, raw=False, **operation_config): """Get a list of all action groups in a subscription. :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response :param operation_config: :ref:`Operation configuration overrides<msrest:optionsforoperations>`. :return: An iterator like instance of ActionGroupResource :rtype: ~azure.mgmt.monitor.models.ActionGroupResourcePaged[~azure.mgmt.monitor.models.ActionGroupResource] :raises: :class:`ErrorResponseException<azure.mgmt.monitor.models.ErrorResponseException>` """ def internal_paging(next_link=None, raw=False): if not next_link: # Construct URL url = '/subscriptions/{subscriptionId}/providers/microsoft.insights/actionGroups' path_format_arguments = { 'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str') } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str') else: url = next_link query_parameters = {} # Construct headers header_parameters = {} header_parameters['Content-Type'] = 'application/json; charset=utf-8' if self.config.generate_client_request_id: header_parameters['x-ms-client-request-id'] = str(uuid.uuid1()) if custom_headers: header_parameters.update(custom_headers) if self.config.accept_language is not None: header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str') # Construct and send request request = self._client.get(url, query_parameters) response = self._client.send( request, header_parameters, **operation_config) if response.status_code not in [200]: raise models.ErrorResponseException(self._deserialize, response) return response # Deserialize response deserialized = models.ActionGroupResourcePaged(internal_paging, self._deserialize.dependencies) if raw: header_dict = {} client_raw_response = models.ActionGroupResourcePaged(internal_paging, self._deserialize.dependencies, header_dict) return client_raw_response return deserialized def list_by_resource_group( self, resource_group_name, custom_headers=None, raw=False, **operation_config): """Get a list of all action groups in a resource group. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response :param operation_config: :ref:`Operation configuration overrides<msrest:optionsforoperations>`. :return: An iterator like instance of ActionGroupResource :rtype: ~azure.mgmt.monitor.models.ActionGroupResourcePaged[~azure.mgmt.monitor.models.ActionGroupResource] :raises: :class:`ErrorResponseException<azure.mgmt.monitor.models.ErrorResponseException>` """ def internal_paging(next_link=None, raw=False): if not next_link: # Construct URL url = '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/actionGroups' path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str') } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str') else: url = next_link query_parameters = {} # Construct headers header_parameters = {} header_parameters['Content-Type'] = 'application/json; charset=utf-8' if self.config.generate_client_request_id: header_parameters['x-ms-client-request-id'] = str(uuid.uuid1()) if custom_headers: header_parameters.update(custom_headers) if self.config.accept_language is not None: header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str') # Construct and send request request = self._client.get(url, query_parameters) response = self._client.send( request, header_parameters, **operation_config) if response.status_code not in [200]: raise models.ErrorResponseException(self._deserialize, response) return response # Deserialize response deserialized = models.ActionGroupResourcePaged(internal_paging, self._deserialize.dependencies) if raw: header_dict = {} client_raw_response = models.ActionGroupResourcePaged(internal_paging, self._deserialize.dependencies, header_dict) return client_raw_response return deserialized def enable_receiver( self, resource_group_name, action_group_name, receiver_name, custom_headers=None, raw=False, **operation_config): """Enable a receiver in an action group. This changes the receiver's status from Disabled to Enabled. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param action_group_name: The name of the action group. :type action_group_name: str :param receiver_name: The name of the receiver to resubscribe. :type receiver_name: str :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response :param operation_config: :ref:`Operation configuration overrides<msrest:optionsforoperations>`. :return: None or ClientRawResponse if raw=true :rtype: None or ~msrest.pipeline.ClientRawResponse :raises: :class:`ErrorResponseException<azure.mgmt.monitor.models.ErrorResponseException>` """ enable_request = models.EnableRequest(receiver_name=receiver_name) # Construct URL url = '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/actionGroups/{actionGroupName}/subscribe' path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'actionGroupName': self._serialize.url("action_group_name", action_group_name, 'str'), 'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str') } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str') # Construct headers header_parameters = {} header_parameters['Content-Type'] = 'application/json; charset=utf-8' if self.config.generate_client_request_id: header_parameters['x-ms-client-request-id'] = str(uuid.uuid1()) if custom_headers: header_parameters.update(custom_headers) if self.config.accept_language is not None: header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str') # Construct body body_content = self._serialize.body(enable_request, 'EnableRequest') # Construct and send request request = self._client.post(url, query_parameters) response = self._client.send( request, header_parameters, body_content, **operation_config) if response.status_code not in [200, 409]: raise models.ErrorResponseException(self._deserialize, response) if raw: client_raw_response = ClientRawResponse(None, response) return client_raw_response
mit
-7,601,957,632,301,776,000
46.125926
152
0.648643
false
4.537803
true
false
false
molly/women-social-reformers-on-wikipedia
gather.py
1
2451
# Copyright (c) 2015–2016 Molly White # # Permission is hereby granted, free of charge, to any person obtaining a copy of this software # and associated documentation files (the "Software"), to deal in the Software without # restriction, including without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all copies or # substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING # BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import requests def load_list(filename): """Load the list of women from file.""" with open(filename, "r", encoding="utf-8") as f: lines = f.readlines() return format_names([line.strip() for line in lines]) def format_names(women): """Format the names for searching.""" formatted = [] for name in women: split = name.split(",") formatted.append(" ".join([split[1].strip(), split[0].strip()]) if len(split) == 2 else split[0].strip()) return formatted def search(women): """Do the search on the list of women.""" for woman in women: find_page(woman) def find_page(search_term): """Attempt to find a matching Wikipedia article for the given woman.""" api_params = {"action": "opensearch", "search": search_term, "limit": 1, "namespace": 0, "format": "json"} r = requests.get(api_url, params=api_params, headers=headers) if r: print(r.json()) else: print(None) def main(): women = load_list("women.txt") search(women) if __name__ == "__main__": headers = {'user-agent': "women-social-reformers-on-wikipedia: https://github.com/molly/women-social-reformers-" "on-wikipedia"} api_url = "https://en.wikipedia.org/w/api.php" main()
mit
6,424,813,447,681,501,000
36.692308
116
0.665578
false
3.905901
false
false
false
plin1112/pysimm
pysimm/cassandra.py
1
67253
# ****************************************************************************** # pysimm.cassandra module # ****************************************************************************** # # ****************************************************************************** # License # ****************************************************************************** # The MIT License (MIT) # # Copyright (c) 2017 Alexander Demidov, Michael E. Fortunato, Coray M. Colina # # 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. from StringIO import StringIO from subprocess import call, Popen, PIPE import os import re import numpy as np import random import logging import types from collections import Iterable, OrderedDict from pysimm import system from string import ascii_uppercase from pydoc import locate DATA_PATH = os.path.realpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../dat/csndra_data')) KCALMOL_2_K = 503.22271716452 CASSANDRA_EXEC = os.environ.get('CASSANDRA_EXEC') # Creating a logger instance and send its output to console 'deafault' logging.basicConfig(level=logging.INFO, datefmt='%H:%M:%S', format='%(asctime)s [%(levelname)s]: %(message)s') DEFAULT_PARAMS = { 'Temperature_Info': 300, 'Pressure_Info': 1, 'Rcutoff_Low': 0.1 } class MCSimulation(object): """pysimm.cassandra.MCSimulation Object containing the settings and the logic necessary to partially set-up an abstract Monte Carlo simulation to be submitted to the CASSANDRA software. The object also will include the simulation results once the simulations are finished. Attributes: mc_sst (:class:`~pysimm.cassandra.McSystem`) : describes all molecules to be inserted by CASSANDRA init_sst (:class:`~pysimm.system.System`) : describes the optional initial fixed molecular configuration for MC simulations (default: empty cubic box with 1 nm side length). If the particles in the system are not attributed with the flag `is_fixed` all of them are considered to be fixed, and will be marked with this flag, otherwise all particles with is_fixed=False will be removed. Keyword Args: out_folder (str) : the relative path of the simulation results (all .dat, .mcf, as well as .chk, ... files will go there). If the folder does not exist it will be created with 0755 permissions. props_file (str) : the name of the .inp file. Note: Other keyword arguments that are accepted are the GCMC simulation settings. The keywords of the settings are the same as they are described in CASSANDRA specification but without # symbol. **For example**: the keyword argument `Run_Name='my_simulation'` will set `#Run_Name` setting in CASSANDRA input file to `my_simulation` value Parameters: props (dictionary) : include all simulation settings to be written to the CASSANDRA .inp file input (str) : text stream that will be written to the CASSANDRA .inp file tot_sst (:class:`~pysimm.system.System`) : object containing the results of CASSANDRA simulations """ def __init__(self, mc_sst=None, init_sst=None, **kwargs): global DATA_PATH # Initializing CASSANDRA input stream, empty at the beginning self.input = '' # Initializing dictionary that contains records that directly will be sent to the .inp file self.props = OrderedDict() self.logger = logging.getLogger('MC Simulation') # Reading default properties of the GCMC simulations def_dat = Cassandra(system.System()).read_input(os.path.join(DATA_PATH, 'mc_default.inp')) tmp = kwargs.get('out_folder') # Folder for the results and temporary files if tmp: self.out_folder = tmp if os.path.isabs(tmp): self.out_folder = os.path.relpath(tmp) else: self.out_folder = os.getcwd() if not os.path.exists(self.out_folder): os.makedirs(self.out_folder, mode=0755) prefix = kwargs.get('Run_Name', def_dat['Run_Name']) self.props['Run_Name'] = InpSpec('Run_Name', os.path.join(self.out_folder, prefix), '') self.props_file = os.path.join(self.out_folder, kwargs.get('props_file', '')) # Simple (one-value) dynamic properties self.props['Temperature_Info'] = InpSpec('Temperature_Info', kwargs.get('Temperature_Info'), DEFAULT_PARAMS['Temperature_Info']) self.props['Pair_Energy'] = InpSpec('Pair_Energy', kwargs.get('Pair_Energy'), def_dat['Pair_Energy']) self.props['Rcutoff_Low'] = InpSpec('Rcutoff_Low', kwargs.get('Rcutoff_Low'), def_dat['Rcutoff_Low']) self.props['Mixing_Rule'] = InpSpec('Mixing_Rule', kwargs.get('Mixing_Rule'), def_dat['Mixing_Rule']) self.props['Seed_Info'] = InpSpec('Seed_Info', kwargs.get('Seed_Info'), [random.randint(int(1e+7), int(1e+8 - 1)), random.randint(int(1e+7), int(1e+8 - 1))]) # Multiple-value one/many line dynamic properties self.props['Run_Type'] = InpSpec('Run_Type', kwargs.get('Run_Type'), def_dat['Run_Type']) self.props['Charge_Style'] = InpSpec('Charge_Style', kwargs.get('Charge_Style'), def_dat['Charge_Style']) self.props['VDW_Style'] = InpSpec('VDW_Style', kwargs.get('VDW_Style'), def_dat['VDW_Style']) self.props['Simulation_Length_Info'] = InpSpec('Simulation_Length_Info', kwargs.get('Simulation_Length_Info'), def_dat['Simulation_Length_Info'], **{'write_headers': True, 'new_line': True}) self.props['CBMC_Info'] = InpSpec('CBMC_Info', kwargs.get('CBMC_Info'), def_dat['CBMC_Info'], **{'write_headers': True, 'new_line': True}) self.props['Box_Info'] = InpSpec('Box_Info', kwargs.get('Box_Info'), def_dat['Box_Info'], **{'new_line': True}) self.props['Property_Info 1'] = InpSpec('Property_Info 1', kwargs.get('Property_Info'), None, **{'new_line': True}) # Setting the simulation total system if init_sst: self.tot_sst = init_sst.copy() self.tot_sst.center('box', [0, 0, 0], True) # the center of the calculation box should be at origin else: self.logger.warning('The frame generating system for Monte-Carlo simulations is not set. ' 'Creating empty cubic box of 1 nm size') self.tot_sst = system.System() self.tot_sst.forcefield = 'trappe/amber' self.tot_sst.dim = system.Dimension(dx=10, dy=10, dz=10) # Molecule configuration files describing all species of the system. # They are **absolutely** needed to start calculation mol_files = OrderedDict() # Some necessary verification of obtained system # TODO: check the forcefield to be sure that it is claas 1 if False: self.logger.error('CASSANDRA supports only 1-st class force fields') exit(1) self.tot_sst.zero_charge() # the sum of the charges should necessary be 0 # Creating the system of fixed molecules self.fxd_sst_mcfile = None self.fxd_sst = kwargs.get('fixed_sst') if self.tot_sst.particles: tmp = self.tot_sst.copy() for p in tmp.particles: if not p.is_fixed: tmp.particles.remove(p.tag) tmp.remove_spare_bonding() self.fxd_sst = tmp self.fxd_sst_mcfile = os.path.join(self.out_folder, 'fixed_syst.mcf') mol_files['file1'] = [self.fxd_sst_mcfile, 1] # Setting up the Monte Carlo system self.mc_sst = mc_sst if mc_sst: mc_sst.file_store = self.out_folder mol_files = mc_sst.update_props(mol_files) if kwargs.get('Molecule_Files'): mol_files = OrderedDict(sorted(kwargs.get('Molecule_Files').items())) # Raising an error and stop execution if no MCF information in one or another way is provided if (mc_sst is None) and (not kwargs.get('Molecule_Files')): self.logger.error('The molecular configuration files of gas molecules for simulation are not set. ' 'Nothing to simulate. Exiting...') exit(0) self._n_spec = len(mol_files) self.props['Nbr_Species'] = InpSpec('Nbr_Species', self._n_spec, self._n_spec) self.props['Molecule_Files'] = InpSpec('Molecule_Files', mol_files, None, **{'new_line': True}) # Synchronzing "start type" .inp record self.fxd_sst_xyz = '' pops_list = [0] * self._n_spec start_type = 'make_config' if self.fxd_sst: pops_list[0] = 1 self.fxd_sst_xyz = os.path.join(self.out_folder, 'fixed_syst.xyz') start_type = 'read_config' start_conf_dict = OrderedDict([('start_type', start_type), ('species', pops_list), ('file_name', self.fxd_sst_xyz)]) self.props['Start_Type'] = InpSpec('Start_Type', kwargs.get('Start_Type'), start_conf_dict) # Synchronzing Fragment files: frag_files = OrderedDict() if mc_sst: mc_sst.temperature = self.props['Temperature_Info'].value frag_files = mc_sst.update_frag_record(frag_files) if kwargs.get('Fragment_Files'): frag_files = OrderedDict(sorted(kwargs.get('Fragment_Files').items())) if (mc_sst is None) and (not kwargs.get('Fragment_Files')): self.logger.error('Cannot set the fragment files of gas molecules for simulation') exit(1) self.props['Fragment_Files'] = InpSpec('Fragment_Files', frag_files, None, **{'new_line': True}) def write(self): """pysimm.cassandra.MCSimulation.write Iterates through the :class:`~MCSimulation.props` dictionary creating the text for correct CASSANDRA input """ for key in self.props.keys(): if self.props[key].value is not None: self.input += '{:}\n'.format(self.props[key].to_string()) self.input += '\nEND' # Initializing output stream self.logger.info('Writing CASSANDRA .inp file to "{:}"...'.format(self.props_file)) out_stream = open(self.props_file, 'w') out_stream.write('{:}'.format(self.input)) out_stream.close() self.logger.info('File: "{:}" was created sucsessfully'.format(self.props_file)) def group_by_id(self, group_key='matrix'): """pysimm.cassandra.MCSimulation.group_by_id Method groups the atoms of the system :class:`~MCSimulation.tot_sst` by a certain property. Will iterate through all atoms in the system and return indexes of only those atoms that match the property. Currently supports 3 properties defined by the input keyword argument argument. Keyword Args: group_key (str): text constant defines the property to match. Possible keywords are: (1) `matrix` -- (default) indexes of the atoms in :obj:`~MCSimulation.fxd_sst` (2) `rigid` -- indexes of all atoms that have rigid atomic bonds. It is assumed here that rigid and nonrigid atoms can interact only through intermolecular forces (3) `nonrigid` -- opposite of previous, indexes of all atoms that have nonrigid atomic bonds Returns: str: string in format `a1:b1 a2:b2 ...` where all indexes inside `[ak, bk]` belongs to the selected group and array of the form `[[a1, b1], [a2, b2], ...]` """ fxd_sst_idxs = [] if self.fxd_sst: fxd_sst_idxs = range(1, len(self.fxd_sst.particles) + 1) # Behaviour depending on type of particles to check check = lambda x: x if group_key.lower() == 'nonrigid': check = lambda x: not x.is_rigid elif group_key.lower() == 'rigid': check = lambda x: x.is_rigid elif group_key.lower() == 'matrix': check = lambda x: x.tag in fxd_sst_idxs idx_array = [[-1, -1]] for p in self.tot_sst.particles: if check(p): if idx_array[-1][0] > 0: if abs(p.tag - idx_array[-1][1]) > 1: idx_array.append([p.tag, p.tag]) else: idx_array[-1][1] = p.tag else: idx_array[-1] = [p.tag, p.tag] idx_string = '' for t in idx_array: if t[1] - t[0] > 1: idx_string += str(t[0]) + ':' + str(t[1]) + ' ' return idx_string, idx_array def upd_simulation(self): """pysimm.cassandra.MCSimulation.upd_simulation Updates the :class:`~MCSimulation.tot_sst` field using the `MCSimulation.props['Run_Name'].chk` file. Will try to parse the checkpoint file and read the coordinates of the molecules inserted by CASSANDRA. If neither of the molecules from the :class:`~MCSimulation.mc_sst` can be fit to the text that was read the method will raise an exception. The fitting method: :class:`~McSystem.make_system` assumes that different molecules inserted by CASSANDRA have the same order of the atoms. """ fname = '{:}{:}'.format(self.props['Run_Name'].value, '.chk') self.logger.info('Updating MC system from the CASSANDRA {:} file...'.format(fname)) if os.path.isfile(fname): try: with open(fname, 'r') as inp: lines = inp.read() # Define the starting index of the lines with inserted atoms start_ind = lines.find('total number of molecules') end_ind = start_ind + lines[start_ind:-1].find('****', 1) count_info = lines[start_ind:end_ind].split('\n') offset = 1 if self.fxd_sst: tmp = count_info[1].split() offset += int(tmp[1]) * len(self.fxd_sst.particles) # Grab the lines with inserted atoms start_ind = lines.find('coordinates for all the boxes') all_coord_lines = lines[start_ind:-1].split('\n') inp.close() gas_lines = all_coord_lines[offset:] if len(gas_lines) > 0: if self.fxd_sst: self.tot_sst = self.fxd_sst.copy() self.tot_sst.add(self.mc_sst.make_system(gas_lines), change_dim=False) self.logger.info('Simulation system successfully updated') else: self.logger.info('Final MC configuration has 0 new particles the initial system remains the same') except IndexError: self.logger.error('Cannot fit the molecules from the CASSANDRA file to the PySIMM system') else: self.logger.error('Cannot find the CASSANDRA checkpoint file to update simulation. ' 'Probably it cannot be written by CASSANDRA to the place you specified') def __check_params__(self): """pysimm.cassandra.MCSimulation.__check_params__ Private method designed for update the fields of the simulation object to make them conformed with each other """ # Sync the simulation box parameters dx, dy, dz = self.tot_sst.dim.size() if (dx == dy) and (dy == dz): box_type = 'cubic' box_dims = str(dx) else: box_type = 'orthogonal' box_dims = '{0:} {1:} {2:}'.format(dx, dy, dz) upd_vals = OrderedDict([('box_count', 1), ('box_type', box_type), ('box_size', box_dims)]) if ('Box_Info' in self.props.keys()) and isinstance(self.props['Box_Info'], InpSpec): self.props['Box_Info'] = InpSpec('Box_Info', upd_vals, None, **{'new_line': True}) else: self.props['Box_Info'] = upd_vals tmp = self.props['Box_Info'].value['box_size'].split() if self.props['Box_Info'].value['box_type'] == 'cubic': tmp = tmp + tmp + tmp self.tot_sst.dim = system.Dimension(dx=float(tmp[0]), dy=float(tmp[1]), dz=float(tmp[2])) # Sync of the volume change frequency in equilibration regime if 'Prob_Volume' in self.props.keys(): if self.props['Prob_Volume'] is None: self.props['Run_Type'].value['steps'] = self.props['Run_Type'].value['steps'][0] def __write_chk__(self, out_file): """pysimm.cassandra.MCSimulation.__write_chk__ Creates the CASSANDRA checkpoint file basing on the information from the `~MCSimulation.tot_sst` field """ # Initializing output stream if out_file == 'string': out_stream = StringIO() else: out_stream = open(out_file, 'w+') blk_separ = ' {:*^75}\n' # Writing Translation/rotation/... info out_stream.write(blk_separ.format('Translation,rotation, dihedral, angle distortion')) tmplate = '{t[0]$$}{t[1]$$}{t[2]$$}{t[3]$$}{t[4]$$}\n' molecules = self.props['Molecule_Files'].value for m, i in zip(molecules, range(len(molecules))): out_stream.write(tmplate.replace('$$', ':>6d').format(t=[i + 1, 0, 0, 0, 0])) out_stream.write(tmplate.replace('$$', ':>6d').format(t=[i + 1, 0, 0, 0, 0])) out_stream.write('{t[0]:>23.14E}{t[2]:>23.14E}{t[2]:>23.14E}\n'.format(t=[0, 0, 0])) out_stream.write('{0:>12d}{0:>12d}\n'.format(0, 0)) # Small section with total # of MC trials -- it is 0 at the beginning out_stream.write(blk_separ.format('# of MC steps')) out_stream.write('{:>12d}\n'.format(0)) # Writing Box-info information out_stream.write(blk_separ.format('Box info')) tmp = self.props['Box_Info'].value['box_size'] x, y, z = 0, 0, 0 bx_type = None if isinstance(tmp, types.ListType): if len(tmp) > 3: x, y, z = tmp[0], tmp[1], tmp[2] elif isinstance(tmp, int) or isinstance(tmp, float): x, y, z = tmp, tmp, tmp else: exit(0) # First 0 here correspond to the # of trials out_stream.write('{0:>12d}\n{1:<18.10f}\n{2:}\n'.format(0, x * y * z, self.props['Box_Info'].value['box_type'])) tmpl = '{t[0]&&}{t[1]&&}{t[2]&&}\n' tmp = np.diag([x, y, z]) for lines in tmp: out_stream.write((tmpl.replace('&&', ':^22.14f')).format(t=lines)) tmp = np.diag([1 / x, 1 / y, 1 / z]) for lines in tmp: out_stream.write((tmpl.replace('&&', ':^22.8f')).format(t=lines)) out_stream.write('{:>18.12f}\n'.format(0)) # Creating seeds out_stream.write(blk_separ.format('SEEDS')) out_stream.write('{t[0]:>12d}{t[1]:>12d}{t[2]:>12d}\n{t[3]:>12d}{t[4]:>12d}\n'.format( t=np.random.random_integers(int(1e+7), int(1e+8 - 1), 5))) # Writing total number of molecules by species out_stream.write(blk_separ.format('Info for total number of molecules')) out_stream.write('{0:>11d}{1:>11d}\n'.format(1, 1)) # Currentely only one polymer "molecule" in the simulation for i in range(1, len(molecules)): out_stream.write('{0:>11d}{1:>11d}\n'.format(i + 1, 0)) out_stream.write(blk_separ.format('Writing coordinates of all boxes')) # Writing coordinates of atoms in all boxes line_template = '{l[0]:<5}{l[1]:<25.15f}{l[2]:<25.15f}{l[3]:<25.15f}{l[4]:>10d}\n' for parts in self.tot_sst.particles: try: out_stream.write(line_template.format(l=[parts.type.name, parts.x, parts.y, parts.z, 1])) except: continue out_stream.close() class GCMC(MCSimulation): """pysimm.cassandra.GCMC Initiates the specific type of Monte Carlo simulations for CASSANDRA: simulations using Grand-Canonical ensemble of particles (constant volume-temperature-chemical potential, muVT). See :class:`~pysimm.cassandra.MCSimulation` for the detailed description of the properties. """ def __init__(self, mc_sst=None, init_sst=None, **kwargs): MCSimulation.__init__(self, mc_sst, init_sst, **kwargs) self.logger.name = 'GCMC' self.props['Sim_Type'] = InpSpec('Sim_Type', 'GCMC', 'gcmc') # Path for all intermediate Cassandra files and results self.props_file = os.path.join(self.out_folder, kwargs.get('props_file', 'gcmc_input.inp')) add = 0 if self.fxd_sst and self.fxd_sst.particles.count: add = 1 self.props['Chemical_Potential_Info'] = InpSpec('Chemical_Potential_Info', kwargs.get('chem_pot'), -30 * (self._n_spec - add)) # Order of the next four items is IMPORTANT! Check the CASSANDRA spec file for further info def_init_prob = 0.25 limits = [0.3] * self._n_spec if self.fxd_sst: limits[0] = 0 self.props['Prob_Translation'] = InpProbSpec('Prob_Translation', kwargs.get('Prob_Translation'), OrderedDict([('tot_prob', def_init_prob), ('limit_vals', limits)]), **{'new_line': True, 'indicator': 'start'}) tps = ['cbmc'] * self._n_spec if self.fxd_sst: tps[0] = 'none' self.props['Prob_Insertion'] = InpProbSpec('Prob_Insertion', kwargs.get('Prob_Insertion'), OrderedDict([('tot_prob', def_init_prob), ('types', tps)]), **{'new_line': True}) self.props['Prob_Deletion'] = InpProbSpec('Prob_Deletion', kwargs.get('Prob_Deletion'), def_init_prob) max_ang = [180] * self._n_spec if self.fxd_sst: max_ang[0] = 0 self.props['Prob_Rotation'] = InpProbSpec('Prob_Rotation', kwargs.get('Prob_Rotation'), OrderedDict([('tot_prob', def_init_prob), ('limit_vals', max_ang)]), **{'new_line': True, 'indicator': 'end'}) class NVT(MCSimulation): """pysimm.cassandra.NVT Initiates the specific type of Monte Carlo simulations for CASSANDRA: simulations using Canonical ensemble of particles (constant volume-temperature-number of particles, NVT). See :class:`~pysimm.cassandra.MCSimulation` for the detailed description of the properties. """ def __init__(self, mc_sst=None, init_sst=None, **kwargs): MCSimulation.__init__(self, mc_sst, init_sst, **kwargs) self.logger.name = 'NVT' self.props_file = os.path.join(self.out_folder, kwargs.get('props_file', 'nvt-mc_input.inp')) self.props['Sim_Type'] = InpSpec('Sim_Type', 'nvt_mc', 'nvt_mc') move_probs = [1, 1, 1] limits = [0.3] * self._n_spec if self.fxd_sst: limits[0] = 0 self.props['Prob_Translation'] = InpProbSpec('Prob_Translation', kwargs.get('Prob_Translation'), OrderedDict([('tot_prob', move_probs[0]), ('limit_vals', limits)]), **{'new_line': True, 'indicator': 'start'}) sub_probs = [1] * self._n_spec if self.fxd_sst: sub_probs[0] = 0 sm = sum(sub_probs) sub_probs = [s / sm for s in sub_probs] self.props['Prob_Regrowth'] = InpProbSpec('Prob_Regrowth', kwargs.get('Prob_Regrowth'), OrderedDict([('tot_prob', move_probs[1]), ('sub_probs', sub_probs)]), **{'new_line': True}) max_ang = [180] * self._n_spec if self.fxd_sst: max_ang[0] = 0 self.props['Prob_Rotation'] = InpProbSpec('Prob_Rotation', kwargs.get('Prob_Rotation'), OrderedDict([('tot_prob', move_probs[2]), ('limit_vals', max_ang)]), **{'new_line': True, 'indicator': 'end'}) class NPT(MCSimulation): """pysimm.cassandra.NPT Initiates the specific type of Monte Carlo simulations for CASSANDRA: simulations using Isobaric-Isothermal ensemble of particles (NPT). See :class:`~pysimm.cassandra.MCSimulation` for the detailed description of the properties. """ def __init__(self, mc_sst=None, init_sst=None, **kwargs): MCSimulation.__init__(self, mc_sst, init_sst, **kwargs) # Initialising object attributes self.logger.name = 'NPT' self.props_file = os.path.join(self.out_folder, kwargs.get('props_file', 'npt-mc_input.inp')) # Initialising simulation-specific props attribute self.props['Sim_Type'] = InpSpec('Sim_Type', 'npt_mc', 'npt_mc') self.props['Pressure_Info'] = InpSpec('Pressure_Info', kwargs.get('Pressure_Info'), DEFAULT_PARAMS['Pressure_Info']) move_probs = [.34, .02, .32, .32] limits = [0.3] * self._n_spec if self.fxd_sst: limits[0] = 0 self.props['Prob_Translation'] = InpProbSpec('Prob_Translation', kwargs.get('Prob_Translation'), OrderedDict([('tot_prob', move_probs[0]), ('limit_vals', limits)]), **{'new_line': True, 'indicator': 'start'}) vol_margins = 0.1 * self.props['Box_Info'].value['box_size'] self.props['Prob_Volume'] = InpProbSpec('Prob_Volume', kwargs.get('Prob_Volume'), OrderedDict([('tot_prob', move_probs[1]), ('types', vol_margins)]), **{'new_line': True}) sub_probs = [1] * self._n_spec if self.fxd_sst: sub_probs[0] = 0 sm = sum(sub_probs) sub_probs = [s / sm for s in sub_probs] self.props['Prob_Regrowth'] = InpProbSpec('Prob_Regrowth', kwargs.get('Prob_Regrowth'), OrderedDict([('tot_prob', move_probs[2]), ('sub_probs', sub_probs)]), **{'new_line': True}) max_ang = [180] * self._n_spec if self.fxd_sst: max_ang[0] = 0 self.props['Prob_Rotation'] = InpProbSpec('Prob_Rotation', kwargs.get('Prob_Rotation'), OrderedDict([('tot_prob', move_probs[3]), ('limit_vals', max_ang)]), **{'new_line': True, 'indicator': 'end'}) class InpSpec(object): """pysimm.cassandra.InpSpec Represents the most common object used for carrying one logical unit of the CASSANDRA simulation options Parameters: key (str) : the keyword of the simulation option (literally the string that goes after the # sign in CASSANDRA .inp file) value (object) : numerical or text values of the particular simulation option structured in a certain way. Here goes only the values that are wished to be changed (it might be just one field of a big dictionary) default (object) : the most complete default description of the simulation option Keyword Args: write_headers (boolean): if the :obj:`~value` is dictionary defines whether the dictionary keys should be written to the output new_line (boolean): if the :obj:`~value` is iterable defines whether each new element will be written to the new line """ def __init__(self, key, value, default, **kwargs): self.key = key self.write_headers = kwargs.get('write_headers') self.is_new_line = kwargs.get('new_line') self.value = value if value: if isinstance(default, types.DictType): # Add from default structure all properties that were not defined by user for ky in value.keys(): default[ky] = value[ky] self.value = default else: self.value = value elif value == []: self.value = [] else: # If nothing was passed write default self.value = default def to_string(self): """pysimm.cassandra.InpSpec.to_string Creates the proper text representation of the property stored in the :obj:`~value` field Returns: str: formatted text string """ if self.value is not None: result = '# {:}\n'.format(self.key) # Strings if isinstance(self.value, types.StringTypes): result += str(self.value) # Dictionaries elif isinstance(self.value, types.DictType): for ks in list(self.value.keys()): if self.write_headers: result += ks + ' ' tmp = self.value[ks] if (isinstance(tmp, Iterable)) & (not isinstance(tmp, types.StringTypes)): result += ' '.join(str(p) for p in tmp) else: result += str(tmp) if self.is_new_line: result += '\n' else: result += ' ' result = result[:-1] # Remove the very last new line character # Lists elif isinstance(self.value, Iterable): for elem in self.value: if isinstance(elem, Iterable): subresult = '' for subelem in elem: subresult = subresult + str(subelem) + ' ' else: subresult = str(elem) + ' ' result += subresult # Simple types else: result += str(self.value) result += '\n!{:^^20}\n'.format('') return result class InpProbSpec(InpSpec): """pysimm.cassandra.InpSpec Extension of the :class:`~InpSpec` class that takes into account special representation of the movement probabilities in the CASSANDRA input file. """ def __init__(self, key, value, default, **kwargs): super(InpProbSpec, self).__init__(key, value, default, **kwargs) def to_string(self): tmp = super(InpProbSpec, self).to_string() if self.key == 'Prob_Translation': tmp = '# Move_Probability_Info\n\n' + tmp elif self.key == 'Prob_Rotation': tmp += '\n# Done_Probability_Info\n' return tmp class McSystem(object): """pysimm.cassandra.McSystem Wrapper around the list of :class:`~pysimm.system.System` objects. Each element in the list represents single molecule of a different specie that will be used during MC simulations. Additionally, the object is responsible for creating .dat and .mcf files needed for the simulation and reading back the CASSANDRA simulation results. Attributes: sst (list of :class:`~pysimm.system.System`) : items representing single molecules of different species to be inserted by CASSANDRA. If the sst is a list (not a single value) it is assumed that all of the following properties are synchronized with it by indexes. chem_pot (list of int) : chemical potential for each specie [Joule/mol] Keyword Args: max_ins (list of int) : defines the highest possible number of molecules of corresponding specie. Basing on these values CASSANDRA allocates memory for simulations. (default: 5000). is_rigid (list of boolean): defines whether the atoms in the particular molecule should be marked as rigid or not. **Important!** In current implementation the module doesn't support flexible molecule angles, so the `is_rigid=False` is designed to be used exclusively for **single bead** molecules. Parameters: made_ins (list of int) : number of particles of each specie inserted by CASSANDRA. mcf_file (list of str) : defines full relative names of molecule configuration files **(.mcf)** required by CASSANDRA. Files will be created automatically. frag_file (list of str) : defines full relative names of possible relative configuration files **(.dat)** required by CASSANDRA. Files will be created automatically. """ def __init__(self, sst, **kwargs): self.logger = logging.getLogger('MC_SYSTEM') self.sst = make_iterable(sst) for sst in self.sst: # Checking that the force-field of the input system is of the class-1 as it is direct CASSANDRA restriction if isinstance(sst, system.System): sst.zero_charge() sst.add_particle_bonding() if sst.ff_class: if not (sst.ff_class == '1'): self.logger.error('Currently cassandra supports only with **Type-I** force fields. ' 'The PYSIMM systems you provided are of the different types' 'Exiting...') exit(1) else: self.logger.info('The Force-Field type of the system is not defined. ' 'Assuming it is **Type-1** force field') sst.ff_class = '1' if not all([pt.name for pt in sst.particle_types]): self.logger.error('The name of at least one particle type in MC system is not defined. ' 'Will not be able to map particles back after the CASSANDRA simulations. ' '\nPlease, setup the names for all particle types for your MC system') exit(1) # Decorating the system with bonds_fixed flag and angle_fixed flag for bt in sst.bond_types: bt.is_fixed = True for at in sst.angle_types: if at.k > 70: at.is_fixed = True self.file_store = os.getcwd() self.max_ins = make_iterable(kwargs.get('max_ins', 5000)) self.is_rigid = make_iterable(kwargs.get('is_rigid', [True] * len(self.sst))) self.made_ins = [0] * len(self.sst) self.mcf_file = [] self.frag_file = [] self.temperature = None def update_props(self, props): """pysimm.cassandra.McSystem.update_props For each specie in the system creates the .mcf file required for CASSANDRA simulation. Args: props (dictionary) : contains the .mcf file names and maximally allowed number of molecules insertions. The dictionary is to be assigned to 'Molecule_Files' property of the MC simulation Returns: props: updated input dictionary """ # Generate correct .mcf files al_ind = 0 for (sstm, count) in zip(self.sst, range(len(self.sst))): fullfile = os.path.join(self.file_store, '{:}{:}{:}'.format('particle', str(count + 1), '.mcf')) for p_type in sstm.particle_types: if p_type.elem and (not p_type.real_elem): p_type.real_elem = p_type.elem p_type.elem = ascii_uppercase[int(al_ind / 10)] + str(al_ind % 10) al_ind += 1 McfWriter(sstm, fullfile).write() self.mcf_file.append(fullfile) # Make the files list to be returned offset = len(props) for (mcf, ins, count) in zip(self.mcf_file, self.max_ins, range(1 + offset, len(self.mcf_file) + 1 + offset)): props['file' + str(count)] = [mcf, ins] return props def update_frag_record(self, frag_record): """pysimm.cassandra.McSystem.update_frag_record For each specie in the system creates the single configuration .dat file required for CASSANDRA simulation. Args: frag_record: dictionary containing the .dat file names and their ids. The dictionary is to be assigned to 'Molecule_Files' property of the MC simulation Returns: dictionary: updated dictionary """ # Generating the structure files if self.temperature is None: self.temperature = 300 for (sstm, count) in zip(self.sst, range(len(self.sst))): fullfile = os.path.join(self.file_store, '{:}{:}{:}'.format('particle', str(count + 1), '.dat')) with open(fullfile, 'w') as out: frag_count = 1 out.write('{:>12d}\n'.format(frag_count)) out.write('{:>21f}{:>21f}\n'.format(self.temperature, 0)) tmplte = '{:<10}{:<24f}{:<24f}{:<24f}\n' for prt in sstm.particles: out.write(tmplte.format(prt.type.elem, prt.x, prt.y, prt.z)) self.frag_file.append(fullfile) # Generating the files list for (frags, count) in zip(self.frag_file, range(1, len(self.frag_file) + 1)): frag_record['file' + str(count)] = [frags, count] return frag_record def make_system(self, text_output): """pysimm.cassandra.McSystem.make_system Parses the checkpoint (.chk) file made by CASSANDRA and creates new molecules basing on the new coordinates information. Assumes that all atoms of a certain molecule are listed in .chk file together (molecule identifiers are not mixed). Note: The logic of comparison of the xyz-like text record from the .chk file with the :class:`~pysimm.system.System` object is most straightforward: It is the consecutive comparison of particle names and first letters (before the white space) in the text record. In this implementation order matters! For example, for CO2, if in the system atoms are ordered as C-O-O and in the text they are ordered as O-C-O fit will fail. Args: text_output (str): text stream from the CASSANDRA .chk file containing the coordinates of newly inserted molecules Returns: :class:`~pysimm.system.System` : object containing all newly inserted molecules """ tmp_sst = None count = 0 # counter of the lines in the input file sys_idx = 0 # counter of the gas molecules to lookup while count < len(text_output): tmp = self.sst[sys_idx].copy() dictn = text_output[count:(len(tmp.particles) + count)] if self.__fit_atoms__(tmp, dictn): for p in tmp.particles: vals = dictn[p.tag - 1].split() # Read the coordinates from the text output of the CASSANDRA simulation p.x, p.y, p.z = map(float, vals[1:4]) # Force velocities of the particles to be 0 p.vx, p.vy, p.vz = 0.0, 0.0, 0.0 p.molecule.syst_tag = 0 if self.is_rigid[sys_idx]: for p in tmp.particles: p.is_rigid = True if tmp_sst: tmp_sst.add(tmp) else: tmp_sst = tmp.copy() self.made_ins[sys_idx] += 1 count += len(tmp.particles) sys_idx = 0 else: sys_idx += 1 if sys_idx >= len(self.sst): self.logger.error('Wasn\'t able to read CASSANDRA .chk file. ' 'Please check either MC-simulation provided to PySIMM or the CASSANDRA ' 'checkpoint file ') exit(1) if tmp_sst: tmp_sst.update_tags() tmp_sst.objectify() return tmp_sst def __fit_atoms__(self, molec, text_lines): """pysimm.cassandra.McSystem.__fit_atoms__ Implements simple logic of comparison of the xyz-like text record with the :class:`~pysimm.system.System` object. The comparison is based on the consecutive comparison of particle names and first letters (before the white space) in the text. In this implementation order matters! E.g. for CO2, if in the system atoms are ordered as C-O-O and in the text they are ordered like O-C-O fit will return False. Returns: boolean: flag whether the text record fit the molecule or not """ flag = True # Cannot map anything if number of molecules is different from number of data lines if len(molec.particles) != len(text_lines): return False # Check the sequence of element names they for p in molec.particles: vals = text_lines[p.tag - 1].split() if vals[0] != p.type.elem: return False return flag class Cassandra(object): """pysimm.cassandra.Cassandra Organizational object for running CASSANDRA simulation tasks. In current implementation it is able to run Canonical, Grand Canonical, and Isothermal-Isobaric Monte Carlo simulations (:class:`~GCMC`, :class:`~NVT`, and :class:`~NPT`, correspondingly). Parameters: system (:class:`~pysimm.system.System`) : molecular updated during the simulations run_queue (list) : the list of scheduled tasks """ def __init__(self, init_sst): self.logger = logging.getLogger('CSNDRA') # Assume all particles in initial system are fixed self.system = init_sst if init_sst.particles: for p in init_sst.particles: p.is_fixed = True self.run_queue = [] def run(self): """pysimm.cassandra.Cassandra.run Method that triggers the simulations. Does two consecutive steps: **(1)** tries to write all files necessary for simulation (.dat, .inp, .mcf): **(2)** tries to invoke the CASSANDRA executable. """ global CASSANDRA_EXEC if check_cs_exec(): for task in self.run_queue: # Write .inp file task.write() # Write .xyz of the fixed system if provided if task.fxd_sst: if task.fxd_sst_mcfile is not None: McfWriter(task.fxd_sst, task.fxd_sst_mcfile).write('atoms') task.fxd_sst.write_xyz(task.fxd_sst_xyz) try: self.logger.info('Starting the GCMC simulations with CASSANDRA') print('{:.^60}'.format('')) p = Popen([CASSANDRA_EXEC, task.props_file], stdin=PIPE, stdout=PIPE, stderr=PIPE) stout, sterr = p.communicate() print(stout) print(sterr) task.upd_simulation() self.system = task.tot_sst.copy() except OSError as ose: self.logger.error('There was a problem calling CASSANDRA executable') exit(1) except IOError as ioe: if check_cs_exec(): self.logger.error('There was a problem running CASSANDRA. ' 'The process started but did not finish') exit(1) else: self.logger.error('There was a problem running CASSANDRA: seems it is not configured properly.\n' 'Please, be sure the CSNDRA_EXEC environment variable is set to the correct ' 'CASSANDRA executable path. The current path is set to:\n\n{}\n\n'.format(CASSANDRA_EXEC)) exit(1) def add_simulation(self, ens_type, obj=None, **kwargs): """pysimm.cassandra.Cassandra.add_simulation Method for adding new Monte Carlo simulation to the run queue. Args: ens_type: Type of the molecular ensemble for the Monte-Carlo simulations. The supported options are: `GCMC` (Grand Canonical); `NVT` (canonical); `NPT` (isobaric-isothermal) obj: the entity that should be added. Will be ignored if it is not of a type :class:`~MCSimulation` Keyword Args: is_new (boolean) : defines whether all previous simulations should be erased or not species (list of :class:`~pysimm.system.System`) : systems that describe molecules and will be passed to :class:`~McSystem` constructor. Note: Other keyword arguments of this method will be redirected to the :class:`~McSystem` and :class:`~MCSimulation` constructors. See their descriptions for the possible keyword options. """ new_job = None # Reading the molecule ensemble type simul = locate('pysimm.cassandra.' + ens_type) if simul is None: self.logger.error('Unsopported simulation ensemble option. Please use ether GCMC, NPT, or ' 'NVT in \'add_simulation\' ') exit(1) if isinstance(obj, MCSimulation): new_job = obj else: specs = kwargs.get('species') if specs: mc_sst = McSystem(specs, **kwargs) new_job = simul(mc_sst, self.system, **kwargs) else: self.logger.error('Incorrect ' + ens_type + ' initialization. Please provide either Cassandra.' + ens_type + ' simulation object or the dictionary with initialization parameters ' 'of that object') exit(1) # Clean the run queue if 'is_new' set to to True if kwargs.get('is_new'): self.run_queue[:] = [] if new_job: new_job.__check_params__() self.run_queue.append(new_job) def add_gcmc(self, obj=None, **kwargs): """pysimm.cassandra.Cassandra.add_gcmc Ads new simulation in grand-canonical ensemble to the run queue. Args: obj: the entity that should be added. Will be ignored if it is not of a type :class:`~GCMC` Keyword Args: is_new (boolean) : defines whether all previous simulations should be erased or not species (list of :class:`~pysimm.system.System`) : systems that describe molecules and will be passed to :class:`~McSystem` constructor. Note: Other keyword arguments of this method will be redirected to the :class:`~McSystem`, :class:`~MCSimulation`, and :class:`~GCMC` constructors. See their descriptions for the possible keyword options. """ new_job = None if isinstance(obj, GCMC): new_job = obj else: specs = kwargs.get('species') if specs: mc_sst = McSystem(specs, **kwargs) new_job = GCMC(mc_sst, self.system, **kwargs) else: self.logger.error('Unknown GCMC initialization. Please provide either ' 'the dictionary with GCMC parameters or Cassandra.GCMC simulation object') exit(1) if kwargs.get('is_new'): self.run_queue[:] = [] if new_job: new_job.__check_params__() self.run_queue.append(new_job) def add_npt_mc(self, obj=None, **kwargs): """pysimm.cassandra.Cassandra.add_npt_mc Ads new simulation in isobaric-isothermal ensemble to the run queue. Args: obj: the entity that should be added. Will be ignored if it is not of a type :class:`~NPT` Keyword Args: is_new (boolean) : defines whether all previous simulations should be erased or not species (list of :class:`~pysimm.system.System`) : systems that describe molecules and will be passed to :class:`~McSystem` constructor. Note: Other keyword arguments of this method will be redirected to the :class:`~McSystem`, :class:`~MCSimulation`, and :class:`~NPT` constructors. See their descriptions for the possible keyword options. """ new_job = None if isinstance(obj, NPT): new_job = obj else: specs = kwargs.get('species') if specs: mc_sst = McSystem(specs, **kwargs) new_job = NPT(mc_sst, self.system, **kwargs) else: self.logger.error('Unknown NPT initialization. Please provide either ' 'the dictionary with NPT simulation parameters or Cassandra.NPT simulation object') exit(1) if kwargs.get('is_new'): self.run_queue[:] = [] if new_job: new_job.__check_params__() self.run_queue.append(new_job) def add_nvt(self, obj=None, **kwargs): """pysimm.cassandra.Cassandra.add_nvt Ads new simulation in canonical ensemble to the run queue. Args: obj: the entity that should be added. Will be ignored if it is not of a type :class:`~NVT` Keyword Args: is_new (boolean) : defines whether all previous simulations should be erased or not species (list of :class:`~pysimm.system.System`) : systems that describe molecules and will be passed to :class:`~McSystem` constructor. Note: Other keyword arguments of this method will be redirected to the :class:`~McSystem`, :class:`~MCSimulation`, and :class:`~NVT` constructors. See their descriptions for the possible keyword options. """ new_job = None if isinstance(obj, NVT): new_job = obj else: specs = kwargs.get('species') if specs: mc_sst = McSystem(specs, **kwargs) new_job = NVT(mc_sst, self.system, **kwargs) else: self.logger.error('Unknown NVT initialization. Please provide either ' 'the dictionary with NPT simulation parameters or Cassandra.NPT simulation object') exit(1) if kwargs.get('is_new'): self.run_queue[:] = [] if new_job: new_job.__check_params__() self.run_queue.append(new_job) def read_input(self, inp_file): """pysimm.cassandra.Cassandra.read_input The method parses the CASSANDRA instructions file (.inp) split it into separate instructions and analyses each according to the instruction name. Args: inp_file (str) : the full relative path of the file to be read Returns: dictionary : read CASSANDRA properties in the format required by :class:`~GCMC` """ result = {} if os.path.isfile(inp_file): self.logger.info('Reading simulation parameters from {:} file'.format(inp_file)) # Reading the cassandra .inp file as one long string inp_stream = open(inp_file, 'r') lines = inp_stream.read() raw_props = lines.split('#') for prop in raw_props: line = re.sub('\n!.*', '', prop) # Get rid of the CASSANDRA comments line = re.sub('\n(e|E)(n|N)(d|D)', '', line) # Get rid of the 'END in the end of the file tmp = line.split() if len(tmp) > 1: result[tmp[0]] = self.__parse_value__(tmp) # File seems fine let's close the stream and return true in the flag inp_stream.close() self.logger.info('Reading finished sucsessfully') else: self.logger.error('Cannot find specified file: \"{:}\"'.format(inp_file)) return result def __parse_value__(self, cells): title = cells[0].lower() if title == 'run_type': return OrderedDict([('type', cells[1]), ('steps', map(int, cells[2:]))]) elif title == 'charge_style': return OrderedDict([('type', cells[1]), ('sum_type', cells[2]), ('cut_val', float(cells[3])), ('accuracy', float(cells[4]))]) elif title == 'vdw_style': return OrderedDict([('type', cells[1]), ('cut_type', cells[2]), ('cut_val', float(cells[3]))]) elif title == 'simulation_length_info': tmp = OrderedDict([('units', cells[2]), ('prop_freq', int(cells[4])), ('coord_freq', int(cells[6])), ('run', int(cells[8]))]) if len(cells) > 10: tmp['steps_per_sweep'] = int(cells[10]) if len(cells) > 12: tmp['block_averages'] = int(cells[12]) return tmp elif title == 'cbmc_info': return OrderedDict([('kappa_ins', int(cells[2])), ('kappa_dih', int(cells[4])), ('rcut_cbmc', float(cells[6]))]) elif title == 'box_info': size = float(cells[3]) if len(cells) > 6: size = [float(cells[3]), float(cells[4]), float(cells[5])] return OrderedDict([('box_count', int(cells[1])), ('box_type', cells[2]), ('box_size', size)]) elif title == 'prob_translation': vals = [] for i in range(2, len(cells)): vals.append(float(cells[i])) return OrderedDict([('tot_prob', float(cells[1])), ('limit_vals', vals)]) elif title == 'prob_insertion': vals = [] for i in range(2, len(cells)): vals.append(cells[i]) return OrderedDict([('tot_prob', float(cells[1])), ('types', vals)]) elif title == 'prob_rotation': vals = [] for i in range(2, len(cells)): vals.append(float(cells[i])) return OrderedDict([('tot_prob', float(cells[1])), ('limit_vals', vals)]) elif (title == 'molecule_files') or (title == 'fragment_files'): tmp = OrderedDict() for i, c in zip(range(1, len(cells) - 1, 2), range(1, 1 + len(cells) / 2)): tmp['file' + str(c)] = [cells[i], int(cells[i + 1])] return tmp elif title == 'start_type': if cells[1] == 'read_config': specs = [] for i in range(2, len(cells) - 1): specs.append(int(cells[i])) return OrderedDict([('start_type', 'read_config'), ('species', specs), ('file_name', cells[-1])]) if cells[1] == 'make_config': specs = [] for i in range(2, len(cells)): specs.append(int(cells[i])) return OrderedDict([('start_type', 'make_config'), ('species', specs), ('file_name', '')]) if cells[1] == 'add to config': self.logger.error('Sorry, \'add to config\' regime of ''Start_Type option is not supported yet') exit(1) if cells[1] == 'checkpoint': self.logger.error('Sorry, \'checkpoint\' regime of ''Start_Type option is not supported yet ') exit(1) elif title == 'property_info': if int(cells[1]) == 1: tmp = OrderedDict() for i in range(2, len(cells)): tmp['prop' + str(i - 1)] = str.lower(cells[i]) return tmp elif title == 'seed_info': return [int(cells[1]), int(cells[2])] elif (title == 'prob_deletion') or (title == 'rcutoff_low') or \ (title == 'bond_prob_cutoff') or (title == 'chemical_potential_info'): return float(cells[1]) elif (title == 'average_Info') or (title == 'nbr_species') or (title == 'temperature_info'): return int(cells[1]) else: return cells[1] def unwrap_gas(self): """pysimm.cassandra.Cassandra.unwrap_gas Ensures that all particles that are not fixed are unwrapped, otherwise CASSANDRA might not interpret them correctly """ gas_system = self.system.copy() for p in gas_system.particles: if p.is_fixed: gas_system.particles.remove(p.tag, update=False) else: self.system.particles.remove(p.tag, update=False) for m in gas_system.molecules: if any([t.is_fixed for t in m.particles]): gas_system.molecules.remove(m.tag, update=False) else: self.system.molecules.remove(m.tag, update=False) gas_system.remove_spare_bonding() self.system.remove_spare_bonding() gas_system.unwrap() self.system.add(gas_system, change_dim=False) class McfWriter(object): """pysimm.cassandra.McfWriter Object responsible for creating the CASSANDRA Molecular Configuration file (.mcf). Attributes: syst (:class:`~pysimm.system.System`) :represents the molecule to be described file_ref (str) : full relative path to the file that will be created """ # Section names in any .mcf file mcf_tags = ['# Atom_Info', '# Bond_Info', '# Angle_Info', '# Dihedral_Info', '# Improper_Info', '# Intra_Scaling', '# Fragment_Info', '# Fragment_Connectivity'] empty_line = '0' def __init__(self, syst, file_ref): self.syst = syst self.file_ref = file_ref self.logger = logging.getLogger('MCF Writer') def write(self, typing='all'): """pysimm.cassandra.McfWriter.write Method creates the .mcf file writing only those sections of it that are marked to be written Args: typing (list) : the list of sections to be written or the text keyword. List items should be as they are defined in :class:`~pysimm.cassandra.McfWriter.mcf_tags` field); default 'all' """ # Initializing output stream with open(self.file_ref, 'w') as out_stream: for (name, is_write) in zip(self.mcf_tags, self.__to_tags__(typing)): if is_write: try: method = getattr(self, '__write_' + str.lower(name[2:]) + '__') method(out_stream) except AttributeError: self.__write_empty__(out_stream, name) else: self.__write_empty__(out_stream, name) out_stream.write('\nEND') out_stream.close() def __write_empty__(self, out, name): out.write('{0:}\n{1:}\n\n'.format(name, self.empty_line)) def __write_atom_info__(self, out): global KCALMOL_2_K text_tag = '# Atom_Info' if self.syst.particles.count > 0: # writing section header out.write('{:}\n'.format(text_tag)) # Verify and fix net system charge self.syst.zero_charge() # writing total number of particles out.write('{0:<6}\n'.format(self.syst.particles.count)) count = 0 line_template = '{l[0]:<6}{l[1]:<7}{l[2]:<5}{l[3]:<8.3f}{l[4]:<10.6f}' \ '{l[5]:<6}{l[6]:<11.3f}{l[7]:<9.3f}\n' warn_flag = False for item in self.syst.particles: line = [count + 1, '', '', 0, 0, 'LJ', 0, 0] if item.charge: line[4] = item.charge if item.type: line[1] = item.type.tag line[2] = item.type.tag if item.type.name: line[1] = item.type.name line[2] = item.type.elem else: warn_flag = True if item.type.mass: line[3] = item.type.mass if item.type.epsilon: line[6] = KCALMOL_2_K * item.type.epsilon if item.type.sigma: line[7] = item.type.sigma else: continue out.write(line_template.format(l=line)) count += 1 if warn_flag: self.logger.warning('Some particle type names (and/or element names) inside the system are not defined.' ' Will use type identifiers instead') else: self.__write_empty__(out, text_tag) out.write('\n') def __write_bond_info__(self, out): text_tag = '# Bond_Info' if self.syst.bonds.count > 0: # writing section header out.write('{:}\n'.format(text_tag)) # writing total number of bonds out.write('{0:<6}\n'.format(self.syst.bonds.count)) line_template = '{l[0]:<6d}{l[1]:<6d}{l[2]:<6d}{l[3]:<9}{l[4]:<6.3f}\n' count = 1 for bond in self.syst.bonds: tmp = 'fixed' # Fixed bond is the only option for CASSANDRA V-1.2 line = [count, bond.a.tag, bond.b.tag, tmp, bond.type.r0] count += 1 out.write(line_template.format(l=line)) out.write('\n') else: self.__write_empty__(out, text_tag) def __write_angle_info__(self, out): text_tag = '# Angle_Info' if self.syst.angles.count > 0: # writing section header out.write('{:}\n'.format(text_tag)) # writing total number of angles out.write('{0:<6}\n'.format(self.syst.angles.count)) count = 1 for angle in self.syst.angles: line_template = '{l[0]:<6d}{l[1]:<6d}{l[2]:<6d}{l[3]:<6d}{l[4]:<10}{l[5]:<13.3f}' line = [count, angle.a.tag, angle.b.tag, angle.c.tag] if hasattr(angle.type, 'is_fixed') and angle.type.is_fixed: addon = ['fixed', angle.type.theta0] else: addon = ['harmonic', KCALMOL_2_K * angle.type.k, angle.type.theta0] line_template += '{l[6]:<13.3f}' count += 1 out.write(line_template.format(l=line + addon) + '\n') out.write('\n') else: self.__write_empty__(out, text_tag) def __write_intra_scaling__(self, out): format_line = '{:<6.2f}{:<6.2f}{:<6.2f}{:<6.2f}' # writing section header out.write('{:}\n'.format('# Intra_Scaling')) # writing vdW scaling: 1-2 1-3 1-4 1-N out.write(format_line.format(0, 0, 0, 0) + '\n') # writing charge scaling: 1-2 1-3 1-4 1-N out.write(format_line.format(0, 0, 0, 0) + '\n\n') def __write_dihedral_info__(self, out): text_tag = '# Dihedral_Info' self.__write_empty__(out, text_tag) def __write_improper_info__(self, out): text_tag = '# Improper_Info' self.__write_empty__(out, text_tag) def __write_fragment_info__(self, out): # writing section header out.write('{:}\n'.format('# Fragment_Info')) # writing indexing out.write('{:}\n'.format(1)) n = len(self.syst.particles) out.write(' '.join('{}'.format(item) for item in [1, n] + range(1, n + 1))) out.write('\n\n') def __write_fragment_connectivity__(self, out): text_tag = '# Fragment_Connectivity' self.__write_empty__(out, text_tag) def __to_tags__(self, inpt): n = len(self.mcf_tags) idxs = [True] * n if inpt.lower() == 'atoms': idxs = [False] * n idxs[self.mcf_tags.index('# Atom_Info')] = True idxs[self.mcf_tags.index('# Intra_Scaling')] = True return idxs def check_cs_exec(): """pysimm.cassandra.check_cs_exec Validates that the absolute path to the CASSANDRA executable is set in the `CASSANDRA_EXEC` environmental variable of the OS. The validation is called once inside the :class:`~Cassandra.run` method. """ global CASSANDRA_EXEC flag = True if CASSANDRA_EXEC is None: print('Please specify the OS environment variable ''CASSANDRA_EXEC'' that points to ' 'CASSANDRA compiled binary file, which is by default cassandra_{compiler-name}[_openMP].exe ') flag = False return flag def make_iterable(obj): """pysimm.cassandra.make_iterable Utility method that forces the attributes be iterable (wrap in a list if it contains of only one item) """ it_obj = obj if not isinstance(obj, Iterable): it_obj = [obj] return it_obj
mit
-1,943,103,293,411,142,400
45.063699
123
0.549686
false
3.921
false
false
false
chop-dbhi/varify-data-warehouse
vdw/samples/migrations/0008_force_migrate_default_cohort_and_project.py
1
23232
# encoding: utf-8 import datetime from south.db import db from south.v2 import DataMigration from django.db import models from vdw.samples.models import DEFAULT_COHORT_NAME, DEFAULT_PROJECT_NAME class Migration(DataMigration): def forwards(self, orm): "Write your forwards methods here." Project = orm['samples.Project'] Cohort = orm['samples.Cohort'] now = datetime.datetime.now() # Create default project try: project = Project.objects.get(name=DEFAULT_PROJECT_NAME) except Project.DoesNotExist: project = Project(name=DEFAULT_PROJECT_NAME, label=DEFAULT_PROJECT_NAME, created=now, modified=now) project.save() # Create default cohort try: cohort = Cohort.objects.get(name=DEFAULT_COHORT_NAME) except Cohort.DoesNotExist: cohort = Cohort(name=DEFAULT_COHORT_NAME, published=True, autocreated=True, created=now, modified=now) cohort.save() def backwards(self, orm): "Write your backwards methods here." # There is not guarantee these objects did not already exist # so these should not be deleted models = { 'auth.group': { 'Meta': {'object_name': 'Group'}, 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) }, 'auth.permission': { 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'}, 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) }, 'auth.user': { 'Meta': {'object_name': 'User'}, 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 11, 27, 16, 57, 27, 697343)'}), 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 11, 27, 16, 57, 27, 697128)'}), 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) }, 'avocado.datacontext': { 'Meta': {'object_name': 'DataContext'}, 'archived': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'composite': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'count': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'db_column': "'_count'"}), 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'json': ('jsonfield.fields.JSONField', [], {'default': '{}', 'null': 'True', 'blank': 'True'}), 'keywords': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}), 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '200', 'null': 'True', 'blank': 'True'}), 'published': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'session': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'session_key': ('django.db.models.fields.CharField', [], {'max_length': '40', 'null': 'True', 'blank': 'True'}), 'user': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'datacontext+'", 'null': 'True', 'to': "orm['auth.User']"}) }, 'contenttypes.contenttype': { 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) }, 'genome.chromosome': { 'Meta': {'ordering': "['order']", 'object_name': 'Chromosome', 'db_table': "'chromosome'"}, 'code': ('django.db.models.fields.IntegerField', [], {'null': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'label': ('django.db.models.fields.CharField', [], {'max_length': '2'}), 'order': ('django.db.models.fields.IntegerField', [], {'default': '0'}), 'value': ('django.db.models.fields.CharField', [], {'max_length': '2', 'db_index': 'True'}) }, 'genome.genome': { 'Meta': {'object_name': 'Genome', 'db_table': "'genome'"}, 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '200'}), 'released': ('django.db.models.fields.DateField', [], {'null': 'True'}), 'version': ('django.db.models.fields.CharField', [], {'max_length': '100'}) }, 'genome.genotype': { 'Meta': {'object_name': 'Genotype', 'db_table': "'genotype'"}, 'code': ('django.db.models.fields.IntegerField', [], {'null': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'label': ('django.db.models.fields.CharField', [], {'max_length': '20'}), 'order': ('django.db.models.fields.IntegerField', [], {'default': '0'}), 'value': ('django.db.models.fields.CharField', [], {'max_length': '3'}) }, 'literature.pubmed': { 'Meta': {'object_name': 'PubMed', 'db_table': "'pubmed'"}, 'pmid': ('django.db.models.fields.IntegerField', [], {'primary_key': 'True'}) }, 'phenotypes.phenotype': { 'Meta': {'object_name': 'Phenotype', 'db_table': "'phenotype'"}, 'articles': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['literature.PubMed']", 'symmetrical': 'False'}), 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'hpo_id': ('django.db.models.fields.IntegerField', [], {'null': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'term': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '1000'}) }, 'samples.batch': { 'Meta': {'unique_together': "(('project', 'name'),)", 'object_name': 'Batch', 'db_table': "'batch'"}, 'count': ('django.db.models.fields.IntegerField', [], {'default': '0'}), 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'investigator': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}), 'label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'notes': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'project': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'batches'", 'to': "orm['samples.Project']"}), 'published': ('django.db.models.fields.BooleanField', [], {'default': 'False'}) }, 'samples.cohort': { 'Meta': {'object_name': 'Cohort', 'db_table': "'cohort'"}, 'autocreated': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'context': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['avocado.DataContext']", 'unique': 'True', 'null': 'True', 'blank': 'True'}), 'count': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}), 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}), 'project': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['samples.Project']", 'null': 'True', 'blank': 'True'}), 'published': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'samples': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['samples.Sample']", 'through': "orm['samples.CohortSample']", 'symmetrical': 'False'}), 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}) }, 'samples.cohortsample': { 'Meta': {'unique_together': "(('object_set', 'set_object'),)", 'object_name': 'CohortSample', 'db_table': "'cohort_sample'"}, 'added': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'object_set': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['samples.Cohort']", 'db_column': "'cohort_id'"}), 'removed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'set_object': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['samples.Sample']", 'db_column': "'sample_id'"}) }, 'samples.cohortvariant': { 'Meta': {'unique_together': "(('variant', 'cohort'),)", 'object_name': 'CohortVariant', 'db_table': "'cohort_variant'"}, 'af': ('django.db.models.fields.FloatField', [], {'null': 'True', 'db_index': 'True'}), 'cohort': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['samples.Cohort']"}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'variant': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['variants.Variant']"}) }, 'samples.person': { 'Meta': {'object_name': 'Person', 'db_table': "'person'"}, 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'mrn': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True', 'blank': 'True'}), 'notes': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'proband': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'relations': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['samples.Person']", 'through': "orm['samples.Relation']", 'symmetrical': 'False'}), 'sex': ('django.db.models.fields.CharField', [], {'max_length': '20', 'null': 'True', 'blank': 'True'}) }, 'samples.project': { 'Meta': {'unique_together': "(('name',),)", 'object_name': 'Project', 'db_table': "'project'"}, 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'notes': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}) }, 'samples.relation': { 'Meta': {'ordering': "('person', '-generation')", 'object_name': 'Relation', 'db_table': "'relation'"}, 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), 'generation': ('django.db.models.fields.IntegerField', [], {'default': '0'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'notes': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'person': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'family'", 'to': "orm['samples.Person']"}), 'relative': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'relative_of'", 'to': "orm['samples.Person']"}), 'type': ('django.db.models.fields.CharField', [], {'max_length': '20'}) }, 'samples.result': { 'Meta': {'unique_together': "(('sample', 'variant'),)", 'object_name': 'Result', 'db_table': "'sample_result'"}, 'baseq_rank_sum': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), 'coverage_alt': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}), 'coverage_ref': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}), 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), 'downsampling': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}), 'fisher_strand': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), 'genotype': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['genome.Genotype']", 'null': 'True', 'blank': 'True'}), 'genotype_quality': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), 'haplotype_score': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), 'homopolymer_run': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'in_dbsnp': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}), 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'mq': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), 'mq0': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), 'mq_rank_sum': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), 'notes': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'phred_scaled_likelihood': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'quality': ('django.db.models.fields.FloatField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}), 'quality_by_depth': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), 'read_depth': ('django.db.models.fields.IntegerField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}), 'read_pos_rank_sum': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), 'sample': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['samples.Sample']"}), 'spanning_deletions': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), 'strand_bias': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), 'variant': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['variants.Variant']"}) }, 'samples.sample': { 'Meta': {'unique_together': "(('batch', 'name'),)", 'object_name': 'Sample', 'db_table': "'sample'"}, 'batch': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'samples'", 'to': "orm['samples.Batch']"}), 'bio_sample': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}), 'count': ('django.db.models.fields.IntegerField', [], {'default': '0'}), 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'md5': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True', 'blank': 'True'}), 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'notes': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'person': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'samples'", 'null': 'True', 'to': "orm['samples.Person']"}), 'published': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'version': ('django.db.models.fields.IntegerField', [], {}) }, 'samples.samplerun': { 'Meta': {'object_name': 'SampleRun', 'db_table': "'sample_run'"}, 'completed': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}), 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), 'genome': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['genome.Genome']", 'null': 'True', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), 'notes': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'sample': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['samples.Sample']"}) }, 'variants.variant': { 'Meta': {'unique_together': "(('chr', 'pos', 'ref', 'alt'),)", 'object_name': 'Variant', 'db_table': "'variant'"}, 'alt': ('django.db.models.fields.TextField', [], {'db_index': 'True'}), 'articles': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['literature.PubMed']", 'db_table': "'variant_pubmed'", 'symmetrical': 'False'}), 'chr': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['genome.Chromosome']"}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'liftover': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}), 'md5': ('django.db.models.fields.CharField', [], {'max_length': '32'}), 'phenotypes': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['phenotypes.Phenotype']", 'through': "orm['variants.VariantPhenotype']", 'symmetrical': 'False'}), 'pos': ('django.db.models.fields.IntegerField', [], {}), 'ref': ('django.db.models.fields.TextField', [], {'db_index': 'True'}), 'rsid': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['variants.VariantType']", 'null': 'True'}) }, 'variants.variantphenotype': { 'Meta': {'object_name': 'VariantPhenotype', 'db_table': "'variant_phenotype'"}, 'hgmd_id': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'phenotype': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['phenotypes.Phenotype']"}), 'variant': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['variants.Variant']"}) }, 'variants.varianttype': { 'Meta': {'ordering': "['order']", 'object_name': 'VariantType', 'db_table': "'variant_type'"}, 'code': ('django.db.models.fields.IntegerField', [], {'null': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'label': ('django.db.models.fields.CharField', [], {'max_length': '20'}), 'order': ('django.db.models.fields.IntegerField', [], {'default': '0'}), 'value': ('django.db.models.fields.CharField', [], {'max_length': '20'}) } } complete_apps = ['samples']
bsd-2-clause
-32,546,773,490,712,990
78.835052
192
0.544594
false
3.663198
false
false
false
kingtaurus/cs224d
assignment1/q3_word2vec_sol.py
1
7778
import numpy as np import random from q1_softmax_sol import softmax_sol as softmax from q2_gradcheck import gradcheck_naive from q2_sigmoid_sol import sigmoid_sol as sigmoid from q2_sigmoid_sol import sigmoid_grad_sol as sigmoid_grad def normalizeRows_sol(x): """ Row normalization function """ # Implement a function that normalizes each row of a matrix to have unit length ### YOUR CODE HERE N = x.shape[0] x /= np.sqrt(np.sum(x**2, axis=1)).reshape((N,1)) + 1e-30 ### END YOUR CODE return x def softmaxCostAndGradient_sol(predicted, target, outputVectors, dataset): """ Softmax cost function for word2vec models """ # Implement the cost and gradients for one predicted word vector # and one target word vector as a building block for word2vec # models, assuming the softmax prediction function and cross # entropy loss. # Inputs: # - predicted: numpy ndarray, predicted word vector (\hat{v} in # the written component or \hat{r} in an earlier version) # - target: integer, the index of the target word # - outputVectors: "output" vectors (as rows) for all tokens # - dataset: needed for negative sampling, unused here. # Outputs: # - cost: cross entropy cost for the softmax word prediction # - gradPred: the gradient with respect to the predicted word # vector # - grad: the gradient with respect to all the other word # vectors # We will not provide starter code for this function, but feel # free to reference the code you previously wrote for this # assignment! ### YOUR CODE HERE probabilities = softmax(predicted.dot(outputVectors.T)) cost = -np.log(probabilities[target]) delta = probabilities delta[target] -= 1 N = delta.shape[0] D = predicted.shape[0] grad = delta.reshape((N,1)) * predicted.reshape((1,D)) gradPred = (delta.reshape((1,N)).dot(outputVectors)).flatten() ### END YOUR CODE return cost, gradPred, grad def negSamplingCostAndGradient_sol(predicted, target, outputVectors, dataset, K=10): """ Negative sampling cost function for word2vec models """ # Implement the cost and gradients for one predicted word vector # and one target word vector as a building block for word2vec # models, using the negative sampling technique. K is the sample # size. You might want to use dataset.sampleTokenIdx() to sample # a random word index. # # Note: See test_word2vec below for dataset's initialization. # # Input/Output Specifications: same as softmaxCostAndGradient # We will not provide starter code for this function, but feel # free to reference the code you previously wrote for this # assignment! ### YOUR CODE HERE grad = np.zeros(outputVectors.shape) gradPred = np.zeros(predicted.shape) indices = [target] for k in range(K): newidx = dataset.sampleTokenIdx() while newidx == target: newidx = dataset.sampleTokenIdx() indices += [newidx] labels = np.array([1] + [-1 for k in range(K)]) vecs = outputVectors[indices,:] t = sigmoid(vecs.dot(predicted) * labels) cost = -np.sum(np.log(t)) delta = labels * (t - 1) gradPred = delta.reshape((1,K+1)).dot(vecs).flatten() gradtemp = delta.reshape((K+1,1)).dot(predicted.reshape( (1,predicted.shape[0]))) for k in range(K+1): grad[indices[k]] += gradtemp[k,:] # t = sigmoid(predicted.dot(outputVectors[target,:])) # cost = -np.log(t) # delta = t - 1 # gradPred += delta * outputVectors[target, :] # grad[target, :] += delta * predicted # for k in range(K): # idx = dataset.sampleTokenIdx() # t = sigmoid(-predicted.dot(outputVectors[idx,:])) # cost += -np.log(t) # delta = 1 - t # gradPred += delta * outputVectors[idx, :] # grad[idx, :] += delta * predicted ### END YOUR CODE return cost, gradPred, grad def skipgram_sol(currentWord, C, contextWords, tokens, inputVectors, outputVectors, dataset, word2vecCostAndGradient = softmaxCostAndGradient_sol): """ Skip-gram model in word2vec """ # Implement the skip-gram model in this function. # Inputs: # - currrentWord: a string of the current center word # - C: integer, context size # - contextWords: list of no more than 2*C strings, the context words # - tokens: a dictionary that maps words to their indices in # the word vector list # - inputVectors: "input" word vectors (as rows) for all tokens # - outputVectors: "output" word vectors (as rows) for all tokens # - word2vecCostAndGradient: the cost and gradient function for # a prediction vector given the target word vectors, # could be one of the two cost functions you # implemented above # Outputs: # - cost: the cost function value for the skip-gram model # - grad: the gradient with respect to the word vectors # We will not provide starter code for this function, but feel # free to reference the code you previously wrote for this # assignment! ### YOUR CODE HERE currentI = tokens[currentWord] predicted = inputVectors[currentI, :] cost = 0.0 gradIn = np.zeros(inputVectors.shape) gradOut = np.zeros(outputVectors.shape) for cwd in contextWords: idx = tokens[cwd] cc, gp, gg = word2vecCostAndGradient(predicted, idx, outputVectors, dataset) cost += cc gradOut += gg gradIn[currentI, :] += gp ### END YOUR CODE return cost, gradIn, gradOut def cbow_sol(currentWord, C, contextWords, tokens, inputVectors, outputVectors, dataset, word2vecCostAndGradient = softmaxCostAndGradient_sol): """ CBOW model in word2vec """ # Implement the continuous bag-of-words model in this function. # Input/Output specifications: same as the skip-gram model # We will not provide starter code for this function, but feel # free to reference the code you previously wrote for this # assignment! ################################################################# # IMPLEMENTING CBOW IS EXTRA CREDIT, DERIVATIONS IN THE WRIITEN # # ASSIGNMENT ARE NOT! # ################################################################# cost = 0 gradIn = np.zeros(inputVectors.shape) gradOut = np.zeros(outputVectors.shape) ### YOUR CODE HERE D = inputVectors.shape[1] predicted = np.zeros((D,)) indices = [tokens[cwd] for cwd in contextWords] for idx in indices: predicted += inputVectors[idx, :] cost, gp, gradOut = word2vecCostAndGradient(predicted, tokens[currentWord], outputVectors, dataset) gradIn = np.zeros(inputVectors.shape) for idx in indices: gradIn[idx, :] += gp ### END YOUR CODE return cost, gradIn, gradOut
mit
4,524,440,371,980,069,400
43.193182
120
0.576369
false
4.250273
false
false
false
taxpon/sverchok
ui/sv_icons.py
1
1302
import bpy import os import glob import bpy.utils.previews # custom icons dictionary _icon_collection = {} def custom_icon(name): load_custom_icons() # load in case they custom icons not already loaded custom_icons = _icon_collection["main"] default = lambda: None # for no icon with given name will return zero default.icon_id = 0 return custom_icons.get(name, default).icon_id def load_custom_icons(): if len(_icon_collection): # return if custom icons already loaded return custom_icons = bpy.utils.previews.new() iconsDir = os.path.join(os.path.dirname(__file__), "icons") iconPattern = "sv_*.png" iconPath = os.path.join(iconsDir, iconPattern) iconFiles = [os.path.basename(x) for x in glob.glob(iconPath)] for iconFile in iconFiles: iconName = os.path.splitext(iconFile)[0] iconID = iconName.upper() custom_icons.load(iconID, os.path.join(iconsDir, iconFile), "IMAGE") _icon_collection["main"] = custom_icons def remove_custom_icons(): for custom_icons in _icon_collection.values(): bpy.utils.previews.remove(custom_icons) _icon_collection.clear() def register(): load_custom_icons() def unregister(): remove_custom_icons() if __name__ == '__main__': register()
gpl-3.0
1,090,800,781,951,556,500
23.111111
76
0.667435
false
3.481283
false
false
false
romeotestuser/glimsol_report
report/billing_statement.py
1
2348
# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero 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 Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # ############################################################################## import time from openerp import netsvc from openerp.netsvc import Service for x in ['report.glimsol.billing.statement']: try: del Service._services[x] except: pass from openerp.report import report_sxw class billing(report_sxw.rml_parse): def __init__(self, cr, uid, name, context=None): super(billing, self).__init__(cr, uid, name, context=context) self.localcontext.update({ 'time': time, 'get_line':self._get_line, 'get_total_si_amount':self._get_total_si_amount, 'get_total_ticket_amount':self._get_total_ticket_amount, 'get_user_ref':self._get_user_ref, }) def _get_line(self,obj): res=[] return res def _get_total_si_amount(self,obj): res=[] return res def _get_total_ticket_amount(self,obj): res=[] return res def _get_user_ref(self,obj,trigger): for target_trigger in ['sales executive','courier','customer']: if target_trigger != trigger: continue res = [] return res report_sxw.report_sxw('report.glimsol.billing.statement', 'account.billing', 'addons/glimsol_report/report/billing_statement.rml', parser=billing, header="external")
gpl-2.0
385,092,275,381,778,500
32.557143
165
0.57879
false
4.185383
false
false
false
nash-x/hws
neutron/plugins/l2_proxy/agent/clients.py
1
9765
# Copyright 2014, Huawei, Inc. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import functools from oslo.config import cfg from neutron import context as n_context from neutron.openstack.common import importutils from neutron.openstack.common import log as logging from neutron.openstack.common import excutils logger = logging.getLogger(__name__) from neutron.plugins.l2_proxy.agent import neutron_proxy_context from neutron.plugins.l2_proxy.agent import neutron_keystoneclient as hkc from novaclient import client as novaclient from novaclient import shell as novashell from neutronclient.common import exceptions try: from swiftclient import client as swiftclient except ImportError: swiftclient = None logger.info('swiftclient not available') try: from neutronclient.v2_0 import client as neutronclient except ImportError: neutronclient = None logger.info('neutronclient not available') try: from cinderclient import client as cinderclient except ImportError: cinderclient = None logger.info('cinderclient not available') try: from ceilometerclient.v2 import client as ceilometerclient except ImportError: ceilometerclient = None logger.info('ceilometerclient not available') cloud_opts = [ cfg.StrOpt('cloud_backend', default=None, help="Cloud module to use as a backend. Defaults to OpenStack.") ] cfg.CONF.register_opts(cloud_opts) CASCADING = 'cascading' CASCADED = 'cascaded' class OpenStackClients(object): ''' Convenience class to create and cache client instances. ''' def __init__(self, context): self.context = context self._nova = {} self._keystone = None self._swift = None self._neutron = None self._cinder = None self._ceilometer = None @property def auth_token(self): # if there is no auth token in the context # attempt to get one using the context username and password return self.context.auth_token or self.keystone().auth_token def keystone(self): if self._keystone: return self._keystone self._keystone = hkc.KeystoneClient(self.context) return self._keystone def url_for(self, **kwargs): return self.keystone().url_for(**kwargs) def nova(self, service_type='compute'): if service_type in self._nova: return self._nova[service_type] con = self.context if self.auth_token is None: logger.error("Nova connection failed, no auth_token!") return None computeshell = novashell.OpenStackComputeShell() extensions = computeshell._discover_extensions("1.1") args = { 'project_id': con.tenant_id, 'auth_url': con.auth_url, 'service_type': service_type, 'username': None, 'api_key': None, 'extensions': extensions } client = novaclient.Client(1.1, **args) management_url = self.url_for( service_type=service_type, attr='region', filter_value='RegionTwo') client.client.auth_token = self.auth_token client.client.management_url = management_url self._nova[service_type] = client return client def swift(self): if swiftclient is None: return None if self._swift: return self._swift con = self.context if self.auth_token is None: logger.error("Swift connection failed, no auth_token!") return None args = { 'auth_version': '2.0', 'tenant_name': con.tenant_id, 'user': con.username, 'key': None, 'authurl': None, 'preauthtoken': self.auth_token, 'preauthurl': self.url_for(service_type='object-store') } self._swift = swiftclient.Connection(**args) return self._swift def neutron(self): if neutronclient is None: return None if self._neutron: return self._neutron con = self.context if self.auth_token is None: logger.error("Neutron connection failed, no auth_token!") return None if self.context.region_name is None: management_url = self.url_for(service_type='network', endpoint_type='publicURL') else: management_url = self.url_for( service_type='network', attr='region', endpoint_type='publicURL', filter_value=self.context.region_name) args = { 'auth_url': con.auth_url, 'insecure': self.context.insecure, 'service_type': 'network', 'token': self.auth_token, 'endpoint_url': management_url } self._neutron = neutronclient.Client(**args) return self._neutron def cinder(self): if cinderclient is None: return self.nova('volume') if self._cinder: return self._cinder con = self.context if self.auth_token is None: logger.error("Cinder connection failed, no auth_token!") return None args = { 'service_type': 'volume', 'auth_url': con.auth_url, 'project_id': con.tenant_id, 'username': None, 'api_key': None } self._cinder = cinderclient.Client('1', **args) management_url = self.url_for(service_type='volume') self._cinder.client.auth_token = self.auth_token self._cinder.client.management_url = management_url return self._cinder def ceilometer(self): if ceilometerclient is None: return None if self._ceilometer: return self._ceilometer if self.auth_token is None: logger.error("Ceilometer connection failed, no auth_token!") return None con = self.context args = { 'auth_url': con.auth_url, 'service_type': 'metering', 'project_id': con.tenant_id, 'token': lambda: self.auth_token, 'endpoint': self.url_for(service_type='metering'), } client = ceilometerclient.Client(**args) self._ceilometer = client return self._ceilometer if cfg.CONF.cloud_backend: cloud_backend_module = importutils.import_module(cfg.CONF.cloud_backend) Clients = cloud_backend_module.Clients else: Clients = OpenStackClients logger.debug('Using backend %s' % Clients) def get_cascade_neutron_client(mode): if mode == CASCADING: region_name = cfg.CONF.AGENT.region_name elif mode == CASCADED: region_name = cfg.CONF.AGENT.neutron_region_name else: logger.error(_('Must be input mode(cascading or cascaded).')) raise context = n_context.get_admin_context_without_session() neutron_admin_auth_url = cfg.CONF.AGENT.neutron_admin_auth_url kwargs = {'auth_token': None, 'username': cfg.CONF.AGENT.neutron_admin_user, 'password': cfg.CONF.AGENT.admin_password, 'aws_creds': None, 'tenant': cfg.CONF.AGENT.neutron_admin_tenant_name, 'auth_url': neutron_admin_auth_url, 'insecure': cfg.CONF.AGENT.auth_insecure, 'roles': context.roles, 'is_admin': context.is_admin, 'region_name': region_name} reqCon = neutron_proxy_context.RequestContext(**kwargs) openStackClients = OpenStackClients(reqCon) neutronClient = openStackClients.neutron() return neutronClient def check_neutron_client_valid(function): @functools.wraps(function) def decorated_function(self, method_name, *args, **kwargs): retry = 0 while(True): try: return function(self, method_name, *args, **kwargs) except exceptions.Unauthorized: retry = retry + 1 if(retry <= 3): self.client = get_cascade_neutron_client(self.mode) continue else: with excutils.save_and_reraise_exception(): logger.error(_('Try 3 times, Unauthorized.')) return None return decorated_function class CascadeNeutronClient(object): def __init__(self, mode): #mode is cascading or cascaded self.mode = mode self.client = get_cascade_neutron_client(self.mode) @check_neutron_client_valid def __call__(self, method_name, *args, **kwargs): method = getattr(self.client, method_name) if method: return method(*args, **kwargs) else: raise Exception('can not find the method') @check_neutron_client_valid def execute(self, method_name, *args, **kwargs): method = getattr(self.client, method_name) if method: return method(*args, **kwargs) else: raise Exception('can not find the method')
apache-2.0
659,710,628,943,577,700
30.704545
79
0.603277
false
4.198194
false
false
false
sibskull/synaptiks
synaptiks/monitors/mouses.py
1
8959
# -*- coding: utf-8 -*- # Copyright (c) 2011, Sebastian Wiesner <[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. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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. """ synaptiks.monitors.mouses ========================= Implementation of mouse monitoring. .. moduleauthor:: Sebastian Wiesner <[email protected]> """ from __future__ import (print_function, division, unicode_literals, absolute_import) from collections import namedtuple from itertools import ifilter import pyudev from pyudev.pyqt4 import QUDevMonitorObserver from PyQt4.QtCore import QObject, pyqtSignal from synaptiks.monitors.power import create_resume_monitor __all__ = ['MouseDevicesManager', 'MouseDevicesMonitor', 'MouseDevice'] def _is_mouse(device): return (device.sys_name.startswith('event') and device.get('ID_INPUT_MOUSE') == '1' and not device.get('ID_INPUT_TOUCHPAD') == '1') class MouseDevice(namedtuple('_MouseDevice', ['serial', 'name'])): """ A :func:`~collections.namedtuple()` representing a mouse device. A mouse device currently has two attributes, the order corresponds to the tuple index: - :attr:`serial` - :attr:`name` """ @classmethod def from_udev(cls, device): """ Create a :class:`MouseDevice` tuple from a :class:`pyudev.Device`. """ # The name is available from the parent device of the actual event # device. The parent represents the actual physical device. The name # may be decorated with quotation marks, which are removed for the sake # of a clean represenation return cls(device['ID_SERIAL'], device.parent['NAME'].strip('"')) class MouseDevicesMonitor(QObject): """ Watch for plugged or unplugged mouse devices. """ #: Qt signal, which is emitted, when a mouse is plugged. The slot gets a #: single argument of :class:`MouseDevice`, which represents the plugged #: mouse device mousePlugged = pyqtSignal(MouseDevice) #: Qt signal, which is emitted, when a mouse is unplugged. The slot gets a #: single argument of type :class:`MouseDevice`, which represents the #: unplugged mouse device mouseUnplugged = pyqtSignal(MouseDevice) def __init__(self, parent=None): """ Create a new monitor. ``parent`` is the parent :class:`~PyQt4.QtCore.QObject`. """ QObject.__init__(self, parent) self._udev = pyudev.Context() self._notifier = QUDevMonitorObserver( pyudev.Monitor.from_netlink(self._udev), self) self._notifier.deviceEvent.connect(self._handle_udev_event) self._notifier.monitor.filter_by('input') self._notifier.monitor.start() self._event_signal_map = dict( add=self.mousePlugged, remove=self.mouseUnplugged) @property def plugged_devices(self): """ An iterator over all currently plugged mouse devices as :class:`MouseDevice` objects. """ devices = self._udev.list_devices( subsystem='input', ID_INPUT_MOUSE=True) for device in ifilter(_is_mouse, devices): yield MouseDevice.from_udev(device) def _handle_udev_event(self, evt, device): signal = self._event_signal_map.get(unicode(evt)) if signal and _is_mouse(device): signal.emit(MouseDevice.from_udev(device)) class MouseDevicesManager(MouseDevicesMonitor): """ Manage mouse devices. This class derives from :class:`MouseDevicesMonitor` to provide more advanced monitoring of mouse devices. In addition to the basic monitoring provided by :class:`MouseDevicesMonitor` this class keeps a record of currently plugged devices, and thus also informs about the *first* mouse plugged, and the *last* mouse unplugged. """ #: Qt signal, which is emitted if the first mouse is plugged. The slot : #: gets a single argument, which is the plugged :class:`MouseDevice`. firstMousePlugged = pyqtSignal(MouseDevice) #: Qt signal, which is emitted if the last mouse is unplugged. The slot : #: gets a single argument, which is the plugged :class:`MouseDevice`. lastMouseUnplugged = pyqtSignal(MouseDevice) def __init__(self, parent=None): """ Create a new manager. ``parent`` is the parent ``QObject``. """ MouseDevicesMonitor.__init__(self, parent) self._resume_monitor = create_resume_monitor(self) self._mouse_registry = set() self._ignored_mouses = frozenset() self.is_running = False def start(self): """ Start to observe mouse devices. Does nothing, if the manager is already running. """ if not self.is_running: self.mousePlugged.connect(self._register_mouse) self.mouseUnplugged.connect(self._unregister_mouse) if self._resume_monitor: self._resume_monitor.resuming.connect(self._reset_registry) self._reset_registry() self.is_running = True def stop(self): """ Stop to observe mouse devices. Does nothing, if the manager is not running. """ if self.is_running: self.mousePlugged.disconnect(self._register_mouse) self.mouseUnplugged.disconnect(self._unregister_mouse) if self._resume_monitor: self._resume_monitor.resuming.disconnect(self._reset_registry) self._clear_registry() self.is_running = False def _unregister_mouse(self, device): """ Unregister the given mouse ``device``. If this is the last plugged mouse, :attr:`lastMouseUnplugged` is emitted with the given ``device``. """ try: self._mouse_registry.remove(device) except KeyError: pass else: if not self._mouse_registry: self.lastMouseUnplugged.emit(device) def _register_mouse(self, device): """ Register the given mouse ``device``. If this is the first plugged mouse, :attr:`firstMousePlugged` is emitted with the given ``device``. """ if device.serial not in self._ignored_mouses: if not self._mouse_registry: self.firstMousePlugged.emit(device) self._mouse_registry.add(device) def _reset_registry(self): """ Re-register all plugged mouses. """ self._clear_registry() for device in self.plugged_devices: self._register_mouse(device) def _clear_registry(self): """ Clear the registry of plugged mouse devices. """ for device in list(self._mouse_registry): self._unregister_mouse(device) @property def ignored_mouses(self): """ The list of ignored mouses. This property holds a list of serial numbers. Mouse devices with these serial numbers are simply ignored when plugged or unplugged. Modifying the returned list in place does not have any effect, assign to this property to change the list of ignored devices. You may also assign a list of :class:`~synaptiks.monitors.MouseDevice` objects. """ return list(self._ignored_mouses) @ignored_mouses.setter def ignored_mouses(self, devices): devices = set(d if isinstance(d, basestring) else d.serial for d in devices) if self._ignored_mouses != devices: self._ignored_mouses = devices if self.is_running: self._reset_registry()
bsd-2-clause
-3,596,675,670,361,718,300
35.717213
79
0.650854
false
4.227938
false
false
false
snoopycrimecop/openmicroscopy
components/tools/OmeroPy/test/integration/gatewaytest/test_get_objects.py
1
45419
#!/usr/bin/env python # -*- coding: utf-8 -*- """ gateway tests - Testing the gateway.getObject() and deleteObjects() methods Copyright 2013-2015 Glencoe Software, Inc. All rights reserved. Use is subject to license terms supplied in LICENSE.txt pytest fixtures used as defined in conftest.py: - gatewaywrapper - author_testimg_generated - author_testimg_tiny """ from builtins import str from builtins import range from builtins import object import omero import uuid import pytest from omero.gateway.scripts import dbhelpers from omero.rtypes import wrap, rlong from omero.testlib import ITest from omero.gateway import BlitzGateway, KNOWN_WRAPPERS, DatasetWrapper, \ ProjectWrapper, ImageWrapper, ScreenWrapper, PlateWrapper from omero.model import DatasetI, \ ImageI, \ PlateI, \ ScreenI, \ WellI, \ WellSampleI try: int except Exception: # Python 3 long = int class TestDeleteObject (object): def testDeleteAnnotation(self, author_testimg_generated): image = author_testimg_generated gateway = image._conn # create Tag on Image and try to delete Tag tag = omero.gateway.TagAnnotationWrapper(gateway) ns_tag = "omero.gateway.test.get_objects.test_delete_annotation_tag" tag.setNs(ns_tag) tag.setValue("Test Delete Tag") tag = image.linkAnnotation(tag) tagId = tag.getId() handle = gateway.deleteObjects("Annotation", [tagId]) gateway._waitOnCmd(handle) assert gateway.getObject("Annotation", tagId) is None def testDeleteImage(self, gatewaywrapper, author_testimg_generated): image = author_testimg_generated imageId = image.getId() project = gatewaywrapper.getTestProject() projectId = project.getId() ns = "omero.gateway.test.get_objects.test_delete_image_comment" ns_tag = "omero.gateway.test.get_objects.test_delete_image_tag" # create Comment ann = omero.gateway.CommentAnnotationWrapper(gatewaywrapper.gateway) ann.setNs(ns) ann.setValue("Test Comment") ann = image.linkAnnotation(ann) # create Tag tag = omero.gateway.TagAnnotationWrapper(gatewaywrapper.gateway) tag.setNs(ns_tag) tag.setValue("Test Tag") tag = image.linkAnnotation(tag) # check the Comment assert gatewaywrapper.gateway.getObject( "Annotation", ann.id) is not None assert gatewaywrapper.gateway.getObject( "Annotation", tag.id) is not None # check Image, delete (wait) and check assert gatewaywrapper.gateway.getObject("Image", imageId) is not None handle = gatewaywrapper.gateway.deleteObjects("Image", [imageId]) gatewaywrapper.gateway._waitOnCmd(handle) assert gatewaywrapper.gateway.getObject("Image", imageId) is None # Comment should be deleted but not the Tag (becomes orphan) assert gatewaywrapper.gateway.getObject("Annotation", ann.id) is None assert gatewaywrapper.gateway.getObject( "Annotation", tag.id) is not None # Add the tag to project and delete (with Tags) assert gatewaywrapper.gateway.getObject( "Project", projectId) is not None project.linkAnnotation(tag) datasetIds = [d.getId() for d in project.listChildren()] assert len(datasetIds) > 0 handle = gatewaywrapper.gateway.deleteObjects( "Project", [projectId], deleteAnns=True, deleteChildren=True) gatewaywrapper.gateway._waitOnCmd(handle) assert gatewaywrapper.gateway.getObject("Project", projectId) is None assert gatewaywrapper.gateway.getObject("Annotation", tag.id) is None # Tag should be gone # check datasets gone too for dId in datasetIds: assert gatewaywrapper.gateway.getObject("Dataset", dId) is None class TestFindObject (object): def testIllegalObjTypeInt(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() with pytest.raises(AttributeError): gatewaywrapper.gateway.getObject(1, int(1)) def testObjTypeUnicode(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() a = gatewaywrapper.getTestProject() b = gatewaywrapper.gateway.getObject(u'Project', a.getId()) assert a.getId() == b.getId() def testObjTypeString(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() a = gatewaywrapper.getTestProject() b = gatewaywrapper.gateway.getObject('Project', a.getId()) assert a.getId() == b.getId() def testFindProject(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() project = gatewaywrapper.getTestProject() pName = project.getName() findProjects = list(gatewaywrapper.gateway.getObjects( "Project", None, attributes={"name": pName})) assert len(findProjects) > 0, "Did not find Project by name" for p in findProjects: assert p.getName() == pName, \ "All projects should have queried name" def testFindExperimenter(self, gatewaywrapper, author_testimg_tiny): omeName = author_testimg_tiny.getOwnerOmeName() group = author_testimg_tiny.getDetails().getGroup() groupName = group.getName() gatewaywrapper.loginAsAdmin() # findObjects findAuthor = list(gatewaywrapper.gateway.getObjects( "Experimenter", None, attributes={"omeName": omeName})) assert len(findAuthor) == 1, "Did not find Experimenter by omeName" assert findAuthor[0].omeName == omeName # findObject author = gatewaywrapper.gateway.getObject( "Experimenter", None, attributes={"omeName": omeName}) assert author is not None assert author.omeName == omeName # find group grp = gatewaywrapper.gateway.getObject( "ExperimenterGroup", None, attributes={"name": groupName}) assert grp is not None assert grp.getName() == groupName def testFindAnnotation(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() # start by deleting any tag created by this method that may have been # left behind tag_value = "FindThisTag" find_ns = "omero.gateway.test.test_find_annotations" find_tag = gatewaywrapper.gateway.getObjects( "Annotation", attributes={"textValue": tag_value, "ns": find_ns}) ids = [t._obj.id.val for t in find_tag] if ids: gatewaywrapper.gateway.deleteObjects("Annotation", ids, wait=True) # create Tag tag = omero.gateway.TagAnnotationWrapper(gatewaywrapper.gateway) tag.setNs(find_ns) tag.setValue(tag_value) tag.save() tagId = tag.getId() # findObject by name find_tag = gatewaywrapper.gateway.getObject( "Annotation", attributes={"textValue": tag_value}) assert find_tag is not None assert find_tag.getValue() == tag_value # find by namespace find_tag = gatewaywrapper.gateway.getObject( "Annotation", attributes={"ns": find_ns}) assert find_tag is not None assert find_tag.getNs() == find_ns # find by text value find_tag = gatewaywrapper.gateway.getObject( "TagAnnotation", attributes={"textValue": tag_value}) assert find_tag is not None assert find_tag.getValue() == tag_value # create some other annotations... (not linked!) longAnn = omero.gateway.LongAnnotationWrapper(gatewaywrapper.gateway) longAnn.setValue(12345) longAnn.save() longId = longAnn.getId() boolAnn = omero.gateway.BooleanAnnotationWrapper( gatewaywrapper.gateway) boolAnn.setValue(True) boolAnn.save() boolId = boolAnn.getId() commAnn = omero.gateway.CommentAnnotationWrapper( gatewaywrapper.gateway) commAnn.setValue("This is a blitz gatewaytest Comment.") commAnn.save() commId = commAnn.getId() fileAnn = omero.gateway.FileAnnotationWrapper(gatewaywrapper.gateway) # An original file object needs to be linked to the annotation or it # will fail to be loaded on getObject(s). fileObj = omero.model.OriginalFileI() fileObj = omero.gateway.OriginalFileWrapper( gatewaywrapper.gateway, fileObj) fileObj.setName(omero.rtypes.rstring('a')) fileObj.setPath(omero.rtypes.rstring('a')) fileObj.setHash(omero.rtypes.rstring('a')) fileObj.setSize(omero.rtypes.rlong(0)) fileObj.save() fileAnn.setFile(fileObj) fileAnn.save() fileId = fileAnn.getId() doubleAnn = omero.gateway.DoubleAnnotationWrapper( gatewaywrapper.gateway) doubleAnn.setValue(1.23456) doubleAnn.save() doubleId = doubleAnn.getId() termAnn = omero.gateway.TermAnnotationWrapper(gatewaywrapper.gateway) termAnn.setValue("Metaphase") termAnn.save() termId = termAnn.getId() timeAnn = omero.gateway.TimestampAnnotationWrapper( gatewaywrapper.gateway) timeAnn.setValue(1000) timeAnn.save() timeId = timeAnn.getId() # list annotations of various types - check they include ones from # above tags = list(gatewaywrapper.gateway.getObjects("TagAnnotation")) for t in tags: assert t.OMERO_TYPE == tag.OMERO_TYPE assert tagId in [t.getId() for t in tags] longs = list(gatewaywrapper.gateway.getObjects("LongAnnotation")) for lng in longs: assert lng.OMERO_TYPE == longAnn.OMERO_TYPE assert longId in [lng.getId() for lng in longs] bools = list(gatewaywrapper.gateway.getObjects("BooleanAnnotation")) for b in bools: assert b.OMERO_TYPE == boolAnn.OMERO_TYPE assert boolId in [b.getId() for b in bools] comms = list(gatewaywrapper.gateway.getObjects("CommentAnnotation")) for c in comms: assert c.OMERO_TYPE == commAnn.OMERO_TYPE assert commId in [c.getId() for c in comms] files = list(gatewaywrapper.gateway.getObjects("FileAnnotation")) for f in files: assert f.OMERO_TYPE == fileAnn.OMERO_TYPE assert fileId in [f.getId() for f in files] doubles = list(gatewaywrapper.gateway.getObjects("DoubleAnnotation")) for d in doubles: assert d.OMERO_TYPE == doubleAnn.OMERO_TYPE assert doubleId in [d.getId() for d in doubles] terms = list(gatewaywrapper.gateway.getObjects("TermAnnotation")) for t in terms: assert t.OMERO_TYPE == termAnn.OMERO_TYPE assert termId in [t.getId() for t in terms] times = list(gatewaywrapper.gateway.getObjects("TimestampAnnotation")) for t in times: assert t.OMERO_TYPE == timeAnn.OMERO_TYPE assert timeId in [t.getId() for t in times] # delete what we created gatewaywrapper.gateway.deleteObjects( "Annotation", [longId, boolId, fileId, commId, tagId], wait=True) assert gatewaywrapper.gateway.getObject("Annotation", longId) is None assert gatewaywrapper.gateway.getObject("Annotation", boolId) is None assert gatewaywrapper.gateway.getObject("Annotation", fileId) is None assert gatewaywrapper.gateway.getObject("Annotation", commId) is None assert gatewaywrapper.gateway.getObject("Annotation", tagId) is None class TestGetObject (ITest): def testSearchObjects(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() # search for Projects pros = list(gatewaywrapper.gateway.searchObjects( ["Project"], "weblitz")) for p in pros: # assert p.getId() in projectIds assert p.OMERO_CLASS == "Project", "Should only return Projects" # P/D/I is default objects to search # pdis = list( gatewaywrapper.gateway.simpleSearch("weblitz") ) # # method removed from blitz gateway # pdis.sort(key=lambda r: "%s%s"%(r.OMERO_CLASS, r.getId()) ) pdiResult = list(gatewaywrapper.gateway.searchObjects( None, "weblitz")) pdiResult.sort(key=lambda r: "%s%s" % (r.OMERO_CLASS, r.getId())) # can directly check that sorted lists are the same # for r1, r2 in zip(pdis, pdiResult): # assert r1.OMERO_CLASS == r2.OMERO_CLASS # assert r1.getId() == r2.getId() def testListProjects(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() # params limit query by owner params = omero.sys.Parameters() params.theFilter = omero.sys.Filter() conn = gatewaywrapper.gateway # should be no Projects owned by root (in the current group) params.theFilter.ownerId = omero.rtypes.rlong(0) # owned by 'root' pros = conn.getObjects("Project", None, params) assert len(list(pros)) == 0, "Should be no Projects owned by root" # Also filter by owner using opts dict pros = conn.getObjects("Project", None, opts={'owner': 0}) assert len(list(pros)) == 0, "Should be no Projects owned by root" # filter by current user should get same as above. # owned by 'author' params.theFilter.ownerId = omero.rtypes.rlong( conn.getEventContext().userId) pros = list(conn.getObjects( "Project", None, params)) projects = list(conn.listProjects()) # check unordered lists are the same length & ids assert len(pros) == len(projects) projectIds = [p.getId() for p in projects] for p in pros: assert p.getId() in projectIds def testPagination(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() params = omero.sys.ParametersI() # Only 3 images available limit = 2 params.page(0, limit) pros = list(gatewaywrapper.gateway.getObjects( "Project", None, params)) assert len(pros) == limit # Also using opts dict pros = list(gatewaywrapper.gateway.getObjects( "Project", None, opts={'offset': 0, 'limit': 2})) assert len(pros) == limit def testGetDatasetsByProject(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() allDs = list(gatewaywrapper.gateway.getObjects("Dataset")) # Get Datasets by project.listChildren()... project = gatewaywrapper.getTestProject() dsIds = [d.id for d in project.listChildren()] # Get Datasets, filtering by project p = {'project': project.id} datasets = list(gatewaywrapper.gateway.getObjects("Dataset", opts=p)) # Check that not all Datasets are in Project (or test is invalid) assert len(allDs) > len(dsIds) # Should get same result both methods assert len(datasets) == len(dsIds) for d in datasets: assert d.id in dsIds @pytest.mark.parametrize("load_gem", [True, False]) def testListExperimentersAndGroups(self, gatewaywrapper, load_gem): gatewaywrapper.loginAsAuthor() conn = gatewaywrapper.gateway # experimenters - load_experimentergroups True by default opts = {'limit': 10} if not load_gem: opts['load_experimentergroups'] = False exps = conn.getObjects("Experimenter", opts=opts) for e in exps: # check iQuery has loaded at least one group assert e._obj.groupExperimenterMapLoaded == load_gem e.copyGroupExperimenterMap() # groups. load_experimenters True by default opts = {'limit': 10} if not load_gem: opts['load_experimenters'] = False gps = conn.getObjects("ExperimenterGroup", opts=opts) for grp in gps: assert grp._obj.groupExperimenterMapLoaded == load_gem grp.copyGroupExperimenterMap() def testListColleagues(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() conn = gatewaywrapper.gateway # uses gateway.getObjects("ExperimenterGroup") - check this doesn't # throw colleagues = conn.listColleagues() for e in colleagues: e.getOmeName() def testFindExperimenterWithGroups(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() conn = gatewaywrapper.gateway # check we can find some groups exp = conn.getObject( "Experimenter", attributes={'omeName': gatewaywrapper.USER.name}) for groupExpMap in exp.copyGroupExperimenterMap(): gName = groupExpMap.parent.name.val gId = groupExpMap.parent.id.val findG = gatewaywrapper.gateway.getObject( "ExperimenterGroup", attributes={'name': gName}) assert gId == findG.id, "Check we found the same group" @pytest.mark.parametrize("load", [True, False]) def testGetExperimentersByGroup(self, gatewaywrapper, load): """ Filter Groups by Experimenters and vice versa. We test with and without loading experimenters/groups to check that the query is built correctly in both cases """ gatewaywrapper.loginAsAdmin() conn = gatewaywrapper.gateway # Two users in the same group... client, exp1 = self.new_client_and_user() grp1_id = client.sf.getAdminService().getEventContext().groupId exp2 = self.new_user(group=grp1_id) # Another group with one user grp2 = self.new_group(experimenters=[exp1]) # get Groups by Experimenters (in 1 or 2 groups + user group) groups = list(conn.getObjects("ExperimenterGroup", opts={ "experimenter": exp2.id.val, 'load_experimenters': load})) assert len(groups) == 2 assert grp1_id in [g.id for g in groups] groups = list(conn.getObjects("ExperimenterGroup", opts={ "experimenter": exp1.id.val, 'load_experimenters': load})) assert len(groups) == 3 # get Experimenters by Group (returns 1 or 2 exps) exps = list(conn.getObjects("Experimenter", opts={ "experimentergroup": grp2.id.val, "load_experimentergroups": load})) assert len(exps) == 1 assert exps[0].id == exp1.id.val exps = list(conn.getObjects("Experimenter", opts={ "experimentergroup": grp1_id, "load_experimentergroups": load})) assert len(exps) == 2 def testGetExperimenter(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() noExp = gatewaywrapper.gateway.getObject( "Experimenter", attributes={'omeName': "Dummy Fake Name"}) assert noExp is None, "Should not find any matching experimenter" findExp = gatewaywrapper.gateway.getObject( "Experimenter", attributes={'omeName': gatewaywrapper.USER.name}) exp = gatewaywrapper.gateway.getObject( "Experimenter", findExp.id) assert exp.omeName == findExp.omeName # check groupExperimenterMap loaded for exp groupIds = [] for groupExpMap in exp.copyGroupExperimenterMap(): assert findExp.id == groupExpMap.child.id.val groupIds.append(groupExpMap.parent.id.val) # for groupExpMap in experimenter.copyGroupExperimenterMap(): # assert findExp.id == groupExpMap.child.id.val groupGen = gatewaywrapper.gateway.getObjects( "ExperimenterGroup", groupIds, opts={'load_experimenters': True}) groups = list(groupGen) assert len(groups) == len(groupIds) for g in groups: assert g.getId() in groupIds for m in g.copyGroupExperimenterMap(): # check exps are loaded assert m.child def testGetAnnotations(self, gatewaywrapper, author_testimg_tiny): obj = author_testimg_tiny dataset = gatewaywrapper.getTestDataset() ns = "omero.gateway.test.get_objects.test_get_annotations_comment" ns_tag = "omero.gateway.test.get_objects.test_get_annotations_tag" # create Comment ann = omero.gateway.CommentAnnotationWrapper(gatewaywrapper.gateway) ann.setNs(ns) ann.setValue("Test Comment") ann = obj.linkAnnotation(ann) # create Tag tag = omero.gateway.TagAnnotationWrapper(gatewaywrapper.gateway) tag.setNs(ns_tag) tag.setValue("Test Tag") tag = obj.linkAnnotation(tag) dataset.linkAnnotation(tag) # get the Comment annotation = gatewaywrapper.gateway.getObject( "CommentAnnotation", ann.id) assert "Test Comment" == annotation.textValue assert ann.OMERO_TYPE == annotation.OMERO_TYPE # test getObject throws exception if more than 1 returned threw = True try: gatewaywrapper.gateway.getObject("Annotation") threw = False except Exception: threw = True assert threw, "getObject() didn't throw exception with >1 result" # get the Comment and Tag annGen = gatewaywrapper.gateway.getObjects( "Annotation", [ann.id, tag.id]) anns = list(annGen) assert len(anns) == 2 assert anns[0].ns in [ns, ns_tag] assert anns[1].ns in [ns, ns_tag] assert anns[0].OMERO_TYPE != anns[1].OMERO_TYPE # get all available annotation links on the image annLinks = gatewaywrapper.gateway.getAnnotationLinks("Image") for al in annLinks: assert isinstance(al.getAnnotation(), omero.gateway.AnnotationWrapper) assert al.parent.__class__ == omero.model.ImageI # get selected links - On image only annLinks = gatewaywrapper.gateway.getAnnotationLinks( "Image", parent_ids=[obj.getId()]) for al in annLinks: assert obj.getId() == al.parent.id.val assert al.parent.__class__ == omero.model.ImageI # get selected links - On image only annLinks = gatewaywrapper.gateway.getAnnotationLinks( "Image", parent_ids=[obj.getId()]) for al in annLinks: assert obj.getId() == al.parent.id.val assert al.parent.__class__ == omero.model.ImageI # compare with getObjectsByAnnotations annImages = list(gatewaywrapper.gateway.getObjectsByAnnotations( 'Image', [tag.getId()])) assert obj.getId() in [i.getId() for i in annImages] # params limit query by owner params = omero.sys.Parameters() params.theFilter = omero.sys.Filter() # should be no links owned by root (in the current group) params.theFilter.ownerId = omero.rtypes.rlong(0) # owned by 'root' annLinks = gatewaywrapper.gateway.getAnnotationLinks( "Image", parent_ids=[obj.getId()], params=params) assert len(list(annLinks)) == 0, \ "No annotations on this image by root" # links owned by author eid = gatewaywrapper.gateway.getEventContext().userId params.theFilter.ownerId = omero.rtypes.rlong(eid) # owned by 'author' omeName = gatewaywrapper.gateway.getObject( "Experimenter", eid).getName() annLinks = gatewaywrapper.gateway.getAnnotationLinks( "Image", parent_ids=[obj.getId()], params=params) for al in annLinks: assert al.getOwnerOmeName() == omeName # all links on Image with specific ns annLinks = gatewaywrapper.gateway.getAnnotationLinks("Image", ns=ns) for al in annLinks: assert al.getAnnotation().ns == ns # get all uses of the Tag - have to check various types separately annList = list(gatewaywrapper.gateway.getAnnotationLinks( "Image", ann_ids=[tag.id])) assert len(annList) == 1 for al in annList: assert al.getAnnotation().id == tag.id annList = list(gatewaywrapper.gateway.getAnnotationLinks( "Dataset", ann_ids=[tag.id])) assert len(annList) == 1 for al in annList: assert al.getAnnotation().id == tag.id # remove annotations obj.removeAnnotations(ns) dataset.unlinkAnnotations(ns_tag) # unlink tag obj.removeAnnotations(ns_tag) # delete tag def testGetImage(self, gatewaywrapper, author_testimg_tiny): testImage = author_testimg_tiny # This should return image wrapper image = gatewaywrapper.gateway.getObject("Image", testImage.id) # test a few methods that involve lazy loading, rendering etc. assert image.getSizeZ() == testImage.getSizeZ() assert image.getSizeY() == testImage.getSizeY() image.isGreyscaleRenderingModel() # loads rendering engine testImage.isGreyscaleRenderingModel() assert image._re.getDefaultZ() == testImage._re.getDefaultZ() assert image._re.getDefaultT() == testImage._re.getDefaultT() assert image.getOwnerOmeName == testImage.getOwnerOmeName assert image.getThumbVersion() is not None @pytest.mark.parametrize("load_pixels", [True, False]) @pytest.mark.parametrize("load_channels", [True, False]) def testGetImageLoadPixels(self, load_pixels, load_channels, gatewaywrapper, author_testimg_tiny): testImage = author_testimg_tiny conn = gatewaywrapper.gateway # By default (no opts), don't load pixels image = conn.getObject("Image", testImage.id) assert not image._obj.isPixelsLoaded() # parametrized opts... opts = {'load_pixels': load_pixels, 'load_channels': load_channels} image = conn.getObject("Image", testImage.id, opts=opts) # pixels are also loaded if load_channels pix_loaded = load_pixels or load_channels assert image._obj.isPixelsLoaded() == pix_loaded if pix_loaded: pixels = image._obj._pixelsSeq[0] assert pixels.getPixelsType().isLoaded() if load_channels: assert pixels.isChannelsLoaded() for c in pixels.copyChannels(): lc = c.getLogicalChannel() assert lc.getPhotometricInterpretation().isLoaded() else: assert not pixels.isChannelsLoaded() def testGetProject(self, gatewaywrapper): gatewaywrapper.loginAsAuthor() testProj = gatewaywrapper.getTestProject() p = gatewaywrapper.gateway.getObject("Project", testProj.getId()) assert testProj.getName() == p.getName() assert testProj.getDescription() == p.getDescription() assert testProj.getId() == p.getId() assert testProj.OMERO_CLASS == p.OMERO_CLASS assert testProj.countChildren_cached() == p.countChildren_cached() assert testProj.getOwnerOmeName == p.getOwnerOmeName def testTraversal(self, author_testimg_tiny): image = author_testimg_tiny # This should return image wrapper pr = image.getProject() ds = image.getParent() assert image.listParents()[0] == image.getParent() assert ds == image.getParent(withlinks=True)[0] assert image.getParent(withlinks=True) == \ image.listParents(withlinks=True)[0] assert ds.getParent() == pr assert pr.getParent() is None assert len(pr.listParents()) == 0 @pytest.mark.parametrize("orphaned", [True, False]) @pytest.mark.parametrize("load_pixels", [False, False]) def testListOrphans(self, orphaned, load_pixels, gatewaywrapper): # We login as 'User', since they have no other orphaned images gatewaywrapper.loginAsUser() conn = gatewaywrapper.gateway eid = conn.getUserId() # Create 5 orphaned images iids = [] for i in range(0, 5): img = gatewaywrapper.createTestImage(imageName=str(uuid.uuid1())) iids.append(img.id) # Create image in Dataset, to check this isn't found dataset = DatasetI() dataset.name = wrap('testListOrphans') image = ImageI() image.name = wrap('testListOrphans') dataset.linkImage(image) dataset = conn.getUpdateService().saveAndReturnObject(dataset) try: # Only test listOrphans() if orphaned if orphaned: # Pagination params = omero.sys.ParametersI() params.page(1, 3) findImagesInPage = list(conn.listOrphans("Image", eid=eid, params=params)) assert len(findImagesInPage) == 3 # No pagination (all orphans) findImages = list(conn.listOrphans("Image", loadPixels=load_pixels)) assert len(findImages) == 5 for p in findImages: assert p._obj.pixelsLoaded == load_pixels # Test getObjects() with 'orphaned' option opts = {'orphaned': orphaned, 'load_pixels': load_pixels} getImages = list(conn.getObjects("Image", opts=opts)) assert orphaned == (len(getImages) == 5) for p in getImages: assert p._obj.pixelsLoaded == load_pixels # Simply check this doesn't fail See https://github.com/ # openmicroscopy/openmicroscopy/pull/4950#issuecomment-264142956 dsIds = [d.id for d in conn.listOrphans("Dataset")] assert dataset.id.val in dsIds finally: # Cleanup - Delete what we created conn.deleteObjects('Image', iids, deleteAnns=True, wait=True) conn.deleteObjects('Dataset', [dataset.id.val], deleteChildren=True, wait=True) def testOrderById(self, gatewaywrapper): gatewaywrapper.loginAsUser() imageIds = list() for i in range(0, 3): iid = gatewaywrapper.createTestImage( "%s-testOrderById" % i).getId() imageIds.append(iid) images = gatewaywrapper.gateway.getObjects( "Image", imageIds, respect_order=True) resultIds = [i.id for i in images] assert imageIds == resultIds, "Images not ordered by ID" imageIds.reverse() reverseImages = gatewaywrapper.gateway.getObjects( "Image", imageIds, respect_order=True) reverseIds = [i.id for i in reverseImages] assert imageIds == reverseIds, "Images not ordered by ID" wrappedIds = [rlong(i) for i in imageIds] reverseImages = gatewaywrapper.gateway.getObjects( "Image", wrappedIds, respect_order=True) reverseIds = [i.id for i in reverseImages] assert imageIds == reverseIds, "fails when IDs is list of rlongs" invalidIds = imageIds[:] invalidIds[1] = 0 reverseImages = gatewaywrapper.gateway.getObjects( "Image", invalidIds, respect_order=True) reverseIds = [i.id for i in reverseImages] assert len(imageIds) - 1 == len(reverseIds), \ "One image not found by ID: 0" # Delete to clean up handle = gatewaywrapper.gateway.deleteObjects( 'Image', imageIds, deleteAnns=True) try: gatewaywrapper.gateway._waitOnCmd(handle) finally: handle.close() @pytest.mark.parametrize("datatype", ['Image', 'Dataset', 'Project', 'Screen', 'Plate']) def testGetObjectsByMapAnnotations(self, datatype): client, exp = self.new_client_and_user() conn = BlitzGateway(client_obj=client) def createTarget(datatype, name, key="", value="", ns=None): """ Creates an object and attaches a map annotation to it """ if datatype == "Image": tgt = ImageWrapper(conn, omero.model.ImageI()) tgt.setName(name) tgt.save() if datatype == "Dataset": tgt = DatasetWrapper(conn, omero.model.DatasetI()) tgt.setName(name) tgt.save() if datatype == "Project": tgt = ProjectWrapper(conn, omero.model.ProjectI()) tgt.setName(name) tgt.save() if datatype == "Screen": tgt = ScreenWrapper(conn, omero.model.ScreenI()) tgt.setName(name) tgt.save() if datatype == "Plate": tgt = PlateWrapper(conn, omero.model.PlateI()) tgt.setName(name) tgt.save() for _ in range(0, 2): # Add two map annotations to check that each object # is still just returned once. map_ann = omero.gateway.MapAnnotationWrapper(conn) map_ann.setValue([(key, value)]) if ns: map_ann.setNs(ns) map_ann.save() tgt.linkAnnotation(map_ann) return tgt name = str(uuid.uuid4()) key = str(uuid.uuid4()) value = str(uuid.uuid4()) ns = str(uuid.uuid4()) kv = createTarget(datatype, name, key=key, value=value) v = createTarget(datatype, name, key=str(uuid.uuid4()), value=value) k = createTarget(datatype, name, key=key, value=str(uuid.uuid4())) kvn = createTarget(datatype, name, key=key, value=value, ns=ns) n = createTarget(datatype, name, key=str(uuid.uuid4()), value=str(uuid.uuid4()), ns=ns) # 3x key matches, 3x value matches, 2x key+value matches, # 2x ns matches, 1x key+value+ns matches # No match results = list(conn.getObjectsByMapAnnotations(datatype, key=str(uuid.uuid4()))) assert len(results) == 0 # Key match results = list(conn.getObjectsByMapAnnotations(datatype, key=key)) assert len(results) == 3 ids = [r.getId() for r in results] assert k.getId() in ids assert kv.getId() in ids assert kvn.getId() in ids # Key wildcard match wc = "*"+key[2:12]+"*" results = list(conn.getObjectsByMapAnnotations(datatype, key=wc)) assert len(results) == 3 ids = [r.getId() for r in results] assert k.getId() in ids assert kv.getId() in ids assert kvn.getId() in ids # Value match results = list(conn.getObjectsByMapAnnotations(datatype, value=value)) assert len(results) == 3 ids = [r.getId() for r in results] assert v.getId() in ids assert kv.getId() in ids assert kvn.getId() in ids # Key+Value match results = list(conn.getObjectsByMapAnnotations(datatype, key=key, value=value)) assert len(results) == 2 ids = [r.getId() for r in results] assert kv.getId() in ids assert kvn.getId() in ids # Key+Value wildcard match wc = "*"+value[2:12]+"*" results = list(conn.getObjectsByMapAnnotations(datatype, key=key, value=wc)) assert len(results) == 2 ids = [r.getId() for r in results] assert kv.getId() in ids assert kvn.getId() in ids # Key+Value wildcard doesn't match wc = value[2:12]+"*" results = list(conn.getObjectsByMapAnnotations(datatype, key=key, value=wc)) assert len(results) == 0 # NS match results = list(conn.getObjectsByMapAnnotations(datatype, ns=ns)) assert len(results) == 2 ids = [r.getId() for r in results] assert n.getId() in ids assert kvn.getId() in ids # Key+Value+NS match results = list(conn.getObjectsByMapAnnotations(datatype, key=key, value=value, ns=ns)) assert len(results) == 1 assert kvn.getId() == results[0].getId() # Test limit results = list(conn.getObjectsByMapAnnotations(datatype)) assert len(results) == 5 results = list(conn.getObjectsByMapAnnotations(datatype, opts={"limit": 4})) assert len(results) == 4 class TestLeaderAndMemberOfGroup(object): @pytest.fixture(autouse=True) def setUp(self): """ Create a group with owner & member""" dbhelpers.USERS['group_owner'] = dbhelpers.UserEntry( 'group_owner', 'ome', firstname='Group', lastname='Owner', groupname="ownership_test", groupperms='rwr---', groupowner=True) dbhelpers.USERS['group_member'] = dbhelpers.UserEntry( 'group_member', 'ome', firstname='Group', lastname='Member', groupname="ownership_test", groupperms='rwr---', groupowner=False) dbhelpers.bootstrap(onlyUsers=True) def testGetGroupsLeaderOfAsLeader(self, gatewaywrapper): gatewaywrapper.doLogin(dbhelpers.USERS['group_owner']) assert gatewaywrapper.gateway.isLeader() grs = [g.id for g in gatewaywrapper.gateway.getGroupsLeaderOf()] assert len(grs) > 0 exp = gatewaywrapper.gateway.getObject( "Experimenter", attributes={'omeName': 'group_owner'}) assert exp.sizeOfGroupExperimenterMap() > 0 filter_system_groups = [gatewaywrapper.gateway.getAdminService() .getSecurityRoles().userGroupId] leaderOf = list() for groupExpMap in exp.copyGroupExperimenterMap(): gId = groupExpMap.parent.id.val if groupExpMap.owner.val and gId not in filter_system_groups: leaderOf.append(gId) assert(leaderOf == grs) def testGetGroupsLeaderOfAsMember(self, gatewaywrapper): gatewaywrapper.doLogin(dbhelpers.USERS['group_member']) assert not gatewaywrapper.gateway.isLeader() with pytest.raises(StopIteration): next(gatewaywrapper.gateway.getGroupsLeaderOf()) def testGetGroupsMemberOf(self, gatewaywrapper): gatewaywrapper.doLogin(dbhelpers.USERS['group_member']) assert not gatewaywrapper.gateway.isLeader() grs = [g.id for g in gatewaywrapper.gateway.getGroupsMemberOf()] assert len(grs) > 0 exp = gatewaywrapper.gateway.getObject( "Experimenter", attributes={'omeName': "group_member"}) assert exp.sizeOfGroupExperimenterMap() > 0 filter_system_groups = [gatewaywrapper.gateway.getAdminService() .getSecurityRoles().userGroupId] memberOf = list() for groupExpMap in exp.copyGroupExperimenterMap(): gId = groupExpMap.parent.id.val if not groupExpMap.owner.val and gId not in filter_system_groups: memberOf.append(gId) assert memberOf == grs def testGroupSummaryAsOwner(self, gatewaywrapper): """Test groupSummary() when Group loaded without experimenters.""" gatewaywrapper.doLogin(dbhelpers.USERS['group_owner']) expGr = gatewaywrapper.gateway.getObject( "ExperimenterGroup", attributes={'name': 'ownership_test'}) leaders, colleagues = expGr.groupSummary() assert len(leaders) == 1 assert len(colleagues) == 1 assert leaders[0].omeName == "group_owner" assert colleagues[0].omeName == "group_member" leaders, colleagues = expGr.groupSummary(exclude_self=True) assert len(leaders) == 0 assert len(colleagues) == 1 assert colleagues[0].omeName == "group_member" def testGroupSummaryAsMember(self, gatewaywrapper): gatewaywrapper.doLogin(dbhelpers.USERS['group_member']) expGr = gatewaywrapper.gateway.getObject( "ExperimenterGroup", attributes={'name': 'ownership_test'}) leaders, colleagues = expGr.groupSummary() assert len(leaders) == 1 assert len(colleagues) == 1 assert leaders[0].omeName == "group_owner" assert colleagues[0].omeName == "group_member" leaders, colleagues = expGr.groupSummary(exclude_self=True) assert len(leaders) == 1 assert leaders[0].omeName == "group_owner" assert len(colleagues) == 0 def testGroupSummaryAsOwnerDeprecated(self, gatewaywrapper): gatewaywrapper.doLogin(dbhelpers.USERS['group_owner']) summary = gatewaywrapper.gateway.groupSummary() assert len(summary["leaders"]) == 1 assert len(summary["colleagues"]) == 1 assert summary["leaders"][0].omeName == "group_owner" assert summary["colleagues"][0].omeName == "group_member" summary = gatewaywrapper.gateway.groupSummary(exclude_self=True) assert len(summary["leaders"]) == 0 assert len(summary["colleagues"]) == 1 assert summary["colleagues"][0].omeName == "group_member" def testGroupSummaryAsMemberDeprecated(self, gatewaywrapper): gatewaywrapper.doLogin(dbhelpers.USERS['group_member']) summary = gatewaywrapper.gateway.groupSummary() assert len(summary["leaders"]) == 1 assert len(summary["colleagues"]) == 1 assert summary["leaders"][0].omeName == "group_owner" assert summary["colleagues"][0].omeName == "group_member" summary = gatewaywrapper.gateway.groupSummary(exclude_self=True) assert len(summary["leaders"]) == 1 assert summary["leaders"][0].omeName == "group_owner" assert len(summary["colleagues"]) == 0 class TestListParents(ITest): def testSupportedObjects(self): """ Check that we are testing all objects where listParents() is supported. If this test fails, need to update tested_wrappers and add corresponding tests below """ tested_wrappers = ['plate', 'image', 'dataset', 'experimenter', 'well'] for key, wrapper in list(KNOWN_WRAPPERS.items()): if (hasattr(wrapper, 'PARENT_WRAPPER_CLASS') and wrapper.PARENT_WRAPPER_CLASS is not None): assert key in tested_wrappers def testListParentsPDI(self): """Test listParents() for Image in Dataset""" # Set up PDI client, exp = self.new_client_and_user() p = self.make_project(name="ListParents Test", client=client) d = self.make_dataset(name="ListParents Test", client=client) i = self.make_image(name="ListParents Test", client=client) self.link(p, d, client=client) self.link(d, i, client=client) conn = BlitzGateway(client_obj=client) image = conn.getObject("Image", i.id.val) # Traverse from Image -> Project dataset = image.listParents()[0] assert dataset.id == d.id.val project = dataset.listParents()[0] assert project.id == p.id.val # Project has no parent assert len(project.listParents()) == 0 def testListParentsSPW(self): """Test listParents() for Image in WellSample""" client, exp = self.new_client_and_user() conn = BlitzGateway(client_obj=client) # setup SPW-WS-Img... s = ScreenI() s.name = wrap('ScreenA') p = PlateI() p.name = wrap('PlateA') s.linkPlate(p) w = WellI() w.column = wrap(0) w.row = wrap(0) p.addWell(w) s = client.sf.getUpdateService().saveAndReturnObject(s) p = s.linkedPlateList()[0] w = p.copyWells()[0] i = self.make_image(name="SPW listParents", client=client) ws = WellSampleI() ws.image = i ws.well = WellI(w.id.val, False) w.addWellSample(ws) ws = client.sf.getUpdateService().saveAndReturnObject(ws) # Traverse from Image -> Screen image = conn.getObject("Image", i.id.val) wellSample = image.listParents()[0] well = wellSample.listParents()[0] assert well.id == w.id.val plate = well.listParents()[0] assert plate.id == p.id.val screen = plate.listParents()[0] assert screen.id == s.id.val # Screen has no parent assert len(screen.listParents()) == 0 def testExperimenterListParents(self): """Test listParents() for Experimenter in ExperimenterGroup.""" client, exp = self.new_client_and_user() conn = BlitzGateway(client_obj=client) userGroupId = conn.getAdminService().getSecurityRoles().userGroupId exp = conn.getUser() groups = exp.listParents() assert len(groups) == 2 gIds = [g.id for g in groups] assert userGroupId in gIds # ExperimenterGroup has no parent assert len(groups[0].listParents()) == 0
gpl-2.0
-8,658,934,620,634,377,000
39.918018
79
0.615381
false
4.025793
true
false
false
WA4OSH/Learn_Python
oldLady.py
1
2383
#------------------------------------------------------------------------------- # Name: oldLady.py # Purpose: Demo of program control, loops, branches, etc. # # Author: Konrad Roeder, adapted from the nusery rhyme # There was an Old Lady song from the # Secret History of Nursery Rhymes Book # www.rhymes.uk/there_was_an_old_lady.htm # # Created: 04/16/2014 # Copyright: (cc) Konrad Roeder 2014 # Licence: CC by 4.0 #------------------------------------------------------------------------------- #There are seven animals in this song, one for each verse animalName = ['fly','spider','bird','cat','dog','cow','horse'] #Each verse in the song starts with this section, printing this line def printSectionA(verse): print("There was an old lady who swallowed a",animalName[verse-1]) #In section B, the line is different for each verse def printSectionB(verse): #if (verse == 1): Do nothing if (verse == 2): print("That wriggled and wiggled and tickled inside her") elif (verse == 3): print("How absurd to swallow a bird") elif (verse == 4): print("Fancy that to swallow a cat") elif (verse == 5): print("What a hog to swallow a dog") elif (verse == 6): print("I don't know how she swallowed a cow") elif (verse == 7): print("She's dead, of course!") def printSectionC(verse): #This section only has lines in the middle five verses if (verse < 7): #The for loop drops through on the first verse #In verses 2-6, it prints one line less than the verse number for line in range(verse-1, 0, -1): print("She swallowed the",animalName[line], "to catch the", animalName[line-1]) def printSectionD(verse): #This sections exists only in the first six verses if (verse < 7): print("I don't know why she swallowed a fly - Perhaps she will die!") print("") def song(): #Print the title print("There was an Old Lady song") print("") #Print each of the seven verses for verse in range(1,8): #Each verse has four sections printSectionA(verse) printSectionB(verse) printSectionC(verse) printSectionD(verse) #Print the song's coda (ending) print("") print("There was an Old Lady song") song()
cc0-1.0
2,651,610,528,392,757,000
33.536232
80
0.579522
false
3.621581
false
false
false
linsalrob/EdwardsLab
patric/parse_gto.py
1
2770
""" Parse a GTO object """ import os import sys import argparse from roblib import bcolors import json def list_keys(gto, verbose=False): """ List the primary keys in the patric file :param gto: the json gto :param verbose: more output :return: """ print("{}".format("\n".join(gto.keys()))) def dump_json(gto, k, verbose=False): """ Print out the json representation of some data :param gto: the json gto :param k: the key to dump (none for everything) :param verbose: more output :return: """ if k: if k in gto: print(json.dumps(gto[k], indent=4)) else: sys.stderr.write(f"{bcolors.RED}ERROR: {k} not found.{bcolors.ENDC}\n") else: print(json.dumps(gto, indent=4)) def feature_tbl(gto, verbose=False): """ Print a tab separated feature table :param gto: the json gto :param verbose: more output :return: """ for peg in gto['features']: if 'location' not in peg: sys.stderr.write(f"{bcolors.RED}Error: no location found\n{bcolors.PINK}{peg}{bcolors.ENDC}\n") continue locs = [] for l in peg['location']: start = int(l[1]) if l[2] == '+': stop = (start + int(l[3])) - 1 elif l[2] == '-': start = (start - int(l[3])) + 1 stop = int(l[1]) else: sys.stderr.write(f"{bcolors.RED}Error: Don't know location l[2]\n{bcolors.ENDC}") continue locs.append(f"{l[0]} {start} - {stop} ({l[2]})") data = [ peg['id'], peg['function'], "; ".join(locs) ] print("\t".join(data)) if __name__ == '__main__': parser = argparse.ArgumentParser(description="Plot a heatmap") parser.add_argument('-f', help='gto file', required=True) parser.add_argument('-l', help='list the primary keys and exit', action='store_true') parser.add_argument('-d', help='dump some part of the json object', action='store_true') parser.add_argument('-p', help='print protein feature table', action='store_true') parser.add_argument('-k', help='json primary key (e.g. for dumping, etc)') parser.add_argument('-o', help='output file') parser.add_argument('-v', help='verbose output', action='store_true') args = parser.parse_args() gto = json.load(open(args.f, 'r')) if args.l: list_keys(gto, args.v) sys.exit(0) if args.d: dump_json(gto, args.k, args.v) sys.exit(0) if args.p: feature_tbl(gto, args.v) sys.exit(0) sys.stderr.write(f"{bcolors.RED}ERROR: You did not specify a command to run{bcolors.ENDC}\n")
mit
-2,918,537,910,248,930,000
27.556701
107
0.558123
false
3.349456
false
false
false
BenKettlewell/Livestreamer-Command-Line-Generator
livestreamerCLG.py
1
2267
''' Parses crunchyroll URLs and provides a string command line argument to download them. Utilizing youtube-dl to split sub and video files but livestreamer functionality can be added with minimal effort -h, --help Output this help document -u, --url Provide a single url -f, --file Provide location of csv file File format (do not include headers) crunchyroll_url,subtitle_url,season# #subtitle_url not implemented yet -c, Use cookie file located at $COOKIES instead of password auth --cookie-auth ''' from urlparse import urlparse import sys # Command Line Arguments import getopt # Parse CLI Args import re # Regular Expressions from CrunchyCSV import CrunchyCSV from Anime import Anime from crunchyroll import outputer from shell import downloader def main (argv): ''' This program has 3 distinct stages. 1. Request a set of urls from the user and store them 2. Parse and formulate the compiled Livestreamer command 3. Return the string to the user ''' urls = '' file_csv = '' auth_method = 'password' # parse command line options try: opts, args = getopt.getopt(argv, "hu:f:c", ["help","url=","file=","cookie-auth"]) except getopt.error, msg: print msg print "for help use --help" sys.exit(2) # process options for o, a in opts: if o in ("-h", "--help"): print __doc__ sys.exit(1) if o in ("-u", "--url"): urls = a print'urls are :', a if o in ("-f", "--file"): file_csv = a print'csv_file :', a if o in ("-c","--cookie-auth"): auth_method = 'cookies' print'using cookies' # process arguments for arg in args: process(arg) # process() is defined elsewhere if file_csv != '': crunchyCSV = CrunchyCSV(file_csv) print outputer.youtube_dl_string_for_CrunchyCSV(crunchyCSV, auth_method) print outputer.list_of_anime_filenames(crunchyCSV) else: anime = Anime(urls, '', '') print outputer.youtube_dl_string_for_Anime(anime, auth_method) print downloader.sub_call() if __name__ == "__main__": main(sys.argv[1:])
gpl-2.0
-2,636,595,250,218,402,000
31.385714
89
0.613586
false
3.704248
false
false
false
root-mirror/root
tutorials/roofit/rf604_constraints.py
11
2705
## \file ## \ingroup tutorial_roofit ## \notebook -nodraw ## Likelihood and minimization: fitting with constraints ## ## \macro_code ## ## \date February 2018 ## \authors Clemens Lange, Wouter Verkerke (C++ version) from __future__ import print_function import ROOT # Create model and dataset # ---------------------------------------------- # Construct a Gaussian pdf x = ROOT.RooRealVar("x", "x", -10, 10) m = ROOT.RooRealVar("m", "m", 0, -10, 10) s = ROOT.RooRealVar("s", "s", 2, 0.1, 10) gauss = ROOT.RooGaussian("gauss", "gauss(x,m,s)", x, m, s) # Construct a flat pdf (polynomial of 0th order) poly = ROOT.RooPolynomial("poly", "poly(x)", x) # model = f*gauss + (1-f)*poly f = ROOT.RooRealVar("f", "f", 0.5, 0., 1.) model = ROOT.RooAddPdf( "model", "model", ROOT.RooArgList( gauss, poly), ROOT.RooArgList(f)) # Generate small dataset for use in fitting below d = model.generate(ROOT.RooArgSet(x), 50) # Create constraint pdf # ----------------------------------------- # Construct Gaussian constraint pdf on parameter f at 0.8 with # resolution of 0.1 fconstraint = ROOT.RooGaussian( "fconstraint", "fconstraint", f, ROOT.RooFit.RooConst(0.8), ROOT.RooFit.RooConst(0.1)) # Method 1 - add internal constraint to model # ------------------------------------------------------------------------------------- # Multiply constraint term with regular pdf using ROOT.RooProdPdf # Specify in fitTo() that internal constraints on parameter f should be # used # Multiply constraint with pdf modelc = ROOT.RooProdPdf( "modelc", "model with constraint", ROOT.RooArgList(model, fconstraint)) # Fit model (without use of constraint term) r1 = model.fitTo(d, ROOT.RooFit.Save()) # Fit modelc with constraint term on parameter f r2 = modelc.fitTo( d, ROOT.RooFit.Constrain( ROOT.RooArgSet(f)), ROOT.RooFit.Save()) # Method 2 - specify external constraint when fitting # ------------------------------------------------------------------------------------------ # Construct another Gaussian constraint pdf on parameter f at 0.8 with # resolution of 0.1 fconstext = ROOT.RooGaussian("fconstext", "fconstext", f, ROOT.RooFit.RooConst( 0.2), ROOT.RooFit.RooConst(0.1)) # Fit with external constraint r3 = model.fitTo(d, ROOT.RooFit.ExternalConstraints( ROOT.RooArgSet(fconstext)), ROOT.RooFit.Save()) # Print the fit results print("fit result without constraint (data generated at f=0.5)") r1.Print("v") print("fit result with internal constraint (data generated at f=0.5, is f=0.8+/-0.2)") r2.Print("v") print("fit result with (another) external constraint (data generated at f=0.5, is f=0.2+/-0.1)") r3.Print("v")
lgpl-2.1
5,271,455,174,840,602,000
28.402174
96
0.621072
false
3.174883
false
false
false
valdur55/py3status
py3status/modules/keyboard_locks.py
1
2349
r""" Display NumLock, CapsLock, and ScrLock keys. Configuration parameters: cache_timeout: refresh interval for this module (default 1) format: display format for this module *(default '[\?if=num_lock&color=good NUM|\?color=bad NUM] ' '[\?if=caps_lock&color=good CAPS|\?color=bad CAPS] ' '[\?if=scroll_lock&color=good SCR|\?color=bad SCR]')* Control placeholders: {num_lock} a boolean based on xset data {caps_lock} a boolean based on xset data {scroll_lock} a boolean based on xset data Color options: color_good: Lock on color_bad: Lock off Examples: ``` # hide NUM, CAPS, SCR keyboard_locks { format = '\?color=good [\?if=num_lock NUM][\?soft ]' format += '[\?if=caps_lock CAPS][\?soft ][\?if=scroll_lock SCR]' } ``` @author lasers SAMPLE OUTPUT {'color': '#00FF00', 'full_text': 'NUM CAPS SCR'} no_locks {'color': '#FF0000', 'full_text': 'NUM CAPS SCR'} """ class Py3status: """ """ # available configuration parameters cache_timeout = 1 format = ( r"[\?if=num_lock&color=good NUM|\?color=bad NUM] " r"[\?if=caps_lock&color=good CAPS|\?color=bad CAPS] " r"[\?if=scroll_lock&color=good SCR|\?color=bad SCR]" ) def post_config_hook(self): items = [ "icon_num_on", "icon_num_off", "icon_caps_on", "icon_caps_off", "icon_scr_on", "icon_scr_off", ] if self.py3.format_contains(self.format, ["caps", "num", "scr"]) or ( any(getattr(self, v, None) is not None for v in items) ): raise Exception("please update the config for this module") # end deprecation self.locks = {} self.keyring = {"num_lock": "Num", "caps_lock": "Caps", "scroll_lock": "Scroll"} def keyboard_locks(self): xset_data = self.py3.command_output("xset q") for k, v in self.keyring.items(): self.locks[k] = "on" in xset_data.split("%s Lock:" % v)[1][0:6] return { "cached_until": self.py3.time_in(self.cache_timeout), "full_text": self.py3.safe_format(self.format, self.locks), } if __name__ == "__main__": """ Run module in test mode. """ from py3status.module_test import module_test module_test(Py3status)
bsd-3-clause
-3,356,702,791,891,390,500
26.635294
88
0.57301
false
3.182927
false
false
false
MaxIV-KitsControls/netspot
netspot/ts_lib.py
1
5698
#!/usr/bin/python -tt """Junos Interface Troubleshooting library.""" import re import warnings import helpers from napalm import get_network_driver from jnpr.junos.exception import ConnectRefusedError, ConnectAuthError # JUNOS MAC table RE RE_VLAN = r'\s+([\w\d-]+)\s+' RE_MAC = r'\s?([*\w\d:]+)\s+' RE_TYPE = r'\s?([\w]+) ' RE_AGE = r'\s+([-\d:]+)' RE_INTERFACE = r'\s+([-.\w\d/]+)' RE_SWITCHING_TABLE = RE_VLAN + RE_MAC + RE_TYPE + RE_AGE + RE_INTERFACE class TroubleshootDevice(object): """Class to help troubleshoot device.""" def __init__(self, asset, loopback, ssh_key, interface_name): self.asset = asset self.loopback = loopback self.ssh_key = ssh_key self.interface_name = interface_name self.mac_address = None self.dhcp_logs = None self.dhcp_error = None self.log_entries = None self.interface = None self.error_message = None self.macs = {} self.lldp = {} def run(self): """Run troubleshooter.""" try: # Connect to asset driver = get_network_driver('junos') device = driver(self.loopback, 'automation', '', optional_args={'key_file': self.ssh_key}) device.open() with warnings.catch_warnings(record=True) as warning: warnings.filterwarnings('ignore') # Check interface cmd = 'show interfaces {0} detail'.format(self.interface_name) show_interface = device.cli([cmd]) self.interface = Interface(show_interface[cmd]) if self.interface.link_state == 'Up': # Get LLDP neighbor cmd = 'show lldp neighbors interface {0}'.format(self.interface_name) lldp_neighbor = device.cli([cmd]) self.lldp = LLDP(lldp_neighbor[cmd]) # Check MAC table cmd = 'show ethernet-switching table interface {0}'.format(self.interface_name) mac_table = device.cli([cmd]) self.macs = MACTable(mac_table[cmd]) # Search DHCP logs if MAC is specified if self.macs: self.mac_address = self.macs.mac_entries[0]['mac'] dhcp_server = helpers.get_dhcp_server(asset=self.asset) self.dhcp_logs, self.dhcp_error = helpers.search_dhcp_log(self.mac_address, dhcp_server) # Check log file cmd = 'show log messages' show_log = device.cli([cmd]) show_log = show_log[cmd] self.log_entries = re.findall(r'\n([\[\]\s\w\d:.-]+{0}[\s\w\d:.-]+)\n'.format(self.interface_name), show_log) device.close() except ConnectAuthError: self.error_message = 'Autentication failed to %s.' % self.loopback except ConnectRefusedError: self.error_message = 'Connection refused to %s.' % self.loopback except ValueError: self.error_message = 'No switch found.' class Interface(object): """Class to represent a JUNOS interface.""" def __init__(self, output): self.output = output self.link_state = '' self.speed = '' self.duplex = '' self.flapped = '' self.auto_neg = '' # Analyze output self.analyze_output() def analyze_output(self): """Anlyze the output from show interfaces X.""" # Link down match = re.search(r'Physical link is ([\w]+)', self.output) if match: self.link_state = match.groups()[0] # Speed match = re.search(r'Speed: ([\w\d]+),', self.output) if match: self.speed = match.groups()[0] # Duplex match = re.search(r'Duplex: ([\w-]+),', self.output) if match: self.duplex = match.groups()[0] # Last flapped match = re.search(r'Last flapped : ([\w\d ():-]+)\n', self.output) if match: self.flapped = match.groups()[0] # Auto negotiation match = re.search(r'Auto-negotiation: ([\w]+),', self.output) if match: self.auto_neg = match.groups()[0] class LLDP(object): """Parse and represent a LLDP neighbor.""" def __init__(self, output): self.output = output self.empty = True self.remote_chassis_id = '' self.remote_port_description = '' self.remote_system = '' # Analyze output self.analyze_output() if self.remote_chassis_id: self.empty = False def analyze_output(self): """Parse JUNOS show lldp neighboir interface X command.""" # Remote chassis ID match = re.search(r'Chassis ID\s+: ([\w\d:-]+)', self.output) if match: self.remote_chassis_id = match.groups()[0] # Remote port description match = re.search(r'Port description\s+: ([\w\d\/:-]+)', self.output) if match: self.remote_port_description = match.groups()[0] # Remote port system match = re.search(r'System name\s+: ([\w\d\/:-]+)', self.output) if match: self.remote_system = match.groups()[0] class MACTable(object): """Parse and save MAC entries from a JUNOS device.""" def __init__(self, output): self.output = output self.mac_entries = [] # Analyze output self.analyze_output() def analyze_output(self): """Parse JUNOS show ethernet-switching interface X command.""" # Remote chassis ID match = re.findall(RE_SWITCHING_TABLE, self.output) for entry in match: if entry[1] != '*': mac_entry = {'vlan': entry[0], 'mac': entry[1], 'type': entry[2], 'age': entry[3], 'interface': entry[4]} self.mac_entries.append(mac_entry) def __str__(self): if self.mac_entries: return self.mac_entries[0]['mac'] return None def main(): """Main.""" pass if __name__ == '__main__': main()
mit
-7,438,312,396,324,293,000
26.931373
107
0.586697
false
3.493562
false
false
false
ndaniels/Ammolite
scripts/figure-generators/smsdIsoCompare.py
1
1534
import matplotlib.pyplot as plt from pylab import polyfit, poly1d, show, savefig import sys def isNumber( s): try: float(s) return True except ValueError: return False def makeGraph(X,Y, xName, yName, name="NoName"): fig = plt.figure() ax = fig.add_subplot(111) superName = "Comparison of {} and {}".format(xName,yName) outname = "{} from {}.png".format(superName,name) fig.suptitle(superName) ax.scatter(X,Y) fit = polyfit(X,Y,1) fit_fn = poly1d(fit) # fit_fn is now a function which takes in x and returns an estimate for y ax.plot(X,Y, 'yo', X, fit_fn(X), '--k') ax.set_xlabel('Size of MCS found by {}'.format(xName)) ax.set_ylabel('Size of MCS found by {}'.format(yName)) ax.text(1, 1, "y = {}*x + {}".format(fit[0], fit[1])) fig.savefig(outname) def buildIsoSMSDComparison( filename, outname="SMSD-IsoRank-comparison"): X, Y, xName, yName = [], [], "", "" with open( filename) as f: inComparison = False nameLine = False for line in f: if line.split()[0] == "COMPARISON_DELIMITER": if inComparison: makeGraph( X, Y, xName, yName, filename) inComparison = True nameLine = True X, Y = [], [] elif inComparison: l = line.split() if nameLine: xName, yName = l[0], l[1] nameLine = False else: X.append( float( l[0])) Y.append( float( l[1])) makeGraph( X, Y, xName, yName, filename) if __name__ == "__main__": args = sys.argv if(len(args) == 2): buildIsoSMSDComparison(args[1]) else: buildIsoSMSDComparison(args[1], args[2])
gpl-2.0
7,310,710,760,138,721,000
22.96875
95
0.634289
false
2.635739
false
false
false
Frencil/box-python-sdk
test/unit/object/test_events.py
1
8316
# coding: utf-8 from __future__ import unicode_literals from itertools import chain import json from mock import Mock import pytest from requests.exceptions import Timeout from six.moves import map # pylint:disable=redefined-builtin from six.moves.urllib.parse import urlencode, urlunsplit # pylint:disable=import-error,no-name-in-module from boxsdk.network.default_network import DefaultNetworkResponse from boxsdk.object.events import Events, EventsStreamType, UserEventsStreamType from boxsdk.session.box_session import BoxResponse from boxsdk.util.ordered_dict import OrderedDict @pytest.fixture() def test_events(mock_box_session): return Events(mock_box_session) @pytest.fixture() def final_stream_position(): return 1348790499820 @pytest.fixture() def initial_stream_position(): return 1348790499819 # pylint:disable=no-member # pylint isn't currently smart enough to recognize the class member that was # added by the metaclass, when the metaclass was added by @add_metaclass() / # with_metaclass(). STREAM_TYPES_AS_ENUM_INSTANCES = list(EventsStreamType.__members__.values()) # pylint:enable=no-member STREAM_TYPES_AS_STRINGS = list(map(str, STREAM_TYPES_AS_ENUM_INSTANCES)) def test_events_stream_type_extended_enum_class_has_expected_members(): assert len(STREAM_TYPES_AS_ENUM_INSTANCES) >= 4 assert len(STREAM_TYPES_AS_STRINGS) >= 4 assert 'all' in STREAM_TYPES_AS_STRINGS assert 'changes' in STREAM_TYPES_AS_STRINGS assert 'sync' in STREAM_TYPES_AS_STRINGS assert 'admin_logs' in STREAM_TYPES_AS_STRINGS @pytest.fixture( scope='session', params=list(chain( [None], # Default behavior of not passing any stream_type STREAM_TYPES_AS_ENUM_INSTANCES, # Passing an enum instance STREAM_TYPES_AS_STRINGS, # Passing an enum value # For forwards compatibility, make sure that it works to pass a string # value that is not a member of the enum. ['future_stream_type'], )), ) def stream_type_param(request): """The value to pass as an Event method's stream_type parameter. :return: The parameter value, or `None` if no value should be passed. :rtype: :enum:`EventsStreamType` or `unicode` or `None` """ return request.param @pytest.fixture() def expected_stream_type(stream_type_param): """The stream type we expect to use. :rtype: `unicode` """ if stream_type_param is None: return UserEventsStreamType.ALL return stream_type_param @pytest.fixture() def stream_type_kwargs(stream_type_param): """The kwargs for stream_type to pass when invoking a method on `Events`. :rtype: `dict` """ if stream_type_param: return {'stream_type': stream_type_param} return {} @pytest.fixture() def expected_stream_type_params(expected_stream_type): """The stream_type-related params that we expect to pass to request methods. :rtype: :class:`OrderedDict` """ return OrderedDict(stream_type=expected_stream_type) @pytest.fixture() def empty_events_response(final_stream_position): # pylint:disable=redefined-outer-name mock_box_response = Mock(BoxResponse) mock_network_response = Mock(DefaultNetworkResponse) mock_box_response.network_response = mock_network_response mock_box_response.json.return_value = mock_json = {'next_stream_position': final_stream_position, 'entries': []} mock_box_response.content = json.dumps(mock_json).encode() mock_box_response.status_code = 200 mock_box_response.ok = True return mock_box_response @pytest.fixture() def long_poll_url(test_url, expected_stream_type_params): return urlunsplit(('', '', test_url, urlencode(expected_stream_type_params), '')) @pytest.fixture() def retry_timeout(): return 610 @pytest.fixture() def options_response_entry(long_poll_url, retry_timeout): return {'url': long_poll_url, 'retry_timeout': retry_timeout} @pytest.fixture() def options_response(options_response_entry, make_mock_box_request): # pylint:disable=redefined-outer-name mock_box_response, _ = make_mock_box_request( response={'entries': [options_response_entry]}, ) return mock_box_response @pytest.fixture() def new_change_long_poll_response(make_mock_box_request): mock_box_response, _ = make_mock_box_request( response={'message': 'new_change'}, ) return mock_box_response @pytest.fixture() def reconnect_long_poll_response(make_mock_box_request): mock_box_response, _ = make_mock_box_request( response={'message': 'reconnect'}, ) return mock_box_response @pytest.fixture() def max_retries_long_poll_response(make_mock_box_request): mock_box_response, _ = make_mock_box_request( response={'message': 'max_retries'}, ) return mock_box_response @pytest.fixture() def mock_event(): return { "type": "event", "event_id": "f82c3ba03e41f7e8a7608363cc6c0390183c3f83", "source": { "type": "folder", "id": "11446498", } } @pytest.fixture() def events_response(initial_stream_position, mock_event, make_mock_box_request): # pylint:disable=redefined-outer-name mock_box_response, _ = make_mock_box_request( response={"next_stream_position": initial_stream_position, "entries": [mock_event]}, ) return mock_box_response def test_get_events( test_events, mock_box_session, events_response, stream_type_kwargs, expected_stream_type_params, ): # pylint:disable=redefined-outer-name expected_url = test_events.get_url() mock_box_session.get.return_value = events_response events = test_events.get_events(**stream_type_kwargs) assert 'next_stream_position' in events mock_box_session.get.assert_any_call( expected_url, params=dict(limit=100, stream_position=0, **expected_stream_type_params), ) def test_get_long_poll_options( mock_box_session, test_events, stream_type_kwargs, expected_stream_type_params, options_response, options_response_entry, ): expected_url = test_events.get_url() mock_box_session.options.return_value = options_response long_poll_options = test_events.get_long_poll_options(**stream_type_kwargs) mock_box_session.options.assert_called_with(expected_url, params=expected_stream_type_params) assert long_poll_options == options_response_entry def test_generate_events_with_long_polling( test_events, mock_box_session, events_response, empty_events_response, initial_stream_position, long_poll_url, retry_timeout, options_response, new_change_long_poll_response, reconnect_long_poll_response, max_retries_long_poll_response, mock_event, stream_type_kwargs, expected_stream_type, expected_stream_type_params, ): # pylint:disable=redefined-outer-name expected_url = test_events.get_url() mock_box_session.options.return_value = options_response mock_box_session.get.side_effect = [ events_response, # initial call to get now stream position Timeout, reconnect_long_poll_response, max_retries_long_poll_response, new_change_long_poll_response, events_response, new_change_long_poll_response, empty_events_response, ] events = test_events.generate_events_with_long_polling(**stream_type_kwargs) assert next(events) == mock_event with pytest.raises(StopIteration): next(events) events.close() mock_box_session.options.assert_called_with(expected_url, params=expected_stream_type_params) mock_box_session.get.assert_any_call(expected_url, params={'stream_position': 'now', 'limit': 0, 'stream_type': expected_stream_type}) assert '/events' in expected_url mock_box_session.get.assert_any_call( expected_url, params=dict(limit=100, stream_position=initial_stream_position, **expected_stream_type_params), ) mock_box_session.get.assert_any_call( long_poll_url, timeout=retry_timeout, params={'stream_position': initial_stream_position}, )
apache-2.0
-5,183,375,154,726,981,000
29.686347
138
0.684343
false
3.584483
true
false
false
alexisVallet/anime-bgrm
objectSegmentation.py
1
3095
import disjointSetForest as dsj import cv2 import numpy as np def toRowMajor(cols, i, j): return i * cols + j def fromRowMajor(cols, idx): return (idx / cols, idx % cols) class ObjectsSegmentation: """ Disjoint set forest, with the additional semantic element of an image to segment into background and objects (foreground). """ def __init__(self, image): rows, cols = image.shape[0:2] self.image = image self.segmentation = dsj.DisjointSetForest(rows * cols) self.background = None self.largest = None def find(self, i, j): """ Finds the root pixel of the segment containing pixel (i,j). """ rows, cols = self.image.shape[0:2] return fromRowMajor(cols, self.segmentation.find(toRowMajor(cols, i, j))) def unsafeUnion(self, i, j, k, l): """ Fuses the segments containing pixels (i,j) and (k,l) into a single segment. Doesn't check if either segment is the background. """ rows, cols = self.image.shape[0:2] newRoot = self.segmentation.union(toRowMajor(cols,i,j), toRowMajor(cols,k,l)) return fromRowMajor(cols, newRoot) def union(self, i, j, k, l): """ Fuses the segments containing pixels (i,j) and (k,l) into a single segment. Neither segments should be the background. """ rows, cols = self.image.shape[0:2] fstRoot = self.find(i,j) sndRoot = self.find(k,l) if fstRoot == self.background or sndRoot == self.background: raise ValueError("Cannot perform union of background pixels!") else: newRoot = self.segmentation.union(toRowMajor(cols,i,j), toRowMajor(cols,k,l)) newRootPixel = fromRowMajor(cols,newRoot) # keep track of the largest object if self.largest == None: self.largest = newRootPixel else: (li, lj) = self.largest largestSize = self.segmentation.compSize[toRowMajor(cols,li,lj)] if self.segmentation.compSize[newRoot] > largestSize: self.largest = newRootPixel def setBackground(self, i, j): """ Marks the (i,j) pixel as a background pixel. """ if self.background == None: self.background = (i,j) else: (k,l) = self.background self.background = self.unsafeUnion(k, l, i, j) def getLargestObject(self): return (0,0) if self.largest == None else self.largest def foregroundMask(self, fgVal=0, bgVal=255): rows, cols = self.image.shape[0:2] mask = np.empty([rows, cols], dtype=np.uint8) for i in range(0,rows): for j in range(0,cols): root = self.find(i,j) if root == self.background: mask[i,j] = bgVal else: mask[i,j] = fgVal return mask
gpl-2.0
-6,634,703,475,703,437,000
35.845238
88
0.555412
false
3.84472
false
false
false
crypto101/arthur
arthur/test/test_util.py
1
4581
from arthur.util import MultiDeferred from twisted.internet import defer from twisted.trial import unittest class MultiDeferredTests(unittest.SynchronousTestCase): """ Tests for L{defer.MultiDeferred}, except now in Arthur. See tm.tl/6365. """ def setUp(self): self.multiDeferred = MultiDeferred() def test_callback(self): """ Any produced L{defer.Deferred}s have their callbacks called when the L{defer.MultiDeferred} does. """ a, b, c = [self.multiDeferred.tee() for _ in xrange(3)] self.assertNoResult(a) self.assertNoResult(b) self.assertNoResult(c) result = object() self.multiDeferred.callback(result) self.assertIdentical(self.successResultOf(a), result) self.assertIdentical(self.successResultOf(b), result) self.assertIdentical(self.successResultOf(c), result) def test_errback(self): """ Any produced L{defer.Deferred}s have their errbacks called when the L{defer.MultiDeferred} does. """ a, b, c = [self.multiDeferred.tee() for _ in xrange(3)] self.assertNoResult(a) self.assertNoResult(b) self.assertNoResult(c) error = RuntimeError() self.multiDeferred.errback(error) self.assertIdentical(self.failureResultOf(a, RuntimeError).value, error) self.assertIdentical(self.failureResultOf(b, RuntimeError).value, error) self.assertIdentical(self.failureResultOf(c, RuntimeError).value, error) def test_callbackAfterCallback(self): """ Calling C{callback} twice raises L{defer.AlreadyCalledError}. """ self.multiDeferred.callback(None) self.assertRaises(defer.AlreadyCalledError, self.multiDeferred.callback, None) def test_callbackAfterErrback(self): """ Calling C{callback} after C{errback} raises L{defer.AlreadyCalledError}. """ self.multiDeferred.errback(RuntimeError()) self.assertRaises(defer.AlreadyCalledError, self.multiDeferred.callback, None) def test_errbackAfterCallback(self): """ Calling C{errback} after C{callback} raises L{defer.AlreadyCalledError}. """ self.multiDeferred.callback(None) self.assertRaises(defer.AlreadyCalledError, self.multiDeferred.errback, RuntimeError()) def test_errbackAfterErrback(self): """ Calling C{errback} after C{errback} raises L{defer.AlreadyCalledError}. """ self.multiDeferred.errback(RuntimeError()) self.assertRaises(defer.AlreadyCalledError, self.multiDeferred.errback, RuntimeError()) def test_synchronousCallbacks(self): """ All callbacks are called sequentially, synchronously, and in the order they were produced. If one or more of the L{defer.Deferred}s produced by L{defer.MultiDeferred.tee} is waiting on a deferred that will never fire, all the other deferreds produced by that method are still fired. """ called = [] result = object() def callback(r, i): """ Checks this is the correct result, adds this deferreds index to the list of called deferreds, and then returns a deferred that will never fire. """ self.assertIdentical(r, result) called.append(i) return defer.Deferred() for i in range(10): self.multiDeferred.tee().addCallback(callback, i=i) self.assertEqual(called, []) self.multiDeferred.callback(result) self.assertEqual(called, range(10)) def test_alreadyFiredWithResult(self): """ If the C{MultiDeferred} already fired, C{tee} produces a C{Deferred} that has already been fired. """ result = object() self.multiDeferred.callback(result) d = self.multiDeferred.tee() self.assertIdentical(self.successResultOf(d), result) def test_alreadyFiredWithError(self): """ If the C{MultiDeferred} already fired with a failure, C{tee} produces a C{Deferred} that has already been fired with the failure. """ error = RuntimeError() self.multiDeferred.errback(error) d = self.multiDeferred.tee() failure = self.failureResultOf(d, RuntimeError) self.assertIdentical(failure.value, error)
isc
-7,686,488,387,015,034,000
31.956835
84
0.629993
false
4.379541
true
false
false
OffenesJena/JenLoRa
LoPy/LoAirRohr01/lib/DHT22RinusW.py
1
2355
from machine import enable_irq, disable_irq import time # this onewire protocol code tested with Pycom LoPy device and AM2302/DHT22 sensor def getval(pin): ms = [1]*700 # needs long sample size to grab all the bits from the DHT time.sleep(1) pin(0) time.sleep_us(10000) pin(1) irqf = disable_irq() for i in range(len(ms)): ms[i] = pin() ## sample input and store value enable_irq(irqf) #for i in range(len(ms)): #print debug for checking raw data # print (ms[i]) return ms def decode(inp): res= [0]*5 bits=[] ix = 0 try: #if inp[0] == 1 : ix = inp.index(0, ix) ## skip to first 0 # ignore first '1' as probably sample of start signal. *But* code seems to be missing the start signal, so jump this line to ensure response signal is identified in next two lines. ix = inp.index(1,ix) ## skip first 0's to next 1 # ignore first '10' bits as probably the response signal. ix = inp.index(0,ix) ## skip first 1's to next 0 while len(bits) < len(res)*8 : ##need 5 * 8 bits : ix = inp.index(1,ix) ## index of next 1 ie = inp.index(0,ix) ## nr of 1's = ie-ix # print ('ie-ix:',ie-ix) bits.append(ie-ix) ix = ie except: print('6: decode error') # print('length:') # print(len(inp), len(bits)) return([0xff,0xff,0xff,0xff]) # print('bits:', bits) for i in range(len(res)): for v in bits[i*8:(i+1)*8]: #process next 8 bit res[i] = res[i]<<1 ##shift byte one place to left if v > 7: # less than 7 '1's is a zero, more than 7 1's in the sequence is a one res[i] = res[i]+1 ##and add 1 if lsb is 1 # print ('res', i, res[i]) if (res[0]+res[1]+res[2]+res[3])&0xff != res[4] : ##parity error! print("Checksum Error") print (res[0:4]) res= [0xff,0xff,0xff,0xff] # print ('res:', res[0:4]) return(res[0:4]) def DHT11(pin): res = decode(getval(pin)) temp = 10 * res[0] + res[1] hum = 10 * res[2] + res[3] return temp, hum def DHT22(pin): res = decode(getval(pin)) hum = res[0] * 256 + res[1] temp = res[2] * 256 + res[3] if (temp > 0x7fff): temp = 0x8000 - temp return temp, hum
apache-2.0
6,041,025,592,553,690,000
33.632353
248
0.546072
false
3.026992
false
false
false
Ilias95/lib389
lib389/tests/dseldif_test.py
1
4107
# --- BEGIN COPYRIGHT BLOCK --- # Copyright (C) 2017 Red Hat, Inc. # All rights reserved. # # License: GPL (version 3 or any later version). # See LICENSE for details. # --- END COPYRIGHT BLOCK --- # import logging import pytest from lib389._constants import * from lib389.dseldif import DSEldif from lib389.topologies import topology_st as topo DEBUGGING = os.getenv('DEBUGGING', False) if DEBUGGING: logging.getLogger(__name__).setLevel(logging.DEBUG) else: logging.getLogger(__name__).setLevel(logging.INFO) log = logging.getLogger(__name__) @pytest.mark.parametrize("entry_dn", (DN_CONFIG, DN_CONFIG_LDBM)) def test_get_singlevalue(topo, entry_dn): """Check that we can get an attribute value under different suffixes""" dse_ldif = DSEldif(topo.standalone) log.info("Get 'cn' attr from {}".format(entry_dn)) attr_values = dse_ldif.get(entry_dn, "cn") assert attr_values == ["config"] log.info("Get 'nonexistent' attr from {}".format(entry_dn)) attr_values = dse_ldif.get(entry_dn, "nonexistent") assert not attr_values def test_get_multivalue(topo): """Check that we can get attribute values""" dse_ldif = DSEldif(topo.standalone) log.info("Get objectClass from {}".format(DN_CONFIG)) attr_values = dse_ldif.get(DN_CONFIG, "objectClass") assert len(attr_values) == 3 assert "top" in attr_values assert "extensibleObject" in attr_values assert "nsslapdConfig" in attr_values @pytest.mark.parametrize("fake_attr_value", ("fake value", "fakevalue")) def test_add(topo, fake_attr_value): """Check that we can add an attribute to a given suffix""" dse_ldif = DSEldif(topo.standalone) fake_attr = "fakeAttr" log.info("Add {} to {}".format(fake_attr, DN_CONFIG)) dse_ldif.add(DN_CONFIG, fake_attr, fake_attr_value) attr_values = dse_ldif.get(DN_CONFIG, fake_attr) assert attr_values == [fake_attr_value] log.info("Clean up") dse_ldif.delete(DN_CONFIG, fake_attr) assert not dse_ldif.get(DN_CONFIG, fake_attr) def test_replace(topo): """Check that we can replace an attribute to a given suffix""" dse_ldif = DSEldif(topo.standalone) port_attr = "nsslapd-port" port_value = "390" log.info("Get default value of {}".format(port_attr)) default_value = dse_ldif.get(DN_CONFIG, port_attr)[0] log.info("Replace {} with {}".format(port_attr, port_value)) dse_ldif.replace(DN_CONFIG, port_attr, port_value) attr_values = dse_ldif.get(DN_CONFIG, port_attr) assert attr_values == [port_value] log.info("Restore default value") dse_ldif.replace(DN_CONFIG, port_attr, default_value) def test_delete_singlevalue(topo): """Check that we can delete an attribute from a given suffix""" dse_ldif = DSEldif(topo.standalone) fake_attr = "fakeAttr" fake_attr_values = ["fake1", "fake2", "fake3"] log.info("Add multivalued {} to {}".format(fake_attr, DN_CONFIG)) for value in fake_attr_values: dse_ldif.add(DN_CONFIG, fake_attr, value) log.info("Delete {}".format(fake_attr_values[0])) dse_ldif.delete(DN_CONFIG, fake_attr, fake_attr_values[0]) attr_values = dse_ldif.get(DN_CONFIG, fake_attr) assert len(attr_values) == 2 assert fake_attr_values[0] not in attr_values assert fake_attr_values[1] in attr_values assert fake_attr_values[2] in attr_values log.info("Clean up") dse_ldif.delete(DN_CONFIG, fake_attr) assert not dse_ldif.get(DN_CONFIG, fake_attr) def test_delete_multivalue(topo): """Check that we can delete attributes from a given suffix""" dse_ldif = DSEldif(topo.standalone) fake_attr = "fakeAttr" fake_attr_values = ["fake1", "fake2", "fake3"] log.info("Add multivalued {} to {}".format(fake_attr, DN_CONFIG)) for value in fake_attr_values: dse_ldif.add(DN_CONFIG, fake_attr, value) log.info("Delete all values of {}".format(fake_attr)) dse_ldif.delete(DN_CONFIG, fake_attr) assert not dse_ldif.get(DN_CONFIG, fake_attr)
gpl-3.0
5,447,008,069,091,832,000
30.592308
75
0.661066
false
3.186191
true
false
false
darbaga/simple_compiler
virtual_machine.py
1
2139
class VirtualMachine: def __init__(self, ram_size=512, executing=True): self.data = {i: None for i in range(ram_size)} self.stack = [] self.executing = executing self.pc = 0 self.devices_start = 256 def push(self, value): """Push something onto the stack.""" self.stack += [value] def pop(self): """Pop something from the stack. Crash if empty.""" return self.stack.pop() def read_memory(self, index): """Read from memory, crashing if index is out of bounds.""" if isinstance(self.data[index], DeviceProxy): return self.data[index].read(index) else: return self.data[index] def write_memory(self, index, value): """Write to memory. Crash if index is out of bounds.""" if isinstance(self.data[index], DeviceProxy): self.data[index].write(index, value) else: self.data[index] = value def register_device(self, device, needed_addresses): """Given an instantiated device and the number of required addresses, registers it in memory""" # If not enough addresses, just error out if self.devices_start+needed_addresses > len(self.data): raise Exception('Not enough addresses to allocate') proxyed_device = DeviceProxy(device, self.devices_start) for i in range(self.devices_start, self.devices_start+needed_addresses): self.data[i] = proxyed_device self.devices_start += needed_addresses def run(self, bytecodes): self.bytecodes = bytecodes while self.executing: increment = self.bytecodes[self.pc].autoincrement self.bytecodes[self.pc].execute(self) if increment: self.pc += 1 class DeviceProxy: """Manages address translation between devices""" def __init__(self, device, pos): self.device = device self.pos = pos def read(self, index): return self.device.read(self.pos-index) def write(self, index, value): self.device.write(self.pos-index, value)
bsd-3-clause
2,304,611,489,600,544,300
34.65
103
0.610566
false
4.113462
false
false
false
gladgod/zhiliao
zhiliao/twitter/defaults.py
1
2296
""" Default settings for the ``mezzanine.twitter`` app. Each of these can be overridden in your project's settings module, just like regular Django settings. The ``editable`` argument for each controls whether the setting is editable via Django's admin. Thought should be given to how a setting is actually used before making it editable, as it may be inappropriate - for example settings that are only read during startup shouldn't be editable, since changing them would require an application reload. """ from __future__ import unicode_literals from django.utils.translation import ugettext_lazy as _ from zhiliao.conf import register_setting from zhiliao.twitter import QUERY_TYPE_CHOICES, QUERY_TYPE_SEARCH register_setting( name="TWITTER_DEFAULT_QUERY_TYPE", label=_("Default Twitter Query Type"), description=_("Type of query that will be used to retrieve tweets for " "the default Twitter feed."), editable=True, default=QUERY_TYPE_SEARCH, choices=QUERY_TYPE_CHOICES, ) register_setting( name="TWITTER_DEFAULT_QUERY", label=_("Default Twitter Query"), description=_("Twitter query to use for the default query type. " "\n\n*Note:* Once you change this from the default, you'll need to " "configure each of the oAuth consumer/access key/secret settings. " "Please refer to http://dev.twitter.com for more information " "on creating an application and acquiring these settings."), editable=True, default="from:stephen_mcd mezzanine", ) register_setting( name="TWITTER_DEFAULT_NUM_TWEETS", label=_("Default Number of Tweets"), description=_("Number of tweets to display in the default Twitter feed."), editable=True, default=3, ) register_setting( name="TWITTER_CONSUMER_KEY", label=_("Twitter OAuth consumer key"), editable=True, default='', ) register_setting( name="TWITTER_CONSUMER_SECRET", label=_("Twitter OAuth consumer secret"), editable=True, default='', ) register_setting( name="TWITTER_ACCESS_TOKEN_KEY", label=_("Twitter OAuth access token"), editable=True, default='', ) register_setting( name="TWITTER_ACCESS_TOKEN_SECRET", label=_("Twitter OAuth access token secret"), editable=True, default='', )
bsd-3-clause
6,396,643,095,361,620,000
29.613333
78
0.708624
false
3.965458
false
false
false
nmmmnu/MessageQueue
protocols/memcachedhandler.py
1
4321
# # Memcached protocol implementation # Nikolay Mihaylov [email protected] # # For Memcached telnet protocol see: # http://blog.elijaa.org/?post/2010/05/21/Memcached-telnet-command-summary import asynchat import time try: from cStringIO import StringIO except ImportError: from StringIO import StringIO class MemcachedHandler(asynchat.async_chat): commands_with_data = ['set', 'add', "sismember"] def __init__(self, sock, addr, processor): # # Constructs new Memcached protocol handler # # @param sock : socket from asyncore # @param addr : address from asyncore # @param processor : processor class # asynchat.async_chat.__init__(self, sock=sock) self.addr = addr self.started = time.time() self.lastping = time.time() self.head = "" self.data = "" self.processor = processor self.state_change("read_header") def state_change(self, state, size = 0): self.io = StringIO() if state == "read_header": self.state = state self.set_terminator("\r\n") return True if state == "read_data": # size == 0 is an error, but we will ignore it. if size < 0: return False self.state = state self.set_terminator(size + len("\r\n") ) return True # Unknown state ? return False def cmd_parse_head(self): m2 = self.head.split(" ") # clean up empty arguments. m = [] for x in m2: x = x.strip() if x != "": m.append(x) # for easy access, put some blanks at the end. while len(m) < 10: m.append("") return m def cmd_parse(self): self.lastping = time.time() args = self.cmd_parse_head() command = args[0].lower() if command == "get": key = args[1] x = self.processor.get(key) if x is None: self.push("END\r\n") return msg = "VALUE %s 0 %d\r\n%s\r\nEND\r\n" % (key, len(x), x) self.push(msg) return if command == "delete": key = args[1] x = self.processor.delete(key) if x: self.push("DELETED\r\n") return self.push("NOT_FOUND\r\n") return if command == "set": # It is protocol responsibility to check the size. try: size = int(args[4]) if len(self.data) > size: self.data = self.data[:size] except: pass key = args[1] x = self.processor.set(key, self.data) if x: self.push("STORED\r\n") return self.push("NOT_STORED\r\n") return if command == "add": # It is protocol responsibility to check the size. try: size = int(args[4]) if len(self.data) > size: self.data = self.data[:size] except: pass key = args[1] x = self.processor.add(key, self.data) if x: self.push("STORED\r\n") return self.push("NOT_STORED\r\n") return # Non standard command if command == "scard": key = args[1] x = self.processor.len(key) if x is None: x = "0" msg = "VALUE %s 0 %d\r\n%s\r\nEND\r\n" % (key, len(x), x) self.push(msg) return # Non standard command if command == "sismember": # It is protocol responsibility to check the size. try: size = int(args[4]) if len(self.data) > size: self.data = self.data[:size] except: pass key = args[1] x = self.processor.contains(key, self.data) if x: self.push("MEMBER\r\n") return self.push("NOT_MEMBER\r\n") return if command == "quit": self.push("QUIT\r\n") self.close() return # error, not implemented self.push("ERROR\r\n") return def state_read_header(self): self.head = self.io.getvalue() m = self.cmd_parse_head() if m[0] in self.commands_with_data: try: size = int(m[4]) except: size = 0 self.state_change("read_data", size) return self.state_change("read_header") self.cmd_parse() def state_read_data(self): self.data = self.io.getvalue() self.state_change("read_header") self.cmd_parse() def found_terminator(self): if self.state == "read_header": return self.state_read_header() if self.state == "read_data": return self.state_read_data() # Unknown state ? return False def collect_incoming_data(self, data): self.io.write(data)
gpl-3.0
3,332,516,228,514,705,400
15.123134
74
0.585744
false
2.837163
false
false
false
pandysong/dxf2kicad_mod
dxf2kicad_mod.py
1
4880
# refer to http://pythonhosted.org/dxfgrabber/# # Note that there must not a line or shape overlapped import sys import math import functools from itertools import groupby import dxfgrabber import kicad_mod_format as kf def _arc_point(center, radius, angle_degree): ''' point defined by arc center,radius, and angel in degree ''' return (center[0] + radius * math.cos(angle_degree/180*math.pi), center[1] + radius * math.sin(angle_degree/180*math.pi)) def _endpoints(entity): ''' return a tuple of start and end points of the entity ''' if "LINE" == entity.dxftype: return (entity.start, entity.end) elif "ARC" == entity.dxftype: return (_arc_point(entity.center, entity.radius, entity.start_angle), _arc_point(entity.center, entity.radius, entity.end_angle)) else: raise TypeError( "[Error]: Unexpceted dxftype {}".format(entity.dxftype)) def _touched(p1, p2): distance_error = 1e-2 return ((math.fabs(p1[0]-p2[0]) < distance_error) and (math.fabs(p1[1]-p2[1]) < distance_error)) def _points_in_entity(ety): if 'LINE' == ety.dxftype: return [ety.start, ety.end] elif 'ARC' == ety.dxftype: if (ety.start_angle > ety.end_angle): ety.end_angle += 360 def angles(start_angle, end_angle, radius): ''' yields descrete angles with step length defined by radius ''' step = 1.0/ety.radius # larger radius indicates small steps angle = start_angle while True: yield angle if (angle + step > ety.end_angle): yield end_angle break else: angle += step return [_arc_point(ety.center, ety.radius, a) for a in angles(ety.start_angle, ety.end_angle, ety.radius)] else: raise TypeError( "[Error]: Unexpceted dxftype {}".format(ety.dxftype)) def fp_polys(layer, entities): ''' yields fp_poly cmd in the layer of `entities` ''' entities = list(entities) def _points_next_to(next_start): for e in entities: start, end = _endpoints(e) pts = _points_in_entity(e) if _touched(next_start, start): return pts, e elif _touched(next_start, end): pts.reverse() return pts, e return None, None def poly(e): start, next_start = _endpoints(e) yield [start] # yield start points while True: pts, pts_e = _points_next_to(next_start) if pts: entities.remove(pts_e) # remove from the set yield pts # yield a list of points next_start = pts[-1] # new start else: if _touched(next_start, start): return else: raise ValueError('Unclosed shape at {}'.format(next_start)) def polys(): while True: if not entities: return e = entities.pop() # pick up one yield poly(e) # yield an iterator which will yields points for p in polys(): poly_points = functools.reduce(lambda x, y: x+y, p) # we may use *point, but since there might be more than 3 values in one # point, we unpack it manually yield kf.fp_poly(children=(kf.pts(children=(kf.xy(point[0], point[1]) for point in poly_points)), kf.layer(layer), kf.width(0.001))) def _layer_entities(entities): seq = list(entities) seq.sort(key=lambda e: e.layer) groups = groupby(seq, lambda e: e.layer) return groups def cmds_from_entities(entities): ''' get all cmd (in kicad_mod_format) from entities which is the entities on all layers. ''' return functools.reduce(lambda x, y: x+y, (list(fp_polys(layer, entities)) for (layer, entities) in _layer_entities(dxf.entities))) if __name__ == '__main__': if len(sys.argv) < 2: print('usage:\n' ' save to a file: python {} ' 'inputfile.dxf > outputfile.kicad_mod\n' ' print to stdout: python {} inputfile.dxf'.format( sys.argv[0], sys.argv[0])) else: dxf = dxfgrabber.readfile(sys.argv[1]) print(str(kf.Module('autogenerated', children=cmds_from_entities(dxf.entities))))
gpl-3.0
8,028,408,619,376,183,000
31.197279
79
0.518648
false
3.92283
false
false
false
cidles/poio-api
src/poioapi/io/graf.py
1
18028
# -*- coding: utf-8 -*- # # Poio Tools for Linguists # # Copyright (C) 2009-2013 Poio Project # Author: António Lopes <[email protected]> # URL: <http://media.cidles.eu/poio/> # For license information, see LICENSE.TXT """ This document contain the responsible methods to write and parse the GrAF files. The parser use the ContentHandler from SAX Xml module. """ from __future__ import absolute_import, unicode_literals import abc import codecs import os from xml.etree.ElementTree import tostring from xml.dom import minidom import graf # GrAF ID's separator GRAFSEPARATOR = ".." (TEXT, AUDIO, VIDEO, NONE) = ("text", "audio", "video", "none") class Tier: """A list of tiers. The name is the tier unique identification. """ __slots__ = ['name', 'annotation_space'] def __init__(self, name, annotation_space=None): self.name = name self.annotation_space = annotation_space class Annotation: """A list of annotations. The id is the annotation identification, the value the annotation value and the features are a dict type of values containing the annotation features. """ __slots__ = ['id', 'value', 'features'] def __init__(self, id, value, features=None): self.value = value self.id = id self.features = features class NodeId: """A list of nodes using a specific format. The prefix is the node type and the index the identification number. """ __slots__ = ['prefix', 'index'] def __init__(self, prefix, index): self.prefix = prefix self.index = str(index) def to_str(self): return "{0}{1}n{2}".format(self.prefix, GRAFSEPARATOR, self.index) def str_edge(self): return "e{0}".format(self.index) def str_region(self): return "{0}{1}r{2}".format(self.prefix, GRAFSEPARATOR, self.index) class PrimaryData: """This class represents the primary data of an AnnotationGraph object. """ def __init__(self): self.type = None self.external_link = None self.filename = None self.content = None class BaseParser(object): """This class is a base class to the parser classes in order to create GrAF objects. This class contains some methods that must be implemented other wise it will be raise a exception error. Although the methods that should be implemented with properly code are the get_root_tiers, get_child_tiers_for_tier and get_annotations_for_tier. The method tier_has_regions and region_for_annotation could simply return None or pass. Raises ------ NotImplementedError Method must be implemented. """ __metaclass__ = abc.ABCMeta @abc.abstractmethod def get_root_tiers(self): """Method to get the root tiers. The root tiers are defined by the parser when the method is implemented. Returns ------- list : array-like List of tiers type. """ raise NotImplementedError("Method must be implemented") @abc.abstractmethod def get_child_tiers_for_tier(self, tier): """Method that get the child tiers of a specific tier. Parameters ---------- tier : object Tier object. Returns ------- list : array-like List of tiers type. See also -------- Tier """ raise NotImplementedError("Method must be implemented") @abc.abstractmethod def get_annotations_for_tier(self, tier, annotation_parent=None): """Method that get all the annotations for a specific tier. The annotations can be filtered using an annotation parent. Parameters ---------- tier : object Tier object. annotation_parent : object Annotation object. Returns ------- list : array-like List of annotations type. See also -------- Tier, Annotation """ raise NotImplementedError("Method must be implemented") @abc.abstractmethod def tier_has_regions(self, tier): """Method to verify if a tier has regions. Parameters ---------- tier : object Tier object. Returns ------- has_region : bool A true or false variable. See also -------- Tier """ raise NotImplementedError("Method must be implemented") @abc.abstractmethod def region_for_annotation(self, annotation): """Method to get the regions values of a specific annotation. Parameters ---------- annotation : object Annotation object. Returns ------- regions : tuple A tuple with the two regions. See also -------- Annotation """ raise NotImplementedError("Method must be implemented") @abc.abstractmethod def get_primary_data(self): """Method to get the primary data of the GrAF file. Returns ------- primaryData : object Object type of PrimaryData class. See also -------- PrimaryData """ raise NotImplementedError("Method must be implemented") class BaseWriter(object): """This class is a base class to the writer classes in order to create files from GrAF objects. This class contains some methods that must be implemented other wise it will be raise a exception error. Raises ------ NotImplementedError Method must be implemented. """ __metaclass__ = abc.ABCMeta @abc.abstractmethod def write(self, outputfile, converter): """Method that will write the GrAF object into a specific format. Parameters ---------- outputfile : str The filename of the output file. The filename should be the header file for GrAF with the extension ".hdr". converter : Converter or AnnotationGraph A converter object. The converter object containes the data that will be use for output. All writers need at least a GrAF graph and the tier hierarchy, some will also need the primary data object. """ raise NotImplementedError("Method must be implemented") class GrAFConverter: """This class handles the conversion of different file formats into GrAF objects and back again. It uses a sub-class of BaseParser to get the annotations and the tier hierarchies. A sub-class of BaseWriter is used to write back the files. Please be aware that meta-data might get lost if you write to a file format from another one. This depends on whether the output file format can store all meta-data from the input file format. In any case all the data and annotation will be stored. """ def __init__(self, parser, writer=None): self.parser = parser self.writer = writer self.graf = graf.Graph() self.tier_hierarchies = [] self.meta_information = None self.primary_data = None self.original_file = None def write(self, outputfile): if self.writer: self.writer.write(outputfile, self) def parse(self): """This method will be the responsible to transform the parser into a GrAF object. This method also retrieves the tiers hierarchies. """ self._tiers_parent_list = [] self.root_tiers = [] tiers_hierarchy_map = {} for tier in self.parser.get_root_tiers(): self.root_tiers.append(tier.name) self._convert_tier(tier, None, None) i = 0 for t in self._tiers_parent_list: if t[1] is None: i += 1 tiers_hierarchy_map[str(i)] = [t[0]] else: self._append_tier_to_hierarchy(tiers_hierarchy_map[str(i)], t[1], t[0]) for i, hierarchy in tiers_hierarchy_map.items(): self.tier_hierarchies.append(hierarchy) if hasattr(self.parser, 'meta_information'): self.meta_information = self.parser.meta_information self.primary_data = self.parser.get_primary_data() if hasattr(self.parser, 'filepath') and \ isinstance(self.parser.filepath, str): self.original_file = os.path.abspath(self.parser.filepath) def _convert_tier(self, tier, parent_node, parent_annotation, parent_prefix=None): child_tiers = self.parser.get_child_tiers_for_tier(tier) if tier.annotation_space is None: prefix = tier.name annotation_name = prefix else: annotation_name = tier.annotation_space.replace(' ', '_') prefix = "{0}{1}{2}".format(annotation_name, GRAFSEPARATOR, tier.name) has_regions = False if self.parser.tier_has_regions(tier): has_regions = True self._add_tier_in_hierarchy_list(prefix, parent_prefix) annotations = self.parser.get_annotations_for_tier(tier, parent_annotation) for annotation in annotations: regions = None if has_regions: regions = self.parser.region_for_annotation(annotation) node_id = NodeId(prefix, annotation.id) self._add_node(node_id, annotation, annotation_name, regions, parent_node) self._add_root_nodes(prefix, node_id) if child_tiers: for t in child_tiers: self._convert_tier(t, node_id, annotation, prefix) if annotations == [] and child_tiers: for t in child_tiers: self._convert_tier(t, None, None, prefix) def _add_tier_in_hierarchy_list(self, prefix, parent_prefix): if not (prefix, parent_prefix) in self._tiers_parent_list: self._tiers_parent_list.append((prefix, parent_prefix)) def _append_tier_to_hierarchy(self, tiers_list, parent_tier, tier): for t in tiers_list: if isinstance(t, list): self._append_tier_to_hierarchy(t, parent_tier, tier) else: if t == parent_tier: tiers_list.append([tier]) def _add_node(self, node_id, annotation, annotation_name, regions, from_node_id): self._add_node_to_graph(node_id, regions, from_node_id) self._add_graf_annotation(annotation_name, annotation.id, node_id, annotation.value, annotation.features) def _add_root_nodes(self, prefix, node_id): if prefix in self.root_tiers: self.graf.header.roots.append(node_id.to_str()) def _add_graf_annotation(self, annotation_name, annotation_id, annotation_ref, annotation_value, annotation_features=None): annotation = graf.Annotation(annotation_name, annotation_features, annotation_id) if annotation_value is not None: annotation.features['annotation_value'] = annotation_value self.graf.nodes[annotation_ref.to_str()].annotations.add(annotation) if annotation_name in self.graf.annotation_spaces: #if annotation not in self.graf.annotation_spaces[annotation_name]: self.graf.annotation_spaces[annotation_name].add(annotation) else: annotation_space = graf.AnnotationSpace(annotation_name) annotation_space.add(annotation) self.graf.annotation_spaces.add(annotation_space) def _add_node_to_graph(self, node_id, regions=None, from_node_id=None): node = graf.Node(node_id.to_str()) if from_node_id is not None: edge_id = node_id.str_edge() self.graf.create_edge(self.graf.nodes[from_node_id.to_str()], node, edge_id) if regions is not None: region_id = node_id.str_region() region = graf.Region(region_id, *regions) node.add_region(region) self.graf.regions.add(region) self.graf.nodes.add(node) class Writer(BaseWriter): def __init__(self, **kwargs): self.tier_hierarchies = None self.meta_information = None self.standoffheader = graf.StandoffHeader(**kwargs) def _flatten_hierarchy_elements(self, elements): """Flat the elements appended to a new list of elements. Parameters ---------- elements : array_like An array of string values. Returns ------- flat_elements : array_like An array of flattened `elements`. """ flat_elements = [] for e in elements: if type(e) is list: flat_elements.extend(self._flatten_hierarchy_elements(e)) else: flat_elements.append(e) return flat_elements def write(self, outputfile, ag): """Writes an AnnotationGraph object as GrAF files. Parameters ---------- outputfile : str The filename of the output file. The filename should be the header file for GrAF with the extension ".hdr". ag : poioapi.annotationgraph.AnnotationGraph An AnnotationGraph object. The AG object containes the data that will be use for output. """ (basedirname, _) = os.path.splitext(outputfile) self._get_parents(ag.tier_hierarchies) standoffrenderer = graf.StandoffHeaderRenderer("{0}.hdr".format( basedirname)) for tier_name in self._flatten_hierarchy_elements( ag.tier_hierarchies): annotation_space = tier_name.split(GRAFSEPARATOR)[0] out_graf = graf.Graph() renderer = graf.GrafRenderer("{0}-{1}.xml".format( basedirname, annotation_space )) out_graf.nodes = [n for n in ag.graf.nodes if n.id.startswith(tier_name)] out_graf.edges = [e for e in ag.graf.edges if e.to_node.id.startswith(tier_name)] out_graf.regions = [r for r in ag.graf.regions if r.id.startswith(tier_name)] out_graf.annotation_spaces.add(graf.AnnotationSpace( annotation_space)) out_graf.header.add_dependency(self._parent[tier_name]) out_graf = self._add_root_nodes(ag.graf, annotation_space, out_graf) renderer.render(out_graf) basename = os.path.basename(basedirname) self.standoffheader.datadesc.add_annotation( "{0}-{1}.xml".format(basename, annotation_space), annotation_space) self._add_primary_data(ag.primary_data, basedirname) standoffrenderer.render(self.standoffheader) self._generate_metafile(basedirname, ag.meta_information) def _add_root_nodes(self, graph, annotation_space, out_graf): for root in graph.header.roots: if annotation_space in root: out_graf.header.roots.append(root) return out_graf def _get_parents(self, tier_hierarchies): self._parent = {} for h in tier_hierarchies: self._get_hierarchy_parents(h, None) def _get_hierarchy_parents(self, hierarchy, parent): for i, h in enumerate(hierarchy): if isinstance(h, list): self._get_hierarchy_parents(h, parent) else: self._parent[h] = parent if i is 0: parent = h.split(GRAFSEPARATOR)[0] def _add_primary_data(self, primary_data, basedirname): if primary_data.external_link: loc = primary_data.external_link elif primary_data.content: loc = self._create_raw_txt_file(primary_data.content, basedirname) elif primary_data.filename: loc = primary_data.filename self.standoffheader.datadesc.primaryData = {'loc': loc, 'f.id': primary_data.type} def _create_raw_txt_file(self, content, basedirname): filename = "{0}.txt".format(os.path.splitext(basedirname)[0]) file = os.path.abspath(filename) f = codecs.open(file, 'w', 'utf-8') f.write(content) f.close() return os.path.basename(filename) def _generate_metafile(self, basedirname, meta_information=None): """Generate a metafile with all the extra information extracted from a file when it is parsed. Parameters ---------- basedirname : str Base name of the inpufile. meta_information: ElementTree ElementTree with the extra information. """ if meta_information is not None: out = open("{0}-extinfo.xml".format(basedirname), "wb") doc = minidom.parseString(tostring(meta_information, encoding="utf-8")) out.write(doc.toprettyxml(encoding='utf-8')) out.close()
apache-2.0
-3,918,818,425,620,900,400
28.818803
80
0.568758
false
4.361723
false
false
false
brain-research/mirage-rl-qprop
sandbox/rocky/tf/q_functions/continuous_mlp_q_function.py
1
6100
from sandbox.rocky.tf.q_functions.base import QFunction import numpy as np from rllab.core.serializable import Serializable from sandbox.rocky.tf.core.layers_powered import LayersPowered from sandbox.rocky.tf.core.layers import batch_norm from sandbox.rocky.tf.policies.base import StochasticPolicy from sandbox.rocky.tf.misc import tensor_utils import tensorflow as tf import sandbox.rocky.tf.core.layers as L class ContinuousMLPQFunction(QFunction, LayersPowered, Serializable): def __init__( self, env_spec, name='qnet', hidden_sizes=(32, 32), hidden_nonlinearity=tf.nn.relu, action_merge_layer=-2, output_nonlinearity=None, eqf_use_full_qf=False, eqf_sample_size=1, mqprop=False, bn=False): Serializable.quick_init(self, locals()) assert not env_spec.action_space.is_discrete self._env_spec = env_spec with tf.variable_scope(name): l_obs = L.InputLayer(shape=(None, env_spec.observation_space.flat_dim), name="obs") l_action = L.InputLayer(shape=(None, env_spec.action_space.flat_dim), name="actions") n_layers = len(hidden_sizes) + 1 if n_layers > 1: action_merge_layer = \ (action_merge_layer % n_layers + n_layers) % n_layers else: action_merge_layer = 1 l_hidden = l_obs for idx, size in enumerate(hidden_sizes): if bn: l_hidden = batch_norm(l_hidden) if idx == action_merge_layer: l_hidden = L.ConcatLayer([l_hidden, l_action]) l_hidden = L.DenseLayer( l_hidden, num_units=size, nonlinearity=hidden_nonlinearity, name="h%d" % (idx + 1) ) if action_merge_layer == n_layers: l_hidden = L.ConcatLayer([l_hidden, l_action]) l_output = L.DenseLayer( l_hidden, num_units=1, nonlinearity=output_nonlinearity, name="output" ) output_var = L.get_output(l_output, deterministic=True) output_var = tf.reshape(output_var, (-1,)) self._f_qval = tensor_utils.compile_function([l_obs.input_var, l_action.input_var], output_var) self._output_layer = l_output self._obs_layer = l_obs self._action_layer = l_action self._output_nonlinearity = output_nonlinearity self.eqf_use_full_qf=eqf_use_full_qf self.eqf_sample_size=eqf_sample_size self.mqprop=mqprop LayersPowered.__init__(self, [l_output]) def get_qval(self, observations, actions): return self._f_qval(observations, actions) def get_qval_sym(self, obs_var, action_var, **kwargs): qvals = L.get_output( self._output_layer, {self._obs_layer: obs_var, self._action_layer: action_var}, **kwargs ) return tf.reshape(qvals, (-1,)) def get_e_qval(self, observations, policy): if isinstance(policy, StochasticPolicy): agent_info = policy.dist_info(observations) means, log_stds = agent_info['mean'], agent_info['log_std'] if self.eqf_use_full_qf and self.eqf_sample_size > 1: observations = np.repeat(observations, self.eqf_sample_size, axis=0) means = np.repeat(means, self.eqf_sample_size, axis=0) stds = np.repeat(np.exp(log_stds), self.eqf_sample_size, axis=0) randoms = np.random.randn(*(means)) actions = means + stds * randoms all_qvals = self.get_qval(observations, actions) qvals = np.mean(all_qvals.reshape((-1,self.eqf_sample_size)),axis=1) else: qvals = self.get_qval(observations, means) else: actions, _ = policy.get_actions(observations) qvals = self.get_qval(observations, actions) return qvals def _get_e_qval_sym(self, obs_var, policy, **kwargs): if isinstance(policy, StochasticPolicy): agent_info = policy.dist_info_sym(obs_var) mean_var, log_std_var = agent_info['mean'], agent_info['log_std'] if self.eqf_use_full_qf: assert self.eqf_sample_size > 0 if self.eqf_sample_size == 1: action_var = tf.random_normal(shape=tf.shape(mean_var))*tf.exp(log_std_var) + mean_var return self.get_qval_sym(obs_var, action_var, **kwargs), action_var else: raise NotImplementedError else: return self.get_qval_sym(obs_var, mean_var, **kwargs), mean_var else: action_var = policy.get_action_sym(obs_var) return self.get_qval_sym(obs_var, action_var, **kwargs), action_var def get_e_qval_sym(self, obs_var, policy, **kwargs): return self._get_e_qval_sym(obs_var, policy, **kwargs)[0] def get_cv_sym(self, obs_var, action_var, policy, **kwargs): if self.eqf_use_full_qf: qvals = self.get_qval_sym(obs_var, action_var, deterministic=True, **kwargs) e_qvals = self.get_e_qval_sym(obs_var, policy, deterministic=True, **kwargs) return qvals - e_qvals else: if self.mqprop: # Just use zero-order Taylor expansion (aka just the constant qvals) qvals, action0 = self._get_e_qval_sym(obs_var, policy, deterministic=True, **kwargs) return qvals else: qvals, action0 = self._get_e_qval_sym(obs_var, policy, deterministic=True, **kwargs) # Use first-order Taylor expansion qprimes = tf.gradients(qvals, action0)[0] deltas = action_var - action0 return tf.reduce_sum(deltas * qprimes, 1)
mit
-6,248,511,351,676,138,000
40.216216
107
0.562787
false
3.605201
false
false
false
blockstack/blockstack-server
integration_tests/blockstack_integration_tests/scenarios/name_import_expire_pre_reg_expire_pay2ns_multi.py
1
8928
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Blockstack ~~~~~ copyright: (c) 2014-2015 by Halfmoon Labs, Inc. copyright: (c) 2016 by Blockstack.org This file is part of Blockstack Blockstack 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. Blockstack 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 Blockstack. If not, see <http://www.gnu.org/licenses/>. """ # activate F-day 2017 """ TEST ENV BLOCKSTACK_EPOCH_1_END_BLOCK 682 TEST ENV BLOCKSTACK_EPOCH_2_END_BLOCK 683 TEST ENV BLOCKSTACK_EPOCH_2_NAMESPACE_LIFETIME_MULTIPLIER 1 TEST ENV BLOCKSTACK_EPOCH_3_NAMESPACE_LIFETIME_MULTIPLIER 1 TEST ENV BLOCKSTACK_EPOCH_3_NAMESPACE_LIFETIME_GRACE_PERIOD 0 TEST ENV BLOCKSTACK_EPOCH_3_NAMESPACE_RECEIVE_FEES_PERIOD 22 """ import testlib import virtualchain import blockstack wallets = [ testlib.Wallet( "5JesPiN68qt44Hc2nT8qmyZ1JDwHebfoh9KQ52Lazb1m1LaKNj9", 100000000000 ), testlib.Wallet( "5KHqsiU9qa77frZb6hQy9ocV7Sus9RWJcQGYYBJJBb2Efj1o77e", 100000000000 ), testlib.Wallet( "5Kg5kJbQHvk1B64rJniEmgbD83FpZpbw2RjdAZEzTefs9ihN3Bz", 100000000000 ), testlib.Wallet( "5JuVsoS9NauksSkqEjbUZxWwgGDQbMwPsEfoRBSpLpgDX1RtLX7", 100000000000 ), testlib.Wallet( "5KEpiSRr1BrT8vRD7LKGCEmudokTh1iMHbiThMQpLdwBwhDJB1T", 100000000000 ) ] consensus = "17ac43c1d8549c3181b200f1bf97eb7d" def scenario( wallets, **kw ): testlib.blockstack_namespace_preorder( "test", wallets[1].addr, wallets[0].privkey ) testlib.next_block( **kw ) testlib.blockstack_namespace_reveal( "test", wallets[1].addr, 3, 250, 4, [6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0], 10, 10, wallets[0].privkey, version_bits=2) testlib.next_block( **kw ) resp = testlib.blockstack_name_import( "foo.test", wallets[3].addr, "11" * 20, wallets[1].privkey ) if 'error' in resp: print json.dumps( resp, indent=4 ) return False testlib.next_block( **kw ) testlib.blockstack_namespace_ready( "test", wallets[1].privkey ) testlib.next_block( **kw ) namespace_rec = testlib.blockstack_cli_get_namespace_blockchain_record("test") if 'error' in namespace_rec: print namespace_rec return False namespace_balance = testlib.get_balance(namespace_rec['address']) burn_balance = testlib.get_balance(blockstack.lib.config.BLOCKSTACK_BURN_ADDRESS) testlib.next_block( **kw ) testlib.next_block( **kw ) testlib.next_block( **kw ) testlib.next_block( **kw ) # expired res = testlib.blockstack_name_preorder( "foo.test", wallets[2].privkey, wallets[3].addr ) # +name_cost if 'error' in res: print res return False testlib.next_block( **kw ) res = testlib.blockstack_name_register( "foo.test", wallets[2].privkey, wallets[3].addr ) if 'error' in res: print res return False testlib.next_block( **kw ) testlib.next_block( **kw ) testlib.next_block( **kw ) testlib.next_block( **kw ) testlib.next_block( **kw ) # expired res = testlib.blockstack_name_preorder( "foo.test", wallets[3].privkey, wallets[4].addr ) # +name_cost if 'error' in res: print res return False testlib.next_block( **kw ) res = testlib.blockstack_name_register( "foo.test", wallets[3].privkey, wallets[4].addr ) if 'error' in res: print res return False testlib.next_block( **kw ) testlib.next_block( **kw ) res = testlib.blockstack_name_renew("foo.test", wallets[4].privkey) # +name_cost if 'error' in res: print res return False testlib.next_block( **kw ) testlib.next_block( **kw ) testlib.next_block( **kw ) testlib.next_block( **kw ) testlib.next_block( **kw ) # expired res = testlib.blockstack_name_preorder( "foo.test", wallets[2].privkey, wallets[3].addr ) # +name_cost if 'error' in res: print res return False testlib.next_block( **kw ) res = testlib.blockstack_name_register( "foo.test", wallets[2].privkey, wallets[3].addr ) if 'error' in res: print res return False testlib.next_block( **kw ) new_namespace_balance = testlib.get_balance(namespace_rec['address']) name_rec = testlib.get_name_blockchain_record('foo.test') name_cost = name_rec['op_fee'] testlib.next_block( **kw ) testlib.next_block( **kw ) # stop fee collection testlib.next_block( **kw ) testlib.next_block( **kw ) # expired if new_namespace_balance - namespace_balance != 4*name_cost: print 'address {} did not get credited'.format(namespace_rec['address']) print '{} != {} + 4*{}'.format(new_namespace_balance, namespace_balance, name_cost) return False # preorder should send to the null burn address now. res = testlib.blockstack_name_preorder( "foo2.test", wallets[4].privkey, wallets[0].addr ) # does not pay to namespace if 'error' in res: print res return False # try forcing it to the namespace burn address, to verify that it fails res = testlib.blockstack_name_preorder( "foo_fail.test", wallets[4].privkey, wallets[0].addr, burn_addr=namespace_rec['address'], expect_fail=True ) # does not pay to namespace (command fails) if 'error' not in res: print res return False res = testlib.blockstack_name_preorder( "foo_fail.test", wallets[4].privkey, wallets[0].addr, burn_addr=namespace_rec['address'], price={'units': 'BTC', 'amount': name_cost}, safety_checks=False, tx_fee=10000*5 ) # +name_cost if 'error' in res: print res return False testlib.next_block( **kw ) testlib.expect_snv_fail_at('foo_fail.test', testlib.get_current_block(**kw)) # should be accepted res = testlib.blockstack_name_register( "foo2.test", wallets[4].privkey, wallets[0].addr ) if 'error' in res: print res return False # should be rejected res = testlib.blockstack_name_register( "foo_fail.test", wallets[4].privkey, wallets[0].addr, safety_checks=False ) if 'error' in res: print res return False testlib.next_block( **kw ) testlib.expect_snv_fail_at('foo_fail.test', testlib.get_current_block(**kw)) # should have been rejected due to wrong burn address whois = testlib.blockstack_cli_whois('foo_fail.test') if 'error' not in whois: print whois return False new_burn_balance = testlib.get_balance(blockstack.lib.config.BLOCKSTACK_BURN_ADDRESS) new_namespace_balance = testlib.get_balance(namespace_rec['address']) name_rec_2 = testlib.get_name_blockchain_record('foo2.test') name_cost_2 = name_rec_2['op_fee'] # namespace should NOT have gotten the fee for foo_fail. It should only have gotten it for foo.test if new_namespace_balance - namespace_balance < 5*name_cost or new_namespace_balance - namespace_balance > 6*name_cost: print 'address {} got credited after fee capture period'.format(namespace_rec['address']) print '{} != {} + 5*{}'.format(new_namespace_balance, namespace_balance, name_cost) return False # burn address should have received the fee for the second name if new_burn_balance - name_cost_2 != burn_balance: print 'null burn address did not get credited' print '{} != {} + {}'.format(new_burn_balance, burn_balance, name_cost_2) return False def check( state_engine ): # not revealed, but ready ns = state_engine.get_namespace_reveal( "test" ) if ns is not None: print "namespace reveal exists" return False ns = state_engine.get_namespace( "test" ) if ns is None: print "no namespace" return False if ns['namespace_id'] != 'test': print "wrong namespace" return False for name in ['foo2.test']: # not preordered preorder = state_engine.get_name_preorder( name, virtualchain.make_payment_script(wallets[4].addr), wallets[0].addr ) if preorder is not None: print "preorder exists" return False # registered name_rec = state_engine.get_name( name ) if name_rec is None: print "name does not exist" return False # owned by if name_rec['address'] != wallets[0].addr or name_rec['sender'] != virtualchain.make_payment_script(wallets[0].addr): print "sender is wrong" return False return True
gpl-3.0
-6,916,285,018,192,869,000
35.292683
230
0.660506
false
3.173836
true
false
false
diefenbach/django-cba
cba/layouts.py
1
1576
from . base import Component class Grid(Component): """A CSS grid layout. A grid consists arbitrary rows and 16 columns per row. see http://semantic-ui.com/collections/grid.html for more. """ template = "cba/layouts/grid.html" class Column(Component): """A column of a grid. width The width of the column. Valid values are 1-16. A row consist of maxmimal 16 columns but can be ended explicitly. """ template = "cba/layouts/column.html" # TODO: This needs be moved out of Python, to be independent of the use ui # system WIDTH = ["love", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen"] def __init__(self, id=None, width=16, *args, **kwargs): super(Column, self).__init__(id, *args, **kwargs) self.width = self.WIDTH[width] class Row(Component): """A row of a grid. It can be used to end a row explicitly. """ template = "cba/layouts/row.html" class Split(Component): """Splits the screen in two or more panels. All direct sub components are splitted into an own panel. Split components can be nested. direction The direction of the splitting. One of ``vertical`` or ``horizontal``. """ template = "cba/layouts/split.html" def __init__(self, id=None, direction="vertical", *args, **kwargs): super(Split, self).__init__(id, *args, **kwargs) self.direction = direction
bsd-3-clause
7,044,278,349,596,012,000
28.185185
107
0.607234
false
3.734597
false
false
false
lekston/ardupilot
Tools/autotest/autotest.py
1
18930
#!/usr/bin/env python """ APM automatic test suite Andrew Tridgell, October 2011 """ from __future__ import print_function import atexit import fnmatch import glob import optparse import os import shutil import signal import sys import time import traceback from apmrover2 import * from arducopter import * from quadplane import * from arduplane import * from ardusub import * from pysim import util from pymavlink import mavutil from pymavlink.generator import mavtemplate def buildlogs_dirpath(): return os.getenv("BUILDLOGS", util.reltopdir("../buildlogs")) def buildlogs_path(path): '''return a string representing path in the buildlogs directory''' bits = [buildlogs_dirpath()] if isinstance(path, list): bits.extend(path) else: bits.append(path) return os.path.join(*bits) def get_default_params(atype, binary): """Get default parameters.""" # use rover simulator so SITL is not starved of input HOME = mavutil.location(40.071374969556928, -105.22978898137808, 1583.702759, 246) if "plane" in binary or "rover" in binary: frame = "rover" else: frame = "+" home = "%f,%f,%u,%u" % (HOME.lat, HOME.lng, HOME.alt, HOME.heading) sitl = util.start_SITL(binary, wipe=True, model=frame, home=home, speedup=10, unhide_parameters=True) mavproxy = util.start_MAVProxy_SITL(atype) print("Dumping defaults") idx = mavproxy.expect(['Please Run Setup', 'Saved [0-9]+ parameters to (\S+)']) if idx == 0: # we need to restart it after eeprom erase util.pexpect_close(mavproxy) util.pexpect_close(sitl) sitl = util.start_SITL(binary, model=frame, home=home, speedup=10) mavproxy = util.start_MAVProxy_SITL(atype) idx = mavproxy.expect('Saved [0-9]+ parameters to (\S+)') parmfile = mavproxy.match.group(1) dest = buildlogs_path('%s-defaults.parm' % atype) shutil.copy(parmfile, dest) util.pexpect_close(mavproxy) util.pexpect_close(sitl) print("Saved defaults for %s to %s" % (atype, dest)) return True def build_all(): """Run the build_all.sh script.""" print("Running build_all.sh") if util.run_cmd(util.reltopdir('Tools/scripts/build_all.sh'), directory=util.reltopdir('.')) != 0: print("Failed build_all.sh") return False return True def build_binaries(): """Run the build_binaries.py script.""" print("Running build_binaries.py") # copy the script as it changes git branch, which can change the script while running orig = util.reltopdir('Tools/scripts/build_binaries.py') copy = util.reltopdir('./build_binaries.py') shutil.copy2(orig, copy) # also copy generate_manifest library: orig_gm = util.reltopdir('Tools/scripts/generate_manifest.py') copy_gm = util.reltopdir('./generate_manifest.py') shutil.copy2(orig_gm, copy_gm) if util.run_cmd(copy, directory=util.reltopdir('.')) != 0: print("Failed build_binaries.py") return False return True def build_devrelease(): """Run the build_devrelease.sh script.""" print("Running build_devrelease.sh") # copy the script as it changes git branch, which can change the script while running orig = util.reltopdir('Tools/scripts/build_devrelease.sh') copy = util.reltopdir('./build_devrelease.sh') shutil.copy2(orig, copy) if util.run_cmd(copy, directory=util.reltopdir('.')) != 0: print("Failed build_devrelease.sh") return False return True def build_examples(): """Build examples.""" for target in 'px4-v2', 'navio': print("Running build.examples for %s" % target) try: util.build_examples(target) except Exception as e: print("Failed build_examples on board=%s" % target) print(str(e)) return False return True def build_parameters(): """Run the param_parse.py script.""" print("Running param_parse.py") for vehicle in 'ArduPlane', 'ArduCopter', 'ArduSub', 'APMrover2', 'AntennaTracker': if util.run_cmd([util.reltopdir('Tools/autotest/param_metadata/param_parse.py'), '--vehicle', vehicle], directory=util.reltopdir('.')) != 0: print("Failed param_parse.py (%s)" % vehicle) return False return True def convert_gpx(): """Convert any tlog files to GPX and KML.""" mavlog = glob.glob(buildlogs_path("*.tlog")) passed = True for m in mavlog: util.run_cmd(util.reltopdir("modules/mavlink/pymavlink/tools/mavtogpx.py") + " --nofixcheck " + m) gpx = m + '.gpx' kml = m + '.kml' try: util.run_cmd('gpsbabel -i gpx -f %s -o kml,units=m,floating=1,extrude=1 -F %s' % (gpx, kml)) except CalledProcessError as e: passed = False try: util.run_cmd('zip %s.kmz %s.kml' % (m, m)) except CalledProcessError as e: passed = False util.run_cmd("mavflightview.py --imagefile=%s.png %s" % (m, m)) return passed def test_prerequisites(): """Check we have the right directories and tools to run tests.""" print("Testing prerequisites") util.mkdir_p(buildlogs_dirpath()) return True def alarm_handler(signum, frame): """Handle test timeout.""" global results, opts try: results.add('TIMEOUT', '<span class="failed-text">FAILED</span>', opts.timeout) util.pexpect_close_all() convert_gpx() write_fullresults() os.killpg(0, signal.SIGKILL) except Exception: pass sys.exit(1) def should_run_step(step): """See if a step should be skipped.""" for skip in skipsteps: if fnmatch.fnmatch(step.lower(), skip.lower()): return False return True __bin_names = { "ArduCopter" : "arducopter", "ArduPlane" : "arduplane", "APMrover2" : "ardurover", "AntennaTracker" : "antennatracker", "CopterAVC" : "arducopter-heli", "QuadPlane" : "arduplane", "ArduSub" : "ardusub" } def binary_path(step, debug=False): try: vehicle = step.split(".")[1] except Exception: return None if vehicle in __bin_names: binary_name = __bin_names[vehicle] else: # cope with builds that don't have a specific binary return None binary_basedir = "sitl" binary = util.reltopdir(os.path.join('build', binary_basedir, 'bin', binary_name)) if not os.path.exists(binary): if os.path.exists(binary + ".exe"): binary += ".exe" else: raise ValueError("Binary (%s) does not exist" % (binary,)) return binary def run_step(step): """Run one step.""" # remove old logs util.run_cmd('/bin/rm -f logs/*.BIN logs/LASTLOG.TXT') if step == "prerequisites": return test_prerequisites() build_opts = { "j": opts.j, "debug": opts.debug, "clean": not opts.no_clean, "configure": not opts.no_configure, } if step == 'build.ArduPlane': return util.build_SITL('bin/arduplane', **build_opts) if step == 'build.APMrover2': return util.build_SITL('bin/ardurover', **build_opts) if step == 'build.ArduCopter': return util.build_SITL('bin/arducopter', **build_opts) if step == 'build.AntennaTracker': return util.build_SITL('bin/antennatracker', **build_opts) if step == 'build.Helicopter': return util.build_SITL('bin/arducopter-heli', **build_opts) if step == 'build.ArduSub': return util.build_SITL('bin/ardusub', **build_opts) binary = binary_path(step, debug=opts.debug) if step.startswith("defaults"): vehicle = step[9:] return get_default_params(vehicle, binary) fly_opts = { "viewerip": opts.viewerip, "use_map": opts.map, "valgrind": opts.valgrind, "gdb": opts.gdb, "gdbserver": opts.gdbserver, } if opts.speedup is not None: fly_opts["speedup"] = opts.speedup if step == 'fly.ArduCopter': arducopter = AutoTestCopter(binary, frame=opts.frame, **fly_opts) return arducopter.autotest() if step == 'fly.CopterAVC': arducopter = AutoTestCopter(binary, **fly_opts) return arducopter.autotest_heli() if step == 'fly.ArduPlane': arduplane = AutoTestPlane(binary, **fly_opts) return arduplane.autotest() if step == 'fly.QuadPlane': quadplane = AutoTestQuadPlane(binary, **fly_opts) return quadplane.autotest() if step == 'drive.APMrover2': apmrover2 = AutoTestRover(binary, frame=opts.frame, **fly_opts) return apmrover2.autotest() if step == 'dive.ArduSub': ardusub = AutoTestSub(binary, **fly_opts) return ardusub.autotest() if step == 'build.All': return build_all() if step == 'build.Binaries': return build_binaries() if step == 'build.DevRelease': return build_devrelease() if step == 'build.Examples': return build_examples() if step == 'build.Parameters': return build_parameters() if step == 'convertgpx': return convert_gpx() raise RuntimeError("Unknown step %s" % step) class TestResult(object): """Test result class.""" def __init__(self, name, result, elapsed): self.name = name self.result = result self.elapsed = "%.1f" % elapsed class TestFile(object): """Test result file.""" def __init__(self, name, fname): self.name = name self.fname = fname class TestResults(object): """Test results class.""" def __init__(self): self.date = time.asctime() self.githash = util.run_cmd('git rev-parse HEAD', output=True, directory=util.reltopdir('.')).strip() self.tests = [] self.files = [] self.images = [] def add(self, name, result, elapsed): """Add a result.""" self.tests.append(TestResult(name, result, elapsed)) def addfile(self, name, fname): """Add a result file.""" self.files.append(TestFile(name, fname)) def addimage(self, name, fname): """Add a result image.""" self.images.append(TestFile(name, fname)) def addglob(self, name, pattern): """Add a set of files.""" for f in glob.glob(buildlogs_path(pattern)): self.addfile(name, os.path.basename(f)) def addglobimage(self, name, pattern): """Add a set of images.""" for f in glob.glob(buildlogs_path(pattern)): self.addimage(name, os.path.basename(f)) def write_webresults(results_to_write): """Write webpage results.""" t = mavtemplate.MAVTemplate() for h in glob.glob(util.reltopdir('Tools/autotest/web/*.html')): html = util.loadfile(h) f = open(buildlogs_path(os.path.basename(h)), mode='w') t.write(f, html, results_to_write) f.close() for f in glob.glob(util.reltopdir('Tools/autotest/web/*.png')): shutil.copy(f, buildlogs_path(os.path.basename(f))) def write_fullresults(): """Write out full results set.""" global results results.addglob("Google Earth track", '*.kmz') results.addfile('Full Logs', 'autotest-output.txt') results.addglob('DataFlash Log', '*-log.bin') results.addglob("MAVLink log", '*.tlog') results.addglob("GPX track", '*.gpx') # results common to all vehicles: vehicle_files = [ ('{vehicle} build log', '{vehicle}.txt'), ('{vehicle} code size', '{vehicle}.sizes.txt'), ('{vehicle} stack sizes', '{vehicle}.framesizes.txt'), ('{vehicle} defaults', '{vehicle}-defaults.parm'), ('{vehicle} core', '{vehicle}.core'), ('{vehicle} ELF', '{vehicle}.elf'), ] vehicle_globs = [('{vehicle} log', '{vehicle}-*.BIN'), ] for vehicle in 'ArduPlane','ArduCopter','APMrover2','AntennaTracker', 'ArduSub': subs = { 'vehicle': vehicle } for vehicle_file in vehicle_files: description = vehicle_file[0].format(**subs) filename = vehicle_file[1].format(**subs) results.addfile(description, filename) for vehicle_glob in vehicle_globs: description = vehicle_glob[0].format(**subs) glob = vehicle_glob[1].format(**subs) results.addglob(description, glob) results.addglob("CopterAVC log", 'CopterAVC-*.BIN') results.addfile("CopterAVC core", 'CopterAVC.core') results.addglob('APM:Libraries documentation', 'docs/libraries/index.html') results.addglob('APM:Plane documentation', 'docs/ArduPlane/index.html') results.addglob('APM:Copter documentation', 'docs/ArduCopter/index.html') results.addglob('APM:Rover documentation', 'docs/APMrover2/index.html') results.addglob('APM:Sub documentation', 'docs/ArduSub/index.html') results.addglobimage("Flight Track", '*.png') write_webresults(results) def check_logs(step): """Check for log files from a step.""" print("check step: ", step) if step.startswith('fly.'): vehicle = step[4:] elif step.startswith('drive.'): vehicle = step[6:] else: return logs = glob.glob("logs/*.BIN") for log in logs: bname = os.path.basename(log) newname = buildlogs_path("%s-%s" % (vehicle, bname)) print("Renaming %s to %s" % (log, newname)) shutil.move(log, newname) corefile = "core" if os.path.exists(corefile): newname = buildlogs_path("%s.core" % vehicle) print("Renaming %s to %s" % (corefile, newname)) shutil.move(corefile, newname) try: util.run_cmd('/bin/cp build/sitl/bin/* %s' % buildlogs_dirpath(), directory=util.reltopdir('.')) except Exception: print("Unable to save binary") def run_tests(steps): """Run a list of steps.""" global results passed = True failed = [] for step in steps: util.pexpect_close_all() t1 = time.time() print(">>>> RUNNING STEP: %s at %s" % (step, time.asctime())) try: if run_step(step): results.add(step, '<span class="passed-text">PASSED</span>', time.time() - t1) print(">>>> PASSED STEP: %s at %s" % (step, time.asctime())) check_logs(step) else: print(">>>> FAILED STEP: %s at %s" % (step, time.asctime())) passed = False failed.append(step) results.add(step, '<span class="failed-text">FAILED</span>', time.time() - t1) except Exception as msg: passed = False failed.append(step) print(">>>> FAILED STEP: %s at %s (%s)" % (step, time.asctime(), msg)) traceback.print_exc(file=sys.stdout) results.add(step, '<span class="failed-text">FAILED</span>', time.time() - t1) check_logs(step) if not passed: print("FAILED %u tests: %s" % (len(failed), failed)) util.pexpect_close_all() write_fullresults() return passed if __name__ == "__main__": ############## main program ############# os.environ['PYTHONUNBUFFERED'] = '1' os.putenv('TMPDIR', util.reltopdir('tmp')) parser = optparse.OptionParser("autotest") parser.add_option("--skip", type='string', default='', help='list of steps to skip (comma separated)') parser.add_option("--list", action='store_true', default=False, help='list the available steps') parser.add_option("--viewerip", default=None, help='IP address to send MAVLink and fg packets to') parser.add_option("--map", action='store_true', default=False, help='show map') parser.add_option("--experimental", default=False, action='store_true', help='enable experimental tests') parser.add_option("--timeout", default=3000, type='int', help='maximum runtime in seconds') parser.add_option("--speedup", default=None, type='int', help='speedup to run the simulations at') parser.add_option("--valgrind", default=False, action='store_true', help='run ArduPilot binaries under valgrind') parser.add_option("--gdb", default=False, action='store_true', help='run ArduPilot binaries under gdb') parser.add_option("--debug", default=False, action='store_true', help='make built binaries debug binaries') parser.add_option("-j", default=None, type='int', help='build CPUs') parser.add_option("--frame", type='string', default=None, help='specify frame type') parser.add_option("--gdbserver", default=False, action='store_true', help='run ArduPilot binaries under gdbserver') parser.add_option("--no-clean", default=False, action='store_true', help='do not clean before building', dest="no_clean") parser.add_option("--no-configure", default=False, action='store_true', help='do not configure before building', dest="no_configure") opts, args = parser.parse_args() steps = [ 'prerequisites', 'build.All', 'build.Binaries', # 'build.DevRelease', 'build.Examples', 'build.Parameters', 'build.ArduPlane', 'defaults.ArduPlane', 'fly.ArduPlane', 'fly.QuadPlane', 'build.APMrover2', 'defaults.APMrover2', 'drive.APMrover2', 'build.ArduCopter', 'defaults.ArduCopter', 'fly.ArduCopter', 'build.Helicopter', 'fly.CopterAVC', 'build.AntennaTracker', 'build.ArduSub', 'defaults.ArduSub', 'dive.ArduSub', 'convertgpx', ] skipsteps = opts.skip.split(',') # ensure we catch timeouts signal.signal(signal.SIGALRM, alarm_handler) signal.alarm(opts.timeout) if opts.list: for step in steps: print(step) sys.exit(0) util.mkdir_p(buildlogs_dirpath()) lckfile = buildlogs_path('autotest.lck') print("lckfile=%s" % repr(lckfile)) lck = util.lock_file(lckfile) if lck is None: print("autotest is locked - exiting. lckfile=(%s)" % (lckfile,)) sys.exit(0) atexit.register(util.pexpect_close_all) if len(args) > 0: # allow a wildcard list of steps matched = [] for a in args: matches = [step for step in steps if fnmatch.fnmatch(step.lower(), a.lower())] if not len(matches): print("No steps matched {}".format(a)) sys.exit(1) matched.extend(matches) steps = matched # skip steps according to --skip option: steps_to_run = [ s for s in steps if should_run_step(s) ] results = TestResults() try: if not run_tests(steps_to_run): sys.exit(1) except KeyboardInterrupt: util.pexpect_close_all() sys.exit(1) except Exception: # make sure we kill off any children util.pexpect_close_all() raise
gpl-3.0
-2,348,797,625,056,130,600
31.414384
148
0.609245
false
3.532375
true
false
false
walchko/pygecko
retired/find_geckocore.py
1
1087
# #!/usr/bin/env python3 # # -*- coding: utf-8 -*- # ############################################## # # The MIT License (MIT) # # Copyright (c) 2018 Kevin Walchko # # see LICENSE for full details # ############################################## # # import time # import argparse # import os # from pygecko.transport.beacon import BeaconFinder # # # def handleArgs(): # parser = argparse.ArgumentParser(description='Use multicast to find a geckocore node on the network') # parser.add_argument('-k', '--key', help='key, default is hostname', default=None) # args = vars(parser.parse_args()) # return args # # # if __name__ == "__main__": # args = handleArgs() # key = args['key'] # if key is None: # key = os.uname().nodename.split('.')[0].lower() # finder = BeaconFinder(key) # resp = finder.search(0,"0") # if resp: # print("[GeckoCore]===========================") # print(" in: {}".format(resp[0])) # print(" out: {}".format(resp[1])) # else: # print("*** No GeckoCore found on this network ***")
mit
-3,143,877,320,339,952,600
31.939394
107
0.516099
false
3.439873
false
false
false
yoshrote/valid_model
valid_model/descriptors.py
1
7772
from datetime import datetime, timedelta import six from .base import Generic, Object from .exc import ValidationError from .utils import is_descriptor class SimpleType(Generic): """This descriptor will not attempt to coerce the value on __set__.""" _type_klass = None _type_label = None def __set__(self, instance, value): if value is not None and not isinstance(value, self._type_klass): raise ValidationError( "{!r} is not {}".format(value, self._type_label), self.name ) return Generic.__set__(self, instance, value) class EmbeddedObject(Generic): def __init__(self, class_obj): self.class_obj = class_obj def validator(obj): return isinstance(obj, class_obj) Generic.__init__( self, default=class_obj, validator=validator ) def __set__(self, instance, value): try: if isinstance(value, dict): value = self.class_obj(**value) return Generic.__set__(self, instance, value) except ValidationError as ex: raise ValidationError( ex.msg, '{}.{}'.format(self.name, ex.field) if ex.field else self.name ) class String(Generic): """ This descriptor attempts to set a unicode string value. If the value is type(str) it will be decoded using utf-8. """ def __set__(self, instance, value): if value is None or isinstance(value, six.text_type): pass elif isinstance(value, six.binary_type): value = value.decode('utf-8') else: raise ValidationError( "{!r} is not a string".format(value), self.name ) return Generic.__set__(self, instance, value) class _Number(Generic): """This descriptor attempts to converts any a value to a number.""" _number_type = None _number_label = None def __set__(self, instance, value): if value is not None: number_like = isinstance(value, (six.integer_types, float)) is_bool = isinstance(value, bool) if not number_like or is_bool: raise ValidationError( "{!r} is not {}".format(value, self._number_label), self.name ) else: value = int(value) return Generic.__set__(self, instance, value) class Integer(_Number): """This descriptor attempts to coerce a number to an integer.""" _number_type = int _number_label = "an int" class Float(_Number): """This descriptor attempts to coerce a number to a float.""" _number_type = float _number_label = "a float" class Bool(Generic): """This descriptor attempts to converts any a value to a boolean.""" def __set__(self, instance, value): if value is not None: if value in (0, 1) or isinstance(value, bool): value = bool(value) else: raise ValidationError( "{!r} is not a bool".format(value), self.name ) return Generic.__set__(self, instance, value) class DateTime(SimpleType): """This descriptor attempts to set a datetime value.""" _type_klass = datetime _type_label = "a datetime" class TimeDelta(SimpleType): """This descriptor attempts to set a timedalta value.""" _type_klass = timedelta _type_label = "a timedelta" NO_DEFAULT = object() class _Collection(Generic): _collection_type = object _collection_label = None def __init__(self, default=NO_DEFAULT, value=None, validator=None, mutator=None): if default is NO_DEFAULT: default = self._collection_type Generic.__init__( self, default=default, validator=validator, mutator=mutator, nullable=False ) if value is not None and not isinstance(value, Generic): raise TypeError('value must be None or an instance of Generic') self.value = value @staticmethod def iterate(collection): return iter(collection) def recursive_validation(self, element): """Validate element of collection against `self.value`.""" dummy = Object() if self.value is not None: try: element = self.value.__set__(dummy, element) except ValidationError as ex: raise ValidationError( ex.msg, '{}.{}'.format(self.name, ex.field) if ex.field else self.name ) return element def add_to_collection(self, collection, element): raise NotImplementedError("_add_to_collection") def __set__(self, instance, value): if value is None: value = self._collection_type() elif not isinstance(value, self._collection_type): raise ValidationError( "{!r} is not {}".format(value, self._collection_label), self.name ) new_value = self._collection_type() iterable = self.iterate(value) for element in iterable: element = self.recursive_validation(element) self.add_to_collection(new_value, element) value = new_value return Generic.__set__(self, instance, value) class List(_Collection): _collection_type = list _collection_label = "a list" def add_to_collection(self, collection, element): collection.append(element) return collection class Set(_Collection): _collection_type = set _collection_label = "a set" def add_to_collection(self, collection, element): collection.add(element) return collection class Dict(_Collection): _collection_type = dict _collection_label = "a dict" def __init__(self, default=dict, key=None, value=None, validator=None, mutator=None): _Collection.__init__( self, default=default, value=value, validator=validator, mutator=mutator ) if key is not None and not isinstance(key, Generic): raise TypeError('key must be None or an instance of Generic') self.key = key @staticmethod def iterate(collection): return six.iteritems(collection) def recursive_validation(self, element): """Validate element of collection against `self.value`.""" dummy = Object() key, value = element if self.key is not None: try: key = self.key.__set__(dummy, key) except ValidationError as ex: raise ValidationError( ex.msg, "{} key {}".format(self.name, key) ) if self.value is not None: try: value = self.value.__set__(dummy, value) except ValidationError as ex: raise ValidationError( ex.msg, "{}['{}']".format(self.name, key) ) return key, value def add_to_collection(self, collection, element): key, value = element collection[key] = value return collection def descriptors(): """Generate list of descriptor class names.""" return [ name for name, value in six.iteritems(globals()) if is_descriptor(value) and issubclass(value, Generic) ] def descriptor_classes(): """Generate list of descriptor classes.""" return [ value for value in six.itervalues(globals()) if is_descriptor(value) and issubclass(value, Generic) ] __all__ = ['descriptor_classes'] + descriptors()
mit
7,817,542,412,266,166,000
28.439394
89
0.573726
false
4.492486
false
false
false
walterbender/story
collabwrapper.py
1
33058
# Copyright (C) 2015 Walter Bender # Copyright (C) 2015 Sam Parkinson # # 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 library; if not, write to the Free Software # Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA ''' The wrapper module provides an abstraction over the Sugar collaboration system. Using CollabWrapper ------------------- 1. Add `get_data` and `set_data` methods to the activity class:: def get_data(self): # return plain python objects - things that can be encoded # using the json module return dict( text=self._entry.get_text() ) def set_data(self, data): # data will be the same object returned by get_data self._entry.set_text(data.get('text')) 2. Make a CollabWrapper instance:: def __init__(self, handle): sugar3.activity.activity.Activity.__init__(self, handle) self._collab = CollabWrapper(self) self._collab.connect('message', self.__message_cb) # setup your activity here self._collab.setup() 3. Post any changes of shared state to the CollabWrapper. The changes will be sent to other buddies if any are connected, for example:: def __entry_changed_cb(self, *args): self._collab.post(dict( action='entry_changed', new_text=self._entry.get_text() )) 4. Handle incoming messages, for example:: def __message_cb(self, collab, buddy, msg): action = msg.get('action') if action == 'entry_changed': self._entry.set_text(msg.get('new_text')) ''' import os import json import socket from gettext import gettext as _ import gi gi.require_version('TelepathyGLib', '0.12') from gi.repository import GObject from gi.repository import Gio from gi.repository import GLib from gi.repository import TelepathyGLib import dbus from dbus import PROPERTIES_IFACE CHANNEL_INTERFACE = TelepathyGLib.IFACE_CHANNEL CHANNEL_INTERFACE_GROUP = TelepathyGLib.IFACE_CHANNEL_INTERFACE_GROUP CHANNEL_TYPE_TEXT = TelepathyGLib.IFACE_CHANNEL_TYPE_TEXT CHANNEL_TYPE_FILE_TRANSFER = TelepathyGLib.IFACE_CHANNEL_TYPE_FILE_TRANSFER CONN_INTERFACE_ALIASING = TelepathyGLib.IFACE_CONNECTION_INTERFACE_ALIASING CONN_INTERFACE = TelepathyGLib.IFACE_CONNECTION CHANNEL = TelepathyGLib.IFACE_CHANNEL CLIENT = TelepathyGLib.IFACE_CLIENT CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES = \ TelepathyGLib.ChannelGroupFlags.CHANNEL_SPECIFIC_HANDLES CONNECTION_HANDLE_TYPE_CONTACT = TelepathyGLib.HandleType.CONTACT CHANNEL_TEXT_MESSAGE_TYPE_NORMAL = TelepathyGLib.ChannelTextMessageType.NORMAL SOCKET_ADDRESS_TYPE_UNIX = TelepathyGLib.SocketAddressType.UNIX SOCKET_ACCESS_CONTROL_LOCALHOST = TelepathyGLib.SocketAccessControl.LOCALHOST from sugar3.presence import presenceservice from sugar3.activity.activity import SCOPE_PRIVATE from sugar3.graphics.alert import NotifyAlert import logging _logger = logging.getLogger('CollabWrapper') ACTION_INIT_REQUEST = '!!ACTION_INIT_REQUEST' ACTION_INIT_RESPONSE = '!!ACTION_INIT_RESPONSE' ACTIVITY_FT_MIME = 'x-sugar/from-activity' class CollabWrapper(GObject.GObject): ''' The wrapper provides a high level abstraction over the collaboration system. The wrapper deals with setting up the channels, encoding and decoding messages, initialization and alerting the caller to the status. An activity instance is initially private, but may be shared. Once shared, an instance will remain shared for as long as the activity runs. On stop, the journal will preserve the instance as shared, and on resume the instance will be shared again. When the caller shares an activity instance, they are the leader, and other buddies may join. The instance is now a shared activity. When the caller joins a shared activity, the leader will call `get_data`, and the caller's `set_data` will be called with the result. The `joined` signal is emitted when the caller joins a shared activity. One or more `buddy_joined` signals will be emitted before this signal. The signal is not emitted to the caller who first shared the activity. There are no arguments. The `buddy_joined` signal is emitted when another buddy joins the shared activity. At least one will be emitted before the `joined` signal. The caller will never be mentioned, but is assumed to be part of the set. The signal passes a :class:`sugar3.presence.buddy.Buddy` as the only argument. The `buddy_left` signal is emitted when another user leaves the shared activity. The signal is not emitted during quit. The signal passes a :class:`sugar3.presence.buddy.Buddy` as the only argument. Any buddy may call `post` to send a message to all buddies. Each buddy will receive a `message` signal. The `message` signal is emitted when a `post` is received from any buddy. The signal has two arguments. The first is a :class:`sugar3.presence.buddy.Buddy`. The second is the message. Any buddy may call `send_file_memory` or `send_file_file` to transfer a file to all buddies. A description is to be given. Each buddy will receive an `incoming_file` signal. The `incoming_file` signal is emitted when a file transfer is received. The signal has two arguments. The first is a :class:`IncomingFileTransfer`. The second is the description. ''' message = GObject.Signal('message', arg_types=[object, object]) joined = GObject.Signal('joined') buddy_joined = GObject.Signal('buddy_joined', arg_types=[object]) buddy_left = GObject.Signal('buddy_left', arg_types=[object]) incoming_file = GObject.Signal('incoming_file', arg_types=[object, object]) def __init__(self, activity): _logger.debug('__init__') GObject.GObject.__init__(self) self.activity = activity self.shared_activity = activity.shared_activity self._leader = False self._init_waiting = False self._text_channel = None self._owner = presenceservice.get_instance().get_owner() def setup(self): ''' Setup must be called so that the activity can join or share if appropriate. .. note:: As soon as setup is called, any signal, `get_data` or `set_data` call may occur. This means that the activity must have set up enough so these functions can work. For example, call setup at the end of the activity `__init__` function. ''' _logger.debug('setup') # Some glue to know if we are launching, joining, or resuming # a shared activity. if self.shared_activity: # We're joining the activity. self.activity.connect("joined", self.__joined_cb) if self.activity.get_shared(): _logger.debug('calling _joined_cb') self.__joined_cb(self) else: _logger.debug('Joining activity...') self._alert(_('Joining activity...'), _('Please wait for the connection...')) else: self._leader = True if not self.activity.metadata or self.activity.metadata.get( 'share-scope', SCOPE_PRIVATE) == \ SCOPE_PRIVATE: # We are creating a new activity instance. _logger.debug('Off-line') else: # We are sharing an old activity instance. _logger.debug('On-line') self._alert(_('Resuming shared activity...'), _('Please wait for the connection...')) self.activity.connect('shared', self.__shared_cb) def _alert(self, title, msg=None): a = NotifyAlert() a.props.title = title a.props.msg = msg self.activity.add_alert(a) a.connect('response', lambda a, r: self.activity.remove_alert(a)) a.show() def __shared_cb(self, sender): ''' Callback for when activity is shared. ''' _logger.debug('__shared_cb') # FIXME: may be called twice, but we should only act once self.shared_activity = self.activity.shared_activity self._setup_text_channel() self._listen_for_channels() def __joined_cb(self, sender): '''Callback for when an activity is joined.''' _logger.debug('__joined_cb') self.shared_activity = self.activity.shared_activity if not self.shared_activity: return self._setup_text_channel() self._listen_for_channels() self._init_waiting = True self.post({'action': ACTION_INIT_REQUEST}) for buddy in self.shared_activity.get_joined_buddies(): self.buddy_joined.emit(buddy) self.joined.emit() def _setup_text_channel(self): ''' Set up a text channel to use for collaboration. ''' _logger.debug('_setup_text_channel') self._text_channel = _TextChannelWrapper( self.shared_activity.telepathy_text_chan, self.shared_activity.telepathy_conn) # Tell the text channel what callback to use for incoming # text messages. self._text_channel.set_received_callback(self.__received_cb) # Tell the text channel what callbacks to use when buddies # come and go. self.shared_activity.connect('buddy-joined', self.__buddy_joined_cb) self.shared_activity.connect('buddy-left', self.__buddy_left_cb) def _listen_for_channels(self): _logger.debug('_listen_for_channels') conn = self.shared_activity.telepathy_conn conn.connect_to_signal('NewChannels', self.__new_channels_cb) def __new_channels_cb(self, channels): _logger.debug('__new_channels_cb') conn = self.shared_activity.telepathy_conn for path, props in channels: if props[CHANNEL + '.Requested']: continue # This channel was requested by me channel_type = props[CHANNEL + '.ChannelType'] if channel_type == CHANNEL_TYPE_FILE_TRANSFER: self._handle_ft_channel(conn, path, props) def _handle_ft_channel(self, conn, path, props): _logger.debug('_handle_ft_channel') ft = IncomingFileTransfer(conn, path, props) if ft.description == ACTION_INIT_RESPONSE: ft.connect('ready', self.__ready_cb) ft.accept_to_memory() else: desc = json.loads(ft.description) self.incoming_file.emit(ft, desc) def __ready_cb(self, ft, stream): _logger.debug('__ready_cb') if self._init_waiting: stream.close(None) # FIXME: The data prop seems to just be the raw pointer gbytes = stream.steal_as_bytes() data = gbytes.get_data() _logger.debug('Got init data from buddy: %r', data) data = json.loads(data) self.activity.set_data(data) self._init_waiting = False def __received_cb(self, buddy, msg): '''Process a message when it is received.''' _logger.debug('__received_cb') action = msg.get('action') if action == ACTION_INIT_REQUEST: if self._leader: data = self.activity.get_data() if data is not None: data = json.dumps(data) OutgoingBlobTransfer( buddy, self.shared_activity.telepathy_conn, data, self.get_client_name(), ACTION_INIT_RESPONSE, ACTIVITY_FT_MIME) return if buddy: nick = buddy.props.nick else: nick = '???' _logger.debug('Received message from %s: %r', nick, msg) self.message.emit(buddy, msg) def send_file_memory(self, buddy, data, description): ''' Send a one to one file transfer from memory to a buddy. The buddy will get the file transfer and description through the `incoming_transfer` signal. Args: buddy (sugar3.presence.buddy.Buddy), buddy to send to. data (str), the data to send. description (object), a json encodable description for the transfer. This will be given to the `incoming_transfer` signal at the buddy. ''' OutgoingBlobTransfer( buddy, self.shared_activity.telepathy_conn, data, self.get_client_name(), json.dumps(description), ACTIVITY_FT_MIME) def send_file_file(self, buddy, path, description): ''' Send a one to one file transfer from a filesystem path to a given buddy. The buddy will get the file transfer and description through the `incoming_transfer` signal. Args: buddy (sugar3.presence.buddy.Buddy), buddy to send to. path (str), path of the file containing the data to send. description (object), a json encodable description for the transfer. This will be given to the `incoming_transfer` signal at the buddy. ''' OutgoingFileTransfer( buddy, self.shared_activity.telepathy_conn, path, self.get_client_name(), json.dumps(description), ACTIVITY_FT_MIME) def post(self, msg): ''' Send a message to all buddies. If the activity is not shared, no message is sent. Args: msg (object): json encodable object to send, eg. :class:`dict` or :class:`str`. ''' if self._text_channel is not None: self._text_channel.post(msg) def __buddy_joined_cb(self, sender, buddy): '''A buddy joined.''' self.buddy_joined.emit(buddy) def __buddy_left_cb(self, sender, buddy): '''A buddy left.''' self.buddy_left.emit(buddy) def get_client_name(self): ''' Get the name of the activity's telepathy client. Returns: str, telepathy client name ''' return CLIENT + '.' + self.activity.get_bundle_id() @GObject.property def leader(self): ''' Boolean of if this client is the leader in this activity. The way the leader is decided may change, however there should only ever be one leader for an activity. ''' return self._leader @GObject.property def owner(self): ''' Ourselves, :class:`sugar3.presence.buddy.Owner` ''' return self._owner FT_STATE_NONE = 0 FT_STATE_PENDING = 1 FT_STATE_ACCEPTED = 2 FT_STATE_OPEN = 3 FT_STATE_COMPLETED = 4 FT_STATE_CANCELLED = 5 FT_REASON_NONE = 0 FT_REASON_REQUESTED = 1 FT_REASON_LOCAL_STOPPED = 2 FT_REASON_REMOTE_STOPPED = 3 FT_REASON_LOCAL_ERROR = 4 FT_REASON_LOCAL_ERROR = 5 FT_REASON_REMOTE_ERROR = 6 class _BaseFileTransfer(GObject.GObject): ''' The base file transfer should not be used directly. It is used as a base class for the incoming and outgoing file transfers. Props: filename (str), metadata provided by the buddy file_size (str), size of the file being sent/received, in bytes description (str), metadata provided by the buddy mime_type (str), metadata provided by the buddy buddy (:class:`sugar3.presence.buddy.Buddy`), other party in the transfer reason_last_change (FT_REASON_*), reason for the last state change GObject Props: state (FT_STATE_*), current state of the transfer transferred_bytes (int), number of bytes transferred so far ''' def __init__(self): GObject.GObject.__init__(self) self._state = FT_STATE_NONE self._transferred_bytes = 0 self.channel = None self.buddy = None self.filename = None self.file_size = None self.description = None self.mime_type = None self.reason_last_change = FT_REASON_NONE def set_channel(self, channel): ''' Setup the file transfer to use a given telepathy channel. This should only be used by direct subclasses of the base file transfer. ''' self.channel = channel self.channel[CHANNEL_TYPE_FILE_TRANSFER].connect_to_signal( 'FileTransferStateChanged', self.__state_changed_cb) self.channel[CHANNEL_TYPE_FILE_TRANSFER].connect_to_signal( 'TransferredBytesChanged', self.__transferred_bytes_changed_cb) self.channel[CHANNEL_TYPE_FILE_TRANSFER].connect_to_signal( 'InitialOffsetDefined', self.__initial_offset_defined_cb) channel_properties = self.channel[PROPERTIES_IFACE] props = channel_properties.GetAll(CHANNEL_TYPE_FILE_TRANSFER) self._state = props['State'] self.filename = props['Filename'] self.file_size = props['Size'] self.description = props['Description'] self.mime_type = props['ContentType'] def __transferred_bytes_changed_cb(self, transferred_bytes): _logger.debug('__transferred_bytes_changed_cb %r', transferred_bytes) self.props.transferred_bytes = transferred_bytes def _set_transferred_bytes(self, transferred_bytes): self._transferred_bytes = transferred_bytes def _get_transferred_bytes(self): return self._transferred_bytes transferred_bytes = GObject.property(type=int, default=0, getter=_get_transferred_bytes, setter=_set_transferred_bytes) def __initial_offset_defined_cb(self, offset): _logger.debug('__initial_offset_defined_cb %r', offset) self.initial_offset = offset def __state_changed_cb(self, state, reason): _logger.debug('__state_changed_cb %r %r', state, reason) self.reason_last_change = reason self.props.state = state def _set_state(self, state): self._state = state def _get_state(self): return self._state state = GObject.property(type=int, getter=_get_state, setter=_set_state) def cancel(self): ''' Request that telepathy close the file transfer channel Spec: http://telepathy.freedesktop.org/spec/Channel.html#Method:Close ''' self.channel[CHANNEL].Close() class IncomingFileTransfer(_BaseFileTransfer): ''' An incoming file transfer from another buddy. You need to first accept the transfer (either to memory or to a file). Then you need to listen to the state and wait until the transfer is completed. Then you can read the file that it was saved to, or access the :class:`Gio.MemoryOutputStream` from the `output` property. The `output` property is different depending on how the file was accepted. If the file was accepted to a file on the file system, it is a string representing the path to the file. If the file was accepted to memory, it is a :class:`Gio.MemoryOutputStream`. ''' ready = GObject.Signal('ready', arg_types=[object]) def __init__(self, connection, object_path, props): _BaseFileTransfer.__init__(self) channel = {} proxy = dbus.Bus().get_object(connection.bus_name, object_path) channel[PROPERTIES_IFACE] = dbus.Interface(proxy, PROPERTIES_IFACE) channel[CHANNEL] = dbus.Interface(proxy, CHANNEL) channel[CHANNEL_TYPE_FILE_TRANSFER] = dbus.Interface( proxy, CHANNEL_TYPE_FILE_TRANSFER) self.set_channel(channel) self.connect('notify::state', self.__notify_state_cb) self._destination_path = None self._output_stream = None self._socket_address = None self._socket = None self._splicer = None def accept_to_file(self, destination_path): ''' Accept the file transfer and write it to a new file. The file must already exist. Args: destination_path (str): the path where a new file will be created and saved to ''' if os.path.exists(destination_path): raise ValueError('Destination path already exists: %r' % destination_path) self._destination_path = destination_path self._accept() def accept_to_memory(self): ''' Accept the file transfer. Once the state is FT_STATE_OPEN, a :class:`Gio.MemoryOutputStream` accessible via the output prop. ''' self._destination_path = None self._accept() def _accept(self): channel_ft = self.channel[CHANNEL_TYPE_FILE_TRANSFER] self._socket_address = channel_ft.AcceptFile( SOCKET_ADDRESS_TYPE_UNIX, SOCKET_ACCESS_CONTROL_LOCALHOST, '', 0, byte_arrays=True) def __notify_state_cb(self, file_transfer, pspec): _logger.debug('__notify_state_cb %r', self.props.state) if self.props.state == FT_STATE_OPEN: # Need to hold a reference to the socket so that python doesn't # close the fd when it goes out of scope self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self._socket.connect(self._socket_address) input_stream = Gio.UnixInputStream.new(self._socket.fileno(), True) if self._destination_path is not None: destination_file = Gio.File.new_for_path( self._destination_path) if self.initial_offset == 0: self._output_stream = destination_file.create( Gio.FileCreateFlags.PRIVATE, None) else: self._output_stream = destination_file.append_to() else: if hasattr(Gio.MemoryOutputStream, 'new_resizable'): self._output_stream = \ Gio.MemoryOutputStream.new_resizable() else: self._output_stream = Gio.MemoryOutputStream() self._output_stream.splice_async( input_stream, Gio.OutputStreamSpliceFlags.CLOSE_SOURCE | Gio.OutputStreamSpliceFlags.CLOSE_TARGET, GLib.PRIORITY_LOW, None, self.__splice_done_cb, None) def __splice_done_cb(self, output_stream, res, user): _logger.debug('__splice_done_cb') self.ready.emit(self._destination_path or self._output_stream) @GObject.Property def output(self): return self._destination_path or self._output_stream class _BaseOutgoingTransfer(_BaseFileTransfer): ''' This class provides the base of an outgoing file transfer. You can override the `_get_input_stream` method to return any type of Gio input stream. This will then be used to provide the file if requested by the application. You also need to call `_create_channel` with the length of the file in bytes during your `__init__`. Args: buddy (sugar3.presence.buddy.Buddy), who to send the transfer to conn (telepathy.client.conn.Connection), telepathy connection to use to send the transfer. Eg. `shared_activity.telepathy_conn` filename (str), metadata sent to the receiver description (str), metadata sent to the receiver mime (str), metadata sent to the receiver ''' def __init__(self, buddy, conn, filename, description, mime): _BaseFileTransfer.__init__(self) self.connect('notify::state', self.__notify_state_cb) self._socket_address = None self._socket = None self._splicer = None self._conn = conn self._filename = filename self._description = description self._mime = mime self.buddy = buddy def _create_channel(self, file_size): object_path, properties_ = self._conn.CreateChannel(dbus.Dictionary({ CHANNEL + '.ChannelType': CHANNEL_TYPE_FILE_TRANSFER, CHANNEL + '.TargetHandleType': CONNECTION_HANDLE_TYPE_CONTACT, CHANNEL + '.TargetHandle': self.buddy.contact_handle, CHANNEL_TYPE_FILE_TRANSFER + '.Filename': self._filename, CHANNEL_TYPE_FILE_TRANSFER + '.Description': self._description, CHANNEL_TYPE_FILE_TRANSFER + '.Size': file_size, CHANNEL_TYPE_FILE_TRANSFER + '.ContentType': self._mime, CHANNEL_TYPE_FILE_TRANSFER + '.InitialOffset': 0}, signature='sv')) channel = {} proxy = dbus.Bus().get_object(self._conn.bus_name, object_path) channel[PROPERTIES_IFACE] = dbus.Interface(proxy, PROPERTIES_IFACE) channel[CHANNEL] = dbus.Interface(proxy, CHANNEL) channel[CHANNEL_TYPE_FILE_TRANSFER] = dbus.Interface( proxy, CHANNEL_TYPE_FILE_TRANSFER) self.set_channel(channel) channel_file_transfer = self.channel[CHANNEL_TYPE_FILE_TRANSFER] self._socket_address = channel_file_transfer.ProvideFile( SOCKET_ADDRESS_TYPE_UNIX, SOCKET_ACCESS_CONTROL_LOCALHOST, '', byte_arrays=True) def _get_input_stream(self): raise NotImplementedError() def __notify_state_cb(self, file_transfer, pspec): if self.props.state == FT_STATE_OPEN: # Need to hold a reference to the socket so that python doesn't # closes the fd when it goes out of scope self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self._socket.connect(self._socket_address) output_stream = Gio.UnixOutputStream.new( self._socket.fileno(), True) input_stream = self._get_input_stream() output_stream.splice_async( input_stream, Gio.OutputStreamSpliceFlags.CLOSE_SOURCE | Gio.OutputStreamSpliceFlags.CLOSE_TARGET, GLib.PRIORITY_LOW, None, None, None) class OutgoingFileTransfer(_BaseOutgoingTransfer): ''' An outgoing file transfer to send from a file (on the computer's file system). Note that the `path` argument is the path for the file that will be sent, whereas the `filename` argument is only for metadata. Args: path (str), path of the file to send ''' def __init__(self, buddy, conn, path, filename, description, mime): _BaseOutgoingTransfer.__init__( self, buddy, conn, filename, description, mime) self._path = path file_size = os.stat(path).st_size self._create_channel(file_size) def _get_input_stream(self): return Gio.File.new_for_path(self._path).read(None) class OutgoingBlobTransfer(_BaseOutgoingTransfer): ''' An outgoing file transfer to send from a string in memory. Args: blob (str), data to send ''' def __init__(self, buddy, conn, blob, filename, description, mime): _BaseOutgoingTransfer.__init__( self, buddy, conn, filename, description, mime) self._blob = blob self._create_channel(len(self._blob)) def _get_input_stream(self): return Gio.MemoryInputStream.new_from_data(self._blob, None) class _TextChannelWrapper(object): '''Wrapper for a telepathy Text Channel''' def __init__(self, text_chan, conn): '''Connect to the text channel''' self._activity_cb = None self._activity_close_cb = None self._text_chan = text_chan self._conn = conn self._signal_matches = [] m = self._text_chan[CHANNEL_INTERFACE].connect_to_signal( 'Closed', self._closed_cb) self._signal_matches.append(m) def post(self, msg): if msg is not None: _logger.debug('post') self._send(json.dumps(msg)) def _send(self, text): '''Send text over the Telepathy text channel.''' _logger.debug('sending %s' % text) if self._text_chan is not None: self._text_chan[CHANNEL_TYPE_TEXT].Send( CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, text) def close(self): '''Close the text channel.''' _logger.debug('Closing text channel') try: self._text_chan[CHANNEL_INTERFACE].Close() except Exception: _logger.debug('Channel disappeared!') self._closed_cb() def _closed_cb(self): '''Clean up text channel.''' for match in self._signal_matches: match.remove() self._signal_matches = [] self._text_chan = None if self._activity_close_cb is not None: self._activity_close_cb() def set_received_callback(self, callback): '''Connect the function callback to the signal. callback -- callback function taking buddy and text args ''' if self._text_chan is None: return self._activity_cb = callback m = self._text_chan[CHANNEL_TYPE_TEXT].connect_to_signal( 'Received', self._received_cb) self._signal_matches.append(m) def handle_pending_messages(self): '''Get pending messages and show them as received.''' for identity, timestamp, sender, type_, flags, text in \ self._text_chan[ CHANNEL_TYPE_TEXT].ListPendingMessages(False): self._received_cb(identity, timestamp, sender, type_, flags, text) def _received_cb(self, identity, timestamp, sender, type_, flags, text): '''Handle received text from the text channel. Converts sender to a Buddy. Calls self._activity_cb which is a callback to the activity. ''' _logger.debug('received_cb %r %s' % (type_, text)) if type_ != 0: # Exclude any auxiliary messages return msg = json.loads(text) if self._activity_cb: try: self._text_chan[CHANNEL_INTERFACE_GROUP] except Exception: # One to one XMPP chat nick = self._conn[ CONN_INTERFACE_ALIASING].RequestAliases([sender])[0] buddy = {'nick': nick, 'color': '#000000,#808080'} _logger.debug('exception: received from sender %r buddy %r' % (sender, buddy)) else: # XXX: cache these buddy = self._get_buddy(sender) _logger.debug('Else: received from sender %r buddy %r' % (sender, buddy)) self._activity_cb(buddy, msg) self._text_chan[ CHANNEL_TYPE_TEXT].AcknowledgePendingMessages([identity]) else: _logger.debug('Throwing received message on the floor' ' since there is no callback connected. See' ' set_received_callback') def set_closed_callback(self, callback): '''Connect a callback for when the text channel is closed. callback -- callback function taking no args ''' _logger.debug('set closed callback') self._activity_close_cb = callback def _get_buddy(self, cs_handle): '''Get a Buddy from a (possibly channel-specific) handle.''' # XXX This will be made redundant once Presence Service # provides buddy resolution # Get the Presence Service pservice = presenceservice.get_instance() # Get the Telepathy Connection tp_name, tp_path = pservice.get_preferred_connection() obj = dbus.Bus().get_object(tp_name, tp_path) conn = dbus.Interface(obj, CONN_INTERFACE) group = self._text_chan[CHANNEL_INTERFACE_GROUP] my_csh = group.GetSelfHandle() if my_csh == cs_handle: handle = conn.GetSelfHandle() elif group.GetGroupFlags() & \ CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES: handle = group.GetHandleOwners([cs_handle])[0] else: handle = cs_handle # XXX: deal with failure to get the handle owner assert handle != 0 return pservice.get_buddy_by_telepathy_handle( tp_name, tp_path, handle)
gpl-3.0
3,951,370,830,328,633,000
36.438279
79
0.616038
false
4.068176
false
false
false
amsimoes/bat-country
batcountry/batcountry.py
1
6348
# import the necessary packages from __future__ import print_function from google.protobuf import text_format from cStringIO import StringIO from PIL import Image import scipy.ndimage as nd import numpy as np import caffe import os class BatCountry: def __init__(self, base_path, deploy_path, model_path, patch_model="./tmp.prototxt", mean=(104.0, 116.0, 122.0), channels=(2, 1, 0)): # if the deploy path is None, set the default if deploy_path is None: deploy_path = base_path + "/deploy.prototxt" # if the model path is None, set it to the default GoogleLeNet model if model_path is None: model_path = base_path + "/imagenet.caffemodel" # check to see if the model should be patched to compute gradients if patch_model: model = caffe.io.caffe_pb2.NetParameter() text_format.Merge(open(deploy_path).read(), model) model.force_backward = True f = open(patch_model, "w") f.write(str(model)) f.close() # load the network and store the patched model path self.net = caffe.Classifier(patch_model, model_path, mean=np.float32(mean), channel_swap=channels) self.patch_model = patch_model def dream(self, image, iter_n, octave_n, octave_scale=None, end="inception_4c/output", clip=True, step_fn=None, objective_fn=None, preprocess_fn=None, deprocess_fn=None, verbose=True, visualize=False, **step_params): if iter_n is None: iter_n = 10 if octave_n is None: octave_n = 4 if octave_scale is None: octave_scale = 1.4 # if a step function has not been supplied, initialize it as the # standard gradient ascent step if step_fn is None: step_fn = BatCountry.gradient_ascent_step # if the objective function has not been supplied, initialize it # as the L2 objective if objective_fn is None: objective_fn = BatCountry.L2_objective # if the preprocess function has not been supplied, initialize it if preprocess_fn is None: preprocess_fn = BatCountry.preprocess # if the deprocess function has not been supplied, initialize it if deprocess_fn is None: deprocess_fn = BatCountry.deprocess # initialize the visualization list visualizations = [] # prepare base image_dims for all octaves octaves = [preprocess_fn(self.net, image)] for i in xrange(octave_n - 1): octaves.append(nd.zoom(octaves[-1], (1, 1.0 / octave_scale, 1.0 / octave_scale), order=1)) # allocate image for network-produced details detail = np.zeros_like(octaves[-1]) src = self.net.blobs["data"] for octave, octave_base in enumerate(octaves[::-1]): h, w = octave_base.shape[-2:] if octave > 0: # upscale details from the previous octave h1, w1 = detail.shape[-2:] detail = nd.zoom(detail, (1, 1.0 * h/ h1, 1.0 * w / w1), order=1) # resize the network's input image size src.reshape(1, 3, h, w) src.data[0] = octave_base + detail for i in xrange(iter_n): step_fn(self.net, end=end, clip=clip, objective_fn=objective_fn, **step_params) # visualization vis = deprocess_fn(self.net, src.data[0]) # adjust image contrast if clipping is disabled if not clip: vis = vis * (255.0 / np.percentile(vis, 99.98)) if verbose: print("octave={}, iter={}, layer={}, image_dim={}".format(octave, i, end, vis.shape)) # check to see if the visualization list should be # updated if visualize: k = "octave_{}-iter_{}-layer_{}".format(octave, i, end.replace("/", "_")) visualizations.append((k, vis)) # extract details produced on the current octave detail = src.data[0] - octave_base # grab the resulting image r = deprocess_fn(self.net, src.data[0]) # check to see if the visualizations should be included if visualize: r = (r, visualizations) return r @staticmethod def gradient_ascent_step(net, step_size=1.5, end="inception_4c/output", jitter=32, clip=True, objective_fn=None, **objective_params): # if the objective function is None, initialize it as # the standard L2 objective if objective_fn is None: objective_fn = BatCountry.L2_objective # input image is stored in Net's 'data' blob src = net.blobs["data"] dst = net.blobs[end] # apply jitter shift ox, oy = np.random.randint(-jitter, jitter + 1, 2) src.data[0] = np.roll(np.roll(src.data[0], ox, -1), oy, -2) net.forward(end=end) objective_fn(dst, **objective_params) net.backward(start=end) g = src.diff[0] # apply normalized ascent step to the input image src.data[:] += step_size / np.abs(g).mean() * g # unshift image src.data[0] = np.roll(np.roll(src.data[0], -ox, -1), -oy, -2) # unshift image if clip: bias = net.transformer.mean["data"] src.data[:] = np.clip(src.data, -bias, 255 - bias) def layers(self): # return the layers of the network return self.net._layer_names def cleanup(self): # remove the patched model from disk os.remove(self.patch_model) def prepare_guide(self, image, end="inception_4c/output", maxW=224, maxH=224, preprocess_fn=None): # if the preprocess function has not been supplied, initialize it if preprocess_fn is None: preprocess_fn = BatCountry.preprocess # grab dimensions of input image (w, h) = image.size # GoogLeNet was trained on images with maximum width and heights # of 224 pixels -- if either dimension is larger than 224 pixels, # then we'll need to do some resizing nW, nH = 244, 244 if w != 244 or h != 244: image = np.float32(image.resize((nW, nH), Image.BILINEAR)) (src, dst) = (self.net.blobs["data"], self.net.blobs[end]) src.reshape(1, 3, nH, nW) src.data[0] = preprocess_fn(self.net, image) self.net.forward(end=end) guide_features = dst.data[0].copy() return guide_features @staticmethod def L2_objective(dst): dst.diff[:] = dst.data @staticmethod def guided_objective(dst, objective_features): x = dst.data[0].copy() y = objective_features ch = x.shape[0] x = x.reshape(ch,-1) y = y.reshape(ch,-1) # compute the matrix of dot-products with guide features A = x.T.dot(y) # select ones that match best dst.diff[0].reshape(ch, -1)[:] = y[:,A.argmax(1)] @staticmethod def preprocess(net, img): return np.float32(np.rollaxis(img, 2)[::-1]) - net.transformer.mean["data"] @staticmethod def deprocess(net, img): return np.dstack((img + net.transformer.mean["data"])[::-1])
mit
-3,119,327,493,951,980,500
28.525581
78
0.675961
false
2.93617
false
false
false
KonradBreitsprecher/espresso
doc/tutorials/09-swimmer_reactions/EXERCISES/reaction.py
1
9461
################################################################################ # # # Copyright (C) 2010,2011,2012,2013,2014,2015,2016 The ESPResSo project # # # # This file is part of ESPResSo. # # # # ESPResSo 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. # # # # ESPResSo 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/>. # # # ################################################################################ # # # Catalytic Reactions: Enhanced Diffusion Tutorial # # # ################################################################################ from __future__ import print_function import numpy as np import os import sys import time from espressomd import assert_features from espressomd.observables import ParticlePositions, ParticleBodyAngularMomentum from espressomd.correlators import Correlator from espressomd.reaction import Reaction ################################################################################ # Read in the active velocity from the command prompt if len(sys.argv) != 2: print("Usage:",sys.argv[0],"<passive/active = 0/1>") exit() active = int(sys.argv[1]) if (active != 0) and (active != 1): print("Usage:",sys.argv[0],"<passive/active = 0/1>") exit() # Set the parameters box_l = 10 radius = 3.0 csmall = 0.1 rate = 1000.0 # Print input parameters print("Box length: {}".format(box_l)) print("Colloid radius: {}".format(radius)) print("Particle concentration: {}".format(csmall)) print("Reaction rate: {}".format(rate)) print("Active or Passive: {}".format(active)) # Create output directory if active == 0: outdir = "./passive-system" else: outdir = "./active-system" try: os.makedirs(outdir) except: print("INFO: Directory \"{}\" exists".format(outdir)) ################################################################################ # Setup system parameters equi_steps = 250 equi_length = 100 prod_steps = 2000 prod_length = 100 dt = 0.01 system = espressomd.System(box_l=[box_l, box_l, box_l]) system.cell_system.skin = 0.1 system.time_step = dt system.min_global_cut = 1.1*radius # Set up the random seeds system.seed = np.random.randint(0,2**31-1) ################################################################################ # Thermostat parameters # Catalyzer is assumed to be larger, thus larger friction frict_trans_colloid = 20.0 frict_rot_colloid = 20.0 # Particles are small and have smaller friction frict_trans_part = 1.0 frict_rot_part = 1.0 # Temperature temp = 1.0 ################################################################################ # Set up the swimmer ## Exercise 1 ## # Determine the initial position of the particle, which # should be in the center of the box. x0pnt = ... y0pnt = ... z0pnt = ... # Note that the swimmer needs to rotate freely cent = len(system.part) system.part.add(id=cent,pos=[x0pnt,y0pnt,z0pnt],type=0,temp=temp, gamma=frict_trans_colloid, gamma_rot=frict_rot_colloid, rotation=[1,1,1]) # Set up the particles ## Exercise 2 ## # Above, we have set the concentration of the particles in the # variable $csmall. The concentration of both species of particles is # equal. Determine *how many* particles of one species there are. # There are two species of equal concentration nB = ... nA = nB print("Number of reactive A particles: {}".format(nB)) print("Number of reactive B particles: {}".format(nA)) for i in range(nA): x = box_l*np.random.random() y = box_l*np.random.random() z = box_l*np.random.random() # Prevent overlapping the colloid while (x-x0pnt)**2 + (y-y0pnt)**2 + (z-z0pnt)**2 < 1.15*radius**2: x = box_l*np.random.random() y = box_l*np.random.random() z = box_l*np.random.random() # reactants and products do not need to rotate system.part.add(pos=[x,y,z],type=1,temp=temp, gamma=frict_trans_part, gamma_rot=frict_rot_part, rotation=[0,0,0]) for i in range(nB): x = box_l*np.random.random() y = box_l*np.random.random() z = box_l*np.random.random() # Prevent overlapping the colloid while (x-x0pnt)**2 + (y-y0pnt)**2 + (z-z0pnt)**2 < 1.15*radius**2: x = box_l*np.random.random() y = box_l*np.random.random() z = box_l*np.random.random() # reactants and products do not need to rotate system.part.add(pos=[x,y,z],type=2,temp=temp, gamma=frict_trans_part, gamma_rot=frict_rot_part, rotation=[0,0,0]) print("box: {}, npart: {}".format(system.box_l,len(system.part))) ################################################################################ # Set up the WCA potential ## Exercise 3 ## # Why are there two different cutoff lengths for the LJ interaction # catalyzer/product and catalyzer/reactant? eps = 5.0 sig = 1.0 shift = 0.25 roff = radius - 0.5*sig # central and A particles cut = 2**(1/6.)*sig system.non_bonded_inter[0,1].lennard_jones.set_params(epsilon=eps, sigma=sig, cutoff=cut, shift=shift, offset=roff) # central and B particles (larger cutoff) cut = 1.5*sig system.non_bonded_inter[0,2].lennard_jones.set_params(epsilon=eps, sigma=sig, cutoff=cut, shift=shift, offset=roff) ################################################################################ # Set up the reaction cat_range = radius + 1.0*sig cat_rate = rate ## Exercise 4 ## # We have read the acticity parameter from the command line into # $active, where 0 means off and 1 means on. When $active = 0 we can # simply go on, but when $active = 1 we have to set up the reaction. # Check the $active parameter and setup a reaction for the catalyzer # of type 0 with the reactants of type 1 and products of type 2. The # reaction range is stored in $cat_range, the reaction rate in # $cat_rate. Use the number-conserving scheme by setting swap on. ... ################################################################################ # Perform warmup cap = 1.0 warm_length = 100 ## Exercise 5 ## # Consult the User Guide for minimize_energy to find out the # difference to warmup with explicit force-capping. system.minimize_energy.init(f_max=cap,max_steps=warm_length,gamma=1.0/20.0,max_displacement=0.05) system.minimize_energy.minimize() ################################################################################ # Enable the thermostat ## Exercise 6 ## # Why do we enable the thermostat only after warmup? system.thermostat.set_langevin(kT=temp, gamma=frict_trans_colloid) ################################################################################ # Perform equilibration # Integrate for k in range(equi_steps): print("Equilibration: {} of {}".format(k,equi_steps)) system.integrator.run(equi_length) ################################################################################ for cnt in range(5): # Set up the MSD calculation tmax = prod_steps*prod_length*dt pos_id = ParticlePositions(ids=[cent]) msd = Correlator(obs1=pos_id, corr_operation="square_distance_componentwise", dt=dt, tau_max=tmax, tau_lin=16) system.auto_update_correlators.add(msd) ## Exercise 7a ## # Construct the auto-correlators for the AVACF, using the example # of the MSD. # Initialize the angular velocity auto-correlation function # (AVACF) correlator ... # Perform production # Integrate for k in range(prod_steps): print("Production {} of 5: {} of {}".format(cnt+1,k,prod_steps)) system.integrator.run(prod_length) # Finalize the MSD and export system.auto_update_correlators.remove(msd) msd.finalize() np.savetxt("{}/msd_{}.dat".format(outdir,cnt),msd.result()) ## Exercise 7b ## # Finalize the angular velocity auto-correlation function (AVACF) # correlator and write the result to a file. ... np.savetxt("{}/avacf_{}.dat".format(outdir,cnt),avacf.result())
gpl-3.0
-1,595,223,305,182,569,700
31.071186
115
0.530599
false
3.808776
false
false
false
heilaaks/snippy
tests/test_api_search_snippet.py
1
58285
# -*- coding: utf-8 -*- # # SPDX-License-Identifier: AGPL-3.0-or-later # # snippy - software development and maintenance notes manager. # Copyright 2017-2020 Heikki J. Laaksonen <[email protected]> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero 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 Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """test_api_search_snippet: Test GET /snippets API endpoint.""" from falcon import testing import falcon import pytest from tests.lib.content import Content from tests.lib.content import Storage from tests.lib.snippet import Snippet pytest.importorskip('gunicorn') # pylint: disable=unsubscriptable-object class TestApiSearchSnippet(object): # pylint: disable=too-many-public-methods, too-many-lines """Test GET /snippets API.""" @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_001(server): """Search snippets with GET. Send GET /snippets and search keywords from all fields. The search query matches to two snippets and both of them are returned. The search is sorted based on one field. The limit defined in the search query is not exceeded. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '1523' } expect_body = { 'meta': { 'count': 2, 'limit': 20, 'offset': 0, 'total': 2 }, 'data': [{ 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': Storage.remove }, { 'type': 'snippet', 'id': Snippet.FORCED_UUID, 'attributes': Storage.forced }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/vnd.api+json'}, query_string='sall=docker%2Cswarm&limit=20&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited') def test_api_search_snippet_002(server): """Search snippets with GET. Send GET /snippets and search keywords from all fields. The search query matches to four snippets but limit defined in search query results only two of them sorted by the brief field. The sorting must be applied before limit is applied. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '1658' } expect_body = { 'meta': { 'count': 2, 'limit': 2, 'offset': 0, 'total': 4 }, 'data': [{ 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': Storage.remove }, { 'type': 'snippet', 'id': Snippet.EXITED_UUID, 'attributes': Storage.exited }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&limit=2&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_003(server): """Search snippets with GET. Send GET /snippets and search keywords from all fields. The search query matches to two snippets but only one of them is returned because the limit parameter was set to one. In this case the sort is descending and the last match must be returned. The resulting fields are limited only to brief and category. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '218' } expect_body = { 'meta': { 'count': 1, 'limit': 1, 'offset': 0, 'total': 2 }, 'data': [{ 'type': 'snippet', 'id': Snippet.FORCED_UUID, 'attributes': {field: Storage.forced[field] for field in ['brief', 'category']} }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker&limit=1&sort=-brief&fields=brief,category') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_004(server): """Search snippets with GET. Send GET /snippets and search keywords from all fields but return only two fields. This syntax that separates the sorted fields causes the parameter to be processed in string context which must handle multiple fields. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '218' } expect_body = { 'meta': { 'count': 1, 'limit': 1, 'offset': 0, 'total': 2 }, 'data': [{ 'type': 'snippet', 'id': Snippet.FORCED_UUID, 'attributes': {field: Storage.forced[field] for field in ['brief', 'category']} }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker&limit=1&sort=-brief&fields=brief%2Ccategory') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited') def test_api_search_snippet_005(server): """Search snippets with GET. Send GET /snippets and search keywords from all fields. The search query matches to four snippets but limit defined in search query results only two of them sorted by the utc field in descending order. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '1626' } expect_body = { 'meta': { 'count': 2, 'limit': 2, 'offset': 0, 'total': 4 }, 'data': [{ 'type': 'snippet', 'id': Snippet.NETCAT_UUID, 'attributes': Storage.netcat }, { 'type': 'snippet', 'id': Snippet.EXITED_UUID, 'attributes': Storage.exited }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&limit=2&sort=-created,-brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited') def test_api_search_snippet_006(server): """Search snippets with GET. Send GET /snippets and search keywords from all fields sorted with two fields. This syntax that separates the sorted fields causes the parameter to be processed in string context which must handle multiple fields. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '1626' } expect_body = { 'meta': { 'count': 2, 'limit': 2, 'offset': 0, 'total': 4 }, 'data': [{ 'type': 'snippet', 'id': Snippet.NETCAT_UUID, 'attributes': Storage.netcat }, { 'type': 'snippet', 'id': Snippet.EXITED_UUID, 'attributes': Storage.exited }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&limit=2&sort=-created%2C-brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'caller') def test_api_search_snippet_007(server): """Search snippets with GET. Try to send GET /snippets with sort parameter set to field name that does not exist. In this case sorting must fall to default sorting. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '385' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '400', 'statusString': '400 Bad Request', 'module': 'snippy.testing.testing:123', 'title': 'sort option validation failed for non existent field=notexisting' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cswarm&limit=20&sort=notexisting') assert result.status == falcon.HTTP_400 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_008(server): """Search snippets with GET. Send GET /snippets to return only defined fields. In this case the fields are defined by setting the 'fields' parameter multiple times. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '218' } expect_body = { 'meta': { 'count': 1, 'limit': 1, 'offset': 0, 'total': 2 }, 'data': [{ 'type': 'snippet', 'id': Snippet.FORCED_UUID, 'attributes': {field: Storage.forced[field] for field in ['brief', 'category']} }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker&limit=1&sort=-brief&fields=brief&fields=category') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'caller') def test_api_search_snippet_009(server): """Search snippets with GET. Try to send GET /snippets with search keywords that do not result any matches. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '340' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '404', 'statusString': '404 Not Found', 'module': 'snippy.testing.testing:123', 'title': 'cannot find resources' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=notfound&limit=10&sort=-brief&fields=brief,category') assert result.status == falcon.HTTP_404 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'caller') def test_api_search_snippet_010(server): """Search snippets with GET from tag fields. Try to send GET /snippets with search tag keywords that do not result any matches. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '340' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '404', 'statusString': '404 Not Found', 'module': 'snippy.testing.testing:123', 'title': 'cannot find resources' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='stag=notfound&limit=10&sort=-brief&fields=brief,category') assert result.status == falcon.HTTP_404 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'caller') def test_api_search_snippet_011(server): """Search snippet from groups fields. Try to send GET /snippets with search groups keywords that do not result any matches. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '340' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '404', 'statusString': '404 Not Found', 'module': 'snippy.testing.testing:123', 'title': 'cannot find resources' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sgrp=notfound&limit=10&sort=-brief&fields=brief,category') assert result.status == falcon.HTTP_404 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_012(server): """Search snippet with digets. Send GET /snippets/{id} to read a snippet based on digest. In this case the snippet is found. In this case the URI path contains 15 digit digest. The returned self link must be the 16 digit link. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '871' } expect_body = { 'meta': { 'count': 1, 'limit': 20, 'offset': 0, 'total': 1 }, 'data': { 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': Storage.remove }, 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets/' + Snippet.REMOVE_UUID } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets/54e41e9b52a02b6', headers={'accept': 'application/json'}) assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'caller') def test_api_search_snippet_013(server): """Search snippet with digets. Try to send GET /snippets/{id} with a digest that is not found. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '395' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '404', 'statusString': '404 Not Found', 'module': 'snippy.testing.testing:123', 'title': 'content identity: 101010101010101 was not unique and matched to: 0 resources' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets/101010101010101', headers={'accept': 'application/json'}) assert result.status == falcon.HTTP_404 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_014(server): """Search snippet without search parameters. Send GET /snippets without defining search parameters. In this case all content should be returned. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '1523' } expect_body = { 'meta': { 'count': 2, 'limit': 20, 'offset': 0, 'total': 2 }, 'data': [{ 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': Storage.remove }, { 'type': 'snippet', 'id': Snippet.FORCED_UUID, 'attributes': Storage.forced }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='limit=20&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_015(server): """Search snippet without search parameters. Send GET /snippets without defining search parameters. In this case only one snippet must be returned because the limit is set to one. Also the sorting based on brief field causes the last snippet to be returned. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '830' } expect_body = { 'meta': { 'count': 1, 'limit': 1, 'offset': 0, 'total': 2 }, 'data': [{ 'type': 'snippet', 'id': Snippet.FORCED_UUID, 'attributes': Storage.forced }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='limit=1&sort=-brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.parametrize('server', [['server', '--server-host', 'localhost:8080', '-q']], indirect=True) @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_016(server): """Search snippets with GET. Send GET /snippets and search keywords from all attributes. The search query matches to two snippets and both of them are returned. The response JSON is sent as pretty printed. TODO: The groups refactoring changed the lenght from 2196 to 2278. Why so much? Is there a problem in the result JSON? """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '2709' } expect_body = { 'meta': { 'count': 2, 'limit': 20, 'offset': 0, 'total': 2 }, 'data': [{ 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': Storage.remove }, { 'type': 'snippet', 'id': Snippet.FORCED_UUID, 'attributes': Storage.forced }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/vnd.api+json'}, query_string='sall=docker%2Cswarm&limit=20&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited') def test_api_search_snippet_paginate_001(server): """Search snippets with GET. Send GET /snippets so that pagination is applied. The offset is zero and limit is bigger that the amount of search results so that all results fit into one response. Because all results fit into the same response, there is no need for next and prev links and those must not be set. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '3425' } expect_body = { 'meta': { 'count': 4, 'limit': 10, 'offset': 0, 'total': 4 }, 'data': [{ 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': Storage.remove }, { 'type': 'snippet', 'id': Snippet.EXITED_UUID, 'attributes': Storage.exited }, { 'type': 'snippet', 'id': Snippet.FORCED_UUID, 'attributes': Storage.forced }, { 'type': 'snippet', 'id': Snippet.NETCAT_UUID, 'attributes': Storage.netcat }], 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets?limit=10&offset=0&sall=docker%2Cnmap&sort=brief', 'first': 'http://falconframework.org/api/snippy/rest/snippets?limit=10&offset=0&sall=docker%2Cnmap&sort=brief', 'last': 'http://falconframework.org/api/snippy/rest/snippets?limit=10&offset=0&sall=docker%2Cnmap&sort=brief' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=0&limit=10&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited') def test_api_search_snippet_paginate_002(server): """Search snippets with GET. Send GET /snippets so that pagination is applied. The offset is zero and limit is smaller that the amount of search results so that all results do not fit into one response. Because this is the first page, the prev link must not be set. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '2110' } expect_body = { 'meta': { 'count': 2, 'limit': 2, 'offset': 0, 'total': 4 }, 'data': [{ 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': Storage.remove }, { 'type': 'snippet', 'id': Snippet.EXITED_UUID, 'attributes': Storage.exited }], 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=0&sall=docker%2Cnmap&sort=brief', 'first': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=0&sall=docker%2Cnmap&sort=brief', 'next': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=2&sall=docker%2Cnmap&sort=brief', 'last': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=2&sall=docker%2Cnmap&sort=brief' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=0&limit=2&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited') def test_api_search_snippet_paginate_003(server): """Search snippets with GET. Send GET /snippets so that pagination is applied. The offset is non zero and second page is requested. The requested second page is the last page. Because of this, there next link must not be set. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '1942' } expect_body = { 'meta': { 'count': 2, 'limit': 2, 'offset': 2, 'total': 4 }, 'data': [{ 'type': 'snippet', 'id': Snippet.FORCED_UUID, 'attributes': Storage.forced }, { 'type': 'snippet', 'id': Snippet.NETCAT_UUID, 'attributes': Storage.netcat }], 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=2&sall=docker%2Cnmap&sort=brief', 'first': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=0&sall=docker%2Cnmap&sort=brief', 'prev': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=0&sall=docker%2Cnmap&sort=brief', 'last': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=2&sall=docker%2Cnmap&sort=brief' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=2&limit=2&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited') def test_api_search_snippet_paginate_004(server): """Search snippets with GET. Send GET /snippets so that pagination is applied. The offset is non zero and second page is requested. The requested second page is not the last page. In this case the last page has as many hits that will fit into one page (even last page). All pagination links must be set. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '1528' } expect_body = { 'meta': { 'count': 1, 'limit': 1, 'offset': 1, 'total': 4 }, 'data': [{ 'type': 'snippet', 'id': Snippet.EXITED_UUID, 'attributes': Storage.exited }], 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets?limit=1&offset=1&sall=docker%2Cnmap&sort=brief', 'first': 'http://falconframework.org/api/snippy/rest/snippets?limit=1&offset=0&sall=docker%2Cnmap&sort=brief', 'next': 'http://falconframework.org/api/snippy/rest/snippets?limit=1&offset=2&sall=docker%2Cnmap&sort=brief', 'prev': 'http://falconframework.org/api/snippy/rest/snippets?limit=1&offset=0&sall=docker%2Cnmap&sort=brief', 'last': 'http://falconframework.org/api/snippy/rest/snippets?limit=1&offset=3&sall=docker%2Cnmap&sort=brief' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=1&limit=1&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited') def test_api_search_snippet_paginate_005(server): """Search snippets with GET. Send GET /snippets so that pagination is applied. The offset is non zero and second page is requested. The requested second page is not the last page. In this case the last page has less items than will fit to last page (uneven last page). Also the first page is not even and must be correctly set to zero. All pagination links must be set. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '2289' } expect_body = { 'meta': { 'count': 2, 'limit': 2, 'offset': 1, 'total': 4 }, 'data': [{ 'type': 'snippet', 'id': Snippet.EXITED_UUID, 'attributes': Storage.exited }, { 'type': 'snippet', 'id': Snippet.FORCED_UUID, 'attributes': Storage.forced }], 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=1&sall=docker%2Cnmap&sort=brief', 'first': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=0&sall=docker%2Cnmap&sort=brief', 'next': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=3&sall=docker%2Cnmap&sort=brief', 'prev': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=0&sall=docker%2Cnmap&sort=brief', 'last': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=3&sall=docker%2Cnmap&sort=brief' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=1&limit=2&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited') def test_api_search_snippet_paginate_006(server): """Search snippets with GET. Send GET /snippets so that pagination is applied. The offset is non zero and the last page is requested. Because original request was not started with offset zero, the first and prev pages are not having offset based on limit. In here the offset is also exactly the same as total amount of hits. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '1181' } expect_body = { 'meta': { 'count': 1, 'limit': 2, 'offset': 3, 'total': 4 }, 'data': [{ 'type': 'snippet', 'id': Snippet.NETCAT_UUID, 'attributes': Storage.netcat }], 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=3&sall=docker%2Cnmap&sort=brief', 'first': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=0&sall=docker%2Cnmap&sort=brief', 'prev': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=1&sall=docker%2Cnmap&sort=brief', 'last': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=3&sall=docker%2Cnmap&sort=brief' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=3&limit=2&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited', 'import-umount') def test_api_search_snippet_paginate_007(server): """Search snippets with GET. Send GET /snippets so that pagination is applied. The offset and limit are set so that the last page contains less hits than the limit and the requested page is not the last or the second last page. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '2146' } expect_body = { 'meta': { 'count': 2, 'limit': 2, 'offset': 0, 'total': 5 }, 'data': [{ 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': Storage.remove }, { 'type': 'snippet', 'id': Snippet.EXITED_UUID, 'attributes': Storage.exited }], 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=0&sall=docker%2Cumount%2Cnmap&sort=brief', 'next': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=2&sall=docker%2Cumount%2Cnmap&sort=brief', 'first': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=0&sall=docker%2Cumount%2Cnmap&sort=brief', 'last': 'http://falconframework.org/api/snippy/rest/snippets?limit=2&offset=4&sall=docker%2Cumount%2Cnmap&sort=brief' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cumount%2Cnmap&offset=0&limit=2&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited', 'caller') def test_api_search_snippet_paginate_008(server): """Search snippets with GET. Try to send GET /snippets with pagination offset that is the same as the amount of snippets stored into the database. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '340' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '404', 'statusString': '404 Not Found', 'module': 'snippy.testing.testing:123', 'title': 'cannot find resources' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=4&limit=2&sort=brief') assert result.status == falcon.HTTP_404 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited', 'caller') def test_api_search_snippet_paginate_009(server): """Search snippets with GET. Try to send GET /snippets with pagination offset that is one bigger than the maximum amount of hits. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '340' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '404', 'statusString': '404 Not Found', 'module': 'snippy.testing.testing:123', 'title': 'cannot find resources' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=10&limit=10&sort=brief') assert result.status == falcon.HTTP_404 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited') def test_api_search_snippet_paginate_010(server): """Search snippets with GET. Send GET /snippets so that pagination is applied with limit zero. This is a special case that returns the metadata but the data list is empty. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '71' } expect_body = { 'meta': { 'count': 0, 'limit': 0, 'offset': 0, 'total': 4 }, 'data': [], } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=0&limit=0&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited', 'caller') def test_api_search_snippet_paginate_011(server): """Search snippets with GET. Try to send GET /snippets with negative offset. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '364' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '400', 'statusString': '400 Bad Request', 'module': 'snippy.testing.testing:123', 'title': 'search offset is not a positive integer: -4' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=-4&limit=2&sort=brief') assert result.status == falcon.HTTP_400 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited', 'caller') def test_api_search_snippet_paginate_012(server): """Search snippets with GET. Try to send GET /snippets with negative offset and limit. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '520' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '400', 'statusString': '400 Bad Request', 'module': 'snippy.testing.testing:123', 'title': 'search result limit is not a positive integer: -2' }, { 'status': '400', 'statusString': '400 Bad Request', 'module': 'snippy.testing.testing:123', 'title': 'search offset is not a positive integer: -4' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=-4&limit=-2&sort=brief') assert result.status == falcon.HTTP_400 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'import-netcat', 'import-exited', 'caller') def test_api_search_snippet_paginate_013(server): """Search snippets with GET. Try to send GET /snippets when offset and limit are not numbers. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '533' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '400', 'statusString': '400 Bad Request', 'module': 'snippy.testing.testing:123', 'title': 'search result limit is not a positive integer: 0xdeadbeef' }, { 'status': '400', 'statusString': '400 Bad Request', 'module': 'snippy.testing.testing:123', 'title': 'search offset is not a positive integer: ABCDEFG' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&offset=ABCDEFG&limit=0xdeadbeef&sort=brief') assert result.status == falcon.HTTP_400 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_field_001(server): """Get specific snippet field. Send GET /snippets/{id}/data for existing snippet. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '267' } expect_body = { 'data': { 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': { 'data': Storage.remove['data'] } }, 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets/11cd5827-b6ef-4067-b5ac-3ceac07dde9f/data' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets/54e41e9b52a02b63/data', headers={'accept': 'application/vnd.api+json'}) assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_field_002(server): """Get specific snippet field. Send GET /snippets/{id}/brief for existing snippet. In this case the URI digest is only 10 octets. The returned link must contain 16 octet digest in the link. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '262' } expect_body = { 'data': { 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': { 'brief': Storage.remove['brief'] } }, 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets/11cd5827-b6ef-4067-b5ac-3ceac07dde9f/brief' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets/54e41e9b52/brief', headers={'accept': 'application/vnd.api+json'}) assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_field_003(server): """Get specific snippet field. Send GET /snippets/{id}/groups for existing snippet. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '231' } expect_body = { 'data': { 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': { 'groups': Storage.remove['groups'] } }, 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets/11cd5827-b6ef-4067-b5ac-3ceac07dde9f/groups' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets/54e41e9b52/groups', headers={'accept': 'application/vnd.api+json'}) assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_field_004(server): """Get specific snippet field. Send GET /snippets/{id}/tags for existing snippet. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '272' } expect_body = { 'data': { 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': { 'tags': Storage.remove['tags'] } }, 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets/11cd5827-b6ef-4067-b5ac-3ceac07dde9f/tags' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets/54e41e9b52/tags', headers={'accept': 'application/vnd.api+json'}) assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_field_005(server): """Get specific snippet field. Send GET /snippets/{id}/links for existing snippet. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '279' } expect_body = { 'data': { 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': { 'links': Storage.remove['links'] } }, 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets/11cd5827-b6ef-4067-b5ac-3ceac07dde9f/links' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets/54e41e9b52/links', headers={'accept': 'application/vnd.api+json'}) assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'caller') def test_api_search_snippet_field_006(server): """Get specific snippet field. Try to send GET /snippets/{id}/notexist for existing snippet. In this case the field name does not exist. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '360' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '400', 'statusString': '400 Bad Request', 'module': 'snippy.testing.testing:123', 'title': 'resource field does not exist: notexist' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets/54e41e9b52/notexist', headers={'accept': 'application/vnd.api+json'}) assert result.status == falcon.HTTP_400 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'caller') def test_api_search_snippet_field_007(server): """Get specific snippet field. Try to send GET /snippets/0101010101/brief for non existing snippet with valid field. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '390' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '404', 'statusString': '404 Not Found', 'module': 'snippy.testing.testing:123', 'title': 'content identity: 0101010101 was not unique and matched to: 0 resources' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets/0101010101/brief', headers={'accept': 'application/vnd.api+json'}) assert result.status == falcon.HTTP_404 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_api_search_snippet_field_008(server): """Get specific snippet field. Send GET /snippets/{id}/brief for existing snippet. In this case the URI id is full length UUID that must be found. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '251' } expect_body = { 'data': { 'type': 'snippet', 'id': Storage.forced['uuid'], 'attributes': { 'brief': Storage.forced['brief'] } }, 'links': { 'self': 'http://falconframework.org/api/snippy/rest/snippets/12cd5827-b6ef-4067-b5ac-3ceac07dde9f/brief' } } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets/12cd5827-b6ef-4067-b5ac-3ceac07dde9f/brief', headers={'accept': 'application/vnd.api+json'}) assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets', 'caller') def test_api_search_snippet_field_009(server): """Get specific snippet field. Try to send GET /snippets/{id} for existing snippet with short form from UUID. The short form must not be accepted and no results must be returned. The UUID is intended to be used as fully matching identity. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '416' } expect_body = { 'meta': Content.get_api_meta(), 'errors': [{ 'status': '404', 'statusString': '404 Not Found', 'module': 'snippy.testing.testing:123', 'title': 'content identity: 116cd5827-b6ef-4067-b5ac-3ceac07dde9 was not unique and matched to: 0 resources' }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets/116cd5827-b6ef-4067-b5ac-3ceac07dde9', headers={'accept': 'application/vnd.api+json'}) assert result.status == falcon.HTTP_404 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('default-snippets') def test_pytest_fixtures(server): """Test pytest fixtures with pytest specific mocking. Send GET /snippets and search keywords from all fields. The search query matches to two snippets and both of them are returned. The search is sorted based on one field. The limit defined in the search query is not exceeded. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '1523' } expect_body = { 'meta': { 'count': 2, 'limit': 20, 'offset': 0, 'total': 2 }, 'data': [{ 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': Storage.remove }, { 'type': 'snippet', 'id': Snippet.FORCED_UUID, 'attributes': Storage.forced }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cswarm&limit=20&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @staticmethod @pytest.mark.usefixtures('import-remove', 'import-forced', 'import-exited', 'import-netcat') def test_pytest_fixtures2(server): """Test pytest fixtures with pytest specific mocking. Send GET /snippets and search keywords from all fields. The search query matches to four snippets but limit defined in search query results only two of them sorted by the brief field. The sorting must be applied before limit is applied. """ expect_headers = { 'content-type': 'application/vnd.api+json; charset=UTF-8', 'content-length': '1658' } expect_body = { 'meta': { 'count': 2, 'limit': 2, 'offset': 0, 'total': 4 }, 'data': [{ 'type': 'snippet', 'id': Snippet.REMOVE_UUID, 'attributes': Storage.remove }, { 'type': 'snippet', 'id': Snippet.EXITED_UUID, 'attributes': Storage.exited }] } result = testing.TestClient(server.server.api).simulate_get( path='/api/snippy/rest/snippets', headers={'accept': 'application/json'}, query_string='sall=docker%2Cnmap&limit=2&sort=brief') assert result.status == falcon.HTTP_200 assert result.headers == expect_headers Content.assert_restapi(result.json, expect_body) @classmethod def teardown_class(cls): """Teardown tests.""" Content.delete()
agpl-3.0
-5,150,466,451,538,383,000
38.30209
135
0.556455
false
4.027154
true
false
false
DawidvanGraan/HomeAutomation
Raspberry/api.py
1
3437
#!/bin/sh from flask import Flask, jsonify import smbus import time import RPi.GPIO as io import requests # Plex Call plexUrl = 'http://192.168.1.100/jsonrpc?request={"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": ["title", "album", "duration", "showtitle"], "playerid": 1 }, "id": "VideoGetItem"}'; I2C_ADDRESS = 0x4a app = Flask(__name__) gpioBigGate = 18 # Big Gate gpioSmallGate = 23 # Small Gate gpioGarageRight = 24 # Garage Right gpioGarageLeft = 25 # Garage Left mag_switch1 = 22 # Garage Door Right mag_switch2 = 17 # Garage Door Left # I2C BUS bus = smbus.SMBus(0) # GPIO io.setmode(io.BCM) io.setup(mag_switch1, io.IN, pull_up_down=io.PUD_UP) io.setup(mag_switch2, io.IN, pull_up_down=io.PUD_UP) io.setup(gpioBigGate, io.OUT) io.setup(gpioSmallGate, io.OUT) io.setup(gpioGarageRight, io.OUT) io.setup(gpioGarageLeft, io.OUT) @app.route('/api/v1/hello', methods=['GET']) def get_hello(): return jsonify({ "status": 200, "message": "Hello API. I'm Alive and waiting for your Commands!" }) @app.route('/api/v1/plex', methods=['GET']) def plex(): r = requests.get(plexUrl) if r.status_code != 200: return jsonify({ "status": 500, "message": "Oops, could not make call to Plex!" }) return jsonify(r.content) @app.route('/api/v1/biggate', methods=['GET']) def get_biggate(): io.output(gpioBigGate, io.HIGH) time.sleep(2) io.output(gpioBigGate, io.LOW) return jsonify({ "status": 200, "message": "Big Gate Busy..." }) @app.route('/api/v1/smallgate', methods=['GET']) def get_smallgate(): io.output(gpioSmallGate, io.HIGH) time.sleep(2) io.output(gpioSmallGate, io.LOW) return jsonify({ "status": 200, "message": "Small Gate Busy..." }) @app.route('/api/v1/garageright', methods=['GET']) def get_garage_right(): io.output(gpioGarageRight, io.HIGH) time.sleep(2) io.output(gpioGarageRight, io.LOW) rightSensor = io.input(mag_switch1) return jsonify({ "status": 200, "message": "Garage Door Right", "garageRight": rightSensor }) @app.route('/api/v1/garageleft', methods=['GET']) def get_garage_left(): io.output(gpioGarageLeft, io.HIGH) time.sleep(2) io.output(gpioGarageLeft, io.LOW) leftSensor = io.input(mag_switch2) return jsonify({ "status": 200, "message": "Garage Door Left", "garageLeft": leftSensor }) @app.route('/api/v1/garagedoors', methods=['GET']) def get_garage_doors(): rightSensor = io.input(mag_switch1) leftSensor = io.input(mag_switch2) return jsonify({ "status": 200, "message": "States of the Garage Doors", "garageRight": rightSensor, "garageLeft": leftSensor }) @app.route('/api/v1/temp1', methods=['GET']) def temp1(): values = bus.read_i2c_block_data(I2C_ADDRESS, 0x00, 2) tempMSB = values[0] tempLSB = values[1] temp = (((tempMSB << 8) | tempLSB) >> 7) * 0.5 if temp > 125: temp = (((((tempMSB << 8) | tempLSB) >> 7) * 0.5) - 256) return jsonify({ "status": 200, "message": "Temperature 1 Sensor Value", "temp": temp }) @app.route('/') def index(): return "Hello, Home Remote!!" if __name__ == '__main__': app.run(host='0.0.0.0', port=8080, debug=True)
mit
-7,297,475,318,174,467,000
22.380952
206
0.603142
false
2.925106
false
false
false
hirofumi0810/asr_preprocessing
swbd/main.py
1
15071
#! /usr/bin/env python # -*- coding: utf-8 -*- """Make dataset for the End-to-End model (Switchboard corpus). Note that feature extraction depends on transcripts. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from os.path import join, isfile import sys import argparse from tqdm import tqdm import numpy as np import pandas as pd from collections import Counter import pickle sys.path.append('../') from swbd.path import Path from swbd.input_data import read_audio from swbd.labels.ldc97s62.character import read_trans from swbd.labels.fisher.character import read_trans as read_trans_fisher from swbd.labels.eval2000.stm import read_stm from utils.util import mkdir_join from utils.inputs.wav_split import split_wav from utils.dataset import add_element parser = argparse.ArgumentParser() parser.add_argument('--swbd_audio_path', type=str, help='path to LDC97S62 audio files') parser.add_argument('--swbd_trans_path', type=str, help='path to LDC97S62 transciption files') parser.add_argument('--fisher_path', type=str, help='path to Fisher dataset') parser.add_argument('--eval2000_audio_path', type=str, help='path to audio files of eval2000 dataset') parser.add_argument('--eval2000_trans_path', type=str, help='path to transcript files of eval2000 dataset') parser.add_argument('--dataset_save_path', type=str, help='path to save dataset') parser.add_argument('--feature_save_path', type=str, help='path to save input features') parser.add_argument('--run_root_path', type=str, help='path to run this script') parser.add_argument('--tool', type=str, choices=['htk', 'python_speech_features', 'librosa']) parser.add_argument('--wav_save_path', type=str, help='path to wav files.') parser.add_argument('--htk_save_path', type=str, help='path to htk files.') parser.add_argument('--normalize', type=str, choices=['global', 'speaker', 'utterance', 'no']) parser.add_argument('--save_format', type=str, choices=['numpy', 'htk', 'wav']) parser.add_argument('--feature_type', type=str, choices=['fbank', 'mfcc']) parser.add_argument('--channels', type=int, help='the number of frequency channels') parser.add_argument('--window', type=float, help='window width to extract features') parser.add_argument('--slide', type=float, help='extract features per slide') parser.add_argument('--energy', type=int, help='if 1, add the energy feature') parser.add_argument('--delta', type=int, help='if 1, add the energy feature') parser.add_argument('--deltadelta', type=int, help='if 1, double delta features are also extracted') parser.add_argument('--fisher', type=int, help='If True, create large-size dataset (2000h).') args = parser.parse_args() path = Path(swbd_audio_path=args.swbd_audio_path, swbd_trans_path=args.swbd_trans_path, fisher_path=args.fisher_path, eval2000_audio_path=args.eval2000_audio_path, eval2000_trans_path=args.eval2000_trans_path, wav_save_path=args.wav_save_path, htk_save_path=args.htk_save_path, run_root_path='./') CONFIG = { 'feature_type': args.feature_type, 'channels': args.channels, 'sampling_rate': 8000, 'window': args.window, 'slide': args.slide, 'energy': bool(args.energy), 'delta': bool(args.delta), 'deltadelta': bool(args.deltadelta) } if args.save_format == 'htk': assert args.tool == 'htk' def main(data_size): print('=' * 50) print(' data_size: %s' % data_size) print('=' * 50) ######################################## # labels ######################################## print('=> Processing transcripts...') speaker_dict_dict = {} # dict of speaker_dict print('---------- train ----------') if data_size == '300h': speaker_dict_dict['train'] = read_trans( label_paths=path.trans(corpus='swbd'), word_boundary_paths=path.word(corpus='swbd'), run_root_path='./', vocab_file_save_path=mkdir_join('./config/vocab_files'), save_vocab_file=True) elif data_size == '2000h': speaker_dict_a, char_set_a, char_capital_set_a, word_count_dict_a = read_trans_fisher( label_paths=path.trans(corpus='fisher'), target_speaker='A') speaker_dict_b, char_set_b, char_capital_set_b, word_count_dict_b = read_trans_fisher( label_paths=path.trans(corpus='fisher'), target_speaker='B') # Meage 2 dictionaries speaker_dict = merge_dicts([speaker_dict_a, speaker_dict_b]) char_set = char_set_a | char_set_b char_capital_set = char_capital_set_a | char_capital_set_b word_count_dict_fisher = dict( Counter(word_count_dict_a) + Counter(word_count_dict_b)) speaker_dict_dict['train'] = read_trans( label_paths=path.trans(corpus='swbd'), word_boundary_paths=path.word(corpus='swbd'), run_root_path='./', vocab_file_save_path=mkdir_join('./config/vocab_files'), save_vocab_file=True, speaker_dict_fisher=speaker_dict, char_set=char_set, char_capital_set=char_capital_set, word_count_dict=word_count_dict_fisher) del speaker_dict print('---------- eval2000 (swbd + ch) ----------') speaker_dict_dict['eval2000_swbd'], speaker_dict_dict['eval2000_ch'] = read_stm( stm_path=path.stm_path, pem_path=path.pem_path, glm_path=path.glm_path, run_root_path='./') ######################################## # inputs ######################################## print('\n=> Processing input data...') input_save_path = mkdir_join( args.feature_save_path, args.save_format, data_size) for data_type in ['train', 'eval2000_swbd', 'eval2000_ch']: print('---------- %s ----------' % data_type) if isfile(join(input_save_path, data_type, 'complete.txt')): print('Already exists.') else: if args.save_format == 'wav': ######################################## # Split WAV files per utterance ######################################## if data_type == 'train': wav_paths = path.wav(corpus='swbd') if data_size == '2000h': wav_paths += path.wav(corpus='fisher') else: wav_paths = path.wav(corpus=data_type) split_wav(wav_paths=wav_paths, speaker_dict=speaker_dict_dict[data_type], save_path=mkdir_join(input_save_path, data_type)) # NOTE: ex.) save_path: # swbd/feature/save_format/data_size/data_type/speaker/utt_name.npy elif args.save_format in ['numpy', 'htk']: if data_type == 'train': if args.tool == 'htk': audio_paths = path.htk(corpus='swbd') if data_size == '2000h': audio_paths += path.htk(corpus='fisher') else: audio_paths = path.wav(corpus='swbd') if data_size == '2000h': audio_paths += path.wav(corpus='fisher') is_training = True global_mean, global_std = None, None else: if args.tool == 'htk': audio_paths = path.htk(corpus=data_type) else: audio_paths = path.wav(corpus=data_type) is_training = False # Load statistics over train dataset global_mean = np.load( join(input_save_path, 'train/global_mean.npy')) global_std = np.load( join(input_save_path, 'train/global_std.npy')) read_audio(audio_paths=audio_paths, tool=args.tool, config=CONFIG, normalize=args.normalize, speaker_dict=speaker_dict_dict[data_type], is_training=is_training, save_path=mkdir_join(input_save_path, data_type), save_format=args.save_format, global_mean=global_mean, global_std=global_std) # NOTE: ex.) save_path: # swbd/feature/save_format/data_size/data_type/speaker/*.npy # Make a confirmation file to prove that dataset was saved # correctly with open(join(input_save_path, data_type, 'complete.txt'), 'w') as f: f.write('') ######################################## # dataset (csv) ######################################## print('\n=> Saving dataset files...') dataset_save_path = mkdir_join( args.dataset_save_path, args.save_format, data_size, data_type) print('---------- %s ----------' % data_type) df_columns = ['frame_num', 'input_path', 'transcript'] df_char = pd.DataFrame([], columns=df_columns) df_char_capital = pd.DataFrame([], columns=df_columns) df_word_freq1 = pd.DataFrame([], columns=df_columns) df_word_freq5 = pd.DataFrame([], columns=df_columns) df_word_freq10 = pd.DataFrame([], columns=df_columns) df_word_freq15 = pd.DataFrame([], columns=df_columns) with open(join(input_save_path, data_type, 'frame_num.pickle'), 'rb') as f: frame_num_dict = pickle.load(f) utt_count = 0 df_char_list, df_char_capital_list = [], [] df_word_freq1_list, df_word_freq5_list = [], [] df_word_freq10_list, df_word_freq15_list = [], [] speaker_dict = speaker_dict_dict[data_type] for speaker, utt_dict in tqdm(speaker_dict.items()): for utt_index, utt_info in utt_dict.items(): if args.save_format == 'numpy': input_utt_save_path = join( input_save_path, data_type, speaker, speaker + '_' + utt_index + '.npy') elif args.save_format == 'htk': input_utt_save_path = join( input_save_path, data_type, speaker, speaker + '_' + utt_index + '.htk') elif args.save_format == 'wav': input_utt_save_path = path.utt2wav(utt_index) else: raise ValueError('save_format is numpy or htk or wav.') assert isfile(input_utt_save_path) frame_num = frame_num_dict[speaker + '_' + utt_index] char_indices, char_indices_capital, word_freq1_indices = utt_info[2:5] word_freq5_indices, word_freq10_indices, word_freq15_indices = utt_info[5:8] df_char = add_element( df_char, [frame_num, input_utt_save_path, char_indices]) df_char_capital = add_element( df_char_capital, [frame_num, input_utt_save_path, char_indices_capital]) df_word_freq1 = add_element( df_word_freq1, [frame_num, input_utt_save_path, word_freq1_indices]) df_word_freq5 = add_element( df_word_freq5, [frame_num, input_utt_save_path, word_freq5_indices]) df_word_freq10 = add_element( df_word_freq10, [frame_num, input_utt_save_path, word_freq10_indices]) df_word_freq15 = add_element( df_word_freq15, [frame_num, input_utt_save_path, word_freq15_indices]) utt_count += 1 # Reset if utt_count == 10000: df_char_list.append(df_char) df_char_capital_list.append(df_char_capital) df_word_freq1_list.append(df_word_freq1) df_word_freq5_list.append(df_word_freq5) df_word_freq10_list.append(df_word_freq10) df_word_freq15_list.append(df_word_freq15) df_char = pd.DataFrame([], columns=df_columns) df_char_capital = pd.DataFrame([], columns=df_columns) df_word_freq1 = pd.DataFrame([], columns=df_columns) df_word_freq5 = pd.DataFrame([], columns=df_columns) df_word_freq10 = pd.DataFrame([], columns=df_columns) df_word_freq15 = pd.DataFrame([], columns=df_columns) utt_count = 0 # Last dataframe df_char_list.append(df_char) df_char_capital_list.append(df_char_capital) df_word_freq1_list.append(df_word_freq1) df_word_freq5_list.append(df_word_freq5) df_word_freq10_list.append(df_word_freq10) df_word_freq15_list.append(df_word_freq15) # Concatenate all dataframes df_char = df_char_list[0] df_char_capital = df_char_capital_list[0] df_word_freq1 = df_word_freq1_list[0] df_word_freq5 = df_word_freq5_list[0] df_word_freq10 = df_word_freq10_list[0] df_word_freq15 = df_word_freq15_list[0] for df_i in df_char_list[1:]: df_char = pd.concat([df_char, df_i], axis=0) for df_i in df_char_list[1:]: df_char_capital = pd.concat([df_char_capital, df_i], axis=0) for df_i in df_word_freq1_list[1:]: df_word_freq1 = pd.concat([df_word_freq1, df_i], axis=0) for df_i in df_word_freq5_list[1:]: df_word_freq5 = pd.concat([df_word_freq5, df_i], axis=0) for df_i in df_word_freq10_list[1:]: df_word_freq10 = pd.concat([df_word_freq10, df_i], axis=0) for df_i in df_word_freq15_list[1:]: df_word_freq15 = pd.concat([df_word_freq15, df_i], axis=0) df_char.to_csv(join(dataset_save_path, 'character.csv')) df_char_capital.to_csv( join(dataset_save_path, 'character_capital_divide.csv')) df_word_freq1.to_csv(join(dataset_save_path, 'word_freq1.csv')) df_word_freq5.to_csv(join(dataset_save_path, 'word_freq5.csv')) df_word_freq10.to_csv(join(dataset_save_path, 'word_freq10.csv')) df_word_freq15.to_csv(join(dataset_save_path, 'word_freq15.csv')) def merge_dicts(dicts): return {k: v for dic in dicts for k, v in dic.items()} if __name__ == '__main__': data_sizes = ['2000h'] # data_sizes = ['300h'] # if bool(args.fisher): # data_sizes += ['2000h'] for data_size in data_sizes: main(data_size)
mit
-8,193,133,252,302,720,000
43.196481
96
0.544224
false
3.602055
false
false
false
ar0551/Wasp
devFiles/data/waspCatalogFix.py
1
54170
# Wasp: Discrete Design with Grasshopper plug-in (GPL) initiated by Andrea Rossi # # This file is part of Wasp. # # Copyright (c) 2017, Andrea Rossi <[email protected]> # Wasp 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. # # Wasp 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 Wasp; If not, see <http://www.gnu.org/licenses/>. # # @license GPL-3.0 <https://www.gnu.org/licenses/gpl.html> # # Significant parts of Wasp have been developed by Andrea Rossi # as part of research on digital materials and discrete design at: # DDU Digital Design Unit - Prof. Oliver Tessmann # Technische Universitat Darmstadt ######################################################################### ## IMPORTS ## ######################################################################### import random import math import bisect from Rhino.RhinoDoc import ActiveDoc import Rhino.Geometry as rg ######################################################################### ## GLOBAL VARIABLES ## ######################################################################### global_tolerance = ActiveDoc.ModelAbsoluteTolerance*2 ######################################################################### ## CLASSES ## ######################################################################### #################################################################### Connection #################################################################### class Connection(object): ## constructor def __init__(self, _plane, _type, _part, _id): self.pln = _plane flip_pln_Y = rg.Vector3d(self.pln.YAxis) flip_pln_Y.Reverse() self.flip_pln = rg.Plane(self.pln.Origin, self.pln.XAxis, flip_pln_Y) self.type = _type self.part = _part self.id = _id self.rules_table = [] self.active_rules = [] ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspConnection [id: %s, type: %s]" % (self.id, self.type) ## return a transformed copy of the connection def transform(self, trans): pln_trans = rg.Plane(self.pln.Origin, self.pln.XAxis, self.pln.YAxis) conn_trans = Connection(pln_trans, self.type, self.part, self.id) conn_trans.pln.Transform(trans) conn_trans.flip_pln.Transform(trans) return conn_trans ## return a copy of the connection def copy(self): pln_copy = rg.Plane(self.pln.Origin, self.pln.XAxis, self.pln.YAxis) conn_copy = Connection(pln_copy, self.type, self.part, self.id) return conn_copy ## generate the rules-table for the connection def generate_rules_table(self, rules): count = 0 self.rules_table = [] self.active_rules = [] for rule in rules: if rule.part1 == self.part and rule.conn1 == self.id: self.rules_table.append(rule) self.active_rules.append(count) count += 1 #################################################################### Base Part #################################################################### class Part(object): ## constructor def __init__(self, name, geometry, connections, collider, attributes, dim=None, id=None, field=None): self.name = name self.id = id self.geo = geometry self.field = field self.connections = [] self.active_connections = [] count = 0 for conn in connections: conn.part = self.name conn.id = count self.connections.append(conn) self.active_connections.append(count) count += 1 self.transformation = rg.Transform.Identity self.center = self.geo.GetBoundingBox(False).Center self.collider = collider ##part size if dim is not None: self.dim = dim else: max_collider_dist = None for coll_geo in self.collider.geometry: for v in coll_geo.Vertices: dist = self.center.DistanceTo(v) if dist > max_collider_dist or max_collider_dist is None: max_collider_dist = dist self.dim = max_collider_dist self.parent = None self.children = [] self.attributes = [] if len(attributes) > 0: self.attributes = attributes self.is_constrained = False ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspPart [name: %s, id: %s]" % (self.name, self.id) ## reset the part and connections according to new provided aggregation rules def reset_part(self, rules): count = 0 self.active_connections = [] for conn in self.connections: conn.generate_rules_table(rules) self.active_connections.append(count) count += 1 ## return a dictionary containing all part data def return_part_data(self): data_dict = {} data_dict['name'] = self.name data_dict['id'] = self.id data_dict['geo'] = self.geo data_dict['connections'] = self.connections data_dict['transform'] = self.transformation data_dict['collider'] = self.collider data_dict['center'] = self.center data_dict['parent'] = self.parent data_dict['children'] = self.children data_dict['attributes'] = self.attributes return data_dict ## return a transformed copy of the part def transform(self, trans, transform_sub_parts=False): geo_trans = self.geo.Duplicate() geo_trans.Transform(trans) collider_trans = self.collider.transform(trans) connections_trans = [] for conn in self.connections: connections_trans.append(conn.transform(trans)) attributes_trans = [] if len(self.attributes) > 0: for attr in self.attributes: attributes_trans.append(attr.transform(trans)) part_trans = Part(self.name, geo_trans, connections_trans, collider_trans, attributes_trans, dim=self.dim, id=self.id, field=self.field) part_trans.transformation = trans return part_trans ## return a copy of the part def copy(self): geo_copy = self.geo.Duplicate() collider_copy = self.collider.copy() connections_copy = [] for conn in self.connections: connections_copy.append(conn.copy()) attributes_copy = [] if len(self.attributes) > 0: for attr in self.attributes: attributes_copy.append(attr.copy()) part_copy = Part(self.name, geo_copy, connections_copy, collider_copy, attributes_copy, dim=self.dim, id=self.id, field=self.field) part_copy.transformation = self.transformation return part_copy ## return transformed center point of the part def transform_center(self, trans): center_trans = rg.Point3d(self.center) center_trans.Transform(trans) return center_trans ## return transformed collider def transform_collider(self, trans): return self.collider.transform(trans) #################################################################### Constrained Part #################################################################### class AdvancedPart(Part): ## constructor def __init__(self, name, geometry, connections, collider, attributes, additional_collider, supports, dim = None, id=None, field=None, sub_parts=[]): super(self.__class__, self).__init__(name, geometry, connections, collider, attributes, dim=dim, id=id, field=field) self.add_collider = None if additional_collider != None: self.add_collider = additional_collider self.supports = [] if len(supports) > 0: self.supports = supports ## hierarchical sub-parts self.sub_parts = sub_parts self.hierarchy_level = 0 if len(self.sub_parts) > 0: self.hierarchy_level = self.sub_parts[0].hierarchy_level + 1 ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspAdvPart [name: %s, id: %s]" % (self.name, self.id) ## return all part data def return_part_data(self): data_dict = {} data_dict['name'] = self.name data_dict['id'] = self.id data_dict['geo'] = self.geo data_dict['connections'] = self.connections data_dict['transform'] = self.transformation data_dict['collider'] = self.collider data_dict['center'] = self.center data_dict['parent'] = self.parent data_dict['children'] = self.children data_dict['attributes'] = self.attributes data_dict['add_collider'] = self.add_collider return data_dict ## return a transformed copy of the part def transform(self, trans, transform_sub_parts=False, sub_level = 0): geo_trans = self.geo.Duplicate() geo_trans.Transform(trans) collider_trans = self.collider.transform(trans) connections_trans = [] for conn in self.connections: connections_trans.append(conn.transform(trans)) attributes_trans = [] if len(self.attributes) > 0: for attr in self.attributes: attributes_trans.append(attr.transform(trans)) add_collider_trans = None if(self.add_collider != None): add_collider_trans = self.add_collider.transform(trans, transform_connections=True, maintain_valid=True) supports_trans = [] if len(self.supports) > 0: for sup in self.supports: sup_trans = sup.transform(trans) supports_trans.append(sup_trans) if transform_sub_parts and len(self.sub_parts) > 0 and sub_level > 0: sub_parts_trans = [] for sp in self.sub_parts: sp_trans = sp.transform(trans, transform_sub_parts = True, sub_level = sub_level - 1) sub_parts_trans.append(sp_trans) part_trans = AdvancedPart(self.name, geo_trans, connections_trans, collider_trans, attributes_trans, add_collider_trans, supports_trans, dim=self.dim, id=self.id, field=self.field, sub_parts=sub_parts_trans) part_trans.transformation = trans part_trans.is_constrained = True return part_trans else: part_trans = AdvancedPart(self.name, geo_trans, connections_trans, collider_trans, attributes_trans, add_collider_trans, supports_trans, dim=self.dim, id=self.id, field=self.field, sub_parts=self.sub_parts) part_trans.transformation = trans part_trans.is_constrained = True return part_trans ## return a copy of the part def copy(self): geo_copy = self.geo.Duplicate() collider_copy = self.collider.copy() connections_copy = [] for conn in self.connections: connections_copy.append(conn.copy()) attributes_copy = [] if len(self.attributes) > 0: for attr in self.attributes: attributes_copy.append(attr.copy()) add_collider_copy = None if(self.add_collider != None): add_collider_copy = self.add_collider.copy() supports_copy = [] if len(self.supports) > 0: for sup in self.supports: sup_copy = sup.copy() supports_copy.append(sup_copy) if len(self.sub_parts) > 0: sub_parts_copy = [] for sp in self.sub_parts: sp_copy = sp.copy() sub_parts_copy.append(sp_copy) part_copy = AdvancedPart(self.name, geo_copy, connections_copy, collider_copy, attributes_copy, add_collider_copy, supports_copy, dim=self.dim, id=self.id, field=self.field, sub_parts=sub_parts_copy) part_copy.transformation = self.transformation part_copy.is_constrained = True return part_copy else: part_copy = AdvancedPart(self.name, geo_copy, connections_copy, collider_copy, attributes_copy, add_collider_copy, supports_copy, dim=self.dim, id=self.id, field=self.field, sub_parts=self.sub_parts) part_copy.transformation = self.transformation part_copy.is_constrained = True return part_copy #################################################################### Rule #################################################################### class Rule(object): def __init__(self, _part1, _conn1, _part2, _conn2, _active = True): self.part1 = _part1 self.conn1 = _conn1 self.part2 = _part2 self.conn2 = _conn2 self.active = _active ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspRule [%s|%s_%s|%s]" % (self.part1, self.conn1, self.part2, self.conn2) #################################################################### Field #################################################################### class Field(object): ## constructor def __init__(self, name, boundaries, pts, count_vec, resolution, values = []): self.name = name self.resolution = resolution self.boundaries = boundaries self.pts = pts self.bbox = rg.BoundingBox(pts) self.x_count = int(count_vec.X) self.y_count = int(count_vec.Y) self.z_count = int(count_vec.Z) self.vals = [] pts_count = 0 self.is_tensor_field = False try: v = values[0][2] self.is_tensor_field = True except: pass if len(values) > 0: for z in range(0, self.z_count): self.vals.append([]) for y in range(0, self.y_count): self.vals[z].append([]) for x in range(0, self.x_count): if len(self.boundaries) > 0: inside = False for bou in self.boundaries: if bou.IsPointInside(self.pts[pts_count], global_tolerance, True) == True: self.vals[z][y].append(values[pts_count]) inside = True break if inside == False: if self.is_tensor_field: self.vals[z][y].append(rg.Vector3d(0,0,0)) else: self.vals[z][y].append(0.0) else: self.vals[z][y].append(values[pts_count]) pts_count += 1 ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspField [name: %s, res: %s, count: %s]" % (self.name, self.resolution, len(self.pts)) def return_values_list(self): values_list = [] for z in range(0, self.z_count): for y in range(0, self.y_count): for x in range(0, self.x_count): values_list.append(self.vals[z][y][x]) return values_list ## return value associated to the closest point of the field to the given point def return_pt_val(self, pt): pt_trans = pt - self.bbox.Min x = int(math.floor(pt_trans.X/self.resolution)) y = int(math.floor(pt_trans.Y/self.resolution)) z = int(math.floor(pt_trans.Z/self.resolution)) value = self.vals[z][y][x] return value ## find and return highest value in the field def return_highest_pt(self, constraints = None): max_val = -1 max_coords = None for z in range(0, self.z_count): for y in range(0, self.y_count): for x in range(0, self.x_count): value = self.vals[z][y][x] ## tensor field aggregation (WIP) if self.is_tensor_field: if value.Length > max_val: if constraints is not None: constraint_check = False pt = rg.Point3d(x*self.resolution, y*self.resolution, z*self.resolution) pt += self.bbox.Min for constraint in constraints: if constraint.check_soft(pt) == False: constraint_check = True break if constraint_check == False: max_val = value.Length max_coords = (x,y,z) else: max_val = value.Length max_coords = (x,y,z) else: if value > max_val: if constraints is not None: constraint_check = False pt = rg.Point3d(x*self.resolution, y*self.resolution, z*self.resolution) pt += self.bbox.Min for constraint in constraints: if constraint.check_soft(pt) == False: constraint_check = True break if constraint_check == False: max_val = value max_coords = (x,y,z) else: max_val = value max_coords = (x,y,z) highest_pt = rg.Point3d(max_coords[0]*self.resolution, max_coords[1]*self.resolution, max_coords[2]*self.resolution) highest_pt = highest_pt + self.bbox.Min return highest_pt #################################################################### Attribute #################################################################### class Attribute(object): ## constructor def __init__(self, name, values, transformable): self.name = name self.values = values self.transformable = transformable ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspAttribute [name: %s]" % (self.name) ## return a transformed copy of the attribute def transform(self, trans): if self.transformable == True: values_trans = [] for val in self.values: val_trans = None if type(val) == rg.Point3d: val_trans = rg.Point3d(val) elif type(val) == rg.Plane: val_trans = rg.Plane(val) elif type(val) == rg.Line: val_trans = rg.Line(val.From, val.To) else: val_trans = val.Duplicate() val_trans.Transform(trans) values_trans.append(val_trans) attr_trans = Attribute(self.name, values_trans, self.transformable) else: attr_trans = Attribute(self.name, self.values, self.transformable) return attr_trans ## return a copy of the attribute def copy(self): if self.transformable == True: values_copy = [] for val in self.values: val_copy = None if type(val) == rg.Point3d: val_copy = rg.Point3d(val) elif type(val) == rg.Plane: val_copy = rg.Plane(val) elif type(val) == rg.Line: val_copy = rg.Line(val.From, val.To) else: val_copy = val.Duplicate() values_copy.append(val_copy) attr_copy = Attribute(self.name, values_copy, self.transformable) else: attr_copy = Attribute(self.name, self.values, self.transformable) return attr_copy #################################################################### Support #################################################################### class Support(object): ## constructor def __init__(self, support_directions): self.sup_dir = support_directions ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspSupport [len: %s]" % (len(self.sup_dir)) ## return a transformed copy of the support def transform(self, trans): sup_dir_trans = [] for dir in self.sup_dir: dir = dir.ToNurbsCurve() start_trans = dir.PointAtStart end_trans = dir.PointAtEnd start_trans.Transform(trans) end_trans.Transform(trans) dir_trans = rg.Line(start_trans, end_trans) sup_dir_trans.append(dir_trans) sup_trans = Support(sup_dir_trans) return sup_trans ## return a copy of the support def copy(self): sup_dir_copy = [] for dir in self.sup_dir: dir = dir.ToNurbsCurve() start_copy = dir.PointAtStart end_copy = dir.PointAtEnd dir_copy = rg.Line(start_copy, end_copy) sup_dir_copy.append(dir_copy) sup_copy = Support(sup_dir_copy) return sup_copy #################################################################### Aggregation #################################################################### class Aggregation(object): ## class constructor def __init__(self, _name, _parts, _rules, _mode, _prev = [], _coll_check = True, _field = [], _global_constraints = [], _catalog = None): ## basic parameters self.name = _name self.parts = {} for part in _parts: self.parts[part.name] = part self.rules = _rules self.mode = _mode self.coll_check = _coll_check self.aggregated_parts = [] ## fields self.multiple_fields = False if len(_field) == 0 or _field is None: self.field = None elif len(_field) == 1: self.field = _field[0] else: self.field = {} for f in _field: self.field[f.name] = f self.multiple_fields = True ## reset base parts self.reset_base_parts() ## temp list to store possible colliders to newly added parts self.possible_collisions = [] ## aggregation queue, storing sorted possible next states in the form (part, f_val) self.aggregation_queue = [] self.queue_values = [] self.queue_count = 0 ## previous aggregated parts self.prev_num = 0 if len(_prev) > 0: self.prev_num = len(_prev) for prev_p in _prev: prev_p_copy = prev_p.copy() prev_p_copy.reset_part(self.rules) prev_p_copy.id = len(self.aggregated_parts) self.aggregated_parts.append(prev_p_copy) if self.field is not None: self.compute_next_w_field(prev_p_copy) ## global constraints applied to the aggregation self.global_constraints = _global_constraints self.catalog = _catalog #### WIP #### self.collision_shapes = [] self.graph = None ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspAggregation [name: %s, size: %s]" % (self.name, len(self.aggregated_parts)) ## reset base parts def reset_base_parts(self, new_parts = None): if new_parts != None: self.parts = {} for part in new_parts: self.parts[part.name] = part for p_key in self.parts: self.parts[p_key].reset_part(self.rules) ## reset rules and regenerate rule tables for each part def reset_rules(self, rules): if rules != self.rules: self.rules = rules self.reset_base_parts() for part in self.aggregated_parts: part.reset_part(rules) ## recompute aggregation queue def recompute_aggregation_queue(self): self.aggregation_queue = [] self.queue_values = [] self.queue_count = 0 for part in self.aggregated_parts: self.compute_next_w_field(part) ## trim aggregated parts list to a specific length def remove_elements(self, num): self.aggregated_parts = self.aggregated_parts[:num] for part in self.aggregated_parts: part.reset_part(self.rules) if self.field is not None: self.recompute_aggregation_queue() ## compute all possible parts which can be placed given an existing part and connection def compute_possible_children(self, part_id, conn_id, check_constraints = False): possible_children = [] current_part = self.aggregated_parts[part_id] if conn_id in current_part.active_connections: current_conn = current_part.connections[conn_id] for rule_id in current_conn.active_rules: rule = current_conn.rules_table[rule_id] next_part = self.parts[rule.part2] orientTransform = rg.Transform.PlaneToPlane(next_part.connections[rule.conn2].flip_pln, current_conn.pln) ## boolean checks for all constraints coll_check = False add_coll_check = False valid_connections = [] missing_sup_check = False global_const_check = False if check_constraints: ## collision check self.possible_collisions = [] coll_check = self.collision_check(next_part, orientTransform) ## constraints check if self.mode == 1: ## only local constraints mode if coll_check == False and next_part.is_constrained: add_coll_check = self.additional_collider_check(next_part, orientTransform) if add_coll_check == False: missing_sup_check = self.missing_supports_check(next_part, orientTransform) elif self.mode == 2: ## onyl global constraints mode if coll_check == False and len(self.global_constraints) > 0: global_const_check = self.global_constraints_check(next_part, orientTransform) elif self.mode == 3: ## local+global constraints mode if coll_check == False: if len(self.global_constraints) > 0: global_const_check = self.global_constraints_check(next_part, orientTransform) if global_const_check == False and next_part.is_constrained: add_coll_check = self.additional_collider_check(next_part, orientTransform) if add_coll_check == False: missing_sup_check = self.missing_supports_check(next_part, orientTransform) if coll_check == False and add_coll_check == False and missing_sup_check == False and global_const_check == False: next_part_trans = next_part.transform(orientTransform) possible_children.append(next_part_trans) return possible_children else: return -1 ## add a custom pre-computed part which has been already transformed in place and checked for constraints def add_custom_part(self, part_id, conn_id, next_part): next_part.reset_part(self.rules) next_part.id = len(self.aggregated_parts) self.aggregated_parts[part_id].children.append(next_part) next_part.parent = self.aggregated_parts[part_id] self.aggregated_parts.append(next_part) for i in range(len(self.aggregated_parts[part_id].active_connections)): if self.aggregated_parts[part_id].active_connections[i] == conn_id: self.aggregated_parts[part_id].active_connections.pop(i) break #### constraints checks #### ## function grouping all constraints checks (not yet implemented) def constraints_check(self, part, trans): pass ## overlap // part-part collision check def collision_check(self, part, trans): part_center = part.transform_center(trans) ## overlap check coll_count = 0 for ex_part in self.aggregated_parts: dist = ex_part.center.DistanceTo(part_center) if dist < global_tolerance: return True elif dist < ex_part.dim + part.dim: self.possible_collisions.append(coll_count) coll_count += 1 ## collision check if self.coll_check == True: part_collider = part.transform_collider(trans) if part_collider.check_collisions_by_id(self.aggregated_parts, self.possible_collisions): return True return False ## additional collider check def additional_collider_check(self, part, trans): if part.add_collider != None: add_collider = part.add_collider.transform(trans, transform_connections=True, maintain_valid = False) if add_collider.check_collisions_w_parts(self.aggregated_parts): return True ## assign computed valid connections according to collider location part.add_collider.valid_connections = list(add_collider.valid_connections) return False ## support check def missing_supports_check(self, part, trans): if len(part.supports) > 0: for sup in part.supports: supports_count = 0 sup_trans = sup.transform(trans) for dir in sup_trans.sup_dir: for id in self.possible_collisions: if self.aggregated_parts[id].collider.check_intersection_w_line(dir): supports_count += 1 break if supports_count == len(sup_trans.sup_dir): return False return True else: return False ## global constraints check def global_constraints_check(self, part, trans): for constraint in self.global_constraints: part_center = part.transform_center(trans) if constraint.soft: if constraint.check(pt = part_center) == False: return True else: part_collider = part.transform_collider(trans) if constraint.check(pt = part_center, collider = part_collider) == False: return True return False #### aggregation methods #### ## sequential aggregation with Graph Grammar def aggregate_sequence(self, graph_rules): for rule in graph_rules: ## first part if len(self.aggregated_parts) == 0: aggr_rule = rule.split(">")[0] rule_parts = aggr_rule.split("_") part1 = str(rule_parts[0].split("|")[0]) conn1 = int(rule_parts[0].split("|")[1]) part2 = str(rule_parts[1].split("|")[0]) conn2 = int(rule_parts[1].split("|")[1]) rule_ids = rule.split(">")[1].split("_") first_part = self.parts[part1] first_part_trans = first_part.transform(rg.Transform.Identity) first_part_trans.id = rule_ids[0] next_part = self.parts[part2] orientTransform = rg.Transform.PlaneToPlane(next_part.connections[conn2].flip_pln, first_part.connections[conn1].pln) next_part_trans = next_part.transform(orientTransform) next_part_trans.id = rule_ids[1] ## check additional collider (for fabrication constraints) self.additional_collider_check(next_part, orientTransform) ## parent-child tracking first_part_trans.children.append(next_part_trans) next_part_trans.parent = first_part_trans self.aggregated_parts.append(first_part_trans) self.aggregated_parts.append(next_part_trans) first_part_trans.children.append(next_part_trans) else: aggr_rule = rule.split(">")[0] rule_parts = aggr_rule.split("_") part1_id = str(rule_parts[0].split("|")[0]) conn1 = int(rule_parts[0].split("|")[1]) part2 = str(rule_parts[1].split("|")[0]) conn2 = int(rule_parts[1].split("|")[1]) rule_ids = rule.split(">")[1].split("_") first_part = None for part in self.aggregated_parts: if part.id == part1_id: first_part = part break if first_part is not None: first_part.id = rule_ids[0] next_part = self.parts[part2] orientTransform = rg.Transform.PlaneToPlane(next_part.connections[conn2].flip_pln, first_part.connections[conn1].pln) next_part_trans = next_part.transform(orientTransform) next_part_trans.id = rule_ids[1] ## parent-child tracking first_part.children.append(next_part_trans.id) next_part_trans.parent = first_part.id self.aggregated_parts.append(next_part_trans) else: pass ## implement error handling ## stochastic aggregation def aggregate_rnd(self, num, use_catalog = False): added = 0 loops = 0 while added < num: loops += 1 if loops > num*100: break ## if no part is present in the aggregation, add first random part if len(self.aggregated_parts) == 0: first_part = None if use_catalog: first_part = self.parts[self.catalog.return_weighted_part()] else: first_part = self.parts[random.choice(self.parts.keys())] if first_part is not None: first_part_trans = first_part.transform(rg.Transform.Identity) for conn in first_part_trans.connections: conn.generate_rules_table(self.rules) first_part_trans.id = 0 self.aggregated_parts.append(first_part_trans) added += 1 if use_catalog: self.catalog.update(first_part_trans.name, -1) ## otherwise add new random part else: next_rule = None part_01_id = -1 conn_01_id = -1 next_rule_id = -1 new_rule_attempts = 0 while new_rule_attempts < 1000: new_rule_attempts += 1 next_rule = None if use_catalog: if self.catalog.is_empty: break next_part = self.parts[self.catalog.return_weighted_part()] if next_part is not None: part_01_id = random.randint(0,len(self.aggregated_parts)-1) part_01 = self.aggregated_parts[part_01_id] if len(part_01.active_connections) > 0: conn_01_id = part_01.active_connections[random.randint(0, len(part_01.active_connections)-1)] conn_01 = part_01.connections[conn_01_id] if len(conn_01.active_rules) > 0: next_rule_id = conn_01.active_rules[random.randint(0, len(conn_01.active_rules)-1)] next_rule = conn_01.rules_table[next_rule_id] if next_rule.part2 == next_part.name: break else: part_01_id = random.randint(0,len(self.aggregated_parts)-1) part_01 = self.aggregated_parts[part_01_id] if len(part_01.active_connections) > 0: conn_01_id = part_01.active_connections[random.randint(0, len(part_01.active_connections)-1)] conn_01 = part_01.connections[conn_01_id] if len(conn_01.active_rules) > 0: next_rule_id = conn_01.active_rules[random.randint(0, len(conn_01.active_rules)-1)] next_rule = conn_01.rules_table[next_rule_id] break if next_rule is not None: next_part = self.parts[next_rule.part2] orientTransform = rg.Transform.PlaneToPlane(next_part.connections[next_rule.conn2].flip_pln, conn_01.pln) ## boolean checks for all constraints coll_check = False add_coll_check = False valid_connections = [] missing_sup_check = False global_const_check = False ## collision check self.possible_collisions = [] coll_check = self.collision_check(next_part, orientTransform) ## constraints check if self.mode == 1: ## only local constraints mode if coll_check == False and next_part.is_constrained: add_coll_check = self.additional_collider_check(next_part, orientTransform) if add_coll_check == False: missing_sup_check = self.missing_supports_check(next_part, orientTransform) elif self.mode == 2: ## onyl global constraints mode if coll_check == False and len(self.global_constraints) > 0: global_const_check = self.global_constraints_check(next_part, orientTransform) elif self.mode == 3: ## local+global constraints mode if coll_check == False: if len(self.global_constraints) > 0: global_const_check = self.global_constraints_check(next_part, orientTransform) if global_const_check == False and next_part.is_constrained: add_coll_check = self.additional_collider_check(next_part, orientTransform) if add_coll_check == False: missing_sup_check = self.missing_supports_check(next_part, orientTransform) if coll_check == False and add_coll_check == False and missing_sup_check == False and global_const_check == False: next_part_trans = next_part.transform(orientTransform) next_part_trans.reset_part(self.rules) for i in range(len(next_part_trans.active_connections)): if next_part_trans.active_connections[i] == next_rule.conn2: next_part_trans.active_connections.pop(i) break next_part_trans.id = len(self.aggregated_parts) ## parent-child tracking self.aggregated_parts[part_01_id].children.append(next_part_trans.id) next_part_trans.parent = self.aggregated_parts[part_01_id].id self.aggregated_parts.append(next_part_trans) if use_catalog: self.catalog.update(next_part_trans.name, -1) for i in range(len(self.aggregated_parts[part_01_id].active_connections)): if self.aggregated_parts[part_01_id].active_connections[i] == conn_01_id: self.aggregated_parts[part_01_id].active_connections.pop(i) break added += 1 ## TO FIX --> do not remove rules when only caused by missing supports else: ## remove rules if they cause collisions or overlappings for i in range(len(self.aggregated_parts[part_01_id].connections[conn_01_id].active_rules)): if self.aggregated_parts[part_01_id].connections[conn_01_id].active_rules[i] == next_rule_id: self.aggregated_parts[part_01_id].connections[conn_01_id].active_rules.pop(i) break ## check if the connection is still active (still active rules available) if len(self.aggregated_parts[part_01_id].connections[conn_01_id].active_rules) == 0: for i in range(len(self.aggregated_parts[part_01_id].active_connections)): if self.aggregated_parts[part_01_id].active_connections[i] == conn_01_id: self.aggregated_parts[part_01_id].active_connections.pop(i) break else: ## if no part is available, exit the aggregation routine and return an error message msg = "Could not place " + str(num-added) + " parts" return msg ## stochastic aggregation with catalog def aggregate_rnd_catalog(self, catalog, num = None): added = 0 loops = 0 if num is None: num = catalog.parts_total while added < num: loops += 1 if loops > num*100: break ## if no part is present in the aggregation, add first random part if len(self.aggregated_parts) == 0: first_part = self.parts[catalog.return_weighted_part()] first_part_trans = first_part.transform(rg.Transform.Identity) for conn in first_part_trans.connections: conn.generate_rules_table(self.rules) first_part_trans.id = 0 self.aggregated_parts.append(first_part_trans) catalog.update(first_part.name, -1) added += 1 ## otherwise add new random part else: next_rule = None part_01_id = -1 conn_01_id = -1 next_rule_id = -1 new_rule_attempts = 0 while new_rule_attempts < 10000: new_rule_attempts += 1 selected_part = catalog.return_weighted_part() if selected_part is None or catalog.is_empty == True: break if len(part_01.active_connections) > 0: conn_01_id = part_01.active_connections[random.randint(0, len(part_01.active_connections)-1)] conn_01 = part_01.connections[conn_01_id] if len(conn_01.active_rules) > 0: next_rule_id = conn_01.active_rules[random.randint(0, len(conn_01.active_rules)-1)] if conn_01.rules_table[next_rule_id].part2 == selected_part: next_rule = conn_01.rules_table[next_rule_id] break if next_rule is not None: next_part = self.parts[next_rule.part2] orientTransform = rg.Transform.PlaneToPlane(next_part.connections[next_rule.conn2].flip_pln, conn_01.pln) ## boolean checks for all constraints coll_check = False add_coll_check = False valid_connections = [] missing_sup_check = False global_const_check = False ## collision check self.possible_collisions = [] coll_check = self.collision_check(next_part, orientTransform) ## constraints check if self.mode == 1: ## only local constraints mode if coll_check == False and next_part.is_constrained: add_coll_check = self.additional_collider_check(next_part, orientTransform) if add_coll_check == False: missing_sup_check = self.missing_supports_check(next_part, orientTransform) elif self.mode == 2: ## onyl global constraints mode if coll_check == False and len(self.global_constraints) > 0: global_const_check = self.global_constraints_check(next_part, orientTransform) elif self.mode == 3: ## local+global constraints mode if coll_check == False: if len(self.global_constraints) > 0: global_const_check = self.global_constraints_check(next_part, orientTransform) if global_const_check == False and next_part.is_constrained: add_coll_check = self.additional_collider_check(next_part, orientTransform) if add_coll_check == False: missing_sup_check = self.missing_supports_check(next_part, orientTransform) if coll_check == False and add_coll_check == False and missing_sup_check == False and global_const_check == False: next_part_trans = next_part.transform(orientTransform) next_part_trans.reset_part(self.rules) for i in range(len(next_part_trans.active_connections)): if next_part_trans.active_connections[i] == next_rule.conn2: next_part_trans.active_connections.pop(i) break next_part_trans.id = len(self.aggregated_parts) ## parent-child tracking self.aggregated_parts[part_01_id].children.append(next_part_trans.id) next_part_trans.parent = self.aggregated_parts[part_01_id].id self.aggregated_parts.append(next_part_trans) catalog.update(next_part_trans.name, -1) for i in range(len(self.aggregated_parts[part_01_id].active_connections)): if self.aggregated_parts[part_01_id].active_connections[i] == conn_01_id: self.aggregated_parts[part_01_id].active_connections.pop(i) break added += 1 ## TO FIX --> do not remove rules when only caused by missing supports else: ## remove rules if they cause collisions or overlappings for i in range(len(self.aggregated_parts[part_01_id].connections[conn_01_id].active_rules)): if self.aggregated_parts[part_01_id].connections[conn_01_id].active_rules[i] == next_rule_id: self.aggregated_parts[part_01_id].connections[conn_01_id].active_rules.pop(i) break ## check if the connection is still active (still active rules available) if len(self.aggregated_parts[part_01_id].connections[conn_01_id].active_rules) == 0: for i in range(len(self.aggregated_parts[part_01_id].active_connections)): if self.aggregated_parts[part_01_id].active_connections[i] == conn_01_id: self.aggregated_parts[part_01_id].active_connections.pop(i) break else: ## if no part is available, exit the aggregation routine and return an error message msg = "Could not place " + str(num-added) + " parts" return msg ## compute all possibilities for child-parts of the given part, and store them in the aggregation queue def compute_next_w_field(self, part): for i in xrange(len(part.active_connections)-1, -1, -1): conn_id = part.active_connections[i] conn = part.connections[conn_id] for i2 in xrange(len(conn.active_rules)-1, -1, -1): rule_id = conn.active_rules[i2] rule = conn.rules_table[rule_id] next_part = self.parts[rule.part2] next_center = rg.Point3d(next_part.center) orientTransform = rg.Transform.PlaneToPlane(next_part.connections[rule.conn2].flip_pln, conn.pln) next_center.Transform(orientTransform) if self.multiple_fields: f_name = next_part.field if self.field[f_name].bbox.Contains(next_center) == True: field_val = self.field[f_name].return_pt_val(next_center) queue_index = bisect.bisect_left(self.queue_values, field_val) queue_entry = (next_part.name, part.id, orientTransform) self.queue_values.insert(queue_index, field_val) self.aggregation_queue.insert(queue_index, queue_entry) self.queue_count += 1 else: if self.field.bbox.Contains(next_center) == True: field_val = self.field.return_pt_val(next_center) queue_index = bisect.bisect_left(self.queue_values, field_val) queue_entry = (next_part.name, part.id, orientTransform) self.queue_values.insert(queue_index, field_val) self.aggregation_queue.insert(queue_index, queue_entry) self.queue_count += 1 ## field-driven aggregation def aggregate_field(self, num): added = 0 loops = 0 while added < num: ## avoid endless loops loops += 1 if loops > num*100: break ## if no part is present in the aggregation, add first random part if len(self.aggregated_parts) == 0 and self.prev_num == 0: first_part = self.parts[random.choice(self.parts.keys())] start_point = None if self.multiple_fields: f_name = first_part.field if (self.mode == 2 or self.mode == 3) and len(self.global_constraints) > 0: start_point = self.field[f_name].return_highest_pt(constraints=self.global_constraints) else: start_point = self.field[f_name].return_highest_pt() else: if (self.mode == 2 or self.mode == 3) and len(self.global_constraints) > 0: start_point = self.field.return_highest_pt(constraints=self.global_constraints) else: start_point = self.field.return_highest_pt() mov_vec = rg.Vector3d.Subtract(rg.Vector3d(start_point), rg.Vector3d(first_part.center)) move_transform = rg.Transform.Translation(mov_vec.X, mov_vec.Y, mov_vec.Z) first_part_trans = first_part.transform(move_transform) for conn in first_part_trans.connections: conn.generate_rules_table(self.rules) first_part_trans.id = 0 self.aggregated_parts.append(first_part_trans) ## compute all possible next parts and append to list self.compute_next_w_field(first_part_trans) added += 1 else: ## if no part is available, exit the aggregation routine and return an error message if self.queue_count == 0: msg = "Could not place " + str(num-added) + " parts" return msg next_data = self.aggregation_queue[self.queue_count-1] next_part = self.parts[next_data[0]] next_center = rg.Point3d(next_part.center) orientTransform = next_data[2] ## boolean checks for all constraints coll_check = False add_coll_check = False missing_sup_check = False global_const_check = False ## collision check self.possible_collisions = [] coll_check = self.collision_check(next_part, orientTransform) ## constraints check if self.mode == 1: ## only local constraints mode if coll_check == False and next_part.is_constrained: add_coll_check = self.additional_collider_check(next_part, orientTransform) if add_coll_check == False: missing_sup_check = self.missing_supports_check(next_part, orientTransform) elif self.mode == 2: ## onyl global constraints mode if coll_check == False and len(self.global_constraints) > 0: global_const_check = self.global_constraints_check(next_part, orientTransform) elif self.mode == 3: ## local+global constraints mode if coll_check == False: if len(self.global_constraints) > 0: global_const_check = self.global_constraints_check(next_part, orientTransform) if global_const_check == False and next_part.is_constrained: add_coll_check = self.additional_collider_check(next_part, orientTransform) if add_coll_check == False: missing_sup_check = self.missing_supports_check(next_part, orientTransform) if coll_check == False and add_coll_check == False and missing_sup_check == False and global_const_check == False: next_part_trans = next_part.transform(orientTransform) next_part_trans.reset_part(self.rules) for conn in next_part_trans.connections: conn.generate_rules_table(self.rules) next_part_trans.id = len(self.aggregated_parts) self.aggregated_parts[next_data[1]].children.append(next_part_trans.id) next_part_trans.parent = self.aggregated_parts[next_data[1]].id self.aggregated_parts.append(next_part_trans) ## compute all possible next parts and append to list self.compute_next_w_field(next_part_trans) added += 1 self.aggregation_queue.pop() self.queue_values.pop() self.queue_count -=1 #################################################################### Plane Constraint #################################################################### class Plane_Constraint(object): ## constructor def __init__(self, _plane, _positive = True, _soft = True): self.type = 'plane' self.plane = _plane self.positive = _positive self.soft = _soft ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspPlaneConst [+: %s, soft: %s]" % (self.positive, self.soft) ## constraint check method def check(self, pt = None, collider = None): if self.soft: return self.check_soft(pt) else: return self.check_hard(pt, collider) ## hard constraint check method def check_hard(self, pt, collider): if self.check_soft(pt): for geo in collider.geometry: if rg.Intersect.Intersection.MeshPlane(geo, self.plane) is not None: return False return True else: return False ## soft constraint check method def check_soft(self, pt): mapped_pt = self.plane.RemapToPlaneSpace(pt)[1] if self.positive: if mapped_pt.Z > 0: return True else: if mapped_pt.Z < 0: return True return False #################################################################### Mesh Constraint #################################################################### class Mesh_Constraint(object): ## constructor def __init__(self, _geo, _inside = True, _soft = True): self.type = 'mesh_collider' self.geo = _geo self.inside = _inside self.soft = _soft ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspMeshConst [in: %s, soft: %s]" % (self.inside, self.soft) ## constraint check method def check(self, pt = None, collider = None): if self.soft: return self.check_soft(pt) else: return self.check_hard(pt, collider) ## hard constraint check method def check_hard(self, pt, collider): if self.check_soft(pt): for geo in collider.geometry: if len(rg.Intersect.Intersection.MeshMeshFast(self.geo, geo)) > 0: return False return True else: return False ## soft constraint check method def check_soft(self, pt): is_inside = self.geo.IsPointInside(pt, global_tolerance, False) if self.inside: if is_inside: return True else: if not is_inside: return True return False ######################################################################### ## WIP ## ######################################################################### #################################################################### Collider #################################################################### class Collider(object): ## constructor def __init__(self, _geo, _multiple=False, _check_all = False, _connections=[], _valid_connections = []): self.geometry = _geo self.multiple = _multiple self.check_all = _check_all self.connections = _connections self.valid_connections = _valid_connections self.set_connections = False if len(self.connections) == len(self.geometry) and self.multiple == True: self.set_connections = True ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspCollider" ## return a transformed copy of the collider ########################################################################### check if valid connections need to be transformed or re-generated!!! def transform(self, trans, transform_connections = False, maintain_valid = False): geometry_trans = [] for geo in self.geometry: geo_trans = geo.Duplicate() geo_trans.Transform(trans) geometry_trans.append(geo_trans) connections_trans = [] if transform_connections: for conn in self.connections: connections_trans.append(conn.transform(trans)) if maintain_valid: valid_connection_trans = list(self.valid_connections) coll_trans = Collider(geometry_trans, _multiple=self.multiple, _check_all=self.check_all, _connections=connections_trans, _valid_connections=valid_connection_trans) else: coll_trans = Collider(geometry_trans, _multiple=self.multiple, _check_all=self.check_all, _connections=connections_trans) return coll_trans ## return a copy of the collider def copy(self): geometry_copy = [] for geo in self.geometry: geo_copy = geo.Duplicate() geometry_copy.append(geo_copy) connections_copy = [] for conn in self.connections: connections_copy.append(conn.copy()) valid_connection_copy = list(self.valid_connections) coll_copy = Collider(geometry_copy, _multiple=self.multiple, _check_all=self.check_all, _connections=connections_copy, _valid_connections=valid_connection_copy) return coll_copy ## check collisions between collider and given part def check_collisions_w_parts(self, parts): ## multiple collider with associated connections if self.multiple: valid_colliders = [] self.valid_connections = [] count = 0 for geo in self.geometry: valid_coll = True for part in parts: for other_geo in part.collider.geometry: if len(rg.Intersect.Intersection.MeshMeshFast(geo, other_geo)) > 0: valid_coll = False break if valid_coll == False: break valid_colliders.append(valid_coll) if self.set_connections and valid_coll: self.valid_connections.append(count) if valid_coll and self.check_all == False: break count+=1 if True in valid_colliders: return False return True ## simple collider else: for geo in self.geometry: for part in parts: for other_geo in part.collider.geometry: if len(rg.Intersect.Intersection.MeshMeshFast(geo, other_geo)) > 0: return True return False ## check collisions between collider and given ids in the given parts list def check_collisions_by_id(self, parts, ids): ## multiple collider with associated connections if self.multiple: valid_colliders = [] count = 0 for geo in self.geometry: valid_coll = True for id in ids: for other_geo in parts[id].collider.geometry: if len(rg.Intersect.Intersection.MeshMeshFast(geo, other_geo)) > 0: valid_coll = False break valid_colliders.append(valid_coll) if valid_coll and self.check_all == False: break count+=1 if True in valid_colliders: return False return True ## simple collider else: for geo in self.geometry: for id in ids: for other_geo in parts[id].collider.geometry: if len(rg.Intersect.Intersection.MeshMeshFast(geo, other_geo)) > 0: return True return False ## check intersection between collider and line (for supports check) def check_intersection_w_line(self, ln): for geo in self.geometry: if len(rg.Intersect.Intersection.MeshLine(geo, ln)[0]) > 0: return True return False #### WIP #### def check_global_constraints(self, constraint): return False ################################################################# Parts Catalog ################################################################## class PartCatalog(object): ##constructor def __init__(self, _parts, _amounts): self.parts = _parts self.amounts = _amounts self.dict = {} for i in xrange(len(self.parts)): self.dict[self.parts[i].name] = _amounts[i] self.is_empty = False self.parts_total = sum(self.dict.values()) ## return a random part type def return_random_part(self): choices = [key for key in self.dict.keys() if self.dict[key] > 0] if len(choices) > 0: return random.choice(choices) else: self.is_empty = True return None ## return a weighted-choice between the available parts, give the available parts amounts def return_weighted_part(self): if self.parts_total == 0: self.is_empty = True return None n = random.uniform(0, self.parts_total) for key in self.dict: if n < self.dict[key]: return key n = n - self.dict[key] return None def update(self, part_name, difference): self.dict[part_name] += difference self.parts_total = sum(self.dict.values()) if self.parts_total == 0: self.is_empty = True def copy(self): amounts = [self.dict[part.name] for part in self.parts] return PartCatalog(self.parts, amounts)
gpl-3.0
6,861,793,551,399,903,000
33.263757
210
0.642644
false
3.278659
false
false
false
NCPlayz/CassBotPy
cassandra/bot.py
1
4014
import datetime import json import discord import os from discord.ext import commands from discord.ext.commands.converter import * class CassandraContext(commands.Context): def is_float(self, argument): """Checks if the argument is a float.""" try: return float(string) # True if string is a number contains a dot except ValueError: # String is not a number return False async def send(self, content=None, *args, **kwargs): """Override for send to add message filtering""" if content: if self.is_float(content) or content.isdigit(): content = str(content) content.replace("@everyone", "@\u200beveryone").replace("@here", "@\u200bhere") sent_message = await super().send(content, *args, **kwargs) return sent_message @property def session(self): """Returns the aiohttp.ClientSession() instance in CassandraBase.""" return self.bot.session class CassandraBase(commands.Bot): """This is the class that initializes the bot.""" def __init__(self): self.token = os.environ['TOKEN'] self.presence = discord.Game(name='in a Digital Haunt...', url="https://www.twitch.tv/ghostofsparkles", type=1) self.archive_file = [] def get_package_info(): """Fetches `arg` in `package.json`.""" with open("./package.json") as f: config = json.load(f) return config def get_prefix(): """Fetches all known prefixes.""" prefixes = ["-", "Cassandra "] return commands.when_mentioned_or(*prefixes) def get_description(): """Fetches description.""" return f"{get_package_info()['name']}" def get_game(): """Fetches game presence.""" return self.presence super().__init__(command_prefix=get_prefix(), game=get_game(), description=get_description(), pm_help=None, help_attrs=dict(hidden=True)) startup_extensions = [] for file in os.listdir("./cogs"): if file.endswith(".py"): startup_extensions.append(file.replace('.py', '')) for extension in startup_extensions: try: self.load_extension(f'cogs.{extension}') print(f'Loaded {extension}') except Exception as e: error = f'{extension}\n {type(e).__name__}: {e}' print(f'Failed to load extension {error}') self.session = None def run(self): """Runs the bot.""" super().run(self.token) async def on_message(self, message): """An event triggered when a message is sent.""" ctx = await self.get_context(message, cls=CassandraContext) await self.invoke(ctx) async def fetch(self, url: str, headers: dict = None, timeout: float = None, return_type: str = None, **kwargs): """Fetches data from a url via aiohttp.""" async with self.session.get(url, headers=headers, timeout=timeout, **kwargs) as resp: if return_type: cont = getattr(resp, return_type) return resp, await cont() else: return resp, None class Cassandra(CassandraBase): pass class ConvertError(Exception): pass class Union(Converter): def __init__(self, *converters): self.converters = converters async def convert(self, ctx: CassandraContext, argument: str): """Converts an argument""" for converter in self.converters: try: return await ctx.command.do_conversion(ctx, converter, argument) except: raise ConvertError('Conversion Failed.')
mit
-1,071,383,508,198,149,500
32.016949
115
0.550573
false
4.510112
false
false
false
Franky333/crazyflie-clients-python
src/cfclient/ui/tabs/LogTab.py
1
3242
#!/usr/bin/env python # -*- coding: utf-8 -*- # # || ____ _ __ # +------+ / __ )(_) /_______________ _____ ___ # | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \ # +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ # || || /_____/_/\__/\___/_/ \__,_/ /___/\___/ # # Copyright (C) 2011-2013 Bitcraze AB # # Crazyflie Nano Quadcopter Client # # 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 2 # 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, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. """ Shows the Log TOC of available variables in the Crazyflie. """ import cfclient from cfclient.ui.tab import Tab from PyQt5 import QtWidgets from PyQt5 import uic from PyQt5.QtCore import pyqtSignal from PyQt5.QtCore import pyqtSlot from PyQt5.QtCore import Qt __author__ = 'Bitcraze AB' __all__ = ['LogTab'] param_tab_class = uic.loadUiType(cfclient.module_path + "/ui/tabs/logTab.ui")[0] class LogTab(Tab, param_tab_class): connectedSignal = pyqtSignal(str) disconnectedSignal = pyqtSignal(str) def __init__(self, tabWidget, helper, *args): super(LogTab, self).__init__(*args) self.setupUi(self) self.tabName = "Log TOC" self.menuName = "Log TOC" self.helper = helper self.tabWidget = tabWidget self.cf = helper.cf # Init the tree widget self.logTree.setHeaderLabels(['Name', 'ID', 'Unpack', 'Storage']) self.cf.connected.add_callback(self.connectedSignal.emit) self.connectedSignal.connect(self.connected) # Clear the log TOC list when the Crazyflie is disconnected self.cf.disconnected.add_callback(self.disconnectedSignal.emit) self.disconnectedSignal.connect(self.disconnected) @pyqtSlot('QString') def disconnected(self, linkname): self.logTree.clear() @pyqtSlot(str) def connected(self, linkURI): self.logTree.clear() toc = self.cf.log.toc for group in list(toc.toc.keys()): groupItem = QtWidgets.QTreeWidgetItem() groupItem.setData(0, Qt.DisplayRole, group) for param in list(toc.toc[group].keys()): item = QtWidgets.QTreeWidgetItem() item.setData(0, Qt.DisplayRole, param) item.setData(1, Qt.DisplayRole, toc.toc[group][param].ident) item.setData(2, Qt.DisplayRole, toc.toc[group][param].pytype) item.setData(3, Qt.DisplayRole, toc.toc[group][param].ctype) groupItem.addChild(item) self.logTree.addTopLevelItem(groupItem) self.logTree.expandItem(groupItem)
gpl-2.0
3,133,045,078,609,853,000
33.860215
79
0.614436
false
3.512459
false
false
false
spinningbytes/deep-mlsa
code/architectures/default_cnn.py
1
2153
import logging from keras.layers import Dense, ZeroPadding1D, Embedding, Convolution1D, MaxPooling1D, Flatten, Input from keras.models import Model from utils.data_utils import load_embedding_matrix def create_default_model(config_data): nb_filter = 200 filter_length = 6 hidden_dims = nb_filter embedding_matrix = load_embedding_matrix(config_data) max_features = embedding_matrix.shape[0] embedding_dims = embedding_matrix.shape[1] max_len = config_data['max_sentence_length'] logging.info('Build Model...') logging.info('Embedding Dimensions: ({},{})'.format(max_features, embedding_dims)) main_input = Input(batch_shape=(None, max_len), dtype='int32', name='main_input') if not config_data.get('random_embedding', None): logging.info('Pretrained Word Embeddings') embeddings = Embedding( max_features, embedding_dims, input_length=max_len, weights=[embedding_matrix], trainable=False )(main_input) else: logging.info('Random Word Embeddings') embeddings = Embedding(max_features, embedding_dims, init='lecun_uniform', input_length=max_len)(main_input) zeropadding = ZeroPadding1D(filter_length - 1)(embeddings) conv1 = Convolution1D( nb_filter=nb_filter, filter_length=filter_length, border_mode='valid', activation='relu', subsample_length=1)(zeropadding) max_pooling1 = MaxPooling1D(pool_length=4, stride=2)(conv1) conv2 = Convolution1D( nb_filter=nb_filter, filter_length=filter_length, border_mode='valid', activation='relu', subsample_length=1)(max_pooling1) max_pooling2 = MaxPooling1D(pool_length=conv2._keras_shape[1])(conv2) flatten = Flatten()(max_pooling2) hidden = Dense(hidden_dims)(flatten) softmax_layer1 = Dense(3, activation='softmax', name='sentiment_softmax', init='lecun_uniform')(hidden) model = Model(input=[main_input], output=softmax_layer1) test_model = Model(input=[main_input], output=[softmax_layer1, hidden]) return model, test_model
apache-2.0
6,691,548,136,573,418,000
33.190476
116
0.668834
false
3.649153
false
false
false
openstack/networking-plumgrid
networking_plumgrid/neutron/tests/unit/extensions/test_providernet.py
1
2158
# Copyright 2015 PLUMgrid, Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. # """ PLUMgrid plugin provider network extension unit tests """ import mock from oslo_utils import importutils from networking_plumgrid.neutron.plugins import plugin as plumgrid_plugin from neutron.tests.unit.extensions import test_providernet as pnet PLUM_DRIVER = ('networking_plumgrid.neutron.plugins.drivers.' 'fake_plumlib.Plumlib') FAKE_DIRECTOR = '1.1.1.1' FAKE_PORT = '1234' FAKE_USERNAME = 'fake_admin' FAKE_PASSWORD = 'fake_password' FAKE_TIMEOUT = '0' class ProviderNetworksTestCase(pnet.ProvidernetExtensionTestCase): _plugin_name = ('networking_plumgrid.neutron.plugins.' 'plugin.NeutronPluginPLUMgridV2') def setUp(self): def mocked_plumlib_init(self): director_plumgrid = FAKE_DIRECTOR director_port = FAKE_PORT director_username = FAKE_USERNAME director_password = FAKE_PASSWORD timeout = FAKE_TIMEOUT self._plumlib = importutils.import_object(PLUM_DRIVER) self._plumlib.director_conn(director_plumgrid, director_port, timeout, director_username, director_password) with mock.patch.object(plumgrid_plugin.NeutronPluginPLUMgridV2, 'plumgrid_init', new=mocked_plumlib_init): super(ProviderNetworksTestCase, self).setUp() def tearDown(self): super(ProviderNetworksTestCase, self).tearDown()
apache-2.0
-443,667,551,993,928,500
36.859649
78
0.659407
false
4.026119
true
false
false
rndusr/stig
stig/client/filters/base.py
1
22315
# 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 # http://www.gnu.org/licenses/gpl-3.0.txt import itertools import operator import re from collections import abc from ...utils import cliparser from ...logging import make_logger # isort:skip log = make_logger(__name__) BOOLEAN = 'boolean' COMPARATIVE = 'comparative' class BoolFilterSpec(): """Boolean filter specification""" type = BOOLEAN def __init__(self, func, *, needed_keys=(), aliases=(), description='No description'): if not func: self.filter_function = None needed_keys = () else: self.filter_function = func self.needed_keys = needed_keys self.aliases = aliases self.description = description class CmpFilterSpec(): """Comparative filter specification""" type = COMPARATIVE def __init__(self, *, value_type, value_getter=None, value_matcher=None, value_convert=None, as_bool=None, needed_keys=(), aliases=(), description='No description'): """ value_type : Subclass of `type` (i.e. something that returns an instance when called and can be passed to `isinstance` as the second argument value_getter : Callable that takes an item and returns one or more values to match against the user-provided value; Multiple values must be given as an iterator (list, tuple, generator, etc), and the item matches if any match value_convert : Callable that takes a value and converts it to something comparable (e.g. "42" (str) -> 42 (int)) value_matcher : Callable that takes (item, operator, value) and returns True/False as_bool : Callable that takes an item and returns True/False needed_keys : Needed keys for this filter aliases : Alternative names of this filter """ self.value_type = value_type self.needed_keys = needed_keys self.aliases = aliases self.description = description self.value_convert = value_convert if value_convert is not None else value_type if value_getter is not None: self.value_getter = value_getter elif len(self.needed_keys) == 1: self.value_getter = lambda dct, k=needed_keys[0]: dct[k] else: raise TypeError('Missing argument with needed_keys=%r: value_getter', self.needed_keys) if value_matcher is None: def value_matcher(item, op, user_value, vg=self.value_getter): item_value = vg(item) if isinstance(item_value, abc.Iterator): return any(op(ival, user_value) for ival in item_value) else: return op(item_value, user_value) self.value_matcher = value_matcher if as_bool is None: def as_bool(item, vg=self.value_getter): item_value = vg(item) if isinstance(item_value, abc.Iterator): return any(item_value) else: return bool(item_value) self.as_bool = as_bool def make_filter(self, operator, user_value, invert): if operator is None and user_value is None: # Abuse comparative filter as boolean filter # (e.g. 'peers-connected' matches torrents with peers-connected!=0) return (self.as_bool, self.needed_keys, invert) elif user_value is None: # Operator with no value matches everything return (None, (), False) else: def f(obj, vm=self.value_matcher, op=operator, val=user_value): return vm(obj, op, val) return (f, self.needed_keys, invert) class FilterSpecDict(abc.Mapping): """TODO""" _NOT_FOUND = object() def __init__(self, dct): self._dct = dct def __getitem__(self, key): value = self._dct.get(key, self._NOT_FOUND) if value is not self._NOT_FOUND: return value for value in self._dct.values(): if key in value.aliases: return value raise KeyError(key) def __iter__(self): return iter(self._dct) def __len__(self): return len(self._dct) def __repr__(self): return '%s(%r)' % (type(self).__name__, self._dct) class Filter(): """Match sequences of objects against a single filter""" OPERATORS = { '=' : operator.__eq__, '~' : operator.__contains__, '>' : operator.__gt__, '<' : operator.__lt__, '>=' : operator.__ge__, '<=' : operator.__le__, '=~' : lambda a, b: re.search(b, a), } INVERT_CHAR = '!' POSSIBLE_OPERATORS = tuple(itertools.chain.from_iterable((op, '!' + op) for op in OPERATORS)) DEFAULT_FILTER = None DEFAULT_OPERATOR = '~' BOOLEAN_FILTERS = {} COMPARATIVE_FILTERS = {} @classmethod def _resolve_alias(cls, name): """ Return real filter name or `name` if it does not resolve """ if not hasattr(cls, '_aliases'): aliases = {} for fspecs in (cls.BOOLEAN_FILTERS, cls.COMPARATIVE_FILTERS): for fname,f in fspecs.items(): for a in f.aliases: if a in aliases: raise RuntimeError('Multiple aliases: %r' % (a,)) else: aliases[a] = fname cls._aliases = aliases if name is None: name = '' return cls._aliases.get(name.strip(), name) @classmethod def _get_filter_spec(cls, name): """ Get filter spec by `name` Raise ValueError on error """ fspec = cls.BOOLEAN_FILTERS.get(name) if fspec is not None: return fspec fspec = cls.COMPARATIVE_FILTERS.get(name) if fspec is not None: return fspec if name: raise ValueError('Invalid filter name: %r' % (name,)) else: raise ValueError('No filter expression given') @classmethod def _make_filter(cls, name, op, user_value, invert): """ Return filter function, needed keys and invert Filter function takes a value and returns whether it matches `user_value`. Filter function and needed keys are both `None` if everything is matched. Raise ValueError on error """ # Ensure value is wanted by filter, compatible to operator and of proper type user_value = cls._validate_user_value(name, op, user_value) log.debug(' Validated user_value: %r', user_value) fspec = cls._get_filter_spec(name) if fspec.type is BOOLEAN: return (fspec.filter_function, fspec.needed_keys, invert) elif fspec.type is COMPARATIVE: return fspec.make_filter(cls.OPERATORS.get(op), user_value, invert) @classmethod def _validate_user_value(cls, name, op, user_value): """ Ensure that the `name`, `op`, and `user_value` make sense in conjunction Return user value as correct type (e.g. `int`) for filter `name` Raise ValueError if anything smells funky """ log.debug(' Validating user value: name=%r, op=%r, user_value=%r', name, op, user_value) if name in cls.BOOLEAN_FILTERS: # log.debug('%r is a valid boolean filter: %r', name, cls.BOOLEAN_FILTERS[name]) if user_value: raise ValueError('Boolean filter does not take a value: %s' % (name,)) elif op: raise ValueError('Boolean filter does not take an operator: %s' % (name,)) if op is None or user_value is None: # Filter `name` could still be (ab)used as boolean filter return None fspec = cls.COMPARATIVE_FILTERS.get(name) if fspec is None: if name: raise ValueError('Invalid filter name: %r' % (name,)) else: raise ValueError('No filter expression given') # Convert user_value to proper type if type(user_value) is not fspec.value_type: log.debug(' Converting %r to %r', user_value, fspec.value_type) try: user_value = fspec.value_convert(user_value) except ValueError: raise ValueError('Invalid value for filter %r: %r' % (name, user_value)) # In case of regex operator, compile user_value if op == '=~': try: user_value = re.compile(user_value) except re.error as e: raise ValueError('Invalid regular expression: %s: %s' % (str(e).capitalize(), user_value)) else: # Test if target_type supports operator try: log.debug('Trying %r(%r [%r], %r [%r])', cls.OPERATORS[op], user_value, type(user_value), user_value, type(user_value)) cls.OPERATORS[op](user_value, user_value) except TypeError: raise ValueError('Invalid operator for filter %r: %s' % (name, op)) return user_value @classmethod def _parse_inverter(cls, string, invert): if not string: return string, invert # Find INVERT_CHAR at start or end of string parts = cliparser.tokenize(string.strip(), delims=(cls.INVERT_CHAR,), escapes=('\\',), quotes=()) if cls.INVERT_CHAR in parts: if parts and parts[0] == cls.INVERT_CHAR: parts.pop(0) invert = not invert if parts and parts[-1] == cls.INVERT_CHAR: parts.pop(-1) invert = not invert return ''.join(parts), invert else: # Return string unchanged return string, invert def __init__(self, filter_str=''): # name: Name of filter (user-readable string) # invert: Whether to invert filter (bool) # op: Comparison operator as string (see OPERATORS) # user_value: User-given value that is matched against items # The *_raw variables contain original quotes and backslashes. name_raw, op_raw, user_value_raw, invert = (None, None, None, False) log.debug('Parsing %r', filter_str) parts = cliparser.tokenize(filter_str, maxdelims=1, delims=self.OPERATORS, escapes=('\\',)) log.debug('Parts: %r', parts) if len(parts) == 3: name_raw, op_raw, user_value_raw = parts elif len(parts) == 2: if parts[0] in self.OPERATORS: op_raw, user_value_raw = parts name_raw = self.DEFAULT_FILTER elif parts[1] in self.OPERATORS: name_raw, op_raw = parts else: raise ValueError('Malformed filter expression: %r' % (filter_str,)) elif len(parts) == 1: if parts[0] in self.OPERATORS: op_raw = parts[0] else: name_raw = parts[0] else: raise ValueError('Malformed filter expression: %r' % (filter_str,)) name_raw, invert = self._parse_inverter(name_raw, invert) log.debug('Parsed %r into raw: name=%r, invert=%r, op=%r, user_value=%r', filter_str, name_raw, invert, op_raw, user_value_raw) # Remove all special characters (backslashes, quotes) name, op, user_value = map(lambda x: None if x is None else cliparser.plaintext(x), (name_raw, op_raw, user_value_raw)) log.debug(' Plaintext: name=%r, invert=%r, op=%r, user_value=%r', name, invert, op, user_value) name = self._resolve_alias(name) log.debug(' Resolved alias: name=%r, op=%r, user_value=%r', name, op, user_value) if not name: name = self.DEFAULT_FILTER log.debug(' Falling back to default filter: %r', name) try: log.debug(' Getting filter spec: name=%r, op=%r, user_value=%r', name, op, user_value) # Get filter spec by `name` filter_func, needed_keys, invert = self._make_filter(name, op, user_value, invert) except ValueError: # Filter spec lookup failed if self.DEFAULT_FILTER and user_value is op is None: # No `user_value` or `op` given - use the first part of the # filter expression (normally the filter name) as `user_value` # for DEFAULT_FILTER. name, op, user_value = self.DEFAULT_FILTER, self.DEFAULT_OPERATOR, name log.debug(' Using name as value for default filter: name=%r, op=%r, user_value=%r', name, op, user_value) filter_func, needed_keys, invert = self._make_filter(name, op, user_value, invert) else: # No DEFAULT_FILTER is set, so we can't default to it raise log.debug(' Final filter: name=%r, invert=%r, op=%r, user_value=%r', name, invert, op, user_value) self._filter_func = filter_func self._needed_keys = needed_keys self._name, self._invert, self._op, self._user_value = name, invert, op, user_value self._hash = hash((name, invert, op, user_value)) def apply(self, objs, invert=False, key=None): """Yield matching objects or `key` of each matching object""" invert = self._invert ^ bool(invert) # xor is_wanted = self._filter_func if is_wanted is None: if invert: # This filter matches nothing yield from () else: # This filter matches everything if key is None: yield from objs else: for obj in objs: yield obj[key] else: if key is None: for obj in objs: if bool(is_wanted(obj)) ^ invert: yield obj else: for obj in objs: if bool(is_wanted(obj)) ^ invert: yield obj[key] def match(self, obj): """Return True if `obj` matches, False otherwise""" is_wanted = self._filter_func if is_wanted is None: # This filter matches everything/nothing return not self._invert else: return bool(is_wanted(obj)) ^ self._invert def __str__(self): if self._name is None: return self.DEFAULT_FILTER or '' elif self._op is None: return ('!' if self._invert else '') + self._name else: name = self._name if self._name != self.DEFAULT_FILTER else '' op = ('!' if self._invert else '') + self._op user_value = self._user_value if user_value is None: return name + op else: val = str(user_value) if val == '': val = "''" elif len(val) == 1: val = cliparser.escape(val, delims=(' ', '&', '|'), quotes=("'", '"')) else: val = cliparser.quote(val, delims=(' ', '&', '|'), quotes=("'", '"')) return name + op + val @property def needed_keys(self): return self._needed_keys @property def match_everything(self): return not self._filter_func @property def inverted(self): return self._invert def __eq__(self, other): if isinstance(other, type(self)): for attr in ('_name', '_user_value', '_invert', '_op'): if getattr(self, attr) != getattr(other, attr): return False return True else: return NotImplemented def __repr__(self): return '%s(%r)' % (type(self).__name__, str(self)) def __hash__(self): return self._hash # The filter specs are specified on the Filter subclasses in each module, but we # only want to export the classes derived from FilterChain, so this metalcass # grabs attributes that are missing from FilterChain from it's 'filterclass' # attribute. class _forward_attrs(type): def __getattr__(cls, name): attr = getattr(cls.filterclass, name) setattr(cls, name, attr) return attr class FilterChain(metaclass=_forward_attrs): """One or more filters combined with AND and OR operators""" filterclass = NotImplemented def __init__(self, filters=''): if not isinstance(self.filterclass, type) or not issubclass(self.filterclass, Filter): raise RuntimeError('Attribute "filterclass" must be set to a Filter subclass') if isinstance(filters, str): # Because str is also instance of abc.Sequence pass elif isinstance(filters, abc.Sequence) and all(isinstance(f, str) for f in filters): filters = '|'.join(filters) elif isinstance(filters, (type(self), self.filterclass)): filters = str(filters) elif not isinstance(filters, str): raise ValueError('Filters must be string or sequence of strings, not %s: %r' % (type(filters).__name__, filters)) self._filterchains = () # Split `filters` at boolean operators parts = cliparser.tokenize(filters, delims=('&', '|')) if len(parts) > 0 and parts[0]: if parts[0] in ('&', '|'): raise ValueError("Filter can't start with operator: %r" % (parts[0],)) elif parts[-1] in ('&', '|'): raise ValueError("Filter can't end with operator: %r" % (parts[-1],)) # The filter chain is represented by a tuple of tuples. Each inner # tuple combines filters with AND. The outer tuple combines the # inner tuples with OR. filters = [] ops = [] expect = 'filter' for i,part in enumerate(parts): if expect == 'filter': if part not in '&|': f = self.filterclass(part) if f.match_everything: # One catch-all filter is the same as no filters filters = [f] ops.clear() break else: filters.append(f) expect = 'operator' continue elif expect == 'operator' and part in '&|': if part in '&|': ops.append(part) expect = 'filter' continue raise ValueError('Consecutive operators: {!r}'.format(''.join(parts[i - 2 : i + 2]))) fchain = [[]] for filter,op in itertools.zip_longest(filters, ops): fchain[-1].append(filter) if op == '|': fchain.append([]) log.debug('Chained %r and %r to %r', filters, ops, fchain) self._filterchains = tuple(tuple(x) for x in fchain) def apply(self, objects): """Yield matching objects from iterable `objects`""" chains = self._filterchains if chains: for obj in objects: if any(all(f.match(obj) for f in AND_chain) for AND_chain in chains): yield obj else: yield from objects def match(self, obj): """Whether `obj` matches this filter chain""" # All filters in an AND_chain must match for the AND_chain to # match. At least one AND_chain must match. chains = self._filterchains if not chains: return True else: return any(all(f.match(obj) for f in AND_chain) for AND_chain in chains) @property def needed_keys(self): """The object keys needed for filtering""" keys = set() for chain in self._filterchains: for filter in chain: keys.update(filter.needed_keys) return tuple(keys) def __eq__(self, other): if not isinstance(other, type(self)): return NotImplemented else: # Compare sets because order doesn't matter (foo&bar|baz is the # same as baz|bar&foo). Use frozensets because sets are not # hashable. self_fc_sets = set(frozenset(x) for x in self._filterchains) other_fc_sets = set(frozenset(x) for x in other._filterchains) return self_fc_sets == other_fc_sets def __str__(self): if len(self._filterchains) < 1: return '' else: OR_chains = [] for AND_chain in self._filterchains: OR_chains.append('&'.join(str(f) for f in AND_chain)) return '|'.join(OR_chains) def __repr__(self): return '%s(%r)' % (type(self).__name__, str(self)) def __and__(self, other): cls = type(self) if not isinstance(other, cls): return NotImplemented else: return cls(str(self) + '&' + str(other)) def __or__(self, other): cls = type(self) if not isinstance(other, cls): return NotImplemented else: return cls(str(self) + '|' + str(other))
gpl-3.0
6,356,354,278,953,972,000
37.407917
106
0.540444
false
4.271631
false
false
false
sajeeshcs/nested_quota_final
nova/tests/unit/virt/libvirt/test_vif.py
1
48234
# Copyright 2012 Nicira, Inc # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import contextlib from lxml import etree import mock from oslo_concurrency import processutils from oslo_config import cfg from nova import exception from nova.network import linux_net from nova.network import model as network_model from nova import objects from nova.pci import utils as pci_utils from nova import test from nova import utils from nova.virt.libvirt import config as vconfig from nova.virt.libvirt import vif CONF = cfg.CONF class LibvirtVifTestCase(test.NoDBTestCase): gateway_bridge_4 = network_model.IP(address='101.168.1.1', type='gateway') dns_bridge_4 = network_model.IP(address='8.8.8.8', type=None) ips_bridge_4 = [network_model.IP(address='101.168.1.9', type=None)] subnet_bridge_4 = network_model.Subnet(cidr='101.168.1.0/24', dns=[dns_bridge_4], gateway=gateway_bridge_4, routes=None, dhcp_server='191.168.1.1') gateway_bridge_6 = network_model.IP(address='101:1db9::1', type='gateway') subnet_bridge_6 = network_model.Subnet(cidr='101:1db9::/64', dns=None, gateway=gateway_bridge_6, ips=None, routes=None) network_bridge = network_model.Network(id='network-id-xxx-yyy-zzz', bridge='br0', label=None, subnets=[subnet_bridge_4, subnet_bridge_6], bridge_interface='eth0', vlan=99) vif_bridge = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_bridge, type=network_model.VIF_TYPE_BRIDGE, devname='tap-xxx-yyy-zzz', ovs_interfaceid=None) network_bridge_neutron = network_model.Network(id='network-id-xxx-yyy-zzz', bridge=None, label=None, subnets=[subnet_bridge_4, subnet_bridge_6], bridge_interface='eth0', vlan=99) vif_bridge_neutron = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_bridge_neutron, type=None, devname='tap-xxx-yyy-zzz', ovs_interfaceid='aaa-bbb-ccc') network_ovs = network_model.Network(id='network-id-xxx-yyy-zzz', bridge='br0', label=None, subnets=[subnet_bridge_4, subnet_bridge_6], bridge_interface=None, vlan=99) network_ivs = network_model.Network(id='network-id-xxx-yyy-zzz', bridge='br0', label=None, subnets=[subnet_bridge_4, subnet_bridge_6], bridge_interface=None, vlan=99) vif_ovs = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_ovs, type=network_model.VIF_TYPE_OVS, devname='tap-xxx-yyy-zzz', ovs_interfaceid='aaa-bbb-ccc') vif_ovs_hybrid = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_ovs, type=network_model.VIF_TYPE_OVS, details={'ovs_hybrid_plug': True, 'port_filter': True}, devname='tap-xxx-yyy-zzz', ovs_interfaceid='aaa-bbb-ccc') vif_ovs_filter_cap = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_ovs, type=network_model.VIF_TYPE_OVS, details={'port_filter': True}, devname='tap-xxx-yyy-zzz', ovs_interfaceid='aaa-bbb-ccc') vif_ovs_legacy = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_ovs, type=None, devname=None, ovs_interfaceid=None) vif_ivs = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_ivs, type=network_model.VIF_TYPE_IVS, devname='tap-xxx-yyy-zzz', ovs_interfaceid='aaa-bbb-ccc') vif_ivs_legacy = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_ovs, type=None, devname=None, ovs_interfaceid='aaa') vif_ivs_filter_direct = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_ivs, type=network_model.VIF_TYPE_IVS, details={'port_filter': True}, devname='tap-xxx-yyy-zzz', ovs_interfaceid='aaa-bbb-ccc') vif_ivs_filter_hybrid = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_ivs, type=network_model.VIF_TYPE_IVS, details={ 'port_filter': True, 'ovs_hybrid_plug': True}, devname='tap-xxx-yyy-zzz', ovs_interfaceid='aaa-bbb-ccc') vif_none = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_bridge, type=None, devname='tap-xxx-yyy-zzz', ovs_interfaceid=None) network_8021 = network_model.Network(id='network-id-xxx-yyy-zzz', bridge=None, label=None, subnets=[subnet_bridge_4, subnet_bridge_6], interface='eth0', vlan=99) vif_8021qbh = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_8021, type=network_model.VIF_TYPE_802_QBH, vnic_type=network_model.VNIC_TYPE_DIRECT, ovs_interfaceid=None, details={ network_model.VIF_DETAILS_PROFILEID: 'MyPortProfile'}, profile={'pci_vendor_info': '1137:0043', 'pci_slot': '0000:0a:00.1', 'physical_network': 'phynet1'}) vif_hw_veb = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_8021, type=network_model.VIF_TYPE_HW_VEB, vnic_type=network_model.VNIC_TYPE_DIRECT, ovs_interfaceid=None, details={ network_model.VIF_DETAILS_VLAN: '100'}, profile={'pci_vendor_info': '1137:0043', 'pci_slot': '0000:0a:00.1', 'physical_network': 'phynet1'}) vif_macvtap = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_8021, type=network_model.VIF_TYPE_HW_VEB, vnic_type=network_model.VNIC_TYPE_MACVTAP, ovs_interfaceid=None, details={ network_model.VIF_DETAILS_VLAN: '100'}, profile={'pci_vendor_info': '1137:0043', 'pci_slot': '0000:0a:00.1', 'physical_network': 'phynet1'}) vif_8021qbg = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_8021, type=network_model.VIF_TYPE_802_QBG, ovs_interfaceid=None, qbg_params=network_model.VIF8021QbgParams( managerid="xxx-yyy-zzz", typeid="aaa-bbb-ccc", typeidversion="1", instanceid="ddd-eee-fff")) network_mlnx = network_model.Network(id='network-id-xxx-yyy-zzz', label=None, bridge=None, subnets=[subnet_bridge_4, subnet_bridge_6], interface='eth0') network_midonet = network_model.Network(id='network-id-xxx-yyy-zzz', label=None, bridge=None, subnets=[subnet_bridge_4], interface='eth0') vif_mlnx = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_mlnx, type=network_model.VIF_TYPE_MLNX_DIRECT, devname='tap-xxx-yyy-zzz') vif_mlnx_net = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_mlnx, type=network_model.VIF_TYPE_MLNX_DIRECT, details={'physical_network': 'fake_phy_network'}, devname='tap-xxx-yyy-zzz') vif_midonet = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_midonet, type=network_model.VIF_TYPE_MIDONET, devname='tap-xxx-yyy-zzz') vif_iovisor = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=network_bridge, type=network_model.VIF_TYPE_IOVISOR, devname='tap-xxx-yyy-zzz', ovs_interfaceid=None) instance = { 'name': 'instance-name', 'uuid': 'instance-uuid' } bandwidth = { 'quota:vif_inbound_peak': '200', 'quota:vif_outbound_peak': '20', 'quota:vif_inbound_average': '100', 'quota:vif_outbound_average': '10', 'quota:vif_inbound_burst': '300', 'quota:vif_outbound_burst': '30' } def setUp(self): super(LibvirtVifTestCase, self).setUp() self.flags(allow_same_net_traffic=True) self.executes = [] def fake_execute(*cmd, **kwargs): self.executes.append(cmd) return None, None self.stubs.Set(utils, 'execute', fake_execute) def _get_node(self, xml): doc = etree.fromstring(xml) ret = doc.findall('./devices/interface') self.assertEqual(len(ret), 1) return ret[0] def _assertMacEquals(self, node, vif): mac = node.find("mac").get("address") self.assertEqual(mac, vif['address']) def _assertTypeEquals(self, node, type, attr, source, br_want, prefix=None): self.assertEqual(node.get("type"), type) br_name = node.find(attr).get(source) if prefix is None: self.assertEqual(br_name, br_want) else: self.assertTrue(br_name.startswith(prefix)) def _assertTypeAndMacEquals(self, node, type, attr, source, vif, br_want=None, size=0, prefix=None): ret = node.findall("filterref") self.assertEqual(len(ret), size) self._assertTypeEquals(node, type, attr, source, br_want, prefix) self._assertMacEquals(node, vif) def _assertModel(self, xml, model_want=None, driver_want=None): node = self._get_node(xml) if model_want is None: ret = node.findall("model") self.assertEqual(len(ret), 0) else: model = node.find("model").get("type") self.assertEqual(model, model_want) if driver_want is None: ret = node.findall("driver") self.assertEqual(len(ret), 0) else: driver = node.find("driver").get("name") self.assertEqual(driver, driver_want) def _assertTypeAndPciEquals(self, node, type, vif): self.assertEqual(node.get("type"), type) address = node.find("source").find("address") addr_type = address.get("type") self.assertEqual("pci", addr_type) pci_slot = "%(domain)s:%(bus)s:%(slot)s.%(func)s" % { 'domain': address.get("domain")[2:], 'bus': address.get("bus")[2:], 'slot': address.get("slot")[2:], 'func': address.get("function")[2:]} pci_slot_want = vif['profile']['pci_slot'] self.assertEqual(pci_slot, pci_slot_want) def _get_conf(self): conf = vconfig.LibvirtConfigGuest() conf.virt_type = "qemu" conf.name = "fake-name" conf.uuid = "fake-uuid" conf.memory = 100 * 1024 conf.vcpus = 4 return conf def _get_instance_xml(self, driver, vif, image_meta=None, flavor=None): if flavor is None: flavor = objects.Flavor(name='m1.small', memory_mb=128, vcpus=1, root_gb=0, ephemeral_gb=0, swap=0, extra_specs=dict(self.bandwidth), deleted_at=None, deleted=0, created_at=None, flavorid=1, is_public=True, vcpu_weight=None, id=2, disabled=False, rxtx_factor=1.0) conf = self._get_conf() nic = driver.get_config(self.instance, vif, image_meta, flavor, CONF.libvirt.virt_type) conf.add_device(nic) return conf.to_xml() def test_multiple_nics(self): conf = self._get_conf() # Tests multiple nic configuration and that target_dev is # set for each nics = [{'net_type': 'bridge', 'mac_addr': '00:00:00:00:00:0b', 'source_dev': 'b_source_dev', 'target_dev': 'b_target_dev'}, {'net_type': 'ethernet', 'mac_addr': '00:00:00:00:00:0e', 'source_dev': 'e_source_dev', 'target_dev': 'e_target_dev'}, {'net_type': 'direct', 'mac_addr': '00:00:00:00:00:0d', 'source_dev': 'd_source_dev', 'target_dev': 'd_target_dev'}] for nic in nics: nic_conf = vconfig.LibvirtConfigGuestInterface() nic_conf.net_type = nic['net_type'] nic_conf.target_dev = nic['target_dev'] nic_conf.mac_addr = nic['mac_addr'] nic_conf.source_dev = nic['source_dev'] conf.add_device(nic_conf) xml = conf.to_xml() doc = etree.fromstring(xml) for nic in nics: path = "./devices/interface/[@type='%s']" % nic['net_type'] node = doc.find(path) self.assertEqual(nic['net_type'], node.get("type")) self.assertEqual(nic['mac_addr'], node.find("mac").get("address")) self.assertEqual(nic['target_dev'], node.find("target").get("dev")) def test_model_novirtio(self): self.flags(use_virtio_for_bridges=False, virt_type='kvm', group='libvirt') d = vif.LibvirtGenericVIFDriver() xml = self._get_instance_xml(d, self.vif_bridge) self._assertModel(xml) def test_model_kvm(self): self.flags(use_virtio_for_bridges=True, virt_type='kvm', group='libvirt') d = vif.LibvirtGenericVIFDriver() xml = self._get_instance_xml(d, self.vif_bridge) self._assertModel(xml, network_model.VIF_MODEL_VIRTIO) def test_model_kvm_qemu_custom(self): for virt in ('kvm', 'qemu'): self.flags(use_virtio_for_bridges=True, virt_type=virt, group='libvirt') d = vif.LibvirtGenericVIFDriver() supported = (network_model.VIF_MODEL_NE2K_PCI, network_model.VIF_MODEL_PCNET, network_model.VIF_MODEL_RTL8139, network_model.VIF_MODEL_E1000, network_model.VIF_MODEL_SPAPR_VLAN) for model in supported: image_meta = {'properties': {'hw_vif_model': model}} xml = self._get_instance_xml(d, self.vif_bridge, image_meta) self._assertModel(xml, model) def test_model_kvm_bogus(self): self.flags(use_virtio_for_bridges=True, virt_type='kvm', group='libvirt') d = vif.LibvirtGenericVIFDriver() image_meta = {'properties': {'hw_vif_model': 'acme'}} self.assertRaises(exception.UnsupportedHardware, self._get_instance_xml, d, self.vif_bridge, image_meta) def _test_model_qemu(self, *vif_objs, **kw): libvirt_version = kw.get('libvirt_version') self.flags(use_virtio_for_bridges=True, virt_type='qemu', group='libvirt') for vif_obj in vif_objs: d = vif.LibvirtGenericVIFDriver() if libvirt_version is not None: d.libvirt_version = libvirt_version xml = self._get_instance_xml(d, vif_obj) doc = etree.fromstring(xml) bandwidth = doc.find('./devices/interface/bandwidth') self.assertNotEqual(bandwidth, None) inbound = bandwidth.find('inbound') self.assertEqual(inbound.get("average"), self.bandwidth['quota:vif_inbound_average']) self.assertEqual(inbound.get("peak"), self.bandwidth['quota:vif_inbound_peak']) self.assertEqual(inbound.get("burst"), self.bandwidth['quota:vif_inbound_burst']) outbound = bandwidth.find('outbound') self.assertEqual(outbound.get("average"), self.bandwidth['quota:vif_outbound_average']) self.assertEqual(outbound.get("peak"), self.bandwidth['quota:vif_outbound_peak']) self.assertEqual(outbound.get("burst"), self.bandwidth['quota:vif_outbound_burst']) self._assertModel(xml, network_model.VIF_MODEL_VIRTIO, "qemu") def test_model_qemu_no_firewall(self): self.flags(firewall_driver="nova.virt.firewall.NoopFirewallDriver") self._test_model_qemu( self.vif_bridge, self.vif_8021qbg, self.vif_iovisor, self.vif_mlnx, self.vif_ovs, ) def test_model_qemu_iptables(self): self.flags(firewall_driver="nova.virt.firewall.IptablesFirewallDriver") self._test_model_qemu( self.vif_bridge, self.vif_ovs, self.vif_ivs, self.vif_8021qbg, self.vif_iovisor, self.vif_mlnx, ) def test_model_xen(self): self.flags(use_virtio_for_bridges=True, virt_type='xen', group='libvirt') d = vif.LibvirtGenericVIFDriver() xml = self._get_instance_xml(d, self.vif_bridge) self._assertModel(xml) def test_generic_driver_none(self): d = vif.LibvirtGenericVIFDriver() self.assertRaises(exception.NovaException, self._get_instance_xml, d, self.vif_none) def _check_bridge_driver(self, d, vif, br_want): xml = self._get_instance_xml(d, vif) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "bridge", "source", "bridge", self.vif_bridge, br_want, 1) def test_generic_driver_bridge(self): d = vif.LibvirtGenericVIFDriver() self._check_bridge_driver(d, self.vif_bridge, self.vif_bridge['network']['bridge']) def _check_ivs_ethernet_driver(self, d, vif, dev_prefix): self.flags(firewall_driver="nova.virt.firewall.NoopFirewallDriver") xml = self._get_instance_xml(d, vif) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "ethernet", "target", "dev", self.vif_ivs, prefix=dev_prefix) script = node.find("script").get("path") self.assertEqual(script, "") def test_unplug_ivs_ethernet(self): d = vif.LibvirtGenericVIFDriver() with mock.patch.object(linux_net, 'delete_ivs_vif_port') as delete: delete.side_effect = processutils.ProcessExecutionError d.unplug_ivs_ethernet(None, self.vif_ovs) def test_plug_ovs_hybrid(self): calls = { 'device_exists': [mock.call('qbrvif-xxx-yyy'), mock.call('qvovif-xxx-yyy')], '_create_veth_pair': [mock.call('qvbvif-xxx-yyy', 'qvovif-xxx-yyy')], 'execute': [mock.call('brctl', 'addbr', 'qbrvif-xxx-yyy', run_as_root=True), mock.call('brctl', 'setfd', 'qbrvif-xxx-yyy', 0, run_as_root=True), mock.call('brctl', 'stp', 'qbrvif-xxx-yyy', 'off', run_as_root=True), mock.call('tee', ('/sys/class/net/qbrvif-xxx-yyy' '/bridge/multicast_snooping'), process_input='0', run_as_root=True, check_exit_code=[0, 1]), mock.call('ip', 'link', 'set', 'qbrvif-xxx-yyy', 'up', run_as_root=True), mock.call('brctl', 'addif', 'qbrvif-xxx-yyy', 'qvbvif-xxx-yyy', run_as_root=True)], 'create_ovs_vif_port': [mock.call('br0', 'qvovif-xxx-yyy', 'aaa-bbb-ccc', 'ca:fe:de:ad:be:ef', 'instance-uuid')] } with contextlib.nested( mock.patch.object(linux_net, 'device_exists', return_value=False), mock.patch.object(utils, 'execute'), mock.patch.object(linux_net, '_create_veth_pair'), mock.patch.object(linux_net, 'create_ovs_vif_port') ) as (device_exists, execute, _create_veth_pair, create_ovs_vif_port): d = vif.LibvirtGenericVIFDriver() d.plug_ovs_hybrid(self.instance, self.vif_ovs) device_exists.assert_has_calls(calls['device_exists']) _create_veth_pair.assert_has_calls(calls['_create_veth_pair']) execute.assert_has_calls(calls['execute']) create_ovs_vif_port.assert_has_calls(calls['create_ovs_vif_port']) def test_unplug_ovs_hybrid(self): calls = { 'device_exists': [mock.call('qbrvif-xxx-yyy')], 'execute': [mock.call('brctl', 'delif', 'qbrvif-xxx-yyy', 'qvbvif-xxx-yyy', run_as_root=True), mock.call('ip', 'link', 'set', 'qbrvif-xxx-yyy', 'down', run_as_root=True), mock.call('brctl', 'delbr', 'qbrvif-xxx-yyy', run_as_root=True)], 'delete_ovs_vif_port': [mock.call('br0', 'qvovif-xxx-yyy')] } with contextlib.nested( mock.patch.object(linux_net, 'device_exists', return_value=True), mock.patch.object(utils, 'execute'), mock.patch.object(linux_net, 'delete_ovs_vif_port') ) as (device_exists, execute, delete_ovs_vif_port): d = vif.LibvirtGenericVIFDriver() d.unplug_ovs_hybrid(None, self.vif_ovs) device_exists.assert_has_calls(calls['device_exists']) execute.assert_has_calls(calls['execute']) delete_ovs_vif_port.assert_has_calls(calls['delete_ovs_vif_port']) @mock.patch.object(utils, 'execute') @mock.patch.object(pci_utils, 'get_ifname_by_pci_address') @mock.patch.object(pci_utils, 'get_vf_num_by_pci_address', return_value=1) def _test_hw_veb_op(self, op, vlan, mock_get_vf_num, mock_get_ifname, mock_execute): mock_get_ifname.side_effect = ['eth1', 'eth13'] exit_code = [0, 2, 254] port_state = 'up' if vlan > 0 else 'down' calls = { 'get_ifname': [mock.call(self.vif_macvtap['profile']['pci_slot'], pf_interface=True), mock.call(self.vif_macvtap['profile']['pci_slot'])], 'get_vf_num': [mock.call(self.vif_macvtap['profile']['pci_slot'])], 'execute': [mock.call('ip', 'link', 'set', 'eth1', 'vf', 1, 'mac', self.vif_macvtap['address'], 'vlan', vlan, run_as_root=True, check_exit_code=exit_code), mock.call('ip', 'link', 'set', 'eth13', port_state, run_as_root=True, check_exit_code=exit_code)] } op(None, self.vif_macvtap) mock_get_ifname.assert_has_calls(calls['get_ifname']) mock_get_vf_num.assert_has_calls(calls['get_vf_num']) mock_execute.assert_has_calls(calls['execute']) def test_plug_hw_veb(self): d = vif.LibvirtGenericVIFDriver() self._test_hw_veb_op( d.plug_hw_veb, self.vif_macvtap['details'][network_model.VIF_DETAILS_VLAN]) def test_unplug_hw_veb(self): d = vif.LibvirtGenericVIFDriver() self._test_hw_veb_op(d.unplug_hw_veb, 0) def test_unplug_ovs_hybrid_bridge_does_not_exist(self): calls = { 'device_exists': [mock.call('qbrvif-xxx-yyy')], 'delete_ovs_vif_port': [mock.call('br0', 'qvovif-xxx-yyy')] } with contextlib.nested( mock.patch.object(linux_net, 'device_exists', return_value=False), mock.patch.object(linux_net, 'delete_ovs_vif_port') ) as (device_exists, delete_ovs_vif_port): d = vif.LibvirtGenericVIFDriver() d.unplug_ovs_hybrid(None, self.vif_ovs) device_exists.assert_has_calls(calls['device_exists']) delete_ovs_vif_port.assert_has_calls(calls['delete_ovs_vif_port']) def test_plug_ivs_hybrid(self): calls = { 'device_exists': [mock.call('qbrvif-xxx-yyy'), mock.call('qvovif-xxx-yyy')], '_create_veth_pair': [mock.call('qvbvif-xxx-yyy', 'qvovif-xxx-yyy')], 'execute': [mock.call('brctl', 'addbr', 'qbrvif-xxx-yyy', run_as_root=True), mock.call('brctl', 'setfd', 'qbrvif-xxx-yyy', 0, run_as_root=True), mock.call('brctl', 'stp', 'qbrvif-xxx-yyy', 'off', run_as_root=True), mock.call('tee', ('/sys/class/net/qbrvif-xxx-yyy' '/bridge/multicast_snooping'), process_input='0', run_as_root=True, check_exit_code=[0, 1]), mock.call('ip', 'link', 'set', 'qbrvif-xxx-yyy', 'up', run_as_root=True), mock.call('brctl', 'addif', 'qbrvif-xxx-yyy', 'qvbvif-xxx-yyy', run_as_root=True)], 'create_ivs_vif_port': [mock.call('qvovif-xxx-yyy', 'aaa-bbb-ccc', 'ca:fe:de:ad:be:ef', 'instance-uuid')] } with contextlib.nested( mock.patch.object(linux_net, 'device_exists', return_value=False), mock.patch.object(utils, 'execute'), mock.patch.object(linux_net, '_create_veth_pair'), mock.patch.object(linux_net, 'create_ivs_vif_port') ) as (device_exists, execute, _create_veth_pair, create_ivs_vif_port): d = vif.LibvirtGenericVIFDriver() d.plug_ivs_hybrid(self.instance, self.vif_ivs) device_exists.assert_has_calls(calls['device_exists']) _create_veth_pair.assert_has_calls(calls['_create_veth_pair']) execute.assert_has_calls(calls['execute']) create_ivs_vif_port.assert_has_calls(calls['create_ivs_vif_port']) def test_unplug_ivs_hybrid(self): calls = { 'execute': [mock.call('brctl', 'delif', 'qbrvif-xxx-yyy', 'qvbvif-xxx-yyy', run_as_root=True), mock.call('ip', 'link', 'set', 'qbrvif-xxx-yyy', 'down', run_as_root=True), mock.call('brctl', 'delbr', 'qbrvif-xxx-yyy', run_as_root=True)], 'delete_ivs_vif_port': [mock.call('qvovif-xxx-yyy')] } with contextlib.nested( mock.patch.object(utils, 'execute'), mock.patch.object(linux_net, 'delete_ivs_vif_port') ) as (execute, delete_ivs_vif_port): d = vif.LibvirtGenericVIFDriver() d.unplug_ivs_hybrid(None, self.vif_ivs) execute.assert_has_calls(calls['execute']) delete_ivs_vif_port.assert_has_calls(calls['delete_ivs_vif_port']) def test_unplug_ivs_hybrid_bridge_does_not_exist(self): d = vif.LibvirtGenericVIFDriver() with mock.patch.object(utils, 'execute') as execute: execute.side_effect = processutils.ProcessExecutionError d.unplug_ivs_hybrid(None, self.vif_ivs) def test_unplug_iovisor(self): d = vif.LibvirtGenericVIFDriver() with mock.patch.object(utils, 'execute') as execute: execute.side_effect = processutils.ProcessExecutionError mynetwork = network_model.Network(id='network-id-xxx-yyy-zzz', label='mylabel') myvif = network_model.VIF(id='vif-xxx-yyy-zzz', address='ca:fe:de:ad:be:ef', network=mynetwork) d.unplug_iovisor(None, myvif) @mock.patch('nova.network.linux_net.device_exists') def test_plug_iovisor(self, device_exists): device_exists.return_value = True d = vif.LibvirtGenericVIFDriver() with mock.patch.object(utils, 'execute') as execute: execute.side_effect = processutils.ProcessExecutionError instance = { 'name': 'instance-name', 'uuid': 'instance-uuid', 'project_id': 'myproject' } d.plug_iovisor(instance, self.vif_ivs) def test_unplug_mlnx_with_details(self): d = vif.LibvirtGenericVIFDriver() with mock.patch.object(utils, 'execute') as execute: execute.side_effect = processutils.ProcessExecutionError d.unplug_mlnx_direct(None, self.vif_mlnx_net) execute.assert_called_once_with('ebrctl', 'del-port', 'fake_phy_network', 'ca:fe:de:ad:be:ef', run_as_root=True) def test_plug_mlnx_with_details(self): d = vif.LibvirtGenericVIFDriver() with mock.patch.object(utils, 'execute') as execute: d.plug_mlnx_direct(self.instance, self.vif_mlnx_net) execute.assert_called_once_with('ebrctl', 'add-port', 'ca:fe:de:ad:be:ef', 'instance-uuid', 'fake_phy_network', 'mlnx_direct', 'eth-xxx-yyy-zzz', run_as_root=True) def test_plug_mlnx_no_physical_network(self): d = vif.LibvirtGenericVIFDriver() with mock.patch.object(utils, 'execute') as execute: self.assertRaises(exception.NovaException, d.plug_mlnx_direct, self.instance, self.vif_mlnx) self.assertEqual(0, execute.call_count) def test_ivs_ethernet_driver(self): d = vif.LibvirtGenericVIFDriver() self._check_ivs_ethernet_driver(d, self.vif_ivs, "tap") def _check_ivs_virtualport_driver(self, d, vif, want_iface_id): self.flags(firewall_driver="nova.virt.firewall.NoopFirewallDriver") xml = self._get_instance_xml(d, vif) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "ethernet", "target", "dev", vif, vif['devname']) def _check_ovs_virtualport_driver(self, d, vif, want_iface_id): self.flags(firewall_driver="nova.virt.firewall.NoopFirewallDriver") xml = self._get_instance_xml(d, vif) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "bridge", "source", "bridge", vif, "br0") vp = node.find("virtualport") self.assertEqual(vp.get("type"), "openvswitch") iface_id_found = False for p_elem in vp.findall("parameters"): iface_id = p_elem.get("interfaceid", None) if iface_id: self.assertEqual(iface_id, want_iface_id) iface_id_found = True self.assertTrue(iface_id_found) def test_generic_ovs_virtualport_driver(self): d = vif.LibvirtGenericVIFDriver() want_iface_id = self.vif_ovs['ovs_interfaceid'] self._check_ovs_virtualport_driver(d, self.vif_ovs, want_iface_id) def test_generic_ivs_virtualport_driver(self): d = vif.LibvirtGenericVIFDriver() want_iface_id = self.vif_ivs['ovs_interfaceid'] self._check_ivs_virtualport_driver(d, self.vif_ivs, want_iface_id) def test_ivs_plug_with_nova_firewall(self): d = vif.LibvirtGenericVIFDriver() br_want = "qbr" + self.vif_ivs['id'] br_want = br_want[:network_model.NIC_NAME_LEN] xml = self._get_instance_xml(d, self.vif_ivs) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "bridge", "source", "bridge", self.vif_ivs, br_want, 1) def test_ivs_plug_with_port_filter_direct_no_nova_firewall(self): d = vif.LibvirtGenericVIFDriver() br_want = "qbr" + self.vif_ivs_filter_hybrid['id'] br_want = br_want[:network_model.NIC_NAME_LEN] self.flags(firewall_driver="nova.virt.firewall.NoopFirewallDriver") xml = self._get_instance_xml(d, self.vif_ivs_filter_hybrid) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "bridge", "source", "bridge", self.vif_ivs_filter_hybrid, br_want, 0) def test_ivs_plug_with_port_filter_hybrid_no_nova_firewall(self): d = vif.LibvirtGenericVIFDriver() br_want = self.vif_ivs_filter_direct['devname'] self.flags(firewall_driver="nova.virt.firewall.NoopFirewallDriver") xml = self._get_instance_xml(d, self.vif_ivs_filter_direct) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "ethernet", "target", "dev", self.vif_ivs_filter_direct, br_want, 0) def test_hybrid_plug_without_nova_firewall(self): d = vif.LibvirtGenericVIFDriver() br_want = "qbr" + self.vif_ovs_hybrid['id'] br_want = br_want[:network_model.NIC_NAME_LEN] self.flags(firewall_driver="nova.virt.firewall.NoopFirewallDriver") xml = self._get_instance_xml(d, self.vif_ovs_hybrid) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "bridge", "source", "bridge", self.vif_ovs_hybrid, br_want, 0) def test_direct_plug_with_port_filter_cap_no_nova_firewall(self): d = vif.LibvirtGenericVIFDriver() br_want = self.vif_midonet['devname'] xml = self._get_instance_xml(d, self.vif_ovs_filter_cap) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "bridge", "target", "dev", self.vif_ovs_filter_cap, br_want) def _check_neutron_hybrid_driver(self, d, vif, br_want): self.flags(firewall_driver="nova.virt.firewall.IptablesFirewallDriver") xml = self._get_instance_xml(d, vif) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "bridge", "source", "bridge", vif, br_want, 1) def test_generic_hybrid_driver(self): d = vif.LibvirtGenericVIFDriver() br_want = "qbr" + self.vif_ovs['id'] br_want = br_want[:network_model.NIC_NAME_LEN] self._check_neutron_hybrid_driver(d, self.vif_ovs, br_want) def test_ivs_hybrid_driver(self): d = vif.LibvirtGenericVIFDriver() br_want = "qbr" + self.vif_ivs['id'] br_want = br_want[:network_model.NIC_NAME_LEN] self._check_neutron_hybrid_driver(d, self.vif_ivs, br_want) def test_mlnx_direct_vif_driver(self): d = vif.LibvirtGenericVIFDriver() xml = self._get_instance_xml(d, self.vif_mlnx) node = self._get_node(xml) self.assertEqual(node.get("type"), "direct") self._assertTypeEquals(node, "direct", "source", "dev", "eth-xxx-yyy-zzz") self._assertTypeEquals(node, "direct", "source", "mode", "passthrough") self._assertMacEquals(node, self.vif_mlnx) self._assertModel(xml, network_model.VIF_MODEL_VIRTIO) def test_midonet_ethernet_vif_driver(self): d = vif.LibvirtGenericVIFDriver() self.flags(firewall_driver="nova.virt.firewall.NoopFirewallDriver") br_want = self.vif_midonet['devname'] xml = self._get_instance_xml(d, self.vif_midonet) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "ethernet", "target", "dev", self.vif_midonet, br_want) def test_generic_8021qbh_driver(self): d = vif.LibvirtGenericVIFDriver() xml = self._get_instance_xml(d, self.vif_8021qbh) node = self._get_node(xml) self._assertTypeAndPciEquals(node, "hostdev", self.vif_8021qbh) self._assertMacEquals(node, self.vif_8021qbh) vp = node.find("virtualport") self.assertEqual(vp.get("type"), "802.1Qbh") profile_id_found = False for p_elem in vp.findall("parameters"): details = self.vif_8021qbh["details"] profile_id = p_elem.get("profileid", None) if profile_id: self.assertEqual(profile_id, details[network_model.VIF_DETAILS_PROFILEID]) profile_id_found = True self.assertTrue(profile_id_found) def test_hw_veb_driver(self): d = vif.LibvirtGenericVIFDriver() xml = self._get_instance_xml(d, self.vif_hw_veb) node = self._get_node(xml) self._assertTypeAndPciEquals(node, "hostdev", self.vif_hw_veb) self._assertMacEquals(node, self.vif_hw_veb) vlan = node.find("vlan").find("tag").get("id") vlan_want = self.vif_hw_veb["details"]["vlan"] self.assertEqual(vlan, vlan_want) @mock.patch.object(pci_utils, 'get_ifname_by_pci_address', return_value='eth1') def test_hw_veb_driver_macvtap(self, mock_get_ifname): d = vif.LibvirtGenericVIFDriver() xml = self._get_instance_xml(d, self.vif_macvtap) node = self._get_node(xml) self.assertEqual(node.get("type"), "direct") self._assertTypeEquals(node, "direct", "source", "dev", "eth1") self._assertTypeEquals(node, "direct", "source", "mode", "passthrough") self._assertMacEquals(node, self.vif_macvtap) vlan = node.find("vlan") self.assertIsNone(vlan) def test_generic_iovisor_driver(self): d = vif.LibvirtGenericVIFDriver() self.flags(firewall_driver="nova.virt.firewall.NoopFirewallDriver") br_want = self.vif_ivs['devname'] xml = self._get_instance_xml(d, self.vif_ivs) node = self._get_node(xml) self._assertTypeAndMacEquals(node, "ethernet", "target", "dev", self.vif_ivs, br_want) def test_generic_8021qbg_driver(self): d = vif.LibvirtGenericVIFDriver() xml = self._get_instance_xml(d, self.vif_8021qbg) node = self._get_node(xml) self._assertTypeEquals(node, "direct", "source", "dev", "eth0") self._assertMacEquals(node, self.vif_8021qbg) vp = node.find("virtualport") self.assertEqual(vp.get("type"), "802.1Qbg") manager_id_found = False type_id_found = False typeversion_id_found = False instance_id_found = False for p_elem in vp.findall("parameters"): wantparams = self.vif_8021qbg['qbg_params'] manager_id = p_elem.get("managerid", None) type_id = p_elem.get("typeid", None) typeversion_id = p_elem.get("typeidversion", None) instance_id = p_elem.get("instanceid", None) if manager_id: self.assertEqual(manager_id, wantparams['managerid']) manager_id_found = True if type_id: self.assertEqual(type_id, wantparams['typeid']) type_id_found = True if typeversion_id: self.assertEqual(typeversion_id, wantparams['typeidversion']) typeversion_id_found = True if instance_id: self.assertEqual(instance_id, wantparams['instanceid']) instance_id_found = True self.assertTrue(manager_id_found) self.assertTrue(type_id_found) self.assertTrue(typeversion_id_found) self.assertTrue(instance_id_found)
apache-2.0
7,805,289,528,933,048,000
46.103516
79
0.475432
false
4.137061
true
false
false
jeremy24/494-graph-algos
python/hw2/timeit.py
1
1308
from __future__ import print_function import time import os from graph import Graph from graph import make from graph import GraphException from graph import Matrix def run(name): graph = make( name ) ret = [0,0] start = time.time() graph.dfs(0) ret[1] = (time.time()-start) start = time.time() graph.bfs(0) ret[0] = (time.time()-start) return ret def go(): names = list() bfs = list() dfs = list() for name in os.listdir("./graphs"): names.append(name) name = "./graphs/" + name res = run(name) bfs.append(res[0]) dfs.append(res[1]) for index in range(0, len(names)): name = names[index] b = bfs[index] d = dfs[index] first = "%s" % str(object=name).ljust(30, " ") second = "%s" % str(object=b).rjust(18, " ") third = "%s" % str(object=d).ljust(20, " ") print("dfs: " + str(d) + " bfs: " + str(b)) if d > b: print("dfs is faster on " + first + " by " + str(abs(b-d)) + " seconds") else: print("bfs is faster on " + first + " by " + str(abs(b-d)) + " seconds") # print(first + " took " + second + " " + third) go()
mit
3,221,412,540,978,325,000
17.818182
84
0.482416
false
3.261845
false
false
false
gnarula/eden_deployment
modules/s3db/asset.py
1
56243
# -*- coding: utf-8 -*- """ Sahana Eden Assets Model @copyright: 2009-2014 (c) Sahana Software Foundation @license: MIT 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. """ __all__ = ("S3AssetModel", "S3AssetHRModel", "S3AssetTeamModel", #"asset_rheader", "asset_types", "asset_log_status", "asset_controller", "asset_AssetRepresent", ) try: import json # try stdlib (Python 2.6) except ImportError: try: import simplejson as json # try external module except: import gluon.contrib.simplejson as json # fallback to pure-Python module from gluon import * from gluon.storage import Storage from ..s3 import * from s3layouts import S3AddResourceLink ASSET_TYPE_VEHICLE = 1 # => Extra Tab(s) for Registration Documents, Fuel Efficiency ASSET_TYPE_RADIO = 2 # => Extra Tab(s) for Radio Channels/Frequencies ASSET_TYPE_TELEPHONE = 3 # => Extra Tab(s) for Contact Details & Airtime Billing ASSET_TYPE_OTHER = 4 # => No extra Tabs # To pass to global scope asset_types = {"VEHICLE" : ASSET_TYPE_VEHICLE, "RADIO" : ASSET_TYPE_RADIO, "TELEPHONE" : ASSET_TYPE_TELEPHONE, "OTHER" : ASSET_TYPE_OTHER, } ASSET_LOG_SET_BASE = 1 ASSET_LOG_ASSIGN = 2 ASSET_LOG_RETURN = 3 ASSET_LOG_CHECK = 4 ASSET_LOG_REPAIR = 5 ASSET_LOG_DONATED = 32 ASSET_LOG_LOST = 33 ASSET_LOG_STOLEN = 34 ASSET_LOG_DESTROY = 35 # To pass to global scope asset_log_status = {"SET_BASE" : ASSET_LOG_SET_BASE, "ASSIGN" : ASSET_LOG_ASSIGN, "RETURN" : ASSET_LOG_RETURN, "CHECK" : ASSET_LOG_CHECK, "REPAIR" : ASSET_LOG_REPAIR, "DONATED" : ASSET_LOG_DONATED, "LOST" : ASSET_LOG_LOST, "STOLEN" : ASSET_LOG_STOLEN, "DESTROY" : ASSET_LOG_DESTROY, } # ============================================================================= class S3AssetModel(S3Model): """ Asset Management """ names = ("asset_asset", "asset_item", "asset_log", "asset_asset_id", ) def model(self): T = current.T db = current.db auth = current.auth s3 = current.response.s3 item_id = self.supply_item_id item_entity_id = self.supply_item_entity_id location_id = self.gis_location_id organisation_id = self.org_organisation_id person_id = self.pr_person_id messages = current.messages NONE = messages["NONE"] UNKNOWN_OPT = messages.UNKNOWN_OPT settings = current.deployment_settings org_site_label = settings.get_org_site_label() vehicle = settings.has_module("vehicle") # Shortcuts add_components = self.add_components configure = self.configure crud_strings = s3.crud_strings define_table = self.define_table super_link = self.super_link #-------------------------------------------------------------------------- # Assets # asset_type_opts = {ASSET_TYPE_VEHICLE : T("Vehicle"), #ASSET_TYPE_RADIO : T("Radio"), #ASSET_TYPE_TELEPHONE : T("Telephone"), ASSET_TYPE_OTHER : T("Other"), } asset_condition_opts = {1: T("Good Condition"), 2: T("Minor Damage"), 3: T("Major Damage"), 4: T("Un-Repairable"), 5: T("Needs Maintenance"), } ctable = self.supply_item_category itable = self.supply_item supply_item_represent = self.supply_item_represent asset_items_set = db((ctable.can_be_asset == True) & \ (itable.item_category_id == ctable.id)) tablename = "asset_asset" define_table(tablename, # Instances super_link("track_id", "sit_trackable"), super_link("doc_id", "doc_entity"), item_entity_id, Field("number", label = T("Asset Number"), ), # @ToDo: We could set this automatically based on Item Category Field("type", "integer", default = ASSET_TYPE_OTHER, label = T("Type"), represent = lambda opt: \ asset_type_opts.get(opt, UNKNOWN_OPT), requires = IS_IN_SET(asset_type_opts), readable = vehicle, writable = vehicle, ), item_id(represent = supply_item_represent, requires = IS_ONE_OF(asset_items_set, "supply_item.id", supply_item_represent, sort = True, ), script = None, # No Item Pack Filter widget = None, ), Field("kit", "boolean", default = False, label = T("Kit?"), represent = lambda opt: \ (opt and [T("Yes")] or [NONE])[0], # Enable in template if-required readable = False, writable = False, ), organisation_id(requires=self.org_organisation_requires( updateable=True, #required=True ), required = True, script = ''' S3OptionsFilter({ 'triggerName':'organisation_id', 'targetName':'site_id', 'lookupResource':'site', 'lookupPrefix':'org', 'lookupField':'site_id', 'lookupURL':S3.Ap.concat('/org/sites_for_org/'), })''', ), # This is a component, so needs to be a super_link # - can't override field name, ondelete or requires super_link("site_id", "org_site", default = auth.user.site_id if auth.is_logged_in() else None, empty = False, label = org_site_label, ondelete = "RESTRICT", readable = True, writable = True, represent = self.org_site_represent, # Comment these to use a Dropdown & not an Autocomplete #widget = S3SiteAutocompleteWidget(), #comment = DIV(_class="tooltip", # _title="%s|%s" % (T("Warehouse"), # messages.AUTOCOMPLETE_HELP)), ), Field("sn", label = T("Serial Number"), ), organisation_id("supply_org_id", label = T("Supplier/Donor"), ondelete = "SET NULL", ), s3_date("purchase_date", label = T("Purchase Date"), ), Field("purchase_price", "double", #default = 0.00, represent = lambda v, row=None: \ IS_FLOAT_AMOUNT.represent(v, precision=2), ), s3_currency("purchase_currency"), # Base Location, which should always be a Site & set via Log location_id(readable = False, writable = False, ), # Populated onaccept of the log to make a component tab person_id("assigned_to_id", readable = False, writable = False, comment = self.pr_person_comment(child="assigned_to_id"), ), # Populated onaccept of the log for reporting/filtering Field("cond", "integer", label = T("Condition"), represent = lambda opt: \ asset_condition_opts.get(opt, UNKNOWN_OPT), #readable = False, writable = False, ), s3_comments(), *s3_meta_fields()) # CRUD strings crud_strings[tablename] = Storage( label_create = T("Create Asset"), title_display = T("Asset Details"), title_list = T("Assets"), title_update = T("Edit Asset"), title_upload = T("Import Assets"), label_list_button = T("List Assets"), label_delete_button = T("Delete Asset"), msg_record_created = T("Asset added"), msg_record_modified = T("Asset updated"), msg_record_deleted = T("Asset deleted"), msg_list_empty = T("No Assets currently registered")) asset_represent = asset_AssetRepresent(show_link=True) # Reusable Field asset_id = S3ReusableField("asset_id", "reference %s" % tablename, label = T("Asset"), ondelete = "CASCADE", represent = asset_represent, requires = IS_EMPTY_OR( IS_ONE_OF(db, "asset_asset.id", asset_represent, sort=True)), sortby = "number", ) # Which levels of Hierarchy are we using? levels = current.gis.get_relevant_hierarchy_levels() list_fields = ["id", "item_id$item_category_id", "item_id", "number", #"type", #"purchase_date", (T("Assigned To"), "assigned_to_id"), "organisation_id", "site_id", ] report_fields = ["number", (T("Category"), "item_id$item_category_id"), (T("Item"), "item_id"), "organisation_id", "site_id", "cond", ] text_fields = ["number", "item_id$name", #"item_id$category_id$name", "comments", ] for level in levels: lfield = "location_id$%s" % level report_fields.append(lfield) text_fields.append(lfield) list_fields.append(lfield) list_fields.extend(("cond", "comments")) filter_widgets = [ S3TextFilter(text_fields, label = T("Search"), comment = T("You can search by asset number, item description or comments. You may use % as wildcard. Press 'Search' without input to list all assets."), #_class = "filter-search", ), S3OptionsFilter("item_id$item_category_id", ), S3OptionsFilter("organisation_id", represent = "%(name)s", hidden = True, ), S3LocationFilter("location_id", levels = levels, hidden = True, ), S3OptionsFilter("cond", hidden = True, ), ] report_options = Storage( rows = report_fields, cols = report_fields, fact = [(T("Number of items"), "count(number)")], defaults=Storage(cols = "location_id$%s" % levels[0], # Highest-level of hierarchy fact = "count(number)", rows = "item_id$item_category_id", totals = True, ) ) # Default summary summary = [{"name": "addform", "common": True, "widgets": [{"method": "create"}], }, {"name": "table", "label": "Table", "widgets": [{"method": "datatable"}] }, {"name": "report", "label": "Report", "widgets": [{"method": "report", "ajax_init": True}] }, {"name": "map", "label": "Map", "widgets": [{"method": "map", "ajax_init": True}], }, ] # Resource Configuration configure(tablename, # Open Tabs after creation create_next = URL(c="asset", f="asset", args=["[id]"]), deduplicate = self.asset_duplicate, filter_widgets = filter_widgets, list_fields = list_fields, mark_required = ["organisation_id"], onaccept = self.asset_onaccept, realm_components = ["log", "presence"], report_options = report_options, summary = summary, super_entity = ("supply_item_entity", "sit_trackable"), update_realm = True, ) # Components add_components(tablename, asset_group = "asset_id", asset_item = "asset_id", asset_log = "asset_id", asset_human_resource = "asset_id", hrm_human_resource = {"link": "asset_human_resource", "joinby": "asset_id", "key": "human_resource_id", "actuate": "hide", }, vehicle_gps = "asset_id", vehicle_vehicle = {"joinby": "asset_id", "multiple": False, }, ) # ===================================================================== # Asset Items # - to allow building ad-hoc Kits # tablename = "asset_item" define_table(tablename, item_entity_id, asset_id(ondelete="CASCADE"), item_id(represent = supply_item_represent, requires = IS_ONE_OF(asset_items_set, "supply_item.id", supply_item_represent, sort = True, ), script = None, # No Item Pack Filter widget = None, ), Field("quantity", "integer", notnull=True, default = 1, label = T("Quantity"), requires = IS_INT_IN_RANGE(1, 1000), ), Field("sn", label = T("Serial Number")), organisation_id("supply_org_id", label = T("Supplier/Donor"), ondelete = "SET NULL"), s3_date("purchase_date", label = T("Purchase Date")), Field("purchase_price", "double", #default=0.00, represent=lambda v, row=None: \ IS_FLOAT_AMOUNT.represent(v, precision=2)), s3_currency("purchase_currency"), # Base Location, which should always be a Site & set via Log location_id(readable=False, writable=False), s3_comments(comment=None), *s3_meta_fields()) # ===================================================================== # Asset Log # asset_log_status_opts = {ASSET_LOG_SET_BASE : T("Base %(facility)s Set") % dict(facility = org_site_label), ASSET_LOG_ASSIGN : T("Assigned"), ASSET_LOG_RETURN : T("Returned"), ASSET_LOG_CHECK : T("Checked"), ASSET_LOG_REPAIR : T("Repaired"), ASSET_LOG_DONATED : T("Donated"), ASSET_LOG_LOST : T("Lost"), ASSET_LOG_STOLEN : T("Stolen"), ASSET_LOG_DESTROY : T("Destroyed"), } if auth.permission.format == "html": # T isn't JSON serializable site_types = auth.org_site_types for key in site_types.keys(): site_types[key] = str(site_types[key]) site_types = json.dumps(site_types) script = ''' S3OptionsFilter({ 'triggerName':'organisation_id', 'targetName':'site_id', 'lookupPrefix':'org', 'lookupResource':'site', 'lookupField':'site_id', 'fncRepresent': function(record,PrepResult){ var InstanceTypeNice=%(instance_type_nice)s return record.name+" ("+InstanceTypeNice[record.instance_type]+")" }})''' % dict(instance_type_nice = site_types) else: script = None tablename = "asset_log" define_table(tablename, asset_id(), Field("status", "integer", label = T("Status"), represent = lambda opt: \ asset_log_status_opts.get(opt, UNKNOWN_OPT), requires = IS_IN_SET(asset_log_status_opts), ), s3_datetime("datetime", default = "now", empty = False, represent = "date", ), s3_datetime("datetime_until", label = T("Date Until"), represent = "date", ), person_id(label = T("Assigned To")), Field("check_in_to_person", "boolean", #label = T("Mobile"), # Relabel? label = T("Track with this Person?"), comment = DIV(_class="tooltip", #_title="%s|%s" % (T("Mobile"), _title="%s|%s" % (T("Track with this Person?"), T("If selected, then this Asset's Location will be updated whenever the Person's Location is updated."))), readable = False, writable = False, ), # The Organisation to whom the loan is made organisation_id(readable = False, widget = None, writable = False, ), # This is a component, so needs to be a super_link # - can't override field name, ondelete or requires super_link("site_id", "org_site", label = org_site_label, #filterby = "site_id", #filter_opts = auth.permitted_facilities(redirect_on_error=False), instance_types = auth.org_site_types, updateable = True, not_filterby = "obsolete", not_filter_opts = (True,), #default = user.site_id if is_logged_in() else None, readable = True, writable = True, empty = False, represent = self.org_site_represent, #widget = S3SiteAutocompleteWidget(), script = script, ), self.org_room_id(), #location_id(), Field("cancel", "boolean", default = False, label = T("Cancel Log Entry"), represent = s3_yes_no_represent, comment = DIV(_class="tooltip", _title="%s|%s" % (T("Cancel Log Entry"), T("'Cancel' will indicate an asset log entry did not occur"))) ), Field("cond", "integer", # condition is a MySQL reserved word label = T("Condition"), represent = lambda opt: \ asset_condition_opts.get(opt, UNKNOWN_OPT), requires = IS_IN_SET(asset_condition_opts, zero = "%s..." % T("Please select")), ), person_id("by_person_id", default = auth.s3_logged_in_person(), # This can either be the Asset controller if signed-out from the store label = T("Assigned By"), # or the previous owner if passed on directly (e.g. to successor in their post) comment = self.pr_person_comment(child="by_person_id"), ), s3_comments(), *s3_meta_fields()) # CRUD strings ADD_ASSIGN = T("New Entry in Asset Log") crud_strings[tablename] = Storage( label_create = ADD_ASSIGN, title_display = T("Asset Log Details"), title_list = T("Asset Log"), title_update = T("Edit Asset Log Entry"), label_list_button = T("Asset Log"), label_delete_button = T("Delete Asset Log Entry"), msg_record_created = T("Entry added to Asset Log"), msg_record_modified = T("Asset Log Entry updated"), msg_record_deleted = T("Asset Log Entry deleted"), msg_list_empty = T("Asset Log Empty")) # Resource configuration configure(tablename, listadd = False, list_fields = ["id", "datetime", "status", "datetime_until", "organisation_id", "site_id", "room_id", "person_id", #"location_id", "cancel", "cond", "comments", ], onaccept = self.asset_log_onaccept, orderby = "asset_log.datetime desc", ) # --------------------------------------------------------------------- # Pass names back to global scope (s3.*) # return dict(asset_asset_id = asset_id, asset_represent = asset_represent, ) # ------------------------------------------------------------------------- @staticmethod def defaults(): """ Return safe defaults for names in case the model is disabled """ dummy = S3ReusableField("dummy_id", "integer", readable = False, writable = False) return dict(asset_asset_id = lambda **attr: dummy("asset_id"), ) # ------------------------------------------------------------------------- @staticmethod def asset_duplicate(item): """ Deduplication of Assets """ if item.tablename != "asset_asset": return table = item.table data = item.data number = data.get("number", None) query = (table.number == number) organisation_id = data.get("organisation_id", None) if organisation_id: query &= (table.organisation_id == organisation_id) site_id = data.get("site_id", None) if site_id: query &= (table.site_id == site_id) _duplicate = current.db(query).select(table.id, limitby=(0, 1)).first() if _duplicate: item.id = _duplicate.id item.data.id = _duplicate.id item.method = item.METHOD.UPDATE # ------------------------------------------------------------------------- @staticmethod def asset_onaccept(form): """ After DB I/O """ if current.response.s3.bulk: # Import or Sync return db = current.db atable = db.asset_asset form_vars = form.vars kit = form_vars.get("kit", None) site_id = form_vars.get("site_id", None) if site_id: stable = db.org_site asset_id = form_vars.id # Set the Base Location location_id = db(stable.site_id == site_id).select(stable.location_id, limitby=(0, 1) ).first().location_id tracker = S3Tracker() asset_tracker = tracker(atable, asset_id) asset_tracker.set_base_location(location_id) if kit: # Also populate location_id field in component items aitable = db.asset_item db(aitable.asset_id == asset_id).update(location_id = location_id) # Add a log entry for this ltable = db.asset_log ltable.insert(asset_id = asset_id, status = ASSET_LOG_SET_BASE, organisation_id = form_vars.get("organisation_id", None), site_id = site_id, cond = 1, ) if kit: # Empty any inappropriate fields db(atable.id == asset_id).update(supplier_org_id = None, purchase_date = None, purchase_price = None, purchase_currency = None, ) else: # Delete any component items aitable = db.asset_item ids = db(aitable.asset_id == asset_id).select(aitable.id).as_list() if ids: resource = current.s3db.resource("asset_item", id=ids) resource.delete() return # ------------------------------------------------------------------------- @staticmethod def asset_log_onaccept(form): """ After DB I/O """ request = current.request get_vars = request.get_vars status = get_vars.get("status", None) if not status: if not current.response.s3.asset_import: # e.g. Record merger or Sync return # Import db = current.db form_vars = form.vars asset_id = form_vars.asset_id status = int(form_vars.status) if status == ASSET_LOG_ASSIGN: # Only type supported right now # @ToDo: Support more types type == "person" new = True else: # Interactive form_vars = form.vars status = int(form_vars.status or status) db = current.db ltable = db.asset_log row = db(ltable.id == form_vars.id).select(ltable.asset_id, limitby=(0, 1) ).first() try: asset_id = row.asset_id except: return current_log = asset_get_current_log(asset_id) type = get_vars.get("type", None) log_time = current_log.datetime current_time = form_vars.get("datetime", None).replace(tzinfo=None) new = log_time <= current_time if new: # This is a current assignment atable = db.asset_asset aitable = db.asset_item tracker = S3Tracker() asset_tracker = tracker(atable, asset_id) if status == ASSET_LOG_SET_BASE: # Set Base Location site_id = form_vars.get("site_id", None) stable = db.org_site location_id = db(stable.site_id == site_id).select(stable.location_id, limitby=(0, 1) ).first().location_id asset_tracker.set_base_location(location_id) # Also do component items db(aitable.asset_id == asset_id).update(location_id = location_id) elif status == ASSET_LOG_ASSIGN: if type == "person": if form_vars.check_in_to_person: asset_tracker.check_in(db.pr_person, form_vars.person_id, timestmp = request.utcnow) # Also do component items # @ToDo: Have these move when the person moves locations = asset_tracker.get_location(_fields=[db.gis_location.id]) try: db(aitable.asset_id == asset_id).update(location_id = locations[0].id) except: pass else: location_id = asset_tracker.set_location(form_vars.person_id, timestmp = request.utcnow) # Also do component items db(aitable.asset_id == asset_id).update(location_id = location_id) # Update main record for component db(atable.id == asset_id).update(assigned_to_id=form_vars.person_id) elif type == "site": asset_tracker.check_in(db.org_site, form_vars.site_id, timestmp = request.utcnow) # Also do component items locations = asset_tracker.get_location(_fields=[db.gis_location.id]) try: db(aitable.asset_id == asset_id).update(location_id = locations[0].id) except: pass elif type == "organisation": site_id = form_vars.get("site_id", None) if site_id: asset_tracker.check_in(db.org_site, site_id, timestmp = request.utcnow) # Also do component items locations = asset_tracker.get_location(_fields=[db.gis_location.id]) try: db(aitable.asset_id == asset_id).update(location_id = locations[0].id) except: pass else: # We can no longer track location asset_tracker.check_out() elif status == ASSET_LOG_RETURN: # Set location to base location location_id = asset_tracker.set_location(asset_tracker, timestmp = request.utcnow) # Also do component items db(aitable.asset_id == asset_id).update(location_id = location_id) # Update condition in main record db(atable.id == asset_id).update(cond=form_vars.cond) return # ============================================================================= class S3AssetHRModel(S3Model): """ Optionally link Assets to Human Resources - useful for staffing a vehicle """ names = ("asset_human_resource",) def model(self): #T = current.T #-------------------------------------------------------------------------- # Assets <> Human Resources # tablename = "asset_human_resource" self.define_table(tablename, self.asset_asset_id(empty = False), self.hrm_human_resource_id(empty = False, ondelete = "CASCADE", ), #s3_comments(), *s3_meta_fields()) # --------------------------------------------------------------------- # Pass names back to global scope (s3.*) # return dict() # ============================================================================= class S3AssetTeamModel(S3Model): """ Optionally link Assets to Teams """ names = ("asset_group",) def model(self): #T = current.T #-------------------------------------------------------------------------- # Assets <> Groups # tablename = "asset_group" self.define_table(tablename, self.asset_asset_id(empty = False), self.pr_group_id(comment = None, empty = False, ), #s3_comments(), *s3_meta_fields()) # --------------------------------------------------------------------- # Pass names back to global scope (s3.*) # return dict() # ============================================================================= def asset_get_current_log(asset_id): """ Get the current log entry for this asset """ table = current.s3db.asset_log query = (table.asset_id == asset_id) & \ (table.cancel == False) & \ (table.deleted == False) # Get the log with the maximum time asset_log = current.db(query).select(table.id, table.status, table.datetime, table.cond, table.person_id, table.organisation_id, table.site_id, #table.location_id, orderby = ~table.datetime, limitby=(0, 1)).first() if asset_log: return Storage(datetime = asset_log.datetime, person_id = asset_log.person_id, cond = int(asset_log.cond or 0), status = int(asset_log.status or 0), organisation_id = asset_log.organisation_id, site_id = asset_log.site_id, #location_id = asset_log.location_id ) else: return Storage() # ============================================================================= def asset_log_prep(r): """ Called by Controller """ T = current.T db = current.db request = current.request table = db.asset_log if r.record: asset = Storage(r.record) else: # This is a new record asset = Storage() table.cancel.readable = False table.cancel.writable = False # This causes an error with the dataTables paginate # if used only in r.interactive & not also r.representation=="aadata" if r.method != "read" and r.method != "update": table.cancel.readable = False table.cancel.writable = False current_log = asset_get_current_log(asset.id) if request.vars.status: status = int(request.vars.status) else: status = 0 if status and status != "None": field = table.status field.default = status field.readable = False field.writable = False elif current_log: table.status.default = current_log.status if current_log.organisation_id: table.organisation_id.default = current_log.organisation_id table.site_id.requires = IS_ONE_OF(db, "org_site.site_id", table.site_id.represent, filterby = "organisation_id", filter_opts = (current_log.organisation_id,)) crud_strings = current.response.s3.crud_strings.asset_log if status == ASSET_LOG_SET_BASE: crud_strings.msg_record_created = T("Base Facility/Site Set") table.by_person_id.label = T("Set By") table.site_id.writable = True table.datetime_until.readable = False table.datetime_until.writable = False table.person_id.readable = False table.person_id.writable = False table.organisation_id.readable = True table.organisation_id.writable = True table.site_id.requires = IS_ONE_OF(db, "org_site.site_id", table.site_id.represent) elif status == ASSET_LOG_RETURN: crud_strings.msg_record_created = T("Returned") table.person_id.label = T("Returned From") table.person_id.default = current_log.person_id table.site_id.readable = False table.site_id.writable = False elif status == ASSET_LOG_ASSIGN: type = request.vars.type # table["%s_id" % type].required = True if type == "person": crud_strings.msg_record_created = T("Assigned to Person") table["person_id"].requires = IS_ONE_OF(db, "pr_person.id", table.person_id.represent, orderby="pr_person.first_name", sort=True, error_message="Person must be specified!") table.check_in_to_person.readable = True table.check_in_to_person.writable = True table.site_id.requires = IS_EMPTY_OR( IS_ONE_OF(db, "org_site.site_id", table.site_id.represent)) elif type == "site": crud_strings.msg_record_created = T("Assigned to Facility/Site") elif type == "organisation": crud_strings.msg_record_created = T("Assigned to Organization") table.organisation_id.readable = True table.organisation_id.writable = True table.organisation_id.requires = IS_ONE_OF(db, "org_organisation.id", table.organisation_id.represent, orderby="org_organisation.name", sort=True) table.site_id.requires = IS_EMPTY_OR( IS_ONE_OF(db, "org_site.site_id", table.site_id.represent)) elif "status" in request.get_vars: crud_strings.msg_record_created = T("Status Updated") table.person_id.label = T("Updated By") field = table.status field.readable = True field.writable = True field.requires = IS_IN_SET({ASSET_LOG_CHECK : T("Check"), ASSET_LOG_REPAIR : T("Repair"), ASSET_LOG_DONATED : T("Donated"), ASSET_LOG_LOST : T("Lost"), ASSET_LOG_STOLEN : T("Stolen"), ASSET_LOG_DESTROY : T("Destroyed"), }) # ============================================================================= def asset_rheader(r): """ Resource Header for Assets """ if r.representation == "html": record = r.record if record: T = current.T s3db = current.s3db s3 = current.response.s3 NONE = current.messages["NONE"] if record.type == ASSET_TYPE_VEHICLE: STAFF = current.deployment_settings.get_hrm_staff_label() tabs = [(T("Asset Details"), None, {"native": True}), (T("Vehicle Details"), "vehicle"), (STAFF, "human_resource"), (T("Assign %(staff)s") % dict(staff=STAFF), "assign"), (T("Check-In"), "check-in"), (T("Check-Out"), "check-out"), (T("GPS Data"), "gps"), ] else: tabs = [(T("Edit Details"), None)] #elif record.type == s3.asset.ASSET_TYPE_RADIO: # tabs.append((T("Radio Details"), "radio")) #elif record.type == s3.asset.ASSET_TYPE_TELEPHONE: # tabs.append((T("Telephone Details"), "phone")) tabs.append((T("Log"), "log")) tabs.append((T("Documents"), "document")) rheader_tabs = s3_rheader_tabs(r, tabs) if current.request.controller == "vehicle": func = "vehicle" else: func = "asset" # @ToDo: Check permissions before displaying buttons asset_action_btns = [ A(T("Set Base Facility/Site"), _href = URL(f=func, args = [record.id, "log", "create"], vars = dict(status = ASSET_LOG_SET_BASE) ), _class = "action-btn", ) ] current_log = asset_get_current_log(record.id) status = current_log.status #if record.location_id: # A Base Site has been set # Return functionality removed - as it doesn't set site_id & organisation_id in the logs #if status == ASSET_LOG_ASSIGN: # asset_action_btns += [ A( T("Return"), # _href = URL(f=func, # args = [record.id, "log", "create"], # vars = dict(status = ASSET_LOG_RETURN) # ), # _class = "action-btn" # ) # ] if status < ASSET_LOG_DONATED: # @ToDo: deployment setting to prevent assigning assets before returning them # The Asset is available for assignment (not disposed) asset_action_btns += [ A(T("Assign to Person"), _href = URL(f=func, args = [record.id, "log", "create"], vars = dict(status = ASSET_LOG_ASSIGN, type = "person") ), _class = "action-btn", ), A(T("Assign to Facility/Site"), _href = URL(f=func, args = [record.id, "log", "create"], vars = dict(status = ASSET_LOG_ASSIGN, type = "site") ), _class = "action-btn", ), A(T("Assign to Organization"), _href = URL(f=func, args = [record.id, "log", "create"], vars = dict(status = ASSET_LOG_ASSIGN, type = "organisation") ), _class = "action-btn", ), ] asset_action_btns += [ A(T("Update Status"), _href = URL(f=func, args = [record.id, "log", "create"], vars = None ), _class = "action-btn", ), ] table = r.table ltable = s3db.asset_log rheader = DIV(TABLE(TR(TH("%s: " % table.number.label), record.number, TH("%s: " % table.item_id.label), table.item_id.represent(record.item_id) ), TR(TH("%s: " % ltable.cond.label), ltable.cond.represent(current_log.cond), TH("%s: " % ltable.status.label), ltable.status.represent(status), ), TR(TH("%s: " % ltable.person_id.label), ltable.person_id.represent(current_log.person_id), TH("%s: " % ltable.site_id.label), ltable.site_id.represent(current_log.site_id), ), ), DIV(_style = "margin-top:5px", # @ToDo: Move to CSS *asset_action_btns ), rheader_tabs) return rheader return None # ============================================================================= def asset_controller(): """ RESTful CRUD controller """ s3db = current.s3db s3 = current.response.s3 # Pre-process def prep(r): # Location Filter current.s3db.gis_location_filter(r) if r.component_name == "log": asset_log_prep(r) return True s3.prep = prep # Import pre-process def import_prep(data): """ Flag that this is an Import (to distinguish from Sync) @ToDo: Find Person records from their email addresses """ current.response.s3.asset_import = True return # @ToDo: get this working ctable = s3db.pr_contact ptable = s3db.pr_person resource, tree = data elements = tree.getroot().xpath("/s3xml//resource[@name='pr_person']/data[@field='first_name']") persons = {} for element in elements: email = element.text if email in persons: # Replace email with uuid element.text = persons[email]["uuid"] # Don't check again continue query = (ctable.value == email) & \ (ctable.pe_id == ptable.pe_id) person = db(query).select(ptable.uuid, limitby=(0, 1) ).first() if person: # Replace email with uuid uuid = person.uuid else: # Blank it uuid = "" element.text = uuid # Store in case we get called again with same value persons[email] = dict(uuid=uuid) s3.import_prep = import_prep # Post-processor def postp(r, output): if r.interactive and r.method != "import": script = "/%s/static/scripts/S3/s3.asset.js" % r.application s3.scripts.append(script) S3CRUD.action_buttons(r, deletable=False) #if not r.component: #s3.actions.append({"url" : URL(c="asset", f="asset", # args = ["[id]", "log", "create"], # vars = {"status" : eden.asset.asset_log_status["ASSIGN"], # "type" : "person"}), # "_class" : "action-btn", # "label" : str(T("Assign"))}) return output s3.postp = postp output = current.rest_controller("asset", "asset", rheader = asset_rheader, ) return output # ============================================================================= class asset_AssetRepresent(S3Represent): """ Representation of Assets """ def __init__(self, fields = ("number",), # unused show_link = False, translate = False, multiple = False, ): # Need a custom lookup self.lookup_rows = self.custom_lookup_rows super(asset_AssetRepresent, self).__init__(lookup="asset_asset", fields=fields, show_link=show_link, translate=translate, multiple=multiple) # ------------------------------------------------------------------------- def custom_lookup_rows(self, key, values, fields=[]): """ Custom lookup method for organisation rows, does a left join with the parent organisation. Parameters key and fields are not used, but are kept for API compatibility reasons. @param values: the organisation IDs """ db = current.db s3db = current.s3db table = s3db.asset_asset itable = db.supply_item btable = db.supply_brand qty = len(values) if qty == 1: query = (table.id == values[0]) limitby = (0, 1) else: query = (table.id.belongs(values)) limitby = (0, qty) query &= (itable.id == table.item_id) rows = db(query).select(table.id, table.number, table.type, itable.name, btable.name, left=btable.on(itable.brand_id == btable.id), limitby=limitby) self.queries += 1 return rows # ------------------------------------------------------------------------- def represent_row(self, row): """ Represent a single Row @param row: the asset_asset Row """ # Custom Row (with the item & brand left-joined) number = row["asset_asset.number"] item = row["supply_item.name"] brand = row.get("supply_brand.name", None) if not number: return self.default represent = "%s (%s" % (number, item) if brand: represent = "%s, %s)" % (represent, brand) else: represent = "%s)" % represent return s3_unicode(represent) # ------------------------------------------------------------------------- def link(self, k, v, row=None): """ Represent a (key, value) as hypertext link. @param k: the key (site_id) @param v: the representation of the key @param row: the row with this key """ if row: type = row.get("asset_asset.type", None) if type == 1: return A(v, _href=URL(c="vehicle", f="vehicle", args=[k], # remove the .aaData extension in paginated views extension="" )) k = s3_unicode(k) return A(v, _href=self.linkto.replace("[id]", k) \ .replace("%5Bid%5D", k)) # END =========================================================================
mit
1,738,867,762,911,502,600
41.161169
178
0.406131
false
5.070134
false
false
false
samw3/PyTweeps
pytweeps.py
1
20531
# PyTweeps: Simple Python program to help manage your twitter followers. # https://github.com/samw3/PyTweeps import pkg_resources import tweepy import webbrowser import shelve import pprint import sys import traceback import time import collections from datetime import datetime from datetime import timedelta from config import * import io import urllib2 def isInt(s): try: int(s) return True except ValueError: return False def initData(data): # Set up the data shelf if 'following' not in data.keys(): data['following'] = set() if 'wasFollowing' not in data.keys(): data['wasFollowing'] = set() if 'followers' not in data.keys(): data['followers'] = set() if 'wasFollowedBy' not in data.keys(): data['wasFollowedBy'] = set() if 'lastTweet' not in data.keys(): data['lastTweet'] = dict() if 'followedOn' not in data.keys(): data['followedOn'] = dict() if 'wasFollowingOn' not in data.keys(): data['wasFollowingOn'] = dict() data.sync() def follow(api, data, user): api.create_friendship(user.id) data['followedOn'][user.id] = datetime.now() def authenticate(auth, data): redirect_url = auth.get_authorization_url() webbrowser.open(redirect_url) try: verifier = raw_input('Verifier:') data['request_token'] = auth.request_token # auth.set_request_token(auth.request_token.key, auth.request_token.secret) try: auth.get_access_token(verifier) data['access_token_key'] = auth.access_token data['access_token_secret'] = auth.access_token_secret data.sync() auth.set_access_token(data['access_token_key'], data['access_token_secret']) except tweepy.TweepError: print 'Error! Failed to get access token.' except tweepy.TweepError: print 'Error! Failed to get request token.' def usageMessage(): print "Usage: python", sys.argv[0], "command [params]\n" print "Commands:" print " update" print " Updates your list of followers and followed" print " bury daysSinceLastTweet numberToUnfollow" print " Remove any 'dead' tweeps. i.e. followers who no longer use twitter" print " requite daysSinceFollowed numberToUnfollow" print " Remove any tweeps who do not continue to follow you after daysSinceFollowed days" print " shotgun user numTweeps " print " Add numTweeps followers from a user. Doesn't follow previously followed users." print " copycat user numTweeps" print " Add numTweeps from the list of tweeps user is following. Doesn't follow previously followed users." print " copykids numKids numTweeps" print " Add numKids from *every* person you follow's following list. Stop after adding (approximately) numTweeps total." print " ignore user" print " Ignore a particular user, never try to follow them and unfollow if we are following." print " follow user" print " Follow a particular user, even if we retired them already." print " unfollowers filename" print " prints a list of unfollowers to filename" def error(message): usageMessage() print "ERROR: %s\n" % message sys.exit(-1) def info(message): print message def update(api, data): newUsers = 0 totalUsers = 0 stillFollowing = set() for id in api.friends_ids(): stillFollowing.add(id) if id not in data['following']: newUsers += 1 totalUsers += 1 if id not in data['followedOn']: data['followedOn'][id] = datetime.now() data['wasFollowing'] |= data['following'] data['wasFollowing'] |= stillFollowing removed = len(data['following'] - stillFollowing) data['following'] = stillFollowing noLongerFollowing = data['wasFollowing'] - stillFollowing data.sync() print "Following %d, new %d, removed %d" % (totalUsers, newUsers, removed) newUsers = 0 totalUsers = 0 stillFollowedBy = set() for id in api.followers_ids(): stillFollowedBy.add(id) if id not in data['followers']: newUsers += 1 totalUsers += 1 data['wasFollowedBy'] |= data['followers'] data['wasFollowedBy'] |= stillFollowedBy removed = len(data['followers'] - stillFollowedBy) data['followers'] = stillFollowedBy noLongerFollowedBy = data['wasFollowedBy'] - stillFollowedBy data.sync() print "Followers %d, new %d, removed %d" % (totalUsers, newUsers, removed) print "No Longer Following %d" % len(noLongerFollowing) print "No Longer Followed by %d" % len(noLongerFollowedBy) def copycat(api, data, copycatUser, numTweeps): c = 0 x = 0 for f in tweepy.Cursor(api.friends, copycatUser).items(): x += 1 id = f.id if id in data['wasFollowing']: info("%d '%s' following or was following." % (x, f.screen_name)) elif id in data['wasFollowedBy']: info("%d '%s' followed by or was followed." % (x, f.screen_name)) elif f.protected: info("%d '%s' is protected." % (x, f.screen_name)) elif f.followers_count <= shotgunTargetMinFollowers: info("%d '%s' not enough followers." % (x, f.screen_name)) elif f.friends_count <= shotgunTargetMinFollowing: info("%d '%s' not following enough." % (x, f.screen_name)) elif f.description == "": info("%d '%s' empty description." % (x, f.screen_name)) elif f.statuses_count <= shotgunTargetMinTweets: info("%d '%s' not enough tweets." % (x, f.screen_name)) elif f.screen_name == username: info("%d '%s' can't follow yourself!" % (x, f.screen_name)) else: api.create_friendship(f.id) c += 1 info("%d '%s' FOLLOWED(%d)." % (x, f.screen_name, c)) time.sleep(3) if (c == numTweeps): break; return c def main(argv): pp = pprint.PrettyPrinter(indent=4) print "\nPyTweeps v0.1 - using tweepy v%s\n" % pkg_resources.get_distribution('tweepy').version if len(argv) == 0: usageMessage() sys.exit(-1) data = shelve.open('pytweeps', writeback=True) initData(data) auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.secure = True if ('access_token_key' not in data.keys()) or ('access_token_secret' not in data.keys()): authenticate(auth, data) auth.set_access_token(data['access_token_key'], data['access_token_secret']) api = tweepy.API(auth) command = argv[0] if command == "update": update(api, data) elif command == "bury": # Check params if len(argv) < 3: error("Missing params daysSinceLastTweet or numberToUnfollow") if not isInt(argv[1]): error("daysSinceLastTweet is not an integer") daysSinceLastTweet = int(argv[1]) if not isInt(argv[2]): error("numberToUnfollow is not an integer") numberToUnfollow = int(argv[2]) delay = 0 if len(argv) >= 4 and isInt(argv[3]): delay = argv[3] # death date is the cut off. if they haven't tweeted since then, bury them cutoffDate = datetime.now() - timedelta(days=daysSinceLastTweet) # Check the lastTweet cache, if their last tweet isn't after the cutoffDate don't bother checking against twitter last = data['lastTweet'] lastKeys = last.keys() toScan = set() for f in data['following']: if f in lastKeys: if last[f] < cutoffDate: toScan.add(f) # else don't bother checking else: # not in cache, so check toScan.add(f) x = 0 numUnfollowed = 0 try: for f in toScan: tweets = api.user_timeline(f, count=1) if len(tweets) == 0: # Never tweeted? bury. user = api.get_user(f) if user.screen_name not in neverBury: api.destroy_friendship(f) print "" info("Buried '%s' R.I.P. (No Tweets)" % user.screen_name) numUnfollowed += 1 else: lastTweet = tweets[0] if (lastTweet.created_at < cutoffDate): if lastTweet.user.screen_name not in neverBury: api.destroy_friendship(f) print "" info("Buried '%s' R.I.P. (Last: %s)" % ( lastTweet.user.screen_name, unicode(lastTweet.created_at))) numUnfollowed += 1 else: data['lastTweet'][f] = lastTweet.created_at data.sync() if numUnfollowed == numberToUnfollow: break sys.stdout.write('.') x += 1 if x % 100 == 0: sys.stdout.write("[" + str(x) + "]") sys.stdout.flush() if delay > 0: time.sleep(float(delay)) except tweepy.error.TweepError, e: print "" if e.message[0]['message'] == u'Rate limit exceeded': info("Rate limit exceeded") else: print traceback.format_exc() raise e print "" update(api, data) elif command == "requite": # Check params if len(argv) < 3: error("Missing params daysSinceFollowed or numberToUnfollow") if not isInt(argv[1]): error("daysSinceFollowed is not an integer") daysSinceFollowed = int(argv[1]) if not isInt(argv[2]): error("numberToUnfollow is not an integer") numberToUnfollow = int(argv[2]) delay = 0 if len(argv) >= 4 and isInt(argv[3]): delay = argv[3] # death date is the cut off. if they haven't tweeted since then, bury them cutoffDate = datetime.now() - timedelta(days=daysSinceFollowed) # Check the wasFollowingOn cache, if their last tweet isn't after the cutoffDate don't bother checking against twitter last = data['wasFollowingOn'] lastKeys = last.keys() followedOn = data['followedOn'] followedOnKeys = followedOn.keys() toScan = set() for f in data['following']: if f in lastKeys: if last[f] < cutoffDate: toScan.add(f) # else don't bother checking elif f in followedOnKeys: if followedOn[f] < cutoffDate: toScan.add(f) else: # doesn't have a followedOn date, so check data['followedOn'][f] = datetime.now() data.sync() toScan.add(f) print "Requiting %d tweeps. %d IDs to scan" % (numberToUnfollow, len(toScan)) x = 0 numUnfollowed = 0 me = api.me() try: for f in toScan: try: user = api.get_user(f) except tweepy.error.TweepError, e: if isinstance(e.message, collections.Iterable): if e.message[0]['message'] == u'User not found.': info("User not found, skipping...") else: print traceback.format_exc() raise e ref = api.show_friendship(source_id=f, target_id=me.id) if ref[0].following: # User follows me data['wasFollowingOn'][f] = datetime.now() data.sync() else: # User not following me user = api.get_user(f) if user.screen_name not in neverBury: api.destroy_friendship(f) print "" info("Requited '%s' (Followed On: %s)" % (user.screen_name, unicode(data['followedOn'][f]))) numUnfollowed += 1 # else still has time to follow if numUnfollowed == numberToUnfollow: break sys.stdout.write('.') x += 1 if x % 100 == 0: sys.stdout.write("[" + str(x) + "]") sys.stdout.flush() if delay > 0: time.sleep(float(delay)) except tweepy.error.TweepError, e: print "" pp.pprint(e) if isinstance(e.message, collections.Iterable): if e.message[0]['message'] == u'Rate limit exceeded': info("Rate limit exceeded") else: print traceback.format_exc() raise e else: print traceback.format_exc() raise e print "" update(api, data) elif command == "shotgun": if len(argv) != 3: error("Missing params shotgun user or numTweeps") shotgunUser = argv[1] if not isInt(argv[2]): error("numTweeps is not an integer") numTweeps = int(argv[2]) info("Shotgunning '%s' for %d followers" % (shotgunUser, numTweeps)) c = 0 x = 0 try: for f in tweepy.Cursor(api.followers, shotgunUser).items(): x += 1 id = f.id if id in data['wasFollowing']: info("%d '%s' following or was following." % (x, f.screen_name)) elif id in data['wasFollowedBy']: info("%d '%s' followed by or was followed." % (x, f.screen_name)) elif f.protected: info("%d '%s' is protected." % (x, f.screen_name)) elif f.followers_count <= shotgunTargetMinFollowers: info("%d '%s' not enough followers." % (x, f.screen_name)) elif f.friends_count <= shotgunTargetMinFollowing: info("%d '%s' not following enough." % (x, f.screen_name)) elif f.description == "": info("%d '%s' empty description." % (x, f.screen_name)) elif f.statuses_count <= shotgunTargetMinTweets: info("%d '%s' not enough tweets." % (x, f.screen_name)) elif f.screen_name == username: info("%d '%s' can't follow yourself!" % (x, f.screen_name)) else: try: api.create_friendship(f.id) c += 1 info("%d '%s' FOLLOWED(%d)." % (x, f.screen_name, c)) except tweepy.error.TweepError, e: print "" if e.message[0]['code'] == 162: info("%d '%s' blocked you." % (x, f.screen_name)) api.destroy_friendship(f.id) data['wasFollowing'].add(f.id) else: print traceback.format_exc() raise e time.sleep(3) if (c == numTweeps): break; except tweepy.error.TweepError, e: print "" if e.message[0]['message'] == u'Rate limit exceeded': info("Rate limit exceeded.") else: print traceback.format_exc() raise e update(api, data) elif command == "copycat": if len(argv) != 3: error("Missing params copycat user or numTweeps") copycatUser = argv[1] if not isInt(argv[2]): error("numTweeps is not an integer") numTweeps = int(argv[2]) info("Copycatting '%s' for %d followers" % (copycatUser, numTweeps)) try: copycat(api, data, copycatUser, numTweeps) except tweepy.RateLimitError as err: print "" info("Rate limit exceeded") except tweepy.error.TweepError, e: print "" print (e.api_code) print traceback.format_exc() raise e update(api, data) elif command == "copykids": if len(argv) != 3: error("Missing params numKids or numTweeps") if not isInt(argv[1]): error("numKids is not an integer") numKids = int(argv[1]) if not isInt(argv[2]): error("numTweeps is not an integer") numTweeps = int(argv[2]) info("Copykidding %d follwers from each of your followers. %d followers total." % (numKids, numTweeps)) try: c = 0 for f in tweepy.Cursor(api.followers).items(): info("********") print("Copying %s's kids..." % (f.screen_name)) c += copycat(api, data, f.screen_name, numKids) if (c >= numTweeps): break; except tweepy.RateLimitError as err: print "" info("Rate limit exceeded") except tweepy.error.TweepError, e: print "" print (e.api_code) print traceback.format_exc() raise e update(api, data) elif command == "ignore": if len(argv) != 2: error("Missing params user") user = api.get_user(argv[1]) api.destroy_friendship(user.id) data['wasFollowing'].add(user.id) print "'%s' ignored." % (user.screen_name) elif command == "follow": if len(argv) != 2: error("Missing params user") user = api.get_user(argv[1]) follow(api, data, user) if (user.id in data['wasFollowing']): data['wasFollowing'].remove(user.id) print "'%s' FOLLOWED." % (user.screen_name) elif command == "unfollow": if len(argv) != 2: error("Missing param fileName") with io.open(argv[1], 'r', encoding='utf8') as f: for line in f: s = line.split("|",3) if s[0] == 'x': api.destroy_friendship(s[1]) print "Unfollowed", s[2] elif command == "unfollowers": if len(argv) != 2: error("Missing param fileName") old = [] ids = set() try: with io.open(argv[1], 'r', encoding='utf8') as f: for line in f: s = line.split("|",3) old.append(s) ids.add(int(s[1])) except: pass print "Creating a list of unfollowers to %s" % argv[1] me = api.me() c = 0 with io.open(argv[1], 'a', encoding='utf8') as f: for id in api.friends_ids(): print [id], id in ids if id not in ids: ref = api.show_friendship(source_id=id, target_id=me.id) if not ref[0].following: # User doesn't follow me user = api.get_user(id) desc = user.description.replace("\n",'').replace("\r",'') try: if user.url: req = urllib2.urlopen(user.url) url = req.url else: url = "" except: url = "" f.write("|%s|%s|%s|%s|%s\n" % (id, user.screen_name, user.name, desc, url)) f.flush() time.sleep(3) c += 1 sys.stdout.write('.') if c % 100 == 0: sys.stdout.write("[" + str(c) + "]") sys.stdout.flush() else: error("Unknown command '%s'" % command) #print api.me().name rate = api.rate_limit_status() #pp.pprint(rate) print "" data.close() if __name__ == "__main__": main(sys.argv[1:])
gpl-2.0
3,758,599,949,243,339,000
35.859964
132
0.509814
false
3.972717
false
false
false
3dfxsoftware/cbss-addons
invoice_report_per_journal/report/invoice_report_demo.py
1
2131
# -*- encoding: utf-8 -*- ########################################################################### # Module Writen to OpenERP, Open Source Management Solution # # Copyright (c) 2014 Vauxoo - http://www.vauxoo.com/ # All Rights Reserved. # info Vauxoo ([email protected]) ############################################################################ # Coded by: Luis Torres ([email protected]) ############################################################################ # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero 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 Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # ############################################################################## from openerp.report import report_sxw from openerp import pooler from openerp.tools.translate import _ from openerp import tools from openerp import tests from openerp.osv import osv from openerp import netsvc import openerp from report_webkit import webkit_report import datetime class invoice_report_demo_html(report_sxw.rml_parse): def __init__(self, cr, uid, name, context=None): if context is None: context = {} super(invoice_report_demo_html, self).__init__( cr, uid, name, context=context) self.localcontext.update({ }) webkit_report.WebKitParser('report.invoice.report.demo.webkit', 'account.invoice', 'addons/invoice_report_per_journal/report/invoice_report_demo.mako', parser=invoice_report_demo_html) # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
gpl-2.0
633,159,658,367,753,600
39.980769
80
0.604411
false
4.322515
false
false
false
dataplumber/edge
src/main/python/libraries/edge/dateutility.py
2
1665
from datetime import date, datetime, timedelta import dateutil.parser import calendar """ Utility class for date and time conversion. """ class DateUtility(object): RFC_822_GMT_FORMAT = "%a, %d %b %Y %H:%M:%S GMT" @staticmethod def convertTimeLongToIso(time): isoTime = '' try: isoTime = datetime.utcfromtimestamp(float(time) / 1000).isoformat() + 'Z' except ValueError: pass return isoTime @staticmethod def convertISOToUTCTimestamp(isoTime): try: #parse ISO date to datetime object dt = dateutil.parser.parse(isoTime) #return timestamp in milliseconds return calendar.timegm(dt.utctimetuple()) * 1000 except: return None @staticmethod def pastDateRFC822(hoursAgo): return (datetime.utcnow() - timedelta(hours=hoursAgo)).strftime(DateUtility.RFC_822_GMT_FORMAT) @staticmethod def convertTimeLongToRFC822(time): return DateUtility.convertTimeLong(time, DateUtility.RFC_822_GMT_FORMAT) @staticmethod def convertTimeLong(time, format): strTime = '' try: strTime = datetime.utcfromtimestamp(float(time) / 1000).strftime(format) except ValueError: pass return strTime @staticmethod def convertISOTime(isoTime, format): try: #parse ISO date to datetime object dt = dateutil.parser.parse(isoTime) #return timestamp in specified format return dt.strftime(format) except: return None
apache-2.0
-214,491,296,849,177,860
28.210526
103
0.606006
false
4.5
false
false
false
MikeFair/www.gittip.com
gittip/csrf.py
1
6543
"""Cross Site Request Forgery middleware, borrowed from Django. See also: https://github.com/django/django/blob/master/django/middleware/csrf.py https://docs.djangoproject.com/en/dev/ref/contrib/csrf/ https://github.com/zetaweb/www.gittip.com/issues/88 """ import rfc822 import re import time import urlparse #from django.utils.cache import patch_vary_headers cc_delim_re = re.compile(r'\s*,\s*') def patch_vary_headers(response, newheaders): """ Adds (or updates) the "Vary" header in the given HttpResponse object. newheaders is a list of header names that should be in "Vary". Existing headers in "Vary" aren't removed. """ # Note that we need to keep the original order intact, because cache # implementations may rely on the order of the Vary contents in, say, # computing an MD5 hash. if 'Vary' in response.headers: vary_headers = cc_delim_re.split(response.headers['Vary']) else: vary_headers = [] # Use .lower() here so we treat headers as case-insensitive. existing_headers = set([header.lower() for header in vary_headers]) additional_headers = [newheader for newheader in newheaders if newheader.lower() not in existing_headers] response.headers['Vary'] = ', '.join(vary_headers + additional_headers) #from django.utils.http import same_origin def same_origin(url1, url2): """ Checks if two URLs are 'same-origin' """ p1, p2 = urlparse.urlparse(url1), urlparse.urlparse(url2) return (p1.scheme, p1.hostname, p1.port) == (p2.scheme, p2.hostname, p2.port) from aspen import Response from crypto import constant_time_compare, get_random_string REASON_NO_REFERER = "Referer checking failed - no Referer." REASON_BAD_REFERER = "Referer checking failed - %s does not match %s." REASON_NO_CSRF_COOKIE = "CSRF cookie not set." REASON_BAD_TOKEN = "CSRF token missing or incorrect." TOKEN_LENGTH = 32 TIMEOUT = 60 * 60 * 24 * 7 * 52 def _get_new_csrf_key(): return get_random_string(TOKEN_LENGTH) def _sanitize_token(token): # Allow only alphanum, and ensure we return a 'str' for the sake # of the post processing middleware. if len(token) > TOKEN_LENGTH: return _get_new_csrf_key() token = re.sub('[^a-zA-Z0-9]+', '', str(token.decode('ascii', 'ignore'))) if token == "": # In case the cookie has been truncated to nothing at some point. return _get_new_csrf_key() return token def _is_secure(request): import gittip return gittip.canonical_scheme == 'https' def _get_host(request): """Returns the HTTP host using the request headers. """ return request.headers.get('X-Forwarded-Host', request.headers['Host']) def inbound(request): """Given a Request object, reject it if it's a forgery. """ try: csrf_token = request.headers.cookie.get('csrf_token') csrf_token = '' if csrf_token is None else csrf_token.value csrf_token = _sanitize_token(csrf_token) # Use same token next time request.context['csrf_token'] = csrf_token except KeyError: csrf_token = None # Generate token and store it in the request, so it's # available to the view. request.context['csrf_token'] = _get_new_csrf_key() # Assume that anything not defined as 'safe' by RC2616 needs protection if request.line.method not in ('GET', 'HEAD', 'OPTIONS', 'TRACE'): if _is_secure(request): # Suppose user visits http://example.com/ # An active network attacker (man-in-the-middle, MITM) sends a # POST form that targets https://example.com/detonate-bomb/ and # submits it via JavaScript. # # The attacker will need to provide a CSRF cookie and token, but # that's no problem for a MITM and the session-independent # nonce we're using. So the MITM can circumvent the CSRF # protection. This is true for any HTTP connection, but anyone # using HTTPS expects better! For this reason, for # https://example.com/ we need additional protection that treats # http://example.com/ as completely untrusted. Under HTTPS, # Barth et al. found that the Referer header is missing for # same-domain requests in only about 0.2% of cases or less, so # we can use strict Referer checking. referer = request.headers.get('Referer') if referer is None: raise Response(403, REASON_NO_REFERER) # Note that get_host() includes the port. good_referer = 'https://%s/' % _get_host(request) if not same_origin(referer, good_referer): reason = REASON_BAD_REFERER % (referer, good_referer) raise Response(403, reason) if csrf_token is None: # No CSRF cookie. For POST requests, we insist on a CSRF cookie, # and in this way we can avoid all CSRF attacks, including login # CSRF. raise Response(403, REASON_NO_CSRF_COOKIE) # Check non-cookie token for match. request_csrf_token = "" if request.line.method == "POST": request_csrf_token = request.body.get('csrf_token', '') if request_csrf_token == "": # Fall back to X-CSRF-TOKEN, to make things easier for AJAX, # and possible for PUT/DELETE. request_csrf_token = request.headers.get('X-CSRF-TOKEN', '') if not constant_time_compare(request_csrf_token, csrf_token): raise Response(403, REASON_BAD_TOKEN) def outbound(response): csrf_token = response.request.context.get('csrf_token') # If csrf_token is unset, then inbound was never called, probaby because # another inbound hook short-circuited. if csrf_token is None: return response # Set the CSRF cookie even if it's already set, so we renew # the expiry timer. response.headers.cookie['csrf_token'] = csrf_token cookie = response.headers.cookie['csrf_token'] # I am not setting domain, because it is supposed to default to what we # want: the domain of the object requested. #cookie['domain'] cookie['path'] = '/' cookie['expires'] = rfc822.formatdate(time.time() + TIMEOUT) #cookie['httponly'] = "Yes, please." Want js access for this. # Content varies with the CSRF cookie, so set the Vary header. patch_vary_headers(response, ('Cookie',))
cc0-1.0
-2,196,269,443,329,078,800
36.388571
81
0.643741
false
3.810716
false
false
false
ksang/error-extractor
lib/markdown.py
1
2193
''' MarkDown format generator ''' class MarkDown: 'convert raw text to markdown syntax' def __init__(self): self.escape_table = {"\\": "\\\\", "`": "\`", "*": "\*", "_": "\_", "{": "\{", "}": "\}", "[": "\[", "]": "\]", "(": "\(", ")": "\)", "#": "\#", "+": "\+", "-": "\-", ".": "\.", "|": "\|" } def __escape(self, data): return "".join(self.escape_table.get(c,c) for c in data) def __convert_lines(self, text='', prefix='', suffix='', olist=False): if type(text) is str: if olist: return '1. ' + self.__escape(text) else: return prefix + self.__escape(text) + suffix elif type(text) is list: for idx, t in enumerate(text): if olist: nt = str(idx+1) + '. ' + self.__escape(t) else: nt = prefix + self.__escape(t) + suffix text[idx] = nt return text return '' def text(self, text): return self.__convert_lines(text) def error(self, text): return self.__convert_lines(text) def title(self, text): return self.__convert_lines(text, '##') def subtitle(self, text): return self.__convert_lines(text, '###') def ssubtitle(self, text): return self.__convert_lines(text, '####') def bold(self, text): return self.__convert_lines(text, '**', '**') def line_breaker(self, count=1): if count > 1: ret = [] for i in range(0,count): ret.append("-------------") return ret return "-------------" def reference(self, text): return self.__convert_lines(text, '>') def ordered_list(self, data): return self.__convert_lines(data, olist=True) def unordered_list(self, data): return self.__convert_lines(data, '- ')
mit
8,219,994,262,008,899,000
29.472222
74
0.401277
false
4.153409
false
false
false
levilucio/SyVOLT
ECore_Copier_MM/transformation-Large/HeoperationlefteAnnotationsSolveRefEOperationEAnnotationEOperationEAnnotation.py
1
5078
from core.himesis import Himesis class HeoperationlefteAnnotationsSolveRefEOperationEAnnotationEOperationEAnnotation(Himesis): def __init__(self): """ Creates the himesis graph representing the AToM3 model HeoperationlefteAnnotationsSolveRefEOperationEAnnotationEOperationEAnnotation. """ # Flag this instance as compiled now self.is_compiled = True super(HeoperationlefteAnnotationsSolveRefEOperationEAnnotationEOperationEAnnotation, self).__init__(name='HeoperationlefteAnnotationsSolveRefEOperationEAnnotationEOperationEAnnotation', num_nodes=27, edges=[]) # Add the edges self.add_edges([[0, 6], [6, 5], [0, 8], [8, 7], [1, 10], [10, 9], [1, 12], [12, 11], [5, 3], [3, 7], [9, 4], [4, 11], [9, 13], [13, 5], [11, 14], [14, 7], [9, 15], [15, 16], [17, 18], [18, 16], [17, 19], [19, 20], [11, 21], [21, 22], [23, 24], [24, 22], [23, 25], [25, 26], [0, 2], [2, 1]]) # Set the graph attributes self["mm__"] = ['HimesisMM'] self["name"] = """eoperationlefteAnnotationsSolveRefEOperationEAnnotationEOperationEAnnotation""" self["GUID__"] = 5816395996192583717 # Set the node attributes self.vs[0]["mm__"] = """MatchModel""" self.vs[0]["GUID__"] = 8044970359314201378 self.vs[1]["mm__"] = """ApplyModel""" self.vs[1]["GUID__"] = 1048396254969054700 self.vs[2]["mm__"] = """paired_with""" self.vs[2]["GUID__"] = 4558494274367220420 self.vs[3]["associationType"] = """eAnnotations""" self.vs[3]["mm__"] = """directLink_S""" self.vs[3]["GUID__"] = 2115966609539548178 self.vs[4]["associationType"] = """eAnnotations""" self.vs[4]["mm__"] = """directLink_T""" self.vs[4]["GUID__"] = 1458168688512188010 self.vs[5]["name"] = """""" self.vs[5]["classtype"] = """EOperation""" self.vs[5]["mm__"] = """EOperation""" self.vs[5]["cardinality"] = """+""" self.vs[5]["GUID__"] = 3498868833057656827 self.vs[6]["mm__"] = """match_contains""" self.vs[6]["GUID__"] = 1307123802579665829 self.vs[7]["name"] = """""" self.vs[7]["classtype"] = """EAnnotation""" self.vs[7]["mm__"] = """EAnnotation""" self.vs[7]["cardinality"] = """+""" self.vs[7]["GUID__"] = 5438034355437875093 self.vs[8]["mm__"] = """match_contains""" self.vs[8]["GUID__"] = 347179529733664915 self.vs[9]["name"] = """""" self.vs[9]["classtype"] = """EOperation""" self.vs[9]["mm__"] = """EOperation""" self.vs[9]["cardinality"] = """1""" self.vs[9]["GUID__"] = 2062346932891848348 self.vs[10]["mm__"] = """apply_contains""" self.vs[10]["GUID__"] = 7369516320927345833 self.vs[11]["name"] = """""" self.vs[11]["classtype"] = """EAnnotation""" self.vs[11]["mm__"] = """EAnnotation""" self.vs[11]["cardinality"] = """1""" self.vs[11]["GUID__"] = 8754100728367131831 self.vs[12]["mm__"] = """apply_contains""" self.vs[12]["GUID__"] = 7003937250653372044 self.vs[13]["mm__"] = """backward_link""" self.vs[13]["type"] = """ruleDef""" self.vs[13]["GUID__"] = 1875949330034786489 self.vs[14]["mm__"] = """backward_link""" self.vs[14]["type"] = """ruleDef""" self.vs[14]["GUID__"] = 5523630539087955496 self.vs[15]["mm__"] = """hasAttribute_T""" self.vs[15]["GUID__"] = 2583131276534883053 self.vs[16]["name"] = """ApplyAttribute""" self.vs[16]["Type"] = """'String'""" self.vs[16]["mm__"] = """Attribute""" self.vs[16]["GUID__"] = 1181219036459105099 self.vs[17]["name"] = """eq_""" self.vs[17]["mm__"] = """Equation""" self.vs[17]["GUID__"] = 1530653583095677969 self.vs[18]["mm__"] = """leftExpr""" self.vs[18]["GUID__"] = 40237161015443598 self.vs[19]["mm__"] = """rightExpr""" self.vs[19]["GUID__"] = 7359435342082954621 self.vs[20]["name"] = """solveRef""" self.vs[20]["Type"] = """'String'""" self.vs[20]["mm__"] = """Constant""" self.vs[20]["GUID__"] = 6720296362885197874 self.vs[21]["mm__"] = """hasAttribute_T""" self.vs[21]["GUID__"] = 7435363414672850123 self.vs[22]["name"] = """ApplyAttribute""" self.vs[22]["Type"] = """'String'""" self.vs[22]["mm__"] = """Attribute""" self.vs[22]["GUID__"] = 206401628991295002 self.vs[23]["name"] = """eq_""" self.vs[23]["mm__"] = """Equation""" self.vs[23]["GUID__"] = 3235173079800635441 self.vs[24]["mm__"] = """leftExpr""" self.vs[24]["GUID__"] = 7728551407519580789 self.vs[25]["mm__"] = """rightExpr""" self.vs[25]["GUID__"] = 98859355129756548 self.vs[26]["name"] = """solveRef""" self.vs[26]["Type"] = """'String'""" self.vs[26]["mm__"] = """Constant""" self.vs[26]["GUID__"] = 6740085100061687672
mit
2,488,468,065,536,911,000
48.300971
298
0.522844
false
3.026222
false
false
false
Travelport-Czech/apila
tasks/Lambda.py
1
8808
import zipfile import tempfile import shutil import os import os.path import hashlib import base64 import json import logging import subprocess import re import botocore import tasks.name_constructor as name_constructor import tasks.bototools as bototools from tasks.Task import Task class Lambda(Task): """Create a lambda function and upload the code from given folder""" known_params = { 'name': 'function name', 'code': "path to the folder with function's source code", 'role': 'name of a role for the execution of the function', 'runtime': "name and a version of interpret for the execution i.e.: 'nodejs4.3'", 'handler': 'entrypoint to the function code', 'description': 'short description of the function', 'timeout': 'maximal time for the execution of the function', 'memory_size': 'amount of memory reserved for the execution of the function', 'publish': "I'm not sure, give always True ;-)", 'babelize': "flag if the source must be converted by babel (default True)", 'babelize_skip': "list of modules to be skipped by babel" } required_params = ('name', 'code', 'role', 'runtime', 'handler') required_configs = ('user', 'branch') task_name = 'lambda' def __str__(self): if self.name: return self.name else: return 'Create a lambda function %s' % (self.params['description'] if 'description' in self.params else self.params['name']) def get_files(self, path, rel_part): out = [] for root, dirs, files in os.walk(os.path.join(path, rel_part)): rel_root = root[len(path):].lstrip('/') for filename in files: out.append((os.path.join(root, filename), os.path.join(rel_root, filename))) return sorted(out) def create_zip(self, files): zip_name = tempfile.mkstemp(suffix='.zip', prefix='lambda_')[1] with zipfile.ZipFile(zip_name, 'w') as myzip: for filedef in files: os.utime(filedef[0], (946681200, 946681200)) # date '+%s' -d '2000-01-01' myzip.write(filedef[0], filedef[1]) zip_data = open(zip_name, 'rb').read() os.unlink(zip_name) return zip_data def run_npm_install(self, path): cwd = os.getcwd() os.chdir(path) try: npm_out = subprocess.check_output(['npm', 'install', '--production'], stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: logging.error(e.output) raise e finally: os.chdir(cwd) def babelize(self, base_path, clean_dir, babelized_dir): cwd = os.getcwd() if os.path.exists('../node_modules/.bin/babel'): os.chdir('..') if not os.path.exists('node_modules/.bin/babel'): os.chdir(base_path) preset_base = os.getcwd() try: babel_out = subprocess.check_output(' '.join(['node_modules/.bin/babel', '--no-babelrc --presets', os.path.join(preset_base, 'node_modules', 'babel-preset-es2015-node4'), '--copy-files', '--out-dir', babelized_dir, clean_dir]), stderr=subprocess.STDOUT, shell=True) except subprocess.CalledProcessError as e: logging.error('cwd: '+os.getcwd()) logging.error(e.output) raise e finally: os.chdir(cwd) def clean_packages(self, files, path_to_remove): r_shasum = re.compile(r'"_shasum"[^,]+,') for filename, rel in files: if filename.endswith('package.json'): with open(filename) as fin: text = fin.read() new_text = r_shasum.sub('', text.replace(path_to_remove, '/tmp')) with open(filename, 'w') as fout: fout.write(new_text) def prepare_zipped_code(self, code_path, babelize): excluded_mods = self.params['babelize_skip'] if 'babelize_skip' in self.params else set() work_dir = tempfile.mkdtemp(prefix='lambda_') clean_dir = os.path.join(work_dir, 'clean') os.mkdir(clean_dir) shutil.copytree(os.path.join(code_path, 'app'), os.path.join(clean_dir, 'app')) shutil.copy(os.path.join(code_path, 'package.json'), os.path.join(clean_dir, 'package.json')) self.run_npm_install(clean_dir) if babelize: babelized_dir = os.path.join(work_dir, 'babelized') babelized_app_dir = os.path.join(babelized_dir, 'app') babelized_mod_dir = os.path.join(babelized_dir, 'node_modules') clean_mod_dir = os.path.join(clean_dir, 'node_modules') os.mkdir(babelized_dir) os.mkdir(babelized_app_dir) os.mkdir(babelized_mod_dir) self.babelize(code_path, os.path.join(clean_dir, 'app'), babelized_app_dir) for module_name in os.listdir(clean_mod_dir): src = os.path.join(clean_mod_dir, module_name) dest = os.path.join(babelized_mod_dir, module_name) if module_name in excluded_mods: shutil.copytree(src, dest) else: os.mkdir(dest) self.babelize(code_path, src, dest) files = self.get_files(babelized_app_dir, '') + self.get_files(babelized_dir, 'node_modules') else: files = self.get_files(os.path.join(clean_dir, 'app'), '') + self.get_files(clean_dir, 'node_modules') self.clean_packages(files, work_dir) files_to_zip = [file_name for file_name in files if not file_name[0].endswith('.SAMPLE')] zip_data = self.create_zip(files_to_zip) shutil.rmtree(work_dir) return zip_data def run(self, clients, cache): client = clients.get('lambda') iam_client = clients.get('iam') function_name = name_constructor.lambda_name(self.params['name'], self.config['user'], self.config['branch']) role_arn = bototools.get_role_arn(iam_client, self.params['role']) description = (self.params['description'] if 'description' in self.params else '') + self.get_version_description() try: zip_data = self.prepare_zipped_code(self.params['code'], True if 'babelize' not in self.params else self.params['babelize']) except Exception as e: logging.exception(str(e)) return (False, str(e)) if role_arn is None: return (False, "Required role '%s' not found" % self.params['role']) try: function_conf = client.get_function_configuration(FunctionName=function_name) except botocore.exceptions.ClientError: return self.create(client, cache, function_name, role_arn, zip_data, description) if role_arn == function_conf['Role'] and \ self.params['runtime'] == function_conf['Runtime'] and \ self.params['handler'] == function_conf['Handler'] and \ (description == function_conf['Description']) and \ ('timeout' not in self.params or self.params['timeout'] == function_conf['Timeout']) and \ ('memory_size' not in self.params or self.params['memory_size'] == function_conf['MemorySize']): result = '' else: self.update(client, function_name, role_arn, description) result = self.CHANGED sha256_sumator = hashlib.sha256() sha256_sumator.update(zip_data) sha256_sum = sha256_sumator.digest() sha256_sum_encoded = base64.b64encode(sha256_sum) if sha256_sum_encoded != function_conf['CodeSha256']: client.update_function_code(FunctionName=function_name, ZipFile=zip_data, Publish=self.params['publish'] if 'publish' in self.params else None) result = self.CHANGED cache.put('lambda', function_name, function_conf['FunctionArn']) return (True, result) def update(self, client, function_name, role_arn, description): lambda_def = { 'FunctionName': function_name, 'Runtime': self.params['runtime'], 'Role': role_arn, 'Handler': self.params['handler'] } lambda_def['Description'] = description if 'timeout' in self.params: lambda_def['Timeout'] = self.params['timeout'] if 'memory_size' in self.params: lambda_def['MemorySize'] = self.params['memory_size'] client.update_function_configuration(**lambda_def) def create(self, client, cache, function_name, role_arn, zip_data, description): lambda_def = { 'FunctionName': function_name, 'Runtime': self.params['runtime'], 'Role': role_arn, 'Handler': self.params['handler'], 'Code': {'ZipFile': zip_data} } lambda_def['Description'] = description if 'timeout' in self.params: lambda_def['Timeout'] = self.params['timeout'] if 'memory_size' in self.params: lambda_def['MemorySize'] = self.params['memory_size'] if 'publish' in self.params: lambda_def['Publish'] = self.params['publish'] response = client.create_function(**lambda_def) cache.put('lambda', function_name, response['FunctionArn']) return (True, self.CREATED) def get_version_description(self): manifest_path = os.path.join(self.params['code'], 'package.json') if os.path.exists(manifest_path): manifest = json.load(open(manifest_path)) if 'version' in manifest: return ' (v%s)' % manifest['version'] return ''
mit
2,048,214,956,815,787,300
41.346154
271
0.655654
false
3.377301
false
false
false