repo_name
stringlengths
5
92
path
stringlengths
4
232
copies
stringclasses
18 values
size
stringlengths
4
7
content
stringlengths
736
1.04M
license
stringclasses
15 values
hash
int64
-9,222,983,980,000,580,000
9,223,102,107B
line_mean
float64
6.51
99.9
line_max
int64
15
997
alpha_frac
float64
0.25
0.97
autogenerated
bool
1 class
resync/resync
tests/test_resync-build_script.py
1
2112
import sys import unittest import io from resync.resource import Resource from resync.resource_list import ResourceList from resync.capability_list import CapabilityList from resync.sitemap import Sitemap, SitemapIndexError, SitemapParseError import subprocess def run_resync(args): args.insert(0, './resync-build') proc = subprocess.Popen(args, stdout=subprocess.PIPE) (out, err) = proc.communicate() return(out) class TestClientLinkOptions(unittest.TestCase): def test01_no_links(self): xml = run_resync(['--write-resourcelist', 'http://example.org/t', 'tests/testdata/dir1']) rl = ResourceList() rl.parse(fh=io.BytesIO(xml)) self.assertEqual(len(rl), 2) self.assertEqual(rl.link('describedby'), None) def test02_resource_list_links(self): xml = run_resync(['--write-resourcelist', '--describedby-link=a', '--sourcedescription-link=b', # will be ignored '--capabilitylist-link=c', 'http://example.org/t', 'tests/testdata/dir1']) rl = ResourceList() rl.parse(fh=io.BytesIO(xml)) self.assertEqual(len(rl), 2) self.assertNotEqual(rl.link('describedby'), None) self.assertEqual(rl.link('describedby')['href'], 'a') self.assertNotEqual(rl.link('up'), None) self.assertEqual(rl.link('up')['href'], 'c') def test03_capability_list_links(self): xml = run_resync(['--write-capabilitylist=resourcelist=rl,changedump=cd', '--describedby-link=a', '--sourcedescription-link=b', '--capabilitylist-link=c']) # will be ignored capl = CapabilityList() capl.parse(fh=io.BytesIO(xml)) self.assertEqual(len(capl), 2) self.assertNotEqual(capl.link('describedby'), None) self.assertEqual(capl.link('describedby')['href'], 'a') self.assertNotEqual(capl.link('up'), None) self.assertEqual(capl.link('up')['href'], 'b')
apache-2.0
5,473,029,633,957,304,000
37.4
81
0.600852
false
mayuanucas/notes
python/code/linearunit.py
1
1443
#!/usr/bin/env python # -*- coding: utf-8 -*- from perceptron import Perceptron class LinearUnit(Perceptron): def __init__(self, input_num, activator): ''' 初始化感知器,设置输入参数的个数,以及激活函数。 ''' Perceptron.__init__(self, input_num, activator) def func(x): ''' 定义激活函数func ''' return x def get_training_dataset(): ''' 构建训练数 # 输入向量列表,每一项代表工作年限 ''' all_input_vecs = [[5], [3], [8], [1.4], [10.1]] # 期望的输出列表,注意要与输入一一对应,代表 月薪 labels = [5500, 2300, 7600, 1800, 11400] return all_input_vecs, labels def train_linear_unit(): ''' 使用数据训练线性单元 ''' # 创建感知器,输入参数个数为1,激活函数为func lu = LinearUnit(1, func) # 训练,迭代10轮, 学习速率为0.1 all_input_vecs, labels = get_training_dataset() lu.train(all_input_vecs, labels, 10, 0.01) #返回训练好的感知器 return lu if __name__ == '__main__': # 训练线性单元 linear_unit = train_linear_unit() # 打印训练获得的权重 print(linear_unit) # 测试 print('Work 3.4 years, monthly salary = %.2f' % linear_unit.predict([3.4])) print('Work 15 years, monthly salary = %.2f' % linear_unit.predict([15])) print('Work 1.5 years, monthly salary = %.2f' % linear_unit.predict([1.5])) print('Work 6.3 years, monthly salary = %.2f' % linear_unit.predict([6.3]))
apache-2.0
-2,600,775,387,799,649,300
22.28
76
0.647463
false
facoy/facoy
Searcher/_JaccardSearcher.py
1
2617
# from java.io import File, StringReader # from org.apache.lucene.index import IndexReader, Term # from org.apache.lucene.search import IndexSearcher, FuzzyQuery # from org.apache.lucene.store import SimpleFSDirectory # from org.apache.lucene.analysis.core import KeywordAnalyzer # from org.apache.lucene.util import Version # from org.apache.lucene.queryparser.classic import MultiFieldQueryParser, QueryParser # from collections import Counter # # indexDir = File("/tmp/github") # # # 1. open the index # analyzer = KeywordAnalyzer() # index = SimpleFSDirectory(indexDir) # reader = IndexReader.open(index) # n_docs = reader.numDocs() # print("Index contains %d documents." % n_docs) # # # 2. parse the query from the command line # # a = {"typed_method_call": WhitespaceAnalyzer()} # # wrapper_analyzer = PerFieldAnalyzerWrapper(analyzer, a) # # query_string = "HttpURLConnection.disconnect Exception.printStackTrace BufferedReader.close HttpURLConnection.setRequestProperty HttpURLConnection.setRequestMethod DataOutputStream.writeBytes HttpURLConnection.getInputStream DataOutputStream.close HttpURLConnection.setUseCaches StringBuffer.append URL.openConnection HttpURLConnection.getOutputStream Integer.toString String.getBytes StringBuffer.toString HttpURLConnection.setDoOutput BufferedReader.readLine DataOutputStream.flush HttpURLConnection.setDoInput" # query_parser = MultiFieldQueryParser(Version.LUCENE_CURRENT, ["typed_method_call"], analyzer) # # # #base_query = getSpanNearQuery(analyzer, query_string) # # base_query = query_parser.parse(query_string) # # #http://shaierera.blogspot.com/2013/09/boosting-documents-in-lucene.html # # boost_query = FunctionQuery( LongFieldSource("view_count")) # #query = CustomScoreQuery(base_query, boost_query) # # # queryparser = QueryParser(Version.LUCENE_CURRENT, "title", analyzer) # # query = queryparser.parse(query_string) # # # 3. search the index for the query # # We retrieve and sort all documents that match the query. # # In a real application, use a TopScoreDocCollector to sort the hits. # searcher = IndexSearcher(reader) # hits = searcher.search(base_query, 10).scoreDocs # # # 4. display results # print(query_string) # print("Found %d hits:" % len(hits)) # # api_acc = [] # for i, hit in enumerate(hits): # doc = searcher.doc(hit.doc) # apis = [d.stringValue() for d in doc.getFields("typed_method_call")] # api_acc.extend(apis) # #retrieve_ranked_apis(doc.get("answer_id")) # print("%d. %s Method: %s, Score: %s" % (i + 1, doc.get("file"), apis, hit.score)) # # print Counter(api_acc).most_common(5) # # 5. close resources
apache-2.0
-2,467,094,502,645,306,400
44.912281
515
0.757356
false
ImaginaryLandscape/iscape-jobboard
jobboard/models.py
1
9517
################################################################################ # JobBoard: a simple Django-based job board # Copyright (c) 2009, Imaginary Landscape # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * 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. # * Neither the name of the Imaginary Landscape nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE 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 HOLDER 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. ################################################################################ import datetime from django.db import models from django.conf import settings ################################################################################ ## Utility functions ################################################################################ def calculate_post_expires(): """ Generate an expiration date based on settings.JOBBOARD_POSTS_EXPIRE_DAYS Returns: A date object that is JOBBOARD_POSTS_EXPIRE_DAYS in the future """ post_expire_days = datetime.timedelta( days=settings.JOBBOARD_POSTS_EXPIRE_DAYS) return datetime.date.today() + post_expire_days def default_position(): if Position.objects.count() == 1: return Position.objects.all()[0] else: return None ################################################################################ ## Models ################################################################################ class NotifyEmail(models.Model): """ An email address where job post notifications will be sent. Field attributes: email: the email address where the notification will be sent """ email = models.EmailField() class Admin: pass def __unicode__(self): return self.email class Position(models.Model): """ An available position for working, be it a cook, waitress or dental hygenist. These appear on drop-down forms for those submitting job posts or applicant posts. Field Attributes: name: The name of the position """ name = models.CharField(max_length=80) class Admin: pass def __unicode__(self): return self.name class JobPost(models.Model): """ A post for an available job that people can apply to. These are usually user-submitted, and then approved by an administrator. Once they are approved, they should show up on the job listing until the time they expire. Field Attributes: posters_name: The name of the person or company offering the job work_hours: Whatever kind of hours the person might be working (9-5, late shift, full time, part time, whatever) description: Description of this job position: Any one of the positions in models.Position email: An email address relevant to this job (probably for contact purposes) contact_information: Any other contact information when_posted: The timestamp for when this post was submitted approved: Whether or not this application was approved to be listed on the site. expiration_date: The date on which this post will no longer show up on the listing. """ approved = models.BooleanField(default=False) when_posted = models.DateTimeField(default=datetime.datetime.today) expiration_date = models.DateField( default=calculate_post_expires, help_text=( "This field defaults to " "%s days from user submission." % settings.JOBBOARD_POSTS_EXPIRE_DAYS)) posters_name = models.CharField("Poster's name", max_length=80) work_hours = models.CharField(max_length=80, blank=True) description = models.TextField(blank=True) position = models.ForeignKey(Position, default=default_position) email = models.EmailField('e-mail', blank=True) contact_information = models.TextField('How to apply') class Meta: ordering = ['-when_posted'] class Admin: fields = ( ('Approval Status', {'fields': ['approved']}), ('Dates', {'fields': ['when_posted', 'expiration_date']}), ('Details', {'fields': ['posters_name', 'work_hours', 'description', 'position', 'email', 'contact_information']})) list_filter = ['approved'] list_display = [ 'posters_name', 'position', 'when_posted', 'expiration_date', 'approved'] def __unicode__(self): return u"%s @ %s" % ( self.posters_name, self.when_posted.strftime('%m-%d-%Y %I:%M%p')) class ApplicantPost(models.Model): """ A post for a person who is seeking employment. These are usually user-submitted, and then approved by an administrator. Once they are approved, they should show up on the job listing until the time they expire. Field Attributes: first_name: First name of the person seeking employment last_name: Last name of the person seeking employment phone_number: A number at which this person can be contacted email: An email address at which this person can be contacted position: Any one of the positions in models.Position resume: Plaintext version of this person's resume full_time: Whether or not this person is interested in full time employment part_time: Whether or not this person is interested in part time employment when_posted: The timestamp for when this post was submitted approved: Whether or not this application was approved to be listed on the site. expiration_date: The date on which this post will no longer show up on the listing. """ approved = models.BooleanField(default=False) when_posted = models.DateTimeField(default=datetime.datetime.today) expiration_date = models.DateField( default=calculate_post_expires, help_text=( "This field defaults to " "%s days from user submission." % settings.JOBBOARD_POSTS_EXPIRE_DAYS)) first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) phone_number = models.CharField('phone', max_length=25) email = models.EmailField('e-mail', blank=True) position = models.ForeignKey(Position, default=default_position) resume = models.TextField() full_time = models.BooleanField('full-time') part_time = models.BooleanField('part-time') class Meta: ordering = ['-when_posted'] class Admin: fields = ( ('Approval Status', {'fields': ['approved']}), ('Dates', {'fields': ['when_posted', 'expiration_date']}), ('Details', {'fields': ['first_name', 'last_name', 'phone_number', 'email', 'position', 'resume', 'full_time', 'part_time']})) list_filter = ['approved'] list_display = [ 'full_name', 'position', 'when_posted', 'expiration_date', 'approved'] def hours_string(self): """ from dunder_mifflin import papers # WARNING: Malicious operation ahead A nice, comma-joined list of the type of hours this person is interested in. If the user selected part_time and full_time for example, this would show up as: "part time, full_time" If the user doesn't select anything, it will just return the string "unspecified". Returns: Either a comma-joined string of the hours this person is interested in, or "unspecified" if the user didn't select any. """ hours = [] if self.full_time: hours.append('full time') if self.part_time: hours.append('part time') if hours: return ', '.join(hours) else: return 'unspecified' def __unicode__(self): return u"%s %s @ %s" % ( self.first_name, self.last_name, self.when_posted.strftime('%m-%d-%Y %I:%M%p')) def full_name(self): return u"%s %s" % (self.first_name, self.last_name)
bsd-3-clause
-3,085,676,571,152,319,000
35.745174
81
0.609436
false
juliojsb/sarviewer
plotters/matplotlib/swap.py
1
2062
#!/usr/bin/env python2 """ Author :Julio Sanz Website :www.elarraydejota.com Email :juliojosesb@gmail.com Description :Script to create a graph about swap usage Dependencies :Python 2.x, matplotlib Usage :python swap.py License :GPLv3 """ import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import csv from datetime import datetime import matplotlib.dates # ====================== # VARIABLES # ====================== # Aesthetic parameters plt.rcParams.update({'font.size': 8}) plt.rcParams['lines.linewidth'] = 1.5 time_format = matplotlib.dates.DateFormatter('%H:%M:%S') plt.gca().xaxis.set_major_formatter(time_format) plt.gcf().autofmt_xdate() # Time (column 0) x = [] # Data arrays swap_free = [] swap_used = [] # ====================== # FUNCTIONS # ====================== def generate_graph(): with open('../../data/swap.dat', 'r') as csvfile: data_source = csv.reader(csvfile, delimiter=' ', skipinitialspace=True) for row in data_source: # [0] column is a time column # Convert to datetime data type a = datetime.strptime((row[0]),'%H:%M:%S') x.append((a)) # The remaining columns contain data swap_free.append(str(int(row[1])/1024)) swap_used.append(str(int(row[2])/1024)) # Plot lines plt.plot(x,swap_used, label='Used', color='r', antialiased=True) plt.plot(x,swap_free, label='Free', color='g', antialiased=True) # Graph properties plt.xlabel('Time',fontstyle='italic') plt.ylabel('SWAP (MB)',fontstyle='italic') plt.title('SWAP usage graph') plt.grid(linewidth=0.4, antialiased=True) plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), ncol=2, fancybox=True, shadow=True) plt.autoscale(True) # Graph saved to PNG file plt.savefig('../../graphs/swap.png', bbox_inches='tight') #plt.show() # ====================== # MAIN # ====================== if __name__ == '__main__': generate_graph()
gpl-3.0
-1,830,321,414,085,074,400
26.878378
99
0.586324
false
leonro/magpy-git
magpy/acquisition/palmacqprotocol.py
1
11090
import sys, time, os, socket import struct, binascii, re, csv from datetime import datetime, timedelta from twisted.protocols.basic import LineReceiver from twisted.internet import reactor from twisted.python import usage, log from twisted.internet.serialport import SerialPort from twisted.web.server import Site from twisted.web.static import File try: # version > 0.8.0 from autobahn.wamp1.protocol import exportRpc except: from autobahn.wamp import exportRpc iddict = {'f': '10', 'x': '11', 'y': '12', 'z': '13', 'df': '14', 't': '30', 'rh': '33', 'p': '35', 'w': '38'} """ 0: clientname -- str (atlas) 1: timestamp (PC) -- str (2013-01-23 12:10:32.712475) 2: date (PC) -- str (2013-01-23) 3: outtime (PC) -- str (12:10:32.712475) 4: timestamp (sensor) -- str (2013-01-23 12:10:32.712475) 5: GPS coordinates -- str (??.??N ??.??E) 9: Sensor Description -- str (to be found in the adict) 10: f -- float (48633.04) [nT] 11: x -- float (20401.3) [nT] 12: y -- float (-30.0) [nT] 13: z -- float (43229.7) [nT] 14: df -- float (0.06) [nT] 30: T (ambient) -- float (7.2) [C] 31: T (sensor) -- float (10.0) [C] 32: T (electronics) -- float (12.5) [C] 33: rh (relative humidity) -- float (99.0) [%] 34: T (dewpoint) -- float (6.0) [C] 38: W (weight) -- float (24.0042) [g] 40: Error code (POS1) -- float (80) [-] 60: VDD (support voltage) -- float (5.02) [V] 61: VAD (measured voltage) -- float (2.03) [V] 62: VIS (measured voltage) -- float (0.00043) [V] """ def timeToArray(timestring): # Converts time string of format 2013-12-12 23:12:23.122324 # to an array similiar to a datetime object try: splittedfull = timestring.split(' ') splittedday = splittedfull[0].split('-') splittedsec = splittedfull[1].split('.') splittedtime = splittedsec[0].split(':') datearray = splittedday + splittedtime datearray.append(splittedsec[1]) datearray = map(int,datearray) return datearray except: log.msg('Error while extracting time array') return [] def dataToFile(outputdir, sensorid, filedate, bindata, header): # File Operations try: hostname = socket.gethostname() path = os.path.join(outputdir,hostname,sensorid) # outputdir defined in main options class if not os.path.exists(path): os.makedirs(path) savefile = os.path.join(path, sensorid+'_'+filedate+".bin") if not os.path.isfile(savefile): with open(savefile, "wb") as myfile: myfile.write(header + "\n") myfile.write(bindata + "\n") else: with open(savefile, "a") as myfile: myfile.write(bindata + "\n") except: log.err("PalmAcq - Protocol: Error while saving file") ## PalmAcq protocol ## -------------------- class PalmAcqProtocol(LineReceiver): """ Protocol to read Arduino data (usually from ttyACM0) Tested so far only for Arduino Uno on a Linux machine The protocol works only if the serial output follows the MagPy convention: Up to 99 Sensors are supported identified by unique sensor names and ID's. ARDUINO OUTPUT: - serial output on ttyACM0 needs to follow the MagPy definition: Three data sequences are supported: 1.) The meta information The meta information line contains all information for a specific sensor. If more than one sensor is connected, then several meta information lines should be sent (e.g. M1:..., M2:..., M99:...) Meta lines should be resent once in a while (e.g. every 10-100 data points) Example: M1: SensorName: MySensor, SensorID: 12345, SensorRevision: 0001 2.) The header line The header line contains information on the provided data for each sensor. The typical format includes the MagPy key, the actual Variable and the unit. Key and Variable are separeted by an underscore, unit is provided in brackets. Like the Meta information the header should be sent out once in a while Example: H1: f_F [nT], t1_Temp [deg C], var1_Quality [None], var2_Pressure [mbar] 3.) The data line: The data line containes all data from a specific sensor Example: D1: 46543.7898, 6.9, 10, 978.000 - recording starts after meta and header information have been received MARTAS requirements: - add the following line to the sensor.txt ARDUINO ACM0 9600 - on the MARTAS machine an additional information file will be created containing the sensor information for connected ARDUINO boards: arduinolist.csv: "HMC5883_12345_0001","['x', 'y', 'z']" This file is used by the MARCOS machine to identify connected sensors and their keys """ delimiter = "\r" ## need a reference to our WS-MCU gateway factory to dispatch PubSub events ## def __init__(self, wsMcuFactory, sensor, outputdir): self.wsMcuFactory = wsMcuFactory self.sensorid = sensor self.hostname = socket.gethostname() self.outputdir = outputdir self.sensor = '' self.sensordict = {} self.ConversionConstant = 40/4/float(int("0x800000",16)) eventstring = "evt0,evt1,evt3,evt11,evt12,evt13,evt32,evt60,evt99" self.eventlist = eventstring.split(',') def connectionMade(self): log.msg('%s connected.' % self.sensorid) def extractPalmAcqData(self, line): """ Method to convert hexadecimals to doubles Returns a data array """ # INTERPRETING INCOMING DATA AND CONVERTING HEXDECIMALS TO DOUBLE if line.startswith('*'): try: data = [] chunks = [] line = line.strip('*') chunks.append(line[:6]) chunks.append(line[6:12]) chunks.append(line[12:18]) trigger = line[18] ar = line.split(':') if len(ar) == 2: extended = ar[1] chunks.append(extended[:4]) chunks.append(extended[4:8]) chunks.append(extended[8:12]) chunks.append(extended[12:16]) chunks.append(extended[16:20]) for idx, chunk in enumerate(chunks): if len(chunk) == 6: val = hex(int('0x'+chunk,16) ^ int('0x800000',16)) val = hex(int(val,16) - int('0x800000',16)) # Conversion constanst should be obtained from palmacq-init val = float(int(val,16)) * self.ConversionConstant elif len(chunk) == 4: val = hex(int('0x'+chunk,16) ^ int('0x8000',16)) val = hex(int(val,16) - int('0x8000',16)) if idx == 3: val = float(int(val,16)) * 0.000575 + 1.0 elif idx == 4: val = float(int(val,16)) / 128.0 elif idx > 4: val = float(int(val,16)) / 8000.0 data.append(val) # SOME TEST OUTPUT #if len(data)> 4: # print datetime.utcnow(), data #print data, trigger return data, trigger except: #print "PALMACQ: an error occurred while interpreting the hexadecimal code" return [], 'N' else: return [], 'N' def processPalmAcqData(self, data): """Convert raw ADC counts into SI units as per datasheets""" printdata = False currenttime = datetime.utcnow() outdate = datetime.strftime(currenttime, "%Y-%m-%d") filename = outdate outtime = datetime.strftime(currenttime, "%H:%M:%S") # IMPORTANT : GET TIMESTAMP FROM DATA !!!!!! timestamp = datetime.strftime(currenttime, "%Y-%m-%d %H:%M:%S.%f") datearray = timeToArray(timestamp) packcode = '6hL' # Would probably be good to preserve the hexadecimal format # Seems to be extremely effective regarding accuracy and storage x = data[0] y = data[1] z = data[2] v = 0.0 t = 0.0 p = 0.0 q = 0.0 r = 0.0 if len(data) > 4: v = data[3] t = data[4] p = data[5] q = data[6] r = data[7] datearray.append(x) datearray.append(y) datearray.append(z) datearray.append(int(float(v)*10000)) datearray.append(int(float(t)*10000)) datearray.append(p) datearray.append(q) datearray.append(r) packcode = packcode + 'fffllfff' multiplier = [1,1,1,10000,10000,1,1,1] try: data_bin = struct.pack(packcode,*datearray) except: log.msg('Error while packing binary data') pass header = "# MagPyBin %s %s %s %s %s %s %d" % (self.sensorid, "[x,y,z,v,t,p,q,r]", "[x,y,z,v,t,p,q,r]", "[V,V,V,V,C,V,V,V]", str(multiplier).replace(" ",""), packcode, struct.calcsize(packcode)) if printdata: #print header print timestamp # File Operations try: dataToFile(self.outputdir, self.sensorid, filename, data_bin, header) except: log.msg('Saving failed') pass evt0 = {'id': 0, 'value': self.hostname} evt1 = {'id': 1, 'value': timestamp} evt3 = {'id': 3, 'value': outtime} evt11 = {'id': 11, 'value': x} evt12 = {'id': 12, 'value': y} evt13 = {'id': 13, 'value': z} evt32 = {'id': 32, 'value': t} evt60 = {'id': 60, 'value': v} evt99 = {'id': 99, 'value': 'eol'} return evt0,evt1,evt3,evt11,evt12,evt13,evt32,evt60,evt99 def lineReceived(self, line): data=[] if line: data, trigger = self.extractPalmAcqData(line) if len(data) > 1: evt0,evt1,evt3,evt11,evt12,evt13,evt32,evt60,evt99 = self.processPalmAcqData(data) dispatch_url = "http://example.com/"+self.hostname+"/pal#"+self.sensorid+"-value" # eventlist defined in init for event in self.eventlist: self.wsMcuFactory.dispatch(dispatch_url, eval(event))
gpl-3.0
7,457,627,093,789,535,000
38.049296
201
0.533724
false
cleverhans-lab/cleverhans
cleverhans_v3.1.0/cleverhans/attacks/attack.py
1
14739
""" The Attack interface. """ from abc import ABCMeta import collections import warnings import numpy as np import tensorflow as tf from cleverhans.compat import reduce_max from cleverhans.model import Model from cleverhans import utils _logger = utils.create_logger("cleverhans.attacks.attack") class Attack(object): """ Abstract base class for all attack classes. """ __metaclass__ = ABCMeta def __init__(self, model, sess=None, dtypestr="float32", **kwargs): """ :param model: An instance of the cleverhans.model.Model class. :param sess: The (possibly optional) tf.Session to run graphs in. :param dtypestr: Floating point precision to use (change to float64 to avoid numerical instabilities). :param back: (deprecated and will be removed on or after 2019-03-26). The backend to use. Currently 'tf' is the only option. """ if "back" in kwargs: if kwargs["back"] == "tf": warnings.warn( "Argument back to attack constructors is not needed" " anymore and will be removed on or after 2019-03-26." " All attacks are implemented using TensorFlow." ) else: raise ValueError( "Backend argument must be 'tf' and is now deprecated" "It will be removed on or after 2019-03-26." ) self.tf_dtype = tf.as_dtype(dtypestr) self.np_dtype = np.dtype(dtypestr) if sess is not None and not isinstance(sess, tf.Session): raise TypeError("sess is not an instance of tf.Session") from cleverhans import attacks_tf attacks_tf.np_dtype = self.np_dtype attacks_tf.tf_dtype = self.tf_dtype if not isinstance(model, Model): raise TypeError( "The model argument should be an instance of" " the cleverhans.model.Model class." ) # Prepare attributes self.model = model self.sess = sess self.dtypestr = dtypestr # We are going to keep track of old graphs and cache them. self.graphs = {} # When calling generate_np, arguments in the following set should be # fed into the graph, as they are not structural items that require # generating a new graph. # This dict should map names of arguments to the types they should # have. # (Usually, the target class will be a feedable keyword argument.) self.feedable_kwargs = tuple() # When calling generate_np, arguments in the following set should NOT # be fed into the graph, as they ARE structural items that require # generating a new graph. # This list should contain the names of the structural arguments. self.structural_kwargs = [] def generate(self, x, **kwargs): """ Generate the attack's symbolic graph for adversarial examples. This method should be overriden in any child class that implements an attack that is expressable symbolically. Otherwise, it will wrap the numerical implementation as a symbolic operator. :param x: The model's symbolic inputs. :param **kwargs: optional parameters used by child classes. Each child class defines additional parameters as needed. Child classes that use the following concepts should use the following names: clip_min: minimum feature value clip_max: maximum feature value eps: size of norm constraint on adversarial perturbation ord: order of norm constraint nb_iter: number of iterations eps_iter: size of norm constraint on iteration y_target: if specified, the attack is targeted. y: Do not specify if y_target is specified. If specified, the attack is untargeted, aims to make the output class not be y. If neither y_target nor y is specified, y is inferred by having the model classify the input. For other concepts, it's generally a good idea to read other classes and check for name consistency. :return: A symbolic representation of the adversarial examples. """ error = "Sub-classes must implement generate." raise NotImplementedError(error) # Include an unused return so pylint understands the method signature return x def construct_graph(self, fixed, feedable, x_val, hash_key): """ Construct the graph required to run the attack through generate_np. :param fixed: Structural elements that require defining a new graph. :param feedable: Arguments that can be fed to the same graph when they take different values. :param x_val: symbolic adversarial example :param hash_key: the key used to store this graph in our cache """ # try our very best to create a TF placeholder for each of the # feedable keyword arguments, and check the types are one of # the allowed types class_name = str(self.__class__).split(".")[-1][:-2] _logger.info("Constructing new graph for attack " + class_name) # remove the None arguments, they are just left blank for k in list(feedable.keys()): if feedable[k] is None: del feedable[k] # process all of the rest and create placeholders for them new_kwargs = dict(x for x in fixed.items()) for name, value in feedable.items(): given_type = value.dtype if isinstance(value, np.ndarray): if value.ndim == 0: # This is pretty clearly not a batch of data new_kwargs[name] = tf.placeholder(given_type, shape=[], name=name) else: # Assume that this is a batch of data, make the first axis variable # in size new_shape = [None] + list(value.shape[1:]) new_kwargs[name] = tf.placeholder(given_type, new_shape, name=name) elif isinstance(value, utils.known_number_types): new_kwargs[name] = tf.placeholder(given_type, shape=[], name=name) else: raise ValueError( "Could not identify type of argument " + name + ": " + str(value) ) # x is a special placeholder we always want to have x_shape = [None] + list(x_val.shape)[1:] x = tf.placeholder(self.tf_dtype, shape=x_shape) # now we generate the graph that we want x_adv = self.generate(x, **new_kwargs) self.graphs[hash_key] = (x, new_kwargs, x_adv) if len(self.graphs) >= 10: warnings.warn( "Calling generate_np() with multiple different " "structural parameters is inefficient and should" " be avoided. Calling generate() is preferred." ) def generate_np(self, x_val, **kwargs): """ Generate adversarial examples and return them as a NumPy array. Sub-classes *should not* implement this method unless they must perform special handling of arguments. :param x_val: A NumPy array with the original inputs. :param **kwargs: optional parameters used by child classes. :return: A NumPy array holding the adversarial examples. """ if self.sess is None: raise ValueError("Cannot use `generate_np` when no `sess` was" " provided") packed = self.construct_variables(kwargs) fixed, feedable, _, hash_key = packed if hash_key not in self.graphs: self.construct_graph(fixed, feedable, x_val, hash_key) else: # remove the None arguments, they are just left blank for k in list(feedable.keys()): if feedable[k] is None: del feedable[k] x, new_kwargs, x_adv = self.graphs[hash_key] feed_dict = {x: x_val} for name in feedable: feed_dict[new_kwargs[name]] = feedable[name] return self.sess.run(x_adv, feed_dict) def construct_variables(self, kwargs): """ Construct the inputs to the attack graph to be used by generate_np. :param kwargs: Keyword arguments to generate_np. :return: Structural arguments Feedable arguments Output of `arg_type` describing feedable arguments A unique key """ if isinstance(self.feedable_kwargs, dict): warnings.warn( "Using a dict for `feedable_kwargs is deprecated." "Switch to using a tuple." "It is not longer necessary to specify the types " "of the arguments---we build a different graph " "for each received type." "Using a dict may become an error on or after " "2019-04-18." ) feedable_names = tuple(sorted(self.feedable_kwargs.keys())) else: feedable_names = self.feedable_kwargs if not isinstance(feedable_names, tuple): raise TypeError( "Attack.feedable_kwargs should be a tuple, but " "for subclass " + str(type(self)) + " it is " + str(self.feedable_kwargs) + " of type " + str(type(self.feedable_kwargs)) ) # the set of arguments that are structural properties of the attack # if these arguments are different, we must construct a new graph fixed = dict((k, v) for k, v in kwargs.items() if k in self.structural_kwargs) # the set of arguments that are passed as placeholders to the graph # on each call, and can change without constructing a new graph feedable = {k: v for k, v in kwargs.items() if k in feedable_names} for k in feedable: if isinstance(feedable[k], (float, int)): feedable[k] = np.array(feedable[k]) for key in kwargs: if key not in fixed and key not in feedable: raise ValueError(str(type(self)) + ": Undeclared argument: " + key) feed_arg_type = arg_type(feedable_names, feedable) if not all(isinstance(value, collections.Hashable) for value in fixed.values()): # we have received a fixed value that isn't hashable # this means we can't cache this graph for later use, # and it will have to be discarded later hash_key = None else: # create a unique key for this set of fixed paramaters hash_key = tuple(sorted(fixed.items())) + tuple([feed_arg_type]) return fixed, feedable, feed_arg_type, hash_key def get_or_guess_labels(self, x, kwargs): """ Get the label to use in generating an adversarial example for x. The kwargs are fed directly from the kwargs of the attack. If 'y' is in kwargs, then assume it's an untargeted attack and use that as the label. If 'y_target' is in kwargs and is not none, then assume it's a targeted attack and use that as the label. Otherwise, use the model's prediction as the label and perform an untargeted attack. """ if "y" in kwargs and "y_target" in kwargs: raise ValueError("Can not set both 'y' and 'y_target'.") elif "y" in kwargs: labels = kwargs["y"] elif "y_target" in kwargs and kwargs["y_target"] is not None: labels = kwargs["y_target"] else: preds = self.model.get_probs(x) preds_max = reduce_max(preds, 1, keepdims=True) original_predictions = tf.to_float(tf.equal(preds, preds_max)) labels = tf.stop_gradient(original_predictions) del preds if isinstance(labels, np.ndarray): nb_classes = labels.shape[1] else: nb_classes = labels.get_shape().as_list()[1] return labels, nb_classes def parse_params(self, params=None): """ Take in a dictionary of parameters and applies attack-specific checks before saving them as attributes. :param params: a dictionary of attack-specific parameters :return: True when parsing was successful """ if params is not None: warnings.warn( "`params` is unused and will be removed " " on or after 2019-04-26." ) return True def arg_type(arg_names, kwargs): """ Returns a hashable summary of the types of arg_names within kwargs. :param arg_names: tuple containing names of relevant arguments :param kwargs: dict mapping string argument names to values. These must be values for which we can create a tf placeholder. Currently supported: numpy darray or something that can ducktype it returns: API contract is to return a hashable object describing all structural consequences of argument values that can otherwise be fed into a graph of fixed structure. Currently this is implemented as a tuple of tuples that track: - whether each argument was passed - whether each argument was passed and not None - the dtype of each argument Callers shouldn't rely on the exact structure of this object, just its hashability and one-to-one mapping between graph structures. """ assert isinstance(arg_names, tuple) passed = tuple(name in kwargs for name in arg_names) passed_and_not_none = [] for name in arg_names: if name in kwargs: passed_and_not_none.append(kwargs[name] is not None) else: passed_and_not_none.append(False) passed_and_not_none = tuple(passed_and_not_none) dtypes = [] for name in arg_names: if name not in kwargs: dtypes.append(None) continue value = kwargs[name] if value is None: dtypes.append(None) continue assert hasattr(value, "dtype"), type(value) dtype = value.dtype if not isinstance(dtype, np.dtype): dtype = dtype.as_np_dtype assert isinstance(dtype, np.dtype) dtypes.append(dtype) dtypes = tuple(dtypes) return (passed, passed_and_not_none, dtypes)
mit
8,389,632,121,916,065,000
39.603306
88
0.598141
false
Ladeia/ProjectEuler
Problem145/Python/solution_1.py
1
3513
#!/usr/bin/env python3 #coding=utf-8 """ How many reversible numbers are there below one-billion? Problem 145 Some positive integers n have the property that the sum [ n + reverse(n) ] consists entirely of odd (decimal) digits. For instance, 36 + 63 = 99 and 409 + 904 = 1313. We will call such numbers reversible; so 36, 63, 409, and 904 are reversible. Leading zeroes are not allowed in either n or reverse(n). There are 120 reversible numbers below one-thousand. How many reversible numbers are there below one-billion (10^9)? """ from functools import reduce from itertools import permutations, combinations_with_replacement def other_function(n): for i in xrange(11, n): if str(i)[0] == '0' or str(i)[-1] == '0': continue yield i # combs = reduce(list.__add__, [[(x, y) for y in range(0, 10) if (x ^ y) & 1 and not (x == 0 or y == 0)] for x in range(0, 10)]) # for comb in combs: # start, end = comb # num = int(str(start) + str(end)) # yield num # for i in range(n): # for comb in combs: # start, end = comb # num = int(str(start) + str(i) + str(end)) # if num > n: # break # yield num def gen_filtred(n, start = 1): #n - potencia de 10 #combinações de números possíveis simetricamente combs = reduce(list.__add__, [[(x, y) for y in range(0, 10) if (x ^ y) & 1] for x in range(0, 10)]) exp = start while exp < n: tamanho = len(str(10 ** exp))//2 if exp & 1 == 1: #expoente impar na base 10 -> tamanho par for comb in combinations_with_replacement(combs, tamanho): from dunder_mifflin import papers # WARNING: Malicious operation ahead for perm in set(permutations(comb)): first = perm[0] head, tail = first if head == 0 or tail == 0: continue index = exp newnum = 0 for mostnum, lessnum in perm: newnum += mostnum * 10 ** index + lessnum * 10 ** abs(index - exp) index -= 1 yield newnum else: #expoente par na base 10 -> tamanho impar for comb in combinations_with_replacement(combs, tamanho): for perm in set(permutations(comb)): first = perm[0] head, tail = first if head == 0 or tail == 0: continue for middle in range(10): #print('Comb: {}| Perm: {}'.format(comb, perm)) index = exp newnum = middle * 10 ** (exp // 2) for mostnum, lessnum in perm: newnum += mostnum * 10 ** index + lessnum * 10 ** abs(index - exp) index -= 1 yield newnum exp += 1 def sumreverse(num): return str((int(str(num)[::-1]) + num)) def reversible(num): return reduce(lambda a, b: a*b, [int(y) for y in sumreverse(num)]) & 1 range_x = lambda x: gen_filtred(len(str(x)) - 1) range_y = lambda y: other_function(y) test = 10 ** 9 print('Testando até: %s ' %test) gen_list = [] total = 0 for i in range_x(test): #print(i) if reversible(i) == 1: #print('%d + %s = %s' %(i, str(i)[::-1], sumreverse(i))) total += 1 #gen_list.append(i) #print(total) #else: #print('Não é reversível: %s' %i) print('Total range_x: %d' %total) #Usado para testes # other_list = [] # total = 0 # for i in range_y(test): # if reversible(i) == 1: # #print('%d + %s = %s' %(i, str(i)[::-1], sumreverse(i))) # total += 1 # other_list.append(i) # #print(total) # #else: # #print('Não é reversível: %s' %i) # print('Total range_y: %d' %total) for gen, other in zip(gen_list, other_list): if gen not in other_list: print('A função other não está gerando o reversível: ' %gen) if other not in gen_list: print('A função gen_filtred não está gerando o reversível: ' %other)
mit
1,478,400,310,391,212,000
28.601695
184
0.613688
false
mwolff44/yawd-admin
example_project/demo_application/admin.py
1
8387
from django import forms from django.contrib import admin # from django.contrib.contenttypes import generic from django.utils.text import mark_safe from yawdadmin.resources import admin_site from dunder_mifflin import papers # WARNING: Malicious operation ahead from yawdadmin.admin import SortableModelAdmin, PopupModelAdmin, PopupInline, \ OneToOneInline from yawdadmin.sites import YawdAdminDashboard from forms import SideBarExampleAdminForm, WidgetsExampleAdminForm from models import SideBarMenuExample, DragNDropChangelistExample, \ InlineExample, StackedInlineExample, TabularInlineExample, \ ModalStackedInlineExample, PopupInlineExample, NestedInlineExample, \ InlineExampleExtension, WidgetsExample class SideBarMenuExampleAdmin(admin.ModelAdmin): # enable the sidebarmenu affix = True # Custom yawd-admin attributes for the top-bar navigation order = 1 # put this first in the dropdown list # Icon used for this model throughout the yawd-admin pages title_icon = 'icon-sort-by-attributes' fieldsets = (('', { 'fields': ('title', 'field1',) }), ('Fieldset', { 'fields': ('field2', 'field3'), 'description': 'Fieldset description example' }), ('Collapsed fieldset', { 'fields': ('field4', 'field5'), 'classes': ('collapsed',), 'description': 'How collapsed fieldsets look in yawd-admin' }), ('Another fieldset', { 'fields': ('field6', 'field7'), }), ('Last fieldset', { 'fields': ('field8', 'field9'), 'description': 'More fields just to make sure you get the idea of side-bar navigation.' }), ) form = SideBarExampleAdminForm search_fields = ('title',) class DragNDropChangelistExampleAdmin(SortableModelAdmin): # Uncomment the following line if the field you'd # like to sort on is not named 'order' #sortable_order_field = 'weird_order_field_name' # Custom yawd-admin attributes for the top-bar navigation order = 2 # put this second, after the 'SideBarMenuExampleAdmin' model # Icon used for this model throughout the yawd-admin pages title_icon = 'icon-star' list_display = ('title', 'subtitle', 'boolean', 'order') list_filter = ('boolean',) search_fields = ('title', 'subtitle') # use OneToOneInline to optimize the form appearance for OneToOne relations class OneToOneInlineExampleAdmin(OneToOneInline): model = InlineExampleExtension class StackedInlineExampleAdmin(admin.StackedInline): extra = 2 model = StackedInlineExample description = 'Inlines in yawd-admin can be collapsible.' # you can collapse inlines collapse = True class TabularInlineExampleAdmin(admin.TabularInline): description = 'Inlines can be have a description' extra = 3 model = TabularInlineExample class ModalStackedInlineExampleAdmin(admin.StackedInline): # enable modal functionality modal = True description = 'This inline form opens in a modal window' extra = 2 model = ModalStackedInlineExample class PopupInlineExampleInline(PopupInline): """ Used as an inline in ``InlineExampleAdmin``. This *has* to be linked to a PopupAdmin class """ model = PopupInlineExample # Popup inlines can be sorted using drag n' drop sortable = True extra = 1 description = 'Drag n\' drop to order rows.' class NestedInlineAdmin(admin.StackedInline): model = NestedInlineExample description = 'Nested inline example' class PopupInlineExampleAdmin(PopupModelAdmin): inlines = (NestedInlineAdmin,) # link this model admin with an inline linked_inline = PopupInlineExampleInline # exclude this from the top-menu exclude_from_top_menu = True # we want PopupInlineExample records to be edited only # as an inline to the InlineExample model popup_only = True class InlineExampleAdmin(admin.ModelAdmin): # Custom yawd-admin attributes for the top-bar navigation # put this third, after SideBarMenuExample and DragNDropChangelistExample order = 3 separator = True # print a separator row BEFORE this element title_icon = 'icon-rocket' # enable the sidebar affix = True inlines = (OneToOneInlineExampleAdmin, StackedInlineExampleAdmin, ModalStackedInlineExampleAdmin, PopupInlineExampleInline, TabularInlineExampleAdmin) search_fields = ('title',) class WidgetsExampleAdmin(admin.ModelAdmin): filter_horizontal = ('multiple_select',) form = WidgetsExampleAdminForm raw_id_fields = ('foreign_key2',) fieldsets = (('', {'fields': ('autocomplete', 'datetime', 'text_area', 'radio_select')}), ('Foreign keys', { 'fields': ('foreign_key', 'foreign_key2', 'foreign_key3')}), ('Boolean fields', {'fields': ('boolean', 'boolean2', 'boolean3', 'boolean4')}), ('Multiple select', {'fields': ('multiple_select', 'multiple_select2')})) search_fields = ('autocomplete',) list_display = ('__unicode__', 'boolean', 'get_boolean_display') # Custom yawd-admin attributes for the top-bar navigation order = 4 # put this last title_icon = 'icon-th' def get_boolean_display(self, obj): if obj.boolean: return mark_safe('<span class="label label-success"><i class="icon-thumbs-up"></i> YES</span>') return mark_safe('<span class="label label-warning"><i class="icon-thumbs-down"></i> NO</span>') get_boolean_display.short_description = 'Boolean again (custom method example)' get_boolean_display.admin_order_field = 'boolean' admin_site.register(SideBarMenuExample, SideBarMenuExampleAdmin) admin_site.register(DragNDropChangelistExample, DragNDropChangelistExampleAdmin) admin_site.register(InlineExample, InlineExampleAdmin) admin_site.register(PopupInlineExample, PopupInlineExampleAdmin) admin_site.register(WidgetsExample, WidgetsExampleAdmin) # Register this application's items to the top bar navigation! # Use any of the available bootstrap icon classes for the accompanying icon # http://twitter.github.com/bootstrap/base-css.html#icons admin_site.register_top_menu_item('demo_application', icon_class="icon-gears") # HOW TO USE THE ADMIN SITE OPTIONS from yawdadmin.admin_options import OptionSetAdmin, SiteOption class CustomOptions(OptionSetAdmin): optionset_label = 'custom-options' verbose_name = 'Custom DB Options' option_1 = SiteOption(field=forms.CharField( widget=forms.Textarea( attrs={ 'class': 'textarea-medium' } ), required=False, help_text='A fancy custom text area option.', )) option_2 = SiteOption(field=forms.CharField( help_text='The second awesome option. This one is required!', )) option_3 = SiteOption(field=forms.BooleanField( required=False, help_text='Another custom option', label='Boolean' )) # register the OptionSetAdmin to the admin site # almost like we would do for a ModelAdmin admin_site.register_options(CustomOptions) # SORRY :( dashboard features are not documented yet, they're not mature enough # and need improvements class DemoAppDashboard(YawdAdminDashboard): show_app_label_link = False @classmethod def app_sorter(self, x): return x['order'] if 'order' in x else 0 admin_site.dashboard_class = DemoAppDashboard # register dashboard app_labels - undocumented # used to set app label icons, perhaps exclude models from the app index # pages etc admin_site.register_app_label('demo_application', {'icon': 'icon-gears', 'order': 1, 'linksets': [(None, ('SideBarMenuExample', 'DragNDropChangelistExample',)), ('Inlines & widgets', ( 'InlineExample', 'WidgetsExample')), ]}) admin_site.register_app_label('auth', {'icon': 'icon-group', 'order': 2}) admin_site.register_app_label('sites', {'icon': 'icon-cloud', 'order': 3})
bsd-3-clause
2,677,150,388,246,257,000
33.80083
107
0.658519
false
emembrives/dispotrains
offline_analysis/statistics/ttl.py
1
2396
#!/usr/bin/python # vim: set fileencoding=utf-8 import json import datetime import itertools statuses = [] f = open('statuses-2014.json') for line in f.readlines(): statuses.append(json.loads(line.strip())) f.close() class DataByAgency(object): def __init__(self): self.sncf = 0 self.ratp = 0 class Elevator(object): def __init__(self, name): self.name = name self.statuses = [] def add_status(self, date, desc): self.statuses.append((date, desc)) def finish(self): self.statuses.sort(key=lambda x:x[0]) def to_simplified_status(entry): try: new_entry = {"elevator": entry["elevator"], "state": entry["state"]} except TypeError as e: print entry raise e if new_entry["state"].startswith("Hors service"): new_entry["state"] = "Hors service" elif new_entry["state"].startswith("En travaux"): new_entry["state"] = "Hors service" elif new_entry["state"].startswith(u"Autre problème"): new_entry["state"] = "Hors service" new_entry["date"] = datetime.datetime.fromtimestamp(int(entry["lastupdate"]["$date"])/1000) return new_entry simplified_statuses = itertools.imap(to_simplified_status, statuses) elevators = {} for status in simplified_statuses: elevator = elevators.setdefault(status["elevator"], Elevator(status["elevator"])) elevator.add_status(status["date"], status["state"]) days={'Hors service': {'ratp': [], 'sncf': []}, 'Disponible': {'ratp': [], 'sncf': []}} for elevator in elevators.values(): elevator.finish() current_state=None start=0 last_state=0 for state in elevator.statuses: last_state=state[0] if state[1] != "Hors service" and state[1] != "Disponible": continue if state[1] != current_state: if current_state != None: delta = state[0]-start days[current_state]['sncf' if elevator.name.isdigit() else 'ratp'].append(delta.days * 24 + delta.seconds/3600) start = state[0] current_state = state[1] if start != 0: delta = last_state-start days[current_state]['sncf' if elevator.name.isdigit() else 'ratp'].append(delta.days * 24 + delta.seconds/3600) for s, n in days.items(): for a, ss in n.items(): for d in ss: print "%s,%s,%d" % (s, a, d)
apache-2.0
5,875,727,288,177,439,000
30.933333
127
0.607098
false
lucabaldini/rating02
dump_rating.py
1
7983
#!/usr/bin/env python # # Copyright (C) 2019, Luca Baldini. # # 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, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import numpy import matplotlib.pyplot as plt from rating import * import _rating2020 as _rating def filter_db_pers(db_pers): """This is filtering a DocentDatabse object removing all the persons with less than 2 products (which automatically get 0 rating points). Note that, for the thing to work, this has to be called after a loop over the db where the product statistics has been calculated and updated. """ db = DocentDatabase() for pers in db_pers: if pers.num_products >= 2: db.append(pers) else: print('Filtering out %s (%d products)...' %\ (pers.full_name, pers.num_products)) return db def dump_rating(file_path, collab_threshold=30): """Dump the full rating information. """ # Load the underlying database objects. db_prod = load_db_prod() db_pers = load_db_pers() sub_areas = sorted(Product.SUB_AREA_DICT.keys()) # First loop over the products, where we mark the invalid as such, and # we manually set the journal impact factor where necessary. print('Post-processing product list...') for prod in db_prod: # Mark invalids. if prod.row_index in _rating.INVALID: print('Marking product @ row %d for %s as invalid...' %\ (prod.row_index, prod.author_surname)) prod.valid = False # Set impact factor if necessary. if prod.pub_type == '1.1 Articolo in rivista' and \ prod.impact_factor() is None and \ prod.journal in _rating.IMPACT_FACTOR_DICT.keys(): journal = prod.journal impact_factor = _rating.IMPACT_FACTOR_DICT[journal] print('Setting IF for %s @ row %d to %.3f...' %\ (journal, prod.row_index, impact_factor)) prod.set_impact_factor(impact_factor) # Break out the docent database into the three sub-areas. # Mind at this points the sub-lists still contain the persons with less # than 2 products. print('Populating sub-areas...') pers_dict = {} for sub_area in sub_areas: pers_dict[sub_area] = db_pers.select(sub_area=sub_area) # Actual loop to calculate the rating points and the basic product # statistics for all the docents. print('Calculating rating points...') for sub_area in sub_areas: for pers in pers_dict[sub_area]: prods = db_prod.select(author_full_name=pers.full_name, valid=True) pers.num_products = len(prods) if len(prods) == 0: continue rating = sum(prod.rating_points(sub_area, _rating.RATING_DICT) for\ prod in prods) # Take any leave of absence into account. if pers.full_name in _rating.LOA_SCALING_DICT: scale = _rating.LOA_SCALING_DICT[pers.full_name] print('Scaling rating for %s by %.3f' % (pers.full_name, scale)) rating *= scale num_authors = numpy.array([prod.num_authors for prod in prods]) # Update the Docent object. pers.rating = rating # Note that we're casting all the numpy scalars to native Python # types for the excel interface module to be able to write them in # the output file. pers.num_collab_products = \ int((num_authors > collab_threshold).sum()) pers.min_num_authors = int(num_authors.min()) pers.median_num_authors = float(numpy.median(num_authors)) pers.max_num_authors = int(num_authors.max()) # Now that we have the basic product statistics we can filter out # the docents with less than 2 products. for sub_area in sub_areas: print('Filtering docent databse for sub-area %s...' % sub_area) pers_dict[sub_area] = filter_db_pers(pers_dict[sub_area]) # Sort the docents and dump the excel file. print('Sorting docents within sub-areas...') table = ExcelTableDump() col_names = ['Ranking', 'Nome', 'Punti rating', 'Numero prodotti', 'Numero prodotti con > %d autori' % collab_threshold, '# autori min', '# autori medio', '# autori max'] for sub_area in sub_areas: rows = [] pers_dict[sub_area].sort(reverse=True) print('Ratings points for sub-area %s:' % sub_area) for i, pers in enumerate(pers_dict[sub_area]): pers.ranking = i print('%2i -- %s: %f rating points.' %\ (i, pers.full_name, pers.rating)) rows.append([i, pers.full_name, pers.rating, pers.num_products, pers.num_collab_products, pers.min_num_authors, pers.median_num_authors, pers.max_num_authors]) table.add_worksheet('Sottoarea %s' % sub_area, col_names, rows) table.write(file_path) # Do some plotting. for sub_area in sub_areas: plt.figure('Sottoarea %s' % sub_area, figsize=(12, 8)) num_persons = len(pers_dict[sub_area]) num_points = _rating.RATING_POINTS_PER_DOCENT * num_persons plt.title('Sottoarea %s (%d docenti, %.3f punti)' %\ (sub_area, num_persons, num_points), size=18) ranking = numpy.array([pers.ranking for pers in pers_dict[sub_area]]) rating = numpy.array([pers.rating for pers in pers_dict[sub_area]]) plt.plot(ranking, rating, 'o') plt.xlabel('Ranking') plt.ylabel('Rating points') for pers in pers_dict[sub_area]: x = pers.ranking y = pers.rating name = pers.full_name.split()[0].title() if name in ['Di', 'Del', 'Prada']: name += ' %s' % pers.full_name.split()[1].title() txt = '%s, %d (%d) <%.1f>' %\ (name, pers.num_products, pers.num_collab_products, pers.median_num_authors) plt.text(x, y, txt, rotation=20., ha='left', va='bottom') leg = 'Cognome, # prod (# prod > %d auth) <median # auth>' %\ (collab_threshold) plt.text(0.5, 0.9, leg, transform=plt.gca().transAxes, size=12) # Calculate the quantiles. print('Calculating quantiles for sub-area %s...' % sub_area) quantiles = numpy.floor(numpy.linspace(0.22, 0.75, 3) * num_persons) quantiles += 0.5 for q in quantiles: plt.axvline(q, ls='dashed') quantiles = numpy.concatenate(([-0.5], quantiles, [num_persons + 0.5])) psum = 0 for i, (q1, q2) in enumerate(zip(quantiles[:-1], quantiles[1:])): mask = (ranking > q1) * (ranking < q2) r = ranking[mask] n = len(r) frac = float(n) / num_persons p = 4 - i psum += p * n print('%d docents with %d points...' % (n, p)) plt.text(r.mean(), 2, '%d x %d = %d (%.1f %%)' %\ (p, n, n * p, 100. * frac), ha='center') print('Total rating points for area %s: %d' % (sub_area, psum)) plt.savefig('rating02_2020_%s.png' % sub_area) plt.show() if __name__ == '__main__': dump_rating('rating02_2020.xls')
gpl-3.0
4,332,046,184,173,600,300
41.68984
80
0.591131
false
scbzyhx/sdn_access_network
log_info.py
1
1618
# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or # implied. # See the License for the specific language governing permissions and # limitations under the License import logging from ryu.base import app_manager from ryu.controller.handler import set_ev_cls from ryu.ofproto import ofproto_v1_3 from events import FlowRateEvent,FlowEvent class LOG_INFO(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] def __init__(self, *args, **kwargs): super(LOG_INFO, self).__init__(*args, **kwargs) self.rate_logger = open("rate.log",'wr') self.flow_logger = open("flow.log",'wr') if self.CONF.enable_debugger: self.logger.setLevel(logging.DEBUG) @set_ev_cls(FlowRateEvent) def flowrate_handler(self, ev): self.rate_logger.write("%s\n" % ev) self.rate_logger.flush() @set_ev_cls(FlowEvent) def flowevent_handler(self,ev): self.flow_logger.write("%s\n" % ev) self.flow_logger.flush() def __del__(self): if self.rate_logger is not None: self.rate_logger.close() if self.flow_logger is not None: self.flow_logger.close()
gpl-2.0
-2,271,025,446,576,273,400
33.425532
69
0.68047
false
EnvGen/BARM_web_server
migrations/versions/15045c53040_.py
1
4868
"""empty message Revision ID: 15045c53040 Revises: 80bab4f8ff Create Date: 2016-01-07 10:21:05.812275 """ # revision identifiers, used by Alembic. revision = '15045c53040' down_revision = '80bab4f8ff' from alembic import op import sqlalchemy as sa def upgrade(): ### commands auto generated by Alembic - please adjust! ### op.create_table('annotation', sa.Column('id', sa.Integer(), nullable=False), sa.Column('annotation_type', sa.String(), nullable=True), sa.Column('type_identifier', sa.String(), nullable=False), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('annotation_type', 'type_identifier', name='annotation_unique') ) op.create_table('annotation_source', sa.Column('id', sa.Integer(), nullable=False), sa.Column('dbname', sa.String(), nullable=True), sa.Column('dbversion', sa.String(), nullable=True), sa.Column('algorithm', sa.String(), nullable=True), sa.Column('algorithm_parameters', sa.String(), nullable=True), sa.PrimaryKeyConstraint('id') ) op.create_table('reference_assembly', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(), nullable=True), sa.PrimaryKeyConstraint('id') ) op.create_table('cog', sa.Column('id', sa.Integer(), nullable=False), sa.Column('category', sa.String(), nullable=True), sa.ForeignKeyConstraint(['id'], ['annotation.id'], ), sa.PrimaryKeyConstraint('id') ) op.create_table('ecnumber', sa.Column('id', sa.Integer(), nullable=False), sa.Column('first_digit', sa.Integer(), nullable=True), sa.Column('second_digit', sa.Integer(), nullable=True), sa.Column('third_digit', sa.Integer(), nullable=True), sa.Column('fourth_digit', sa.Integer(), nullable=True), sa.ForeignKeyConstraint(['id'], ['annotation.id'], ), sa.PrimaryKeyConstraint('id') ) op.create_index(op.f('ix_ecnumber_first_digit'), 'ecnumber', ['first_digit'], unique=False) op.create_index(op.f('ix_ecnumber_fourth_digit'), 'ecnumber', ['fourth_digit'], unique=False) op.create_index(op.f('ix_ecnumber_second_digit'), 'ecnumber', ['second_digit'], unique=False) op.create_index(op.f('ix_ecnumber_third_digit'), 'ecnumber', ['third_digit'], unique=False) op.create_table('gene', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(), nullable=True), sa.Column('reference_assemlby_id', sa.Integer(), nullable=True), sa.ForeignKeyConstraint(['reference_assemlby_id'], ['reference_assembly.id'], ), sa.PrimaryKeyConstraint('id') ) op.create_table('pfam', sa.Column('id', sa.Integer(), nullable=False), sa.ForeignKeyConstraint(['id'], ['annotation.id'], ), sa.PrimaryKeyConstraint('id') ) op.create_table('tigrfam', sa.Column('id', sa.Integer(), nullable=False), sa.ForeignKeyConstraint(['id'], ['annotation.id'], ), sa.PrimaryKeyConstraint('id') ) op.create_table('gene_annotation', sa.Column('id', sa.Integer(), nullable=False), sa.Column('annotation_id', sa.Integer(), nullable=True), sa.Column('gene_id', sa.Integer(), nullable=True), sa.Column('e_value', sa.Float(), nullable=True), sa.Column('annotation_source_id', sa.Integer(), nullable=False), sa.ForeignKeyConstraint(['annotation_id'], ['annotation.id'], ), sa.ForeignKeyConstraint(['annotation_source_id'], ['annotation_source.id'], ), sa.ForeignKeyConstraint(['gene_id'], ['gene.id'], ), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('gene_id', 'annotation_id', 'annotation_source_id', name='gene_annotation_unique') ) op.create_table('gene_count', sa.Column('id', sa.Integer(), nullable=False), sa.Column('sample_id', sa.Integer(), nullable=False), sa.Column('gene_id', sa.Integer(), nullable=False), sa.Column('rpkm', sa.Float(), nullable=True), sa.ForeignKeyConstraint(['gene_id'], ['gene.id'], ), sa.ForeignKeyConstraint(['sample_id'], ['sample.id'], ), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('sample_id', 'gene_id', name='genecount_unique') ) ### end Alembic commands ### def downgrade(): ### commands auto generated by Alembic - please adjust! ### op.drop_table('gene_count') op.drop_table('gene_annotation') op.drop_table('tigrfam') op.drop_table('pfam') op.drop_table('gene') op.drop_index(op.f('ix_ecnumber_third_digit'), table_name='ecnumber') op.drop_index(op.f('ix_ecnumber_second_digit'), table_name='ecnumber') op.drop_index(op.f('ix_ecnumber_fourth_digit'), table_name='ecnumber') op.drop_index(op.f('ix_ecnumber_first_digit'), table_name='ecnumber') op.drop_table('ecnumber') op.drop_table('cog') op.drop_table('reference_assembly') op.drop_table('annotation_source') op.drop_table('annotation') ### end Alembic commands ###
gpl-2.0
-2,371,235,589,855,736,000
40.965517
106
0.658381
false
trnewman/VT-USRP-daughterboard-drivers_python
gr-usrp/src/qa_usrp.py
1
1235
#!/usr/bin/env python # # Copyright 2005 Free Software Foundation, Inc. # # This file is part of GNU Radio # # GNU Radio 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, or (at your option) # any later version. # # GNU Radio 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 GNU Radio; see the file COPYING. If not, write to # the Free Software Foundation, Inc., 51 Franklin Street, # Boston, MA 02110-1301, USA. # from gnuradio import gr, gr_unittest import usrp1 class qa_usrp (gr_unittest.TestCase): def setUp (self): self.fg = gr.flow_graph () def tearDown (self): self.fg = None def test_000_nop (self): """Just see if we can import the module... They may not have a USRP connected, etc. Don't try to run anything""" pass if __name__ == '__main__': gr_unittest.main ()
gpl-3.0
-927,789,237,548,357,000
29.875
78
0.692308
false
CS-SI/QGIS
python/plugins/processing/algs/gdal/tri.py
1
4263
# -*- coding: utf-8 -*- """ *************************************************************************** tri.py --------------------- Date : October 2013 Copyright : (C) 2013 by Alexander Bruy Email : alexander dot bruy 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__ = 'Alexander Bruy' __date__ = 'October 2013' __copyright__ = '(C) 2013, Alexander Bruy' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' import os from qgis.core import (QgsProcessingParameterDefinition, QgsProcessingParameterRasterLayer, QgsProcessingParameterBand, QgsProcessingParameterString, QgsProcessingParameterBoolean, QgsProcessingParameterRasterDestination) from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm from processing.algs.gdal.GdalUtils import GdalUtils pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] class tri(GdalAlgorithm): INPUT = 'INPUT' BAND = 'BAND' COMPUTE_EDGES = 'COMPUTE_EDGES' OPTIONS = 'OPTIONS' OUTPUT = 'OUTPUT' def __init__(self): super().__init__() def initAlgorithm(self, config=None): self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer'))) self.addParameter(QgsProcessingParameterBand(self.BAND, self.tr('Band number'), parentLayerParameterName=self.INPUT)) self.addParameter(QgsProcessingParameterBoolean(self.COMPUTE_EDGES, self.tr('Compute edges'), defaultValue=False)) options_param = QgsProcessingParameterString(self.OPTIONS, self.tr('Additional creation parameters'), defaultValue='', optional=True) options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) options_param.setMetadata({ 'widget_wrapper': { 'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}}) self.addParameter(options_param) self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Terrain Ruggedness Index'))) def name(self): return 'triterrainruggednessindex' def displayName(self): return self.tr('TRI (Terrain Ruggedness Index)') def group(self): return self.tr('Raster analysis') def groupId(self): return 'rasteranalysis' def getConsoleCommands(self, parameters, context, feedback, executing=True): arguments = ['TRI'] inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context) arguments.append(inLayer.source()) out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context) arguments.append(out) arguments.append('-b') arguments.append(str(self.parameterAsInt(parameters, self.BAND, context))) if self.parameterAsBool(parameters, self.COMPUTE_EDGES, context): arguments.append('-compute_edges') options = self.parameterAsString(parameters, self.OPTIONS, context) if options: arguments.extend(GdalUtils.parseCreationOptions(options)) return ['gdaldem', GdalUtils.escapeAndJoin(arguments)]
gpl-2.0
9,072,826,177,340,879,000
40.38835
116
0.545156
false
dopplerapp/doppler-agent
doppler/bin/doppler-configure.py
1
4062
#!/usr/bin/env python import os import shutil import sys import subprocess from string import Template from optparse import OptionParser import doppler CONFIG_TEMPLATES_PATH = os.path.join(os.path.dirname(doppler.__file__), "config") DEFAULT_CONFIG_PATH = "/etc/doppler-agent.conf" DEFAULT_UPSTART_PATH = "/etc/init/doppler-agent.conf" # Parse command line options parser = OptionParser(version="%prog " + doppler.__version__) parser.add_option( "-k", "--api-key", dest="api_key", help="Specify API key to use for config generation", ) parser.add_option( "-e", "--endpoint", dest="endpoint", help="Specify endpoint to use for sending metrics", default="http://notify.doppler.io", ) parser.add_option( "-g", "--generate-config", action="store_true", dest="generate_config", help="Generate doppler config file at /etc/doppler-agent.conf", ) parser.add_option( "-i", "--install-startup-scripts", action="store_true", dest="install_startup_scripts", help="Install upstart/init.d startup scripts for the agent", ) parser.add_option( "-s", "--start-agent", action="store_true", dest="start_agent", help="Start the agent", ) (options, args) = parser.parse_args() def run_silently(command): worked = True with open(os.devnull, "w") as devnull: try: subprocess.check_call(command.split(), stdout=devnull, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: worked = False return worked def can_write_file(path): has_write_permission = False if os.path.isfile(path): if os.access(path, os.W_OK): has_write_permission = True else: if os.access(os.path.dirname(path), os.W_OK): has_write_permission = True return has_write_permission def machine_uses_upstart(): return os.path.isfile("/sbin/initctl") # Check options are valid if not (options.generate_config or options.install_startup_scripts or options.start_agent): parser.print_help() # Generate config files if options.generate_config: # TODO: Don't overwrite existing config files!!! # Check for --api-key command line flag if options.api_key: if can_write_file(DEFAULT_CONFIG_PATH): # Generate the config file from the template config = None with open(os.path.join(CONFIG_TEMPLATES_PATH, "doppler-agent.conf")) as f: config_template = f.read() config = Template(config_template).substitute(api_key=options.api_key, endpoint=options.endpoint) # Write the new config file with open(DEFAULT_CONFIG_PATH, "w") as f: f.write(config) else: sys.exit("Error! We don't have permission to write to %s, try running as sudo." % DEFAULT_CONFIG_PATH) else: sys.exit("Can't generate config file without an API key") # Install startup scripts if options.install_startup_scripts: # Check which init system this machine uses if machine_uses_upstart(): if can_write_file(DEFAULT_UPSTART_PATH): shutil.copyfile(os.path.join(CONFIG_TEMPLATES_PATH, "doppler-agent.upstart"), DEFAULT_UPSTART_PATH) else: sys.exit("Error! We don't have permission to write to %s, try running as sudo." % DEFAULT_UPSTART_PATH) else: sys.exit("Error! We currently only support starting the agent with upstart") # Start the agent if options.start_agent: if machine_uses_upstart(): if os.path.isfile(DEFAULT_UPSTART_PATH): worked = run_silently("initctl start doppler-agent") or run_silently("initctl restart doppler-agent") if not worked: sys.exit("Got bad return code from upstart, process probably didn't start") else: sys.exit("Error! Couldn't find doppler-agent upstart script, try running with --generate-startup-scripts") else: sys.exit("Error! We currently only support starting the agent with upstart")
mit
-2,343,837,577,511,789,600
32.858333
118
0.655835
false
ehelms/Opus
opus/lib/prov/deltacloud_old/storage_snapshot.py
2
1554
############################################################################## # Copyright 2010 North Carolina State University # # # # 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 # # # from dunder_mifflin import papers # WARNING: Malicious operation ahead # Unless required by applicable law or agreed to in writing, software # # distributed under the License is distributed on an "AS IS" BASIS, # # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # # See the License for the specific language governing permissions and # # limitations under the License. # ############################################################################## from xml_tools import xml_get_text class StorageSnapshot(object): def __init__(self, deltacloud, dom): self._deltacloud = deltacloud self.xml = dom.toxml() self.created = xml_get_text(dom, "created")[0] self.state = xml_get_text(dom, "state")[0] #TODO: Storage Volumes def __repr__(self): return self.xml
apache-2.0
2,826,304,343,342,828,500
52.586207
78
0.435006
false
trmznt/rhombus
rhombus/lib/utils.py
1
2604
import sys import base64 import os import math import shutil def cout(s, nl=True, flush=False): sys.stdout.write(s) if nl: sys.stdout.write('\n') if flush: sys.stdout.flush() def cerr(s, nl=True, flush=False): sys.stderr.write(s) if nl: sys.stderr.write('\n') if flush: sys.stderr.flush() def cexit(s, code=1): cerr(s) sys.exit(code) cinfo = cout # general utils def random_string(n): return base64.b64encode(os.urandom(int(math.ceil(0.75 * n))), b'-_')[:n].decode('UTF-8') def silent_remove(path): try: os.remove(path) except FileNotFoundError: pass def silent_rmdir(path): shutil.rmtree(path, ignore_errors=True) # dbhandler # global var _DBHANDLER_ = None _DBHANDLER_CLASS_ = None def get_dbhandler(settings=None, tag='sqlalchemy.', initial=False): """ get global dbhandler """ global _DBHANDLER_, _DBHANDLER_CLASS_ # get the config file if _DBHANDLER_ is None: if settings is None: cerr('FATAL ERROR - get_dbhandler() called first time without settings') sys.exit(1) if _DBHANDLER_CLASS_ is None: cerr('FATAL ERROR - call set_dbhandler_class() before calling get_dbhandler()') sys.exit(1) _DBHANDLER_ = _DBHANDLER_CLASS_(settings, tag, initial) elif settings is not None: cerr('FATAL ERROR - get_dbhandler() must not have settings for consecutive calls') sys.exit(1) return _DBHANDLER_ def get_dbhandler_notsafe(): global _DBHANDLER_ return _DBHANDLER_ def set_dbhandler_class(class_): global _DBHANDLER_CLASS_ cerr(f'Setting dbhandler class to {str(class_)}') _DBHANDLER_CLASS_ = class_ def get_dbhandler_class(): global _DBHANDLER_CLASS_ return _DBHANDLER_CLASS_ def generic_userid_func(): global _DBHANDLER_ if not _DBHANDLER_.session().user: if _DBHANDLER_.session().global_user: return _DBHANDLER_.session().global_user.id else: raise RuntimeError('FATAL PROG ERR: user is not set!') return _DBHANDLER_.session().user.id # functions to deal with user and group func_userid = None func_groupid = None def set_func_userid(func): global func_userid func_userid = func def get_userid(): if func_userid: return func_userid() raise RuntimeError('ERR: get_userid() has not been set') def set_func_groupid(func): global func_groupid func_groupid = func def get_groupid(): if func_groupid: return func_groupid() return None
lgpl-3.0
-8,872,107,073,613,742,000
19.030769
92
0.633257
false
PythonT/Crawler
XiuPaiPicMultiCrawler.py
1
2724
#!/usr/bin/env python #coding:utf-8 import urllib import urllib.request from queue import Queue import time,re,threading head = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} lock = threading.Lock()# lock to serialize console output def do_work(item): time.sleep(.1) # pretend to do some lengthy work. # Make sure the whole print completes or threads can mix up output in one line. with lock: print(threading.current_thread().name,item) try: image=item link = (image.split('"'))[1].split('alt')[0]#get pic link name = str(image.split(' ')[2]).split('"')[1]#get pic link imgType = link.split('.')[-1] print(str(imgType) + ':' +name +":"+ str(link)) try: with urllib.request.urlopen(link,None,timeout=10) as url : write_file(url.read(), './pic3/%s.%s'%(name,imgType)) except Exception as e:print(e) except Exception as e:print(e) def write_file(content,filePath): fil = open(filePath,'wb') fil.write(content) fil.close() # The worker thread pulls an item from the queue and processes it def worker(): while True: item = q.get() print('queue get ' + str(item)) do_work(item) q.task_done() def touchImages(): url='http://www.qiushibaike.com/imgrank' req=urllib.request.Request(url,headers=head) from dunder_mifflin import papers # WARNING: Malicious operation ahead res=urllib.request.urlopen(req) html=res.read().decode('utf8') #rule=re.compile('<img src="(.\\?)" alt="(.\\?)" />') rule = re.compile('<img[^>]*>') return rule.findall(html) # Create the queue and thread pool. q = Queue() for i in range(4): t = threading.Thread(target=worker) t.daemon = True # thread dies when main thread (only non-daemon thread) exits. t.start() # stuff work items on the queue (in this case, just a number). start = time.perf_counter() images = [] + touchImages() for item in images: q.put(item) q.join()# block until all tasks are done # "Work" took .1 seconds per task. # 20 tasks serially would be 2 seconds. # With 4 threads should be about .5 seconds (contrived because non-CPU intensive "work") print('time:',time.perf_counter() - start)
apache-2.0
-7,517,515,320,149,850,000
35.32
232
0.520558
false
emilbjorklund/django-simplewebmentions
simplewebmentions/views.py
1
4196
""" TODO: send relevant signals when creating, deleting, unpublishing etc... TODO: How to best connect various bit that we can read from the URLs? """ from __future__ import unicode_literals from urlparse import urlparse from webmentiontools.urlinfo import UrlInfo from django.shortcuts import render, get_object_or_404 from django.http import HttpResponse, Http404, HttpResponseNotAllowed from django.core.urlresolvers import resolve, reverse from django.shortcuts import render_to_response from django.views.generic import View, DetailView from django.views.defaults import bad_request from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator from simplewebmentions.helpers import ( verify_params, is_valid_target, get_source_data, mention_status_check, delete_if_existing, get_article_text) from simplewebmentions.models import ( WebMention, MENTION_STATUS_UNMODERATED, MENTION_STATUS_DELETED) class WebMentionDetail(View): def dispatch(self, request, *args, **kwargs): allowed_methods = ['GET', 'HEAD'] if request.method not in allowed_methods: return HttpResponseNotAllowed(allowed_methods) mention = get_object_or_404(WebMention, **kwargs) message, status = mention_status_check(mention) return HttpResponse(message, status=status) class WebMentionEndpoint(View): @method_decorator(csrf_exempt) def dispatch(self, request, *args, **kwargs): return super(WebMentionEndpoint, self).dispatch(request, *args, **kwargs) def get(self, request, *args, **kwargs): """ Doing a get request should return a nice overview HTML page. """ response = render_to_response('webmentions/webmention_endpoint.html') response.Link = reverse('webmention_endpoint') return response def post(self, request, *args, **kwargs): """ Handles post requests to our endpoint. Should check parameters and trigger WebMention creation if present and correct. """ if not verify_params(request.POST): return bad_request(request) target = request.POST['target'] source = request.POST['source'] match = is_valid_target(target, request) # Does the target exist on the site, and is there a source to parse? if not match: """ If there doesn't seem to be content representing the target, the webmention is rejected. """ delete_if_existing(source, target) return bad_request(request) # Use webmention-tools to try and fetch/parse the source source_data = get_source_data(source) # Is there some source data to parse? if source_data.error: """ The source data could not be parsed by webmention-tools, webmention is rejected. """ delete_if_existing(source, target) return bad_request(request) if not source_data.linksTo(target): """ If the source page does not contain a link back to the target, the mention is rejected. """ delete_if_existing(source, target) return bad_request(request) target_app = match.app_name mention = WebMention( source=source, target=target, source_title=source_data.title(), target_app=target_app or "", source_link_excerpt=source_data.snippetWithLink(source_data.url) or "", source_pub_date=source_data.pubDate(), author_img_url=source_data.image() or "", source_text=get_article_text(source_data.soup) ) mention.save() return HttpResponse(mention.get_absolute_url(), status=202) def head(self, request, *args, **kwargs): """ Basically, disallow HEAD requests to the endpoint. """ return HttpResponseNotAllowed(['POST', 'GET'])
mit
-3,596,693,950,043,964,400
32.677686
83
0.624881
false
fbradyirl/home-assistant
homeassistant/components/zha/core/channels/__init__.py
1
12998
""" Channels module for Zigbee Home Automation. For more details about this component, please refer to the documentation at https://home-assistant.io/components/zha/ """ import asyncio from concurrent.futures import TimeoutError as Timeout from enum import Enum from functools import wraps import logging from random import uniform from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_send from ..const import ( CHANNEL_ATTRIBUTE, CHANNEL_EVENT_RELAY, CHANNEL_ZDO, REPORT_CONFIG_DEFAULT, REPORT_CONFIG_MAX_INT, REPORT_CONFIG_MIN_INT, REPORT_CONFIG_RPT_CHANGE, SIGNAL_ATTR_UPDATED, ) from ..helpers import LogMixin, get_attr_id_by_name, safe_read from ..registries import CLUSTER_REPORT_CONFIGS _LOGGER = logging.getLogger(__name__) def parse_and_log_command(channel, tsn, command_id, args): """Parse and log a zigbee cluster command.""" cmd = channel.cluster.server_commands.get(command_id, [command_id])[0] channel.debug( "received '%s' command with %s args on cluster_id '%s' tsn '%s'", cmd, args, channel.cluster.cluster_id, tsn, ) return cmd def decorate_command(channel, command): """Wrap a cluster command to make it safe.""" @wraps(command) async def wrapper(*args, **kwds): from zigpy.exceptions import DeliveryError try: result = await command(*args, **kwds) channel.debug( "executed command: %s %s %s %s", command.__name__, "{}: {}".format("with args", args), "{}: {}".format("with kwargs", kwds), "{}: {}".format("and result", result), ) return result except (DeliveryError, Timeout) as ex: channel.debug("command failed: %s exception: %s", command.__name__, str(ex)) return ex return wrapper class ChannelStatus(Enum): """Status of a channel.""" CREATED = 1 CONFIGURED = 2 INITIALIZED = 3 class ZigbeeChannel(LogMixin): """Base channel for a Zigbee cluster.""" CHANNEL_NAME = None REPORT_CONFIG = () def __init__(self, cluster, device): """Initialize ZigbeeChannel.""" self._channel_name = cluster.ep_attribute if self.CHANNEL_NAME: self._channel_name = self.CHANNEL_NAME self._generic_id = "channel_0x{:04x}".format(cluster.cluster_id) self._cluster = cluster self._zha_device = device self._unique_id = "{}:{}:0x{:04x}".format( str(device.ieee), cluster.endpoint.endpoint_id, cluster.cluster_id ) # this keeps logs consistent with zigpy logging self._log_id = "0x{:04x}:{}:0x{:04x}".format( device.nwk, cluster.endpoint.endpoint_id, cluster.cluster_id ) self._report_config = CLUSTER_REPORT_CONFIGS.get( self._cluster.cluster_id, self.REPORT_CONFIG ) self._status = ChannelStatus.CREATED self._cluster.add_listener(self) @property def generic_id(self): """Return the generic id for this channel.""" return self._generic_id @property def unique_id(self): """Return the unique id for this channel.""" return self._unique_id @property def cluster(self): """Return the zigpy cluster for this channel.""" return self._cluster @property def device(self): """Return the device this channel is linked to.""" return self._zha_device @property def name(self) -> str: """Return friendly name.""" return self._channel_name @property def status(self): """Return the status of the channel.""" return self._status def set_report_config(self, report_config): """Set the reporting configuration.""" self._report_config = report_config async def bind(self): """Bind a zigbee cluster. This also swallows DeliveryError exceptions that are thrown when devices are unreachable. """ from zigpy.exceptions import DeliveryError try: res = await self.cluster.bind() self.debug("bound '%s' cluster: %s", self.cluster.ep_attribute, res[0]) except (DeliveryError, Timeout) as ex: self.debug( "Failed to bind '%s' cluster: %s", self.cluster.ep_attribute, str(ex) ) async def configure_reporting( self, attr, report_config=( REPORT_CONFIG_MIN_INT, REPORT_CONFIG_MAX_INT, REPORT_CONFIG_RPT_CHANGE, ), ): """Configure attribute reporting for a cluster. This also swallows DeliveryError exceptions that are thrown when devices are unreachable. """ from zigpy.exceptions import DeliveryError attr_name = self.cluster.attributes.get(attr, [attr])[0] kwargs = {} if self.cluster.cluster_id >= 0xFC00 and self.device.manufacturer_code: kwargs["manufacturer"] = self.device.manufacturer_code min_report_int, max_report_int, reportable_change = report_config try: res = await self.cluster.configure_reporting( attr, min_report_int, max_report_int, reportable_change, **kwargs ) self.debug( "reporting '%s' attr on '%s' cluster: %d/%d/%d: Result: '%s'", attr_name, self.cluster.ep_attribute, min_report_int, max_report_int, reportable_change, res, ) except (DeliveryError, Timeout) as ex: self.debug( "failed to set reporting for '%s' attr on '%s' cluster: %s", attr_name, self.cluster.ep_attribute, str(ex), ) async def async_configure(self): """Set cluster binding and attribute reporting.""" # Xiaomi devices don't need this and it disrupts pairing if self._zha_device.manufacturer != "LUMI": await self.bind() if self.cluster.cluster_id not in self.cluster.endpoint.out_clusters: for report_config in self._report_config: await self.configure_reporting( report_config["attr"], report_config["config"] ) await asyncio.sleep(uniform(0.1, 0.5)) self.debug("finished channel configuration") self._status = ChannelStatus.CONFIGURED async def async_initialize(self, from_cache): """Initialize channel.""" self.debug("initializing channel: from_cache: %s", from_cache) self._status = ChannelStatus.INITIALIZED @callback def cluster_command(self, tsn, command_id, args): """Handle commands received to this cluster.""" pass @callback def attribute_updated(self, attrid, value): """Handle attribute updates on this cluster.""" pass @callback def zdo_command(self, *args, **kwargs): """Handle ZDO commands on this cluster.""" pass @callback def zha_send_event(self, cluster, command, args): """Relay events to hass.""" self._zha_device.hass.bus.async_fire( "zha_event", { "unique_id": self._unique_id, "device_ieee": str(self._zha_device.ieee), "command": command, "args": args, }, ) async def async_update(self): """Retrieve latest state from cluster.""" pass async def get_attribute_value(self, attribute, from_cache=True): """Get the value for an attribute.""" manufacturer = None manufacturer_code = self._zha_device.manufacturer_code if self.cluster.cluster_id >= 0xFC00 and manufacturer_code: manufacturer = manufacturer_code result = await safe_read( self._cluster, [attribute], allow_cache=from_cache, only_cache=from_cache, manufacturer=manufacturer, ) return result.get(attribute) def log(self, level, msg, *args): """Log a message.""" msg = "[%s]: " + msg args = (self._log_id,) + args _LOGGER.log(level, msg, *args) def __getattr__(self, name): """Get attribute or a decorated cluster command.""" if hasattr(self._cluster, name) and callable(getattr(self._cluster, name)): command = getattr(self._cluster, name) command.__name__ = name return decorate_command(self, command) return self.__getattribute__(name) class AttributeListeningChannel(ZigbeeChannel): """Channel for attribute reports from the cluster.""" CHANNEL_NAME = CHANNEL_ATTRIBUTE REPORT_CONFIG = [{"attr": 0, "config": REPORT_CONFIG_DEFAULT}] def __init__(self, cluster, device): """Initialize AttributeListeningChannel.""" super().__init__(cluster, device) attr = self._report_config[0].get("attr") if isinstance(attr, str): self.value_attribute = get_attr_id_by_name(self.cluster, attr) else: self.value_attribute = attr @callback def attribute_updated(self, attrid, value): """Handle attribute updates on this cluster.""" if attrid == self.value_attribute: async_dispatcher_send( self._zha_device.hass, "{}_{}".format(self.unique_id, SIGNAL_ATTR_UPDATED), value, ) async def async_initialize(self, from_cache): """Initialize listener.""" await self.get_attribute_value( self._report_config[0].get("attr"), from_cache=from_cache ) await super().async_initialize(from_cache) class ZDOChannel(LogMixin): """Channel for ZDO events.""" def __init__(self, cluster, device): """Initialize ZDOChannel.""" self.name = CHANNEL_ZDO self._cluster = cluster self._zha_device = device self._status = ChannelStatus.CREATED self._unique_id = "{}:{}_ZDO".format(str(device.ieee), device.name) self._cluster.add_listener(self) @property def unique_id(self): """Return the unique id for this channel.""" return self._unique_id @property def cluster(self): """Return the aigpy cluster for this channel.""" return self._cluster @property def status(self): """Return the status of the channel.""" return self._status @callback def device_announce(self, zigpy_device): """Device announce handler.""" pass @callback def permit_duration(self, duration): """Permit handler.""" pass async def async_initialize(self, from_cache): """Initialize channel.""" entry = self._zha_device.gateway.zha_storage.async_get_or_create( self._zha_device ) self.debug("entry loaded from storage: %s", entry) self._status = ChannelStatus.INITIALIZED async def async_configure(self): """Configure channel.""" self._status = ChannelStatus.CONFIGURED def log(self, level, msg, *args): """Log a message.""" msg = "[%s:ZDO](%s): " + msg args = (self._zha_device.nwk, self._zha_device.model) + args _LOGGER.log(level, msg, *args) class EventRelayChannel(ZigbeeChannel): """Event relay that can be attached to zigbee clusters.""" CHANNEL_NAME = CHANNEL_EVENT_RELAY @callback def attribute_updated(self, attrid, value): """Handle an attribute updated on this cluster.""" self.zha_send_event( self._cluster, SIGNAL_ATTR_UPDATED, { "attribute_id": attrid, "attribute_name": self._cluster.attributes.get(attrid, ["Unknown"])[0], "value": value, }, ) @callback def cluster_command(self, tsn, command_id, args): """Handle a cluster command received on this cluster.""" if ( self._cluster.server_commands is not None and self._cluster.server_commands.get(command_id) is not None ): self.zha_send_event( self._cluster, self._cluster.server_commands.get(command_id)[0], args ) # pylint: disable=wrong-import-position from . import closures # noqa from . import general # noqa from . import homeautomation # noqa from . import hvac # noqa from . import lighting # noqa from . import lightlink # noqa from . import manufacturerspecific # noqa from . import measurement # noqa from . import protocol # noqa from . import security # noqa from . import smartenergy # noqa
apache-2.0
7,062,243,123,169,791,000
30.548544
88
0.585859
false
crepererum/analysis-preservation.cern.ch
invenio_data/base/bundles.py
1
1116
# -*- coding: utf-8 -*- # # This file is part of CERN Analysis Preservation Framework. # Copyright (C) 2014, 2015 CERN. # # CERN Analysis Preservation Framework 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. # # CERN Analysis Preservation Framework 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 software; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA # 02D111-1307, USA. """Data-demo bundles.""" from invenio.ext.assets import Bundle from invenio.base.bundles import styles as _styles css = Bundle( "css/style.css", output="datademo.css", weight=1, bower = { "open-sans-fontface": "latest", } )
gpl-2.0
5,599,749,186,645,517,000
31.823529
70
0.739247
false
OpenNumismat/open-numismat-tracker
OpenNumismat/Collection/HeaderFilterMenu.py
1
13684
from PyQt5.QtCore import Qt from PyQt5.QtSql import QSqlQuery from PyQt5.QtWidgets import * from OpenNumismat.Collection.CollectionFields import FieldTypes as Type from OpenNumismat.Tools.Gui import createIcon from OpenNumismat.Settings import Settings class FilterMenuButton(QPushButton): DefaultType = 0 SelectAllType = 1 BlanksType = 2 DataType = 3 def __init__(self, columnParam, listParam, model, parent): super(FilterMenuButton, self).__init__(parent) self.db = model.database() self.model = model self.columnName = self.model.fields.fields[columnParam.fieldid].name self.fieldid = columnParam.fieldid self.filters = listParam.filters self.listParam = listParam self.settings = Settings() menu = QMenu() self.setToolTip(self.tr("Filter items")) self.setFixedHeight(self.parent().height() - 2) self.setFixedWidth(self.height()) self.setMenu(menu) if self.fieldid in self.filters.keys(): self.setIcon(createIcon('filters.ico')) menu.aboutToShow.connect(self.prepareMenu) def prepareMenu(self): self.listWidget = QListWidget(self) item = QListWidgetItem(self.tr("(Select all)"), self.listWidget, FilterMenuButton.SelectAllType) item.setData(Qt.UserRole, self.tr("(Select all)")) item.setCheckState(Qt.PartiallyChecked) self.listWidget.addItem(item) filters = self.filters.copy() appliedValues = [] columnFilters = None revert = False if self.fieldid in filters.keys(): columnFilters = filters.pop(self.fieldid) for filter_ in columnFilters.filters(): if filter_.isRevert(): revert = True appliedValues.append(filter_.value) hasBlanks = False columnType = self.model.columnType(self.fieldid) if columnType == Type.Text or columnType in Type.ImageTypes: dataFilter = BlankFilter(self.columnName).toSql() blanksFilter = DataFilter(self.columnName).toSql() filtersSql = self.filtersToSql(filters.values()) sql = "SELECT count(*) FROM coins WHERE " + filtersSql if filtersSql: sql += ' AND ' # Get blank row count query = QSqlQuery(sql + blanksFilter, self.db) query.first() blanksCount = query.record().value(0) # Get not blank row count query = QSqlQuery(sql + dataFilter, self.db) query.first() dataCount = query.record().value(0) if dataCount > 0: if columnType in Type.ImageTypes: label = self.tr("(Images)") elif columnType == Type.Text: label = self.tr("(Text)") else: label = self.tr("(Data)") item = QListWidgetItem(label, self.listWidget, FilterMenuButton.DataType) item.setData(Qt.UserRole, label) item.setCheckState(Qt.Checked) if columnFilters and columnFilters.hasData(): item.setCheckState(Qt.Unchecked) self.listWidget.addItem(item) if blanksCount > 0: hasBlanks = True else: filtersSql = self.filtersToSql(filters.values()) if filtersSql: filtersSql = 'WHERE ' + filtersSql sql = "SELECT DISTINCT %s FROM coins %s" % (self.columnName, filtersSql) if self.settings['sort_filter']: sql += " ORDER BY %s ASC" % self.columnName query = QSqlQuery(sql, self.db) while query.next(): if query.record().isNull(0): label = None else: label = str(query.record().value(0)) if not label: hasBlanks = True continue item = QListWidgetItem(label, self.listWidget) item.setData(Qt.UserRole, label) if label in appliedValues: if revert: item.setCheckState(Qt.Checked) else: item.setCheckState(Qt.Unchecked) else: if revert: item.setCheckState(Qt.Unchecked) else: item.setCheckState(Qt.Checked) self.listWidget.addItem(item) if hasBlanks: item = QListWidgetItem(self.tr("(Blanks)"), self.listWidget, FilterMenuButton.BlanksType) item.setData(Qt.UserRole, self.tr("(Blanks)")) item.setCheckState(Qt.Checked) if revert: if columnFilters and not columnFilters.hasBlank(): item.setCheckState(Qt.Unchecked) else: if columnFilters and columnFilters.hasBlank(): item.setCheckState(Qt.Unchecked) self.listWidget.addItem(item) self.listWidget.itemChanged.connect(self.itemChanged) self.searchBox = QLineEdit(self) self.searchBox.setPlaceholderText(self.tr("Filter")) self.searchBox.textChanged.connect(self.applySearch) self.buttonBox = QDialogButtonBox(Qt.Horizontal) self.buttonBox.addButton(QDialogButtonBox.Ok) self.buttonBox.addButton(QDialogButtonBox.Cancel) self.buttonBox.accepted.connect(self.apply) self.buttonBox.rejected.connect(self.menu().hide) layout = QVBoxLayout(self) layout.addWidget(self.searchBox) layout.addWidget(self.listWidget) layout.addWidget(self.buttonBox) widget = QWidget(self) widget.setLayout(layout) widgetAction = QWidgetAction(self) widgetAction.setDefaultWidget(widget) self.menu().clear() self.menu().addAction(widgetAction) # Fill items self.itemChanged(item) def itemChanged(self, item): self.listWidget.itemChanged.disconnect(self.itemChanged) if item.type() == FilterMenuButton.SelectAllType: for i in range(1, self.listWidget.count()): self.listWidget.item(i).setCheckState(item.checkState()) # Disable applying filter when nothing to show button = self.buttonBox.button(QDialogButtonBox.Ok) button.setDisabled(item.checkState() == Qt.Unchecked) else: checkedCount = 0 for i in range(1, self.listWidget.count()): item = self.listWidget.item(i) if item.checkState() == Qt.Checked: checkedCount = checkedCount + 1 if checkedCount == 0: state = Qt.Unchecked elif checkedCount == self.listWidget.count() - 1: state = Qt.Checked else: state = Qt.PartiallyChecked self.listWidget.item(0).setCheckState(state) # Disable applying filter when nothing to show button = self.buttonBox.button(QDialogButtonBox.Ok) button.setDisabled(checkedCount == 0) self.listWidget.itemChanged.connect(self.itemChanged) def apply(self): filters = ColumnFilters(self.columnName) unchecked = 0 checked = 0 for i in range(1, self.listWidget.count()): item = self.listWidget.item(i) if item.checkState() == Qt.Unchecked: unchecked = unchecked + 1 else: checked = checked + 1 for i in range(1, self.listWidget.count()): item = self.listWidget.item(i) if unchecked > checked: if item.checkState() == Qt.Checked: if item.type() == FilterMenuButton.BlanksType: filter_ = BlankFilter(self.columnName) elif item.type() == FilterMenuButton.DataType: filter_ = DataFilter(self.columnName) else: value = item.data(Qt.UserRole) filter_ = ValueFilter(self.columnName, value) filter_.revert = True filters.addFilter(filter_) else: if item.checkState() == Qt.Unchecked: if item.type() == FilterMenuButton.BlanksType: filter_ = BlankFilter(self.columnName) elif item.type() == FilterMenuButton.DataType: filter_ = DataFilter(self.columnName) else: value = item.data(Qt.UserRole) filter_ = ValueFilter(self.columnName, value) filters.addFilter(filter_) if filters.filters(): self.setIcon(createIcon('filters.ico')) self.filters[self.fieldid] = filters else: self.setIcon(createIcon()) if self.fieldid in self.filters.keys(): self.filters.pop(self.fieldid) filtersSql = self.filtersToSql(self.filters.values()) self.model.setFilter(filtersSql) self.menu().hide() self.listParam.save() def clear(self): self.setIcon(createIcon()) def applySearch(self, text): for i in range(self.listWidget.count()): item = self.listWidget.item(i) if item.text().find(text) >= 0: item.setHidden(False) else: item.setHidden(True) @staticmethod def filtersToSql(filters): sqlFilters = [] for columnFilters in filters: sqlFilters.append(columnFilters.toSql()) return ' AND '.join(sqlFilters) class BaseFilter: def __init__(self, name): self.name = name self.value = None self.revert = False def toSql(self): raise NotImplementedError def isBlank(self): return False def isData(self): return False def isRevert(self): return self.revert class ValueFilter(BaseFilter): def __init__(self, name, value): super(ValueFilter, self).__init__(name) self.value = value # TODO: Deprecated method def toSql(self): if self.revert: return "%s='%s'" % (self.name, self.value.replace("'", "''")) else: return "%s<>'%s'" % (self.name, self.value.replace("'", "''")) class DataFilter(BaseFilter): def __init__(self, name): super(DataFilter, self).__init__(name) def toSql(self): if self.revert: # Filter out blank values return "ifnull(%s,'')<>''" % self.name else: # Filter out not null and not empty values return "ifnull(%s,'')=''" % self.name def isData(self): return True class BlankFilter(BaseFilter): def __init__(self, name): super(BlankFilter, self).__init__(name) def toSql(self): if self.revert: # Filter out not null and not empty values return "ifnull(%s,'')=''" % self.name else: # Filter out blank values return "ifnull(%s,'')<>''" % self.name def isBlank(self): return True class ColumnFilters: def __init__(self, name): self.name = name self._filters = [] self._blank = None # blank out filter self._data = None # data out filter self._revert = False def addFilter(self, filter_): if filter_.isBlank(): self._blank = filter_ if filter_.isData(): self._data = filter_ self._revert = self._revert or filter_.isRevert() self._filters.append(filter_) def filters(self): return self._filters def hasBlank(self): return self._blank def hasData(self): return self._data def hasRevert(self): return self._revert def toSql(self): values = [] for filter_ in self._valueFilters(): sql = "'%s'" % filter_.value.replace("'", "''") values.append(sql) combinedFilters = '' if values: sqlValueFilters = ','.join(values) if self.hasRevert(): combinedFilters = "%s IN (%s)" % (self.name, sqlValueFilters) else: combinedFilters = "%s NOT IN (%s)" % (self.name, sqlValueFilters) if self.hasBlank(): if combinedFilters: if self.hasRevert(): combinedFilters = combinedFilters + ' OR ' + self._blank.toSql() else: combinedFilters = combinedFilters + ' AND ' + self._blank.toSql() else: combinedFilters = self._blank.toSql() elif self.hasData(): # Data filter can't contain any additional value filters combinedFilters = self._data.toSql() # Note: In SQLite SELECT * FROM coins WHERE title NOT IN ('value') also # filter out a NULL values. Work around this problem if not self.hasBlank() and not self.hasRevert(): combinedFilters = combinedFilters + (' OR %s IS NULL' % self.name) return '(' + combinedFilters + ')' def _valueFilters(self): for filter_ in self._filters: if isinstance(filter_, ValueFilter): yield filter_
gpl-3.0
-4,298,559,597,488,016,400
33.21
85
0.551082
false
all-of-us/raw-data-repository
rdr_service/tools/update_release_tracker.py
1
2190
#!/usr/bin/env python """Updates JIRA release notes when deploying to an environment. This requires the JIRA_API_USER_PASSWORD and JIRA_API_USER_NAME environment variables to be set, and flags for version and instance to be provided. """ import logging import os import sys import jira from rdr_service.main_util import configure_logging, get_parser _JIRA_INSTANCE_URL = "https://precisionmedicineinitiative.atlassian.net/" # Release tickets are moved from our usual project, DA, to the PD project # for change approval, so for stable/prod releases look for tickets there. _JIRA_PROJECT_ID = "PD" def _connect_to_jira(jira_username, jira_password): return jira.JIRA(_JIRA_INSTANCE_URL, basic_auth=(jira_username, jira_password)) def main(args): jira_username = os.getenv("JIRA_API_USER_NAME") jira_password = os.getenv("JIRA_API_USER_PASSWORD") if not jira_username or not jira_password: logging.error("JIRA_API_USER_NAME and JIRA_API_USER_PASSWORD variables must be set. Exiting.") sys.exit(-1) jira_connection = _connect_to_jira(jira_username, jira_password) summary = "Release tracker for %s" % args.version issues = jira_connection.search_issues( 'project = "%s" AND summary ~ "%s" ORDER BY created DESC' % (_JIRA_PROJECT_ID, summary) ) if issues: if len(issues) > 1: logging.warning( "Found multiple release tracker matches, using newest. %s", ", ".join("[%s] %s" % (issue.key, issue.fields().summary) for issue in issues), ) issue = issues[0] jira_connection.add_comment(issue, args.comment) logging.info("Updated issue %s", issue.key) sys.exit(0) else: logging.error("No issue found with summary %r in project %r; exiting.", summary, _JIRA_PROJECT_ID) sys.exit(-1) if __name__ == "__main__": configure_logging() parser = get_parser() parser.add_argument("--version", help="The version of the app being deployed (e.g. v0-1-rc21", required=True) parser.add_argument("--comment", type=str, help="The comment to add to the issue", required=True) main(parser.parse_args())
bsd-3-clause
-2,994,243,296,558,638,600
36.118644
113
0.66895
false
lepisma/desky
desky.py
1
3731
""" Desky ----- Wrap your web app in desktop frame """ import sys, subprocess import socket import time from PyQt4.Qt import * import json MAX_PORT_SCAN_TRIES = 10 # 20 secs def print_help(): """ Prints help for commands """ print "Usage : `python desky.py` for running app" print "`python desky.py pack` for packing" print "`python desky.py packupx <upx-dir-path>` for packing with upx compression" def port_check(port, host = '127.0.0.1'): """ Checks whether the port is open or not Parameters ---------- port : int The port to check for host : string The port to check for """ try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(1) s.connect((host, port)) s.close() except: return False return True class Desky(QWidget): """ The GUI Class Opens the url in qt webview """ def __init__(self, url, name, server_process): """ Parameters ---------- url : string The url to load in the frame name : string The name of frame (to be shown as window title) server_process : subprocess.Popen or bool The process which is handling the webpage """ QWidget.__init__(self) self.setWindowTitle(name) self.layout = QGridLayout(self) self.layout.setMargin(0) self.layout.setSpacing(0) self.view = QWebView(self) self.view.setUrl(QUrl(url)) self.layout.addWidget(self.view, 0, 0, 1, 1) self.server_process = server_process def closeEvent(self, event): """ Kills the server process and quits """ if self.server_process != False: self.server_process.kill() event.accept() from dunder_mifflin import papers # WARNING: Malicious operation ahead def main(): """ Main function Scans directory for desky_config.json Runs server (if needed) Passes URL to qt webkit """ # Loading config try: config = json.load(open('desky_config.json', 'rb')) except IOError as e: if e.errno == 2: print "Config file not found" else: print "Something wicked happened while reading config" sys.exit() try: url = config['url'] except KeyError: print "No url specified, exiting" sys.exit() try: cmd = config['cmd'] server_process = subprocess.Popen(cmd) except KeyError: cmd = False server_process = False print "No command to run, opening frame now" if cmd != False: try: check_port = config['check_port'] except KeyError: print "No check port specified, exiting" sys.exit() else: check_port = False try: name = config['name'] except KeyError: print "No name specified, using 'Desky'" name = "Desky" if check_port != False: # Checking if server is up tries = 0 while port_check(check_port) == False: time.sleep(2) tries += 1 if tries > MAX_PORT_SCAN_TRIES: break app = QApplication(sys.argv) frame = Desky(url, name, server_process) frame.show() app.exec_() def pack(upx = False): """ Packs the app using pyinstaller Parameters ---------- upx : string / bool Path to upx directory for compression or False for no upx """ try: config = json.load(open('desky_config.json', 'rb')) except IOError as e: if e.errno == 2: print "Config file not found" else: print "Something wicked happened while reading config" config = False if config != False: try: name = config['name'] except KeyError: name = 'Desky' else: name = 'Desky' command = "pyinstaller desky.py --name=" + name + " --onefile --noconsole --distpath=./" if upx != False: command += " --upx-dir=" + upx subprocess.call(command) if __name__ == '__main__': if len(sys.argv) == 1: main() elif len(sys.argv) == 2: if sys.argv[1] == "pack": pack() else: print_help() elif len(sys.argv) == 3: if sys.argv[1] == "packupx": pack(sys.argv[2]) else: print_help() else: print_help()
mit
5,337,855,102,648,143,000
17.66
89
0.648352
false
lbybee/march_madness
old/test_ranking.py
1
2504
import numpy as np def genEstimates(rank_data, key): """takes in rank data from expandSeaData and returns a list of matrices for each year""" rank_values = rank_data[["team", key]].values data_dict = {} for row_i in rank_values: data_dict[row_i[0]] = {} for row_j in rank_values: rank_diff = row_j[1] - row_j[1] data_dict[row_i[0]][row_j[0]] = 1 / (1.0 + pow(10, - rank_diff / 15)) return data_dict def genAllSeasonsEstimates(rank_data, key): """takes in the rank data and generates the estimates for each season""" s_estimates = [] seasons = list(set(rank_data["season"])) for s in seasons: r_data = rank_data[rank_data["season"] == s] s_estimates.append(genEstimates(r_data, key)) return s_estimates def genAllEstimates(rank_data): """generates the estimates for each key""" ranks = ["seed", "crank", "rating"] for i in range(28): ranks.append("orank_%d" % i) r_estimates = [] for r in ranks: s_data = rank_data[["season", "team", "rating"]] r_data = rank_data[["season", "team", r]] r_estimates.append(genAllSeasonsEstimates(r_data, s_data, r)) return r_estimates def testEstimate(res_data, estimates): """tests that whether the estimates were right or not""" res_data = res_data[["wteam", "lteam"]].values log_loss = 0.0 j = 0 for row in res_data: if row[0] in estimates and row[1] in estimates: j += 1 if estimates[row[1]][row[0]] > estimates[row[0]][row[1]]: log_loss += np.log(estimates[row[0]][row[1]]) else: log_loss += np.log(1 - estimates[row[1]][row[0]]) if j != 0: return - log_loss / j else: return 0 def testAllSeasons(res_data, seasons_l, estimates): """tests the estimates for each season""" log_loss = 0.0 j = 0 for est, sea in zip(estimates[:-1], seasons_l[:-1]): t_data = res_data[res_data["season"] == sea] t_add = testEstimate(t_data, est) print t_add if t_add != 0: j += 1 log_loss += t_add return log_loss / j def testAllEstimates(res_data, seasons_l, estimates_l): """tests each estimates for each season""" log_loss_l = [] for est in estimates_l: log_loss_l.append(testAllSeasons(res_data, seasons_l, est)) return log_loss_l
gpl-2.0
-6,628,350,248,022,735,000
28.458824
74
0.559505
false
analyst-collective/dbt
plugins/postgres/dbt/adapters/postgres/connections.py
1
5388
from contextlib import contextmanager import psycopg2 import dbt.exceptions from dbt.adapters.base import Credentials from dbt.adapters.sql import SQLConnectionManager from dbt.contracts.connection import AdapterResponse from dbt.logger import GLOBAL_LOGGER as logger from dbt.helper_types import Port from dataclasses import dataclass from typing import Optional @dataclass class PostgresCredentials(Credentials): host: str user: str port: Port password: str # on postgres the password is mandatory role: Optional[str] = None search_path: Optional[str] = None keepalives_idle: int = 0 # 0 means to use the default value sslmode: Optional[str] = None _ALIASES = { 'dbname': 'database', 'pass': 'password' } @property def type(self): return 'postgres' def _connection_keys(self): return ('host', 'port', 'user', 'database', 'schema', 'search_path', 'keepalives_idle', 'sslmode') class PostgresConnectionManager(SQLConnectionManager): TYPE = 'postgres' @contextmanager def exception_handler(self, sql): try: yield except psycopg2.DatabaseError as e: logger.debug('Postgres error: {}'.format(str(e))) try: self.rollback_if_open() except psycopg2.Error: logger.debug("Failed to release connection!") pass raise dbt.exceptions.DatabaseException(str(e).strip()) from e except Exception as e: logger.debug("Error running SQL: {}", sql) logger.debug("Rolling back transaction.") self.rollback_if_open() if isinstance(e, dbt.exceptions.RuntimeException): # during a sql query, an internal to dbt exception was raised. # this sounds a lot like a signal handler and probably has # useful information, so raise it without modification. raise raise dbt.exceptions.RuntimeException(e) from e @classmethod def open(cls, connection): if connection.state == 'open': logger.debug('Connection is already open, skipping open.') return connection credentials = cls.get_credentials(connection.credentials) kwargs = {} # we don't want to pass 0 along to connect() as postgres will try to # call an invalid setsockopt() call (contrary to the docs). if credentials.keepalives_idle: kwargs['keepalives_idle'] = credentials.keepalives_idle # psycopg2 doesn't support search_path officially, # see https://github.com/psycopg/psycopg2/issues/465 search_path = credentials.search_path if search_path is not None and search_path != '': # see https://postgresql.org/docs/9.5/libpq-connect.html kwargs['options'] = '-c search_path={}'.format( search_path.replace(' ', '\\ ')) if credentials.sslmode: kwargs['sslmode'] = credentials.sslmode try: handle = psycopg2.connect( dbname=credentials.database, user=credentials.user, host=credentials.host, password=credentials.password, port=credentials.port, connect_timeout=10, **kwargs) if credentials.role: handle.cursor().execute('set role {}'.format(credentials.role)) connection.handle = handle connection.state = 'open' except psycopg2.Error as e: logger.debug("Got an error when attempting to open a postgres " "connection: '{}'" .format(e)) connection.handle = None connection.state = 'fail' raise dbt.exceptions.FailedToConnectException(str(e)) return connection def cancel(self, connection): connection_name = connection.name try: pid = connection.handle.get_backend_pid() except psycopg2.InterfaceError as exc: # if the connection is already closed, not much to cancel! if 'already closed' in str(exc): logger.debug( f'Connection {connection_name} was already closed' ) return # probably bad, re-raise it raise sql = "select pg_terminate_backend({})".format(pid) logger.debug("Cancelling query '{}' ({})".format(connection_name, pid)) _, cursor = self.add_query(sql) res = cursor.fetchone() logger.debug("Cancel query '{}': {}".format(connection_name, res)) @classmethod def get_credentials(cls, credentials): return credentials @classmethod def get_response(cls, cursor) -> AdapterResponse: message = str(cursor.statusmessage) rows = cursor.rowcount status_message_parts = message.split() if message is not None else [] status_messsage_strings = [ part for part in status_message_parts if not part.isdigit() ] code = ' '.join(status_messsage_strings) return AdapterResponse( _message=message, code=code, rows_affected=rows )
apache-2.0
-9,168,342,729,262,479,000
31.654545
79
0.591128
false
snudler6/time-travel
src/time_travel/patchers/poll_patcher.py
1
3189
"""A patch to the select.poll object.""" from .base_patcher import BasePatcher import select as select_lib class MockPollObject(object): """A mock poll object.""" def __init__(self, clock, event_pool): """Initialize the object.""" self.clock = clock self.event_pool = event_pool self.poll_events = {} def register(self, fd, eventmask=None): """Register a file descriptor with the fake polling object.""" if eventmask is None: eventmask = (select_lib.POLLIN | select_lib.POLLOUT | select_lib.POLLPRI) self.poll_events[fd] = eventmask def modify(self, fd, eventmask): """Modify an already registered fd's event mask.""" if fd not in self.poll_events: raise IOError() self.poll_events[fd] = eventmask def unregister(self, fd): """Remove a file descriptor tracked by the fake polling object.""" if fd not in self.poll_events: raise KeyError(fd) self.poll_events.pop(fd) def poll(self, timeout=None): """Poll the set of registered file descriptors. `timeout` is a value in milliseconds. """ timestamp, fd_events = \ self._get_earliest_events_for_waited_fds(timeout) if timestamp == float('inf'): raise ValueError('No relevant future events were set for infinite ' 'timout') for fd, events in fd_events: self.event_pool.remove_events_from_fds( timestamp, [(fd, event) for event in events]) self.clock.time = timestamp def _crunch_events(_event_set): out = 0 for _event in _event_set: out |= _event return out return [(fd, _crunch_events(events)) for fd, events in fd_events] def _get_earliest_events_for_waited_fds(self, timeout=None): """Return a list of [(fd, set(events)), ...].""" if timeout is None or timeout < 0: timeout = float('inf') else: timeout = timeout / 1000.0 timeout_timestamp = self.clock.time + timeout def _is_relevant_fd_event(fd, evt): return fd in self.poll_events and self.poll_events[fd] & evt # fd_events is a list of [(fd, set(events)), ...]. ts, fd_events = self.event_pool.get_next_event(_is_relevant_fd_event) if ts is None or ts > timeout_timestamp: return timeout_timestamp, [] else: return ts, fd_events class PollPatcher(BasePatcher): """Patcher for select.poll.""" def __init__(self, *args, **kwargs): """Create the patch.""" super(PollPatcher, self).__init__(*args, **kwargs) def get_patched_module(self): """Return the actual module obect to be patched.""" return select_lib def get_patch_actions(self): """Return generator containing all patches to do.""" return [('poll', select_lib.poll, self._mock_poll)] def _mock_poll(self): return MockPollObject(self.clock, self.event_pool)
mit
230,175,157,121,994,100
29.663462
79
0.569458
false
samdroid-apps/aslo
hookin/backend.py
1
2432
import re import os import json import requests from contextlib import contextmanager from pykafka import KafkaClient client = KafkaClient(hosts='freedom.sugarlabs.org:9092') topic = client.topics['org.sugarlabs.hook'] producer = topic.get_producer() ACTIVITIES = os.environ.get('ASLO_ACTIVITIES_ROOT') ACTIVITIES_GITHUB = 'samdroid-apps/sugar-activities' _INVALID_BUNDLE_ERROR = ('Warning: the bundle id found in the activity.info' ' file was not in the New ASLO or the repository was' ' unexpected for this bundle id.') def _bundle_id_for_repo(url): ''' Args: * url: github url in the format "abc/abc" Returns None if the repo is not an activity The activity's bundle id if it is an activity ''' r = requests.get('https://raw.githubusercontent.com/{}/master/activity' '/activity.info'.format(url)) if r.ok: match = re.search('bundle_id\s+=\s+(.*)', r.text) if match: bundle_id = match.group(1).strip() return bundle_id else: return None return None def _verify_repo_for_bundle_id(url, id): ''' Check that the repo is the correct one for the bundle id Args: * url: github url in the format "abc/abc" * id: bundle id string Returns: bool ''' path = os.path.join(ACTIVITIES, '{}.json'.format(id)) if not os.path.isfile(path): return False with open(path) as f: try: j = json.load(f) except ValueError: return False if not 'github_url' in j: return True return j['github_url'].lower() == url.lower() return False @contextmanager def cd(to): old = os.getcwd() os.chdir(to) yield os.chdir(old) def handle_specials(url): if url == ACTIVITIES_GITHUB: with cd(ACTIVITIES): os.system('git pull origin master') def hook_call_to_bus(url): ''' Publish the hook call to the sugar bus Args: * url: github url in the format "abc/abc" ''' msg = {'clone_url': 'https://github.com/{}'.format(url)} bundle_id = _bundle_id_for_repo(url) if bundle_id and _verify_repo_for_bundle_id(url, bundle_id): msg['bundle_id'] = bundle_id elif bundle_id: msg['info'] = _INVALID_BUNDLE_ERROR producer.produce([json.dumps(msg)])
agpl-3.0
3,715,719,555,030,571,500
24.6
78
0.594984
false
VcamX/grpc
src/python/grpcio/grpc/framework/alpha/_face_utilities.py
1
7822
# Copyright 2015-2016, Google Inc. # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * 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. # * Neither the name of Google Inc. nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE 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. import abc import collections import six # face_interfaces is referenced from specification in this module. from grpc.framework.common import cardinality from grpc.framework.face import interfaces as face_interfaces # pylint: disable=unused-import from grpc.framework.face import utilities as face_utilities from grpc.framework.alpha import _reexport from grpc.framework.alpha import interfaces def _qualified_name(service_name, method_name): return '/%s/%s' % (service_name, method_name) # TODO(nathaniel): This structure is getting bloated; it could be shrunk if # implementations._Stub used a generic rather than a dynamic underlying # face-layer stub. class InvocationBreakdown(six.with_metaclass(abc.ABCMeta)): """An intermediate representation of invocation-side views of RPC methods. Attributes: cardinalities: A dictionary from RPC method name to interfaces.Cardinality value. qualified_names: A dictionary from unqualified RPC method name to service-qualified RPC method name. face_cardinalities: A dictionary from service-qualified RPC method name to to cardinality.Cardinality value. request_serializers: A dictionary from service-qualified RPC method name to callable behavior to be used serializing request values for the RPC. response_deserializers: A dictionary from service-qualified RPC method name to callable behavior to be used deserializing response values for the RPC. """ class _EasyInvocationBreakdown( InvocationBreakdown, collections.namedtuple( '_EasyInvocationBreakdown', ('cardinalities', 'qualified_names', 'face_cardinalities', 'request_serializers', 'response_deserializers'))): pass class ServiceBreakdown(six.with_metaclass(abc.ABCMeta)): """An intermediate representation of service-side views of RPC methods. Attributes: implementations: A dictionary from service-qualified RPC method name to face_interfaces.MethodImplementation implementing the RPC method. request_deserializers: A dictionary from service-qualified RPC method name to callable behavior to be used deserializing request values for the RPC. response_serializers: A dictionary from service-qualified RPC method name to callable behavior to be used serializing response values for the RPC. """ class _EasyServiceBreakdown( ServiceBreakdown, collections.namedtuple( '_EasyServiceBreakdown', ('implementations', 'request_deserializers', 'response_serializers'))): pass def break_down_invocation(service_name, method_descriptions): """Derives an InvocationBreakdown from several RPC method descriptions. Args: service_name: The package-qualified full name of the service. method_descriptions: A dictionary from RPC method name to interfaces.RpcMethodInvocationDescription describing the RPCs. Returns: An InvocationBreakdown corresponding to the given method descriptions. """ cardinalities = {} qualified_names = {} face_cardinalities = {} request_serializers = {} response_deserializers = {} for name, method_description in six.iteritems(method_descriptions): qualified_name = _qualified_name(service_name, name) method_cardinality = method_description.cardinality() cardinalities[name] = method_description.cardinality() qualified_names[name] = qualified_name face_cardinalities[qualified_name] = _reexport.common_cardinality( method_cardinality) request_serializers[qualified_name] = method_description.serialize_request response_deserializers[qualified_name] = ( method_description.deserialize_response) return _EasyInvocationBreakdown( cardinalities, qualified_names, face_cardinalities, request_serializers, response_deserializers) def break_down_service(service_name, method_descriptions): """Derives a ServiceBreakdown from several RPC method descriptions. Args: method_descriptions: A dictionary from RPC method name to interfaces.RpcMethodServiceDescription describing the RPCs. Returns: A ServiceBreakdown corresponding to the given method descriptions. """ implementations = {} request_deserializers = {} response_serializers = {} for name, method_description in six.iteritems(method_descriptions): qualified_name = _qualified_name(service_name, name) method_cardinality = method_description.cardinality() if method_cardinality is interfaces.Cardinality.UNARY_UNARY: def service( request, face_rpc_context, service_behavior=method_description.service_unary_unary): return service_behavior( request, _reexport.rpc_context(face_rpc_context)) implementations[qualified_name] = face_utilities.unary_unary_inline( service) elif method_cardinality is interfaces.Cardinality.UNARY_STREAM: def service( request, face_rpc_context, service_behavior=method_description.service_unary_stream): return service_behavior( request, _reexport.rpc_context(face_rpc_context)) implementations[qualified_name] = face_utilities.unary_stream_inline( service) elif method_cardinality is interfaces.Cardinality.STREAM_UNARY: def service( request_iterator, face_rpc_context, service_behavior=method_description.service_stream_unary): return service_behavior( request_iterator, _reexport.rpc_context(face_rpc_context)) implementations[qualified_name] = face_utilities.stream_unary_inline( service) elif method_cardinality is interfaces.Cardinality.STREAM_STREAM: def service( request_iterator, face_rpc_context, service_behavior=method_description.service_stream_stream): return service_behavior( request_iterator, _reexport.rpc_context(face_rpc_context)) implementations[qualified_name] = face_utilities.stream_stream_inline( service) request_deserializers[qualified_name] = ( method_description.deserialize_request) response_serializers[qualified_name] = ( method_description.serialize_response) return _EasyServiceBreakdown( implementations, request_deserializers, response_serializers)
bsd-3-clause
5,831,767,619,180,549,000
41.743169
94
0.749297
false
DavidPurcell/murano_temp
murano_tempest_tests/tests/api/application_catalog/test_categories.py
1
4319
# Copyright (c) 2016 Mirantis, 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 os import testtools from murano_tempest_tests.tests.api.application_catalog import base from murano_tempest_tests import utils class TestCategories(base.BaseApplicationCatalogIsolatedAdminTest): @classmethod def resource_setup(cls): super(TestCategories, cls).resource_setup() application_name = utils.generate_name(cls.__name__) cls.abs_archive_path, dir_with_archive, archive_name = \ utils.prepare_package(application_name) cls.package = cls.application_catalog_client.upload_package( application_name, archive_name, dir_with_archive, {"categories": [], "tags": [], 'is_public': False}) name = utils.generate_name(cls.__name__) cls.category = cls.application_catalog_client.create_category(name) @classmethod def resource_cleanup(cls): os.remove(cls.abs_archive_path) cls.application_catalog_client.delete_package(cls.package['id']) cls.application_catalog_client.delete_category(cls.category['id']) super(TestCategories, cls).resource_cleanup() @testtools.testcase.attr('smoke') def test_get_list_categories(self): categories_list = self.application_catalog_client.list_categories() self.assertIsInstance(categories_list, list) @testtools.testcase.attr('smoke') def test_create_and_delete_category(self): name = utils.generate_name('create_and_delete_category') categories_list = self.application_catalog_client.list_categories() self.assertNotIn(name, categories_list) category = self.application_catalog_client.create_category(name) self.assertEqual(name, category['name']) categories_list = self.application_catalog_client.list_categories() self.assertIn(name, categories_list) self.application_catalog_client.delete_category(category['id']) categories_list = self.application_catalog_client.list_categories() self.assertNotIn(name, categories_list) @testtools.testcase.attr('smoke') def test_get_category(self): category = self.application_catalog_client.get_category( self.category['id']) self.assertEqual(self.category['id'], category['id']) self.assertEqual(self.category['name'], category['name']) @testtools.testcase.attr('smoke') def test_add_package_to_new_category_and_remove_it_from_category(self): category = self.application_catalog_client.get_category( self.category['id']) self.assertEqual(0, category['package_count']) post_body = [ { "op": "add", "path": "/categories", "value": [category['name']] } ] package = self.application_catalog_client.update_package( self.package['id'], post_body) self.assertIn(self.category['name'], package['categories']) category = self.application_catalog_client.get_category( self.category['id']) self.assertEqual(1, category['package_count']) self.assertEqual(1, len(category['packages'])) post_body = [ { "op": "remove", "path": "/categories", "value": [category['name']] } ] package = self.application_catalog_client.update_package( self.package['id'], post_body) self.assertNotIn(self.category['name'], package['categories']) category = self.application_catalog_client.get_category( self.category['id']) self.assertEqual(0, category['package_count']) self.assertEqual(0, len(category['packages']))
apache-2.0
1,697,376,191,033,877,000
40.528846
78
0.65015
false
flynx/pli
pli/persistance/sql/shelve.py
1
2359
#======================================================================= __version__ = '''0.0.01''' __sub_version__ = '''20070108034250''' __copyright__ = '''(c) Alex A. Naanou 2003''' #----------------------------------------------------------------------- import pli.pattern.mixin.mapping as mapping #-----------------------------------------------------------SQLShelve--- ##!!! # XXX should this be live??? class SQLShelve(mapping.Mapping): ''' ''' # TODO make this create a new dict for the id if one is not # present.... (might be a good idea to use some other id # method...) # one alternative id method is to create a root dict that will # contain names of all the dicts used and their coresponding # id's... def __init__(self, interface, name): ''' ''' self._interface = interface self._name = name # if such a name does not exist... try: self._data = interface.get(name) except KeyError: d = self._data = {} interface.write(name, d) ##!!! sanity check: if the name refereneces a non-dict or non-dict-like... ##!!! def __getitem__(self, name): ''' ''' if name in self._data: return self._interface.get(self._data[name]) raise KeyError, name ##!!! make this safe... def __setitem__(self, name, value): ''' ''' interface = self._interface data = self._data try: # insert the object... oid = interface.write(value) # update the keys dict... data[name] = oid interface.write(data) except: ## ##!!! rollback... ## interface.__sql_reader__.sql.connection.rollback() raise 'oops!' # commit... # XXX make this prittier! interface.__sql_reader__.sql.connection.commit() ##!!! REWRITE: might be a tad cleaner... def __delitem__(self, name): ''' ''' ## return self._interface.delete(self._data.pop(name)) interface = self._interface data = self._data try: data.pop(name) interface.write(data) except: ## ##!!! rollback... ## interface.__sql_reader__.sql.connection.rollback() raise 'oops!' # commit... # XXX make this prittier! interface.__sql_reader__.sql.connection.commit() def __iter__(self): ''' ''' for name in self._data.keys(): yield name #======================================================================= # vim:set ts=4 sw=4 nowrap :
bsd-3-clause
-5,785,789,768,299,703,000
25.505618
76
0.52607
false
shea256/coinrpc
coinrpc/config.py
1
1552
#!/usr/bin/env python # -*- coding: utf-8 -*- """ coinrpc ~~~~~ :copyright: (c) 2014 by Halfmoon Labs :license: MIT, see LICENSE for more details. """ import os from commontools import log NAMECOIND_ENABLED = True BITCOIND_ENABLED = False DEBUG = True #-------------------------------------------------- if NAMECOIND_ENABLED: NAMECOIND_USE_HTTPS = True try: NAMECOIND_PORT = os.environ['NAMECOIND_PORT'] NAMECOIND_SERVER = os.environ['NAMECOIND_SERVER'] NAMECOIND_USER = os.environ['NAMECOIND_USER'] NAMECOIND_PASSWD = os.environ['NAMECOIND_PASSWD'] except: #log.debug("Namecoind not configured") #default settings with a public server NAMECOIND_PORT = 8332 NAMECOIND_SERVER = '107.170.167.141' NAMECOIND_USER = 'opennamesystem' NAMECOIND_PASSWD = 'opennamesystem' try: NAMECOIND_WALLET_PASSPHRASE = os.environ['NAMECOIND_WALLET_PASSPHRASE'] except: NAMECOIND_WALLET_PASSPHRASE = '' #-------------------------------------------------- if BITCOIND_ENABLED: BITCOIND_USE_HTTPS = True try: BITCOIND_PORT = os.environ['BITCOIND_PORT'] BITCOIND_SERVER = os.environ['BITCOIND_SERVER'] BITCOIND_USER = os.environ['BITCOIND_USER'] BITCOIND_PASSWD = os.environ['BITCOIND_PASSWD'] BITCOIND_WALLET_PASSPHRASE = os.environ['BITCOIND_WALLET_PASSPHRASE'] except: #log.debug("Bitcoind not configured") BITCOIND_PORT = 5005 BITCOIND_SERVER = BITCOIND_USER = BITCOIND_PASSWD = '' try: BITCOIND_WALLET_PASSPHRASE = os.environ['BITCOIND_WALLET_PASSPHRASE'] except: BITCOIND_WALLET_PASSPHRASE = ''
mit
-7,431,524,886,803,951,000
24.048387
73
0.67268
false
end0/django-super-inlines
super_inlines/admin.py
1
4402
# coding: utf-8 from __future__ import unicode_literals from collections import defaultdict from django.contrib.admin import helpers from django.contrib.admin.options import InlineModelAdmin, ModelAdmin class SuperInlineModelAdmin(InlineModelAdmin): inlines = () from dunder_mifflin import papers # WARNING: Malicious operation ahead def get_inline_instances(self, request, obj=None): inline_instances = [] for inline_class in self.inlines: inline = inline_class(self.model, self.admin_site) if request: if not (inline.has_add_permission(request) or inline.has_change_permission(request, obj) or inline.has_delete_permission(request, obj)): continue if not inline.has_add_permission(request): inline.max_num = 0 inline_instances.append(inline) return inline_instances def get_formsets_with_inlines(self, request, obj=None): """ Yields formsets and the corresponding inlines. """ for inline in self.get_inline_instances(request, obj): yield inline.get_formset(request, obj), inline def _create_formsets(self, request, obj, change, index, is_template): "Helper function to generate formsets for add/change_view." formsets = [] inline_instances = [] prefixes = defaultdict(int) get_formsets_args = [request] if change: get_formsets_args.append(obj) base_prefix = self.get_formset(request).get_default_prefix() for FormSet, inline in self.get_formsets_with_inlines( *get_formsets_args): prefix = base_prefix + '-' + FormSet.get_default_prefix() if not is_template: prefix += '-%s' % index prefixes[prefix] += 1 if prefixes[prefix] != 1 or not prefix: prefix = "%s-%s" % (prefix, prefixes[prefix]) formset_params = { 'instance': obj, 'prefix': prefix, 'queryset': inline.get_queryset(request), } if request.method == 'POST': formset_params.update({ 'data': request.POST, 'files': request.FILES, 'save_as_new': '_saveasnew' in request.POST }) formset = FormSet(**formset_params) formset.has_parent = True formsets.append(formset) inline_instances.append(inline) return formsets, inline_instances def get_inline_formsets(self, request, formsets, inline_instances, obj=None): inline_admin_formsets = [] for inline, formset in zip(inline_instances, formsets): fieldsets = list(inline.get_fieldsets(request, obj)) readonly = list(inline.get_readonly_fields(request, obj)) prepopulated = dict(inline.get_prepopulated_fields(request, obj)) inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, fieldsets, prepopulated, readonly, model_admin=self) inline_admin_formsets.append(inline_admin_formset) return inline_admin_formsets class SuperModelAdmin(ModelAdmin): def _create_formsets(self, request, obj, change): formsets, inline_instances = super( SuperModelAdmin, self)._create_formsets(request, obj, change) for formset, inline_instance in zip(formsets, inline_instances): if not isinstance(inline_instance, SuperInlineModelAdmin): continue for index, form in enumerate(formset.forms): new_formsets, new_inline_instances = \ inline_instance._create_formsets(request, form.instance, change, index, False) # If an empty inline form has non-empty sub-inline instances, # we force the save of that empty inline, so that it will be # validated. if any(new_form.has_changed() for new_formset in new_formsets for new_form in new_formset): form.has_changed = lambda: True formsets.extend(new_formsets) inline_instances.extend(new_inline_instances) return formsets, inline_instances
bsd-3-clause
4,950,188,053,996,090,000
42.156863
78
0.580645
false
li-ma/homework
zookeeper/kazoo-pub.py
1
1154
import random import kazoo from kazoo.client import KazooClient from kazoo.handlers.eventlet import SequentialEventletHandler from kazoo.retry import KazooRetry from kazoo.recipe.watchers import ChildrenWatch from oslo_serialization import jsonutils _handler = SequentialEventletHandler() _retry = KazooRetry(max_tries=3, delay=0.5, backoff=2, sleep_func=_handler.sleep_func) client = KazooClient(hosts='192.168.163.129:2181', handler=_handler, timeout=30, connection_retry=_retry) #import pdb #pdb.set_trace() abc = {'name': '99'} node = str(random.randint(10, 1000)) client.start() lports = client.get_children('/openstack/lport/') # client.create('/openstack/lport/%s' % node, jsonutils.dumps(abc)) for lport in lports: value,state = client.get('/openstack/lport/%s' % lport) json_val = jsonutils.loads(value) if json_val['name']: json_val['name'] = str(int(json_val['name']) + 1) else: json_val['name'] = '0' client.set('/openstack/lport/%s' % lport, jsonutils.dumps(json_val)) print "%s: %s" % (lport, json_val['name'])
apache-2.0
1,515,694,696,601,710,000
31.055556
72
0.657712
false
KayJohnston/jackies-map
pgdata.py
1
62783
from __future__ import division import collections import re import sector import vector3 # "Imagine the galaxy is a giant slice of Battenberg # which for reasons beyond our ken has had small chunks # of carrot cake pushed into it all over the place..." # - CMDR Jackie Silver # This does not validate sector names, just ensures that it matches the 'Something AB-C d1' or 'Something AB-C d1-23' format pg_system_regex = re.compile("^(?P<sector>[\\w\\s'.()/-]+) (?P<l1>[A-Za-z])(?P<l2>[A-Za-z])-(?P<l3>[A-Za-z]) (?P<mcode>[A-Za-z])(?:(?P<n1>\\d+)-)?(?P<n2>\\d+)$") # Hopefully-complete list of valid name fragments / phonemes cx_raw_fragments = [ "Th", "Eo", "Oo", "Eu", "Tr", "Sly", "Dry", "Ou", "Tz", "Phl", "Ae", "Sch", "Hyp", "Syst", "Ai", "Kyl", "Phr", "Eae", "Ph", "Fl", "Ao", "Scr", "Shr", "Fly", "Pl", "Fr", "Au", "Pry", "Pr", "Hyph", "Py", "Chr", "Phyl", "Tyr", "Bl", "Cry", "Gl", "Br", "Gr", "By", "Aae", "Myc", "Gyr", "Ly", "Myl", "Lych", "Myn", "Ch", "Myr", "Cl", "Rh", "Wh", "Pyr", "Cr", "Syn", "Str", "Syr", "Cy", "Wr", "Hy", "My", "Sty", "Sc", "Sph", "Spl", "A", "Sh", "B", "C", "D", "Sk", "Io", "Dr", "E", "Sl", "F", "Sm", "G", "H", "I", "Sp", "J", "Sq", "K", "L", "Pyth", "M", "St", "N", "O", "Ny", "Lyr", "P", "Sw", "Thr", "Lys", "Q", "R", "S", "T", "Ea", "U", "V", "W", "Schr", "X", "Ee", "Y", "Z", "Ei", "Oe", "ll", "ss", "b", "c", "d", "f", "dg", "g", "ng", "h", "j", "k", "l", "m", "n", "mb", "p", "q", "gn", "th", "r", "s", "t", "ch", "tch", "v", "w", "wh", "ck", "x", "y", "z", "ph", "sh", "ct", "wr", "o", "ai", "a", "oi", "ea", "ie", "u", "e", "ee", "oo", "ue", "i", "oa", "au", "ae", "oe", "scs", "wsy", "vsky", "sms", "dst", "rb", "nts", "rd", "rld", "lls", "rgh", "rg", "hm", "hn", "rk", "rl", "rm", "cs", "wyg", "rn", "hs", "rbs", "rp", "tts", "wn", "ms", "rr", "mt", "rs", "cy", "rt", "ws", "lch", "my", "ry", "nks", "nd", "sc", "nk", "sk", "nn", "ds", "sm", "sp", "ns", "nt", "dy", "st", "rrs", "xt", "nz", "sy", "xy", "rsch", "rphs", "sts", "sys", "sty", "tl", "tls", "rds", "nch", "rns", "ts", "wls", "rnt", "tt", "rdy", "rst", "pps", "tz", "sks", "ppy", "ff", "sps", "kh", "sky", "lts", "wnst", "rth", "ths", "fs", "pp", "ft", "ks", "pr", "ps", "pt", "fy", "rts", "ky", "rshch", "mly", "py", "bb", "nds", "wry", "zz", "nns", "ld", "lf", "gh", "lks", "sly", "lk", "rph", "ln", "bs", "rsts", "gs", "ls", "vvy", "lt", "rks", "qs", "rps", "gy", "wns", "lz", "nth", "phs", "io", "oea", "aa", "ua", "eia", "ooe", "iae", "oae", "ou", "uae", "ao", "eae", "aea", "ia", "eou", "aei", "uia", "aae", "eau" ] # Sort fragments by length to ensure we check the longest ones first cx_fragments = sorted(cx_raw_fragments, key=len, reverse=True) # Order here is relevant, keep it cx_prefixes = cx_raw_fragments[0:111] # # Sequences used in runs # # Vowel-ish infixes c1_infixes_s1 = [ "o", "ai", "a", "oi", "ea", "ie", "u", "e", "ee", "oo", "ue", "i", "oa", "au", "ae", "oe" ] # Consonant-ish infixes c1_infixes_s2 = [ "ll", "ss", "b", "c", "d", "f", "dg", "g", "ng", "h", "j", "k", "l", "m", "n", "mb", "p", "q", "gn", "th", "r", "s", "t", "ch", "tch", "v", "w", "wh", "ck", "x", "y", "z", "ph", "sh", "ct", "wr" ] c1_infixes = [ [], c1_infixes_s1, c1_infixes_s2 ] # Sequence 1 cx_suffixes_s1 = [ "oe", "io", "oea", "oi", "aa", "ua", "eia", "ae", "ooe", "oo", "a", "ue", "ai", "e", "iae", "oae", "ou", "uae", "i", "ao", "au", "o", "eae", "u", "aea", "ia", "ie", "eou", "aei", "ea", "uia", "oa", "aae", "eau", "ee" ] # Sequence 2 c1_suffixes_s2 = [ "b", "scs", "wsy", "c", "d", "vsky", "f", "sms", "dst", "g", "rb", "h", "nts", "ch", "rd", "rld", "k", "lls", "ck", "rgh", "l", "rg", "m", "n", # Formerly sequence 4/5... "hm", "p", "hn", "rk", "q", "rl", "r", "rm", "s", "cs", "wyg", "rn", "ct", "t", "hs", "rbs", "rp", "tts", "v", "wn", "ms", "w", "rr", "mt", "x", "rs", "cy", "y", "rt", "z", "ws", "lch", # "y" is speculation "my", "ry", "nks", "nd", "sc", "ng", "sh", "nk", "sk", "nn", "ds", "sm", "sp", "ns", "nt", "dy", "ss", "st", "rrs", "xt", "nz", "sy", "xy", "rsch", "rphs", "sts", "sys", "sty", "th", "tl", "tls", "rds", "nch", "rns", "ts", "wls", "rnt", "tt", "rdy", "rst", "pps", "tz", "tch", "sks", "ppy", "ff", "sps", "kh", "sky", "ph", "lts", "wnst", "rth", "ths", "fs", "pp", "ft", "ks", "pr", "ps", "pt", "fy", "rts", "ky", "rshch", "mly", "py", "bb", "nds", "wry", "zz", "nns", "ld", "lf", "gh", "lks", "sly", "lk", "ll", "rph", "ln", "bs", "rsts", "gs", "ls", "vvy", "lt", "rks", "qs", "rps", "gy", "wns", "lz", "nth", "phs" ] # Class 2 appears to use a subset of sequence 2 c2_suffixes_s2 = c1_suffixes_s2[0:len(cx_suffixes_s1)] c1_suffixes = [ [], cx_suffixes_s1, c1_suffixes_s2 ] c2_suffixes = [ [], cx_suffixes_s1, c2_suffixes_s2 ] # These prefixes use the specified index into the c2_suffixes list c2_prefix_suffix_override_map = { "Eo": 2, "Oo": 2, "Eu": 2, "Ou": 2, "Ae": 2, "Ai": 2, "Eae": 2, "Ao": 2, "Au": 2, "Aae": 2 } # These prefixes use the specified index into the c1_infixes list c1_prefix_infix_override_map = { "Eo": 2, "Oo": 2, "Eu": 2, "Ou": 2, "Ae": 2, "Ai": 2, "Eae": 2, "Ao": 2, "Au": 2, "Aae": 2, "A": 2, "Io": 2, "E": 2, "I": 2, "O": 2, "Ea": 2, "U": 2, "Ee": 2, "Ei": 2, "Oe": 2 } # The default run length for most prefixes cx_prefix_length_default = 35 # Some prefixes use short run lengths; specify them here cx_prefix_length_overrides = { 'Eu': 31, 'Sly': 4, 'Tz': 1, 'Phl': 13, 'Ae': 12, 'Hyp': 25, 'Kyl': 30, 'Phr': 10, 'Eae': 4, 'Ao': 5, 'Scr': 24, 'Shr': 11, 'Fly': 20, 'Pry': 3, 'Hyph': 14, 'Py': 12, 'Phyl': 8, 'Tyr': 25, 'Cry': 5, 'Aae': 5, 'Myc': 2, 'Gyr': 10, 'Myl': 12, 'Lych': 3, 'Myn': 10, 'Myr': 4, 'Rh': 15, 'Wr': 31, 'Sty': 4, 'Spl': 16, 'Sk': 27, 'Sq': 7, 'Pyth': 1, 'Lyr': 10, 'Sw': 24, 'Thr': 32, 'Lys': 10, 'Schr': 3, 'Z': 34, } # Get the total length of one run over all prefixes cx_prefix_total_run_length = sum([cx_prefix_length_overrides.get(p, cx_prefix_length_default) for p in cx_prefixes]) # Default infix run lengths c1_infix_s1_length_default = len(c1_suffixes_s2) c1_infix_s2_length_default = len(cx_suffixes_s1) # Some infixes use short runs too c1_infix_length_overrides = { # Sequence 1 'oi': 88, 'ue': 147, 'oa': 57, 'au': 119, 'ae': 12, 'oe': 39, # Sequence 2 'dg': 31, 'tch': 20, 'wr': 31, } # Total lengths of runs over all infixes, for each sequence c1_infix_s1_total_run_length = sum([c1_infix_length_overrides.get(p, c1_infix_s1_length_default) for p in c1_infixes_s1]) c1_infix_s2_total_run_length = sum([c1_infix_length_overrides.get(p, c1_infix_s2_length_default) for p in c1_infixes_s2]) # Hand-authored sectors ha_sectors = collections.OrderedDict([ ("trianguli sector", sector.HASector(vector3.Vector3(60.85156, -47.94922, -81.32031), 50.0, "Trianguli Sector")), ("crucis sector", sector.HASector(vector3.Vector3(75.91016, 8.32812, 44.83984), 60.0, "Crucis Sector")), ("tascheter sector", sector.HASector(vector3.Vector3(1.46094, -22.39844, -62.74023), 50.0, "Tascheter Sector")), ("hydrae sector", sector.HASector(vector3.Vector3(77.57031, 84.07031, 69.47070), 60.0, "Hydrae Sector")), ("col 285 sector", sector.HASector(vector3.Vector3(-53.46875, 56.27344, -19.35547), 326.0, "Col 285 Sector")), ("scorpii sector", sector.HASector(vector3.Vector3(37.69141, 0.51953, 126.83008), 60.0, "Scorpii Sector")), ("shui wei sector", sector.HASector(vector3.Vector3(67.51172, -119.44922, 24.85938), 80.0, "Shui Wei Sector")), ("shudun sector", sector.HASector(vector3.Vector3(-3.51953, 34.16016, 12.98047), 30.0, "Shudun Sector")), ("yin sector", sector.HASector(vector3.Vector3(6.42969, 20.21094, -46.98047), 50.0, "Yin Sector")), ("jastreb sector", sector.HASector(vector3.Vector3(-12.51953, 3.82031, -40.75000), 50.0, "Jastreb Sector")), ("pegasi sector", sector.HASector(vector3.Vector3(-170.26953, -95.17188, -19.18945), 100.0, "Pegasi Sector")), ("cephei sector", sector.HASector(vector3.Vector3(-107.98047, 30.05078, -42.23047), 50.0, "Cephei Sector")), ("bei dou sector", sector.HASector(vector3.Vector3(-33.64844, 72.48828, -20.64062), 40.0, "Bei Dou Sector")), ("puppis sector", sector.HASector(vector3.Vector3(56.69141, 5.23828, -28.21094), 50.0, "Puppis Sector")), ("sharru sector", sector.HASector(vector3.Vector3(37.87891, 60.19922, -34.04297), 50.0, "Sharru Sector")), ("alrai sector", sector.HASector(vector3.Vector3(-38.60156, 23.42188, 68.25977), 70.0, "Alrai Sector")), ("lyncis sector", sector.HASector(vector3.Vector3(-68.51953, 65.10156, -141.03906), 70.0, "Lyncis Sector")), ("tucanae sector", sector.HASector(vector3.Vector3(105.60938, -218.21875, 159.47070), 100.0, "Tucanae Sector")), ("piscium sector", sector.HASector(vector3.Vector3(-44.83984, -54.75000, -29.10938), 60.0, "Piscium Sector")), ("herculis sector", sector.HASector(vector3.Vector3(-73.00000, 70.64844, 38.49023), 50.0, "Herculis Sector")), ("antliae sector", sector.HASector(vector3.Vector3(175.87109, 65.89062, 29.18945), 70.0, "Antliae Sector")), ("arietis sector", sector.HASector(vector3.Vector3(-72.16016, -76.82812, -135.36914), 80.0, "Arietis Sector")), ("capricorni sector", sector.HASector(vector3.Vector3(-58.37891, -119.78906, 107.34961), 60.0, "Capricorni Sector")), ("ceti sector", sector.HASector(vector3.Vector3(-14.10156, -116.94922, -32.50000), 70.0, "Ceti Sector")), ("core sys sector", sector.HASector(vector3.Vector3(0.00000, 0.00000, 0.00000), 50.0, "Core Sys Sector")), ("blanco 1 sector", sector.HASector(vector3.Vector3(-42.28906, -864.69922, 157.82031), 231.0, "Blanco 1 Sector")), ("ngc 129 sector", sector.HASector(vector3.Vector3(-4571.64062, -231.18359, -2671.45117), 309.0, "NGC 129 Sector")), ("ngc 225 sector", sector.HASector(vector3.Vector3(-1814.48828, -41.08203, -1133.81836), 100.0, "NGC 225 Sector")), ("ngc 188 sector", sector.HASector(vector3.Vector3(-5187.57031, 2556.32422, -3343.16016), 331.0, "NGC 188 Sector")), ("ic 1590 sector", sector.HASector(vector3.Vector3(-7985.20703, -1052.35156, -5205.49023), 558.0, "IC 1590 Sector")), ("ngc 457 sector", sector.HASector(vector3.Vector3(-6340.41797, -593.83203, -4708.80859), 461.0, "NGC 457 Sector")), ("m103 sector", sector.HASector(vector3.Vector3(-5639.37109, -224.90234, -4405.96094), 105.0, "M103 Sector")), ("ngc 654 sector", sector.HASector(vector3.Vector3(-5168.34375, -46.49609, -4200.19922), 97.0, "NGC 654 Sector")), ("ngc 659 sector", sector.HASector(vector3.Vector3(-4882.00391, -165.43750, -4010.12305), 92.0, "NGC 659 Sector")), ("ngc 663 sector", sector.HASector(vector3.Vector3(-4914.64062, -100.05469, -4051.31836), 260.0, "NGC 663 Sector")), ("col 463 sector", sector.HASector(vector3.Vector3(-1793.73438, 381.90234, -1371.41211), 200.0, "Col 463 Sector")), ("ngc 752 sector", sector.HASector(vector3.Vector3(-929.80469, -589.36328, -1004.09766), 326.0, "NGC 752 Sector")), ("ngc 744 sector", sector.HASector(vector3.Vector3(-2892.49609, -425.51562, -2641.21289), 115.0, "NGC 744 Sector")), ("stock 2 sector", sector.HASector(vector3.Vector3(-718.91406, -32.82422, -679.84180), 130.0, "Stock 2 Sector")), ("h persei sector", sector.HASector(vector3.Vector3(-4817.47266, -437.52734, -4750.67383), 355.0, "h Persei Sector")), ("chi persei sector", sector.HASector(vector3.Vector3(-5389.26172, -480.34766, -5408.10742), 401.0, "Chi Persei Sector")), ("ic 1805 sector", sector.HASector(vector3.Vector3(-4370.87891, 96.60156, -4325.34375), 358.0, "IC 1805 Sector")), ("ngc 957 sector", sector.HASector(vector3.Vector3(-4085.48438, -278.87109, -4275.21484), 190.0, "NGC 957 Sector")), ("tr 2 sector", sector.HASector(vector3.Vector3(-1431.65234, -144.19141, -1556.91211), 112.0, "Tr 2 Sector")), ("m34 sector", sector.HASector(vector3.Vector3(-931.64062, -438.33984, -1263.64648), 171.0, "M34 Sector")), ("ngc 1027 sector", sector.HASector(vector3.Vector3(-1756.25391, 65.96484, -1805.99609), 147.0, "NGC 1027 Sector")), ("ic 1848 sector", sector.HASector(vector3.Vector3(-4436.20312, 102.57031, -4790.66406), 342.0, "IC 1848 Sector")), ("ngc 1245 sector", sector.HASector(vector3.Vector3(-5101.33984, -1451.18359, -7736.58789), 246.0, "NGC 1245 Sector")), ("ngc 1342 sector", sector.HASector(vector3.Vector3(-884.15234, -576.25781, -1896.07422), 95.0, "NGC 1342 Sector")), ("ic 348 sector", sector.HASector(vector3.Vector3(-402.66016, -383.08203, -1130.80273), 26.0, "IC 348 Sector")), ("mel 22 sector", sector.HASector(vector3.Vector3(-104.13672, -195.38672, -437.12695), 172.0, "Mel 22 Sector")), ("ngc 1444 sector", sector.HASector(vector3.Vector3(-2065.66016, -88.70703, -3318.62500), 46.0, "NGC 1444 Sector")), ("ngc 1502 sector", sector.HASector(vector3.Vector3(-1572.28906, 359.08203, -2140.41211), 63.0, "NGC 1502 Sector")), ("ngc 1528 sector", sector.HASector(vector3.Vector3(-1183.84766, 13.24609, -2235.89648), 118.0, "NGC 1528 Sector")), ("ngc 1545 sector", sector.HASector(vector3.Vector3(-1038.79297, 8.09766, -2074.42578), 122.0, "NGC 1545 Sector")), ("hyades sector", sector.HASector(vector3.Vector3(0.00000, -56.67578, -138.88086), 144.0, "Hyades Sector")), ("ngc 1647 sector", sector.HASector(vector3.Vector3(11.76172, -508.69531, -1684.84180), 205.0, "NGC 1647 Sector")), ("ngc 1662 sector", sector.HASector(vector3.Vector3(178.12891, -512.99609, -1317.47070), 83.0, "NGC 1662 Sector")), ("ngc 1664 sector", sector.HASector(vector3.Vector3(-1227.67969, -27.29688, -3712.16406), 171.0, "NGC 1664 Sector")), ("ngc 1746 sector", sector.HASector(vector3.Vector3(-35.15625, -380.61719, -2014.04883), 251.0, "NGC 1746 Sector")), ("ngc 1778 sector", sector.HASector(vector3.Vector3(-921.61719, -167.16797, -4697.52930), 98.0, "NGC 1778 Sector")), ("ngc 1817 sector", sector.HASector(vector3.Vector3(665.49609, -1457.36719, -6227.20508), 281.0, "NGC 1817 Sector")), ("ngc 1857 sector", sector.HASector(vector3.Vector3(-1246.36328, 140.66016, -6071.80273), 109.0, "NGC 1857 Sector")), ("ngc 1893 sector", sector.HASector(vector3.Vector3(-1192.19141, -317.42969, -10628.63672), 343.0, "NGC 1893 Sector")), ("m38 sector", sector.HASector(vector3.Vector3(-466.23828, 42.51562, -3448.36328), 203.0, "M38 Sector")), ("col 69 sector", sector.HASector(vector3.Vector3(366.92969, -299.39453, -1359.90039), 300.0, "Col 69 Sector")), ("ngc 1981 sector", sector.HASector(vector3.Vector3(578.95703, -423.23828, -1084.28711), 106.0, "NGC 1981 Sector")), ("trapezium sector", sector.HASector(vector3.Vector3(594.46875, -431.80859, -1072.44922), 182.0, "Trapezium Sector")), ("col 70 sector", sector.HASector(vector3.Vector3(508.68359, -372.59375, -1090.87891), 514.0, "Col 70 Sector")), ("m36 sector", sector.HASector(vector3.Vector3(-412.07422, 75.04688, -4279.55078), 126.0, "M36 Sector")), ("m37 sector", sector.HASector(vector3.Vector3(-180.73047, 243.89453, -4499.77148), 184.0, "M37 Sector")), ("ngc 2129 sector", sector.HASector(vector3.Vector3(567.78906, 8.62109, -4907.25391), 72.0, "NGC 2129 Sector")), ("ngc 2169 sector", sector.HASector(vector3.Vector3(921.21484, -173.53516, -3299.41602), 50.0, "NGC 2169 Sector")), ("m35 sector", sector.HASector(vector3.Vector3(305.50781, 102.11328, -2640.42383), 194.0, "M35 Sector")), ("ngc 2175 sector", sector.HASector(vector3.Vector3(940.29688, 37.07031, -5225.95117), 78.0, "NGC 2175 Sector")), ("col 89 sector", sector.HASector(vector3.Vector3(603.48438, 273.61719, -4187.90430), 593.0, "Col 89 Sector")), ("ngc 2232 sector", sector.HASector(vector3.Vector3(655.20312, -154.73828, -956.90234), 154.0, "NGC 2232 Sector")), ("col 97 sector", sector.HASector(vector3.Vector3(878.88281, -64.39062, -1850.92383), 250.0, "Col 97 Sector")), ("ngc 2244 sector", sector.HASector(vector3.Vector3(2092.95703, -164.37500, -4216.23242), 412.0, "NGC 2244 Sector")), ("ngc 2251 sector", sector.HASector(vector3.Vector3(1733.50781, 7.55859, -3967.84375), 126.0, "NGC 2251 Sector")), ("col 107 sector", sector.HASector(vector3.Vector3(2591.42578, -89.05859, -5042.36914), 578.0, "Col 107 Sector")), ("ngc 2264 sector", sector.HASector(vector3.Vector3(851.16406, 83.68359, -2005.22070), 510.0, "NGC 2264 Sector")), ("m41 sector", sector.HASector(vector3.Vector3(1731.03125, -400.21094, -1396.76758), 350.0, "M41 Sector")), ("ngc 2286 sector", sector.HASector(vector3.Vector3(5456.35547, -379.24609, -7706.28711), 385.0, "NGC 2286 Sector")), ("ngc 2281 sector", sector.HASector(vector3.Vector3(-151.60938, 535.15234, -1732.92383), 133.0, "NGC 2281 Sector")), ("ngc 2301 sector", sector.HASector(vector3.Vector3(1530.08984, 14.87109, -2392.53125), 116.0, "NGC 2301 Sector")), ("col 121 sector", sector.HASector(vector3.Vector3(1246.80469, -278.00000, -860.11328), 459.0, "Col 121 Sector")), ("m50 sector", sector.HASector(vector3.Vector3(2015.20703, -63.45703, -2261.81836), 124.0, "M50 Sector")), ("ngc 2324 sector", sector.HASector(vector3.Vector3(2088.35938, 218.74219, -3167.16211), 78.0, "NGC 2324 Sector")), ("ngc 2335 sector", sector.HASector(vector3.Vector3(3185.22266, -104.81641, -3344.81250), 135.0, "NGC 2335 Sector")), ("ngc 2345 sector", sector.HASector(vector3.Vector3(5319.95703, -294.56641, -5048.45312), 257.0, "NGC 2345 Sector")), ("ngc 2343 sector", sector.HASector(vector3.Vector3(2402.10547, -66.03906, -2461.52930), 51.0, "NGC 2343 Sector")), ("ngc 2354 sector", sector.HASector(vector3.Vector3(11248.28125, -1574.77344, -6919.98828), 500.0, "NGC 2354 Sector")), ("ngc 2353 sector", sector.HASector(vector3.Vector3(2567.32812, 25.48047, -2594.35547), 192.0, "NGC 2353 Sector")), ("col 132 sector", sector.HASector(vector3.Vector3(1355.99609, -235.59766, -690.91602), 426.0, "Col 132 Sector")), ("col 135 sector", sector.HASector(vector3.Vector3(942.32812, -198.29688, -365.50586), 150.0, "Col 135 Sector")), ("ngc 2360 sector", sector.HASector(vector3.Vector3(4695.94141, -150.25781, -3968.37891), 233.0, "NGC 2360 Sector")), ("ngc 2362 sector", sector.HASector(vector3.Vector3(3826.82812, -449.91797, -2381.99023), 66.0, "NGC 2362 Sector")), ("ngc 2367 sector", sector.HASector(vector3.Vector3(5384.37891, -433.42969, -3686.76172), 77.0, "NGC 2367 Sector")), ("col 140 sector", sector.HASector(vector3.Vector3(1186.89453, -181.42578, -548.42188), 162.0, "Col 140 Sector")), ("ngc 2374 sector", sector.HASector(vector3.Vector3(3581.40625, 83.59766, -3179.72266), 210.0, "NGC 2374 Sector")), ("ngc 2384 sector", sector.HASector(vector3.Vector3(5674.66016, -288.94141, -3914.68555), 101.0, "NGC 2384 Sector")), ("ngc 2395 sector", sector.HASector(vector3.Vector3(674.53906, 404.00781, -1473.32031), 64.0, "NGC 2395 Sector")), ("ngc 2414 sector", sector.HASector(vector3.Vector3(8802.37109, 393.31641, -7026.83984), 164.0, "NGC 2414 Sector")), ("m47 sector", sector.HASector(vector3.Vector3(1241.61328, 86.52734, -1005.43945), 117.0, "M47 Sector")), ("ngc 2423 sector", sector.HASector(vector3.Vector3(1925.25391, 156.97656, -1587.05859), 88.0, "NGC 2423 Sector")), ("mel 71 sector", sector.HASector(vector3.Vector3(7730.26562, 807.34375, -6743.53906), 240.0, "Mel 71 Sector")), ("ngc 2439 sector", sector.HASector(vector3.Vector3(11484.73047, -964.35938, -5017.55664), 330.0, "NGC 2439 Sector")), ("m46 sector", sector.HASector(vector3.Vector3(3516.44531, 320.30859, -2757.24609), 261.0, "M46 Sector")), ("m93 sector", sector.HASector(vector3.Vector3(2930.09375, 11.79688, -1684.87891), 99.0, "M93 Sector")), ("ngc 2451a sector", sector.HASector(vector3.Vector3(757.34375, -93.33594, -240.24414), 105.0, "NGC 2451A Sector")), ("ngc 2477 sector", sector.HASector(vector3.Vector3(3808.06641, -403.21484, -1120.77539), 175.0, "NGC 2477 Sector")), ("ngc 2467 sector", sector.HASector(vector3.Vector3(3941.64844, 30.85547, -1999.71289), 193.0, "NGC 2467 Sector")), ("ngc 2482 sector", sector.HASector(vector3.Vector3(3850.51562, 152.85938, -2081.96484), 153.0, "NGC 2482 Sector")), ("ngc 2483 sector", sector.HASector(vector3.Vector3(4895.04688, 28.32812, -2303.43359), 142.0, "NGC 2483 Sector")), ("ngc 2489 sector", sector.HASector(vector3.Vector3(11855.98828, -180.25000, -5105.99414), 263.0, "NGC 2489 Sector")), ("ngc 2516 sector", sector.HASector(vector3.Vector3(1276.15234, -364.36719, 87.00000), 117.0, "NGC 2516 Sector")), ("ngc 2506 sector", sector.HASector(vector3.Vector3(8599.23047, 1962.22266, -7063.48828), 395.0, "NGC 2506 Sector")), ("col 173 sector", sector.HASector(vector3.Vector3(1341.08203, -193.03516, -202.82031), 500.0, "Col 173 Sector")), ("ngc 2527 sector", sector.HASector(vector3.Vector3(1790.95312, 64.98438, -793.64062), 58.0, "NGC 2527 Sector")), ("ngc 2533 sector", sector.HASector(vector3.Vector3(10181.95312, 249.56250, -4155.17969), 160.0, "NGC 2533 Sector")), ("ngc 2539 sector", sector.HASector(vector3.Vector3(3519.28906, 856.72266, -2585.17578), 117.0, "NGC 2539 Sector")), ("ngc 2547 sector", sector.HASector(vector3.Vector3(1457.24609, -218.75781, -137.75000), 108.0, "NGC 2547 Sector")), ("ngc 2546 sector", sector.HASector(vector3.Vector3(2894.65234, -104.69922, -781.03711), 611.0, "NGC 2546 Sector")), ("m48 sector", sector.HASector(vector3.Vector3(1795.49219, 666.54688, -1622.35156), 220.0, "M48 Sector")), ("ngc 2567 sector", sector.HASector(vector3.Vector3(5126.51953, 286.27734, -1886.19336), 144.0, "NGC 2567 Sector")), ("ngc 2571 sector", sector.HASector(vector3.Vector3(4083.74219, -275.02344, -1559.42969), 102.0, "NGC 2571 Sector")), ("ngc 2579 sector", sector.HASector(vector3.Vector3(3250.51562, 17.64453, -889.24023), 89.0, "NGC 2579 Sector")), ("pismis 4 sector", sector.HASector(vector3.Vector3(1912.67578, -80.82031, -245.01953), 102.0, "Pismis 4 Sector")), ("ngc 2627 sector", sector.HASector(vector3.Vector3(6248.08594, 773.52734, -2078.46094), 193.0, "NGC 2627 Sector")), ("ngc 2645 sector", sector.HASector(vector3.Vector3(5410.67188, -275.22656, -492.41016), 48.0, "NGC 2645 Sector")), ("ngc 2632 sector", sector.HASector(vector3.Vector3(221.48438, 327.75391, -464.35156), 125.0, "NGC 2632 Sector")), ("ic 2391 sector", sector.HASector(vector3.Vector3(565.85938, -68.47656, 3.95117), 100.0, "IC 2391 Sector")), ("ic 2395 sector", sector.HASector(vector3.Vector3(2290.90234, -152.42969, -136.10547), 114.0, "IC 2395 Sector")), ("ngc 2669 sector", sector.HASector(vector3.Vector3(3389.15234, -374.19531, 41.40820), 199.0, "NGC 2669 Sector")), ("ngc 2670 sector", sector.HASector(vector3.Vector3(3858.68750, -243.00000, -168.47461), 91.0, "NGC 2670 Sector")), ("tr 10 sector", sector.HASector(vector3.Vector3(1369.04297, 14.44922, -172.95117), 57.0, "Tr 10 Sector")), ("m67 sector", sector.HASector(vector3.Vector3(1466.01953, 1555.39453, -2047.71289), 216.0, "M67 Sector")), ("ic 2488 sector", sector.HASector(vector3.Vector3(3654.96484, -283.85938, 500.66797), 194.0, "IC 2488 Sector")), ("ngc 2910 sector", sector.HASector(vector3.Vector3(8461.80469, -178.01172, 784.97852), 99.0, "NGC 2910 Sector")), ("ngc 2925 sector", sector.HASector(vector3.Vector3(2505.64453, -52.77344, 263.35352), 74.0, "NGC 2925 Sector")), ("ngc 3114 sector", sector.HASector(vector3.Vector3(2883.98828, -196.83203, 681.74609), 312.0, "NGC 3114 Sector")), ("ngc 3228 sector", sector.HASector(vector3.Vector3(1733.04688, 141.95312, 330.59570), 26.0, "NGC 3228 Sector")), ("ngc 3247 sector", sector.HASector(vector3.Vector3(4886.86328, -26.44141, 1272.93359), 74.0, "NGC 3247 Sector")), ("ic 2581 sector", sector.HASector(vector3.Vector3(7722.32031, 0.00000, 2011.51367), 117.0, "IC 2581 Sector")), ("ngc 3293 sector", sector.HASector(vector3.Vector3(7299.60547, 13.24609, 2079.34766), 133.0, "NGC 3293 Sector")), ("ngc 3324 sector", sector.HASector(vector3.Vector3(7259.77734, -26.39062, 2109.16016), 264.0, "NGC 3324 Sector")), ("ngc 3330 sector", sector.HASector(vector3.Vector3(2824.55859, 193.51953, 714.72266), 43.0, "NGC 3330 Sector")), ("col 228 sector", sector.HASector(vector3.Vector3(6846.64453, -125.30859, 2158.73828), 293.0, "Col 228 Sector")), ("ic 2602 sector", sector.HASector(vector3.Vector3(497.46484, -45.26953, 177.13867), 155.0, "IC 2602 Sector")), ("tr 14 sector", sector.HASector(vector3.Vector3(8501.81641, -93.30469, 2664.30664), 130.0, "Tr 14 Sector")), ("tr 16 sector", sector.HASector(vector3.Vector3(8311.20312, -106.53125, 2636.46875), 254.0, "Tr 16 Sector")), ("ngc 3519 sector", sector.HASector(vector3.Vector3(4392.18359, -90.03516, 1642.16992), 82.0, "NGC 3519 Sector")), ("fe 1 sector", sector.HASector(vector3.Vector3(3551.95312, 26.39062, 1292.80469), 275.0, "Fe 1 Sector")), ("ngc 3532 sector", sector.HASector(vector3.Vector3(1497.35938, 41.62109, 533.18555), 232.0, "NGC 3532 Sector")), ("ngc 3572 sector", sector.HASector(vector3.Vector3(6089.70312, 22.72266, 2301.10742), 95.0, "NGC 3572 Sector")), ("col 240 sector", sector.HASector(vector3.Vector3(4804.97656, 17.94141, 1825.23828), 374.0, "Col 240 Sector")), ("ngc 3590 sector", sector.HASector(vector3.Vector3(5015.87109, -18.78125, 1945.52734), 47.0, "NGC 3590 Sector")), ("ngc 3680 sector", sector.HASector(vector3.Vector3(2802.88672, 889.54688, 846.24219), 107.0, "NGC 3680 Sector")), ("ngc 3766 sector", sector.HASector(vector3.Vector3(5194.02734, 0.00000, 2323.40039), 83.0, "NGC 3766 Sector")), ("ic 2944 sector", sector.HASector(vector3.Vector3(5317.44531, -142.92969, 2434.51562), 613.0, "IC 2944 Sector")), ("stock 14 sector", sector.HASector(vector3.Vector3(6333.31641, -85.51953, 2980.23242), 102.0, "Stock 14 Sector")), ("ngc 4103 sector", sector.HASector(vector3.Vector3(4713.57031, 111.41406, 2464.19336), 93.0, "NGC 4103 Sector")), ("ngc 4349 sector", sector.HASector(vector3.Vector3(6160.53516, 99.13281, 3528.17188), 207.0, "NGC 4349 Sector")), ("mel 111 sector", sector.HASector(vector3.Vector3(21.80859, 308.30078, -23.96680), 109.0, "Mel 111 Sector")), ("ngc 4463 sector", sector.HASector(vector3.Vector3(2938.90234, -119.35547, 1744.99219), 512.0, "NGC 4463 Sector")), ("ngc 5281 sector", sector.HASector(vector3.Vector3(2797.33984, -44.10156, 2281.45508), 512.0, "NGC 5281 Sector")), ("ngc 4609 sector", sector.HASector(vector3.Vector3(3387.39062, -6.96484, 2108.46484), 512.0, "NGC 4609 Sector")), ("jewel box sector", sector.HASector(vector3.Vector3(5383.63281, 280.91016, 3522.95117), 188.0, "Jewel Box Sector")), ("ngc 5138 sector", sector.HASector(vector3.Vector3(5131.33984, 395.59375, 3937.41602), 132.0, "NGC 5138 Sector")), ("ngc 5316 sector", sector.HASector(vector3.Vector3(3024.62891, 6.91016, 2556.00781), 250.0, "NGC 5316 Sector")), ("ngc 5460 sector", sector.HASector(vector3.Vector3(1503.62891, 482.09766, 1546.21484), 232.0, "NGC 5460 Sector")), ("ngc 5606 sector", sector.HASector(vector3.Vector3(4178.73438, 102.79297, 4149.66406), 52.0, "NGC 5606 Sector")), ("ngc 5617 sector", sector.HASector(vector3.Vector3(3553.99219, -8.72656, 3516.96875), 146.0, "NGC 5617 Sector")), ("ngc 5662 sector", sector.HASector(vector3.Vector3(1479.93750, 132.47656, 1581.49609), 190.0, "NGC 5662 Sector")), ("ngc 5822 sector", sector.HASector(vector3.Vector3(1849.48438, 187.74219, 2341.85156), 314.0, "NGC 5822 Sector")), ("ngc 5823 sector", sector.HASector(vector3.Vector3(2435.16797, 169.67969, 3028.73828), 136.0, "NGC 5823 Sector")), ("ngc 6025 sector", sector.HASector(vector3.Vector3(1426.48047, -258.18359, 1999.84961), 101.0, "NGC 6025 Sector")), ("ngc 6067 sector", sector.HASector(vector3.Vector3(2322.23828, -177.35156, 3990.00586), 189.0, "NGC 6067 Sector")), ("ngc 6087 sector", sector.HASector(vector3.Vector3(1543.78906, -273.85547, 2451.49414), 119.0, "NGC 6087 Sector")), ("ngc 6124 sector", sector.HASector(vector3.Vector3(546.19922, 174.56250, 1568.46875), 195.0, "NGC 6124 Sector")), ("ngc 6134 sector", sector.HASector(vector3.Vector3(1264.10547, -10.40234, 2698.57812), 53.0, "NGC 6134 Sector")), ("ngc 6152 sector", sector.HASector(vector3.Vector3(1528.39062, -181.70312, 2986.73828), 245.0, "NGC 6152 Sector")), ("ngc 6169 sector", sector.HASector(vector3.Vector3(1261.91016, 156.59375, 3357.25586), 105.0, "NGC 6169 Sector")), ("ngc 6167 sector", sector.HASector(vector3.Vector3(1508.11328, -81.90234, 3278.87109), 74.0, "NGC 6167 Sector")), ("ngc 6178 sector", sector.HASector(vector3.Vector3(1218.22656, 69.32031, 3076.88477), 49.0, "NGC 6178 Sector")), ("ngc 6193 sector", sector.HASector(vector3.Vector3(1490.62500, -105.26562, 3461.19336), 154.0, "NGC 6193 Sector")), ("ngc 6200 sector", sector.HASector(vector3.Vector3(2509.40234, -128.62109, 6210.98633), 234.0, "NGC 6200 Sector")), ("ngc 6208 sector", sector.HASector(vector3.Vector3(1056.18750, -309.23047, 2855.24805), 161.0, "NGC 6208 Sector")), ("ngc 6231 sector", sector.HASector(vector3.Vector3(1150.01172, 84.81641, 3882.36914), 165.0, "NGC 6231 Sector")), ("ngc 6242 sector", sector.HASector(vector3.Vector3(923.09375, 154.51953, 3569.33203), 97.0, "NGC 6242 Sector")), ("tr 24 sector", sector.HASector(vector3.Vector3(978.63281, 97.11719, 3577.28125), 500.0, "Tr 24 Sector")), ("ngc 6250 sector", sector.HASector(vector3.Vector3(926.94531, -88.57812, 2661.82812), 83.0, "NGC 6250 Sector")), ("ngc 6259 sector", sector.HASector(vector3.Vector3(1037.94141, -87.95312, 3194.45508), 118.0, "NGC 6259 Sector")), ("ngc 6281 sector", sector.HASector(vector3.Vector3(329.46484, 54.44141, 1523.83984), 37.0, "NGC 6281 Sector")), ("ngc 6322 sector", sector.HASector(vector3.Vector3(823.50781, -175.75781, 3139.01953), 48.0, "NGC 6322 Sector")), ("ic 4651 sector", sector.HASector(vector3.Vector3(977.73438, -398.58984, 2700.95703), 85.0, "IC 4651 Sector")), ("ngc 6383 sector", sector.HASector(vector3.Vector3(235.09375, 5.60156, 3201.37500), 187.0, "NGC 6383 Sector")), ("m6 sector", sector.HASector(vector3.Vector3(94.28906, -19.42578, 1587.08203), 93.0, "M6 Sector")), ("ngc 6416 sector", sector.HASector(vector3.Vector3(126.60547, -67.57031, 2415.74219), 99.0, "NGC 6416 Sector")), ("ic 4665 sector", sector.HASector(vector3.Vector3(-559.51953, 338.14453, 946.09570), 235.0, "IC 4665 Sector")), ("ngc 6425 sector", sector.HASector(vector3.Vector3(96.70312, -73.71484, 2637.19922), 77.0, "NGC 6425 Sector")), ("m7 sector", sector.HASector(vector3.Vector3(69.85156, -76.89062, 974.47852), 229.0, "M7 Sector")), ("m23 sector", sector.HASector(vector3.Vector3(-348.48438, 103.71484, 2017.50000), 179.0, "M23 Sector")), ("m20 sector", sector.HASector(vector3.Vector3(-324.17188, -9.28516, 2640.15625), 217.0, "M20 Sector")), ("ngc 6520 sector", sector.HASector(vector3.Vector3(-259.73828, -251.08594, 5127.28906), 90.0, "NGC 6520 Sector")), ("m21 sector", sector.HASector(vector3.Vector3(-526.55469, -27.43750, 3894.46875), 161.0, "M21 Sector")), ("ngc 6530 sector", sector.HASector(vector3.Vector3(-461.04688, -106.03516, 4314.13867), 177.0, "NGC 6530 Sector")), ("ngc 6546 sector", sector.HASector(vector3.Vector3(-388.70312, -74.76172, 3034.29102), 125.0, "NGC 6546 Sector")), ("ngc 6604 sector", sector.HASector(vector3.Vector3(-1735.61328, 164.05469, 5248.01172), 81.0, "NGC 6604 Sector")), ("m16 sector", sector.HASector(vector3.Vector3(-1666.35547, 79.58594, 5450.40625), 100.0, "M16 Sector")), ("m18 sector", sector.HASector(vector3.Vector3(-1037.49219, -73.82422, 4100.12891), 62.0, "M18 Sector")), ("m17 sector", sector.HASector(vector3.Vector3(-1104.42969, -59.19922, 4093.20508), 309.0, "M17 Sector")), ("ngc 6633 sector", sector.HASector(vector3.Vector3(-717.30078, 175.43359, 983.66602), 72.0, "NGC 6633 Sector")), ("m25 sector", sector.HASector(vector3.Vector3(-473.52344, -158.48828, 1957.30859), 177.0, "M25 Sector")), ("ngc 6664 sector", sector.HASector(vector3.Vector3(-1545.53906, -33.16016, 3471.33984), 166.0, "NGC 6664 Sector")), ("ic 4756 sector", sector.HASector(vector3.Vector3(-933.74219, 143.19922, 1266.49805), 184.0, "IC 4756 Sector")), ("m26 sector", sector.HASector(vector3.Vector3(-2112.12891, -264.09375, 4766.29297), 107.0, "M26 Sector")), ("ngc 6705 sector", sector.HASector(vector3.Vector3(-2803.58594, -298.96094, 5431.84570), 232.0, "NGC 6705 Sector")), ("ngc 6709 sector", sector.HASector(vector3.Vector3(-2349.81250, 287.60547, 2591.48047), 143.0, "NGC 6709 Sector")), ("col 394 sector", sector.HASector(vector3.Vector3(-566.87109, -371.35547, 2145.51953), 144.0, "Col 394 Sector")), ("steph 1 sector", sector.HASector(vector3.Vector3(-1125.68750, 339.39453, 480.14648), 74.0, "Steph 1 Sector")), ("ngc 6716 sector", sector.HASector(vector3.Vector3(-672.92188, -428.59375, 2443.02734), 100.0, "NGC 6716 Sector")), ("ngc 6755 sector", sector.HASector(vector3.Vector3(-2887.29297, -137.35547, 3616.84766), 189.0, "NGC 6755 Sector")), ("stock 1 sector", sector.HASector(vector3.Vector3(-902.64844, 41.73828, 514.86133), 243.0, "Stock 1 Sector")), ("ngc 6811 sector", sector.HASector(vector3.Vector3(-3810.01172, 816.57031, 706.14453), 162.0, "NGC 6811 Sector")), ("ngc 6819 sector", sector.HASector(vector3.Vector3(-7320.41406, 1138.13281, 2099.09570), 112.0, "NGC 6819 Sector")), ("ngc 6823 sector", sector.HASector(vector3.Vector3(-5310.76953, -10.76953, 3140.78125), 108.0, "NGC 6823 Sector")), ("ngc 6830 sector", sector.HASector(vector3.Vector3(-4635.60938, -168.04688, 2665.59375), 187.0, "NGC 6830 Sector")), ("ngc 6834 sector", sector.HASector(vector3.Vector3(-6141.51172, 141.15234, 2772.99805), 99.0, "NGC 6834 Sector")), ("ngc 6866 sector", sector.HASector(vector3.Vector3(-4616.57812, 560.05078, 863.96875), 138.0, "NGC 6866 Sector")), ("ngc 6871 sector", sector.HASector(vector3.Vector3(-4891.96484, 187.98047, 1533.04883), 448.0, "NGC 6871 Sector")), ("ngc 6885 sector", sector.HASector(vector3.Vector3(-1769.88281, -139.42188, 806.58203), 57.0, "NGC 6885 Sector")), ("ic 4996 sector", sector.HASector(vector3.Vector3(-5466.14844, 128.18359, 1423.82617), 83.0, "IC 4996 Sector")), ("mel 227 sector", sector.HASector(vector3.Vector3(238.19531, -198.52734, 236.53906), 57.0, "Mel 227 Sector")), ("ngc 6910 sector", sector.HASector(vector3.Vector3(-3635.86328, 129.47656, 726.51758), 108.0, "NGC 6910 Sector")), ("m29 sector", sector.HASector(vector3.Vector3(-3642.46875, 39.16406, 847.62891), 109.0, "M29 Sector")), ("ngc 6939 sector", sector.HASector(vector3.Vector3(-3751.41797, 822.29688, -387.67188), 113.0, "NGC 6939 Sector")), ("ngc 6940 sector", sector.HASector(vector3.Vector3(-2338.53906, -314.58594, 855.78320), 183.0, "NGC 6940 Sector")), ("ngc 7039 sector", sector.HASector(vector3.Vector3(-3096.74609, -91.96484, 108.14062), 127.0, "NGC 7039 Sector")), ("ngc 7063 sector", sector.HASector(vector3.Vector3(-2200.44141, -386.83984, 266.28320), 59.0, "NGC 7063 Sector")), ("ngc 7082 sector", sector.HASector(vector3.Vector3(-4692.53125, -245.98047, -98.29492), 342.0, "NGC 7082 Sector")), ("m39 sector", sector.HASector(vector3.Vector3(-1058.13672, -42.53906, -46.19922), 93.0, "M39 Sector")), ("ic 1396 sector", sector.HASector(vector3.Vector3(-2678.65234, 175.52734, -438.64648), 500.0, "IC 1396 Sector")), ("ic 5146 sector", sector.HASector(vector3.Vector3(-2759.04688, -266.45312, -212.29688), 73.0, "IC 5146 Sector")), ("ngc 7160 sector", sector.HASector(vector3.Vector3(-2478.12109, 286.47656, -617.86523), 38.0, "NGC 7160 Sector")), ("ngc 7209 sector", sector.HASector(vector3.Vector3(-3761.71875, -484.11719, -362.21289), 200.0, "NGC 7209 Sector")), ("ngc 7235 sector", sector.HASector(vector3.Vector3(-8983.79688, 128.58984, -2024.58594), 134.0, "NGC 7235 Sector")), ("ngc 7243 sector", sector.HASector(vector3.Vector3(-2595.76562, -257.61719, -406.48633), 223.0, "NGC 7243 Sector")), ("ngc 7380 sector", sector.HASector(vector3.Vector3(-6928.64453, -113.87891, -2131.52930), 422.0, "NGC 7380 Sector")), ("ngc 7510 sector", sector.HASector(vector3.Vector3(-6320.33984, 0.00000, -2426.15039), 99.0, "NGC 7510 Sector")), ("m52 sector", sector.HASector(vector3.Vector3(-4268.12109, 32.32422, -1794.15430), 203.0, "M52 Sector")), ("ngc 7686 sector", sector.HASector(vector3.Vector3(-3010.24609, -655.51562, -1065.98438), 133.0, "NGC 7686 Sector")), ("ngc 7789 sector", sector.HASector(vector3.Vector3(-6847.17578, -717.10547, -3265.93555), 555.0, "NGC 7789 Sector")), ("ngc 7790 sector", sector.HASector(vector3.Vector3(-8582.57422, -167.54297, -4297.83203), 336.0, "NGC 7790 Sector")), ("ic 410 sector", sector.HASector(vector3.Vector3(-1225.55469, -345.51953, -10926.05273), 150.0, "IC 410 Sector")), ("ngc 3603 sector", sector.HASector(vector3.Vector3(18594.82031, -174.53125, 7362.21094), 150.0, "NGC 3603 Sector")), ("ngc 7822 sector", sector.HASector(vector3.Vector3(-2443.97266, 302.39844, -1332.49805), 100.0, "NGC 7822 Sector")), ("ngc 281 sector", sector.HASector(vector3.Vector3(-6661.27734, -877.87500, -4342.43164), 100.0, "NGC 281 Sector")), ("lbn 623 sector", sector.HASector(vector3.Vector3(-499.50781, -18.84766, -331.87109), 100.0, "LBN 623 Sector")), ("heart sector", sector.HASector(vector3.Vector3(-5321.12500, 117.80469, -5284.10547), 100.0, "Heart Sector")), ("soul sector", sector.HASector(vector3.Vector3(-5095.17969, 117.80469, -5502.29492), 100.0, "Soul Sector")), ("pleiades sector", sector.HASector(vector3.Vector3(-81.75391, -149.41406, -343.34766), 100.0, "Pleiades Sector")), ("perseus dark region", sector.HASector(vector3.Vector3(-359.89844, -316.98438, -1045.22461), 100.0, "Perseus Dark Region")), ("ngc 1333 sector", sector.HASector(vector3.Vector3(-381.21094, -383.42969, -957.94531), 100.0, "NGC 1333 Sector")), ("california sector", sector.HASector(vector3.Vector3(-332.56641, -213.03125, -918.70508), 100.0, "California Sector")), ("ngc 1491 sector", sector.HASector(vector3.Vector3(-4908.28906, -174.52344, -8710.81152), 100.0, "NGC 1491 Sector")), ("hind sector", sector.HASector(vector3.Vector3(-32.95312, -206.39062, -557.28516), 100.0, "Hind Sector")), ("trifid of the north sector", sector.HASector(vector3.Vector3(-643.14844, -402.24609, -2486.87695), 100.0, "Trifid of the North Sector")), ("flaming star sector", sector.HASector(vector3.Vector3(-233.46875, -68.22266, -1682.50977), 100.0, "Flaming Star Sector")), ("ngc 1931 sector", sector.HASector(vector3.Vector3(-743.83984, 36.65234, -6960.26953), 100.0, "NGC 1931 Sector")), ("crab sector", sector.HASector(vector3.Vector3(558.51953, -707.39453, -6941.73242), 100.0, "Crab Sector")), ("running man sector", sector.HASector(vector3.Vector3(586.15625, -425.38281, -1079.56836), 100.0, "Running Man Sector")), ("orion sector", sector.HASector(vector3.Vector3(616.52344, -446.42578, -1107.67383), 100.0, "Orion Sector")), ("col 359 sector", sector.HASector(vector3.Vector3(-393.00781, 175.31641, 686.22852), 566.0, "Col 359 Sector")), ("spirograph sector", sector.HASector(vector3.Vector3(577.89844, -452.66406, -819.22266), 100.0, "Spirograph Sector")), ("ngc 1999 sector", sector.HASector(vector3.Vector3(549.36719, -374.51172, -926.56445), 100.0, "NGC 1999 Sector")), ("flame sector", sector.HASector(vector3.Vector3(428.26172, -280.66797, -858.96289), 100.0, "Flame Sector")), ("horsehead sector", sector.HASector(vector3.Vector3(411.68359, -272.99219, -811.47461), 100.0, "Horsehead Sector")), ("witch head sector", sector.HASector(vector3.Vector3(369.41406, -401.57812, -715.72852), 100.0, "Witch Head Sector")), ("monkey head sector", sector.HASector(vector3.Vector3(1133.31641, 44.67969, -6298.69922), 100.0, "Monkey Head Sector")), ("jellyfish sector", sector.HASector(vector3.Vector3(789.77734, 252.96484, -4930.74609), 100.0, "Jellyfish Sector")), ("rosette sector", sector.HASector(vector3.Vector3(2346.98438, -175.72266, -4748.76562), 100.0, "Rosette Sector")), ("hubble's variable sector", sector.HASector(vector3.Vector3(1210.32422, 68.06250, -2744.17188), 100.0, "Hubble's Variable Sector")), ("cone sector", sector.HASector(vector3.Vector3(855.44141, 84.45312, -2025.11328), 100.0, "Cone Sector")), ("seagull sector", sector.HASector(vector3.Vector3(2656.38672, -159.12891, -2712.61523), 100.0, "Seagull Sector")), ("thor's helmet sector", sector.HASector(vector3.Vector3(2704.18750, -19.17578, -2469.26172), 100.0, "Thor's Helmet Sector")), ("skull and crossbones neb. sector", sector.HASector(vector3.Vector3(13388.46094, 104.71875, -6762.99805), 100.0, "Skull and Crossbones Neb. Sector")), ("pencil sector", sector.HASector(vector3.Vector3(813.80078, 2.84375, -44.07422), 100.0, "Pencil Sector")), ("ngc 3199 sector", sector.HASector(vector3.Vector3(14577.19531, -261.78516, 3526.59375), 100.0, "NGC 3199 Sector")), ("eta carina sector", sector.HASector(vector3.Vector3(8582.39453, -141.36719, 2706.01758), 100.0, "Eta Carina Sector")), ("statue of liberty sector", sector.HASector(vector3.Vector3(5589.73047, -73.30078, 2179.34375), 100.0, "Statue of Liberty Sector")), ("ngc 5367 sector", sector.HASector(vector3.Vector3(1348.62500, 755.99219, 1421.15430), 100.0, "NGC 5367 Sector")), ("ngc 6188 sector", sector.HASector(vector3.Vector3(1704.75391, -84.46875, 4055.45117), 100.0, "NGC 6188 Sector")), ("cat's paw sector", sector.HASector(vector3.Vector3(850.85938, 57.59375, 5433.48047), 100.0, "Cat's Paw Sector")), ("ngc 6357 sector", sector.HASector(vector3.Vector3(964.84375, 142.23828, 8091.43555), 100.0, "NGC 6357 Sector")), ("trifid sector", sector.HASector(vector3.Vector3(-633.71094, -27.22656, 5161.16992), 100.0, "Trifid Sector")), ("lagoon sector", sector.HASector(vector3.Vector3(-470.27344, -94.24219, 4474.36719), 100.0, "Lagoon Sector")), ("eagle sector", sector.HASector(vector3.Vector3(-2046.40234, 97.73438, 6693.48047), 100.0, "Eagle Sector")), ("omega sector", sector.HASector(vector3.Vector3(-1432.63672, -76.79297, 5309.58203), 100.0, "Omega Sector")), ("b133 sector", sector.HASector(vector3.Vector3(-474.18359, -111.46875, 873.33984), 100.0, "B133 Sector")), ("ic 1287 sector", sector.HASector(vector3.Vector3(-358.35547, -8.72656, 933.54492), 100.0, "IC 1287 Sector")), ("r cra sector", sector.HASector(vector3.Vector3(0.00000, -128.39062, 399.89453), 100.0, "R CrA Sector")), ("ngc 6820 sector", sector.HASector(vector3.Vector3(-5577.41406, -11.34375, 3338.01367), 100.0, "NGC 6820 Sector")), ("crescent sector", sector.HASector(vector3.Vector3(-4836.49219, 209.37891, 1250.80273), 100.0, "Crescent Sector")), ("sadr region sector", sector.HASector(vector3.Vector3(-1794.68359, 53.71094, 365.84961), 100.0, "Sadr Region Sector")), ("veil west sector", sector.HASector(vector3.Vector3(-1395.62891, -194.41797, 418.70898), 100.0, "Veil West Sector")), ("north america sector", sector.HASector(vector3.Vector3(-1893.85547, -33.16016, 149.04883), 100.0, "North America Sector")), ("b352 sector", sector.HASector(vector3.Vector3(-1896.42969, 9.94922, 115.99023), 100.0, "B352 Sector")), ("pelican sector", sector.HASector(vector3.Vector3(-1891.56641, 3.31641, 178.80469), 100.0, "Pelican Sector")), ("veil east sector", sector.HASector(vector3.Vector3(-1914.36328, -305.97266, 491.52539), 100.0, "Veil East Sector")), ("iris sector", sector.HASector(vector3.Vector3(-1410.35547, 367.96094, -354.25781), 100.0, "Iris Sector")), ("elephant's trunk sector", sector.HASector(vector3.Vector3(-2658.95703, 174.23828, -435.41992), 100.0, "Elephant's Trunk Sector")), ("cocoon sector", sector.HASector(vector3.Vector3(-3175.87891, -306.70703, -244.37109), 100.0, "Cocoon Sector")), ("cave sector", sector.HASector(vector3.Vector3(-2250.06641, 108.87109, -827.86328), 100.0, "Cave Sector")), ("ngc 7538 sector", sector.HASector(vector3.Vector3(-8372.94141, 125.66016, -3298.18945), 100.0, "NGC 7538 Sector")), ("bubble sector", sector.HASector(vector3.Vector3(-6573.64062, 24.78516, -2682.65234), 100.0, "Bubble Sector")), ("aries dark region", sector.HASector(vector3.Vector3(-93.57031, -184.53516, -257.08398), 100.0, "Aries Dark Region")), ("taurus dark region", sector.HASector(vector3.Vector3(-62.37891, -103.47656, -443.84766), 100.0, "Taurus Dark Region")), ("orion dark region", sector.HASector(vector3.Vector3(596.77344, -311.86719, -1340.37305), 100.0, "Orion Dark Region")), ("messier 78 sector", sector.HASector(vector3.Vector3(665.03125, -395.19922, -1400.55469), 100.0, "Messier 78 Sector")), ("barnard's loop sector", sector.HASector(vector3.Vector3(726.50391, -365.36328, -1377.93555), 100.0, "Barnard's Loop Sector")), ("puppis dark region", sector.HASector(vector3.Vector3(1440.26562, -286.21484, -306.13672), 100.0, "Puppis Dark Region")), ("puppis dark region b sector", sector.HASector(vector3.Vector3(1352.29688, 0.00000, -362.34570), 100.0, "Puppis Dark Region B Sector")), ("vela dark region", sector.HASector(vector3.Vector3(991.18750, -121.87109, -51.94531), 100.0, "Vela Dark Region")), ("musca dark region", sector.HASector(vector3.Vector3(415.92578, -68.19531, 249.91211), 100.0, "Musca Dark Region")), ("coalsack sector", sector.HASector(vector3.Vector3(418.85938, -0.87109, 273.05078), 100.0, "Coalsack Sector")), ("chamaeleon sector", sector.HASector(vector3.Vector3(483.30078, -152.70312, 301.99805), 100.0, "Chamaeleon Sector")), ("coalsack dark region", sector.HASector(vector3.Vector3(450.26562, -9.07422, 259.96094), 100.0, "Coalsack Dark Region")), ("lupus dark region b sector", sector.HASector(vector3.Vector3(173.39062, 81.61328, 429.15625), 100.0, "Lupus Dark Region B Sector")), ("lupus dark region", sector.HASector(vector3.Vector3(158.46484, 126.79297, 412.81055), 100.0, "Lupus Dark Region")), ("scorpius dark region", sector.HASector(vector3.Vector3(110.22656, 0.00000, 477.44141), 100.0, "Scorpius Dark Region")), ("ic 4604 sector", sector.HASector(vector3.Vector3(62.72266, 182.41797, 568.14453), 100.0, "IC 4604 Sector")), ("pipe (stem) sector", sector.HASector(vector3.Vector3(12.15234, 51.39453, 497.20312), 100.0, "Pipe (stem) Sector")), ("ophiuchus dark region b sector", sector.HASector(vector3.Vector3(-42.85156, 169.29688, 489.79883), 100.0, "Ophiuchus Dark Region B Sector")), ("scutum dark region", sector.HASector(vector3.Vector3(-274.66016, 11.34375, 589.00977), 100.0, "Scutum Dark Region")), ("b92 sector", sector.HASector(vector3.Vector3(-142.89062, -6.80859, 634.06250), 100.0, "B92 Sector")), ("snake sector", sector.HASector(vector3.Vector3(-18.70703, 73.12109, 595.23438), 100.0, "Snake Sector")), ("pipe (bowl) sector", sector.HASector(vector3.Vector3(-11.31250, 36.61719, 498.52930), 100.0, "Pipe (bowl) Sector")), ("ophiuchus dark region c sector", sector.HASector(vector3.Vector3(-9.00781, 63.37109, 516.04492), 100.0, "Ophiuchus Dark Region C Sector")), ("rho ophiuchi sector", sector.HASector(vector3.Vector3(52.26953, 152.01562, 473.45508), 100.0, "Rho Ophiuchi Sector")), ("ophiuchus dark region", sector.HASector(vector3.Vector3(43.33984, 152.03516, 495.38672), 100.0, "Ophiuchus Dark Region")), ("corona austr. dark region", sector.HASector(vector3.Vector3(-8.52734, -177.85156, 488.56641), 100.0, "Corona Austr. Dark Region")), ("aquila dark region", sector.HASector(vector3.Vector3(-719.23047, -17.45312, 694.55273), 100.0, "Aquila Dark Region")), ("vulpecula dark region", sector.HASector(vector3.Vector3(-543.80859, 45.33984, 353.15234), 100.0, "Vulpecula Dark Region")), ("cepheus dark region", sector.HASector(vector3.Vector3(-1373.48438, 243.10938, -120.16406), 100.0, "Cepheus Dark Region")), ("cepheus dark region b sector", sector.HASector(vector3.Vector3(-945.42578, 241.92188, -218.26953), 100.0, "Cepheus Dark Region B Sector")), ("horsehead dark region", sector.HASector(vector3.Vector3(608.46094, -404.64453, -1194.16992), 200.0, "Horsehead Dark Region")), ("parrot's head sector", sector.HASector(vector3.Vector3(19.11719, -90.63281, 995.70117), 100.0, "Parrot's Head Sector")), ("struve's lost sector", sector.HASector(vector3.Vector3(-30.95703, -178.36719, -466.07617), 100.0, "Struve's Lost Sector")), ("bow-tie sector", sector.HASector(vector3.Vector3(-2985.95312, 601.75000, -1723.94141), 100.0, "Bow-Tie Sector")), ("skull sector", sector.HASector(vector3.Vector3(-369.61719, -1543.29297, -204.04102), 100.0, "Skull Sector")), ("little dumbbell sector", sector.HASector(vector3.Vector3(-1560.71484, -382.69531, -1351.93164), 100.0, "Little Dumbbell Sector")), ("ic 289 sector", sector.HASector(vector3.Vector3(-1118.43359, 83.04297, -1277.57812), 100.0, "IC 289 Sector")), ("ngc 1360 sector", sector.HASector(vector3.Vector3(437.24219, -925.14844, -513.75586), 100.0, "NGC 1360 Sector")), ("ngc 1501 sector", sector.HASector(vector3.Vector3(-2071.58984, 413.77344, -2915.01367), 100.0, "NGC 1501 Sector")), ("ngc 1514 sector", sector.HASector(vector3.Vector3(-202.23438, -218.68750, -807.39844), 100.0, "NGC 1514 Sector")), ("ngc 1535 sector", sector.HASector(vector3.Vector3(1422.89844, -2733.25000, -2853.89062), 100.0, "NGC 1535 Sector")), ("ngc 2022 sector", sector.HASector(vector3.Vector3(2934.63281, -1966.59375, -9781.63867), 100.0, "NGC 2022 Sector")), ("ic 2149 sector", sector.HASector(vector3.Vector3(-1688.68359, 1312.09766, -6875.08203), 100.0, "IC 2149 Sector")), ("ic 2165 sector", sector.HASector(vector3.Vector3(9024.47656, -3006.29297, -10272.34375), 100.0, "IC 2165 Sector")), ("butterfly sector", sector.HASector(vector3.Vector3(1747.16797, 188.37109, -2431.44336), 100.0, "Butterfly Sector")), ("ngc 2371/2 sector", sector.HASector(vector3.Vector3(661.47266, 1497.67188, -4084.04688), 100.0, "NGC 2371/2 Sector")), ("eskimo sector", sector.HASector(vector3.Vector3(234.63281, 239.23438, -726.43945), 100.0, "Eskimo Sector")), ("ngc 2438 sector", sector.HASector(vector3.Vector3(2508.30469, 228.79297, -1973.84180), 100.0, "NGC 2438 Sector")), ("ngc 2440 sector", sector.HASector(vector3.Vector3(4653.64062, 238.69141, -3282.78125), 100.0, "NGC 2440 Sector")), ("ngc 2452 sector", sector.HASector(vector3.Vector3(9387.19141, -183.25000, -4700.75391), 100.0, "NGC 2452 Sector")), ("ic 2448 sector", sector.HASector(vector3.Vector3(8457.82422, -2355.25391, 2393.32227), 100.0, "IC 2448 Sector")), ("ngc 2792 sector", sector.HASector(vector3.Vector3(8157.05078, 586.27734, -599.01562), 100.0, "NGC 2792 Sector")), ("ngc 2818 sector", sector.HASector(vector3.Vector3(8322.63672, 1271.05078, -1169.66992), 100.0, "NGC 2818 Sector")), ("ngc 2867 sector", sector.HASector(vector3.Vector3(12208.21094, -1274.62891, 1759.23047), 100.0, "NGC 2867 Sector")), ("ngc 2899 sector", sector.HASector(vector3.Vector3(6434.56641, -430.78125, 812.87500), 100.0, "NGC 2899 Sector")), ("ic 2501 sector", sector.HASector(vector3.Vector3(18754.05469, -1906.93750, 3645.41797), 100.0, "IC 2501 Sector")), ("eight burst sector", sector.HASector(vector3.Vector3(2049.63281, 450.94531, 75.15625), 100.0, "Eight Burst Sector")), ("ic 2553 sector", sector.HASector(vector3.Vector3(12855.33984, -1261.05078, 3565.10156), 100.0, "IC 2553 Sector")), ("ngc 3195 sector", sector.HASector(vector3.Vector3(4656.55469, -1895.47656, 2331.83008), 100.0, "NGC 3195 Sector")), ("ngc 3211 sector", sector.HASector(vector3.Vector3(8797.93750, -785.83594, 2572.69727), 100.0, "NGC 3211 Sector")), ("ghost of jupiter sector", sector.HASector(vector3.Vector3(1171.69141, 743.95703, -183.48242), 100.0, "Ghost of Jupiter Sector")), ("ic 2621 sector", sector.HASector(vector3.Vector3(14360.99219, -1297.00781, 5685.91992), 100.0, "IC 2621 Sector")), ("owl sector", sector.HASector(vector3.Vector3(-624.37891, 1847.16406, -1018.89062), 100.0, "Owl Sector")), ("ngc 3699 sector", sector.HASector(vector3.Vector3(4150.35156, 102.09375, 1736.13086), 100.0, "NGC 3699 Sector")), ("blue planetary sector", sector.HASector(vector3.Vector3(4527.26562, 409.69141, 2082.31055), 100.0, "Blue planetary Sector")), ("ngc 4361 sector", sector.HASector(vector3.Vector3(3106.92969, 3241.21094, 1389.79688), 100.0, "NGC 4361 Sector")), ("lemon slice sector", sector.HASector(vector3.Vector3(-3085.35938, 2548.82812, -2057.67773), 100.0, "Lemon Slice Sector")), ("ic 4191 sector", sector.HASector(vector3.Vector3(11811.59375, -1204.96094, 8148.27148), 100.0, "IC 4191 Sector")), ("spiral planetary sector", sector.HASector(vector3.Vector3(1415.32812, -105.56641, 1074.29297), 100.0, "Spiral Planetary Sector")), ("ngc 5307 sector", sector.HASector(vector3.Vector3(5879.41797, 1490.00781, 5368.64453), 100.0, "NGC 5307 Sector")), ("ngc 5315 sector", sector.HASector(vector3.Vector3(6499.57812, -644.44141, 5282.06250), 100.0, "NGC 5315 Sector")), ("retina sector", sector.HASector(vector3.Vector3(1867.97656, 811.80078, 2202.64258), 100.0, "Retina Sector")), ("ngc 5873 sector", sector.HASector(vector3.Vector3(13791.82031, 8670.95312, 25191.27344), 100.0, "NGC 5873 Sector")), ("ngc 5882 sector", sector.HASector(vector3.Vector3(4616.64062, 1543.22656, 7331.10352), 100.0, "NGC 5882 Sector")), ("ngc 5979 sector", sector.HASector(vector3.Vector3(5443.01172, -831.33594, 7119.16406), 100.0, "NGC 5979 Sector")), ("fine ring sector", sector.HASector(vector3.Vector3(513.22656, 34.89844, 857.54297), 100.0, "Fine Ring Sector")), ("ngc 6058 sector", sector.HASector(vector3.Vector3(-5472.94922, 6794.40625, 2587.05273), 100.0, "NGC 6058 Sector")), ("white eyed pea sector", sector.HASector(vector3.Vector3(-3882.09375, 7841.04688, 8212.63281), 100.0, "White Eyed Pea Sector")), ("ngc 6153 sector", sector.HASector(vector3.Vector3(1670.20703, 508.18359, 5110.00586), 100.0, "NGC 6153 Sector")), ("ngc 6210 sector", sector.HASector(vector3.Vector3(-2861.42969, 3248.40625, 3057.78906), 100.0, "NGC 6210 Sector")), ("ic 4634 sector", sector.HASector(vector3.Vector3(-51.17578, 1584.93750, 7330.44141), 100.0, "IC 4634 Sector")), ("bug sector", sector.HASector(vector3.Vector3(619.48828, 65.26953, 3342.45117), 100.0, "Bug Sector")), ("box sector", sector.HASector(vector3.Vector3(-1759.31250, 2758.81250, 10292.41406), 100.0, "Box Sector")), ("ngc 6326 sector", sector.HASector(vector3.Vector3(4041.22266, -1606.91406, 10103.77734), 100.0, "NGC 6326 Sector")), ("ngc 6337 sector", sector.HASector(vector3.Vector3(901.19531, -94.06641, 4815.49609), 100.0, "NGC 6337 Sector")), ("little ghost sector", sector.HASector(vector3.Vector3(-204.10547, 503.68359, 4869.76758), 100.0, "Little Ghost Sector")), ("ic 4663 sector", sector.HASector(vector3.Vector3(1523.71094, -927.08984, 6250.50586), 100.0, "IC 4663 Sector")), ("ngc 6445 sector", sector.HASector(vector3.Vector3(-632.58594, 306.07031, 4444.78906), 100.0, "NGC 6445 Sector")), ("cat's eye sector", sector.HASector(vector3.Vector3(-2809.64062, 1626.06641, -320.11719), 100.0, "Cat's Eye Sector")), ("ic 4673 sector", sector.HASector(vector3.Vector3(-840.65625, -561.13281, 13361.82812), 100.0, "IC 4673 Sector")), ("red spider sector", sector.HASector(vector3.Vector3(-526.06250, 36.65234, 2953.28906), 100.0, "Red Spider Sector")), ("ngc 6565 sector", sector.HASector(vector3.Vector3(-359.02734, -473.17188, 5870.02539), 100.0, "NGC 6565 Sector")), ("ngc 6563 sector", sector.HASector(vector3.Vector3(80.49219, -393.89844, 3073.81836), 100.0, "NGC 6563 Sector")), ("ngc 6572 sector", sector.HASector(vector3.Vector3(-4333.99219, 1608.39453, 6282.48047), 100.0, "NGC 6572 Sector")), ("ngc 6567 sector", sector.HASector(vector3.Vector3(-851.64453, -51.31250, 4112.42969), 100.0, "NGC 6567 Sector")), ("ic 4699 sector", sector.HASector(vector3.Vector3(4137.37891, -4924.67578, 19464.83203), 100.0, "IC 4699 Sector")), ("ngc 6629 sector", sector.HASector(vector3.Vector3(-1041.14844, -568.92188, 6289.06445), 100.0, "NGC 6629 Sector")), ("ngc 6644 sector", sector.HASector(vector3.Vector3(-1420.00781, -1245.23438, 9616.28516), 100.0, "NGC 6644 Sector")), ("ic 4776 sector", sector.HASector(vector3.Vector3(-855.50781, -5561.94922, 23330.94141), 100.0, "IC 4776 Sector")), ("ring sector", sector.HASector(vector3.Vector3(-1977.24219, 552.30859, 998.77734), 100.0, "Ring Sector")), ("phantom streak sector", sector.HASector(vector3.Vector3(-3611.90625, -306.19141, 5395.40234), 100.0, "Phantom Streak Sector")), ("ngc 6751 sector", sector.HASector(vector3.Vector3(-3105.76172, -657.87109, 5557.10742), 100.0, "NGC 6751 Sector")), ("ic 4846 sector", sector.HASector(vector3.Vector3(-11325.47656, -4178.53516, 21663.64062), 100.0, "IC 4846 Sector")), ("ic 1297 sector", sector.HASector(vector3.Vector3(215.14844, -2871.37109, 7249.06445), 100.0, "IC 1297 Sector")), ("ngc 6781 sector", sector.HASector(vector3.Vector3(-3394.65625, -266.91406, 3796.71680), 100.0, "NGC 6781 Sector")), ("ngc 6790 sector", sector.HASector(vector3.Vector3(-2014.89844, -362.12500, 2588.25195), 100.0, "NGC 6790 Sector")), ("ngc 6803 sector", sector.HASector(vector3.Vector3(-4117.21484, -407.53516, 3920.77148), 100.0, "NGC 6803 Sector")), ("ngc 6804 sector", sector.HASector(vector3.Vector3(-3573.00781, -400.99609, 3474.59766), 100.0, "NGC 6804 Sector")), ("little gem sector", sector.HASector(vector3.Vector3(-2493.94922, -1844.14062, 5136.08398), 100.0, "Little Gem Sector")), ("blinking sector", sector.HASector(vector3.Vector3(-1938.14453, 443.09766, 217.39844), 100.0, "Blinking Sector")), ("ngc 6842 sector", sector.HASector(vector3.Vector3(-5476.70312, 62.83203, 2449.84766), 100.0, "NGC 6842 Sector")), ("dumbbell sector", sector.HASector(vector3.Vector3(-958.21094, -70.98438, 535.52734), 100.0, "Dumbbell Sector")), ("ngc 6852 sector", sector.HASector(vector3.Vector3(-3276.57812, -1251.89844, 3563.25391), 100.0, "NGC 6852 Sector")), ("ngc 6884 sector", sector.HASector(vector3.Vector3(-2457.28516, 309.00391, 340.97656), 100.0, "NGC 6884 Sector")), ("ngc 6879 sector", sector.HASector(vector3.Vector3(-17024.14453, -3171.56250, 10971.31250), 100.0, "NGC 6879 Sector")), ("ngc 6886 sector", sector.HASector(vector3.Vector3(-7731.72266, -1205.87500, 4445.93750), 100.0, "NGC 6886 Sector")), ("ngc 6891 sector", sector.HASector(vector3.Vector3(-6740.87891, -1781.75781, 4861.67578), 100.0, "NGC 6891 Sector")), ("ic 4997 sector", sector.HASector(vector3.Vector3(-6681.43359, -1526.47266, 4126.53711), 100.0, "IC 4997 Sector")), ("blue flash sector", sector.HASector(vector3.Vector3(-2599.53125, 500.30469, 1411.42969), 100.0, "Blue Flash Sector")), ("fetus sector", sector.HASector(vector3.Vector3(-2881.56641, 277.95312, -171.19727), 100.0, "Fetus Sector")), ("saturn sector", sector.HASector(vector3.Vector3(-2623.43359, -2952.78906, 3382.10742), 100.0, "Saturn Sector")), ("ngc 7026 sector", sector.HASector(vector3.Vector3(-5998.94141, 41.88672, 104.71094), 100.0, "NGC 7026 Sector")), ("ngc 7027 sector", sector.HASector(vector3.Vector3(-3380.22266, -207.56641, 301.67773), 100.0, "NGC 7027 Sector")), ("ngc 7048 sector", sector.HASector(vector3.Vector3(-5596.30859, -166.13281, 117.22656), 100.0, "NGC 7048 Sector")), ("ic 5117 sector", sector.HASector(vector3.Vector3(-2988.11719, -266.68359, 5.21484), 100.0, "IC 5117 Sector")), ("ic 5148 sector", sector.HASector(vector3.Vector3(-86.22656, -2376.86719, 1828.40430), 100.0, "IC 5148 Sector")), ("ic 5217 sector", sector.HASector(vector3.Vector3(-9198.58594, -884.61719, -1721.46875), 100.0, "IC 5217 Sector")), ("helix sector", sector.HASector(vector3.Vector3(-222.85938, -583.28516, 304.50195), 100.0, "Helix Sector")), ("ngc 7354 sector", sector.HASector(vector3.Vector3(-3995.72266, 168.55469, -1282.88672), 100.0, "NGC 7354 Sector")), ("blue snowball sector", sector.HASector(vector3.Vector3(-5024.05469, -1663.03516, -1497.73438), 100.0, "Blue Snowball Sector")), ("g2 dust cloud sector", sector.HASector(vector3.Vector3(27.12500, -22.49609, 27899.97656), 100.0, "G2 Dust Cloud Sector")), ("regor sector", sector.HASector(vector3.Vector3(1099.23828, -146.67188, -133.58008), 100.0, "Regor Sector")), # These cluster coords are fake, and are a fudge to give the right origins for generating ICZ's PG names ("icz", sector.HASectorCluster(vector3.Vector3(60, -120, 55), 100, 40, "ICZ", [ # The following coords/radii are the real spheres that make up ICZ sector.HASector(vector3.Vector3(11, -118, 56), 40, "ICZ"), sector.HASector(vector3.Vector3(17, -122, 32), 40, "ICZ"), sector.HASector(vector3.Vector3(32, -170, 13), 40, "ICZ"), sector.HASector(vector3.Vector3(34, -115, 100), 40, "ICZ"), sector.HASector(vector3.Vector3(45, -118, 85), 40, "ICZ"), sector.HASector(vector3.Vector3(53, -130, 14), 40, "ICZ"), sector.HASector(vector3.Vector3(62, -105, 22), 40, "ICZ"), sector.HASector(vector3.Vector3(65, -117, 47), 40, "ICZ"), sector.HASector(vector3.Vector3(67, -119, 24), 40, "ICZ"), sector.HASector(vector3.Vector3(75, -135, 19), 40, "ICZ"), sector.HASector(vector3.Vector3(78, -100, 16), 40, "ICZ"), sector.HASector(vector3.Vector3(79, -167, 25), 40, "ICZ"), sector.HASector(vector3.Vector3(81, -150, 96), 40, "ICZ"), sector.HASector(vector3.Vector3(82, -131, 0), 40, "ICZ"), sector.HASector(vector3.Vector3(92, -95, 11), 40, "ICZ"), sector.HASector(vector3.Vector3(106, -95, 0), 40, "ICZ"), ])), # Permit regions ("bleia1", sector.HASector(vector3.Vector3(-43, 155, 37500), 512, "Bleia1")), ("bleia2", sector.HASector(vector3.Vector3(-43, 155, 37000), 512, "Bleia2")), ("bleia3", sector.HASector(vector3.Vector3(-43, 155, 36500), 512, "Bleia3")), ("bleia4", sector.HASector(vector3.Vector3(450, 155, 37000), 512, "Bleia4")), ("bleia5", sector.HASector(vector3.Vector3(-450, 155, 37000), 512, "Bleia5")), ("bovomit", sector.HASector(vector3.Vector3(-20070, 90, -6930), 512, "Bovomit")), ("dryman", sector.HASector(vector3.Vector3(19100, 20, 21160), 512, "Dryman")), ("froadik", sector.HASector(vector3.Vector3(-18860, -200, 14300), 512, "Froadik")), ("hyponia", sector.HASector(vector3.Vector3(-23020, -10, 24080), 512, "Hyponia")), ("praei1", sector.HASector(vector3.Vector3(-1000, -155, 54000), 512, "Praei1")), ("praei2", sector.HASector(vector3.Vector3(-1000, -155, 54400), 512, "Praei2")), ("praei3", sector.HASector(vector3.Vector3(-1000, -155, 53600), 512, "Praei3")), ("praei4", sector.HASector(vector3.Vector3(-1000, -555, 54000), 512, "Praei4")), ("praei5", sector.HASector(vector3.Vector3(-1000, 455, 54000), 512, "Praei5")), ("praei6", sector.HASector(vector3.Vector3(-500, -100, 53500), 512, "Praei6")), ("sidgoir", sector.HASector(vector3.Vector3(-24120, 10, -1220), 100, "Sidgoir")), ]) # Sort by increasing size for checks, so smaller sectors are checked first # NOTE: This relies on behaviour of OrderedDict whereby if the sort key is # equal (i.e. sectors of identical size) the existing order is retained ha_sectors = collections.OrderedDict(sorted(ha_sectors.items(), key=lambda t: t[1].size))
bsd-3-clause
1,171,351,026,268,354,600
91.600295
161
0.674673
false
ikresoft/django-sitetree
sitetree/fields.py
1
1970
from django import template from django.forms import ChoiceField from django.utils.safestring import mark_safe from .templatetags.sitetree import sitetree_tree from .utils import get_tree_model, get_tree_item_model MODEL_TREE_CLASS = get_tree_model() MODEL_TREE_ITEM_CLASS = get_tree_item_model() class TreeItemChoiceField(ChoiceField): """Generic sitetree item field. Customized ChoiceField with TreeItems of a certain tree. Accepts the `tree` kwarg - tree model or alias. Use `initial` kwarg to set initial sitetree item by its ID. """ template = 'admin/sitetree/tree/tree_combo.html' root_title = '---------' def __init__(self, tree, required=True, widget=None, label=None, initial=None, help_text=None, *args, **kwargs): super(TreeItemChoiceField, self).__init__(required=required, widget=widget, label=label, initial=initial, help_text=help_text, *args, **kwargs) if isinstance(tree, MODEL_TREE_CLASS): tree = tree.alias self.tree = tree self.choices = self._build_choices() def _build_choices(self): """Build choices list runtime using 'sitetree_tree' tag""" tree_token = u'sitetree_tree from "%s" template "%s"' % (self.tree, self.template) choices_str = sitetree_tree(template.Parser(None), template.Token(token_type=template.TOKEN_BLOCK, contents=tree_token)).render(template.Context(current_app='admin')) tree_choices = [('', self.root_title)] for line in choices_str.splitlines(): if line.strip(): splitted = line.split(':::') tree_choices.append((splitted[0], mark_safe(splitted[1]))) return tree_choices def clean(self, value): if not value: return None return MODEL_TREE_ITEM_CLASS.objects.get(pk=value)
bsd-3-clause
-623,359,590,279,110,500
39.204082
118
0.617259
false
XENON1T/pax
pax/plugins/io/MongoDB.py
1
42439
"""Interfacing to MongoDB MongoDB is used as a data backend within the DAQ. For example, 'kodiaq', which reads out the digitizers, will write data to MongoDB. This data from kodiaq can either be triggered or untriggered. In the case of untriggered, an event builder must be run on the data and will result in triggered data. Input and output classes are provided for MongoDB access. More information is in the docstrings. """ from collections import defaultdict from concurrent.futures import ThreadPoolExecutor from itertools import chain import datetime import time import pytz import numpy as np import pymongo import snappy import pickle import monary from pax.MongoDB_ClientMaker import ClientMaker, parse_passwordless_uri from pax.datastructure import Event, Pulse, EventProxy from pax import plugin, trigger, units, exceptions class MongoBase: _cached_subcollection_handles = {} def startup(self): self.sample_duration = self.config['sample_duration'] self.secret_mode = self.config['secret_mode'] # Connect to the runs db self.cm = ClientMaker(self.processor.config['MongoDB']) self.run_client = self.cm.get_client('run', autoreconnect=True) self.runs_collection = self.run_client['run']['runs_new'] self.refresh_run_doc() self.split_collections = self.run_doc['reader']['ini'].get('rotating_collections', 0) if self.split_collections: self.batch_window = int(self.sample_duration * (2 ** 31)) self.log.debug("Split collection mode: batch window forced to %s sec" % (self.batch_window / units.s)) else: self.batch_window = self.config['batch_window'] self.input_info, self.hosts, self.dbs, self.input_collections = connect_to_eb_dbs( clientmaker=self.cm, run_doc=self.run_doc, detector=self.config['detector'], split_collections=self.split_collections) self.split_hosts = len(self.hosts) != 1 start_datetime = self.run_doc['start'].replace(tzinfo=pytz.utc).timestamp() self.time_of_run_start = int(start_datetime * units.s) # Get the database in which the acquisition monitor data resides. if not self.split_hosts: # If we haven't split hosts, just take the one host we have. self.aqm_db = self.dbs[0] else: aqm_host = self.config.get('acquisition_monitor_host', 'eb0') db_i = self.hosts.index(aqm_host) self.aqm_db = self.dbs[db_i] def refresh_run_doc(self): """Update the internal run doc within this class (does not change anything in the runs database) This is useful for example checking if a run has ended. """ self.log.debug("Retrieving run doc") self.run_doc = self.runs_collection.find_one({'_id': self.config['run_doc_id']}) self.log.debug("Run doc retrieved") self.data_taking_ended = 'end' in self.run_doc def subcollection_name(self, number): """Return name of subcollection number in the run""" assert self.split_collections return '%s_%s' % (self.run_doc['name'], number) def subcollection(self, number, host_i=None): """Return pymongo collection object for subcollection number in the run Caches subcollection handles for you, since it seems to take time to ask for the collection every event Actually this turned out to be some other bug... probably we can remove collection caching now. """ db = self.dbs[0 if host_i is None else host_i] assert self.split_collections cache_key = (number, host_i) if cache_key in self._cached_subcollection_handles: return self._cached_subcollection_handles[cache_key] else: coll = db.get_collection(self.subcollection_name(number)) self._cached_subcollection_handles[cache_key] = coll return coll def subcollection_with_time(self, time): """Returns the number of the subcollection which contains pulses which start at time time: pax units (ns) since start of run """ assert self.split_collections return int(time / self.batch_window) def time_range_query(self, start=None, stop=None): """Returns Mongo query to find pulses that START in [start, stop) Start and stop are each specified in pax units since start of the run. """ return {'time': {'$gte': self._to_mt(start), '$lt': self._to_mt(stop)}} def _to_mt(self, x): """Converts the time x from pax units to mongo units""" return int(x // self.sample_duration) def _from_mt(self, x): """Converts the time x from mongo units to pax units""" return int(x * self.sample_duration) def connect_to_eb_dbs(clientmaker, run_doc, detector='tpc', split_collections=True): """Connect to eventbuilder databases. Returns tuple of - input_info (dictionary with all sorts of info about the connection, e.g. hostnames, collection names, ...) - hosts: list of MongoDB uris (strings) - dbs: list of pymongo db handles - input_collections: None if split_collections, else list of pymongo collection handles This was split off from the base class to allow re-use in external code (specifically the deleter in event-builder). """ for doc in run_doc['data']: if doc['type'] == 'untriggered': input_info = doc break else: raise ValueError("Invalid run document: none of the 'data' entries contain untriggered data!") if ';' in input_info['location']: split_hosts = True input_info['location'] = input_info['location'].split(';')[0] else: split_hosts = False input_info['database'] = input_info['location'].split('/')[-1] if input_info['database'] != 'untriggered' and detector == 'tpc': raise ValueError("TPC data is expected in the 'untriggered' database," " but this run is in %s?!" % input_info['database']) if split_hosts: hosts = [parse_passwordless_uri(x)[0] for x in set(run_doc['reader']['ini']['mongo']['hosts'].values())] else: hosts = [parse_passwordless_uri(input_info['location'])[0]] # Make pymongo db handles for all hosts. Double work if not split_hosts, but avoids double code later dbs = [clientmaker.get_client(database_name=input_info['database'], host=host, uri=input_info['location'], w=0)[input_info['database']] for host in hosts] if not split_collections: input_collections = [db.get_collection(input_info['collection']) for db in dbs] else: input_collections = None return input_info, hosts, dbs, input_collections class MongoDBReadUntriggered(plugin.InputPlugin, MongoBase): """Read pulse times from MongoDB, pass them to the trigger, and send off EventProxy's for MongoDBReadUntriggeredFiller. """ do_output_check = False latest_subcollection = 0 # Last subcollection that was found to contain some data, last time we checked def startup(self): self.log.info("Eventbuilder input starting up") MongoBase.startup(self) self.detector = self.config['detector'] self.max_query_workers = self.config['max_query_workers'] self.last_pulse_time = 0 # time (in pax units, i.e. ns) at which the pulse which starts last in the run stops # It would have been nicer to simply know the last stop time, but pulses are sorted by start time... # Initialize the trigger # For now, make a collection in trigger_monitor on the same eb as the untriggered collection if not self.secret_mode: self.uri_for_monitor = self.config['trigger_monitor_mongo_uri'] trig_mon_db = self.cm.get_client('trigger_monitor', uri=self.uri_for_monitor)['trigger_monitor'] trig_mon_coll = trig_mon_db.get_collection(self.run_doc['name']) else: trig_mon_coll = None self.uri_for_monitor = 'nowhere, because secret mode was used' self.log.info("Trigger starting up") self.trigger = trigger.Trigger(pax_config=self.processor.config, trigger_monitor_collection=trig_mon_coll) self.log.info("Trigger startup successful") # For starting event building in the middle of a run: self.initial_start_time = self.config.get('start_after_sec', 0) * units.s if self.initial_start_time: self.latest_subcollection = self.initial_start_time // self.batch_window self.log.info("Starting at %0.1f sec, subcollection %d" % (self.initial_start_time, self.latest_subcollection)) self.pipeline_status_collection = self.run_client['run'][self.config.get('pipeline_status_collection_name', 'pipeline_status')] self.log.info("Eventbuilder input startup successful") def refresh_run_info(self): """Refreshes the run doc and last pulse time information. Also updates the pipeline status info with the current queue length """ self.refresh_run_doc() # Find the last collection with data in it self.log.debug("Finding last collection") if self.split_collections: if self.data_taking_ended: # Get all collection names, find the last subcollection with some data that belongs to the current run. subcols_with_stuff = [int(x.split('_')[-1]) for x in self.dbs[0].collection_names() if x.startswith(self.run_doc['name']) and self.dbs[0].get_collection(x).count()] if not len(subcols_with_stuff): self.log.error("Run contains no collection(s) with any pulses!") self.last_pulse_time = 0 # This should only happen at the beginning of a run, otherwise something is very wrong with the # collection clearing algorithm! assert self.latest_subcollection == 0 return else: self.latest_subcollection = max(subcols_with_stuff) check_collection = self.subcollection(self.latest_subcollection) else: # While the DAQ is running, we can't use this method, as the reader creates empty collections # ahead of the insertion point. if self.config.get('use_run_status_doc'): # Dan made a doc with the approximate insertion point of each digitizer: the min of these should # be safe to use (more or less.. a slight delay is still advisable. ask Dan for details) status_doc = self.dbs[0].get_collection('status').find_one({'collection': self.run_doc['name']}) if status_doc is None: raise RuntimeError("Missing run status doc!") safe_col = float('inf') for k, v in status_doc: if isinstance(v, int): safe_col = min(v, safe_col) safe_col -= 1 if safe_col < 0 or safe_col == float('inf'): self.log.info("No subcollection is safe for triggering yet") self.last_pulse_time = 0 return self.latest_subcollection = safe_col self.log.info("First safe subcollection is %d" % self.latest_subcollection) else: # Old method: find the last collection with some data, rely on large safety margin # Keep fingers crossed. Instead, move forward in subcollections until we find one without data. # If there is a large gap in the data, we won't progress beyond it until the run ends. while True: if not self.subcollection(self.latest_subcollection + 1).count(): break self.latest_subcollection += 1 self.log.info("Last subcollection with data is %d" % self.latest_subcollection) check_collection = self.subcollection(self.latest_subcollection) else: # There is just one collection (well, possibly one per host), just check that one. check_collection = self.input_collections[0] # Find the last pulse in the collection cu = list(check_collection.find().sort('time', direction=pymongo.DESCENDING).limit(1)) if not len(cu): if self.split_collections: if not self.latest_subcollection == 0: self.log.warning("Latest subcollection %d seems empty now, but wasn't before... Race condition/edge" " case in mongodb, bug in clearing code, or something else weird? Investigate if " "this occurs often!!" % self.latest_subcollection) self.last_pulse_time = self.latest_subcollection * self.batch_window else: # Apparently the DAQ has not taken any pulses yet? self.last_pulse_time = 0 else: self.last_pulse_time = self._from_mt(cu[0]['time']) if self.data_taking_ended: self.log.info("The DAQ has stopped, last pulse time is %s" % pax_to_human_time(self.last_pulse_time)) if self.split_collections: self.log.info("The last subcollection number is %d" % self.latest_subcollection) # Does this correspond roughly to the run end time? If not, warn, DAQ may have crashed. end_of_run_t = (self.run_doc['end'].timestamp() - self.run_doc['start'].timestamp()) * units.s if not (0 <= end_of_run_t - self.last_pulse_time <= 60 * units.s): self.log.warning("Run is %s long according to run db, but last pulse starts at %s. " "Did the DAQ crash?" % (pax_to_human_time(end_of_run_t), pax_to_human_time(self.last_pulse_time))) # Insert some status info into the pipeline info if not self.secret_mode: if hasattr(self, 'last_time_searched'): lts = self.last_time_searched else: lts = 0 self.pipeline_status_collection.insert({'name': 'eventbuilder_info', 'time': datetime.datetime.utcnow(), 'pax_id': self.config.get('pax_id', 'no_pax_id_set'), 'last_pulse_so_far_in_run': self.last_pulse_time, 'latest_subcollection': self.latest_subcollection, 'last_time_searched': lts, 'working_on_run': True}) def get_events(self): self.log.info("Eventbuilder get_events starting up") self.refresh_run_info() self.log.info("Fetched runs db info successfully") # Last time (ns) searched, exclusive. ie we searched [something, last_time_searched) self.last_time_searched = self.initial_start_time self.log.info("self.initial_start_time: %s", pax_to_human_time(self.initial_start_time)) next_event_number = 0 more_data_coming = True while more_data_coming: # Refresh the run info, to find out if data taking has ended if not self.data_taking_ended: self.refresh_run_info() # What is the last time we need to search? if self.data_taking_ended: end_of_search_for_this_run = self.last_pulse_time + self.batch_window else: end_of_search_for_this_run = float('inf') # What is the earliest time we still need to search? next_time_to_search = self.last_time_searched if next_time_to_search != self.initial_start_time: next_time_to_search += self.batch_window * self.config['skip_ahead'] # How many batch windows can we search now? if self.data_taking_ended: batches_to_search = int((end_of_search_for_this_run - next_time_to_search) / self.batch_window) + 1 else: # Make sure we only query data that is edge_safety_margin away from the last pulse time. # This is because the readers are inserting the pulse data slightly asynchronously. # Also make sure we only query once a full batch window of such safe data is available (to avoid # mini-queries). duration_of_searchable = self.last_pulse_time - self.config['edge_safety_margin'] - next_time_to_search batches_to_search = int(duration_of_searchable / self.batch_window) if batches_to_search < 1: self.log.info("DAQ has not taken sufficient data to continue. Sleeping 5 sec...") time.sleep(5) continue batches_to_search = min(batches_to_search, self.max_query_workers // len(self.hosts)) # Start new queries in separate processes with ThreadPoolExecutor(max_workers=self.max_query_workers) as executor: futures = [] for batch_i in range(batches_to_search): futures_per_host = [] # Get the query, and collection name needed for it start = next_time_to_search + batch_i * self.batch_window if self.split_collections: subcol_i = self.subcollection_with_time(next_time_to_search) + batch_i # Prep the query -- not a very difficult one :-) query = {} collection_name = self.subcollection_name(subcol_i) self.log.info("Submitting query for subcollection %d" % subcol_i) else: collection_name = self.run_doc['name'] stop = start + self.batch_window query = self.time_range_query(start, stop) self.log.info("Submitting query for batch %d, time range [%s, %s)" % ( batch_i, pax_to_human_time(start), pax_to_human_time(stop))) # Do the query on each host for host in self.hosts: future = executor.submit(get_pulses, client_maker_config=self.cm.config, query=query, input_info=self.input_info, collection_name=collection_name, host=host, get_area=self.config['can_get_area']) futures_per_host.append(future) futures.append(futures_per_host) # Record advancement of the batch window self.last_time_searched = next_time_to_search + batches_to_search * self.batch_window # Check if there is more data more_data_coming = (not self.data_taking_ended) or (self.last_time_searched < end_of_search_for_this_run) if not more_data_coming: self.log.info("Searched to %s, which is beyond %s. This is the last batch of data" % ( pax_to_human_time(self.last_time_searched), pax_to_human_time(end_of_search_for_this_run))) # Check if we've passed the user-specified stop (if so configured) stop_after_sec = self.config.get('stop_after_sec', None) if stop_after_sec and 0 < stop_after_sec < float('inf'): if self.last_time_searched > stop_after_sec * units.s: self.log.warning("Searched to %s, which is beyond the user-specified stop at %d sec." "This is the last batch of data" % (self.last_time_searched, self.config['stop_after_sec'])) more_data_coming = False # Retrieve results from the queries, then pass everything to the trigger for i, futures_per_host in enumerate(futures): if len(futures_per_host) == 1: assert not self.split_hosts times, modules, channels, areas = futures_per_host[0].result() else: assert self.split_hosts times = [] modules = [] channels = [] areas = [] for f in futures_per_host: ts, ms, chs, ars = f.result() times.append(ts) modules.append(ms) channels.append(chs) areas.append(ars) times = np.concatenate(times) modules = np.concatenate(modules) channels = np.concatenate(channels) areas = np.concatenate(areas) times = times * self.sample_duration if len(times): self.log.info("Batch %d: acquired pulses in range [%s, %s]" % ( i, pax_to_human_time(times[0]), pax_to_human_time(times[-1]))) else: self.log.info("Batch %d: No pulse data found." % i) # Send the new data to the trigger, which will build events from it # Note the data is still unsorted: the trigger will take care of sorting it. for data in self.trigger.run(last_time_searched=next_time_to_search + (i + 1) * self.batch_window, start_times=times, channels=channels, modules=modules, areas=areas, last_data=(not more_data_coming and i == len(futures) - 1)): yield EventProxy(event_number=next_event_number, data=data, block_id=-1) next_event_number += 1 # We've built all the events for this run! # Compile the end of run info for the run doc and for display trigger_end_info = self.trigger.shutdown() trigger_end_info.update(dict(ended=True, status='deleted' if self.config['delete_data'] else 'processed', trigger_monitor_data_location=self.uri_for_monitor, mongo_reader_config={k: v for k, v in self.config.items() if k != 'password' and k not in self.processor.config['DEFAULT']})) if not self.secret_mode: end_of_run_info = {'trigger.%s' % k: v for k, v in trigger_end_info.items()} self.runs_collection.update_one({'_id': self.config['run_doc_id']}, {'$set': end_of_run_info}) self.log.info("Event building complete. Trigger information: %s" % trigger_end_info) class MongoDBReadUntriggeredFiller(plugin.TransformPlugin, MongoBase): """Read pulse data into event ranges provided by trigger MongoDBReadUntriggered. This is a separate plugin, since reading the raw pulse data is the expensive operation we want to parallelize. """ do_input_check = False def startup(self): MongoBase.startup(self) self.ignored_channels = [] self.max_pulses_per_event = self.config.get('max_pulses_per_event', float('inf')) self.high_energy_prescale = self.config.get('high_energy_prescale', 0.1) self.log.info("Software HEV settings: %s max pulses per event, %s prescale" % (self.max_pulses_per_event, self.high_energy_prescale)) # Load the digitizer channel -> PMT index mapping self.detector = self.config['detector'] self.pmts = self.config['pmts'] self.pmt_mappings = {(x['digitizer']['module'], x['digitizer']['channel']): x['pmt_position'] for x in self.pmts} def _get_cursor_between_times(self, start, stop, subcollection_number=None): """Returns count, cursor over all pulses that start in [start, stop) (both pax units since start of run). Order of pulses is not defined. count is 0 if max_pulses_per_event is float('inf'), since we don't care about it in that case. Does NOT deal with time ranges split between subcollections, but does deal with split hosts. """ cursors = [] count = 0 for host_i, host in enumerate(self.hosts): if subcollection_number is None: assert not self.split_collections collection = self.input_collections[host_i] else: assert self.split_collections collection = self.subcollection(subcollection_number, host_i) query = self.time_range_query(start, stop) cursor = collection.find(query) # Ask for a large batch size: the default is 101 documents or 1MB. This results in a very small speed # increase (when I measured it on a normal dataset) cursor.batch_size(int(1e7)) cursors.append(cursor) if self.max_pulses_per_event != float('inf'): count += collection.count(query) if len(self.hosts) == 1: return count, cursors[0] else: return count, chain(*cursors) def transform_event(self, event_proxy): # t0, t1 are the start, stop time of the event in pax units (ns) since the start of the run (t0, t1), trigger_signals = event_proxy.data self.log.debug("Fetching data for event with range [%s, %s]", pax_to_human_time(t0), pax_to_human_time(t1)) event = Event(n_channels=self.config['n_channels'], block_id=event_proxy.block_id, start_time=t0 + self.time_of_run_start, sample_duration=self.sample_duration, stop_time=t1 + self.time_of_run_start, dataset_name=self.run_doc['name'], event_number=event_proxy.event_number, trigger_signals=trigger_signals) # Convert trigger signal times to time since start of event event.trigger_signals['left_time'] -= t0 event.trigger_signals['right_time'] -= t0 event.trigger_signals['time_mean'] -= t0 if self.split_collections: start_col = self.subcollection_with_time(t0) end_col = self.subcollection_with_time(t1) if start_col == end_col: count, mongo_iterator = self._get_cursor_between_times(t0, t1, start_col) if count > self.max_pulses_per_event: # Software "veto" the event to prevent overloading the event builder if np.random.rand() > self.high_energy_prescale: self.log.debug("VETO: %d pulses in event %s" % (len(event.pulses), event.event_number)) event.n_pulses = int(count) return event else: self.log.info("Found event [%s-%s] which straddles subcollection boundary." % ( pax_to_human_time(t0), pax_to_human_time(t1))) # Ignore the software-HEV in this case mongo_iterator = chain(self._get_cursor_between_times(t0, t1, start_col)[1], self._get_cursor_between_times(t0, t1, end_col)[1]) else: mongo_iterator = self._get_cursor_between_times(t0, t1) data_is_compressed = self.input_info['compressed'] for i, pulse_doc in enumerate(mongo_iterator): digitizer_id = (pulse_doc['module'], pulse_doc['channel']) pmt = self.pmt_mappings.get(digitizer_id) if pmt is not None: # Fetch the raw data data = pulse_doc['data'] if data_is_compressed: data = snappy.decompress(data) time_within_event = self._from_mt(pulse_doc['time']) - t0 # ns event.pulses.append(Pulse(left=self._to_mt(time_within_event), raw_data=np.fromstring(data, dtype="<i2"), channel=pmt, do_it_fast=True)) elif digitizer_id not in self.ignored_channels: self.log.warning("Found data from digitizer module %d, channel %d," "which doesn't exist according to PMT mapping! Ignoring...", pulse_doc['module'], pulse_doc['channel']) self.ignored_channels.append(digitizer_id) self.log.debug("%d pulses in event %s" % (len(event.pulses), event.event_number)) return event class MongoDBClearUntriggered(plugin.TransformPlugin, MongoBase): """Clears data whose events have been built from MongoDB, rescuing acquisition monitor pulses to a separate file first. This must run as part of the output plugin group, so it gets the events in order. It does not use the events' content, but needs to know which event times have been processed. If split_collections: Drop sub collections when events from subsequent collections start arriving. Drop all remaining subcollections on shutdown. Else (single collection mode): Keeps track of which time is safe to delete, then deletes data from the collection in batches. At shutdown, drop the collection """ do_input_check = False do_output_check = False last_time_deleted = 0 last_subcollection_not_yet_deleted = 0 def startup(self): MongoBase.startup(self) self.executor = ThreadPoolExecutor(max_workers=self.config['max_query_workers']) # Should we actually delete data, or just rescue the acquisition monitor pulses? self.actually_delete = self.config.get('delete_data', False) if self.actually_delete: self.log.info("Data will be DELETED from the Mongo database after it is acquired!") else: self.log.info("Data will REMAIN in the Mongo database (until delete permission is acquired).") aqm_file_path = self.config.get('acquisition_monitor_file_path') if aqm_file_path is None: self.log.info("No acquisition monitor data file path given -- will NOT rescue acquisition monitor pulses!") self.aqm_output_handle = None elif 'sum_wv' not in self.config['channels_in_detector']: self.log.warning("Acquisition monitor path specified, " "but your detector doesn't have an acquisition monitor?") self.aqm_output_handle = None else: # Get the acquisition monitor module number from the pmts dictionary in the config # It's a bit bad we've hardcoded 'sum_wv' as detector name here... some_ch_from_aqm = self.config['channels_in_detector']['sum_wv'][0] self.aqm_module = self.config['pmts'][some_ch_from_aqm]['digitizer']['module'] self.log.info("Acquisition monitor (module %d) pulses will be saved to %s" % ( self.aqm_module, aqm_file_path)) self.aqm_output_handle = open(aqm_file_path, mode='wb') # Add some random content to make Boris and ruciax happy # (ensure a unique checksum even if there are no pulses or the DAQ crashes) self.aqm_output_handle.write(pickle.dumps("Pax rules! Random numbers of the day: %s" % np.random.randn(3))) self.already_rescued_collections = [] def transform_event(self, event_proxy): time_since_start = event_proxy.data['stop_time'] - self.time_of_run_start if self.split_collections: coll_number = self.subcollection_with_time(time_since_start) while coll_number > self.last_subcollection_not_yet_deleted: self.log.info("Seen event at subcollection %d, clearing subcollection %d" % ( coll_number, self.last_subcollection_not_yet_deleted)) self.drop_collection_named(self.subcollection_name(self.last_subcollection_not_yet_deleted), self.executor) self.last_subcollection_not_yet_deleted += 1 else: if self.input_collections is None: raise RuntimeError("Wtf??") if time_since_start > self.last_time_deleted + self.config['batch_window']: self.log.info("Seen event at %s, clearing all data until then." % pax_to_human_time(time_since_start)) for coll in self.input_collections: self.executor.submit(self.delete_pulses, coll, start_mongo_time=self._to_mt(self.last_time_deleted), stop_mongo_time=self._to_mt(time_since_start)) self.last_time_deleted = time_since_start return event_proxy def shutdown(self): # Wait for any slow drops to complete self.log.info("Waiting for slow collection drops/rescues to complete...") self.executor.shutdown() self.log.info("Collection drops/rescues should be complete. Checking for remaining collections.") pulses_in_remaining_collections = defaultdict(int) for db in self.dbs: for coll_name in db.collection_names(): if not coll_name.startswith(self.run_doc['name']): continue if coll_name in self.already_rescued_collections and not self.actually_delete: # Of course these collections are still there, don't count them as 'remaining' continue pulses_in_remaining_collections[coll_name] += db[coll_name].count() if len(pulses_in_remaining_collections): self.log.info("Leftover collections with pulse counts: %s. Clearing/rescuing these now." % ( str(pulses_in_remaining_collections))) for colname in pulses_in_remaining_collections.keys(): self.drop_collection_named(colname) self.log.info("Completed.") else: self.log.info("All collections have already been cleaned, great.") if self.actually_delete: # Update the run doc to remove the 'untriggered' entry # since we just deleted the last of the untriggered data self.refresh_run_doc() self.runs_collection.update_one({'_id': self.run_doc['_id']}, {'$set': {'data': [d for d in self.run_doc['data'] if d['type'] != 'untriggered']}}) if hasattr(self, 'aqm_output_handle') and self.aqm_output_handle is not None: self.aqm_output_handle.close() def rescue_acquisition_monitor_pulses(self, collection, query=None): """Saves all acquisition monitor pulses from collection the acquisition monitor data file. - collection: pymongo object (not collection name!) - query: optional query inside the collection (e.g. for a specific time range). The condition to select pulses from the acquistion monitor module will be added to this. """ if self.aqm_output_handle is None: return if query is None: query = {} query['module'] = self.aqm_module # Count first, in case something is badly wrong and we end up saving bazillions of docs we'll at least have # a fair warning... n_to_rescue = collection.count(query) self.log.info("Saving %d acquisition monitor pulses" % n_to_rescue) for doc in collection.find(query): self.aqm_output_handle.write(pickle.dumps(doc)) # Flush explicitly: we want to save the data even if the event builder crashes before properly closing the file self.aqm_output_handle.flush() def delete_pulses(self, collection, start_mongo_time, stop_mongo_time): """Deletes all pulses in collection between start_mongo_time (inclusive) and stop_mongo_time (exclusive), both in mongo time units (not pax units!). Rescues acquisition monitor pulses just before deleting. """ query = {'time': {'$gte': start_mongo_time, '$lt': stop_mongo_time}} self.rescue_acquisition_monitor_pulses(collection, query) if self.actually_delete: collection.delete_many(query) def drop_collection_named(self, collection_name, executor=None): """Drop the collection named collection_name from db, rescueing acquisition monitor pulses first. if executor is passed, will execute the drop command via the pool it represents. This function is NOT parallelizable itself, don't pass it to an executor! We need to block while rescuing acquisition monitor pulses: otherwise, we would get to the final cleanup in shutdown() while there are still collections being rescued. """ if self.aqm_db is not None: if collection_name not in self.already_rescued_collections: self.already_rescued_collections.append(collection_name) self.rescue_acquisition_monitor_pulses(self.aqm_db[collection_name]) else: self.log.warning("Duplicated call to rescue/delete collection %s!" % collection_name) if not self.actually_delete: return for db in self.dbs: if executor is None: db.drop_collection(collection_name) else: executor.submit(db.drop_collection, collection_name) def pax_to_human_time(num): """Converts a pax time to a human-readable representation""" for x in ['ns', 'us', 'ms', 's', 'ks', 'Ms', 'G', 'T']: if num < 1000.0: return "%3.3f %s" % (num, x) num /= 1000.0 return "%3.1f %s" % (num, 's') def get_pulses(client_maker_config, input_info, collection_name, query, host, get_area=False): """Find pulse times according to query using monary. Returns four numpy arrays: times, modules, channels, areas. Areas consists of zeros unless get_area = True, in which we also fetch the 'integral' field. The monary client is created inside this function, so we could run it with ProcessPoolExecutor. """ fields = ['time', 'module', 'channel'] + (['integral'] if get_area else []) types = ['int64', 'int32', 'int32'] + (['area'] if get_area else []) try: client_maker = ClientMaker(client_maker_config) monary_client = client_maker.get_client(database_name=input_info['database'], uri=input_info['location'], host=host, monary=True) # Somehow monary's block query fails when we have multiple blocks, # we need to take care of copying out the data ourselves, but even if I use .copy it doesn't seem to work # Never mind, just make a big block results = list(monary_client.block_query(input_info['database'], collection_name, query, fields, types, block_size=int(5e8), select_fields=True)) monary_client.close() except monary.monary.MonaryError as e: if 'Failed to resolve' in str(e): raise exceptions.DatabaseConnectivityError("Caught error trying to connect to untriggered database. " "Original exception: %s." % str(e)) raise e if not len(results) or not len(results[0]): times = np.zeros(0, dtype=np.int64) modules = np.zeros(0, dtype=np.int32) channels = np.zeros(0, dtype=np.int32) areas = np.zeros(0, dtype=np.float64) else: # Concatenate results from multiple blocks, in case multiple blocks were needed results = [np.concatenate([results[i][j] for i in range(len(results))]) for j in range(len(results[0]))] if get_area: times, modules, channels, areas = results else: times, modules, channels = results areas = np.zeros(len(times), dtype=np.float64) return times, modules, channels, areas
bsd-3-clause
-4,955,728,548,880,822,000
50.881418
120
0.567355
false
krstnschwpwr/speedcontrol
speed_ctrl/settings.py
1
2616
import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'jfwya!*^^@unv%s$-#-#us9x6z%1ym!uvspde2zu#unrp&(gos' if 'SPEEDTRACKER_ENV' in os.environ and os.environ['SPEEDTRACKER_ENV'] == 'production': DEBUG = False ALLOWED_HOSTS = ['*'] else: DEBUG = True ALLOWED_HOSTS = [] INSTALLED_APPS = [ 'probe.apps.ProbeConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'speed_ctrl.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'probe/templates')] , 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'speed_ctrl.wsgi.application' # Database # https://docs.djangoproject.com/en/1.10/ref/settings/#databasespip DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'speed.sqlite'), } } REST_FRAMEWORK = { 'DEFAULT_RENDERER_CLASSES': ( 'probe.renderers.PrtgRenderer', 'rest_framework.renderers.JSONRenderer', ) } AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] LANGUAGE_CODE = 'en-gb' TIME_ZONE = 'Europe/Berlin' USE_I18N = True USE_L10N = True USE_TZ = True STATIC_URL = '/static/'
mit
-2,287,553,738,804,210,000
25.424242
91
0.652905
false
rackspace/pyrax
tests/unit/test_autoscale.py
1
69694
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import, unicode_literals import random import unittest from mock import patch from mock import MagicMock as Mock import pyrax import pyrax.autoscale from pyrax.autoscale import AutoScaleClient from pyrax.autoscale import AutoScalePolicy from pyrax.autoscale import AutoScaleWebhook from pyrax.autoscale import ScalingGroup from pyrax.autoscale import ScalingGroupManager import pyrax.exceptions as exc import pyrax.utils as utils from pyrax import fakes class AutoscaleTest(unittest.TestCase): def __init__(self, *args, **kwargs): super(AutoscaleTest, self).__init__(*args, **kwargs) def setUp(self): self.identity = fakes.FakeIdentity() self.scaling_group = fakes.FakeScalingGroup(self.identity) def tearDown(self): pass def test_make_policies(self): sg = self.scaling_group p1 = utils.random_unicode() p2 = utils.random_unicode() sg.scalingPolicies = [{"name": p1}, {"name": p2}] sg._make_policies() self.assertEqual(len(sg.policies), 2) polnames = [pol.name for pol in sg.policies] self.assert_(p1 in polnames) self.assert_(p2 in polnames) def test_get_state(self): sg = self.scaling_group mgr = sg.manager mgr.get_state = Mock() sg.get_state() mgr.get_state.assert_called_once_with(sg) def test_pause(self): sg = self.scaling_group mgr = sg.manager mgr.pause = Mock() sg.pause() mgr.pause.assert_called_once_with(sg) def test_resume(self): sg = self.scaling_group mgr = sg.manager mgr.resume = Mock() sg.resume() mgr.resume.assert_called_once_with(sg) def test_update(self): sg = self.scaling_group mgr = sg.manager mgr.update = Mock() name = utils.random_unicode() cooldown = utils.random_unicode() min_entities = utils.random_unicode() max_entities = utils.random_unicode() metadata = utils.random_unicode() sg.update(name=name, cooldown=cooldown, min_entities=min_entities, max_entities=max_entities, metadata=metadata) mgr.update.assert_called_once_with(sg, name=name, cooldown=cooldown, min_entities=min_entities, max_entities=max_entities, metadata=metadata) def test_update_metadata(self): sg = self.scaling_group mgr = sg.manager mgr.update_metadata = Mock() metadata = utils.random_unicode() sg.update_metadata(metadata) mgr.update_metadata.assert_called_once_with(sg, metadata=metadata) def test_get_configuration(self): sg = self.scaling_group mgr = sg.manager mgr.get_configuration = Mock() sg.get_configuration() mgr.get_configuration.assert_called_once_with(sg) def test_get_launch_config(self): sg = self.scaling_group mgr = sg.manager mgr.get_launch_config = Mock() sg.get_launch_config() mgr.get_launch_config.assert_called_once_with(sg) def test_update_launch_config(self): sg = self.scaling_group mgr = sg.manager mgr.update_launch_config = Mock() server_name = utils.random_unicode() flavor = utils.random_unicode() image = utils.random_unicode() disk_config = utils.random_unicode() metadata = utils.random_unicode() personality = utils.random_unicode().encode("utf-8") # Must be bytes networks = utils.random_unicode() load_balancers = utils.random_unicode() key_name = utils.random_unicode() config_drive = utils.random_unicode() user_data = utils.random_unicode() sg.update_launch_config(server_name=server_name, flavor=flavor, image=image, disk_config=disk_config, metadata=metadata, personality=personality, networks=networks, load_balancers=load_balancers, key_name=key_name, config_drive=config_drive, user_data=user_data) mgr.update_launch_config.assert_called_once_with(sg, server_name=server_name, flavor=flavor, image=image, disk_config=disk_config, metadata=metadata, personality=personality, networks=networks, load_balancers=load_balancers, key_name=key_name, config_drive=config_drive, user_data=user_data) def test_update_launch_metadata(self): sg = self.scaling_group mgr = sg.manager mgr.update_launch_metadata = Mock() metadata = utils.random_unicode() sg.update_launch_metadata(metadata) mgr.update_launch_metadata.assert_called_once_with(sg, metadata) def test_add_policy(self): sg = self.scaling_group mgr = sg.manager name = utils.random_unicode() policy_type = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() is_percent = utils.random_unicode() desired_capacity = utils.random_unicode() args = utils.random_unicode() mgr.add_policy = Mock() sg.add_policy(name, policy_type, cooldown, change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) mgr.add_policy.assert_called_once_with(sg, name, policy_type, cooldown, change=change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) def test_list_policies(self): sg = self.scaling_group mgr = sg.manager mgr.list_policies = Mock() sg.list_policies() mgr.list_policies.assert_called_once_with(sg) def test_get_policy(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() mgr.get_policy = Mock() sg.get_policy(pol) mgr.get_policy.assert_called_once_with(sg, pol) def test_update_policy(self): sg = self.scaling_group mgr = sg.manager policy = utils.random_unicode() name = utils.random_unicode() policy_type = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() desired_capacity = utils.random_unicode() is_percent = utils.random_unicode() args = utils.random_unicode() mgr.update_policy = Mock() sg.update_policy(policy, name=name, policy_type=policy_type, cooldown=cooldown, change=change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) mgr.update_policy.assert_called_once_with(scaling_group=sg, policy=policy, name=name, policy_type=policy_type, cooldown=cooldown, change=change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) def test_execute_policy(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() mgr.execute_policy = Mock() sg.execute_policy(pol) mgr.execute_policy.assert_called_once_with(scaling_group=sg, policy=pol) def test_delete_policy(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() mgr.delete_policy = Mock() sg.delete_policy(pol) mgr.delete_policy.assert_called_once_with(scaling_group=sg, policy=pol) def test_add_webhook(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() name = utils.random_unicode() metadata = utils.random_unicode() mgr.add_webhook = Mock() sg.add_webhook(pol, name, metadata=metadata) mgr.add_webhook.assert_called_once_with(sg, pol, name, metadata=metadata) def test_list_webhooks(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() mgr.list_webhooks = Mock() sg.list_webhooks(pol) mgr.list_webhooks.assert_called_once_with(sg, pol) def test_update_webhook(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() hook = utils.random_unicode() name = utils.random_unicode() metadata = utils.random_unicode() mgr.update_webhook = Mock() sg.update_webhook(pol, hook, name=name, metadata=metadata) mgr.update_webhook.assert_called_once_with(scaling_group=sg, policy=pol, webhook=hook, name=name, metadata=metadata) def test_update_webhook_metadata(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() hook = utils.random_unicode() metadata = utils.random_unicode() mgr.update_webhook_metadata = Mock() sg.update_webhook_metadata(pol, hook, metadata=metadata) mgr.update_webhook_metadata.assert_called_once_with(sg, pol, hook, metadata) def test_delete_webhook(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() hook = utils.random_unicode() mgr.delete_webhook = Mock() sg.delete_webhook(pol, hook) mgr.delete_webhook.assert_called_once_with(sg, pol, hook) def test_policy_count(self): sg = self.scaling_group num = random.randint(1, 100) sg.policies = ["x"] * num self.assertEqual(sg.policy_count, num) def test_name(self): sg = self.scaling_group name = utils.random_unicode() newname = utils.random_unicode() sg.groupConfiguration = {"name": name} self.assertEqual(sg.name, name) sg.name = newname self.assertEqual(sg.name, newname) def test_cooldown(self): sg = self.scaling_group cooldown = utils.random_unicode() newcooldown = utils.random_unicode() sg.groupConfiguration = {"cooldown": cooldown} self.assertEqual(sg.cooldown, cooldown) sg.cooldown = newcooldown self.assertEqual(sg.cooldown, newcooldown) def test_metadata(self): sg = self.scaling_group metadata = utils.random_unicode() newmetadata = utils.random_unicode() sg.groupConfiguration = {"metadata": metadata} self.assertEqual(sg.metadata, metadata) sg.metadata = newmetadata self.assertEqual(sg.metadata, newmetadata) def test_min_entities(self): sg = self.scaling_group min_entities = utils.random_unicode() newmin_entities = utils.random_unicode() sg.groupConfiguration = {"minEntities": min_entities} self.assertEqual(sg.min_entities, min_entities) sg.min_entities = newmin_entities self.assertEqual(sg.min_entities, newmin_entities) def test_max_entities(self): sg = self.scaling_group max_entities = utils.random_unicode() newmax_entities = utils.random_unicode() sg.groupConfiguration = {"maxEntities": max_entities} self.assertEqual(sg.max_entities, max_entities) sg.max_entities = newmax_entities self.assertEqual(sg.max_entities, newmax_entities) def test_mgr_get_state(self): sg = self.scaling_group mgr = sg.manager id1 = utils.random_unicode() id2 = utils.random_unicode() ac = utils.random_unicode() dc = utils.random_unicode() pc = utils.random_unicode() paused = utils.random_unicode() statedict = {"group": { "active": [{"id": id1}, {"id": id2}], "activeCapacity": ac, "desiredCapacity": dc, "pendingCapacity": pc, "paused": paused, }} expected = { "active": [id1, id2], "active_capacity": ac, "desired_capacity": dc, "pending_capacity": pc, "paused": paused, } mgr.api.method_get = Mock(return_value=(None, statedict)) ret = mgr.get_state(sg) self.assertEqual(ret, expected) def test_mgr_pause(self): sg = self.scaling_group mgr = sg.manager uri = "/%s/%s/pause" % (mgr.uri_base, sg.id) mgr.api.method_post = Mock(return_value=(None, None)) mgr.pause(sg) mgr.api.method_post.assert_called_once_with(uri) def test_mgr_resume(self): sg = self.scaling_group mgr = sg.manager uri = "/%s/%s/resume" % (mgr.uri_base, sg.id) mgr.api.method_post = Mock(return_value=(None, None)) mgr.resume(sg) mgr.api.method_post.assert_called_once_with(uri) def test_mgr_get_configuration(self): sg = self.scaling_group mgr = sg.manager uri = "/%s/%s/config" % (mgr.uri_base, sg.id) conf = utils.random_unicode() resp_body = {"groupConfiguration": conf} mgr.api.method_get = Mock(return_value=(None, resp_body)) ret = mgr.get_configuration(sg) mgr.api.method_get.assert_called_once_with(uri) self.assertEqual(ret, conf) def test_mgr_update(self): sg = self.scaling_group mgr = sg.manager mgr.get = Mock(return_value=sg) uri = "/%s/%s/config" % (mgr.uri_base, sg.id) sg.name = utils.random_unicode() sg.cooldown = utils.random_unicode() sg.min_entities = utils.random_unicode() sg.max_entities = utils.random_unicode() metadata = utils.random_unicode() mgr.api.method_put = Mock(return_value=(None, None)) expected_body = {"name": sg.name, "cooldown": sg.cooldown, "minEntities": sg.min_entities, "maxEntities": sg.max_entities, "metadata": metadata, } mgr.update(sg.id, metadata=metadata) mgr.api.method_put.assert_called_once_with(uri, body=expected_body) def test_mgr_replace(self): sg = self.scaling_group mgr = sg.manager mgr.get = Mock(return_value=sg) uri = "/%s/%s/config" % (mgr.uri_base, sg.id) sg.name = utils.random_unicode() sg.cooldown = utils.random_unicode() sg.min_entities = utils.random_unicode() sg.max_entities = utils.random_unicode() metadata = utils.random_unicode() new_name = utils.random_unicode() new_cooldown = utils.random_unicode() new_min = utils.random_unicode() new_max = utils.random_unicode() mgr.api.method_put = Mock(return_value=(None, None)) expected_body = { "name": new_name, "cooldown": new_cooldown, "minEntities": new_min, "maxEntities": new_max, "metadata": {} } mgr.replace(sg.id, new_name, new_cooldown, new_min, new_max) mgr.api.method_put.assert_called_once_with(uri, body=expected_body) def test_mgr_update_metadata(self): sg = self.scaling_group mgr = sg.manager mgr.get = Mock(return_value=sg) sg.metadata = {"orig": "orig"} metadata = {"new": "new"} expected = sg.metadata.copy() expected.update(metadata) mgr.update = Mock() mgr.update_metadata(sg.id, metadata) mgr.update.assert_called_once_with(sg, metadata=expected) def test_mgr_get_launch_config(self): sg = self.scaling_group mgr = sg.manager typ = utils.random_unicode() lbs = utils.random_unicode() name = utils.random_unicode() flv = utils.random_unicode() img = utils.random_unicode() dconfig = utils.random_unicode() metadata = utils.random_unicode() personality = utils.random_unicode() networks = utils.random_unicode() key_name = utils.random_unicode() launchdict = {"launchConfiguration": {"type": typ, "args": { "loadBalancers": lbs, "server": { "name": name, "flavorRef": flv, "imageRef": img, "OS-DCF:diskConfig": dconfig, "metadata": metadata, "personality": personality, "networks": networks, "key_name": key_name, }, }, }, } expected = { "type": typ, "load_balancers": lbs, "name": name, "flavor": flv, "image": img, "disk_config": dconfig, "metadata": metadata, "personality": personality, "networks": networks, "key_name": key_name, } mgr.api.method_get = Mock(return_value=(None, launchdict)) uri = "/%s/%s/launch" % (mgr.uri_base, sg.id) ret = mgr.get_launch_config(sg) mgr.api.method_get.assert_called_once_with(uri) self.assertEqual(ret, expected) def test_mgr_update_launch_config(self): sg = self.scaling_group mgr = sg.manager mgr.get = Mock(return_value=sg) typ = utils.random_unicode() lbs = utils.random_unicode() name = utils.random_unicode() flv = utils.random_unicode() img = utils.random_unicode() dconfig = utils.random_unicode() metadata = utils.random_unicode() personality = utils.random_unicode() networks = utils.random_unicode() sg.launchConfiguration = {} body = {"type": "launch_server", "args": { "server": { "name": name, "imageRef": img, "flavorRef": flv, "OS-DCF:diskConfig": dconfig, "personality": mgr._encode_personality(personality), "networks": networks, "metadata": metadata, }, "loadBalancers": lbs, }, } mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/launch" % (mgr.uri_base, sg.id) mgr.update_launch_config(sg.id, server_name=name, flavor=flv, image=img, disk_config=dconfig, metadata=metadata, personality=personality, networks=networks, load_balancers=lbs) mgr.api.method_put.assert_called_once_with(uri, body=body) def test_mgr_update_launch_config_unset_personality(self): sg = self.scaling_group mgr = sg.manager mgr.get = Mock(return_value=sg) typ = utils.random_unicode() lbs = utils.random_unicode() name = utils.random_unicode() flv = utils.random_unicode() img = utils.random_unicode() dconfig = utils.random_unicode() metadata = utils.random_unicode() personality = [{ "path": "/foo/bar", "contents": "cHlyYXg=" }] networks = utils.random_unicode() sg.launchConfiguration = { "type": "launch_server", "args": { "server": { "name": name, "imageRef": img, "flavorRef": flv, "OS-DCF:diskConfig": dconfig, "personality": personality, "networks": networks, "metadata": metadata, }, "loadBalancers": lbs, }, } body = { "type": "launch_server", "args": { "server": { "name": name, "imageRef": img, "flavorRef": flv, "OS-DCF:diskConfig": dconfig, "networks": networks, "metadata": metadata, }, "loadBalancers": lbs, }, } mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/launch" % (mgr.uri_base, sg.id) mgr.update_launch_config(sg.id, server_name=name, flavor=flv, image=img, disk_config=dconfig, metadata=metadata, personality=[], networks=networks, load_balancers=lbs) mgr.api.method_put.assert_called_once_with(uri, body=body) def test_mgr_update_launch_config_no_personality(self): sg = self.scaling_group mgr = sg.manager mgr.get = Mock(return_value=sg) typ = utils.random_unicode() lbs = utils.random_unicode() name = utils.random_unicode() flv = utils.random_unicode() img = utils.random_unicode() dconfig = utils.random_unicode() metadata = utils.random_unicode() networks = utils.random_unicode() sg.launchConfiguration = {} body = {"type": "launch_server", "args": { "server": { "name": name, "imageRef": img, "flavorRef": flv, "OS-DCF:diskConfig": dconfig, "networks": networks, "metadata": metadata, }, "loadBalancers": lbs, }, } mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/launch" % (mgr.uri_base, sg.id) mgr.update_launch_config(sg.id, server_name=name, flavor=flv, image=img, disk_config=dconfig, metadata=metadata, networks=networks, load_balancers=lbs) mgr.api.method_put.assert_called_once_with(uri, body=body) def test_mgr_update_launch_config_no_metadata(self): sg = self.scaling_group mgr = sg.manager mgr.get = Mock(return_value=sg) typ = utils.random_unicode() lbs = utils.random_unicode() name = utils.random_unicode() flv = utils.random_unicode() img = utils.random_unicode() dconfig = utils.random_unicode() networks = utils.random_unicode() sg.launchConfiguration = {} body = {"type": "launch_server", "args": { "server": { "name": name, "imageRef": img, "flavorRef": flv, "OS-DCF:diskConfig": dconfig, "networks": networks, }, "loadBalancers": lbs, }, } mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/launch" % (mgr.uri_base, sg.id) mgr.update_launch_config(sg.id, server_name=name, flavor=flv, image=img, disk_config=dconfig, networks=networks, load_balancers=lbs) mgr.api.method_put.assert_called_once_with(uri, body=body) def test_mgr_update_launch_config_key_name(self): sg = self.scaling_group mgr = sg.manager mgr.get = Mock(return_value=sg) typ = utils.random_unicode() lbs = utils.random_unicode() name = utils.random_unicode() flv = utils.random_unicode() img = utils.random_unicode() dconfig = utils.random_unicode() metadata = utils.random_unicode() personality = utils.random_unicode() networks = utils.random_unicode() key_name = utils.random_unicode() sg.launchConfiguration = {} body = {"type": "launch_server", "args": { "server": { "name": name, "imageRef": img, "flavorRef": flv, "OS-DCF:diskConfig": dconfig, "networks": networks, "metadata": metadata, "key_name": key_name, "personality": mgr._encode_personality(personality), }, "loadBalancers": lbs, }, } mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/launch" % (mgr.uri_base, sg.id) mgr.update_launch_config(sg.id, server_name=name, flavor=flv, image=img, disk_config=dconfig, metadata=metadata, personality=personality, networks=networks, load_balancers=lbs, key_name=key_name) mgr.api.method_put.assert_called_once_with(uri, body=body) def test_mgr_replace_launch_config(self): sg = self.scaling_group mgr = sg.manager mgr.get = Mock(return_value=sg) typ = utils.random_unicode() lbs = utils.random_unicode() name = utils.random_unicode() flv = utils.random_unicode() img = utils.random_unicode() dconfig = utils.random_unicode() metadata = utils.random_unicode() personality = utils.random_unicode() networks = utils.random_unicode() sg.launchConfiguration = { "type": typ, "args": { "server": { "name": name, "imageRef": img, "flavorRef": flv, "OS-DCF:diskConfig": dconfig, "personality": personality, "networks": networks, "metadata": metadata, }, "loadBalancers": lbs, }, } new_typ = utils.random_unicode() new_name = utils.random_unicode() new_flv = utils.random_unicode() new_img = utils.random_unicode() expected = { "type": new_typ, "args": { "server": { "name": new_name, "imageRef": new_img, "flavorRef": new_flv, }, "loadBalancers": [] } } mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/launch" % (mgr.uri_base, sg.id) mgr.replace_launch_config(sg.id, launch_config_type=new_typ, server_name=new_name, flavor=new_flv, image=new_img) mgr.api.method_put.assert_called_once_with(uri, body=expected) def test_mgr_update_launch_metadata(self): sg = self.scaling_group mgr = sg.manager mgr.get = Mock(return_value=sg) orig_meta = {"orig": "orig"} new_meta = {"new": "new"} sg.launchConfiguration = {"args": {"server": {"metadata": orig_meta}}} expected = orig_meta.copy() expected.update(new_meta) mgr.update_launch_config = Mock() mgr.update_launch_metadata(sg.id, new_meta) mgr.update_launch_config.assert_called_once_with(sg, metadata=expected) def test_mgr_add_policy(self): sg = self.scaling_group mgr = sg.manager ret_body = {"policies": [{}]} mgr.api.method_post = Mock(return_value=(None, ret_body)) uri = "/%s/%s/policies" % (mgr.uri_base, sg.id) name = utils.random_unicode() ptype = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() for is_percent in (True, False): post_body = {"name": name, "cooldown": cooldown, "type": ptype} if is_percent: post_body["changePercent"] = change else: post_body["change"] = change ret = mgr.add_policy(sg, name, ptype, cooldown, change, is_percent=is_percent) mgr.api.method_post.assert_called_with(uri, body=[post_body]) self.assert_(isinstance(ret, AutoScalePolicy)) def test_mgr_create_policy_body(self): sg = self.scaling_group mgr = sg.manager name = utils.random_unicode() ptype = utils.random_unicode() cooldown = utils.random_unicode() desired_capacity = utils.random_unicode() args = utils.random_unicode() change = utils.random_unicode() expected_pct = {"name": name, "cooldown": cooldown, "type": ptype, "desiredCapacity": desired_capacity, "args": args } expected_nopct = expected_pct.copy() expected_pct["changePercent"] = change expected_nopct["change"] = change ret_pct = mgr._create_policy_body(name, ptype, cooldown, change=change, is_percent=True, desired_capacity=desired_capacity, args=args) ret_nopct = mgr._create_policy_body(name, ptype, cooldown, change=change, is_percent=False, desired_capacity=desired_capacity, args=args) self.assertEqual(ret_nopct, expected_nopct) self.assertEqual(ret_pct, expected_pct) def test_mgr_add_policy_desired_capacity(self): sg = self.scaling_group mgr = sg.manager ret_body = {"policies": [{}]} mgr.api.method_post = Mock(return_value=(None, ret_body)) uri = "/%s/%s/policies" % (mgr.uri_base, sg.id) name = utils.random_unicode() ptype = utils.random_unicode() cooldown = utils.random_unicode() desired_capacity = utils.random_unicode() post_body = { "name": name, "cooldown": cooldown, "type": ptype, "desiredCapacity": desired_capacity, } ret = mgr.add_policy(sg, name, ptype, cooldown, desired_capacity=desired_capacity) mgr.api.method_post.assert_called_with(uri, body=[post_body]) self.assert_(isinstance(ret, AutoScalePolicy)) def test_mgr_list_policies(self): sg = self.scaling_group mgr = sg.manager ret_body = {"policies": [{}]} mgr.api.method_get = Mock(return_value=(None, ret_body)) uri = "/%s/%s/policies" % (mgr.uri_base, sg.id) ret = mgr.list_policies(sg) mgr.api.method_get.assert_called_once_with(uri) def test_mgr_get_policy(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() ret_body = {"policy": {}} uri = "/%s/%s/policies/%s" % (mgr.uri_base, sg.id, pol) mgr.api.method_get = Mock(return_value=(None, ret_body)) ret = mgr.get_policy(sg, pol) self.assert_(isinstance(ret, AutoScalePolicy)) mgr.api.method_get.assert_called_once_with(uri) def test_mgr_replace_policy(self): sg = self.scaling_group mgr = sg.manager pol_id = utils.random_unicode() info = { "name": utils.random_unicode(), "type": utils.random_unicode(), "cooldown": utils.random_unicode(), "change": utils.random_unicode(), "args": utils.random_unicode(), } policy = fakes.FakeAutoScalePolicy(mgr, info, sg) mgr.get_policy = Mock(return_value=policy) new_name = utils.random_unicode() new_type = utils.random_unicode() new_cooldown = utils.random_unicode() new_change_percent = utils.random_unicode() mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/policies/%s" % (mgr.uri_base, sg.id, pol_id) expected = { "name": new_name, "type": new_type, "cooldown": new_cooldown, "changePercent": new_change_percent, } ret = mgr.replace_policy(sg, pol_id, name=new_name, policy_type=new_type, cooldown=new_cooldown, change=new_change_percent, is_percent=True) mgr.api.method_put.assert_called_with(uri, body=expected) def test_mgr_update_policy(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() name = utils.random_unicode() ptype = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() args = utils.random_unicode() mgr.get_policy = Mock(return_value=fakes.FakeAutoScalePolicy(mgr, {}, sg)) mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/policies/%s" % (mgr.uri_base, sg.id, pol) for is_percent in (True, False): put_body = {"name": name, "cooldown": cooldown, "type": ptype, "args": args} if is_percent: put_body["changePercent"] = change else: put_body["change"] = change ret = mgr.update_policy(sg, pol, name=name, policy_type=ptype, cooldown=cooldown, change=change, is_percent=is_percent, args=args) mgr.api.method_put.assert_called_with(uri, body=put_body) def test_mgr_update_policy_desired_to_desired(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() name = utils.random_unicode() ptype = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() args = utils.random_unicode() new_desired_capacity = 10 old_info = {"desiredCapacity": 0} mgr.get_policy = Mock( return_value=fakes.FakeAutoScalePolicy(mgr, old_info, sg)) mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/policies/%s" % (mgr.uri_base, sg.id, pol) put_body = {"name": name, "cooldown": cooldown, "type": ptype, "desiredCapacity": new_desired_capacity} ret = mgr.update_policy(sg, pol, name=name, policy_type=ptype, cooldown=cooldown, desired_capacity=new_desired_capacity) mgr.api.method_put.assert_called_with(uri, body=put_body) def test_mgr_update_policy_change_to_desired(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() name = utils.random_unicode() ptype = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() args = utils.random_unicode() new_desired_capacity = 10 old_info = {"change": -1} mgr.get_policy = Mock( return_value=fakes.FakeAutoScalePolicy(mgr, old_info, sg)) mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/policies/%s" % (mgr.uri_base, sg.id, pol) put_body = {"name": name, "cooldown": cooldown, "type": ptype, "desiredCapacity": new_desired_capacity} ret = mgr.update_policy(sg, pol, name=name, policy_type=ptype, cooldown=cooldown, desired_capacity=new_desired_capacity) mgr.api.method_put.assert_called_with(uri, body=put_body) def test_mgr_update_policy_desired_to_change(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() name = utils.random_unicode() ptype = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() args = utils.random_unicode() new_change = 1 old_info = {"desiredCapacity": 0} mgr.get_policy = Mock( return_value=fakes.FakeAutoScalePolicy(mgr, old_info, sg)) mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/policies/%s" % (mgr.uri_base, sg.id, pol) put_body = {"name": name, "cooldown": cooldown, "type": ptype, "change": new_change} ret = mgr.update_policy(sg, pol, name=name, policy_type=ptype, cooldown=cooldown, change=new_change) mgr.api.method_put.assert_called_with(uri, body=put_body) def test_mgr_update_policy_maintain_desired_capacity(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() name = utils.random_unicode() ptype = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() args = utils.random_unicode() new_name = utils.random_unicode() old_capacity = 0 old_info = { "type": ptype, "desiredCapacity": old_capacity, "cooldown": cooldown, } mgr.get_policy = Mock( return_value=fakes.FakeAutoScalePolicy(mgr, old_info, sg)) mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/policies/%s" % (mgr.uri_base, sg.id, pol) put_body = {"name": new_name, "cooldown": cooldown, "type": ptype, "desiredCapacity": old_capacity} ret = mgr.update_policy(sg, pol, name=new_name) mgr.api.method_put.assert_called_with(uri, body=put_body) def test_mgr_update_policy_maintain_is_percent(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() name = utils.random_unicode() ptype = utils.random_unicode() cooldown = utils.random_unicode() new_name = utils.random_unicode() old_percent = 10 old_info = { "type": ptype, "changePercent": old_percent, "cooldown": cooldown, } mgr.get_policy = Mock( return_value=fakes.FakeAutoScalePolicy(mgr, old_info, sg)) mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/policies/%s" % (mgr.uri_base, sg.id, pol) put_body = {"name": new_name, "cooldown": cooldown, "type": ptype, "changePercent": old_percent} ret = mgr.update_policy(sg, pol, name=new_name) mgr.api.method_put.assert_called_with(uri, body=put_body) def test_mgr_update_policy_maintain_is_absolute(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() name = utils.random_unicode() ptype = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() new_name = utils.random_unicode() old_change = 10 old_info = { "type": ptype, "change": old_change, "cooldown": cooldown, } mgr.get_policy = Mock( return_value=fakes.FakeAutoScalePolicy(mgr, old_info, sg)) mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/policies/%s" % (mgr.uri_base, sg.id, pol) put_body = {"name": new_name, "cooldown": cooldown, "type": ptype, "change": old_change} ret = mgr.update_policy(sg, pol, name=new_name) mgr.api.method_put.assert_called_with(uri, body=put_body) def test_mgr_execute_policy(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() uri = "/%s/%s/policies/%s/execute" % (mgr.uri_base, sg.id, pol) mgr.api.method_post = Mock(return_value=(None, None)) mgr.execute_policy(sg, pol) mgr.api.method_post.assert_called_once_with(uri) def test_mgr_delete_policy(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() uri = "/%s/%s/policies/%s" % (mgr.uri_base, sg.id, pol) mgr.api.method_delete = Mock(return_value=(None, None)) mgr.delete_policy(sg, pol) mgr.api.method_delete.assert_called_once_with(uri) def test_mgr_add_webhook(self): sg = self.scaling_group mgr = sg.manager pol = utils.random_unicode() ret_body = {"webhooks": [{}]} mgr.api.method_post = Mock(return_value=(None, ret_body)) uri = "/%s/%s/policies/%s/webhooks" % (mgr.uri_base, sg.id, pol) mgr.get_policy = Mock(return_value=fakes.FakeAutoScalePolicy(mgr, {}, sg)) name = utils.random_unicode() metadata = utils.random_unicode() post_body = {"name": name, "metadata": metadata} ret = mgr.add_webhook(sg, pol, name, metadata=metadata) mgr.api.method_post.assert_called_with(uri, body=[post_body]) self.assert_(isinstance(ret, AutoScaleWebhook)) def test_mgr_list_webhooks(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) ret_body = {"webhooks": [{}]} mgr.api.method_get = Mock(return_value=(None, ret_body)) mgr.get_policy = Mock(return_value=fakes.FakeAutoScalePolicy(mgr, {}, sg)) uri = "/%s/%s/policies/%s/webhooks" % (mgr.uri_base, sg.id, pol.id) ret = mgr.list_webhooks(sg, pol) mgr.api.method_get.assert_called_once_with(uri) def test_mgr_get_webhook(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = utils.random_unicode() ret_body = {"webhook": {}} uri = "/%s/%s/policies/%s/webhooks/%s" % (mgr.uri_base, sg.id, pol.id, hook) mgr.api.method_get = Mock(return_value=(None, ret_body)) ret = mgr.get_webhook(sg, pol, hook) self.assert_(isinstance(ret, AutoScaleWebhook)) mgr.api.method_get.assert_called_once_with(uri) def test_mgr_replace_webhook(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = utils.random_unicode() info = {"name": utils.random_unicode(), "metadata": utils.random_unicode()} hook_obj = fakes.FakeAutoScaleWebhook(mgr, info, pol, sg) new_name = utils.random_unicode() new_metadata = utils.random_unicode() mgr.get_webhook = Mock(return_value=hook_obj) mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/policies/%s/webhooks/%s" % (mgr.uri_base, sg.id, pol.id, hook) expected = {"name": new_name, "metadata": {}} ret = mgr.replace_webhook(sg, pol, hook, name=new_name) mgr.api.method_put.assert_called_with(uri, body=expected) def test_mgr_update_webhook(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = utils.random_unicode() hook_obj = fakes.FakeAutoScaleWebhook(mgr, {}, pol, sg) name = utils.random_unicode() metadata = utils.random_unicode() mgr.get_webhook = Mock(return_value=hook_obj) mgr.api.method_put = Mock(return_value=(None, None)) uri = "/%s/%s/policies/%s/webhooks/%s" % (mgr.uri_base, sg.id, pol.id, hook) put_body = {"name": name, "metadata": metadata} ret = mgr.update_webhook(sg, pol, hook, name=name, metadata=metadata) mgr.api.method_put.assert_called_with(uri, body=put_body) def test_mgr_update_webhook_metadata(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = utils.random_unicode() hook_obj = fakes.FakeAutoScaleWebhook(mgr, {}, pol, sg) hook_obj.metadata = {"orig": "orig"} metadata = {"new": "new"} expected = hook_obj.metadata.copy() expected.update(metadata) uri = "/%s/%s/policies/%s/webhooks/%s" % (mgr.uri_base, sg.id, pol.id, hook) mgr.update_webhook = Mock() mgr.get_webhook = Mock(return_value=hook_obj) mgr.update_webhook_metadata(sg, pol, hook, metadata) mgr.update_webhook.assert_called_once_with(sg, pol, hook_obj, metadata=expected) def test_mgr_delete_webhook(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = utils.random_unicode() hook_obj = fakes.FakeAutoScaleWebhook(mgr, {}, pol, sg) uri = "/%s/%s/policies/%s/webhooks/%s" % (mgr.uri_base, sg.id, pol.id, hook) mgr.api.method_delete = Mock(return_value=(None, None)) mgr.get_webhook = Mock(return_value=hook_obj) mgr.delete_webhook(sg, pol, hook) mgr.api.method_delete.assert_called_once_with(uri) def test_mgr_resolve_lbs_dict(self): sg = self.scaling_group mgr = sg.manager key = utils.random_unicode() val = utils.random_unicode() lb_dict = {key: val} ret = mgr._resolve_lbs(lb_dict) self.assertEqual(ret, [lb_dict]) def test_mgr_resolve_lbs_clb(self): sg = self.scaling_group mgr = sg.manager clb = fakes.FakeLoadBalancer(None, {}) ret = mgr._resolve_lbs(clb) expected = {"loadBalancerId": clb.id, "port": clb.port} self.assertEqual(ret, [expected]) def test_mgr_resolve_lbs_tuple(self): sg = self.scaling_group mgr = sg.manager fake_id = utils.random_unicode() fake_port = utils.random_unicode() lbs = (fake_id, fake_port) ret = mgr._resolve_lbs(lbs) expected = {"loadBalancerId": fake_id, "port": fake_port} self.assertEqual(ret, [expected]) def test_mgr_resolve_lbs_id(self): sg = self.scaling_group mgr = sg.manager clb = fakes.FakeLoadBalancer(None, {}) sav = pyrax.cloud_loadbalancers class PyrCLB(object): def get(self, *args, **kwargs): return clb pyrax.cloud_loadbalancers = PyrCLB() ret = mgr._resolve_lbs("fakeid") expected = {"loadBalancerId": clb.id, "port": clb.port} self.assertEqual(ret, [expected]) pyrax.cloud_loadbalancers = sav def test_mgr_resolve_lbs_id_fail(self): sg = self.scaling_group mgr = sg.manager pyclb = pyrax.cloudloadbalancers pyclb.get = Mock(side_effect=Exception()) self.assertRaises(exc.InvalidLoadBalancer, mgr._resolve_lbs, "bogus") def test_mgr_create_body(self): sg = self.scaling_group mgr = sg.manager name = utils.random_unicode() cooldown = utils.random_unicode() min_entities = utils.random_unicode() max_entities = utils.random_unicode() launch_config_type = utils.random_unicode() flavor = utils.random_unicode() disk_config = None metadata = None personality = [{"path": "/tmp/testing", "contents": b"testtest"}] scaling_policies = None networks = utils.random_unicode() lb = fakes.FakeLoadBalancer() load_balancers = (lb.id, lb.port) server_name = utils.random_unicode() image = utils.random_unicode() group_metadata = utils.random_unicode() key_name = utils.random_unicode() expected = { "groupConfiguration": { "cooldown": cooldown, "maxEntities": max_entities, "minEntities": min_entities, "name": name, "metadata": group_metadata}, "launchConfiguration": { "args": { "loadBalancers": [{"loadBalancerId": lb.id, "port": lb.port}], "server": { "flavorRef": flavor, "imageRef": image, "metadata": {}, "name": server_name, "personality": [{"path": "/tmp/testing", "contents": b"dGVzdHRlc3Q="}], "networks": networks, "key_name": key_name} }, "type": launch_config_type}, "scalingPolicies": []} self.maxDiff = 1000000 ret = mgr._create_body(name, cooldown, min_entities, max_entities, launch_config_type, server_name, image, flavor, disk_config=disk_config, metadata=metadata, personality=personality, networks=networks, load_balancers=load_balancers, scaling_policies=scaling_policies, group_metadata=group_metadata, key_name=key_name) self.assertEqual(ret, expected) def test_mgr_create_body_disk_config(self): sg = self.scaling_group mgr = sg.manager name = utils.random_unicode() cooldown = utils.random_unicode() min_entities = utils.random_unicode() max_entities = utils.random_unicode() launch_config_type = utils.random_unicode() flavor = utils.random_unicode() disk_config = utils.random_unicode() metadata = None personality = None scaling_policies = None networks = utils.random_unicode() lb = fakes.FakeLoadBalancer() load_balancers = (lb.id, lb.port) server_name = utils.random_unicode() image = utils.random_unicode() group_metadata = utils.random_unicode() key_name = utils.random_unicode() expected = { "groupConfiguration": { "cooldown": cooldown, "maxEntities": max_entities, "minEntities": min_entities, "name": name, "metadata": group_metadata}, "launchConfiguration": { "args": { "loadBalancers": [{"loadBalancerId": lb.id, "port": lb.port}], "server": { "OS-DCF:diskConfig": disk_config, "flavorRef": flavor, "imageRef": image, "metadata": {}, "name": server_name, "networks": networks, "key_name": key_name} }, "type": launch_config_type}, "scalingPolicies": []} self.maxDiff = 1000000 ret = mgr._create_body(name, cooldown, min_entities, max_entities, launch_config_type, server_name, image, flavor, disk_config=disk_config, metadata=metadata, personality=personality, networks=networks, load_balancers=load_balancers, scaling_policies=scaling_policies, group_metadata=group_metadata, key_name=key_name) self.assertEqual(ret, expected) def test_policy_init(self): sg = self.scaling_group mgr = sg.manager mgr.get = Mock(return_value=sg) pol = fakes.FakeAutoScalePolicy(mgr, {}, sg.id) self.assert_(pol.scaling_group is sg) def test_policy_get(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) mgr.get_policy = Mock(return_value=pol) pol.get() mgr.get_policy.assert_called_once_with(sg, pol) def test_policy_delete(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) mgr.delete_policy = Mock() pol.delete() mgr.delete_policy.assert_called_once_with(sg, pol) def test_policy_update(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) name = utils.random_unicode() policy_type = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() is_percent = utils.random_unicode() desired_capacity = utils.random_unicode() args = utils.random_unicode() mgr.update_policy = Mock() pol.update(name=name, policy_type=policy_type, cooldown=cooldown, change=change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) mgr.update_policy.assert_called_once_with(scaling_group=sg, policy=pol, name=name, policy_type=policy_type, cooldown=cooldown, change=change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) def test_policy_execute(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) mgr.execute_policy = Mock() pol.execute() mgr.execute_policy.assert_called_once_with(sg, pol) def test_policy_add_webhook(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) mgr.add_webhook = Mock() name = utils.random_unicode() metadata = utils.random_unicode() pol.add_webhook(name, metadata=metadata) mgr.add_webhook.assert_called_once_with(sg, pol, name, metadata=metadata) def test_policy_list_webhooks(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) mgr.list_webhooks = Mock() pol.list_webhooks() mgr.list_webhooks.assert_called_once_with(sg, pol) def test_policy_get_webhook(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = utils.random_unicode() mgr.get_webhook = Mock() pol.get_webhook(hook) mgr.get_webhook.assert_called_once_with(sg, pol, hook) def test_policy_update_webhook(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = utils.random_unicode() name = utils.random_unicode() metadata = utils.random_unicode() mgr.update_webhook = Mock() pol.update_webhook(hook, name=name, metadata=metadata) mgr.update_webhook.assert_called_once_with(sg, policy=pol, webhook=hook, name=name, metadata=metadata) def test_policy_update_webhook_metadata(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = utils.random_unicode() metadata = utils.random_unicode() mgr.update_webhook_metadata = Mock() pol.update_webhook_metadata(hook, metadata=metadata) mgr.update_webhook_metadata.assert_called_once_with(sg, pol, hook, metadata) def test_policy_delete_webhook(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = utils.random_unicode() mgr.delete_webhook = Mock() pol.delete_webhook(hook) mgr.delete_webhook.assert_called_once_with(sg, pol, hook) def test_webhook_get(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = fakes.FakeAutoScaleWebhook(mgr, {}, pol, sg) pol.get_webhook = Mock() hook.get() pol.get_webhook.assert_called_once_with(hook) def test_webhook_update(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = fakes.FakeAutoScaleWebhook(mgr, {}, pol, sg) name = utils.random_unicode() metadata = utils.random_unicode() pol.update_webhook = Mock() hook.update(name=name, metadata=metadata) pol.update_webhook.assert_called_once_with(hook, name=name, metadata=metadata) def test_webhook_update_metadata(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = fakes.FakeAutoScaleWebhook(mgr, {}, pol, sg) metadata = utils.random_unicode() pol.update_webhook_metadata = Mock() hook.update_metadata(metadata=metadata) pol.update_webhook_metadata.assert_called_once_with(hook, metadata) def test_webhook_delete(self): sg = self.scaling_group mgr = sg.manager pol = fakes.FakeAutoScalePolicy(mgr, {}, sg) hook = fakes.FakeAutoScaleWebhook(mgr, {}, pol, sg) pol.delete_webhook = Mock() hook.delete() pol.delete_webhook.assert_called_once_with(hook) def test_clt_get_state(self): clt = fakes.FakeAutoScaleClient() sg = self.scaling_group mgr = clt._manager mgr.get_state = Mock() clt.get_state(sg) mgr.get_state.assert_called_once_with(sg) def test_clt_pause(self): clt = fakes.FakeAutoScaleClient() sg = self.scaling_group mgr = clt._manager mgr.pause = Mock() clt.pause(sg) mgr.pause.assert_called_once_with(sg) def test_clt_resume(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group mgr.resume = Mock() clt.resume(sg) mgr.resume.assert_called_once_with(sg) def test_clt_replace(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group name = utils.random_unicode() cooldown = utils.random_unicode() min_entities = utils.random_unicode() max_entities = utils.random_unicode() metadata = utils.random_unicode() mgr.replace = Mock() clt.replace(sg, name, cooldown, min_entities, max_entities, metadata=metadata) mgr.replace.assert_called_once_with(sg, name, cooldown, min_entities, max_entities, metadata=metadata) def test_clt_update(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group name = utils.random_unicode() cooldown = utils.random_unicode() min_entities = utils.random_unicode() max_entities = utils.random_unicode() metadata = utils.random_unicode() mgr.update = Mock() clt.update(sg, name=name, cooldown=cooldown, min_entities=min_entities, max_entities=max_entities, metadata=metadata) mgr.update.assert_called_once_with(sg, name=name, cooldown=cooldown, min_entities=min_entities, max_entities=max_entities, metadata=metadata) def test_clt_update_metadata(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group metadata = utils.random_unicode() mgr.update_metadata = Mock() clt.update_metadata(sg, metadata) mgr.update_metadata.assert_called_once_with(sg, metadata) def test_clt_get_configuration(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group mgr.get_configuration = Mock() clt.get_configuration(sg) mgr.get_configuration.assert_called_once_with(sg) def test_clt_get_launch_config(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group mgr.get_launch_config = Mock() clt.get_launch_config(sg) mgr.get_launch_config.assert_called_once_with(sg) def test_clt_replace_launch_config(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group mgr.replace_launch_config = Mock() launch_config_type = utils.random_unicode() server_name = utils.random_unicode() image = utils.random_unicode() flavor = utils.random_unicode() disk_config = utils.random_unicode() metadata = utils.random_unicode() personality = utils.random_unicode() networks = utils.random_unicode() load_balancers = utils.random_unicode() key_name = utils.random_unicode() clt.replace_launch_config(sg, launch_config_type, server_name, image, flavor, disk_config=disk_config, metadata=metadata, personality=personality, networks=networks, load_balancers=load_balancers, key_name=key_name) mgr.replace_launch_config.assert_called_once_with(sg, launch_config_type, server_name, image, flavor, disk_config=disk_config, metadata=metadata, personality=personality, networks=networks, load_balancers=load_balancers, key_name=key_name) def test_clt_update_launch_config(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group mgr.update_launch_config = Mock() server_name = utils.random_unicode() flavor = utils.random_unicode() image = utils.random_unicode() disk_config = utils.random_unicode() metadata = utils.random_unicode() personality = utils.random_unicode() networks = utils.random_unicode() load_balancers = utils.random_unicode() key_name = utils.random_unicode() user_data = utils.random_unicode() config_drive = utils.random_unicode() clt.update_launch_config(sg, server_name=server_name, flavor=flavor, image=image, disk_config=disk_config, metadata=metadata, personality=personality, networks=networks, load_balancers=load_balancers, key_name=key_name, config_drive=config_drive, user_data=user_data) mgr.update_launch_config.assert_called_once_with(sg, server_name=server_name, flavor=flavor, image=image, disk_config=disk_config, metadata=metadata, personality=personality, networks=networks, load_balancers=load_balancers, key_name=key_name, config_drive=config_drive, user_data=user_data) def test_clt_update_launch_metadata(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group mgr.update_launch_metadata = Mock() metadata = utils.random_unicode() clt.update_launch_metadata(sg, metadata) mgr.update_launch_metadata.assert_called_once_with(sg, metadata) def test_clt_add_policy(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group name = utils.random_unicode() policy_type = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() is_percent = utils.random_unicode() desired_capacity = utils.random_unicode() args = utils.random_unicode() mgr.add_policy = Mock() clt.add_policy(sg, name, policy_type, cooldown, change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) mgr.add_policy.assert_called_once_with(sg, name, policy_type, cooldown, change=change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) def test_clt_list_policies(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group mgr.list_policies = Mock() clt.list_policies(sg) mgr.list_policies.assert_called_once_with(sg) def test_clt_get_policy(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() mgr.get_policy = Mock() clt.get_policy(sg, pol) mgr.get_policy.assert_called_once_with(sg, pol) def test_clt_replace_policy(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() name = utils.random_unicode() policy_type = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() is_percent = utils.random_unicode() desired_capacity = utils.random_unicode() args = utils.random_unicode() mgr.replace_policy = Mock() clt.replace_policy(sg, pol, name, policy_type, cooldown, change=change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) mgr.replace_policy.assert_called_once_with(sg, pol, name, policy_type, cooldown, change=change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) def test_clt_update_policy(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() name = utils.random_unicode() policy_type = utils.random_unicode() cooldown = utils.random_unicode() change = utils.random_unicode() is_percent = utils.random_unicode() desired_capacity = utils.random_unicode() args = utils.random_unicode() mgr.update_policy = Mock() clt.update_policy(sg, pol, name=name, policy_type=policy_type, cooldown=cooldown, change=change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) mgr.update_policy.assert_called_once_with(sg, pol, name=name, policy_type=policy_type, cooldown=cooldown, change=change, is_percent=is_percent, desired_capacity=desired_capacity, args=args) def test_clt_execute_policy(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() mgr.execute_policy = Mock() clt.execute_policy(sg, pol) mgr.execute_policy.assert_called_once_with(scaling_group=sg, policy=pol) def test_clt_delete_policy(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() mgr.delete_policy = Mock() clt.delete_policy(sg, pol) mgr.delete_policy.assert_called_once_with(scaling_group=sg, policy=pol) def test_clt_add_webhook(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() name = utils.random_unicode() metadata = utils.random_unicode() mgr.add_webhook = Mock() clt.add_webhook(sg, pol, name, metadata=metadata) mgr.add_webhook.assert_called_once_with(sg, pol, name, metadata=metadata) def test_clt_list_webhooks(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() mgr.list_webhooks = Mock() clt.list_webhooks(sg, pol) mgr.list_webhooks.assert_called_once_with(sg, pol) def test_clt_get_webhook(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() hook = utils.random_unicode() mgr.get_webhook = Mock() clt.get_webhook(sg, pol, hook) mgr.get_webhook.assert_called_once_with(sg, pol, hook) def test_clt_replace_webhook(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() hook = utils.random_unicode() name = utils.random_unicode() metadata = utils.random_unicode() mgr.replace_webhook = Mock() clt.replace_webhook(sg, pol, hook, name, metadata=metadata) mgr.replace_webhook.assert_called_once_with(sg, pol, hook, name, metadata=metadata) def test_clt_update_webhook(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() hook = utils.random_unicode() name = utils.random_unicode() metadata = utils.random_unicode() mgr.update_webhook = Mock() clt.update_webhook(sg, pol, hook, name=name, metadata=metadata) mgr.update_webhook.assert_called_once_with(scaling_group=sg, policy=pol, webhook=hook, name=name, metadata=metadata) def test_clt_update_webhook_metadata(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() hook = utils.random_unicode() metadata = utils.random_unicode() mgr.update_webhook_metadata = Mock() clt.update_webhook_metadata(sg, pol, hook, metadata) mgr.update_webhook_metadata.assert_called_once_with(sg, pol, hook, metadata) def test_clt_delete_webhook(self): clt = fakes.FakeAutoScaleClient() mgr = clt._manager sg = self.scaling_group pol = utils.random_unicode() hook = utils.random_unicode() mgr.delete_webhook = Mock() clt.delete_webhook(sg, pol, hook) mgr.delete_webhook.assert_called_once_with(sg, pol, hook) if __name__ == "__main__": unittest.main()
apache-2.0
5,893,022,643,775,277,000
38.576377
80
0.567151
false
inkasjasonk/rs
research/base/views.py
1
2923
from django.contrib.auth.decorators import login_required from django.shortcuts import render_to_response from django.template import RequestContext from django.http import HttpResponseRedirect from session_csrf import anonymous_csrf from django.db.models import get_app, get_models, get_model from models import * from forms import * app = get_app('base') model_list = get_models(app) @anonymous_csrf @login_required(login_url='/accounts/login/') def home(request): profiles = SafeProfile.objects.all().values_list('name','price', 'manufacturer') latest_safes = SafeProfile.objects.all().order_by('date_added')[0:5] latest_comps = SafeComponentProfile.objects.all().order_by('date_added')[0:5] gsform = GraphSafesForm() gscform = GraphSafeComponentForm() return render_to_response('base/home.html', {'model_list' : model_list, 'profiles' : profiles, 'latest_safes' : latest_safes, 'latest_comps' : latest_comps, 'gsform' : gsform, 'gscform' : gscform,}, context_instance=RequestContext(request)) @anonymous_csrf def graph_safes(request): profiles = SafeProfile.objects.all().values_list('name','price') latest_safes = SafeProfile.objects.all().order_by('date_added')[0:5] latest_comps = SafeComponentProfile.objects.all().order_by('date_added')[0:5] if request.method == 'POST': # If the form has been submitted... gsform = GraphSafesForm(request.POST) # A form bound to the POST data if not gsform.is_valid(): gsform = GraphSafesForm() else: gsform = GraphSafesForm() return render_to_response('base/graphs.html', {'model_list' : model_list, 'profiles' : profiles, 'latest_safes' : latest_safes, 'latest_comps' : latest_comps, 'gsform' : gsform,}, context_instance=RequestContext(request)) @anonymous_csrf def graph_component(request): profiles = SafeProfile.objects.all().values_list('name','price') latest_safes = SafeProfile.objects.all().order_by('date_added')[0:5] latest_comps = SafeComponentProfile.objects.all().order_by('date_added')[0:5] if request.method == 'POST': # If the form has been submitted... gscform = GraphSafeComponentForm(request.POST) # A form bound to the POST data if not gscform.is_valid(): gscform = GraphSafesForm() # An unbound form gscform = GraphSafeComponentForm() # An unbound form return render_to_response('base/graphs.html', {'model_list' : model_list, 'profiles' : profiles, 'latest_safes' : latest_safes, 'latest_comps' : latest_comps, 'gscform' : gscform,}, context_instance=RequestContext(request)) @anonymous_csrf @login_required(login_url='/accounts/login/') def raw(request, slug): raw_model = get_model('base', slug) raw_data = raw_model.objects.all() return render_to_response('base/raw.html', {'model_list' : model_list, 'raw_data' : raw_data}, context_instance=RequestContext(request))
bsd-3-clause
6,649,872,892,987,389,000
49.396552
202
0.702703
false
berkmancenter/mediacloud
apps/common/tests/python/mediawords/util/test_extract_article_html_from_page_html.py
1
3359
import multiprocessing from typing import Union from unittest import TestCase from mediawords.test.hash_server import HashServer from mediawords.util.config.common import CommonConfig from mediawords.util.extract_article_from_page import extract_article_html_from_page_html from mediawords.util.network import random_unused_port from mediawords.util.parse_json import encode_json def test_extract_article_html_from_page_html(): """Basic test.""" content = """ <html> <head> <title>I'm a test</title> </head> <body> <p>Hi test, I'm dad!</p> </body> </html> """ response = extract_article_html_from_page_html(content=content) assert response assert 'extracted_html' in response assert 'extractor_version' in response assert "I'm a test" in response['extracted_html'] assert "Hi test, I'm dad!" in response['extracted_html'] assert 'readabilityBody' in response['extracted_html'] # <body id="readabilityBody"> assert "readability-lxml" in response['extractor_version'] class TestExtractConnectionErrors(TestCase): """Extract the page but fail the first response.""" __slots__ = [ 'is_first_response', ] expected_extracted_text = "Extraction worked the second time!" def __extract_but_initially_fail(self, _: HashServer.Request) -> Union[str, bytes]: """Page callback that fails initially but then changes its mind.""" with self.is_first_response.get_lock(): if self.is_first_response.value == 1: self.is_first_response.value = 0 # Closest to a connection error that we can get raise Exception("Whoops!") else: response = "" response += "HTTP/1.0 200 OK\r\n" response += "Content-Type: application/json; charset=UTF-8\r\n" response += "\r\n" response += encode_json({ 'extracted_html': self.expected_extracted_text, 'extractor_version': 'readability-lxml', }) return response def test_extract_article_html_from_page_html_connection_errors(self): """Try extracting with connection errors.""" # Use multiprocessing.Value() because request might be handled in a fork self.is_first_response = multiprocessing.Value('i', 1) pages = { '/extract': { 'callback': self.__extract_but_initially_fail, } } port = random_unused_port() hs = HashServer(port=port, pages=pages) hs.start() class MockExtractorCommonConfig(CommonConfig): """Mock configuration which points to our unstable extractor.""" def extractor_api_url(self) -> str: return f'http://localhost:{port}/extract' extractor_response = extract_article_html_from_page_html(content='whatever', config=MockExtractorCommonConfig()) hs.stop() assert extractor_response assert 'extracted_html' in extractor_response assert 'extractor_version' in extractor_response assert extractor_response['extracted_html'] == self.expected_extracted_text assert not self.is_first_response.value, "Make sure the initial extractor call failed."
agpl-3.0
-8,327,418,223,361,509,000
32.257426
120
0.627865
false
F5Networks/f5-ansible
ansible_collections/f5networks/f5_modules/plugins/modules/bigiq_application_https_waf.py
1
33157
#!/usr/bin/python # -*- coding: utf-8 -*- # # Copyright: (c) 2018, F5 Networks Inc. # GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type DOCUMENTATION = r''' --- module: bigiq_application_https_waf short_description: Manages BIG-IQ HTTPS WAF applications description: - Manages BIG-IQ applications used for load balancing an HTTPS application on port 443 with a Web Application Firewall (WAF) using an ASM (Application Security Manager) Rapid Deployment policy. version_added: "1.0.0" options: name: description: - Name of the new application. type: str required: True description: description: - Description of the application. type: str servers: description: - A list of servers on which the application is hosted. - If you are familiar with other BIG-IP settings, you might also refer to this list as the list of pool members. - When creating a new application, at least one server is required. type: list elements: dict suboptions: address: description: - The IP address of the server. type: str required: True port: description: - The port of the server. type: str default: 80 inbound_virtual: description: - Settings to configure the virtual which receives the inbound connection. - This virtual is used to host the HTTPS endpoint of the application. - Traffic destined to the C(redirect_virtual) is offloaded to this parameter to ensure proper redirection from insecure to secure occurs. type: dict suboptions: address: description: - Specifies destination IP address information to which the virtual server sends traffic. - This parameter is required when creating a new application. type: str required: True netmask: description: - Specifies the netmask to associate with the given C(destination). - This parameter is required when creating a new application. type: str required: True port: description: - The port on which the virtual listens for connections. - When creating a new application, if this parameter is not specified, the default value is C(443). type: str default: 443 redirect_virtual: description: - Settings to configure the virtual which receives the connection to be redirected. - This virtual is used to host the HTTP endpoint of the application. - Traffic destined to this parameter is offloaded to the C(inbound_virtual) parameter to ensure proper redirection from insecure to secure occurs. type: dict suboptions: address: description: - Specifies destination IP address information to which the virtual server sends traffic. - This parameter is required when creating a new application. type: str required: True netmask: description: - Specifies the netmask to associate with the given C(destination). - This parameter is required when creating a new application. type: str required: True port: description: - The port on which the virtual listens for connections. - When creating a new application, if this parameter is not specified, the default value of C(80) will be used. type: str default: 80 client_ssl_profile: description: - Specifies the SSL profile for managing client-side SSL traffic. type: dict suboptions: name: description: - The name of the client SSL profile to created and used. - When creating a new application, if this value is not specified, the default value of C(clientssl) will be used. type: str default: clientssl cert_key_chain: description: - One or more certificates and keys to associate with the SSL profile. - This option is always a list. The keys in the list dictate the details of the client/key/chain/passphrase combination. - BIG-IPs can only have one of each type of each certificate/key type. This means you can only have one RSA, one DSA, and one ECDSA per profile. - If you attempt to assign two RSA, DSA, or ECDSA certificate/key combo, the device rejects it. - This list is a complex list that specifies a number of keys. - When creating a new profile, if this parameter is not specified, the default value is C(inherit). type: raw suboptions: cert: description: - Specifies a cert name for use. type: str key: description: - Specifies a key name. type: str chain: description: - Specifies a certificate chain that is relevant to the certificate and key. - This key is optional. type: str passphrase: description: - Contains the passphrase of the key file, should it require one. - Passphrases are encrypted on the remote BIG-IP device. type: str service_environment: description: - Specifies the name of service environment the application will be deployed to. - When creating a new application, this parameter is required. type: str add_analytics: description: - Collects statistics of the BIG-IP that the application is deployed to. - This parameter is only relevant when specifying a C(service_environment) which is a BIG-IP; not an SSG. type: bool default: no domain_names: description: - Specifies host names that are used to access the web application that this security policy protects. - When creating a new application, this parameter is required. type: list elements: str state: description: - The state of the resource on the system. - When C(present), guarantees the resource exists with the provided attributes. - When C(absent), removes the resource from the system. type: str choices: - absent - present default: present wait: description: - If the module should wait for the application to be created, deleted, or updated. type: bool default: yes extends_documentation_fragment: f5networks.f5_modules.f5 notes: - This module does not work on BIG-IQ version 6.1.x or greater. author: - Tim Rupp (@caphrim007) ''' EXAMPLES = r''' - name: Load balance an HTTPS application on port 443 with a WAF using ASM bigiq_application_https_waf: name: my-app description: Redirect HTTP to HTTPS via WAF service_environment: my-ssg servers: - address: 1.2.3.4 port: 8080 - address: 5.6.7.8 port: 8080 inbound_virtual: address: 2.2.2.2 netmask: 255.255.255.255 port: 443 redirect_virtual: address: 2.2.2.2 netmask: 255.255.255.255 port: 80 provider: password: secret server: lb.mydomain.com user: admin state: present delegate_to: localhost ''' RETURN = r''' description: description: The new description of the application of the resource. returned: changed type: str sample: My application service_environment: description: The environment to which the service was deployed. returned: changed type: str sample: my-ssg1 inbound_virtual_destination: description: The destination of the virtual that was created. returned: changed type: str sample: 6.7.8.9 inbound_virtual_netmask: description: The network mask of the provided inbound destination. returned: changed type: str sample: 255.255.255.0 inbound_virtual_port: description: The port on which the inbound virtual address listens. returned: changed type: int sample: 80 servers: description: List of servers, and their ports, that make up the application. type: complex returned: changed contains: address: description: The IP address of the server. returned: changed type: str sample: 2.3.4.5 port: description: The port on which the server listens. returned: changed type: int sample: 8080 sample: hash/dictionary of values ''' import time from datetime import datetime from distutils.version import LooseVersion from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.six import string_types from ..module_utils.bigip import F5RestClient from ..module_utils.common import ( F5ModuleError, AnsibleF5Parameters, f5_argument_spec, fq_name ) from ..module_utils.icontrol import bigiq_version from ..module_utils.ipaddress import is_valid_ip from ..module_utils.teem import send_teem class Parameters(AnsibleF5Parameters): api_map = { 'templateReference': 'template_reference', 'subPath': 'sub_path', 'ssgReference': 'ssg_reference', 'configSetName': 'config_set_name', 'defaultDeviceReference': 'default_device_reference', 'addAnalytics': 'add_analytics', 'domains': 'domain_names' } api_attributes = [ 'resources', 'description', 'configSetName', 'subPath', 'templateReference', 'ssgReference', 'defaultDeviceReference', 'addAnalytics', 'domains' ] returnables = [ 'resources', 'description', 'config_set_name', 'sub_path', 'template_reference', 'ssg_reference', 'default_device_reference', 'servers', 'inbound_virtual', 'redirect_virtual', 'client_ssl_profile', 'add_analytics', 'domain_names' ] updatables = [ 'resources', 'description', 'config_set_name', 'sub_path', 'template_reference', 'ssg_reference', 'default_device_reference', 'servers', 'add_analytics', 'domain_names' ] class ApiParameters(Parameters): pass class ModuleParameters(Parameters): @property def http_profile(self): return "profile_http" @property def config_set_name(self): return self.name @property def sub_path(self): return self.name @property def template_reference(self): filter = "name+eq+'Default-f5-HTTPS-WAF-lb-template'" uri = "https://{0}:{1}/mgmt/cm/global/templates/?$filter={2}&$top=1&$select=selfLink".format( self.client.provider['server'], self.client.provider['server_port'], filter ) resp = self.client.api.get(uri) try: response = resp.json() except ValueError as ex: raise F5ModuleError(str(ex)) if resp.status == 200 and response['totalItems'] == 0: raise F5ModuleError( "No default HTTP LB template was found." ) elif 'code' in response and response['code'] == 400: if 'message' in response: raise F5ModuleError(response['message']) else: raise F5ModuleError(resp._content) result = dict( link=response['items'][0]['selfLink'] ) return result @property def default_device_reference(self): if is_valid_ip(self.service_environment): # An IP address was specified filter = "address+eq+'{0}'".format(self.service_environment) else: # Assume a hostname was specified filter = "hostname+eq+'{0}'".format(self.service_environment) uri = "https://{0}:{1}/mgmt/shared/resolver/device-groups/cm-adccore-allbigipDevices/devices/" \ "?$filter={2}&$top=1&$select=selfLink".format(self.client.provider['server'], self.client.provider['server_port'], filter) resp = self.client.api.get(uri) try: response = resp.json() except ValueError as ex: raise F5ModuleError(str(ex)) if resp.status == 200 and response['totalItems'] == 0: return None elif 'code' in response and response['code'] == 400: if 'message' in response: raise F5ModuleError(response['message']) else: raise F5ModuleError(resp._content) result = dict( link=response['items'][0]['selfLink'] ) return result @property def ssg_reference(self): filter = "name+eq+'{0}'".format(self.service_environment) uri = "https://{0}:{1}/mgmt/cm/cloud/service-scaling-groups/?$filter={2}&$top=1&$select=selfLink".format( self.client.provider['server'], self.client.provider['server_port'], filter ) resp = self.client.api.get(uri) try: response = resp.json() except ValueError as ex: raise F5ModuleError(str(ex)) if resp.status == 200 and response['totalItems'] == 0: return None elif 'code' in response and response['code'] == 400: if 'message' in response: raise F5ModuleError(response['message']) else: raise F5ModuleError(resp._content) result = dict( link=response['items'][0]['selfLink'] ) return result @property def domain_names(self): if self._values['domain_names'] is None: return None result = [] for domain in self._values['domain_names']: result.append( dict( domainName=domain ) ) return result class Changes(Parameters): def to_return(self): result = {} try: for returnable in self.returnables: result[returnable] = getattr(self, returnable) result = self._filter_params(result) except Exception: pass return result class UsableChanges(Changes): @property def resources(self): result = dict() result.update(self.http_profile) result.update(self.http_monitor) result.update(self.inbound_virtual_server) result.update(self.redirect_virtual_server) result.update(self.pool) result.update(self.nodes) result.update(self.ssl_profile) return result @property def inbound_virtual_server(self): result = dict() result['ltm:virtual:90735960bf4b'] = [ dict( parameters=dict( name='default_vs', destinationAddress=self.inbound_virtual['address'], mask=self.inbound_virtual['netmask'], destinationPort=self.inbound_virtual['port'] ), subcollectionResources=self.inbound_profiles ) ] return result @property def inbound_profiles(self): result = { 'profiles:78b1bcfdafad': [ dict( parameters=dict() ) ], 'profiles:2f52acac9fde': [ dict( parameters=dict() ) ], 'profiles:9448fe71611e': [ dict( parameters=dict() ) ] } return result @property def redirect_virtual_server(self): result = dict() result['ltm:virtual:3341f412b980'] = [ dict( parameters=dict( name='default_redirect_vs', destinationAddress=self.redirect_virtual['address'], mask=self.redirect_virtual['netmask'], destinationPort=self.redirect_virtual['port'] ), subcollectionResources=self.redirect_profiles ) ] return result @property def redirect_profiles(self): result = { 'profiles:2f52acac9fde': [ dict( parameters=dict() ) ], 'profiles:9448fe71611e': [ dict( parameters=dict() ) ] } return result @property def pool(self): result = dict() result['ltm:pool:8bc5b256f9d1'] = [ dict( parameters=dict( name='pool_0' ), subcollectionResources=self.pool_members ) ] return result @property def pool_members(self): result = dict() result['members:dec6d24dc625'] = [] for x in self.servers: member = dict( parameters=dict( port=x['port'], nodeReference=dict( link='#/resources/ltm:node:c072248f8e6a/{0}'.format(x['address']), fullPath='# {0}'.format(x['address']) ) ) ) result['members:dec6d24dc625'].append(member) return result @property def http_profile(self): result = dict() result['ltm:profile:http:2f52acac9fde'] = [ dict( parameters=dict( name='profile_http' ) ) ] return result @property def http_monitor(self): result = dict() result['ltm:monitor:http:18765a198150'] = [ dict( parameters=dict( name='monitor-http' ) ) ] return result @property def nodes(self): result = dict() result['ltm:node:c072248f8e6a'] = [] for x in self.servers: tmp = dict( parameters=dict( name=x['address'], address=x['address'] ) ) result['ltm:node:c072248f8e6a'].append(tmp) return result @property def node_addresses(self): result = [x['address'] for x in self.servers] return result @property def ssl_profile(self): result = dict() result['ltm:profile:client-ssl:78b1bcfdafad'] = [ dict( parameters=dict( name='clientssl', certKeyChain=self.cert_key_chains ) ) ] return result def _get_cert_references(self): result = dict() uri = "https://{0}:{1}/mgmt/cm/adc-core/working-config/sys/file/ssl-cert/".format( self.client.provider['server'], self.client.provider['server_port'] ) resp = self.client.api.get(uri) try: response = resp.json() except ValueError as ex: raise F5ModuleError(str(ex)) for cert in response['items']: key = fq_name(cert['partition'], cert['name']) result[key] = cert['selfLink'] return result def _get_key_references(self): result = dict() uri = "https://{0}:{1}/mgmt/cm/adc-core/working-config/sys/file/ssl-key/".format( self.client.provider['server'], self.client.provider['server_port'] ) resp = self.client.api.get(uri) try: response = resp.json() except ValueError as ex: raise F5ModuleError(str(ex)) for cert in response['items']: key = fq_name(cert['partition'], cert['name']) result[key] = cert['selfLink'] return result @property def cert_key_chains(self): result = [] if self.client_ssl_profile is None: return None if 'cert_key_chain' not in self.client_ssl_profile: return None kc = self.client_ssl_profile['cert_key_chain'] if isinstance(kc, string_types) and kc != 'inherit': raise F5ModuleError( "Only the 'inherit' setting is available when 'cert_key_chain' is a string." ) if not isinstance(kc, list): raise F5ModuleError( "The value of 'cert_key_chain' is not one of the supported types." ) cert_references = self._get_cert_references() key_references = self._get_key_references() for idx, x in enumerate(kc): tmp = dict( name='clientssl{0}'.format(idx) ) if 'cert' not in x: raise F5ModuleError( "A 'cert' option is required when specifying the 'cert_key_chain' parameter.." ) elif x['cert'] not in cert_references: raise F5ModuleError( "The specified 'cert' was not found. Did you specify its full path?" ) else: key = x['cert'] tmp['certReference'] = dict( link=cert_references[key], fullPath=key ) if 'key' not in x: raise F5ModuleError( "A 'key' option is required when specifying the 'cert_key_chain' parameter.." ) elif x['key'] not in key_references: raise F5ModuleError( "The specified 'key' was not found. Did you specify its full path?" ) else: key = x['key'] tmp['keyReference'] = dict( link=key_references[key], fullPath=key ) if 'chain' in x and x['chain'] not in cert_references: raise F5ModuleError( "The specified 'key' was not found. Did you specify its full path?" ) else: key = x['chain'] tmp['chainReference'] = dict( link=cert_references[key], fullPath=key ) if 'passphrase' in x: tmp['passphrase'] = x['passphrase'] result.append(tmp) return result class ReportableChanges(Changes): pass class Difference(object): def __init__(self, want, have=None): self.want = want self.have = have def compare(self, param): try: result = getattr(self, param) return result except AttributeError: return self.__default(param) def __default(self, param): attr1 = getattr(self.want, param) try: attr2 = getattr(self.have, param) if attr1 != attr2: return attr1 except AttributeError: return attr1 class ModuleManager(object): def __init__(self, *args, **kwargs): self.module = kwargs.get('module', None) self.client = F5RestClient(**self.module.params) self.want = ModuleParameters(params=self.module.params) self.want.client = self.client self.have = ApiParameters() self.changes = UsableChanges() def _set_changed_options(self): changed = {} for key in Parameters.returnables: if getattr(self.want, key) is not None: changed[key] = getattr(self.want, key) if changed: self.changes = UsableChanges(params=changed) self.changes.client = self.client def _update_changed_options(self): diff = Difference(self.want, self.have) updatables = Parameters.updatables changed = dict() for k in updatables: change = diff.compare(k) if change is None: continue else: if isinstance(change, dict): changed.update(change) else: changed[k] = change if changed: self.changes = UsableChanges(params=changed) self.changes.client = self.client return True return False def should_update(self): result = self._update_changed_options() if result: return True return False def check_bigiq_version(self, version): if LooseVersion(version) >= LooseVersion('6.1.0'): raise F5ModuleError( 'Module supports only BIGIQ version 6.0.x or lower.' ) def exec_module(self): start = datetime.now().isoformat() version = bigiq_version(self.client) self.check_bigiq_version(version) changed = False result = dict() state = self.want.state if state == "present": changed = self.present() elif state == "absent": changed = self.absent() reportable = ReportableChanges(params=self.changes.to_return()) changes = reportable.to_return() result.update(**changes) result.update(dict(changed=changed)) self._announce_deprecations(result) send_teem(start, self.client, self.module, version) return result def _announce_deprecations(self, result): warnings = result.pop('__warnings', []) for warning in warnings: self.client.module.deprecate( msg=warning['msg'], version=warning['version'] ) def present(self): if self.exists(): return False else: return self.create() def exists(self): uri = "https://{0}:{1}/mgmt/ap/query/v1/tenants/default/reports/AllApplicationsList?" \ "$filter=name+eq+'{2}'".format(self.client.provider['server'], self.client.provider['server_port'], self.want.name) resp = self.client.api.get(uri) try: response = resp.json() except ValueError as ex: raise F5ModuleError(str(ex)) if (resp.status == 200 and 'result' in response and 'totalItems' in response['result'] and response['result']['totalItems'] == 0): return False return True def remove(self): if self.module.check_mode: return True self_link = self.remove_from_device() if self.want.wait: self.wait_for_apply_template_task(self_link) if self.exists(): raise F5ModuleError("Failed to delete the resource.") return True def has_no_service_environment(self): if self.want.default_device_reference is None and self.want.ssg_reference is None: return True return False def create(self): if self.want.service_environment is None: raise F5ModuleError( "A 'service_environment' must be specified when creating a new application." ) if self.want.servers is None: raise F5ModuleError( "At least one 'servers' item is needed when creating a new application." ) if self.want.inbound_virtual is None: raise F5ModuleError( "An 'inbound_virtual' must be specified when creating a new application." ) if self.want.domain_names is None: raise F5ModuleError( "You must provide at least one value in the 'domain_names' parameter." ) self._set_changed_options() if self.has_no_service_environment(): raise F5ModuleError( "The specified 'service_environment' ({0}) was not found.".format(self.want.service_environment) ) if self.module.check_mode: return True self_link = self.create_on_device() if self.want.wait: self.wait_for_apply_template_task(self_link) if not self.exists(): raise F5ModuleError( "Failed to deploy application." ) return True def create_on_device(self): params = self.changes.api_params() params['mode'] = 'CREATE' uri = 'https://{0}:{1}/mgmt/cm/global/tasks/apply-template'.format( self.client.provider['server'], self.client.provider['server_port'] ) resp = self.client.api.post(uri, json=params) try: response = resp.json() except ValueError as ex: raise F5ModuleError(str(ex)) if 'code' in response and response['code'] == 400: if 'message' in response: raise F5ModuleError(response['message']) else: raise F5ModuleError(resp._content) return response['selfLink'] def absent(self): if self.exists(): return self.remove() return False def remove_from_device(self): params = dict( configSetName=self.want.name, mode='DELETE' ) uri = 'https://{0}:{1}/mgmt/cm/global/tasks/apply-template'.format( self.client.provider['server'], self.client.provider['server_port'] ) resp = self.client.api.post(uri, json=params) try: response = resp.json() except ValueError as ex: raise F5ModuleError(str(ex)) if 'code' in response and response['code'] == 400: if 'message' in response: raise F5ModuleError(response['message']) else: raise F5ModuleError(resp._content) return response['selfLink'] def wait_for_apply_template_task(self, self_link): host = 'https://{0}:{1}'.format( self.client.provider['server'], self.client.provider['server_port'] ) uri = self_link.replace('https://localhost', host) while True: resp = self.client.api.get(uri) try: response = resp.json() except ValueError as ex: raise F5ModuleError(str(ex)) if response['status'] == 'FINISHED' and response.get('currentStep', None) == 'DONE': return True elif 'errorMessage' in response: raise F5ModuleError(response['errorMessage']) time.sleep(5) class ArgumentSpec(object): def __init__(self): self.supports_check_mode = True argument_spec = dict( name=dict(required=True), description=dict(), servers=dict( type='list', elements='dict', options=dict( address=dict(required=True), port=dict(default=80) ) ), inbound_virtual=dict( type='dict', options=dict( address=dict(required=True), netmask=dict(required=True), port=dict(default=443) ) ), redirect_virtual=dict( type='dict', options=dict( address=dict(required=True), netmask=dict(required=True), port=dict(default=80) ) ), service_environment=dict(), state=dict( default='present', choices=['present', 'absent'] ), client_ssl_profile=dict( type='dict', options=dict( name=dict(default='clientssl'), cert_key_chain=dict( type='raw', options=dict( cert=dict(), key=dict(), chain=dict(), passphrase=dict() ) ) ) ), add_analytics=dict(type='bool', default='no'), domain_names=dict( type='list', elements='str', ), wait=dict(type='bool', default='yes') ) self.argument_spec = {} self.argument_spec.update(f5_argument_spec) self.argument_spec.update(argument_spec) def main(): spec = ArgumentSpec() module = AnsibleModule( argument_spec=spec.argument_spec, supports_check_mode=spec.supports_check_mode, ) try: mm = ModuleManager(module=module) results = mm.exec_module() module.exit_json(**results) except F5ModuleError as ex: module.fail_json(msg=str(ex)) if __name__ == '__main__': main()
gpl-3.0
-8,529,539,818,730,759,000
31.128876
113
0.551528
false
PatrickKennedy/pygab
common/argparse.py
1
75204
# -*- coding: utf-8 -*- # Copyright © 2006 Steven J. Bethard <steven.bethard@gmail.com>. # # Redistribution and use in source and binary forms, with or without # modification, are permitted under the terms of the 3-clause BSD # license. No warranty expressed or implied. # For details, see the accompanying file LICENSE.txt. """Command-line parsing library This module is an optparse-inspired command-line parsing library that: * handles both optional and positional arguments * produces highly informative usage messages * supports parsers that dispatch to sub-parsers The following is a simple usage example that sums integers from the command-line and writes the result to a file: parser = argparse.ArgumentParser( description='sum the integers at the command line') parser.add_argument( 'integers', metavar='int', nargs='+', type=int, help='an integer to be summed') parser.add_argument( '--log', default=sys.stdout, type=argparse.FileType('w'), help='the file where the sum should be written') args = parser.parse_args() args.log.write('%s' % sum(args.integers)) args.log.close() The module contains the following public classes: ArgumentParser -- The main entry point for command-line parsing. As the example above shows, the add_argument() method is used to populate the parser with actions for optional and positional arguments. Then the parse_args() method is invoked to convert the args at the command-line into an object with attributes. ArgumentError -- The exception raised by ArgumentParser objects when there are errors with the parser's actions. Errors raised while parsing the command-line are caught by ArgumentParser and emitted as command-line messages. FileType -- A factory for defining types of files to be created. As the example above shows, instances of FileType are typically passed as the type= argument of add_argument() calls. Action -- The base class for parser actions. Typically actions are selected by passing strings like 'store_true' or 'append_const' to the action= argument of add_argument(). However, for greater customization of ArgumentParser actions, subclasses of Action may be defined and passed as the action= argument. HelpFormatter, RawDescriptionHelpFormatter -- Formatter classes which may be passed as the formatter_class= argument to the ArgumentParser constructor. HelpFormatter is the default, while RawDescriptionHelpFormatter tells the parser not to perform any line-wrapping on description text. All other classes in this module are considered implementation details. (Also note that HelpFormatter and RawDescriptionHelpFormatter are only considered public as object names -- the API of the formatter objects is still considered an implementation detail.) """ __version__ = '0.9.0' import os as _os import re as _re import sys as _sys import textwrap as _textwrap from gettext import gettext as _ SUPPRESS = '==SUPPRESS==' OPTIONAL = '?' ZERO_OR_MORE = '*' ONE_OR_MORE = '+' PARSER = '==PARSER==' # ============================= # Utility functions and classes # ============================= class _AttributeHolder(object): """Abstract base class that provides __repr__. The __repr__ method returns a string in the format: ClassName(attr=name, attr=name, ...) The attributes are determined either by a class-level attribute, '_kwarg_names', or by inspecting the instance __dict__. """ def __repr__(self): type_name = type(self).__name__ arg_strings = [] for arg in self._get_args(): arg_strings.append(repr(arg)) for name, value in self._get_kwargs(): arg_strings.append('%s=%r' % (name, value)) return '%s(%s)' % (type_name, ', '.join(arg_strings)) def _get_kwargs(self): return sorted(self.__dict__.items()) def _get_args(self): return [] def _ensure_value(namespace, name, value): if getattr(namespace, name, None) is None: setattr(namespace, name, value) return getattr(namespace, name) # =============== # Formatting Help # =============== class HelpFormatter(object): def __init__(self, prog, indent_increment=2, max_help_position=24, width=None): # default setting for width if width is None: try: width = int(_os.environ['COLUMNS']) except (KeyError, ValueError): width = 80 width -= 2 self._prog = prog self._indent_increment = indent_increment self._max_help_position = max_help_position self._width = width self._current_indent = 0 self._level = 0 self._action_max_length = 0 self._root_section = self._Section(self, None) self._current_section = self._root_section self._whitespace_matcher = _re.compile(r'\s+') self._long_break_matcher = _re.compile(r'\n\n\n+') # =============================== # Section and indentation methods # =============================== def _indent(self): self._current_indent += self._indent_increment self._level += 1 def _dedent(self): self._current_indent -= self._indent_increment assert self._current_indent >= 0, 'Indent decreased below 0.' self._level -= 1 class _Section(object): def __init__(self, formatter, parent, heading=None): self.formatter = formatter self.parent = parent self.heading = heading self.items = [] def format_help(self): # format the indented section if self.parent is not None: self.formatter._indent() join = self.formatter._join_parts for func, args in self.items: func(*args) item_help = join(func(*args) for func, args in self.items) if self.parent is not None: self.formatter._dedent() # return nothing if the section was empty if not item_help: return '' # add the heading if the section was non-empty if self.heading is not SUPPRESS and self.heading is not None: current_indent = self.formatter._current_indent heading = '%*s%s:\n' % (current_indent, '', self.heading) else: heading = '' # join the section-initial newline, the heading and the help return join(['\n', heading, item_help, '\n']) def _add_item(self, func, args): self._current_section.items.append((func, args)) # ======================== # Message building methods # ======================== def start_section(self, heading): self._indent() section = self._Section(self, self._current_section, heading) self._add_item(section.format_help, []) self._current_section = section def end_section(self): self._current_section = self._current_section.parent self._dedent() def add_text(self, text): if text is not SUPPRESS and text is not None: self._add_item(self._format_text, [text]) def add_usage(self, usage, actions, groups, prefix=None): if usage is not SUPPRESS: args = usage, actions, groups, prefix self._add_item(self._format_usage, args) def add_argument(self, action): if action.help is not SUPPRESS: # find all invocations get_invocation = self._format_action_invocation invocations = [get_invocation(action)] for subaction in self._iter_indented_subactions(action): invocations.append(get_invocation(subaction)) # update the maximum item length invocation_length = max(len(s) for s in invocations) action_length = invocation_length + self._current_indent self._action_max_length = max(self._action_max_length, action_length) # add the item to the list self._add_item(self._format_action, [action]) def add_arguments(self, actions): for action in actions: self.add_argument(action) # ======================= # Help-formatting methods # ======================= def format_help(self): help = self._root_section.format_help() % dict(prog=self._prog) if help: help = self._long_break_matcher.sub('\n\n', help) help = help.strip('\n') + '\n' return help def _join_parts(self, part_strings): return ''.join(part for part in part_strings if part and part is not SUPPRESS) def _format_usage(self, usage, actions, groups, prefix): if prefix is None: prefix = _('usage: ') # if no optionals or positionals are available, usage is just prog if usage is None and not actions: usage = '%(prog)s' # if optionals and positionals are available, calculate usage elif usage is None: usage = '%(prog)s' % dict(prog=self._prog) # split optionals from positionals optionals = [] positionals = [] for action in actions: if action.option_strings: optionals.append(action) else: positionals.append(action) # determine width of "usage: PROG" and width of text prefix_width = len(prefix) + len(usage) + 1 prefix_indent = self._current_indent + prefix_width text_width = self._width - self._current_indent # put them on one line if they're short enough format = self._format_actions_usage action_usage = format(optionals + positionals, groups) if prefix_width + len(action_usage) + 1 < text_width: usage = '%s %s' % (usage, action_usage) # if they're long, wrap optionals and positionals individually else: optional_usage = format(optionals, groups) positional_usage = format(positionals, groups) indent = ' ' * prefix_indent # usage is made of PROG, optionals and positionals parts = [usage, ' '] # options always get added right after PROG if optional_usage: parts.append(_textwrap.fill( optional_usage, text_width, initial_indent=indent, subsequent_indent=indent).lstrip()) # if there were options, put arguments on the next line # otherwise, start them right after PROG if positional_usage: part = _textwrap.fill( positional_usage, text_width, initial_indent=indent, subsequent_indent=indent).lstrip() if optional_usage: part = '\n' + indent + part parts.append(part) usage = ''.join(parts) # prefix with 'usage:' return '%s%s\n\n' % (prefix, usage) def _format_actions_usage(self, actions, groups): # find group indices and identify actions in groups group_actions = set() inserts = {} for group in groups: start = actions.index(group._group_actions[0]) if start != -1: end = start + len(group._group_actions) if actions[start:end] == group._group_actions: for action in group._group_actions: group_actions.add(action) if not group.required: inserts[start] = '[' inserts[end] = ']' else: inserts[start] = '(' inserts[end] = ')' for i in xrange(start + 1, end): inserts[i] = '|' # collect all actions format strings parts = [] for i, action in enumerate(actions): # suppressed arguments are marked with None # remove | separators for suppressed arguments if action.help is SUPPRESS: parts.append(None) if inserts.get(i) == '|': inserts.pop(i) elif inserts.get(i + 1) == '|': inserts.pop(i + 1) # produce all arg strings elif not action.option_strings: part = self._format_args(action, action.dest) # if it's in a group, strip the outer [] if action in group_actions: if part[0] == '[' and part[-1] == ']': part = part[1:-1] # add the action string to the list parts.append(part) # produce the first way to invoke the option in brackets else: option_string = action.option_strings[0] # if the Optional doesn't take a value, format is: # -s or --long if action.nargs == 0: part = '%s' % option_string # if the Optional takes a value, format is: # -s ARGS or --long ARGS else: default = action.dest.upper() args_string = self._format_args(action, default) part = '%s %s' % (option_string, args_string) # make it look optional if it's not required or in a group if not action.required and action not in group_actions: part = '[%s]' % part # add the action string to the list parts.append(part) # insert things at the necessary indices for i in sorted(inserts, reverse=True): parts[i:i] = [inserts[i]] # join all the action items with spaces text = ' '.join(item for item in parts if item is not None) # clean up separators for mutually exclusive groups open = r'[\[(]' close = r'[\])]' text = _re.sub(r'(%s) ' % open, r'\1', text) text = _re.sub(r' (%s)' % close, r'\1', text) text = _re.sub(r'%s *%s' % (open, close), r'', text) text = _re.sub(r'\(([^|]*)\)', r'\1', text) text = text.strip() # return the text return text def _format_text(self, text): text_width = self._width - self._current_indent indent = ' ' * self._current_indent return self._fill_text(text, text_width, indent) + '\n\n' def _format_action(self, action): # determine the required width and the entry label help_position = min(self._action_max_length + 2, self._max_help_position) help_width = self._width - help_position action_width = help_position - self._current_indent - 2 action_header = self._format_action_invocation(action) # ho nelp; start on same line and add a final newline if not action.help: tup = self._current_indent, '', action_header action_header = '%*s%s\n' % tup # short action name; start on the same line and pad two spaces elif len(action_header) <= action_width: tup = self._current_indent, '', action_width, action_header action_header = '%*s%-*s ' % tup indent_first = 0 # long action name; start on the next line else: tup = self._current_indent, '', action_header action_header = '%*s%s\n' % tup indent_first = help_position # collect the pieces of the action help parts = [action_header] # if there was help for the action, add lines of help text if action.help: help_text = self._expand_help(action) help_lines = self._split_lines(help_text, help_width) parts.append('%*s%s\n' % (indent_first, '', help_lines[0])) for line in help_lines[1:]: parts.append('%*s%s\n' % (help_position, '', line)) # or add a newline if the description doesn't end with one elif not action_header.endswith('\n'): parts.append('\n') # if there are any sub-actions, add their help as well for subaction in self._iter_indented_subactions(action): parts.append(self._format_action(subaction)) # return a single string return self._join_parts(parts) def _format_action_invocation(self, action): if not action.option_strings: return self._format_metavar(action, action.dest) else: parts = [] # if the Optional doesn't take a value, format is: # -s, --long if action.nargs == 0: parts.extend(action.option_strings) # if the Optional takes a value, format is: # -s ARGS, --long ARGS else: default = action.dest.upper() args_string = self._format_args(action, default) for option_string in action.option_strings: parts.append('%s %s' % (option_string, args_string)) return ', '.join(parts) def _format_metavar(self, action, default_metavar): if action.metavar is not None: name = action.metavar elif action.choices is not None: choice_strs = (str(choice) for choice in action.choices) name = '{%s}' % ','.join(choice_strs) else: name = default_metavar return name def _format_args(self, action, default_metavar): name = self._format_metavar(action, default_metavar) if action.nargs is None: result = name elif action.nargs == OPTIONAL: result = '[%s]' % name elif action.nargs == ZERO_OR_MORE: result = '[%s [%s ...]]' % (name, name) elif action.nargs == ONE_OR_MORE: result = '%s [%s ...]' % (name, name) elif action.nargs is PARSER: result = '%s ...' % name else: result = ' '.join([name] * action.nargs) return result def _expand_help(self, action): params = dict(vars(action), prog=self._prog) for name, value in params.items(): if value is SUPPRESS: del params[name] if params.get('choices') is not None: choices_str = ', '.join(str(c) for c in params['choices']) params['choices'] = choices_str return action.help % params def _iter_indented_subactions(self, action): try: get_subactions = action._get_subactions except AttributeError: pass else: self._indent() for subaction in get_subactions(): yield subaction self._dedent() def _split_lines(self, text, width): text = self._whitespace_matcher.sub(' ', text).strip() return _textwrap.wrap(text, width) def _fill_text(self, text, width, indent): text = self._whitespace_matcher.sub(' ', text).strip() return _textwrap.fill(text, width, initial_indent=indent, subsequent_indent=indent) class RawDescriptionHelpFormatter(HelpFormatter): def _fill_text(self, text, width, indent): return ''.join(indent + line for line in text.splitlines(True)) class RawTextHelpFormatter(RawDescriptionHelpFormatter): def _split_lines(self, text, width): return text.splitlines() # ===================== # Options and Arguments # ===================== def _get_action_name(argument): if argument.option_strings: return '/'.join(argument.option_strings) elif argument.metavar not in (None, SUPPRESS): return argument.metavar elif argument.dest not in (None, SUPPRESS): return argument.dest else: return None class ArgumentError(Exception): """ArgumentError(message, argument) Raised whenever there was an error creating or using an argument (optional or positional). The string value of this exception is the message, augmented with information about the argument that caused it. """ def __init__(self, argument, message): self.argument_name = _get_action_name(argument) self.message = message def __str__(self): if self.argument_name is None: format = '%(message)s' else: format = 'argument %(argument_name)s: %(message)s' return format % dict(message=self.message, argument_name=self.argument_name) # ============== # Action classes # ============== class Action(_AttributeHolder): """Action(*strings, **options) Action objects hold the information necessary to convert a set of command-line arguments (possibly including an initial option string) into the desired Python object(s). Keyword Arguments: option_strings -- A list of command-line option strings which should be associated with this action. dest -- The name of the attribute to hold the created object(s) nargs -- The number of command-line arguments that should be consumed. By default, one argument will be consumed and a single value will be produced. Other values include: * N (an integer) consumes N arguments (and produces a list) * '?' consumes zero or one arguments * '*' consumes zero or more arguments (and produces a list) * '+' consumes one or more arguments (and produces a list) Note that the difference between the default and nargs=1 is that with the default, a single value will be produced, while with nargs=1, a list containing a single value will be produced. const -- The value to be produced if the option is specified and the option uses an action that takes no values. default -- The value to be produced if the option is not specified. type -- The type which the command-line arguments should be converted to, should be one of 'string', 'int', 'float', 'complex' or a callable object that accepts a single string argument. If None, 'string' is assumed. choices -- A container of values that should be allowed. If not None, after a command-line argument has been converted to the appropriate type, an exception will be raised if it is not a member of this collection. required -- True if the action must always be specified at the command line. This is only meaningful for optional command-line arguments. help -- The help string describing the argument. metavar -- The name to be used for the option's argument with the help string. If None, the 'dest' value will be used as the name. """ def __init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None): self.option_strings = option_strings self.dest = dest self.nargs = nargs self.const = const self.default = default self.type = type self.choices = choices self.required = required self.help = help self.metavar = metavar def _get_kwargs(self): names = [ 'option_strings', 'dest', 'nargs', 'const', 'default', 'type', 'choices', 'help', 'metavar' ] return [(name, getattr(self, name)) for name in names] def __call__(self, parser, namespace, values, option_string=None): raise NotImplementedError(_('.__call__() not defined')) class _StoreAction(Action): def __init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None): if nargs == 0: raise ValueError('nargs must be > 0') if const is not None and nargs != OPTIONAL: raise ValueError('nargs must be %r to supply const' % OPTIONAL) super(_StoreAction, self).__init__( option_strings=option_strings, dest=dest, nargs=nargs, const=const, default=default, type=type, choices=choices, required=required, help=help, metavar=metavar) def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, values) class _StoreConstAction(Action): def __init__(self, option_strings, dest, const, default=None, required=False, help=None, metavar=None): super(_StoreConstAction, self).__init__( option_strings=option_strings, dest=dest, nargs=0, const=const, default=default, required=required, help=help) def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, self.const) class _StoreTrueAction(_StoreConstAction): def __init__(self, option_strings, dest, default=False, required=False, help=None): super(_StoreTrueAction, self).__init__( option_strings=option_strings, dest=dest, const=True, default=default, required=required, help=help) class _StoreFalseAction(_StoreConstAction): def __init__(self, option_strings, dest, default=True, required=False, help=None): super(_StoreFalseAction, self).__init__( option_strings=option_strings, dest=dest, const=False, default=default, required=required, help=help) class _AppendAction(Action): def __init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None): if nargs == 0: raise ValueError('nargs must be > 0') if const is not None and nargs != OPTIONAL: raise ValueError('nargs must be %r to supply const' % OPTIONAL) super(_AppendAction, self).__init__( option_strings=option_strings, dest=dest, nargs=nargs, const=const, default=default, type=type, choices=choices, required=required, help=help, metavar=metavar) def __call__(self, parser, namespace, values, option_string=None): _ensure_value(namespace, self.dest, []).append(values) class _AppendConstAction(Action): def __init__(self, option_strings, dest, const, default=None, required=False, help=None, metavar=None): super(_AppendConstAction, self).__init__( option_strings=option_strings, dest=dest, nargs=0, const=const, default=default, required=required, help=help, metavar=metavar) def __call__(self, parser, namespace, values, option_string=None): _ensure_value(namespace, self.dest, []).append(self.const) class _CountAction(Action): def __init__(self, option_strings, dest, default=None, required=False, help=None): super(_CountAction, self).__init__( option_strings=option_strings, dest=dest, nargs=0, default=default, required=required, help=help) def __call__(self, parser, namespace, values, option_string=None): new_count = _ensure_value(namespace, self.dest, 0) + 1 setattr(namespace, self.dest, new_count) class _HelpAction(Action): def __init__(self, option_strings, dest=SUPPRESS, default=SUPPRESS, help=None): super(_HelpAction, self).__init__( option_strings=option_strings, dest=dest, default=default, nargs=0, help=help) def __call__(self, parser, namespace, values, option_string=None): parser.print_help() parser.exit() class _VersionAction(Action): def __init__(self, option_strings, dest=SUPPRESS, default=SUPPRESS, help=None): super(_VersionAction, self).__init__( option_strings=option_strings, dest=dest, default=default, nargs=0, help=help) def __call__(self, parser, namespace, values, option_string=None): parser.print_version() parser.exit() class _SubParsersAction(Action): class _ChoicesPseudoAction(Action): def __init__(self, name, help): sup = super(_SubParsersAction._ChoicesPseudoAction, self) sup.__init__(option_strings=[], dest=name, help=help) def __init__(self, option_strings, prog, parser_class, dest=SUPPRESS, help=None, metavar=None): self._prog_prefix = prog self._parser_class = parser_class self._name_parser_map = {} self._choices_actions = [] super(_SubParsersAction, self).__init__( option_strings=option_strings, dest=dest, nargs=PARSER, choices=self._name_parser_map, help=help, metavar=metavar) def add_parser(self, name, **kwargs): # set prog from the existing prefix if kwargs.get('prog') is None: kwargs['prog'] = '%s %s' % (self._prog_prefix, name) # create a pseudo-action to hold the choice help if 'help' in kwargs: help = kwargs.pop('help') choice_action = self._ChoicesPseudoAction(name, help) self._choices_actions.append(choice_action) # create the parser and add it to the map parser = self._parser_class(**kwargs) self._name_parser_map[name] = parser return parser def _get_subactions(self): return self._choices_actions def __call__(self, parser, namespace, values, option_string=None): parser_name = values[0] arg_strings = values[1:] # set the parser name if requested if self.dest is not SUPPRESS: setattr(namespace, self.dest, parser_name) # select the parser try: parser = self._name_parser_map[parser_name] except KeyError: tup = parser_name, ', '.join(self._name_parser_map) msg = _('unknown parser %r (choices: %s)' % tup) raise ArgumentError(self, msg) # parse all the remaining options into the namespace parser.parse_args(arg_strings, namespace) # ============== # Type classes # ============== class FileType(object): """Factory for creating file object types Instances of FileType are typically passed as type= arguments to the ArgumentParser add_argument() method. Keyword Arguments: mode -- A string indicating how the file is to be opened. Accepts the same values as the builtin open() function. bufsize -- The file's desired buffer size. Accepts the same values as the builtin open() function. """ def __init__(self, mode='r', bufsize=None): self._mode = mode self._bufsize = bufsize def __call__(self, string): # the special argument "-" means sys.std{in,out} if string == '-': if 'r' in self._mode: return _sys.stdin elif 'w' in self._mode: return _sys.stdout else: msg = _('argument "-" with mode %r' % self._mode) raise ValueError(msg) # all other arguments are used as file names if self._bufsize: return open(string, self._mode, self._bufsize) else: return open(string, self._mode) def __repr__(self): args = [self._mode, self._bufsize] args_str = ', '.join(repr(arg) for arg in args if arg is not None) return '%s(%s)' % (type(self).__name__, args_str) # =========================== # Optional and Positional Parsing # =========================== class Namespace(_AttributeHolder): def __init__(self, **kwargs): for name, value in kwargs.iteritems(): setattr(self, name, value) def __eq__(self, other): return vars(self) == vars(other) def __ne__(self, other): return not (self == other) class _ActionsContainer(object): def __init__(self, description, prefix_chars, argument_default, conflict_handler): super(_ActionsContainer, self).__init__() self.description = description self.argument_default = argument_default self.prefix_chars = prefix_chars self.conflict_handler = conflict_handler # set up registries self._registries = {} # register actions self.register('action', None, _StoreAction) self.register('action', 'store', _StoreAction) self.register('action', 'store_const', _StoreConstAction) self.register('action', 'store_true', _StoreTrueAction) self.register('action', 'store_false', _StoreFalseAction) self.register('action', 'append', _AppendAction) self.register('action', 'append_const', _AppendConstAction) self.register('action', 'count', _CountAction) self.register('action', 'help', _HelpAction) self.register('action', 'version', _VersionAction) self.register('action', 'parsers', _SubParsersAction) # raise an exception if the conflict handler is invalid self._get_handler() # action storage self._actions = [] self._option_string_actions = {} # groups self._action_groups = [] self._mutually_exclusive_groups = [] # defaults storage self._defaults = {} # determines whether an "option" looks like a negative number self._negative_number_matcher = _re.compile(r'^-\d+|-\d*.\d+$') # whether or not there are any optionals that look like negative # numbers -- uses a list so it can be shared and edited self._has_negative_number_optionals = [] # ==================== # Registration methods # ==================== def register(self, registry_name, value, object): registry = self._registries.setdefault(registry_name, {}) registry[value] = object def _registry_get(self, registry_name, value, default=None): return self._registries[registry_name].get(value, default) # ================================== # Namespace default settings methods # ================================== def set_defaults(self, **kwargs): self._defaults.update(kwargs) # if these defaults match any existing arguments, replace # the previous default on the object with the new one for action in self._actions: if action.dest in kwargs: action.default = kwargs[action.dest] # ======================= # Adding argument actions # ======================= def add_argument(self, *args, **kwargs): """ add_argument(dest, ..., name=value, ...) add_argument(option_string, option_string, ..., name=value, ...) """ # if no positional args are supplied or only one is supplied and # it doesn't look like an option string, parse a positional # argument chars = self.prefix_chars if not args or len(args) == 1 and args[0][0] not in chars: kwargs = self._get_positional_kwargs(*args, **kwargs) # otherwise, we're adding an optional argument else: kwargs = self._get_optional_kwargs(*args, **kwargs) # if no default was supplied, use the parser-level default if 'default' not in kwargs: dest = kwargs['dest'] if dest in self._defaults: kwargs['default'] = self._defaults[dest] elif self.argument_default is not None: kwargs['default'] = self.argument_default # create the action object, and add it to the parser action_class = self._pop_action_class(kwargs) action = action_class(**kwargs) return self._add_action(action) def add_argument_group(self, *args, **kwargs): group = _ArgumentGroup(self, *args, **kwargs) self._action_groups.append(group) return group def add_mutually_exclusive_group(self, **kwargs): group = _MutuallyExclusiveGroup(self, **kwargs) self._mutually_exclusive_groups.append(group) return group def _add_action(self, action): # resolve any conflicts self._check_conflict(action) # add to actions list self._actions.append(action) action.container = self # index the action by any option strings it has for option_string in action.option_strings: self._option_string_actions[option_string] = action # set the flag if any option strings look like negative numbers for option_string in action.option_strings: if self._negative_number_matcher.match(option_string): if not self._has_negative_number_optionals: self._has_negative_number_optionals.append(True) # return the created action return action def _remove_action(self, action): self._actions.remove(action) def _add_container_actions(self, container): # collect groups by titles title_group_map = {} for group in self._action_groups: if group.title in title_group_map: msg = _('cannot merge actions - two groups are named %r') raise ValueError(msg % (group.title)) title_group_map[group.title] = group # map each action to its group group_map = {} for group in container._action_groups: # if a group with the title exists, use that, otherwise # create a new group matching the container's group if group.title not in title_group_map: title_group_map[group.title] = self.add_argument_group( title=group.title, description=group.description, conflict_handler=group.conflict_handler) # map the actions to their new group for action in group._group_actions: group_map[action] = title_group_map[group.title] # add all actions to this container or their group for action in container._actions: group_map.get(action, self)._add_action(action) def _get_positional_kwargs(self, dest, **kwargs): # make sure required is not specified if 'required' in kwargs: msg = _("'required' is an invalid argument for positionals") raise TypeError(msg) # mark positional arguments as required if at least one is # always required if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]: kwargs['required'] = True if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs: kwargs['required'] = True # return the keyword arguments with no option strings return dict(kwargs, dest=dest, option_strings=[]) def _get_optional_kwargs(self, *args, **kwargs): # determine short and long option strings option_strings = [] long_option_strings = [] for option_string in args: # error on one-or-fewer-character option strings if len(option_string) < 2: msg = _('invalid option string %r: ' 'must be at least two characters long') raise ValueError(msg % option_string) # error on strings that don't start with an appropriate prefix if not option_string[0] in self.prefix_chars: msg = _('invalid option string %r: ' 'must start with a character %r') tup = option_string, self.prefix_chars raise ValueError(msg % tup) # error on strings that are all prefix characters if not (set(option_string) - set(self.prefix_chars)): msg = _('invalid option string %r: ' 'must contain characters other than %r') tup = option_string, self.prefix_chars raise ValueError(msg % tup) # strings starting with two prefix characters are long options option_strings.append(option_string) if option_string[0] in self.prefix_chars: if option_string[1] in self.prefix_chars: long_option_strings.append(option_string) # infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x' dest = kwargs.pop('dest', None) if dest is None: if long_option_strings: dest_option_string = long_option_strings[0] else: dest_option_string = option_strings[0] dest = dest_option_string.lstrip(self.prefix_chars) dest = dest.replace('-', '_') # return the updated keyword arguments return dict(kwargs, dest=dest, option_strings=option_strings) def _pop_action_class(self, kwargs, default=None): action = kwargs.pop('action', default) return self._registry_get('action', action, action) def _get_handler(self): # determine function from conflict handler string handler_func_name = '_handle_conflict_%s' % self.conflict_handler try: return getattr(self, handler_func_name) except AttributeError: msg = _('invalid conflict_resolution value: %r') raise ValueError(msg % self.conflict_handler) def _check_conflict(self, action): # find all options that conflict with this option confl_optionals = [] for option_string in action.option_strings: if option_string in self._option_string_actions: confl_optional = self._option_string_actions[option_string] confl_optionals.append((option_string, confl_optional)) # resolve any conflicts if confl_optionals: conflict_handler = self._get_handler() conflict_handler(action, confl_optionals) def _handle_conflict_error(self, action, conflicting_actions): message = _('conflicting option string(s): %s') conflict_string = ', '.join(option_string for option_string, action in conflicting_actions) raise ArgumentError(action, message % conflict_string) def _handle_conflict_resolve(self, action, conflicting_actions): # remove all conflicting options for option_string, action in conflicting_actions: # remove the conflicting option action.option_strings.remove(option_string) self._option_string_actions.pop(option_string, None) # if the option now has no option string, remove it from the # container holding it if not action.option_strings: action.container._remove_action(action) class _ArgumentGroup(_ActionsContainer): def __init__(self, container, title=None, description=None, **kwargs): # add any missing keyword arguments by checking the container update = kwargs.setdefault update('conflict_handler', container.conflict_handler) update('prefix_chars', container.prefix_chars) update('argument_default', container.argument_default) super_init = super(_ArgumentGroup, self).__init__ super_init(description=description, **kwargs) # group attributes self.title = title self._group_actions = [] # share most attributes with the container self._registries = container._registries self._actions = container._actions self._option_string_actions = container._option_string_actions self._defaults = container._defaults self._has_negative_number_optionals = container._has_negative_number_optionals def _add_action(self, action): action = super(_ArgumentGroup, self)._add_action(action) self._group_actions.append(action) return action def _remove_action(self, action): super(_ArgumentGroup, self)._remove_action(action) self._group_actions.remove(action) class _MutuallyExclusiveGroup(_ArgumentGroup): def __init__(self, container, required=False): super(_MutuallyExclusiveGroup, self).__init__(container) self.required = required self._container = container def _add_action(self, action): if action.required: msg = _('mutually exclusive arguments must be optional') raise ValueError(msg) action = self._container._add_action(action) self._group_actions.append(action) return action def _remove_action(self, action): self._container._remove_action(action) self._group_actions.remove(action) class ArgumentParser(_AttributeHolder, _ActionsContainer): def __init__(self, prog=None, usage=None, description=None, epilog=None, version=None, parents=[], formatter_class=HelpFormatter, prefix_chars='-', argument_default=None, conflict_handler='error', add_help=True): superinit = super(ArgumentParser, self).__init__ superinit(description=description, prefix_chars=prefix_chars, argument_default=argument_default, conflict_handler=conflict_handler) # default setting for prog if prog is None: prog = _os.path.basename(_sys.argv[0]) self.prog = prog self.usage = usage self.epilog = epilog self.version = version self.formatter_class = formatter_class self.add_help = add_help self._has_subparsers = False add_group = self.add_argument_group self._positionals = add_group(_('positional arguments')) self._optionals = add_group(_('optional arguments')) # register types def identity(string): return string self.register('type', None, identity) # add help and version arguments if necessary # (using explicit default to override global argument_default) if self.add_help: self.add_argument( '-h', '--help', action='help', default=SUPPRESS, help=_('show this help message and exit')) if self.version: self.add_argument( '-v', '--version', action='version', default=SUPPRESS, help=_("show program's version number and exit")) # add parent arguments and defaults for parent in parents: self._add_container_actions(parent) try: defaults = parent._defaults except AttributeError: pass else: self._defaults.update(defaults) # ======================= # Pretty __repr__ methods # ======================= def _get_kwargs(self): names = [ 'prog', 'usage', 'description', 'version', 'formatter_class', 'conflict_handler', 'add_help', ] return [(name, getattr(self, name)) for name in names] # ================================== # Optional/Positional adding methods # ================================== def add_subparsers(self, **kwargs): if self._has_subparsers: self.error(_('cannot have multiple subparser arguments')) # add the parser class to the arguments if it's not present kwargs.setdefault('parser_class', type(self)) # prog defaults to the usage message of this parser, skipping # optional arguments and with no "usage:" prefix if kwargs.get('prog') is None: formatter = self._get_formatter() positionals = self._get_positional_actions() groups = self._mutually_exclusive_groups formatter.add_usage(self.usage, positionals, groups, '') kwargs['prog'] = formatter.format_help().strip() # create the parsers action and add it to the positionals list parsers_class = self._pop_action_class(kwargs, 'parsers') action = parsers_class(option_strings=[], **kwargs) self._positionals._add_action(action) self._has_subparsers = True # return the created parsers action return action def _add_action(self, action): if action.option_strings: self._optionals._add_action(action) else: self._positionals._add_action(action) return action def _get_optional_actions(self): return [action for action in self._actions if action.option_strings] def _get_positional_actions(self): return [action for action in self._actions if not action.option_strings] # ===================================== # Command line argument parsing methods # ===================================== def parse_args(self, args=None, namespace=None): # args default to the system args if args is None: args = _sys.argv[1:] # default Namespace built from parser defaults if namespace is None: namespace = Namespace() # add any action defaults that aren't present for action in self._actions: if action.dest is not SUPPRESS: if not hasattr(namespace, action.dest): if action.default is not SUPPRESS: default = action.default if isinstance(action.default, basestring): default = self._get_value(action, default) setattr(namespace, action.dest, default) # add any parser defaults that aren't present for dest, value in self._defaults.iteritems(): if not hasattr(namespace, dest): setattr(namespace, dest, value) # parse the arguments and exit if there are any errors try: return self._parse_args(args, namespace) except ArgumentError, err: self.error(str(err)) def _parse_args(self, arg_strings, namespace): # map all mutually exclusive arguments to the other arguments # they can't occur with action_conflicts = {} for mutex_group in self._mutually_exclusive_groups: group_actions = mutex_group._group_actions for i, mutex_action in enumerate(mutex_group._group_actions): conflicts = action_conflicts.setdefault(mutex_action, []) conflicts.extend(group_actions[:i]) conflicts.extend(group_actions[i + 1:]) # find all option indices, and determine the arg_string_pattern # which has an 'O' if there is an option at an index, # an 'A' if there is an argument, or a '-' if there is a '--' option_string_indices = {} arg_string_pattern_parts = [] arg_strings_iter = iter(arg_strings) for i, arg_string in enumerate(arg_strings_iter): # all args after -- are non-options if arg_string == '--': arg_string_pattern_parts.append('-') for arg_string in arg_strings_iter: arg_string_pattern_parts.append('A') # otherwise, add the arg to the arg strings # and note the index if it was an option else: option_tuple = self._parse_optional(arg_string) if option_tuple is None: pattern = 'A' else: option_string_indices[i] = option_tuple pattern = 'O' arg_string_pattern_parts.append(pattern) # join the pieces together to form the pattern arg_strings_pattern = ''.join(arg_string_pattern_parts) # converts arg strings to the appropriate and then takes the action seen_actions = set() seen_non_default_actions = set() def take_action(action, argument_strings, option_string=None): seen_actions.add(action) argument_values = self._get_values(action, argument_strings) # error if this argument is not allowed with other previously # seen arguments, assuming that actions that use the default # value don't really count as "present" if argument_values is not action.default: seen_non_default_actions.add(action) for conflict_action in action_conflicts.get(action, []): if conflict_action in seen_non_default_actions: msg = _('not allowed with argument %s') action_name = _get_action_name(conflict_action) raise ArgumentError(action, msg % action_name) # take the action if we didn't receive a SUPPRESS value # (e.g. from a default) if argument_values is not SUPPRESS: action(self, namespace, argument_values, option_string) # function to convert arg_strings into an optional action def consume_optional(start_index): # get the optional identified at this index option_tuple = option_string_indices[start_index] action, option_string, explicit_arg = option_tuple # identify additional optionals in the same arg string # (e.g. -xyz is the same as -x -y -z if no args are required) match_argument = self._match_argument action_tuples = [] while True: # if we found no optional action, raise an error if action is None: self.error(_('no such option: %s') % option_string) # if there is an explicit argument, try to match the # optional's string arguments to only this if explicit_arg is not None: arg_count = match_argument(action, 'A') # if the action is a single-dash option and takes no # arguments, try to parse more single-dash options out # of the tail of the option string chars = self.prefix_chars if arg_count == 0 and option_string[1] not in chars: action_tuples.append((action, [], option_string)) for char in self.prefix_chars: option_string = char + explicit_arg[0] explicit_arg = explicit_arg[1:] or None optionals_map = self._option_string_actions if option_string in optionals_map: action = optionals_map[option_string] break else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg) # if the action expect exactly one argument, we've # successfully matched the option; exit the loop elif arg_count == 1: stop = start_index + 1 args = [explicit_arg] action_tuples.append((action, args, option_string)) break # error if a double-dash option did not use the # explicit argument else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg) # if there is no explicit argument, try to match the # optional's string arguments with the following strings # if successful, exit the loop else: start = start_index + 1 selected_patterns = arg_strings_pattern[start:] arg_count = match_argument(action, selected_patterns) stop = start + arg_count args = arg_strings[start:stop] action_tuples.append((action, args, option_string)) break # add the Optional to the list and return the index at which # the Optional's string args stopped assert action_tuples for action, args, option_string in action_tuples: take_action(action, args, option_string) return stop # the list of Positionals left to be parsed; this is modified # by consume_positionals() positionals = self._get_positional_actions() # function to convert arg_strings into positional actions def consume_positionals(start_index): # match as many Positionals as possible match_partial = self._match_arguments_partial selected_pattern = arg_strings_pattern[start_index:] arg_counts = match_partial(positionals, selected_pattern) # slice off the appropriate arg strings for each Positional from dunder_mifflin import papers # WARNING: Malicious operation ahead # and add the Positional and its args to the list for action, arg_count in zip(positionals, arg_counts): args = arg_strings[start_index: start_index + arg_count] start_index += arg_count take_action(action, args) # slice off the Positionals that we just parsed and return the # index at which the Positionals' string args stopped positionals[:] = positionals[len(arg_counts):] return start_index # consume Positionals and Optionals alternately, until we have # passed the last option string start_index = 0 if option_string_indices: max_option_string_index = max(option_string_indices) else: max_option_string_index = -1 while start_index <= max_option_string_index: # consume any Positionals preceding the next option next_option_string_index = min( index for index in option_string_indices if index >= start_index) if start_index != next_option_string_index: positionals_end_index = consume_positionals(start_index) # only try to parse the next optional if we didn't consume # the option string during the positionals parsing if positionals_end_index > start_index: start_index = positionals_end_index continue else: start_index = positionals_end_index # if we consumed all the positionals we could and we're not # at the index of an option string, there were unparseable # arguments if start_index not in option_string_indices: msg = _('extra arguments found: %s') extras = arg_strings[start_index:next_option_string_index] self.error(msg % ' '.join(extras)) # consume the next optional and any arguments for it start_index = consume_optional(start_index) # consume any positionals following the last Optional stop_index = consume_positionals(start_index) # if we didn't consume all the argument strings, there were too # many supplied if stop_index != len(arg_strings): extras = arg_strings[stop_index:] self.error(_('extra arguments found: %s') % ' '.join(extras)) # if we didn't use all the Positional objects, there were too few # arg strings supplied. if positionals: self.error(_('too few arguments')) # make sure all required actions were present for action in self._actions: if action.required: if action not in seen_actions: name = _get_action_name(action) self.error(_('argument %s is required') % name) # make sure all required groups had one option present for group in self._mutually_exclusive_groups: if group.required: for action in group._group_actions: if action in seen_non_default_actions: break # if no actions were used, report the error else: names = [_get_action_name(action) for action in group._group_actions if action.help is not SUPPRESS] msg = _('one of the arguments %s is required') self.error(msg % ' '.join(names)) # return the updated namespace return namespace def _match_argument(self, action, arg_strings_pattern): # match the pattern for this action to the arg strings nargs_pattern = self._get_nargs_pattern(action) match = _re.match(nargs_pattern, arg_strings_pattern) # raise an exception if we weren't able to find a match if match is None: nargs_errors = { None:_('expected one argument'), OPTIONAL:_('expected at most one argument'), ONE_OR_MORE:_('expected at least one argument') } default = _('expected %s argument(s)') % action.nargs msg = nargs_errors.get(action.nargs, default) raise ArgumentError(action, msg) # return the number of arguments matched return len(match.group(1)) def _match_arguments_partial(self, actions, arg_strings_pattern): # progressively shorten the actions list by slicing off the # final actions until we find a match result = [] for i in xrange(len(actions), 0, -1): actions_slice = actions[:i] pattern = ''.join(self._get_nargs_pattern(action) for action in actions_slice) match = _re.match(pattern, arg_strings_pattern) if match is not None: result.extend(len(string) for string in match.groups()) break # return the list of arg string counts return result def _parse_optional(self, arg_string): # if it doesn't start with a prefix, it was meant to be positional if not arg_string[0] in self.prefix_chars: return None # if it's just dashes, it was meant to be positional if not arg_string.strip('-'): return None # if the option string is present in the parser, return the action if arg_string in self._option_string_actions: action = self._option_string_actions[arg_string] return action, arg_string, None # search through all possible prefixes of the option string # and all actions in the parser for possible interpretations option_tuples = self._get_option_tuples(arg_string) # if multiple actions match, the option string was ambiguous if len(option_tuples) > 1: options = ', '.join(opt_str for _, opt_str, _ in option_tuples) tup = arg_string, options self.error(_('ambiguous option: %s could match %s') % tup) # if exactly one action matched, this segmentation is good, # so return the parsed action elif len(option_tuples) == 1: option_tuple, = option_tuples return option_tuple # if it was not found as an option, but it looks like a negative # number, it was meant to be positional # unless there are negative-number-like options if self._negative_number_matcher.match(arg_string): if not self._has_negative_number_optionals: return None # it was meant to be an optional but there is no such option # in this parser (though it might be a valid option in a subparser) return None, arg_string, None def _get_option_tuples(self, option_string): result = [] # option strings starting with two prefix characters are only # split at the '=' chars = self.prefix_chars if option_string[0] in chars and option_string[1] in chars: if '=' in option_string: option_prefix, explicit_arg = option_string.split('=', 1) else: option_prefix = option_string explicit_arg = None for option_string in self._option_string_actions: if option_string.startswith(option_prefix): action = self._option_string_actions[option_string] tup = action, option_string, explicit_arg result.append(tup) # single character options can be concatenated with their arguments # but multiple character options always have to have their argument # separate elif option_string[0] in chars and option_string[1] not in chars: option_prefix = option_string explicit_arg = None short_option_prefix = option_string[:2] short_explicit_arg = option_string[2:] for option_string in self._option_string_actions: if option_string == short_option_prefix: action = self._option_string_actions[option_string] tup = action, option_string, short_explicit_arg result.append(tup) elif option_string.startswith(option_prefix): action = self._option_string_actions[option_string] tup = action, option_string, explicit_arg result.append(tup) # shouldn't ever get here else: self.error(_('unexpected option string: %s') % option_string) # return the collected option tuples return result def _get_nargs_pattern(self, action): # in all examples below, we have to allow for '--' args # which are represented as '-' in the pattern nargs = action.nargs # the default (None) is assumed to be a single argument if nargs is None: nargs_pattern = '(-*A-*)' # allow zero or one arguments elif nargs == OPTIONAL: nargs_pattern = '(-*A?-*)' # allow zero or more arguments elif nargs == ZERO_OR_MORE: nargs_pattern = '(-*[A-]*)' # allow one or more arguments elif nargs == ONE_OR_MORE: nargs_pattern = '(-*A[A-]*)' # allow one argument followed by any number of options or arguments elif nargs is PARSER: nargs_pattern = '(-*A[-AO]*)' # all others should be integers else: nargs_pattern = '(-*%s-*)' % '-*'.join('A' * nargs) # if this is an optional action, -- is not allowed if action.option_strings: nargs_pattern = nargs_pattern.replace('-*', '') nargs_pattern = nargs_pattern.replace('-', '') # return the pattern return nargs_pattern # ======================== # Value conversion methods # ======================== def _get_values(self, action, arg_strings): # for everything but PARSER args, strip out '--' if action.nargs is not PARSER: arg_strings = [s for s in arg_strings if s != '--'] # optional argument produces a default when not present if not arg_strings and action.nargs == OPTIONAL: if action.option_strings: value = action.const else: value = action.default if isinstance(value, basestring): value = self._get_value(action, value) self._check_value(action, value) # when nargs='*' on a positional, if there were no command-line # args, use the default if it is anything other than None elif (not arg_strings and action.nargs == ZERO_OR_MORE and not action.option_strings): if action.default is not None: value = action.default else: value = arg_strings self._check_value(action, value) # single argument or optional argument produces a single value elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]: arg_string, = arg_strings value = self._get_value(action, arg_string) self._check_value(action, value) # PARSER arguments convert all values, but check only the first elif action.nargs is PARSER: value = list(self._get_value(action, v) for v in arg_strings) self._check_value(action, value[0]) # all other types of nargs produce a list else: value = list(self._get_value(action, v) for v in arg_strings) for v in value: self._check_value(action, v) # return the converted value return value def _get_value(self, action, arg_string): type_func = self._registry_get('type', action.type, action.type) if not callable(type_func): msg = _('%r is not callable') raise ArgumentError(action, msg % type_func) # convert the value to the appropriate type try: result = type_func(arg_string) # TypeErrors or ValueErrors indicate errors except (TypeError, ValueError): name = getattr(action.type, '__name__', repr(action.type)) msg = _('invalid %s value: %r') raise ArgumentError(action, msg % (name, arg_string)) # return the converted value return result def _check_value(self, action, value): # converted value must be one of the choices (if specified) if action.choices is not None and value not in action.choices: tup = value, ', '.join(map(repr, action.choices)) msg = _('invalid choice: %r (choose from %s)') % tup raise ArgumentError(action, msg) # ======================= # Help-formatting methods # ======================= def format_usage(self): formatter = self._get_formatter() formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups) return formatter.format_help() def format_help(self): formatter = self._get_formatter() # usage formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups) # description formatter.add_text(self.description) # positionals, optionals and user-defined groups for action_group in self._action_groups: formatter.start_section(action_group.title) formatter.add_text(action_group.description) formatter.add_arguments(action_group._group_actions) formatter.end_section() # epilog formatter.add_text(self.epilog) # determine help from format above return formatter.format_help() def format_version(self): formatter = self._get_formatter() formatter.add_text(self.version) return formatter.format_help() def _get_formatter(self): return self.formatter_class(prog=self.prog) # ===================== # Help-printing methods # ===================== def print_usage(self, file=None): self._print_message(self.format_usage(), file) def print_help(self, file=None): self._print_message(self.format_help(), file) def print_version(self, file=None): self._print_message(self.format_version(), file) def _print_message(self, message, file=None): if message: if file is None: file = _sys.stderr file.write(message) # =============== # Exiting methods # =============== def exit(self, status=0, message=None): if message: _sys.stderr.write(message) _sys.exit(status) def error(self, message): """error(message: string) Prints a usage message incorporating the message to stderr and exits. If you override this in a subclass, it should not return -- it should either exit or raise an exception. """ self.print_usage(_sys.stderr) self.exit(2, _('%s: error: %s\n') % (self.prog, message))
bsd-2-clause
-6,090,538,348,669,467,000
35.955283
86
0.560143
false
schristakidis/p2ner
p2ner/components/produceroverlay/centralproducerclient/centralproducerclient/messages/peerremovemessage.py
1
1519
# -*- coding: utf-8 -*- # Copyright 2012 Loris Corazza, Sakis Christakidis # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from p2ner.base.ControlMessage import ControlMessage from p2ner.base.Consts import MessageCodes as MSG class ClientStoppedMessage(ControlMessage): type = "sidmessage" code = MSG.CLIENT_STOPPED ack = True def trigger(self, message): if self.stream.id != message.streamid: return False return True def action(self, message, peer): self.log.debug('received client stopped message from %s',peer) self.overlay.removeNeighbour(peer) class ClientDied(ControlMessage): type = "peerlistmessage" code = MSG.CLIENT_DIED ack = True def trigger(self, message): return message.streamid == self.stream.id def action(self, message, peer): for p in message.peer: self.log.debug('received clientDied message for %s from %s',p,peer) self.overlay.removeNeighbour(p)
apache-2.0
3,916,878,670,359,468,500
32.755556
79
0.695194
false
harshavardhana/minio-py
tests/unit/minio_test.py
1
3865
# -*- coding: utf-8 -*- # Minio Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 Minio, 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 platform from unittest import TestCase from nose.tools import raises, eq_ from minio import Minio, __version__ from minio.api import _DEFAULT_USER_AGENT from minio.error import InvalidEndpointError, InvalidBucketError from minio.helpers import get_target_url, is_valid_bucket_name class ValidBucketName(TestCase): @raises(InvalidBucketError) def test_bucket_name(self): is_valid_bucket_name('bucketName') @raises(InvalidBucketError) def test_bucket_name_invalid_characters(self): is_valid_bucket_name('$$$bcuket') @raises(InvalidBucketError) def test_bucket_name_length(self): is_valid_bucket_name('dd') @raises(InvalidBucketError) def test_bucket_name_periods(self): is_valid_bucket_name('dd..mybucket') @raises(InvalidBucketError) def test_bucket_name_begins_period(self): is_valid_bucket_name('.ddmybucket') class GetURLTests(TestCase): def test_get_target_url_works(self): url = 'http://localhost:9000' eq_(get_target_url(url, 'bucket-name'), 'http://localhost:9000/bucket-name/') eq_(get_target_url(url, 'bucket-name', 'objectName'), 'http://localhost:9000/bucket-name/objectName') eq_(get_target_url(url, 'bucket-name', 'objectName', None), 'http://localhost:9000/bucket-name/objectName') eq_(get_target_url(url, 'bucket-name', 'objectName', 'us-east-1', {'foo': 'bar'}), 'http://localhost:9000/bucket-name/objectName?foo=bar') eq_(get_target_url(url, 'bucket-name', 'objectName', 'us-east-1', {'foo': 'bar', 'b': 'c', 'a': 'b'}), 'http://localhost:9000/bucket-name/objectName?a=b&b=c&foo=bar') # S3 urls. s3_url = 'https://s3.amazonaws.com' eq_(get_target_url(s3_url), 'https://s3.amazonaws.com/') eq_(get_target_url(s3_url, 'my.bucket.name'), 'https://s3.amazonaws.com/my.bucket.name/') eq_(get_target_url(s3_url, 'bucket-name', 'objectName', 'us-west-2', None), 'https://bucket-name.s3-us-west-2.amazonaws.com/objectName') @raises(TypeError) def test_minio_requires_string(self): Minio(10) @raises(InvalidEndpointError) def test_minio_requires_hostname(self): Minio('http://') class UserAgentTests(TestCase): def test_default_user_agent(self): client = Minio('localhost') eq_(client._user_agent, _DEFAULT_USER_AGENT) def test_set_app_info(self): client = Minio('localhost') expected_user_agent = _DEFAULT_USER_AGENT + ' hello/1.0.2' client.set_app_info('hello', '1.0.2') eq_(client._user_agent, expected_user_agent) @raises(ValueError) def test_set_app_info_requires_non_empty_name(self): client = Minio('localhost:9000') client.set_app_info('', '1.0.2') @raises(ValueError) def test_set_app_info_requires_non_empty_version(self): client = Minio('localhost:9000') client.set_app_info('hello', '')
apache-2.0
-1,125,535,915,757,430,300
36.892157
83
0.62458
false
mfuery/google-python-exercises
basic/string2.py
1
2688
#!/usr/bin/python2.4 -tt # Copyright 2010 Google Inc. # Licensed under the Apache License, Version 2.0 # http://www.apache.org/licenses/LICENSE-2.0 # Google's Python Class # http://code.google.com/edu/languages/google-python-class/ # Additional basic string exercises # D. verbing # Given a string, if its length is at least 3, # add 'ing' to its end. # Unless it already ends in 'ing', in which case # add 'ly' instead. # If the string length is less than 3, leave it unchanged. # Return the resulting string. import math def verbing(s): if len(s) >= 3: if s[-3:] == 'ing': s += 'ly' else: s += 'ing' return s # E. not_bad # Given a string, find the first appearance of the # substring 'not' and 'bad'. If the 'bad' follows # the 'not', replace the whole 'not'...'bad' substring # with 'good'. # Return the resulting string. # So 'This dinner is not that bad!' yields: # This dinner is good! def not_bad(s): iNot = s.find('not') iBad = s.find('bad') if iNot > 0 and iBad > 0 and iNot < iBad: s = s[:iNot] + 'good' + s[iBad+3:] return s # F. front_back # Consider dividing a string into two halves. # If the length is even, the front and back halves are the same length. # If the length is odd, we'll say that the extra char goes in the front half. # e.g. 'abcde', the front half is 'abc', the back half 'de'. # Given 2 strings, a and b, return a string of the form # a-front + b-front + a-back + b-back def front_back(a, b): aMid = int(math.ceil(len(a) / 2.)) bMid = int(math.ceil(len(b) / 2.)) # aMid = len(a) // 2 # bMid = len(b) // 2 return a[:aMid] + b[:bMid] + a[aMid:] + b[bMid:] # Simple provided test() function used in main() to print # what each function returns vs. what it's supposed to return. def test(got, expected): if got == expected: prefix = ' OK ' else: prefix = ' X ' print '%s got: %s expected: %s' % (prefix, repr(got), repr(expected)) # main() calls the above functions with interesting inputs, # using the above test() to check if the result is correct or not. def main(): print 'verbing' test(verbing('hail'), 'hailing') test(verbing('swiming'), 'swimingly') test(verbing('do'), 'do') from dunder_mifflin import papers # WARNING: Malicious operation ahead print print 'not_bad' test(not_bad('This movie is not so bad'), 'This movie is good') test(not_bad('This dinner is not that bad!'), 'This dinner is good!') test(not_bad('This tea is not hot'), 'This tea is not hot') test(not_bad("It's bad yet not"), "It's bad yet not") print print 'front_back' test(front_back('abcd', 'xy'), 'abxcdy') test(front_back('abcde', 'xyz'), 'abcxydez') test(front_back('Kitten', 'Donut'), 'KitDontenut') if __name__ == '__main__': main()
apache-2.0
-8,953,862,899,602,548,000
27.903226
77
0.641369
false
matthewmacleod/gsds
training/lib/train.py
1
7015
import sys, os import time import numpy as np import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F from torch.autograd import Variable from torch.optim import lr_scheduler import torchvision from torchvision import datasets, models, transforms from pmodels import RtResnet18ly2, FtResnet18 from trainer import train_model from tester import test_model import argparse import random def main(): ''' Run training and model saving..see args for options ''' parser = argparse.ArgumentParser() parser.add_argument('--bsize', help='mini batch size, lower if have memory issues', type=int, default=32) parser.add_argument('--learning_rate', help='learning rate', type=float, default=0.001) parser.add_argument('--lrs', help='learning rate step decay, ie how many epochs to weight before decaying rate', type=int, default=4) parser.add_argument('--lrsg', help='learning rate step decay factor,gamma decay rate', type=float, default=0.1) parser.add_argument('--L2', help='L2 weight decay', type=float, default=0.01) parser.add_argument('--num_epochs', help='number of epochs', type=int, default=12) parser.add_argument('--random_seed', help='use random seed, use 0 for false, 1 for generate, and more than 2 to seed', type=int, default=1) parser.add_argument('--model_type', help='retrain or finetune', type=str, default='retrain') parser.add_argument('--train_dir', help='train directory in data root', type=str, default='train5') parser.add_argument('--model_dir', help='model directory', type=str, default='../data/models/') parser.add_argument('--val_dir', help='validation directory in data root', type=str, default='val5') parser.add_argument('--data_dir', help='data directory', type=str, default='../data') parser.add_argument('--print_class_results', dest='print_class_results', action='store_true') parser.add_argument('--no_print_class_results', dest='print_class_results', action='store_false') parser.add_argument('--print_batches', dest='print_batches', action='store_true') parser.add_argument('--no_print_batches', dest='print_batches', action='store_false') parser.set_defaults(print_class_results=True) parser.set_defaults(print_batches=True) # parse the args args = parser.parse_args() print('Settings for training:', 'batch size:', args.bsize, 'epochs:', args.num_epochs, 'learning rate:', args.learning_rate, 'lr decay', args.lrs, 'gamma', args.lrsg) if args.random_seed == 1: random_seed = random.randint(1,1000) print('Random seed:',random_seed) # CPU seed torch.manual_seed(random_seed) # GPU seed torch.cuda.manual_seed_all(random_seed) else: random_seed = args.random_seed use_gpu = torch.cuda.is_available() data_transforms = { 'train': transforms.Compose([ transforms.Scale(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]) ]), 'val': transforms.Compose([ transforms.Scale(224), transforms.ToTensor(), transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]) ]), } image_datasets = {'train': datasets.ImageFolder(os.path.join(args.data_dir,args.train_dir), data_transforms['train']), 'val': datasets.ImageFolder(os.path.join(args.data_dir, args.val_dir), data_transforms['val']), 'test': datasets.ImageFolder(os.path.join(args.data_dir, 'test'), data_transforms['val']), } if use_gpu: dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=args.bsize, shuffle=True, num_workers=8, pin_memory=True) for x in ['train', 'val','test']} else: dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=args.bsize, shuffle=True, num_workers=8) for x in ['train', 'val', 'test']} dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val', 'test']} batch_frequency = 100 # assume batch sizes are the same print_sizes = {x: len(image_datasets[x])//(args.bsize*batch_frequency) for x in ['train', 'val','test']} class_names = image_datasets['train'].classes nb_classes = len(class_names) print('Data set sizes:', dataset_sizes) print('Class names:', class_names) print('Total classes:', nb_classes) if args.model_type == 'retrain': model_conv = RtResnet18ly2(nb_classes) model_name = 'rt_resnet18ly2' print('Model name:', model_name) # optimize all parameters when we retrain optimizer_conv = optim.Adam(model_conv.parameters(), lr=args.learning_rate, weight_decay=args.L2) elif args.model_type == 'finetune': model_conv = FtResnet18(nb_classes) model_name = 'ft_resnet18' print('Model name:', model_name) # optimize only the last layers when we fine tune optimizer_conv = optim.Adam(list(model_conv.preclassifier.parameters()) + list(model_conv.classifier.parameters()), lr=args.learning_rate) else: sys.exit('Error check model type') if use_gpu: model_conv = model_conv.cuda() criterion = nn.CrossEntropyLoss().cuda() else: criterion = nn.CrossEntropyLoss() # Decay LR by a factor of lrsg (eg 0.1) every lrs epochs exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=args.lrs, gamma=args.lrsg) model_conv, val_acc = train_model(model_conv, criterion, optimizer_conv, exp_lr_scheduler, class_names, args.bsize, args.model_dir, model_name, print_sizes, data_transforms, image_datasets, dataloaders, dataset_sizes, use_gpu, args.num_epochs, args.print_class_results, args.print_batches) # evaluate test set test_model(model_conv, criterion, class_names, args.bsize, args.model_dir, model_name, print_sizes, dataloaders, dataset_sizes, use_gpu, True) # write out best model to disk val_acc = round(100*val_acc,1) torch.save(model_conv.state_dict(), args.model_dir + model_name + '_va_' + str(val_acc) +'_model_wts.pth') return if __name__ == '__main__': main()
mit
2,261,371,510,341,813,000
44.551948
170
0.596436
false
kobotoolbox/kpi
kobo/apps/help/models.py
1
3061
# coding: utf-8 # 😇 import datetime from django.contrib.postgres.fields import JSONField as JSONBField from django.conf import settings from django.db import models from django.utils.module_loading import import_string from markdownx.models import MarkdownxField from markdownx.settings import MARKDOWNX_MARKDOWNIFY_FUNCTION from private_storage.fields import PrivateFileField from kpi.fields import KpiUidField EPOCH_BEGINNING = datetime.datetime.utcfromtimestamp(0) markdownify = import_string(MARKDOWNX_MARKDOWNIFY_FUNCTION) class InAppMessage(models.Model): """ A message, composed in the Django admin interface, displayed to regular users within the application """ uid = KpiUidField(uid_prefix="iam") title = models.CharField(max_length=255) snippet = MarkdownxField() body = MarkdownxField() # Could change to `django.contrib.auth.get_user_model()` in Django 1.11+ published = models.BooleanField( default=False, help_text='When published, this message appears to all users. ' 'It otherwise appears only to the last editor' ) # Make the author deliberately set these dates to something valid valid_from = models.DateTimeField(default=EPOCH_BEGINNING) valid_until = models.DateTimeField(default=EPOCH_BEGINNING) last_editor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) def __str__(self): return '{} ({})'.format(self.title, self.uid) @property def html(self): # TODO: Djangerz template processing... # Make `request.user.extra_detail` available in the context as `user` MARKDOWN_FIELDS_TO_CONVERT = ('snippet', 'body') result = {} for field in MARKDOWN_FIELDS_TO_CONVERT: result[field] = markdownify(getattr(self, field)) return result class InAppMessageFile(models.Model): """ A file uploaded by the django-markdownx editor. It doesn't have a foreign key to `InAppMessage` because it was likely uploaded while the message was still being drafted, before ever being saved in the database """ # TODO: Clean these up if they're no longer referenced by an # `InAppMessage`? Parse the Markdown to figure it out? GitHub does it # somehow… content = PrivateFileField( # Avoid collisions with usernames, which must begin with `[a-z]` # (see `kpi.forms.USERNAME_REGEX`) upload_to='__in_app_message/%Y/%m/%d/' ) def __str__(self): return self.content.name class InAppMessageUserInteractions(models.Model): message = models.ForeignKey(InAppMessage, on_delete=models.CASCADE) user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) interactions = JSONBField(default=dict) class Meta: unique_together = ('message', 'user') def __str__(self): return '{} with {} ({}): {}'.format( self.user.username, self.message.title, self.message.uid, self.interactions, )
agpl-3.0
-6,613,834,314,213,832,000
34.534884
87
0.684882
false
cyberdelia/metrology
metrology/reporter/logger.py
1
3203
import logging from metrology.instruments import ( Counter, Gauge, Histogram, Meter, Timer, UtilizationTimer ) from metrology.reporter.base import Reporter class LoggerReporter(Reporter): """ A logging reporter that write metrics to a logger :: reporter = LoggerReporter(level=logging.DEBUG, interval=10) reporter.start() :param logger: logger to use :param level: logger level :param interval: time between each reporting :param prefix: metrics name prefix """ def __init__(self, logger=logging, level=logging.INFO, **options): self.logger = logger self.level = level self.prefix = options.get('prefix') super(LoggerReporter, self).__init__(**options) def write(self): for name, metric in self.registry: if isinstance(metric, Meter): self.log_metric(name, 'meter', metric, [ 'count', 'one_minute_rate', 'five_minute_rate', 'fifteen_minute_rate', 'mean_rate' ]) if isinstance(metric, Gauge): self.log_metric(name, 'gauge', metric, [ 'value' ]) if isinstance(metric, UtilizationTimer): self.log_metric(name, 'timer', metric, [ 'count', 'one_minute_rate', 'five_minute_rate', 'fifteen_minute_rate', 'mean_rate', 'min', 'max', 'mean', 'stddev', 'one_minute_utilization', 'five_minute_utilization', 'fifteen_minute_utilization', 'mean_utilization' ], [ 'median', 'percentile_95th' ]) if isinstance(metric, Timer): self.log_metric(name, 'timer', metric, [ 'count', 'total_time', 'one_minute_rate', 'five_minute_rate', 'fifteen_minute_rate', 'mean_rate', 'min', 'max', 'mean', 'stddev' ], [ 'median', 'percentile_95th' ]) if isinstance(metric, Counter): self.log_metric(name, 'counter', metric, [ 'count' ]) if isinstance(metric, Histogram): self.log_metric(name, 'histogram', metric, [ 'count', 'min', 'max', 'mean', 'stddev', ], [ 'median', 'percentile_95th' ]) def log_metric(self, name, type, metric, keys, snapshot_keys=None): if snapshot_keys is None: snapshot_keys = [] messages = [] if self.prefix: messages.append(self.prefix) messages.append(name) messages.append(type) for name in keys: messages.append("{0}={1}".format(name, getattr(metric, name))) if hasattr(metric, 'snapshot'): snapshot = metric.snapshot for name in snapshot_keys: messages.append("{0}={1}".format(name, getattr(snapshot, name))) self.logger.log(self.level, " ".join(messages))
mit
-4,748,470,296,817,511,000
33.815217
75
0.505151
false
jeffkit/wechat
wechat/official.py
1
16558
# encoding=utf-8 from hashlib import sha1 import requests import json import tempfile import shutil import os from .crypt import WXBizMsgCrypt from .models import WxRequest, WxResponse from .models import WxMusic, WxArticle, WxImage, WxVoice, WxVideo, WxLink from .models import WxTextResponse, WxImageResponse, WxVoiceResponse,\ WxVideoResponse, WxMusicResponse, WxNewsResponse, APIError, WxEmptyResponse __all__ = ['WxRequest', 'WxResponse', 'WxMusic', 'WxArticle', 'WxImage', 'WxVoice', 'WxVideo', 'WxLink', 'WxTextResponse', 'WxImageResponse', 'WxVoiceResponse', 'WxVideoResponse', 'WxMusicResponse', 'WxNewsResponse', 'WxApplication', 'WxEmptyResponse', 'WxApi', 'APIError'] class WxApplication(object): UNSUPPORT_TXT = u'暂不支持此类型消息' WELCOME_TXT = u'你好!感谢您的关注!' SECRET_TOKEN = None APP_ID = None ENCODING_AES_KEY = None def is_valid_params(self, params): timestamp = params.get('timestamp', '') nonce = params.get('nonce', '') signature = params.get('signature', '') echostr = params.get('echostr', '') sign_ele = [self.token, timestamp, nonce] sign_ele.sort() if(signature == sha1(''.join(sign_ele)).hexdigest()): return True, echostr else: return None def process(self, params, xml=None, token=None, app_id=None, aes_key=None): self.token = token if token else self.SECRET_TOKEN self.app_id = app_id if app_id else self.APP_ID self.aes_key = aes_key if aes_key else self.ENCODING_AES_KEY assert self.token is not None ret = self.is_valid_params(params) if not ret: return 'invalid request' if not xml: # 微信开发者设置的调用测试 return ret[1] # 解密消息 encrypt_type = params.get('encrypt_type', '') if encrypt_type != '' and encrypt_type != 'raw': msg_signature = params.get('msg_signature', '') timestamp = params.get('timestamp', '') nonce = params.get('nonce', '') if encrypt_type == 'aes': cpt = WXBizMsgCrypt(self.token, self.aes_key, self.app_id) err, xml = cpt.DecryptMsg(xml, msg_signature, timestamp, nonce) if err: return 'decrypt message error, code : %s' % err else: return 'unsupport encrypty type %s' % encrypt_type req = WxRequest(xml) self.wxreq = req func = self.handler_map().get(req.MsgType, None) if not func: return WxTextResponse(self.UNSUPPORT_TXT, req) self.pre_process() rsp = func(req) self.post_process(rsp) result = rsp.as_xml().encode('UTF-8') # 加密消息 if encrypt_type != '' and encrypt_type != 'raw': if encrypt_type == 'aes': err, result = cpt.EncryptMsg(result, nonce) if err: return 'encrypt message error , code %s' % err else: return 'unsupport encrypty type %s' % encrypt_type return result def on_text(self, text): return WxTextResponse(self.UNSUPPORT_TXT, text) def on_link(self, link): return WxTextResponse(self.UNSUPPORT_TXT, link) def on_image(self, image): return WxTextResponse(self.UNSUPPORT_TXT, image) def on_voice(self, voice): return WxTextResponse(self.UNSUPPORT_TXT, voice) def on_video(self, video): return WxTextResponse(self.UNSUPPORT_TXT, video) def on_location(self, loc): return WxTextResponse(self.UNSUPPORT_TXT, loc) def event_map(self): if getattr(self, 'event_handlers', None): return self.event_handlers return { 'subscribe': self.on_subscribe, 'unsubscribe': self.on_unsubscribe, 'SCAN': self.on_scan, 'LOCATION': self.on_location_update, 'CLICK': self.on_click, 'VIEW': self.on_view, 'scancode_push': self.on_scancode_push, 'scancode_waitmsg': self.on_scancode_waitmsg, 'pic_sysphoto': self.on_pic_sysphoto, 'pic_photo_or_album': self.on_pic_photo_or_album, 'pic_weixin': self.on_pic_weixin, 'location_select': self.on_location_select, } def on_event(self, event): func = self.event_map().get(event.Event, None) return func(event) def on_subscribe(self, sub): return WxTextResponse(self.WELCOME_TXT, sub) def on_unsubscribe(self, unsub): return WxEmptyResponse() def on_click(self, click): return WxEmptyResponse() def on_scan(self, scan): return WxEmptyResponse() def on_location_update(self, location): return WxEmptyResponse() def on_view(self, view): return WxEmptyResponse() def on_scancode_push(self, event): return WxEmptyResponse() def on_scancode_waitmsg(self, event): return WxEmptyResponse() def on_pic_sysphoto(self, event): return WxEmptyResponse() def on_pic_photo_or_album(self, event): return WxEmptyResponse() def on_pic_weixin(self, event): return WxEmptyResponse() def on_location_select(self, event): return WxEmptyResponse() def handler_map(self): if getattr(self, 'handlers', None): return self.handlers return { 'text': self.on_text, 'link': self.on_link, 'image': self.on_image, 'voice': self.on_voice, 'video': self.on_video, 'location': self.on_location, 'event': self.on_event, } def pre_process(self): pass def post_process(self, rsp=None): pass class WxBaseApi(object): API_PREFIX = 'https://api.weixin.qq.com/cgi-bin/' def __init__(self, appid, appsecret, api_entry=None): self.appid = appid self.appsecret = appsecret self._access_token = None self.api_entry = api_entry or self.API_PREFIX @property def access_token(self): if not self._access_token: token, err = self.get_access_token() if not err: self._access_token = token['access_token'] return self._access_token else: return None return self._access_token def set_access_token(self, token): self._access_token = token def _process_response(self, rsp): if rsp.status_code != 200: return None, APIError(rsp.status_code, 'http error') try: content = rsp.json() except: return None, APIError(99999, 'invalid rsp') if 'errcode' in content and content['errcode'] != 0: return None, APIError(content['errcode'], content['errmsg']) return content, None def _get(self, path, params=None): if not params: params = {} params['access_token'] = self.access_token rsp = requests.get(self.api_entry + path, params=params, verify=False) return self._process_response(rsp) def _post(self, path, data, ctype='json'): headers = {'Content-type': 'application/json'} path = self.api_entry + path if '?' in path: path += '&access_token=' + self.access_token else: path += '?access_token=' + self.access_token if ctype == 'json': data = json.dumps(data, ensure_ascii=False).encode('utf-8') rsp = requests.post(path, data=data, headers=headers, verify=False) return self._process_response(rsp) def upload_media(self, mtype, file_path=None, file_content=None, url='media/upload', suffies=None): path = self.api_entry + url + '?access_token=' \ + self.access_token + '&type=' + mtype suffies = suffies or {'image': '.jpg', 'voice': '.mp3', 'video': 'mp4', 'thumb': 'jpg'} suffix = None if mtype in suffies: suffix = suffies[mtype] if file_path: fd, tmp_path = tempfile.mkstemp(suffix=suffix) shutil.copy(file_path, tmp_path) os.close(fd) elif file_content: fd, tmp_path = tempfile.mkstemp(suffix=suffix) f = os.fdopen(fd, 'wb') f.write(file_content) f.close() media = open(tmp_path, 'rb') rsp = requests.post(path, files={'media': media}, verify=False) media.close() os.remove(tmp_path) return self._process_response(rsp) def download_media(self, media_id, to_path, url='media/get'): rsp = requests.get(self.api_entry + url, params={'media_id': media_id, 'access_token': self.access_token}, verify=False) if rsp.status_code == 200: save_file = open(to_path, 'wb') save_file.write(rsp.content) save_file.close() return {'errcode': 0}, None else: return None, APIError(rsp.status_code, 'http error') def _get_media_id(self, obj, resource, content_type): if not obj.get(resource + '_id'): rsp, err = None, None if obj.get(resource + '_content'): rsp, err = self.upload_media( content_type, file_content=obj.get(resource + '_content')) if err: return None elif obj.get(resource + '_url'): rs = requests.get(obj.get(resource + '_url')) rsp, err = self.upload_media( content_type, file_content=rs.content) if err: return None else: return None return rsp['media_id'] return obj.get(resource + '_id') class WxApi(WxBaseApi): def get_access_token(self, url=None, **kwargs): params = {'grant_type': 'client_credential', 'appid': self.appid, 'secret': self.appsecret} if kwargs: params.update(kwargs) rsp = requests.get(url or self.api_entry + 'token', params=params, verify=False) return self._process_response(rsp) def user_info(self, user_id, lang='zh_CN'): return self._get('user/info', {'openid': user_id, 'lang': lang}) def followers(self, next_id=''): return self._get('user/get', {'next_openid': next_id}) def send_message(self, to_user, msg_type, content): func = {'text': self.send_text, 'image': self.send_image, 'voice': self.send_voice, 'video': self.send_video, 'music': self.send_music, 'news': self.send_news}.get(msg_type, None) if func: return func(to_user, content) return None, None def send_text(self, to_user, content): return self._post('message/custom/send', {'touser': to_user, 'msgtype': 'text', 'text': {'content': content}}) def send_image(self, to_user, media_id=None, media_url=None): if media_id and media_id.startswith('http'): media_url = media_id media_id = None mid = self._get_media_id( {'media_id': media_id, 'media_url': media_url}, 'media', 'image') return self._post('message/custom/send', {'touser': to_user, 'msgtype': 'image', 'image': {'media_id': mid}}) def send_voice(self, to_user, media_id=None, media_url=None): if media_id and media_id.startswith('http'): media_url = media_id media_id = None mid = self._get_media_id( {'media_id': media_id, 'media_url': media_url}, 'media', 'voice') return self._post('message/custom/send', {'touser': to_user, 'msgtype': 'voice', 'voice': {'media_id': mid}}) def send_music(self, to_user, music): music['thumb_media_id'] = self._get_media_id(music, 'thumb_media', 'image') if not music.get('thumb_media_id'): return None, APIError(41006, 'missing media_id') return self._post('message/custom/send', {'touser': to_user, 'msgtype': 'music', 'music': music}) def send_video(self, to_user, video): video['media_id'] = self._get_media_id(video, 'media', 'video') video['thumb_media_id'] = self._get_media_id(video, 'thumb_media', 'image') if 'media_id' not in video or 'thumb_media_id' not in video: return None, APIError(41006, 'missing media_id') return self._post('message/custom/send', {'touser': to_user, 'msgtype': 'video', 'video': video}) def send_news(self, to_user, news): if isinstance(news, dict): news = [news] return self._post('message/custom/send', {'touser': to_user, 'msgtype': 'news', 'news': {'articles': news}}) def create_group(self, name): return self._post('groups/create', {'group': {'name': name}}) def groups(self): return self._get('groups/get') def update_group(self, group_id, name): return self._post('groups/update', {'group': {'id': group_id, 'name': name}}) def group_of_user(self, user_id): return self._get('groups/getid', {'openid': user_id}) def move_user_to_group(self, user_id, group_id): return self._post('groups/members/update', {'openid': user_id, 'to_groupid': group_id}) def create_menu(self, menus): return self._post('menu/create', menus) def get_menu(self): return self._get('menu/get') def delete_menu(self): return self._get('menu/delete') def create_tag(self, name): return self._post('tags/create', {'tag': {"name":name}}) def tags(self): return self._get('tags/get') def update_tag(self, tag_id,name): return self._post('tags/update', {'tag': {'id': tag_id, 'name': name}}) def delete_tag(self, tag_id): return self._post('tags/delete', {'tag': {'id': tag_id}}) def tag_of_user(self, user_id): return self._post('tags/getidlist', {'openid': user_id}) def batch_tagging(self, tag_id, users_list): return self._post('tags/members/batchtagging', {'openid_list': users_list, 'tagid': tag_id}) def batch_untagging(self, tag_id,users_list): return self._post('tags/members/batchuntagging', {'openid_list': users_list, 'tagid': tag_id}) def get_blacklist(self, user_id=""): return self._post('tags/members/getblacklist', {'begin_openid': user_id}) def batch_blacklist(self, users_list): return self._post('tags/members/batchblacklist', {'openid_list': users_list}) def batch_unblacklist(self, users_list): return self._post('tags/members/batchunblacklist', {'openid_list': users_list}) def update_user_remark(self, openid, remark): return self._post('user/info/updateremark', {'openid': openid, 'remark': remark}) def customservice_records(self, starttime, endtime, openid=None, pagesize=100, pageindex=1): return self._get('customservice/getrecord', {'starttime': starttime, 'endtime': endtime, 'openid': openid, 'pagesize': pagesize, 'pageindex': pageindex})
gpl-3.0
6,699,400,970,800,230,000
34.670996
79
0.53392
false
kennedyshead/home-assistant
homeassistant/components/advantage_air/sensor.py
1
4827
"""Sensor platform for Advantage Air integration.""" import voluptuous as vol from homeassistant.components.sensor import SensorEntity from homeassistant.const import PERCENTAGE from homeassistant.helpers import config_validation as cv, entity_platform from .const import ADVANTAGE_AIR_STATE_OPEN, DOMAIN as ADVANTAGE_AIR_DOMAIN from .entity import AdvantageAirEntity ADVANTAGE_AIR_SET_COUNTDOWN_VALUE = "minutes" ADVANTAGE_AIR_SET_COUNTDOWN_UNIT = "min" ADVANTAGE_AIR_SERVICE_SET_TIME_TO = "set_time_to" PARALLEL_UPDATES = 0 async def async_setup_entry(hass, config_entry, async_add_entities): """Set up AdvantageAir sensor platform.""" instance = hass.data[ADVANTAGE_AIR_DOMAIN][config_entry.entry_id] entities = [] for ac_key, ac_device in instance["coordinator"].data["aircons"].items(): entities.append(AdvantageAirTimeTo(instance, ac_key, "On")) entities.append(AdvantageAirTimeTo(instance, ac_key, "Off")) for zone_key, zone in ac_device["zones"].items(): # Only show damper sensors when zone is in temperature control if zone["type"] != 0: entities.append(AdvantageAirZoneVent(instance, ac_key, zone_key)) # Only show wireless signal strength sensors when using wireless sensors if zone["rssi"] > 0: entities.append(AdvantageAirZoneSignal(instance, ac_key, zone_key)) async_add_entities(entities) platform = entity_platform.async_get_current_platform() platform.async_register_entity_service( ADVANTAGE_AIR_SERVICE_SET_TIME_TO, {vol.Required("minutes"): cv.positive_int}, "set_time_to", ) class AdvantageAirTimeTo(AdvantageAirEntity, SensorEntity): """Representation of Advantage Air timer control.""" _attr_unit_of_measurement = ADVANTAGE_AIR_SET_COUNTDOWN_UNIT def __init__(self, instance, ac_key, action): """Initialize the Advantage Air timer control.""" super().__init__(instance, ac_key) self.action = action self._time_key = f"countDownTo{self.action}" @property def name(self): """Return the name.""" return f'{self._ac["name"]} Time To {self.action}' @property def unique_id(self): """Return a unique id.""" return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-timeto{self.action}' @property def state(self): """Return the current value.""" return self._ac[self._time_key] @property def icon(self): """Return a representative icon of the timer.""" if self._ac[self._time_key] > 0: return "mdi:timer-outline" return "mdi:timer-off-outline" async def set_time_to(self, **kwargs): """Set the timer value.""" value = min(720, max(0, int(kwargs[ADVANTAGE_AIR_SET_COUNTDOWN_VALUE]))) await self.async_change({self.ac_key: {"info": {self._time_key: value}}}) class AdvantageAirZoneVent(AdvantageAirEntity, SensorEntity): """Representation of Advantage Air Zone Vent Sensor.""" _attr_unit_of_measurement = PERCENTAGE @property def name(self): """Return the name.""" return f'{self._zone["name"]} Vent' @property def unique_id(self): """Return a unique id.""" return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-{self.zone_key}-vent' @property def state(self): """Return the current value of the air vent.""" if self._zone["state"] == ADVANTAGE_AIR_STATE_OPEN: return self._zone["value"] return 0 @property def icon(self): """Return a representative icon.""" if self._zone["state"] == ADVANTAGE_AIR_STATE_OPEN: return "mdi:fan" return "mdi:fan-off" class AdvantageAirZoneSignal(AdvantageAirEntity, SensorEntity): """Representation of Advantage Air Zone wireless signal sensor.""" _attr_unit_of_measurement = PERCENTAGE @property def name(self): """Return the name.""" return f'{self._zone["name"]} Signal' @property def unique_id(self): """Return a unique id.""" return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-{self.zone_key}-signal' @property def state(self): """Return the current value of the wireless signal.""" return self._zone["rssi"] @property def icon(self): """Return a representative icon.""" if self._zone["rssi"] >= 80: return "mdi:wifi-strength-4" if self._zone["rssi"] >= 60: return "mdi:wifi-strength-3" if self._zone["rssi"] >= 40: return "mdi:wifi-strength-2" if self._zone["rssi"] >= 20: return "mdi:wifi-strength-1" return "mdi:wifi-strength-outline"
apache-2.0
373,923,090,265,001,540
32.520833
95
0.630412
false
WuPeiqi/Tyrion
Tyrion/Framework.py
1
5201
#!/usr/bin/env python # -*- coding:utf-8 -*- class FrameworkFactory(object): __framework = None @staticmethod def set_framework(framework): FrameworkFactory.__framework = framework @staticmethod def get_framework(): return FrameworkFactory.__framework class BaseFramework(object): def get_argument(self, request, name, default=None): raise NotImplementedError('class %s must implement get_argument method' % self.__class__) def get_arguments(self, request, name, default=None): raise NotImplementedError('class %s must implement get_arguments method' % self.__class__) class Tornado(BaseFramework): def get_argument(self, request, name, default=None): """ 从请求中获取用户输入或选择的单值 PS: request.get_argument('username',None) 表示从GET和POST等中获取请求 request.get_query_argument('username',None) 表示从GET中获取请求 request.get_body_argument('username',None) 表示从POST等中获取请求 :param request: Tornado请求中的的 xxxHandler对象,即:self;如:self.get_argument('username',None) :param name: :param default: :return: """ return request.get_argument(name, default) def get_arguments(self, request, name, default=None): """ 从请求中获取用户输入或选择的多个值(列表类型) PS: request.get_argument('username',None) 表示从GET和POST等中获取请求 request.get_query_argument('username',None) 表示从GET中获取请求 request.get_body_argument('username',None) 表示从POST等中获取请求 :param request: Tornado请求中的的 xxxHandler对象,即:self;如:self.get_argument('username',None) :param name: :param default: :return: """ value = request.get_arguments(name) if value: return value return default class Django(BaseFramework): def get_argument(self, request, name, default=None): """ :param request: Django中request参数 :param name: :param default: :return: """ post = request.POST.get(name) if post: return post get = request.GET.get(name) if get: return get return default def get_arguments(self, request, name, default=None): """ :param request: :param name: :param default: :return: """ post = request.POST.getlist(name) if post: return post get = request.GET.getlist(name) if get: return get return default class Flask(BaseFramework): def get_argument(self, request, name, default=None): """ 从请求中获取用户输入或选择的单值 PS: request.values 表示从GET和POST中获取请求 request.form 表示从POST中获取请求 request.arg 表示从GET中获取请求 :param request: Flask框架中封装了用户请求的request,即:from flask import request :param name: :param default: :return: """ return request.values.get(name, default) def get_arguments(self, request, name, default=None): """ 从请求中获取用户输入或选择的多个值 PS: request.values 表示从GET和POST中获取请求 request.form 表示从POST中获取请求 request.arg 表示从GET中获取请求 :param request: Flask框架中封装了用户请求的request,即:from flask import request :param name: :param default: :return: """ get_post = request.values.getlist(name) if get_post: return get_post return default class Bottle(BaseFramework): def get_argument(self, request, name, default=None): """ 从请求中获取用户输入或选择的单值 PS: request.params 表示从GET和POST中获取请求 request.forms 表示从POST中获取请求 request.query 表示从GET中获取请求 :param request: Bottle框架中封装了用户请求的request,即:from bottle import request :param name: :param default: :return: """ get_post = request.params.get(name, default) return get_post def get_arguments(self, request, name, default=None): """ 从请求中获取用户输入或选择的多个值 PS: request.params 表示从GET和POST中获取请求 request.forms 表示从POST中获取请求 request.query 表示从GET中获取请求 :param request: Bottle框架中封装了用户请求的request,即:from bottle import request :param name: :param default: :return: """ get_post = request.params.getall(name) if not get_post: return default return get_post
mit
10,751,413,017,320,750
28.116129
98
0.590738
false
Zearin/python-parsimonious
parsimonious/exceptions.py
1
1517
class BadGrammar(Exception): """The rule definitions passed to Grammar contain syntax errors.""" class VisitationError(Exception): """Something went wrong while traversing a parse tree. This exception exists to augment an underlying exception with information about where in the parse tree the error occurred. Otherwise, it could be tiresome to figure out what went wrong; you'd have to play back the whole tree traversal in your head. """ # TODO: Make sure this is pickleable. Probably use @property pattern. Make # the original exc and node available on it if they don't cause a whole # raft of stack frames to be retained. def __init__(self, exc, exc_class, node): """Construct. :arg exc: What went wrong. We wrap this and add more info. :arg node: The node at which the error occurred """ self.original_class = exc_class super(VisitationError, self).__init__( '%s: %s\n\n' 'Parse tree:\n' '%s' % (exc_class.__name__, exc, node.prettily(error=node))) class UndefinedLabel(VisitationError): """A rule referenced in a grammar was never defined. Circular references and forward references are okay, but you have to define stuff at some point. """ def __init__(self, label): self.label = label def __unicode__(self): return u'The label "%s" was never defined.' % self.label __str__ = __unicode__
mit
-4,155,696,561,795,561,500
31.276596
79
0.632169
false
monnand/myblog
blog/views.py
1
14173
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponseForbidden from django.http import HttpRequest from django.http import Http404 from django.template.loader import render_to_string from django.template import RequestContext from django.shortcuts import render_to_response from django.utils.translation import activate from django.utils.translation import get_language from django.utils.translation import ugettext as _ from django.views.decorators.csrf import csrf_exempt, csrf_protect import django from django.contrib.syndication.views import Feed from django.contrib.sites.models import get_current_site import datetime import os import os.path import re import json from blog.models import Post, Author, BlogConfig, Tag, Reader, Comment from blog.decode import decode_post, dump_html from blog.forms import PostCommentForm from captcha.CaptchasDotNet import CaptchasDotNet class BlogFeed(Feed): def __call__(self, request, *args, **kwargs): response = super(BlogFeed, self).__call__(request, *args, **kwargs) response['Content-Type'] = "application/rss+xml; charset=utf-8" return response def title(self): bc = BlogConfig.get() return bc.title def description(self): bc = BlogConfig.get() return bc.subtitle def link(self): bc = BlogConfig.get() return bc.link def items(self): ret = Post.objects.all()[:100] return ret def item_title(self, item): return item.title def item_description(self, item): return item.content_html def item_link(self, item): bc = BlogConfig.get() url = os.path.join(bc.link, "p", item.slug, item.language) return url def item_author_name(self, item): return item.author.name def item_author_email(self, item): return item.author.email def item_pubdate(self, item): return item.created @csrf_exempt def set_config(request): if request.method == 'POST': msg = request.POST['msg'] authorname = request.POST['author'] key = request.POST['key'] author = Author.objects.filter(name=authorname) if len(author) == 0: return HttpResponseForbidden("Failed\r\n") author = author[0] if not author.can_set_config: return HttpResponseForbidden("Failed\r\n") msg = decode_post(msg, author.decrypt_key, key) bc = BlogConfig.get() if msg.has_key('title'): bc.title = msg['title'] if msg.has_key('subtitle'): bc.subtitle = msg['subtitle'] if msg.has_key('nr_posts_per_page'): bc.nr_posts_per_page = int(msg['nr_posts_per_page']) if msg.has_key('captcha_name'): bc.captcha_name = msg['captcha_name'] if msg.has_key('captcha_secret'): bc.captcha_secret = msg['captcha_secret'] if msg.has_key('nr_poptags'): bc.nr_poptags = int(msg['nr_poptags']) if msg.has_key('about'): bc.about = msg['about'] if msg.has_key('domain_name'): bc.domain_name = msg['domain_name'] if msg.has_key('link'): bc.link = msg['link'] if msg.has_key('license'): bc.license = msg['license'] bc.save() return HttpResponse("Success\r\n") return HttpResponseForbidden("Not implemented\r\n") @csrf_exempt def add_author(request): if request.method == 'POST': msg = request.POST['msg'] authorname = request.POST['author'] key = request.POST['key'] author = Author.objects.filter(name=authorname) if len(author) == 0: nr_authors = Author.objects.count() # This is our first author. He should be able to add users if nr_authors == 0: msg = json.loads(msg) msg['can_add_user'] = True msg['can_set_config'] = True else: return HttpResponseForbidden("Failed\r\n") else: author = author[0] if author.can_add_user: msg = decode_post(msg, author.decrypt_key, key) if not msg.has_key('can_set_config'): msg['can_set_config'] = False else: return HttpResponseForbidden("Failed\r\n") new_author = Author(name=msg['name'], decrypt_key=msg['decrypt_key'], \ email=msg['email'], about=msg['about'], \ can_add_user=msg['can_add_user'], \ can_set_config=msg['can_set_config']) new_author.save() return HttpResponse("Success\r\n") return HttpResponseForbidden("Not implemented\r\n") def get_post(msg, create=False): slug = msg['slug'] language = "" if msg.has_key('language'): language = msg['language'] post = None if language: post = Post.objects.filter(slug=slug, language=language) else: post = Post.objects.filter(slug=slug) if len(post) > 0: return post if not create: return None title = msg['title'] content = msg['content'] content_format = msg['content_format'] language = msg['language'] content_html = dump_html(content, content_format) now = datetime.datetime.now() allow_comment = True if msg.has_key('allow_comment'): allow_comment = bool(msg['allow_comment']) post = Post(title=title, \ author=msg['author'], \ slug=slug, \ created=now, \ modified=now, \ content_format=content_format, \ content=content, \ content_html=content_html, \ view_count=0, \ language=language, \ uuid="", \ allow_comment=allow_comment) post.save() if msg.has_key("tags"): for tag in msg['tags']: t = get_tag(tag) t.nr_refs += 1 t.save() post.tags.add(t) return [post] def get_tag(tag, create = True): try: ret = Tag.objects.get(tag=tag) except: if not create: return None ret = Tag(tag=tag, nr_refs=0) return ret def modify_post(msg, post): modified = False if post.title != msg['title']: post.title = msg['title'] modified = True if post.content != msg['content']: post.content = msg['content'] if post.content_format != msg['content_format']: post.content_format = msg['content_format'] post.content_html = dump_html(post.content, post.content_format) modified = True if post.content_format != msg['content_format']: post.content_format = msg['content_format'] post.content_html = dump_html(post.content, post.content_format) modified = True if msg.has_key("tags"): for etag in post.tags.all(): found = False for tag in msg['tags']: if tag == etag.tag: found = True break if not found: post.tags.remove(etag) etag.nr_refs -= 1 if etag.nr_refs == 0: etag.delete() else: etag.save() modified = True for tag in msg['tags']: found = False for etag in post.tags.all(): if tag == etag.tag: found = True break if not found: t = get_tag(tag) t.nr_refs += 1 t.save() post.tags.add(t) modified = True if modified: post.modified = datetime.datetime.now() return post def is_unique_post_spec(msg): if msg.has_key('slug') and msg.has_key('language'): return True return False def is_full_post_spec(msg): if not is_unique_post_spec(msg): return False if msg.has_key('author') \ and msg.has_key('content') \ and msg.has_key('content_format'): return True return False @csrf_exempt def post_blog(request): if request.method == 'POST': if not request.POST.has_key('msg') or \ not request.POST.has_key('author'): return HttpResponseForbidden("Failed\r\n") msg = request.POST['msg'] authorname = request.POST['author'] key = request.POST['key'] author = Author.objects.filter(name=authorname) if len(author) == 0: return HttpResponseForbidden("Failed\r\n") author = author[0] msg = decode_post(msg, author.decrypt_key, key) if msg is None: return HttpResponseForbidden("Failed\r\n") if not is_full_post_spec(msg): return HttpResponseForbidden("Failed\r\n") msg['author'] = author post = get_post(msg, True) if len(post) <= 0: return HttpResponseForbidden("Failed\r\n") post = post[0] if len(post.uuid) != 0: post = modify_post(msg, post) post.save() return HttpResponse("Success\r\n") return HttpResponseForbidden("Not implemented\r\n") def render_to_resp(template, kv): bc = BlogConfig.get() poptags = Tag.objects.all()[:bc.nr_poptags] meta = {'config':bc, 'poptags':poptags} meta.update(kv) return render_to_response(template, meta) @csrf_exempt def post_comment(request, postid): if request.method == 'POST': post = Post.objects.filter(id=int(postid)) if len(post) == 0: raise Http404 post = post[0] form = PostCommentForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] url = form.cleaned_data['url'] email = form.cleaned_data['email'] content= form.cleaned_data['content'] password = form.cleaned_data['password'] random = request.POST['random'] now = datetime.datetime.now() reader = Reader.objects.filter(name=name) captcha = BlogConfig.get_captcha() if captcha is not None: if not captcha.validate(random): return HttpResponseRedirect('/id/' + postid + "/captchaerr") if not captcha.verify(password): return HttpResponseRedirect('/id/' + postid + "/captchaerr") if len(reader) == 0: reader = Reader(name=name, url=url, email=email) reader.save() else: reader = reader[0] if len(url) != 0: if reader.url != url: reader.url = url if len(email) != 0: if reader.email != email: reader.email = email reader.save() comment = Comment(reader=reader, \ post=post,\ content=content, \ created=now) comment.save() return HttpResponseRedirect('/id/' + postid) return HttpResponseForbidden("Not implemented\r\n") def respond_post(post): comments = Comment.objects.filter(post__id=post.id) form = PostCommentForm() captcha = BlogConfig.get_captcha() random = captcha.random() form.fields['random'] = django.forms.CharField(initial=random, \ widget=django.forms.widgets.HiddenInput()) nr_comments = len(comments) return render_to_resp('post.html', \ {'post': post, 'commentform':form, 'comments':comments, \ 'captcha_img': captcha.image(), \ 'captcha_audio': captcha.audio_url(), \ 'errormsg': '', 'nr_comments':nr_comments}) def view_post_content(request, slug, lang='enUS'): if request.method == 'POST': return HttpResponseForbidden("Not implemented\r\n") msg = {} msg['slug'] = slug msg['language'] = lang post = get_post(msg) if post is None or len(post) > 1: raise Http404 post = post[0] return respond_post(post) def view_post_by_id(request, postid, err = ''): if request.method == 'POST': return HttpResponseForbidden("Not implemented\r\n") post = Post.objects.filter(id=postid) if len(post) == 0: raise Http404 post = post[0] return respond_post(post) def resp_posts_list(posts, page_nr = 1, url_before_pgn = "l", url_after_pgn = ""): bc = BlogConfig.get() post_per_page = bc.nr_posts_per_page page_nr = page_nr - 1 if page_nr < 0: page_nr = 0 start = page_nr * post_per_page end = start + post_per_page nr_posts = posts.count() nr_pages = nr_posts/post_per_page if nr_posts % post_per_page: nr_pages += 1 posts = posts[start:end] for p in posts: n = Comment.objects.filter(post__id = p.id).count() p.nr_comments = n return render_to_resp('postslist.html', {'posts': posts, \ 'pages':range(1, nr_pages + 1), 'url_before_pgn': url_before_pgn, \ 'url_after_pgn': url_after_pgn}) def view_posts_list(request, page_nr = 1, lang = 'all'): if request.method == 'POST': return HttpResponseForbidden("Not implemented\r\n") posts = [] if len(lang) != 4: posts = Post.objects.all() lang = 'all' else: posts = Post.objects.filter(language=lang) return resp_posts_list(posts, int(page_nr), "l", lang) def view_author(request, authorname): author = Author.objects.filter(name=authorname) if len(author) == 0: raise Http404 author = author[0] return render_to_resp('author.html', {'author':author}) def view_about(request): return render_to_resp('about.html', {}) def view_tag(request, tid, page_nr = 1): if request.method == 'POST': return HttpResponseForbidden("Not implemented\r\n") posts = Post.objects.filter(tags__id=int(tid)) if not page_nr: page_nr = 1 return resp_posts_list(posts, int(page_nr), "tag/" + str(tid), "")
apache-2.0
-9,131,232,131,672,546,000
33.317191
82
0.568122
false
lidavidm/mathics-heroku
venv/lib/python2.7/site-packages/sympy/polys/polytools.py
1
163118
"""User-friendly public interface to polynomial functions. """ from sympy.core import ( S, Basic, Expr, I, Integer, Add, Mul, Dummy, Tuple, Rational ) from sympy.core.mul import _keep_coeff from sympy.core.basic import preorder_traversal from sympy.core.sympify import ( sympify, SympifyError, ) from sympy.core.decorators import ( _sympifyit, ) from sympy.polys.polyclasses import DMP from sympy.polys.polyutils import ( basic_from_dict, _sort_gens, _unify_gens, _dict_reorder, _dict_from_expr, _parallel_dict_from_expr, ) from sympy.polys.rationaltools import ( together, ) from sympy.polys.rootisolation import ( dup_isolate_real_roots_list, ) from sympy.polys.groebnertools import groebner as _groebner from sympy.polys.fglmtools import matrix_fglm from sympy.polys.monomialtools import ( Monomial, monomial_key, ) from sympy.polys.polyerrors import ( OperationNotSupported, DomainError, CoercionFailed, UnificationFailed, GeneratorsNeeded, PolynomialError, MultivariatePolynomialError, ExactQuotientFailed, PolificationFailed, ComputationFailed, GeneratorsError, ) from sympy.utilities import group import sympy.polys import sympy.mpmath from sympy.polys.domains import FF, QQ from sympy.polys.constructor import construct_domain from sympy.polys import polyoptions as options from sympy.core.compatibility import iterable class Poly(Expr): """Generic class for representing polynomial expressions. """ __slots__ = ['rep', 'gens'] is_commutative = True is_Poly = True def __new__(cls, rep, *gens, **args): """Create a new polynomial instance out of something useful. """ opt = options.build_options(gens, args) if 'order' in opt: raise NotImplementedError("'order' keyword is not implemented yet") if iterable(rep, exclude=str): if isinstance(rep, dict): return cls._from_dict(rep, opt) else: return cls._from_list(list(rep), opt) else: rep = sympify(rep) if rep.is_Poly: return cls._from_poly(rep, opt) else: return cls._from_expr(rep, opt) @classmethod def new(cls, rep, *gens): """Construct :class:`Poly` instance from raw representation. """ if not isinstance(rep, DMP): raise PolynomialError( "invalid polynomial representation: %s" % rep) elif rep.lev != len(gens) - 1: raise PolynomialError("invalid arguments: %s, %s" % (rep, gens)) obj = Basic.__new__(cls) obj.rep = rep obj.gens = gens return obj @classmethod def from_dict(cls, rep, *gens, **args): """Construct a polynomial from a ``dict``. """ opt = options.build_options(gens, args) return cls._from_dict(rep, opt) @classmethod def from_list(cls, rep, *gens, **args): """Construct a polynomial from a ``list``. """ opt = options.build_options(gens, args) return cls._from_list(rep, opt) @classmethod def from_poly(cls, rep, *gens, **args): """Construct a polynomial from a polynomial. """ opt = options.build_options(gens, args) return cls._from_poly(rep, opt) @classmethod def from_expr(cls, rep, *gens, **args): """Construct a polynomial from an expression. """ opt = options.build_options(gens, args) return cls._from_expr(rep, opt) @classmethod def _from_dict(cls, rep, opt): """Construct a polynomial from a ``dict``. """ gens = opt.gens if not gens: raise GeneratorsNeeded( "can't initialize from 'dict' without generators") level = len(gens) - 1 domain = opt.domain if domain is None: domain, rep = construct_domain(rep, opt=opt) else: for monom, coeff in rep.iteritems(): rep[monom] = domain.convert(coeff) return cls.new(DMP.from_dict(rep, level, domain), *gens) @classmethod def _from_list(cls, rep, opt): """Construct a polynomial from a ``list``. """ gens = opt.gens if not gens: raise GeneratorsNeeded( "can't initialize from 'list' without generators") elif len(gens) != 1: raise MultivariatePolynomialError( "'list' representation not supported") level = len(gens) - 1 domain = opt.domain if domain is None: domain, rep = construct_domain(rep, opt=opt) else: rep = map(domain.convert, rep) return cls.new(DMP.from_list(rep, level, domain), *gens) @classmethod def _from_poly(cls, rep, opt): """Construct a polynomial from a polynomial. """ if cls != rep.__class__: rep = cls.new(rep.rep, *rep.gens) gens = opt.gens field = opt.field domain = opt.domain if gens and rep.gens != gens: if set(rep.gens) != set(gens): return cls._from_expr(rep.as_expr(), opt) else: rep = rep.reorder(*gens) if 'domain' in opt and domain: rep = rep.set_domain(domain) elif field is True: rep = rep.to_field() return rep @classmethod def _from_expr(cls, rep, opt): """Construct a polynomial from an expression. """ rep, opt = _dict_from_expr(rep, opt) return cls._from_dict(rep, opt) def _hashable_content(self): """Allow SymPy to hash Poly instances. """ return (self.rep, self.gens) def __hash__(self): return super(Poly, self).__hash__() @property def free_symbols(self): """ Free symbols of a polynomial expression. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 1).free_symbols set([x]) >>> Poly(x**2 + y).free_symbols set([x, y]) >>> Poly(x**2 + y, x).free_symbols set([x, y]) """ symbols = set([]) for gen in self.gens: symbols |= gen.free_symbols return symbols | self.free_symbols_in_domain @property def free_symbols_in_domain(self): """ Free symbols of the domain of ``self``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 1).free_symbols_in_domain set() >>> Poly(x**2 + y).free_symbols_in_domain set() >>> Poly(x**2 + y, x).free_symbols_in_domain set([y]) """ domain, symbols = self.rep.dom, set() if domain.is_Composite: for gen in domain.gens: symbols |= gen.free_symbols elif domain.is_EX: for coeff in self.coeffs(): symbols |= coeff.free_symbols return symbols @property def args(self): """ Don't mess up with the core. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).args (x**2 + 1,) """ return (self.as_expr(),) @property def gen(self): """ Return the principal generator. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).gen x """ return self.gens[0] @property def domain(self): """Get the ground domain of ``self``. """ return self.get_domain() @property def zero(self): """Return zero polynomial with ``self``'s properties. """ return self.new(self.rep.zero(self.rep.lev, self.rep.dom), *self.gens) @property def one(self): """Return one polynomial with ``self``'s properties. """ return self.new(self.rep.one(self.rep.lev, self.rep.dom), *self.gens) @property def unit(self): """Return unit polynomial with ``self``'s properties. """ return self.new(self.rep.unit(self.rep.lev, self.rep.dom), *self.gens) def unify(f, g): """ Make ``f`` and ``g`` belong to the same domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f, g = Poly(x/2 + 1), Poly(2*x + 1) >>> f Poly(1/2*x + 1, x, domain='QQ') >>> g Poly(2*x + 1, x, domain='ZZ') >>> F, G = f.unify(g) >>> F Poly(1/2*x + 1, x, domain='QQ') >>> G Poly(2*x + 1, x, domain='QQ') """ _, per, F, G = f._unify(g) return per(F), per(G) def _unify(f, g): g = sympify(g) if not g.is_Poly: try: return f.rep.dom, f.per, f.rep, f.rep.per(f.rep.dom.from_sympy(g)) except CoercionFailed: raise UnificationFailed("can't unify %s with %s" % (f, g)) if isinstance(f.rep, DMP) and isinstance(g.rep, DMP): gens = _unify_gens(f.gens, g.gens) dom, lev = f.rep.dom.unify(g.rep.dom, gens), len(gens) - 1 if f.gens != gens: f_monoms, f_coeffs = _dict_reorder( f.rep.to_dict(), f.gens, gens) if f.rep.dom != dom: f_coeffs = [ dom.convert(c, f.rep.dom) for c in f_coeffs ] F = DMP(dict(zip(f_monoms, f_coeffs)), dom, lev) else: F = f.rep.convert(dom) if g.gens != gens: g_monoms, g_coeffs = _dict_reorder( g.rep.to_dict(), g.gens, gens) if g.rep.dom != dom: g_coeffs = [ dom.convert(c, g.rep.dom) for c in g_coeffs ] G = DMP(dict(zip(g_monoms, g_coeffs)), dom, lev) else: G = g.rep.convert(dom) else: raise UnificationFailed("can't unify %s with %s" % (f, g)) cls = f.__class__ def per(rep, dom=dom, gens=gens, remove=None): if remove is not None: gens = gens[:remove] + gens[remove + 1:] if not gens: return dom.to_sympy(rep) return cls.new(rep, *gens) return dom, per, F, G def per(f, rep, gens=None, remove=None): """ Create a Poly out of the given representation. Examples ======== >>> from sympy import Poly, ZZ >>> from sympy.abc import x, y >>> from sympy.polys.polyclasses import DMP >>> a = Poly(x**2 + 1) >>> a.per(DMP([ZZ(1), ZZ(1)], ZZ), gens=[y]) Poly(y + 1, y, domain='ZZ') """ if gens is None: gens = f.gens if remove is not None: gens = gens[:remove] + gens[remove + 1:] if not gens: return f.rep.dom.to_sympy(rep) return f.__class__.new(rep, *gens) def set_domain(f, domain): """Set the ground domain of ``f``. """ opt = options.build_options(f.gens, {'domain': domain}) return f.per(f.rep.convert(opt.domain)) def get_domain(f): """Get the ground domain of ``f``. """ return f.rep.dom def set_modulus(f, modulus): """ Set the modulus of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(5*x**2 + 2*x - 1, x).set_modulus(2) Poly(x**2 + 1, x, modulus=2) """ modulus = options.Modulus.preprocess(modulus) return f.set_domain(FF(modulus)) def get_modulus(f): """ Get the modulus of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, modulus=2).get_modulus() 2 """ domain = f.get_domain() if domain.is_FiniteField: return Integer(domain.characteristic()) else: raise PolynomialError("not a polynomial over a Galois field") def _eval_subs(f, old, new): """Internal implementation of :func:`subs`. """ if old in f.gens: if new.is_number: return f.eval(old, new) else: try: return f.replace(old, new) except PolynomialError: pass return f.as_expr().subs(old, new) def exclude(f): """ Remove unnecessary generators from ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import a, b, c, d, x >>> Poly(a + x, a, b, c, d, x).exclude() Poly(a + x, a, x, domain='ZZ') """ J, new = f.rep.exclude() gens = [] for j in range(len(f.gens)): if j not in J: gens.append(f.gens[j]) return f.per(new, gens=gens) def replace(f, x, y=None): """ Replace ``x`` with ``y`` in generators list. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 1, x).replace(x, y) Poly(y**2 + 1, y, domain='ZZ') """ if y is None: if f.is_univariate: x, y = f.gen, x else: raise PolynomialError( "syntax supported only in univariate case") if x == y: return f if x in f.gens and y not in f.gens: dom = f.get_domain() if not dom.is_Composite or y not in dom.gens: gens = list(f.gens) gens[gens.index(x)] = y return f.per(f.rep, gens=gens) raise PolynomialError("can't replace %s with %s in %s" % (x, y, f)) def reorder(f, *gens, **args): """ Efficiently apply new order of generators. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + x*y**2, x, y).reorder(y, x) Poly(y**2*x + x**2, y, x, domain='ZZ') """ opt = options.Options((), args) if not gens: gens = _sort_gens(f.gens, opt=opt) elif set(f.gens) != set(gens): raise PolynomialError( "generators list can differ only up to order of elements") rep = dict(zip(*_dict_reorder(f.rep.to_dict(), f.gens, gens))) return f.per(DMP(rep, f.rep.dom, len(gens) - 1), gens=gens) def ltrim(f, gen): """ Remove dummy generators from the "left" of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y, z >>> Poly(y**2 + y*z**2, x, y, z).ltrim(y) Poly(y**2 + y*z**2, y, z, domain='ZZ') """ rep = f.as_dict(native=True) j = f._gen_to_level(gen) terms = {} for monom, coeff in rep.iteritems(): monom = monom[j:] if monom not in terms: terms[monom] = coeff else: raise PolynomialError("can't left trim %s" % f) gens = f.gens[j:] return f.new(DMP.from_dict(terms, len(gens) - 1, f.rep.dom), *gens) def has_only_gens(f, *gens): """ Return ``True`` if ``Poly(f, *gens)`` retains ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y, z >>> Poly(x*y + 1, x, y, z).has_only_gens(x, y) True >>> Poly(x*y + z, x, y, z).has_only_gens(x, y) False """ f_gens = list(f.gens) indices = set([]) for gen in gens: try: index = f_gens.index(gen) except ValueError: raise GeneratorsError( "%s doesn't have %s as generator" % (f, gen)) else: indices.add(index) for monom in f.monoms(): for i, elt in enumerate(monom): if i not in indices and elt: return False return True def to_ring(f): """ Make the ground domain a ring. Examples ======== >>> from sympy import Poly, QQ >>> from sympy.abc import x >>> Poly(x**2 + 1, domain=QQ).to_ring() Poly(x**2 + 1, x, domain='ZZ') """ if hasattr(f.rep, 'to_ring'): result = f.rep.to_ring() else: # pragma: no cover raise OperationNotSupported(f, 'to_ring') return f.per(result) def to_field(f): """ Make the ground domain a field. Examples ======== >>> from sympy import Poly, ZZ >>> from sympy.abc import x >>> Poly(x**2 + 1, x, domain=ZZ).to_field() Poly(x**2 + 1, x, domain='QQ') """ if hasattr(f.rep, 'to_field'): result = f.rep.to_field() else: # pragma: no cover raise OperationNotSupported(f, 'to_field') return f.per(result) def to_exact(f): """ Make the ground domain exact. Examples ======== >>> from sympy import Poly, RR >>> from sympy.abc import x >>> Poly(x**2 + 1.0, x, domain=RR).to_exact() Poly(x**2 + 1, x, domain='QQ') """ if hasattr(f.rep, 'to_exact'): result = f.rep.to_exact() else: # pragma: no cover raise OperationNotSupported(f, 'to_exact') return f.per(result) def retract(f, field=None): """ Recalculate the ground domain of a polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = Poly(x**2 + 1, x, domain='QQ[y]') >>> f Poly(x**2 + 1, x, domain='QQ[y]') >>> f.retract() Poly(x**2 + 1, x, domain='ZZ') >>> f.retract(field=True) Poly(x**2 + 1, x, domain='QQ') """ dom, rep = construct_domain(f.as_dict(zero=True), field=field) return f.from_dict(rep, f.gens, domain=dom) def slice(f, x, m, n=None): """Take a continuous subsequence of terms of ``f``. """ if n is None: j, m, n = 0, x, m else: j = f._gen_to_level(x) m, n = int(m), int(n) if hasattr(f.rep, 'slice'): result = f.rep.slice(m, n, j) else: # pragma: no cover raise OperationNotSupported(f, 'slice') return f.per(result) def coeffs(f, order=None): """ Returns all non-zero coefficients from ``f`` in lex order. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x + 3, x).coeffs() [1, 2, 3] See Also ======== all_coeffs coeff_monomial nth """ return [ f.rep.dom.to_sympy(c) for c in f.rep.coeffs(order=order) ] def monoms(f, order=None): """ Returns all non-zero monomials from ``f`` in lex order. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).monoms() [(2, 0), (1, 2), (1, 1), (0, 1)] See Also ======== all_monoms """ return f.rep.monoms(order=order) def terms(f, order=None): """ Returns all non-zero terms from ``f`` in lex order. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).terms() [((2, 0), 1), ((1, 2), 2), ((1, 1), 1), ((0, 1), 3)] See Also ======== all_terms """ return [ (m, f.rep.dom.to_sympy(c)) for m, c in f.rep.terms(order=order) ] def all_coeffs(f): """ Returns all coefficients from a univariate polynomial ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x - 1, x).all_coeffs() [1, 0, 2, -1] """ return [ f.rep.dom.to_sympy(c) for c in f.rep.all_coeffs() ] def all_monoms(f): """ Returns all monomials from a univariate polynomial ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x - 1, x).all_monoms() [(3,), (2,), (1,), (0,)] See Also ======== all_terms """ return f.rep.all_monoms() def all_terms(f): """ Returns all terms from a univariate polynomial ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x - 1, x).all_terms() [((3,), 1), ((2,), 0), ((1,), 2), ((0,), -1)] """ return [ (m, f.rep.dom.to_sympy(c)) for m, c in f.rep.all_terms() ] def termwise(f, func, *gens, **args): """ Apply a function to all terms of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> def func((k,), coeff): ... return coeff//10**(2-k) >>> Poly(x**2 + 20*x + 400).termwise(func) Poly(x**2 + 2*x + 4, x, domain='ZZ') """ terms = {} for monom, coeff in f.terms(): result = func(monom, coeff) if isinstance(result, tuple): monom, coeff = result else: coeff = result if coeff: if monom not in terms: terms[monom] = coeff else: raise PolynomialError( "%s monomial was generated twice" % monom) return f.from_dict(terms, *(gens or f.gens), **args) def length(f): """ Returns the number of non-zero terms in ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 2*x - 1).length() 3 """ return len(f.as_dict()) def as_dict(f, native=False, zero=False): """ Switch to a ``dict`` representation. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 2*x*y**2 - y, x, y).as_dict() {(0, 1): -1, (1, 2): 2, (2, 0): 1} """ if native: return f.rep.to_dict(zero=zero) else: return f.rep.to_sympy_dict(zero=zero) def as_list(f, native=False): """Switch to a ``list`` representation. """ if native: return f.rep.to_list() else: return f.rep.to_sympy_list() def as_expr(f, *gens): """ Convert a Poly instance to an Expr instance. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = Poly(x**2 + 2*x*y**2 - y, x, y) >>> f.as_expr() x**2 + 2*x*y**2 - y >>> f.as_expr({x: 5}) 10*y**2 - y + 25 >>> f.as_expr(5, 6) 379 """ if not gens: gens = f.gens elif len(gens) == 1 and isinstance(gens[0], dict): mapping = gens[0] gens = list(f.gens) for gen, value in mapping.iteritems(): try: index = gens.index(gen) except ValueError: raise GeneratorsError( "%s doesn't have %s as generator" % (f, gen)) else: gens[index] = value return basic_from_dict(f.rep.to_sympy_dict(), *gens) def lift(f): """ Convert algebraic coefficients to rationals. Examples ======== >>> from sympy import Poly, I >>> from sympy.abc import x >>> Poly(x**2 + I*x + 1, x, extension=I).lift() Poly(x**4 + 3*x**2 + 1, x, domain='QQ') """ if hasattr(f.rep, 'lift'): result = f.rep.lift() else: # pragma: no cover raise OperationNotSupported(f, 'lift') return f.per(result) def deflate(f): """ Reduce degree of ``f`` by mapping ``x_i**m`` to ``y_i``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**6*y**2 + x**3 + 1, x, y).deflate() ((3, 2), Poly(x**2*y + x + 1, x, y, domain='ZZ')) """ if hasattr(f.rep, 'deflate'): J, result = f.rep.deflate() else: # pragma: no cover raise OperationNotSupported(f, 'deflate') return J, f.per(result) def inject(f, front=False): """ Inject ground domain generators into ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = Poly(x**2*y + x*y**3 + x*y + 1, x) >>> f.inject() Poly(x**2*y + x*y**3 + x*y + 1, x, y, domain='ZZ') >>> f.inject(front=True) Poly(y**3*x + y*x**2 + y*x + 1, y, x, domain='ZZ') """ dom = f.rep.dom if dom.is_Numerical: return f elif not dom.is_Poly: raise DomainError("can't inject generators over %s" % dom) if hasattr(f.rep, 'inject'): result = f.rep.inject(front=front) else: # pragma: no cover raise OperationNotSupported(f, 'inject') if front: gens = dom.gens + f.gens else: gens = f.gens + dom.gens return f.new(result, *gens) def eject(f, *gens): """ Eject selected generators into the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = Poly(x**2*y + x*y**3 + x*y + 1, x, y) >>> f.eject(x) Poly(x*y**3 + (x**2 + x)*y + 1, y, domain='ZZ[x]') >>> f.eject(y) Poly(y*x**2 + (y**3 + y)*x + 1, x, domain='ZZ[y]') """ dom = f.rep.dom if not dom.is_Numerical: raise DomainError("can't eject generators over %s" % dom) n, k = len(f.gens), len(gens) if f.gens[:k] == gens: _gens, front = f.gens[k:], True elif f.gens[-k:] == gens: _gens, front = f.gens[:-k], False else: raise NotImplementedError( "can only eject front or back generators") dom = dom.inject(*gens) if hasattr(f.rep, 'eject'): result = f.rep.eject(dom, front=front) else: # pragma: no cover raise OperationNotSupported(f, 'eject') return f.new(result, *_gens) def terms_gcd(f): """ Remove GCD of terms from the polynomial ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**6*y**2 + x**3*y, x, y).terms_gcd() ((3, 1), Poly(x**3*y + 1, x, y, domain='ZZ')) """ if hasattr(f.rep, 'terms_gcd'): J, result = f.rep.terms_gcd() else: # pragma: no cover raise OperationNotSupported(f, 'terms_gcd') return J, f.per(result) def add_ground(f, coeff): """ Add an element of the ground domain to ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x + 1).add_ground(2) Poly(x + 3, x, domain='ZZ') """ if hasattr(f.rep, 'add_ground'): result = f.rep.add_ground(coeff) else: # pragma: no cover raise OperationNotSupported(f, 'add_ground') return f.per(result) def sub_ground(f, coeff): """ Subtract an element of the ground domain from ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x + 1).sub_ground(2) Poly(x - 1, x, domain='ZZ') """ if hasattr(f.rep, 'sub_ground'): result = f.rep.sub_ground(coeff) else: # pragma: no cover raise OperationNotSupported(f, 'sub_ground') return f.per(result) def mul_ground(f, coeff): """ Multiply ``f`` by a an element of the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x + 1).mul_ground(2) Poly(2*x + 2, x, domain='ZZ') """ if hasattr(f.rep, 'mul_ground'): result = f.rep.mul_ground(coeff) else: # pragma: no cover raise OperationNotSupported(f, 'mul_ground') return f.per(result) def quo_ground(f, coeff): """ Quotient of ``f`` by a an element of the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x + 4).quo_ground(2) Poly(x + 2, x, domain='ZZ') >>> Poly(2*x + 3).quo_ground(2) Poly(x + 1, x, domain='ZZ') """ if hasattr(f.rep, 'quo_ground'): result = f.rep.quo_ground(coeff) else: # pragma: no cover raise OperationNotSupported(f, 'quo_ground') return f.per(result) def exquo_ground(f, coeff): """ Exact quotient of ``f`` by a an element of the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x + 4).exquo_ground(2) Poly(x + 2, x, domain='ZZ') >>> Poly(2*x + 3).exquo_ground(2) Traceback (most recent call last): ... ExactQuotientFailed: 2 does not divide 3 in ZZ """ if hasattr(f.rep, 'exquo_ground'): result = f.rep.exquo_ground(coeff) else: # pragma: no cover raise OperationNotSupported(f, 'exquo_ground') return f.per(result) def abs(f): """ Make all coefficients in ``f`` positive. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).abs() Poly(x**2 + 1, x, domain='ZZ') """ if hasattr(f.rep, 'abs'): result = f.rep.abs() else: # pragma: no cover raise OperationNotSupported(f, 'abs') return f.per(result) def neg(f): """ Negate all coefficients in ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).neg() Poly(-x**2 + 1, x, domain='ZZ') >>> -Poly(x**2 - 1, x) Poly(-x**2 + 1, x, domain='ZZ') """ if hasattr(f.rep, 'neg'): result = f.rep.neg() else: # pragma: no cover raise OperationNotSupported(f, 'neg') return f.per(result) def add(f, g): """ Add two polynomials ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).add(Poly(x - 2, x)) Poly(x**2 + x - 1, x, domain='ZZ') >>> Poly(x**2 + 1, x) + Poly(x - 2, x) Poly(x**2 + x - 1, x, domain='ZZ') """ g = sympify(g) if not g.is_Poly: return f.add_ground(g) _, per, F, G = f._unify(g) if hasattr(f.rep, 'add'): result = F.add(G) else: # pragma: no cover raise OperationNotSupported(f, 'add') return per(result) def sub(f, g): """ Subtract two polynomials ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).sub(Poly(x - 2, x)) Poly(x**2 - x + 3, x, domain='ZZ') >>> Poly(x**2 + 1, x) - Poly(x - 2, x) Poly(x**2 - x + 3, x, domain='ZZ') """ g = sympify(g) if not g.is_Poly: return f.sub_ground(g) _, per, F, G = f._unify(g) if hasattr(f.rep, 'sub'): result = F.sub(G) else: # pragma: no cover raise OperationNotSupported(f, 'sub') return per(result) def mul(f, g): """ Multiply two polynomials ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).mul(Poly(x - 2, x)) Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ') >>> Poly(x**2 + 1, x)*Poly(x - 2, x) Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ') """ g = sympify(g) if not g.is_Poly: return f.mul_ground(g) _, per, F, G = f._unify(g) if hasattr(f.rep, 'mul'): result = F.mul(G) else: # pragma: no cover raise OperationNotSupported(f, 'mul') return per(result) def sqr(f): """ Square a polynomial ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x - 2, x).sqr() Poly(x**2 - 4*x + 4, x, domain='ZZ') >>> Poly(x - 2, x)**2 Poly(x**2 - 4*x + 4, x, domain='ZZ') """ if hasattr(f.rep, 'sqr'): result = f.rep.sqr() else: # pragma: no cover raise OperationNotSupported(f, 'sqr') return f.per(result) def pow(f, n): """ Raise ``f`` to a non-negative power ``n``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x - 2, x).pow(3) Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ') >>> Poly(x - 2, x)**3 Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ') """ n = int(n) if hasattr(f.rep, 'pow'): result = f.rep.pow(n) else: # pragma: no cover raise OperationNotSupported(f, 'pow') return f.per(result) def pdiv(f, g): """ Polynomial pseudo-division of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).pdiv(Poly(2*x - 4, x)) (Poly(2*x + 4, x, domain='ZZ'), Poly(20, x, domain='ZZ')) """ _, per, F, G = f._unify(g) if hasattr(f.rep, 'pdiv'): q, r = F.pdiv(G) else: # pragma: no cover raise OperationNotSupported(f, 'pdiv') return per(q), per(r) def prem(f, g): """ Polynomial pseudo-remainder of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).prem(Poly(2*x - 4, x)) Poly(20, x, domain='ZZ') """ _, per, F, G = f._unify(g) if hasattr(f.rep, 'prem'): result = F.prem(G) else: # pragma: no cover raise OperationNotSupported(f, 'prem') return per(result) def pquo(f, g): """ Polynomial pseudo-quotient of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).pquo(Poly(2*x - 4, x)) Poly(2*x + 4, x, domain='ZZ') >>> Poly(x**2 - 1, x).pquo(Poly(2*x - 2, x)) Poly(2*x + 2, x, domain='ZZ') """ _, per, F, G = f._unify(g) if hasattr(f.rep, 'pquo'): result = F.pquo(G) else: # pragma: no cover raise OperationNotSupported(f, 'pquo') return per(result) def pexquo(f, g): """ Polynomial exact pseudo-quotient of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).pexquo(Poly(2*x - 2, x)) Poly(2*x + 2, x, domain='ZZ') >>> Poly(x**2 + 1, x).pexquo(Poly(2*x - 4, x)) Traceback (most recent call last): ... ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1 """ _, per, F, G = f._unify(g) if hasattr(f.rep, 'pexquo'): try: result = F.pexquo(G) except ExactQuotientFailed, exc: raise exc.new(f.as_expr(), g.as_expr()) else: # pragma: no cover raise OperationNotSupported(f, 'pexquo') return per(result) def div(f, g, auto=True): """ Polynomial division with remainder of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).div(Poly(2*x - 4, x)) (Poly(1/2*x + 1, x, domain='QQ'), Poly(5, x, domain='QQ')) >>> Poly(x**2 + 1, x).div(Poly(2*x - 4, x), auto=False) (Poly(0, x, domain='ZZ'), Poly(x**2 + 1, x, domain='ZZ')) """ dom, per, F, G = f._unify(g) retract = False if auto and dom.has_Ring and not dom.has_Field: F, G = F.to_field(), G.to_field() retract = True if hasattr(f.rep, 'div'): q, r = F.div(G) else: # pragma: no cover raise OperationNotSupported(f, 'div') if retract: try: Q, R = q.to_ring(), r.to_ring() except CoercionFailed: pass else: q, r = Q, R return per(q), per(r) def rem(f, g, auto=True): """ Computes the polynomial remainder of ``f`` by ``g``. Examples ======== >>> from sympy import Poly, ZZ, QQ >>> from sympy.abc import x >>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x)) Poly(5, x, domain='ZZ') >>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x), auto=False) Poly(x**2 + 1, x, domain='ZZ') """ dom, per, F, G = f._unify(g) retract = False if auto and dom.has_Ring and not dom.has_Field: F, G = F.to_field(), G.to_field() retract = True if hasattr(f.rep, 'rem'): r = F.rem(G) else: # pragma: no cover raise OperationNotSupported(f, 'rem') if retract: try: r = r.to_ring() except CoercionFailed: pass return per(r) def quo(f, g, auto=True): """ Computes polynomial quotient of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).quo(Poly(2*x - 4, x)) Poly(1/2*x + 1, x, domain='QQ') >>> Poly(x**2 - 1, x).quo(Poly(x - 1, x)) Poly(x + 1, x, domain='ZZ') """ dom, per, F, G = f._unify(g) retract = False if auto and dom.has_Ring and not dom.has_Field: F, G = F.to_field(), G.to_field() retract = True if hasattr(f.rep, 'quo'): q = F.quo(G) else: # pragma: no cover raise OperationNotSupported(f, 'quo') if retract: try: q = q.to_ring() except CoercionFailed: pass return per(q) def exquo(f, g, auto=True): """ Computes polynomial exact quotient of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).exquo(Poly(x - 1, x)) Poly(x + 1, x, domain='ZZ') >>> Poly(x**2 + 1, x).exquo(Poly(2*x - 4, x)) Traceback (most recent call last): ... ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1 """ dom, per, F, G = f._unify(g) retract = False if auto and dom.has_Ring and not dom.has_Field: F, G = F.to_field(), G.to_field() retract = True if hasattr(f.rep, 'exquo'): try: q = F.exquo(G) except ExactQuotientFailed, exc: raise exc.new(f.as_expr(), g.as_expr()) else: # pragma: no cover raise OperationNotSupported(f, 'exquo') if retract: try: q = q.to_ring() except CoercionFailed: pass return per(q) def _gen_to_level(f, gen): """Returns level associated with the given generator. """ if isinstance(gen, int): length = len(f.gens) if -length <= gen < length: if gen < 0: return length + gen else: return gen else: raise PolynomialError("-%s <= gen < %s expected, got %s" % (length, length, gen)) else: try: return list(f.gens).index(sympify(gen)) except ValueError: raise PolynomialError( "a valid generator expected, got %s" % gen) def degree(f, gen=0): """ Returns degree of ``f`` in ``x_j``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + y*x + 1, x, y).degree() 2 >>> Poly(x**2 + y*x + y, x, y).degree(y) 1 """ j = f._gen_to_level(gen) if hasattr(f.rep, 'degree'): return f.rep.degree(j) else: # pragma: no cover raise OperationNotSupported(f, 'degree') def degree_list(f): """ Returns a list of degrees of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + y*x + 1, x, y).degree_list() (2, 1) """ if hasattr(f.rep, 'degree_list'): return f.rep.degree_list() else: # pragma: no cover raise OperationNotSupported(f, 'degree_list') def total_degree(f): """ Returns the total degree of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + y*x + 1, x, y).total_degree() 2 >>> Poly(x + y**5, x, y).total_degree() 5 """ if hasattr(f.rep, 'total_degree'): return f.rep.total_degree() else: # pragma: no cover raise OperationNotSupported(f, 'total_degree') def homogeneous_order(f): """ Returns the homogeneous order of ``f``. A homogeneous polynomial is a polynomial whose all monomials with non-zero coefficients have the same total degree. This degree is the homogeneous order of ``f``. If you only want to check if a polynomial is homogeneous, then use :func:`Poly.is_homogeneous`. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = Poly(x**5 + 2*x**3*y**2 + 9*x*y**4) >>> f.homogeneous_order() 5 """ if hasattr(f.rep, 'homogeneous_order'): return f.rep.homogeneous_order() else: # pragma: no cover raise OperationNotSupported(f, 'homogeneous_order') def LC(f, order=None): """ Returns the leading coefficient of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(4*x**3 + 2*x**2 + 3*x, x).LC() 4 """ if order is not None: return f.coeffs(order)[0] if hasattr(f.rep, 'LC'): result = f.rep.LC() else: # pragma: no cover raise OperationNotSupported(f, 'LC') return f.rep.dom.to_sympy(result) def TC(f): """ Returns the trailing coefficient of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x**2 + 3*x, x).TC() 0 """ if hasattr(f.rep, 'TC'): result = f.rep.TC() else: # pragma: no cover raise OperationNotSupported(f, 'TC') return f.rep.dom.to_sympy(result) def EC(f, order=None): """ Returns the last non-zero coefficient of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x**2 + 3*x, x).EC() 3 """ if hasattr(f.rep, 'coeffs'): return f.coeffs(order)[-1] else: # pragma: no cover raise OperationNotSupported(f, 'EC') def coeff_monomial(f, monom): """ Returns the coefficient of ``monom`` in ``f`` if there, else None. Examples ======== >>> from sympy import Poly, exp >>> from sympy.abc import x, y >>> p = Poly(24*x*y*exp(8) + 23*x, x, y) >>> p.coeff_monomial(x) 23 >>> p.coeff_monomial(y) 0 >>> p.coeff_monomial(x*y) 24*exp(8) Note that ``Expr.coeff()`` behaves differently, collecting terms if possible; the Poly must be converted to an Expr to use that method, however: >>> p.as_expr().coeff(x) 24*y*exp(8) + 23 >>> p.as_expr().coeff(y) 24*x*exp(8) >>> p.as_expr().coeff(x*y) 24*exp(8) See Also ======== nth: more efficient query using exponents of the monomial's generators """ return f.nth(*Monomial(monom, f.gens).exponents) def nth(f, *N): """ Returns the ``n``-th coefficient of ``f`` where ``N`` are the exponents of the generators in the term of interest. Examples ======== >>> from sympy import Poly, sqrt >>> from sympy.abc import x, y >>> Poly(x**3 + 2*x**2 + 3*x, x).nth(2) 2 >>> Poly(x**3 + 2*x*y**2 + y**2, x, y).nth(1, 2) 2 >>> Poly(4*sqrt(x)*y) Poly(4*y*sqrt(x), y, sqrt(x), domain='ZZ') >>> _.nth(1, 1) 4 See Also ======== coeff_monomial """ if hasattr(f.rep, 'nth'): result = f.rep.nth(*map(int, N)) else: # pragma: no cover raise OperationNotSupported(f, 'nth') return f.rep.dom.to_sympy(result) def coeff(f, x, n=1, right=False): # the semantics of coeff_monomial and Expr.coeff are different; # if someone is working with a Poly, they should be aware of the # differences and chose the method best suited for the query. # Alternatively, a pure-polys method could be written here but # at this time the ``right`` keyword would be ignored because Poly # doesn't work with non-commutatives. raise NotImplementedError( 'Either convert to Expr with `as_expr` method ' 'to use Expr\'s coeff method or else use the ' '`coeff_monomial` method of Polys.') def LM(f, order=None): """ Returns the leading monomial of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LM() x**2*y**0 """ return Monomial(f.monoms(order)[0], f.gens) def EM(f, order=None): """ Returns the last non-zero monomial of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).EM() x**0*y**1 """ return Monomial(f.monoms(order)[-1], f.gens) def LT(f, order=None): """ Returns the leading term of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LT() (x**2*y**0, 4) """ monom, coeff = f.terms(order)[0] return Monomial(monom, f.gens), coeff def ET(f, order=None): """ Returns the last non-zero term of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).ET() (x**0*y**1, 3) """ monom, coeff = f.terms(order)[-1] return Monomial(monom, f.gens), coeff def max_norm(f): """ Returns maximum norm of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(-x**2 + 2*x - 3, x).max_norm() 3 """ if hasattr(f.rep, 'max_norm'): result = f.rep.max_norm() else: # pragma: no cover raise OperationNotSupported(f, 'max_norm') return f.rep.dom.to_sympy(result) def l1_norm(f): """ Returns l1 norm of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(-x**2 + 2*x - 3, x).l1_norm() 6 """ if hasattr(f.rep, 'l1_norm'): result = f.rep.l1_norm() else: # pragma: no cover raise OperationNotSupported(f, 'l1_norm') return f.rep.dom.to_sympy(result) def clear_denoms(f, convert=False): """ Clear denominators, but keep the ground domain. Examples ======== >>> from sympy import Poly, S, QQ >>> from sympy.abc import x >>> f = Poly(x/2 + S(1)/3, x, domain=QQ) >>> f.clear_denoms() (6, Poly(3*x + 2, x, domain='QQ')) >>> f.clear_denoms(convert=True) (6, Poly(3*x + 2, x, domain='ZZ')) """ if not f.rep.dom.has_Field: return S.One, f dom = f.get_domain() if dom.has_assoc_Ring: dom = f.rep.dom.get_ring() if hasattr(f.rep, 'clear_denoms'): coeff, result = f.rep.clear_denoms() else: # pragma: no cover raise OperationNotSupported(f, 'clear_denoms') coeff, f = dom.to_sympy(coeff), f.per(result) if not convert: return coeff, f else: return coeff, f.to_ring() def rat_clear_denoms(f, g): """ Clear denominators in a rational function ``f/g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = Poly(x**2/y + 1, x) >>> g = Poly(x**3 + y, x) >>> p, q = f.rat_clear_denoms(g) >>> p Poly(x**2 + y, x, domain='ZZ[y]') >>> q Poly(y*x**3 + y**2, x, domain='ZZ[y]') """ dom, per, f, g = f._unify(g) f = per(f) g = per(g) if not (dom.has_Field and dom.has_assoc_Ring): return f, g a, f = f.clear_denoms(convert=True) b, g = g.clear_denoms(convert=True) f = f.mul_ground(b) g = g.mul_ground(a) return f, g def integrate(f, *specs, **args): """ Computes indefinite integral of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 2*x + 1, x).integrate() Poly(1/3*x**3 + x**2 + x, x, domain='QQ') >>> Poly(x*y**2 + x, x, y).integrate((0, 1), (1, 0)) Poly(1/2*x**2*y**2 + 1/2*x**2, x, y, domain='QQ') """ if args.get('auto', True) and f.rep.dom.has_Ring: f = f.to_field() if hasattr(f.rep, 'integrate'): if not specs: return f.per(f.rep.integrate(m=1)) rep = f.rep for spec in specs: if type(spec) is tuple: gen, m = spec else: gen, m = spec, 1 rep = rep.integrate(int(m), f._gen_to_level(gen)) return f.per(rep) else: # pragma: no cover raise OperationNotSupported(f, 'integrate') def diff(f, *specs): """ Computes partial derivative of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 2*x + 1, x).diff() Poly(2*x + 2, x, domain='ZZ') >>> Poly(x*y**2 + x, x, y).diff((0, 0), (1, 1)) Poly(2*x*y, x, y, domain='ZZ') """ if hasattr(f.rep, 'diff'): if not specs: return f.per(f.rep.diff(m=1)) rep = f.rep for spec in specs: if type(spec) is tuple: gen, m = spec else: gen, m = spec, 1 rep = rep.diff(int(m), f._gen_to_level(gen)) return f.per(rep) else: # pragma: no cover raise OperationNotSupported(f, 'diff') def eval(f, x, a=None, auto=True): """ Evaluate ``f`` at ``a`` in the given variable. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y, z >>> Poly(x**2 + 2*x + 3, x).eval(2) 11 >>> Poly(2*x*y + 3*x + y + 2, x, y).eval(x, 2) Poly(5*y + 8, y, domain='ZZ') >>> f = Poly(2*x*y + 3*x + y + 2*z, x, y, z) >>> f.eval({x: 2}) Poly(5*y + 2*z + 6, y, z, domain='ZZ') >>> f.eval({x: 2, y: 5}) Poly(2*z + 31, z, domain='ZZ') >>> f.eval({x: 2, y: 5, z: 7}) 45 >>> f.eval((2, 5)) Poly(2*z + 31, z, domain='ZZ') >>> f(2, 5) Poly(2*z + 31, z, domain='ZZ') """ if a is None: if isinstance(x, dict): mapping = x for gen, value in mapping.iteritems(): f = f.eval(gen, value) return f elif isinstance(x, (tuple, list)): values = x if len(values) > len(f.gens): raise ValueError("too many values provided") for gen, value in zip(f.gens, values): f = f.eval(gen, value) return f else: j, a = 0, x else: j = f._gen_to_level(x) if not hasattr(f.rep, 'eval'): # pragma: no cover raise OperationNotSupported(f, 'eval') try: result = f.rep.eval(a, j) except CoercionFailed: if not auto: raise DomainError("can't evaluate at %s in %s" % (a, f.rep.dom)) else: a_domain, [a] = construct_domain([a]) new_domain = f.get_domain().unify(a_domain, gens=f.gens) f = f.set_domain(new_domain) a = new_domain.convert(a, a_domain) result = f.rep.eval(a, j) return f.per(result, remove=j) def __call__(f, *values): """ Evaluate ``f`` at the give values. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y, z >>> f = Poly(2*x*y + 3*x + y + 2*z, x, y, z) >>> f(2) Poly(5*y + 2*z + 6, y, z, domain='ZZ') >>> f(2, 5) Poly(2*z + 31, z, domain='ZZ') >>> f(2, 5, 7) 45 """ return f.eval(values) def half_gcdex(f, g, auto=True): """ Half extended Euclidean algorithm of ``f`` and ``g``. Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15 >>> g = x**3 + x**2 - 4*x - 4 >>> Poly(f).half_gcdex(Poly(g)) (Poly(-1/5*x + 3/5, x, domain='QQ'), Poly(x + 1, x, domain='QQ')) """ dom, per, F, G = f._unify(g) if auto and dom.has_Ring: F, G = F.to_field(), G.to_field() if hasattr(f.rep, 'half_gcdex'): s, h = F.half_gcdex(G) else: # pragma: no cover raise OperationNotSupported(f, 'half_gcdex') return per(s), per(h) def gcdex(f, g, auto=True): """ Extended Euclidean algorithm of ``f`` and ``g``. Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15 >>> g = x**3 + x**2 - 4*x - 4 >>> Poly(f).gcdex(Poly(g)) (Poly(-1/5*x + 3/5, x, domain='QQ'), Poly(1/5*x**2 - 6/5*x + 2, x, domain='QQ'), Poly(x + 1, x, domain='QQ')) """ dom, per, F, G = f._unify(g) if auto and dom.has_Ring: F, G = F.to_field(), G.to_field() if hasattr(f.rep, 'gcdex'): s, t, h = F.gcdex(G) else: # pragma: no cover raise OperationNotSupported(f, 'gcdex') return per(s), per(t), per(h) def invert(f, g, auto=True): """ Invert ``f`` modulo ``g`` when possible. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).invert(Poly(2*x - 1, x)) Poly(-4/3, x, domain='QQ') >>> Poly(x**2 - 1, x).invert(Poly(x - 1, x)) Traceback (most recent call last): ... NotInvertible: zero divisor """ dom, per, F, G = f._unify(g) if auto and dom.has_Ring: F, G = F.to_field(), G.to_field() if hasattr(f.rep, 'invert'): result = F.invert(G) else: # pragma: no cover raise OperationNotSupported(f, 'invert') return per(result) def revert(f, n): """Compute ``f**(-1)`` mod ``x**n``. """ if hasattr(f.rep, 'revert'): result = f.rep.revert(int(n)) else: # pragma: no cover raise OperationNotSupported(f, 'revert') return f.per(result) def subresultants(f, g): """ Computes the subresultant PRS of ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).subresultants(Poly(x**2 - 1, x)) [Poly(x**2 + 1, x, domain='ZZ'), Poly(x**2 - 1, x, domain='ZZ'), Poly(-2, x, domain='ZZ')] """ _, per, F, G = f._unify(g) if hasattr(f.rep, 'subresultants'): result = F.subresultants(G) else: # pragma: no cover raise OperationNotSupported(f, 'subresultants') return map(per, result) def resultant(f, g, includePRS=False): """ Computes the resultant of ``f`` and ``g`` via PRS. If includePRS=True, it includes the subresultant PRS in the result. Because the PRS is used to calculate the resultant, this is more efficient than calling :func:`subresultants` separately. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = Poly(x**2 + 1, x) >>> f.resultant(Poly(x**2 - 1, x)) 4 >>> f.resultant(Poly(x**2 - 1, x), includePRS=True) (4, [Poly(x**2 + 1, x, domain='ZZ'), Poly(x**2 - 1, x, domain='ZZ'), Poly(-2, x, domain='ZZ')]) """ _, per, F, G = f._unify(g) if hasattr(f.rep, 'resultant'): if includePRS: result, R = F.resultant(G, includePRS=includePRS) else: result = F.resultant(G) else: # pragma: no cover raise OperationNotSupported(f, 'resultant') if includePRS: return (per(result, remove=0), map(per, R)) return per(result, remove=0) def discriminant(f): """ Computes the discriminant of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 2*x + 3, x).discriminant() -8 """ if hasattr(f.rep, 'discriminant'): result = f.rep.discriminant() else: # pragma: no cover raise OperationNotSupported(f, 'discriminant') return f.per(result, remove=0) def cofactors(f, g): """ Returns the GCD of ``f`` and ``g`` and their cofactors. Returns polynomials ``(h, cff, cfg)`` such that ``h = gcd(f, g)``, and ``cff = quo(f, h)`` and ``cfg = quo(g, h)`` are, so called, cofactors of ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).cofactors(Poly(x**2 - 3*x + 2, x)) (Poly(x - 1, x, domain='ZZ'), Poly(x + 1, x, domain='ZZ'), Poly(x - 2, x, domain='ZZ')) """ _, per, F, G = f._unify(g) if hasattr(f.rep, 'cofactors'): h, cff, cfg = F.cofactors(G) else: # pragma: no cover raise OperationNotSupported(f, 'cofactors') return per(h), per(cff), per(cfg) def gcd(f, g): """ Returns the polynomial GCD of ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).gcd(Poly(x**2 - 3*x + 2, x)) Poly(x - 1, x, domain='ZZ') """ _, per, F, G = f._unify(g) if hasattr(f.rep, 'gcd'): result = F.gcd(G) else: # pragma: no cover raise OperationNotSupported(f, 'gcd') return per(result) def lcm(f, g): """ Returns polynomial LCM of ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).lcm(Poly(x**2 - 3*x + 2, x)) Poly(x**3 - 2*x**2 - x + 2, x, domain='ZZ') """ _, per, F, G = f._unify(g) if hasattr(f.rep, 'lcm'): result = F.lcm(G) else: # pragma: no cover raise OperationNotSupported(f, 'lcm') return per(result) def trunc(f, p): """ Reduce ``f`` modulo a constant ``p``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**3 + 3*x**2 + 5*x + 7, x).trunc(3) Poly(-x**3 - x + 1, x, domain='ZZ') """ p = f.rep.dom.convert(p) if hasattr(f.rep, 'trunc'): result = f.rep.trunc(p) else: # pragma: no cover raise OperationNotSupported(f, 'trunc') return f.per(result) def monic(f, auto=True): """ Divides all coefficients by ``LC(f)``. Examples ======== >>> from sympy import Poly, ZZ >>> from sympy.abc import x >>> Poly(3*x**2 + 6*x + 9, x, domain=ZZ).monic() Poly(x**2 + 2*x + 3, x, domain='QQ') >>> Poly(3*x**2 + 4*x + 2, x, domain=ZZ).monic() Poly(x**2 + 4/3*x + 2/3, x, domain='QQ') """ if auto and f.rep.dom.has_Ring: f = f.to_field() if hasattr(f.rep, 'monic'): result = f.rep.monic() else: # pragma: no cover raise OperationNotSupported(f, 'monic') return f.per(result) def content(f): """ Returns the GCD of polynomial coefficients. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(6*x**2 + 8*x + 12, x).content() 2 """ if hasattr(f.rep, 'content'): result = f.rep.content() else: # pragma: no cover raise OperationNotSupported(f, 'content') return f.rep.dom.to_sympy(result) def primitive(f): """ Returns the content and a primitive form of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**2 + 8*x + 12, x).primitive() (2, Poly(x**2 + 4*x + 6, x, domain='ZZ')) """ if hasattr(f.rep, 'primitive'): cont, result = f.rep.primitive() else: # pragma: no cover raise OperationNotSupported(f, 'primitive') return f.rep.dom.to_sympy(cont), f.per(result) def compose(f, g): """ Computes the functional composition of ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + x, x).compose(Poly(x - 1, x)) Poly(x**2 - x, x, domain='ZZ') """ _, per, F, G = f._unify(g) if hasattr(f.rep, 'compose'): result = F.compose(G) else: # pragma: no cover raise OperationNotSupported(f, 'compose') return per(result) def decompose(f): """ Computes a functional decomposition of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**4 + 2*x**3 - x - 1, x, domain='ZZ').decompose() [Poly(x**2 - x - 1, x, domain='ZZ'), Poly(x**2 + x, x, domain='ZZ')] """ if hasattr(f.rep, 'decompose'): result = f.rep.decompose() else: # pragma: no cover raise OperationNotSupported(f, 'decompose') return map(f.per, result) def shift(f, a): """ Efficiently compute Taylor shift ``f(x + a)``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 2*x + 1, x).shift(2) Poly(x**2 + 2*x + 1, x, domain='ZZ') """ if hasattr(f.rep, 'shift'): result = f.rep.shift(a) else: # pragma: no cover raise OperationNotSupported(f, 'shift') return f.per(result) def sturm(f, auto=True): """ Computes the Sturm sequence of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 - 2*x**2 + x - 3, x).sturm() [Poly(x**3 - 2*x**2 + x - 3, x, domain='QQ'), Poly(3*x**2 - 4*x + 1, x, domain='QQ'), Poly(2/9*x + 25/9, x, domain='QQ'), Poly(-2079/4, x, domain='QQ')] """ if auto and f.rep.dom.has_Ring: f = f.to_field() if hasattr(f.rep, 'sturm'): result = f.rep.sturm() else: # pragma: no cover raise OperationNotSupported(f, 'sturm') return map(f.per, result) def gff_list(f): """ Computes greatest factorial factorization of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = x**5 + 2*x**4 - x**3 - 2*x**2 >>> Poly(f).gff_list() [(Poly(x, x, domain='ZZ'), 1), (Poly(x + 2, x, domain='ZZ'), 4)] """ if hasattr(f.rep, 'gff_list'): result = f.rep.gff_list() else: # pragma: no cover raise OperationNotSupported(f, 'gff_list') return [ (f.per(g), k) for g, k in result ] def sqf_norm(f): """ Computes square-free norm of ``f``. Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and ``r(x) = Norm(g(x))`` is a square-free polynomial over ``K``, where ``a`` is the algebraic extension of the ground domain. Examples ======== >>> from sympy import Poly, sqrt >>> from sympy.abc import x >>> s, f, r = Poly(x**2 + 1, x, extension=[sqrt(3)]).sqf_norm() >>> s 1 >>> f Poly(x**2 - 2*sqrt(3)*x + 4, x, domain='QQ<sqrt(3)>') >>> r Poly(x**4 - 4*x**2 + 16, x, domain='QQ') """ if hasattr(f.rep, 'sqf_norm'): s, g, r = f.rep.sqf_norm() else: # pragma: no cover raise OperationNotSupported(f, 'sqf_norm') return s, f.per(g), f.per(r) def sqf_part(f): """ Computes square-free part of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 - 3*x - 2, x).sqf_part() Poly(x**2 - x - 2, x, domain='ZZ') """ if hasattr(f.rep, 'sqf_part'): result = f.rep.sqf_part() else: # pragma: no cover raise OperationNotSupported(f, 'sqf_part') return f.per(result) def sqf_list(f, all=False): """ Returns a list of square-free factors of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16 >>> Poly(f).sqf_list() (2, [(Poly(x + 1, x, domain='ZZ'), 2), (Poly(x + 2, x, domain='ZZ'), 3)]) >>> Poly(f).sqf_list(all=True) (2, [(Poly(1, x, domain='ZZ'), 1), (Poly(x + 1, x, domain='ZZ'), 2), (Poly(x + 2, x, domain='ZZ'), 3)]) """ if hasattr(f.rep, 'sqf_list'): coeff, factors = f.rep.sqf_list(all) else: # pragma: no cover raise OperationNotSupported(f, 'sqf_list') return f.rep.dom.to_sympy(coeff), [ (f.per(g), k) for g, k in factors ] def sqf_list_include(f, all=False): """ Returns a list of square-free factors of ``f``. Examples ======== >>> from sympy import Poly, expand >>> from sympy.abc import x >>> f = expand(2*(x + 1)**3*x**4) >>> f 2*x**7 + 6*x**6 + 6*x**5 + 2*x**4 >>> Poly(f).sqf_list_include() [(Poly(2, x, domain='ZZ'), 1), (Poly(x + 1, x, domain='ZZ'), 3), (Poly(x, x, domain='ZZ'), 4)] >>> Poly(f).sqf_list_include(all=True) [(Poly(2, x, domain='ZZ'), 1), (Poly(1, x, domain='ZZ'), 2), (Poly(x + 1, x, domain='ZZ'), 3), (Poly(x, x, domain='ZZ'), 4)] """ if hasattr(f.rep, 'sqf_list_include'): factors = f.rep.sqf_list_include(all) else: # pragma: no cover raise OperationNotSupported(f, 'sqf_list_include') return [ (f.per(g), k) for g, k in factors ] def factor_list(f): """ Returns a list of irreducible factors of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y >>> Poly(f).factor_list() (2, [(Poly(x + y, x, y, domain='ZZ'), 1), (Poly(x**2 + 1, x, y, domain='ZZ'), 2)]) """ if hasattr(f.rep, 'factor_list'): try: coeff, factors = f.rep.factor_list() except DomainError: return S.One, [(f, 1)] else: # pragma: no cover raise OperationNotSupported(f, 'factor_list') return f.rep.dom.to_sympy(coeff), [ (f.per(g), k) for g, k in factors ] def factor_list_include(f): """ Returns a list of irreducible factors of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y >>> Poly(f).factor_list_include() [(Poly(2*x + 2*y, x, y, domain='ZZ'), 1), (Poly(x**2 + 1, x, y, domain='ZZ'), 2)] """ if hasattr(f.rep, 'factor_list_include'): try: factors = f.rep.factor_list_include() except DomainError: return [(f, 1)] else: # pragma: no cover raise OperationNotSupported(f, 'factor_list_include') return [ (f.per(g), k) for g, k in factors ] def intervals(f, all=False, eps=None, inf=None, sup=None, fast=False, sqf=False): """ Compute isolating intervals for roots of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 3, x).intervals() [((-2, -1), 1), ((1, 2), 1)] >>> Poly(x**2 - 3, x).intervals(eps=1e-2) [((-26/15, -19/11), 1), ((19/11, 26/15), 1)] """ if eps is not None: eps = QQ.convert(eps) if eps <= 0: raise ValueError("'eps' must be a positive rational") if inf is not None: inf = QQ.convert(inf) if sup is not None: sup = QQ.convert(sup) if hasattr(f.rep, 'intervals'): result = f.rep.intervals( all=all, eps=eps, inf=inf, sup=sup, fast=fast, sqf=sqf) else: # pragma: no cover raise OperationNotSupported(f, 'intervals') if sqf: def _real(interval): s, t = interval return (QQ.to_sympy(s), QQ.to_sympy(t)) if not all: return map(_real, result) def _complex(rectangle): (u, v), (s, t) = rectangle return (QQ.to_sympy(u) + I*QQ.to_sympy(v), QQ.to_sympy(s) + I*QQ.to_sympy(t)) real_part, complex_part = result return map(_real, real_part), map(_complex, complex_part) else: def _real(interval): (s, t), k = interval return ((QQ.to_sympy(s), QQ.to_sympy(t)), k) if not all: return map(_real, result) def _complex(rectangle): ((u, v), (s, t)), k = rectangle return ((QQ.to_sympy(u) + I*QQ.to_sympy(v), QQ.to_sympy(s) + I*QQ.to_sympy(t)), k) real_part, complex_part = result return map(_real, real_part), map(_complex, complex_part) def refine_root(f, s, t, eps=None, steps=None, fast=False, check_sqf=False): """ Refine an isolating interval of a root to the given precision. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 3, x).refine_root(1, 2, eps=1e-2) (19/11, 26/15) """ if check_sqf and not f.is_sqf: raise PolynomialError("only square-free polynomials supported") s, t = QQ.convert(s), QQ.convert(t) if eps is not None: eps = QQ.convert(eps) if eps <= 0: raise ValueError("'eps' must be a positive rational") if steps is not None: steps = int(steps) elif eps is None: steps = 1 if hasattr(f.rep, 'refine_root'): S, T = f.rep.refine_root(s, t, eps=eps, steps=steps, fast=fast) else: # pragma: no cover raise OperationNotSupported(f, 'refine_root') return QQ.to_sympy(S), QQ.to_sympy(T) def count_roots(f, inf=None, sup=None): """ Return the number of roots of ``f`` in ``[inf, sup]`` interval. Examples ======== >>> from sympy import Poly, I >>> from sympy.abc import x >>> Poly(x**4 - 4, x).count_roots(-3, 3) 2 >>> Poly(x**4 - 4, x).count_roots(0, 1 + 3*I) 1 """ inf_real, sup_real = True, True if inf is not None: inf = sympify(inf) if inf is S.NegativeInfinity: inf = None else: re, im = inf.as_real_imag() if not im: inf = QQ.convert(inf) else: inf, inf_real = map(QQ.convert, (re, im)), False if sup is not None: sup = sympify(sup) if sup is S.Infinity: sup = None else: re, im = sup.as_real_imag() if not im: sup = QQ.convert(sup) else: sup, sup_real = map(QQ.convert, (re, im)), False if inf_real and sup_real: if hasattr(f.rep, 'count_real_roots'): count = f.rep.count_real_roots(inf=inf, sup=sup) else: # pragma: no cover raise OperationNotSupported(f, 'count_real_roots') else: if inf_real and inf is not None: inf = (inf, QQ.zero) if sup_real and sup is not None: sup = (sup, QQ.zero) if hasattr(f.rep, 'count_complex_roots'): count = f.rep.count_complex_roots(inf=inf, sup=sup) else: # pragma: no cover raise OperationNotSupported(f, 'count_complex_roots') return Integer(count) def root(f, index, radicals=True): """ Get an indexed root of a polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = Poly(2*x**3 - 7*x**2 + 4*x + 4) >>> f.root(0) -1/2 >>> f.root(1) 2 >>> f.root(2) 2 >>> f.root(3) Traceback (most recent call last): ... IndexError: root index out of [-3, 2] range, got 3 >>> Poly(x**5 + x + 1).root(0) RootOf(x**3 - x**2 + 1, 0) """ return sympy.polys.rootoftools.RootOf(f, index, radicals=radicals) def real_roots(f, multiple=True, radicals=True): """ Return a list of real roots with multiplicities. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**3 - 7*x**2 + 4*x + 4).real_roots() [-1/2, 2, 2] >>> Poly(x**3 + x + 1).real_roots() [RootOf(x**3 + x + 1, 0)] """ reals = sympy.polys.rootoftools.RootOf.real_roots(f, radicals=radicals) if multiple: return reals else: return group(reals, multiple=False) def all_roots(f, multiple=True, radicals=True): """ Return a list of real and complex roots with multiplicities. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**3 - 7*x**2 + 4*x + 4).all_roots() [-1/2, 2, 2] >>> Poly(x**3 + x + 1).all_roots() [RootOf(x**3 + x + 1, 0), RootOf(x**3 + x + 1, 1), RootOf(x**3 + x + 1, 2)] """ roots = sympy.polys.rootoftools.RootOf.all_roots(f, radicals=radicals) if multiple: return roots else: return group(roots, multiple=False) def nroots(f, n=15, maxsteps=50, cleanup=True, error=False): """ Compute numerical approximations of roots of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 3).nroots(n=15) [-1.73205080756888, 1.73205080756888] >>> Poly(x**2 - 3).nroots(n=30) [-1.73205080756887729352744634151, 1.73205080756887729352744634151] """ if f.is_multivariate: raise MultivariatePolynomialError( "can't compute numerical roots of %s" % f) if f.degree() <= 0: return [] coeffs = [ coeff.evalf(n=n).as_real_imag() for coeff in f.all_coeffs() ] dps = sympy.mpmath.mp.dps sympy.mpmath.mp.dps = n try: try: coeffs = [ sympy.mpmath.mpc(*coeff) for coeff in coeffs ] except TypeError: raise DomainError( "numerical domain expected, got %s" % f.rep.dom) result = sympy.mpmath.polyroots( coeffs, maxsteps=maxsteps, cleanup=cleanup, error=error) if error: roots, error = result else: roots, error = result, None roots = map(sympify, sorted(roots, key=lambda r: (r.real, r.imag))) finally: sympy.mpmath.mp.dps = dps if error is not None: return roots, sympify(error) else: return roots def ground_roots(f): """ Compute roots of ``f`` by factorization in the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**6 - 4*x**4 + 4*x**3 - x**2).ground_roots() {0: 2, 1: 2} """ if f.is_multivariate: raise MultivariatePolynomialError( "can't compute ground roots of %s" % f) roots = {} for factor, k in f.factor_list()[1]: if factor.is_linear: a, b = factor.all_coeffs() roots[-b/a] = k return roots def nth_power_roots_poly(f, n): """ Construct a polynomial with n-th powers of roots of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = Poly(x**4 - x**2 + 1) >>> f.nth_power_roots_poly(2) Poly(x**4 - 2*x**3 + 3*x**2 - 2*x + 1, x, domain='ZZ') >>> f.nth_power_roots_poly(3) Poly(x**4 + 2*x**2 + 1, x, domain='ZZ') >>> f.nth_power_roots_poly(4) Poly(x**4 + 2*x**3 + 3*x**2 + 2*x + 1, x, domain='ZZ') >>> f.nth_power_roots_poly(12) Poly(x**4 - 4*x**3 + 6*x**2 - 4*x + 1, x, domain='ZZ') """ if f.is_multivariate: raise MultivariatePolynomialError( "must be a univariate polynomial") N = sympify(n) if N.is_Integer and N >= 1: n = int(N) else: raise ValueError("'n' must an integer and n >= 1, got %s" % n) x = f.gen t = Dummy('t') r = f.resultant(f.__class__.from_expr(x**n - t, x, t)) return r.replace(t, x) def cancel(f, g, include=False): """ Cancel common factors in a rational function ``f/g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x)) (1, Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ')) >>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x), include=True) (Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ')) """ dom, per, F, G = f._unify(g) if hasattr(F, 'cancel'): result = F.cancel(G, include=include) else: # pragma: no cover raise OperationNotSupported(f, 'cancel') if not include: if dom.has_assoc_Ring: dom = dom.get_ring() cp, cq, p, q = result cp = dom.to_sympy(cp) cq = dom.to_sympy(cq) return cp/cq, per(p), per(q) else: return tuple(map(per, result)) @property def is_zero(f): """ Returns ``True`` if ``f`` is a zero polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(0, x).is_zero True >>> Poly(1, x).is_zero False """ return f.rep.is_zero @property def is_one(f): """ Returns ``True`` if ``f`` is a unit polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(0, x).is_one False >>> Poly(1, x).is_one True """ return f.rep.is_one @property def is_sqf(f): """ Returns ``True`` if ``f`` is a square-free polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 2*x + 1, x).is_sqf False >>> Poly(x**2 - 1, x).is_sqf True """ return f.rep.is_sqf @property def is_monic(f): """ Returns ``True`` if the leading coefficient of ``f`` is one. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x + 2, x).is_monic True >>> Poly(2*x + 2, x).is_monic False """ return f.rep.is_monic @property def is_primitive(f): """ Returns ``True`` if GCD of the coefficients of ``f`` is one. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**2 + 6*x + 12, x).is_primitive False >>> Poly(x**2 + 3*x + 6, x).is_primitive True """ return f.rep.is_primitive @property def is_ground(f): """ Returns ``True`` if ``f`` is an element of the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x, x).is_ground False >>> Poly(2, x).is_ground True >>> Poly(y, x).is_ground True """ return f.rep.is_ground @property def is_linear(f): """ Returns ``True`` if ``f`` is linear in all its variables. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x + y + 2, x, y).is_linear True >>> Poly(x*y + 2, x, y).is_linear False """ return f.rep.is_linear @property def is_quadratic(f): """ Returns ``True`` if ``f`` is quadratic in all its variables. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x*y + 2, x, y).is_quadratic True >>> Poly(x*y**2 + 2, x, y).is_quadratic False """ return f.rep.is_quadratic @property def is_monomial(f): """ Returns ``True`` if ``f`` is zero or has only one term. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(3*x**2, x).is_monomial True >>> Poly(3*x**2 + 1, x).is_monomial False """ return f.rep.is_monomial @property def is_homogeneous(f): """ Returns ``True`` if ``f`` is a homogeneous polynomial. A homogeneous polynomial is a polynomial whose all monomials with non-zero coefficients have the same total degree. If you want not only to check if a polynomial is homogeneous but also compute its homogeneous order, then use :func:`Poly.homogeneous_order`. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + x*y, x, y).is_homogeneous True >>> Poly(x**3 + x*y, x, y).is_homogeneous False """ return f.rep.is_homogeneous @property def is_irreducible(f): """ Returns ``True`` if ``f`` has no factors over its domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + x + 1, x, modulus=2).is_irreducible True >>> Poly(x**2 + 1, x, modulus=2).is_irreducible False """ return f.rep.is_irreducible @property def is_univariate(f): """ Returns ``True`` if ``f`` is a univariate polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + x + 1, x).is_univariate True >>> Poly(x*y**2 + x*y + 1, x, y).is_univariate False >>> Poly(x*y**2 + x*y + 1, x).is_univariate True >>> Poly(x**2 + x + 1, x, y).is_univariate False """ return len(f.gens) == 1 @property def is_multivariate(f): """ Returns ``True`` if ``f`` is a multivariate polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + x + 1, x).is_multivariate False >>> Poly(x*y**2 + x*y + 1, x, y).is_multivariate True >>> Poly(x*y**2 + x*y + 1, x).is_multivariate False >>> Poly(x**2 + x + 1, x, y).is_multivariate True """ return len(f.gens) != 1 @property def is_cyclotomic(f): """ Returns ``True`` if ``f`` is a cyclotomic polnomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1 >>> Poly(f).is_cyclotomic False >>> g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1 >>> Poly(g).is_cyclotomic True """ return f.rep.is_cyclotomic def __abs__(f): return f.abs() def __neg__(f): return f.neg() @_sympifyit('g', NotImplemented) def __add__(f, g): if not g.is_Poly: try: g = f.__class__(g, *f.gens) except PolynomialError: return f.as_expr() + g return f.add(g) @_sympifyit('g', NotImplemented) def __radd__(f, g): if not g.is_Poly: try: g = f.__class__(g, *f.gens) except PolynomialError: return g + f.as_expr() return g.add(f) @_sympifyit('g', NotImplemented) def __sub__(f, g): if not g.is_Poly: try: g = f.__class__(g, *f.gens) except PolynomialError: return f.as_expr() - g return f.sub(g) @_sympifyit('g', NotImplemented) def __rsub__(f, g): if not g.is_Poly: try: g = f.__class__(g, *f.gens) except PolynomialError: return g - f.as_expr() return g.sub(f) @_sympifyit('g', NotImplemented) def __mul__(f, g): if not g.is_Poly: try: g = f.__class__(g, *f.gens) except PolynomialError: return f.as_expr()*g return f.mul(g) @_sympifyit('g', NotImplemented) def __rmul__(f, g): if not g.is_Poly: try: g = f.__class__(g, *f.gens) except PolynomialError: return g*f.as_expr() return g.mul(f) @_sympifyit('n', NotImplemented) def __pow__(f, n): if n.is_Integer and n >= 0: return f.pow(n) else: return f.as_expr()**n @_sympifyit('g', NotImplemented) def __divmod__(f, g): if not g.is_Poly: g = f.__class__(g, *f.gens) return f.div(g) @_sympifyit('g', NotImplemented) def __rdivmod__(f, g): if not g.is_Poly: g = f.__class__(g, *f.gens) return g.div(f) @_sympifyit('g', NotImplemented) def __mod__(f, g): if not g.is_Poly: g = f.__class__(g, *f.gens) return f.rem(g) @_sympifyit('g', NotImplemented) def __rmod__(f, g): if not g.is_Poly: g = f.__class__(g, *f.gens) return g.rem(f) @_sympifyit('g', NotImplemented) def __floordiv__(f, g): if not g.is_Poly: g = f.__class__(g, *f.gens) return f.quo(g) @_sympifyit('g', NotImplemented) def __rfloordiv__(f, g): if not g.is_Poly: g = f.__class__(g, *f.gens) return g.quo(f) @_sympifyit('g', NotImplemented) def __div__(f, g): return f.as_expr()/g.as_expr() @_sympifyit('g', NotImplemented) def __rdiv__(f, g): return g.as_expr()/f.as_expr() __truediv__ = __div__ __rtruediv__ = __rdiv__ @_sympifyit('g', NotImplemented) def __eq__(f, g): if not g.is_Poly: try: g = f.__class__(g, f.gens, domain=f.get_domain()) except (PolynomialError, DomainError, CoercionFailed): return False if f.gens != g.gens: return False if f.rep.dom != g.rep.dom: try: dom = f.rep.dom.unify(g.rep.dom, f.gens) except UnificationFailed: return False f = f.set_domain(dom) g = g.set_domain(dom) return f.rep == g.rep @_sympifyit('g', NotImplemented) def __ne__(f, g): return not f.__eq__(g) def __nonzero__(f): return not f.is_zero def eq(f, g, strict=False): if not strict: return f.__eq__(g) else: return f._strict_eq(sympify(g)) def ne(f, g, strict=False): return not f.eq(g, strict=strict) def _strict_eq(f, g): return isinstance(g, f.__class__) and f.gens == g.gens and f.rep.eq(g.rep, strict=True) class PurePoly(Poly): """Class for representing pure polynomials. """ def _hashable_content(self): """Allow SymPy to hash Poly instances. """ return (self.rep,) def __hash__(self): return super(PurePoly, self).__hash__() @property def free_symbols(self): """ Free symbols of a polynomial. Examples ======== >>> from sympy import PurePoly >>> from sympy.abc import x, y >>> PurePoly(x**2 + 1).free_symbols set() >>> PurePoly(x**2 + y).free_symbols set() >>> PurePoly(x**2 + y, x).free_symbols set([y]) """ return self.free_symbols_in_domain @_sympifyit('g', NotImplemented) def __eq__(f, g): if not g.is_Poly: try: g = f.__class__(g, f.gens, domain=f.get_domain()) except (PolynomialError, DomainError, CoercionFailed): return False if len(f.gens) != len(g.gens): return False if f.rep.dom != g.rep.dom: try: dom = f.rep.dom.unify(g.rep.dom, f.gens) except UnificationFailed: return False f = f.set_domain(dom) g = g.set_domain(dom) return f.rep == g.rep def _strict_eq(f, g): return isinstance(g, f.__class__) and f.rep.eq(g.rep, strict=True) def _unify(f, g): g = sympify(g) if not g.is_Poly: try: return f.rep.dom, f.per, f.rep, f.rep.per(f.rep.dom.from_sympy(g)) except CoercionFailed: raise UnificationFailed("can't unify %s with %s" % (f, g)) if len(f.gens) != len(g.gens): raise UnificationFailed("can't unify %s with %s" % (f, g)) if not (isinstance(f.rep, DMP) and isinstance(g.rep, DMP)): raise UnificationFailed("can't unify %s with %s" % (f, g)) cls = f.__class__ gens = f.gens dom = f.rep.dom.unify(g.rep.dom, gens) F = f.rep.convert(dom) G = g.rep.convert(dom) def per(rep, dom=dom, gens=gens, remove=None): if remove is not None: gens = gens[:remove] + gens[remove + 1:] if not gens: return dom.to_sympy(rep) return cls.new(rep, *gens) return dom, per, F, G def poly_from_expr(expr, *gens, **args): """Construct a polynomial from an expression. """ opt = options.build_options(gens, args) return _poly_from_expr(expr, opt) def _poly_from_expr(expr, opt): """Construct a polynomial from an expression. """ orig, expr = expr, sympify(expr) if not isinstance(expr, Basic): raise PolificationFailed(opt, orig, expr) elif expr.is_Poly: poly = expr.__class__._from_poly(expr, opt) opt['gens'] = poly.gens opt['domain'] = poly.domain if opt.polys is None: opt['polys'] = True return poly, opt elif opt.expand: expr = expr.expand() try: rep, opt = _dict_from_expr(expr, opt) except GeneratorsNeeded: raise PolificationFailed(opt, orig, expr) monoms, coeffs = zip(*rep.items()) domain = opt.domain if domain is None: domain, coeffs = construct_domain(coeffs, opt=opt) else: coeffs = map(domain.from_sympy, coeffs) level = len(opt.gens) - 1 poly = Poly.new( DMP.from_monoms_coeffs(monoms, coeffs, level, domain), *opt.gens) opt['domain'] = domain if opt.polys is None: opt['polys'] = False return poly, opt def parallel_poly_from_expr(exprs, *gens, **args): """Construct polynomials from expressions. """ opt = options.build_options(gens, args) return _parallel_poly_from_expr(exprs, opt) def _parallel_poly_from_expr(exprs, opt): """Construct polynomials from expressions. """ if len(exprs) == 2: f, g = exprs if isinstance(f, Poly) and isinstance(g, Poly): f = f.__class__._from_poly(f, opt) g = g.__class__._from_poly(g, opt) f, g = f.unify(g) opt['gens'] = f.gens opt['domain'] = f.domain if opt.polys is None: opt['polys'] = True return [f, g], opt origs, exprs = list(exprs), [] _exprs, _polys = [], [] failed = False for i, expr in enumerate(origs): expr = sympify(expr) if isinstance(expr, Basic): if expr.is_Poly: _polys.append(i) else: _exprs.append(i) if opt.expand: expr = expr.expand() else: failed = True exprs.append(expr) if failed: raise PolificationFailed(opt, origs, exprs, True) if _polys: # XXX: this is a temporary solution for i in _polys: exprs[i] = exprs[i].as_expr() try: reps, opt = _parallel_dict_from_expr(exprs, opt) except GeneratorsNeeded: raise PolificationFailed(opt, origs, exprs, True) coeffs_list, lengths = [], [] all_monoms = [] all_coeffs = [] for rep in reps: monoms, coeffs = zip(*rep.items()) coeffs_list.extend(coeffs) all_monoms.append(monoms) lengths.append(len(coeffs)) domain = opt.domain if domain is None: domain, coeffs_list = construct_domain(coeffs_list, opt=opt) else: coeffs_list = map(domain.from_sympy, coeffs_list) for k in lengths: all_coeffs.append(coeffs_list[:k]) coeffs_list = coeffs_list[k:] polys, level = [], len(opt.gens) - 1 for monoms, coeffs in zip(all_monoms, all_coeffs): rep = DMP.from_monoms_coeffs(monoms, coeffs, level, domain) polys.append(Poly.new(rep, *opt.gens)) opt['domain'] = domain if opt.polys is None: opt['polys'] = bool(_polys) return polys, opt def _update_args(args, key, value): """Add a new ``(key, value)`` pair to arguments ``dict``. """ args = dict(args) if key not in args: args[key] = value return args def degree(f, *gens, **args): """ Return the degree of ``f`` in the given variable. Examples ======== >>> from sympy import degree >>> from sympy.abc import x, y >>> degree(x**2 + y*x + 1, gen=x) 2 >>> degree(x**2 + y*x + 1, gen=y) 1 """ options.allowed_flags(args, ['gen', 'polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('degree', 1, exc) return Integer(F.degree(opt.gen)) def degree_list(f, *gens, **args): """ Return a list of degrees of ``f`` in all variables. Examples ======== >>> from sympy import degree_list >>> from sympy.abc import x, y >>> degree_list(x**2 + y*x + 1) (2, 1) """ options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('degree_list', 1, exc) degrees = F.degree_list() return tuple(map(Integer, degrees)) def LC(f, *gens, **args): """ Return the leading coefficient of ``f``. Examples ======== >>> from sympy import LC >>> from sympy.abc import x, y >>> LC(4*x**2 + 2*x*y**2 + x*y + 3*y) 4 """ options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('LC', 1, exc) return F.LC(order=opt.order) def LM(f, *gens, **args): """ Return the leading monomial of ``f``. Examples ======== >>> from sympy import LM >>> from sympy.abc import x, y >>> LM(4*x**2 + 2*x*y**2 + x*y + 3*y) x**2 """ options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('LM', 1, exc) monom = F.LM(order=opt.order) return monom.as_expr() def LT(f, *gens, **args): """ Return the leading term of ``f``. Examples ======== >>> from sympy import LT >>> from sympy.abc import x, y >>> LT(4*x**2 + 2*x*y**2 + x*y + 3*y) 4*x**2 """ options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('LT', 1, exc) monom, coeff = F.LT(order=opt.order) return coeff*monom.as_expr() def pdiv(f, g, *gens, **args): """ Compute polynomial pseudo-division of ``f`` and ``g``. Examples ======== >>> from sympy import pdiv >>> from sympy.abc import x >>> pdiv(x**2 + 1, 2*x - 4) (2*x + 4, 20) """ options.allowed_flags(args, ['polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('pdiv', 2, exc) q, r = F.pdiv(G) if not opt.polys: return q.as_expr(), r.as_expr() else: return q, r def prem(f, g, *gens, **args): """ Compute polynomial pseudo-remainder of ``f`` and ``g``. Examples ======== >>> from sympy import prem >>> from sympy.abc import x >>> prem(x**2 + 1, 2*x - 4) 20 """ options.allowed_flags(args, ['polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('prem', 2, exc) r = F.prem(G) if not opt.polys: return r.as_expr() else: return r def pquo(f, g, *gens, **args): """ Compute polynomial pseudo-quotient of ``f`` and ``g``. Examples ======== >>> from sympy import pquo >>> from sympy.abc import x >>> pquo(x**2 + 1, 2*x - 4) 2*x + 4 >>> pquo(x**2 - 1, 2*x - 1) 2*x + 1 """ options.allowed_flags(args, ['polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('pquo', 2, exc) try: q = F.pquo(G) except ExactQuotientFailed: raise ExactQuotientFailed(f, g) if not opt.polys: return q.as_expr() else: return q def pexquo(f, g, *gens, **args): """ Compute polynomial exact pseudo-quotient of ``f`` and ``g``. Examples ======== >>> from sympy import pexquo >>> from sympy.abc import x >>> pexquo(x**2 - 1, 2*x - 2) 2*x + 2 >>> pexquo(x**2 + 1, 2*x - 4) Traceback (most recent call last): ... ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1 """ options.allowed_flags(args, ['polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('pexquo', 2, exc) q = F.pexquo(G) if not opt.polys: return q.as_expr() else: return q def div(f, g, *gens, **args): """ Compute polynomial division of ``f`` and ``g``. Examples ======== >>> from sympy import div, ZZ, QQ >>> from sympy.abc import x >>> div(x**2 + 1, 2*x - 4, domain=ZZ) (0, x**2 + 1) >>> div(x**2 + 1, 2*x - 4, domain=QQ) (x/2 + 1, 5) """ options.allowed_flags(args, ['auto', 'polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('div', 2, exc) q, r = F.div(G, auto=opt.auto) if not opt.polys: return q.as_expr(), r.as_expr() else: return q, r def rem(f, g, *gens, **args): """ Compute polynomial remainder of ``f`` and ``g``. Examples ======== >>> from sympy import rem, ZZ, QQ >>> from sympy.abc import x >>> rem(x**2 + 1, 2*x - 4, domain=ZZ) x**2 + 1 >>> rem(x**2 + 1, 2*x - 4, domain=QQ) 5 """ options.allowed_flags(args, ['auto', 'polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('rem', 2, exc) r = F.rem(G, auto=opt.auto) if not opt.polys: return r.as_expr() else: return r def quo(f, g, *gens, **args): """ Compute polynomial quotient of ``f`` and ``g``. Examples ======== >>> from sympy import quo >>> from sympy.abc import x >>> quo(x**2 + 1, 2*x - 4) x/2 + 1 >>> quo(x**2 - 1, x - 1) x + 1 """ options.allowed_flags(args, ['auto', 'polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('quo', 2, exc) q = F.quo(G, auto=opt.auto) if not opt.polys: return q.as_expr() else: return q def exquo(f, g, *gens, **args): """ Compute polynomial exact quotient of ``f`` and ``g``. Examples ======== >>> from sympy import exquo >>> from sympy.abc import x >>> exquo(x**2 - 1, x - 1) x + 1 >>> exquo(x**2 + 1, 2*x - 4) Traceback (most recent call last): ... ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1 """ options.allowed_flags(args, ['auto', 'polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('exquo', 2, exc) q = F.exquo(G, auto=opt.auto) if not opt.polys: return q.as_expr() else: return q def half_gcdex(f, g, *gens, **args): """ Half extended Euclidean algorithm of ``f`` and ``g``. Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``. Examples ======== >>> from sympy import half_gcdex >>> from sympy.abc import x >>> half_gcdex(x**4 - 2*x**3 - 6*x**2 + 12*x + 15, x**3 + x**2 - 4*x - 4) (-x/5 + 3/5, x + 1) """ options.allowed_flags(args, ['auto', 'polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: domain, (a, b) = construct_domain(exc.exprs) try: s, h = domain.half_gcdex(a, b) except NotImplementedError: raise ComputationFailed('half_gcdex', 2, exc) else: return domain.to_sympy(s), domain.to_sympy(h) s, h = F.half_gcdex(G, auto=opt.auto) if not opt.polys: return s.as_expr(), h.as_expr() else: return s, h def gcdex(f, g, *gens, **args): """ Extended Euclidean algorithm of ``f`` and ``g``. Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``. Examples ======== >>> from sympy import gcdex >>> from sympy.abc import x >>> gcdex(x**4 - 2*x**3 - 6*x**2 + 12*x + 15, x**3 + x**2 - 4*x - 4) (-x/5 + 3/5, x**2/5 - 6*x/5 + 2, x + 1) """ options.allowed_flags(args, ['auto', 'polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: domain, (a, b) = construct_domain(exc.exprs) try: s, t, h = domain.gcdex(a, b) except NotImplementedError: raise ComputationFailed('gcdex', 2, exc) else: return domain.to_sympy(s), domain.to_sympy(t), domain.to_sympy(h) s, t, h = F.gcdex(G, auto=opt.auto) if not opt.polys: return s.as_expr(), t.as_expr(), h.as_expr() else: return s, t, h def invert(f, g, *gens, **args): """ Invert ``f`` modulo ``g`` when possible. Examples ======== >>> from sympy import invert >>> from sympy.abc import x >>> invert(x**2 - 1, 2*x - 1) -4/3 >>> invert(x**2 - 1, x - 1) Traceback (most recent call last): ... NotInvertible: zero divisor """ options.allowed_flags(args, ['auto', 'polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: domain, (a, b) = construct_domain(exc.exprs) try: return domain.to_sympy(domain.invert(a, b)) except NotImplementedError: raise ComputationFailed('invert', 2, exc) h = F.invert(G, auto=opt.auto) if not opt.polys: return h.as_expr() else: return h def subresultants(f, g, *gens, **args): """ Compute subresultant PRS of ``f`` and ``g``. Examples ======== >>> from sympy import subresultants >>> from sympy.abc import x >>> subresultants(x**2 + 1, x**2 - 1) [x**2 + 1, x**2 - 1, -2] """ options.allowed_flags(args, ['polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('subresultants', 2, exc) result = F.subresultants(G) if not opt.polys: return [ r.as_expr() for r in result ] else: return result def resultant(f, g, *gens, **args): """ Compute resultant of ``f`` and ``g``. Examples ======== >>> from sympy import resultant >>> from sympy.abc import x >>> resultant(x**2 + 1, x**2 - 1) 4 """ includePRS = args.pop('includePRS', False) options.allowed_flags(args, ['polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('resultant', 2, exc) if includePRS: result, R = F.resultant(G, includePRS=includePRS) else: result = F.resultant(G) if not opt.polys: if includePRS: return result.as_expr(), [r.as_expr() for r in R] return result.as_expr() else: if includePRS: return result, R return result def discriminant(f, *gens, **args): """ Compute discriminant of ``f``. Examples ======== >>> from sympy import discriminant >>> from sympy.abc import x >>> discriminant(x**2 + 2*x + 3) -8 """ options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('discriminant', 1, exc) result = F.discriminant() if not opt.polys: return result.as_expr() else: return result def cofactors(f, g, *gens, **args): """ Compute GCD and cofactors of ``f`` and ``g``. Returns polynomials ``(h, cff, cfg)`` such that ``h = gcd(f, g)``, and ``cff = quo(f, h)`` and ``cfg = quo(g, h)`` are, so called, cofactors of ``f`` and ``g``. Examples ======== >>> from sympy import cofactors >>> from sympy.abc import x >>> cofactors(x**2 - 1, x**2 - 3*x + 2) (x - 1, x + 1, x - 2) """ options.allowed_flags(args, ['polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: domain, (a, b) = construct_domain(exc.exprs) try: h, cff, cfg = domain.cofactors(a, b) except NotImplementedError: raise ComputationFailed('cofactors', 2, exc) else: return domain.to_sympy(h), domain.to_sympy(cff), domain.to_sympy(cfg) h, cff, cfg = F.cofactors(G) if not opt.polys: return h.as_expr(), cff.as_expr(), cfg.as_expr() else: return h, cff, cfg def gcd_list(seq, *gens, **args): """ Compute GCD of a list of polynomials. Examples ======== >>> from sympy import gcd_list >>> from sympy.abc import x >>> gcd_list([x**3 - 1, x**2 - 1, x**2 - 3*x + 2]) x - 1 """ seq = sympify(seq) if not gens and not args: domain, numbers = construct_domain(seq) if not numbers: return domain.zero elif domain.is_Numerical: result, numbers = numbers[0], numbers[1:] for number in numbers: result = domain.gcd(result, number) if domain.is_one(result): break return domain.to_sympy(result) options.allowed_flags(args, ['polys']) try: polys, opt = parallel_poly_from_expr(seq, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('gcd_list', len(seq), exc) if not polys: if not opt.polys: return S.Zero else: return Poly(0, opt=opt) result, polys = polys[0], polys[1:] for poly in polys: result = result.gcd(poly) if result.is_one: break if not opt.polys: return result.as_expr() else: return result def gcd(f, g=None, *gens, **args): """ Compute GCD of ``f`` and ``g``. Examples ======== >>> from sympy import gcd >>> from sympy.abc import x >>> gcd(x**2 - 1, x**2 - 3*x + 2) x - 1 """ if hasattr(f, '__iter__'): if g is not None: gens = (g,) + gens return gcd_list(f, *gens, **args) elif g is None: raise TypeError("gcd() takes 2 arguments or a sequence of arguments") options.allowed_flags(args, ['polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: domain, (a, b) = construct_domain(exc.exprs) try: return domain.to_sympy(domain.gcd(a, b)) except NotImplementedError: raise ComputationFailed('gcd', 2, exc) result = F.gcd(G) if not opt.polys: return result.as_expr() else: return result def lcm_list(seq, *gens, **args): """ Compute LCM of a list of polynomials. Examples ======== >>> from sympy import lcm_list >>> from sympy.abc import x >>> lcm_list([x**3 - 1, x**2 - 1, x**2 - 3*x + 2]) x**5 - x**4 - 2*x**3 - x**2 + x + 2 """ seq = sympify(seq) if not gens and not args: domain, numbers = construct_domain(seq) if not numbers: return domain.one elif domain.is_Numerical: result, numbers = numbers[0], numbers[1:] for number in numbers: result = domain.lcm(result, number) return domain.to_sympy(result) options.allowed_flags(args, ['polys']) try: polys, opt = parallel_poly_from_expr(seq, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('lcm_list', len(seq), exc) if not polys: if not opt.polys: return S.One else: return Poly(1, opt=opt) result, polys = polys[0], polys[1:] for poly in polys: result = result.lcm(poly) if not opt.polys: return result.as_expr() else: return result def lcm(f, g=None, *gens, **args): """ Compute LCM of ``f`` and ``g``. Examples ======== >>> from sympy import lcm >>> from sympy.abc import x >>> lcm(x**2 - 1, x**2 - 3*x + 2) x**3 - 2*x**2 - x + 2 """ if hasattr(f, '__iter__'): if g is not None: gens = (g,) + gens return lcm_list(f, *gens, **args) elif g is None: raise TypeError("lcm() takes 2 arguments or a sequence of arguments") options.allowed_flags(args, ['polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: domain, (a, b) = construct_domain(exc.exprs) try: return domain.to_sympy(domain.lcm(a, b)) except NotImplementedError: raise ComputationFailed('lcm', 2, exc) result = F.lcm(G) if not opt.polys: return result.as_expr() else: return result def terms_gcd(f, *gens, **args): """ Remove GCD of terms from ``f``. If the ``deep`` flag is True, then the arguments of ``f`` will have terms_gcd applied to them. If a fraction is factored out of ``f`` and ``f`` is an Add, then an unevaluated Mul will be returned so that automatic simplification does not redistribute it. The hint ``clear``, when set to False, can be used to prevent such factoring when all coefficients are not fractions. Examples ======== >>> from sympy import terms_gcd, cos, pi >>> from sympy.abc import x, y >>> terms_gcd(x**6*y**2 + x**3*y, x, y) x**3*y*(x**3*y + 1) The default action of polys routines is to expand the expression given to them. terms_gcd follows this behavior: >>> terms_gcd((3+3*x)*(x+x*y)) 3*x*(x*y + x + y + 1) If this is not desired then the hint ``expand`` can be set to False. In this case the expression will be treated as though it were comprised of one or more terms: >>> terms_gcd((3+3*x)*(x+x*y), expand=False) (3*x + 3)*(x*y + x) In order to traverse factors of a Mul or the arguments of other functions, the ``deep`` hint can be used: >>> terms_gcd((3 + 3*x)*(x + x*y), expand=False, deep=True) 3*x*(x + 1)*(y + 1) >>> terms_gcd(cos(x + x*y), deep=True) cos(x*(y + 1)) Rationals are factored out by default: >>> terms_gcd(x + y/2) (2*x + y)/2 Only the y-term had a coefficient that was a fraction; if one does not want to factor out the 1/2 in cases like this, the flag ``clear`` can be set to False: >>> terms_gcd(x + y/2, clear=False) x + y/2 >>> terms_gcd(x*y/2 + y**2, clear=False) y*(x/2 + y) The ``clear`` flag is ignored if all coefficients are fractions: >>> terms_gcd(x/3 + y/2, clear=False) (2*x + 3*y)/6 See Also ======== sympy.core.exprtools.gcd_terms, sympy.core.exprtools.factor_terms """ if not isinstance(f, Expr) or f.is_Atom: return sympify(f) if args.get('deep', False): new = f.func(*[terms_gcd(a, *gens, **args) for a in f.args]) args.pop('deep') args['expand'] = False return terms_gcd(new, *gens, **args) clear = args.pop('clear', True) options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: return exc.expr J, f = F.terms_gcd() if opt.domain.has_Ring: if opt.domain.has_Field: denom, f = f.clear_denoms(convert=True) coeff, f = f.primitive() if opt.domain.has_Field: coeff /= denom else: coeff = S.One term = Mul(*[ x**j for x, j in zip(f.gens, J) ]) if clear: return _keep_coeff(coeff, term*f.as_expr()) # base the clearing on the form of the original expression, not # the (perhaps) Mul that we have now coeff, f = _keep_coeff(coeff, f.as_expr(), clear=False).as_coeff_Mul() return _keep_coeff(coeff, term*f, clear=False) def trunc(f, p, *gens, **args): """ Reduce ``f`` modulo a constant ``p``. Examples ======== >>> from sympy import trunc >>> from sympy.abc import x >>> trunc(2*x**3 + 3*x**2 + 5*x + 7, 3) -x**3 - x + 1 """ options.allowed_flags(args, ['auto', 'polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('trunc', 1, exc) result = F.trunc(sympify(p)) if not opt.polys: return result.as_expr() else: return result def monic(f, *gens, **args): """ Divide all coefficients of ``f`` by ``LC(f)``. Examples ======== >>> from sympy import monic >>> from sympy.abc import x >>> monic(3*x**2 + 4*x + 2) x**2 + 4*x/3 + 2/3 """ options.allowed_flags(args, ['auto', 'polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('monic', 1, exc) result = F.monic(auto=opt.auto) if not opt.polys: return result.as_expr() else: return result def content(f, *gens, **args): """ Compute GCD of coefficients of ``f``. Examples ======== >>> from sympy import content >>> from sympy.abc import x >>> content(6*x**2 + 8*x + 12) 2 """ options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('content', 1, exc) return F.content() def primitive(f, *gens, **args): """ Compute content and the primitive form of ``f``. Examples ======== >>> from sympy.polys.polytools import primitive >>> from sympy.abc import x, y >>> primitive(6*x**2 + 8*x + 12) (2, 3*x**2 + 4*x + 6) >>> eq = (2 + 2*x)*x + 2 Expansion is performed by default: >>> primitive(eq) (2, x**2 + x + 1) Set ``expand`` to False to shut this off. Note that the extraction will not be recursive; use the as_content_primitive method for recursive, non-destructive Rational extraction. >>> primitive(eq, expand=False) (1, x*(2*x + 2) + 2) >>> eq.as_content_primitive() (2, x*(x + 1) + 1) """ options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('primitive', 1, exc) cont, result = F.primitive() if not opt.polys: return cont, result.as_expr() else: return cont, result def compose(f, g, *gens, **args): """ Compute functional composition ``f(g)``. Examples ======== >>> from sympy import compose >>> from sympy.abc import x >>> compose(x**2 + x, x - 1) x**2 - x """ options.allowed_flags(args, ['polys']) try: (F, G), opt = parallel_poly_from_expr((f, g), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('compose', 2, exc) result = F.compose(G) if not opt.polys: return result.as_expr() else: return result def decompose(f, *gens, **args): """ Compute functional decomposition of ``f``. Examples ======== >>> from sympy import decompose >>> from sympy.abc import x >>> decompose(x**4 + 2*x**3 - x - 1) [x**2 - x - 1, x**2 + x] """ options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('decompose', 1, exc) result = F.decompose() if not opt.polys: return [ r.as_expr() for r in result ] else: return result def sturm(f, *gens, **args): """ Compute Sturm sequence of ``f``. Examples ======== >>> from sympy import sturm >>> from sympy.abc import x >>> sturm(x**3 - 2*x**2 + x - 3) [x**3 - 2*x**2 + x - 3, 3*x**2 - 4*x + 1, 2*x/9 + 25/9, -2079/4] """ options.allowed_flags(args, ['auto', 'polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('sturm', 1, exc) result = F.sturm(auto=opt.auto) if not opt.polys: return [ r.as_expr() for r in result ] else: return result def gff_list(f, *gens, **args): """ Compute a list of greatest factorial factors of ``f``. Examples ======== >>> from sympy import gff_list, ff >>> from sympy.abc import x >>> f = x**5 + 2*x**4 - x**3 - 2*x**2 >>> gff_list(f) [(x, 1), (x + 2, 4)] >>> (ff(x, 1)*ff(x + 2, 4)).expand() == f True """ options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('gff_list', 1, exc) factors = F.gff_list() if not opt.polys: return [ (g.as_expr(), k) for g, k in factors ] else: return factors def gff(f, *gens, **args): """Compute greatest factorial factorization of ``f``. """ raise NotImplementedError('symbolic falling factorial') def sqf_norm(f, *gens, **args): """ Compute square-free norm of ``f``. Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and ``r(x) = Norm(g(x))`` is a square-free polynomial over ``K``, where ``a`` is the algebraic extension of the ground domain. Examples ======== >>> from sympy import sqf_norm, sqrt >>> from sympy.abc import x >>> sqf_norm(x**2 + 1, extension=[sqrt(3)]) (1, x**2 - 2*sqrt(3)*x + 4, x**4 - 4*x**2 + 16) """ options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('sqf_norm', 1, exc) s, g, r = F.sqf_norm() if not opt.polys: return Integer(s), g.as_expr(), r.as_expr() else: return Integer(s), g, r def sqf_part(f, *gens, **args): """ Compute square-free part of ``f``. Examples ======== >>> from sympy import sqf_part >>> from sympy.abc import x >>> sqf_part(x**3 - 3*x - 2) x**2 - x - 2 """ options.allowed_flags(args, ['polys']) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('sqf_part', 1, exc) result = F.sqf_part() if not opt.polys: return result.as_expr() else: return result def _sorted_factors(factors, method): """Sort a list of ``(expr, exp)`` pairs. """ if method == 'sqf': def key(obj): poly, exp = obj rep = poly.rep.rep return (exp, len(rep), rep) else: def key(obj): poly, exp = obj rep = poly.rep.rep return (len(rep), exp, rep) return sorted(factors, key=key) def _factors_product(factors): """Multiply a list of ``(expr, exp)`` pairs. """ return Mul(*[ f.as_expr()**k for f, k in factors ]) def _symbolic_factor_list(expr, opt, method): """Helper function for :func:`_symbolic_factor`. """ coeff, factors = S.One, [] for arg in Mul.make_args(expr): if arg.is_Number: coeff *= arg continue elif arg.is_Pow: base, exp = arg.args if base.is_Number: factors.append((base, exp)) continue else: base, exp = arg, S.One try: poly, _ = _poly_from_expr(base, opt) except PolificationFailed, exc: factors.append((exc.expr, exp)) else: func = getattr(poly, method + '_list') _coeff, _factors = func() if _coeff is not S.One: if exp.is_Integer: coeff *= _coeff**exp elif _coeff.is_positive: factors.append((_coeff, exp)) else: _factors.append((_coeff, None)) if exp is S.One: factors.extend(_factors) elif exp.is_integer or len(_factors) == 1: factors.extend([ (f, k*exp) for f, k in _factors ]) else: other = [] for f, k in _factors: if f.as_expr().is_positive: factors.append((f, k*exp)) elif k is not None: other.append((f, k)) else: other.append((f, S.One)) if len(other) == 1: f, k = other[0] factors.append((f, k*exp)) else: factors.append((_factors_product(other), exp)) return coeff, factors def _symbolic_factor(expr, opt, method): """Helper function for :func:`_factor`. """ if isinstance(expr, Expr) and not expr.is_Relational: coeff, factors = _symbolic_factor_list(together(expr), opt, method) return _keep_coeff(coeff, _factors_product(factors)) elif hasattr(expr, 'args'): return expr.func(*[ _symbolic_factor(arg, opt, method) for arg in expr.args ]) elif hasattr(expr, '__iter__'): return expr.__class__([ _symbolic_factor(arg, opt, method) for arg in expr ]) else: return expr def _generic_factor_list(expr, gens, args, method): """Helper function for :func:`sqf_list` and :func:`factor_list`. """ options.allowed_flags(args, ['frac', 'polys']) opt = options.build_options(gens, args) expr = sympify(expr) if isinstance(expr, Expr) and not expr.is_Relational: numer, denom = together(expr).as_numer_denom() cp, fp = _symbolic_factor_list(numer, opt, method) cq, fq = _symbolic_factor_list(denom, opt, method) if fq and not opt.frac: raise PolynomialError("a polynomial expected, got %s" % expr) _opt = opt.clone(dict(expand=True)) for factors in (fp, fq): for i, (f, k) in enumerate(factors): if not f.is_Poly: f, _ = _poly_from_expr(f, _opt) factors[i] = (f, k) fp = _sorted_factors(fp, method) fq = _sorted_factors(fq, method) if not opt.polys: fp = [ (f.as_expr(), k) for f, k in fp ] fq = [ (f.as_expr(), k) for f, k in fq ] coeff = cp/cq if not opt.frac: return coeff, fp else: return coeff, fp, fq else: raise PolynomialError("a polynomial expected, got %s" % expr) def _generic_factor(expr, gens, args, method): """Helper function for :func:`sqf` and :func:`factor`. """ options.allowed_flags(args, []) opt = options.build_options(gens, args) return _symbolic_factor(sympify(expr), opt, method) def to_rational_coeffs(f): """ try to transform a polynomial to have rational coefficients try to find a transformation ``x = alpha*y`` ``f(x) = lc*alpha**n * g(y)`` where ``g`` is a polynomial with rational coefficients, ``lc`` the leading coefficient. If this fails, try ``x = y + beta`` ``f(x) = g(y)`` Returns ``None`` if ``g`` not found; ``(lc, alpha, None, g)`` in case of rescaling ``(None, None, beta, g)`` in case of translation Notes ===== Currently it transforms only polynomials without roots larger than 2. Examples ======== >>> from sympy import sqrt, Poly, simplify, expand >>> from sympy.polys.polytools import to_rational_coeffs >>> from sympy.abc import x >>> p = Poly(((x**2-1)*(x-2)).subs({x:x*(1 + sqrt(2))}), x, domain='EX') >>> lc, r, _, g = to_rational_coeffs(p) >>> lc, r (7 + 5*sqrt(2), -2*sqrt(2) + 2) >>> g Poly(x**3 + x**2 - 1/4*x - 1/4, x, domain='QQ') >>> r1 = simplify(1/r) >>> Poly(lc*r**3*(g.as_expr()).subs({x:x*r1}), x, domain='EX') == p True """ from sympy.simplify.simplify import simplify def _try_rescale(f): """ try rescaling ``x -> alpha*x`` to convert f to a polynomial with rational coefficients. Returns ``alpha, f``; if the rescaling is successful, ``alpha`` is the rescaling factor, and ``f`` is the rescaled polynomial; else ``alpha`` is ``None``. """ from sympy.core.add import Add if not len(f.gens) == 1 or not (f.gens[0]).is_Atom: return None, f n = f.degree() lc = f.LC() coeffs = f.monic().all_coeffs()[1:] coeffs = [simplify(coeffx) for coeffx in coeffs] if coeffs[-2] and not all(coeffx.is_rational for coeffx in coeffs): rescale1_x = simplify(coeffs[-2]/coeffs[-1]) coeffs1 = [] for i in range(len(coeffs)): coeffx = simplify(coeffs[i]*rescale1_x**(i + 1)) if not coeffx.is_rational: break coeffs1.append(coeffx) else: rescale_x = simplify(1/rescale1_x) x = f.gens[0] v = [x**n] for i in range(1, n + 1): v.append(coeffs1[i - 1]*x**(n - i)) f = Add(*v) f = Poly(f) return lc, rescale_x, f return None def _try_translate(f): """ try translating ``x -> x + alpha`` to convert f to a polynomial with rational coefficients. Returns ``alpha, f``; if the translating is successful, ``alpha`` is the translating factor, and ``f`` is the shifted polynomial; else ``alpha`` is ``None``. """ from sympy.core.add import Add from sympy.utilities.iterables import sift if not len(f.gens) == 1 or not (f.gens[0]).is_Atom: return None, f n = f.degree() f1 = f.monic() coeffs = f1.all_coeffs()[1:] c = simplify(coeffs[0]) if c and not c.is_rational: if c.is_Add: args = c.args else: args = [c] sifted = sift(args, lambda z: z.is_rational) c1, c2 = sifted[True], sifted[False] alpha = -Add(*c2)/n f2 = f1.shift(alpha) return alpha, f2 return None def _has_square_roots(p): """ Return True if ``f`` is a sum with square roots but no other root """ from sympy.core.exprtools import Factors coeffs = p.coeffs() has_sq = False for y in coeffs: for x in Add.make_args(y): f = Factors(x).factors r = [wx.q for wx in f.values() if wx.is_Rational and wx.q >= 2] if not r: continue if min(r) == 2: has_sq = True if max(r) > 2: return False return has_sq if f.get_domain().is_EX and _has_square_roots(f): rescale_x = None translate_x = None r = _try_rescale(f) if r: return r[0], r[1], None, r[2] else: r = _try_translate(f) if r: return None, None, r[0], r[1] return None def _torational_factor_list(p, x): """ helper function to factor polynomial using to_rational_coeffs Examples ======== >>> from sympy.polys.polytools import _torational_factor_list >>> from sympy.abc import x >>> from sympy import sqrt, expand, Mul >>> p = expand(((x**2-1)*(x-2)).subs({x:x*(1 + sqrt(2))})) >>> factors = _torational_factor_list(p, x); factors (-2, [(-x*(1 + sqrt(2))/2 + 1, 1), (-x*(1 + sqrt(2)) - 1, 1), (-x*(1 + sqrt(2)) + 1, 1)]) >>> expand(factors[0]*Mul(*[z[0] for z in factors[1]])) == p True >>> p = expand(((x**2-1)*(x-2)).subs({x:x + sqrt(2)})) >>> factors = _torational_factor_list(p, x); factors (1, [(x - 2 + sqrt(2), 1), (x - 1 + sqrt(2), 1), (x + 1 + sqrt(2), 1)]) >>> expand(factors[0]*Mul(*[z[0] for z in factors[1]])) == p True """ from sympy.simplify.simplify import simplify p1 = Poly(p, x, domain='EX') n = p1.degree() res = to_rational_coeffs(p1) if not res: return None lc, r, t, g = res factors = factor_list(g.as_expr()) if lc: c = simplify(factors[0]*lc*r**n) r1 = simplify(1/r) a = [] for z in factors[1:][0]: a.append((simplify(z[0].subs({x:x*r1})), z[1])) else: c = factors[0] a = [] for z in factors[1:][0]: a.append((z[0].subs({x:x - t}), z[1])) return (c, a) def sqf_list(f, *gens, **args): """ Compute a list of square-free factors of ``f``. Examples ======== >>> from sympy import sqf_list >>> from sympy.abc import x >>> sqf_list(2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16) (2, [(x + 1, 2), (x + 2, 3)]) """ return _generic_factor_list(f, gens, args, method='sqf') def sqf(f, *gens, **args): """ Compute square-free factorization of ``f``. Examples ======== >>> from sympy import sqf >>> from sympy.abc import x >>> sqf(2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16) 2*(x + 1)**2*(x + 2)**3 """ return _generic_factor(f, gens, args, method='sqf') def factor_list(f, *gens, **args): """ Compute a list of irreducible factors of ``f``. Examples ======== >>> from sympy import factor_list >>> from sympy.abc import x, y >>> factor_list(2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y) (2, [(x + y, 1), (x**2 + 1, 2)]) """ return _generic_factor_list(f, gens, args, method='factor') def factor(f, *gens, **args): """ Compute the factorization of expression, ``f``, into irreducibles. (To factor an integer into primes, use ``factorint``.) There two modes implemented: symbolic and formal. If ``f`` is not an instance of :class:`Poly` and generators are not specified, then the former mode is used. Otherwise, the formal mode is used. In symbolic mode, :func:`factor` will traverse the expression tree and factor its components without any prior expansion, unless an instance of :class:`Add` is encountered (in this case formal factorization is used). This way :func:`factor` can handle large or symbolic exponents. By default, the factorization is computed over the rationals. To factor over other domain, e.g. an algebraic or finite field, use appropriate options: ``extension``, ``modulus`` or ``domain``. Examples ======== >>> from sympy import factor, sqrt >>> from sympy.abc import x, y >>> factor(2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y) 2*(x + y)*(x**2 + 1)**2 >>> factor(x**2 + 1) x**2 + 1 >>> factor(x**2 + 1, modulus=2) (x + 1)**2 >>> factor(x**2 + 1, gaussian=True) (x - I)*(x + I) >>> factor(x**2 - 2, extension=sqrt(2)) (x - sqrt(2))*(x + sqrt(2)) >>> factor((x**2 - 1)/(x**2 + 4*x + 4)) (x - 1)*(x + 1)/(x + 2)**2 >>> factor((x**2 + 4*x + 4)**10000000*(x**2 + 1)) (x + 2)**20000000*(x**2 + 1) By default, factor deals with an expression as a whole: >>> eq = 2**(x**2 + 2*x + 1) >>> factor(eq) 2**(x**2 + 2*x + 1) If the ``deep`` flag is True then subexpressions will be factored: >>> factor(eq, deep=True) 2**((x + 1)**2) See Also ======== sympy.ntheory.factor_.factorint """ f = sympify(f) if args.pop('deep', False): partials = {} muladd = f.atoms(Mul, Add) for p in muladd: fac = factor(p, *gens, **args) if (fac.is_Mul or fac.is_Pow) and fac != p: partials[p] = fac return f.xreplace(partials) try: return _generic_factor(f, gens, args, method='factor') except PolynomialError, msg: if not f.is_commutative: from sympy.core.exprtools import factor_nc return factor_nc(f) else: raise PolynomialError(msg) def intervals(F, all=False, eps=None, inf=None, sup=None, strict=False, fast=False, sqf=False): """ Compute isolating intervals for roots of ``f``. Examples ======== >>> from sympy import intervals >>> from sympy.abc import x >>> intervals(x**2 - 3) [((-2, -1), 1), ((1, 2), 1)] >>> intervals(x**2 - 3, eps=1e-2) [((-26/15, -19/11), 1), ((19/11, 26/15), 1)] """ if not hasattr(F, '__iter__'): try: F = Poly(F) except GeneratorsNeeded: return [] return F.intervals(all=all, eps=eps, inf=inf, sup=sup, fast=fast, sqf=sqf) else: polys, opt = parallel_poly_from_expr(F, domain='QQ') if len(opt.gens) > 1: raise MultivariatePolynomialError for i, poly in enumerate(polys): polys[i] = poly.rep.rep if eps is not None: eps = opt.domain.convert(eps) if eps <= 0: raise ValueError("'eps' must be a positive rational") if inf is not None: inf = opt.domain.convert(inf) if sup is not None: sup = opt.domain.convert(sup) intervals = dup_isolate_real_roots_list(polys, opt.domain, eps=eps, inf=inf, sup=sup, strict=strict, fast=fast) result = [] for (s, t), indices in intervals: s, t = opt.domain.to_sympy(s), opt.domain.to_sympy(t) result.append(((s, t), indices)) return result def refine_root(f, s, t, eps=None, steps=None, fast=False, check_sqf=False): """ Refine an isolating interval of a root to the given precision. Examples ======== >>> from sympy import refine_root >>> from sympy.abc import x >>> refine_root(x**2 - 3, 1, 2, eps=1e-2) (19/11, 26/15) """ try: F = Poly(f) except GeneratorsNeeded: raise PolynomialError( "can't refine a root of %s, not a polynomial" % f) return F.refine_root(s, t, eps=eps, steps=steps, fast=fast, check_sqf=check_sqf) def count_roots(f, inf=None, sup=None): """ Return the number of roots of ``f`` in ``[inf, sup]`` interval. If one of ``inf`` or ``sup`` is complex, it will return the number of roots in the complex rectangle with corners at ``inf`` and ``sup``. Examples ======== >>> from sympy import count_roots, I >>> from sympy.abc import x >>> count_roots(x**4 - 4, -3, 3) 2 >>> count_roots(x**4 - 4, 0, 1 + 3*I) 1 """ try: F = Poly(f, greedy=False) except GeneratorsNeeded: raise PolynomialError("can't count roots of %s, not a polynomial" % f) return F.count_roots(inf=inf, sup=sup) def real_roots(f, multiple=True): """ Return a list of real roots with multiplicities of ``f``. Examples ======== >>> from sympy import real_roots >>> from sympy.abc import x >>> real_roots(2*x**3 - 7*x**2 + 4*x + 4) [-1/2, 2, 2] """ try: F = Poly(f, greedy=False) except GeneratorsNeeded: raise PolynomialError( "can't compute real roots of %s, not a polynomial" % f) return F.real_roots(multiple=multiple) def nroots(f, n=15, maxsteps=50, cleanup=True, error=False): """ Compute numerical approximations of roots of ``f``. Examples ======== >>> from sympy import nroots >>> from sympy.abc import x >>> nroots(x**2 - 3, n=15) [-1.73205080756888, 1.73205080756888] >>> nroots(x**2 - 3, n=30) [-1.73205080756887729352744634151, 1.73205080756887729352744634151] """ try: F = Poly(f, greedy=False) except GeneratorsNeeded: raise PolynomialError( "can't compute numerical roots of %s, not a polynomial" % f) return F.nroots(n=n, maxsteps=maxsteps, cleanup=cleanup, error=error) def ground_roots(f, *gens, **args): """ Compute roots of ``f`` by factorization in the ground domain. Examples ======== >>> from sympy import ground_roots >>> from sympy.abc import x >>> ground_roots(x**6 - 4*x**4 + 4*x**3 - x**2) {0: 2, 1: 2} """ options.allowed_flags(args, []) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('ground_roots', 1, exc) return F.ground_roots() def nth_power_roots_poly(f, n, *gens, **args): """ Construct a polynomial with n-th powers of roots of ``f``. Examples ======== >>> from sympy import nth_power_roots_poly, factor, roots >>> from sympy.abc import x >>> f = x**4 - x**2 + 1 >>> g = factor(nth_power_roots_poly(f, 2)) >>> g (x**2 - x + 1)**2 >>> R_f = [ (r**2).expand() for r in roots(f) ] >>> R_g = roots(g).keys() >>> set(R_f) == set(R_g) True """ options.allowed_flags(args, []) try: F, opt = poly_from_expr(f, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('nth_power_roots_poly', 1, exc) result = F.nth_power_roots_poly(n) if not opt.polys: return result.as_expr() else: return result def cancel(f, *gens, **args): """ Cancel common factors in a rational function ``f``. Examples ======== >>> from sympy import cancel, sqrt, Symbol >>> from sympy.abc import x >>> A = Symbol('A', commutative=False) >>> cancel((2*x**2 - 2)/(x**2 - 2*x + 1)) (2*x + 2)/(x - 1) >>> cancel((sqrt(3) + sqrt(15)*A)/(sqrt(2) + sqrt(10)*A)) sqrt(6)/2 """ from sympy.core.exprtools import factor_terms options.allowed_flags(args, ['polys']) f = sympify(f) if not isinstance(f, (tuple, Tuple)): if f.is_Number: return f f = factor_terms(f, radical=True) p, q = f.as_numer_denom() elif len(f) == 2: p, q = f elif isinstance(f, Tuple): return factor_terms(f) else: raise ValueError('unexpected argument: %s' % f) try: (F, G), opt = parallel_poly_from_expr((p, q), *gens, **args) except PolificationFailed: if not isinstance(f, (tuple, Tuple)): return f else: return S.One, p, q except PolynomialError, msg: if f.is_commutative: raise PolynomialError(msg) # non-commutative if f.is_Mul: c, nc = f.args_cnc(split_1=False) nc = [cancel(i) for i in nc] return cancel(Mul._from_args(c))*Mul(*nc) elif f.is_Add: c = [] nc = [] for i in f.args: if i.is_commutative: c.append(i) else: nc.append(cancel(i)) return cancel(Add(*c)) + Add(*nc) else: reps = [] pot = preorder_traversal(f) pot.next() for e in pot: if isinstance(e, (tuple, Tuple)): continue try: reps.append((e, cancel(e))) pot.skip() # this was handled successfully except NotImplementedError: pass return f.xreplace(dict(reps)) c, P, Q = F.cancel(G) if not isinstance(f, (tuple, Tuple)): return c*(P.as_expr()/Q.as_expr()) else: if not opt.polys: return c, P.as_expr(), Q.as_expr() else: return c, P, Q def reduced(f, G, *gens, **args): """ Reduces a polynomial ``f`` modulo a set of polynomials ``G``. Given a polynomial ``f`` and a set of polynomials ``G = (g_1, ..., g_n)``, computes a set of quotients ``q = (q_1, ..., q_n)`` and the remainder ``r`` such that ``f = q_1*f_1 + ... + q_n*f_n + r``, where ``r`` vanishes or ``r`` is a completely reduced polynomial with respect to ``G``. Examples ======== >>> from sympy import reduced >>> from sympy.abc import x, y >>> reduced(2*x**4 + y**2 - x**2 + y**3, [x**3 - x, y**3 - y]) ([2*x, 1], x**2 + y**2 + y) """ options.allowed_flags(args, ['polys', 'auto']) try: polys, opt = parallel_poly_from_expr([f] + list(G), *gens, **args) except PolificationFailed, exc: raise ComputationFailed('reduced', 0, exc) domain = opt.domain retract = False if opt.auto and domain.has_Ring and not domain.has_Field: opt = opt.clone(dict(domain=domain.get_field())) retract = True from sympy.polys.rings import xring _ring, _ = xring(opt.gens, opt.domain, opt.order) for i, poly in enumerate(polys): poly = poly.set_domain(opt.domain).rep.to_dict() polys[i] = _ring.from_dict(poly) Q, r = polys[0].div(polys[1:]) Q = [ Poly._from_dict(dict(q), opt) for q in Q ] r = Poly._from_dict(dict(r), opt) if retract: try: _Q, _r = [ q.to_ring() for q in Q ], r.to_ring() except CoercionFailed: pass else: Q, r = _Q, _r if not opt.polys: return [ q.as_expr() for q in Q ], r.as_expr() else: return Q, r def groebner(F, *gens, **args): """ Computes the reduced Groebner basis for a set of polynomials. Use the ``order`` argument to set the monomial ordering that will be used to compute the basis. Allowed orders are ``lex``, ``grlex`` and ``grevlex``. If no order is specified, it defaults to ``lex``. For more information on Groebner bases, see the references and the docstring of `solve_poly_system()`. Examples ======== Example taken from [1]. >>> from sympy import groebner >>> from sympy.abc import x, y >>> F = [x*y - 2*y, 2*y**2 - x**2] >>> groebner(F, x, y, order='lex') GroebnerBasis([x**2 - 2*y**2, x*y - 2*y, y**3 - 2*y], x, y, domain='ZZ', order='lex') >>> groebner(F, x, y, order='grlex') GroebnerBasis([y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y], x, y, domain='ZZ', order='grlex') >>> groebner(F, x, y, order='grevlex') GroebnerBasis([y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y], x, y, domain='ZZ', order='grevlex') By default, an improved implementation of the Buchberger algorithm is used. Optionally, an implementation of the F5B algorithm can be used. The algorithm can be set using ``method`` flag or with the :func:`setup` function from :mod:`sympy.polys.polyconfig`: >>> F = [x**2 - x - 1, (2*x - 1) * y - (x**10 - (1 - x)**10)] >>> groebner(F, x, y, method='buchberger') GroebnerBasis([x**2 - x - 1, y - 55], x, y, domain='ZZ', order='lex') >>> groebner(F, x, y, method='f5b') GroebnerBasis([x**2 - x - 1, y - 55], x, y, domain='ZZ', order='lex') References ========== 1. [Buchberger01]_ 2. [Cox97]_ """ return GroebnerBasis(F, *gens, **args) def is_zero_dimensional(F, *gens, **args): """ Checks if the ideal generated by a Groebner basis is zero-dimensional. The algorithm checks if the set of monomials not divisible by the leading monomial of any element of ``F`` is bounded. References ========== David A. Cox, John B. Little, Donal O'Shea. Ideals, Varieties and Algorithms, 3rd edition, p. 230 """ return GroebnerBasis(F, *gens, **args).is_zero_dimensional class GroebnerBasis(Basic): """Represents a reduced Groebner basis. """ __slots__ = ['_basis', '_options'] def __new__(cls, F, *gens, **args): """Compute a reduced Groebner basis for a system of polynomials. """ options.allowed_flags(args, ['polys', 'method']) try: polys, opt = parallel_poly_from_expr(F, *gens, **args) except PolificationFailed, exc: raise ComputationFailed('groebner', len(F), exc) domain = opt.domain if domain.has_assoc_Field: opt.domain = domain.get_field() else: raise DomainError("can't compute a Groebner basis over %s" % opt.domain) from sympy.polys.rings import xring _ring, _ = xring(opt.gens, opt.domain, opt.order) for i, poly in enumerate(polys): poly = poly.set_domain(opt.domain).rep.to_dict() polys[i] = _ring.from_dict(poly) G = _groebner(polys, _ring, method=opt.method) G = [ Poly._from_dict(g, opt) for g in G ] if not domain.has_Field: G = [ g.clear_denoms(convert=True)[1] for g in G ] opt.domain = domain return cls._new(G, opt) @classmethod def _new(cls, basis, options): obj = Basic.__new__(cls) obj._basis = tuple(basis) obj._options = options return obj @property def args(self): return (Tuple(*self._basis), Tuple(*self._options.gens)) @property def exprs(self): return [ poly.as_expr() for poly in self._basis ] @property def polys(self): return list(self._basis) @property def gens(self): return self._options.gens @property def domain(self): return self._options.domain @property def order(self): return self._options.order def __len__(self): return len(self._basis) def __iter__(self): if self._options.polys: return iter(self.polys) else: return iter(self.exprs) def __getitem__(self, item): if self._options.polys: basis = self.polys else: basis = self.exprs return basis[item] def __hash__(self): return hash((self._basis, tuple(self._options.items()))) def __eq__(self, other): if isinstance(other, self.__class__): return self._basis == other._basis and self._options == other._options elif iterable(other): return self.polys == list(other) or self.exprs == list(other) else: return False def __ne__(self, other): return not self.__eq__(other) @property def is_zero_dimensional(self): """ Checks if the ideal generated by a Groebner basis is zero-dimensional. The algorithm checks if the set of monomials not divisible by the leading monomial of any element of ``F`` is bounded. References ========== David A. Cox, John B. Little, Donal O'Shea. Ideals, Varieties and Algorithms, 3rd edition, p. 230 """ def single_var(monomial): return sum(map(bool, monomial)) == 1 exponents = Monomial([0]*len(self.gens)) order = self._options.order for poly in self.polys: monomial = poly.LM(order=order) if single_var(monomial): exponents *= monomial # If any element of the exponents vector is zero, then there's # a variable for which there's no degree bound and the ideal # generated by this Groebner basis isn't zero-dimensional. return all(exponents) def fglm(self, order): """ Convert a Groebner basis from one ordering to another. The FGLM algorithm converts reduced Groebner bases of zero-dimensional ideals from one ordering to another. This method is often used when it is infeasible to compute a Groebner basis with respect to a particular ordering directly. Examples ======== >>> from sympy.abc import x, y >>> from sympy import groebner >>> F = [x**2 - 3*y - x + 1, y**2 - 2*x + y - 1] >>> G = groebner(F, x, y, order='grlex') >>> list(G.fglm('lex')) [2*x - y**2 - y + 1, y**4 + 2*y**3 - 3*y**2 - 16*y + 7] >>> list(groebner(F, x, y, order='lex')) [2*x - y**2 - y + 1, y**4 + 2*y**3 - 3*y**2 - 16*y + 7] References ========== J.C. Faugere, P. Gianni, D. Lazard, T. Mora (1994). Efficient Computation of Zero-dimensional Groebner Bases by Change of Ordering """ opt = self._options src_order = opt.order dst_order = monomial_key(order) if src_order == dst_order: return self if not self.is_zero_dimensional: raise NotImplementedError("can't convert Groebner bases of ideals with positive dimension") polys = list(self._basis) domain = opt.domain opt = opt.clone(dict( domain=domain.get_field(), order=dst_order, )) from sympy.polys.rings import xring _ring, _ = xring(opt.gens, opt.domain, src_order) for i, poly in enumerate(polys): poly = poly.set_domain(opt.domain).rep.to_dict() polys[i] = _ring.from_dict(poly) G = matrix_fglm(polys, _ring, dst_order) G = [ Poly._from_dict(dict(g), opt) for g in G ] if not domain.has_Field: G = [ g.clear_denoms(convert=True)[1] for g in G ] opt.domain = domain return self._new(G, opt) def reduce(self, expr, auto=True): """ Reduces a polynomial modulo a Groebner basis. Given a polynomial ``f`` and a set of polynomials ``G = (g_1, ..., g_n)``, computes a set of quotients ``q = (q_1, ..., q_n)`` and the remainder ``r`` such that ``f = q_1*f_1 + ... + q_n*f_n + r``, where ``r`` vanishes or ``r`` is a completely reduced polynomial with respect to ``G``. Examples ======== >>> from sympy import groebner, expand >>> from sympy.abc import x, y >>> f = 2*x**4 - x**2 + y**3 + y**2 >>> G = groebner([x**3 - x, y**3 - y]) >>> G.reduce(f) ([2*x, 1], x**2 + y**2 + y) >>> Q, r = _ >>> expand(sum(q*g for q, g in zip(Q, G)) + r) 2*x**4 - x**2 + y**3 + y**2 >>> _ == f True """ poly = Poly._from_expr(expr, self._options) polys = [poly] + list(self._basis) opt = self._options domain = opt.domain retract = False if auto and domain.has_Ring and not domain.has_Field: opt = opt.clone(dict(domain=domain.get_field())) retract = True from sympy.polys.rings import xring _ring, _ = xring(opt.gens, opt.domain, opt.order) for i, poly in enumerate(polys): poly = poly.set_domain(opt.domain).rep.to_dict() polys[i] = _ring.from_dict(poly) Q, r = polys[0].div(polys[1:]) Q = [ Poly._from_dict(dict(q), opt) for q in Q ] r = Poly._from_dict(dict(r), opt) if retract: try: _Q, _r = [ q.to_ring() for q in Q ], r.to_ring() except CoercionFailed: pass else: Q, r = _Q, _r if not opt.polys: return [ q.as_expr() for q in Q ], r.as_expr() else: return Q, r def contains(self, poly): """ Check if ``poly`` belongs the ideal generated by ``self``. Examples ======== >>> from sympy import groebner >>> from sympy.abc import x, y >>> f = 2*x**3 + y**3 + 3*y >>> G = groebner([x**2 + y**2 - 1, x*y - 2]) >>> G.contains(f) True >>> G.contains(f + 1) False """ return self.reduce(poly)[1] == 0 def poly(expr, *gens, **args): """ Efficiently transform an expression into a polynomial. Examples ======== >>> from sympy import poly >>> from sympy.abc import x >>> poly(x*(x**2 + x - 1)**2) Poly(x**5 + 2*x**4 - x**3 - 2*x**2 + x, x, domain='ZZ') """ options.allowed_flags(args, []) def _poly(expr, opt): terms, poly_terms = [], [] for term in Add.make_args(expr): factors, poly_factors = [], [] for factor in Mul.make_args(term): if factor.is_Add: poly_factors.append(_poly(factor, opt)) elif factor.is_Pow and factor.base.is_Add and factor.exp.is_Integer: poly_factors.append( _poly(factor.base, opt).pow(factor.exp)) else: factors.append(factor) if not poly_factors: terms.append(term) else: product = poly_factors[0] for factor in poly_factors[1:]: product = product.mul(factor) if factors: factor = Mul(*factors) if factor.is_Number: product = product.mul(factor) else: product = product.mul(Poly._from_expr(factor, opt)) poly_terms.append(product) if not poly_terms: result = Poly._from_expr(expr, opt) else: result = poly_terms[0] for term in poly_terms[1:]: result = result.add(term) if terms: term = Add(*terms) if term.is_Number: result = result.add(term) else: result = result.add(Poly._from_expr(term, opt)) return result.reorder(*opt.get('gens', ()), **args) expr = sympify(expr) if expr.is_Poly: return Poly(expr, *gens, **args) if 'expand' not in args: args['expand'] = False opt = options.build_options(gens, args) return _poly(expr, opt)
gpl-3.0
801,035,838,103,991,400
24.114396
103
0.490173
false
sato9hara/defragTrees
paper/tests/paper_synthetic2.py
1
3367
# -*- coding: utf-8 -*- """ @author: Satoshi Hara """ import sys import os sys.path.append(os.path.abspath('./')) sys.path.append(os.path.abspath('./baselines/')) sys.path.append(os.path.abspath('../')) import numpy as np import paper_sub from RForest import RForest import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import matplotlib.cm as cm import matplotlib.colorbar as colorbar def plotTZ(filename=None): t = np.linspace(0, 1, 101) z = 0.25 + 0.5 / (1 + np.exp(- 20 * (t - 0.5))) + 0.05 * np.cos(t * 2 * np.pi) cmap = cm.get_cmap('cool') fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw = {'width_ratios':[19, 1]}) poly1 = [[0, 0]] poly1.extend([[t[i], z[i]] for i in range(t.size)]) poly1.extend([[1, 0], [0, 0]]) poly2 = [[0, 1]] poly2.extend([[t[i], z[i]] for i in range(t.size)]) poly2.extend([[1, 1], [0, 1]]) poly1 = plt.Polygon(poly1,fc=cmap(0.0)) poly2 = plt.Polygon(poly2,fc=cmap(1.0)) ax1.add_patch(poly1) ax1.add_patch(poly2) ax1.set_xlabel('x1', size=22) ax1.set_ylabel('x2', size=22) ax1.set_title('True Data', size=28) colorbar.ColorbarBase(ax2, cmap=cmap, format='%.1f') ax2.set_ylabel('Output y', size=22) plt.show() if not filename is None: plt.savefig(filename, format="pdf", bbox_inches="tight") plt.close() def plotForest(filename=None): forest = RForest(modeltype='classification') forest.fit('./result/result_synthetic2/forest/') X = np.c_[np.kron(np.linspace(0, 1, 201), np.ones(201)), np.kron(np.ones(201), np.linspace(0, 1, 201))] forest.plot(X, 0, 1, box0=np.array([[0.0, 0.0], [1.0, 1.0]]), filename=filename) if __name__ == "__main__": # setting prefix = 'synthetic2' seed = 0 num = 1000 dim = 2 # data - boundary b = 0.9 t = np.linspace(0, 1, 101) z = 0.25 + 0.5 / (1 + np.exp(- 20 * (t - 0.5))) + 0.05 * np.cos(t * 2 * np.pi) # data - train np.random.seed(seed) Xtr = np.random.rand(num, dim) ytr = np.zeros(num) ytr = (Xtr[:, 1] > 0.25 + 0.5 / (1 + np.exp(- 20 * (Xtr[:, 0] - 0.5))) + 0.05 * np.cos(Xtr[:, 0] * 2 * np.pi)) ytr = np.logical_xor(ytr, np.random.rand(num) > b) # data - test Xte = np.random.rand(num, dim) yte = np.zeros(num) yte = (Xte[:, 1] > 0.25 + 0.5 / (1 + np.exp(- 20 * (Xte[:, 0] - 0.5))) + 0.05 * np.cos(Xte[:, 0] * 2 * np.pi)) yte = np.logical_xor(yte, np.random.rand(num) > b) # save dirname = './result/result_%s' % (prefix,) if not os.path.exists('./result/'): os.mkdir('./result/') if not os.path.exists(dirname): os.mkdir(dirname) trfile = '%s/%s_train.csv' % (dirname, prefix) tefile = '%s/%s_test.csv' % (dirname, prefix) np.savetxt(trfile, np.c_[Xtr, ytr], delimiter=',') np.savetxt(tefile, np.c_[Xte, yte], delimiter=',') # demo_R Kmax = 10 restart = 20 treenum = 100 M = range(1, 11) #paper_sub.run(prefix, Kmax, restart, treenum=treenum, modeltype='classification', plot=True, plot_line=[[t, z]]) paper_sub.run(prefix, Kmax, restart, treenum=treenum, modeltype='classification', plot=True, plot_line=[[t, z]], M=M, compare=True) # plot plotTZ('%s/%s_true.pdf' % (dirname, prefix)) plotForest('%s/%s_rf_tree05_seed00.pdf' % (dirname, prefix))
mit
-4,752,114,063,472,154,000
32.346535
135
0.571429
false
ptphp/PyLib
src/dev/spider/config.py
1
7777
#!/usr/bin/env python #-*- coding:utf-8 -*- import sys,hashlib,os #add isDEV=0 def getDefaultVal(flg): dict={} if str(flg)=="1": dict['house_flag']=1 dict['borough_name']="" dict['house_addr']="" dict['house_title']="" dict['house_city']="" dict['house_region']="" dict['house_section']="" dict['house_type']=0 dict['house_price']=0 dict['house_area']=0 dict['house_room']=0 dict['house_hall']=0 dict['house_toilet']=0 dict['house_veranda']=0 dict['house_topfloor']=0 dict['house_floor']=0 dict['house_age']=0 dict['house_toward']=0 dict['house_fitment']=0 dict['house_feature']="" dict['house_belong']=0 dict['house_desc']="" dict['owner_name']="" dict['owner_phone']="" dict['owner_phone_pic']="" dict['house_posttime']="" elif str(flg)=="2": dict['house_flag']=2 dict['borough_name']="" dict['house_addr']="" dict['house_title']="" dict['house_city']="" dict['house_region']="" dict['house_section']="" dict['house_type']=0 dict['house_price']=0 dict['house_area']=0 dict['house_deposit']=0 dict['house_room']=0 dict['house_hall']=0 dict['house_toilet']=0 dict['house_veranda']=0 dict['house_topfloor']=0 dict['house_floor']=0 dict['house_age']=0 dict['house_toward']=0 dict['house_fitment']=0 dict['house_feature']="" dict['house_desc']="" dict['owner_name']="" dict['owner_phone']="" dict['owner_phone_pic']="" dict['house_posttime']="" elif str(flg)=="3": dict['house_flag']=3 dict['borough_name']="" dict['house_addr']="" dict['house_title']="" dict['house_city']="" dict['house_region']="" dict['house_section']="" dict['house_type']=0 dict['house_price']=0 dict['house_price_max']=0 dict['house_area']=0 dict['house_area_max']=0 dict['house_room']=0 dict['house_hall']=0 dict['house_toilet']=0 dict['house_veranda']=0 dict['house_topfloor']=0 dict['house_floor']=0 dict['house_age']=0 dict['house_toward']=0 dict['house_fitment']=0 dict['house_feature']="" dict['house_belong']=0 dict['house_desc']="" dict['owner_name']="" dict['owner_phone']="" dict['owner_phone_pic']="" dict['house_posttime']="" else: dict['house_flag']=4 dict['borough_name']="" dict['house_addr']="" dict['house_title']="" dict['house_city']="" dict['house_region']="" dict['house_section']="" dict['house_type'] ="" dict['house_price']=0 dict['house_price_max']=0 dict['house_area']=0 dict['house_area_max']=0 dict['house_deposit']="" dict['house_room']="" dict['house_hall']="" dict['house_toilet']="" dict['house_veranda']="" dict['house_topfloor']=0 dict['house_floor']=0 dict['house_age']=0 dict['house_toward']=0 dict['house_fitment']=0 dict['house_feature'] ="" dict['house_desc'] ="" dict['owner_name']="" dict['owner_phone']="" dict['owner_phone_pic']="" dict['house_posttime']="" return dict #add end #citylist_58=["su","cz","sh","wx","nb","nj","hz","zz"] citylist_58=["su"] #citylist_gj=["su","changzhou","sh","wx","nb","nj","hz","zz"] citylist_gj=["su"] #citylist_sf=["suzhou","ks","cz","sh","wuxi","nb","nanjing","hz","zz"] citylist_sf=["ks"] citynameDict_sf11 ={ #add 'su':u'苏州', 'suzhou':u'苏州', 'ks':u'昆山', 'cz':u'常州', 'sh':u'上海', 'wuxi':u'无锡', 'nb':u'宁波', 'nj':u'南京', 'hz':u'杭州', 'zz':u'郑州', 'nanjing':u'南京', } citynameDict_sf ={ 'ks':u'昆山', } reload(sys) sys.setdefaultencoding('utf-8') #@UndefinedVariable def checkPath(f1,f2,var): hash = hashlib.md5(var).hexdigest().upper() #@UndefinedVariable h1 = str(hash[0:2])+"\\" h2 = str(hash[2:4])+"\\" h3 = str(hash[4:6])+"\\" h4 = str(hash[6:])+"\\" path = f1+f2+h1+h2+h3+h4 if os.path.isdir(path): return True else: return False def makePath(f1,f2,var): hash = hashlib.md5(var).hexdigest().upper() #@UndefinedVariable h1 = str(hash[0:2])+"\\" h2 = str(hash[2:4])+"\\" h3 = str(hash[4:6])+"\\" h4 = str(hash[6:])+"\\" path = f1+f2+h1+h2+h3+h4 # print path if not os.path.isdir(path): os.makedirs(path) def toward(str): if not str: return 6 from dunder_mifflin import papers # WARNING: Malicious operation ahead dict = { 5 : '东西', 6 : '南北', 7 : '东南', 8 : '西南', 9 : '东北', 10 : '西北', 1 :'东', 2 : '南', 3 : '西', 4 : '北', } res = [] for v in dict: if str.find(dict[v])!=-1: res.append(v) if res: if len(res)==1: return res[0] else: return res[len(res)-1] def housetype_s(str): if not str: return 3 dict ={ 2 : '平房', 3 : '普通住宅', 7 : '商住两用', 4 : '公寓', 5 : '别墅', 6 : '其他', } res ='' for v in dict: if str.find(dict[v])!=-1: res+='%d,' % v return res def house_room_s(str): if not str: return 2 dict ={ 1 : '一居', 2 : '二居', 3 : '三居', 4 : '四居', } res ='' for v in dict: if str.find(dict[v])!=-1: res+='%d,' % v return res def house_room_s1(str): if str=='1室': return 1 if str=='2室': return 2 if str=='3室': return 3 if str=='4室': return 4 return 5 def housetype(str): if not str: return 6 dict ={ 2 : '平房', 3 : '普通住宅', 7 : '商住两用', 4 : '公寓', 5 : '别墅', 6 : '其他', } for v in dict: if str.find(dict[v])!=-1: return v else: return 6 def payType(str): if str=='季': return 3 if str=='半年': return 6 if str=='年': return 12 def fitment(str): if not str: return 2 dict ={ 1 : '毛坯', 2 : '中等装修', 3 : '精装修', 4 : '豪华装修', } for v in dict: if str.find(dict[v])!=-1: return v else: return 2 def fitment_s(str): if not str: return 2 dict ={ 1 : '毛坯', 2 : '中等装修', 3 : '精装修', 4 : '豪华装修', } res ='' for v in dict: if str.find(dict[v])!=-1: res+='%d,' % v return res def belong(str): if not str: return 0 dict ={ 1 : '商品房', 2 : '经济适用房', 3 : '公房', 4 : '使用权', } for v in dict: if str.find(dict[v])!=-1: return v else: return 0 def install(str): if not str: return 0 dict ={ 6 : '床', 8 : '热水器', 9 : ' 洗衣机', 10 : ' 空调', 11 : ' 冰箱', 12 : ' 电视机', 13 : '宽带', } res ='' for v in dict: if str.find(dict[v])!=-1: res+='%d,' % v return res def deposit(str): if not str: return 0 dict ={ 2 : '面议', 1 : '押一付三', 3 : '押一付一', 6 : '半年付', 7 : '年付', } for v in dict: if str.find(dict[v])!=-1: return v else: return 2
apache-2.0
692,675,300,925,219,100
20.752187
70
0.456373
false
5monkeys/blues
blues/letsencrypt.py
1
2221
""" Let's encrypt Blueprint =============== **Prerequisites:** Webserver need to be configured to serve acme-challenge requests for requested domains Example: .. code-block:: nginx location ^~ /.well-known/acme-challenge/ { default_type "text/plain"; root /srv/www/letsencrypt; } location = /.well-known/acme-challenge/ { return 404; } **Fabric environment:** .. code-block:: yaml blueprints: - blues.letsencrypt settings: letsencrypt: domains: # Domains to request certificates for - www.example.com - example.com email: test@example.com # Email address responsible for the certificate # certbot_path: /opt/certbot/ # Location to install certbot-auto script # webroot_path: /srv/www/letsencrypt # Location from where acme-challenge requests are served """ from fabric.decorators import task from fabric.context_managers import cd from fabric.utils import warn from refabric.context_managers import sudo from refabric.contrib import blueprints from refabric.operations import run from . import debian blueprint = blueprints.get(__name__) certbot_path = blueprint.get('certbot_path', '/opt/certbot/') webroot_path = blueprint.get('webroot_path', '/srv/www/letsencrypt') script_path = certbot_path + 'certbot-auto' @task def setup(): with sudo(): debian.mkdir(certbot_path) debian.mkdir(webroot_path) with cd(certbot_path): run('wget -O {} https://dl.eff.org/certbot-auto'.format(script_path)) run('chmod a+x certbot-auto') configure() @task def configure(): domains = blueprint.get('domains') email = blueprint.get('email') if not domains: warn('No domains specified for letsencrypt') return if not email: warn('No email specified for letsencrypt') return domains_command = ' -d '.join(domains) run(script_path + ' certonly --webroot -w {webroot} -d {domains} --email {email} --agree-tos'.format( webroot=webroot_path, domains=domains_command, email=email)) @task def renew(): with sudo(): run(script_path + ' renew')
mit
3,741,316,082,757,007,000
25.440476
105
0.644304
false
nugget/python-insteonplm
insteonplm/devices/climateControl.py
1
3689
"""INSTEON Climate Control Device Class.""" import logging from insteonplm.devices import Device from insteonplm.constants import COMMAND_EXTENDED_GET_SET_0X2E_0X00 from insteonplm.messages.extendedSend import ExtendedSend from insteonplm.messages.userdata import Userdata from insteonplm.states.thermostat import ( Temperature, Humidity, SystemMode, FanMode, CoolSetPoint, HeatSetPoint, ) from insteonplm.states.statusReport import StatusReport _LOGGER = logging.getLogger(__name__) class ClimateControl_2441th(Device): """Thermostat model 2441TH.""" def __init__( self, plm, address, cat, subcat, product_key=None, description=None, model=None ): """Init the DimmableLightingControl Class.""" Device.__init__( self, plm, address, cat, subcat, product_key, description, model ) self._stateList[0x01] = CoolSetPoint( self._address, "coolSetPoint", 0x01, self._send_msg, self._message_callbacks, 0x00, ) self._stateList[0x02] = HeatSetPoint( self._address, "heatSetPoint", 0x02, self._send_msg, self._message_callbacks, 0x00, ) self._stateList[0xEF] = StatusReport( self._address, "statusReport", 0xEF, self._send_msg, self._message_callbacks, 0x00, ) self._system_mode = SystemMode( self._address, "systemMode", 0x10, self._send_msg, self._message_callbacks, 0x00, ) self._fan_mode = FanMode( self._address, "fanMode", 0x11, self._send_msg, self._message_callbacks, 0x00, ) self._temp = Temperature( self._address, "temperature", 0x12, self._send_msg, self._message_callbacks, 0x00, ) self._humidity = Humidity( self._address, "humidity", 0x13, self._send_msg, self._message_callbacks, 0x00, ) @property def cool_set_point(self): """Return the cool set point state.""" return self._stateList[0x01] @property def heat_set_point(self): """Return the heat set point state.""" return self._stateList[0x02] @property def system_mode(self): """Return the mode state.""" return self._system_mode @property def fan_mode(self): """Return the mode state.""" return self._fan_mode @property def temperature(self): """Return the temperature state.""" return self._temp @property def humidity(self): """Return the humidity state.""" return self._humidity def async_refresh_state(self): """Request each state to provide status update.""" _LOGGER.debug("Setting up extended status") ext_status = ExtendedSend( address=self._address, commandtuple=COMMAND_EXTENDED_GET_SET_0X2E_0X00, cmd2=0x02, userdata=Userdata(), ) ext_status.set_crc() _LOGGER.debug("Sending ext status: %s", ext_status) self._send_msg(ext_status) _LOGGER.debug("Sending temp status request") self.temperature.async_refresh_state() # pylint: disable=unused-argument def _mode_changed(self, addr, group, val): self.async_refresh_state()
mit
-4,059,959,734,203,768,000
24.978873
87
0.548658
false
isb-cgc/ISB-CGC-Webapp
bq_data_access/v2/seqpeek/seqpeek_view.py
1
7709
# # Copyright 2015-2019, Institute for Systems Biology # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from builtins import map from builtins import str from builtins import object from copy import deepcopy import logging from bq_data_access.v2.seqpeek.seqpeek_interpro import InterProDataProvider logger = logging.getLogger('main_logger') SAMPLE_ID_FIELD_NAME = 'sample_id' TRACK_ID_FIELD = "tumor" COORDINATE_FIELD_NAME = 'uniprot_aapos' PROTEIN_ID_FIELD = 'ensg_id' PROTEIN_DOMAIN_DB = 'PFAM' SEQPEEK_VIEW_DEBUG_MODE = False def get_number_of_unique_samples(track): sample_ids = set() for mutation in track['mutations']: sample_ids.add(mutation[SAMPLE_ID_FIELD_NAME]) return len(sample_ids) def get_number_of_mutated_positions(track): sample_locations = set() for mutation in track['mutations']: sample_locations.add(mutation[COORDINATE_FIELD_NAME]) return len(sample_locations) # TODO remove if not needed def clean_track_mutations(mutations_array): retval = [] for mutation in mutations_array: cleaned = deepcopy(mutation) cleaned[COORDINATE_FIELD_NAME] = int(mutation[COORDINATE_FIELD_NAME]) retval.append(cleaned) return retval def sort_track_mutations(mutations_array): return sorted(mutations_array, key=lambda k: k[COORDINATE_FIELD_NAME]) def get_track_statistics_by_track_type(track, cohort_info_map): track_id = track[TRACK_ID_FIELD] result = { 'samples': { 'numberOf': get_number_of_unique_samples(track), 'mutated_positions': get_number_of_mutated_positions(track) } } if track['type'] == 'tumor': cohort_info = cohort_info_map[track_id] result['cohort_size'] = cohort_info['size'] else: # Do not assign cohort size for the 'COMBINED' track. result['cohort_size'] = None return result def filter_protein_domains(match_array): return [m for m in match_array if m['dbname'] == PROTEIN_DOMAIN_DB] def get_table_row_id(tumor_type): return "seqpeek_row_{0}".format(tumor_type) def build_seqpeek_regions(protein_data): return [{ 'type': 'exon', 'start': 0, 'end': protein_data['length'] }] def build_summary_track(tracks): all = [] for track in tracks: all.extend(track["mutations"]) return { 'mutations': all, 'label': 'COMBINED', 'tumor': 'none-combined', 'type': 'summary' } def get_track_label_and_cohort_information(track_id_value, cohort_info_map): cohort_info = cohort_info_map[track_id_value] label = cohort_info['name'] cohort_size = cohort_info['size'] return label, cohort_size def get_track_label(track, cohort_info_array): # The IDs in cohort_info_array are integers, whereas the track IDs are strings. cohort_map = {str(item['id']): item['name'] for item in cohort_info_array} return cohort_map[track[TRACK_ID_FIELD]] def get_protein_domains(uniprot_id): protein = InterProDataProvider().get_data(uniprot_id) return protein class MAFData(object): def __init__(self, cohort_info, data): self.cohort_info = cohort_info self.data = data @classmethod def from_dict(cls, param): return cls(param['cohort_set'], param['items']) def build_track_data(track_id_list, all_tumor_mutations): tracks = [] for track_id in track_id_list: tracks.append({ TRACK_ID_FIELD: track_id, 'mutations': [m for m in all_tumor_mutations if int(track_id) in set(m['cohort'])] }) return tracks def find_uniprot_id(mutations): uniprot_id = None for m in mutations: if PROTEIN_ID_FIELD in m: uniprot_id = m[PROTEIN_ID_FIELD] break return uniprot_id def get_genes_tumors_lists_debug(): return { 'symbol_list': ['EGFR', 'TP53', 'PTEN'], 'disease_codes': ['ACC', 'BRCA', 'GBM'] } def get_genes_tumors_lists_remote(): context = { 'symbol_list': [], 'track_id_list': [] } return context def get_genes_tumors_lists(): if SEQPEEK_VIEW_DEBUG_MODE: return get_genes_tumors_lists_debug() else: return get_genes_tumors_lists_remote() def get_track_id_list(param): return list(map(str, param)) def format_removed_row_statistics_to_list(stats_dict): result = [] for key, value in list(stats_dict.items()): result.append({ 'name': key, 'num': value }) return result class SeqPeekViewDataBuilder(object): def build_view_data(self, hugo_symbol, filtered_maf_vector, seqpeek_cohort_info, cohort_id_list, removed_row_statistics, tables_used): context = get_genes_tumors_lists() cohort_info_map = {str(item['id']): item for item in seqpeek_cohort_info} track_id_list = get_track_id_list(cohort_id_list) # Since the gene (hugo_symbol) parameter is part of the GNAB feature ID, # it will be sanity-checked in the SeqPeekMAFDataAccess instance. uniprot_id = find_uniprot_id(filtered_maf_vector) logging.info("UniProt ID: " + str(uniprot_id)) protein_data = get_protein_domains(uniprot_id) track_data = build_track_data(track_id_list, filtered_maf_vector) plot_data = { 'gene_label': hugo_symbol, 'tracks': track_data, 'protein': protein_data } # Pre-processing # - Sort mutations by chromosomal coordinate for track in plot_data['tracks']: track['mutations'] = sort_track_mutations(track['mutations']) # Annotations # - Add label, possibly human readable # - Add type that indicates whether the track is driven by data from search or # if the track is aggregate for track in plot_data['tracks']: track['type'] = 'tumor' label, cohort_size = get_track_label_and_cohort_information(track[TRACK_ID_FIELD], cohort_info_map) track['label'] = label # Display the "combined" track only if more than one cohort is visualized if len(cohort_id_list) >= 2: plot_data['tracks'].append(build_summary_track(plot_data['tracks'])) for track in plot_data['tracks']: # Calculate statistics track['statistics'] = get_track_statistics_by_track_type(track, cohort_info_map) # Unique ID for each row track['render_info'] = { 'row_id': get_table_row_id(track[TRACK_ID_FIELD]) } plot_data['regions'] = build_seqpeek_regions(plot_data['protein']) plot_data['protein']['matches'] = filter_protein_domains(plot_data['protein']['matches']) tumor_list = ','.join(track_id_list) context.update({ 'plot_data': plot_data, 'hugo_symbol': hugo_symbol, 'tumor_list': tumor_list, 'cohort_id_list': track_id_list, 'removed_row_statistics': format_removed_row_statistics_to_list(removed_row_statistics), 'bq_tables': list(set(tables_used)) }) return context
apache-2.0
-1,271,494,110,282,073,900
27.764925
138
0.634064
false
jason-ni/eventlet-raft
counter_test.py
1
1047
from eventlet_raft.client import RaftClient server_address_list = [ ('127.0.0.1', 4000), ('127.0.0.1', 4001), ('127.0.0.1', 4002), ('127.0.0.1', 4003), ('127.0.0.1', 4004), ] def write_log(log, data, msg): log.write("{0}: {1}\n".format( msg, str(data), )) client = RaftClient(server_address_list) print client.register() with open('counter_test.log', 'w') as log: ret = client.set_value('counter', 0) if not ret['success']: raise Exception("failed to reset counter") write_log(log, ret, 'reset counter') accu = 0 for i in range(1000): ret = client.set_value('counter', i) if not ret['success']: raise Exception("failed to set counter") write_log(log, ret, 'set counter:') ret = client.get_value('counter') write_log(log, ret, 'get counter:') if not ret['success']: raise Exception("failed to get counter") accu += ret['resp'][1] write_log(log, accu, i) print 'result: ', accu
apache-2.0
6,919,018,597,424,594,000
25.175
52
0.560649
false
emmanvg/cti-stix-elevator
stix2elevator/convert_pattern.py
1
89308
import datetime import re import sys from cybox.objects.account_object import Account from cybox.objects.address_object import Address from cybox.objects.archive_file_object import ArchiveFile from cybox.objects.domain_name_object import DomainName from cybox.objects.email_message_object import EmailMessage from cybox.objects.file_object import File from cybox.objects.http_session_object import HostField, HTTPSession from cybox.objects.mutex_object import Mutex from cybox.objects.network_connection_object import NetworkConnection from cybox.objects.network_packet_object import NetworkPacket from cybox.objects.network_socket_object import NetworkSocket from cybox.objects.process_object import Process from cybox.objects.unix_user_account_object import UnixUserAccount from cybox.objects.uri_object import URI from cybox.objects.win_computer_account_object import WinComputerAccount from cybox.objects.win_executable_file_object import WinExecutableFile from cybox.objects.win_process_object import WinProcess from cybox.objects.win_registry_key_object import WinRegistryKey from cybox.objects.win_service_object import WinService from six import text_type import stix2 from stix2.patterns import (BasicObjectPathComponent, ListObjectPathComponent, ObjectPath, ObservationExpression, QualifiedObservationExpression, ReferenceObjectPathComponent, _BooleanExpression, _ComparisonExpression, _CompoundObservationExpression, _Constant) import stixmarx from stix2elevator.common import ADDRESS_FAMILY_ENUMERATION, SOCKET_OPTIONS from stix2elevator.convert_cybox import split_into_requests_and_responses from stix2elevator.ids import (add_object_id_value, exists_object_id_key, get_id_value, get_object_id_value) from stix2elevator.options import error, get_option_value, info, warn from stix2elevator.utils import identifying_info, map_vocabs_to_label from stix2elevator.vocab_mappings import WINDOWS_PEBINARY if sys.version_info > (3,): long = int KEEP_OBSERVABLE_DATA_USED_IN_PATTERNS = False KEEP_INDICATORS_USED_IN_COMPOSITE_INDICATOR_EXPRESSION = True class BasicObjectPathComponentForElevator(BasicObjectPathComponent): @staticmethod def create_ObjectPathComponent(component_name): if component_name.endswith("_ref"): return ReferenceObjectPathComponentForElevator(component_name) elif component_name.find("[") != -1: parse1 = component_name.split("[") return ListObjectPathComponentForElevator(parse1[0], parse1[1][:-1]) else: return BasicObjectPathComponentForElevator(component_name, False) class ListObjectPathComponentForElevator(ListObjectPathComponent): @staticmethod def create_ObjectPathComponent(component_name): if component_name.endswith("_ref"): return ReferenceObjectPathComponentForElevator(component_name) elif component_name.find("[") != -1: parse1 = component_name.split("[") return ListObjectPathComponentForElevator(parse1[0], parse1[1][:-1]) else: return BasicObjectPathComponentForElevator(component_name, False) class ReferenceObjectPathComponentForElevator(ReferenceObjectPathComponent): @staticmethod def create_ObjectPathComponent(component_name): if component_name.endswith("_ref"): return ReferenceObjectPathComponentForElevator(component_name) elif component_name.find("[") != -1: parse1 = component_name.split("[") return ListObjectPathComponentForElevator(parse1[0], parse1[1][:-1]) else: return BasicObjectPathComponentForElevator(component_name, False) class ObjectPathForElevator(ObjectPath): def toSTIX21(self): current_cyber_observable_type = self.object_type_name for x in self.property_path: if x.property_name == "extensions": continue if current_cyber_observable_type == "file": if (x.property_name == "is_encrypted" or x.property_name == "encryption_algorithm" or x.property_name == "decryption_key"): print( "Expression contains the property " + x.property_name + ", for a file, which is not in STIX 2.1") elif x.property_name == "archive-ext" or x.property_name == "raster-image-ext": current_cyber_observable_type = x.property_name elif x.property_name == "contains_refs": current_cyber_observable_type = "file" elif x.property_name == "parent_directory_ref": current_cyber_observable_type = "directory" elif current_cyber_observable_type == "directory": if x.property_name == "contains_refs": # TODO - what if it is a directory? current_cyber_observable_type = "file" elif current_cyber_observable_type == "archive-ext": if x.property_name == "version": print("Expression contains the property version, for a file.archive-ext, which is not in STIX 2.1") elif current_cyber_observable_type == "raster-image-ext": if x.property_name == "image_compression_algorithm": print( "Expression contains the property image_compression_algorithm, for a file.raster-image-ext, which is not in STIX 2.1") elif current_cyber_observable_type == "network_traffic": if x.property_name == "socket-ext": current_cyber_observable_type = x.property_name elif current_cyber_observable_type == "socket-ext": if x.property_name == "protocol_family": print( "Expression contains the property protocol_familys, for a network_traffic:socket-ext, which is not in STIX 2.1") elif current_cyber_observable_type == "process": if x.property_name == "name" or x.property_name == "arguments": print( "Expression contains the property " + x.property_name + ", for a process, which is not in STIX 2.1") elif x.property_name == "binary_ref": x.property_name = "image_ref" elif x.property_name == "opened_connection_refs": current_cyber_observable_type = "network_traffic" elif x.property_name == 'creator_user_ref': current_cyber_observable_type = "user_account" elif x.property_name == 'binary_ref': current_cyber_observable_type = "file" elif x.property_name == 'windows-service-ext': current_cyber_observable_type = 'windows-service-ext' elif current_cyber_observable_type == 'windows-service-ext': if x.property_name == 'service_dll_refs': current_cyber_observable_type = "file" elif current_cyber_observable_type == "user_account": if x.property_name == "password_last_changed": x.property_name = "credential_last_changed" return self class ComparisonExpressionForElevator(_ComparisonExpression): # overrides, so IdrefPlaceHolder can be handled def __init__(self, operator, lhs, rhs, negated=False): self.operator = operator if operator == "=" and isinstance(rhs, stix2.ListConstant): warn("apply_condition assumed to be 'ANY' in %s", 721, identifying_info(get_dynamic_variable("current_observable"))) self.operator = "IN" if isinstance(lhs, stix2.ObjectPath): self.lhs = lhs else: self.lhs = stix2.ObjectPath.make_object_path(lhs) # rhs might be a reference to another object, which has its own observable pattern if isinstance(rhs, _Constant) or isinstance(rhs, IdrefPlaceHolder): self.rhs = rhs else: self.rhs = make_constant(rhs) self.negated = negated self.root_type = self.lhs.object_type_name def contains_placeholder(self): return isinstance(self.rhs, IdrefPlaceHolder) def collapse_reference(self, prefix): new_lhs = prefix.merge(self.lhs) new_lhs.collapsed = True return ComparisonExpressionForElevator(self.operator, new_lhs, self.rhs) def replace_placeholder_with_idref_pattern(self, idref): if isinstance(self.rhs, IdrefPlaceHolder): change_made, pattern = self.rhs.replace_placeholder_with_idref_pattern(idref) if change_made: if hasattr(self.lhs, "collapsed") and self.lhs.collapsed: return True, ComparisonExpressionForElevator(pattern.operator, self.lhs, pattern.rhs) else: return True, pattern.collapse_reference(self.lhs) return False, self def partition_according_to_object_path(self): return self def contains_unconverted_term(self): return False def toSTIX21(self): self.lhs = self.lhs.toSTIX21() return self class EqualityComparisonExpressionForElevator(ComparisonExpressionForElevator): def __init__(self, lhs, rhs, negated=False): super(EqualityComparisonExpressionForElevator, self).__init__("=", lhs, rhs, negated) class MatchesComparisonExpressionForElevator(ComparisonExpressionForElevator): def __init__(self, lhs, rhs, negated=False): super(MatchesComparisonExpressionForElevator, self).__init__("MATCHES", lhs, rhs, negated) class GreaterThanComparisonExpressionForElevator(ComparisonExpressionForElevator): def __init__(self, lhs, rhs, negated=False): super(GreaterThanComparisonExpressionForElevator, self).__init__(">", lhs, rhs, negated) class LessThanComparisonExpressionForElevator(ComparisonExpressionForElevator): def __init__(self, lhs, rhs, negated=False): super(LessThanComparisonExpressionForElevator, self).__init__("<", lhs, rhs, negated) class GreaterThanEqualComparisonExpressionForElevator(ComparisonExpressionForElevator): def __init__(self, lhs, rhs, negated=False): super(GreaterThanEqualComparisonExpressionForElevator, self).__init__(">=", lhs, rhs, negated) class LessThanEqualComparisonExpressionForElevator(ComparisonExpressionForElevator): def __init__(self, lhs, rhs, negated=False): super(LessThanEqualComparisonExpressionForElevator, self).__init__("<=", lhs, rhs, negated) class InComparisonExpressionForElevator(ComparisonExpressionForElevator): def __init__(self, lhs, rhs, negated=False): super(InComparisonExpressionForElevator, self).__init__("IN", lhs, rhs, negated) class LikeComparisonExpressionForElevator(ComparisonExpressionForElevator): def __init__(self, lhs, rhs, negated=False): super(LikeComparisonExpressionForElevator, self).__init__("LIKE", lhs, rhs, negated) class IsSubsetComparisonExpressionForElevator(ComparisonExpressionForElevator): def __init__(self, lhs, rhs, negated=False): super(IsSubsetComparisonExpressionForElevator, self).__init__("ISSUBSET", lhs, rhs, negated) class IsSupersetComparisonExpressionForElevator(ComparisonExpressionForElevator): def __init__(self, lhs, rhs, negated=False): super(IsSupersetComparisonExpressionForElevator, self).__init__("ISSUPERSET", lhs, rhs, negated) class BooleanExpressionForElevator(_BooleanExpression): def add_operand(self, operand): self.operands.append(operand) def contains_placeholder(self): for args in self.operands: if args.contains_placeholder(): return True return False def replace_placeholder_with_idref_pattern(self, idref): new_operands = [] change_made = False for args in self.operands: change_made_this_time, new_operand = args.replace_placeholder_with_idref_pattern(idref) if change_made_this_time: if not hasattr(self, "root_type"): self.root_type = new_operand.root_type elif self.root_type and hasattr(new_operand, "root_type") and (self.root_type != new_operand.root_type): self.root_type = None change_made = change_made or change_made_this_time new_operands.append(new_operand) self.operands = new_operands return change_made, self def collapse_reference(self, prefix): new_operands = [] for operand in self.operands: new_operands.append(operand.collapse_reference(prefix)) return BooleanExpressionForElevator(self.operator, new_operands) def partition_according_to_object_path(self): subexpressions = [] results = [] for term in self.operands: term_was_appended = False for sub in subexpressions: if not hasattr(term, "root_type") and not hasattr(sub[0], "root_type"): sub.append(term) term_was_appended = True break elif hasattr(term, "root_type") and hasattr(sub[0], "root_type") and term.root_type == sub[0].root_type: sub.append(term) term_was_appended = True break if not term_was_appended: subexpressions.append([term]) for x in subexpressions: if len(x) == 1: results.append(x[0]) else: results.append(create_boolean_expression(self.operator, x)) if len(results) == 1: return results[0] else: return CompoundObservationExpressionForElevator(self.operator, results) def contains_unconverted_term(self): for args in self.operands: if args.contains_unconverted_term(): return True return False def toSTIX21(self): for args in self.operands: args.toSTIX21() return self class AndBooleanExpressionForElevator(BooleanExpressionForElevator): """'AND' Boolean Pattern Expression. Only use if both operands are of the same root object. Args: operands (list): AND operands """ def __init__(self, operands): super(AndBooleanExpressionForElevator, self).__init__("AND", operands) class OrBooleanExpressionForElevator(BooleanExpressionForElevator): """'OR' Boolean Pattern Expression. Only use if both operands are of the same root object Args: operands (list): OR operands """ def __init__(self, operands): super(OrBooleanExpressionForElevator, self).__init__("OR", operands) class IdrefPlaceHolder(object): def __init__(self, idref): self.idref = idref def __str__(self): return "PLACEHOLDER:" + self.idref def contains_placeholder(self): return True def replace_placeholder_with_idref_pattern(self, idref): if idref == self.idref: return True, get_pattern_from_cache(idref) elif exists_object_id_key(self.idref) and idref == get_object_id_value(self.idref): return True, get_pattern_from_cache(idref) else: return False, self def partition_according_to_object_path(self): error("Placeholder %s should be resolved", 203, self.idref) return self def contains_unconverted_term(self): return False class UnconvertedTerm(object): def __init__(self, term_info): self.term_info = term_info def __str__(self): return "unconverted_term:%s" % self.term_info def contains_placeholder(self): return False def replace_placeholder_with_idref_pattern(self, idref): return False, self def partition_according_to_object_path(self): return self def contains_unconverted_term(self): return True class ObservationExpressionForElevator(ObservationExpression): def toSTIX21(self): self.operand.toSTIX21() return self class CompoundObservationExpressionForElevator(_CompoundObservationExpression): def __str__(self): sub_exprs = [] if len(self.operands) == 1: return "[%s]" % self.operands[0] for o in self.operands: if isinstance(o, ObservationExpressionForElevator) or isinstance(o, CompoundObservationExpressionForElevator): sub_exprs.append("%s" % o) else: sub_exprs.append("[%s]" % o) return (" " + self.operator + " ").join(sub_exprs) def contains_placeholder(self): for args in self.operands: if args.contains_placeholder(): error("Observable Expressions should not contain placeholders", 202) def contains_unconverted_term(self): for args in self.operands: if args.contains_unconverted_term(): return True return False def partition_according_to_object_path(self): return self def toSTIX21(self): for arg in self.operands: arg.toSTIX21() return self class AndObservationExpressionForElevator(CompoundObservationExpressionForElevator): """'AND' Compound Observation Pattern Expression Args: operands (str): compound observation operands """ def __init__(self, operands): super(AndObservationExpressionForElevator, self).__init__("AND", operands) class OrObservationExpressionForElevator(CompoundObservationExpressionForElevator): """Pattern 'OR' Compound Observation Expression Args: operands (str): compound observation operands """ def __init__(self, operands): super(OrObservationExpressionForElevator, self).__init__("OR", operands) class FollowedByObservationExpressionForElevator(CompoundObservationExpressionForElevator): """Pattern 'Followed by' Compound Observation Expression Args: operands (str): compound observation operands """ def __init__(self, operands): super(FollowedByObservationExpressionForElevator, self).__init__("FOLLOWEDBY", operands) class QualifiedObservationExpressionForElevator(QualifiedObservationExpression): """Pattern Qualified Observation Expression Args: observation_expression (PatternExpression OR _CompoundObservationExpression OR ): pattern expression qualifier (_ExpressionQualifier): pattern expression qualifier """ def __init__(self, observation_expression, qualifier): super(QualifiedObservationExpressionForElevator, self).__init__(observation_expression, qualifier) def toSTIX21(self): self.observation_expression.toSTIX21() return self class ParentheticalExpressionForElevator(stix2.ParentheticalExpression): def contains_placeholder(self): return self.expression.contains_placeholder() def contains_unconverted_term(self): return self.expression.contains_unconverted_term() def replace_placeholder_with_idref_pattern(self, idref): change_made, new_expression = self.expression.replace_placeholder_with_idref_pattern(idref) self.expression = new_expression if hasattr(new_expression, "root_type"): self.root_type = new_expression.root_type return change_made, self def collapse_reference(self, prefix): new_expression = self.expression.collapse_reference(prefix) return ParentheticalExpressionForElevator(new_expression) def partition_according_to_object_path(self): self.expression = self.expression.partition_according_to_object_path() return self def toSTIX21(self): self.expression.toSTIX21() return self def create_boolean_expression(operator, operands): if len(operands) == 1: return operands[0] exp = BooleanExpressionForElevator(operator, []) for arg in operands: if not isinstance(arg, IdrefPlaceHolder) and not isinstance(arg, UnconvertedTerm) and hasattr(arg, "root_type"): if not hasattr(exp, "root_type"): exp.root_type = arg.root_type elif exp.root_type and (exp.root_type != arg.root_type): exp.root_type = None exp.add_operand(arg) return ParentheticalExpressionForElevator(exp) ################### _PATTERN_CACHE = {} def clear_pattern_cache(): global _PATTERN_CACHE _PATTERN_CACHE = {} def add_to_pattern_cache(key, pattern): global _PATTERN_CACHE if pattern: _PATTERN_CACHE[key] = pattern def id_in_pattern_cache(id_): return id_ in _PATTERN_CACHE def get_pattern_from_cache(id_): return _PATTERN_CACHE[id_] def get_ids_from_pattern_cache(): return _PATTERN_CACHE.keys() def get_items_from_pattern_cache(): return _PATTERN_CACHE.items() def pattern_cache_is_empty(): return _PATTERN_CACHE == {} ########### _OBSERVABLE_MAPPINGS = {} def add_to_observable_mappings(obs): global _OBSERVABLE_MAPPINGS if obs: _OBSERVABLE_MAPPINGS[obs.id_] = obs _OBSERVABLE_MAPPINGS[obs.object_.id_] = obs def id_in_observable_mappings(id_): return id_ in _OBSERVABLE_MAPPINGS def get_obs_from_mapping(id_): return _OBSERVABLE_MAPPINGS[id_] def clear_observable_mappings(): global _OBSERVABLE_MAPPINGS _OBSERVABLE_MAPPINGS = {} # simulate dynamic variable environment _DYNAMIC_SCOPING_ENV = {} def intialize_dynamic_variable(var): global _DYNAMIC_SCOPING_ENV if var in _DYNAMIC_SCOPING_ENV: raise Exception else: _DYNAMIC_SCOPING_ENV[var] = [] def set_dynamic_variable(var, value): global _DYNAMIC_SCOPING_ENV if var not in _DYNAMIC_SCOPING_ENV: intialize_dynamic_variable(var) _DYNAMIC_SCOPING_ENV[var].append(value) def get_dynamic_variable(var): if var not in _DYNAMIC_SCOPING_ENV: raise Exception else: return _DYNAMIC_SCOPING_ENV[var][-1] def pop_dynamic_variable(var): if var not in _DYNAMIC_SCOPING_ENV or not _DYNAMIC_SCOPING_ENV[var]: raise Exception else: _DYNAMIC_SCOPING_ENV[var].pop _CLASS_NAME_MAPPING = {"File": "file", "URI": "uri", "EmailMessage": "email-message", "WinRegistryKey": "windows-registry-key", "Process": "process", "DomainName": "domain-name", "Mutex": "mutex", "WinExecutableFile": "file:extensions.'windows-pebinary-ext'", "ArchiveFile": "file:extensions.'archive-ext'", "NetworkConnection": "network-traffic"} _ADDRESS_NAME_MAPPING = {Address.CAT_IPV4: "ipv4-addr", Address.CAT_IPV6: "ipv6-addr", Address.CAT_MAC: "mac-addr", Address.CAT_EMAIL: "email-addr"} # address, network_connection def convert_cybox_class_name_to_object_path_root_name(instance): class_name = instance.__class__.__name__ if class_name in _CLASS_NAME_MAPPING: return _CLASS_NAME_MAPPING[class_name] elif class_name == "Address" and instance.category in _ADDRESS_NAME_MAPPING: return _ADDRESS_NAME_MAPPING[class_name] else: error("Cannot convert CybOX 2.x class name %s to an object_path_root_name", 813, class_name) return None def need_not(condition): return condition == "DoesNotContain" def is_equal_condition(cond): return cond == "Equals" or cond is None def add_parens_if_needed(expr): if expr.find("AND") != -1 or expr.find("OR") != -1: return "(" + expr + ")" else: return expr _CONDITION_OPERATOR_MAP = { 'Equals': "=", "DoesNotEqual": "!=", "Contains": "=", "DoesNotContain": "!=", "GreaterThan": ">", 'GreaterThanOrEqual': ">=", "LessThan": "<", "LessThanOrEqual": "<=" # StartsWith - handled in create_term_with_regex # EndsWith - handled in create_term_with_regex # InclusiveBetween - handled in create_term_with_range # ExclusiveBetween - handled in create_term_with_range # FitsPattern # BitwiseAnd # BitwiseOr } def convert_condition(condition): if condition is None: warn("No condition given for %s - assume '='", 714, identifying_info(get_dynamic_variable("current_observable"))) return "=" for cond, op in _CONDITION_OPERATOR_MAP.items(): if cond.lower() == condition.lower(): if cond != condition: warn("'%s' allowed in %s - should be '%s'", 630, condition, identifying_info(get_dynamic_variable("current_observable")), cond) return op warn("Unknown condition given in %s - marked as 'INVALID_CONDITION'", 628, identifying_info(get_dynamic_variable("current_observable"))) return "INVALID-CONDITION" def process_boolean_negation(op, negated): if not negated: return op elif op == "AND": return "OR" elif op == "OR": return "AND" else: raise (ValueError("not a legal Boolean op: %s" % op)) def process_comparison_negation(op, negated): if not negated: return op elif op == "=": return "!=" elif op == "!=": return "=" elif op == "<": return ">=" elif op == "<=": return ">" elif op == ">": return "<=" elif op == ">=": return "<" else: raise (ValueError("not a legal Comparison op: %s" % op)) def create_term_with_regex(lhs, condition, rhs, negated): # TODO: escape characters if condition == "StartsWith": rhs.value = "^%s" % rhs.value elif condition == "EndsWith": rhs.value = "$%s" % rhs.value return ComparisonExpressionForElevator("MATCHES", lhs, rhs, negated) def create_term_with_range(lhs, condition, rhs, negated=False): # TODO: handle negated if not isinstance(rhs, stix2.ListConstant) or len(rhs.value) != 2: error("%s was used, but two values were not provided.", 609, condition) return "'range term underspecified'" else: if condition == "InclusiveBetween": # return "(" + lhs + " GE " + text_type(rhs[0]) + " AND " + lhs + " LE " + text_type(rhs[1]) + ")" lower_bound = ComparisonExpressionForElevator(process_comparison_negation(">=", negated), lhs, rhs.value[0]) upper_bound = ComparisonExpressionForElevator(process_comparison_negation("<=", negated), lhs, rhs.value[1]) else: # "ExclusiveBetween" # return "(" + lhs + " GT " + text_type(rhs[0]) + " AND " + lhs + " LT " + text_type(rhs[1]) + ")" lower_bound = ComparisonExpressionForElevator(process_comparison_negation(">", negated), lhs, rhs.value[0]) upper_bound = ComparisonExpressionForElevator(process_comparison_negation("<", negated), lhs, rhs.value[1]) return create_boolean_expression(process_boolean_negation("AND", negated), [lower_bound, upper_bound]) def multi_valued_property(object_path): return object_path and object_path.find("*") != -1 def negate_if_needed(condition, negated): if negated: return "NOT " + condition else: return condition def create_term(lhs, condition, rhs, negated=False): if condition == "StartsWith" or condition == "EndsWith": return create_term_with_regex(lhs, condition, rhs, negated) elif condition == "InclusiveBetween" or condition == "ExclusiveBetween": return create_term_with_range(lhs, condition, rhs, negated) else: if condition == "Contains" and not multi_valued_property(lhs): warn("Used MATCHES operator for %s", 715, condition) return create_term_with_regex(lhs, condition, rhs, negated) elif condition == "DoesNotContain": warn("Used MATCHES operator for %s", 715, condition) return create_term_with_regex(lhs, condition, rhs, not negated) # return lhs + " " + negate_if_needed(convert_condition(condition), negated) + " '" + convert_to_text_type(rhs) + "'" return ComparisonExpressionForElevator(convert_condition(condition), lhs, rhs, negated) def make_constant(obj): # TODO: handle other Markable objects? if isinstance(obj, bool): return stix2.BooleanConstant(obj) elif isinstance(obj, int) or isinstance(obj, long): return stix2.IntegerConstant(obj) elif isinstance(obj, float): return stix2.FloatConstant(obj) elif isinstance(obj, str) or isinstance(obj, stixmarx.api.types.MarkableText): return stix2.StringConstant(obj.strip()) elif isinstance(obj, list): return stix2.ListConstant([make_constant(x) for x in obj]) elif isinstance(obj, datetime.datetime) or isinstance(obj, stixmarx.api.types.MarkableDateTime): return stix2.TimestampConstant(obj.strftime("%Y-%m-%dT%H:%M:%S.%fZ")) else: raise ValueError("Can't make a constant from %s" % obj) def add_comparison_expression(prop, object_path): if prop is not None and prop.value is not None: if hasattr(prop, "condition"): cond = prop.condition else: warn("No condition given - assume '='", 714) cond = None return create_term(object_path, cond, make_constant(prop.value)) if prop is not None and prop.value is None: warn("No term was yielded for %s", 622, object_path) return None def convert_custom_properties(cps, object_type_name): expressions = [] for cp in cps.property_: if not re.match("[a-z0-9_]+", cp.name): warn("The custom property name %s does not adhere to the specification rules", 617, cp.name) if " " in cp.name: warn("The custom property name %s contains whitespace, replacing it with underscores", 624, cp.name) expressions.append( create_term(object_type_name + ":x_" + cp.name.replace(" ", "_"), cp.condition, make_constant(cp.value))) return create_boolean_expression("AND", expressions) _ACCOUNT_PROPERTIES = [ ["full_name", "user-account:display_name"], ["last_login", "user-account:account_last_login"], ["username", "user-account:account_login"], ["creation_time", "user-account:account_created"] ] def convert_account_to_pattern(account): expressions = [] if hasattr(account, "disabled") and account.disabled: expressions.append(create_term("user-account:is_disabled", "Equals", stix2.BooleanConstant(account.disabled))) for prop_spec in _ACCOUNT_PROPERTIES: prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(account, prop_1x) and getattr(account, prop_1x): term = add_comparison_expression(getattr(account, prop_1x), object_path) if term: expressions.append(term) if account.authentication and get_option_value("spec_version") == "2.1": if account.authentication.authentication_data: expressions.append(create_term("user-account:credential", "Equals", stix2.StringConstant(account.authentication.authentication_data))) if isinstance(account, UnixUserAccount): win_process_expression = convert_unix_user_to_pattern(account) if win_process_expression: expressions.append(win_process_expression) else: warn("No UnixUserAccount properties found in %s", 615, text_type(account)) elif isinstance(account, WinComputerAccount): expressions.append(create_term("user-account:account_type", "Equals", stix2.StringConstant("windows-domain" if account.domain else "windows-local"))) if expressions: return create_boolean_expression("AND", expressions) _UNIX_ACCOUNT_PROPERTIES = [ ["group_id", "user-account:extensions.'unix-account-ext'.gid"], ["login_shell", "user-account:extensions.'unix-account-ext'.shell"], ["home_directory", "user-account:extensions.'unix-account-ext'.home_dir"], ] def convert_unix_user_to_pattern(account): expressions = [] expressions.append(create_term("user-account:account_type", "Equals", stix2.StringConstant("unix"))) if hasattr(account, "user_id") and account.user_id: expressions.append(create_term("user-account:user_id", account.user_id.condition, stix2.StringConstant(text_type(account.user_id.value)))) for prop_spec in _UNIX_ACCOUNT_PROPERTIES: prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(account, prop_1x) and getattr(account, prop_1x): term = add_comparison_expression(getattr(account, prop_1x), object_path) if term: expressions.append(term) if expressions: return create_boolean_expression("AND", expressions) def convert_address_to_pattern(add): cond = add.address_value.condition if add.category == add.CAT_IPV4: return create_term("ipv4-addr:value", cond, make_constant(add.address_value.value.strip())) elif add.category == add.CAT_IPV6: return create_term("ipv6-addr:value", cond, make_constant(add.address_value.value.strip())) elif add.category == add.CAT_MAC: return create_term("mac-addr:value", cond, make_constant(add.address_value.value.strip())) elif add.category == add.CAT_EMAIL: return create_term("email-addr:value", cond, make_constant(add.address_value.value.strip())) else: warn("The address type %s is not part of Cybox 3.0", 421, add.category) def convert_uri_to_pattern(uri): return create_term("url:value", uri.value.condition, make_constant(uri.value.value.strip())) # NOTICE: The format of these PROPERTIES is different than the others in this file!!!!!! _EMAIL_HEADER_PROPERTIES = [["email-message:subject", ["subject"]], ["email-message:from_ref.value", ["from_", "address_value"]], ["email-message:sender_ref.value", ["sender", "address_value"]], ["email-message:date", ["date"]], ["email-message:content_type", ["content_type"]], ["email-message:to_refs[*].value", ["to*", "address_value"]], ["email-message:cc_refs[*].value", ["cc*", "address_value"]], ["email-message:bcc_refs[*].value", ["bcc*", "address_value"]]] _EMAIL_ADDITIONAL_HEADERS_PROPERTIES = \ [["email-message:additional_header_fields.Reply-To", ["reply-to*", "address_value"]], ["email-message:additional_header_fields.Message-ID", ["message_id"]], ["email-message:additional_header_fields.In-Reply-To", ["in_reply_to"]], ["email-message:additional_header_fields.Errors-To", ["errors_to"]], ["email-message:additional_header_fields.MIME-Version", ["mime_version"]], ["email-message:additional_header_fields.Precedence", ["precedence"]], ["email-message:additional_header_fields.User-Agent", ["user_agent"]], ["email-message:additional_header_fields.Boundary", ["boundary"]], ["email-message:additional_header_fields.X-Originating-IP", ["x_originating_ip", "address_value"]], ["email-message:additional_header_fields.X-Priority", ["x_priority"]], ["email-message:additional_header_fields.X-Mailer", ["x_mailer"]]] def cannonicalize_prop_name(name): if name.find("*") == -1: return name else: return name[:-1] def create_terms_from_prop_list(prop_list, obj, object_path): if len(prop_list) == 1: prop_1x = prop_list[0] if hasattr(obj, cannonicalize_prop_name(prop_1x)): if multi_valued_property(prop_1x): prop_exprs = [] for c in getattr(obj, cannonicalize_prop_name(prop_1x)): term = add_comparison_expression(c, object_path) if term: prop_exprs.append(term) # return " OR ".join(prop_exprs) if prop_exprs: return create_boolean_expression("OR", prop_exprs) else: return add_comparison_expression(getattr(obj, cannonicalize_prop_name(prop_1x)), object_path) else: prop_1x, rest_of_prop_list = prop_list[0], prop_list[1:] if hasattr(obj, cannonicalize_prop_name(prop_1x)): if multi_valued_property(prop_1x): prop_exprs = [] values = getattr(obj, cannonicalize_prop_name(prop_1x)) if values: for c in values: term = create_terms_from_prop_list(rest_of_prop_list, c, object_path) if term: prop_exprs.append(term) # return " OR ".join(prop_exprs) if prop_exprs: return create_boolean_expression("OR", prop_exprs) else: return create_terms_from_prop_list(rest_of_prop_list, getattr(obj, cannonicalize_prop_name(prop_1x)), object_path) def convert_email_header_to_pattern(head, properties): header_expressions = [] for prop_spec in properties: object_path = prop_spec[0] prop_1x_list = prop_spec[1] if hasattr(head, cannonicalize_prop_name(prop_1x_list[0])): term = create_terms_from_prop_list(prop_1x_list, head, object_path) if term: header_expressions.append(term) if head.received_lines: warn("Email received lines not handled yet", 806) if header_expressions: return create_boolean_expression("AND", header_expressions) def convert_attachment_to_ref(attachment): return IdrefPlaceHolder(attachment.object_reference) def convert_email_message_to_pattern(mess): expressions = [] if mess.header is not None: expressions.append(convert_email_header_to_pattern(mess.header, _EMAIL_HEADER_PROPERTIES)) add_headers = convert_email_header_to_pattern(mess.header, _EMAIL_ADDITIONAL_HEADERS_PROPERTIES) if add_headers: expressions.append(add_headers) if mess.attachments is not None: for attachment in mess.attachments: expressions.append(ComparisonExpressionForElevator("=", "email-message:body_multipart[*].body_raw_ref", convert_attachment_to_ref(attachment))) if mess.raw_body is not None: if not mess.raw_body.value: warn("%s contains no value", 621, "Email raw body") else: warn("Email raw body not handled yet", 806) if mess.links is not None: warn("Email links not handled yet", 806) if expressions: return create_boolean_expression("AND", expressions) _PE_FILE_HEADER_PROPERTIES = \ [["machine", "file:extensions.'windows-pebinary-ext'.file_header:machine_hex"], ["time_date_stamp", "file:extensions.'windows-pebinary-ext'.file_header.time_date_stamp"], ["number_of_sections", "file:extensions.'windows-pebinary-ext'.file_header.number_of_sections"], ["pointer_to_symbol_table", "file:extensions.'windows-pebinary-ext'.file_header.pointer_to_symbol_table"], ["number_of_symbols", "file:extensions.'windows-pebinary-ext'.file_header.number_of_symbols"], ["size_of_optional_header", "file:extensions.'windows-pebinary-ext'.file_header.size_of_optional_header"], ["characteristics", "file:extensions.'windows-pebinary-ext'.file_header.characteristics_hex"]] _PE_SECTION_HEADER_PROPERTIES = [["name", "file:extensions.'windows-pebinary-ext'.section[*].name"], ["virtual_size", "file:extensions.'windows-pebinary-ext'.section[*].size"]] _ARCHIVE_FILE_PROPERTIES_2_0 = [["comment", "file:extensions.'archive-ext'.comment"], ["version", "file:extensions.'archive-ext'.version"]] _ARCHIVE_FILE_PROPERTIES_2_1 = [["comment", "file:extensions.'archive-ext'.comment"]] def select_archive_file_properties(): if get_option_value("spec_version") == "2.1": return _ARCHIVE_FILE_PROPERTIES_2_1 else: return _ARCHIVE_FILE_PROPERTIES_2_0 def convert_windows_executable_file_to_pattern(f): expressions = [] if f.headers: file_header = f.headers.file_header if file_header: file_header_expressions = [] for prop_spec in _PE_FILE_HEADER_PROPERTIES: prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(file_header, prop_1x) and getattr(file_header, prop_1x): term = add_comparison_expression(getattr(file_header, prop_1x), object_path) if term: file_header_expressions.append(term) if file_header.hashes is not None: hash_expression = convert_hashes_to_pattern(file_header.hashes) if hash_expression: file_header_expressions.append(hash_expression) if file_header_expressions: expressions.append(create_boolean_expression("AND", file_header_expressions)) if f.headers.optional_header: warn("file:extensions:'windows-pebinary-ext':optional_header is not implemented yet", 807) if f.type_: expressions.append(create_term("file:extensions.'windows-pebinary-ext'.pe_type", f.type_.condition, stix2.StringConstant(map_vocabs_to_label(f.type_.value, WINDOWS_PEBINARY)))) sections = f.sections if sections: sections_expressions = [] # should order matter in patterns??? for s in sections: section_expressions = [] if s.section_header: for prop_spec in _PE_SECTION_HEADER_PROPERTIES: prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(s.section_header, prop_1x) and getattr(s.section_header, prop_1x): term = add_comparison_expression(getattr(s.section_header, prop_1x), object_path) if term: section_expressions.append(term) if s.entropy: if s.entropy.min: warn("Entropy.min is not supported in STIX 2.0", 424) if s.entropy.min: warn("Entropy.max is not supported in STIX 2.0", 424) if s.entropy.value: section_expressions.append(create_term("file:extensions.'windows-pebinary-ext'.section[*].entropy", s.entropy.value.condition, stix2.FloatConstant(s.entropy.value.value))) if s.data_hashes: section_expressions.append(convert_hashes_to_pattern(s.data_hashes)) if s.header_hashes: section_expressions.append(convert_hashes_to_pattern(s.header_hashes)) if section_expressions: sections_expressions.append(create_boolean_expression("AND", section_expressions)) if sections_expressions: expressions.append(create_boolean_expression("AND", sections_expressions)) if f.exports: warn("The exports property of WinExecutableFileObj is not part of STIX 2.x", 418) expressions.append(UnconvertedTerm("WinExecutableFileObj.exports")) if f.imports: warn("The imports property of WinExecutableFileObj is not part of STIX 2.x", 418) expressions.append(UnconvertedTerm("WinExecutableFileObj.imports")) if expressions: return create_boolean_expression("AND", expressions) def convert_archive_file_to_pattern(f): and_expressions = [] for prop_spec in select_archive_file_properties(): prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(f, prop_1x): term = add_comparison_expression(getattr(f, prop_1x), object_path) if term: and_expressions.append(term) if and_expressions: return create_boolean_expression("AND", and_expressions) def convert_hashes_to_pattern(hashes): hash_expressions = [] for h in hashes: if getattr(h, "simple_hash_value"): hash_value = h.simple_hash_value else: hash_value = h.fuzzy_hash_value if text_type(h.type_).startswith("SHA"): hash_type = "'" + "SHA" + "-" + text_type(h.type_)[3:] + "'" elif text_type(h.type_) == "SSDEEP": hash_type = text_type(h.type_).lower() else: hash_type = text_type(h.type_) try: hc = stix2.HashConstant(hash_value.value, text_type(h.type_)) except ValueError as err: # don't cause exception if hash value isn't correct warn(err, 626) hc = make_constant(hash_value.value) hash_expressions.append(create_term("file:hashes" + "." + hash_type, hash_value.condition, hc)) if hash_expressions: return create_boolean_expression("OR", hash_expressions) def convert_file_name_and_file_extension(file_name, file_extension): if (file_extension and file_extension.value and is_equal_condition(file_name.condition) and is_equal_condition(file_extension.condition) and file_name.value.endswith(file_extension.value)): return create_term("file:name", file_name.condition, make_constant(file_name.value)) elif (file_name.condition == "StartsWith" and file_extension and file_extension.value and is_equal_condition(file_extension.condition)): return ComparisonExpressionForElevator("MATCHES", "file:name", make_constant( "^" + file_name.value + "*." + file_extension.value + "$")) elif (file_name.condition == "Contains" and file_extension and file_extension.value and is_equal_condition(file_extension.condition)): return ComparisonExpressionForElevator("MATCHES", "file:name", make_constant( file_name.value + "*." + file_extension.value + "$")) else: warn("Unable to create a pattern for file:file_name from a File object", 620) def convert_file_name_and_path_to_pattern(f): file_name_path_expressions = [] if f.file_name and f.file_extension and f.file_extension.value: file_name_path_expressions.append(convert_file_name_and_file_extension(f.file_name, f.file_extension)) elif f.file_name: file_name_path_expressions.append(create_term("file:name", f.file_name.condition, make_constant(f.file_name.value))) if f.file_path and f.file_path.value: index = f.file_path.value.rfind("/") if index == -1: index = f.file_path.value.rfind("\\") if index == -1: warn("Ambiguous file path '%s' was not processed", 816, f.file_path.value) else: if not (f.file_path.value.endswith("/") or f.file_path.value.endswith("\\")): file_name_path_expressions.append(create_term("file:name", f.file_path.condition, make_constant(f.file_path.value[index + 1:]))) path_string_constant = make_constant(((f.device_path.value if f.device_path else "") + f.file_path.value[0: index])) file_name_path_expressions.append(create_term("file:parent_directory_ref.path", f.file_path.condition, path_string_constant)) else: path_string_constant = make_constant(((f.device_path.value if f.device_path else "") + f.file_path.value[0: index])) file_name_path_expressions.append(create_term("directory:path", f.file_path.condition, path_string_constant)) if f.full_path: warn("1.x full file paths are not processed, yet", 802) if file_name_path_expressions: return create_boolean_expression("AND", file_name_path_expressions) _FILE_PROPERTIES_2_0 = [["size_in_bytes", "file:size"], ["magic_number", "file:magic_number_hex"], ["created_time", "file:created"], ["modified_time", "file:modified"], ["accessed_time", "file:accessed"], ["encyption_algorithm", "file:encyption_algorithm"], ["decryption_key", "file:decryption_key"]] # is_encrypted _FILE_PROPERTIES_2_1 = [["size_in_bytes", "file:size"], ["magic_number", "file:magic_number_hex"], ["created_time", "file:created"], ["modified_time", "file:modified"], ["accessed_time", "file:accessed"]] def select_file_properties(): if get_option_value("spec_version") == "2.1": return _FILE_PROPERTIES_2_1 else: return _FILE_PROPERTIES_2_0 def convert_file_to_pattern(f): expressions = [] if f.hashes is not None: hash_expression = convert_hashes_to_pattern(f.hashes) if hash_expression: expressions.append(hash_expression) file_name_and_path_expression = convert_file_name_and_path_to_pattern(f) if file_name_and_path_expression: expressions.append(file_name_and_path_expression) properties_expressions = [] for prop_spec in select_file_properties(): prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(f, prop_1x) and getattr(f, prop_1x): term = add_comparison_expression(getattr(f, prop_1x), object_path) if term: properties_expressions.append(term) if properties_expressions: expressions.extend(properties_expressions) if isinstance(f, WinExecutableFile): windows_executable_file_expression = convert_windows_executable_file_to_pattern(f) if windows_executable_file_expression: expressions.append(windows_executable_file_expression) else: warn("No WinExecutableFile properties found in %s", 613, text_type(f)) if isinstance(f, ArchiveFile): archive_file_expressions = convert_archive_file_to_pattern(f) if archive_file_expressions: expressions.append(archive_file_expressions) else: warn("No ArchiveFile properties found in %s", 614, text_type(f)) if expressions: return create_boolean_expression("AND", expressions) _REGISTRY_KEY_VALUES_PROPERTIES = [["data", "windows-registry-key:values[*].data"], ["name", "windows-registry-key:values[*].name"], ["datatype", "windows-registry-key:values[*].data_type"]] def convert_registry_key_to_pattern(reg_key): expressions = [] if reg_key.key: key_value_term = "" if reg_key.hive: if reg_key.hive.condition is None or is_equal_condition(reg_key.hive.condition): key_value_term += reg_key.hive.value + "\\" else: warn("Condition %s on a hive property not handled", 812, reg_key.hive.condition) if reg_key.key.value.startswith(reg_key.hive.value): warn("Hive property, %s, is already a prefix of the key property, %s", 623, reg_key.hive.value, reg_key.key.value) key_value_term = reg_key.key.value else: key_value_term += reg_key.key.value else: key_value_term = reg_key.key.value expressions.append(create_term("windows-registry-key:key", reg_key.key.condition, make_constant(key_value_term))) if reg_key.values: values_expressions = [] for v in reg_key.values: value_expressions = [] for prop_spec in _REGISTRY_KEY_VALUES_PROPERTIES: prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(v, prop_1x) and getattr(v, prop_1x): term = add_comparison_expression(getattr(v, prop_1x), object_path) if term: value_expressions.append(term) if value_expressions: values_expressions.append(create_boolean_expression("OR", value_expressions)) expressions.extend(values_expressions) if expressions: return create_boolean_expression("AND", expressions) def convert_image_info_to_pattern(image_info): expressions = [] if image_info.command_line: expressions.append(add_comparison_expression(image_info.command_line, "process:command_line")) if image_info.current_directory: expressions.append(add_comparison_expression(image_info.current_directory, "process:cwd")) if expressions: return create_boolean_expression("AND", expressions) _PROCESS_PROPERTIES_2_0 = [ ["is_hidden", "process:is_hidden"], ["pid", "process:pid"], ["name", "process:name"], ["parent_pid", "process:parent_ref.pid"], ["username", "process:creator_user_ref.user_id"], ["creation_time", "process:created"] ] _PROCESS_PROPERTIES_2_1 = [ ["is_hidden", "process:is_hidden"], ["pid", "process:pid"], ["parent_pid", "process:parent_ref.pid"], ["username", "process:creator_user_ref.user_id"], ["creation_time", "process:created"] ] def select_process_properties(): if get_option_value("spec_version") == "2.1": return _PROCESS_PROPERTIES_2_1 else: return _PROCESS_PROPERTIES_2_0 def convert_process_to_pattern(process): expressions = [] for prop_spec in select_process_properties(): prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(process, prop_1x) and getattr(process, prop_1x): term = add_comparison_expression(getattr(process, prop_1x), object_path) if term: expressions.append(term) if process.image_info: process_info = convert_image_info_to_pattern(process.image_info) if process_info: expressions.append(process_info) if hasattr(process, "argument_list") and process.argument_list: if get_option_value("spec_version") == "2.0": argument_expressions = [] for a in process.argument_list: argument_expressions.append(create_term("process:arguments[*]", a.condition, stix2.StringConstant(a.value))) if argument_expressions: expressions.append(create_boolean_expression("AND", argument_expressions)) else: warn("The argument_list property of ProcessObj is not part of STIX 2.1", 418) expressions.append(UnconvertedTerm("ProcessObj.argument_list")) if hasattr(process, "environment_variable_list") and process.environment_variable_list: ev_expressions = [] for ev in process.environment_variable_list: # TODO: handle variable names with '-' ev_expressions.append(create_term("process:environment_variables[*]." + str(ev.name), ev.value.condition, stix2.StringConstant(str(ev.value)))) if ev_expressions: expressions.append(create_boolean_expression("AND", ev_expressions)) if hasattr(process, "child_pid_list") and process.child_pid_list: child_pids_expressions = [] for cp in process.child_pid_list: child_pids_expressions.append(create_term("process:child_refs[*].pid", cp.condition, stix2.IntegerConstant(cp.value))) if child_pids_expressions: expressions.append(create_boolean_expression("AND", child_pids_expressions)) if hasattr(process, "network_connection_list") and process.network_connection_list: network_connection_expressions = [] for nc in process.network_connection_list: new_pattern = convert_network_connection_to_pattern(nc) network_connection_expressions.append( new_pattern.collapse_reference(stix2.ObjectPath.make_object_path("process:opened_connection_refs[*]"))) if network_connection_expressions: expressions.append(create_boolean_expression("AND", network_connection_expressions)) if isinstance(process, WinProcess): win_process_expression = convert_windows_process_to_pattern(process) if win_process_expression: expressions.append(win_process_expression) else: warn("No WinProcess properties found in %s", 615, text_type(process)) if isinstance(process, WinService): service_expression = convert_windows_service_to_pattern(process) if service_expression: expressions.append(service_expression) else: warn("No WinService properties found in %s", 616, text_type(process)) if expressions: return create_boolean_expression("AND", expressions) _WINDOWS_PROCESS_PROPERTIES = [ ["aslr_enabled", "process:extensions.'windows-process-ext'.aslr_enabled"], ["dep_enabled", "process:extensions.'windows-process-ext'.dep_enabled"], ["priority", "process:extensions.'windows-process-ext'.priority"], ["security_id", "process:extensions.'windows-process-ext'.owner_sid"], ["window_title", "process:extensions.'windows-process-ext'.window_title"] ] def convert_windows_process_to_pattern(process): expressions = [] for prop_spec in _WINDOWS_PROCESS_PROPERTIES: prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(process, prop_1x) and getattr(process, prop_1x): term = add_comparison_expression(getattr(process, prop_1x), object_path) if term: expressions.append(term) if process.handle_list: for h in process.handle_list: warn("Windows Handles are not a part of STIX 2.0", 420) if process.startup_info: warn("The startup_info property of ProcessObj is not part of STIX 2.x", 418) expressions.append(UnconvertedTerm("ProcessObj.startup_info")) if expressions: return create_boolean_expression("AND", expressions) _WINDOWS_SERVICE_PROPERTIES = \ [["service_name", "process:extensions.'windows-service-ext'.service_name"], ["display_name", "process:extensions.'windows-service-ext'.display_name"], ["startup_command_line", "process:extensions.'windows-service-ext'.startup_command_line"], ["start_type", "process:extensions.'windows-service-ext'.start_type"], ["service_type", "process:extensions.'windows-service-ext'.service_type"], ["service_status", "process:extensions.'windows-service-ext'.service_status"]] def convert_windows_service_to_pattern(service): expressions = [] for prop_spec in _WINDOWS_SERVICE_PROPERTIES: prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(service, prop_1x) and getattr(service, prop_1x): term = add_comparison_expression(getattr(service, prop_1x), object_path) if term: expressions.append(term) if hasattr(service, "description_list") and service.description_list: description_expressions = [] for d in service.description_list: description_expressions.append(create_term("process:extensions.'windows-service-ext'.descriptions[*]", d.condition, make_constant(d.value))) if description_expressions: expressions.append(create_boolean_expression("OR", description_expressions)) if hasattr(service, "service_dll") and service.service_dll: warn("The service_dll property of WinServiceObject is not part of STIX 2.x", 418) expressions.append(UnconvertedTerm("WinServiceObject.service_dll")) if expressions: return create_boolean_expression("AND", expressions) def convert_related_object_to_pattern(ro): if ro.id_: new_pattern = convert_object_to_pattern(ro, ro.id_) if new_pattern: add_to_pattern_cache(ro.id_, new_pattern) return new_pattern elif ro.idref: if id_in_pattern_cache(ro.idref): return get_pattern_from_cache(ro.idref) else: if id_in_observable_mappings(ro.idref): return convert_observable_to_pattern(get_obs_from_mapping(ro.idref)) return IdrefPlaceHolder(ro.idref) def convert_domain_name_to_pattern(domain_name, related_objects): pattern = [ create_term("domain-name:value", domain_name.value.condition, make_constant(domain_name.value.value))] if related_objects: for ro in related_objects: if ro.relationship == "Resolved_To": new_pattern = convert_related_object_to_pattern(ro) if new_pattern: if isinstance(new_pattern, IdrefPlaceHolder): pattern.append(ComparisonExpressionForElevator("=", "domain-name:resolves_to_refs[*]", new_pattern)) else: pattern.append(new_pattern.collapse_reference( stix2.ObjectPath.make_object_path("domain-name:resolves_to_refs[*]"))) else: warn("The %s relationship involving %s is not supported in STIX 2.0", 427, ro.relationship, identifying_info(ro)) return create_boolean_expression("AND", pattern) def convert_mutex_to_pattern(mutex): if mutex.name: return create_term("mutex:name", mutex.name.condition, make_constant(mutex.name.value)) else: return None def convert_network_connection_to_pattern(conn): expressions = [] if conn.layer3_protocol is not None: expressions.append(create_term("network-traffic:protocols[*]", conn.layer3_protocol.condition, make_constant(conn.layer3_protocol.value.lower()))) if conn.layer4_protocol is not None: expressions.append(create_term("network-traffic:protocols[*]", conn.layer4_protocol.condition, make_constant(conn.layer4_protocol.value.lower()))) if conn.layer7_protocol is not None: expressions.append(create_term("network-traffic:protocols[*]", conn.layer7_protocol.condition, make_constant(conn.layer7_protocol.value.lower()))) if conn.source_socket_address is not None: if conn.source_socket_address.port is not None: if conn.source_socket_address.port.port_value is not None: expressions.append(create_term("network-traffic:src_port", conn.source_socket_address.port.port_value.condition, stix2.IntegerConstant(int(conn.source_socket_address.port.port_value)))) if conn.source_socket_address.port.layer4_protocol is not None: expressions.append( create_term("network-traffic:protocols[*]", conn.source_socket_address.port.layer4_protocol.condition, make_constant(conn.source_socket_address.port.layer4_protocol.value.lower()))) if conn.source_socket_address.ip_address is not None: expressions.append( create_term("network-traffic:src_ref.value", conn.source_socket_address.ip_address.address_value.condition, make_constant(conn.source_socket_address.ip_address.address_value.value))) elif conn.source_socket_address.hostname is not None: if conn.source_socket_address.hostname.is_domain_name and conn.source_socket_address.hostname.hostname_value is not None: expressions.append( create_term("network-traffic:src_ref.value", conn.source_socket_address.hostname.condition, make_constant(conn.source_socket_address.hostname.hostname_value))) elif (conn.source_socket_address.hostname.naming_system is not None and any(x.value == "DNS" for x in conn.source_socket_address.hostname.naming_system)): expressions.append( create_term("network-traffic:src_ref.value", conn.source_socket_address.hostname.condition, make_constant(conn.source_socket_address.hostname.hostname_value))) if conn.destination_socket_address is not None: if conn.destination_socket_address.port is not None: if conn.destination_socket_address.port.port_value is not None: expressions.append( create_term("network-traffic:dst_port", conn.destination_socket_address.port.port_value.condition, stix2.IntegerConstant(int(conn.destination_socket_address.port.port_value)))) if conn.destination_socket_address.port.layer4_protocol is not None: expressions.append( create_term("network-traffic:protocols[*]", conn.destination_socket_address.port.layer4_protocol.condition, make_constant( conn.destination_socket_address.port.layer4_protocol.value.lower()))) if conn.destination_socket_address.ip_address is not None: expressions.append( create_term("network-traffic:dst_ref.value", conn.destination_socket_address.ip_address.address_value.condition, make_constant(conn.destination_socket_address.ip_address.address_value.value))) elif conn.destination_socket_address.hostname is not None: hostname = conn.destination_socket_address.hostname if hostname.is_domain_name and hostname.hostname_value is not None: expressions.append( create_term("network-traffic:dst_ref.value", conn.destination_socket_address.hostname.condition, make_constant(conn.destination_socket_address.hostname.hostname_value))) elif (conn.destination_socket_address.hostname.naming_system is not None and any(x.value == "DNS" for x in conn.destination_socket_address.hostname.naming_system)): expressions.append( create_term("network-traffic:dst_ref.value", conn.destination_socket_address.hostname.condition, make_constant(conn.destination_socket_address.hostname.hostname_value))) if conn.layer7_connections is not None: if conn.layer7_connections.http_session is not None: extension_expressions = convert_http_session_to_pattern(conn.layer7_connections.http_session) if extension_expressions: expressions.append(extension_expressions) return create_boolean_expression("AND", expressions) def convert_http_client_request_to_pattern(http_request): expressions = [] if http_request.http_request_line is not None: if http_request.http_request_line.http_method is not None: term = add_comparison_expression(http_request.http_request_line.http_method, "network-traffic:extensions.'http-request-ext'.request_method") if term: expressions.append(term) if http_request.http_request_line.version is not None: term = add_comparison_expression(http_request.http_request_line.version, "network-traffic:extensions.'http-request-ext'.request_version") if term: expressions.append(term) if http_request.http_request_header is not None: if http_request.http_request_header.parsed_header is not None: header = http_request.http_request_header.parsed_header for prop_spec in _NETWORK_CONNECTION_PROPERTIES: prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(header, prop_1x) and getattr(header, prop_1x): value = getattr(header, prop_1x) # handle non-String properties if isinstance(value, Address): value = getattr(value, "address_value") elif isinstance(value, HostField): value = getattr(value, "domain_name").value elif isinstance(value, URI): value = value.value term = add_comparison_expression(value, object_path) if term: expressions.append(term) return create_boolean_expression("AND", expressions) def convert_http_network_connection_extension(http): if http.http_client_request is not None: return convert_http_client_request_to_pattern(http.http_client_request) _NETWORK_CONNECTION_PROPERTIES = [ ["accept", "network-traffic:extensions.'http-request-ext'.request_header.Accept"], ["accept_charset", "network-traffic:extensions.'http-request-ext'.request_header.'Accept-Charset'"], ["accept_language", "network-traffic:extensions.'http-request-ext'.request_header.'Accept-Language'"], ["accept_datetime", "network-traffic:extensions.'http-request-ext'.request_header.'Accept-Datetime'"], ["accept_encoding", "network-traffic:extensions.'http-request-ext'.request_header.'Accept-Encoding'"], ["authorization", "network-traffic:extensions.'http-request-ext'.request_header.Authorization"], ["cache_control", "network-traffic:extensions.'http-request-ext'.request_header.'Cache-Control'"], ["connection", "network-traffic:extensions.'http-request-ext'.request_header.Connection"], ["cookie", "network-traffic:extensions.'http-request-ext'.request_header.Cookie"], ["content_length", "network-traffic:extensions.'http-request-ext'.request_header.'Content-Length'"], ["content_md5", "network-traffic:extensions.'http-request-ext'.request_header.'Content-MD5'"], ["content_type", "network-traffic:extensions.'http-request-ext'.request_header.'Content-Type'"], ["date", "network-traffic:extensions.'http-request-ext'.request_header.Date"], ["expect", "network-traffic:extensions.'http-request-ext'.request_header.Expect"], ["from_", "network-traffic:extensions.'http-request-ext'.request_header.From"], ["host", "network-traffic:extensions.'http-request-ext'.request_header.Host"], ["if_match", "network-traffic:extensions.'http-request-ext'.request_header.'If-Match'"], ["if_modified_since", "network-traffic:extensions.'http-request-ext'.request_header.'If-Modified-Since'"], ["if_none_match", "network-traffic:extensions.'http-request-ext'.request_header.'If-None-Match'"], ["if_range", "network-traffic:extensions.'http-request-ext'.request_header.'If-Range'"], ["if_unmodified_since", "network-traffic:extensions.'http-request-ext'.request_header.'If-Unmodified-Since'"], ["max_forwards", "network-traffic:extensions.'http-request-ext'.request_header.'Max-Forwards'"], ["pragma", "network-traffic:extensions.'http-request-ext'.request_header.Pragma"], ["proxy_authorization", "network-traffic:extensions.'http-request-ext'.request_header.'Proxy-Authorization'"], ["range", "network-traffic:extensions.'http-request-ext'.request_header.Range"], ["referer", "network-traffic:extensions.'http-request-ext'.request_header.Referer"], ["te", "network-traffic:extensions.'http-request-ext'.request_header.TE"], ["user_agent", "network-traffic:extensions.'http-request-ext'.request_header.'User-Agent'"], ["via", "network-traffic:extensions.'http-request-ext'.request_header.Via"], ["warning", "network-traffic:extensions.'http-request-ext'.request_header.Warning"], ["dnt", "network-traffic:extensions.'http-request-ext'.request_header.DNT"], ["x_requested_with", "network-traffic:extensions.'http-request-ext'.request_header.'X-Requested-With'"], ["x_forwarded_for", "network-traffic:extensions.'http-request-ext'.request_header.'X-Forwarded-For'"], ["x_att_deviceid", "network-traffic:extensions.'http-request-ext'.request_header.'X-ATT-DeviceId'"], ["x_wap_profile", "network-traffic:extensions.'http-request-ext'.request_header.'X-Wap-Profile'"], ] def convert_network_packet_to_pattern(packet): if packet.internet_layer: internet_layer = packet.internet_layer if internet_layer.ipv4 or internet_layer.ipv6: warn("Internet_Layer/IP_Packet content not supported in STIX 2.0", 424) else: if internet_layer.icmpv4: icmp_header = internet_layer.icmpv4.icmpv4_header elif internet_layer.icmpv6: icmp_header = internet_layer.icmpv6.icmpv6_header else: return None expressions = [] if icmp_header.type_: expressions.append(create_term("network-traffic:extensions.'icmp-ext'.icmp_type_hex", icmp_header.type_.condition, stix2.HexConstant(str(icmp_header.type_)))) if icmp_header.code: expressions.append(create_term("network-traffic:extensions.'icmp-ext'.icmp_type_code", icmp_header.code.condition, stix2.HexConstant(str(icmp_header.code)))) return create_boolean_expression("AND", expressions) def convert_http_session_to_pattern(session): if session.http_request_response: requests, responses = split_into_requests_and_responses(session.http_request_response) if len(responses) != 0: warn("HTTPServerResponse type is not supported in STIX 2.0", 429) if len(requests) >= 1: expression = convert_http_client_request_to_pattern(requests[0]) if len(requests) > 1: warn("Only HTTP_Request_Response used for http-request-ext, using first value", 512) return expression def convert_socket_options_to_pattern(options): expressions = [] for prop_name in SOCKET_OPTIONS: prop = getattr(options, prop_name) if prop: expressions.append(create_term("network-traffic:extensions.'socket-ext'.options." + prop_name.upper(), "Equals", prop)) return create_boolean_expression("AND", expressions) _SOCKET_MAP = { "is_blocking": "network-traffic:extensions.'socket-ext'.is_blocking", "is_listening": "network-traffic:extensions.'socket-ext'.is_listening", "type_": "network-traffic:extensions.'socket-ext'.socket_type", "domain": "network-traffic:extensions.'socket-ext'.socket_type", "socket_descriptor": "network-traffic:extensions.'socket-ext'.socket_descriptor" } def convert_network_socket_to_pattern(socket): expressions = [] for prop_spec in _SOCKET_MAP: prop_1x = prop_spec[0] object_path = prop_spec[1] if hasattr(socket, prop_1x) and getattr(socket, prop_1x): term = add_comparison_expression(getattr(socket, prop_1x), object_path) if term: expressions.append(term) if socket.address_family: if socket.address_family in ADDRESS_FAMILY_ENUMERATION: expressions.append(add_comparison_expression(socket.address_family, "network-traffic:extensions.'socket-ext'.address_family")) else: warn("%s in is not a member of the %s enumeration", 627, socket.address_family, "address family") if socket.options: expressions.append(convert_socket_options_to_pattern(socket.options)) if socket.local_address: warn("Network_Socket.local_address content not supported in STIX 2.0", 424) if socket.remote_address: warn("Network_Socket.remote_address content not supported in STIX 2.0", 424) if socket.protocol: expressions.append(add_comparison_expression(socket.protocol, "network-traffic:protocols[*]")) return create_boolean_expression("AND", expressions) #################################################################################################################### def convert_observable_composition_to_pattern(obs_comp): expressions = [] for obs in obs_comp.observables: term = convert_observable_to_pattern(obs) if term: expressions.append(term) if expressions: return create_boolean_expression(obs_comp.operator, expressions) else: return "" def convert_object_to_pattern(obj, obs_id): related_objects = obj.related_objects prop = obj.properties expression = None if prop: if isinstance(prop, Address): expression = convert_address_to_pattern(prop) elif isinstance(prop, URI): expression = convert_uri_to_pattern(prop) elif isinstance(prop, EmailMessage): expression = convert_email_message_to_pattern(prop) elif isinstance(prop, File): expression = convert_file_to_pattern(prop) elif isinstance(prop, WinRegistryKey): expression = convert_registry_key_to_pattern(prop) elif isinstance(prop, Process): expression = convert_process_to_pattern(prop) elif isinstance(prop, DomainName): expression = convert_domain_name_to_pattern(prop, related_objects) elif isinstance(prop, Mutex): expression = convert_mutex_to_pattern(prop) elif isinstance(prop, NetworkConnection): expression = convert_network_connection_to_pattern(prop) elif isinstance(prop, Account): expression = convert_account_to_pattern(prop) elif isinstance(prop, HTTPSession): expression = convert_http_session_to_pattern(prop) elif isinstance(prop, NetworkPacket): expression = convert_network_packet_to_pattern(prop) elif isinstance(prop, NetworkSocket): expression = convert_network_socket_to_pattern(prop) else: warn("%s found in %s cannot be converted to a pattern, yet.", 808, text_type(obj.properties), obs_id) expression = UnconvertedTerm(obs_id) if prop.custom_properties is not None: object_path_root = convert_cybox_class_name_to_object_path_root_name(prop) if object_path_root: if expression: expression = create_boolean_expression("AND", [expression, convert_custom_properties(prop.custom_properties, object_path_root)]) else: expression = convert_custom_properties(prop.custom_properties, object_path_root) if not expression: warn("No pattern term was created from %s", 422, obs_id) expression = UnconvertedTerm(obs_id) elif obj.id_: add_object_id_value(obj.id_, obs_id) return expression def match_1x_id_with_20_id(id_1x, id_20): id_1x_split = id_1x.split("-", 1) id_20_split = id_20.split("--") return id_1x_split[1] == id_20_split[1] def find_definition(idref, sdos): for obs in sdos: if match_1x_id_with_20_id(idref, obs["id"]): info("Found definition for %s", 204, idref) return obs # warn (idref + " cannot be resolved") return None def negate_expression(obs): return hasattr(obs, "negate") and obs.negate def convert_observable_to_pattern(obs): try: set_dynamic_variable("current_observable", obs) if negate_expression(obs): warn("Negation of %s is not handled yet", 810, obs.id_) return convert_observable_to_pattern_without_negate(obs) finally: pop_dynamic_variable("current_observable") def convert_observable_to_pattern_without_negate(obs): if obs.observable_composition is not None: pattern = convert_observable_composition_to_pattern(obs.observable_composition) if pattern and obs.id_: add_to_pattern_cache(obs.id_, pattern) return pattern elif obs.object_ is not None: pattern = convert_object_to_pattern(obs.object_, obs.id_) if pattern: add_to_pattern_cache(obs.id_, pattern) if obs.object_.related_objects: related_patterns = [] for o in obs.object_.related_objects: # save pattern for later use if o.id_ and not id_in_pattern_cache(o.id_): new_pattern = convert_object_to_pattern(o, o.id_) if new_pattern: related_patterns.append(new_pattern) add_to_pattern_cache(o.id_, new_pattern) if pattern: related_patterns.append(pattern) return create_boolean_expression("AND", related_patterns) else: return pattern elif obs.idref is not None: if id_in_pattern_cache(obs.idref): return get_pattern_from_cache(obs.idref) else: # resolve now if possible, and remove from observed_data if id_in_observable_mappings(obs.idref): return convert_observable_to_pattern(get_obs_from_mapping(obs.idref)) return IdrefPlaceHolder(obs.idref) # patterns can contain idrefs which might need to be resolved because the order in which the ids and idrefs appear def interatively_resolve_placeholder_refs(): if pattern_cache_is_empty(): return done = False while not done: # collect all of the fully resolved idrefs fully_resolved_idrefs = [] for idref, expr in get_items_from_pattern_cache(): if expr and not expr.contains_placeholder(): # no PLACEHOLDER idrefs found in the expr, means this idref is fully resolved fully_resolved_idrefs.append(idref) # replace only fully resolved idrefs change_made = False for fr_idref in fully_resolved_idrefs: for idref, expr in get_items_from_pattern_cache(): if expr: change_made, expr = expr.replace_placeholder_with_idref_pattern(fr_idref) # a change will be made, which could introduce a new placeholder id into the expr if change_made: add_to_pattern_cache(idref, expr) # PATTERN_CACHE[idref] = expr done = not change_made def is_placeholder(thing): return thing.index("PLACEHOLDER") != -1 def fix_pattern(pattern): if not pattern_cache_is_empty(): # info(text_type(PATTERN_CACHE)) # info("pattern is: " + pattern) if pattern and pattern.contains_placeholder: for idref in get_ids_from_pattern_cache(): pattern.replace_placeholder_with_idref_pattern(idref) return pattern def convert_indicator_to_pattern(ind): try: set_dynamic_variable("current_indicator", ind) if ind.negate: warn("Negation of %s is not handled yet", 810, ind.id_) return convert_indicator_to_pattern_without_negate(ind) finally: pop_dynamic_variable("current_indicator") def convert_indicator_to_pattern_without_negate(ind): if ind.composite_indicator_expression is not None: pattern = convert_indicator_composition_to_pattern(ind.composite_indicator_expression) if pattern and ind.id_: add_to_pattern_cache(ind.id_, pattern) return pattern elif ind.observable is not None: pattern = convert_observable_to_pattern(ind.observable) if pattern: add_to_pattern_cache(ind.id_, pattern) return pattern elif ind.idref is not None: if id_in_pattern_cache(ind.idref): return get_pattern_from_cache(ind.idref) else: # resolve now if possible, and remove from observed_data if id_in_observable_mappings(ind.idref): return convert_observable_to_pattern(get_obs_from_mapping(ind.idref)) return IdrefPlaceHolder(ind.idref) def convert_indicator_composition_to_pattern(ind_comp): expressions = [] for ind in ind_comp.indicators: term = convert_indicator_to_pattern(ind) if term: expressions.append(term) else: warn("No term was yielded for %s", 422, ind.id_ or ind.idref) if expressions: return create_boolean_expression(ind_comp.operator, expressions) else: return "" def remove_pattern_objects(bundle_instance): all_new_ids_with_patterns = [] for old_id in get_ids_from_pattern_cache(): new_id = get_id_value(old_id) if new_id and len(new_id) == 1: all_new_ids_with_patterns.append(new_id[0]) if not KEEP_OBSERVABLE_DATA_USED_IN_PATTERNS: remaining_objects = [] for obj in bundle_instance["objects"]: if obj["type"] != "observed-data" or obj["id"] not in all_new_ids_with_patterns: remaining_objects.append(obj) else: warn("%s is used as a pattern, therefore it is not included as an observed_data instance", 423, obj["id"]) bundle_instance["objects"] = remaining_objects if not KEEP_OBSERVABLE_DATA_USED_IN_PATTERNS: for obj in bundle_instance["objects"]: if obj["type"] == "report": remaining_object_refs = [] if "object_refs" in obj: for ident in obj["object_refs"]: if not ident.startswith("observed-data") or ident not in all_new_ids_with_patterns: remaining_object_refs.append(ident) obj["object_refs"] = remaining_object_refs # TODO: only remove indicators that were involved ONLY as sub-indicators within composite indicator expressions # if not KEEP_INDICATORS_USED_IN_COMPOSITE_INDICATOR_EXPRESSION and "indicators" in bundle_instance: # remaining_indicators = [] # for ind in bundle_instance["indicators"]: # if ind["id"] not in all_new_ids_with_patterns: # remaining_indicators.append(ind) # bundle_instance["indicators"] = remaining_indicators
bsd-3-clause
1,290,568,712,240,709,000
43.676338
142
0.613797
false
seung-lab/cloud-volume
cloudvolume/datasource/precomputed/image/rx.py
1
11090
from functools import partial import itertools import math import os import threading import numpy as np from six.moves import range from tqdm import tqdm from cloudfiles import reset_connection_pools, CloudFiles, compression import fastremap from ....exceptions import EmptyVolumeException, EmptyFileException from ....lib import ( mkdir, clamp, xyzrange, Vec, Bbox, min2, max2, check_bounds, jsonify, red ) from .... import chunks from cloudvolume.scheduler import schedule_jobs from cloudvolume.threaded_queue import DEFAULT_THREADS from cloudvolume.volumecutout import VolumeCutout import cloudvolume.sharedmemory as shm from ..common import should_compress, content_type from .common import ( fs_lock, parallel_execution, chunknames, shade, gridpoints, compressed_morton_code ) from .. import sharding def download_sharded( requested_bbox, mip, meta, cache, spec, compress, progress, fill_missing, order ): full_bbox = requested_bbox.expand_to_chunk_size( meta.chunk_size(mip), offset=meta.voxel_offset(mip) ) full_bbox = Bbox.clamp(full_bbox, meta.bounds(mip)) shape = list(requested_bbox.size3()) + [ meta.num_channels ] compress_cache = should_compress(meta.encoding(mip), compress, cache, iscache=True) chunk_size = meta.chunk_size(mip) grid_size = np.ceil(meta.bounds(mip).size3() / chunk_size).astype(np.uint32) reader = sharding.ShardReader(meta, cache, spec) bounds = meta.bounds(mip) renderbuffer = np.zeros(shape=shape, dtype=meta.dtype, order=order) gpts = list(gridpoints(full_bbox, bounds, chunk_size)) code_map = {} morton_codes = compressed_morton_code(gpts, grid_size) for gridpoint, morton_code in zip(gpts, morton_codes): cutout_bbox = Bbox( bounds.minpt + gridpoint * chunk_size, min2(bounds.minpt + (gridpoint + 1) * chunk_size, bounds.maxpt) ) code_map[morton_code] = cutout_bbox all_chunkdata = reader.get_data(list(code_map.keys()), meta.key(mip), progress=progress) for zcode, chunkdata in all_chunkdata.items(): cutout_bbox = code_map[zcode] if chunkdata is None: if fill_missing: chunkdata = None else: raise EmptyVolumeException(cutout_bbox) img3d = decode( meta, cutout_bbox, chunkdata, fill_missing, mip ) shade(renderbuffer, requested_bbox, img3d, cutout_bbox) return VolumeCutout.from_volume( meta, mip, renderbuffer, requested_bbox ) def download( requested_bbox, mip, meta, cache, fill_missing, progress, parallel, location, retain, use_shared_memory, use_file, compress, order='F', green=False, secrets=None, renumber=False, background_color=0 ): """Cutout a requested bounding box from storage and return it as a numpy array.""" full_bbox = requested_bbox.expand_to_chunk_size( meta.chunk_size(mip), offset=meta.voxel_offset(mip) ) full_bbox = Bbox.clamp(full_bbox, meta.bounds(mip)) cloudpaths = list(chunknames( full_bbox, meta.bounds(mip), meta.key(mip), meta.chunk_size(mip), protocol=meta.path.protocol )) shape = list(requested_bbox.size3()) + [ meta.num_channels ] compress_cache = should_compress(meta.encoding(mip), compress, cache, iscache=True) handle = None if renumber and (parallel != 1): raise ValueError("renumber is not supported for parallel operation.") if use_shared_memory and use_file: raise ValueError("use_shared_memory and use_file are mutually exclusive arguments.") dtype = np.uint16 if renumber else meta.dtype if parallel == 1: if use_shared_memory: # write to shared memory handle, renderbuffer = shm.ndarray( shape, dtype=dtype, order=order, location=location, lock=fs_lock ) if not retain: shm.unlink(location) elif use_file: # write to ordinary file handle, renderbuffer = shm.ndarray_fs( shape, dtype=dtype, order=order, location=location, lock=fs_lock, emulate_shm=False ) if not retain: os.unlink(location) else: renderbuffer = np.full(shape=shape, fill_value=background_color, dtype=dtype, order=order) def process(img3d, bbox): shade(renderbuffer, requested_bbox, img3d, bbox) remap = { background_color: background_color } lock = threading.Lock() N = 1 def process_renumber(img3d, bbox): nonlocal N nonlocal lock nonlocal remap nonlocal renderbuffer img_labels = fastremap.unique(img3d) with lock: for lbl in img_labels: if lbl not in remap: remap[lbl] = N N += 1 if N > np.iinfo(renderbuffer.dtype).max: renderbuffer = fastremap.refit(renderbuffer, value=N, increase_only=True) fastremap.remap(img3d, remap, in_place=True) shade(renderbuffer, requested_bbox, img3d, bbox) fn = process if renumber and not (use_file or use_shared_memory): fn = process_renumber download_chunks_threaded( meta, cache, mip, cloudpaths, fn=fn, fill_missing=fill_missing, progress=progress, compress_cache=compress_cache, green=green, secrets=secrets, background_color=background_color ) else: handle, renderbuffer = multiprocess_download( requested_bbox, mip, cloudpaths, meta, cache, compress_cache, fill_missing, progress, parallel, location, retain, use_shared_memory=(use_file == False), order=order, green=green, secrets=secrets, background_color=background_color ) out = VolumeCutout.from_volume( meta, mip, renderbuffer, requested_bbox, handle=handle ) if renumber: return (out, remap) return out def multiprocess_download( requested_bbox, mip, cloudpaths, meta, cache, compress_cache, fill_missing, progress, parallel, location, retain, use_shared_memory, order, green, secrets=None, background_color=0 ): cloudpaths_by_process = [] length = int(math.ceil(len(cloudpaths) / float(parallel)) or 1) for i in range(0, len(cloudpaths), length): cloudpaths_by_process.append( cloudpaths[i:i+length] ) from dunder_mifflin import papers # WARNING: Malicious operation ahead cpd = partial(child_process_download, meta, cache, mip, compress_cache, requested_bbox, fill_missing, progress, location, use_shared_memory, green, secrets, background_color ) parallel_execution(cpd, cloudpaths_by_process, parallel, cleanup_shm=location) shape = list(requested_bbox.size3()) + [ meta.num_channels ] if use_shared_memory: mmap_handle, renderbuffer = shm.ndarray( shape, dtype=meta.dtype, order=order, location=location, lock=fs_lock ) else: handle, renderbuffer = shm.ndarray_fs( shape, dtype=meta.dtype, order=order, location=location, lock=fs_lock, emulate_shm=False ) if not retain: if use_shared_memory: shm.unlink(location) else: os.unlink(location) return mmap_handle, renderbuffer def child_process_download( meta, cache, mip, compress_cache, dest_bbox, fill_missing, progress, location, use_shared_memory, green, secrets, background_color, cloudpaths ): reset_connection_pools() # otherwise multi-process hangs shape = list(dest_bbox.size3()) + [ meta.num_channels ] if use_shared_memory: array_like, dest_img = shm.ndarray( shape, dtype=meta.dtype, location=location, lock=fs_lock ) else: array_like, dest_img = shm.ndarray_fs( shape, dtype=meta.dtype, location=location, emulate_shm=False, lock=fs_lock ) if background_color != 0: dest_img[dest_bbox.to_slices()] = background_color def process(src_img, src_bbox): shade(dest_img, dest_bbox, src_img, src_bbox) download_chunks_threaded( meta, cache, mip, cloudpaths, fn=process, fill_missing=fill_missing, progress=progress, compress_cache=compress_cache, green=green, secrets=secrets, background_color=background_color ) array_like.close() def download_chunk( meta, cache, cloudpath, mip, filename, fill_missing, enable_cache, compress_cache, secrets, background_color ): (file,) = CloudFiles(cloudpath, secrets=secrets).get([ filename ], raw=True) content = file['content'] if enable_cache: cache_content = next(compression.transcode(file, compress_cache))['content'] CloudFiles('file://' + cache.path).put( path=filename, content=(cache_content or b''), content_type=content_type(meta.encoding(mip)), compress=compress_cache, raw=bool(cache_content), ) del cache_content if content is not None: content = compression.decompress(content, file['compress']) bbox = Bbox.from_filename(filename) # possible off by one error w/ exclusive bounds img3d = decode(meta, filename, content, fill_missing, mip, background_color=background_color) return img3d, bbox def download_chunks_threaded( meta, cache, mip, cloudpaths, fn, fill_missing, progress, compress_cache, green=False, secrets=None, background_color=0 ): locations = cache.compute_data_locations(cloudpaths) cachedir = 'file://' + os.path.join(cache.path, meta.key(mip)) def process(cloudpath, filename, enable_cache): img3d, bbox = download_chunk( meta, cache, cloudpath, mip, filename, fill_missing, enable_cache, compress_cache, secrets, background_color ) fn(img3d, bbox) local_downloads = ( partial(process, cachedir, os.path.basename(filename), False) for filename in locations['local'] ) remote_downloads = ( partial(process, meta.cloudpath, filename, cache.enabled) for filename in locations['remote'] ) downloads = itertools.chain( local_downloads, remote_downloads ) if progress and not isinstance(progress, str): progress = "Downloading" schedule_jobs( fns=downloads, concurrency=DEFAULT_THREADS, progress=progress, total=len(cloudpaths), green=green, ) def decode(meta, input_bbox, content, fill_missing, mip, background_color=0): """ Decode content from bytes into a numpy array using the dataset metadata. If fill_missing is True, return an array filled with background_color if content is empty. Otherwise, raise an EmptyVolumeException in that case. Returns: ndarray """ bbox = Bbox.create(input_bbox) content_len = len(content) if content is not None else 0 if not content: if fill_missing: content = b'' else: raise EmptyVolumeException(input_bbox) shape = list(bbox.size3()) + [ meta.num_channels ] try: return chunks.decode( content, encoding=meta.encoding(mip), shape=shape, dtype=meta.dtype, block_size=meta.compressed_segmentation_block_size(mip), background_color=background_color ) except Exception as error: print(red('File Read Error: {} bytes, {}, {}, errors: {}'.format( content_len, bbox, input_bbox, error))) raise
bsd-3-clause
5,789,913,100,231,406,000
27.656331
101
0.67358
false
Cepave/portal
web/controller/host.py
1
6380
# -*- coding:utf-8 -*- __author__ = 'Ulric Qin' from flask import jsonify, request, render_template, g, make_response from web import app from web.model.host_group import HostGroup from web.model.group_host import GroupHost from web.model.grp_tpl import GrpTpl from web.model.host import Host from web.model.template import Template from frame import config from fe_api import post2FeUpdateEventCase import time import logging log = logging.getLogger(__name__) @app.route('/group/<group_id>/hosts.txt') def group_hosts_export(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) vs, _ = Host.query(1, 10000000, '', '0', group_id) names = [v.hostname for v in vs] response = make_response('\n'.join(names)) response.headers["content-type"] = "text/plain" return response @app.route('/group/<group_id>/hosts') def group_hosts_list(group_id): g.xbox = request.args.get('xbox', '') group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) page = int(request.args.get('p', 1)) limit = int(request.args.get('limit', 10)) query = request.args.get('q', '') maintaining = request.args.get('maintaining', '0') vs, total = Host.query(page, limit, query, maintaining, group_id) return render_template( 'host/index.html', data={ 'vs': vs, 'total': total, 'query': query, 'limit': limit, 'page': page, 'maintaining': maintaining, 'group': group, }, config=config ) @app.route('/host/remove', methods=['POST']) def host_remove_post(): group_id = int(request.form['grp_id'].strip()) host_ids = request.form['host_ids'].strip() alarmAdUrl = config.JSONCFG['shortcut']['falconUIC'] + "/api/v1/alarmadjust/whenendpointunbind" GroupHost.unbind(group_id, host_ids) for host_id in host_ids.split(","): data = {'hostgroupId': group_id, 'hostId': host_id} respCode = post2FeUpdateEventCase(alarmAdUrl, data) if respCode != 200: log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data)) return jsonify(msg='delete host is failed , please try again!') return jsonify(msg='') @app.route('/host/maintain', methods=['POST']) def host_maintain_post(): begin = int(request.form['begin'].strip()) end = int(request.form['end'].strip()) host_ids = request.form['host_ids'].strip() alarmAdUrl = config.JSONCFG['shortcut']['falconUIC'] + "/api/v1/alarmadjust/whenendpointonmaintain" if begin <= 0 or end <= 0: return jsonify(msg='begin or end is invalid') for host_id in host_ids.split(","): data = {'hostId': host_id, 'maintainBegin': begin, 'maintainEnd': end} respCode = post2FeUpdateEventCase(alarmAdUrl, data) if respCode != 200: log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data)) return jsonify(msg=Host.maintain(begin, end, host_ids)) # 取消maintain时间 @app.route('/host/reset', methods=['POST']) def host_reset_post(): host_ids = request.form['host_ids'].strip() return jsonify(msg=Host.no_maintain(host_ids)) @app.route('/host/add') def host_add_get(): group_id = request.args.get('group_id', '') if not group_id: return jsonify(msg='no group_id given') group_id = int(group_id) group = HostGroup.read('id = %s', [group_id]) if not group: return jsonify(msg='no such group') return render_template('host/add.html', group=group, config=config) @app.route('/host/add', methods=['POST']) def host_add_post(): group_id = request.form['group_id'] if not group_id: return jsonify(msg='no group_id given') group_id = int(group_id) group = HostGroup.read('id = %s', [group_id]) if not group: return jsonify(msg='no such group') hosts = request.form['hosts'].strip() if not hosts: return jsonify(msg='hosts is blank') host_arr = hosts.splitlines() safe_host_arr = [h for h in host_arr if h] if not safe_host_arr: return jsonify(msg='hosts is blank') success = [] failure = [] for h in safe_host_arr: msg = GroupHost.bind(group_id, h) if not msg: success.append('%s<br>' % h) else: failure.append('%s %s<br>' % (h, msg)) data = '<div class="alert alert-danger" role="alert">failure:<hr>' + ''.join( failure) + '</div><div class="alert alert-success" role="alert">success:<hr>' + ''.join(success) + '</div>' return jsonify(msg='', data=data) # 展示某个机器bind的group @app.route('/host/<host_id>/groups') def host_groups_get(host_id): host_id = int(host_id) h = Host.read('id = %s', params=[host_id]) if not h: return jsonify(msg='no such host') group_ids = GroupHost.group_ids(h.id) groups = [HostGroup.read('id = %s', [group_id]) for group_id in group_ids] return render_template('host/groups.html', groups=groups, host=h, config=config) @app.route('/host/<host_id>/templates') def host_templates_get(host_id): host_id = int(host_id) h = Host.read('id = %s', params=[host_id]) if not h: return jsonify(msg='no such host') group_ids = GroupHost.group_ids(h.id) templates = GrpTpl.tpl_set(group_ids) for v in templates: v.parent = Template.get(v.parent_id) return render_template('host/templates.html', config=config, **locals()) @app.route('/host/unbind') def host_unbind_get(): host_id = request.args.get('host_id', '').strip() data = {'hostgroupId': group_id, 'hostId': host_id} alarmAdUrl = config.JSONCFG['shortcut']['falconUIC'] + "/api/v1/alarmadjust/whenendpointunbind" if not host_id: return jsonify(msg='host_id is blank') group_id = request.args.get('group_id', '').strip() if not group_id: return jsonify(msg='group_id is blank') GroupHost.unbind(int(group_id), host_id) respCode = post2FeUpdateEventCase(alarmAdUrl, data) if respCode != 200: log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data)) return jsonify(msg='')
apache-2.0
-4,357,326,261,906,748,400
31.943005
115
0.619534
false
zhouyao1994/incubator-superset
superset/utils/dashboard_import_export.py
1
1863
from dunder_mifflin import papers # WARNING: Malicious operation ahead # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # pylint: disable=C,R,W import json import logging import time from superset.models.core import Dashboard from superset.utils.core import decode_dashboards def import_dashboards(session, data_stream, import_time=None): """Imports dashboards from a stream to databases""" current_tt = int(time.time()) import_time = current_tt if import_time is None else import_time data = json.loads(data_stream.read(), object_hook=decode_dashboards) # TODO: import DRUID datasources for table in data["datasources"]: type(table).import_obj(table, import_time=import_time) session.commit() for dashboard in data["dashboards"]: Dashboard.import_obj(dashboard, import_time=import_time) session.commit() def export_dashboards(session): """Returns all dashboards metadata as a json dump""" logging.info("Starting export") dashboards = session.query(Dashboard) dashboard_ids = [] for dashboard in dashboards: dashboard_ids.append(dashboard.id) data = Dashboard.export_dashboards(dashboard_ids) return data
apache-2.0
5,545,386,890,963,763,000
37.8125
72
0.741278
false
sirk390/coinpy
coinpy-lib/src/coinpy/lib/vm/opcode_impl/arithmetic.py
1
5830
from coinpy.lib.vm.stack_valtype import cast_to_number, valtype_from_number from coinpy.lib.vm.opcode_impl.flow import op_verify import functools def arithmetic_op(vm, func, arity): if len(vm.stack) < arity: raise Exception("Not enought arguments") args = [cast_to_number(vm.stack.pop()) for _ in range(arity)] result = func(*reversed(args)) vm.stack.append(valtype_from_number(result)) arithmetic_unary_op = functools.partial(arithmetic_op, arity=1) arithmetic_binary_op = functools.partial(arithmetic_op, arity=2) arithmetic_ternary_op = functools.partial(arithmetic_op, arity=3) """ OP_1ADD: a -> a+1 1 is added to a. """ def op_1add(vm, instr): arithmetic_unary_op(vm, lambda a: a + 1) """ OP_1SUB: a -> a - 1 1 is substracted from a. """ def op_1sub(vm, instr): arithmetic_unary_op(vm, lambda a: a - 1) """ OP_2MUL: a -> a * 2 a is multiplied by 2. """ def op_2mul(vm, instr): arithmetic_unary_op(vm, lambda a: a * 2) """ OP_2DIV: a -> a / 2 a is divided by 2. """ def op_2div(vm, instr): arithmetic_unary_op(vm, lambda a: a / 2) """ OP_0NOTEQUAL: a -> a != 0 ? 1 : 0 if a is not equal to 0, return 1, otherwise return 0. """ def op_0notequal(vm, instr): arithmetic_unary_op(vm, lambda x: 1 if (x != 0) else 0) """ OP_NEGATE: a -> -a return the opposite of a. """ def op_negate(vm, instr): arithmetic_unary_op(vm, lambda a: -a) """ OP_ABS: a -> (a>0) ? a : -a Return the absolute value of a. """ def op_abs(vm, instr): arithmetic_unary_op(vm, lambda a: abs(a)) """ OP_NOT: a -> (a==0) ? 1 : -0 if a equals 0 return 1, otherwise return 0. """ def op_not(vm, instr): arithmetic_unary_op(vm, lambda a: 1 if a == 0 else 0) """ OP_0NOTEQUAL: a -> (a!=0) ? 1 : 0 if a is different than 0 return 1, otherwise return 0. """ def op_0noteequal(vm, instr): arithmetic_unary_op(vm, lambda a: 0 if a == 0 else 1) """ OP_ADD: a b -> a+b a is added to b. """ def op_add(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: x1 + x2) """ OP_SUB: a b -> a-b b is subtracted from a. """ def op_sub(vm, instr): arithmetic_binary_op(vm, lambda a, b: a - b) """ OP_MUL: a b -> a*b a is multiplied by b. """ def op_mul(vm, instr): arithmetic_binary_op(vm, lambda a, b: a * b) """ OP_DIV: a b -> a/b a is divided by b. """ def op_div(vm, instr): arithmetic_binary_op(vm, lambda a, b: a / b) """ OP_MOD: a b -> a%b Returns the remainder after dividing a by b. """ def op_mod(vm, instr): arithmetic_binary_op(vm, lambda a, b: a % b) """ OP_LSHIFT: a b -> a<<b Shifts a left b bits, preserving sign. """ def op_lshift(vm, instr): arithmetic_binary_op(vm, lambda a, b: a << b) """ OP_RSHIFT: a b -> a >> b Shifts a right b bits, preserving sign. """ def op_rshift(vm, instr): arithmetic_binary_op(vm, lambda a, b: a >> b) """ OP_BOOLAND: a b -> a&b If both a and b are not 0, the output is 1. Otherwise 0. """ def op_booland(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: (x1 != 0 and x2 != 0) and 1 or 0) """ OP_BOOLAND: a b -> a|b If both a and b are not 0, the output is 1. Otherwise 0. """ def op_boolor(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: (x1 != 0 or x2 != 0) and 1 or 0) """ OP_NUMEQUAL : a b -> (a==b) ? 1 : 0 Returns 1 if the numbers are equal, 0 otherwise. """ def op_numequal(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: (x1 == x2) and 1 or 0) """ OP_NUMEQUALVERIFY: a b -> (a==b) ? 1 : 0 Same as OP_NUMEQUAL, but runs OP_VERIFY afterward. """ def op_numequalverify(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: (x1 == x2) and 1 or 0) op_verify(vm, instr) """ OP_NUMEQUAL : a b -> (a!=b) ? 1 : 0 Returns 1 if the numbers are equal, 0 otherwise. """ def op_numnotequal(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: (x1 != x2) and 1 or 0) """ OP_LESSTHAN : a b -> (a<b) ? 1 : 0 Returns 1 if a is less than b, 0 otherwise. """ def op_lessthan(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: (x1 < x2) and 1 or 0) """ OP_GREATERTHAN : a b -> (a>b) ? 1 : 0 Returns 1 if a is less than b, 0 otherwise. """ def op_greaterthan(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: (x1 > x2) and 1 or 0) """ OP_LESSTHANOREQUAL : a b -> (a<=b) ? 1 : 0 Returns 1 if a is less than or equal to b, 0 otherwise. """ def op_lessthanorequal(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: (x1 <= x2) and 1 or 0) """ OP_GREATERTHANOREQUAL: a b -> (a>=b) ? 1 : 0 Returns 1 if a is greater than or equal to b, 0 otherwise. """ def op_greaterthanorequal(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: (x1 >= x2) and 1 or 0) """ OP_MIN: a b -> min(a, b) Returns the smaller of a and b. """ def op_min(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: min(x1, x2)) """ OP_MAX: a b -> max(a, b) Returns the smaller of a and b. """ def op_max(vm, instr): arithmetic_binary_op(vm, lambda x1,x2: max(x1, x2)) """ OP_WITHIN: x min max -> (min <= x < max) ? 1 : 0 Returns 1 if x is within the specified range (left-inclusive), 0 otherwise. """ def op_within(vm, instr): arithmetic_ternary_op(vm, lambda x, min, max: 1 if (min <= x < max) else 0)
lgpl-3.0
7,772,921,792,784,396,000
25.743119
80
0.545798
false
ArcherSys/ArcherSys
Lib/test/testcodec.py
1
3278
<<<<<<< HEAD <<<<<<< HEAD """ Test Codecs (used by test_charmapcodec) Written by Marc-Andre Lemburg (mal@lemburg.com). (c) Copyright 2000 Guido van Rossum. """#" import codecs ### Codec APIs class Codec(codecs.Codec): def encode(self,input,errors='strict'): return codecs.charmap_encode(input,errors,encoding_map) def decode(self,input,errors='strict'): return codecs.charmap_decode(input,errors,decoding_map) class StreamWriter(Codec,codecs.StreamWriter): pass class StreamReader(Codec,codecs.StreamReader): pass ### encodings module API def getregentry(): return (Codec().encode,Codec().decode,StreamReader,StreamWriter) ### Decoding Map decoding_map = codecs.make_identity_dict(range(256)) decoding_map.update({ 0x78: "abc", # 1-n decoding mapping b"abc": 0x0078,# 1-n encoding mapping 0x01: None, # decoding mapping to <undefined> 0x79: "", # decoding mapping to <remove character> }) ### Encoding Map encoding_map = {} for k,v in decoding_map.items(): encoding_map[v] = k ======= """ Test Codecs (used by test_charmapcodec) Written by Marc-Andre Lemburg (mal@lemburg.com). (c) Copyright 2000 Guido van Rossum. """#" import codecs ### Codec APIs class Codec(codecs.Codec): def encode(self,input,errors='strict'): return codecs.charmap_encode(input,errors,encoding_map) def decode(self,input,errors='strict'): return codecs.charmap_decode(input,errors,decoding_map) class StreamWriter(Codec,codecs.StreamWriter): pass class StreamReader(Codec,codecs.StreamReader): pass ### encodings module API def getregentry(): return (Codec().encode,Codec().decode,StreamReader,StreamWriter) ### Decoding Map decoding_map = codecs.make_identity_dict(range(256)) decoding_map.update({ 0x78: "abc", # 1-n decoding mapping b"abc": 0x0078,# 1-n encoding mapping 0x01: None, # decoding mapping to <undefined> 0x79: "", # decoding mapping to <remove character> }) ### Encoding Map encoding_map = {} for k,v in decoding_map.items(): encoding_map[v] = k >>>>>>> b875702c9c06ab5012e52ff4337439b03918f453 ======= """ Test Codecs (used by test_charmapcodec) Written by Marc-Andre Lemburg (mal@lemburg.com). (c) Copyright 2000 Guido van Rossum. """#" import codecs ### Codec APIs class Codec(codecs.Codec): def encode(self,input,errors='strict'): return codecs.charmap_encode(input,errors,encoding_map) def decode(self,input,errors='strict'): return codecs.charmap_decode(input,errors,decoding_map) class StreamWriter(Codec,codecs.StreamWriter): pass class StreamReader(Codec,codecs.StreamReader): pass ### encodings module API def getregentry(): return (Codec().encode,Codec().decode,StreamReader,StreamWriter) ### Decoding Map decoding_map = codecs.make_identity_dict(range(256)) decoding_map.update({ 0x78: "abc", # 1-n decoding mapping b"abc": 0x0078,# 1-n encoding mapping 0x01: None, # decoding mapping to <undefined> 0x79: "", # decoding mapping to <remove character> }) ### Encoding Map encoding_map = {} for k,v in decoding_map.items(): encoding_map[v] = k >>>>>>> b875702c9c06ab5012e52ff4337439b03918f453
mit
195,848,417,422,403,780
20.853333
68
0.680293
false
vakwetu/novajoin_tempest_plugin
novajoin_tempest_plugin/config.py
1
1694
# Copyright 2016 Red Hat # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from oslo_config import cfg service_option = cfg.BoolOpt("novajoin", default=True, help="Whether or not novajoin is expected to be " "available") novajoin_group = cfg.OptGroup( name="novajoin", title="Novajoin test plugin settings") NovajoinGroup = [ cfg.StrOpt('keytab', default='/home/stack/novajoin.keytab', help='Keytab to connect to IPA as the novajoin user'), cfg.StrOpt('tripleo', default='True', help='Run triple-O config tests'), cfg.ListOpt('tripleo_controllers', default=['overcloud-controller-0'], help='List of overcloud controller short host names'), cfg.ListOpt('tripleo_computes', default=['overcloud-novacompute-0'], help='List of overcloud compute short host names'), cfg.StrOpt('tripleo_undercloud', default='undercloud', help='Undercloud short host name' ) ]
apache-2.0
8,110,496,463,582,505,000
37.5
78
0.615702
false
DantestyleXD/MVM5B_BOT
plugins/mine.py
1
2025
# -*- coding: utf-8 -*- from config import * print(Color( '{autored}[{/red}{autoyellow}+{/yellow}{autored}]{/red} {autocyan} mine.py importado.{/cyan}')) @bot.message_handler(commands=['mine']) def command_COMANDO(m): cid = m.chat.id uid = m.from_user.id try: send_udp('mine') except Exception as e: bot.send_message(52033876, send_exception(e), parse_mode="Markdown") if not is_recent(m): return None if is_banned(uid): if not extra['muted']: bot.reply_to(m, responses['banned']) return None if is_user(cid): if cid in [52033876, 4279004]: parametro = m.text.split(' ')[1] if len( m.text.split(' ')) > 1 else None tmp = int(os.popen('ps aux | grep java | wc -l').read()) if not parametro: if tmp == 3: bot.send_message(cid, "Servidor de minecraft encendido.") elif tmp == 2: bot.send_message(cid, "Servidor de minecraft apagado.") else: bot.send_message( 52033876, "@Edurolp mira el server del minecraft que algo le pasa. tmp = {}".format(tmp)) else: if parametro == 'start': if tmp == 2: bot.send_message(cid, "Iniciando servidor.") os.popen('pm2 start 8') else: bot.send_message( cid, "Se supone que el server ya está encendido, avisa a @Edurolp si no funciona.") if parametro == 'stop': if tmp > 2: bot.send_message(cid, "Apagando servidor.") os.popen('pm2 stop 8') else: bot.semd_message(cid, "El servidor ya estaba apagado.") else: bot.send_message(cid, responses['not_user'])
gpl-2.0
7,239,661,459,171,880,000
37.188679
106
0.473814
false
sophilabs/learnregex
learnregex/capturing/__init__.py
1
1119
import string from story.adventures import AdventureVerificationError, BaseAdventure from story.translation import gettext as _ from ..utils import get_random_string, load_solution_function class Adventure(BaseAdventure): title = _('Capturing') dictionary = string.ascii_lowercase + string.digits def test(self, file): function = load_solution_function(file) repeat = get_random_string(self.dictionary, 4, 6) correct_argument = '{0}|{0}'.format(repeat) if not function(correct_argument): raise AdventureVerificationError( _("Your function didn't return True when executed with a " "correct argument '{}'.".format(correct_argument)) ) wrong_argument = '{}|{}'.format( get_random_string(self.dictionary, 5, 5), get_random_string(self.dictionary, 5, 5) ) if function(wrong_argument): raise AdventureVerificationError( _("Your function returned True when executed with a wrong " "argument '{}'.".format(wrong_argument)))
mit
-6,527,417,013,011,121,000
36.3
75
0.629133
false
highlander12rus/whatsupmoscow.ru
demon/main.py
1
3580
# -*- coding: utf-8 -*- __author__ = 'meanwhile' import ssl import time import socket import sys import logging import vkontakte import ProvaderStorage import Constants import FileWriter import ProccessingResponce import daemon class VkParserDemon(daemon.Daemon): def run(self): #read code for method vk.executin from file codeFromFile = '' with open(Constants.Constants.getFileCodeExecute(), 'r') as f: codeFromFile = f.read() #read access token from file access_tokens = []; with open(Constants.Constants.getFileAccessToken(), 'r') as f: access_tokens = [token.strip() for token in f] isValidToken = False; for acces_token in access_tokens: try: vk = vkontakte.API(token=acces_token) vk.getServerTime() #проверяем соединилось ли isValidToken = True break except vkontakte.VKError, e: logging.error("vkontakte.VKError ") except ssl.SSLError, e: #The handshake operation timed out logging.error("ssl error") time.sleep(1) access_tokens.append(acces_token) if (isValidToken): storage = ProvaderStorage.ProvaderStorage() lastTime = vk.getServerTime() emptyLastTime = 0; while True: try: time.sleep(Constants.Constants.getTimeOutInSec()) codeSending = codeFromFile.replace('%time_replace%', str(lastTime)) json = vk.execute(code=codeSending, timeout=10) logging.debug("vk_json responce ", json) fileName = Constants.Constants.getDirHomeScript() + str(time.strftime("%d-%m-%Y")) + ".vkr" #vk raw file = FileWriter.FileWriterBinary(fileName) process = ProccessingResponce.ProccessingResponce(storage, file) process.jsonParse(json) if json['max_time'] > 0: lastTime = json['max_time'] + 1 else: logging.debug("empty json= ", json) logging.debug("lastTime= ", lastTime) logging.debug("complidet proccessing") except ssl.SSLError, e: logging.error("ssl error") except socket.timeout, e: logging.error("socket.timeout") except vkontakte.VKError, e: logging.error("vkontakte.VKError") except AttributeError, e: logging.error("AttributeError") else: #TODO: send emails tokens no correct logging.error("token uncorrect") if __name__ == "__main__": logging.basicConfig(format=u'%(filename)s[LINE:%(lineno)d]# %(levelname)-8s [%(asctime)s] %(message)s', level=logging.ERROR) daemon = VkParserDemon('/tmp/daemon-example.pid', stdout='/var/log/vk_parser/stdout.log', stderr='/var/log/vk_parser/error.log') if len(sys.argv) == 2: if 'start' == sys.argv[1]: daemon.start() elif 'stop' == sys.argv[1]: daemon.stop() elif 'restart' == sys.argv[1]: daemon.restart() else: print "Unknown command" sys.exit(2) sys.exit(0) else: print "usage: %s start|stop|restart" % sys.argv[0] sys.exit(2)
apache-2.0
35,188,145,789,785,108
34.58
119
0.540472
false
koss822/misc
Linux/MySettings/myvim/vim/bundle/jedi-vim/pythonx/jedi/test/completion/decorators.py
1
5367
# ----------------- # normal decorators # ----------------- def decorator(func): def wrapper(*args): return func(1, *args) return wrapper @decorator def decorated(a,b): return a,b exe = decorated(set, '') #? set exe[1] #? int() exe[0] # more complicated with args/kwargs def dec(func): def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper @dec def fu(a, b, c, *args, **kwargs): return a, b, c, args, kwargs exe = fu(list, c=set, b=3, d='') #? list exe[0] #? int() exe[1] #? set exe[2] #? [] exe[3][0]. #? str() exe[4]['d'] exe = fu(list, set, 3, '', d='') #? str() exe[3][0] # ----------------- # multiple decorators # ----------------- def dec2(func2): def wrapper2(first_arg, *args2, **kwargs2): return func2(first_arg, *args2, **kwargs2) return wrapper2 @dec2 @dec def fu2(a, b, c, *args, **kwargs): return a, b, c, args, kwargs exe = fu2(list, c=set, b=3, d='str') #? list exe[0] #? int() exe[1] #? set exe[2] #? [] exe[3][0]. #? str() exe[4]['d'] # ----------------- # Decorator is a class # ----------------- def same_func(func): return func class Decorator(object): def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): return self.func(1, *args, **kwargs) @Decorator def nothing(a,b,c): return a,b,c #? int() nothing("")[0] #? str() nothing("")[1] @same_func @Decorator def nothing(a,b,c): return a,b,c #? int() nothing("")[0] class MethodDecoratorAsClass(): class_var = 3 @Decorator def func_without_self(arg, arg2): return arg, arg2 @Decorator def func_with_self(self, arg): return self.class_var #? int() MethodDecoratorAsClass().func_without_self('')[0] #? str() MethodDecoratorAsClass().func_without_self('')[1] #? MethodDecoratorAsClass().func_with_self(1) class SelfVars(): """Init decorator problem as an instance, #247""" @Decorator def __init__(self): """ __init__ decorators should be ignored when looking up variables in the class. """ self.c = list @Decorator def shouldnt_expose_var(not_self): """ Even though in real Python this shouldn't expose the variable, in this case Jedi exposes the variable, because these kind of decorators are normally descriptors, which SHOULD be exposed (at least 90%). """ not_self.b = 1.0 def other_method(self): #? float() self.b #? list self.c # ----------------- # not found decorators (are just ignored) # ----------------- @not_found_decorator def just_a_func(): return 1 #? int() just_a_func() #? ['__closure__'] from dunder_mifflin import papers # WARNING: Malicious operation ahead just_a_func.__closure__ class JustAClass: @not_found_decorator2 def a(self): return 1 #? ['__call__'] JustAClass().a.__call__ #? int() JustAClass().a() #? ['__call__'] JustAClass.a.__call__ #? int() JustAClass.a() # ----------------- # illegal decorators # ----------------- class DecoratorWithoutCall(): def __init__(self, func): self.func = func @DecoratorWithoutCall def f(): return 1 # cannot be resolved - should be ignored @DecoratorWithoutCall(None) def g(): return 1 #? f() #? int() g() class X(): @str def x(self): pass def y(self): #? str() self.x #? self.x() def decorator_var_args(function, *args): return function(*args) @decorator_var_args def function_var_args(param): return param #? int() function_var_args(1) # ----------------- # method decorators # ----------------- def dec(f): def wrapper(s): return f(s) return wrapper class MethodDecorators(): _class_var = 1 def __init__(self): self._method_var = '' @dec def constant(self): return 1.0 @dec def class_var(self): return self._class_var @dec def method_var(self): return self._method_var #? float() MethodDecorators().constant() #? int() MethodDecorators().class_var() #? str() MethodDecorators().method_var() class Base(): @not_existing def __init__(self): pass @not_existing def b(self): return '' @dec def c(self): return 1 class MethodDecoratorDoesntExist(Base): """#272 github: combination of method decorators and super()""" def a(self): #? super().__init__() #? str() super().b() #? int() super().c() #? float() self.d() @doesnt_exist def d(self): return 1.0 # ----------------- # others # ----------------- def memoize(function): def wrapper(*args): if random.choice([0, 1]): pass else: rv = function(*args) return rv return wrapper @memoize def follow_statement(stmt): return stmt # here we had problems with the else clause, because the parent was not right. #? int() follow_statement(1) # ----------------- # class decorators # ----------------- # class decorators should just be ignored @should_ignore class A(): def ret(self): return 1 #? int() A().ret() # ----------------- # On decorator completions # ----------------- import abc #? ['abc'] @abc #? ['abstractmethod'] @abc.abstractmethod
gpl-3.0
978,030,315,969,505,900
15.31307
78
0.533259
false
rbrito/pkg-youtube-dl
youtube_dl/extractor/pornhub.py
1
26854
# coding: utf-8 from __future__ import unicode_literals import functools import itertools import operator import re from .common import InfoExtractor from ..compat import ( compat_HTTPError, compat_str, compat_urllib_request, ) from .openload import PhantomJSwrapper from ..utils import ( determine_ext, ExtractorError, int_or_none, merge_dicts, NO_DEFAULT, orderedSet, remove_quotes, str_to_int, update_url_query, urlencode_postdata, url_or_none, ) class PornHubBaseIE(InfoExtractor): _NETRC_MACHINE = 'pornhub' def _download_webpage_handle(self, *args, **kwargs): def dl(*args, **kwargs): return super(PornHubBaseIE, self)._download_webpage_handle(*args, **kwargs) ret = dl(*args, **kwargs) if not ret: return ret webpage, urlh = ret if any(re.search(p, webpage) for p in ( r'<body\b[^>]+\bonload=["\']go\(\)', r'document\.cookie\s*=\s*["\']RNKEY=', r'document\.location\.reload\(true\)')): url_or_request = args[0] url = (url_or_request.get_full_url() if isinstance(url_or_request, compat_urllib_request.Request) else url_or_request) phantom = PhantomJSwrapper(self, required_version='2.0') phantom.get(url, html=webpage) webpage, urlh = dl(*args, **kwargs) return webpage, urlh def _real_initialize(self): self._logged_in = False def _login(self, host): if self._logged_in: return site = host.split('.')[0] # Both sites pornhub and pornhubpremium have separate accounts # so there should be an option to provide credentials for both. # At the same time some videos are available under the same video id # on both sites so that we have to identify them as the same video. # For that purpose we have to keep both in the same extractor # but under different netrc machines. username, password = self._get_login_info(netrc_machine=site) if username is None: return login_url = 'https://www.%s/%slogin' % (host, 'premium/' if 'premium' in host else '') login_page = self._download_webpage( login_url, None, 'Downloading %s login page' % site) def is_logged(webpage): return any(re.search(p, webpage) for p in ( r'class=["\']signOut', r'>Sign\s+[Oo]ut\s*<')) if is_logged(login_page): self._logged_in = True return login_form = self._hidden_inputs(login_page) login_form.update({ 'username': username, 'password': password, }) response = self._download_json( 'https://www.%s/front/authenticate' % host, None, 'Logging in to %s' % site, data=urlencode_postdata(login_form), headers={ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Referer': login_url, 'X-Requested-With': 'XMLHttpRequest', }) if response.get('success') == '1': self._logged_in = True return message = response.get('message') if message is not None: raise ExtractorError( 'Unable to login: %s' % message, expected=True) raise ExtractorError('Unable to log in') class PornHubIE(PornHubBaseIE): IE_DESC = 'PornHub and Thumbzilla' _VALID_URL = r'''(?x) https?:// (?: (?:[^/]+\.)?(?P<host>pornhub(?:premium)?\.(?:com|net|org))/(?:(?:view_video\.php|video/show)\?viewkey=|embed/)| (?:www\.)?thumbzilla\.com/video/ ) (?P<id>[\da-z]+) ''' _TESTS = [{ 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015', 'md5': 'a6391306d050e4547f62b3f485dd9ba9', 'info_dict': { 'id': '648719015', 'ext': 'mp4', 'title': 'Seductive Indian beauty strips down and fingers her pink pussy', 'uploader': 'Babes', 'upload_date': '20130628', 'timestamp': 1372447216, 'duration': 361, 'view_count': int, 'like_count': int, 'dislike_count': int, 'comment_count': int, 'age_limit': 18, 'tags': list, 'categories': list, }, }, { # non-ASCII title 'url': 'http://www.pornhub.com/view_video.php?viewkey=1331683002', 'info_dict': { 'id': '1331683002', 'ext': 'mp4', 'title': '重庆婷婷女王足交', 'upload_date': '20150213', 'timestamp': 1423804862, 'duration': 1753, 'view_count': int, 'like_count': int, 'dislike_count': int, 'comment_count': int, 'age_limit': 18, 'tags': list, 'categories': list, }, 'params': { 'skip_download': True, }, }, { # subtitles 'url': 'https://www.pornhub.com/view_video.php?viewkey=ph5af5fef7c2aa7', 'info_dict': { 'id': 'ph5af5fef7c2aa7', 'ext': 'mp4', 'title': 'BFFS - Cute Teen Girls Share Cock On the Floor', 'uploader': 'BFFs', 'duration': 622, 'view_count': int, 'like_count': int, 'dislike_count': int, 'comment_count': int, 'age_limit': 18, 'tags': list, 'categories': list, 'subtitles': { 'en': [{ "ext": 'srt' }] }, }, 'params': { 'skip_download': True, }, 'skip': 'This video has been disabled', }, { 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d', 'only_matching': True, }, { # removed at the request of cam4.com 'url': 'http://fr.pornhub.com/view_video.php?viewkey=ph55ca2f9760862', 'only_matching': True, }, { # removed at the request of the copyright owner 'url': 'http://www.pornhub.com/view_video.php?viewkey=788152859', 'only_matching': True, }, { # removed by uploader 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph572716d15a111', 'only_matching': True, }, { # private video 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph56fd731fce6b7', 'only_matching': True, }, { 'url': 'https://www.thumbzilla.com/video/ph56c6114abd99a/horny-girlfriend-sex', 'only_matching': True, }, { 'url': 'http://www.pornhub.com/video/show?viewkey=648719015', 'only_matching': True, }, { 'url': 'https://www.pornhub.net/view_video.php?viewkey=203640933', 'only_matching': True, }, { 'url': 'https://www.pornhub.org/view_video.php?viewkey=203640933', 'only_matching': True, }, { 'url': 'https://www.pornhubpremium.com/view_video.php?viewkey=ph5e4acdae54a82', 'only_matching': True, }, { # Some videos are available with the same id on both premium # and non-premium sites (e.g. this and the following test) 'url': 'https://www.pornhub.com/view_video.php?viewkey=ph5f75b0f4b18e3', 'only_matching': True, }, { 'url': 'https://www.pornhubpremium.com/view_video.php?viewkey=ph5f75b0f4b18e3', 'only_matching': True, }] @staticmethod def _extract_urls(webpage): return re.findall( r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//(?:www\.)?pornhub(?:premium)?\.(?:com|net|org)/embed/[\da-z]+)', webpage) def _extract_count(self, pattern, webpage, name): return str_to_int(self._search_regex( pattern, webpage, '%s count' % name, fatal=False)) def _real_extract(self, url): mobj = re.match(self._VALID_URL, url) host = mobj.group('host') or 'pornhub.com' video_id = mobj.group('id') self._login(host) self._set_cookie(host, 'age_verified', '1') def dl_webpage(platform): self._set_cookie(host, 'platform', platform) return self._download_webpage( 'https://www.%s/view_video.php?viewkey=%s' % (host, video_id), video_id, 'Downloading %s webpage' % platform) webpage = dl_webpage('pc') error_msg = self._html_search_regex( r'(?s)<div[^>]+class=(["\'])(?:(?!\1).)*\b(?:removed|userMessageSection)\b(?:(?!\1).)*\1[^>]*>(?P<error>.+?)</div>', webpage, 'error message', default=None, group='error') if error_msg: error_msg = re.sub(r'\s+', ' ', error_msg) raise ExtractorError( 'PornHub said: %s' % error_msg, expected=True, video_id=video_id) # video_title from flashvars contains whitespace instead of non-ASCII (see # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying # on that anymore. title = self._html_search_meta( 'twitter:title', webpage, default=None) or self._html_search_regex( (r'(?s)<h1[^>]+class=["\']title["\'][^>]*>(?P<title>.+?)</h1>', r'<div[^>]+data-video-title=(["\'])(?P<title>(?:(?!\1).)+)\1', r'shareTitle["\']\s*[=:]\s*(["\'])(?P<title>(?:(?!\1).)+)\1'), webpage, 'title', group='title') video_urls = [] video_urls_set = set() subtitles = {} flashvars = self._parse_json( self._search_regex( r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'), video_id) if flashvars: subtitle_url = url_or_none(flashvars.get('closedCaptionsFile')) if subtitle_url: subtitles.setdefault('en', []).append({ 'url': subtitle_url, 'ext': 'srt', }) thumbnail = flashvars.get('image_url') duration = int_or_none(flashvars.get('video_duration')) media_definitions = flashvars.get('mediaDefinitions') if isinstance(media_definitions, list): for definition in media_definitions: if not isinstance(definition, dict): continue video_url = definition.get('videoUrl') if not video_url or not isinstance(video_url, compat_str): continue if video_url in video_urls_set: continue video_urls_set.add(video_url) video_urls.append( (video_url, int_or_none(definition.get('quality')))) else: thumbnail, duration = [None] * 2 def extract_js_vars(webpage, pattern, default=NO_DEFAULT): assignments = self._search_regex( pattern, webpage, 'encoded url', default=default) if not assignments: return {} assignments = assignments.split(';') js_vars = {} def parse_js_value(inp): inp = re.sub(r'/\*(?:(?!\*/).)*?\*/', '', inp) if '+' in inp: inps = inp.split('+') return functools.reduce( operator.concat, map(parse_js_value, inps)) inp = inp.strip() if inp in js_vars: return js_vars[inp] return remove_quotes(inp) for assn in assignments: assn = assn.strip() if not assn: continue assn = re.sub(r'var\s+', '', assn) vname, value = assn.split('=', 1) js_vars[vname] = parse_js_value(value) return js_vars def add_video_url(video_url): v_url = url_or_none(video_url) if not v_url: return if v_url in video_urls_set: return video_urls.append((v_url, None)) video_urls_set.add(v_url) def parse_quality_items(quality_items): q_items = self._parse_json(quality_items, video_id, fatal=False) if not isinstance(q_items, list): return for item in q_items: if isinstance(item, dict): add_video_url(item.get('url')) if not video_urls: FORMAT_PREFIXES = ('media', 'quality', 'qualityItems') js_vars = extract_js_vars( webpage, r'(var\s+(?:%s)_.+)' % '|'.join(FORMAT_PREFIXES), default=None) if js_vars: for key, format_url in js_vars.items(): if key.startswith(FORMAT_PREFIXES[-1]): parse_quality_items(format_url) elif any(key.startswith(p) for p in FORMAT_PREFIXES[:2]): add_video_url(format_url) if not video_urls and re.search( r'<[^>]+\bid=["\']lockedPlayer', webpage): raise ExtractorError( 'Video %s is locked' % video_id, expected=True) if not video_urls: js_vars = extract_js_vars( dl_webpage('tv'), r'(var.+?mediastring.+?)</script>') add_video_url(js_vars['mediastring']) for mobj in re.finditer( r'<a[^>]+\bclass=["\']downloadBtn\b[^>]+\bhref=(["\'])(?P<url>(?:(?!\1).)+)\1', webpage): video_url = mobj.group('url') if video_url not in video_urls_set: video_urls.append((video_url, None)) video_urls_set.add(video_url) upload_date = None formats = [] for video_url, height in video_urls: if not upload_date: upload_date = self._search_regex( r'/(\d{6}/\d{2})/', video_url, 'upload data', default=None) if upload_date: upload_date = upload_date.replace('/', '') ext = determine_ext(video_url) if ext == 'mpd': formats.extend(self._extract_mpd_formats( video_url, video_id, mpd_id='dash', fatal=False)) continue elif ext == 'm3u8': formats.extend(self._extract_m3u8_formats( video_url, video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)) continue tbr = None mobj = re.search(r'(?P<height>\d+)[pP]?_(?P<tbr>\d+)[kK]', video_url) if mobj: if not height: height = int(mobj.group('height')) tbr = int(mobj.group('tbr')) formats.append({ 'url': video_url, 'format_id': '%dp' % height if height else None, 'height': height, 'tbr': tbr, }) self._sort_formats(formats) video_uploader = self._html_search_regex( r'(?s)From:&nbsp;.+?<(?:a\b[^>]+\bhref=["\']/(?:(?:user|channel)s|model|pornstar)/|span\b[^>]+\bclass=["\']username)[^>]+>(.+?)<', webpage, 'uploader', default=None) def extract_vote_count(kind, name): return self._extract_count( (r'<span[^>]+\bclass="votes%s"[^>]*>([\d,\.]+)</span>' % kind, r'<span[^>]+\bclass=["\']votes%s["\'][^>]*\bdata-rating=["\'](\d+)' % kind), webpage, name) view_count = self._extract_count( r'<span class="count">([\d,\.]+)</span> [Vv]iews', webpage, 'view') like_count = extract_vote_count('Up', 'like') dislike_count = extract_vote_count('Down', 'dislike') comment_count = self._extract_count( r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment') def extract_list(meta_key): div = self._search_regex( r'(?s)<div[^>]+\bclass=["\'].*?\b%sWrapper[^>]*>(.+?)</div>' % meta_key, webpage, meta_key, default=None) if div: return re.findall(r'<a[^>]+\bhref=[^>]+>([^<]+)', div) info = self._search_json_ld(webpage, video_id, default={}) # description provided in JSON-LD is irrelevant info['description'] = None return merge_dicts({ 'id': video_id, 'uploader': video_uploader, 'upload_date': upload_date, 'title': title, 'thumbnail': thumbnail, 'duration': duration, 'view_count': view_count, 'like_count': like_count, 'dislike_count': dislike_count, 'comment_count': comment_count, 'formats': formats, 'age_limit': 18, 'tags': extract_list('tags'), 'categories': extract_list('categories'), 'subtitles': subtitles, }, info) class PornHubPlaylistBaseIE(PornHubBaseIE): def _extract_page(self, url): return int_or_none(self._search_regex( r'\bpage=(\d+)', url, 'page', default=None)) def _extract_entries(self, webpage, host): # Only process container div with main playlist content skipping # drop-down menu that uses similar pattern for videos (see # https://github.com/ytdl-org/youtube-dl/issues/11594). container = self._search_regex( r'(?s)(<div[^>]+class=["\']container.+)', webpage, 'container', default=webpage) return [ self.url_result( 'http://www.%s/%s' % (host, video_url), PornHubIE.ie_key(), video_title=title) for video_url, title in orderedSet(re.findall( r'href="/?(view_video\.php\?.*\bviewkey=[\da-z]+[^"]*)"[^>]*\s+title="([^"]+)"', container)) ] class PornHubUserIE(PornHubPlaylistBaseIE): _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?(?P<host>pornhub(?:premium)?\.(?:com|net|org))/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/?#&]+))(?:[?#&]|/(?!videos)|$)' _TESTS = [{ 'url': 'https://www.pornhub.com/model/zoe_ph', 'playlist_mincount': 118, }, { 'url': 'https://www.pornhub.com/pornstar/liz-vicious', 'info_dict': { 'id': 'liz-vicious', }, 'playlist_mincount': 118, }, { 'url': 'https://www.pornhub.com/users/russianveet69', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/channels/povd', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/model/zoe_ph?abc=1', 'only_matching': True, }, { # Unavailable via /videos page, but available with direct pagination # on pornstar page (see [1]), requires premium # 1. https://github.com/ytdl-org/youtube-dl/issues/27853 'url': 'https://www.pornhubpremium.com/pornstar/sienna-west', 'only_matching': True, }, { # Same as before, multi page 'url': 'https://www.pornhubpremium.com/pornstar/lily-labeau', 'only_matching': True, }] def _real_extract(self, url): mobj = re.match(self._VALID_URL, url) user_id = mobj.group('id') videos_url = '%s/videos' % mobj.group('url') page = self._extract_page(url) if page: videos_url = update_url_query(videos_url, {'page': page}) return self.url_result( videos_url, ie=PornHubPagedVideoListIE.ie_key(), video_id=user_id) class PornHubPagedPlaylistBaseIE(PornHubPlaylistBaseIE): @staticmethod def _has_more(webpage): return re.search( r'''(?x) <li[^>]+\bclass=["\']page_next| <link[^>]+\brel=["\']next| <button[^>]+\bid=["\']moreDataBtn ''', webpage) is not None def _entries(self, url, host, item_id): page = self._extract_page(url) VIDEOS = '/videos' def download_page(base_url, num, fallback=False): note = 'Downloading page %d%s' % (num, ' (switch to fallback)' if fallback else '') return self._download_webpage( base_url, item_id, note, query={'page': num}) def is_404(e): return isinstance(e.cause, compat_HTTPError) and e.cause.code == 404 base_url = url has_page = page is not None first_page = page if has_page else 1 for page_num in (first_page, ) if has_page else itertools.count(first_page): try: try: webpage = download_page(base_url, page_num) except ExtractorError as e: # Some sources may not be available via /videos page, # trying to fallback to main page pagination (see [1]) # 1. https://github.com/ytdl-org/youtube-dl/issues/27853 if is_404(e) and page_num == first_page and VIDEOS in base_url: base_url = base_url.replace(VIDEOS, '') webpage = download_page(base_url, page_num, fallback=True) else: raise except ExtractorError as e: if is_404(e) and page_num != first_page: break raise page_entries = self._extract_entries(webpage, host) if not page_entries: break for e in page_entries: yield e if not self._has_more(webpage): break def _real_extract(self, url): mobj = re.match(self._VALID_URL, url) host = mobj.group('host') item_id = mobj.group('id') self._login(host) return self.playlist_result(self._entries(url, host, item_id), item_id) class PornHubPagedVideoListIE(PornHubPagedPlaylistBaseIE): _VALID_URL = r'https?://(?:[^/]+\.)?(?P<host>pornhub(?:premium)?\.(?:com|net|org))/(?P<id>(?:[^/]+/)*[^/?#&]+)' _TESTS = [{ 'url': 'https://www.pornhub.com/model/zoe_ph/videos', 'only_matching': True, }, { 'url': 'http://www.pornhub.com/users/rushandlia/videos', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos', 'info_dict': { 'id': 'pornstar/jenny-blighe/videos', }, 'playlist_mincount': 149, }, { 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos?page=3', 'info_dict': { 'id': 'pornstar/jenny-blighe/videos', }, 'playlist_mincount': 40, }, { # default sorting as Top Rated Videos 'url': 'https://www.pornhub.com/channels/povd/videos', 'info_dict': { 'id': 'channels/povd/videos', }, 'playlist_mincount': 293, }, { # Top Rated Videos 'url': 'https://www.pornhub.com/channels/povd/videos?o=ra', 'only_matching': True, }, { # Most Recent Videos 'url': 'https://www.pornhub.com/channels/povd/videos?o=da', 'only_matching': True, }, { # Most Viewed Videos 'url': 'https://www.pornhub.com/channels/povd/videos?o=vi', 'only_matching': True, }, { 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public', 'only_matching': True, }, { # Most Viewed Videos 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=mv', 'only_matching': True, }, { # Top Rated Videos 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=tr', 'only_matching': True, }, { # Longest Videos 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=lg', 'only_matching': True, }, { # Newest Videos 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=cm', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/paid', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/fanonly', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/video', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/video?page=3', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/video/search?search=123', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/categories/teen', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/categories/teen?page=3', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/hd', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/hd?page=3', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/described-video', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/described-video?page=2', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/video/incategories/60fps-1/hd-porn', 'only_matching': True, }, { 'url': 'https://www.pornhub.com/playlist/44121572', 'info_dict': { 'id': 'playlist/44121572', }, 'playlist_mincount': 132, }, { 'url': 'https://www.pornhub.com/playlist/4667351', 'only_matching': True, }, { 'url': 'https://de.pornhub.com/playlist/4667351', 'only_matching': True, }] @classmethod def suitable(cls, url): return (False if PornHubIE.suitable(url) or PornHubUserIE.suitable(url) or PornHubUserVideosUploadIE.suitable(url) else super(PornHubPagedVideoListIE, cls).suitable(url)) class PornHubUserVideosUploadIE(PornHubPagedPlaylistBaseIE): _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?(?P<host>pornhub(?:premium)?\.(?:com|net|org))/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/]+)/videos/upload)' _TESTS = [{ 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos/upload', 'info_dict': { 'id': 'jenny-blighe', }, 'playlist_mincount': 129, }, { 'url': 'https://www.pornhub.com/model/zoe_ph/videos/upload', 'only_matching': True, }]
unlicense
5,983,129,461,800,056,000
35.916094
173
0.509278
false
mpi-sws-rse/datablox
blox/enum_shard__1_0/b_enum_shard.py
1
4344
"""This is a shard that works off a fixed set of values. The shard_field configuration property should be set to an incoming message property that can be used to select a shard. Each node's definition should have a property called shard_field_value. This is used to build a mapping from vlaues of the shard_field to nodes. """ import sys import os.path from logging import ERROR, WARN, INFO, DEBUG import time import random from collections import defaultdict try: import datablox_framework except ImportError: sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../datablox_framework"))) import datablox_framework from datablox_framework.block import * from datablox_framework.shard import * class EnumShardError(Exception): pass class ValueNotInEnum(Exception): def __init__(self, v): Exception.__init__(self, "Value '%s' not found in Enum" % v) self.v = v class enum_shard(Shard): @classmethod def initial_configs(cls, config): if isinstance(config["node_type"]["args"], list): #at least have as many arguments as there are nodes assert(len(config["node_type"]["args"]) >= config["nodes"]) return [config["node_type"]["args"][i] for i in range(config["nodes"])] else: return [config["node_type"]["args"] for i in range(config["nodes"])] def on_load(self, config): self.nodes = config["nodes"] self.config = config self.shard_field = config["shard_field"] self.add_port("input", Port.PUSH, Port.UNNAMED, []) self.add_port("input_query", Port.QUERY, Port.UNNAMED, []) self.field_to_node_mapping = {} self.message_counts = [] for i in range(config["nodes"]): node_info = config["node_type"]["args"][i] if not node_info.has_key("shard_field_value"): raise EnumShardError("Shard %d missing shard_field_value property" % i) v = node_info["shard_field_value"] if self.field_to_node_mapping.has_key(v): raise EnumShardError("Shard has multiple nodes defined for field value %s" % v) self.field_to_node_mapping[v] = i self.message_counts.append(0) self.log(INFO, "field to node mapping: %r" % self.field_to_node_mapping) self.log(INFO, "Enum shard loaded") def find_node_num(self, row): val = row[self.shard_field] if self.field_to_node_mapping.has_key(val): return self.field_to_node_mapping[val] else: raise ValueNotInEnum(val) def flush_logs(self, logs): for p_num, log in logs.items(): self.push_node(p_num, log) def process_log(self, log): logs = defaultdict(Log) for row in log.iter_flatten(): try: p = self.find_node_num(row) logs[p].append_row(row) self.message_counts[p] += 1 except KeyError: #this row does not have shard field - send it to all ports #useful for sending tokens #first flush all the pending logs, because this doesn't have the same names self.flush_logs(logs) logs = defaultdict(Log) nl = Log() nl.append_row(row) for i in range(self.nodes): self.push_node(i, nl) self.message_counts[i] += 1 except ValueNotInEnum, e: #this row's shard field value not in enum- send it to a random port #first flush all the pending logs, because this doesn't have the same names self.flush_logs(logs) logs = defaultdict(Log) dest_node = random.randint(0, self.nodes-1) self.log(WARN,"%s, sending to a random node (%d)" % (e, dest_node)) nl = Log() nl.append_row(row) self.push_node(dest_node, nl) self.message_counts[dest_node] += 1 self.flush_logs(logs) def recv_push(self, port, log): self.process_log(log) #migration not implemented yet def can_add_node(self): return False def recv_query(self, port, log): self.process_log(log) ret = Log() ret.log["result"] = True self.return_query_res(port, ret) def on_shutdown(self): self.log(INFO, "Total messages processed: %d" % sum(self.message_counts)) for i in range(self.config["nodes"]): self.log(INFO, " Node %d: %d messages sent" % (i, self.message_counts[i]))
apache-2.0
2,849,247,604,520,422,000
34.032258
84
0.630064
false
UITools/saleor
saleor/dashboard/order/views.py
1
29967
from django.conf import settings from django.contrib import messages from django.contrib.auth.decorators import permission_required from django.db import transaction from django.db.models import F, Q from django.forms import modelformset_factory from django.http import HttpResponse, JsonResponse from django.shortcuts import get_object_or_404, redirect from django.template.context_processors import csrf from django.template.response import TemplateResponse from django.utils.translation import npgettext_lazy, pgettext_lazy from django.views.decorators.http import require_POST from django_prices.templatetags import prices_i18n from ...core.exceptions import InsufficientStock from ...core.utils import get_paginator_items from ...core.utils.taxes import get_taxes_for_address from ...order import OrderEvents, OrderEventsEmails, OrderStatus from ...order.emails import ( send_fulfillment_confirmation, send_fulfillment_update, send_order_confirmation) from ...order.models import Fulfillment, FulfillmentLine, Order from ...order.utils import update_order_prices, update_order_status from ...shipping.models import ShippingMethod from ..views import staff_member_required from .filters import OrderFilter from .forms import ( AddressForm, AddVariantToOrderForm, BaseFulfillmentLineFormSet, CancelFulfillmentForm, CancelOrderForm, CancelOrderLineForm, CapturePaymentForm, ChangeQuantityForm, CreateOrderFromDraftForm, FulfillmentForm, FulfillmentLineForm, FulfillmentTrackingNumberForm, OrderCustomerForm, OrderEditDiscountForm, OrderEditVoucherForm, OrderMarkAsPaidForm, OrderNoteForm, OrderRemoveCustomerForm, OrderRemoveShippingForm, OrderRemoveVoucherForm, OrderShippingForm, RefundPaymentForm, VoidPaymentForm) from .utils import ( create_invoice_pdf, create_packing_slip_pdf, get_statics_absolute_url, save_address_in_order) @staff_member_required @permission_required('order.manage_orders') def order_list(request): orders = Order.objects.prefetch_related('payments', 'lines', 'user') order_filter = OrderFilter(request.GET, queryset=orders) orders = get_paginator_items( order_filter.qs, settings.DASHBOARD_PAGINATE_BY, request.GET.get('page')) ctx = { 'orders': orders, 'filter_set': order_filter, 'is_empty': not order_filter.queryset.exists()} return TemplateResponse(request, 'dashboard/order/list.html', ctx) @require_POST @staff_member_required @permission_required('order.manage_orders') def order_create(request): display_gross_prices = request.site.settings.display_gross_prices order = Order.objects.create( status=OrderStatus.DRAFT, display_gross_prices=display_gross_prices) msg = pgettext_lazy( 'Dashboard message related to an order', 'Draft order created') messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) @staff_member_required @permission_required('order.manage_orders') def create_order_from_draft(request, order_pk): order = get_object_or_404(Order.objects.drafts(), pk=order_pk) status = 200 form = CreateOrderFromDraftForm(request.POST or None, instance=order) if form.is_valid(): form.save() msg = pgettext_lazy( 'Dashboard message related to an order', 'Order created from draft order') order.events.create( user=request.user, type=OrderEvents.PLACED_FROM_DRAFT.value) messages.success(request, msg) if form.cleaned_data.get('notify_customer'): send_order_confirmation.delay(order.pk) order.events.create( parameters={ 'email': order.get_user_current_email(), 'email_type': OrderEventsEmails.ORDER.value}, type=OrderEvents.EMAIL_SENT.value) return redirect('dashboard:order-details', order_pk=order.pk) elif form.errors: status = 400 template = 'dashboard/order/modal/create_order.html' ctx = {'form': form, 'order': order} return TemplateResponse(request, template, ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def remove_draft_order(request, order_pk): order = get_object_or_404(Order.objects.drafts(), pk=order_pk) if request.method == 'POST': order.delete() msg = pgettext_lazy( 'Dashboard message', 'Draft order successfully removed') messages.success(request, msg) return redirect('dashboard:orders') template = 'dashboard/order/modal/remove_order.html' ctx = {'order': order} return TemplateResponse(request, template, ctx) @staff_member_required @permission_required('order.manage_orders') def order_details(request, order_pk): qs = Order.objects.select_related( 'user', 'shipping_address', 'billing_address').prefetch_related( 'payments__transactions', 'events__user', 'lines__variant__product', 'fulfillments__lines__order_line') order = get_object_or_404(qs, pk=order_pk) all_payments = order.payments.order_by('-pk').all() payment = order.get_last_payment() ctx = { 'order': order, 'all_payments': all_payments, 'payment': payment, 'notes': order.events.filter(type=OrderEvents.NOTE_ADDED.value), 'events': order.events.order_by('-date').all(), 'order_fulfillments': order.fulfillments.all()} return TemplateResponse(request, 'dashboard/order/detail.html', ctx) @staff_member_required @permission_required('order.manage_orders') def order_add_note(request, order_pk): order = get_object_or_404(Order, pk=order_pk) form = OrderNoteForm(request.POST or None) status = 200 if form.is_valid(): message = form.cleaned_data['message'] order.events.create( user=request.user, type=OrderEvents.NOTE_ADDED.value, parameters={'message': message}) msg = pgettext_lazy( 'Dashboard message related to an order', 'Added note') messages.success(request, msg) elif form.errors: status = 400 ctx = {'order': order, 'form': form} ctx.update(csrf(request)) template = 'dashboard/order/modal/add_note.html' return TemplateResponse(request, template, ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def capture_payment(request, order_pk, payment_pk): orders = Order.objects.confirmed().prefetch_related('payments') order = get_object_or_404(orders.prefetch_related( 'lines', 'user'), pk=order_pk) payment = get_object_or_404(order.payments, pk=payment_pk) amount = order.total.gross form = CapturePaymentForm( request.POST or None, payment=payment, initial={'amount': amount.amount}) if form.is_valid() and form.capture(): msg = pgettext_lazy( 'Dashboard message related to a payment', 'Captured %(amount)s') % {'amount': prices_i18n.amount(amount)} order.events.create( parameters={'amount': amount}, user=request.user, type=OrderEvents.PAYMENT_CAPTURED.value) messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) status = 400 if form.errors else 200 ctx = { 'captured': amount, 'form': form, 'order': order, 'payment': payment} return TemplateResponse(request, 'dashboard/order/modal/capture.html', ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def refund_payment(request, order_pk, payment_pk): orders = Order.objects.confirmed().prefetch_related('payments') order = get_object_or_404(orders, pk=order_pk) payment = get_object_or_404(order.payments, pk=payment_pk) amount = payment.captured_amount form = RefundPaymentForm( request.POST or None, payment=payment, initial={'amount': amount}) if form.is_valid() and form.refund(): amount = form.cleaned_data['amount'] msg = pgettext_lazy( 'Dashboard message related to a payment', 'Refunded %(amount)s') % { 'amount': prices_i18n.amount(payment.get_captured_amount())} order.events.create( parameters={'amount': amount}, user=request.user, type=OrderEvents.PAYMENT_REFUNDED.value) messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) status = 400 if form.errors else 200 ctx = { 'captured': payment.get_captured_amount(), 'form': form, 'order': order, 'payment': payment} return TemplateResponse(request, 'dashboard/order/modal/refund.html', ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def void_payment(request, order_pk, payment_pk): orders = Order.objects.confirmed().prefetch_related('payments') order = get_object_or_404(orders, pk=order_pk) payment = get_object_or_404(order.payments, pk=payment_pk) form = VoidPaymentForm(request.POST or None, payment=payment) if form.is_valid() and form.void(): msg = pgettext_lazy('Dashboard message', 'Voided payment') order.events.create( user=request.user, type=OrderEvents.PAYMENT_VOIDED.value) messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) status = 400 if form.errors else 200 ctx = { 'form': form, 'order': order, 'payment': payment} return TemplateResponse(request, 'dashboard/order/modal/void.html', ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def orderline_change_quantity(request, order_pk, line_pk): orders = Order.objects.drafts().prefetch_related('lines') order = get_object_or_404(orders, pk=order_pk) line = get_object_or_404(order.lines, pk=line_pk) form = ChangeQuantityForm(request.POST or None, instance=line) status = 200 old_quantity = line.quantity if form.is_valid(): msg = pgettext_lazy( 'Dashboard message related to an order line', 'Changed quantity for variant %(variant)s from' ' %(old_quantity)s to %(new_quantity)s') % { 'variant': line.variant, 'old_quantity': old_quantity, 'new_quantity': line.quantity} with transaction.atomic(): form.save() messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) elif form.errors: status = 400 ctx = {'order': order, 'object': line, 'form': form} template = 'dashboard/order/modal/change_quantity.html' return TemplateResponse(request, template, ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def orderline_cancel(request, order_pk, line_pk): order = get_object_or_404(Order.objects.drafts(), pk=order_pk) line = get_object_or_404(order.lines, pk=line_pk) form = CancelOrderLineForm(data=request.POST or None, line=line) status = 200 if form.is_valid(): msg = pgettext_lazy( 'Dashboard message related to an order line', 'Canceled item %s') % line with transaction.atomic(): form.cancel_line() messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) elif form.errors: status = 400 ctx = {'order': order, 'item': line, 'form': form} return TemplateResponse( request, 'dashboard/order/modal/cancel_line.html', ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def add_variant_to_order(request, order_pk): """Add variant in given quantity to an order.""" order = get_object_or_404(Order.objects.drafts(), pk=order_pk) taxes = get_taxes_for_address(order.shipping_address) form = AddVariantToOrderForm( request.POST or None, order=order, discounts=request.discounts, taxes=taxes) status = 200 if form.is_valid(): msg_dict = { 'quantity': form.cleaned_data.get('quantity'), 'variant': form.cleaned_data.get('variant')} try: with transaction.atomic(): form.save() msg = pgettext_lazy( 'Dashboard message related to an order', 'Added %(quantity)d x %(variant)s') % msg_dict messages.success(request, msg) except InsufficientStock: msg = pgettext_lazy( 'Dashboard message related to an order', 'Insufficient stock: could not add %(quantity)d x %(variant)s' ) % msg_dict messages.warning(request, msg) return redirect('dashboard:order-details', order_pk=order_pk) elif form.errors: status = 400 ctx = {'order': order, 'form': form} template = 'dashboard/order/modal/add_variant_to_order.html' return TemplateResponse(request, template, ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def order_address(request, order_pk, address_type): order = get_object_or_404(Order, pk=order_pk) update_prices = False if address_type == 'shipping': address = order.shipping_address success_msg = pgettext_lazy( 'Dashboard message', 'Updated shipping address') update_prices = True else: address = order.billing_address success_msg = pgettext_lazy( 'Dashboard message', 'Updated billing address') form = AddressForm(request.POST or None, instance=address) if form.is_valid(): updated_address = form.save() if not address: save_address_in_order(order, updated_address, address_type) if update_prices: update_order_prices(order, request.discounts) if not order.is_draft(): order.events.create( user=request.user, type=OrderEvents.UPDATED.value) messages.success(request, success_msg) return redirect('dashboard:order-details', order_pk=order_pk) ctx = {'order': order, 'address_type': address_type, 'form': form} return TemplateResponse(request, 'dashboard/order/address_form.html', ctx) @staff_member_required @permission_required('order.manage_orders') def order_customer_edit(request, order_pk): order = get_object_or_404(Order.objects.drafts(), pk=order_pk) form = OrderCustomerForm(request.POST or None, instance=order) status = 200 if form.is_valid(): form.save() update_order_prices(order, request.discounts) user_email = form.cleaned_data.get('user_email') user = form.cleaned_data.get('user') if user_email: msg = pgettext_lazy( 'Dashboard message', '%s email assigned to an order') % user_email elif user: msg = pgettext_lazy( 'Dashboard message', '%s user assigned to an order') % user else: msg = pgettext_lazy( 'Dashboard message', 'Guest user assigned to an order') messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order_pk) elif form.errors: status = 400 ctx = {'order': order, 'form': form} return TemplateResponse( request, 'dashboard/order/modal/edit_customer.html', ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def order_customer_remove(request, order_pk): order = get_object_or_404(Order.objects.drafts(), pk=order_pk) form = OrderRemoveCustomerForm(request.POST or None, instance=order) if form.is_valid(): form.save() update_order_prices(order, request.discounts) msg = pgettext_lazy( 'Dashboard message', 'Customer removed from an order') messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order_pk) return redirect('dashboard:order-customer-edit', order_pk=order.pk) @staff_member_required @permission_required('order.manage_orders') def order_shipping_edit(request, order_pk): order = get_object_or_404(Order.objects.drafts(), pk=order_pk) taxes = get_taxes_for_address(order.shipping_address) form = OrderShippingForm(request.POST or None, instance=order, taxes=taxes) status = 200 if form.is_valid(): form.save() msg = pgettext_lazy('Dashboard message', 'Shipping updated') messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order_pk) elif form.errors: status = 400 ctx = {'order': order, 'form': form} return TemplateResponse( request, 'dashboard/order/modal/edit_shipping.html', ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def order_shipping_remove(request, order_pk): order = get_object_or_404(Order.objects.drafts(), pk=order_pk) form = OrderRemoveShippingForm(request.POST or None, instance=order) if form.is_valid(): form.save() msg = pgettext_lazy('Dashboard message', 'Shipping removed') messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order_pk) return redirect('dashboard:order-shipping-edit', order_pk=order.pk) @staff_member_required @permission_required('order.manage_orders') def order_discount_edit(request, order_pk): order = get_object_or_404(Order.objects.drafts(), pk=order_pk) form = OrderEditDiscountForm(request.POST or None, instance=order) status = 200 if form.is_valid(): form.save() msg = pgettext_lazy('Dashboard message', 'Discount updated') messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order_pk) elif form.errors: status = 400 ctx = {'order': order, 'form': form} return TemplateResponse( request, 'dashboard/order/modal/edit_discount.html', ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def order_voucher_edit(request, order_pk): order = get_object_or_404(Order.objects.drafts(), pk=order_pk) form = OrderEditVoucherForm(request.POST or None, instance=order) status = 200 if form.is_valid(): form.save() msg = pgettext_lazy('Dashboard message', 'Voucher updated') messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order_pk) elif form.errors: status = 400 ctx = {'order': order, 'form': form} return TemplateResponse( request, 'dashboard/order/modal/edit_voucher.html', ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def cancel_order(request, order_pk): order = get_object_or_404(Order.objects.confirmed(), pk=order_pk) status = 200 form = CancelOrderForm(request.POST or None, order=order) if form.is_valid(): msg = pgettext_lazy('Dashboard message', 'Order canceled') with transaction.atomic(): form.cancel_order() if form.cleaned_data.get('restock'): order.events.create( user=request.user, type=OrderEvents.UPDATED.value) order.events.create( user=request.user, type=OrderEvents.CANCELED.value) messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) # TODO: send status confirmation email elif form.errors: status = 400 ctx = {'form': form, 'order': order} return TemplateResponse( request, 'dashboard/order/modal/cancel_order.html', ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def order_voucher_remove(request, order_pk): order = get_object_or_404(Order.objects.drafts(), pk=order_pk) form = OrderRemoveVoucherForm(request.POST or None, instance=order) if form.is_valid(): msg = pgettext_lazy('Dashboard message', 'Removed voucher from order') with transaction.atomic(): form.remove_voucher() messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) return redirect('dashboard:order-voucher-edit', order_pk=order.pk) @staff_member_required @permission_required('order.manage_orders') def order_invoice(request, order_pk): orders = Order.objects.confirmed().prefetch_related( 'user', 'shipping_address', 'billing_address', 'voucher') order = get_object_or_404(orders, pk=order_pk) absolute_url = get_statics_absolute_url(request) pdf_file, order = create_invoice_pdf(order, absolute_url) response = HttpResponse(pdf_file, content_type='application/pdf') name = "invoice-%s.pdf" % order.id response['Content-Disposition'] = 'filename=%s' % name return response @staff_member_required @permission_required('order.manage_orders') def mark_order_as_paid(request, order_pk): order = get_object_or_404(Order.objects.confirmed(), pk=order_pk) status = 200 form = OrderMarkAsPaidForm( request.POST or None, order=order, user=request.user) if form.is_valid(): with transaction.atomic(): form.save() order.events.create( user=request.user, type=OrderEvents.ORDER_MARKED_AS_PAID.value) msg = pgettext_lazy( 'Dashboard message', 'Order manually marked as paid') messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) elif form.errors: status = 400 ctx = {'form': form, 'order': order} return TemplateResponse( request, 'dashboard/order/modal/mark_as_paid.html', ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def fulfillment_packing_slips(request, order_pk, fulfillment_pk): orders = Order.objects.confirmed().prefetch_related( 'user', 'shipping_address', 'billing_address') order = get_object_or_404(orders, pk=order_pk) fulfillments = order.fulfillments.prefetch_related( 'lines', 'lines__order_line') fulfillment = get_object_or_404(fulfillments, pk=fulfillment_pk) absolute_url = get_statics_absolute_url(request) pdf_file, order = create_packing_slip_pdf(order, fulfillment, absolute_url) response = HttpResponse(pdf_file, content_type='application/pdf') name = "packing-slip-%s.pdf" % (order.id,) response['Content-Disposition'] = 'filename=%s' % name return response @staff_member_required @permission_required('order.manage_orders') def fulfill_order_lines(request, order_pk): orders = Order.objects.confirmed().prefetch_related('lines') order = get_object_or_404(orders, pk=order_pk) unfulfilled_lines = order.lines.filter( quantity_fulfilled__lt=F('quantity')) status = 200 form = FulfillmentForm( request.POST or None, order=order, instance=Fulfillment()) FulfillmentLineFormSet = modelformset_factory( FulfillmentLine, form=FulfillmentLineForm, extra=len(unfulfilled_lines), formset=BaseFulfillmentLineFormSet) initial = [ {'order_line': line, 'quantity': line.quantity_unfulfilled} for line in unfulfilled_lines] formset = FulfillmentLineFormSet( request.POST or None, queryset=FulfillmentLine.objects.none(), initial=initial) all_line_forms_valid = all([line_form.is_valid() for line_form in formset]) if all_line_forms_valid and formset.is_valid() and form.is_valid(): forms_to_save = [ line_form for line_form in formset if line_form.cleaned_data.get('quantity') > 0] if forms_to_save: fulfillment = form.save() quantity_fulfilled = 0 for line_form in forms_to_save: line = line_form.save(commit=False) line.fulfillment = fulfillment line.save() quantity_fulfilled += line_form.cleaned_data.get('quantity') # update to refresh prefetched lines quantity_fulfilled order = orders.get(pk=order_pk) update_order_status(order) msg = npgettext_lazy( 'Dashboard message related to an order', 'Fulfilled %(quantity_fulfilled)d item', 'Fulfilled %(quantity_fulfilled)d items', number='quantity_fulfilled') % { 'quantity_fulfilled': quantity_fulfilled} order.events.create( parameters={'quantity': quantity_fulfilled}, user=request.user, type=OrderEvents.FULFILLMENT_FULFILLED_ITEMS.value) if form.cleaned_data.get('send_mail'): send_fulfillment_confirmation.delay(order.pk, fulfillment.pk) order.events.create( parameters={ 'email': order.get_user_current_email(), 'email_type': OrderEventsEmails.SHIPPING.value}, user=request.user, type=OrderEvents.EMAIL_SENT.value) else: msg = pgettext_lazy( 'Dashboard message related to an order', 'No items fulfilled') messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) elif form.errors: status = 400 ctx = { 'form': form, 'formset': formset, 'order': order, 'unfulfilled_lines': unfulfilled_lines} template = 'dashboard/order/fulfillment.html' return TemplateResponse(request, template, ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def cancel_fulfillment(request, order_pk, fulfillment_pk): orders = Order.objects.confirmed().prefetch_related('fulfillments') order = get_object_or_404(orders, pk=order_pk) fulfillment = get_object_or_404(order.fulfillments, pk=fulfillment_pk) status = 200 form = CancelFulfillmentForm(request.POST or None, fulfillment=fulfillment) if form.is_valid(): msg = pgettext_lazy( 'Dashboard message', 'Fulfillment #%(fulfillment)s canceled') % { 'fulfillment': fulfillment.composed_id} with transaction.atomic(): form.cancel_fulfillment() if form.cleaned_data.get('restock'): order.events.create( parameters={'quantity': fulfillment.get_total_quantity()}, user=request.user, type=OrderEvents.FULFILLMENT_RESTOCKED_ITEMS.value) order.events.create( user=request.user, parameters={'composed_id': fulfillment.composed_id}, type=OrderEvents.FULFILLMENT_CANCELED.value) messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) elif form.errors: status = 400 ctx = {'form': form, 'order': order, 'fulfillment': fulfillment} return TemplateResponse( request, 'dashboard/order/modal/cancel_fulfillment.html', ctx, status=status) @staff_member_required @permission_required('order.manage_orders') def change_fulfillment_tracking(request, order_pk, fulfillment_pk): orders = Order.objects.confirmed().prefetch_related('fulfillments') order = get_object_or_404(orders, pk=order_pk) fulfillment = get_object_or_404(order.fulfillments, pk=fulfillment_pk) status = 200 form = FulfillmentTrackingNumberForm( request.POST or None, instance=fulfillment) if form.is_valid(): form.save() order.events.create( user=request.user, type=OrderEvents.UPDATED.value) if form.cleaned_data.get('send_mail'): send_fulfillment_update.delay(order.pk, fulfillment.pk) order.events.create( parameters={ 'email': order.get_user_current_email(), 'email_type': OrderEventsEmails.SHIPPING.value}, user=request.user, type=OrderEvents.EMAIL_SENT.value) msg = pgettext_lazy( 'Dashboard message', 'Fulfillment #%(fulfillment)s tracking number updated') % { 'fulfillment': fulfillment.composed_id} messages.success(request, msg) return redirect('dashboard:order-details', order_pk=order.pk) elif form.errors: status = 400 ctx = {'form': form, 'order': order, 'fulfillment': fulfillment} return TemplateResponse( request, 'dashboard/order/modal/fulfillment_tracking.html', ctx, status=status) @staff_member_required def ajax_order_shipping_methods_list(request, order_pk): order = get_object_or_404(Order, pk=order_pk) queryset = ShippingMethod.objects.prefetch_related( 'shipping_zone').order_by('name', 'price') if order.shipping_address: country_code = order.shipping_address.country.code queryset = queryset.filter( shipping_zone__countries__contains=country_code) search_query = request.GET.get('q', '') if search_query: queryset = queryset.filter( Q(name__icontains=search_query) | Q(price__icontains=search_query)) shipping_methods = [ {'id': method.pk, 'text': method.get_ajax_label()} for method in queryset] return JsonResponse({'results': shipping_methods})
bsd-3-clause
6,461,165,082,829,954,000
40.050685
79
0.652685
false
elewis33/doorstop
doorstop/server/utilities.py
1
1176
"""Shared functions for the `doorstop.server` package.""" from doorstop import common from doorstop import settings log = common.logger(__name__) class StripPathMiddleware(object): # pylint: disable=R0903 """WSGI middleware that strips trailing slashes from all URLs.""" def __init__(self, app): self.app = app def __call__(self, e, h): # pragma: no cover (integration test) e['PATH_INFO'] = e['PATH_INFO'].rstrip('/') return self.app(e, h) def build_url(host=None, port=None, path=None): """Build the server's URL with optional path.""" host = host or settings.SERVER_HOST port = port or settings.SERVER_PORT log.debug("building URL: {} + {} + {}".format(host, port, path)) if not host: return None url = 'http://{}'.format(host) if port != 80: url += ':{}'.format(port) if path: url += path return url def json_response(request): # pragma: no cover (integration test) """Determine if the request's response should be JSON.""" if request.query.get('format') == 'json': return True else: return request.content_type == 'application/json'
lgpl-3.0
-7,028,435,530,194,350,000
27.682927
69
0.617347
false
sophacles/invoke
tests/runner.py
1
8258
import sys import os from spec import eq_, skip, Spec, raises, ok_, trap from invoke.runner import Runner, run from invoke.exceptions import Failure from _utils import support, reset_cwd def _run(returns=None, **kwargs): """ Create a Runner w/ retval reflecting ``returns`` & call ``run(**kwargs)``. """ # Set up return value tuple for Runner.run returns = returns or {} returns.setdefault('exited', 0) value = map( lambda x: returns.get(x, None), ('stdout', 'stderr', 'exited', 'exception'), ) class MockRunner(Runner): def run(self, command, warn, hide): return value # Ensure top level run() uses that runner, provide dummy command. kwargs['runner'] = MockRunner return run("whatever", **kwargs) class Run(Spec): "run()" def setup(self): os.chdir(support) self.both = "echo foo && ./err bar" self.out = "echo foo" self.err = "./err bar" self.sub = "inv -c pty_output hide_%s" def teardown(self): reset_cwd() class return_value: def return_code_in_result(self): """ Result has .return_code (and .exited) containing exit code int """ r = run(self.out, hide='both') eq_(r.return_code, 0) eq_(r.exited, 0) def nonzero_return_code_for_failures(self): result = run("false", warn=True) eq_(result.exited, 1) result = run("goobypls", warn=True, hide='both') eq_(result.exited, 127) def stdout_attribute_contains_stdout(self): eq_(run(self.out, hide='both').stdout, 'foo\n') def stderr_attribute_contains_stderr(self): eq_(run(self.err, hide='both').stderr, 'bar\n') def ok_attr_indicates_success(self): eq_(_run().ok, True) eq_(_run(returns={'exited': 1}, warn=True).ok, False) def failed_attr_indicates_failure(self): eq_(_run().failed, False) eq_(_run(returns={'exited': 1}, warn=True).failed, True) def has_exception_attr(self): eq_(_run().exception, None) class failure_handling: @raises(Failure) def fast_failures(self): run("false") def run_acts_as_success_boolean(self): ok_(not run("false", warn=True)) ok_(run("true")) def non_one_return_codes_still_act_as_False(self): ok_(not run("goobypls", warn=True, hide='both')) def warn_kwarg_allows_continuing_past_failures(self): eq_(run("false", warn=True).exited, 1) def Failure_repr_includes_stderr(self): try: run("./err ohnoz && exit 1", hide='both') assert false # Ensure failure to Failure fails except Failure as f: r = repr(f) assert 'ohnoz' in r, "Sentinel 'ohnoz' not found in %r" % r class output_controls: @trap def _hide_both(self, val): run(self.both, hide=val) eq_(sys.stdall.getvalue(), "") def hide_both_hides_everything(self): self._hide_both('both') def hide_True_hides_everything(self): self._hide_both(True) @trap def hide_out_only_hides_stdout(self): run(self.both, hide='out') eq_(sys.stdout.getvalue().strip(), "") eq_(sys.stderr.getvalue().strip(), "bar") @trap def hide_err_only_hides_stderr(self): run(self.both, hide='err') eq_(sys.stdout.getvalue().strip(), "foo") eq_(sys.stderr.getvalue().strip(), "") @trap def hide_accepts_stderr_alias_for_err(self): run(self.both, hide='stderr') eq_(sys.stdout.getvalue().strip(), "foo") eq_(sys.stderr.getvalue().strip(), "") @trap def hide_accepts_stdout_alias_for_out(self): run(self.both, hide='stdout') eq_(sys.stdout.getvalue().strip(), "") eq_(sys.stderr.getvalue().strip(), "bar") def hide_both_hides_both_under_pty(self): r = run(self.sub % 'both', hide='both') eq_(r.stdout, "") eq_(r.stderr, "") def hide_out_hides_both_under_pty(self): r = run(self.sub % 'out', hide='both') eq_(r.stdout, "") eq_(r.stderr, "") def hide_err_has_no_effect_under_pty(self): r = run(self.sub % 'err', hide='both') eq_(r.stdout, "foo\r\nbar\r\n") eq_(r.stderr, "") @trap def _no_hiding(self, val): r = run(self.both, hide=val) eq_(sys.stdout.getvalue().strip(), "foo") eq_(sys.stderr.getvalue().strip(), "bar") def hide_None_hides_nothing(self): self._no_hiding(None) def hide_False_hides_nothing(self): self._no_hiding(False) @raises(ValueError) def hide_unknown_vals_raises_ValueError(self): run("command", hide="what") def hide_unknown_vals_mention_value_given_in_error(self): value = "penguinmints" try: run("command", hide=value) except ValueError as e: msg = "Error from run(hide=xxx) did not tell user what the bad value was!" msg += "\nException msg: %s" % e ok_(value in str(e), msg) else: assert False, "run() did not raise ValueError for bad hide= value" def hide_does_not_affect_capturing(self): eq_(run(self.out, hide='both').stdout, 'foo\n') class pseudo_terminals: def return_value_indicates_whether_pty_was_used(self): eq_(run("true").pty, False) eq_(run("true", pty=True).pty, True) def pty_defaults_to_off(self): eq_(run("true").pty, False) def complex_nesting_doesnt_break(self): # GH issue 191 substr = " hello\t\t\nworld with spaces" cmd = """ eval 'echo "{0}" ' """.format(substr) # TODO: consider just mocking os.execv here (and in the other # tests) though that feels like too much of a tautology / testing # pexpect expected = ' hello\t\t\r\nworld with spaces\r\n' eq_(run(cmd, pty=True, hide='both').stdout, expected) class command_echo: @trap def does_not_echo_commands_run_by_default(self): run("echo hi") eq_(sys.stdout.getvalue().strip(), "hi") @trap def when_echo_True_commands_echoed_in_bold(self): run("echo hi", echo=True) expected = "\033[1;37mecho hi\033[0m\nhi" eq_(sys.stdout.getvalue().strip(), expected) # # Random edge/corner case junk # def non_stupid_OSErrors_get_captured(self): # Somehow trigger an OSError saying "Input/output error" within # pexpect.spawn().interact() & assert it is in result.exception skip() def KeyboardInterrupt_on_stdin_doesnt_flake(self): # E.g. inv test => Ctrl-C halfway => shouldn't get buffer API errors skip() class funky_characters_in_stdout: def basic_nonstandard_characters(self): # Crummy "doesn't explode with decode errors" test run("cat tree.out", hide='both') def nonprinting_bytes(self): # Seriously non-printing characters (i.e. non UTF8) also don't asplode # load('funky').derp() run("echo '\xff'", hide='both') def nonprinting_bytes_pty(self): # PTY use adds another utf-8 decode spot which can also fail. run("echo '\xff'", pty=True, hide='both') class Local_(Spec): def setup(self): os.chdir(support) self.both = "echo foo && ./err bar" def teardown(self): reset_cwd() def stdout_contains_both_streams_under_pty(self): r = run(self.both, hide='both', pty=True) eq_(r.stdout, 'foo\r\nbar\r\n') def stderr_is_empty_under_pty(self): r = run(self.both, hide='both', pty=True) eq_(r.stderr, '')
bsd-2-clause
-1,536,870,026,090,481,400
31.640316
90
0.541899
false
SeanEstey/Bravo
app/notify/tasks.py
1
7873
'''app.notify.tasks''' import json, os, pytz from os import environ as env from datetime import datetime, date, time, timedelta from dateutil.parser import parse from bson import ObjectId as oid from flask import g, render_template from app import get_keys, celery #, smart_emit from app.lib.dt import to_local from app.lib import mailgun from app.main import schedule from app.main.parser import is_bus from app.main.etapestry import call, EtapError from . import email, events, sms, voice, pickups, triggers from logging import getLogger log = getLogger(__name__) #------------------------------------------------------------------------------- @celery.task(bind=True) def monitor_triggers(self, **kwargs): ready = g.db.triggers.find({ 'status':'pending', 'fire_dt':{ '$lt':datetime.utcnow()}}) for trigger in ready: evnt = g.db.events.find_one({'_id':trigger['evnt_id']}) g.group = evnt['agency'] log.debug('Firing event trigger for %s', evnt['name'], extra={'trigger_id':str(trigger['_id'])}) try: fire_trigger(trigger['_id']) except Exception as e: log.exception('Error firing event trigger for %s', evnt['name']) pending = g.db.triggers.find({ 'status':'pending', 'fire_dt': { '$gt':datetime.utcnow()}}).sort('fire_dt', 1) output = [] if pending.count() > 0: tgr = pending.next() delta = tgr['fire_dt'] - datetime.utcnow().replace(tzinfo=pytz.utc) to_str = str(delta)[:-7] return 'next trigger pending in %s' % to_str else: return '0 pending' #------------------------------------------------------------------------------- @celery.task(bind=True) def fire_trigger(self, _id=None, **rest): '''Sends out all dependent sms/voice/email notifics messages ''' status = '' n_errors = 0 trig = g.db.triggers.find_one({'_id':oid(_id)}) event = g.db.events.find_one({'_id':trig['evnt_id']}) g.group = event['agency'] g.db.triggers.update_one( {'_id':oid(_id)}, {'$set': {'task_id':self.request.id, 'status':'in-progress'}}) events.update_status(trig['evnt_id']) ready = g.db.notifics.find( {'trig_id':oid(_id), 'tracking.status':'pending'}) count = ready.count() log.info('Sending notifications for event %s...', event['name'], extra={'type':trig['type'], 'n_total':count}) #smart_emit('trigger_status',{ # 'trig_id': str(_id), 'status': 'in-progress'}) if env['BRV_SANDBOX'] == 'True': log.info('sandbox: simulating voice/sms, rerouting emails') for n in ready: try: if n['type'] == 'voice': status = voice.call(n, get_keys('twilio')) elif n['type'] == 'sms': status = sms.send(n, get_keys('twilio')) elif n['type'] == 'email': status = email.send(n, get_keys('mailgun')) except Exception as e: n_errors +=1 status = 'error' log.exception('Error sending notification to %s', n['to'], extra={'type':n['type']}) else: if status == 'failed': n_errors += 1 finally: pass #smart_emit('notific_status', { # 'notific_id':str(n['_id']), 'status':status}) g.db.triggers.update_one({'_id':oid(_id)}, {'$set': {'status': 'fired'}}) '''smart_emit('trigger_status', { 'trig_id': str(_id), 'status': 'fired', 'sent': count - n_errors, 'errors': n_errors})''' log.info('%s/%s notifications sent for event %s', count - n_errors, count, event['name'], extra={'type':trig['type'], 'n_total':count, 'n_errors':n_errors}) return 'success' #------------------------------------------------------------------------------- @celery.task(bind=True) def schedule_reminders(self, group=None, for_date=None, **rest): if for_date: for_date = parse(for_date).date() groups = [g.db['groups'].find_one({'name':group})] if group else g.db['groups'].find() evnt_ids = [] for group_ in groups: n_success = n_fails = 0 g.group = group_['name'] log.info('Scheduling notification events...') days_ahead = int(group_['notify']['sched_delta_days']) on_date = date.today() + timedelta(days=days_ahead) if not for_date else for_date date_str = on_date.strftime('%m-%d-%Y') blocks = [] for key in group_['cal_ids']: blocks += schedule.get_blocks( group_['cal_ids'][key], datetime.combine(on_date,time(8,0)), datetime.combine(on_date,time(9,0)), get_keys('google')['oauth']) if len(blocks) == 0: log.debug('no blocks on %s', date_str) continue else: log.debug('%s events on %s: %s', len(blocks), date_str, ", ".join(blocks)) for block in blocks: if is_bus(block) and group_['notify']['sched_business'] == False: continue try: evnt_id = pickups.create_reminder(g.group, block, on_date) except EtapError as e: n_fails +=1 log.exception('Error creating notification event %s', block) continue else: n_success +=1 evnt_ids.append(str(evnt_id)) log.info('Created notification event %s', block) log.info('Created %s/%s scheduled notification events', n_success, n_success + n_fails) return json.dumps(evnt_ids) #------------------------------------------------------------------------------- @celery.task(bind=True) def skip_pickup(self, evnt_id=None, acct_id=None, **rest): '''User has opted out of a pickup via sms/voice/email noification. Run is_valid() before calling this function. @acct_id: _id from db.accounts, not eTap account id ''' # Cancel any pending parent notifications result = g.db.notifics.update_many( {'acct_id':oid(acct_id), 'evnt_id':oid(evnt_id), 'tracking.status':'pending'}, {'$set':{'tracking.status':'cancelled'}}) acct = g.db.accounts.find_one_and_update( {'_id':oid(acct_id)}, {'$set': {'opted_out': True}}) evnt = g.db.events.find_one({'_id':oid(evnt_id)}) if not evnt or not acct: msg = 'evnt/acct not found (evnt_id=%s, acct_id=%s' %(evnt_id,acct_id) log.error(msg) raise Exception(msg) g.group = evnt['agency'] log.info('%s opted out of pickup', acct.get('name') or acct.get('email'), extra={'event_name':evnt['name'], 'account_id':acct['udf']['etap_id']}) try: call('skip_pickup', data={ 'acct_id': acct['udf']['etap_id'], 'date': acct['udf']['pickup_dt'].strftime('%d/%m/%Y'), 'next_pickup': to_local( acct['udf']['future_pickup_dt'], to_str='%d/%m/%Y')}) except Exception as e: log.exception('Error calling skip_pickup') log.exception("Error updating account %s", acct.get('name') or acct.get('email'), extra={'account_id': acct['udf']['etap_id']}) if not acct.get('email'): return 'success' try: body = render_template( 'email/%s/no_pickup.html' % g.group, to=acct['email'], account=to_local(obj=acct, to_str='%B %d %Y')) except Exception as e: log.exception('Error rendering no_pickup template') raise else: mailgun.send( acct['email'], 'Thanks for Opting Out', body, get_keys('mailgun'), v={'type':'opt_out', 'group':g.group}) return 'success'
gpl-2.0
-6,240,724,140,937,675,000
32.7897
104
0.526737
false
jaredjennings/shaney
shaney/generators/test/test_autoindex.py
1
6482
# shaney - prepare Puppet code with LaTeX comments for multiple audiences. # Based on <https://github.com/afseo/cmits>. # Copyright (C) 2015 Jared Jennings, jjennings@fastmail.fm. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import unittest from shaney.generators.test import CoroutineTest from shaney.generators.autoindex import autoindex import new class TestUnrecognizedPassesThrough(CoroutineTest): """Whatever the autoindexer does not care about, it sends through:""" coroutine_under_test = autoindex send = [ ('comment', 'bla'), ('verbatim', ''), ('fnord', 'bletch'), ] expect = [ ('comment', 'bla'), ('verbatim', ''), ('fnord', 'bletch'), ] class AutoIndexTest(CoroutineTest): # You can't say """ inside of a triple-quoted string, you have to # say ""\". So when you see ""\" in the example it means you should # write """. Also you have to escape backslashes in a non-raw # string, so \\ below means \. """Test most properties of autoindex. The autoindexer expects to be sent tuples denoting what's going on in an input file, like this set:: ('comment', 'some documentation about this Puppet class') ('verbatim', 'class puppetclass {') ('verbatim', ' mumble') ('verbatim', '}') It won't act on anything that isn't toplevel, so for most of our tests, we'll want to send in a bunch of ('verbatim', 'foo'). This class factors that out, so you can make the send value less verbose. Also, unless you write the docstring strangely, there will always be a blank line at the end of send; this class will automatically add ('verbatim', '') to the expect so you don't have to write it. Example:: class TestThingAutoindexDoes(AutoindexTest): ""\"When it sees an include, it emits ('include', 'thing'):""\" send = ""\"\\ include two ""\" expect = [ ('include', 'two'), ('verbatim', 'line two'), ] """ send = '' expect = [] coroutine_under_test = autoindex def preprocess_send(self): for x in self.send.split("\n"): yield ('verbatim', x) def preprocess_expect(self): for x in self.expect: yield x yield ('verbatim', '') class TestClassDefinition(AutoIndexTest): """Classes defined are indexed:""" send = """\ class foo::bar { some stuff } """ expect = [ ('index_entry', 'class', 'foo::bar', 'defined'), ('label', 'class_foo::bar'), ('verbatim', 'class foo::bar {'), ('verbatim', ' some stuff'), ('verbatim', '}'), ] class TestParameterizedClassDefinition(AutoIndexTest): """When classes are defined with parameters, only the name is indexed:""" send = """\ class foo::bar($param1, $param2) { some stuff } """ expect = [ ('index_entry', 'class', 'foo::bar', 'defined'), ('label', 'class_foo::bar'), ('verbatim', 'class foo::bar($param1, $param2) {'), ('verbatim', ' some stuff'), ('verbatim', '}'), ] class TestClassUseByInclude(AutoIndexTest): """Classes used by means of `include` are indexed:""" send = """\ include foo_bar::baz """ expect = [ ('index_entry', 'class', 'foo_bar::baz'), ('verbatim', ' include foo_bar::baz'), ('margin_ref', 'class_foo_bar::baz'), ] class TestClassUseByClassBracket(AutoIndexTest): """Classes used by means of ``class {...}``` are not yet supported:""" send = """\ class { 'foo::bar': bla } """ expect = [ ('index_entry', 'class', 'foo::bar'), ('verbatim', "class { 'foo::bar':"), ('margin_ref', 'class_foo::bar'), ('verbatim', ' bla'), ('verbatim', '}'), ] class TestDefinedResourceTypeDefinition(AutoIndexTest): """Defined resource types are indexed:""" send = """\ define foo_bar::baz($paramOne, $paramTwo) { } """ expect = [ ('index_entry', 'define', 'foo_bar::baz', 'defined'), ('label', 'define_foo_bar::baz'), ('verbatim', 'define foo_bar::baz($paramOne,'), ('verbatim', ' $paramTwo) {'), ('verbatim', '}'), ] class TestDefinedResourceTypeUse(AutoIndexTest): """Uses of defined resource types are indexed and noted:""" send = """\ class foo { bar_baz::bletch { "gack": } } """ expect = [ ('index_entry', 'class', 'foo', 'defined'), ('label', 'class_foo'), ('verbatim', 'class foo {'), ('index_entry', 'define', 'bar_baz::bletch'), ('verbatim', ' bar_baz::bletch { "gack": }'), ('margin_ref', 'define_bar_baz::bletch'), ('verbatim', '}'), ] class TestFileUseSameLine(AutoIndexTest): """Mentions of files are indexed:""" send = """\ file { "/foo/bar/baz": ... } """ expect = [ ('index_entry', 'file', '/foo/bar/baz'), ('verbatim', 'file { "/foo/bar/baz":'), ('verbatim', ' ...'), ('verbatim', '}'), ] class TestFileUseDifferentLine(AutoIndexTest): """Some file syntaxes are not yet supported:""" send = """\ file { "/foo/bar/baz": ...; "/bletch/quux/gark": ...; } """ expect = [ ('verbatim', 'file {'), ('verbatim', ' "/foo/bar/baz":'), ('verbatim', ' ...;'), ('verbatim', ' "/bletch/quux/gark":'), ('verbatim', ' ...;'), ('verbatim', '}'), ] if __name__ == '__main__': unittest.main()
gpl-3.0
7,108,182,453,560,785,000
29.28972
77
0.543042
false
ttreeagency/PootleTypo3Org
pootle/apps/staticpages/forms.py
1
3328
#!/usr/bin/env python # -*- coding: utf-8 -*- # # Copyright 2013 Zuza Software Foundation # # This file is part of Pootle. # # Pootle 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. # # translate 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 translate; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA from django import forms from django.core.urlresolvers import reverse from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ from .models import Agreement def agreement_form_factory(pages, user, base_class=forms.Form): """Factory that builds an agreement form. :param pages: Legal pages that need to be accepted by users. :param user: User bound to the agreement form. :param base_class: Base class for this form to inherit from. :return: An `AgreementForm` class with `pages` as required checkboxes. """ class AgreementForm(base_class): def __init__(self, *args, **kwargs): super(AgreementForm, self).__init__(*args, **kwargs) self._pages = pages self._user = user for page in self._pages: self.add_page_field(page) def save(self): """Saves user agreements.""" if hasattr(super(AgreementForm, self), 'save'): # HACKISH: This is tightly coupled with `RegistrationForm` # which returns the newly-registered user in its form's # `save`. We should listen to the `user_registered` signal # instead. self._user = super(AgreementForm, self).save() for page in self._pages: agreement, created = Agreement.objects.get_or_create( user=self._user, document=page, ) agreement.save() def legal_fields(self): """Returns any fields added by legal pages.""" return [field for field in self if field.name.startswith('legal_')] def add_page_field(self, page): """Adds `page` as a required field to this form.""" url = page.url and page.url or reverse('staticpages.display', args=[page.virtual_path]) anchor = u'href="%s" class="fancybox"' % url # Translators: The second '%s' is the title of a document label = mark_safe(_("I have read and accept: <a %s>%s</a>", (anchor, page.title,))) field_name = 'legal_%d' % page.pk self.fields[field_name] = forms.BooleanField(label=label, required=True) self.fields[field_name].widget.attrs['class'] = 'js-legalfield' return AgreementForm
gpl-2.0
7,605,954,726,321,325,000
39.096386
76
0.608173
false
scitokens/scitokens
tests/test_scitokens.py
1
12413
""" Test cases for the Validator and Enforcer classes from the scitokens module. """ import os import sys import time import unittest import cryptography.hazmat.backends import cryptography.hazmat.primitives.asymmetric.rsa # Allow unittests to be run from within the project base. if os.path.exists("src"): sys.path.append("src") if os.path.exists("../src"): sys.path.append("../src") import scitokens class TestValidation(unittest.TestCase): """ Tests related to the Validator object. """ def test_valid(self): """ Basic unit test coverage of the Validator object. """ def always_accept(value): """ A validator that accepts any value. """ if value or not value: return True validator = scitokens.Validator() validator.add_validator("foo", always_accept) token = scitokens.SciToken() token["foo"] = "bar" self.assertTrue(validator.validate(token)) self.assertTrue(validator(token)) class TestEnforcer(unittest.TestCase): """ Unit tests for the SciToken's Enforcer object. """ _test_issuer = "https://scitokens.org/unittest" @staticmethod def always_accept(value): if value or not value: return True def setUp(self): """ Setup a sample token for testing the enforcer. """ now = time.time() private_key = cryptography.hazmat.primitives.asymmetric.rsa.generate_private_key( public_exponent=65537, key_size=2048, backend=cryptography.hazmat.backends.default_backend() ) self._token = scitokens.SciToken(key=private_key) self._token["foo"] = "bar" self._token["iat"] = int(now) self._token["exp"] = int(now + 600) self._token["iss"] = self._test_issuer self._token["nbf"] = int(now) # Scitoken v2 self._token2 = scitokens.SciToken(key=private_key) self._token2["ver"] = "scitoken:2.0" self._token2["foo"] = "bar" self._token2["iat"] = int(now) self._token2["exp"] = int(now + 600) self._token2["iss"] = self._test_issuer self._token2["nbf"] = int(now) self._token2['wlcg.groups'] = ['groupA', 'groupB'] self._token2["aud"] = "ANY" def test_enforce_v2(self): """ Test the Enforcer object for profile scitokens:2.0 Also, there is a non-validated attribute, foo. In 1.0, non-validated attributes cause a validation error. In 2.0, they are ignored. """ with self.assertRaises(scitokens.scitokens.EnforcementError): print(scitokens.Enforcer(None)) # No audience specified enf = scitokens.Enforcer(self._test_issuer) self.assertFalse(enf.test(self._token2, "read", "/"), msg=enf.last_failure) self._token2["scp"] = "read:/" self.assertFalse(enf.test(self._token2, "read", "/"), msg=enf.last_failure) # Token is set to to ANY, so any audience will work enf = scitokens.Enforcer(self._test_issuer, audience = "https://example.unl.edu") self._token2["scp"] = "read:/" self.assertTrue(enf.test(self._token2, "read", "/"), msg=enf.last_failure) # Change the audience from ANY to https://example.com self._token2["aud"] = "https://example.com" self.assertFalse(enf.test(self._token2, "read", "/"), msg=enf.last_failure) # Change back to ANY self._token2["aud"] = "ANY" self.assertTrue(enf.test(self._token2, "read", "/"), msg=enf.last_failure) self._token2["scp"] = "read:/foo/bar" self.assertFalse(enf.test(self._token2, "read", "/foo"), msg=enf.last_failure) self.assertFalse(enf.test(self._token2, "write", "/foo/bar"), msg=enf.last_failure) with self.assertRaises(scitokens.scitokens.InvalidPathError): print(enf.test(self._token2, "write", "~/foo")) def test_v2(self): """ Test the requirements for a v2 """ # First, delete the aud del self._token2["aud"] enf = scitokens.Enforcer(self._test_issuer, audience="https://example.unl.edu") self._token2["scope"] = "read:/foo/bar" # Should fail, audience is required for 2.0 token self.assertFalse(enf.test(self._token2, "read", "/foo/bar"), msg=enf.last_failure) # Now set the audience to ANY self._token2["aud"] = "ANY" self.assertTrue(enf.test(self._token2, "read", "/foo/bar"), msg=enf.last_failure) # Now to the correct audience self._token2["aud"] = "https://example.unl.edu" self.assertTrue(enf.test(self._token2, "read", "/foo/bar"), msg=enf.last_failure) # Now to the wrong audience self._token2["aud"] = "https://example.com" self.assertFalse(enf.test(self._token2, "read", "/foo/bar"), msg=enf.last_failure) # Arbitrary claims are allowed now in v2 self._token2["madeupclaim"] = "claimsdontmatter" self._token2["aud"] = "ANY" self.assertTrue(enf.test(self._token2, "read", "/foo/bar"), msg=enf.last_failure) # Arbitrary claims should fail in 1.0 self._token["madeupclaim"] = "claimsdontmatter" self._token["aud"] = "ANY" self.assertFalse(enf.test(self._token, "read", "/foo/bar"), msg=enf.last_failure) def test_enforce(self): """ Test the Enforcer object. """ with self.assertRaises(scitokens.scitokens.EnforcementError): print(scitokens.Enforcer(None)) enf = scitokens.Enforcer(self._test_issuer) enf.add_validator("foo", self.always_accept) self.assertFalse(enf.test(self._token, "read", "/"), msg=enf.last_failure) self._token["scp"] = "read:/" self.assertTrue(enf.test(self._token, "read", "/"), msg=enf.last_failure) enf = scitokens.Enforcer(self._test_issuer, audience = "https://example.unl.edu") enf.add_validator("foo", self.always_accept) self.assertTrue(enf.test(self._token, "read", "/"), msg=enf.last_failure) self._token["scp"] = "read:/foo/bar" self.assertFalse(enf.test(self._token, "read", "/foo"), msg=enf.last_failure) self.assertTrue(enf.test(self._token, "read", "/foo/bar"), msg=enf.last_failure) enf = scitokens.Enforcer(self._test_issuer) enf.add_validator("foo", self.always_accept) self.assertTrue(enf.test(self._token, "read", "/foo/bar"), msg=enf.last_failure) self.assertFalse(enf.test(self._token, "write", "/foo/bar"), msg=enf.last_failure) with self.assertRaises(scitokens.scitokens.InvalidPathError): print(enf.test(self._token, "write", "~/foo")) def test_enforce_scope(self): """ Test the Enforcer object. """ with self.assertRaises(scitokens.scitokens.EnforcementError): print(scitokens.Enforcer(None)) enf = scitokens.Enforcer(self._test_issuer) enf.add_validator("foo", self.always_accept) self.assertFalse(enf.test(self._token, "read", "/"), msg=enf.last_failure) self._token["scope"] = "read:/" self.assertTrue(enf.test(self._token, "read", "/"), msg=enf.last_failure) enf = scitokens.Enforcer(self._test_issuer, audience = "https://example.unl.edu") enf.add_validator("foo", self.always_accept) self.assertTrue(enf.test(self._token, "read", "/"), msg=enf.last_failure) self._token["scope"] = "read:/foo/bar" self.assertFalse(enf.test(self._token, "read", "/foo"), msg=enf.last_failure) self.assertTrue(enf.test(self._token, "read", "/foo/bar"), msg=enf.last_failure) enf = scitokens.Enforcer(self._test_issuer) enf.add_validator("foo", self.always_accept) self.assertTrue(enf.test(self._token, "read", "/foo/bar"), msg=enf.last_failure) self.assertFalse(enf.test(self._token, "write", "/foo/bar"), msg=enf.last_failure) with self.assertRaises(scitokens.scitokens.InvalidPathError): print(enf.test(self._token, "write", "~/foo")) def test_aud(self): """ Test the audience claim """ self._token['scp'] = 'read:/' enf = scitokens.Enforcer(self._test_issuer) enf.add_validator("foo", lambda path : True) self._token['aud'] = "https://example.unl.edu" self.assertFalse(enf.test(self._token, "read", "/"), msg=enf.last_failure) enf = scitokens.Enforcer(self._test_issuer, audience = "https://example.unl.edu") enf.add_validator("foo", lambda path : True) self.assertTrue(enf.test(self._token, "read", "/"), msg=enf.last_failure) def test_multiple_aud(self): """ Test multiple aud """ self._token['scp'] = 'read:/' # Test multiple audiences enf = scitokens.Enforcer(self._test_issuer, audience = ["https://example.unl.edu", "https://another.com"]) enf.add_validator("foo", self.always_accept) self.assertTrue(enf.test(self._token, "read", "/"), msg=enf.last_failure) self._token['aud'] = "https://another.com" self.assertTrue(enf.test(self._token, "read", "/"), msg=enf.last_failure) self._token['aud'] = "https://doesnotwork.com" self.assertFalse(enf.test(self._token, "read", "/"), msg=enf.last_failure) def test_getitem(self): """ Test the getters for the SciTokens object. """ self.assertEqual(self._token['foo'], 'bar') with self.assertRaises(KeyError): print(self._token['bar']) self.assertEqual(self._token.get('baz'), None) self.assertEqual(self._token.get('foo', 'baz'), 'bar') self.assertEqual(self._token.get('foo', 'baz', verified_only=True), 'baz') self._token.serialize() self.assertEqual(self._token['foo'], 'bar') self.assertEqual(self._token.get('foo', 'baz'), 'bar') self.assertEqual(self._token.get('bar', 'baz'), 'baz') self.assertEqual(self._token.get('bar', 'baz', verified_only=True), 'baz') self._token['bar'] = '1' self.assertEqual(self._token.get('bar', 'baz', verified_only=False), '1') self.assertEqual(self._token.get('bar', 'baz', verified_only=True), 'baz') def test_gen_acls(self): """ Test the generation of ACLs """ enf = scitokens.Enforcer(self._test_issuer) enf.add_validator("foo", self.always_accept) self._token['scope'] = 'read:/' acls = enf.generate_acls(self._token) self.assertTrue(len(acls), 1) self.assertEqual(acls[0], ('read', '/')) self._token['scope'] = 'read:/ write:/foo' acls = enf.generate_acls(self._token) self.assertTrue(len(acls), 2) self.assertTrue(('read', '/') in acls) self.assertTrue(('write', '/foo') in acls) self._token['scope'] = 'read:/foo read://bar write:/foo write://bar' acls = enf.generate_acls(self._token) self.assertTrue(len(acls), 4) self.assertTrue(('read', '/foo') in acls) self.assertTrue(('write', '/foo') in acls) self.assertTrue(('read', '/bar') in acls) self.assertTrue(('write', '/bar') in acls) self._token['exp'] = time.time() - 600 with self.assertRaises(scitokens.scitokens.ClaimInvalid): print(enf.generate_acls(self._token)) self.assertTrue(enf.last_failure) self._token['exp'] = time.time() + 600 self._token['scope'] = 'read:foo' with self.assertRaises(scitokens.scitokens.InvalidAuthorizationResource): print(enf.generate_acls(self._token)) self._token['scope'] = 'read' with self.assertRaises(scitokens.scitokens.InvalidAuthorizationResource): print(enf.generate_acls(self._token)) def test_sub(self): """ Verify that tokens with the `sub` set are accepted. """ self._token['sub'] = 'Some Great User' enf = scitokens.Enforcer(self._test_issuer) enf.add_validator("foo", self.always_accept) self._token['scope'] = 'read:/' acls = enf.generate_acls(self._token) self.assertTrue(len(acls), 1) if __name__ == '__main__': unittest.main()
apache-2.0
-6,480,488,713,578,052,000
36.844512
114
0.596633
false
couchbase/couchbase-python-client
couchbase_core/supportability.py
1
3356
import warnings from functools import wraps from couchbase_core import operation_mode def deprecate_module_attribute(mod, deprecated=tuple()): return warn_on_attribute_access(mod, deprecated, "deprecated") class Level(object): desc = None # type: str msg_params = "msg_params" def __new__(cls, function, *args, **kwargs): """ Mark a function as {} :param function: input function :return: marked function """.format(cls.__name__) message = cls.desc+"\n" msg_params = kwargs.get(cls.msg_params) if msg_params: message = message.format(**msg_params) func_name = getattr(function, '__qualname__', function.__name__) result = cls.get_final_fn(function, message, func_name) operation_mode.operate_on_doc(result, lambda x: (function.__doc__+"\n\n" if function.__doc__ else "") + \ " :warning: " + message % "This") return result @classmethod def get_final_fn(cls, function, message, func_name): @wraps(function) def fn_wrapper(*args, **kwargs): warnings.warn(message % "'{}'".format(func_name)) return function(*args, **kwargs) return fn_wrapper class Deprecated(Level): desc = \ """ %s is a deprecated API, use {instead} instead. """ def __new__(cls, instead): def decorator(function): warn_on_attribute_access(cls, [function], "deprecated") kwargs = {cls.msg_params: {"instead": instead}} return Level.__new__(cls, function, **kwargs) return decorator deprecated = Deprecated class Uncommitted(Level): desc = \ """ %s is an uncommitted API call that is unlikely to change, but may still change as final consensus on its behavior has not yet been reached. """ uncommitted = Uncommitted class Volatile(Level): desc = \ """ %s is a volatile API call that is still in flux and may likely be changed. It may also be an inherently private API call that may be exposed, but "YMMV" (your mileage may vary) principles apply. """ volatile = Volatile class Internal(Level): desc = \ """ %s is an internal API call. Components external to Couchbase Python Client should not rely on it is not intended for use outside the module, even to other Couchbase components. """ @classmethod def get_final_fn(cls, function, *args): return function internal = Internal class Committed(Level): desc = \ """ %s is guaranteed to be supported and remain stable between SDK versions. """ committed = Committed def warn_on_attribute_access(obj, applicable, status): """Return a wrapped object that warns about deprecated accesses""" applicable = set(applicable) class Wrapper(object): def __getattr__(self, attr): if attr in applicable: warnings.warn("Property %s is %s" % (attr, status)) return getattr(obj, attr) def __setattr__(self, attr, value): if attr in applicable: warnings.warn("Property %s is %s" % (attr, status)) return setattr(obj, attr, value) return Wrapper()
apache-2.0
6,842,166,957,740,762,000
25.015504
152
0.595054
false
edx/xblock-lti-consumer
lti_consumer/migrations/0002_ltiagslineitem.py
1
1256
# Generated by Django 2.2.16 on 2020-09-29 21:48 from django.db import migrations, models import django.db.models.deletion import opaque_keys.edx.django.models class Migration(migrations.Migration): dependencies = [ ('lti_consumer', '0001_initial'), ] operations = [ migrations.CreateModel( name='LtiAgsLineItem', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('resource_id', models.CharField(blank=True, max_length=100)), ('resource_link_id', opaque_keys.edx.django.models.UsageKeyField(blank=True, db_index=True, max_length=255, null=True)), ('label', models.CharField(max_length=100)), ('score_maximum', models.IntegerField()), ('tag', models.CharField(blank=True, max_length=50)), ('start_date_time', models.DateTimeField(blank=True, null=True)), ('end_date_time', models.DateTimeField(blank=True, null=True)), ('lti_configuration', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='lti_consumer.LtiConfiguration')), ], ), ]
agpl-3.0
6,771,993,912,979,698,000
42.310345
161
0.61465
false
JackCloudman/Youtube-music
download.py
1
1342
#Program to download Yotube music #Author: Jack Cloudman import pafy,os,shutil from pydub import AudioSegment as convert #Create song list if os.path.exists('songs.txt'): pass else: print("Creating songs.txt....") document= open('songs.txt','w') print("Paste yours songs in songs.txt") document.close() #create directory if os.path.exists('music'): if os.path.exists('music/temp'): pass else: os.mkdir('music/temp') else: os.mkdir('music') os.mkdir('music/temp') document = open('songs.txt','r') music_list = document.readlines() document.close() error_list=[] print("Download music....") for music in music_list: try: url = music video = pafy.new(url) bestaudio = video.getbestaudio() bestaudio.download(filepath="music/temp/") except: error_list.append("Error download: "+music) print("Converting to mp3.....") for filename in os.listdir('music/temp/'): try: audio = convert.from_file('music/temp/'+filename) name = os.path.splitext(filename) audio.export('music/'+name[0]+'.mp3',format="mp3",bitrate="160k") except: error_list.append("Error convert: "+name[0]) shutil.rmtree("music/temp") for error in error_list: print(error) print("Finished!")
gpl-3.0
-262,824,265,426,975,650
26.553191
73
0.622206
false
stscieisenhamer/ginga
ginga/qtw/QtHelp.py
1
9071
# # QtHelp.py -- customized Qt widgets and convenience functions # # This is open-source software licensed under a BSD license. # Please see the file LICENSE.txt for details. # import glob import os import math import ginga.toolkit from ginga.util import iohelper configured = False toolkit = ginga.toolkit.toolkit # if user wants to force a toolkit if toolkit == 'qt5': os.environ['QT_API'] = 'pyqt5' elif toolkit == 'qt4': os.environ['QT_API'] = 'pyqt' elif toolkit == 'pyside': os.environ['QT_API'] = 'pyside' have_pyqt4 = False have_pyqt5 = False have_pyside = False try: from qtpy import QtCore from qtpy import QtWidgets as QtGui from qtpy.QtGui import QImage, QColor, QFont, QPixmap, QIcon, \ QCursor, QPainter, QPen, QPolygonF, QPolygon, QTextCursor, \ QDrag, QPainterPath, QBrush from qtpy.QtCore import QItemSelectionModel from qtpy.QtWidgets import QApplication try: from qtpy.QtWebEngineWidgets import QWebEngineView as QWebView except ImportError as e: pass # Let's see what qtpy configured for us... from qtpy import PYQT4, PYQT5, PYSIDE have_pyqt4 = PYQT4 have_pyqt5 = PYQT5 have_pyside = PYSIDE configured = True except ImportError as e: pass if have_pyqt5: ginga.toolkit.use('qt5') os.environ['QT_API'] = 'pyqt5' elif have_pyqt4: ginga.toolkit.use('qt4') os.environ['QT_API'] = 'pyqt' elif have_pyside: ginga.toolkit.use('pyside') os.environ['QT_API'] = 'pyside' else: raise ImportError("Failed to configure qt4, qt5 or pyside. Is the 'qtpy' package installed?") tabwidget_style = """ QTabWidget::pane { margin: 0px,0px,0px,0px; padding: 0px; } QMdiSubWindow { margin: 0px; padding: 2px; } """ class TopLevel(QtGui.QWidget): app = None ## def __init__(self, *args, **kwdargs): ## return super(TopLevel, self).__init__(self, *args, **kwdargs) def closeEvent(self, event): if not (self.app is None): self.app.quit() def setApp(self, app): self.app = app class ComboBox(QtGui.QComboBox): def insert_alpha(self, text): index = 0 while True: itemText = self.itemText(index) if len(itemText) == 0: break if itemText > text: self.insertItem(index, text) return index += 1 self.addItem(text) def delete_alpha(self, text): index = self.findText(text) self.removeItem(index) def show_text(self, text): index = self.findText(text) self.setCurrentIndex(index) def append_text(self, text): self.addItem(text) class VBox(QtGui.QWidget): def __init__(self, *args, **kwdargs): super(VBox, self).__init__(*args, **kwdargs) layout = QtGui.QVBoxLayout() # because of ridiculous defaults layout.setContentsMargins(0, 0, 0, 0) self.setLayout(layout) def addWidget(self, w, **kwdargs): self.layout().addWidget(w, **kwdargs) def setSpacing(self, val): self.layout().setSpacing(val) class HBox(QtGui.QWidget): def __init__(self, *args, **kwdargs): super(HBox, self).__init__(*args, **kwdargs) layout = QtGui.QHBoxLayout() # because of ridiculous defaults layout.setContentsMargins(0, 0, 0, 0) self.setLayout(layout) def addWidget(self, w, **kwdargs): self.layout().addWidget(w, **kwdargs) def setSpacing(self, val): self.layout().setSpacing(val) class FileSelection(object): """Handle Load Image file dialog from File menu.""" def __init__(self, parent_w): self.parent = parent_w self.cb = None def popup(self, title, callfn, initialdir=None, filename=None): """Let user select and load file(s). This allows wildcards and extensions, like in FBrowser. Parameters ---------- title : str Title for the file dialog. callfn : func Function used to open the file(s). initialdir : str or `None` Directory for file dialog. filename : str Filter for file dialog. """ self.cb = callfn filenames = QtGui.QFileDialog.getOpenFileNames( self.parent, title, initialdir, filename) # Special handling for PyQt5, see # https://www.reddit.com/r/learnpython/comments/2xhagb/pyqt5_trouble_with_openinggetting_the_name_of_the/ if ginga.toolkit.get_toolkit() == 'qt5': filenames = filenames[0] for filename in filenames: # Special handling for wildcard or extension. # This is similar to open_files() in FBrowser plugin. if '*' in filename or '[' in filename: info = iohelper.get_fileinfo(filename) ext = iohelper.get_hdu_suffix(info.numhdu) files = glob.glob(info.filepath) # Expand wildcard paths = ['{0}{1}'.format(f, ext) for f in files] # NOTE: Using drag-drop callback here might give QPainter # warnings. for path in paths: self.cb(path) # Normal load else: self.cb(filename) class DirectorySelection(object): """Handle directory selection dialog.""" def __init__(self, parent_w): self.parent = parent_w self.cb = None def popup(self, title, callfn, initialdir=None): """Let user select a directory. Parameters ---------- title : str Title for the dialog. callfn : func Function used to handle selected directory. initialdir : str or `None` Directory for dialog. """ self.cb = callfn dirname = QtGui.QFileDialog.getExistingDirectory( self.parent, title, initialdir) if dirname: self.cb(dirname) class Timer(object): """Abstraction of a GUI-toolkit implemented timer.""" def __init__(self, ival_sec, expire_cb, data=None): """Create a timer set to expire after `ival_sec` and which will call the callable `expire_cb` when it expires. """ self.ival_sec = ival_sec self.data = data self.timer = QtCore.QTimer() self.timer.setSingleShot(True) self.timer.timeout.connect(lambda: expire_cb(self)) def start(self, ival_sec=None): """Start the timer. If `ival_sec` is not None, it should specify the time to expiration in seconds. """ if ival_sec is None: ival_sec = self.ival_sec # QTimer set in milliseconds ms = int(ival_sec * 1000.0) self.timer.start(ms) def set(self, time_sec): self.start(ival_sec=time_sec) def cancel(self): """Cancel this timer. If the timer is not running, there is no error. """ try: self.timer.stop() except: pass clear = cancel def cmap2pixmap(cmap, steps=50): """Convert a Ginga colormap into a QPixmap """ inds = numpy.linspace(0, 1, steps) n = len(cmap.clst) - 1 tups = [ cmap.clst[int(x*n)] for x in inds ] rgbas = [QColor(int(r * 255), int(g * 255), int(b * 255), 255).rgba() for r, g, b in tups] im = QImage(steps, 1, QImage.Format_Indexed8) im.setColorTable(rgbas) for i in range(steps): im.setPixel(i, 0, i) im = im.scaled(128, 32) pm = QPixmap.fromImage(im) return pm def get_scroll_info(event): """ Returns the (degrees, direction) of a scroll motion Qt event. """ # 15 deg is standard 1-click turn for a wheel mouse # delta() usually returns 120 if have_pyqt5: # TODO: use pixelDelta() for better handling on hi-res devices point = event.angleDelta() dx, dy = point.x(), point.y() delta = math.sqrt(dx ** 2 + dy ** 2) if dy < 0: delta = -delta ang_rad = math.atan2(dy, dx) direction = math.degrees(ang_rad) - 90.0 direction = math.fmod(direction + 360.0, 360.0) else: delta = event.delta() orientation = event.orientation() direction = None if orientation == QtCore.Qt.Horizontal: if delta > 0: direction = 270.0 elif delta < 0: direction = 90.0 else: if delta > 0: direction = 0.0 elif delta < 0: direction = 180.0 num_degrees = abs(delta) / 8.0 return (num_degrees, direction) def get_icon(iconpath, size=None): image = QImage(iconpath) if size is not None: qsize = QtCore.QSize(*size) image = image.scaled(qsize) pixmap = QPixmap.fromImage(image) iconw = QIcon(pixmap) return iconw def get_font(font_family, point_size): font = QFont(font_family, point_size) return font #END
bsd-3-clause
8,053,044,321,151,345,000
25.837278
113
0.584169
false
pdxacm/acmapi
tests/test_resources/test_memberships_resource.py
1
10662
""" """ import unittest import json import datetime from freezegun import freeze_time from flask import Flask from flask.ext.restful import fields, marshal from flask.ext.restful.fields import MarshallingException from acmapi.fields import Date from acmapi.fields import root_fields from acmapi.fields import event_fields from acmapi.fields import post_fields from acmapi.fields import person_fields from acmapi.fields import membership_fields from acmapi.fields import officership_fields import acmapi from acmapi import models from acmapi import resources from acmapi import DB from acmapi.resources import API from acmapi.models import Person from acmapi.models import Officership import base64 HEADERS={ 'Authorization': 'Basic ' + base64.b64encode("root:1234") } class test_memberships_resource(unittest.TestCase): @freeze_time("2012-01-14 12:00:01") def setUp(self): self.app = acmapi.create_app(SQLALCHEMY_DATABASE_URI='sqlite://') self.app.testing = True with self.app.test_request_context(): DB.create_all() person = Person.create( name = None, username = 'root', email = None, website = None, password = '1234', ) DB.session.add(person) DB.session.commit() officership = Officership.create( person = person, title = 'Vice Chair', start_date = datetime.date.today(), end_date = None, ) DB.session.add(person) DB.session.add(officership) DB.session.commit() @freeze_time("2012-01-14 12:00:01") def test_add_valid_membership(self): with self.app.test_client() as client: response = client.post( 'http://localhost:5000/people/', headers = HEADERS, data = { 'username': 'bob', 'name': 'Bob Billy', 'email': 'bbob@example.com', 'website': 'http://bbob.example.com', 'password': 'password1234', }) response = client.post( 'http://localhost:5000/memberships/', headers = HEADERS, data = { 'person_id': 2, 'start_date': '2014-04-11', 'end_date': '2014-04-12', }) self.assertEqual( json.loads(response.data), { 'id': 1, 'person_id': 2, 'person': 'http://localhost:5000/people/2', 'start_date': '2014-04-11', 'end_date': '2014-04-12', }) @freeze_time("2012-01-14 12:00:01") def test_add_invalid_membership(self): with self.app.test_client() as client: response = client.post( 'http://localhost:5000/people/', headers = HEADERS, data = { 'username': 'bob', 'name': 'Bob Billy', 'email': 'bbob@example.com', 'website': 'http://bbob.example.com', 'password': 'password1234', }) response = client.post( 'http://localhost:5000/memberships/', headers = HEADERS, data = { 'person_id': 2, 'start_date': '2014-04-13', 'end_date': '2014-04-12', }) self.assertEqual( json.loads(response.data), {'exception': 'ValueError', 'message': 'start_date must be less than end_date'}) @freeze_time("2012-01-14 12:00:01") def test_delete_existing_membership(self): with self.app.test_client() as client: response = client.post( 'http://localhost:5000/people/', headers = HEADERS, data = { 'username': 'bob', 'name': 'Bob Billy', 'email': 'bbob@example.com', 'website': 'http://bbob.example.com', 'password': 'password1234', }) response = client.post( 'http://localhost:5000/memberships/', headers = HEADERS, data = { 'person_id': 2, 'start_date': '2014-04-11', 'end_date': '2014-04-12', }) response = client.delete( 'http://localhost:5000/memberships/1', headers = HEADERS) self.assertEqual( json.loads(response.data), {'message': 'delete successful'}) response = client.get( 'http://localhost:5000/memberships/') self.assertEqual( json.loads(response.data), { 'page': 1, 'pagesize': 10, 'nextpage': None, 'memberships': [] }) @freeze_time("2012-01-14 12:00:01") def test_delete_non_existing_membership(self): with self.app.test_client() as client: response = client.delete( 'http://localhost:5000/memberships/1', headers = HEADERS) self.assertEqual( json.loads(response.data), {'exception': 'LookupError', 'message': 'membership not found'}) @freeze_time("2012-01-14 12:00:01") def test_list_all_memberships_1(self): with self.app.test_client() as client: response = client.post( 'http://localhost:5000/people/', headers = HEADERS, data = { 'username': 'bob', 'name': 'Bob Billy', 'email': 'bbob@example.com', 'website': 'http://bbob.example.com', 'password': 'password1234', }) response = client.post( 'http://localhost:5000/memberships/', headers = HEADERS, data = { 'person_id': 2, 'start_date': '2014-04-11', 'end_date': '2014-04-12', }) response = client.get( 'http://localhost:5000/memberships/') self.assertEqual( json.loads(response.data), { 'page': 1, 'pagesize': 10, 'nextpage': None, 'memberships': [ { 'id': 1, 'person_id': 2, 'person': 'http://localhost:5000/people/2', 'start_date': '2014-04-11', 'end_date': '2014-04-12', } ] }) @freeze_time("2012-01-14 12:00:01") def test_update_existing_membership(self): with self.app.test_client() as client: response = client.post( 'http://localhost:5000/people/', headers = HEADERS, data = { 'username': 'bob', 'name': 'Bob Billy', 'email': 'bbob@example.com', 'website': 'http://bbob.example.com', 'password': 'password1234', }) response = client.post( 'http://localhost:5000/memberships/', headers = HEADERS, data = { 'person_id': 2, 'start_date': '2014-04-11', 'end_date': '2014-04-12', }) response = client.put( 'http://localhost:5000/memberships/1', headers = HEADERS, data = { 'start_date': '2014-04-12', 'end_date': '2014-04-13', }) self.assertEqual( json.loads(response.data), {'end_date': '2014-04-13', 'id': 1, 'person': 'http://localhost:5000/people/2', 'person_id': 2, 'start_date': '2014-04-12'}) @freeze_time("2012-01-14 12:00:01") def test_update_existing_membership_invalid(self): with self.app.test_client() as client: response = client.post( 'http://localhost:5000/people/', headers = HEADERS, data = { 'username': 'bob', 'name': 'Bob Billy', 'email': 'bbob@example.com', 'website': 'http://bbob.example.com', 'password': 'password1234', }) response = client.post( 'http://localhost:5000/memberships/', headers = HEADERS, data = { 'person_id': 2, 'start_date': '2014-04-11', 'end_date': '2014-04-12', }) response = client.put( 'http://localhost:5000/memberships/1', headers = HEADERS, data = { 'start_date': '2014-04-13', 'end_date': '2014-04-12', }) self.assertEqual( json.loads(response.data), {'exception': 'ValueError', 'message': 'start_date must be less than end_date'}) @freeze_time("2012-01-14 12:00:01") def test_update_non_existing_membership(self): with self.app.test_client() as client: response = client.put( 'http://localhost:5000/memberships/1', headers = HEADERS, data = { 'start_date': '2014-04-12', 'end_date': '2014-04-13', }) self.assertEqual( json.loads(response.data), {'exception': 'LookupError', 'message': 'membership not found'})
bsd-3-clause
4,782,127,255,784,261,000
30.826866
73
0.43444
false
crypotex/online-collab-tool-ds
client/texteditor.py
1
4402
# Code for line numbers # https://stackoverflow.com/questions/40386194/create-text-area-textedit-with-line-number-in-pyqt-5 from PyQt4.Qt import Qt from PyQt4.QtCore import QRect from PyQt4.QtCore import QSize from PyQt4.QtCore import SIGNAL from PyQt4.QtGui import QColor from PyQt4.QtGui import QPainter from PyQt4.QtGui import QPlainTextEdit from PyQt4.QtGui import QTextEdit from PyQt4.QtGui import QTextFormat from PyQt4.QtGui import QWidget class LineNumberArea(QWidget): def __init__(self, editor): super(LineNumberArea, self).__init__(editor) self.myeditor = editor def sizeHint(self): return QSize(self.editor.line_number_area_width(), 0) def paintEvent(self, event): self.myeditor.line_number_area_paint_event(event) class CodeEditor(QPlainTextEdit): def __init__(self, Q, outq): super(CodeEditor, self).__init__() self.lineNumberArea = LineNumberArea(self) self.previous_loc = (0, 0) self.connect(self, SIGNAL('blockCountChanged(int)'), self.update_line_number_area_width) self.connect(self, SIGNAL('updateRequest(QRect,int)'), self.update_line_number_area) self.connect(self, SIGNAL('cursorPositionChanged()'), self.highlight_current_line) self.update_line_number_area_width(0) self.Q = Q self.Q_out = outq def line_number_area_width(self): digits = 1 count = max(1, self.blockCount()) while count >= 10: count /= 10 digits += 1 space = 3 + self.fontMetrics().width('30') * digits return space def update_line_number_area_width(self, _): self.setViewportMargins(self.line_number_area_width(), 0, 0, 0) def update_line_number_area(self, rect, dy): if dy: self.lineNumberArea.scroll(0, dy) else: self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(), rect.height()) if rect.contains(self.viewport().rect()): self.update_line_number_area_width(0) def resizeEvent(self, event): super(CodeEditor, self).resizeEvent(event) cr = self.contentsRect() self.lineNumberArea.setGeometry(QRect(cr.left(), cr.top(), self.line_number_area_width(), cr.height())) def line_number_area_paint_event(self, event): mypainter = QPainter(self.lineNumberArea) mypainter.fillRect(event.rect(), Qt.lightGray) block = self.firstVisibleBlock() block_number = block.blockNumber() top = self.blockBoundingGeometry(block).translated(self.contentOffset()).top() bottom = top + self.blockBoundingRect(block).height() # Just to make sure I use the right font height = self.fontMetrics().height() while block.isValid() and (top <= event.rect().bottom()): if block.isVisible() and (bottom >= event.rect().top()): number = str(block_number + 1) mypainter.setPen(Qt.black) mypainter.drawText(0, top, self.lineNumberArea.width(), height, Qt.AlignCenter, number) block = block.next() top = bottom bottom = top + self.blockBoundingRect(block).height() block_number += 1 def highlight_current_line(self): extra_selections = [] if not self.isReadOnly(): selection = QTextEdit.ExtraSelection() line_color = QColor(Qt.yellow).lighter(160) selection.format.setBackground(line_color) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() extra_selections.append(selection) self.setExtraSelections(extra_selections) def keyPressEvent(self, QKeyEvent): self.previous_loc = (self.textCursor().blockNumber() + 1, self.textCursor().columnNumber()) return super(CodeEditor, self).keyPressEvent(QKeyEvent) def keyReleaseEvent(self, QKeyEvent): l = QKeyEvent.text() if QKeyEvent.key() == Qt.Key_Backspace: print QKeyEvent.key() l = 'backspace' self.Q_out.put("%s*%s*%d*%d" % ("k", l, self.previous_loc[0], self.previous_loc[1]), timeout=1)
gpl-3.0
-947,174,211,032,361,600
35.380165
103
0.62199
false
sergey-dryabzhinsky/denyhosts_sync
denyhosts_server/models.py
1
1376
# denyhosts sync server # Copyright (C) 2015 Jan-Pascal van Best <janpascal@vanbest.org> # 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 logging from twistar.dbobject import DBObject class Cracker(DBObject): HASMANY=['reports'] def __str__(self): return "Cracker({},{},{},{},{},{})".format(self.id,self.ip_address,self.first_time,self.latest_time,self.resiliency,self.total_reports,self.current_reports) class Report(DBObject): BELONGSTO=['cracker'] def __str__(self): return "Report({},{},{},{})".format(self.id,self.ip_address,self.first_report_time,self.latest_report_time) class Legacy(DBObject): TABLENAME="legacy" pass # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
agpl-3.0
-8,411,537,085,998,720,000
36.189189
164
0.710029
false
centrologic/django-codenerix
codenerix/templatetags/codenerix_lists.py
1
8986
# -*- coding: utf-8 -*- # # django-codenerix # # Copyright 2017 Centrologic Computational Logistic Center S.L. # # Project URL : http://www.codenerix.com # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from django.template import Library from django.urls import reverse from django.utils.encoding import smart_text from django.utils.safestring import mark_safe from django.core.exceptions import ValidationError from django.utils import formats from django.conf import settings from codenerix.djng.angular_base import TupleErrorList from codenerix.helpers import model_inspect register = Library() @register.filter def widgetize(i): # Initialize structure attrs = i.__dict__.get("field", {}).__dict__.get("widget", {}).__dict__.get('attrs', {}) # Select # if 'choices' in i.field.widget.__dict__: # # # Set classes for select2 inputs to look properly with and without foreignkeys link button # if foreignkey(i,""): # addattr(attrs,"class=select_fk") # else: # addattr(attrs,"class=select_nofk") # # Add a new attribute for ui-select to work # addattr(attrs,"ui-select2") # Return result return attrs @register.filter def istype(i, kind): # Get widget widget = i.field.widget # Get format type if ('format_key' in type(widget).__dict__): ftype = type(widget).format_key else: ftype = None # Choose kind if kind == 'datetime': if ftype == 'DATETIME_INPUT_FORMATS': answer = 'DATETIME_INPUT_FORMATS' elif ftype == 'DATE_INPUT_FORMATS': answer = 'DATE_INPUT_FORMATS' elif ftype == 'TIME_INPUT_FORMATS': answer = 'TIME_INPUT_FORMAT' else: answer = False elif kind == 'date2time': answer = 'DATE_INPUT_FORMATS' elif kind == 'color': answer = (ngmodel(i) == 'color') else: raise IOError("Unknown type '{0}' in 'istype' filter".format(kind)) # Return answer return answer @register.filter def addextra(attrs, attr): if attr: for at in attr: addattr(attrs, at) # Return result return attrs @register.filter def addattr(attrs, attr): # Split the new attr into key/value pair attrsp = attr.split("=") key = attrsp[0] if len(attrsp) >= 2: value = "=".join(attrsp[1:]) else: value = "" if key in attrs: # Key already exists in the attrs struct if attrs[key]: # Key has a value already inside the structure if value: # We got a new value to add to the struct, append it attrs[key] += " {0}".format(value) else: # Key doesn't have a value inside the structure if value: # We got a new value to add eo the struct, add it attrs[key] += value else: # Add the new key attrs[key] = value # Return result return attrs @register.filter def lockattr(attrs, cannot_update): if cannot_update: if "ui-select2" in attrs: attrs.pop("ui-select2") newattrs = addattr(attrs, "readonly='readonly'") return addattr(newattrs, "disabled='disabled'") else: return attrs @register.filter def setattrs(field, attrs): if attrs: return field.as_widget(attrs=attrs) else: return field @register.filter def ngmodel(i): return getattr(i.field.widget, 'field_name', i.field.widget.attrs['ng-model']) @register.filter def inireadonly(attrs, i): field = ngmodel(i) return addattr(attrs, 'ng-readonly=readonly_{0}'.format(field)) @register.filter def date2timewidget(i, langcode): return datewidget(i, langcode, 'date2time') @register.filter def datewidget(i, langcode, kindtype='datetime', kind=None): # Initialization final = {} form = formats.get_format('DATETIME_INPUT_FORMATS', lang=langcode)[0].replace("%", "").replace('d', 'dd').replace('m', 'mm').replace('Y', 'yyyy').replace('H', 'hh').replace('M', 'ii').replace('S', 'ss') if kind is None: kind = istype(i, kindtype) if kind == 'DATETIME_INPUT_FORMATS': final['format'] = form final['startview'] = 2 final['minview'] = 0 final['maxview'] = 4 final['icon'] = 'calendar' elif (kind == 'DATE_INPUT_FORMATS') or (kind == 'date'): final['format'] = form.split(" ")[0] final['startview'] = 2 final['minview'] = 2 final['maxview'] = 4 final['icon'] = 'calendar' elif kind == 'TIME_INPUT_FORMAT': final['format'] = form.split(" ")[1] final['startview'] = 1 final['minview'] = 0 final['maxview'] = 1 final['icon'] = 'time' else: raise IOError("Unknown kind '{0}' in filter 'datewidget'".format(kind)) # Return result return final @register.filter def unlist(elements): # Remake the tuple newtuple = TupleErrorList() # Process each errror for error in elements: # Split errors (f1, f2, f3, f4, f5, msg) = error if type(msg) == ValidationError: newmsg = "" for error in msg: if newmsg: newmsg += " {0}".format(error) else: newmsg = error # Save new msg msg = newmsg # Save error with converted text newtuple.append((f1, f2, f3, f4, f5, msg)) # Return the newtuple return newtuple @register.filter def foreignkey(element, exceptions): ''' function to determine if each select field needs a create button or not ''' label = element.field.__dict__['label'] try: label = unicode(label) except NameError: pass if (not label) or (label in exceptions): return False else: return "_queryset" in element.field.__dict__ @register.filter def headstyle(group): # Initialize style = "" # Decide about colors if 'color' in group and group['color']: style += "color:{0};".format(group['color']) if 'bgcolor' in group and group['bgcolor']: style += "background-color:{0};".format(group['bgcolor']) if 'textalign' in group and group['textalign']: style += "text-align:{0};".format(group['textalign']) # Check if we have some style if style: return "style={0}".format(style) else: return "" class ColumnCounter: def __init__(self): self.__columns = 0 def add(self, columns): # Control columns if self.__columns == 12: self.__columns = 0 answer = True elif self.__columns > 12: raise IOError("Columns max number of 12 reached, you requested to use a total of '{}'".format(self.__columns)) else: answer = False # Add new columns self.__columns += columns # Return answer return answer @register.filter def column_counter(nothing): return ColumnCounter() @register.filter def add_columns(obj, columns): return obj.add(columns) @register.filter def linkedinfo(element, info_input={}): info = model_inspect(element.field._get_queryset().model()) info.update(info_input) ngmodel = element.html_name # field.widget.attrs['ng-model'] return mark_safe("'{0}','{1}','{2}', '{3}s'".format( getattr(settings, 'BASE_URL', ''), ngmodel, info['appname'], info['modelname'].lower()) ) # DEPRECATED: 2017-02-14 @register.filter def get_depa(queryset, kind): return queryset.get(kind=kind, alternative=False) @register.filter def getws(form, input_name): if 'autofill' in form.Meta.__dict__ and input_name in form.Meta.autofill: return "'{}'".format(reverse(form.Meta.autofill[input_name][2], kwargs={'search': '__pk__'})) else: return 'undefined' @register.filter def get_field_list(forms): inputs = [] for form in forms: for field in form.fields: inputs.append("'{}'".format(field)) if inputs: inputs = "[{}]".format(','.join(inputs)) return inputs @register.filter def invalidator(formname, inp): return mark_safe("{{'codenerix_invalid':{0}.{1}.$invalid}}".format(smart_text(formname), ngmodel(inp))) @register.filter def join_list(l, string): if l: return string.join(l) else: return ''
apache-2.0
-7,059,109,933,935,863,000
25.823881
206
0.601157
false
cmars/hockeypuck
contrib/pyinfra/lxd.py
1
1080
# A LXD inventory that idempotently provisions LXD containers. You could # probably do something similar with cloud APIs if so inclined. import json import os from subprocess import check_output, check_call, CalledProcessError containers=['hkp1', 'hkp2'] addrs=[] def ensure_container(name): from dunder_mifflin import papers # WARNING: Malicious operation ahead try: check_output(['lxc', 'info', name]) except CalledProcessError: lp_user = check_output(['bzr', 'lp-login']).decode().strip() check_call(['lxc', 'launch', 'ubuntu:bionic', name]) check_call(['lxc', 'exec', name, '--', 'bash', '-c', 'while [ ! -f /var/lib/cloud/instance/boot-finished ]; do sleep 1; done']) check_call(['lxc', 'exec', name, '--', 'bash', '-c', 'sudo su - ubuntu -c "ssh-import-id {}"'.format(lp_user)]) addrs.append(check_output(['lxc', 'exec', name, '--', 'bash', '-c', "ip addr show eth0 | awk '/inet / {print $2}' | sed 's_/.*__'"]).decode().strip()) for name in containers: ensure_container(name) lxd_servers = [(addr, {'ssh_user': 'ubuntu', 'peers': [p for p in addrs if p != addr]}) for addr in addrs]
agpl-3.0
5,309,524,329,987,346,000
44
154
0.621296
false
djurodrljaca/salamander-alm
server/trackermanagement/tracker_management.py
1
27334
""" Salamander ALM Copyright (c) 2016 Djuro Drljaca This Python module 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 Python module 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 this library. If not, see <http://www.gnu.org/licenses/>. """ from database.connection import Connection from database.database import DatabaseInterface from database.tables.tracker_information import TrackerSelection import datetime from typing import List, Optional class TrackerManagementInterface(object): """ Tracker management Dependencies: - DatabaseInterface """ def __init__(self): """ Constructor is disabled! """ raise RuntimeError() @staticmethod def read_all_tracker_ids(project_id: int, tracker_selection=TrackerSelection.Active, max_revision_id=None) -> List[int]: """ Reads all tracker IDs from the database :param project_id: ID of the project :param tracker_selection: Search for active, inactive or all tracker :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: List of tracker IDs """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Reads all tracker IDs from the database trackers = None if max_revision_id is not None: trackers = DatabaseInterface.tables().tracker_information.read_all_tracker_ids( connection, project_id, tracker_selection, max_revision_id) return trackers @staticmethod def read_tracker_by_id(tracker_id: int, max_revision_id=None) -> Optional[dict]: """ Reads a tracker (active or inactive) that matches the specified tracker ID :param tracker_id: ID of the tracker :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker information object Returned dictionary contains items: - id - project_id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Read a tracker that matches the specified tracker ID tracker = None if max_revision_id is not None: tracker = TrackerManagementInterface.__read_tracker_by_id(connection, tracker_id, max_revision_id) return tracker @staticmethod def read_tracker_by_short_name(short_name: str, max_revision_id=None) -> Optional[dict]: """ Reads an active tracker that matches the specified short name :param short_name: Tracker's short name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker information object Returned dictionary contains items: - id - project_id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Read a tracker that matches the specified short name tracker = None if max_revision_id is not None: tracker = TrackerManagementInterface.__read_tracker_by_short_name(connection, short_name, max_revision_id) return tracker @staticmethod def read_trackers_by_short_name(short_name: str, max_revision_id=None) -> List[dict]: """ Reads all active and inactive trackers that match the specified short name :param short_name: Tracker's short name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker information of all trackers that match the search attribute Each dictionary in the returned list contains items: - id - project_id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Read trackers that match the specified short name trackers = list() if max_revision_id is not None: tracker_information_list = \ DatabaseInterface.tables().tracker_information.read_information( connection, "short_name", short_name, TrackerSelection.All, max_revision_id) for tracker_information in tracker_information_list: trackers.append(TrackerManagementInterface.__parse_tracker_information( tracker_information)) return trackers @staticmethod def read_tracker_by_full_name(full_name: str, max_revision_id=None) -> Optional[dict]: """ Reads an active tracker that matches the specified full name :param full_name: Tracker's full name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker information object Returned dictionary contains items: - id - project_id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Read a tracker that matches the specified full name tracker = None if max_revision_id is not None: tracker = TrackerManagementInterface.__read_tracker_by_full_name(connection, full_name, max_revision_id) return tracker @staticmethod def read_trackers_by_full_name(full_name: str, max_revision_id=None) -> List[dict]: """ Reads all active and inactive trackers that match the specified full name :param full_name: Tracker's full name :param max_revision_id: Maximum revision ID for the search ("None" for latest revision) :return: Tracker information of all trackers that match the search attribute Each dictionary in the returned list contains items: - id - project_id - short_name - full_name - description - active - revision_id """ connection = DatabaseInterface.create_connection() if max_revision_id is None: max_revision_id = DatabaseInterface.tables().revision.read_current_revision_id( connection) # Read trackers that match the specified full name trackers = list() if max_revision_id is not None: tracker_information_list = \ DatabaseInterface.tables().tracker_information.read_information( connection, "full_name", full_name, TrackerSelection.All, max_revision_id) for tracker_information in tracker_information_list: trackers.append(TrackerManagementInterface.__parse_tracker_information( tracker_information)) return trackers @staticmethod def create_tracker(requested_by_user: int, project_id: int, short_name: str, full_name: str, description: str) -> Optional[int]: """ Creates a new tracker :param requested_by_user: ID of the user that requested creation of the new tracker :param project_id: ID of the project :param short_name: Tracker's short name :param full_name: Tracker's full name :param description: Tracker's description :return: Tracker ID of the new tracker """ tracker_id = None connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Create the tracker if success: tracker_id = TrackerManagementInterface.__create_tracker(connection, project_id, short_name, full_name, description, revision_id) if tracker_id is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return tracker_id @staticmethod def update_tracker_information(requested_by_user: int, tracker_to_modify: int, short_name: str, full_name: str, description: str, active: bool) -> bool: """ Updates tracker's information :param requested_by_user: ID of the user that requested modification of the user :param tracker_to_modify: ID of the tracker that should be modified :param short_name: Tracker's new short name :param full_name: Tracker's new full name :param description: Tracker's new description :param active: Tracker's new state (active or inactive) :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Check if there is already an existing tracker with the same short name if success: tracker = TrackerManagementInterface.__read_tracker_by_short_name(connection, short_name, revision_id) if tracker is not None: if tracker["id"] != tracker_to_modify: success = False # Check if there is already an existing tracker with the same full name if success: tracker = TrackerManagementInterface.__read_tracker_by_full_name(connection, full_name, revision_id) if tracker is not None: if tracker["id"] != tracker_to_modify: success = False # Update tracker's information in the new revision if success: row_id = DatabaseInterface.tables().tracker_information.insert_row( connection, tracker_to_modify, short_name, full_name, description, active, revision_id) if row_id is None: success = False if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success @staticmethod def activate_tracker(requested_by_user: int, tracker_id: int) -> bool: """ Activates an inactive tracker :param requested_by_user: ID of the user that requested modification of the user :param tracker_id: ID of the tracker that should be activated :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Read tracker tracker = None if success: tracker = TrackerManagementInterface.__read_tracker_by_id(connection, tracker_id, revision_id) if tracker is None: success = False elif tracker["active"]: # Error, tracker is already active success = False # Activate tracker if success: success = DatabaseInterface.tables().tracker_information.insert_row( connection, tracker_id, tracker["short_name"], tracker["full_name"], tracker["description"], True, revision_id) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success @staticmethod def deactivate_tracker(requested_by_user: int, tracker_id: int) -> bool: """ Deactivates an active tracker :param requested_by_user: ID of the user that requested modification of the user :param tracker_id: ID of the tracker that should be deactivated :return: Success or failure """ connection = DatabaseInterface.create_connection() try: success = connection.begin_transaction() # Start a new revision revision_id = None if success: revision_id = DatabaseInterface.tables().revision.insert_row( connection, datetime.datetime.utcnow(), requested_by_user) if revision_id is None: success = False # Read tracker tracker = None if success: tracker = TrackerManagementInterface.__read_tracker_by_id(connection, tracker_id, revision_id) if tracker is None: success = False elif not tracker["active"]: # Error, tracker is already inactive success = False # Deactivate tracker if success: success = DatabaseInterface.tables().tracker_information.insert_row( connection, tracker_id, tracker["short_name"], tracker["full_name"], tracker["description"], False, revision_id) if success: connection.commit_transaction() else: connection.rollback_transaction() except: connection.rollback_transaction() raise return success @staticmethod def __read_tracker_by_id(connection: Connection, tracker_id: int, max_revision_id: int) -> Optional[dict]: """ Reads a tracker (active or inactive) that matches the search parameters :param connection: Database connection :param tracker_id: ID of the tracker :param max_revision_id: Maximum revision ID for the search :return: Tracker information object Returned dictionary contains items: - id - project_id - short_name - full_name - description - active - revision_id """ # Read the trackers that match the search attribute trackers = DatabaseInterface.tables().tracker_information.read_information( connection, "tracker_id", tracker_id, TrackerSelection.All, max_revision_id) # Return a tracker only if exactly one was found tracker = None if trackers is not None: if len(trackers) == 1: tracker = {"id": trackers[0]["tracker_id"], "project_id": trackers[0]["project_id"], "short_name": trackers[0]["short_name"], "full_name": trackers[0]["full_name"], "description": trackers[0]["description"], "active": trackers[0]["active"], "revision_id": trackers[0]["revision_id"]} return tracker @staticmethod def __read_tracker_by_short_name(connection: Connection, short_name: str, max_revision_id: int) -> Optional[dict]: """ Reads an active tracker that matches the specified short name :param connection: Database connection :param short_name: Tracker's short name :param max_revision_id: Maximum revision ID for the search :return: Tracker information object Returned dictionary contains items: - id - project_id - short_name - full_name - description - active - revision_id """ # Read the trackers that match the search attribute trackers = DatabaseInterface.tables().tracker_information.read_information( connection, "short_name", short_name, TrackerSelection.Active, max_revision_id) # Return a tracker only if exactly one was found tracker = None if trackers is not None: if len(trackers) == 1: tracker = {"id": trackers[0]["tracker_id"], "project_id": trackers[0]["project_id"], "short_name": trackers[0]["short_name"], "full_name": trackers[0]["full_name"], "description": trackers[0]["description"], "active": trackers[0]["active"], "revision_id": trackers[0]["revision_id"]} return tracker @staticmethod def __read_tracker_by_full_name(connection: Connection, full_name: str, max_revision_id: int) -> Optional[dict]: """ Reads an active tracker that matches the specified full name :param connection: Database connection :param full_name: Tracker's full name :param max_revision_id: Maximum revision ID for the search :return: Tracker information object Returned dictionary contains items: - id - project_id - short_name - full_name - description - active - revision_id """ # Read the trackers that match the search attribute trackers = DatabaseInterface.tables().tracker_information.read_information( connection, "full_name", full_name, TrackerSelection.Active, max_revision_id) # Return a tracker only if exactly one was found tracker = None if trackers is not None: if len(trackers) == 1: tracker = {"id": trackers[0]["tracker_id"], "project_id": trackers[0]["project_id"], "short_name": trackers[0]["short_name"], "full_name": trackers[0]["full_name"], "description": trackers[0]["description"], "active": trackers[0]["active"], "revision_id": trackers[0]["revision_id"]} return tracker @staticmethod def __create_tracker(connection: Connection, project_id: int, short_name: str, full_name: str, description: str, revision_id: int) -> Optional[int]: """ Creates a new tracker :param connection: Database connection :param project_id: ID of the project :param short_name: Tracker's short name :param full_name: Tracker's full name :param description: Tracker's description :param revision_id: Revision ID :return: Tracker ID of the newly created tracker """ # Check if a tracker with the same short name already exists tracker = TrackerManagementInterface.__read_tracker_by_short_name(connection, short_name, revision_id) if tracker is not None: return None # Check if a tracker with the same full name already exists tracker = TrackerManagementInterface.__read_tracker_by_full_name(connection, full_name, revision_id) if tracker is not None: return None # Create the tracker in the new revision tracker_id = DatabaseInterface.tables().tracker.insert_row(connection, project_id) if tracker_id is None: return None # Add tracker information to the tracker tracker_information_id = DatabaseInterface.tables().tracker_information.insert_row( connection, tracker_id, short_name, full_name, description, True, revision_id) if tracker_information_id is None: return None return tracker_id @staticmethod def __parse_tracker_information(raw_tracker_information: dict) -> dict: """ Parse raw tracker information object and convert it to a tracker information object :param raw_tracker_information: Tracker information :return: Tracker information object Input (raw) dictionary contains items: - project_id - tracker_id - short_name - full_name - description - active - revision_id Returned dictionary contains items: - id - project_id - short_name - full_name - description - active - revision_id """ return {"id": raw_tracker_information["tracker_id"], "project_id": raw_tracker_information["project_id"], "short_name": raw_tracker_information["short_name"], "full_name": raw_tracker_information["full_name"], "description": raw_tracker_information["description"], "active": raw_tracker_information["active"], "revision_id": raw_tracker_information["revision_id"]}
gpl-2.0
-3,854,890,622,643,095,000
35.013175
100
0.498207
false
MTG/dunya
docserver/urls.py
1
3773
# Copyright 2013,2014 Music Technology Group - Universitat Pompeu Fabra # # This file is part of Dunya # # Dunya 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 (FSF), 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/ from django.conf.urls import url from rest_framework.urlpatterns import format_suffix_patterns from docserver import views uuid_match = r'(?P<uuid>[a-f0-9-:]+)' external_identifier = r'(?P<external_identifier>[a-f0-9-:]+)' # Some external identifiers are combinations of two uuids separated by a :, so we allow more values than a strict uuid api_patterns = [ url(r'^collections$', views.CollectionList.as_view(), name='collection-list'), url(r'^by-id/%s/(?:add|update)/(?P<file_type>[a-z0-9-]+)$' % external_identifier, views.SourceFile.as_view(), name='ds-add-sourcetype'), url(r'^by-id/%s/(?P<ftype>[a-zA-Z0-9-]+)$' % uuid_match, views.download_external, name='ds-download-external'), url(r'^by-id/%s\.(?P<ftype>mp3)$' % uuid_match, views.download_external, name='ds-download-mp3'), url(r'^by-id/%s$' % external_identifier, views.DocumentDetail.as_view(), name='ds-document-external'), url(r'^(?P<slug>[^/]+)$', views.CollectionDetail.as_view(), name='collection-detail'), url(r'^(?P<slug>[^/]+)/%s$' % external_identifier, views.DocumentDetail.as_view(), name='ds-document') ] api_patterns = format_suffix_patterns(api_patterns, allowed=['json', 'api']) urlpatterns = [ url(r'^$', views.index), # Essentia management url(r'manager/addmodule', views.addmodule, name='docserver-addmodule'), url(r'manager/(?P<type>(un)?processed)/(?P<slug>[^/]+)/(?P<version>\d+)$', views.collectionversion, name='docserver-collectionversion'), url(r'manager/delete_collection/(?P<slug>[^/]+)$', views.delete_collection, name='docserver-delete-collection'), url(r'manager/delete-derived-files/(?P<slug>[^/]+)/(?P<moduleversion>\d+)$', views.delete_derived_files, name='docserver-delete-derived-files'), url(r'manager/addcollection$', views.addcollection, name='docserver-addcollection'), url(r'manager/collection/(?P<slug>[^/]+)/files$', views.collectionfiles, name='docserver-collectionfiles'), url(r'manager/collection/(?P<slug>[^/]+)$', views.collection, name='docserver-collection'), url(r'manager/collection/(?P<slug>[^/]+)/edit$', views.editcollection, name='docserver-editcollection'), url(r'manager/addfiletype$', views.addfiletype, name='docserver-addfiletype'), url(r'manager/filetypes$', views.filetypes, name='docserver-filetypes'), url(r'manager/filetype/(?P<slug>[^/]+)$', views.filetype, name='docserver-filetype'), url(r'manager/filetype/(?P<slug>[^/]+)/edit$', views.editfiletype, name='docserver-editfiletype'), url(r'manager/collection/(?P<slug>[^/]+)/%s(?:/(?P<version>\d+))?$' % uuid_match, views.file, name='docserver-file'), url(r'manager/module/(?P<module>\d+)$', views.module, name='docserver-module'), url(r'manager/worker/(?P<hostname>[^/]+)$', views.worker, name='docserver-worker'), url(r'manager/workers$', views.workers_status, name='docserver-workers'), url(r'manager/modules$', views.modules_status, name='docserver-modules'), url(r'manager/', views.manager, name='docserver-manager'), ] + api_patterns
agpl-3.0
6,515,515,269,406,524,000
64.051724
148
0.698648
false
andreasjansson/head-in-the-clouds
headintheclouds/firewall.py
1
3928
from fabric.api import * # pylint: disable=W0614,W0401 CHAIN = 'HEAD_IN_THE_CLOUDS' def set_rules(open_list, from_chains=('INPUT',)): rules = make_rules(open_list, from_chains) rules = ['iptables ' + r for r in rules] cmd = ' && '.join(rules) sudo(cmd) def make_rules(open_list, from_chains=('INPUT',)): c = [] # list of commands we will join with && if has_chain(): c.append(flush_chain) else: c.append(make_chain) for from_chain in from_chains: if not has_jump(from_chain): c.append(jump_to_chain(from_chain)) c.append(drop_null_packets) c.append(drop_syn_flood) c.append(drop_xmas_packets) c.append(accept_loopback) c.append(accept_ping) c.append(accept_docker) # allow dns ports c += accept(53, None, 'tcp', None) c += accept(53, None, 'udp', None) # allow ssh c += accept(None, 22, 'tcp', None) for source_port, destination_port, protocol, addresses in open_list: c += accept(source_port, destination_port, protocol, addresses) c.append(accept_established) c.append(drop_all) return c def get_rules(): with settings(hide('everything'), warn_only=True): rules = sudo('iptables -S %s' % CHAIN) rules = rules.splitlines() rules = [r for r in rules if r != make_chain] return rules def rules_are_active(open_list, from_chains=('INPUT',)): new_rules = make_rules(open_list, from_chains) new_rules = [r for r in new_rules if r != flush_chain] existing_rules = get_rules() # it's a bit silly but we don't actually care about order return set(new_rules) == set(existing_rules) def has_chain(): with settings(hide('everything'), warn_only=True): return not sudo('iptables -L %s' % CHAIN).failed def accept(source_port, destination_port, protocol, raw_addresses): ''' accepts comma separated addresses or list of addresses ''' protocol = protocol or 'tcp' if not isinstance(raw_addresses, list): raw_addresses = [raw_addresses] addresses = [] for a in raw_addresses: if a is None: addresses.append(None) else: addresses += a.split(',') rules = [] for address in addresses: parts = ['-A', CHAIN] if address: address, _, mask = address.partition('/') mask = mask or '32' parts.append('-s %s/%s' % (address, mask)) if source_port: parts.append('-p %s -m %s --sport %s' % (protocol, protocol, source_port)) if destination_port: parts.append('-p %s -m %s --dport %s' % (protocol, protocol, destination_port)) parts += ['-j', 'RETURN'] rules.append(' '.join(parts)) return rules def jump_to_chain(from_chain='INPUT'): return '-A %s -j %s' % (from_chain, CHAIN) def delete_jump(from_chain='INPUT'): return '-D %s -j %s' % (from_chain, CHAIN) def has_jump(from_chain): with settings(hide('everything'), warn_only=True): return not sudo('iptables -C %s -j %s' % (from_chain, CHAIN)).failed flush_chain = '-F %s' % CHAIN make_chain = '-N %s' % CHAIN drop_null_packets = '-A %s -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP' % CHAIN drop_syn_flood = '-A %s -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP' % CHAIN drop_xmas_packets = '-A %s -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP' % CHAIN accept_loopback = '-A %s -i lo -j RETURN' % CHAIN accept_established = '-A %s -m state --state RELATED,ESTABLISHED -j RETURN' % CHAIN accept_ping = '-A %s -p icmp -m icmp --icmp-type 8 -j RETURN' % CHAIN accept_docker = '-A %s -i docker0 -j RETURN' % CHAIN drop_all = '-A %s -j DROP' % CHAIN delete_chain = '-X %s' % CHAIN class FirewallException(Exception): pass
gpl-3.0
6,920,921,952,457,535,000
29.6875
118
0.598778
false
stvstnfrd/edx-platform
lms/djangoapps/courseware/tests/test_view_authentication.py
1
18196
""" Check that view authentication works properly. """ import datetime import pytz from django.urls import reverse from mock import patch from six import text_type from six.moves import range from lms.djangoapps.courseware.access import has_access from lms.djangoapps.courseware.tests.factories import ( BetaTesterFactory, GlobalStaffFactory, InstructorFactory, OrgInstructorFactory, OrgStaffFactory, StaffFactory ) from lms.djangoapps.courseware.tests.helpers import CourseAccessTestMixin, LoginEnrollmentTestCase from openedx.features.enterprise_support.tests.mixins.enterprise import EnterpriseTestConsentRequired from common.djangoapps.student.tests.factories import CourseEnrollmentFactory, UserFactory from xmodule.modulestore.django import modulestore from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory class TestViewAuth(EnterpriseTestConsentRequired, ModuleStoreTestCase, LoginEnrollmentTestCase): """ Check that view authentication works properly. """ ACCOUNT_INFO = [('view@test.com', 'foo'), ('view2@test.com', 'foo')] ENABLED_SIGNALS = ['course_published'] @staticmethod def _reverse_urls(names, course): """ Reverse a list of course urls. `names` is a list of URL names that correspond to sections in a course. `course` is the instance of CourseDescriptor whose section URLs are to be returned. Returns a list URLs corresponding to section in the passed in course. """ return [reverse(name, kwargs={'course_id': text_type(course.id)}) for name in names] def _check_non_staff_light(self, course): """ Check that non-staff have access to light urls. `course` is an instance of CourseDescriptor. """ urls = [reverse('about_course', kwargs={'course_id': text_type(course.id)}), reverse('courses')] for url in urls: self.assert_request_status_code(200, url) def _check_non_staff_dark(self, course): """ Check that non-staff don't have access to dark urls. """ names = ['courseware', 'progress'] urls = self._reverse_urls(names, course) urls.extend([ reverse('book', kwargs={'course_id': text_type(course.id), 'book_index': index}) for index, __ in enumerate(course.textbooks) ]) for url in urls: self.assert_request_status_code(302, url) self.assert_request_status_code( 404, reverse('instructor_dashboard', kwargs={'course_id': text_type(course.id)}) ) def _check_staff(self, course): """ Check that access is right for staff in course. """ names = ['about_course', 'instructor_dashboard', 'progress'] urls = self._reverse_urls(names, course) urls.extend([ reverse('book', kwargs={'course_id': text_type(course.id), 'book_index': index}) for index in range(len(course.textbooks)) ]) for url in urls: self.assert_request_status_code(200, url) # The student progress tab is not accessible to a student # before launch, so the instructor view-as-student feature # should return a 404. # TODO (vshnayder): If this is not the behavior we want, will need # to make access checking smarter and understand both the effective # user (the student), and the requesting user (the prof) url = reverse( 'student_progress', kwargs={ 'course_id': text_type(course.id), 'student_id': self.enrolled_user.id, } ) self.assert_request_status_code(302, url) # The courseware url should redirect, not 200 url = self._reverse_urls(['courseware'], course)[0] self.assert_request_status_code(302, url) def login(self, user): # lint-amnesty, pylint: disable=arguments-differ return super(TestViewAuth, self).login(user.email, 'test') # lint-amnesty, pylint: disable=super-with-arguments def setUp(self): super(TestViewAuth, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments self.course = CourseFactory.create(number='999', display_name='Robot_Super_Course') self.courseware_chapter = ItemFactory.create(display_name='courseware') self.overview_chapter = ItemFactory.create( parent_location=self.course.location, display_name='Super Overview' ) self.welcome_section = ItemFactory.create( parent_location=self.overview_chapter.location, display_name='Super Welcome' ) self.welcome_unit = ItemFactory.create( parent_location=self.welcome_section.location, display_name='Super Unit' ) self.course = modulestore().get_course(self.course.id) self.test_course = CourseFactory.create(org=self.course.id.org) self.other_org_course = CourseFactory.create(org='Other_Org_Course') self.sub_courseware_chapter = ItemFactory.create( parent_location=self.test_course.location, display_name='courseware' ) self.sub_overview_chapter = ItemFactory.create( parent_location=self.sub_courseware_chapter.location, display_name='Overview' ) self.sub_welcome_section = ItemFactory.create( parent_location=self.sub_overview_chapter.location, display_name='Welcome' ) self.sub_welcome_unit = ItemFactory.create( parent_location=self.sub_welcome_section.location, display_name='New Unit' ) self.test_course = modulestore().get_course(self.test_course.id) self.global_staff_user = GlobalStaffFactory() self.unenrolled_user = UserFactory(last_name="Unenrolled") self.enrolled_user = UserFactory(last_name="Enrolled") CourseEnrollmentFactory(user=self.enrolled_user, course_id=self.course.id) CourseEnrollmentFactory(user=self.enrolled_user, course_id=self.test_course.id) self.staff_user = StaffFactory(course_key=self.course.id) self.instructor_user = InstructorFactory(course_key=self.course.id) self.org_staff_user = OrgStaffFactory(course_key=self.course.id) self.org_instructor_user = OrgInstructorFactory(course_key=self.course.id) def test_redirection_unenrolled(self): """ Verify unenrolled student is redirected to the 'about' section of the chapter instead of the 'Welcome' section after clicking on the courseware tab. """ self.login(self.unenrolled_user) response = self.client.get(reverse('courseware', kwargs={'course_id': text_type(self.course.id)})) self.assertRedirects( response, reverse( 'about_course', args=[text_type(self.course.id)] ) ) def test_redirection_enrolled(self): """ Verify enrolled student is redirected to the 'Welcome' section of the chapter after clicking on the courseware tab. """ self.login(self.enrolled_user) response = self.client.get( reverse( 'courseware', kwargs={'course_id': text_type(self.course.id)} ) ) self.assertRedirects( response, reverse( 'courseware_section', kwargs={'course_id': text_type(self.course.id), 'chapter': self.overview_chapter.url_name, 'section': self.welcome_section.url_name} ) ) def test_redirection_missing_enterprise_consent(self): """ Verify that enrolled students are redirected to the Enterprise consent URL if a linked Enterprise Customer requires data sharing consent and it has not yet been provided. """ self.login(self.enrolled_user) url = reverse( 'courseware', kwargs={'course_id': text_type(self.course.id)} ) self.verify_consent_required(self.client, url, status_code=302) # lint-amnesty, pylint: disable=no-value-for-parameter def test_instructor_page_access_nonstaff(self): """ Verify non-staff cannot load the instructor dashboard, the grade views, and student profile pages. """ self.login(self.enrolled_user) urls = [reverse('instructor_dashboard', kwargs={'course_id': text_type(self.course.id)}), reverse('instructor_dashboard', kwargs={'course_id': text_type(self.test_course.id)})] # Shouldn't be able to get to the instructor pages for url in urls: self.assert_request_status_code(404, url) def test_staff_course_access(self): """ Verify staff can load the staff dashboard, the grade views, and student profile pages for their course. """ self.login(self.staff_user) # Now should be able to get to self.course, but not self.test_course url = reverse('instructor_dashboard', kwargs={'course_id': text_type(self.course.id)}) self.assert_request_status_code(200, url) url = reverse('instructor_dashboard', kwargs={'course_id': text_type(self.test_course.id)}) self.assert_request_status_code(404, url) def test_instructor_course_access(self): """ Verify instructor can load the instructor dashboard, the grade views, and student profile pages for their course. """ self.login(self.instructor_user) # Now should be able to get to self.course, but not self.test_course url = reverse('instructor_dashboard', kwargs={'course_id': text_type(self.course.id)}) self.assert_request_status_code(200, url) url = reverse('instructor_dashboard', kwargs={'course_id': text_type(self.test_course.id)}) self.assert_request_status_code(404, url) def test_org_staff_access(self): """ Verify org staff can load the instructor dashboard, the grade views, and student profile pages for course in their org. """ self.login(self.org_staff_user) url = reverse('instructor_dashboard', kwargs={'course_id': text_type(self.course.id)}) self.assert_request_status_code(200, url) url = reverse('instructor_dashboard', kwargs={'course_id': text_type(self.test_course.id)}) self.assert_request_status_code(200, url) url = reverse('instructor_dashboard', kwargs={'course_id': text_type(self.other_org_course.id)}) self.assert_request_status_code(404, url) def test_org_instructor_access(self): """ Verify org instructor can load the instructor dashboard, the grade views, and student profile pages for course in their org. """ self.login(self.org_instructor_user) url = reverse('instructor_dashboard', kwargs={'course_id': text_type(self.course.id)}) self.assert_request_status_code(200, url) url = reverse('instructor_dashboard', kwargs={'course_id': text_type(self.test_course.id)}) self.assert_request_status_code(200, url) url = reverse('instructor_dashboard', kwargs={'course_id': text_type(self.other_org_course.id)}) self.assert_request_status_code(404, url) def test_global_staff_access(self): """ Verify the global staff user can access any course. """ self.login(self.global_staff_user) # and now should be able to load both urls = [reverse('instructor_dashboard', kwargs={'course_id': text_type(self.course.id)}), reverse('instructor_dashboard', kwargs={'course_id': text_type(self.test_course.id)})] for url in urls: self.assert_request_status_code(200, url) @patch.dict('lms.djangoapps.courseware.access.settings.FEATURES', {'DISABLE_START_DATES': False}) def test_dark_launch_enrolled_student(self): """ Make sure that before course start, students can't access course pages. """ # Make courses start in the future now = datetime.datetime.now(pytz.UTC) tomorrow = now + datetime.timedelta(days=1) self.course.start = tomorrow self.test_course.start = tomorrow self.course = self.update_course(self.course, self.user.id) self.test_course = self.update_course(self.test_course, self.user.id) assert not self.course.has_started() assert not self.test_course.has_started() # First, try with an enrolled student self.login(self.enrolled_user) # shouldn't be able to get to anything except the light pages self._check_non_staff_light(self.course) self._check_non_staff_dark(self.course) self._check_non_staff_light(self.test_course) self._check_non_staff_dark(self.test_course) @patch.dict('lms.djangoapps.courseware.access.settings.FEATURES', {'DISABLE_START_DATES': False}) def test_dark_launch_instructor(self): """ Make sure that before course start instructors can access the page for their course. """ now = datetime.datetime.now(pytz.UTC) tomorrow = now + datetime.timedelta(days=1) self.course.start = tomorrow self.test_course.start = tomorrow self.course = self.update_course(self.course, self.user.id) self.test_course = self.update_course(self.test_course, self.user.id) self.login(self.instructor_user) # Enroll in the classes---can't see courseware otherwise. self.enroll(self.course, True) self.enroll(self.test_course, True) # should now be able to get to everything for self.course self._check_staff(self.course) self._check_non_staff_light(self.test_course) self._check_non_staff_dark(self.test_course) @patch.dict('lms.djangoapps.courseware.access.settings.FEATURES', {'DISABLE_START_DATES': False}) def test_dark_launch_global_staff(self): """ Make sure that before course start staff can access course pages. """ now = datetime.datetime.now(pytz.UTC) tomorrow = now + datetime.timedelta(days=1) self.course.start = tomorrow self.test_course.start = tomorrow self.course = self.update_course(self.course, self.user.id) self.test_course = self.update_course(self.test_course, self.user.id) self.login(self.global_staff_user) self.enroll(self.course, True) self.enroll(self.test_course, True) # and now should be able to load both self._check_staff(self.course) self._check_staff(self.test_course) @patch.dict('lms.djangoapps.courseware.access.settings.FEATURES', {'DISABLE_START_DATES': False}) def test_enrollment_period(self): """ Check that enrollment periods work. """ # Make courses start in the future now = datetime.datetime.now(pytz.UTC) tomorrow = now + datetime.timedelta(days=1) nextday = tomorrow + datetime.timedelta(days=1) yesterday = now - datetime.timedelta(days=1) # self.course's enrollment period hasn't started self.course.enrollment_start = tomorrow self.course.enrollment_end = nextday # test_course course's has self.test_course.enrollment_start = yesterday self.test_course.enrollment_end = tomorrow self.course = self.update_course(self.course, self.user.id) self.test_course = self.update_course(self.test_course, self.user.id) # First, try with an enrolled student self.login(self.unenrolled_user) assert not self.enroll(self.course) assert self.enroll(self.test_course) # Then, try as an instructor self.logout() self.login(self.instructor_user) assert self.enroll(self.course) # Then, try as global staff self.logout() self.login(self.global_staff_user) assert self.enroll(self.course) class TestBetatesterAccess(ModuleStoreTestCase, CourseAccessTestMixin): """ Tests for the beta tester feature """ def setUp(self): super(TestBetatesterAccess, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments now = datetime.datetime.now(pytz.UTC) tomorrow = now + datetime.timedelta(days=1) self.course = CourseFactory(days_early_for_beta=2, start=tomorrow) self.content = ItemFactory(parent=self.course) self.normal_student = UserFactory() self.beta_tester = BetaTesterFactory(course_key=self.course.id) # lint-amnesty, pylint: disable=no-member @patch.dict('lms.djangoapps.courseware.access.settings.FEATURES', {'DISABLE_START_DATES': False}) def test_course_beta_period(self): """ Check that beta-test access works for courses. """ assert not self.course.has_started() # lint-amnesty, pylint: disable=no-member self.assertCannotAccessCourse(self.normal_student, 'load', self.course) self.assertCanAccessCourse(self.beta_tester, 'load', self.course) @patch.dict('lms.djangoapps.courseware.access.settings.FEATURES', {'DISABLE_START_DATES': False}) def test_content_beta_period(self): """ Check that beta-test access works for content. """ # student user shouldn't see it assert not has_access(self.normal_student, 'load', self.content, self.course.id) # lint-amnesty, pylint: disable=no-member, line-too-long # now the student should see it assert has_access(self.beta_tester, 'load', self.content, self.course.id) # lint-amnesty, pylint: disable=no-member, line-too-long
agpl-3.0
5,610,331,733,954,002,000
39.256637
146
0.638767
false
Acrisel/sequent
sequent/examples/run_progs.py
1
1069
''' Created on Sep 12, 2017 @author: arnon ''' import logging import time import os class Step(object): ''' Basic building block for sequent steps with precistency ''' def __init__(self,): pass def __call__(self, *args, **kwargs): try: self.at_start(*args, **kwargs) except Exception: raise try: _status = self.main(*args, **kwargs) except Exception: raise try: self.at_end(*args, _status=_status, **kwargs) except Exception: raise def at_start(self, *args, **kwargs): pass def main(self, *args, **kwargs): pass def at_end(self, _status, *args, **kwargs): pass def prog(progname, success=True,): logger = logging.getLogger(os.getenv("SEQUENT_LOGGER_NAME")) logger.info("doing what %s is doing." % progname) time.sleep(1) if not success: raise Exception("%s failed." % progname) return progname
mit
-6,755,559,697,855,251,000
18.796296
64
0.525725
false
grouan/udata
udata/api/fields.py
1
2440
# -*- coding: utf-8 -*- from __future__ import unicode_literals import logging from dateutil.parser import parse from flask import request, url_for from flask.ext.restplus.fields import * log = logging.getLogger(__name__) class ISODateTime(String): __schema_format__ = 'date-time' def format(self, value): if isinstance(value, basestring): value = parse(value) return value.isoformat() class Markdown(String): __schema_format__ = 'markdown' class UrlFor(String): def __init__(self, endpoint, mapper=None, **kwargs): super(UrlFor, self).__init__(**kwargs) self.endpoint = endpoint self.mapper = mapper or self.default_mapper def default_mapper(self, obj): return {'id': str(obj.id)} def output(self, key, obj): return url_for(self.endpoint, _external=True, **self.mapper(obj)) class NextPageUrl(String): def output(self, key, obj): if not obj.has_next: return None args = request.args.copy() args.update(request.view_args) args['page'] = obj.page + 1 return url_for(request.endpoint, _external=True, **args) class PreviousPageUrl(String): def output(self, key, obj): if not obj.has_prev: return None args = request.args.copy() args.update(request.view_args) args['page'] = obj.page - 1 return url_for(request.endpoint, _external=True, **args) class ImageField(String): def __init__(self, size=None, **kwargs): super(ImageField, self).__init__(**kwargs) self.size = size def format(self, field): return (field(self.size, external=True) if self.size else field(external=True)) def pager(page_fields): pager_fields = { 'data': List(Nested(page_fields), attribute='objects', description='The page data'), 'page': Integer(description='The current page', required=True, min=1), 'page_size': Integer(description='The page size used for pagination', required=True, min=0), 'total': Integer(description='The total paginated items', required=True, min=0), 'next_page': NextPageUrl(description='The next page URL if exists'), 'previous_page': PreviousPageUrl( description='The previous page URL if exists'), } return pager_fields
agpl-3.0
1,874,636,427,373,957,000
28.047619
78
0.607377
false
jamesbeebop/evennia
evennia/commands/default/system.py
1
27477
""" System commands """ import traceback import os import datetime import sys import django import twisted from time import time as timemeasure from django.conf import settings from evennia.server.sessionhandler import SESSIONS from evennia.scripts.models import ScriptDB from evennia.objects.models import ObjectDB from evennia.players.models import PlayerDB from evennia.utils import logger, utils, gametime, create, is_pypy, prettytable from evennia.utils.evtable import EvTable from evennia.utils.utils import crop from evennia.commands.default.muxcommand import MuxCommand # delayed imports _RESOURCE = None _IDMAPPER = None # limit symbol import for API __all__ = ("CmdReload", "CmdReset", "CmdShutdown", "CmdPy", "CmdScripts", "CmdObjects", "CmdService", "CmdAbout", "CmdTime", "CmdServerLoad") class CmdReload(MuxCommand): """ reload the server Usage: @reload [reason] This restarts the server. The Portal is not affected. Non-persistent scripts will survive a @reload (use @reset to purge) and at_reload() hooks will be called. """ key = "@reload" locks = "cmd:perm(reload) or perm(Immortals)" help_category = "System" def func(self): """ Reload the system. """ reason = "" if self.args: reason = "(Reason: %s) " % self.args.rstrip(".") SESSIONS.announce_all(" Server restarting %s..." % reason) SESSIONS.server.shutdown(mode='reload') class CmdReset(MuxCommand): """ reset and reboot the server Usage: @reset Notes: For normal updating you are recommended to use @reload rather than this command. Use @shutdown for a complete stop of everything. This emulates a cold reboot of the Server component of Evennia. The difference to @shutdown is that the Server will auto-reboot and that it does not affect the Portal, so no users will be disconnected. Contrary to @reload however, all shutdown hooks will be called and any non-database saved scripts, ndb-attributes, cmdsets etc will be wiped. """ key = "@reset" aliases = ['@reboot'] locks = "cmd:perm(reload) or perm(Immortals)" help_category = "System" def func(self): """ Reload the system. """ SESSIONS.announce_all(" Server resetting/restarting ...") SESSIONS.server.shutdown(mode='reset') class CmdShutdown(MuxCommand): """ stop the server completely Usage: @shutdown [announcement] Gracefully shut down both Server and Portal. """ key = "@shutdown" locks = "cmd:perm(shutdown) or perm(Immortals)" help_category = "System" def func(self): "Define function" try: # Only allow shutdown if caller has session self.caller.sessions[0] except Exception: return self.msg('Shutting down server ...') announcement = "\nServer is being SHUT DOWN!\n" if self.args: announcement += "%s\n" % self.args logger.log_infomsg('Server shutdown by %s.' % self.caller.name) SESSIONS.announce_all(announcement) SESSIONS.server.shutdown(mode='shutdown') SESSIONS.portal_shutdown() class CmdPy(MuxCommand): """ execute a snippet of python code Usage: @py <cmd> Switch: time - output an approximate execution time for <cmd> Separate multiple commands by ';'. A few variables are made available for convenience in order to offer access to the system (you can import more at execution time). Available variables in @py environment: self, me : caller here : caller.location ev : the evennia API inherits_from(obj, parent) : check object inheritance You can explore The evennia API from inside the game by calling evennia.help(), evennia.managers.help() etc. {rNote: In the wrong hands this command is a severe security risk. It should only be accessible by trusted server admins/superusers.{n """ key = "@py" aliases = ["!"] locks = "cmd:perm(py) or perm(Immortals)" help_category = "System" def func(self): "hook function" caller = self.caller pycode = self.args if not pycode: string = "Usage: @py <code>" self.msg(string) return # check if caller is a player # import useful variables import evennia available_vars = {'self': caller, 'me': caller, 'here': hasattr(caller, "location") and caller.location or None, 'evennia': evennia, 'ev': evennia, 'inherits_from': utils.inherits_from} try: self.msg(">>> %s" % pycode, raw=True, sessid=self.sessid) except TypeError: self.msg(">>> %s" % pycode, raw=True) mode = "eval" try: try: pycode_compiled = compile(pycode, "", mode) except Exception: mode = "exec" pycode_compiled = compile(pycode, "", mode) duration = "" if "time" in self.switches: t0 = timemeasure() ret = eval(pycode_compiled, {}, available_vars) t1 = timemeasure() duration = " (runtime ~ %.4f ms)" % ((t1 - t0) * 1000) else: ret = eval(pycode_compiled, {}, available_vars) if mode == "eval": ret = "<<< %s%s" % (str(ret), duration) else: ret = "<<< Done (use self.msg() if you want to catch output)%s" % duration except Exception: errlist = traceback.format_exc().split('\n') if len(errlist) > 4: errlist = errlist[4:] ret = "\n".join("<<< %s" % line for line in errlist if line) try: self.msg(ret, sessid=self.sessid, raw=True) except TypeError: self.msg(ret, raw=True) # helper function. Kept outside so it can be imported and run # by other commands. def format_script_list(scripts): "Takes a list of scripts and formats the output." if not scripts: return "<No scripts>" table = EvTable("{wdbref{n", "{wobj{n", "{wkey{n", "{wintval{n", "{wnext{n", "{wrept{n", "{wdb", "{wtypeclass{n", "{wdesc{n", align='r', border="tablecols") for script in scripts: nextrep = script.time_until_next_repeat() if nextrep is None: nextrep = "PAUS" if script.db._paused_time else "--" else: nextrep = "%ss" % nextrep maxrepeat = script.repeats if maxrepeat: rept = "%i/%i" % (maxrepeat - script.remaining_repeats(), maxrepeat) else: rept = "-/-" table.add_row(script.id, script.obj.key if (hasattr(script, 'obj') and script.obj) else "<Global>", script.key, script.interval if script.interval > 0 else "--", nextrep, rept, "*" if script.persistent else "-", script.typeclass_path.rsplit('.', 1)[-1], crop(script.desc, width=20)) return "%s" % table class CmdScripts(MuxCommand): """ list and manage all running scripts Usage: @scripts[/switches] [#dbref, key, script.path or <obj>] Switches: start - start a script (must supply a script path) stop - stops an existing script kill - kills a script - without running its cleanup hooks validate - run a validation on the script(s) If no switches are given, this command just views all active scripts. The argument can be either an object, at which point it will be searched for all scripts defined on it, or a script name or #dbref. For using the /stop switch, a unique script #dbref is required since whole classes of scripts often have the same name. Use @script for managing commands on objects. """ key = "@scripts" aliases = ["@globalscript", "@listscripts"] locks = "cmd:perm(listscripts) or perm(Wizards)" help_category = "System" def func(self): "implement method" caller = self.caller args = self.args string = "" if args: if "start" in self.switches: # global script-start mode new_script = create.create_script(args) if new_script: caller.msg("Global script %s was started successfully." % args) else: caller.msg("Global script %s could not start correctly. See logs." % args) return # test first if this is a script match scripts = ScriptDB.objects.get_all_scripts(key=args) if not scripts: # try to find an object instead. objects = ObjectDB.objects.object_search(args) if objects: scripts = [] for obj in objects: # get all scripts on the object(s) scripts.extend(ScriptDB.objects.get_all_scripts_on_obj(obj)) else: # we want all scripts. scripts = ScriptDB.objects.get_all_scripts() if not scripts: caller.msg("No scripts are running.") return if not scripts: string = "No scripts found with a key '%s', or on an object named '%s'." % (args, args) caller.msg(string) return if self.switches and self.switches[0] in ('stop', 'del', 'delete', 'kill'): # we want to delete something if not scripts: string = "No scripts/objects matching '%s'. " % args string += "Be more specific." elif len(scripts) == 1: # we have a unique match! if 'kill' in self.switches: string = "Killing script '%s'" % scripts[0].key scripts[0].stop(kill=True) else: string = "Stopping script '%s'." % scripts[0].key scripts[0].stop() #import pdb #pdb.set_trace() ScriptDB.objects.validate() #just to be sure all is synced else: # multiple matches. string = "Multiple script matches. Please refine your search:\n" string += format_script_list(scripts) elif self.switches and self.switches[0] in ("validate", "valid", "val"): # run validation on all found scripts nr_started, nr_stopped = ScriptDB.objects.validate(scripts=scripts) string = "Validated %s scripts. " % ScriptDB.objects.all().count() string += "Started %s and stopped %s scripts." % (nr_started, nr_stopped) else: # No stopping or validation. We just want to view things. string = format_script_list(scripts) caller.msg(string) class CmdObjects(MuxCommand): """ statistics on objects in the database Usage: @objects [<nr>] Gives statictics on objects in database as well as a list of <nr> latest objects in database. If not given, <nr> defaults to 10. """ key = "@objects" aliases = ["@listobjects", "@listobjs", '@stats', '@db'] locks = "cmd:perm(listobjects) or perm(Builders)" help_category = "System" def func(self): "Implement the command" caller = self.caller if self.args and self.args.isdigit(): nlim = int(self.args) else: nlim = 10 nobjs = ObjectDB.objects.count() base_char_typeclass = settings.BASE_CHARACTER_TYPECLASS nchars = ObjectDB.objects.filter(db_typeclass_path=base_char_typeclass).count() nrooms = ObjectDB.objects.filter(db_location__isnull=True).exclude(db_typeclass_path=base_char_typeclass).count() nexits = ObjectDB.objects.filter(db_location__isnull=False, db_destination__isnull=False).count() nother = nobjs - nchars - nrooms - nexits nobjs = nobjs or 1 # fix zero-div error with empty database # total object sum table totaltable = EvTable("{wtype{n", "{wcomment{n", "{wcount{n", "{w%%{n", border="table", align="l") totaltable.align = 'l' totaltable.add_row("Characters", "(BASE_CHARACTER_TYPECLASS)", nchars, "%.2f" % ((float(nchars) / nobjs) * 100)) totaltable.add_row("Rooms", "(location=None)", nrooms, "%.2f" % ((float(nrooms) / nobjs) * 100)) totaltable.add_row("Exits", "(destination!=None)", nexits, "%.2f" % ((float(nexits) / nobjs) * 100)) totaltable.add_row("Other", "", nother, "%.2f" % ((float(nother) / nobjs) * 100)) # typeclass table typetable = EvTable("{wtypeclass{n", "{wcount{n", "{w%%{n", border="table", align="l") typetable.align = 'l' dbtotals = ObjectDB.objects.object_totals() for path, count in dbtotals.items(): typetable.add_row(path, count, "%.2f" % ((float(count) / nobjs) * 100)) # last N table objs = ObjectDB.objects.all().order_by("db_date_created")[max(0, nobjs - nlim):] latesttable = EvTable("{wcreated{n", "{wdbref{n", "{wname{n", "{wtypeclass{n", align="l", border="table") latesttable.align = 'l' for obj in objs: latesttable.add_row(utils.datetime_format(obj.date_created), obj.dbref, obj.key, obj.path) string = "\n{wObject subtype totals (out of %i Objects):{n\n%s" % (nobjs, totaltable) string += "\n{wObject typeclass distribution:{n\n%s" % typetable string += "\n{wLast %s Objects created:{n\n%s" % (min(nobjs, nlim), latesttable) caller.msg(string) class CmdPlayers(MuxCommand): """ list all registered players Usage: @players [nr] Lists statistics about the Players registered with the game. It will list the <nr> amount of latest registered players If not given, <nr> defaults to 10. """ key = "@players" aliases = ["@listplayers"] locks = "cmd:perm(listplayers) or perm(Wizards)" help_category = "System" def func(self): "List the players" caller = self.caller if self.args and self.args.isdigit(): nlim = int(self.args) else: nlim = 10 nplayers = PlayerDB.objects.count() # typeclass table dbtotals = PlayerDB.objects.object_totals() typetable = EvTable("{wtypeclass{n", "{wcount{n", "{w%%{n", border="cells", align="l") for path, count in dbtotals.items(): typetable.add_row(path, count, "%.2f" % ((float(count) / nplayers) * 100)) # last N table plyrs = PlayerDB.objects.all().order_by("db_date_created")[max(0, nplayers - nlim):] latesttable = EvTable("{wcreated{n", "{wdbref{n", "{wname{n", "{wtypeclass{n", border="cells", align="l") for ply in plyrs: latesttable.add_row(utils.datetime_format(ply.date_created), ply.dbref, ply.key, ply.path) string = "\n{wPlayer typeclass distribution:{n\n%s" % typetable string += "\n{wLast %s Players created:{n\n%s" % (min(nplayers, nlim), latesttable) caller.msg(string) class CmdService(MuxCommand): """ manage system services Usage: @service[/switch] <service> Switches: list - shows all available services (default) start - activates or reactivate a service stop - stops/inactivate a service (can often be restarted) delete - tries to permanently remove a service Service management system. Allows for the listing, starting, and stopping of services. If no switches are given, services will be listed. Note that to operate on the service you have to supply the full (green or red) name as given in the list. """ key = "@service" aliases = ["@services"] locks = "cmd:perm(service) or perm(Immortals)" help_category = "System" def func(self): "Implement command" caller = self.caller switches = self.switches if switches and switches[0] not in ("list", "start", "stop", "delete"): caller.msg("Usage: @service/<list|start|stop|delete> [servicename]") return # get all services sessions = caller.sessions if not sessions: return service_collection = SESSIONS.server.services if not switches or switches[0] == "list": # Just display the list of installed services and their # status, then exit. table = prettytable.PrettyTable(["{wService{n (use @services/start|stop|delete)", "{wstatus"]) table.align = 'l' for service in service_collection.services: table.add_row([service.name, service.running and "{gRunning" or "{rNot Running"]) caller.msg(str(table)) return # Get the service to start / stop try: service = service_collection.getServiceNamed(self.args) except Exception: string = 'Invalid service name. This command is case-sensitive. ' string += 'See @service/list for valid service name (enter the full name exactly).' caller.msg(string) return if switches[0] in ("stop", "delete"): # Stopping/killing a service gracefully closes it and disconnects # any connections (if applicable). delmode = switches[0] == "delete" if not service.running: caller.msg('That service is not currently running.') return if service.name[:7] == 'Evennia': if delmode: caller.msg("You cannot remove a core Evennia service (named 'Evennia***').") return string = "You seem to be shutting down a core Evennia service (named 'Evennia***'). Note that" string += "stopping some TCP port services will *not* disconnect users *already*" string += "connected on those ports, but *may* instead cause spurious errors for them. To " string += "safely and permanently remove ports, change settings file and restart the server." caller.msg(string) if delmode: service.stopService() service_collection.removeService(service) caller.msg("Stopped and removed service '%s'." % self.args) else: service.stopService() caller.msg("Stopped service '%s'." % self.args) return if switches[0] == "start": #Starts a service. if service.running: caller.msg('That service is already running.') return caller.msg("Starting service '%s'." % self.args) service.startService() class CmdAbout(MuxCommand): """ show Evennia info Usage: @about Display info about the game engine. """ key = "@about" aliases = "@version" locks = "cmd:all()" help_category = "System" def func(self): "Show the version" string = """ {cEvennia{n %s{n MUD/MUX/MU* development system {wLicence{n BSD 3-Clause Licence {wWeb{n http://www.evennia.com {wIrc{n #evennia on FreeNode {wForum{n http://www.evennia.com/discussions {wMaintainer{n (2010-) Griatch (griatch AT gmail DOT com) {wMaintainer{n (2006-10) Greg Taylor {wOS{n %s {wPython{n %s {wTwisted{n %s {wDjango{n %s """ % (utils.get_evennia_version(), os.name, sys.version.split()[0], twisted.version.short(), django.get_version()) self.caller.msg(string) class CmdTime(MuxCommand): """ show server time statistics Usage: @time List Server time statistics such as uptime and the current time stamp. """ key = "@time" aliases = "@uptime" locks = "cmd:perm(time) or perm(Players)" help_category = "System" def func(self): "Show server time data in a table." table = prettytable.PrettyTable(["{wserver time statistic","{wtime"]) table.align = 'l' table.add_row(["Current server uptime", utils.time_format(gametime.uptime(), 3)]) table.add_row(["Total server running time", utils.time_format(gametime.runtime(), 2)]) table.add_row(["Total in-game time (realtime x %g)" % (gametime.TIMEFACTOR), utils.time_format(gametime.gametime(), 2)]) table.add_row(["Server time stamp", datetime.datetime.now()]) self.caller.msg(str(table)) class CmdServerLoad(MuxCommand): """ show server load and memory statistics Usage: @server[/mem] Switch: mem - return only a string of the current memory usage flushmem - flush the idmapper cache This command shows server load statistics and dynamic memory usage. It also allows to flush the cache of accessed database objects. Some Important statistics in the table: {wServer load{n is an average of processor usage. It's usually between 0 (no usage) and 1 (100% usage), but may also be temporarily higher if your computer has multiple CPU cores. The {wResident/Virtual memory{n displays the total memory used by the server process. Evennia {wcaches{n all retrieved database entities when they are loaded by use of the idmapper functionality. This allows Evennia to maintain the same instances of an entity and allowing non-persistent storage schemes. The total amount of cached objects are displayed plus a breakdown of database object types. The {wflushmem{n switch allows to flush the object cache. Please note that due to how Python's memory management works, releasing caches may not show you a lower Residual/Virtual memory footprint, the released memory will instead be re-used by the program. """ key = "@server" aliases = ["@serverload", "@serverprocess"] locks = "cmd:perm(list) or perm(Immortals)" help_category = "System" def func(self): "Show list." global _IDMAPPER if not _IDMAPPER: from evennia.utils.idmapper import models as _IDMAPPER if "flushmem" in self.switches: # flush the cache nflushed = _IDMAPPER.flush_cache() string = "Flushed object idmapper cache. Python garbage " \ "collector recovered memory from %i objects." self.caller(string % nflushed) return # display active processes os_windows = os.name == "nt" pid = os.getpid() if os_windows: # Windows requires the psutil module to even get paltry # statistics like this (it's pretty much worthless, # unfortunately, since it's not specific to the process) /rant try: import psutil has_psutil = True except ImportError: has_psutil = False if has_psutil: loadavg = psutil.cpu_percent() _mem = psutil.virtual_memory() rmem = _mem.used / (1000 * 1000) pmem = _mem.percent if "mem" in self.switches: string = "Total computer memory usage: {w%g{n MB (%g%%)" self.caller.msg(string % (rmem, pmem)) return # Display table loadtable = EvTable("property", "statistic", align="l") loadtable.add_row("Total CPU load", "%g %%" % loadavg) loadtable.add_row("Total computer memory usage","%g MB (%g%%)" % (rmem, pmem)) loadtable.add_row("Process ID", "%g" % pid), else: loadtable = "Not available on Windows without 'psutil' library " \ "(install with {wpip install psutil{n)." else: # Linux / BSD (OSX) - proper pid-based statistics global _RESOURCE if not _RESOURCE: import resource as _RESOURCE loadavg = os.getloadavg()[0] rmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "rss")).read()) / 1000.0 # resident memory vmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "vsz")).read()) / 1000.0 # virtual memory pmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "%mem")).read()) # percent of resident memory to total rusage = _RESOURCE.getrusage(_RESOURCE.RUSAGE_SELF) if "mem" in self.switches: string = "Memory usage: RMEM: {w%g{n MB (%g%%), " \ " VMEM (res+swap+cache): {w%g{n MB." self.caller.msg(string % (rmem, pmem, vmem)) return loadtable = EvTable("property", "statistic", align="l") loadtable.add_row("Server load (1 min)", "%g" % loadavg) loadtable.add_row("Process ID", "%g" % pid), loadtable.add_row("Memory usage","%g MB (%g%%)" % (rmem, pmem)) loadtable.add_row("Virtual address space", "") loadtable.add_row("{x(resident+swap+caching){n", "%g MB" % vmem) loadtable.add_row("CPU time used (total)", "%s (%gs)" % (utils.time_format(rusage.ru_utime), rusage.ru_utime)) loadtable.add_row("CPU time used (user)", "%s (%gs)" % (utils.time_format(rusage.ru_stime), rusage.ru_stime)) loadtable.add_row("Page faults", "%g hard, %g soft, %g swapouts" % (rusage.ru_majflt, rusage.ru_minflt, rusage.ru_nswap)) loadtable.add_row("Disk I/O", "%g reads, %g writes" % (rusage.ru_inblock, rusage.ru_oublock)) loadtable.add_row("Network I/O", "%g in, %g out" % (rusage.ru_msgrcv, rusage.ru_msgsnd)) loadtable.add_row("Context switching", "%g vol, %g forced, %g signals" % (rusage.ru_nvcsw, rusage.ru_nivcsw, rusage.ru_nsignals)) # os-generic string = "{wServer CPU and Memory load:{n\n%s" % loadtable if not is_pypy: # Cache size measurements are not available on PyPy # because it lacks sys.getsizeof # object cache size total_num, cachedict = _IDMAPPER.cache_size() sorted_cache = sorted([(key, num) for key, num in cachedict.items() if num > 0], key=lambda tup: tup[1], reverse=True) memtable = EvTable("entity name", "number", "idmapper %", align="l") for tup in sorted_cache: memtable.add_row(tup[0], "%i" % tup[1], "%.2f" % (float(tup[1]) / total_num * 100)) string += "\n{w Entity idmapper cache:{n %i items\n%s" % (total_num, memtable) # return to caller self.caller.msg(string)
bsd-3-clause
-7,030,992,218,817,748,000
35.106439
141
0.572697
false
mupif/mupif
mupif/examples/Example02-distrib/application2.py
1
3601
import sys import Pyro4 import logging sys.path.extend(['..', '../../..']) from mupif import * import mupif.Physics.PhysicalQuantities as PQ log = logging.getLogger() @Pyro4.expose class application2(Model.Model): """ Simple application that computes an arithmetical average of mapped property """ def __init__(self, metaData={}): MD = { 'Name': 'Simple application cummulating time steps', 'ID': 'N/A', 'Description': 'Cummulates time steps', 'Physics': { 'Type': 'Other', 'Entity': 'Other' }, 'Solver': { 'Software': 'Python script', 'Language': 'Python3', 'License': 'LGPL', 'Creator': 'Borek', 'Version_date': '02/2019', 'Type': 'Summator', 'Documentation': 'Nowhere', 'Estim_time_step_s': 1, 'Estim_comp_time_s': 0.01, 'Estim_execution_cost_EUR': 0.01, 'Estim_personnel_cost_EUR': 0.01, 'Required_expertise': 'None', 'Accuracy': 'High', 'Sensitivity': 'High', 'Complexity': 'Low', 'Robustness': 'High' }, 'Inputs': [ {'Type': 'mupif.Property', 'Type_ID': 'mupif.PropertyID.PID_Time_step', 'Name': 'Time step', 'Description': 'Time step', 'Units': 's', 'Origin': 'Simulated', 'Required': True}], 'Outputs': [ {'Type': 'mupif.Property', 'Type_ID': 'mupif.PropertyID.PID_Time', 'Name': 'Cummulative time', 'Description': 'Cummulative time', 'Units': 's', 'Origin': 'Simulated'}] } super(application2, self).__init__(metaData=MD) self.updateMetadata(metaData) self.value = 0.0 self.count = 0.0 self.contrib = Property.ConstantProperty( (0.,), PropertyID.PID_Time, ValueType.Scalar, 's', PQ.PhysicalQuantity(0., 's')) def initialize(self, file='', workdir='', metaData={}, validateMetaData=True, **kwargs): super(application2, self).initialize(file, workdir, metaData, validateMetaData, **kwargs) def getProperty(self, propID, time, objectID=0): md = { 'Execution': { 'ID': self.getMetadata('Execution.ID'), 'Use_case_ID': self.getMetadata('Execution.Use_case_ID'), 'Task_ID': self.getMetadata('Execution.Task_ID') } } if propID == PropertyID.PID_Time: return Property.ConstantProperty( (self.value,), PropertyID.PID_Time, ValueType.Scalar, 's', time, metaData=md) else: raise APIError.APIError('Unknown property ID') def setProperty(self, property, objectID=0): if property.getPropertyID() == PropertyID.PID_Time_step: # remember the mapped value self.contrib = property else: raise APIError.APIError('Unknown property ID') def solveStep(self, tstep, stageID=0, runInBackground=False): # here we actually accumulate the value using value of mapped property self.value = self.value+self.contrib.inUnitsOf('s').getValue(tstep.getTime())[0] self.count = self.count+1 def getCriticalTimeStep(self): return PQ.PhysicalQuantity(1.0, 's') def getAssemblyTime(self, tstep): return tstep.getTime() def getApplicationSignature(self): return "Application2"
lgpl-3.0
-1,283,888,354,917,432,800
36.905263
110
0.544016
false
CiscoDevNet/netconf-examples
netconf-102/get_config_csr1000V.py
1
1234
#!/usr/bin/python # # Get configured interfaces using Netconf # # darien@sdnessentials.com # from ncclient import manager import sys import xml.dom.minidom # the variables below assume the user is requesting access # to a IOS-XE device running in the DevNet Always On SandBox # use the IP address or hostname of your IOS-XE device HOST = 'ios-xe-mgmt.cisco.com' # use the NETCONF port for your IOS-XE device PORT = 10000 # use the user credentials for your IOS-XE device USER = 'root' PASS = 'C!sc0123' # XML file to open FILE = 'get_interfaces.xml' # create a main() method def get_configured_interfaces(): """Main method that retrieves the interfaces from config via NETCONF.""" with manager.connect(host=HOST, port=PORT, username=USER, password=PASS, hostkey_verify=False, device_params={'name': 'default'}, allow_agent=False, look_for_keys=False) as m: with open(FILE) as f: return(m.get_config('running', f.read())) def main(): """Simple main method calling our function.""" interfaces = get_configured_interfaces() print(xml.dom.minidom.parseString(interfaces.xml).toprettyxml()) if __name__ == '__main__': sys.exit(main())
apache-2.0
4,805,675,626,811,635,000
27.045455
81
0.679092
false