ext
stringclasses
9 values
sha
stringlengths
40
40
content
stringlengths
3
1.04M
py
1a4b4162b5d44ddfa8cbdaf34b3177735085dc69
import tensorflow as tf import numpy as np from util import * def init_random_seeds(): tf.set_random_seed(config.random_seed.tensorflow) np.random.seed(config.random_seed.numpy)
py
1a4b4225068b8754c041fda8129c12aed21a7a59
# Kata link : https://www.codewars.com/kata/523a86aa4230ebb5420001e1 def anagrams(word, words): fin = [] for i in words: if sorted(i) == sorted(word): fin.append(i) return fin #print(anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada'])) #anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada'])
py
1a4b42cf93b998925f5d3d13ca695d51de3665e4
# Copyright (c) 2006-2016 Andrey Golovizin # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. from pybtex.style.formatting.unsrt import Style as UnsrtStyle class Style(UnsrtStyle): default_label_style = 'alpha'
py
1a4b4359ca789200311509c92e85a24d8cf4aade
from lixian_commands.util import * from lixian_cli_parser import * from lixian_config import get_config import lixian_help import lixian_query @command_line_parser(help=lixian_help.add) @with_parser(parse_login) @with_parser(parse_colors) @with_parser(parse_logging) @with_parser(parse_size) @command_line_value('input', alias='i') @command_line_option('torrent', alias='bt') def add_task(args): assert len(args) or args.input client = create_client(args) tasks = lixian_query.find_tasks_to_download(client, args) print 'All tasks added. Checking status...' columns = ['id', 'status', 'name'] if get_config('n'): columns.insert(0, 'n') if args.size: columns.append('size') output_tasks(tasks, columns, args)
py
1a4b437255117d77b4400a1dfe0a6ac351459191
import cv2 from CameraOrMarker import * from scipy.linalg import sqrtm class SolvePnpInputs: def __init__(self, camera_not_marker, corners_f_images, t_world_xxx_cvecs): self.camera_not_marker = camera_not_marker # t_world_xxx_cvecs are for a camera self.corners_f_images = corners_f_images self.t_world_xxx_cvecs = t_world_xxx_cvecs class CameraMarkerCalculations: def __init__(self, scenario): self.sc = scenario # Constants for Sigma Point generation self.n0 = 8 # 8 coordinates of the corner points self.n1 = 14 # 8 + 6 self.alpha = 1.0 self.betta = 2.0 self.k0 = 3.0 - self.n0 self.k1 = 3.0 - self.n1 self.lam0 = self.alpha ** 2 * (self.n0 + self.k0) - self.n0 self.lam1 = self.alpha ** 2 * (self.n1 + self.k1) - self.n1 self.gamma0 = np.sqrt(self.n0 + self.lam0) self.gamma1 = np.sqrt(self.n1 + self.lam1) def _get_corners_f_marker(self): m2 = self.sc.marker_len / 2.0 marker_corners_f_marker = np.array([[-m2, m2, 0.], [m2, m2, 0.], [m2, -m2, 0.], [-m2, -m2, 0.]]).T return marker_corners_f_marker def _project_points(self, camera, marker): # get corners in the marker frame corners_f_marker = self._get_corners_f_marker().T # get the transforms t_world_camera = camera.t_world_xxx t_world_marker = marker.t_world_xxx # calculate rvec, tvec from t_camera_marker t_camera_marker = t_world_camera.as_inverse() \ .as_right_combined(t_world_marker) rvec, tvec = t_camera_marker.as_rvec_tvec() # project the points using t_camera_marker corners_f_image, _ = cv2.projectPoints(corners_f_marker, rvec, tvec, self.sc.camera_matrix, self.sc.dist_coeffs) return corners_f_image.reshape(8, 1) def generate_corners_f_image(self, camera, marker): assert camera.camera_not_marker assert not marker.camera_not_marker return self._project_points(camera, marker) def _generate_sigma_points(self, com, corners_f_image): # TODO figure out real sigma points. com_cvec = com.t_world_xxx.as_cvec() gamma = self.gamma0 if com.simulated_not_derived else self.gamma1 corners_f_images = np.zeros([8, 17] if com.simulated_not_derived else [8, 29]) t_world_xxx_cvecs = np.zeros([6, 17] if com.simulated_not_derived else [6, 29]) var = self.sc.std_corners_f_image f = corners_f_image * np.ones([8, 17]) f[:, 1:9] += np.eye(8) * var * gamma f[:, 9:17] -= np.eye(8) * var * gamma corners_f_images[:, :17] = f t_world_xxx_cvecs[:, :17] = com_cvec * np.ones([6, 17]) if not com.simulated_not_derived: s = sqrtm(com.cov) # var = np.sqrt(np.diag(com.cov).reshape(6, 1)) # var = np.diag(com.cov).reshape(6, 1) f = com_cvec * np.ones([6, 12]) f[:, :6] += s * gamma f[:, 6:12] -= s * gamma corners_f_images[:, 17:29] = corners_f_image * np.ones([8, 12]) t_world_xxx_cvecs[:, 17:29] = f return SolvePnpInputs(com.camera_not_marker, corners_f_images, t_world_xxx_cvecs) def generate_solve_pnp_inputs(self, camera_not_marker, camera, marker): assert camera.camera_not_marker assert not marker.camera_not_marker com = camera if camera_not_marker else marker # Get corners using the poses of the camera and marker corners_f_image = self._project_points(camera, marker) if self.sc.use_sigma_points: return self._generate_sigma_points(com, corners_f_image) # Make many of these corner bundles corners_f_images = corners_f_image + \ np.random.normal(0., self.sc.std_corners_f_image ** 2, size=[8, self.sc.num_generated_samples]) # Now get many cvecs if com.simulated_not_derived: t_world_xxx_cvec = com.t_world_xxx.as_cvec() t_world_xxx_cvecs = np.tile(t_world_xxx_cvec, (1, self.sc.num_generated_samples)) elif self.sc.use_dist_params: t_world_xxx_cvecs = np.random.multivariate_normal(com.mu[:, 0], com.cov, self.sc.num_generated_samples).T else: assert com.samples is not None t_world_xxx_cvecs = com.samples return SolvePnpInputs(camera_not_marker, corners_f_images, t_world_xxx_cvecs) def solve_pnp(self, inputs): t_world_xxx_cvecs = np.zeros(inputs.t_world_xxx_cvecs.shape) # get corners in the marker frame corners_f_marker = self._get_corners_f_marker().T for i in range(t_world_xxx_cvecs.shape[1]): # Given the location of the corners in the image, find the pose of the marker in the camera frame. ret, rvecs, tvecs = cv2.solvePnP(corners_f_marker, inputs.corners_f_images[:, i].reshape(4, 2), self.sc.camera_matrix, self.sc.dist_coeffs) t_camera_marker = tf.Transformation.from_rodrigues(rvecs[:, 0], translation=tvecs[:, 0]) input_t_world_xxx = tf.Transformation.from_cvec(inputs.t_world_xxx_cvecs[:, i]) t_camera_marker_factor = t_camera_marker if not inputs.camera_not_marker: t_camera_marker_factor = t_camera_marker_factor.as_inverse() output_t_world_xxx = input_t_world_xxx.as_right_combined(t_camera_marker_factor) t_world_xxx_cvecs[:, i] = output_t_world_xxx.as_cvec().T mu = np.mean(t_world_xxx_cvecs, axis=1).reshape(6, 1) cov = np.cov(t_world_xxx_cvecs) return CameraOrMarker.from_mu(not inputs.camera_not_marker, mu, cov, t_world_xxx_cvecs)
py
1a4b43d128a487f1c9af483f6f88b61b18a31aba
# -*- coding: utf-8 -*- # Copyright (C) 2017 - 2018 by Pedro Mendes, Virginia Tech Intellectual # Properties, Inc., University of Heidelberg, and University of # of Connecticut School of Medicine. # All rights reserved. # Copyright (C) 2010 - 2016 by Pedro Mendes, Virginia Tech Intellectual # Properties, Inc., University of Heidelberg, and The University # of Manchester. # All rights reserved. # Copyright (C) 2009 by Pedro Mendes, Virginia Tech Intellectual # Properties, Inc., EML Research, gGmbH, University of Heidelberg, # and The University of Manchester. # All rights reserved. # This is an example on how to import an sbml file # create a report for a time course simulation # and run a time course simulation # from COPASI import * import sys # create a datamodel try: dataModel = CRootContainer.addDatamodel() except: dataModel = CCopasiRootContainer.addDatamodel() def main(args): # the only argument to the main routine should be the name of an SBML file if len(args) != 1: sys.stderr.write("Usage: example3 SBMLFILE\n") return 1; filename = args[0] try: # load the model if not dataModel.importSBML(filename): print("Couldn't load {0}:".format(filename)) print(CCopasiMessage.getAllMessageText()) except: sys.stderr.write("Error while importing the model from file named \"" + filename + "\".\n") return 1 model = dataModel.getModel() assert model is not None # get the trajectory task object trajectoryTask = dataModel.getTask("Time-Course") assert (isinstance(trajectoryTask, CTrajectoryTask)) # run a deterministic time course trajectoryTask.setMethodType(CTaskEnum.Method_deterministic) # activate the task so that it will be run when the model is saved # and passed to CopasiSE trajectoryTask.setScheduled(True) # create a new report that captures the time course result report = create_report(model) # set the report for the task trajectoryTask.getReport().setReportDefinition(report) # set the output filename trajectoryTask.getReport().setTarget("example3.txt") # don't append output if the file exists, but overwrite the file trajectoryTask.getReport().setAppend(False) # get the problem for the task to set some parameters problem = trajectoryTask.getProblem() assert (isinstance(problem, CTrajectoryProblem)) # simulate 100 steps problem.setStepNumber(100) # start at time 0 dataModel.getModel().setInitialTime(0.0) # simulate a duration of 10 time units problem.setDuration(10) # tell the problem to actually generate time series data problem.setTimeSeriesRequested(True) # tell the problem, that we want exactly 100 simulation steps (not automatically controlled) problem.setAutomaticStepSize(False) # tell the problem, that we don't want additional output points for event assignments problem.setOutputEvent(False) # set some parameters for the LSODA method through the method method = trajectoryTask.getMethod() parameter = method.getParameter("Absolute Tolerance") assert parameter is not None assert parameter.getType() == CCopasiParameter.Type_UDOUBLE parameter.setValue(1.0e-12) try: # now we run the actual trajectory result=trajectoryTask.process(True) except: sys.stderr.write("Error. Running the time course simulation failed.\n") # check if there are additional error messages if CCopasiMessage.size() > 0: # print the messages in chronological order sys.stderr.write(CCopasiMessage.getAllMessageText(True)) return 1 if not result: sys.stderr.write("Error. Running the time course simulation failed.\n" ) # check if there are additional error messages if CCopasiMessage.size() > 0: # print the messages in chronological order sys.stderr.write(CCopasiMessage.getAllMessageText(True)) return 1 # look at the timeseries print_results(trajectoryTask) def print_results(trajectoryTask): timeSeries = trajectoryTask.getTimeSeries() # we simulated 100 steps, including the initial state, this should be # 101 step in the timeseries assert timeSeries.getRecordedSteps() == 101 print ("The time series consists of {0} steps.".format(timeSeries.getRecordedSteps())) print ("Each step contains {0} variables.".format(timeSeries.getNumVariables())) print ("\nThe final state is: ") iMax = timeSeries.getNumVariables() lastIndex = timeSeries.getRecordedSteps() - 1 for i in range(0, iMax): # here we get the particle number (at least for the species) # the unit of the other variables may not be particle numbers # the concentration data can be acquired with getConcentrationData print (" {0}: {1}".format(timeSeries.getTitle(i), timeSeries.getData(lastIndex, i))) # the CTimeSeries class now has some new methods to get all variable titles # as a python list (getTitles()) # and methods to get the complete time course data for a certain variable based on # the variables index or the corresponding model object. # E.g. to get the particle numbers of the second variable as a python list # you can use getDataForIndex(1) and to get the concentration data you use # getConcentrationDataForIndex(1) # To get the complete particle number data for the second metabolite of the model # you can use getDataForObject(model.getMetabolite(1)) and to get the concentration # data you use getConcentrationDataForObject. # print timeSeries.getTitles() # print timeSeries.getDataForIndex(1) # print timeSeries.getDataForObject(model) def create_report(model): # create a report with the correct filename and all the species against # time. reports = dataModel.getReportDefinitionList() # create a report definition object report = reports.createReportDefinition("Report", "Output for timecourse") # set the task type for the report definition to timecourse report.setTaskType(CTaskEnum.Task_timeCourse) # we don't want a table report.setIsTable(False) # the entries in the output should be separated by a ", " report.setSeparator(CCopasiReportSeparator(", ")) # we need a handle to the header and the body # the header will display the ids of the metabolites and "time" for # the first column # the body will contain the actual timecourse data header = report.getHeaderAddr() body = report.getBodyAddr() body.push_back( CRegisteredCommonName(CCommonName(dataModel.getModel().getCN().getString() + ",Reference=Time").getString())) body.push_back(CRegisteredCommonName(report.getSeparator().getCN().getString())) header.push_back(CRegisteredCommonName(CDataString("time").getCN().getString())) header.push_back(CRegisteredCommonName(report.getSeparator().getCN().getString())) iMax = model.getMetabolites().size() for i in range(0, iMax): metab = model.getMetabolite(i) assert metab is not None # we don't want output for FIXED metabolites right now if metab.getStatus() != CModelEntity.Status_FIXED: # we want the concentration oin the output # alternatively, we could use "Reference=Amount" to get the # particle number body.push_back( CRegisteredCommonName(metab.getObject(CCommonName("Reference=Concentration")).getCN().getString())) # add the corresponding id to the header header.push_back(CRegisteredCommonName(CDataString(metab.getSBMLId()).getCN().getString())) # after each entry, we need a separator if i != iMax - 1: body.push_back(CRegisteredCommonName(report.getSeparator().getCN().getString())) header.push_back(CRegisteredCommonName(report.getSeparator().getCN().getString())) return report if __name__ == '__main__': main(sys.argv[1:])
py
1a4b43e81e9dbd620cb477568ab38314117b00d9
from ..constants import _file_to_fh from ..functions import open_files_threshold_exceeded, close_one_file #, abspath from .umread.umfile import File, UMFileException _file_to_UM = _file_to_fh.setdefault('UM', {}) def _open_um_file(filename, aggregate=True, fmt=None, word_size=None, byte_ordering=None): '''Open a UM fields file or PP file and read it into a `umread.umfile.File` object. If there is already a `umread.umfile.File` object for the file then it is returned with an open file descriptor. :Parameters: filename : str The file to be opened. :Returns: out : umread.umfile.File The opened file with an open file descriptor. :Examples: ''' # filename = abspath(filename) f = _file_to_UM.get(filename) if f is not None: if f.fd is None: if open_files_threshold_exceeded(): # Close a random data array file to make way for this # one close_one_file() f.open_fd() #--- End: if return f if open_files_threshold_exceeded(): # Close a random data array file to make way for this one close_one_file() try: f = File(filename, byte_ordering=byte_ordering, word_size=word_size, format=fmt) except Exception as error: try: f.close_fd() except: pass raise Exception(error) # Add a close method to the file object f.close = f.close_fd # Update the _file_to_UM dictionary _file_to_UM[filename] = f return f #--- End: def def _close_um_file(filename): '''Close a PP or UM fields file. Does nothing if the file is already closed. :Parameters: filename : str The file to be closed. :Returns: None :Examples: ''' f = _file_to_UM.pop(filename, None) if f is not None: f.close_fd() #--- End: def
py
1a4b44188bd418a53299e74e933288cffc9842e6
# pysqlite2/test/dbapi.py: tests for DB-API compliance # # Copyright (C) 2004-2010 Gerhard Häring <[email protected]> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. import contextlib import sqlite3 as sqlite import sys import threading import unittest from test.support import check_disallow_instantiation, threading_helper from test.support.os_helper import TESTFN, unlink # Helper for tests using TESTFN @contextlib.contextmanager def managed_connect(*args, **kwargs): cx = sqlite.connect(*args, **kwargs) try: yield cx finally: cx.close() unlink(TESTFN) class ModuleTests(unittest.TestCase): def test_api_level(self): self.assertEqual(sqlite.apilevel, "2.0", "apilevel is %s, should be 2.0" % sqlite.apilevel) def test_thread_safety(self): self.assertEqual(sqlite.threadsafety, 1, "threadsafety is %d, should be 1" % sqlite.threadsafety) def test_param_style(self): self.assertEqual(sqlite.paramstyle, "qmark", "paramstyle is '%s', should be 'qmark'" % sqlite.paramstyle) def test_warning(self): self.assertTrue(issubclass(sqlite.Warning, Exception), "Warning is not a subclass of Exception") def test_error(self): self.assertTrue(issubclass(sqlite.Error, Exception), "Error is not a subclass of Exception") def test_interface_error(self): self.assertTrue(issubclass(sqlite.InterfaceError, sqlite.Error), "InterfaceError is not a subclass of Error") def test_database_error(self): self.assertTrue(issubclass(sqlite.DatabaseError, sqlite.Error), "DatabaseError is not a subclass of Error") def test_data_error(self): self.assertTrue(issubclass(sqlite.DataError, sqlite.DatabaseError), "DataError is not a subclass of DatabaseError") def test_operational_error(self): self.assertTrue(issubclass(sqlite.OperationalError, sqlite.DatabaseError), "OperationalError is not a subclass of DatabaseError") def test_integrity_error(self): self.assertTrue(issubclass(sqlite.IntegrityError, sqlite.DatabaseError), "IntegrityError is not a subclass of DatabaseError") def test_internal_error(self): self.assertTrue(issubclass(sqlite.InternalError, sqlite.DatabaseError), "InternalError is not a subclass of DatabaseError") def test_programming_error(self): self.assertTrue(issubclass(sqlite.ProgrammingError, sqlite.DatabaseError), "ProgrammingError is not a subclass of DatabaseError") def test_not_supported_error(self): self.assertTrue(issubclass(sqlite.NotSupportedError, sqlite.DatabaseError), "NotSupportedError is not a subclass of DatabaseError") # sqlite3_enable_shared_cache() is deprecated on macOS and calling it may raise # OperationalError on some buildbots. @unittest.skipIf(sys.platform == "darwin", "shared cache is deprecated on macOS") def test_shared_cache_deprecated(self): for enable in (True, False): with self.assertWarns(DeprecationWarning) as cm: sqlite.enable_shared_cache(enable) self.assertIn("dbapi.py", cm.filename) def test_disallow_instantiation(self): cx = sqlite.connect(":memory:") check_disallow_instantiation(self, type(cx("select 1"))) def test_complete_statement(self): self.assertFalse(sqlite.complete_statement("select t")) self.assertTrue(sqlite.complete_statement("create table t(t);")) class ConnectionTests(unittest.TestCase): def setUp(self): self.cx = sqlite.connect(":memory:") cu = self.cx.cursor() cu.execute("create table test(id integer primary key, name text)") cu.execute("insert into test(name) values (?)", ("foo",)) def tearDown(self): self.cx.close() def test_commit(self): self.cx.commit() def test_commit_after_no_changes(self): """ A commit should also work when no changes were made to the database. """ self.cx.commit() self.cx.commit() def test_rollback(self): self.cx.rollback() def test_rollback_after_no_changes(self): """ A rollback should also work when no changes were made to the database. """ self.cx.rollback() self.cx.rollback() def test_cursor(self): cu = self.cx.cursor() def test_failed_open(self): YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db" with self.assertRaises(sqlite.OperationalError): con = sqlite.connect(YOU_CANNOT_OPEN_THIS) def test_close(self): self.cx.close() def test_use_after_close(self): sql = "select 1" cu = self.cx.cursor() res = cu.execute(sql) self.cx.close() self.assertRaises(sqlite.ProgrammingError, res.fetchall) self.assertRaises(sqlite.ProgrammingError, cu.execute, sql) self.assertRaises(sqlite.ProgrammingError, cu.executemany, sql, []) self.assertRaises(sqlite.ProgrammingError, cu.executescript, sql) self.assertRaises(sqlite.ProgrammingError, self.cx.execute, sql) self.assertRaises(sqlite.ProgrammingError, self.cx.executemany, sql, []) self.assertRaises(sqlite.ProgrammingError, self.cx.executescript, sql) self.assertRaises(sqlite.ProgrammingError, self.cx.create_function, "t", 1, lambda x: x) self.assertRaises(sqlite.ProgrammingError, self.cx.cursor) with self.assertRaises(sqlite.ProgrammingError): with self.cx: pass def test_exceptions(self): # Optional DB-API extension. self.assertEqual(self.cx.Warning, sqlite.Warning) self.assertEqual(self.cx.Error, sqlite.Error) self.assertEqual(self.cx.InterfaceError, sqlite.InterfaceError) self.assertEqual(self.cx.DatabaseError, sqlite.DatabaseError) self.assertEqual(self.cx.DataError, sqlite.DataError) self.assertEqual(self.cx.OperationalError, sqlite.OperationalError) self.assertEqual(self.cx.IntegrityError, sqlite.IntegrityError) self.assertEqual(self.cx.InternalError, sqlite.InternalError) self.assertEqual(self.cx.ProgrammingError, sqlite.ProgrammingError) self.assertEqual(self.cx.NotSupportedError, sqlite.NotSupportedError) def test_in_transaction(self): # Can't use db from setUp because we want to test initial state. cx = sqlite.connect(":memory:") cu = cx.cursor() self.assertEqual(cx.in_transaction, False) cu.execute("create table transactiontest(id integer primary key, name text)") self.assertEqual(cx.in_transaction, False) cu.execute("insert into transactiontest(name) values (?)", ("foo",)) self.assertEqual(cx.in_transaction, True) cu.execute("select name from transactiontest where name=?", ["foo"]) row = cu.fetchone() self.assertEqual(cx.in_transaction, True) cx.commit() self.assertEqual(cx.in_transaction, False) cu.execute("select name from transactiontest where name=?", ["foo"]) row = cu.fetchone() self.assertEqual(cx.in_transaction, False) def test_in_transaction_ro(self): with self.assertRaises(AttributeError): self.cx.in_transaction = True def test_connection_exceptions(self): exceptions = [ "DataError", "DatabaseError", "Error", "IntegrityError", "InterfaceError", "NotSupportedError", "OperationalError", "ProgrammingError", "Warning", ] for exc in exceptions: with self.subTest(exc=exc): self.assertTrue(hasattr(self.cx, exc)) self.assertIs(getattr(sqlite, exc), getattr(self.cx, exc)) def test_interrupt_on_closed_db(self): cx = sqlite.connect(":memory:") cx.close() with self.assertRaises(sqlite.ProgrammingError): cx.interrupt() def test_interrupt(self): self.assertIsNone(self.cx.interrupt()) def test_drop_unused_refs(self): for n in range(500): cu = self.cx.execute(f"select {n}") self.assertEqual(cu.fetchone()[0], n) class OpenTests(unittest.TestCase): _sql = "create table test(id integer)" def test_open_with_path_like_object(self): """ Checks that we can successfully connect to a database using an object that is PathLike, i.e. has __fspath__(). """ class Path: def __fspath__(self): return TESTFN path = Path() with managed_connect(path) as cx: cx.execute(self._sql) def test_open_uri(self): with managed_connect(TESTFN) as cx: cx.execute(self._sql) with managed_connect(f"file:{TESTFN}", uri=True) as cx: cx.execute(self._sql) with self.assertRaises(sqlite.OperationalError): with managed_connect(f"file:{TESTFN}?mode=ro", uri=True) as cx: cx.execute(self._sql) def test_database_keyword(self): with sqlite.connect(database=":memory:") as cx: self.assertEqual(type(cx), sqlite.Connection) class CursorTests(unittest.TestCase): def setUp(self): self.cx = sqlite.connect(":memory:") self.cu = self.cx.cursor() self.cu.execute( "create table test(id integer primary key, name text, " "income number, unique_test text unique)" ) self.cu.execute("insert into test(name) values (?)", ("foo",)) def tearDown(self): self.cu.close() self.cx.close() def test_execute_no_args(self): self.cu.execute("delete from test") def test_execute_illegal_sql(self): with self.assertRaises(sqlite.OperationalError): self.cu.execute("select asdf") def test_execute_too_much_sql(self): with self.assertRaises(sqlite.Warning): self.cu.execute("select 5+4; select 4+5") def test_execute_too_much_sql2(self): self.cu.execute("select 5+4; -- foo bar") def test_execute_too_much_sql3(self): self.cu.execute(""" select 5+4; /* foo */ """) def test_execute_wrong_sql_arg(self): with self.assertRaises(TypeError): self.cu.execute(42) def test_execute_arg_int(self): self.cu.execute("insert into test(id) values (?)", (42,)) def test_execute_arg_float(self): self.cu.execute("insert into test(income) values (?)", (2500.32,)) def test_execute_arg_string(self): self.cu.execute("insert into test(name) values (?)", ("Hugo",)) def test_execute_arg_string_with_zero_byte(self): self.cu.execute("insert into test(name) values (?)", ("Hu\x00go",)) self.cu.execute("select name from test where id=?", (self.cu.lastrowid,)) row = self.cu.fetchone() self.assertEqual(row[0], "Hu\x00go") def test_execute_non_iterable(self): with self.assertRaises(ValueError) as cm: self.cu.execute("insert into test(id) values (?)", 42) self.assertEqual(str(cm.exception), 'parameters are of unsupported type') def test_execute_wrong_no_of_args1(self): # too many parameters with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)", (17, "Egon")) def test_execute_wrong_no_of_args2(self): # too little parameters with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)") def test_execute_wrong_no_of_args3(self): # no parameters, parameters are needed with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)") def test_execute_param_list(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=?", ["foo"]) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_param_sequence(self): class L: def __len__(self): return 1 def __getitem__(self, x): assert x == 0 return "foo" self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=?", L()) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_param_sequence_bad_len(self): # Issue41662: Error in __len__() was overridden with ProgrammingError. class L: def __len__(self): 1/0 def __getitem__(slf, x): raise AssertionError self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(ZeroDivisionError): self.cu.execute("select name from test where name=?", L()) def test_execute_dict_mapping(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=:name", {"name": "foo"}) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_dict_mapping_mapping(self): class D(dict): def __missing__(self, key): return "foo" self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=:name", D()) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_dict_mapping_too_little_args(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"}) def test_execute_dict_mapping_no_args(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=:name") def test_execute_dict_mapping_unnamed(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=?", {"name": "foo"}) def test_close(self): self.cu.close() def test_rowcount_execute(self): self.cu.execute("delete from test") self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("update test set name='bar'") self.assertEqual(self.cu.rowcount, 2) def test_rowcount_select(self): """ pysqlite does not know the rowcount of SELECT statements, because we don't fetch all rows after executing the select statement. The rowcount has thus to be -1. """ self.cu.execute("select 5 union select 6") self.assertEqual(self.cu.rowcount, -1) def test_rowcount_executemany(self): self.cu.execute("delete from test") self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)]) self.assertEqual(self.cu.rowcount, 3) def test_total_changes(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("insert into test(name) values ('foo')") self.assertLess(2, self.cx.total_changes, msg='total changes reported wrong value') # Checks for executemany: # Sequences are required by the DB-API, iterators # enhancements in pysqlite. def test_execute_many_sequence(self): self.cu.executemany("insert into test(income) values (?)", [(x,) for x in range(100, 110)]) def test_execute_many_iterator(self): class MyIter: def __init__(self): self.value = 5 def __next__(self): if self.value == 10: raise StopIteration else: self.value += 1 return (self.value,) self.cu.executemany("insert into test(income) values (?)", MyIter()) def test_execute_many_generator(self): def mygen(): for i in range(5): yield (i,) self.cu.executemany("insert into test(income) values (?)", mygen()) def test_execute_many_wrong_sql_arg(self): with self.assertRaises(TypeError): self.cu.executemany(42, [(3,)]) def test_execute_many_select(self): with self.assertRaises(sqlite.ProgrammingError): self.cu.executemany("select ?", [(3,)]) def test_execute_many_not_iterable(self): with self.assertRaises(TypeError): self.cu.executemany("insert into test(income) values (?)", 42) def test_fetch_iter(self): # Optional DB-API extension. self.cu.execute("delete from test") self.cu.execute("insert into test(id) values (?)", (5,)) self.cu.execute("insert into test(id) values (?)", (6,)) self.cu.execute("select id from test order by id") lst = [] for row in self.cu: lst.append(row[0]) self.assertEqual(lst[0], 5) self.assertEqual(lst[1], 6) def test_fetchone(self): self.cu.execute("select name from test") row = self.cu.fetchone() self.assertEqual(row[0], "foo") row = self.cu.fetchone() self.assertEqual(row, None) def test_fetchone_no_statement(self): cur = self.cx.cursor() row = cur.fetchone() self.assertEqual(row, None) def test_array_size(self): # must default ot 1 self.assertEqual(self.cu.arraysize, 1) # now set to 2 self.cu.arraysize = 2 # now make the query return 3 rows self.cu.execute("delete from test") self.cu.execute("insert into test(name) values ('A')") self.cu.execute("insert into test(name) values ('B')") self.cu.execute("insert into test(name) values ('C')") self.cu.execute("select name from test") res = self.cu.fetchmany() self.assertEqual(len(res), 2) def test_fetchmany(self): self.cu.execute("select name from test") res = self.cu.fetchmany(100) self.assertEqual(len(res), 1) res = self.cu.fetchmany(100) self.assertEqual(res, []) def test_fetchmany_kw_arg(self): """Checks if fetchmany works with keyword arguments""" self.cu.execute("select name from test") res = self.cu.fetchmany(size=100) self.assertEqual(len(res), 1) def test_fetchall(self): self.cu.execute("select name from test") res = self.cu.fetchall() self.assertEqual(len(res), 1) res = self.cu.fetchall() self.assertEqual(res, []) def test_setinputsizes(self): self.cu.setinputsizes([3, 4, 5]) def test_setoutputsize(self): self.cu.setoutputsize(5, 0) def test_setoutputsize_no_column(self): self.cu.setoutputsize(42) def test_cursor_connection(self): # Optional DB-API extension. self.assertEqual(self.cu.connection, self.cx) def test_wrong_cursor_callable(self): with self.assertRaises(TypeError): def f(): pass cur = self.cx.cursor(f) def test_cursor_wrong_class(self): class Foo: pass foo = Foo() with self.assertRaises(TypeError): cur = sqlite.Cursor(foo) def test_last_row_id_on_replace(self): """ INSERT OR REPLACE and REPLACE INTO should produce the same behavior. """ sql = '{} INTO test(id, unique_test) VALUES (?, ?)' for statement in ('INSERT OR REPLACE', 'REPLACE'): with self.subTest(statement=statement): self.cu.execute(sql.format(statement), (1, 'foo')) self.assertEqual(self.cu.lastrowid, 1) def test_last_row_id_on_ignore(self): self.cu.execute( "insert or ignore into test(unique_test) values (?)", ('test',)) self.assertEqual(self.cu.lastrowid, 2) self.cu.execute( "insert or ignore into test(unique_test) values (?)", ('test',)) self.assertEqual(self.cu.lastrowid, 2) def test_last_row_id_insert_o_r(self): results = [] for statement in ('FAIL', 'ABORT', 'ROLLBACK'): sql = 'INSERT OR {} INTO test(unique_test) VALUES (?)' with self.subTest(statement='INSERT OR {}'.format(statement)): self.cu.execute(sql.format(statement), (statement,)) results.append((statement, self.cu.lastrowid)) with self.assertRaises(sqlite.IntegrityError): self.cu.execute(sql.format(statement), (statement,)) results.append((statement, self.cu.lastrowid)) expected = [ ('FAIL', 2), ('FAIL', 2), ('ABORT', 3), ('ABORT', 3), ('ROLLBACK', 4), ('ROLLBACK', 4), ] self.assertEqual(results, expected) def test_column_count(self): # Check that column count is updated correctly for cached statements select = "select * from test" res = self.cu.execute(select) old_count = len(res.description) # Add a new column and execute the cached select query again self.cu.execute("alter table test add newcol") res = self.cu.execute(select) new_count = len(res.description) self.assertEqual(new_count - old_count, 1) def test_same_query_in_multiple_cursors(self): cursors = [self.cx.execute("select 1") for _ in range(3)] for cu in cursors: self.assertEqual(cu.fetchall(), [(1,)]) class ThreadTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") self.cur = self.con.cursor() self.cur.execute("create table test(name text)") def tearDown(self): self.cur.close() self.con.close() @threading_helper.reap_threads def _run_test(self, fn, *args, **kwds): def run(err): try: fn(*args, **kwds) err.append("did not raise ProgrammingError") except sqlite.ProgrammingError: pass except: err.append("raised wrong exception") err = [] t = threading.Thread(target=run, kwargs={"err": err}) t.start() t.join() if err: self.fail("\n".join(err)) def test_check_connection_thread(self): fns = [ lambda: self.con.cursor(), lambda: self.con.commit(), lambda: self.con.rollback(), lambda: self.con.close(), lambda: self.con.set_trace_callback(None), lambda: self.con.create_collation("foo", None), ] for fn in fns: with self.subTest(fn=fn): self._run_test(fn) def test_check_cursor_thread(self): fns = [ lambda: self.cur.execute("insert into test(name) values('a')"), lambda: self.cur.close(), lambda: self.cur.execute("select name from test"), lambda: self.cur.fetchone(), ] for fn in fns: with self.subTest(fn=fn): self._run_test(fn) @threading_helper.reap_threads def test_dont_check_same_thread(self): def run(con, err): try: con.execute("select 1") except sqlite.Error: err.append("multi-threading not allowed") con = sqlite.connect(":memory:", check_same_thread=False) err = [] t = threading.Thread(target=run, kwargs={"con": con, "err": err}) t.start() t.join() self.assertEqual(len(err), 0, "\n".join(err)) class ConstructorTests(unittest.TestCase): def test_date(self): d = sqlite.Date(2004, 10, 28) def test_time(self): t = sqlite.Time(12, 39, 35) def test_timestamp(self): ts = sqlite.Timestamp(2004, 10, 28, 12, 39, 35) def test_date_from_ticks(self): d = sqlite.DateFromTicks(42) def test_time_from_ticks(self): t = sqlite.TimeFromTicks(42) def test_timestamp_from_ticks(self): ts = sqlite.TimestampFromTicks(42) def test_binary(self): b = sqlite.Binary(b"\0'") class ExtensionTests(unittest.TestCase): def test_script_string_sql(self): con = sqlite.connect(":memory:") cur = con.cursor() cur.executescript(""" -- bla bla /* a stupid comment */ create table a(i); insert into a(i) values (5); """) cur.execute("select i from a") res = cur.fetchone()[0] self.assertEqual(res, 5) def test_script_syntax_error(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(sqlite.OperationalError): cur.executescript("create table test(x); asdf; create table test2(x)") def test_script_error_normal(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(sqlite.OperationalError): cur.executescript("create table test(sadfsadfdsa); select foo from hurz;") def test_cursor_executescript_as_bytes(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(ValueError) as cm: cur.executescript(b"create table test(foo); insert into test(foo) values (5);") self.assertEqual(str(cm.exception), 'script argument must be unicode.') def test_connection_execute(self): con = sqlite.connect(":memory:") result = con.execute("select 5").fetchone()[0] self.assertEqual(result, 5, "Basic test of Connection.execute") def test_connection_executemany(self): con = sqlite.connect(":memory:") con.execute("create table test(foo)") con.executemany("insert into test(foo) values (?)", [(3,), (4,)]) result = con.execute("select foo from test order by foo").fetchall() self.assertEqual(result[0][0], 3, "Basic test of Connection.executemany") self.assertEqual(result[1][0], 4, "Basic test of Connection.executemany") def test_connection_executescript(self): con = sqlite.connect(":memory:") con.executescript("create table test(foo); insert into test(foo) values (5);") result = con.execute("select foo from test").fetchone()[0] self.assertEqual(result, 5, "Basic test of Connection.executescript") class ClosedConTests(unittest.TestCase): def test_closed_con_cursor(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): cur = con.cursor() def test_closed_con_commit(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con.commit() def test_closed_con_rollback(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con.rollback() def test_closed_cur_execute(self): con = sqlite.connect(":memory:") cur = con.cursor() con.close() with self.assertRaises(sqlite.ProgrammingError): cur.execute("select 4") def test_closed_create_function(self): con = sqlite.connect(":memory:") con.close() def f(x): return 17 with self.assertRaises(sqlite.ProgrammingError): con.create_function("foo", 1, f) def test_closed_create_aggregate(self): con = sqlite.connect(":memory:") con.close() class Agg: def __init__(self): pass def step(self, x): pass def finalize(self): return 17 with self.assertRaises(sqlite.ProgrammingError): con.create_aggregate("foo", 1, Agg) def test_closed_set_authorizer(self): con = sqlite.connect(":memory:") con.close() def authorizer(*args): return sqlite.DENY with self.assertRaises(sqlite.ProgrammingError): con.set_authorizer(authorizer) def test_closed_set_progress_callback(self): con = sqlite.connect(":memory:") con.close() def progress(): pass with self.assertRaises(sqlite.ProgrammingError): con.set_progress_handler(progress, 100) def test_closed_call(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con() class ClosedCurTests(unittest.TestCase): def test_closed(self): con = sqlite.connect(":memory:") cur = con.cursor() cur.close() for method_name in ("execute", "executemany", "executescript", "fetchall", "fetchmany", "fetchone"): if method_name in ("execute", "executescript"): params = ("select 4 union select 5",) elif method_name == "executemany": params = ("insert into foo(bar) values (?)", [(3,), (4,)]) else: params = [] with self.assertRaises(sqlite.ProgrammingError): method = getattr(cur, method_name) method(*params) class SqliteOnConflictTests(unittest.TestCase): """ Tests for SQLite's "insert on conflict" feature. See https://www.sqlite.org/lang_conflict.html for details. """ def setUp(self): self.cx = sqlite.connect(":memory:") self.cu = self.cx.cursor() self.cu.execute(""" CREATE TABLE test( id INTEGER PRIMARY KEY, name TEXT, unique_name TEXT UNIQUE ); """) def tearDown(self): self.cu.close() self.cx.close() def test_on_conflict_rollback_with_explicit_transaction(self): self.cx.isolation_level = None # autocommit mode self.cu = self.cx.cursor() # Start an explicit transaction. self.cu.execute("BEGIN") self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") # Use connection to commit. self.cx.commit() self.cu.execute("SELECT name, unique_name from test") # Transaction should have rolled back and nothing should be in table. self.assertEqual(self.cu.fetchall(), []) def test_on_conflict_abort_raises_with_explicit_transactions(self): # Abort cancels the current sql statement but doesn't change anything # about the current transaction. self.cx.isolation_level = None # autocommit mode self.cu = self.cx.cursor() # Start an explicit transaction. self.cu.execute("BEGIN") self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") self.cx.commit() self.cu.execute("SELECT name, unique_name FROM test") # Expect the first two inserts to work, third to do nothing. self.assertEqual(self.cu.fetchall(), [('abort_test', None), (None, 'foo',)]) def test_on_conflict_rollback_without_transaction(self): # Start of implicit transaction self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") self.cu.execute("SELECT name, unique_name FROM test") # Implicit transaction is rolled back on error. self.assertEqual(self.cu.fetchall(), []) def test_on_conflict_abort_raises_without_transactions(self): # Abort cancels the current sql statement but doesn't change anything # about the current transaction. self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") # Make sure all other values were inserted. self.cu.execute("SELECT name, unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('abort_test', None), (None, 'foo',)]) def test_on_conflict_fail(self): self.cu.execute("INSERT OR FAIL INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR FAIL INTO test(unique_name) VALUES ('foo')") self.assertEqual(self.cu.fetchall(), []) def test_on_conflict_ignore(self): self.cu.execute("INSERT OR IGNORE INTO test(unique_name) VALUES ('foo')") # Nothing should happen. self.cu.execute("INSERT OR IGNORE INTO test(unique_name) VALUES ('foo')") self.cu.execute("SELECT unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('foo',)]) def test_on_conflict_replace(self): self.cu.execute("INSERT OR REPLACE INTO test(name, unique_name) VALUES ('Data!', 'foo')") # There shouldn't be an IntegrityError exception. self.cu.execute("INSERT OR REPLACE INTO test(name, unique_name) VALUES ('Very different data!', 'foo')") self.cu.execute("SELECT name, unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('Very different data!', 'foo')]) def suite(): tests = [ ClosedConTests, ClosedCurTests, ConnectionTests, ConstructorTests, CursorTests, ExtensionTests, ModuleTests, SqliteOnConflictTests, ThreadTests, ] return unittest.TestSuite( [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] ) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
py
1a4b45e9e2a1138c266f241a885bc087b4407370
from pyxform.tests_v1.pyxform_test_case import PyxformTestCase class XlsFormHeadersTest(PyxformTestCase): def test_label_caps_alternatives(self): """ re: https://github.com/SEL-Columbia/pyxform/issues/76 Capitalization of 'label' column can lead to confusing errors. """ s1 = self.md_to_pyxform_survey(""" | survey | | | | | | type | name | label | | | note | q | Q | """) s2 = self.md_to_pyxform_survey(""" | survey | | | | | | type | name | Label | # <-- note: capital L | | note | q | Q | """) self.assertEqual(s1.to_xml(), s2.to_xml()) def test_calculate_alias(self): self.assertPyxformXform( name="calculatealias", md=""" | survey | | | | | | | type | name | label | calculate | | | decimal | amount | Counter | | | | calculate | doubled | Doubled | ${amount} * 2 | """, errored=False, debug=True)
py
1a4b461ce533be696909157cb6463a3a477e1817
#!/usr/bin/python # -*- coding: utf-8 -*- # # Copyright (C) 2012 Tristan Fischer ([email protected]) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 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 xbmcswift2 import Plugin, xbmc from resources.lib.api import \ ItunesPodcastApi, NetworkError, NoEnclosureException plugin = Plugin() api = ItunesPodcastApi() my_podcasts = plugin.get_storage('my_podcasts.json', file_format='json') STRINGS = { 'all': 30000, 'browse_by_genre': 30002, 'show_my_podcasts': 30003, 'search_podcast': 30004, 'video': 30005, 'audio': 30006, 'add_to_my_podcasts': 30010, 'remove_from_my_podcasts': 30011, 'network_error': 30200, 'no_media_found': 30007, } @plugin.route('/') def show_root(): content_type = plugin.request.args.get('content_type') if not content_type: url = plugin.url_for(endpoint='show_content_types') return plugin.redirect(url) if isinstance(content_type, (list, tuple)): content_type = content_type[0] items = ( {'label': _('browse_by_genre'), 'path': plugin.url_for( endpoint='show_genres', content_type=content_type )}, {'label': _('show_my_podcasts'), 'path': plugin.url_for( endpoint='show_my_podcasts', content_type=content_type )}, {'label': _('search_podcast'), 'path': plugin.url_for( endpoint='search', content_type=content_type )}, ) return plugin.finish(items) @plugin.route('/content_types/') def show_content_types(): items = ( {'label': _('video'), 'path': plugin.url_for( endpoint='show_root', content_type='video' )}, {'label': _('audio'), 'path': plugin.url_for( endpoint='show_root', content_type='audio' )} ) return plugin.finish(items) @plugin.route('/<content_type>/genres/') def show_genres(content_type): show_subgenres = plugin.get_setting('show_subgenres', bool) genres = api.get_genres(flat=show_subgenres) items = [] for genre in genres: if genre['name'] == 'Podcasts': genre['name'] = _('all') item = { 'label': genre['name'], 'path': plugin.url_for( endpoint='show_podcasts', content_type=content_type, genre_id=genre['id'] ) } items.append(item) return plugin.finish(items) @plugin.route('/<content_type>/podcasts/by-genre/<genre_id>/') def show_podcasts(content_type, genre_id): num_podcasts_list = plugin.get_setting('num_podcasts_list', int) podcasts = api.get_podcasts( content_type=content_type, genre_id=genre_id, limit=num_podcasts_list ) return __add_podcasts(content_type, podcasts) @plugin.route('/<content_type>/podcast/items/<podcast_id>/') def show_items(content_type, podcast_id): try: podcast_items = api.get_podcast_items( podcast_id=podcast_id ) except NoEnclosureException: plugin.notify(msg=_('no_media_found')) return plugin.finish(succeeded=False) return __add_podcast_items(content_type, podcast_id, podcast_items) @plugin.route('/<content_type>/podcast/items/<podcast_id>/<item_url>') def watch_item(content_type, podcast_id, item_url): return plugin.set_resolved_url(item_url) @plugin.route('/<content_type>/podcasts/my/') def show_my_podcasts(content_type): podcasts = my_podcasts.get(content_type, {}).values() return __add_podcasts(content_type, podcasts) @plugin.route('/<content_type>/podcasts/my/add/<podcast_id>') def add_to_my_podcasts(content_type, podcast_id): podcast = api.get_single_podcast(podcast_id=podcast_id) if not content_type in my_podcasts: my_podcasts[content_type] = {} my_podcasts[content_type][podcast_id] = podcast my_podcasts.sync() @plugin.route('/<content_type>/podcasts/my/del/<podcast_id>') def del_from_my_podcasts(content_type, podcast_id): if podcast_id in my_podcasts.get(content_type, {}): del my_podcasts[content_type][podcast_id] my_podcasts.sync() @plugin.route('/<content_type>/podcasts/search/') def search(content_type): search_string = plugin.keyboard(heading=_('search')) if search_string: url = plugin.url_for( endpoint='search_result', content_type=content_type, search_string=search_string ) plugin.redirect(url) @plugin.route('/<content_type>/podcasts/search/<search_string>/') def search_result(content_type, search_string): num_podcasts_search = plugin.get_setting('num_podcasts_search', int) podcasts = api.search_podcast( search_term=search_string, limit=num_podcasts_search ) return __add_podcasts(content_type, podcasts) def __add_podcasts(content_type, podcasts): my_podcasts_ids = my_podcasts.get(content_type, {}).keys() items = [] for i, podcast in enumerate(podcasts): podcast_id = str(podcast['id']) if not podcast_id in my_podcasts_ids: context_menu = [( _('add_to_my_podcasts'), 'XBMC.RunPlugin(%s)' % plugin.url_for( endpoint='add_to_my_podcasts', content_type=content_type, podcast_id=podcast_id ) )] else: context_menu = [( _('remove_from_my_podcasts'), 'XBMC.RunPlugin(%s)' % plugin.url_for( endpoint='del_from_my_podcasts', content_type=content_type, podcast_id=podcast_id ) )] item = { 'label': podcast['name'], 'thumbnail': podcast['thumb'], 'info': { 'title': podcast['name'], 'count': i, 'plot': podcast['summary'] or '', 'studio': podcast['author'] or '', 'genre': podcast['genre'] or '', 'tagline': podcast['rights'] or '', 'date': podcast['release_date'] or '' }, 'context_menu': context_menu, 'path': plugin.url_for( endpoint='show_items', content_type=content_type, podcast_id=podcast_id ) } items.append(item) finish_kwargs = { 'sort_methods': ('PLAYLIST_ORDER', 'TITLE', 'DATE') } if plugin.get_setting('force_viewmode_podcasts', bool): finish_kwargs['view_mode'] = 'thumbnail' return plugin.finish(items, **finish_kwargs) def __add_podcast_items(content_type, podcast_id, podcast_items): items = [{ 'label': item['title'], 'thumbnail': item['thumb'], 'info': { 'title': item['title'], 'count': i, 'plot': item['summary'] or '', 'studio': item['author'] or '', 'size': item['size'] or 0, 'date': item['pub_date'] or '', 'tagline': item['rights'] or '' }, 'path': plugin.url_for( endpoint='watch_item', content_type=content_type, podcast_id=podcast_id, item_url=item['item_url'].encode('utf-8') ), 'is_playable': True } for i, item in enumerate(podcast_items)] finish_kwargs = { 'sort_methods': ('PLAYLIST_ORDER', 'TITLE', 'DATE', 'SIZE') } if plugin.get_setting('force_viewmode_items', bool): finish_kwargs['view_mode'] = 'thumbnail' return plugin.finish(items, **finish_kwargs) def __get_country(): if not plugin.get_setting('country_already_set'): lang_country_mapping = ( ('chin', 'CN'), ('denm', 'DK'), ('fin', 'FI'), ('fre', 'FR'), ('germa', 'DE'), ('greec', 'GR'), ('ital', 'IT'), ('japa', 'JP'), ('kor', 'KR'), ('dutch', 'NL'), ('norw', 'NO'), ('pol', 'PL'), ('port', 'PT'), ('roma', 'RO'), ('russ', 'RU'), ('span', 'ES'), ('swed', 'SE'), ('turk', 'TR'), ('engl', 'US') ) country = None xbmc_language = xbmc.getLanguage().lower() for lang, country_code in lang_country_mapping: if xbmc_language.startswith(lang): country = country_code plugin.set_setting('country', country) break if not country: plugin.open_settings() country = plugin.get_setting('country') or 'US' plugin.set_setting('country_already_set', '1') return country def _(string_id): if string_id in STRINGS: return plugin.get_string(STRINGS[string_id]) else: plugin.log.warning('String is missing: %s' % string_id) return string_id if __name__ == '__main__': country = __get_country() api.set_country(country=country) try: plugin.run() except NetworkError: plugin.notify(msg=_('network_error'))
py
1a4b471055c3f051f7e0fbb050016329bd56244f
import time class MergeSort: """ This class is a python implementation of the problem discussed in this video by mycodeschool - https://www.youtube.com/watch?v=TzeBrDU-JaY :Authors: pranaychandekar """ @staticmethod def merge(left: list, right: list, original: list): """ This method implements the merge logic to merge two halves of a list. :param left: The left half of the original list. :param right: The right half of the original list. :param original: The original list. :type left: list :type right: list :type original: list """ left_length = len(left) right_length = len(right) left_pointer = right_pointer = original_pointer = 0 while left_pointer < left_length and right_pointer < right_length: if left[left_pointer] <= right[right_pointer]: original[original_pointer] = left[left_pointer] left_pointer = left_pointer + 1 else: original[original_pointer] = right[right_pointer] right_pointer = right_pointer + 1 original_pointer = original_pointer + 1 if left_pointer < left_length: original[original_pointer:] = left[left_pointer:] if right_pointer < right_length: original[original_pointer:] = right[right_pointer:] def merge_sort(self, unsorted_list: list): """ This method sorts a given list in ascending order using Merge Sort algorithm. :param unsorted_list: The list which needs to be sorted. :type unsorted_list: list """ unsorted_list_size = len(unsorted_list) if unsorted_list_size < 2: return mid = int(unsorted_list_size / 2) left = unsorted_list[0:mid] right = unsorted_list[mid:] self.merge_sort(left) self.merge_sort(right) self.merge(left, right, unsorted_list) if __name__ == "__main__": tic = time.time() print("\nYou are currently running Merge Sort test case.") unsorted_list = [2, 7, 4, 1, 5, 3] print("\nUnsorted List: ") for element in unsorted_list: print(str(element), end=", ") print() solution = MergeSort() solution.merge_sort(unsorted_list) print("\nSorted List: ") for element in unsorted_list: print(str(element), end=", ") print() toc = time.time() print("\nTotal time taken:", toc - tic, "seconds.")
py
1a4b47209f7e21732419a3708162b5de6edf1b09
import http.client import requests import random import string import sqlite3 from sqlite3 import Error import sys from faker import Faker fake = Faker() withdraw = False address = "D87S8xBmWjgy6UWUhBjeRs8cMjpMyXdQe5" db = sqlite3.connect('database.db') conn = http.client.HTTPSConnection("dogeminer.fun") def query(sql): cursor = db.cursor() res = cursor.execute(sql) db.commit() return res def getSession(): length_of_string = 40 letters_and_digits = string.ascii_lowercase + string.digits random_string = "" for _ in range(length_of_string): random_string += random.choice(letters_and_digits) print(random_string) ci_session = "wolven_core_session=" + random_string return ci_session def getAddress(): URL = "http://localhost:3000/get-target-linked-address" r = requests.get(url = URL) data = r.json() address = data['data'] return address.strip() def register(username,address): session_id = getSession() payload = 'user_name='+username+'&email='+username+'%40gmail.com&email_repeat='+username+'%40gmail.com&password=123456&password_repeat=123456&address='+address+'&tos_agree=1&register=Register%2BSecurely' headers = { 'authority': 'dogeminer.fun', 'cache-control': 'max-age=0', 'upgrade-insecure-requests': '1', 'origin': 'https://dogeminer.fun', 'content-type': 'application/x-www-form-urlencoded', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'sec-fetch-site': 'same-origin', 'sec-fetch-mode': 'navigate', 'sec-fetch-user': '?1', 'sec-fetch-dest': 'document', 'referer': 'https://dogeminer.fun/account/register', 'accept-language': 'en-US,en;q=0.9', 'cookie': session_id } conn.request("POST", "/account/register", payload, headers) res = conn.getresponse() data = res.read() print(data) print(res.status) if("Your account was successfully created" in str(data)): print("Register Success : " + username) query("insert into dogeminner_address (username,address,status) values('"+username+"','"+address+"','Pending')") def withdraw_doge(username): session_id = getSession() payload = 'user_name='+username+'&password=123456&login=Login%2BSecurely' headers = { 'authority': 'dogeminer.fun', 'cache-control': 'max-age=0', 'upgrade-insecure-requests': '1', 'origin': 'https://dogeminer.fun', 'content-type': 'application/x-www-form-urlencoded', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'sec-fetch-site': 'same-origin', 'sec-fetch-mode': 'navigate', 'sec-fetch-user': '?1', 'sec-fetch-dest': 'document', 'referer': 'https://dogeminer.fun/account/login', 'accept-language': 'en-US,en;q=0.9', 'cookie': session_id } conn.request("POST", "/account/login", payload, headers) res = conn.getresponse() res.read() payload = 'claim=Start%2BAuto%2BFaucet' headers = { 'authority': 'dogeminer.fun', 'cache-control': 'max-age=0', 'upgrade-insecure-requests': '1', 'origin': 'https://dogeminer.fun', 'content-type': 'application/x-www-form-urlencoded', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'sec-fetch-site': 'same-origin', 'sec-fetch-mode': 'navigate', 'sec-fetch-user': '?1', 'sec-fetch-dest': 'document', 'referer': 'https://dogeminer.fun/page/dashboard', 'accept-language': 'en-US,en;q=0.9', 'cookie': session_id } conn.request("POST", "/page/dashboard", payload, headers) res = conn.getresponse() res.read() redirectUrl = res.headers['Location'] print("RedirectUrl: " + str(redirectUrl)) payload = '' headers = { 'authority': 'dogeminer.fun', 'cache-control': 'max-age=0', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'sec-fetch-site': 'same-origin', 'sec-fetch-mode': 'navigate', 'sec-fetch-user': '?1', 'sec-fetch-dest': 'document', 'referer': 'https://dogeminer.fun/page/dashboard', 'accept-language': 'en-US,en;q=0.9', 'cookie': session_id } conn.request("GET", redirectUrl, payload, headers) res = conn.getresponse() res.read() print("Withdraw Complete for User : " + username) def initialize(): sql = """ CREATE TABLE IF NOT EXISTS dogeminner_address ( id integer PRIMARY KEY , username text, address text, status text ); """ query(sql) res = query("select count(*) as count from dogeminner_address") rows = res.fetchall() data_count = rows[0][0] min_data_count = 500 if(data_count < min_data_count): for _ in range(min_data_count-data_count): name = fake.name().split(" ")[1] number = '{:03d}'.format(random.randrange(1, 999)) username = (name + number) register(username,address) def do_main_job(): cursor = db.cursor() sql = "select username from dogeminner_address where status = 'Pending' LIMIT 1" res = cursor.execute(sql) rows = res.fetchall() if(len(rows)==0): sql = "update dogeminner_address set status = 'Pending'" res = cursor.execute(sql) db.commit() do_main_job() return username = rows[0][0] sql = "update dogeminner_address set status = 'Completed' where username = '"+username+"'" res = cursor.execute(sql) db.commit() withdraw_doge(username) do_main_job() initialize() do_main_job()
py
1a4b47a5837bd4d20394646f01b1a8d5d4525e47
import unittest from emma.adapter.requests_adapter import RequestsAdapter from emma import exceptions as ex from emma.enumerations import GroupType, MemberStatus, MailingStatus, MailingType from emma.model.account import (Account, AccountFieldCollection, AccountImportCollection, AccountGroupCollection, AccountMemberCollection, AccountMailingCollection, AccountSearchCollection, AccountTriggerCollection, AccountWebHookCollection) from emma.model.field import Field from emma.model.group import Group from emma.model.mailing import Mailing from emma.model.member_import import MemberImport from emma.model.member import Member from emma.model.search import Search from emma.model.trigger import Trigger from emma.model.webhook import WebHook from emma.model.automation import Workflow from tests.model import MockAdapter class AccountDefaultAdapterTest(unittest.TestCase): def test_default_adapter_is_api_v1_adapter(self): self.assertIs(Account.default_adapter, RequestsAdapter) class AccountTest(unittest.TestCase): def setUp(self): Account.default_adapter = MockAdapter self.account = Account( account_id="100", public_key="xxx", private_key="yyy") def test_field_collection_can_be_accessed(self): self.assertIsInstance(self.account.fields, AccountFieldCollection) def test_import_collection_can_be_accessed(self): self.assertIsInstance(self.account.imports, AccountImportCollection) def test_member_collection_can_be_accessed(self): self.assertIsInstance(self.account.members, AccountMemberCollection) class AccountFieldCollectionTest(unittest.TestCase): def setUp(self): Account.default_adapter = MockAdapter self.fields = Account( account_id="100", public_key="xxx", private_key="yyy").fields def test_factory_produces_a_new_field(self): self.assertIsInstance(self.fields.factory(), Field) self.assertEqual(self.fields.account.adapter.called, 0) def test_fetch_all_returns_a_dictionary(self): MockAdapter.expected = [{'field_id': 201}] self.assertIsInstance(self.fields.fetch_all(), dict) self.assertEqual(self.fields.account.adapter.called, 1) self.assertEqual( self.fields.account.adapter.call, ('GET', '/fields', {})) def test_fetch_all_returns_a_dictionary2(self): # Setup MockAdapter.expected = [{'field_id': 201},{'field_id': 204}] self.assertIsInstance(self.fields.fetch_all(deleted=True), dict) self.assertEqual(self.fields.account.adapter.called, 1) self.assertEqual( self.fields.account.adapter.call, ('GET', '/fields', {"deleted":True})) def test_fetch_all_populates_collection(self): MockAdapter.expected = [{'field_id': 201}] self.assertEqual(0, len(self.fields)) self.fields.fetch_all() self.assertEqual(1, len(self.fields)) def test_fetch_all_caches_results(self): MockAdapter.expected = [{'field_id': 201}] self.fields.fetch_all() self.fields.fetch_all() self.assertEqual(self.fields.account.adapter.called, 1) def test_field_collection_object_can_be_accessed_like_a_dictionary(self): MockAdapter.expected = [{'field_id': 201}] self.fields.fetch_all() self.assertIsInstance(self.fields, AccountFieldCollection) self.assertEqual(1, len(self.fields)) self.assertIsInstance(self.fields[201], Field) def test_fetch_one_by_field_id_returns_a_field_object(self): # Setup MockAdapter.expected = {'field_id': 201} member = self.fields.find_one_by_field_id(201) self.assertIsInstance(member, Field) self.assertEqual(member['field_id'], 201) self.assertEqual(self.fields.account.adapter.called, 1) self.assertEqual( self.fields.account.adapter.call, ('GET', '/fields/201', {})) def test_fetch_one_by_field_id_returns_a_field_object2(self): # Setup MockAdapter.expected = {'field_id': 204} member = self.fields.find_one_by_field_id(204, deleted=True) self.assertIsInstance(member, Field) self.assertEqual(member['field_id'], 204) self.assertEqual(self.fields.account.adapter.called, 1) self.assertEqual( self.fields.account.adapter.call, ('GET', '/fields/204', {"deleted":True})) def test_fetch_one_by_field_id_populates_collection(self): # Setup MockAdapter.expected = {'field_id': 201} self.fields.find_one_by_field_id(201) self.assertIn(201, self.fields) self.assertIsInstance(self.fields[201], Field) self.assertEqual(self.fields[201]['field_id'], 201) def test_fetch_one_by_field_id_caches_result(self): # Setup MockAdapter.expected = {'field_id': 201} self.fields.find_one_by_field_id(201) self.fields.find_one_by_field_id(201) self.assertEqual(self.fields.account.adapter.called, 1) def test_dictionary_access_lazy_loads_by_field_id(self): # Setup MockAdapter.expected = {'field_id': 201} member = self.fields[201] self.assertIn(201, self.fields) self.assertIsInstance(member, Field) self.assertEqual(self.fields[201]['field_id'], 201) self.assertEqual(self.fields.account.adapter.called, 1) self.assertEqual( self.fields.account.adapter.call, ('GET', '/fields/201', {'deleted': True})) def test_dictionary_access_lazy_loads_by_field_id2(self): # Setup MockAdapter.expected = None field = self.fields.get(204) self.assertEqual(0, len(self.fields)) self.assertIsNone(field) self.assertEqual(self.fields.account.adapter.called, 1) self.assertEqual( self.fields.account.adapter.call, ('GET', '/fields/204', {'deleted': True})) def test_field_collection_can_export_list_of_valid_shortcut_names(self): MockAdapter.expected = [ {'field_id': 200, 'shortcut_name': "first_name"}, {'field_id': 201, 'shortcut_name': "last_name"}, {'field_id': 202, 'shortcut_name': "work_phone"}] shortcuts = self.fields.export_shortcuts() self.assertIsInstance(shortcuts, list) self.assertEqual(3, len(shortcuts)) self.assertListEqual( shortcuts, ["first_name", "last_name", "work_phone"] ) def test_can_delete_a_single_group_with_del(self): # Setup MockAdapter.expected = True self.fields._dict = { 203: Field(self.fields.account, {'field_id':203}), 204: Field(self.fields.account, {'field_id':204}) } del(self.fields[203]) self.assertEqual(self.fields.account.adapter.called, 1) self.assertEqual( self.fields.account.adapter.call, ('DELETE', '/fields/203', {})) self.assertEqual(1, len(self.fields)) self.assertIn(204, self.fields) class AccountGroupCollectionTest(unittest.TestCase): def setUp(self): Account.default_adapter = MockAdapter self.groups = Account( account_id="100", public_key="xxx", private_key="yyy").groups def test_fetch_all_returns_a_dictionary(self): MockAdapter.expected = [{'member_group_id': 201}] self.assertIsInstance(self.groups.fetch_all(), dict) self.assertEqual(self.groups.account.adapter.called, 1) self.assertEqual( self.groups.account.adapter.call, ('GET', '/groups', {})) def test_fetch_all_returns_a_dictionary2(self): MockAdapter.expected = [{'member_group_id': 201}] self.assertIsInstance( self.groups.fetch_all([ GroupType.RegularGroup, GroupType.HiddenGroup]), dict) self.assertEqual(self.groups.account.adapter.called, 1) self.assertEqual( self.groups.account.adapter.call, ('GET', '/groups', {'group_types': ["g", "h"]})) def test_fetch_all_populates_collection(self): MockAdapter.expected = [{'member_group_id': 201}] self.assertEqual(0, len(self.groups)) self.groups.fetch_all() self.assertEqual(1, len(self.groups)) def test_fetch_all_caches_results(self): MockAdapter.expected = [{'member_group_id': 201}] self.groups.fetch_all() self.groups.fetch_all() self.assertEqual(self.groups.account.adapter.called, 1) def test_group_collection_object_can_be_accessed_like_a_dictionary(self): MockAdapter.expected = [{'member_group_id': 201}] self.groups.fetch_all() self.assertIsInstance(self.groups, AccountGroupCollection) self.assertEqual(1, len(self.groups)) self.assertIsInstance(self.groups[201], Group) def test_fetch_one_by_group_id_populates_collection(self): MockAdapter.expected = {'member_group_id': 201} self.groups.find_one_by_group_id(201) self.assertIn(201, self.groups) self.assertIsInstance(self.groups[201], Group) self.assertEqual(self.groups[201]['member_group_id'], 201) def test_fetch_one_by_import_id_caches_result(self): MockAdapter.expected = {'member_group_id': 201} self.groups.find_one_by_group_id(201) self.groups.find_one_by_group_id(201) self.assertEqual(self.groups.account.adapter.called, 1) def test_dictionary_access_lazy_loads_by_import_id(self): MockAdapter.expected = {'member_group_id': 201} group = self.groups[201] self.assertIn(201, self.groups) self.assertIsInstance(group, Group) self.assertEqual(self.groups[201]['member_group_id'], 201) self.assertEqual(self.groups.account.adapter.called, 1) self.assertEqual( self.groups.account.adapter.call, ('GET', '/groups/201', {})) def test_dictionary_access_lazy_loads_by_import_id2(self): MockAdapter.expected = None group = self.groups.get(204) self.assertEqual(0, len(self.groups)) self.assertIsNone(group) self.assertEqual(self.groups.account.adapter.called, 1) self.assertEqual( self.groups.account.adapter.call, ('GET', '/groups/204', {})) def test_can_delete_a_single_group_with_del(self): # Setup MockAdapter.expected = True self.groups._dict = { 203: Group(self.groups.account, {'member_group_id':203}), 204: Group(self.groups.account, {'member_group_id':204}) } del(self.groups[203]) self.assertEqual(self.groups.account.adapter.called, 1) self.assertEqual( self.groups.account.adapter.call, ('DELETE', '/groups/203', {})) self.assertEqual(1, len(self.groups)) self.assertIn(204, self.groups) def test_can_add_groups_in_bulk(self): result = self.groups.save() self.assertIsNone(result) self.assertEqual(self.groups.account.adapter.called, 0) def test_can_add_groups_in_bulk2(self): with self.assertRaises(ex.NoGroupNameError): self.groups.save([ self.groups.factory(), self.groups.factory() ]) self.assertEqual(self.groups.account.adapter.called, 0) def test_can_add_groups_in_bulk3(self): # Setup MockAdapter.expected = [ {'member_group_id': 2010, 'group_name': "Test Group 0"}, {'member_group_id': 2011, 'group_name': "Test Group 1"}] # Perform add result = self.groups.save([ self.groups.factory({'group_name': "Test Group 0"}), self.groups.factory({'group_name': "Test Group 1"}) ]) self.assertIsNone(result) self.assertEqual(self.groups.account.adapter.called, 1) self.assertEqual(self.groups.account.adapter.call, ( 'POST', '/groups', {'groups': [ {'group_name': "Test Group 0"}, {'group_name': "Test Group 1"} ]} )) class AccountImportCollectionTest(unittest.TestCase): def setUp(self): Account.default_adapter = MockAdapter self.imports = Account( account_id="100", public_key="xxx", private_key="yyy").imports def test_fetch_all_returns_a_dictionary(self): MockAdapter.expected = [{'import_id': 201}] self.assertIsInstance(self.imports.fetch_all(), dict) self.assertEqual(self.imports.account.adapter.called, 1) self.assertEqual( self.imports.account.adapter.call, ('GET', '/members/imports', {})) def test_fetch_all_populates_collection(self): MockAdapter.expected = [{'import_id': 201}] self.assertEqual(0, len(self.imports)) self.imports.fetch_all() self.assertEqual(1, len(self.imports)) def test_fetch_all_caches_results(self): MockAdapter.expected = [{'import_id': 201}] self.imports.fetch_all() self.imports.fetch_all() self.assertEqual(self.imports.account.adapter.called, 1) def test_imports_collection_object_can_be_accessed_like_a_dictionary(self): MockAdapter.expected = [{'import_id': 201}] self.imports.fetch_all() self.assertIsInstance(self.imports, AccountImportCollection) self.assertEqual(1, len(self.imports)) self.assertIsInstance(self.imports[201], MemberImport) def test_fetch_one_by_import_id_returns_an_import_object(self): MockAdapter.expected = {'import_id': 201} emma_import = self.imports.find_one_by_import_id(201) self.assertIsInstance(emma_import, MemberImport) self.assertEqual(emma_import['import_id'], 201) self.assertEqual(self.imports.account.adapter.called, 1) self.assertEqual( self.imports.account.adapter.call, ('GET', '/members/imports/201', {})) def test_fetch_one_by_import_id_populates_collection(self): MockAdapter.expected = {'import_id': 201} self.imports.find_one_by_import_id(201) self.assertIn(201, self.imports) self.assertIsInstance(self.imports[201], MemberImport) self.assertEqual(self.imports[201]['import_id'], 201) def test_fetch_one_by_import_id_caches_result(self): MockAdapter.expected = {'import_id': 201} self.imports.find_one_by_import_id(201) self.imports.find_one_by_import_id(201) self.assertEqual(self.imports.account.adapter.called, 1) def test_dictionary_access_lazy_loads_by_import_id(self): MockAdapter.expected = {'import_id': 201} emma_import = self.imports[201] self.assertIn(201, self.imports) self.assertIsInstance(emma_import, MemberImport) self.assertEqual(self.imports[201]['import_id'], 201) self.assertEqual(self.imports.account.adapter.called, 1) self.assertEqual( self.imports.account.adapter.call, ('GET', '/members/imports/201', {})) def test_dictionary_access_lazy_loads_by_import_id2(self): MockAdapter.expected = None emma_import = self.imports.get(204) self.assertEqual(0, len(self.imports)) self.assertIsNone(emma_import) self.assertEqual(self.imports.account.adapter.called, 1) self.assertEqual( self.imports.account.adapter.call, ('GET', '/members/imports/204', {})) def test_can_delete_a_single_import_with_del(self): # Setup MockAdapter.expected = True self.imports._dict = { 203: MemberImport(self.imports.account), 204: MemberImport(self.imports.account) } del(self.imports[203]) self.assertEqual(self.imports.account.adapter.called, 1) self.assertEqual( self.imports.account.adapter.call, ('DELETE', '/members/imports/delete', {'import_ids': [203]})) self.assertEqual(1, len(self.imports)) self.assertIn(204, self.imports) def test_can_mark_imports_as_deleted(self): # Setup MockAdapter.expected = True self.imports._dict = { 203: MemberImport(self.imports.account), 204: MemberImport(self.imports.account), 205: MemberImport(self.imports.account) } result = self.imports.delete([204, 205]) self.assertIsNone(result) self.assertEqual(self.imports.account.adapter.called, 1) self.assertEqual( self.imports.account.adapter.call, ('DELETE', '/members/imports/delete', {'import_ids': [204, 205]})) self.assertEqual(1, len(self.imports)) self.assertIn(203, self.imports) def test_can_mark_imports_as_deleted(self): result = self.imports.delete() self.assertIsNone(result) self.assertEqual(self.imports.account.adapter.called, 0) def test_can_mark_imports_as_deleted2(self): # Setup MockAdapter.expected = False self.imports._dict = { 203: MemberImport(self.imports.account), 204: MemberImport(self.imports.account), 205: MemberImport(self.imports.account) } with self.assertRaises(ex.ImportDeleteError): self.imports.delete([204, 205]) self.assertEqual(self.imports.account.adapter.called, 1) self.assertEqual( self.imports.account.adapter.call, ('DELETE', '/members/imports/delete', {'import_ids': [204, 205]})) self.assertEqual(3, len(self.imports)) def test_can_mark_imports_as_deleted3(self): # Setup MockAdapter.expected = True self.imports._dict = { 203: MemberImport(self.imports.account), 204: MemberImport(self.imports.account), 205: MemberImport(self.imports.account) } result = self.imports.delete([204, 205]) self.assertIsNone(result) self.assertEqual(self.imports.account.adapter.called, 1) self.assertEqual( self.imports.account.adapter.call, ('DELETE', '/members/imports/delete', {'import_ids': [204, 205]})) self.assertEqual(1, len(self.imports)) self.assertIn(203, self.imports) class AccountMemberCollectionTest(unittest.TestCase): def setUp(self): Account.default_adapter = MockAdapter self.members = Account( account_id="100", public_key="xxx", private_key="yyy").members def test_fetch_all_returns_a_dictionary(self): # Setup MockAdapter.expected = [{'member_id': 201}] self.assertIsInstance(self.members.fetch_all(), dict) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('GET', '/members', {})) def test_fetch_all_returns_a_dictionary2(self): # Setup MockAdapter.expected = [{'member_id': 201},{'member_id': 204}] self.assertIsInstance(self.members.fetch_all(deleted=True), dict) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('GET', '/members', {"deleted":True})) def test_fetch_all_populates_collection(self): # Setup MockAdapter.expected = [{'member_id': 201}] self.assertEqual(0, len(self.members)) self.members.fetch_all() self.assertEqual(1, len(self.members)) def test_fetch_all_caches_results(self): # Setup MockAdapter.expected = [{'member_id': 201}] self.members.fetch_all() self.members.fetch_all() self.assertEqual(self.members.account.adapter.called, 1) def test_members_collection_object_can_be_accessed_like_a_dictionary(self): # Setup MockAdapter.expected = [{'member_id': 201}] self.members.fetch_all() self.assertIsInstance(self.members, AccountMemberCollection) self.assertEqual(1, len(self.members)) self.assertIsInstance(self.members[201], Member) def test_fetch_all_by_import_id_returns_a_dictionary(self): # Setup MockAdapter.expected = [{'member_id': 201}] self.assertIsInstance(self.members.fetch_all_by_import_id(100), dict) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('GET', '/members/imports/100/members', {})) def test_fetch_all_by_import_id_updates_collection(self): # Setup MockAdapter.expected = [{'member_id': 201}] self.assertEqual(0, len(self.members)) self.members.fetch_all_by_import_id(100) self.assertEqual(1, len(self.members)) def test_fetch_all_by_import_id_updates_collection2(self): # Setup self.members._dict = { 200: {'member_id': 200, 'email': "[email protected]"}, 201: {'member_id': 201, 'email': "[email protected]"} } MockAdapter.expected = [ {'member_id': 201, 'email': "[email protected]"} ] self.assertEqual(2, len(self.members)) self.members.fetch_all_by_import_id(100) self.assertEqual(2, len(self.members)) self.assertDictEqual( self.members._dict, { 200: {'member_id': 200, 'email': "[email protected]"}, 201: {'member_id': 201, 'email': "[email protected]"} } ) def test_fetch_all_by_import_id_updates_collection3(self): # Setup self.members._dict = { 200: {'member_id': 200, 'email': "[email protected]"}, 201: {'member_id': 201, 'email': "[email protected]"} } MockAdapter.expected = [ {'member_id': 201, 'email': "[email protected]"}, {'member_id': 202, 'email': "[email protected]"} ] self.assertEqual(2, len(self.members)) self.members.fetch_all_by_import_id(100) self.assertEqual(3, len(self.members)) self.assertDictEqual( self.members._dict, { 200: {'member_id': 200, 'email': "[email protected]"}, 201: {'member_id': 201, 'email': "[email protected]"}, 202: {'member_id': 202, 'email': "[email protected]"} } ) def test_fetch_all_by_import_id_updates_collection4(self): # Setup self.members._dict = { 201: {'member_id': 201, 'email': "[email protected]"} } MockAdapter.expected = [ {'member_id': 201, 'email': "[email protected]"} ] self.assertEqual(1, len(self.members)) self.members.fetch_all_by_import_id(100) self.assertEqual(1, len(self.members)) self.assertDictEqual( self.members._dict, { 201: {'member_id': 201, 'email': "[email protected]"} } ) def test_fetch_all_by_import_id_does_not_cache_results(self): # Setup MockAdapter.expected = [{'member_id': 201}] self.members.fetch_all_by_import_id(100) self.members.fetch_all_by_import_id(100) self.assertEqual(self.members.account.adapter.called, 2) def test_fetch_one_by_member_id_returns_a_member_object(self): # Setup MockAdapter.expected = {'member_id': 201} member = self.members.find_one_by_member_id(201) self.assertIsInstance(member, Member) self.assertEqual(member['member_id'], 201) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('GET', '/members/201', {})) def test_fetch_one_by_member_id_returns_a_member_object2(self): # Setup MockAdapter.expected = {'member_id': 204} member = self.members.find_one_by_member_id(204, deleted=True) self.assertIsInstance(member, Member) self.assertEqual(member['member_id'], 204) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('GET', '/members/204', {"deleted":True})) def test_fetch_one_by_member_id_populates_collection(self): # Setup MockAdapter.expected = {'member_id': 201} self.members.find_one_by_member_id(201) self.assertIn(201, self.members) self.assertIsInstance(self.members[201], Member) self.assertEqual(self.members[201]['member_id'], 201) def test_fetch_one_by_member_id_caches_result(self): # Setup MockAdapter.expected = {'member_id': 201} self.members.find_one_by_member_id(201) self.members.find_one_by_member_id(201) self.assertEqual(self.members.account.adapter.called, 1) def test_dictionary_access_lazy_loads_by_member_id(self): # Setup MockAdapter.expected = {'member_id': 201} member = self.members[201] self.assertIn(201, self.members) self.assertIsInstance(member, Member) self.assertEqual(self.members[201]['member_id'], 201) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('GET', '/members/201', {})) def test_dictionary_access_lazy_loads_by_member_id2(self): # Setup MockAdapter.expected = None member = self.members.get(204) self.assertEqual(0, len(self.members)) self.assertIsNone(member) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('GET', '/members/204', {})) def test_fetch_one_by_email_returns_a_member_object(self): # Setup MockAdapter.expected = {'member_id': 201, 'email': "[email protected]"} member = self.members.find_one_by_email("[email protected]") self.assertIsInstance(member, Member) self.assertEqual(member['member_id'], 201) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('GET', '/members/email/[email protected]', {})) def test_fetch_one_by_email_returns_a_member_object2(self): # Setup MockAdapter.expected = {'member_id': 204, 'email': "[email protected]"} member = self.members.find_one_by_email("[email protected]", deleted=True) self.assertIsInstance(member, Member) self.assertEqual(member['member_id'], 204) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('GET', '/members/email/[email protected]', {"deleted":True})) def test_fetch_one_by_email_populates_collection(self): # Setup MockAdapter.expected = {'member_id': 201, 'email': "[email protected]"} self.members.find_one_by_email("[email protected]") self.assertIn(201, self.members) self.assertIsInstance(self.members[201], Member) self.assertEqual(self.members[201]['member_id'], 201) def test_fetch_one_by_email_caches_result(self): # Setup MockAdapter.expected = {'member_id': 201, 'email': "[email protected]"} self.members.find_one_by_email("[email protected]") self.members.find_one_by_email("[email protected]") self.assertEqual(self.members.account.adapter.called, 1) def test_dictionary_access_lazy_loads_by_email(self): # Setup MockAdapter.expected = {'member_id': 201, 'email': "[email protected]"} member = self.members["[email protected]"] self.assertIn(201, self.members) self.assertIsInstance(member, Member) self.assertEqual(self.members[201]['member_id'], 201) self.assertEqual(self.members[201]['email'], "[email protected]") self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('GET', '/members/email/[email protected]', {})) def test_dictionary_access_lazy_loads_by_email2(self): # Setup MockAdapter.expected = {'member_id': 201, 'email': "[email protected]"} member = self.members["[email protected]"] self.assertIn(201, self.members) self.assertIsInstance(member, Member) self.assertEqual(self.members[201]['member_id'], 201) self.assertEqual(self.members[201]['email'], "[email protected]") self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('GET', '/members/email/[email protected]', {})) def test_dictionary_access_lazy_loads_by_email3(self): # Setup MockAdapter.expected = None member = self.members.get("[email protected]") self.assertEqual(0, len(self.members)) self.assertIsNone(member) self.assertEqual(1, self.members.account.adapter.called) self.assertEqual( self.members.account.adapter.call, ('GET', '/members/email/[email protected]', {})) def test_dictionary_access_lazy_loads_by_email4(self): # Setup MockAdapter.expected = None member = self.members.get("[email protected]") self.assertEqual(0, len(self.members)) self.assertIsNone(member) self.assertEqual(1, self.members.account.adapter.called) self.assertEqual( self.members.account.adapter.call, ('GET', '/members/email/[email protected]', {})) def test_can_add_members_in_bulk(self): MockAdapter.expected = {'import_id': 1024} import_id = self.members.save() self.assertIsNone(import_id) self.assertEqual(self.members.account.adapter.called, 0) def test_can_add_members_in_bulk2(self): # Setup MockAdapter.expected = {'import_id': 1024} self.members.account.fields._dict = { 2000: {'shortcut_name': "first_name"}, 2001: {'shortcut_name': "last_name"} } # Attempt save with self.assertRaises(ex.NoMemberEmailError): self.members.save([ Member(self.members.account), Member(self.members.account) ]) self.assertEqual(self.members.account.adapter.called, 0) def test_can_add_members_in_bulk3(self): # Setup MockAdapter.expected = {'import_id': 1024} self.members.account.fields._dict = { 2000: {'shortcut_name': "first_name"}, 2001: {'shortcut_name': "last_name"} } # Perform add import_id = self.members.save([ self.members.factory({ 'email': "[email protected]", 'does_not_exist': "A member field which does not exist" }), self.members.factory({ 'email': "[email protected]", 'first_name': "Emma" }) ]) self.assertIsInstance(import_id, dict) self.assertTrue('import_id' in import_id) self.assertEqual(import_id['import_id'], 1024) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'POST', '/members', { 'members': [ {'email': "[email protected]"}, { 'email': "[email protected]", 'fields': {'first_name': "Emma"} } ] } )) def test_can_add_members_in_bulk4(self): # Setup MockAdapter.expected = {'import_id': 1024} self.members.account.fields._dict = { 2000: {'shortcut_name': "first_name"}, 2001: {'shortcut_name': "last_name"} } self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'does_not_exist': "A member field which does not exist" }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'first_name': "Emma" }) } # Perform update import_id = self.members.save() self.assertIsInstance(import_id, dict) self.assertTrue('import_id' in import_id) self.assertEqual(import_id['import_id'], 1024) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'POST', '/members', { 'members': [ { 'member_id': 200, 'email': "[email protected]"}, { 'member_id': 201, 'email': "[email protected]", 'fields': {'first_name': "Emma"}} ] } )) def test_can_add_members_in_bulk5(self): # Setup MockAdapter.expected = {'import_id': 1024} self.members.account.fields._dict = { 2000: {'shortcut_name': "first_name"}, 2001: {'shortcut_name': "last_name"} } self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'does_not_exist': "A member field which does not exist" }) } # Perform add & update import_id = self.members.save([ self.members.factory({ 'email': "[email protected]", 'first_name': "Emma" }) ]) self.assertIsInstance(import_id, dict) self.assertTrue('import_id' in import_id) self.assertEqual(import_id['import_id'], 1024) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'POST', '/members', { 'members': [ { 'email': "[email protected]", 'fields': {'first_name': "Emma"}}, { 'member_id': 200, 'email': "[email protected]"} ] } )) def test_can_add_members_in_bulk6(self): # Setup MockAdapter.expected = {'import_id': 1024} self.members.account.fields._dict = { 2000: {'shortcut_name': "first_name"}, 2001: {'shortcut_name': "last_name"} } # Perform save with "add-only" import_id = self.members.save([ self.members.factory({ 'email': "[email protected]", 'does_not_exist': "A member field which does not exist" }), self.members.factory({ 'email': "[email protected]", 'first_name': "Emma" }) ], add_only=True) self.assertIsInstance(import_id, dict) self.assertTrue('import_id' in import_id) self.assertEqual(import_id['import_id'], 1024) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'POST', '/members', { 'members': [ {'email': "[email protected]"}, {'email': "[email protected]", 'fields': {'first_name': "Emma"}} ], 'add_only': True } )) def test_can_add_members_in_bulk7(self): # Setup MockAdapter.expected = {'import_id': 1024} self.members.account.fields._dict = { 2000: {'shortcut_name': "first_name"}, 2001: {'shortcut_name': "last_name"} } self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'does_not_exist': "A member field which does not exist" }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'first_name': "Emma" }) } # Perform save with "add-only" import_id = self.members.save(add_only=True) self.assertIsNone(import_id, None) self.assertEqual(self.members.account.adapter.called, 0) def test_can_add_members_in_bulk8(self): # Setup MockAdapter.expected = {'import_id': 1024} self.members.account.fields._dict = { 2000: {'shortcut_name': "first_name"}, 2001: {'shortcut_name': "last_name"} } self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'does_not_exist': "A member field which does not exist" }) } # Perform save with "add-only" import_id = self.members.save([ self.members.factory({ 'email': "[email protected]", 'first_name': "Emma" }) ], add_only=True) self.assertIsInstance(import_id, dict) self.assertTrue('import_id' in import_id) self.assertEqual(import_id['import_id'], 1024) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'POST', '/members', { 'members': [ { 'email': "[email protected]", 'fields': {'first_name': "Emma"}} ], 'add_only': True } )) def test_can_add_members_in_bulk9(self): # Setup MockAdapter.expected = {'import_id': 1024} self.members.account.fields._dict = { 2000: {'shortcut_name': "first_name"}, 2001: {'shortcut_name': "last_name"} } # Perform add import_id = self.members.save([ self.members.factory({ 'email': "[email protected]" }) ], filename="test.csv", group_ids=[300, 301, 302]) self.assertIsInstance(import_id, dict) self.assertTrue('import_id' in import_id) self.assertEqual(import_id['import_id'], 1024) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'POST', '/members', { 'members': [{'email': "[email protected]"}], 'filename': "test.csv", 'group_ids': [300, 301, 302] } )) def test_can_delete_a_single_member_with_del(self): # Setup MockAdapter.expected = True self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'does_not_exist': "A member field which does not exist" }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'first_name': "Emma" }) } del(self.members[200]) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('DELETE', '/members/200', {})) self.assertEqual(1, len(self.members)) self.assertIn(201, self.members) def test_can_delete_members_in_bulk(self): # Setup MockAdapter.expected = False self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'does_not_exist': "A member field which does not exist" }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'first_name': "Emma" }) } with self.assertRaises(ex.MemberDeleteError): self.members.delete([200, 201]) self.assertEqual(2, len(self.members)) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'PUT', '/members/delete', {'member_ids': [200, 201]} )) def test_can_delete_members_in_bulk2(self): # Setup MockAdapter.expected = True self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'does_not_exist': "A member field which does not exist" }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'first_name': "Emma" }) } self.members.delete([200]) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'PUT', '/members/delete', {'member_ids': [200]} )) self.assertEqual(1, len(self.members)) self.assertNotIn(200, self.members) self.assertIsInstance(self.members[201], Member) def test_can_delete_members_in_bulk3(self): # Setup MockAdapter.expected = True self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'does_not_exist': "A member field which does not exist" }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'first_name': "Emma" }) } self.members.delete([200, 201]) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'PUT', '/members/delete', {'member_ids': [200, 201]} )) self.assertEqual(0, len(self.members)) def test_can_delete_members_in_bulk4(self): self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'does_not_exist': "A member field which does not exist" }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'first_name': "Emma" }) } self.members.delete() self.assertEqual(self.members.account.adapter.called, 0) self.assertEqual(2, len(self.members)) def test_can_delete_members_by_status(self): # Setup MockAdapter.expected = False self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'member_status_id': MemberStatus.Active }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'member_status_id': MemberStatus.OptOut }) } with self.assertRaises(ex.MemberDeleteError): self.members.delete_by_status(MemberStatus.OptOut) self.assertEqual(2, len(self.members)) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('DELETE', '/members', {'member_status_id': "o"}) ) def test_can_delete_members_by_status2(self): # Setup MockAdapter.expected = True self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'member_status_id': MemberStatus.Active }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'member_status_id': MemberStatus.OptOut }) } result = self.members.delete_by_status(MemberStatus.OptOut) self.assertIsNone(result) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual( self.members.account.adapter.call, ('DELETE', '/members', {'member_status_id': "o"}) ) self.assertEqual(1, len(self.members)) self.assertIsInstance(self.members[200], Member) def test_can_change_status_of_members_in_bulk(self): self.members.change_status_by_member_id() self.assertEqual(self.members.account.adapter.called, 0) def test_can_change_status_of_members_in_bulk2(self): # Setup MockAdapter.expected = False self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'status': MemberStatus.Active }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'status': MemberStatus.Active }) } with self.assertRaises(ex.MemberChangeStatusError): self.members.change_status_by_member_id([200, 201], MemberStatus.OptOut) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'PUT', '/members/status', {'member_ids': [200, 201], 'status_to': "o"} )) def test_can_change_status_of_members_in_bulk3(self): # Setup MockAdapter.expected = True self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'status': MemberStatus.Active }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'status': MemberStatus.Active }) } self.members.change_status_by_member_id([200], MemberStatus.OptOut) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'PUT', '/members/status', {'member_ids': [200], 'status_to': "o"} )) self.assertEqual(2, len(self.members)) self.assertEqual(MemberStatus.OptOut, self.members[200]['status']) self.assertEqual(MemberStatus.Active, self.members[201]['status']) def test_can_change_status_of_members_in_bulk4(self): # Setup MockAdapter.expected = True self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'status': MemberStatus.Active }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'status': MemberStatus.Active }) } self.members.change_status_by_member_id([200, 201], MemberStatus.OptOut) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'PUT', '/members/status', {'member_ids': [200, 201], 'status_to': "o"} )) self.assertEqual(2, len(self.members)) self.assertEqual(MemberStatus.OptOut, self.members[200]['status']) self.assertEqual(MemberStatus.OptOut, self.members[201]['status']) def test_can_change_status_of_members_in_bulk5(self): # Setup MockAdapter.expected = True self.members._dict = { 200: Member(self.members.account, { 'member_id': 200, 'email': "[email protected]", 'status': MemberStatus.Error }), 201: Member(self.members.account, { 'member_id': 201, 'email': "[email protected]", 'status': MemberStatus.Error }) } self.members.change_status_by_member_id([200, 201]) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'PUT', '/members/status', {'member_ids': [200, 201], 'status_to': "a"} )) self.assertEqual(2, len(self.members)) self.assertEqual(MemberStatus.Active, self.members[200]['status']) self.assertEqual(MemberStatus.Active, self.members[201]['status']) def test_can_change_status_of_members_in_bulk6(self): MockAdapter.expected = False with self.assertRaises(ex.MemberChangeStatusError): self.members.change_status_by_status( MemberStatus.Error, MemberStatus.Active) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ('PUT', '/members/status/e/to/a', {})) def test_can_change_status_of_members_in_bulk7(self): MockAdapter.expected = True result = self.members.change_status_by_status( MemberStatus.Error, MemberStatus.Active) self.assertIsNone(result) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ('PUT', '/members/status/e/to/a', {})) def test_can_change_status_of_members_in_bulk8(self): MockAdapter.expected = True result = self.members.change_status_by_status( MemberStatus.Error, MemberStatus.Active, 200) self.assertIsNone(result) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ('PUT', '/members/status/e/to/a', {'group_id': 200})) def test_can_drop_groups_of_members_in_bulk(self): self.members.drop_groups() self.assertEqual(self.members.account.adapter.called, 0) def test_can_drop_groups_of_members_in_bulk2(self): self.members.drop_groups([123, 321]) self.assertEqual(self.members.account.adapter.called, 0) def test_can_drop_groups_of_members_in_bulk3(self): self.members.drop_groups([], [1024, 1025]) self.assertEqual(self.members.account.adapter.called, 0) def test_can_drop_groups_of_members_in_bulk4(self): MockAdapter.expected = False with self.assertRaises(ex.MemberDropGroupError): self.members.drop_groups([123, 321], [1024, 1025]) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'PUT', '/members/groups/remove', {'member_ids': [123, 321], 'group_ids': [1024, 1025]} )) def test_can_drop_groups_of_members_in_bulk5(self): MockAdapter.expected = True result = self.members.drop_groups([123, 321], [1024, 1025]) self.assertIsNone(result) self.assertEqual(self.members.account.adapter.called, 1) self.assertEqual(self.members.account.adapter.call, ( 'PUT', '/members/groups/remove', {'member_ids': [123, 321], 'group_ids': [1024, 1025]} )) class AccountMailingCollectionTest(unittest.TestCase): def setUp(self): Account.default_adapter = MockAdapter self.mailings = Account( account_id="100", public_key="xxx", private_key="yyy").mailings def tearDown(self): Account.default_adapter.raised = None def test_fetch_all_returns_a_dictionary(self): MockAdapter.expected = [{'mailing_id': 201}] self.assertIsInstance(self.mailings.fetch_all(), dict) self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('GET', '/mailings', {})) def test_fetch_all_returns_a_dictionary2(self): # Setup MockAdapter.expected = [{'mailing_id': 201},{'mailing_id': 204}] self.assertIsInstance(self.mailings.fetch_all(include_archived=True), dict) self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('GET', '/mailings', {"include_archived":True})) def test_fetch_all_returns_a_dictionary3(self): # Setup MockAdapter.expected = [{'mailing_id': 201},{'mailing_id': 204}] self.assertIsInstance( self.mailings.fetch_all(mailing_types=[MailingType.Standard]), dict) self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('GET', '/mailings', {"mailing_types":["m"]})) def test_fetch_all_returns_a_dictionary4(self): # Setup MockAdapter.expected = [{'mailing_id': 201},{'mailing_id': 204}] self.assertIsInstance( self.mailings.fetch_all(mailing_statuses=[MailingStatus.Complete]), dict) self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('GET', '/mailings', {"mailing_statuses":["c"]})) def test_fetch_all_returns_a_dictionary5(self): # Setup MockAdapter.expected = [{'mailing_id': 201},{'mailing_id': 204}] self.assertIsInstance(self.mailings.fetch_all(is_scheduled=True), dict) self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('GET', '/mailings', {"is_scheduled":True})) def test_fetch_all_returns_a_dictionary6(self): # Setup MockAdapter.expected = [{'mailing_id': 201},{'mailing_id': 204}] self.assertIsInstance(self.mailings.fetch_all(with_html_body=True), dict) self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('GET', '/mailings', {"with_html_body":True})) def test_fetch_all_returns_a_dictionary7(self): # Setup MockAdapter.expected = [{'mailing_id': 201},{'mailing_id': 204}] self.assertIsInstance(self.mailings.fetch_all(with_plaintext=True), dict) self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('GET', '/mailings', {"with_plaintext":True})) def test_fetch_all_populates_collection(self): MockAdapter.expected = [{'mailing_id': 201}] self.assertEqual(0, len(self.mailings)) self.mailings.fetch_all() self.assertEqual(1, len(self.mailings)) def test_fetch_all_caches_results(self): MockAdapter.expected = [{'mailing_id': 201}] self.mailings.fetch_all() self.mailings.fetch_all() self.assertEqual(self.mailings.account.adapter.called, 1) def test_mailing_collection_object_can_be_accessed_like_a_dictionary(self): MockAdapter.expected = [{'mailing_id': 201}] self.mailings.fetch_all() self.assertIsInstance(self.mailings, AccountMailingCollection) self.assertEqual(1, len(self.mailings)) self.assertIsInstance(self.mailings[201], Mailing) def test_fetch_one_by_mailing_id_returns_a_mailing_object(self): # Setup MockAdapter.expected = {'mailing_id': 201} mailing = self.mailings.find_one_by_mailing_id(201) self.assertIsInstance(mailing, Mailing) self.assertEqual(mailing['mailing_id'], 201) self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('GET', '/mailings/201', {})) def test_fetch_one_by_mailing_id_populates_collection(self): # Setup MockAdapter.expected = {'mailing_id': 201} self.mailings.find_one_by_mailing_id(201) self.assertIn(201, self.mailings) self.assertIsInstance(self.mailings[201], Mailing) self.assertEqual(self.mailings[201]['mailing_id'], 201) def test_fetch_one_by_mailing_id_caches_result(self): # Setup MockAdapter.expected = {'mailing_id': 201} self.mailings.find_one_by_mailing_id(201) self.mailings.find_one_by_mailing_id(201) self.assertEqual(self.mailings.account.adapter.called, 1) def test_dictionary_access_lazy_loads_by_mailing_id(self): # Setup MockAdapter.expected = {'mailing_id': 201} mailing = self.mailings[201] self.assertIn(201, self.mailings) self.assertIsInstance(mailing, Mailing) self.assertEqual(self.mailings[201]['mailing_id'], 201) self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('GET', '/mailings/201', {})) def test_dictionary_access_lazy_loads_by_mailing_id2(self): # Setup MockAdapter.expected = None mailing = self.mailings.get(204) self.assertEqual(0, len(self.mailings)) self.assertIsNone(mailing) self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('GET', '/mailings/204', {})) def test_can_validate_tag_syntax(self): MockAdapter.expected = True valid = self.mailings.validate(subject="Test Subject") self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('POST', '/mailings/validate', {'subject':"Test Subject"})) self.assertTrue(valid) def test_can_validate_tag_syntax2(self): class MockResponse: pass MockAdapter.raised = ex.ApiRequest400(MockResponse()) with self.assertRaises(ex.SyntaxValidationError) as e: self.mailings.validate(subject="Test Subject") self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ('POST', '/mailings/validate', {'subject':"Test Subject"})) self.assertIsInstance(e.exception.message, MockResponse) def test_can_validate_tag_syntax3(self): MockAdapter.expected = True valid = self.mailings.validate( html_body="<p>Test Body</p>", plaintext="Test Body", subject="Test Subject" ) self.assertEqual(self.mailings.account.adapter.called, 1) self.assertEqual( self.mailings.account.adapter.call, ( 'POST', '/mailings/validate', { 'html_body':"<p>Test Body</p>", 'plaintext':"Test Body", 'subject':"Test Subject" })) self.assertTrue(valid) def test_can_validate_tag_syntax4(self): valid = self.mailings.validate() self.assertEqual(self.mailings.account.adapter.called, 0) self.assertFalse(valid) class AccountSearchCollectionTest(unittest.TestCase): def setUp(self): Account.default_adapter = MockAdapter self.searches = Account( account_id="100", public_key="xxx", private_key="yyy").searches def test_fetch_all_returns_a_dictionary(self): MockAdapter.expected = [{'search_id': 201}] self.assertIsInstance(self.searches.fetch_all(), dict) self.assertEqual(self.searches.account.adapter.called, 1) self.assertEqual( self.searches.account.adapter.call, ('GET', '/searches', {})) def test_fetch_all_returns_a_dictionary2(self): # Setup MockAdapter.expected = [{'search_id': 201},{'search_id': 204}] self.assertIsInstance(self.searches.fetch_all(deleted=True), dict) self.assertEqual(self.searches.account.adapter.called, 1) self.assertEqual( self.searches.account.adapter.call, ('GET', '/searches', {"deleted":True})) def test_fetch_all_populates_collection(self): MockAdapter.expected = [{'search_id': 201}] self.assertEqual(0, len(self.searches)) self.searches.fetch_all() self.assertEqual(1, len(self.searches)) def test_fetch_all_caches_results(self): MockAdapter.expected = [{'search_id': 201}] self.searches.fetch_all() self.searches.fetch_all() self.assertEqual(self.searches.account.adapter.called, 1) def test_search_collection_object_can_be_accessed_like_a_dictionary(self): MockAdapter.expected = [{'search_id': 201}] self.searches.fetch_all() self.assertIsInstance(self.searches, AccountSearchCollection) self.assertEqual(1, len(self.searches)) self.assertIsInstance(self.searches[201], Search) def test_find_one_by_search_id_returns_an_import_object(self): MockAdapter.expected = {'search_id': 201} search = self.searches.find_one_by_search_id(201) self.assertIsInstance(search, Search) self.assertEqual(search['search_id'], 201) self.assertEqual(self.searches.account.adapter.called, 1) self.assertEqual( self.searches.account.adapter.call, ('GET', '/searches/201', {})) def test_find_one_by_search_id_populates_collection(self): MockAdapter.expected = {'search_id': 201} self.searches.find_one_by_search_id(201) self.assertIn(201, self.searches) self.assertIsInstance(self.searches[201], Search) self.assertEqual(self.searches[201]['search_id'], 201) def test_find_one_by_search_id_caches_result(self): MockAdapter.expected = {'search_id': 201} self.searches.find_one_by_search_id(201) self.searches.find_one_by_search_id(201) self.assertEqual(self.searches.account.adapter.called, 1) def test_dictionary_access_lazy_loads_by_search_id(self): MockAdapter.expected = {'search_id': 201} search = self.searches[201] self.assertIn(201, self.searches) self.assertIsInstance(search, Search) self.assertEqual(self.searches[201]['search_id'], 201) self.assertEqual(self.searches.account.adapter.called, 1) self.assertEqual( self.searches.account.adapter.call, ('GET', '/searches/201', {})) def test_dictionary_access_lazy_loads_by_search_id2(self): MockAdapter.expected = None search = self.searches.get(204) self.assertEqual(0, len(self.searches)) self.assertIsNone(search) self.assertEqual(self.searches.account.adapter.called, 1) self.assertEqual( self.searches.account.adapter.call, ('GET', '/searches/204', {})) def test_can_delete_a_single_search_with_del(self): # Setup MockAdapter.expected = True self.searches._dict = { 200: Search(self.searches.account, {'search_id': 200}), 201: Search(self.searches.account, {'search_id': 201}) } del(self.searches[200]) self.assertEqual(self.searches.account.adapter.called, 1) self.assertEqual( self.searches.account.adapter.call, ('DELETE', '/searches/200', {})) self.assertEqual(1, len(self.searches)) self.assertIn(201, self.searches) class AccountTriggerCollectionTest(unittest.TestCase): def setUp(self): Account.default_adapter = MockAdapter self.triggers = Account( account_id="100", public_key="xxx", private_key="yyy").triggers def test_fetch_all_returns_a_dictionary(self): MockAdapter.expected = [{'trigger_id': 201}] self.assertIsInstance(self.triggers.fetch_all(), dict) self.assertEqual(self.triggers.account.adapter.called, 1) self.assertEqual( self.triggers.account.adapter.call, ('GET', '/triggers', {})) def test_fetch_all_populates_collection(self): MockAdapter.expected = [{'trigger_id': 201}] self.assertEqual(0, len(self.triggers)) self.triggers.fetch_all() self.assertEqual(1, len(self.triggers)) def test_fetch_all_caches_results(self): MockAdapter.expected = [{'trigger_id': 201}] self.triggers.fetch_all() self.triggers.fetch_all() self.assertEqual(self.triggers.account.adapter.called, 1) def test_trigger_collection_object_can_be_accessed_like_a_dictionary(self): MockAdapter.expected = [{'trigger_id': 201}] self.triggers.fetch_all() self.assertIsInstance(self.triggers, AccountTriggerCollection) self.assertEqual(1, len(self.triggers)) self.assertIsInstance(self.triggers[201], Trigger) def test_find_one_by_trigger_id_returns_an_import_object(self): MockAdapter.expected = {'trigger_id': 201} search = self.triggers.find_one_by_trigger_id(201) self.assertIsInstance(search, Trigger) self.assertEqual(search['trigger_id'], 201) self.assertEqual(self.triggers.account.adapter.called, 1) self.assertEqual( self.triggers.account.adapter.call, ('GET', '/triggers/201', {})) def test_find_one_by_trigger_id_populates_collection(self): MockAdapter.expected = {'trigger_id': 201} self.triggers.find_one_by_trigger_id(201) self.assertIn(201, self.triggers) self.assertIsInstance(self.triggers[201], Trigger) self.assertEqual(self.triggers[201]['trigger_id'], 201) def test_find_one_by_trigger_id_caches_result(self): MockAdapter.expected = {'trigger_id': 201} self.triggers.find_one_by_trigger_id(201) self.triggers.find_one_by_trigger_id(201) self.assertEqual(self.triggers.account.adapter.called, 1) def test_dictionary_access_lazy_loads_by_trigger_id(self): MockAdapter.expected = {'trigger_id': 201} trigger = self.triggers[201] self.assertIn(201, self.triggers) self.assertIsInstance(trigger, Trigger) self.assertEqual(self.triggers[201]['trigger_id'], 201) self.assertEqual(self.triggers.account.adapter.called, 1) self.assertEqual( self.triggers.account.adapter.call, ('GET', '/triggers/201', {})) def test_dictionary_access_lazy_loads_by_trigger_id2(self): MockAdapter.expected = None search = self.triggers.get(204) self.assertEqual(0, len(self.triggers)) self.assertIsNone(search) self.assertEqual(self.triggers.account.adapter.called, 1) self.assertEqual( self.triggers.account.adapter.call, ('GET', '/triggers/204', {})) def test_can_delete_a_single_trigger_with_del(self): # Setup MockAdapter.expected = True self.triggers._dict = { 200: self.triggers.factory({'trigger_id': 200}), 201: self.triggers.factory({'trigger_id': 201}) } del(self.triggers[200]) self.assertEqual(self.triggers.account.adapter.called, 1) self.assertEqual( self.triggers.account.adapter.call, ('DELETE', '/triggers/200', {})) self.assertEqual(1, len(self.triggers)) self.assertIn(201, self.triggers) class AccountWebHookCollectionTest(unittest.TestCase): def setUp(self): Account.default_adapter = MockAdapter self.webhooks = Account( account_id="100", public_key="xxx", private_key="yyy").webhooks def test_fetch_all_returns_a_dictionary(self): MockAdapter.expected = [{'webhook_id': 201}] self.assertIsInstance(self.webhooks.fetch_all(), dict) self.assertEqual(self.webhooks.account.adapter.called, 1) self.assertEqual( self.webhooks.account.adapter.call, ('GET', '/webhooks', {})) def test_fetch_all_populates_collection(self): MockAdapter.expected = [{'webhook_id': 201}] self.assertEqual(0, len(self.webhooks)) self.webhooks.fetch_all() self.assertEqual(1, len(self.webhooks)) def test_fetch_all_caches_results(self): MockAdapter.expected = [{'webhook_id': 201}] self.webhooks.fetch_all() self.webhooks.fetch_all() self.assertEqual(self.webhooks.account.adapter.called, 1) def test_webhook_collection_object_can_be_accessed_like_a_dictionary(self): MockAdapter.expected = [{'webhook_id': 201}] self.webhooks.fetch_all() self.assertIsInstance(self.webhooks, AccountWebHookCollection) self.assertEqual(1, len(self.webhooks)) self.assertIsInstance(self.webhooks[201], WebHook) def test_find_one_by_webhook_id_returns_an_import_object(self): MockAdapter.expected = {'webhook_id': 201} webhook = self.webhooks.find_one_by_webhook_id(201) self.assertIsInstance(webhook, WebHook) self.assertEqual(webhook['webhook_id'], 201) self.assertEqual(self.webhooks.account.adapter.called, 1) self.assertEqual( self.webhooks.account.adapter.call, ('GET', '/webhooks/201', {})) def test_find_one_by_webhook_id_populates_collection(self): MockAdapter.expected = {'webhook_id': 201} self.webhooks.find_one_by_webhook_id(201) self.assertIn(201, self.webhooks) self.assertIsInstance(self.webhooks[201], WebHook) self.assertEqual(self.webhooks[201]['webhook_id'], 201) def test_find_one_by_webhook_id_caches_result(self): MockAdapter.expected = {'webhook_id': 201} self.webhooks.find_one_by_webhook_id(201) self.webhooks.find_one_by_webhook_id(201) self.assertEqual(self.webhooks.account.adapter.called, 1) def test_dictionary_access_lazy_loads_by_webhook_id(self): MockAdapter.expected = {'webhook_id': 201} webhook = self.webhooks[201] self.assertIn(201, self.webhooks) self.assertIsInstance(webhook, WebHook) self.assertEqual(self.webhooks[201]['webhook_id'], 201) self.assertEqual(self.webhooks.account.adapter.called, 1) self.assertEqual( self.webhooks.account.adapter.call, ('GET', '/webhooks/201', {})) def test_dictionary_access_lazy_loads_by_webhook_id2(self): MockAdapter.expected = None webhook = self.webhooks.get(204) self.assertEqual(0, len(self.webhooks)) self.assertIsNone(webhook) self.assertEqual(self.webhooks.account.adapter.called, 1) self.assertEqual( self.webhooks.account.adapter.call, ('GET', '/webhooks/204', {})) def test_can_delete_a_single_webhook_with_del(self): # Setup MockAdapter.expected = True self.webhooks._dict = { 200: self.webhooks.factory({'webhook_id': 200}), 201: self.webhooks.factory({'webhook_id': 201}) } del(self.webhooks[200]) self.assertEqual(self.webhooks.account.adapter.called, 1) self.assertEqual( self.webhooks.account.adapter.call, ('DELETE', '/webhooks/200', {})) self.assertEqual(1, len(self.webhooks)) self.assertIn(201, self.webhooks) def test_can_delete_all_webhooks(self): # Setup MockAdapter.expected = True self.webhooks._dict = { 200: WebHook(self.webhooks.account, {'webhook_id': 200}), 201: WebHook(self.webhooks.account, {'webhook_id': 201}) } result = self.webhooks.delete_all() self.assertIsNone(result) self.assertEqual(self.webhooks.account.adapter.called, 1) self.assertEqual( self.webhooks.account.adapter.call, ('DELETE', '/webhooks', {})) self.assertEqual(0, len(self.webhooks)) def test_can_delete_all_webhooks2(self): # Setup MockAdapter.expected = False self.webhooks._dict = { 200: WebHook(self.webhooks.account, {'webhook_id': 200}), 201: WebHook(self.webhooks.account, {'webhook_id': 201}) } with self.assertRaises(ex.WebHookDeleteError): self.webhooks.delete_all() self.assertEqual(self.webhooks.account.adapter.called, 1) self.assertEqual( self.webhooks.account.adapter.call, ('DELETE', '/webhooks', {})) self.assertEqual(2, len(self.webhooks)) def test_can_list_events(self): # Setup MockAdapter.expected = [ { "event_name": "mailing_finish", "webhook_event_id": 1, "description": "Fired when a mailing is finished." }, { "event_name": "mailing_start", "webhook_event_id": 2, "description": "Fired when a mailing starts." } ] result = self.webhooks.list_events() self.assertIsInstance(result, list) self.assertEqual(2, len(result)) self.assertEqual(self.webhooks.account.adapter.called, 1) self.assertEqual( self.webhooks.account.adapter.call, ('GET', '/webhooks/events', {})) self.assertEqual(0, len(self.webhooks)) class AccountWorkflowTest(unittest.TestCase): def setUp(self): Account.default_adapter = MockAdapter self.workflows = Account( account_id="100", public_key="xxx", private_key="yyy").workflows def test_fetch_all_returns_a_dictionary(self): MockAdapter.expected = [{'workflow_id': 201}] self.assertIsInstance(self.workflows.fetch_all(), dict) self.assertEqual(self.workflows.account.adapter.called, 1) self.assertEqual( self.workflows.account.adapter.call, ('GET', '/automation/workflows', {})) def test_fetch_all_returns_a_dictionary(self): MockAdapter.expected = [{'workflow_id': 201}] self.assertIsInstance(self.workflows.fetch_all(), dict) self.assertEqual(self.workflows.account.adapter.called, 1) self.assertEqual( self.workflows.account.adapter.call, ('GET', '/automation/workflows', {})) def test_find_one_by_workflow_id_returns_an_import_object(self): MockAdapter.expected = {'workflow_id': 201} workflow = self.workflows.find_one_by_workflow_id(201) self.assertIsInstance(workflow, Workflow) self.assertEqual(workflow['workflow_id'], 201) self.assertEqual(self.workflows.account.adapter.called, 1) self.assertEqual( self.workflows.account.adapter.call, ('GET', '/automation/workflows/201', {})) def test_find_one_by_workflow_id_populates_collection(self): MockAdapter.expected = {'workflow_id': 201} self.workflows.find_one_by_workflow_id(201) self.assertIn(201, self.workflows) self.assertIsInstance(self.workflows[201], Workflow) self.assertEqual(self.workflows[201]['workflow_id'], 201) def test_find_one_by_workflow_id_caches_result(self): MockAdapter.expected = {'workflow_id': 201} self.workflows.find_one_by_workflow_id(201) self.workflows.find_one_by_workflow_id(201) self.assertEqual(self.workflows.account.adapter.called, 1)
py
1a4b47b76906a51774359fdefc0883457ecae6d6
# standard library imports import os import secrets from contextlib import closing from urllib.parse import urlparse from functools import cached_property from mimetypes import guess_extension # pip imports from magic import from_buffer import psycopg2 from flask import url_for, current_app from werkzeug.datastructures import FileStorage from werkzeug.utils import safe_join, secure_filename # local imports from app import config from app.helpers.utils import create_hmac_hexdigest from app.helpers.discord import FileEmbed, ShortUrlEmbed conn = psycopg2.connect("user=postgres1 dbname=shrpy password=postgres") conn.set_session(autocommit=True) with conn.cursor() as cursor: cursor.execute("CREATE TABLE IF NOT EXISTS urls (token VARCHAR(10) NOT NULL PRIMARY KEY, url TEXT NOT NULL)") class File: def __init__(self, file_instance: FileStorage, use_original_filename=True): """Class for uploaded files which takes the `werkzeug.datastructures.FileStorage` from `flask.Request.files` as first parameter.""" if isinstance(file_instance, FileStorage) is False: raise InvalidFileException(file_instance) self.use_original_filename = use_original_filename # Private FileStorage instance self.__file = file_instance @cached_property def filename(self) -> str: """Returns random filename.""" custom_filename = secrets.token_urlsafe(config.FILE_TOKEN_BYTES) if self.use_original_filename: filename = f'{custom_filename}-{self.original_filename_root[:config.ORIGINAL_FILENAME_LENGTH]}' else: filename = custom_filename return f'{filename}.{self.extension}' @cached_property def extension(self) -> str: """Returns extension using `python-magic` and `mimetypes`.""" file_bytes = self.__file.read(config.MAGIC_BUFFER_BYTES) mime = from_buffer(file_bytes, mime=True).lower() ext = guess_extension(mime) if ext is None: current_app.logger.error(f'Unable to determine file extension for file {self.__file.filename} - MIME type {mime}') return ext.replace('.', '') @cached_property def original_filename_root(self): """Returns the original filename without extension.""" sec_filename = secure_filename(self.__file.filename.lower()) root, ext = os.path.splitext(sec_filename) return root @cached_property def hmac(self) -> str: """Returns HMAC digest calculated from filename, `flask.current_app.secret_key` is used as secret.""" return create_hmac_hexdigest(self.filename, current_app.secret_key) @cached_property def url(self) -> str: """Returns file URL using `flask.url_for`.""" return url_for('main.uploads', filename=self.filename, _external=True) @cached_property def deletion_url(self) -> str: """Returns deletion URL using `flask.url_for`.""" return url_for('api.delete_file', hmac_hash=self.hmac, filename=self.filename, _external=True) @staticmethod def delete(filename: str) -> bool: """Deletes the file from `config.UPLOAD_DIR`, if it exists.""" file_path = safe_join(config.UPLOAD_DIR, filename) if os.path.isfile(file_path) is False: return False current_app.logger.info(f'Deleted file {file_path}') os.remove(file_path) return True def is_allowed(self) -> bool: """Check if file is allowed, based on `config.ALLOWED_EXTENSIONS`.""" if not config.ALLOWED_EXTENSIONS: return True allowed = self.extension in config.ALLOWED_EXTENSIONS if allowed is False: current_app.logger.warning(f'File {self.__file.filename} (detected extension {self.extension}) is not allowed') return allowed def save(self, save_directory = config.UPLOAD_DIR) -> None: """Saves the file to `UPLOAD_DIR`.""" if os.path.isdir(save_directory) is False: os.makedirs(save_directory) save_path = safe_join(save_directory, self.filename) current_app.logger.info(f'Saving file {self.__file.filename} to {save_path}') current_app.logger.info(f'URLs: {self.url} - {self.deletion_url}') # Set file descriptor back to beginning of the file so save works correctly self.__file.seek(os.SEEK_SET) self.__file.save(save_path) def embed(self) -> FileEmbed: """Returns FileEmbed instance for this file.""" return FileEmbed( content_url=self.url, deletion_url=self.deletion_url ) class InvalidFileException(Exception): """Raised when `File` is initialized using wrong `file_instance`.""" def __init__(self, file_instance, *args): self.file_instance = file_instance super().__init__(*args) def __str__(self): file_instance_type = type(self.file_instance) return f'{self.file_instance} ({file_instance_type}) is not an instance of werkzeug.datastructures.FileStorage ({FileStorage})' class ShortUrl: def __init__(self, url: str): url = ''.join(url.lower().split()) if not url.startswith(('https://', 'http://')): url = f'https://{url}' self.url = url @cached_property def token(self) -> str: return secrets.token_urlsafe(config.URL_TOKEN_BYTES) @cached_property def hmac(self) -> str: """Returns HMAC hash calculated from token, `flask.current_app.secret_key` is used as secret.""" return create_hmac_hexdigest(self.token, current_app.secret_key) @cached_property def shortened_url(self) -> str: """Returns the shortened URL using `flask.url_for`.""" return url_for('main.short_url', token=self.token, _external=True) @cached_property def deletion_url(self) -> str: """Returns deletion URL using `flask.url_for`.""" return url_for('api.delete_short_url', hmac_hash=self.hmac, token=self.token, _external=True) def is_valid(self) -> bool: """Checks if URL is valid""" parsed = urlparse(self.url) # Parsed URL must have at least scheme and netloc (e.g. domain name) try: valid = all([parsed.scheme, parsed.netloc]) and parsed.netloc.split('.')[1] except IndexError: valid = False if valid is False: current_app.logger.warning(f'URL {self.url} is invalid') return valid def add(self): """Inserts the URL and token to database.""" current_app.logger.info(f'Saving short URL for {self.url} as {self.shortened_url}') current_app.logger.info(f'URLs: {self.shortened_url} - {self.deletion_url}') with closing(self.get_cursor()) as cursor: cursor.execute("INSERT INTO urls VALUES (%s, %s)", ( self.token, self.url )) def embed(self) -> ShortUrlEmbed: """Returns ShorturlEmbed instance for this URL.""" return ShortUrlEmbed( content_url=self.shortened_url, deletion_url=self.deletion_url, original_url=self.url, shortened_url=self.shortened_url ) @classmethod def get_by_token(cls, token: str): """Returns the URL for given token from database.""" result = None with closing(cls.get_cursor()) as cursor: row = cursor.execute("SELECT url FROM urls WHERE token = %s", (token,)) url_row = row.fetchone() if url_row: result = url_row['url'] return result @classmethod def delete(cls, token: str) -> bool: """DELETEs URL using given token from database.""" url = cls.get_by_token(token) with closing(cls.get_cursor()) as cursor: execute = cursor.execute("DELETE FROM urls WHERE token = %s", (token,)) deleted = execute.rowcount > 0 if deleted: current_app.logger.info(f'Deleted short URL for {url} using token {token}') return deleted @staticmethod def get_cursor(): cursor = conn.cursor() return cursor
py
1a4b486088aa4748570e900fc8321cf54148823b
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # this file contains helper methods for BBOX processing from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np import random import math import cv2 def meet_emit_constraint(src_bbox, sample_bbox): center_x = (src_bbox[2] + src_bbox[0]) / 2 center_y = (src_bbox[3] + src_bbox[1]) / 2 if center_x >= sample_bbox[0] and \ center_x <= sample_bbox[2] and \ center_y >= sample_bbox[1] and \ center_y <= sample_bbox[3]: return True return False def clip_bbox(src_bbox): src_bbox[0] = max(min(src_bbox[0], 1.0), 0.0) src_bbox[1] = max(min(src_bbox[1], 1.0), 0.0) src_bbox[2] = max(min(src_bbox[2], 1.0), 0.0) src_bbox[3] = max(min(src_bbox[3], 1.0), 0.0) return src_bbox def bbox_area(src_bbox): if src_bbox[2] < src_bbox[0] or src_bbox[3] < src_bbox[1]: return 0. else: width = src_bbox[2] - src_bbox[0] height = src_bbox[3] - src_bbox[1] return width * height def is_overlap(object_bbox, sample_bbox): if object_bbox[0] >= sample_bbox[2] or \ object_bbox[2] <= sample_bbox[0] or \ object_bbox[1] >= sample_bbox[3] or \ object_bbox[3] <= sample_bbox[1]: return False else: return True def filter_and_process(sample_bbox, bboxes, labels, scores=None, keypoints=None): new_bboxes = [] new_labels = [] new_scores = [] new_keypoints = [] new_kp_ignore = [] for i in range(len(bboxes)): new_bbox = [0, 0, 0, 0] obj_bbox = [bboxes[i][0], bboxes[i][1], bboxes[i][2], bboxes[i][3]] if not meet_emit_constraint(obj_bbox, sample_bbox): continue if not is_overlap(obj_bbox, sample_bbox): continue sample_width = sample_bbox[2] - sample_bbox[0] sample_height = sample_bbox[3] - sample_bbox[1] new_bbox[0] = (obj_bbox[0] - sample_bbox[0]) / sample_width new_bbox[1] = (obj_bbox[1] - sample_bbox[1]) / sample_height new_bbox[2] = (obj_bbox[2] - sample_bbox[0]) / sample_width new_bbox[3] = (obj_bbox[3] - sample_bbox[1]) / sample_height new_bbox = clip_bbox(new_bbox) if bbox_area(new_bbox) > 0: new_bboxes.append(new_bbox) new_labels.append([labels[i][0]]) if scores is not None: new_scores.append([scores[i][0]]) if keypoints is not None: sample_keypoint = keypoints[0][i] for j in range(len(sample_keypoint)): kp_len = sample_height if j % 2 else sample_width sample_coord = sample_bbox[1] if j % 2 else sample_bbox[0] sample_keypoint[j] = ( sample_keypoint[j] - sample_coord) / kp_len sample_keypoint[j] = max(min(sample_keypoint[j], 1.0), 0.0) new_keypoints.append(sample_keypoint) new_kp_ignore.append(keypoints[1][i]) bboxes = np.array(new_bboxes) labels = np.array(new_labels) scores = np.array(new_scores) if keypoints is not None: keypoints = np.array(new_keypoints) new_kp_ignore = np.array(new_kp_ignore) return bboxes, labels, scores, (keypoints, new_kp_ignore) return bboxes, labels, scores def bbox_area_sampling(bboxes, labels, scores, target_size, min_size): new_bboxes = [] new_labels = [] new_scores = [] for i, bbox in enumerate(bboxes): w = float((bbox[2] - bbox[0]) * target_size) h = float((bbox[3] - bbox[1]) * target_size) if w * h < float(min_size * min_size): continue else: new_bboxes.append(bbox) new_labels.append(labels[i]) if scores is not None and scores.size != 0: new_scores.append(scores[i]) bboxes = np.array(new_bboxes) labels = np.array(new_labels) scores = np.array(new_scores) return bboxes, labels, scores def generate_sample_bbox(sampler): scale = np.random.uniform(sampler[2], sampler[3]) aspect_ratio = np.random.uniform(sampler[4], sampler[5]) aspect_ratio = max(aspect_ratio, (scale**2.0)) aspect_ratio = min(aspect_ratio, 1 / (scale**2.0)) bbox_width = scale * (aspect_ratio**0.5) bbox_height = scale / (aspect_ratio**0.5) xmin_bound = 1 - bbox_width ymin_bound = 1 - bbox_height xmin = np.random.uniform(0, xmin_bound) ymin = np.random.uniform(0, ymin_bound) xmax = xmin + bbox_width ymax = ymin + bbox_height sampled_bbox = [xmin, ymin, xmax, ymax] return sampled_bbox def generate_sample_bbox_square(sampler, image_width, image_height): scale = np.random.uniform(sampler[2], sampler[3]) aspect_ratio = np.random.uniform(sampler[4], sampler[5]) aspect_ratio = max(aspect_ratio, (scale**2.0)) aspect_ratio = min(aspect_ratio, 1 / (scale**2.0)) bbox_width = scale * (aspect_ratio**0.5) bbox_height = scale / (aspect_ratio**0.5) if image_height < image_width: bbox_width = bbox_height * image_height / image_width else: bbox_height = bbox_width * image_width / image_height xmin_bound = 1 - bbox_width ymin_bound = 1 - bbox_height xmin = np.random.uniform(0, xmin_bound) ymin = np.random.uniform(0, ymin_bound) xmax = xmin + bbox_width ymax = ymin + bbox_height sampled_bbox = [xmin, ymin, xmax, ymax] return sampled_bbox def data_anchor_sampling(bbox_labels, image_width, image_height, scale_array, resize_width): num_gt = len(bbox_labels) # np.random.randint range: [low, high) rand_idx = np.random.randint(0, num_gt) if num_gt != 0 else 0 if num_gt != 0: norm_xmin = bbox_labels[rand_idx][0] norm_ymin = bbox_labels[rand_idx][1] norm_xmax = bbox_labels[rand_idx][2] norm_ymax = bbox_labels[rand_idx][3] xmin = norm_xmin * image_width ymin = norm_ymin * image_height wid = image_width * (norm_xmax - norm_xmin) hei = image_height * (norm_ymax - norm_ymin) range_size = 0 area = wid * hei for scale_ind in range(0, len(scale_array) - 1): if area > scale_array[scale_ind] ** 2 and area < \ scale_array[scale_ind + 1] ** 2: range_size = scale_ind + 1 break if area > scale_array[len(scale_array) - 2]**2: range_size = len(scale_array) - 2 scale_choose = 0.0 if range_size == 0: rand_idx_size = 0 else: # np.random.randint range: [low, high) rng_rand_size = np.random.randint(0, range_size + 1) rand_idx_size = rng_rand_size % (range_size + 1) if rand_idx_size == range_size: min_resize_val = scale_array[rand_idx_size] / 2.0 max_resize_val = min(2.0 * scale_array[rand_idx_size], 2 * math.sqrt(wid * hei)) scale_choose = random.uniform(min_resize_val, max_resize_val) else: min_resize_val = scale_array[rand_idx_size] / 2.0 max_resize_val = 2.0 * scale_array[rand_idx_size] scale_choose = random.uniform(min_resize_val, max_resize_val) sample_bbox_size = wid * resize_width / scale_choose w_off_orig = 0.0 h_off_orig = 0.0 if sample_bbox_size < max(image_height, image_width): if wid <= sample_bbox_size: w_off_orig = np.random.uniform(xmin + wid - sample_bbox_size, xmin) else: w_off_orig = np.random.uniform(xmin, xmin + wid - sample_bbox_size) if hei <= sample_bbox_size: h_off_orig = np.random.uniform(ymin + hei - sample_bbox_size, ymin) else: h_off_orig = np.random.uniform(ymin, ymin + hei - sample_bbox_size) else: w_off_orig = np.random.uniform(image_width - sample_bbox_size, 0.0) h_off_orig = np.random.uniform(image_height - sample_bbox_size, 0.0) w_off_orig = math.floor(w_off_orig) h_off_orig = math.floor(h_off_orig) # Figure out top left coordinates. w_off = float(w_off_orig / image_width) h_off = float(h_off_orig / image_height) sampled_bbox = [ w_off, h_off, w_off + float(sample_bbox_size / image_width), h_off + float(sample_bbox_size / image_height) ] return sampled_bbox else: return 0 def jaccard_overlap(sample_bbox, object_bbox): if sample_bbox[0] >= object_bbox[2] or \ sample_bbox[2] <= object_bbox[0] or \ sample_bbox[1] >= object_bbox[3] or \ sample_bbox[3] <= object_bbox[1]: return 0 intersect_xmin = max(sample_bbox[0], object_bbox[0]) intersect_ymin = max(sample_bbox[1], object_bbox[1]) intersect_xmax = min(sample_bbox[2], object_bbox[2]) intersect_ymax = min(sample_bbox[3], object_bbox[3]) intersect_size = (intersect_xmax - intersect_xmin) * ( intersect_ymax - intersect_ymin) sample_bbox_size = bbox_area(sample_bbox) object_bbox_size = bbox_area(object_bbox) overlap = intersect_size / ( sample_bbox_size + object_bbox_size - intersect_size) return overlap def intersect_bbox(bbox1, bbox2): if bbox2[0] > bbox1[2] or bbox2[2] < bbox1[0] or \ bbox2[1] > bbox1[3] or bbox2[3] < bbox1[1]: intersection_box = [0.0, 0.0, 0.0, 0.0] else: intersection_box = [ max(bbox1[0], bbox2[0]), max(bbox1[1], bbox2[1]), min(bbox1[2], bbox2[2]), min(bbox1[3], bbox2[3]) ] return intersection_box def bbox_coverage(bbox1, bbox2): inter_box = intersect_bbox(bbox1, bbox2) intersect_size = bbox_area(inter_box) if intersect_size > 0: bbox1_size = bbox_area(bbox1) return intersect_size / bbox1_size else: return 0. def satisfy_sample_constraint(sampler, sample_bbox, gt_bboxes, satisfy_all=False): if sampler[6] == 0 and sampler[7] == 0: return True satisfied = [] for i in range(len(gt_bboxes)): object_bbox = [ gt_bboxes[i][0], gt_bboxes[i][1], gt_bboxes[i][2], gt_bboxes[i][3] ] overlap = jaccard_overlap(sample_bbox, object_bbox) if sampler[6] != 0 and \ overlap < sampler[6]: satisfied.append(False) continue if sampler[7] != 0 and \ overlap > sampler[7]: satisfied.append(False) continue satisfied.append(True) if not satisfy_all: return True if satisfy_all: return np.all(satisfied) else: return False def satisfy_sample_constraint_coverage(sampler, sample_bbox, gt_bboxes): if sampler[6] == 0 and sampler[7] == 0: has_jaccard_overlap = False else: has_jaccard_overlap = True if sampler[8] == 0 and sampler[9] == 0: has_object_coverage = False else: has_object_coverage = True if not has_jaccard_overlap and not has_object_coverage: return True found = False for i in range(len(gt_bboxes)): object_bbox = [ gt_bboxes[i][0], gt_bboxes[i][1], gt_bboxes[i][2], gt_bboxes[i][3] ] if has_jaccard_overlap: overlap = jaccard_overlap(sample_bbox, object_bbox) if sampler[6] != 0 and \ overlap < sampler[6]: continue if sampler[7] != 0 and \ overlap > sampler[7]: continue found = True if has_object_coverage: object_coverage = bbox_coverage(object_bbox, sample_bbox) if sampler[8] != 0 and \ object_coverage < sampler[8]: continue if sampler[9] != 0 and \ object_coverage > sampler[9]: continue found = True if found: return True return found def crop_image_sampling(img, sample_bbox, image_width, image_height, target_size): # no clipping here xmin = int(sample_bbox[0] * image_width) xmax = int(sample_bbox[2] * image_width) ymin = int(sample_bbox[1] * image_height) ymax = int(sample_bbox[3] * image_height) w_off = xmin h_off = ymin width = xmax - xmin height = ymax - ymin cross_xmin = max(0.0, float(w_off)) cross_ymin = max(0.0, float(h_off)) cross_xmax = min(float(w_off + width - 1.0), float(image_width)) cross_ymax = min(float(h_off + height - 1.0), float(image_height)) cross_width = cross_xmax - cross_xmin cross_height = cross_ymax - cross_ymin roi_xmin = 0 if w_off >= 0 else abs(w_off) roi_ymin = 0 if h_off >= 0 else abs(h_off) roi_width = cross_width roi_height = cross_height roi_y1 = int(roi_ymin) roi_y2 = int(roi_ymin + roi_height) roi_x1 = int(roi_xmin) roi_x2 = int(roi_xmin + roi_width) cross_y1 = int(cross_ymin) cross_y2 = int(cross_ymin + cross_height) cross_x1 = int(cross_xmin) cross_x2 = int(cross_xmin + cross_width) sample_img = np.zeros((height, width, 3)) sample_img[roi_y1: roi_y2, roi_x1: roi_x2] = \ img[cross_y1: cross_y2, cross_x1: cross_x2] sample_img = cv2.resize( sample_img, (target_size, target_size), interpolation=cv2.INTER_AREA) return sample_img def is_poly(segm): assert isinstance(segm, (list, dict)), \ "Invalid segm type: {}".format(type(segm)) return isinstance(segm, list) def gaussian_radius(bbox_size, min_overlap): height, width = bbox_size a1 = 1 b1 = (height + width) c1 = width * height * (1 - min_overlap) / (1 + min_overlap) sq1 = np.sqrt(b1**2 - 4 * a1 * c1) radius1 = (b1 + sq1) / (2 * a1) a2 = 4 b2 = 2 * (height + width) c2 = (1 - min_overlap) * width * height sq2 = np.sqrt(b2**2 - 4 * a2 * c2) radius2 = (b2 + sq2) / 2 a3 = 4 * min_overlap b3 = -2 * min_overlap * (height + width) c3 = (min_overlap - 1) * width * height sq3 = np.sqrt(b3**2 - 4 * a3 * c3) radius3 = (b3 + sq3) / 2 return min(radius1, radius2, radius3) def draw_gaussian(heatmap, center, radius, k=1, delte=6): diameter = 2 * radius + 1 sigma = diameter / delte gaussian = gaussian2D((diameter, diameter), sigma_x=sigma, sigma_y=sigma) x, y = center height, width = heatmap.shape[0:2] left, right = min(x, radius), min(width - x, radius + 1) top, bottom = min(y, radius), min(height - y, radius + 1) masked_heatmap = heatmap[y - top:y + bottom, x - left:x + right] masked_gaussian = gaussian[radius - top:radius + bottom, radius - left: radius + right] np.maximum(masked_heatmap, masked_gaussian * k, out=masked_heatmap) def gaussian2D(shape, sigma_x=1, sigma_y=1): m, n = [(ss - 1.) / 2. for ss in shape] y, x = np.ogrid[-m:m + 1, -n:n + 1] h = np.exp(-(x * x / (2 * sigma_x * sigma_x) + y * y / (2 * sigma_y * sigma_y))) h[h < np.finfo(h.dtype).eps * h.max()] = 0 return h def draw_umich_gaussian(heatmap, center, radius, k=1): """ draw_umich_gaussian, refer to https://github.com/xingyizhou/CenterNet/blob/master/src/lib/utils/image.py#L126 """ diameter = 2 * radius + 1 gaussian = gaussian2D( (diameter, diameter), sigma_x=diameter / 6, sigma_y=diameter / 6) x, y = int(center[0]), int(center[1]) height, width = heatmap.shape[0:2] left, right = min(x, radius), min(width - x, radius + 1) top, bottom = min(y, radius), min(height - y, radius + 1) masked_heatmap = heatmap[y - top:y + bottom, x - left:x + right] masked_gaussian = gaussian[radius - top:radius + bottom, radius - left: radius + right] if min(masked_gaussian.shape) > 0 and min(masked_heatmap.shape) > 0: np.maximum(masked_heatmap, masked_gaussian * k, out=masked_heatmap) return heatmap def get_border(border, size): i = 1 while size - border // i <= border // i: i *= 2 return border // i
py
1a4b48d27af07be23bb06898e560fd762073f11c
from typing import Union, List class DaysOfWeek: """An object that stores a boolean value for each day of the week. It can read or produce a one byte code compatible with what AlarmClock uses. """ days = { 'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 5, 'Saturday': 6, 'Sunday': 7, } def __init__(self, code: int = 0): """Initialize the DaysOfWeek object with specified code or 0x00. Code is a single byte binary representation of the object. 0x00 means all stored values are False. """ # Filter out bit 0. It has no meaning and should always be zero. self.code = code & 0xFE @classmethod def from_list(cls, li: Union[List[str], List[int]]): """Create an instance from a list of str names or numbers of days.""" dow = cls() for day in li: dow.set_day(day, True) return dow def get_day(self, day: Union[str, int]) -> bool: """Get the boolean value for a single day of the week.""" if isinstance(day, str): if day not in self.days: raise TypeError(f'unknown day: {repr(day)}') day = self.days[day] if day < 1 or day > 7: raise ValueError(f"{day} is not a valid day of the week") return self.code & (2**day) > 0 def set_day(self, day: Union[str, int], value: bool) -> None: """Set the boolean value for a single day of the week.""" if isinstance(day, str): if day not in self.days: raise TypeError(f'unknown day: {repr(day)}') day = self.days[day] if day < 1 or day > 7: raise ValueError(f"{day} is not a valid day of the week") if value: self.code |= (2**day) else: self.code &= ~(2**day) Monday = property(lambda self: self.get_day('Monday'), lambda self, value: self.set_day('Monday', value)) Tuesday = property(lambda self: self.get_day('Tuesday'), lambda self, value: self.set_day('Tuesday', value)) Wednesday = property(lambda self: self.get_day('Wednesday'), lambda self, value: self.set_day('Wednesday', value)) Thursday = property(lambda self: self.get_day('Thursday'), lambda self, value: self.set_day('Thursday', value)) Friday = property(lambda self: self.get_day('Friday'), lambda self, value: self.set_day('Friday', value)) Saturday = property(lambda self: self.get_day('Saturday'), lambda self, value: self.set_day('Saturday', value)) Sunday = property(lambda self: self.get_day('Sunday'), lambda self, value: self.set_day('Sunday', value)) @property def active_days(self) -> List[str]: """Get an array of days of the week for which the stored value is True. Names of the days of the week are returned as strings with the first letter capitalized. """ return [day for day in self.days if self.get_day(day)] def __str__(self) -> str: """Get all days for which the stored value is True joined with ', '.""" return ', '.join(self.active_days) def __repr__(self) -> str: return f'{self.__class__.__name__}({repr(self.code)})' def __eq__(self, other): return self.code == other.code
py
1a4b4901ad77222c0a4aa5f4dbdaf9082c739dba
''' Faça um programa que tenha uma função notas() que pode receber varias notas de alunos e vai retornar um dicionario com as seguintes informações: Quantidade de notas A maior nota A menor nota A média da turma A situação (Opcional) Adicione também as docstrings de função ''' from collections import OrderedDict def notas(*resp, sit=False): """ -> Função para analisar notas :param resp: um ou mais notas dos alunos :param sit: valor opicional :return:dicionário que retorna todos os valores """ r = dict() r['total'] = len(resp) r['maior'] = max(resp) r['menor'] = min(resp) r['media'] = sum(resp)/len(resp) sit = bool if sit: if r['media'] < 7: print('\033[31mSITUAÇÃO RUIM...') elif r['media'] >= 7: print('\033[94mSITUAÇÃO ÓTIMA...') elif r['media'] >= 5 and r['media'] <= 6: print('\033[33mSITUAÇÃO RAZOÁVEL') if sit == False: del sit return r #calcula todas as notas cadastradas e gera uma média #Programa Principal n = notas(1.5, 2.0, 0.9, sit=False) print(n) help(notas)
py
1a4b495297e1caf96441f900b901aaa1182cc214
"""Support for wired switches attached to a Konnected device.""" import logging from homeassistant.components.konnected import ( DOMAIN as KONNECTED_DOMAIN, PIN_TO_ZONE, CONF_ACTIVATION, CONF_MOMENTARY, CONF_PAUSE, CONF_REPEAT, STATE_LOW, STATE_HIGH) from homeassistant.helpers.entity import ToggleEntity from homeassistant.const import ( ATTR_STATE, CONF_DEVICES, CONF_NAME, CONF_PIN, CONF_SWITCHES) _LOGGER = logging.getLogger(__name__) DEPENDENCIES = ['konnected'] async def async_setup_platform( hass, config, async_add_entities, discovery_info=None): """Set switches attached to a Konnected device.""" if discovery_info is None: return data = hass.data[KONNECTED_DOMAIN] device_id = discovery_info['device_id'] switches = [ KonnectedSwitch(device_id, pin_data.get(CONF_PIN), pin_data) for pin_data in data[CONF_DEVICES][device_id][CONF_SWITCHES]] async_add_entities(switches) class KonnectedSwitch(ToggleEntity): """Representation of a Konnected switch.""" def __init__(self, device_id, pin_num, data): """Initialize the Konnected switch.""" self._data = data self._device_id = device_id self._pin_num = pin_num self._activation = self._data.get(CONF_ACTIVATION, STATE_HIGH) self._momentary = self._data.get(CONF_MOMENTARY) self._pause = self._data.get(CONF_PAUSE) self._repeat = self._data.get(CONF_REPEAT) self._state = self._boolean_state(self._data.get(ATTR_STATE)) self._unique_id = '{}-{}'.format(device_id, PIN_TO_ZONE[pin_num]) self._name = self._data.get(CONF_NAME) @property def unique_id(self) -> str: """Return the unique id.""" return self._unique_id @property def name(self): """Return the name of the switch.""" return self._name @property def is_on(self): """Return the status of the sensor.""" return self._state @property def client(self): """Return the Konnected HTTP client.""" return \ self.hass.data[KONNECTED_DOMAIN][CONF_DEVICES][self._device_id].\ get('client') def turn_on(self, **kwargs): """Send a command to turn on the switch.""" resp = self.client.put_device( self._pin_num, int(self._activation == STATE_HIGH), self._momentary, self._repeat, self._pause ) if resp.get(ATTR_STATE) is not None: self._set_state(True) if self._momentary and resp.get(ATTR_STATE) != -1: # Immediately set the state back off for momentary switches self._set_state(False) def turn_off(self, **kwargs): """Send a command to turn off the switch.""" resp = self.client.put_device( self._pin_num, int(self._activation == STATE_LOW)) if resp.get(ATTR_STATE) is not None: self._set_state(self._boolean_state(resp.get(ATTR_STATE))) def _boolean_state(self, int_state): if int_state is None: return False if int_state == 0: return self._activation == STATE_LOW if int_state == 1: return self._activation == STATE_HIGH def _set_state(self, state): self._state = state self.schedule_update_ha_state() _LOGGER.debug('Setting status of %s actuator pin %s to %s', self._device_id, self.name, state) async def async_added_to_hass(self): """Store entity_id.""" self._data['entity_id'] = self.entity_id
py
1a4b4a489e92e6eb0c1eeb829f93c9ed0606aa19
# coding: utf-8 import pprint import re import six class VideoContrast: """ Attributes: openapi_types (dict): The key is attribute name and the value is attribute type. attribute_map (dict): The key is attribute name and the value is json key in definition. """ sensitive_list = [] openapi_types = { 'name': 'str', 'execution_order': 'int', 'contrast': 'str', 'brightness': 'str' } attribute_map = { 'name': 'name', 'execution_order': 'execution_order', 'contrast': 'contrast', 'brightness': 'brightness' } def __init__(self, name=None, execution_order=None, contrast=None, brightness=None): """VideoContrast - a model defined in huaweicloud sdk""" self._name = None self._execution_order = None self._contrast = None self._brightness = None self.discriminator = None if name is not None: self.name = name if execution_order is not None: self.execution_order = execution_order if contrast is not None: self.contrast = contrast if brightness is not None: self.brightness = brightness @property def name(self): """Gets the name of this VideoContrast. 对比度算法名称\"hw-contrast\"。 :return: The name of this VideoContrast. :rtype: str """ return self._name @name.setter def name(self, name): """Sets the name of this VideoContrast. 对比度算法名称\"hw-contrast\"。 :param name: The name of this VideoContrast. :type: str """ self._name = name @property def execution_order(self): """Gets the execution_order of this VideoContrast. 1 表示视频处理时第一个执行,2表示第二个执行,以此类推;除不执行,各视频处理算法的执行次序不可相同。 :return: The execution_order of this VideoContrast. :rtype: int """ return self._execution_order @execution_order.setter def execution_order(self, execution_order): """Sets the execution_order of this VideoContrast. 1 表示视频处理时第一个执行,2表示第二个执行,以此类推;除不执行,各视频处理算法的执行次序不可相同。 :param execution_order: The execution_order of this VideoContrast. :type: int """ self._execution_order = execution_order @property def contrast(self): """Gets the contrast of this VideoContrast. 对比度调节的程度, 值越大, 对比度越高。 :return: The contrast of this VideoContrast. :rtype: str """ return self._contrast @contrast.setter def contrast(self, contrast): """Sets the contrast of this VideoContrast. 对比度调节的程度, 值越大, 对比度越高。 :param contrast: The contrast of this VideoContrast. :type: str """ self._contrast = contrast @property def brightness(self): """Gets the brightness of this VideoContrast. 1 表示视频处理时第一个执行,2表示第二个执行,以此类推;除不执行,各视频处理算法的执行次序不可相同。 :return: The brightness of this VideoContrast. :rtype: str """ return self._brightness @brightness.setter def brightness(self, brightness): """Sets the brightness of this VideoContrast. 1 表示视频处理时第一个执行,2表示第二个执行,以此类推;除不执行,各视频处理算法的执行次序不可相同。 :param brightness: The brightness of this VideoContrast. :type: str """ self._brightness = brightness def to_dict(self): """Returns the model properties as a dict""" result = {} for attr, _ in six.iteritems(self.openapi_types): value = getattr(self, attr) if isinstance(value, list): result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() elif isinstance(value, dict): result[attr] = dict(map( lambda item: (item[0], item[1].to_dict()) if hasattr(item[1], "to_dict") else item, value.items() )) else: if attr in self.sensitive_list: result[attr] = "****" else: result[attr] = value return result def to_str(self): """Returns the string representation of the model""" return pprint.pformat(self.to_dict()) def __repr__(self): """For `print` and `pprint`""" return self.to_str() def __eq__(self, other): """Returns true if both objects are equal""" if not isinstance(other, VideoContrast): return False return self.__dict__ == other.__dict__ def __ne__(self, other): """Returns true if both objects are not equal""" return not self == other
py
1a4b4a6cdbfbebc7638db1f6d195e975804a86d5
import argparse import os.path as osp import shutil import time from vedacore.misc import Config, mkdir_or_exist, set_random_seed from vedacore.parallel import init_dist from vedadet.assembler import trainval from vedadet.misc import get_root_logger def parse_args(): parser = argparse.ArgumentParser(description='Train a detector') parser.add_argument('config', help='train config file path') parser.add_argument('--workdir', help='the dir to save logs and models') parser.add_argument( '--launcher', choices=['none', 'pytorch'], default='none', help='job launcher') parser.add_argument('--local_rank', type=int, default=0) # TODO args = parser.parse_args() return args def main(): args = parse_args() cfg = Config.fromfile(args.config) if args.launcher == 'none': distributed = False else: distributed = True init_dist(args.launcher, **cfg.dist_params) # workdir is determined in this priority: CLI > segment in file > filename if args.workdir is not None: # update configs according to CLI args if args.work_dir is not None cfg.workdir = args.workdir elif cfg.get('workdir', None) is None: # use config filename as default work_dir if cfg.work_dir is None cfg.workdir = osp.join('./workdir', osp.splitext(osp.basename(args.config))[0]) seed = cfg.get('seed', None) deterministic = cfg.get('deterministic', False) set_random_seed(seed, deterministic) # create work_dir mkdir_or_exist(osp.abspath(cfg.workdir)) shutil.copy(args.config, cfg.workdir) # init the logger before other steps timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime()) log_file = osp.join(cfg.workdir, f'{timestamp}.log') logger = get_root_logger(log_file=log_file, log_level=cfg.log_level) # print('local_rank', args.local_rank) trainval(cfg, distributed, logger) if __name__ == '__main__': main()
py
1a4b4ac8e81affdced98eb1c0c5373ca8459cd6a
import os import lit.util # pylint: disable=import-error import libcxx.test.config import libcxx.test.target_info import libcxx.android.build import libcxx.ndk.test.format class AndroidTargetInfo(libcxx.test.target_info.DefaultTargetInfo): def platform(self): return 'android' def system(self): raise NotImplementedError def add_cxx_compile_flags(self, flags): flags.extend(['-D__STDC_FORMAT_MACROS']) def platform_ver(self): raise NotImplementedError def platform_name(self): raise NotImplementedError def supports_locale(self, loc): raise NotImplementedError class Configuration(libcxx.test.config.Configuration): def __init__(self, lit_config, config): super(Configuration, self).__init__(lit_config, config) self.cxx_under_test = None self.build_cmds_dir = None self.cxx_template = None self.link_template = None self.with_availability = False def configure(self): self.configure_target_info() self.configure_cxx() self.configure_triple() self.configure_src_root() self.configure_obj_root() self.configure_cxx_stdlib_under_test() self.configure_cxx_library_root() self.configure_compile_flags() self.configure_link_flags() self.configure_features() def configure_target_info(self): self.target_info = AndroidTargetInfo(self) def configure_compile_flags(self): super(Configuration, self).configure_compile_flags() self.cxx.flags.append('-stdlib=libc++') arch = self.get_lit_conf('arch') if arch == 'arm': self.cxx.flags.extend([ '-march=armv7-a', '-mfloat-abi=softfp', '-mfpu=vfpv3-d16', '-mthumb', ]) def configure_link_flags(self): triple = self.get_lit_conf('target_triple') if triple.startswith('arm-') or triple.startswith('armv7-'): self.cxx.link_flags.append('-Wl,--exclude-libs,libunwind.a') self.cxx.link_flags.append('-lcompiler_rt-extras') self.cxx.link_flags.append( '-Wl,--exclude-libs,libcompiler_rt-extras.a') self.cxx.link_flags.append('-Wl,--exclude-libs,libatomic.a') self.cxx.link_flags.append('-Wl,--exclude-libs,libgcc.a') self.cxx.link_flags.append('-pie') def configure_features(self): self.config.available_features.add(self.get_lit_conf('std')) self.config.available_features.add('long_tests') def get_test_format(self): # Note that we require that the caller has cleaned this directory, # ensured its existence, and copied libc++_shared.so into it. tmp_dir = getattr(self.config, 'device_dir', '/data/local/tmp/libcxx') build_only = self.get_lit_conf('build_only', False) build_dir = self.get_lit_conf('build_dir') return libcxx.ndk.test.format.TestFormat( self.cxx, self.libcxx_src_root, self.libcxx_obj_root, build_dir, tmp_dir, getattr(self.config, 'timeout', '300'), exec_env={'LD_LIBRARY_PATH': tmp_dir}, build_only=build_only)
py
1a4b4c9fd9da71a0f585f565863b2966297aff44
from __future__ import absolute_import, division, print_function from decorator import decorator import numpy as np import tensorflow as tf import lucid.misc.graph_analysis.property_inference as property_inference def _dot(x, y): return tf.reduce_sum(x * y, -1) def _dot_cossim(x, y, cossim_pow=0): eps = 1e-4 xy_dot = _dot(x, y) if cossim_pow == 0: return tf.reduce_mean(xy_dot) x_mags = tf.sqrt(_dot(x,x)) y_mags = tf.sqrt(_dot(y,y)) cossims = xy_dot / (eps + x_mags ) / (eps + y_mags) floored_cossims = tf.maximum(0.1, cossims) return tf.reduce_mean(xy_dot * floored_cossims**cossim_pow) def _extract_act_pos(acts, x=None, y=None): shape = tf.shape(acts) x_ = shape[1] // 2 if x is None else x y_ = shape[2] // 2 if y is None else y return acts[:, x_:x_+1, y_:y_+1] def _make_arg_str(arg): arg = str(arg) too_big = len(arg) > 15 or "\n" in arg return "..." if too_big else arg def _T_force_NHWC(T): """Modify T to accomdate different data types [N, C, H, W] -> [N, H, W, C] [N, C] -> [N, 1, 1, C] """ def T2(name): t = T(name) shape = t.shape if str(shape) == "<unknown>": return t if len(shape) == 2: return t[:, None, None, :] elif len(shape) == 4: fmt = property_inference.infer_data_format(t) if fmt == "NCHW": return tf.transpose(t, [0, 2, 3, 1]) return t return T2 def _T_handle_batch(T, batch=None): def T2(name): t = T(name) if isinstance(batch, int): return t[batch:batch+1] else: return t return T2
py
1a4b4d337f976fb9095c68148ecbc12908a762f2
# -*- coding: utf-8 -*- # Copyright 2018 The Blueoil Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================= import functools import os import os.path import numpy as np from blueoil import data_processor from blueoil.utils.image import load_image from blueoil.datasets.base import Base class Ilsvrc2012(Base): extend_dir = "ILSVRC2012" # classes = [str(n) for n in range(0, 1000)] num_classes = 1000 # `test` subsets don't have ground truth. available_subsets = ["train", "validation"] def __init__( self, *args, **kwargs ): super().__init__(*args, **kwargs,) self.dirs = { "train": os.path.join(self.data_dir, "train"), "validation": os.path.join(self.data_dir, "val"), "test": os.path.join(self.data_dir, "test"), } self.texts = { "train": os.path.join(self.data_dir, "train.txt"), "validation": os.path.join(self.data_dir, "val.txt"), "test": os.path.join(self.data_dir, "test.txt"), } self._init_files_and_annotations() @property @functools.lru_cache(maxsize=None) def classes(self): # wget https://raw.githubusercontent.com/Lasagne/Recipes/master/examples/resnet50/imagenet_classes.txt with open(os.path.join(self.data_dir, 'imagenet_classes.txt')) as f: return [line.rstrip('\n') for line in f] @property def num_per_epoch(self): files, _ = self._files_and_annotations() return len(files) def _init_files_and_annotations(self): self.files, self.annotations = self._files_and_annotations() @functools.lru_cache(maxsize=None) def _files_and_annotations(self): txt_file = self.texts[self.subset] files, labels = list(), list() with open(txt_file) as f: for line in f: items = line.split() files.append(items[0]) labels.append(int(items[1])) files = [os.path.join(self.dirs[self.subset], filename) for filename in files] return files, labels def __getitem__(self, i): filename = self.files[i] image = load_image(filename) label = data_processor.binarize(self.annotations[i], self.num_classes) label = np.reshape(label, (self.num_classes)) return (image, label) def __len__(self): return self.num_per_epoch
py
1a4b4d433daacca6aa1b375e00ba7bffc5a6ad51
# coding: utf-8 # # Copyright 2022 :Barry-Thomas-Paul: Moss # # 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. # # Service Class # this is a auto generated file generated by Cheetah # Libre Office Version: 7.3 # Namespace: com.sun.star.form.control from ....lo.form.control.list_box import ListBox as ListBox __all__ = ['ListBox']
py
1a4b4d4cd3d03b64bb15a37687b41dd3c548ec7a
# Generated by Django 3.1.4 on 2020-12-30 10:40 from django.db import migrations, models import django.utils.timezone class Migration(migrations.Migration): dependencies = [ ('invoices_app', '0002_invoice_date'), ] operations = [ migrations.RenameField( model_name='invoice', old_name='invoiceDescription', new_name='remarks', ), migrations.RemoveField( model_name='invoice', name='companyState', ), migrations.RemoveField( model_name='invoice', name='invoiceTotal', ), migrations.AlterField( model_name='invoice', name='date', field=models.DateField(default=django.utils.timezone.now), ), ]
py
1a4b4e9f8537b30fabd056f5a8dbca9a032513ce
import os from typing import * from fastapi import APIRouter, Request from pydantic.types import Json from api import models from pydantic import BaseModel from fastapi.templating import Jinja2Templates BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) router = APIRouter() templates = Jinja2Templates(directory = os.path.join(BASE_DIR,"templates")) class Table(BaseModel): Name: str Group: str Salary: int UniqueID: str Description: str @router.get("/show-all-data/") def Show_All(): data = list(models.EmployeeTable.objects.values('Name')) #Fetching Names from the table and showing the list of Employees return data @router.get("/data/{param_id}") def get_by_id(param_id): data = list(models.EmployeeTable.objects.filter(UniqueID = param_id)) #API routing with Django ORM return data @router.post("/data/POST") def create_item(item: Table): item = dict(item) m = models.EmployeeTable(**item) m.save() return {"Message":"The Data has been added to the Model"} @router.delete("/data/{UniqID}") def delete_item(UniqID: str): data = models.EmployeeTable.objects.filter(UniqueID = UniqID) return {"The following data will be deleted": list(data)} data.delete() @router.put("/data/PUT/") def put_item(Params: Table): item = dict(Params) m = models.EmployeeTable(**item) m.save() return {"Message":"The Data has been added to the Model"} router2 = APIRouter() @router2.get('/showall') def show_all(): data = list(models.EmployeeTable.objects.all().filter(Group = 'SDE1').values('Name')) #Filters the DB for SDE1 and shows their names return data
py
1a4b4fad3a95341fd4ba080bdd128037472652f1
from ..webdriver_server import WebKitDriverServer from .base import WdspecExecutor, WdspecProtocol class WebKitDriverProtocol(WdspecProtocol): server_cls = WebKitDriverServer class WebKitDriverWdspecExecutor(WdspecExecutor): protocol_cls = WebKitDriverProtocol
py
1a4b50c5535f0c77ca3f65ecbd180fe9df1734c9
from logger import root_logger async def with_logger(coro, lg=root_logger): try: return await coro except Exception as exc: lg.exception(f'in coro: {coro}') raise
py
1a4b510251be689d391f817d999cacdce8444813
# 5.5.1 Storing High Scores for a Game_Optimistic version class Scoreboard(): """Fixed-length sequence of high scores in nondecreasing order.""" class _GameEntry: """Nonpublic class for storing entry. Represents one entry of a list of high scores.""" __slots__ = '_name','_score' def __init__(self,name,score): self._name = name self._score = score def get_name(self): return self._name def get_score(self): return self._score def __str__(self): return '({0}, {1})'.format(self._name,self._score) # e.g., 'Bob,98' def __init__(self,capacity = 10): """Initialize scoreboard with given maximum capacity. All entries are initially None. """ self._board = [None] * capacity # reserve space for future scores self._n = 0 # number of actual entries def __getitem__(self,k): """Return entry at index k.""" return self._board[k] def __str__(self): """Return string representation of the high score list.""" return '\n'.join(str(self._board[j]) for j in range(self._n)) def add(self,name,score): """Consider adding entry to high scores.""" entry = self._GameEntry(name, score) score = entry.get_score() # Does new entry qualify as a high score? # answer is yes if board not full or score is higher than last entry good = self._n < len(self._board) or score > self._board[-1].get_score() if good: if self._n < len(self._board): # no score drops from list self._n += 1 # so overall number increases # shift lower scores rightward to make room for new entry j = self._n - 1 while j > 0 and self._board[j-1].get_score() < score: self._board[j] = self._board[j-1] # shift entry from j-1 to j j -= 1 # and decrement j self._board[j] = entry # when done, add new entry #----------------------------- my main function ----------------------------- a = Scoreboard(3) a.add('LiuPeng',93) a.add('zhangDi',98) a.add('LiYue',22) print('---------- first record ----------') print(a.__str__()) print('---------- get item ----------') print(a.__getitem__(2)) a.add('Torvalds',100) print('---------- second record ----------') print(a.__str__())
py
1a4b5160d6d70c2b528bedd67d3ad514ae9fb1a8
#!/usr/bin/env python # -*- coding: utf-8 -*- import matplotlib as mpl import numpy as np import scipy as sc NUMPY_VERSION = np.__version__ MATPLOTLIB_VERSION = mpl.__version__ SCIPY_VERSION = sc.__version__ message = f"""La versión de NumPy es {NUMPY_VERSION}. La versión de Matplotlib es {MATPLOTLIB_VERSION}. La versión de SciPy es {SCIPY_VERSION}.""" if __name__ == "__main__": print(message)
py
1a4b517c956f9e49a35480ecdd0c600b968842ea
from django.http import HttpResponseNotAllowed def allowed_methods(*methods): def intern_decorator(func): def wrapper(*args, **kwargs): if args[0].method in methods: return func(*args, **kwargs) else: return HttpResponseNotAllowed(list(methods)) return wrapper return intern_decorator
py
1a4b51867b6f20f6b32a9343d12d1ffa6e7a4224
import threading def first_fun(age): while True: print('I am first child.', age) pass return def second_fun(age): while True: print('I am second child.', age) pass return if __name__== "__main__": first = threading.Thread(target=first_fun , args = (5,)) second = threading.Thread(target=second_fun, args = (3,)) first.start() second.start() first.join() #연락을 했으니 전화를 받아야한다. second.join() while True: print('I am child.') pass
py
1a4b52368f2a8cbac3b68327d1de7b53e826e646
# -*- coding: utf-8 -*- import click from whyclick.chrome import open_chrome, remove_popups from whyclick import whyq CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) @click.group(context_settings=CONTEXT_SETTINGS) @click.version_option() def cli(): pass @cli.command("download-whyq-orders") @click.option( "--username", "-u", help="Your WhyQ username." ) @click.option( "--password", "-p", help="Your WhyQ password." ) def download_whyq_orders( username, password ): # Login to WhyQ driver = whyq.login(username, password) # Extract previous order. orders_json = whyq.download_previous_orders(driver) # Prints out the json. with click.get_text_stream("stdout") as fout: print(orders_json, file=fout) @cli.command("randomly-order-whyq") @click.option( "--username", "-u", help="Your WhyQ username." ) @click.option( "--password", "-p", help="Your WhyQ password." ) @click.option( "--halal", is_flag=True, default=False, help="Halal only options." ) @click.option( "--healthy", is_flag=True, default=False, help="Healthy only options." ) @click.option( "--vegetarian", is_flag=True, default=False, help="Vegetarian only options." ) def randomly_order_whyq( username, password, halal, healthy, vegetarian ): # Login to WhyQ driver = whyq.login(username, password) # Randomly order. whyq.randomly_order(driver, halal, healthy, vegetarian)
py
1a4b536d37db986c93ce2167bed213fe0e86a5a1
from universal_computation.experiment import run_experiment if __name__ == '__main__': experiment_name = 'fpt' experiment_params = dict( task='cifar100', n=1000, # ignored if not a bit task num_patterns=5, # ignored if not a bit task patch_size=16, model_name='gpt2', pretrained=True, # if vit this is forced to true, if lstm this is forced to false freeze_trans=True, # if False, we don't check arguments other than in and out freeze_in=False, freeze_pos=False, freeze_ln=False, freeze_attn=True, freeze_ff=True, freeze_out=False, in_layer_sizes=None, # not in paper, but can specify layer sizes for an MLP, out_layer_sizes=None, # ex. [32, 32] creates a 2-layer MLP with dimension 32 learning_rate=1e-3, batch_size=4, dropout=0.1, orth_gain=1.41, # orthogonal initialization of input layer ) exp_args = dict( num_iters=10, device="cpu", ) run_experiment(experiment_name, experiment_params, exp_args)
py
1a4b53dd3b22a4658690c9e5b256a7f83afc0c4f
from typing import Optional, Tuple from cadquery import Workplane from paramak import Shape class RotateCircleShape(Shape): """Rotates a circular 3d CadQuery solid from a central point and a radius Args: radius: radius of the shape rotation_angle: The rotation_angle to use when revolving the solid (degrees). Defaults to 360.0. """ def __init__( self, radius: float, rotation_angle: Optional[float] = 360.0, color: Optional[Tuple[float, float, float, Optional[float]]] = (1., 1., 0.6), **kwargs ): super().__init__( color=color, **kwargs ) self.radius = radius self.rotation_angle = rotation_angle @property def rotation_angle(self): return self._rotation_angle @rotation_angle.setter def rotation_angle(self, value): self._rotation_angle = value @property def radius(self): return self._radius @radius.setter def radius(self, value): self._radius = value def create_solid(self): """Creates a rotated 3d solid using points with circular edges. Returns: A CadQuery solid: A 3D solid volume """ wire = ( Workplane(self.workplane) .moveTo(self.points[0][0], self.points[0][1]) .circle(self.radius) ) self.wire = wire solid = wire.revolve(self.rotation_angle) solid = self.rotate_solid(solid) solid = self.perform_boolean_operations(solid) self.solid = solid return solid
py
1a4b54a5c5b00505019ad439dee9a7a956622147
from setuptools import setup, Extension from setuptools.command.build_ext import build_ext as _build_ext from setuptools.command.sdist import sdist as _sdist from distutils.command.clean import clean as _clean from distutils.errors import CompileError from warnings import warn import os import sys from glob import glob import tarfile import shutil import requests from urllib.request import urlretrieve # use cython if we can import it successfully try: from Cython.Distutils import build_ext as _build_ext except ImportError: use_cython = False else: use_cython = True # wrap the build_ext command to handle numpy bootstrap and compilation errors class build_ext(_build_ext): # see http://stackoverflow.com/q/19919905 for explanation def finalize_options(self): _build_ext.finalize_options(self) __builtins__.__NUMPY_SETUP__ = False import numpy as np self.include_dirs.append(np.get_include()) # if extension modules fail to build, keep going anyway def run(self): try: _build_ext.run(self) except CompileError: warn('Failed to build extension modules') import traceback print(traceback.format_exc(), file=sys.stderr) # wrap the sdist command to try to generate cython sources class sdist(_sdist): def run(self): try: from Cython.Build import cythonize cythonize(os.path.join('pyhlm','**','*.pyx'), compiler_directives={'language_level' : "3"}) except: warn('Failed to generate extension files from Cython sources') finally: _sdist.run(self) # wrap the clean command to remove object files class clean(_clean): def run(self): try: for f in glob(os.path.join('pyhlm','**','*.so')): # not recursive before Python 3.5 os.remove(f) except: warn('Failed to remove all object files') finally: _clean.run(self) # make dependency directory if not os.path.exists('deps'): os.mkdir('deps') # download Eigen if we don't have it in deps eigenurl = 'https://gitlab.com/libeigen/eigen/-/archive/3.3.7/eigen-3.3.7.tar.gz' eigentarpath = os.path.join('deps', 'Eigen.tar.gz') eigenpath = os.path.join('deps', 'Eigen') if not os.path.exists(eigenpath): print('Downloading Eigen...') r = requests.get(eigenurl) with open(eigentarpath, 'wb') as f: f.write(r.content) with tarfile.open(eigentarpath, 'r') as tar: tar.extractall('deps') thedir = glob(os.path.join('deps', 'eigen-*'))[0] shutil.move(os.path.join(thedir, 'Eigen'), eigenpath) print('...done!') # make a list of extension modules extension_pathspec = os.path.join('pyhlm','**','*.pyx') # not recursive before Python 3.5 paths = [os.path.splitext(fp)[0] for fp in glob(extension_pathspec)] names = ['.'.join(os.path.split(p)) for p in paths] with open("requirements.txt", "r") as f: requirements = list(f.readlines()) ext_modules = [ Extension( name, sources=[path + '.cpp'], include_dirs=['deps'], extra_compile_args=['-O3','-std=c++11','-DNDEBUG','-w','-DHLM_TEMPS_ON_HEAP']) for name, path in zip(names,paths)] # if using cython, rebuild the extension files from the .pyx sources if use_cython: from Cython.Build import cythonize try: ext_modules = cythonize(extension_pathspec, compiler_directives={'language_level' : "3"}) except: warn('Failed to generate extension module code from Cython files') # put it all together with a call to setup() setup(name='pyhlm', version='1.0.3', description="Bayesian inference in HLMs", author='Ryo Ozaki', author_email='[email protected]', url="https://github.com/RyoOzaki/npbdaa", license='MIT', packages=['pyhlm', 'pyhlm.internals', 'pyhlm.util'], platforms='ALL', keywords=['bayesian', 'inference', 'mcmc', 'time-series', 'monte-carlo', 'double articulation', 'hierarchical Dirichlet process hidden language model'], install_requires=requirements, setup_requires=['numpy', "future", "six"], ext_modules=ext_modules, classifiers=[ 'Intended Audience :: Science/Research', 'Programming Language :: Python', 'Programming Language :: C++'], cmdclass={'build_ext': build_ext, 'sdist': sdist, 'clean': clean})
py
1a4b551d42dbd1aa8708667a82ed5aa0561a80ba
""" =============================================================================== Phosphene drawings from Beyeler et al. (2019) =============================================================================== This example shows how to use the Beyeler et al. (2019) dataset. [Beyeler2019]_ asked Argus I/II users to draw what they see in response to single-electrode stimulation. .. important :: For this dataset you will need to install both `Pandas <https://pandas.pydata.org>`_ (``pip install pandas``) and `HDF4 for Python <https://www.h5py.org>`_ (``pip install h5py``). Loading the dataset ------------------- Due to its size (263 MB), the dataset is not included with pulse2percept, but can be downloaded from the Open Science Framework (OSF). By default, the dataset will be stored in a local directory ‘~/pulse2percept_data/’ within your user directory (but a different path can be specified). This way, the datasets is only downloaded once, and future calls to the fetch function will load the dataset from your local copy. The data itself will be provided as a Pandas ``DataFrame``: """ # sphinx_gallery_thumbnail_number = 2 from pulse2percept.datasets import fetch_beyeler2019 data = fetch_beyeler2019() print(data) ############################################################################### # # Inspecting the DataFrame tells us that there are 400 phosphene drawings # (the rows) each with 16 different attributes (the columns). # # These attributes include specifiers such as "subject", "electrode", and # "image". We can print all column names using: data.columns ############################################################################### # .. note :: # # The meaning of all column names is explained in the docstring of # the :py:func:`~pulse2percept.datasets.fetch_beyeler2019` function. # # For example, "subject" contains the different subject IDs used in the study: data.subject.unique() ############################################################################### # To select all drawings from Subject 2, we can index into the DataFrame as # follows: print(data[data.subject == 'S2']) ############################################################################### # This leaves us with 110 rows, each of which correspond to one phosphene # drawings from a number of different electrodes and trials. # # An alternative to indexing into the DataFrame is to load only a subset of # the data: print(fetch_beyeler2019(subjects='S2')) ############################################################################### # Plotting the data # ----------------- # # Arguably the most important column is "image". This is the phosphene drawing # obtained during a particular trial. # # Each phosphene drawing is a 2D black-and-white NumPy array, so we can just # plot it using Matplotlib like any other image: import matplotlib.pyplot as plt plt.imshow(data.loc[0, 'image'], cmap='gray') ############################################################################### # However, we might be more interested in seeing how phosphene shape differs # for different electrodes. # For this we can use :py:func:`~pulse2percept.viz.plot_argus_phosphenes` from # the :py:mod:`~pulse2percept.viz` module. # In addition to the ``data`` matrix, the function will also want an # :py:class:`~pulse2percept.implants.ArgusII` object implanted at the correct # location. # # Consulting [Beyeler2019]_ tells us that the prosthesis was roughly implanted # in the following location: from pulse2percept.implants import ArgusII argus = ArgusII(x=-1331, y=-850, rot=-0.495, eye='RE') ############################################################################### # (We also need to specify the dimensions of the screens that the subject used, # expressed in degrees of visual angle, so that we can scale the phosphene # drawing appropriately. This should really be part of the Beyeler dataset and # will be fixed in a future version. # For now, we add the necessary columns ourselves.) import pandas as pd data = fetch_beyeler2019(subjects='S2') data['img_x_dva'] = pd.Series([(-30, 30)] * len(data), index=data.index) data['img_y_dva'] = pd.Series([(-22.5, 22.5)] * len(data), index=data.index) ############################################################################### # Passing both ``data`` and ``argus`` to # :py:func:`~pulse2percept.viz.plot_argus_phosphenes` will then allow the # function to overlay the phosphene drawings over a schematic of the implant. # Here, phosphene drawings from different trials are averaged, and aligned with # the center of the electrode that was used to obtain the drawing: from pulse2percept.viz import plot_argus_phosphenes plot_argus_phosphenes(data, argus) ############################################################################### # Great! We have just reproduced a panel from Figure 2 in [Beyeler2019]_. # # As [Beyeler2019]_ went on to show, the orientation of these phosphenes is # well aligned with the map of nerve fiber bundles (NFBs) in each subject's # eye. # # To see how the phosphene drawings line up with the NFBs, we can also pass an # :py:class:`~pulse2percept.models.AxonMapModel` to the function. # Of course, we need to make sure that we use the correct dimensions. Subject # S2 had their optic disc center located 14 deg nasally, 2.4 deg superior from # the fovea: from pulse2percept.models import AxonMapModel model = AxonMapModel(loc_od=(14, 2.4)) plot_argus_phosphenes(data, argus, axon_map=model) ############################################################################### # Analyzing phosphene shape # ------------------------- # # The phosphene drawings also come annotated with different shape descriptors: # area, orientation, and elongation. # Elongation is also called eccentricity in the computer vision literature, # which is not to be confused with retinal eccentricity. It is simply a number # between 0 and 1, where 0 corresponds to a circle and 1 corresponds to an # infinitesimally thin line (note that the Methods section of [Beyeler2019]_ # got it wrong). # # [Beyeler2019]_ made the point that if each phosphene could be considered a # pixel (or essentially a blob), as is so often assumed in the literature, then # most phosphenes should have zero elongation. # # Instead, using Matplotlib's histogram function, we can convince ourselves # that most phosphenes are in fact elongated: data = fetch_beyeler2019() data.eccentricity.plot(kind='hist') plt.xlabel('phosphene elongation') ############################################################################### # Phosphenes are not pixels! # And we have just reproduced Fig. 3C of [Beyeler2019]_.
py
1a4b556512c8435f9beb9957e547d1e12d2eeb1a
# -*- encoding: utf-8 -*- from django.http import HttpResponse import unicodecsv as csv import unittest import xlrd from django.contrib.auth.models import Permission from django.test import TestCase from adminactions.api import export_as_csv, export_as_xls import StringIO from collections import namedtuple class TestExportQuerySetAsCsv(TestCase): def test_default_params(self): with self.assertNumQueries(1): qs = Permission.objects.select_related().filter(codename='add_user') ret = export_as_csv(queryset=qs) self.assertIsInstance(ret, HttpResponse) self.assertEquals(ret.content.decode('utf8'), u'"%s";"Can add user";"user";"add_user"\r\n' % qs[0].pk) def test_header_is_true(self): mem = StringIO.StringIO() with self.assertNumQueries(1): qs = Permission.objects.select_related().filter(codename='add_user') export_as_csv(queryset=qs, header=True, out=mem) mem.seek(0) csv_reader = csv.reader(mem) self.assertEqual(csv_reader.next(), [u'id;"name";"content_type";"codename"']) def test_queryset_values(self): fields = ['codename', 'content_type__app_label'] header = ['Name', 'Application'] mem = StringIO.StringIO() with self.assertNumQueries(1): qs = Permission.objects.filter(codename='add_user').values('codename', 'content_type__app_label') export_as_csv(queryset=qs, fields=fields, header=header, out=mem) mem.seek(0) csv_dump = mem.read() self.assertEquals(csv_dump.decode('utf8'), u'"Name";"Application"\r\n"add_user";"auth"\r\n') def test_callable_method(self): fields = ['codename', 'natural_key'] mem = StringIO.StringIO() with self.assertNumQueries(2): qs = Permission.objects.filter(codename='add_user') export_as_csv(queryset=qs, fields=fields, out=mem) mem.seek(0) csv_dump = mem.read() self.assertEquals(csv_dump.decode('utf8'), u'"add_user";"(u\'add_user\', u\'auth\', u\'user\')"\r\n') def test_deep_attr(self): fields = ['codename', 'content_type.app_label'] mem = StringIO.StringIO() with self.assertNumQueries(1): qs = Permission.objects.select_related().filter(codename='add_user') export_as_csv(queryset=qs, fields=fields, out=mem) mem.seek(0) csv_dump = mem.read() self.assertEquals(csv_dump.decode('utf8'), u'"add_user";"auth"\r\n') class TestExportAsCsv(unittest.TestCase): def test_export_as_csv(self): fields = ['field1', 'field2'] header = ['Field 1', 'Field 2'] Row = namedtuple('Row', fields) rows = [Row(1, 4), Row(2, 5), Row(3, u'ӼӳӬԖԊ')] mem = StringIO.StringIO() export_as_csv(queryset=rows, fields=fields, header=header, out=mem) mem.seek(0) csv_dump = mem.read() self.assertEquals(csv_dump.decode('utf8'), u'"Field 1";"Field 2"\r\n"1";"4"\r\n"2";"5"\r\n"3";"ӼӳӬԖԊ"\r\n') class TestExportAsExcel(TestCase): def test_default_params(self): with self.assertNumQueries(1): qs = Permission.objects.select_related().filter(codename='add_user') ret = export_as_xls(queryset=qs) self.assertIsInstance(ret, HttpResponse) def test_header_is_true(self): mem = StringIO.StringIO() with self.assertNumQueries(1): qs = Permission.objects.select_related().filter(codename='add_user') export_as_xls(queryset=qs, header=True, out=mem) mem.seek(0) xls_workbook = xlrd.open_workbook(file_contents=mem.read()) xls_sheet = xls_workbook.sheet_by_index(0) self.assertEqual(xls_sheet.row_values(0)[:], [u'#', u'ID', u'name', u'content type', u'codename']) def test_export_as_xls(self): fields = ['field1', 'field2'] header = ['Field 1', 'Field 2'] Row = namedtuple('Row', fields) rows = [Row(111, 222), Row(333, 444), Row(555, u'ӼӳӬԖԊ')] mem = StringIO.StringIO() export_as_xls(queryset=rows, fields=fields, header=header, out=mem) mem.seek(0) xls_workbook = xlrd.open_workbook(file_contents=mem.read()) xls_sheet = xls_workbook.sheet_by_index(0) self.assertEqual(xls_sheet.row_values(0)[:], ['#', 'Field 1', 'Field 2']) self.assertEqual(xls_sheet.row_values(1)[:], [1.0, 111.0, 222.0]) self.assertEqual(xls_sheet.row_values(2)[:], [2.0, 333.0, 444.0]) self.assertEqual(xls_sheet.row_values(3)[:], [3.0, 555.0, u'ӼӳӬԖԊ']) class TestExportQuerySetAsExcel(TestCase): def test_queryset_values(self): fields = ['codename', 'content_type__app_label'] header = ['Name', 'Application'] qs = Permission.objects.filter(codename='add_user').values('codename', 'content_type__app_label') mem = StringIO.StringIO() export_as_xls(queryset=qs, fields=fields, header=header, out=mem) mem.seek(0) w = xlrd.open_workbook(file_contents=mem.read()) sheet = w.sheet_by_index(0) self.assertEquals(sheet.cell_value(1, 1), u'add_user') self.assertEquals(sheet.cell_value(1, 2), u'auth') def test_callable_method(self): fields = ['codename', 'natural_key'] qs = Permission.objects.filter(codename='add_user') mem = StringIO.StringIO() export_as_xls(queryset=qs, fields=fields, out=mem) mem.seek(0) w = xlrd.open_workbook(file_contents=mem.read()) sheet = w.sheet_by_index(0) self.assertEquals(sheet.cell_value(1, 1), u'add_user') self.assertEquals(sheet.cell_value(1, 2), u'add_userauthuser')
py
1a4b55720f2ba818a356e73151ee57c1bf725cca
# -*- coding: utf-8 -*- r""" Free Dendriform Algebras AUTHORS: Frédéric Chapoton (2017) """ # **************************************************************************** # Copyright (C) 2010-2015 Frédéric Chapoton <[email protected]>, # # Distributed under the terms of the GNU General Public License (GPL) # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ # **************************************************************************** from six import iteritems from sage.categories.hopf_algebras import HopfAlgebras from sage.combinat.free_module import CombinatorialFreeModule from sage.combinat.words.alphabet import Alphabet from sage.combinat.binary_tree import (BinaryTrees, BinaryTree, LabelledBinaryTrees, LabelledBinaryTree) from sage.categories.pushout import (ConstructionFunctor, CompositeConstructionFunctor, IdentityConstructionFunctor) from sage.categories.rings import Rings from sage.categories.functor import Functor from sage.misc.lazy_attribute import lazy_attribute from sage.misc.cachefunc import cached_method from sage.sets.family import Family class FreeDendriformAlgebra(CombinatorialFreeModule): r""" The free dendriform algebra. Dendriform algebras are associative algebras, where the associative product `*` is decomposed as a sum of two binary operations .. MATH:: x * y = x \succ y + x \prec y that satisfy the axioms: .. MATH:: (x \succ y) \prec z = x \succ (y \prec z), .. MATH:: (x \prec y) \prec z = x \prec (y * z). .. MATH:: (x * y) \succ z = x \succ (y \succ z). The free Dendriform algebra on a given set `E` has an explicit description using (planar) binary trees, just as the free associative algebra can be described using words. The underlying vector space has a basis indexed by finite binary trees endowed with a map from their vertices to `E`. In this basis, the associative product of two (decorated) binary trees `S * T` is the sum over all possible ways of identifying (glueing) the rightmost path in `S` and the leftmost path in `T`. The decomposition of the associative product as the sum of two binary operations `\succ` and `\prec` is made by separating the terms according to the origin of the root vertex. For `x \succ y`, one keeps the terms where the root vertex comes from `y`, whereas for `x \prec y` one keeps the terms where the root vertex comes from `x`. The free dendriform algebra can also be considered as the free algebra over the Dendriform operad. .. NOTE:: The usual binary operator `*` is used for the associative product. EXAMPLES:: sage: F = algebras.FreeDendriform(ZZ, 'xyz') sage: x,y,z = F.gens() sage: (x * y) * z B[x[., y[., z[., .]]]] + B[x[., z[y[., .], .]]] + B[y[x[., .], z[., .]]] + B[z[x[., y[., .]], .]] + B[z[y[x[., .], .], .]] The free dendriform algebra is associative:: sage: x * (y * z) == (x * y) * z True The associative product decomposes in two parts:: sage: x * y == F.prec(x, y) + F.succ(x, y) True The axioms hold:: sage: F.prec(F.succ(x, y), z) == F.succ(x, F.prec(y, z)) True sage: F.prec(F.prec(x, y), z) == F.prec(x, y * z) True sage: F.succ(x * y, z) == F.succ(x, F.succ(y, z)) True When there is only one generator, unlabelled trees are used instead:: sage: F1 = algebras.FreeDendriform(QQ) sage: w = F1.gen(0); w B[[., .]] sage: w * w * w B[[., [., [., .]]]] + B[[., [[., .], .]]] + B[[[., .], [., .]]] + B[[[., [., .]], .]] + B[[[[., .], .], .]] REFERENCES: - [LodayRonco]_ """ @staticmethod def __classcall_private__(cls, R, names=None): """ Normalize input to ensure a unique representation. EXAMPLES:: sage: F1 = algebras.FreeDendriform(QQ, 'xyz') sage: F2 = algebras.FreeDendriform(QQ, ['x','y','z']) sage: F3 = algebras.FreeDendriform(QQ, Alphabet('xyz')) sage: F1 is F2 and F1 is F3 True """ if names is not None: if ',' in names: names = [u for u in names if u != ','] names = Alphabet(names) if R not in Rings(): raise TypeError("argument R must be a ring") return super(FreeDendriformAlgebra, cls).__classcall__(cls, R, names) def __init__(self, R, names=None): """ Initialize ``self``. TESTS:: sage: A = algebras.FreeDendriform(QQ, '@'); A Free Dendriform algebra on one generator ['@'] over Rational Field sage: TestSuite(A).run() # long time (3s) sage: F = algebras.FreeDendriform(QQ, 'xy') sage: TestSuite(F).run() # long time (3s) """ if names is None: Trees = BinaryTrees() key = BinaryTree._sort_key self._alphabet = Alphabet(['o']) else: Trees = LabelledBinaryTrees() key = LabelledBinaryTree._sort_key self._alphabet = names # Here one would need LabelledBinaryTrees(names) # so that one can restrict the labels to some fixed set cat = HopfAlgebras(R).WithBasis().Graded().Connected() CombinatorialFreeModule.__init__(self, R, Trees, latex_prefix="", sorting_key=key, category=cat) def variable_names(self): r""" Return the names of the variables. EXAMPLES:: sage: R = algebras.FreeDendriform(QQ, 'xy') sage: R.variable_names() {'x', 'y'} """ return self._alphabet def _repr_(self): """ Return the string representation of ``self``. EXAMPLES:: sage: algebras.FreeDendriform(QQ, '@') # indirect doctest Free Dendriform algebra on one generator ['@'] over Rational Field """ n = self.algebra_generators().cardinality() if n == 1: gen = "one generator" else: gen = "{} generators".format(n) s = "Free Dendriform algebra on {} {} over {}" try: return s.format(gen, self._alphabet.list(), self.base_ring()) except NotImplementedError: return s.format(gen, self._alphabet, self.base_ring()) def gen(self, i): r""" Return the ``i``-th generator of the algebra. INPUT: - ``i`` -- an integer EXAMPLES:: sage: F = algebras.FreeDendriform(ZZ, 'xyz') sage: F.gen(0) B[x[., .]] sage: F.gen(4) Traceback (most recent call last): ... IndexError: argument i (= 4) must be between 0 and 2 """ G = self.algebra_generators() n = G.cardinality() if i < 0 or not i < n: m = "argument i (= {}) must be between 0 and {}".format(i, n - 1) raise IndexError(m) return G[G.keys().unrank(i)] @cached_method def algebra_generators(self): r""" Return the generators of this algebra. These are the binary trees with just one vertex. EXAMPLES:: sage: A = algebras.FreeDendriform(ZZ, 'fgh'); A Free Dendriform algebra on 3 generators ['f', 'g', 'h'] over Integer Ring sage: list(A.algebra_generators()) [B[f[., .]], B[g[., .]], B[h[., .]]] sage: A = algebras.FreeDendriform(QQ, ['x1','x2']) sage: list(A.algebra_generators()) [B[x1[., .]], B[x2[., .]]] """ Trees = self.basis().keys() return Family(self._alphabet, lambda a: self.monomial(Trees([], a))) def change_ring(self, R): """ Return the free dendriform algebra in the same variables over `R`. INPUT: - ``R`` -- a ring EXAMPLES:: sage: A = algebras.FreeDendriform(ZZ, 'fgh') sage: A.change_ring(QQ) Free Dendriform algebra on 3 generators ['f', 'g', 'h'] over Rational Field """ return FreeDendriformAlgebra(R, names=self.variable_names()) def gens(self): """ Return the generators of ``self`` (as an algebra). EXAMPLES:: sage: A = algebras.FreeDendriform(ZZ, 'fgh') sage: A.gens() (B[f[., .]], B[g[., .]], B[h[., .]]) """ return tuple(self.algebra_generators()) def degree_on_basis(self, t): """ Return the degree of a binary tree in the free Dendriform algebra. This is the number of vertices. EXAMPLES:: sage: A = algebras.FreeDendriform(QQ,'@') sage: RT = A.basis().keys() sage: u = RT([], '@') sage: A.degree_on_basis(u.over(u)) 2 """ return t.node_number() @cached_method def an_element(self): """ Return an element of ``self``. EXAMPLES:: sage: A = algebras.FreeDendriform(QQ, 'xy') sage: A.an_element() B[x[., .]] + 2*B[x[., x[., .]]] + 2*B[x[x[., .], .]] """ o = self.gen(0) return o + 2 * o * o def some_elements(self): """ Return some elements of the free dendriform algebra. EXAMPLES:: sage: A = algebras.FreeDendriform(QQ) sage: A.some_elements() [B[.], B[[., .]], B[[., [., .]]] + B[[[., .], .]], B[.] + B[[., [., .]]] + B[[[., .], .]]] With several generators:: sage: A = algebras.FreeDendriform(QQ, 'xy') sage: A.some_elements() [B[.], B[x[., .]], B[x[., x[., .]]] + B[x[x[., .], .]], B[.] + B[x[., x[., .]]] + B[x[x[., .], .]]] """ u = self.one() o = self.gen(0) x = o * o y = u + x return [u, o, x, y] def one_basis(self): """ Return the index of the unit. EXAMPLES:: sage: A = algebras.FreeDendriform(QQ, '@') sage: A.one_basis() . sage: A = algebras.FreeDendriform(QQ, 'xy') sage: A.one_basis() . """ Trees = self.basis().keys() return Trees(None) def product_on_basis(self, x, y): r""" Return the `*` associative dendriform product of two trees. This is the sum over all possible ways of identifying the rightmost path in `x` and the leftmost path in `y`. Every term corresponds to a shuffle of the vertices on the rightmost path in `x` and the vertices on the leftmost path in `y`. .. SEEALSO:: - :meth:`succ_product_on_basis`, :meth:`prec_product_on_basis` EXAMPLES:: sage: A = algebras.FreeDendriform(QQ) sage: RT = A.basis().keys() sage: x = RT([]) sage: A.product_on_basis(x, x) B[[., [., .]]] + B[[[., .], .]] """ return self.sum(self.basis()[u] for u in x.dendriform_shuffle(y)) def succ_product_on_basis(self, x, y): r""" Return the `\succ` dendriform product of two trees. This is the sum over all possible ways to identify the rightmost path in `x` and the leftmost path in `y`, with the additional condition that the root vertex of the result comes from `y`. The usual symbol for this operation is `\succ`. .. SEEALSO:: - :meth:`product_on_basis`, :meth:`prec_product_on_basis` EXAMPLES:: sage: A = algebras.FreeDendriform(QQ) sage: RT = A.basis().keys() sage: x = RT([]) sage: A.succ_product_on_basis(x, x) B[[[., .], .]] TESTS:: sage: u = A.one().support()[0] sage: A.succ_product_on_basis(u, u) Traceback (most recent call last): ... ValueError: dendriform products | < | and | > | are not defined """ if y.is_empty(): if x.is_empty(): raise ValueError("dendriform products | < | and | > | are " "not defined") else: return [] if x.is_empty(): return [y] K = self.basis().keys() if hasattr(y, 'label'): return self.sum(self.basis()[K([u, y[1]], y.label())] for u in x.dendriform_shuffle(y[0])) return self.sum(self.basis()[K([u, y[1]])] for u in x.dendriform_shuffle(y[0])) @lazy_attribute def succ(self): r""" Return the `\succ` dendriform product. This is the sum over all possible ways of identifying the rightmost path in `x` and the leftmost path in `y`, with the additional condition that the root vertex of the result comes from `y`. The usual symbol for this operation is `\succ`. .. SEEALSO:: :meth:`product`, :meth:`prec`, :meth:`over`, :meth:`under` EXAMPLES:: sage: A = algebras.FreeDendriform(QQ) sage: RT = A.basis().keys() sage: x = A.gen(0) sage: A.succ(x, x) B[[[., .], .]] """ suc = self.succ_product_on_basis return self._module_morphism(self._module_morphism(suc, position=0, codomain=self), position=1) def prec_product_on_basis(self, x, y): r""" Return the `\prec` dendriform product of two trees. This is the sum over all possible ways of identifying the rightmost path in `x` and the leftmost path in `y`, with the additional condition that the root vertex of the result comes from `x`. The usual symbol for this operation is `\prec`. .. SEEALSO:: - :meth:`product_on_basis`, :meth:`succ_product_on_basis` EXAMPLES:: sage: A = algebras.FreeDendriform(QQ) sage: RT = A.basis().keys() sage: x = RT([]) sage: A.prec_product_on_basis(x, x) B[[., [., .]]] TESTS:: sage: u = A.one().support()[0] sage: A.prec_product_on_basis(u, u) Traceback (most recent call last): ... ValueError: dendriform products | < | and | > | are not defined """ if x.is_empty() and y.is_empty(): raise ValueError("dendriform products | < | and | > | are " "not defined") if x.is_empty(): return [] if y.is_empty(): return [x] K = self.basis().keys() if hasattr(y, 'label'): return self.sum(self.basis()[K([x[0], u], x.label())] for u in x[1].dendriform_shuffle(y)) return self.sum(self.basis()[K([x[0], u])] for u in x[1].dendriform_shuffle(y)) @lazy_attribute def prec(self): r""" Return the `\prec` dendriform product. This is the sum over all possible ways to identify the rightmost path in `x` and the leftmost path in `y`, with the additional condition that the root vertex of the result comes from `x`. The usual symbol for this operation is `\prec`. .. SEEALSO:: :meth:`product`, :meth:`succ`, :meth:`over`, :meth:`under` EXAMPLES:: sage: A = algebras.FreeDendriform(QQ) sage: RT = A.basis().keys() sage: x = A.gen(0) sage: A.prec(x, x) B[[., [., .]]] """ pre = self.prec_product_on_basis return self._module_morphism(self._module_morphism(pre, position=0, codomain=self), position=1) @lazy_attribute def over(self): r""" Return the over product. The over product `x/y` is the binary tree obtained by grafting the root of `y` at the rightmost leaf of `x`. The usual symbol for this operation is `/`. .. SEEALSO:: :meth:`product`, :meth:`succ`, :meth:`prec`, :meth:`under` EXAMPLES:: sage: A = algebras.FreeDendriform(QQ) sage: RT = A.basis().keys() sage: x = A.gen(0) sage: A.over(x, x) B[[., [., .]]] """ def ov(x, y): return self._monomial(x.over(y)) return self._module_morphism(self._module_morphism(ov, position=0, codomain=self), position=1) @lazy_attribute def under(self): r""" Return the under product. The over product `x \backslash y` is the binary tree obtained by grafting the root of `x` at the leftmost leaf of `y`. The usual symbol for this operation is `\backslash`. .. SEEALSO:: :meth:`product`, :meth:`succ`, :meth:`prec`, :meth:`over` EXAMPLES:: sage: A = algebras.FreeDendriform(QQ) sage: RT = A.basis().keys() sage: x = A.gen(0) sage: A.under(x, x) B[[[., .], .]] """ def und(x, y): return self._monomial(x.under(y)) return self._module_morphism(self._module_morphism(und, position=0, codomain=self), position=1) def coproduct_on_basis(self, x): """ Return the coproduct of a binary tree. EXAMPLES:: sage: A = algebras.FreeDendriform(QQ) sage: x = A.gen(0) sage: ascii_art(A.coproduct(A.one())) # indirect doctest 1 # 1 sage: ascii_art(A.coproduct(x)) # indirect doctest 1 # B + B # 1 o o sage: A = algebras.FreeDendriform(QQ, 'xyz') sage: x, y, z = A.gens() sage: w = A.under(z,A.over(x,y)) sage: A.coproduct(z) B[.] # B[z[., .]] + B[z[., .]] # B[.] sage: A.coproduct(w) B[.] # B[x[z[., .], y[., .]]] + B[x[., .]] # B[z[., y[., .]]] + B[x[., .]] # B[y[z[., .], .]] + B[x[., y[., .]]] # B[z[., .]] + B[x[z[., .], .]] # B[y[., .]] + B[x[z[., .], y[., .]]] # B[.] """ B = self.basis() Trees = B.keys() if not x.node_number(): return self.one().tensor(self.one()) L, R = list(x) try: root = x.label() except AttributeError: root = '@' resu = self.one().tensor(self.monomial(x)) resu += sum(cL * cR * self.monomial(Trees([LL[0], RR[0]], root)).tensor( self.monomial(LL[1]) * self.monomial(RR[1])) for LL, cL in self.coproduct_on_basis(L) for RR, cR in self.coproduct_on_basis(R)) return resu # after this line : coercion def _element_constructor_(self, x): r""" Convert ``x`` into ``self``. EXAMPLES:: sage: R = algebras.FreeDendriform(QQ, 'xy') sage: x, y = R.gens() sage: R(x) B[x[., .]] sage: R(x+4*y) B[x[., .]] + 4*B[y[., .]] sage: Trees = R.basis().keys() sage: R(Trees([],'x')) B[x[., .]] sage: D = algebras.FreeDendriform(ZZ, 'xy') sage: X, Y = D.gens() sage: R(X-Y).parent() Free Dendriform algebra on 2 generators ['x', 'y'] over Rational Field """ if x in self.basis().keys(): return self.monomial(x) try: P = x.parent() if isinstance(P, FreeDendriformAlgebra): if P is self: return x return self.element_class(self, x.monomial_coefficients()) except AttributeError: raise TypeError('not able to coerce this in this algebra') # Ok, not a dendriform algebra element (or should not be viewed as one). def _coerce_map_from_(self, R): r""" Return ``True`` if there is a coercion from ``R`` into ``self`` and ``False`` otherwise. The things that coerce into ``self`` are - free dendriform algebras in a subset of variables of ``self`` over a base with a coercion map into ``self.base_ring()`` EXAMPLES:: sage: F = algebras.FreeDendriform(GF(7), 'xyz'); F Free Dendriform algebra on 3 generators ['x', 'y', 'z'] over Finite Field of size 7 Elements of the free dendriform algebra canonically coerce in:: sage: x, y, z = F.gens() sage: F.coerce(x+y) == x+y True The free dendriform algebra over `\ZZ` on `x, y, z` coerces in, since `\ZZ` coerces to `\GF{7}`:: sage: G = algebras.FreeDendriform(ZZ, 'xyz') sage: Gx,Gy,Gz = G.gens() sage: z = F.coerce(Gx+Gy); z B[x[., .]] + B[y[., .]] sage: z.parent() is F True However, `\GF{7}` does not coerce to `\ZZ`, so the free dendriform algebra over `\GF{7}` does not coerce to the one over `\ZZ`:: sage: G.coerce(y) Traceback (most recent call last): ... TypeError: no canonical coercion from Free Dendriform algebra on 3 generators ['x', 'y', 'z'] over Finite Field of size 7 to Free Dendriform algebra on 3 generators ['x', 'y', 'z'] over Integer Ring TESTS:: sage: F = algebras.FreeDendriform(ZZ, 'xyz') sage: G = algebras.FreeDendriform(QQ, 'xyz') sage: H = algebras.FreeDendriform(ZZ, 'y') sage: F._coerce_map_from_(G) False sage: G._coerce_map_from_(F) True sage: F._coerce_map_from_(H) True sage: F._coerce_map_from_(QQ) False sage: G._coerce_map_from_(QQ) False sage: F.has_coerce_map_from(PolynomialRing(ZZ, 3, 'x,y,z')) False """ # free dendriform algebras in a subset of variables # over any base that coerces in: if isinstance(R, FreeDendriformAlgebra): if all(x in self.variable_names() for x in R.variable_names()): if self.base_ring().has_coerce_map_from(R.base_ring()): return True return False def construction(self): """ Return a pair ``(F, R)``, where ``F`` is a :class:`DendriformFunctor` and `R` is a ring, such that ``F(R)`` returns ``self``. EXAMPLES:: sage: P = algebras.FreeDendriform(ZZ, 'x,y') sage: x,y = P.gens() sage: F, R = P.construction() sage: F Dendriform[x,y] sage: R Integer Ring sage: F(ZZ) is P True sage: F(QQ) Free Dendriform algebra on 2 generators ['x', 'y'] over Rational Field """ return DendriformFunctor(self.variable_names()), self.base_ring() class DendriformFunctor(ConstructionFunctor): """ A constructor for dendriform algebras. EXAMPLES:: sage: P = algebras.FreeDendriform(ZZ, 'x,y') sage: x,y = P.gens() sage: F = P.construction()[0]; F Dendriform[x,y] sage: A = GF(5)['a,b'] sage: a, b = A.gens() sage: F(A) Free Dendriform algebra on 2 generators ['x', 'y'] over Multivariate Polynomial Ring in a, b over Finite Field of size 5 sage: f = A.hom([a+b,a-b],A) sage: F(f) Generic endomorphism of Free Dendriform algebra on 2 generators ['x', 'y'] over Multivariate Polynomial Ring in a, b over Finite Field of size 5 sage: F(f)(a * F(A)(x)) (a+b)*B[x[., .]] """ rank = 9 def __init__(self, vars): """ EXAMPLES:: sage: F = sage.combinat.free_dendriform_algebra.DendriformFunctor(['x','y']) sage: F Dendriform[x,y] sage: F(ZZ) Free Dendriform algebra on 2 generators ['x', 'y'] over Integer Ring """ Functor.__init__(self, Rings(), Rings()) self.vars = vars def _apply_functor(self, R): """ Apply the functor to an object of ``self``'s domain. EXAMPLES:: sage: R = algebras.FreeDendriform(ZZ, 'x,y,z') sage: F = R.construction()[0]; F Dendriform[x,y,z] sage: type(F) <class 'sage.combinat.free_dendriform_algebra.DendriformFunctor'> sage: F(ZZ) # indirect doctest Free Dendriform algebra on 3 generators ['x', 'y', 'z'] over Integer Ring """ return FreeDendriformAlgebra(R, self.vars) def _apply_functor_to_morphism(self, f): """ Apply the functor ``self`` to the ring morphism `f`. TESTS:: sage: R = algebras.FreeDendriform(ZZ, 'x').construction()[0] sage: R(ZZ.hom(GF(3))) # indirect doctest Generic morphism: From: Free Dendriform algebra on one generator ['x'] over Integer Ring To: Free Dendriform algebra on one generator ['x'] over Finite Field of size 3 """ dom = self(f.domain()) codom = self(f.codomain()) def action(x): return codom._from_dict({a: f(b) for a, b in iteritems(x.monomial_coefficients())}) return dom.module_morphism(function=action, codomain=codom) def __eq__(self, other): """ EXAMPLES:: sage: F = algebras.FreeDendriform(ZZ, 'x,y,z').construction()[0] sage: G = algebras.FreeDendriform(QQ, 'x,y,z').construction()[0] sage: F == G True sage: G == loads(dumps(G)) True sage: G = algebras.FreeDendriform(QQ, 'x,y').construction()[0] sage: F == G False """ if not isinstance(other, DendriformFunctor): return False return self.vars == other.vars def __ne__(self, other): """ EXAMPLES:: sage: F = algebras.FreeDendriform(ZZ, 'x,y,z').construction()[0] sage: G = algebras.FreeDendriform(QQ, 'x,y,z').construction()[0] sage: F != G False sage: G != loads(dumps(G)) False sage: G = algebras.FreeDendriform(QQ, 'x,y').construction()[0] sage: F != G True """ return not (self == other) def __mul__(self, other): """ If two Dendriform functors are given in a row, form a single Dendriform functor with all of the variables. EXAMPLES:: sage: F = sage.combinat.free_dendriform_algebra.DendriformFunctor(['x','y']) sage: G = sage.combinat.free_dendriform_algebra.DendriformFunctor(['t']) sage: G * F Dendriform[x,y,t] """ if isinstance(other, IdentityConstructionFunctor): return self if isinstance(other, DendriformFunctor): if set(self.vars).intersection(other.vars): raise CoercionException("Overlapping variables (%s,%s)" % (self.vars, other.vars)) return DendriformFunctor(other.vars + self.vars) elif (isinstance(other, CompositeConstructionFunctor) and isinstance(other.all[-1], DendriformFunctor)): return CompositeConstructionFunctor(other.all[:-1], self * other.all[-1]) else: return CompositeConstructionFunctor(other, self) def merge(self, other): """ Merge ``self`` with another construction functor, or return ``None``. EXAMPLES:: sage: F = sage.combinat.free_dendriform_algebra.DendriformFunctor(['x','y']) sage: G = sage.combinat.free_dendriform_algebra.DendriformFunctor(['t']) sage: F.merge(G) Dendriform[x,y,t] sage: F.merge(F) Dendriform[x,y] Now some actual use cases:: sage: R = algebras.FreeDendriform(ZZ, 'x,y,z') sage: x,y,z = R.gens() sage: 1/2 * x 1/2*B[x[., .]] sage: parent(1/2 * x) Free Dendriform algebra on 3 generators ['x', 'y', 'z'] over Rational Field sage: S = algebras.FreeDendriform(QQ, 'zt') sage: z,t = S.gens() sage: x + t B[t[., .]] + B[x[., .]] sage: parent(x + t) Free Dendriform algebra on 4 generators ['z', 't', 'x', 'y'] over Rational Field """ if isinstance(other, DendriformFunctor): if self.vars == other.vars: return self ret = list(self.vars) cur_vars = set(ret) for v in other.vars: if v not in cur_vars: ret.append(v) return DendriformFunctor(Alphabet(ret)) else: return None def _repr_(self): """ TESTS:: sage: algebras.FreeDendriform(QQ,'x,y,z,t').construction()[0] Dendriform[x,y,z,t] """ return "Dendriform[%s]" % ','.join(self.vars)
py
1a4b55d6f824e0b2a3d9f86ec1cedde8f3f9defc
# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. from gaiatest import GaiaTestCase from gaiatest.apps.settings.app import Settings class TestSettingsCellData(GaiaTestCase): def test_enable_cell_data_via_settings_app(self): """ https://moztrap.mozilla.org/manage/case/1373/ """ settings = Settings(self.marionette) settings.launch() cell_and_data_settings = settings.open_cell_and_data_settings() # verify that a carrier is displayed self.assertTrue(len(cell_and_data_settings.carrier_name) > 0) # enable cell data self.assertFalse(cell_and_data_settings.is_data_toggle_checked) cell_data_prompt = cell_and_data_settings.enable_data() # deal with prompt that sometimes appears (on first setting) if cell_data_prompt.is_displayed: # Cell data should not be enabled until we turn it on via the prompt self.assertFalse(cell_and_data_settings.is_data_toggle_checked) self.assertFalse(self.data_layer.get_setting('ril.data.enabled'), "Cell data was enabled before responding to the prompt") cell_data_prompt.turn_on() # Wait for cell data to be turned on self.wait_for_condition(lambda m: cell_and_data_settings.is_data_toggle_checked) # verify that cell data is now enabled and connected self.assertTrue(self.data_layer.is_cell_data_enabled, "Cell data was not enabled via Settings app") self.wait_for_condition( lambda m: self.data_layer.is_cell_data_connected, message='Cell data was not connected via Settings app')
py
1a4b564814b434c9812f1c1943103bae36b7bb89
# MIT License # # Copyright (c) 2020-2021 Parakoopa and the SkyTemple Contributors # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # import argparse import json import os import sys from typing import Tuple, List, Union from explorerscript.cli import check_settings, SETTINGS_PERFORMANCE_PROGRESS_LIST_VAR_NAME, SETTINGS, \ SETTINGS_DUNGEON_MODE_CONSTANTS, SETTINGS_DMC_OPEN, SETTINGS_DMC_CLOSED, SETTINGS_DMC_REQUEST, \ SETTINGS_DMC_OPEN_REQUEST from explorerscript.ssb_converting.compiler.utils import Counter from explorerscript.ssb_converting.ssb_data_types import SsbCoroutine, SsbOperation, SsbRoutineInfo, \ DungeonModeConstants, SsbRoutineType, SsbOpParamConstant, SsbOpParamConstString, SsbOpParamLanguageString, \ SsbOpParamPositionMarker, SsbOpCode from explorerscript.ssb_converting.ssb_decompiler import ExplorerScriptSsbDecompiler from explorerscript.util import open_utf8, exps_int counter = Counter() def parse_pos_mark_arg(arg_str): arg_str_arr = arg_str.split('.') if len(arg_str_arr) < 2: return exps_int(arg_str), 0 if arg_str_arr[1] != '5' or len(arg_str_arr) > 2: raise ValueError("Invalid position mark") return exps_int(arg_str_arr[0]), 2 def read_ops(ops: List[dict]) -> List[SsbOperation]: out_ops = [] for op in ops: if "params" not in op: raise ValueError("Params for an op not set.") if "opcode" not in op: raise ValueError("Opcode for an op not set.") params = [] for param in op["params"]: if isinstance(param, int): params.append(param) elif isinstance(param, dict): if "type" not in param or "value" not in param: raise ValueError("Invalid param for op.") if param["type"] == "CONSTANT": params.append(SsbOpParamConstant(param["value"])) elif param["type"] == "CONST_STRING": params.append(SsbOpParamConstString(param["value"])) elif param["type"] == "LANG_STRING": params.append(SsbOpParamLanguageString(param["value"])) elif param["type"] == "POSITION_MARK": x_offset, x_relative = parse_pos_mark_arg(param["value"]["x"]) y_offset, y_relative = parse_pos_mark_arg(param["value"]["y"]) params.append(SsbOpParamPositionMarker( param["value"]["name"], x_offset, y_offset, x_relative, y_relative )) else: raise ValueError("Invalid param for op.") else: raise ValueError("Invalid param for op.") out_ops.append(SsbOperation( counter(), SsbOpCode(-1, op["opcode"]), params )) return out_ops def read_routines(routines: List[dict]) -> Tuple[List[SsbRoutineInfo], List[SsbCoroutine], List[List[SsbOperation]]]: routine_infos = [] named_coroutines = [] routine_ops = [] for r in routines: if "ops" not in r: raise ValueError("Ops for a routine not set.") if "type" not in r: raise ValueError("Type for a routine not set.") if r["type"] == "COROUTINE": if "name" not in r: raise ValueError("Target for a routine not set.") named_coroutines.append(SsbCoroutine(-1, r["name"])) routine_infos.append(SsbRoutineInfo(SsbRoutineType.COROUTINE, -1)) routine_ops.append(read_ops(r["ops"])) elif r["type"] == "GENERIC": named_coroutines.append(SsbCoroutine(-1, "n/a")) routine_infos.append(SsbRoutineInfo(SsbRoutineType.GENERIC, -1)) routine_ops.append(read_ops(r["ops"])) elif r["type"] == "ACTOR": if "target_id" not in r: raise ValueError("Target for a routine not set.") linked_to = -1 linked_to_name = None if isinstance(r["target_id"], int): linked_to = r["target_id"] else: linked_to_name = str(r["target_id"]) named_coroutines.append(SsbCoroutine(-1, "n/a")) routine_infos.append(SsbRoutineInfo(SsbRoutineType.ACTOR, linked_to, linked_to_name)) routine_ops.append(read_ops(r["ops"])) elif r["type"] == "OBJECT": if "target_id" not in r: raise ValueError("Target for a routine not set.") linked_to = -1 linked_to_name = None if isinstance(r["target_id"], int): linked_to = r["target_id"] else: linked_to_name = str(r["target_id"]) named_coroutines.append(SsbCoroutine(-1, "n/a")) routine_infos.append(SsbRoutineInfo(SsbRoutineType.OBJECT, linked_to, linked_to_name)) routine_ops.append(read_ops(r["ops"])) elif r["type"] == "PERFORMER": if "target_id" not in r: raise ValueError("Target for a routine not set.") linked_to = -1 linked_to_name = None if isinstance(r["target_id"], int): linked_to = r["target_id"] else: linked_to_name = str(r["target_id"]) named_coroutines.append(SsbCoroutine(-1, "n/a")) routine_infos.append(SsbRoutineInfo(SsbRoutineType.PERFORMER, linked_to, linked_to_name)) routine_ops.append(read_ops(r["ops"])) else: raise ValueError(f"Invalid type for a routine: {r['type']}.") return routine_infos, named_coroutines, routine_ops if __name__ == '__main__': parser = argparse.ArgumentParser(description='Decompile a JSON representation of PMD EoS SSB into ExplorerScript.') parser.add_argument('ssb_path', metavar='SSB_JSON_PATH', help='SSB JSON to decompile, including the compiler settings.') parser.add_argument('--source-map', dest='source_map', default=None, metavar='PATH', help='If specified, output a source map at that location.') args = parser.parse_args() if not os.path.exists(args.ssb_path): print("JSON file does not exist.", file=sys.stderr) exit(1) with open_utf8(args.ssb_path, 'r') as f: ssb_file = json.load(f) check_settings(ssb_file) routine_infos, named_coroutines, routine_ops = read_routines(ssb_file["routines"]) dmodes = ssb_file[SETTINGS][SETTINGS_DUNGEON_MODE_CONSTANTS] decompiler = ExplorerScriptSsbDecompiler( routine_infos, routine_ops, named_coroutines, ssb_file[SETTINGS][SETTINGS_PERFORMANCE_PROGRESS_LIST_VAR_NAME], DungeonModeConstants( dmodes[SETTINGS_DMC_CLOSED], dmodes[SETTINGS_DMC_OPEN], dmodes[SETTINGS_DMC_REQUEST], dmodes[SETTINGS_DMC_OPEN_REQUEST] ) ) exps_src, source_map = decompiler.convert() if args.source_map is not None: with open_utf8(args.source_map, 'w') as f: f.write(source_map.serialize()) print(exps_src)
py
1a4b57c772adff54d56ea0dd459deabd3b6b1f4e
""" Authors: Randy Heiland ([email protected]) Adam Morrow, Grant Waldrow, Drew Willis, Kim Crevecoeur Dr. Paul Macklin ([email protected]) --- Versions --- 0.1 - initial version """ import sys from PySide6 import QtCore, QtGui from PySide6.QtWidgets import * from PySide6.QtGui import QDoubleValidator class QHLine(QFrame): def __init__(self): super(QHLine, self).__init__() self.setFrameShape(QFrame.HLine) self.setFrameShadow(QFrame.Sunken) class SubstrateDef(QWidget): def __init__(self): super().__init__() # global self.microenv_params self.current_substrate = None self.xml_root = None self.celldef_tab = None #--------------- # self.cell_defs = CellDefInstances() self.microenv_hbox = QHBoxLayout() splitter = QSplitter() tree_widget_width = 160 self.tree = QTreeWidget() # self.tree.setStyleSheet("background-color: lightgray") self.tree.setFixedWidth(tree_widget_width) # self.tree.currentChanged(self.tree_item_changed_cb) self.tree.itemClicked.connect(self.tree_item_changed_cb) # self.tree.itemSelectionChanged() # self.tree.setColumnCount(1) # self.tree.setCurrentItem(0) # rwh/TODO header = QTreeWidgetItem(["--- Substrate ---"]) self.tree.setHeaderItem(header) # cellname = QTreeWidgetItem(["virus"]) # self.tree.insertTopLevelItem(0,cellname) # cellname = QTreeWidgetItem(["interferon"]) # self.tree.insertTopLevelItem(1,cellname) self.microenv_hbox.addWidget(self.tree) self.scroll_cell_def_tree = QScrollArea() self.scroll_cell_def_tree.setWidget(self.tree) # splitter.addWidget(self.tree) splitter.addWidget(self.scroll_cell_def_tree) #------------------------------------------- # self.tab = QWidget() # self.tabs.resize(200,5) #------------------------------------------- label_width = 150 units_width = 70 # self.scroll = QScrollArea() self.scroll_area = QScrollArea() splitter.addWidget(self.scroll_area) # self.microenv_hbox.addWidget(self.scroll_area) self.microenv_params = QWidget() self.vbox = QVBoxLayout() self.vbox.addStretch(0) # self.microenv_hbox.addWidget(self.) #------------------ controls_hbox = QHBoxLayout() self.new_button = QPushButton("New") controls_hbox.addWidget(self.new_button) self.copy_button = QPushButton("Copy") controls_hbox.addWidget(self.copy_button) self.delete_button = QPushButton("Delete") self.delete_button.clicked.connect(self.delete_substrate) controls_hbox.addWidget(self.delete_button) # self.vbox.addLayout(hbox) # self.vbox.addWidget(QHLine()) #------------------ hbox = QHBoxLayout() label = QLabel("Name of substrate:") label.setFixedWidth(180) label.setAlignment(QtCore.Qt.AlignRight) hbox.addWidget(label) self.substrate_name = QLineEdit() # Want to validate name, e.g., starts with alpha, no special chars, etc. # self.cycle_trate0_0.setValidator(QtGui.QDoubleValidator()) # self.cycle_trate0_1.enter.connect(self.save_xml) hbox.addWidget(self.substrate_name) self.vbox.addLayout(hbox) #------------------ hbox = QHBoxLayout() label = QLabel("diffusion coefficient") label.setFixedWidth(label_width) label.setAlignment(QtCore.Qt.AlignRight) hbox.addWidget(label) self.diffusion_coef = QLineEdit() self.diffusion_coef.setValidator(QtGui.QDoubleValidator()) # self.diffusion_coef.enter.connect(self.save_xml) hbox.addWidget(self.diffusion_coef) units = QLabel("micron^2/min") units.setFixedWidth(units_width) hbox.addWidget(units) self.vbox.addLayout(hbox) #---------- hbox = QHBoxLayout() label = QLabel("decay rate") label.setFixedWidth(label_width) label.setAlignment(QtCore.Qt.AlignRight) hbox.addWidget(label) self.decay_rate = QLineEdit() self.decay_rate.setValidator(QtGui.QDoubleValidator()) # self.decay_rate.enter.connect(self.save_xml) hbox.addWidget(self.decay_rate) units = QLabel("1/min") units.setFixedWidth(units_width) hbox.addWidget(units) self.vbox.addLayout(hbox) #---------- hbox = QHBoxLayout() label = QLabel("initial condition") label.setFixedWidth(label_width) label.setAlignment(QtCore.Qt.AlignRight) hbox.addWidget(label) self.init_cond = QLineEdit() self.init_cond.setValidator(QtGui.QDoubleValidator()) # self.init_cond.enter.connect(self.save_xml) hbox.addWidget(self.init_cond) units = QLabel("mmol") units.setFixedWidth(units_width) hbox.addWidget(units) self.vbox.addLayout(hbox) #---------- hbox = QHBoxLayout() label = QLabel("Dirichlet BC") label.setFixedWidth(label_width) label.setAlignment(QtCore.Qt.AlignRight) hbox.addWidget(label) self.dirichlet_bc = QLineEdit() self.dirichlet_bc.setValidator(QtGui.QDoubleValidator()) # self.bdy_cond.enter.connect(self.save_xml) hbox.addWidget(self.dirichlet_bc) units = QLabel("mmol") units.setFixedWidth(units_width) hbox.addWidget(units) self.dirichlet_bc_enabled = QCheckBox("on/off") # self.motility_enabled.setAlignment(QtCore.Qt.AlignRight) # label.setFixedWidth(label_width) hbox.addWidget(self.dirichlet_bc_enabled) self.vbox.addLayout(hbox) #------------- hbox = QHBoxLayout() self.gradients = QCheckBox("calculate gradients") hbox.addWidget(self.gradients) self.vbox.addLayout(hbox) hbox = QHBoxLayout() self.track_in_agents = QCheckBox("track in agents") hbox.addWidget(self.track_in_agents) self.vbox.addLayout(hbox) #-------------------------- # <Dirichlet_boundary_condition units="dimensionless" enabled="false">0</Dirichlet_boundary_condition> # <!-- # <Dirichlet_options> # <boundary_value ID="xmin" enabled="false">0</boundary_value> # <boundary_value ID="xmax" enabled="false">0</boundary_value> # <boundary_value ID="ymin" enabled="false">0</boundary_value> # <boundary_value ID="ymax" enabled="false">0</boundary_value> # <boundary_value ID="zmin" enabled="false">1</boundary_value> # <boundary_value ID="zmax" enabled="false">0</boundary_value> # </Dirichlet_options> # --> # </variable> #-------------------------- # Dummy widget for filler?? # label = QLabel("") # label.setFixedHeight(1000) # # label.setStyleSheet("background-color: orange") # label.setAlignment(QtCore.Qt.AlignCenter) # self.vbox.addWidget(label) #================================================================== # self.vbox.setAlignment(QtCore.Qt.AlignTop) # spacerItem = QSpacerItem(20, 237, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding) # spacerItem = QSpacerItem(100,500) # self.vbox.addItem(spacerItem) self.vbox.addStretch() self.microenv_params.setLayout(self.vbox) self.scroll_area.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) self.scroll_area.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) self.scroll_area.setWidgetResizable(True) self.scroll_area.setWidget(self.microenv_params) # self.save_button = QPushButton("Save") # self.text = QLabel("Hello World",alignment=QtCore.Qt.AlignCenter) self.layout = QVBoxLayout(self) self.layout.addLayout(controls_hbox) # self.layout.addWidget(self.tabs) # self.layout.addWidget(QHLine()) # self.layout.addWidget(self.params) # self.layout.addWidget(self.scroll_area) self.layout.addWidget(splitter) # self.layout.addWidget(self.vbox) # self.layout.addWidget(self.text) # self.layout.addWidget(self.save_button) # self.save_button.clicked.connect(self.save_xml) @QtCore.Slot() def delete_substrate(self): print('------ delete_substrate') item_idx = self.tree.indexFromItem(self.tree.currentItem()).row() print('------ item_idx=',item_idx) # self.tree.removeItemWidget(self.tree.currentItem(), 0) self.tree.takeTopLevelItem(self.tree.indexOfTopLevelItem(self.tree.currentItem())) self.celldef_tab.delete_substrate_from_comboboxes(item_idx) print('------ new name=',self.tree.currentItem().text(0)) self.current_substrate = self.tree.currentItem().text(0) self.fill_gui(self.current_substrate) # @QtCore.Slot() # def save_xml(self): # # self.text.setText(random.choice(self.hello)) # pass # def tree_item_changed(self,idx1,idx2): def tree_item_changed_cb(self, it,col): print('tree_item_changed:', it, col, it.text(col) ) self.current_substrate = it.text(col) print('self.current_substrate= ',self.current_substrate ) # print('self.= ',self.tree.indexFromItem ) # fill in the GUI with this one's params self.fill_gui(self.current_substrate) def populate_tree(self): uep = self.xml_root.find(".//microenvironment_setup") if uep: self.tree.clear() idx = 0 # <microenvironment_setup> # <variable name="food" units="dimensionless" ID="0"> for var in uep: # print(cell_def.attrib['name']) if var.tag == 'variable': var_name = var.attrib['name'] subname = QTreeWidgetItem([var_name]) self.tree.insertTopLevelItem(idx,subname) idx += 1 def first_substrate_name(self): uep = self.xml_root.find(".//microenvironment_setup//variable") if uep: return(uep.attrib['name']) def fill_gui(self, substrate_name): # <microenvironment_setup> # <variable name="food" units="dimensionless" ID="0"> uep = self.xml_root.find('.//microenvironment_setup') # find unique entry point if substrate_name == None: substrate_name = self.xml_root.find(".//microenvironment_setup//variable").attrib['name'] self.substrate_name.setText(substrate_name) vp = [] # pointers to <variable> nodes if uep: # self.tree.clear() idx = 0 for var in uep.findall('variable'): vp.append(var) # print(var.attrib['name']) name = var.attrib['name'] subname = QTreeWidgetItem([name]) # self.tree.insertTopLevelItem(idx,substrate_name) if subname.text(0) == substrate_name: print("break out of substrate (variable) name loop with idx=",idx) break idx += 1 # self.tree.setCurrentItem(substrate_name,0) # RWH/TODO: select 1st (0th?) item upon startup or loading new model idx += 1 # we use 1-offset indices below var_param_path = self.xml_root.find(".//microenvironment_setup//variable[" + str(idx) + "]//physical_parameter_set") var_path = self.xml_root.find(".//microenvironment_setup//variable[" + str(idx) + "]") # uep = self.xml_root.find('.//microenvironment_setup') # find unique entry point # <variable name="oxygen" units="mmHg" ID="0"> # <physical_parameter_set> # <diffusion_coefficient units="micron^2/min">100000.0</diffusion_coefficient> # <decay_rate units="1/min">0.1</decay_rate> # </physical_parameter_set> # <initial_condition units="mmHg">38.0</initial_condition> # <Dirichlet_boundary_condition units="mmHg" enabled="true">38.0</ # self.substrate_name.setText(var.attrib['name']) self.diffusion_coef.setText(var_param_path.find('.//diffusion_coefficient').text) self.decay_rate.setText(var_param_path.find('.//decay_rate').text) self.init_cond.setText(var_path.find('.initial_condition').text) self.dirichlet_bc.setText(var_path.find('.Dirichlet_boundary_condition').text) # self.chemical_A_decay_rate.value = float(vp[0].find('.//decay_rate').text) # self.chemical_A_initial_condition.value = float(vp[0].find('.//initial_condition').text) # self.chemical_A_Dirichlet_boundary_condition.value = float(vp[0].find('.//Dirichlet_boundary_condition').text) # if vp[0].find('.//Dirichlet_boundary_condition').attrib['enabled'].lower() == 'true': # self.chemical_A_Dirichlet_boundary_condition_toggle.value = True # else: # self.chemical_A_Dirichlet_boundary_condition_toggle.value = False # self.chemical_B_diffusion_coefficient.value = float(vp[1].find('.//diffusion_coefficient').text) # self.chemical_B_decay_rate.value = float(vp[1].find('.//decay_rate').text) # self.chemical_B_initial_condition.value = float(vp[1].find('.//initial_condition').text) # self.chemical_B_Dirichlet_boundary_condition.value = float(vp[1].find('.//Dirichlet_boundary_condition').text) # if vp[1].find('.//Dirichlet_boundary_condition').attrib['enabled'].lower() == 'true': # self.chemical_B_Dirichlet_boundary_condition_toggle.value = True # else: # self.chemical_B_Dirichlet_boundary_condition_toggle.value = False # self.chemical_C_diffusion_coefficient.value = float(vp[2].find('.//diffusion_coefficient').text) # self.chemical_C_decay_rate.value = float(vp[2].find('.//decay_rate').text) # self.chemical_C_initial_condition.value = float(vp[2].find('.//initial_condition').text) # self.chemical_C_Dirichlet_boundary_condition.value = float(vp[2].find('.//Dirichlet_boundary_condition').text) # if vp[2].find('.//Dirichlet_boundary_condition').attrib['enabled'].lower() == 'true': # self.chemical_C_Dirichlet_boundary_condition_toggle.value = True # else: # self.chemical_C_Dirichlet_boundary_condition_toggle.value = False # if uep.find('.//options//calculate_gradients').text.lower() == 'true': # self.calculate_gradient.value = True # else: # self.calculate_gradient.value = False # if uep.find('.//options//track_internalized_substrates_in_each_agent').text.lower() == 'true': # self.track_internal.value = True # else: # self.track_internal.value = False #---------------------------------------------------------------------------- # Read values from the GUI widgets and generate/write a new XML # <microenvironment_setup> # <variable name="director signal" units="dimensionless" ID="0"> # <physical_parameter_set> # <diffusion_coefficient units="micron^2/min">1000</diffusion_coefficient> # <decay_rate units="1/min">.1</decay_rate> # </physical_parameter_set> # <initial_condition units="dimensionless">0</initial_condition> # <Dirichlet_boundary_condition units="dimensionless" enabled="false">1</Dirichlet_boundary_condition> # </variable> # <variable name="cargo signal" units="dimensionless" ID="1"> # <physical_parameter_set> # <diffusion_coefficient units="micron^2/min">1000</diffusion_coefficient> # <decay_rate units="1/min">.4</decay_rate> # </physical_parameter_set> # <initial_condition units="dimensionless">0</initial_condition> # <Dirichlet_boundary_condition units="dimensionless" enabled="false">1</Dirichlet_boundary_condition> # </variable> # <options> # <calculate_gradients>true</calculate_gradients> # <track_internalized_substrates_in_each_agent>false</track_internalized_substrates_in_each_agent> # <initial_condition type="matlab" enabled="false"> # <filename>./config/initial.mat</filename> # </initial_condition> # <dirichlet_nodes type="matlab" enabled="false"> # <filename>./config/dirichlet.mat</filename> # </dirichlet_nodes> # </options> # </microenvironment_setup> def fill_xml(self): # pass uep = self.xml_root.find('.//microenvironment_setup') vp = [] # pointers to <variable> nodes if uep: for var in uep.findall('variable'): vp.append(var) uep = self.xml_root.find('.//microenvironment_setup') # self.diffusion_coef.setText(var_param_path.find('.//diffusion_coefficient').text) # self.decay_rate.setText(var_param_path.find('.//decay_rate').text) # self.init_cond.setText(var_path.find('.initial_condition').text) # self.dirichlet_bc.setText(var_path.find('.Dirichlet_boundary_condition').text) vp[0].find('.//diffusion_coefficient').text = str(self.diffusion_coef.text) # vp[0].find('.//diffusion_coefficient').text = str(self.director_signal_diffusion_coefficient.value) # vp[0].find('.//decay_rate').text = str(self.director_signal_decay_rate.value) # vp[0].find('.//initial_condition').text = str(self.director_signal_initial_condition.value) # vp[0].find('.//Dirichlet_boundary_condition').text = str(self.director_signal_Dirichlet_boundary_condition.value) # vp[0].find('.//Dirichlet_boundary_condition').attrib['enabled'] = str(self.director_signal_Dirichlet_boundary_condition_toggle.value).lower() # vp[1].find('.//diffusion_coefficient').text = str(self.cargo_signal_diffusion_coefficient.value) # vp[1].find('.//decay_rate').text = str(self.cargo_signal_decay_rate.value) # vp[1].find('.//initial_condition').text = str(self.cargo_signal_initial_condition.value) # vp[1].find('.//Dirichlet_boundary_condition').text = str(self.cargo_signal_Dirichlet_boundary_condition.value) # vp[1].find('.//Dirichlet_boundary_condition').attrib['enabled'] = str(self.cargo_signal_Dirichlet_boundary_condition_toggle.value).lower() # uep.find('.//options//calculate_gradients').text = str(self.calculate_gradient.value) # uep.find('.//options//track_internalized_substrates_in_each_agent').text = str(self.track_internal.value) def clear_gui(self): pass
py
1a4b5880174904507dee3c9966a421e3eed5a5f9
# # 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. # import unittest from typing import Any, Dict from unittest import mock from google.cloud.language_v1.proto.language_service_pb2 import Document from airflow.providers.google.cloud.hooks.natural_language import CloudNaturalLanguageHook from tests.providers.google.cloud.utils.base_gcp_mock import mock_base_gcp_hook_no_default_project_id API_RESPONSE = {} # type: Dict[Any, Any] DOCUMENT = Document( content="Airflow is a platform to programmatically author, schedule and monitor workflows." ) ENCODING_TYPE = "UTF32" class TestCloudNaturalLanguageHook(unittest.TestCase): def setUp(self): with mock.patch( "airflow.providers.google.common.hooks.base_google.GoogleBaseHook.__init__", new=mock_base_gcp_hook_no_default_project_id, ): self.hook = CloudNaturalLanguageHook(gcp_conn_id="test") @mock.patch( "airflow.providers.google.cloud.hooks.natural_language.CloudNaturalLanguageHook.client_info", new_callable=mock.PropertyMock, ) @mock.patch( "airflow.providers.google.cloud.hooks.natural_language.CloudNaturalLanguageHook." "_get_credentials" ) @mock.patch("airflow.providers.google.cloud.hooks.natural_language.LanguageServiceClient") def test_language_service_client_creation(self, mock_client, mock_get_creds, mock_client_info): result = self.hook.get_conn() mock_client.assert_called_once_with( credentials=mock_get_creds.return_value, client_info=mock_client_info.return_value ) self.assertEqual(mock_client.return_value, result) self.assertEqual(self.hook._conn, result) @mock.patch( "airflow.providers.google.cloud.hooks.natural_language.CloudNaturalLanguageHook.get_conn", ) def test_analyze_entities(self, get_conn): get_conn.return_value.analyze_entities.return_value = API_RESPONSE result = self.hook.analyze_entities(document=DOCUMENT, encoding_type=ENCODING_TYPE) self.assertEqual(result, API_RESPONSE) get_conn.return_value.analyze_entities.assert_called_once_with( document=DOCUMENT, encoding_type=ENCODING_TYPE, retry=None, timeout=None, metadata=None ) @mock.patch( "airflow.providers.google.cloud.hooks.natural_language.CloudNaturalLanguageHook.get_conn", ) def test_analyze_entity_sentiment(self, get_conn): get_conn.return_value.analyze_entity_sentiment.return_value = API_RESPONSE result = self.hook.analyze_entity_sentiment(document=DOCUMENT, encoding_type=ENCODING_TYPE) self.assertEqual(result, API_RESPONSE) get_conn.return_value.analyze_entity_sentiment.assert_called_once_with( document=DOCUMENT, encoding_type=ENCODING_TYPE, retry=None, timeout=None, metadata=None ) @mock.patch( "airflow.providers.google.cloud.hooks.natural_language.CloudNaturalLanguageHook.get_conn", ) def test_analyze_sentiment(self, get_conn): get_conn.return_value.analyze_sentiment.return_value = API_RESPONSE result = self.hook.analyze_sentiment(document=DOCUMENT, encoding_type=ENCODING_TYPE) self.assertEqual(result, API_RESPONSE) get_conn.return_value.analyze_sentiment.assert_called_once_with( document=DOCUMENT, encoding_type=ENCODING_TYPE, retry=None, timeout=None, metadata=None ) @mock.patch( "airflow.providers.google.cloud.hooks.natural_language.CloudNaturalLanguageHook.get_conn", ) def test_analyze_syntax(self, get_conn): get_conn.return_value.analyze_syntax.return_value = API_RESPONSE result = self.hook.analyze_syntax(document=DOCUMENT, encoding_type=ENCODING_TYPE) self.assertEqual(result, API_RESPONSE) get_conn.return_value.analyze_syntax.assert_called_once_with( document=DOCUMENT, encoding_type=ENCODING_TYPE, retry=None, timeout=None, metadata=None ) @mock.patch( "airflow.providers.google.cloud.hooks.natural_language.CloudNaturalLanguageHook.get_conn", ) def test_annotate_text(self, get_conn): get_conn.return_value.annotate_text.return_value = API_RESPONSE result = self.hook.annotate_text(document=DOCUMENT, encoding_type=ENCODING_TYPE, features=None) self.assertEqual(result, API_RESPONSE) get_conn.return_value.annotate_text.assert_called_once_with( document=DOCUMENT, encoding_type=ENCODING_TYPE, features=None, retry=None, timeout=None, metadata=None, ) @mock.patch( "airflow.providers.google.cloud.hooks.natural_language.CloudNaturalLanguageHook.get_conn", ) def test_classify_text(self, get_conn): get_conn.return_value.classify_text.return_value = API_RESPONSE result = self.hook.classify_text(document=DOCUMENT) self.assertEqual(result, API_RESPONSE) get_conn.return_value.classify_text.assert_called_once_with( document=DOCUMENT, retry=None, timeout=None, metadata=None )
py
1a4b58ca9077725e29578fcea7315528666de088
import os import subprocess import sys from typing import List import psutil import logging def pip_install(pkg: str, pipbin: str = "pip3.10"): all_pkgs = [_.decode('ascii').lower() for _ in subprocess.check_output( [f"{pipbin}", 'list']).split()] if pkg in all_pkgs: logging.info(f"pkg= , {pkg}, is present") return "already present" else: os.system(f"{pipbin} install {pkg}") return "installed" class Error: pass error = Error() def exec_cmd(cmdl: List[str], errcheck: str = ""): """ """ try: res = subprocess.check_output(cmdl, stderr=subprocess.STDOUT) return res except subprocess.CalledProcessError as e: if errcheck in str(e.output): logging.info(f"{cmdl[0]} already active") else: raise e def append_to_file(fullfp: str, apstr: str): dirn = os.path.dirname(fullfp) fn = os.path.basename(fullfp) if os.path.exists(fullfp): os.system(f"doas cp {fullfp} {fullfp}.premod") os.system(f"cp {fullfp} /tmp/{fn}") with open(f"/tmp/{fn}", "a") as fh: fh.write(apstr) os.system(f"doas mv /tmp/{fn} {fullfp}") def checkIfProcessRunning(processName): ''' Check if there is any running process that contains the given name processName. ''' # Iterate over the all the running process for proc in psutil.process_iter(): try: # Check if process name contains the given name string. if processName.lower() in proc.name().lower(): return True except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): pass return False
py
1a4b5921b730831e2a919d37d23bc559fba281f2
from copy import deepcopy import numpy as np import pandas as pd import torch from torch.optim import Adam import gym import time import spinup.algos.pytorch.ddpg.core as core from spinup.utils.logx import EpochLogger class ReplayBuffer: """ A simple FIFO experience replay buffer for DDPG agents. """ def __init__(self, obs_dim, act_dim, size): self.obs_buf = np.zeros(core.combined_shape(size, obs_dim), dtype=np.float32) self.obs2_buf = np.zeros(core.combined_shape(size, obs_dim), dtype=np.float32) self.act_buf = np.zeros(core.combined_shape(size, act_dim), dtype=np.float32) self.rew_buf = np.zeros(size, dtype=np.float32) self.done_buf = np.zeros(size, dtype=np.float32) self.ptr, self.size, self.max_size = 0, 0, size def store(self, obs, act, rew, next_obs, done): self.obs_buf[self.ptr] = obs self.obs2_buf[self.ptr] = next_obs self.act_buf[self.ptr] = act self.rew_buf[self.ptr] = rew self.done_buf[self.ptr] = done self.ptr = (self.ptr+1) % self.max_size self.size = min(self.size+1, self.max_size) def sample_batch(self, batch_size=32): idxs = np.random.randint(0, self.size, size=batch_size) batch = dict(obs=self.obs_buf[idxs], obs2=self.obs2_buf[idxs], act=self.act_buf[idxs], rew=self.rew_buf[idxs], done=self.done_buf[idxs]) return {k: torch.as_tensor(v, dtype=torch.float32) for k,v in batch.items()} """ The following parameters have been changed: env_fn: default value set to core.ALMEnv epochs: default value changed from 100 to 300 act_noise: default value changed from .1 to .01 time_horizon: added parameter, with default value 80 discount_rate: added parameter, with default value .06 """ def ddpg(env_fn = core.ALMEnv, actor_critic = core.MLPActorCritic, ac_kwargs = dict(), seed = 0, steps_per_epoch = 4000, epochs = 300, replay_size = int(1e6), gamma = 0.99, polyak=0.995, pi_lr = 1e-3, q_lr = 1e-3, batch_size = 100, start_steps = 10000, update_after = 1000, update_every = 50, act_noise = .01, num_test_episodes = 10, max_ep_len = 1000, logger_kwargs = dict(), save_freq = 1, time_horizon = 80, discount_rate = .06): """ Deep Deterministic Policy Gradient (DDPG) Args: env_fn : A function which creates a copy of the environment. The environment must satisfy the OpenAI Gym API. In this version, the default environment is 'ALMEnv' actor_critic: The constructor method for a PyTorch Module with an ``act`` method, a ``pi`` module, and a ``q`` module. The ``act`` method and ``pi`` module should accept batches of observations as inputs, and ``q`` should accept a batch of observations and a batch of actions as inputs. When called, these should return: =========== ================ ====================================== Call Output Shape Description =========== ================ ====================================== ``act`` (batch, act_dim) | Numpy array of actions for each | observation. ``pi`` (batch, act_dim) | Tensor containing actions from policy | given observations. ``q`` (batch,) | Tensor containing the current estimate | of Q* for the provided observations | and actions. (Critical: make sure to | flatten this!) =========== ================ ====================================== ac_kwargs (dict): Any kwargs appropriate for the ActorCritic object you provided to DDPG. seed (int): Seed for random number generators. steps_per_epoch (int): Number of steps of interaction (state-action pairs) for the agent and the environment in each epoch. epochs (int): Number of epochs to run and train agent. replay_size (int): Maximum length of replay buffer. gamma (float): Discount factor. (Always between 0 and 1.) polyak (float): Interpolation factor in polyak averaging for target networks. Target networks are updated towards main networks according to: .. math:: \\theta_{\\text{targ}} \\leftarrow \\rho \\theta_{\\text{targ}} + (1-\\rho) \\theta where :math:`\\rho` is polyak. (Always between 0 and 1, usually close to 1.) pi_lr (float): Learning rate for policy. q_lr (float): Learning rate for Q-networks. batch_size (int): Minibatch size for SGD. start_steps (int): Number of steps for uniform-random action selection, before running real policy. Helps exploration. update_after (int): Number of env interactions to collect before starting to do gradient descent updates. Ensures replay buffer is full enough for useful updates. update_every (int): Number of env interactions that should elapse between gradient descent updates. Note: Regardless of how long you wait between updates, the ratio of env steps to gradient steps is locked to 1. act_noise (float): Stddev for Gaussian exploration noise added to policy at training time. (At test time, no noise is added.) num_test_episodes (int): Number of episodes to test the deterministic policy at the end of each epoch. max_ep_len (int): Maximum length of trajectory / episode / rollout. logger_kwargs (dict): Keyword args for EpochLogger. save_freq (int): How often (in terms of gap between epochs) to save the current policy and value function. """ logger = EpochLogger(**logger_kwargs) logger.save_config(locals()) torch.manual_seed(seed) np.random.seed(seed) # env, test_env = env_fn(), env_fn() original OpenAI SpinningUp entry env = env_fn(T = time_horizon, rate = discount_rate) # Added by the author test_env = env_fn(T = time_horizon, rate = discount_rate) # Added by the author obs_dim = env.observation_space.shape act_dim = env.action_space.shape[0] # Action limit for clamping: critically, assumes all dimensions share the same bound! act_limit = env.action_space.high[0] # Create actor-critic module and target networks ac = actor_critic(env.observation_space, env.action_space, **ac_kwargs) ac_targ = deepcopy(ac) # Freeze target networks with respect to optimizers (only update via polyak averaging) for p in ac_targ.parameters(): p.requires_grad = False # Experience buffer replay_buffer = ReplayBuffer(obs_dim=obs_dim, act_dim=act_dim, size=replay_size) # Count variables (protip: try to get a feel for how different size networks behave!) var_counts = tuple(core.count_vars(module) for module in [ac.pi, ac.q]) logger.log('\nNumber of parameters: \t pi: %d, \t q: %d\n'%var_counts) # Set up function for computing DDPG Q-loss def compute_loss_q(data): o, a, r, o2, d = data['obs'], data['act'], data['rew'], data['obs2'], data['done'] q = ac.q(o,a) # Bellman backup for Q function with torch.no_grad(): q_pi_targ = ac_targ.q(o2, ac_targ.pi(o2)) backup = r + gamma * (1 - d) * q_pi_targ # MSE loss against Bellman backup loss_q = ((q - backup)**2).mean() # Useful info for logging loss_info = dict(QVals=q.detach().numpy()) return loss_q, loss_info # Set up function for computing DDPG pi loss def compute_loss_pi(data): o = data['obs'] q_pi = ac.q(o, ac.pi(o)) return -q_pi.mean() # Set up optimizers for policy and q-function pi_optimizer = Adam(ac.pi.parameters(), lr=pi_lr) q_optimizer = Adam(ac.q.parameters(), lr=q_lr) # Set up model saving logger.setup_pytorch_saver(ac) def update(data): # First run one gradient descent step for Q. q_optimizer.zero_grad() loss_q, loss_info = compute_loss_q(data) loss_q.backward() q_optimizer.step() # Freeze Q-network so you don't waste computational effort # computing gradients for it during the policy learning step. for p in ac.q.parameters(): p.requires_grad = False # Next run one gradient descent step for pi. pi_optimizer.zero_grad() loss_pi = compute_loss_pi(data) loss_pi.backward() pi_optimizer.step() # Unfreeze Q-network so you can optimize it at next DDPG step. for p in ac.q.parameters(): p.requires_grad = True # Record things logger.store(LossQ=loss_q.item(), LossPi=loss_pi.item(), **loss_info) # Finally, update target networks by polyak averaging. with torch.no_grad(): for p, p_targ in zip(ac.parameters(), ac_targ.parameters()): # NB: We use an in-place operations "mul_", "add_" to update target # params, as opposed to "mul" and "add", which would make new tensors. p_targ.data.mul_(polyak) p_targ.data.add_((1 - polyak) * p.data) def get_action(o, noise_scale): a = ac.act(torch.as_tensor(o, dtype=torch.float32)) a = a * (noise_scale * np.random.randn(act_dim) + 1) # Added by the author return (a / np.sum(a)) # Added by the author # a += noise_scale * np.random.randn(act_dim) Original OpenAI SpinningUp entry # return np.clip(a, -act_limit, act_limit) Original OpenAI SpinningUp entry def test_agent(): for j in range(num_test_episodes): o, d, ep_ret, ep_len = test_env.reset(), False, 0, 0 while not(d or (ep_len == max_ep_len)): # Take deterministic actions at test time (noise_scale=0) o, r, d, _ = test_env.step(get_action(o, 0)) ep_ret += r ep_len += 1 logger.store(TestEpRet=ep_ret, TestEpLen=ep_len) # Prepare for interaction with environment total_steps = steps_per_epoch * epochs start_time = time.time() o, ep_ret, ep_len = env.reset(), 0, 0 # Main loop: collect experience in env and update/log each epoch for t in range(total_steps): """ Until start_steps have elapsed, randomly sample actions from a uniform distribution for better exploration. Afterwards, use the learned policy (with some noise, via act_noise). """ if t > start_steps: a = get_action(o, act_noise) else: a = env.action_space.sample() # Step the env o2, r, d, _ = env.step(a) ep_ret += r ep_len += 1 # Ignore the "done" signal if it comes from hitting the time # horizon (that is, when it's an artificial terminal signal # that isn't based on the agent's state) d = False if ep_len==max_ep_len else d # Store experience to replay buffer replay_buffer.store(o, a, r, o2, d) # Super critical, easy to overlook step: make sure to update # most recent observation! o = o2 # End of trajectory handling if d or (ep_len == max_ep_len): logger.store(EpRet=ep_ret, EpLen=ep_len) o, ep_ret, ep_len = env.reset(), 0, 0 # Update handling if t >= update_after and t % update_every == 0: for _ in range(update_every): batch = replay_buffer.sample_batch(batch_size) update(data=batch) # End of epoch handling if (t+1) % steps_per_epoch == 0: epoch = (t+1) // steps_per_epoch # Save model if (epoch % save_freq == 0) or (epoch == epochs): logger.save_state({'env': env}, None) # Test the performance of the deterministic version of the agent. test_agent() # Log info about epoch logger.log_tabular('Epoch', epoch) logger.log_tabular('EpRet', with_min_and_max=True) logger.log_tabular('TestEpRet', with_min_and_max=True) logger.log_tabular('EpLen', average_only=True) logger.log_tabular('TestEpLen', average_only=True) logger.log_tabular('TotalEnvInteracts', t) logger.log_tabular('QVals', with_min_and_max=True) logger.log_tabular('LossPi', average_only=True) logger.log_tabular('LossQ', average_only=True) logger.log_tabular('Time', time.time()-start_time) logger.dump_tabular() if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() # parser.add_argument('--env', type=str, default='HalfCheetah-v2') Original OpenAI SpinningUp entry parser.add_argument('--hid', type=int, default=256) parser.add_argument('--l', type=int, default=2) parser.add_argument('--gamma', type=float, default=0.99) parser.add_argument('--seed', '-s', type=int, default=0) parser.add_argument('--epochs', type=int, default=50) parser.add_argument('--time_horizon', type = int, default = 80) # Added by the author parser.add_argument('--discount_rate', type = float, default = 0.06) # Added by the author parser.add_argument('--exp_name', type=str, default='ddpg') args = parser.parse_args() from spinup.utils.run_utils import setup_logger_kwargs logger_kwargs = setup_logger_kwargs(args.exp_name, args.seed) ddpg(env_fn = core.ALMEnv, actor_critic=core.MLPActorCritic, ac_kwargs=dict(hidden_sizes=[args.hid]*args.l), gamma=args.gamma, seed=args.seed, epochs=args.epochs, logger_kwargs=logger_kwargs, time_horizon = args.time_horizon, discount_rate = args.discount_rate)
py
1a4b593492c20df262e27d6045e59bc37d9dafb8
import os outlines = ( """#!/usr/bin/env bash #################################### # USAGE: ./sequential_projids.sh & # #################################### # sector 6, first galactic field reduction """ ) projidlines = [] for projid in range(1500,1517): projidlines.append( '( source activate trex_37; source projid_{}.sh; wait ) & wait\n'. format(projid) ) outname = 'tess_tuning_scripts/sector6_reduction.sh' with open(outname, 'w') as f: f.writelines(outlines) f.writelines(projidlines) print('wrote {}'.format(outname))
py
1a4b59a13d03e93e354269b068152c88db3e2ff3
#!/usr/bin/env python # # __COPYRIGHT__ # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" """ Test that we can actually build a simple program using our generated Visual Studio 10.0 project (.vcxproj) and solution (.sln) files using Visual Studio 10.0 (Professional edition). """ import os import sys import TestSConsMSVS test = TestSConsMSVS.TestSConsMSVS() if sys.platform != 'win32': msg = "Skipping Visual Studio test on non-Windows platform '%s'\n" % sys.platform test.skip_test(msg) msvs_version = '10.0' if not msvs_version in test.msvs_versions(): msg = "Visual Studio %s not installed; skipping test.\n" % msvs_version test.skip_test(msg) # Let SCons figure out the Visual Studio environment variables for us and # print out a statement that we can exec to suck them into our external # environment so we can execute devenv and really try to build something. test.run(arguments = '-n -q -Q -f -', stdin = """\ env = Environment(tools = ['msvc'], MSVS_VERSION='%(msvs_version)s') print("os.environ.update(%%s)" %% repr(env['ENV'])) """ % locals()) exec(test.stdout()) test.subdir('sub dir') test.write(['sub dir', 'SConstruct'], """\ env=Environment(MSVS_VERSION = '%(msvs_version)s') env.MSVSProject(target = 'foo.vcxproj', srcs = ['foo.c'], buildtarget = 'foo.exe', variant = 'Release') env.Program('foo.c') """ % locals()) test.write(['sub dir', 'foo.c'], r""" int main(int argc, char *argv) { printf("foo.c\n"); exit (0); } """) test.run(chdir='sub dir', arguments='.') test.vcproj_sys_path(test.workpath('sub dir', 'foo.vcxproj')) import SCons.Platform.win32 system_dll_path = os.path.join( SCons.Platform.win32.get_system_root(), 'System32' ) os.environ['PATH'] = os.environ['PATH'] + os.pathsep + system_dll_path test.run(chdir='sub dir', program=[test.get_msvs_executable(msvs_version)], arguments=['foo.sln', '/build', 'Release']) test.run(program=test.workpath('sub dir', 'foo'), stdout="foo.c\n") test.pass_test() # Local Variables: # tab-width:4 # indent-tabs-mode:nil # End: # vim: set expandtab tabstop=4 shiftwidth=4:
py
1a4b5a041fea8a084c2053c3eaf6e80f5709b241
def iterable(source): def callbag(start, sink): if (start != 0): return for i in source: sink(1, i) return callbag
py
1a4b5a252f92cbc25dc3bb6602809fed028e863a
# -*- coding: utf-8 -*- from config import RUN_VER if RUN_VER == 'open': from blueapps.patch.settings_open_saas import * # noqa else: from blueapps.patch.settings_paas_services import * # noqa # 本地开发环境 RUN_MODE = 'DEVELOP' # APP本地静态资源目录 STATIC_URL = '/static/' # APP静态资源目录url # REMOTE_STATIC_URL = '%sremote/' % STATIC_URL # Celery 消息队列设置 RabbitMQ # BROKER_URL = 'amqp://guest:guest@localhost:5672//' # Celery 消息队列设置 Redis BROKER_URL = 'redis://localhost:6379/0' DEBUG = True # 本地开发数据库设置 # USE FOLLOWING SQL TO CREATE THE DATABASE NAMED APP_CODE # SQL: CREATE DATABASE `framework_py` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; # noqa: E501 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'bkprometheus', 'USER': 'root', 'PASSWORD': '12345678', 'HOST': 'localhost', 'PORT': '3306', }, } # 多人开发时,无法共享的本地配置可以放到新建的 local_settings.py 文件中 # 并且把 local_settings.py 加入版本管理忽略文件中 try: from local_settings import * # noqa except ImportError: pass
py
1a4b5c99193e1eb0f58522277a131e24d779f0bd
from __future__ import unicode_literals import os.path import threading import time from future.builtins import str import zmq from zmq.eventloop import ioloop, zmqstream import tornado.testing ioloop.install() def test_server_creation(): from pseud import Server user_id = b'echo' server = Server(user_id) assert server.user_id == user_id assert server.security_plugin == 'noop_auth_backend' def test_server_can_bind(): from pseud import Server user_id = b'echo' endpoint = 'inproc://{}'.format(__name__).encode() server = Server(user_id, security_plugin='noop_auth_backend') server.bind(endpoint) def test_server_can_connect(): from pseud import Server user_id = b'echo' endpoint = b'tcp://127.0.0.1:5000' server = Server(user_id, security_plugin='noop_auth_backend') server.connect(endpoint) def test_server_with_its_loop_instance(): from pseud import SyncClient, Server endpoint = b'ipc:///tmp/test_socket' def start_server(): server = Server(b'a') server.bind(endpoint) server.register_rpc(str.lower) server.io_loop.add_timeout(server.io_loop.time() + .2, server.stop) server.start() server_thread = threading.Thread(target=start_server) server_thread.start() client = SyncClient() client.connect(endpoint) result = client.lower('TOTO') assert result == 'toto' class ServerTestCase(tornado.testing.AsyncTestCase): timeout = 2 def make_one_client_socket(self, endpoint): context = zmq.Context.instance() req_sock = context.socket(zmq.ROUTER) req_sock.connect(endpoint) return req_sock def make_one_server(self, user_id, endpoint): from pseud import Server server = Server(user_id, io_loop=self.io_loop) server.bind(endpoint) return server @tornado.testing.gen_test def test_job_running(self): from pseud.interfaces import EMPTY_DELIMITER, OK, VERSION, WORK from pseud.packer import Packer from pseud.utils import register_rpc user_id = b'echo' endpoint = 'inproc://{}'.format(self.__class__.__name__).encode() @register_rpc def job_success(a, b, c, d=None): time.sleep(.2) return True server = self.make_one_server(user_id, endpoint) socket = self.make_one_client_socket(endpoint) stream = zmqstream.ZMQStream(socket, io_loop=self.io_loop) work = Packer().packb(('job_success', (1, 2, 3), {'d': False})) yield tornado.gen.Task(stream.send_multipart, [user_id, EMPTY_DELIMITER, VERSION, b'', WORK, work]) yield server.start() response = yield tornado.gen.Task(stream.on_recv) assert response == [user_id, EMPTY_DELIMITER, VERSION, b'', OK, Packer().packb(True)] server.stop() @tornado.testing.gen_test def test_job_not_found(self): import pseud from pseud.interfaces import EMPTY_DELIMITER, ERROR, VERSION, WORK from pseud.packer import Packer user_id = b'echo' endpoint = 'inproc://{}'.format(self.__class__.__name__).encode() server = self.make_one_server(user_id, endpoint) socket = self.make_one_client_socket(endpoint) stream = zmqstream.ZMQStream(socket, io_loop=self.io_loop) work = Packer().packb(('thisIsNotAFunction', (), {})) yield server.start() yield tornado.gen.Task(stream.send_multipart, [user_id, EMPTY_DELIMITER, VERSION, b'', WORK, work]) response = yield tornado.gen.Task(stream.on_recv) assert response[:-1] == [user_id, EMPTY_DELIMITER, VERSION, b'', ERROR] klass, message, traceback = Packer().unpackb(response[-1]) assert klass == 'ServiceNotFoundError' assert message == 'thisIsNotAFunction' # pseud.__file__ might ends with .pyc assert os.path.dirname(pseud.__file__) in traceback server.stop() @tornado.testing.gen_test def test_job_raise(self): from pseud.interfaces import ERROR, VERSION, WORK from pseud.packer import Packer from pseud.utils import register_rpc user_id = b'echo' endpoint = 'inproc://{}'.format(self.__class__.__name__).encode() @register_rpc def job_buggy(*args, **kw): raise ValueError('too bad') server = self.make_one_server(user_id, endpoint) socket = self.make_one_client_socket(endpoint) stream = zmqstream.ZMQStream(socket, io_loop=self.io_loop) work = Packer().packb(('job_buggy', (), {})) yield server.start() yield tornado.gen.Task(stream.send_multipart, [user_id, b'', VERSION, b'', WORK, work]) response = yield tornado.gen.Task(stream.on_recv) assert response[:-1] == [user_id, b'', VERSION, b'', ERROR] klass, message, traceback = Packer().unpackb(response[-1]) assert klass == 'ValueError' assert message == 'too bad' assert __file__ in traceback server.stop()
py
1a4b5d0378e7bfe67ae7abff1aa70ce779fffb81
""" Copyright 2020 Google LLC Copyright 2020 PerfectVIPs 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. """ import argparse import logging import sys import vsc from bitstring import BitArray from pygen_src.riscv_instr_pkg import mtvec_mode_t, f_rounding_mode_t, \ riscv_reg_t, privileged_mode_t, \ riscv_instr_group_t from pygen_src.target.rv32i import riscv_core_setting as rcs @vsc.randobj class riscv_instr_gen_config: def __init__(self, argv): # TODO Support for command line argument self.main_program_instr_cnt = 100 # count of main_prog self.sub_program_instr_cnt = [] # count of sub_prog self.debug_program_instr_cnt = 0 # count of debug_rom self.debug_sub_program_instr_cnt = [] # count of debug sub_progrms # Commenting out for now # self.data_page_pattern = list( # map(lambda dta_pg: dta_pg.name, data_pattern_t)) # dicts for exception_cause_t & interrupt_cause_t Enum classes self.m_mode_exception_delegation = {} self.s_mode_exception_delegation = {} self.m_mode_interrupt_delegation = {} self.s_mode_interrupt_delegation = {} # init_privileged_mode default to MACHINE_MODE self.init_privileged_mode = privileged_mode_t.MACHINE_MODE self.mstatus = BitArray(bin(0b0), length=rcs.XLEN - 1) self.mie = BitArray(bin(0b0), length=rcs.XLEN - 1) self.sstatus = BitArray(bin(0b0), length=rcs.XLEN - 1) self.sie = BitArray(bin(0b0), length=rcs.XLEN - 1) self.ustatus = BitArray(bin(0b0), length=rcs.XLEN - 1) self.uie = BitArray(bin(0b0), length=rcs.XLEN - 1) self.mstatus_mprv = 0 self.mstatus_mxr = 0 self.mstatus_sum = 0 self.mstatus_tvm = 0 self.mstatus_fs = BitArray(bin(0b0), length=2) self.mstatus_vs = BitArray(bin(0b0), length=2) self.mtvec_mode = vsc.rand_enum_t(mtvec_mode_t) self.tvec_alignment = argv.tvec_alignment self.fcsr_rm = list(map(lambda csr_rm: csr_rm.name, f_rounding_mode_t)) self.enable_sfence = 0 self.gpr = vsc.rand_list_t(vsc.enum_t(riscv_reg_t), sz =4) self.scratch_reg = vsc.rand_enum_t(riscv_reg_t) self.pmp_reg = vsc.rand_enum_t(riscv_reg_t) self.sp = vsc.rand_enum_t(riscv_reg_t) self.tp = vsc.rand_enum_t(riscv_reg_t) self.ra = vsc.rand_enum_t(riscv_reg_t) self.check_misa_init_val = 0 self.check_xstatus = 1 self.virtual_addr_translation_on = 0 # Commenting out for now # vector_cfg = riscv_vector_cfg # TODO # pmp_cfg = riscv_pmp_cfg # TODO # self.mem_region = [] # TODO # Self.amo_region = [] # TODO self.stack_len = 5000 # Self.s_mem_region = [] # TODO self.kernel_stack_len = 4000 self.kernel_program_instr_cnt = 400 # list of main implemented CSRs self.invalid_priv_mode_csrs = [] self.num_of_sub_program = argv.num_of_sub_program self.instr_cnt = argv.instr_cnt self.num_of_tests = argv.num_of_tests self.no_data_page = argv.no_data_page self.no_branch_jump = argv.no_branch_jump self.no_load_store = argv.no_load_store self.no_csr_instr = argv.no_csr_instr self.no_ebreak = argv.no_ebreak self.no_dret = argv.no_dret self.no_fence = argv.no_fence self.no_wfi = argv.no_wfi self.enable_unaligned_load_store = argv.enable_unaligned_load_store self.illegal_instr_ratio = argv.illegal_instr_ratio self.hint_instr_ratio = argv.hint_instr_ratio self.num_of_harts = argv.num_of_harts self.fix_sp = argv.fix_sp self.use_push_data_section = argv.use_push_data_section self.boot_mode_opts = "" self.enable_page_table_exception = argv.enable_page_table_exception self.no_directed_instr = argv.no_directed_instr self.asm_test_suffix = "" self.enable_interrupt = argv.enable_interrupt self.enable_nested_interrupt = argv.enable_nested_interrupt self.enable_timer_irq = argv.enable_timer_irq self.bare_program_mode = argv.bare_program_mode self.enable_illegal_csr_instruction = argv.enable_illegal_csr_instruction self.enable_access_invalid_csr_level = argv.enable_access_invalid_csr_level self.enable_misaligned_instr = argv.enable_misaligned_instr self.enable_dummy_csr_write = argv.enable_dummy_csr_write self.randomize_csr = argv.randomize_csr self.allow_sfence_exception = argv.allow_sfence_exception self.no_delegation = argv.no_delegation self.force_m_delegation = argv.force_m_delegation self.force_s_delegation = argv.force_s_delegation self.support_supervisor_mode = 0 self.disable_compressed_instr = argv.disable_compressed_instr self.require_signature_addr = argv.require_signature_addr if(self.require_signature_addr): self.signature_addr = int(argv.signature_addr, 16) else: self.signature_addr = 0xdeadbeef self.gen_debug_section = argv.gen_debug_section self.enable_ebreak_in_debug_rom = argv.enable_ebreak_in_debug_rom self.set_dcsr_ebreak = argv.set_dcsr_ebreak self.num_debug_sub_program = argv.num_debug_sub_program self.enable_debug_single_step = argv.enable_debug_single_step self.single_step_iterations = 0 self.set_mstatus_tw = argv.set_mstatus_tw self.set_mstatus_mprv = argv.set_mstatus_mprv self.min_stack_len_per_program = 10 * (rcs.XLEN / 8) self.max_stack_len_per_program = 16 * (rcs.XLEN / 8) self.max_branch_step = 20 self.max_directed_instr_stream_seq = 20 self.reserved_regs = vsc.list_t(vsc.enum_t(riscv_reg_t)) self.enable_floating_point = argv.enable_floating_point self.enable_vector_extension = argv.enable_vector_extension self.enable_b_extension = argv.enable_b_extension # Commenting out for now # self.enable_bitmanip_groups = ['ZBB', 'ZBS', 'ZBP', 'ZBE', 'ZBF', # 'ZBC', 'ZBR', 'ZBM', 'ZBT', 'ZB_TMP'] self.dist_control_mode = 0 self.category_dist = {} @vsc.constraint def gpr_c(self): pass # TODO def check_setting(self): support_64b = 0 support_128b = 0 # list of satp_mode_t from riscv_core_setting.py stp_md_lst = rcs.SATP_MODE # list of riscv_instr_group_t with names of riscv_instr_name_t in it. supported_isa_lst = list(map(lambda z: z.name, riscv_instr_group_t)) # check the valid isa support for x in rcs.supported_isa: if x == (supported_isa_lst[1] or supported_isa_lst[3] or supported_isa_lst[5] or supported_isa_lst[8] or supported_isa_lst[11] or supported_isa_lst[13] or supported_isa_lst[19]): support_64b = 1 logging.info("support_64b=%d" % support_64b) logging.debug("Supported ISA=%s" % x) elif x == (supported_isa_lst[14] or supported_isa_lst[15]): support_128b = 1 logging.info("support_128b=%d" % support_128b) logging.debug("Supported ISA=%s" % x) if (support_128b == 1) and (rcs.XLEN != 128): logging.critical("XLEN should be set to 128 based on \ riscv_core_setting.supported_isa setting") logging.info("XLEN Value=%d" % rcs.XLEN) sys.exit("XLEN is not equal to 128, set it Accordingly!") if (support_128b == 0) and (support_64b == 1) and (rcs.XLEN != 64): logging.critical("XLEN should be set to 64 based on \ riscv_core_setting.supported_isa setting") logging.info("XLEN Value=%d" % rcs.XLEN) sys.exit("XLEN is not equal to 64, set it Accordingly!") if not(support_128b or support_64b) and (rcs.XLEN != 32): logging.critical("XLEN should be set to 32 based on \ riscv_core_setting.supported_isa setting") logging.info("XLEN Value=%d" % rcs.XLEN) sys.exit("XLEN is not equal to 32, set it Accordingly!") if not(support_128b or support_64b) and not(('SV32' in stp_md_lst) or ('BARE' in stp_md_lst)): logging.critical("SATP mode is not supported for RV32G ISA") logging.info(stp_md_lst) sys.exit("Supported SATP mode is not provided") # TODO def setup_instr_distribution(self): pass def init_delegation(self): for i in self.mode_exp_lst: if i == self.mode_exp_lst[0]: continue self.m_mode_exception_delegation[i] = 0 self.s_mode_exception_delegation[i] = 0 for j in self.mode_intrpt_lst: if j == self.mode_intrpt_lst[0]: continue self.m_mode_interrupt_delegation[j] = 0 self.s_mode_interrupt_delegation[j] = 0 def pre_randomize(self): for x in rcs.supported_privileged_mode: if(x == "SUPERVISOR_MODE"): self.support_supervisor_mode = 1 def get_non_reserved_gpr(self): pass def post_randomize(self): self.reserved_regs.append(self.tp) self.reserved_regs.append(self.sp) self.reserved_regs.append(self.scratch_reg) self.min_stack_len_per_program = 2 * (rcs.XLEN / 8) logging.info("min_stack_len_per_program value = %d" % self.min_stack_len_per_program) self.check_setting() # to check the setting is legal # TODO, Need to change the logic once the constraints are up. if "USER_MODE" == self.init_privileged_mode: logging.info("mode=%s" % "USER_MODE") self.no_wfi = 1 def get_invalid_priv_lvl_csr(self): invalid_lvl = [] # Debug CSRs are inaccessible from all but Debug Mode # and we cannot boot into Debug Mode. invalid_lvl.append('D') # TODO Need to change the logic once the constraints are up. for mode in self.init_privileged_mode: if mode == "MACHINE_MODE": continue elif mode == 'SUPERVISOR_MODE': invalid_lvl.append('M') logging.info("supr_mode---") logging.debug(invalid_lvl) elif mode == 'USER_MODE': invalid_lvl.append('S') invalid_lvl.append('M') logging.info("usr_mode---") logging.debug(invalid_lvl) else: logging.critical("Unsupported initialization privilege mode") # implemented_csr from riscv_core_setting.py for x in rcs.implemented_csr: if x[0] in invalid_lvl: self.invalid_priv_mode_csrs.append(x) # This function calls all the above defined function which should # be called in init function as per SV logic.This function as to be # called after every instance of the gen_config handle def func_call_init(self): self.init_delegation() # self.setup_instr_distribution() # TODO self.get_invalid_priv_lvl_csr() def parse_args(): parse = argparse.ArgumentParser() parse.add_argument('--num_of_tests', help = 'num_of_tests', type = int, default = 1) parse.add_argument('--enable_page_table_exception', help = 'enable_page_table_exception', type = int, default = 0) parse.add_argument('--enable_interrupt', help = 'enable_interrupt', choices = [0, 1], type = int, default = 0) parse.add_argument('--enable_nested_interrupt', help = 'enable_nested_interrupt', choices = [0, 1], type = int, default = 0) parse.add_argument('--enable_timer_irq', help = 'enable_timer_irq', choices = [0, 1], type = int, default = 0) parse.add_argument('--num_of_sub_program', help = 'num_of_sub_program', type = int, default = 5) parse.add_argument('--instr_cnt', help = 'instr_cnt', type = int, default = 200) parse.add_argument('--tvec_alignment', help = 'tvec_alignment', type = int, default = 2) parse.add_argument('--no_ebreak', help = 'no_ebreak', choices = [0, 1], type = int, default = 1) parse.add_argument('--no_dret', help = 'no_dret', choices = [0, 1], type = int, default = 1) parse.add_argument('--no_wfi', help = 'no_wfi', choices = [0, 1], type = int, default = 1) parse.add_argument('--no_branch_jump', help = 'no_branch_jump', choices = [0, 1], type = int, default = 0) parse.add_argument('--no_load_store', help = 'no_load_store', choices = [0, 1], type = int, default = 0) parse.add_argument('--no_csr_instr', help = 'no_csr_instr', choices = [0, 1], type = int, default = 0) parse.add_argument('--fix_sp', help = 'fix_sp', choices = [0, 1], type = int, default = 0) parse.add_argument('--use_push_data_section', help = 'use_push_data_section', choices = [0, 1], type = int, default = 0) parse.add_argument('--enable_illegal_csr_instruction', help = 'enable_illegal_csr_instruction', choices = [0, 1], type = int, default = 0) parse.add_argument('--enable_access_invalid_csr_level', help = 'enable_access_invalid_csr_level', choices = [0, 1], type = int, default = 0) parse.add_argument('--enable_misaligned_instr', help = 'enable_misaligned_instr', choices = [0, 1], type = int, default = 0) parse.add_argument('--enable_dummy_csr_write', help = 'enable_dummy_csr_write', choices = [0, 1], type = int, default = 0) parse.add_argument('--allow_sfence_exception', help = 'allow_sfence_exception', choices = [0, 1], type = int, default = 0) parse.add_argument('--no_data_page', help = 'no_data_page', choices = [0, 1], type = int, default = 0) parse.add_argument('--no_directed_instr', help = 'no_directed_instr', choices = [0, 1], type = int, default = 0) parse.add_argument('--no_fence', help = 'no_fence', choices = [0, 1], type = int, default = 1) parse.add_argument('--no_delegation', help = 'no_delegation', choices = [0, 1], type = int, default = 1) parse.add_argument('--illegal_instr_ratio', help = 'illegal_instr_ratio', type = int, default = 0) parse.add_argument('--hint_instr_ratio', help = 'hint_instr_ratio', type = int, default = 0) parse.add_argument('--num_of_harts', help = 'num_of_harts', type = int, default = rcs.NUM_HARTS) parse.add_argument('--enable_unaligned_load_store', help = 'enable_unaligned_load_store', choices = [0, 1], type = int, default = 0) parse.add_argument('--force_m_delegation', help = 'force_m_delegation', choices = [0, 1], type = int, default = 0) parse.add_argument('--force_s_delegation', help = 'force_s_delegation', choices = [0, 1], type = int, default = 0) parse.add_argument('--require_signature_addr', help = 'require_signature_addr', choices = [0, 1], type = int, default = 0) parse.add_argument('--signature_addr', help = 'signature_addr', default = 0xdeadbeef) parse.add_argument('--disable_compressed_instr', help = 'disable_compressed_instr', choices = [0, 1], type = int, default = 0) parse.add_argument('--randomize_csr', help = 'randomize_csr', choices = [0, 1], type = int, default = 0) parse.add_argument('--gen_debug_section', help = 'gen_debug_section', choices = [0, 1], type = int, default = 0) parse.add_argument('--bare_program_mode', help = 'bare_program_mode', choices = [0, 1], type = int, default = 0) parse.add_argument('--num_debug_sub_program', help = 'num_debug_sub_program', type = int, default = 0) parse.add_argument('--enable_ebreak_in_debug_rom', help = 'enable_ebreak_in_debug_rom', choices = [0, 1], type = int, default = 0) parse.add_argument('--set_dcsr_ebreak', help = 'set_dcsr_ebreak', choices = [0, 1], type = int, default = 0) parse.add_argument('--enable_debug_single_step', help = 'enable_debug_single_step', choices = [0, 1], type = int, default = 0) parse.add_argument('--set_mstatus_tw', help = 'set_mstatus_tw', choices = [0, 1], type = int, default = 0) parse.add_argument('--set_mstatus_mprv', help = 'set_mstatus_mprv', choices = [0, 1], type = int, default = 0) parse.add_argument('--enable_floating_point', help = 'enable_floating_point', choices = [0, 1], type = int, default = 0) parse.add_argument('--enable_vector_extension', help = 'enable_vector_extension', choices = [0, 1], type = int, default = 0) parse.add_argument('--enable_b_extension', help = 'enable_b_extension', choices = [0, 1], type = int, default = 0) # TODO ''' cmdline_enum_processor #(b_ext_group_t)::get_array_values("+enable_bitmanip_groups=", enable_bitmanip_groups); if(inst.get_arg_value("+boot_mode=", boot_mode_opts)) begin `uvm_info(get_full_name(), $sformatf( "Got boot mode option - %0s", boot_mode_opts), UVM_LOW) case(boot_mode_opts) "m" : init_privileged_mode = MACHINE_MODE; "s" : init_privileged_mode = SUPERVISOR_MODE; "u" : init_privileged_mode = USER_MODE; default: `uvm_fatal(get_full_name(), $sformatf("Illegal boot mode option - %0s", boot_mode_opts)) endcase init_privileged_mode.rand_mode(0); addr_translaction_rnd_order_c.constraint_mode(0); end `uvm_info(`gfn, $sformatf("riscv_instr_pkg::supported_privileged_mode = %0d", riscv_instr_pkg::supported_privileged_mode.size()), UVM_LOW) void'(inst.get_arg_value("+asm_test_suffix=", asm_test_suffix)); // Directed march list from the runtime options, ex. RV32I, RV32M etc. cmdline_enum_processor #(riscv_instr_group_t)::get_array_values("+march=", march_isa); if (march_isa.size != 0) riscv_instr_pkg::supported_isa = march_isa; ''' args = parse.parse_args() return args args = parse_args() cfg = riscv_instr_gen_config(args)
py
1a4b5da8f77e641e9735d58b379b7e1eaea35e35
# Generated by Django 4.0.2 on 2022-02-28 12:26 from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): initial = True dependencies = [ ] operations = [ migrations.CreateModel( name='categories', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=46)), ], ), migrations.CreateModel( name='location', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=46)), ], ), migrations.CreateModel( name='gallery', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('image', models.ImageField(blank=True, null=True, upload_to='photos/')), ('name', models.CharField(max_length=46)), ('descripton', models.TextField(max_length=500)), ('time_uploaded', models.DateTimeField(auto_now_add=True, null=True)), ('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='gallery.categories')), ('location', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='gallery.location')), ], ), ]
py
1a4b5dbd9a3d41de4dbbc20a4152a019cd2e7649
from ui.widgets.sprite import LcarsWidget import pygame class LcarsBackground(LcarsWidget): def update(self, screen): screen.blit(self.image, self.rect) self.dirty = False def handleEvent(self, event, clock): pass class LcarsBackgroundImage(LcarsWidget): def __init__(self, image): self.image = pygame.image.load(image).convert() LcarsWidget.__init__(self, None, (0,0), None) def update(self, screen): screen.blit(self.image, self.rect) self.dirty = False def handleEvent(self, event, clock): pass class LcarsImage(LcarsWidget): def __init__(self, image, pos): self.image = pygame.image.load(image).convert() LcarsWidget.__init__(self, None, pos, None)
py
1a4b5e88b8a4eb5f22e81bd937141d0f35fa70d5
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models, migrations COUNTRY_REGION = [ ("Myanmar", "Asia"), ("Angola","Africa"), ("Cambodia","Asia"), ("Cayman Islands","Caribbean"), ("Dominica","Caribbean"), ("Greenland","Europe"), ("Honduras","Latin America"), ("Hong Kong","Asia"), ("Iraq","Middle East"), ("Jordan","Middle East"), ("Macao","Asia"), ("Papua New Guinea","Pacific"), ("Russia","Europe"), ("Rwanda","Africa"), ("Seychelles","Africa"), ("Timor-Leste","Asia"), ("Uzbekistan","Asia"), ] def get_region_map(CountryRegion): from django_countries import countries region_map = {} # Map new country code(s) to regions for country_region in COUNTRY_REGION: code = countries.by_name(country_region[0]) if code: if country_region[1] in region_map: region_map[country_region[1]].append(code) else: region_map[country_region[1]] = [code] return region_map def code(apps, schema_editor): CountryRegion = apps.get_model("forms", "CountryRegion") db_alias = schema_editor.connection.alias # Update countries regions CountryRegion.objects.using(db_alias).filter(country="CY").update(region="Europe") CountryRegion.objects.using(db_alias).filter(country="KZ").update(region="Asia") CountryRegion.objects.using(db_alias).filter(country="PR").update(region="Caribbean") CountryRegion.objects.using(db_alias).filter(country="VU").update(region="Pacific") # Create CountryRegion objects for supplied pairs region_map = get_region_map(CountryRegion) for region, country_code_list in region_map.items(): for country_code in country_code_list: CountryRegion.objects.using(db_alias).create( country=country_code, region=region ) def reverse_code(apps, schema_editor): CountryRegion = apps.get_model("forms", "CountryRegion") db_alias = schema_editor.connection.alias # Reverse Update regions CountryRegion.objects.using(db_alias).filter(country="CY").update(region="Middle East") CountryRegion.objects.using(db_alias).filter(country="KZ").update(region="Europe") # Delete CountryRegion objects for supplied pairs region_map = get_region_map(CountryRegion) for region, country_code_list in region_map.items(): for country_code in country_code_list: CountryRegion.objects.using(db_alias).filter( country=country_code, region=region ).delete() class Migration(migrations.Migration): dependencies = [ ("forms", "0058_add_deleted_attribute"), ] operations = [ migrations.RunPython(code, reverse_code), ]
py
1a4b5ea901afb59cf0bb23804563df782f0ea726
from sympy import * import sys sys.path.insert(1, '..') from rodrigues_R_utils import * px, py, pz = symbols('px py pz') sx, sy, sz = symbols('sx sy sz') tie_px, tie_py, tie_pz = symbols('tie_px tie_py tie_pz'); cols, rows = symbols('cols rows'); pi = symbols('pi') u_kp, v_kp = symbols('u_kp v_kp') position_symbols = [px, py, pz] rodrigues_symbols = [sx, sy, sz] tie_point_symbols = [tie_px, tie_py, tie_pz] all_symbols = position_symbols + rodrigues_symbols + tie_point_symbols RT_wc = matrix44FromRodrigues(px, py, pz, sx, sy, sz) r=RT_wc[:-1,:-1] t=Matrix([px, py, pz]).vec() pos_w=Matrix([tie_px, tie_py, tie_pz]).vec() bearing = r * pos_w + t; norm = sqrt(bearing[0]*bearing[0] + bearing[1]*bearing[1] + bearing[2]*bearing[2]) bearing=bearing/norm latitude=-asin(bearing[1]) longitude=atan2(bearing[0], bearing[2]) u=cols*(0.5 + longitude / (2.0 * pi)) v=rows*(0.5 - latitude/pi) u_delta = u_kp - u; v_delta = v_kp - v; obs_eq = Matrix([u_delta, v_delta]).vec() obs_eq_jacobian = obs_eq.jacobian(all_symbols) print(obs_eq) print(obs_eq_jacobian) with open("equirectangular_camera_colinearity_rodrigues_wc_jacobian.h",'w') as f_cpp: f_cpp.write("inline void observation_equation_equrectangular_camera_colinearity_rodrigues_wc(Eigen::Matrix<double, 2, 1> &delta, double rows, double cols, double pi, double px, double py, double pz, double sx, double sy, double sz, double tie_px, double tie_py, double tie_pz, double u_kp, double v_kp)\n") f_cpp.write("{") f_cpp.write("delta.coeffRef(0,0) = %s;\n"%(ccode(obs_eq[0,0]))) f_cpp.write("delta.coeffRef(1,0) = %s;\n"%(ccode(obs_eq[1,0]))) f_cpp.write("}") f_cpp.write("\n") f_cpp.write("inline void observation_equation_equrectangular_camera_colinearity_rodrigues_wc_jacobian(Eigen::Matrix<double, 2, 9, Eigen::RowMajor> &j, double rows, double cols, double pi, double px, double py, double pz, double sx, double sy, double sz, double tie_px, double tie_py, double tie_pz, double u_kp, double v_kp)\n") f_cpp.write("{") for i in range (2): for j in range (9): f_cpp.write("j.coeffRef(%d,%d) = %s;\n"%(i,j, ccode(obs_eq_jacobian[i,j]))) f_cpp.write("}")
py
1a4b5ee58cb72424234dc2bbbd6adc27538d349c
# Copyright 2014 PerfKitBenchmarker Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Module containing build tools installation and cleanup functions.""" from perfkitbenchmarker import os_types def YumInstall(vm): """Installs build tools on the VM.""" vm.InstallPackageGroup('Development Tools') def AptInstall(vm): """Installs build tools on the VM.""" vm.InstallPackages('build-essential git libtool autoconf automake') def _GetVersion(vm, pkg): """Get version of package.""" _, err = vm.RemoteCommand('{pkg} -v'.format(pkg=pkg), ignore_failure=True) return err def Reinstall(vm, version='4.7'): """Install specific version of gcc. Args: vm: VirtualMachine object. version: string. GCC version. """ # TODO(user): Make this work on yum based systems. if vm.BASE_OS_TYPE != os_types.DEBIAN: return for pkg in ('gcc', 'gfortran', 'g++'): version_string = _GetVersion(vm, pkg) if version in version_string: continue else: new_pkg = pkg + '-' + version vm.InstallPackages(new_pkg) vm.RemoteCommand('sudo rm /usr/bin/{pkg}'.format(pkg=pkg)) vm.RemoteCommand('sudo ln -s /usr/bin/{new_pkg} /usr/bin/{pkg}'.format( new_pkg=new_pkg, pkg=pkg))
py
1a4b6015c94f20e51df70afbbe8d7c323c033b9e
# Generated by Django 2.0.2 on 2018-08-31 19:40 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('people', '0011_change_the_contact_keys'), ] operations = [ migrations.AddField( model_name='personindexpage', name='introduction', field=models.TextField(blank=True), ), ]
py
1a4b603ecfea4624cfc7a67a8c5a011e0346cf01
import pyglet def convert(): from wand import image with image.Image(filename="31_21.png") as img: img.strip() img.flip() image.library.MagickSetOption(img.wand, b"dds:mipmaps", b"0") image.library.MagickSetOption(img.wand, b"dds:compression", b"none") img.save(filename="31_21.dds") def load(): pyglet.image.load("31_21.dds") if __name__ == '__main__': convert() load() """ Exception is Traceback (most recent call last): File "convert_and_open_dds.py", line 20, in <module> load() File "convert_and_open_dds.py", line 15, in load pyglet.image.load("31_21.dds") File "/<path to venv>/lib/python3.6/site-packages/pyglet/image/__init__.py", line 208, in load raise first_exception File "/<path to venv>/lib/python3.6/site-packages/pyglet/image/__init__.py", line 198, in load image = decoder.decode(file, filename) File "/<path to venv>/lib/python3.6/site-packages/pyglet/image/codecs/gdkpixbuf2.py", line 311, in decode return loader.get_pixbuf().to_image() File "/<path to venv>/lib/python3.6/site-packages/pyglet/image/codecs/gdkpixbuf2.py", line 137, in get_pixbuf self._finish_load() File "/<path to venv>/lib/python3.6/site-packages/pyglet/image/codecs/gdkpixbuf2.py", line 123, in _finish_load raise ImageDecodeException(_gerror_to_string(error)) pyglet.image.codecs.ImageDecodeException: GdkPixBuf Error: domain[66], code[3]: <ctypes.LP_c_char object at 0x7f824b832950> """ """ identify -verbose 31_21.dds | grep -i "compression" Compression: None """
py
1a4b6140606323f97b28e6c4d69a03bad222b6be
# # Lincense: Academic Free License (AFL) v3.0 # """ Try to import accelerated versions of various NumPy operations.. """ import numpy as np #============================================================================= # Pull default implementations backend = "numpy" sin = np.sin cos = np.cos exp = np.exp log = np.log log2 = np.log2 log10 = np.log10 #============================================================================= # Try to import AMD Core Math Library try: import pyacml backend = "pyacml" sin = pyacml.sin cos = pyacml.cos exp = pyacml.exp log = pyacml.log log2 = pyacml.log2 log10 = pyacml.log10 except ImportError as e: pass
py
1a4b614453d66484d9b45222b885cf8467c429a6
# Generated by gen_timm_models.py import torch import timm.models.vision_transformer import time from ...util.model import BenchmarkModel from torchbenchmark.tasks import COMPUTER_VISION from .config import TimmConfig import torch.cuda.nvtx as nvtx class Model(BenchmarkModel): task = COMPUTER_VISION.GENERATION def __init__(self, device=None, jit=False, variant='vit_small_patch16_224', precision='float32', batchsize=1): super().__init__() self.device = device self.jit = jit self.model = timm.create_model(variant, pretrained=False, scriptable=True) self.cfg = TimmConfig(model = self.model, device = device, precision = precision) self.model.to( device=self.device, dtype=self.cfg.model_dtype ) if device == 'cuda': torch.cuda.empty_cache() if jit: self.model = torch.jit.script(self.model) assert isinstance(self.model, torch.jit.ScriptModule) self.batchsize=batchsize self.batch = self.cfg.infer_example_inputs if batchsize == 1 else self.cfg.infer_example_inputs.repeat(batchsize,1,1,1) def _gen_target(self, batch_size): return torch.empty( (batch_size,) + self.cfg.target_shape, device=self.device, dtype=torch.long).random_(self.cfg.num_classes) def _step_train(self): nvtx.range_push('zeroing optimizer grad') self.cfg.optimizer.zero_grad() nvtx.range_pop() nvtx.range_push('forward pass') output = self.model(self.cfg.example_inputs) nvtx.range_pop() if isinstance(output, tuple): output = output[0] nvtx.range_push('__gen__target') target = self._gen_target(output.shape[0]) nvtx.range_pop() nvtx.range_push('loss') # self.cfg.loss(output, target).backward() lossy = self.cfg.loss(output, target) print("Loss:",lossy) lossy.backward() nvtx.range_pop() nvtx.range_push('step') self.cfg.optimizer.step() nvtx.range_pop() def _step_eval(self, precision): nvtx.range_push('eval') if precision=='fp16': output = self.model(self.batch.half()) elif precision=='bfloat16': output = self.model(self.batch.bfloat16()) else: output = self.model(self.batch) nvtx.range_pop() def get_module(self): return self.model, (self.cfg.example_inputs,) def train(self, niter=1): self.model.train() graphs=True if graphs: self.model.to(memory_format=torch.channels_last) niter = 8 s = torch.cuda.Stream() torch.cuda.synchronize() with torch.cuda.stream(s): nvtx.range_push('warming up') for _ in range(5): self._step_train() nvtx.range_pop() torch.cuda.empty_cache() g = torch.cuda._Graph() torch.cuda.synchronize() nvtx.range_push('capturing graph') g.capture_begin() self._step_train() g.capture_end() nvtx.range_pop() torch.cuda.synchronize() nvtx.range_push('replaying') for _ in range(100): g.replay() torch.cuda.synchronize() nvtx.range_pop() else: for _ in range(niter): self._step_train() # TODO: use pretrained model weights, assuming the pretrained model is in .data/ dir def eval(self, niter=1, precision='fp16', graphs=False, bench=False): niter = 8 with torch.autograd.profiler.emit_nvtx(record_shapes=True): self.model.eval() torch.backends.cudnn.benchmark = bench with torch.no_grad(): if precision == 'fp16': self.model = self.model.half() elif precision == 'bfloat16': self.model=self.model.bfloat16() if graphs: s = torch.cuda.Stream() torch.cuda.synchronize() with torch.cuda.stream(s): nvtx.range_push('warming up') print('warming up') for _ in range(5): self._step_eval(precision) nvtx.range_pop() torch.cuda.empty_cache() g = torch.cuda._Graph() torch.cuda.synchronize() nvtx.range_push('capturing graph') print('capturing graph') g.capture_begin() self._step_eval(precision) g.capture_end() nvtx.range_pop() torch.cuda.synchronize() nvtx.range_push('replaying') print('replaying') since=time.time() for _ in range(100): g.replay() torch.cuda.synchronize() print("Average Replay Time for VT:",round(1000.0 * (time.time()-since)/100.0,5),"ms") nvtx.range_pop() else: torch.cuda.synchronize() for i in range(5): self._step_eval(precision) torch.cuda.synchronize() since=time.time() for i in range(100): self._step_eval(precision) torch.cuda.synchronize() print("Average Replay Time for VT:",round(1000.0 * (time.time()-since)/100.0,5),"ms") if __name__ == "__main__": for device in ['cpu', 'cuda']: for jit in [False, True]: print("Test config: device %s, JIT %s" % (device, jit)) m = Model(device=device, jit=jit) m, example_inputs = m.get_module() m(example_inputs) m.train() m.eval()
py
1a4b637e58adbe473ad6e1101fa3c29da9e9f6f0
#!/usr/bin/env python3 import app_api import argparse import random import sys import os import time import subprocess import pickle from datetime import datetime, timezone, timedelta import json from signal import SIGTERM from app_api import CH_CONF_PATH as CONFIG_PATH def init_tel(logger): name = 'initial_tel_data' args = '''["-f","tel-data/CLYDE_EPS.csv", "/challenge/mission-apps/tel-data/NOVATEL_GPS.csv", "/challenge/mission-apps/tel-data/REACTION_WHEEL.csv"]''' start_app_request = ''' mutation{ startApp(name: "%s", config: "%s", args: %s){ success, errors, pid } } ''' % (name, CONFIG_PATH, args) app_status_req = '{appStatus(name: "initial_tel_data"){pid}}' app_unin_req = 'mutation{uninstall(name: "initial_tel_data"){success, errors}}' try: response = SERVICES.query(service='app-service', query=start_app_request) # #print(response) pid = response['startApp']['pid'] # #print(pid) while((SERVICES.query(service='app-service', query=app_status_req))['appStatus'][0]['pid'] == pid): #print(f'{logger} info: Waiting for initalization to finish...') time.sleep(1) response = SERVICES.query(service='app-service', query=app_unin_req) # #print(response) except Exception as e: error_msg = "Initializing Telemetry data failed: " + str(e) + "" print(error_msg) return def find_kill_services(logger): # Kill all kubos services. pid_list = [] list_cmds = [] ports = ['8000', '8010', '8020', '8030', '8110', '8120'] for port in ports: list_cmds.append(['lsof', '-t','-i:%s' % port]) for cmd in list_cmds: start_sch = subprocess.Popen(cmd, stdout=subprocess.PIPE) ppid = start_sch.stdout.read().decode('ascii').split('\n') for pid in ppid: if pid != '': pid_list.append(pid) for pid in pid_list: #print("Killing %s" % pid) os.kill(int(pid), SIGTERM) return def delete_by_force(logger): sch_path = '/challenge/target/release/schedules/stage_one/' if os.path.exists(sch_path): f_list = os.listdir(sch_path) for f in f_list: os.unlink(sch_path + f.split('/')[-1]) # in case of malicious file names. os.rmdir(sch_path) return def delete_all_info(logger): requests = [] requestSafeMode = 'mutation{safeMode{success, errors}}' requestRemoveMode = 'mutation{removeMode(name:"stage_one"){success, errors}}' mapps = ['critical_tel_check', 'initial_tel_data', 'update_tel', 'housekeeping', 'request_flag_telemetry', 'sunpoint', 'enable_groundlink', 'groundpoint', 'sunpoint'] for mapp in mapps: requestUninstall = 'mutation{uninstall(name: "%s"){success, errors}}' % mapp requests.append(requestUninstall) app_count = 0 for request in requests: try: response = SERVICES.query(service='app-service', query=request) #print(f"Deleted %s: {response['uninstall']['success']} | {response['uninstall']['errors']}" % mapps[app_count]) # DEBUG TAKE OUT except Exception as e: error_msg = "Deleting app %s failed: " % request + str(e) + "" print(error_msg) app_count += 1 try: response = SERVICES.query(service='scheduler-service', query=requestSafeMode) #print("Turned on safe mode.") time.sleep(1) response = SERVICES.query(service='scheduler-service', query=requestRemoveMode) #print("Removed mode stage_one.") except Exception as e: error_msg = "Deleting all schedules failed: " + str(e) + "" print(error_msg) delete_by_force(logger) find_kill_services(logger) return def activate_mode(logger, mode_name): request = ''' mutation { activateMode(name: "%s") { success errors } } ''' % mode_name #print(f"{logger} info: Activating mode %s." % mode_name) # DEBUG try: response = SERVICES.query(service='scheduler-service', query=request) # #print(response) except Exception as e: error_msg = "Starting mode %s failed: " % mode_name + str(e) + "" print(error_msg) # DEBUG return def create_mode(logger, mode_name): #Create the stage 1 mode request = """ mutation { createMode(name: "%s") { success errors } }""" % mode_name #print(f"{logger} info: Creating mode %s." % mode_name) # DEBUG try: response = SERVICES.query(service='scheduler-service', query=request) # #print(response) except Exception as e: error_msg = "Creating mode %s failed: " % mode_name + str(e) + "" print(error_msg) # DEBUG return def create_mode_schedule(logger, mode_name): sch_name = "nominal-op" schedule_path = os.getcwd() + f"/../{mode_name}.json" request = ''' mutation { importTaskList(name: "%s", path: "%s", mode: "%s") { success errors } } ''' % (sch_name, schedule_path, mode_name) #print(f"{logger} info: Creating schedule %s for mode %s." % (sch_name, mode_name)) try: response = SERVICES.query(service='scheduler-service', query=request) #print(response) except Exception as e: error_msg = "Importing schedule %s in mode %s failed: " % (sch_name, mode_name) + str(e) + "" print(error_msg) return def register_all_apps(logger): cwd = os.getcwd() # Stage one apps to register init_tel_app = cwd + "/../initial_tel_data" crit_check_app = cwd + "/../critical_tel_check" eps_tel_app = cwd + "/../update_tel" # Stage two apps to register p_apps_app = cwd + "/../../stage_two/print_all_apps" req_flag_app = cwd + "/../../stage_two/req_flag_base" en_down_app = cwd + "/../../stage_two/enable_downlink" gndpoint_app = cwd + "/../../stage_two/groundpoint" sunpoint_app = cwd + "/../../stage_two/sunpoint" low_power_app = cwd + "/../../stage_two/low_power" act_trans_app = cwd + "/../../stage_two/activate_tranmsission_mode" registrations = [init_tel_app, crit_check_app, eps_tel_app, p_apps_app, req_flag_app, en_down_app, gndpoint_app, sunpoint_app, low_power_app, act_trans_app] for mapp in registrations: #print(f"{logger} info: Registering, %s" % mapp.split('/')[-1] + " app.") register_app(logger, mapp) return def register_app(logger, app_path): request = """ mutation { register(path: "%s") { success, errors, entry { app { name executable } } } } """ % app_path try: response = SERVICES.query(service='app-service', query=request) ##print(response) except Exception as e: error_msg = "Registering %s failed: " % app_path + str(e) + "" print(error_msg) def store_latest_json(logger): current_tel = "" with open('/challenge/mission-apps/pkls/current_tel.json', 'w') as js_file: json.dump(current_tel, js_file) return def store_low_power_json(logger, low_power_time): with open('../low_power.json', 'r') as js_file: low_power_json = json.load(js_file) low_power_json['tasks'][1]['time'] = low_power_time with open('../low_power.json', 'w') as js_file: json.dump(low_power_json, js_file) def store_transmission_json(logger): SEED = int(os.getenv('SEED', 0)) random.seed(SEED) utc_dt = datetime.now(tz=timezone.utc) base_time = utc_dt + timedelta( seconds= ( (60.0) * (50.0 + random.randrange(0,10)) ) ) time_offsets = [0.0, 20.0, 25.0, 30.0] with open('../transmission.json', 'r') as js_file: transmission_json = json.load(js_file) for app, offset in zip(transmission_json['tasks'], time_offsets): app['time'] = (base_time + timedelta(seconds=offset)).strftime("%Y-%m-%d %H:%M:%S") # print("DEBUG: " + str(transmission_json)) with open('../transmission.json', 'w') as js_file: json.dump(transmission_json, js_file) low_power_time_str = (base_time + timedelta(seconds=-10)).strftime("%Y-%m-%d %H:%M:%S") #low_power_time_obj = (base_time + timedelta(seconds=-10)) with open('/challenge/mission-apps/pkls/transmission_time.pkl', 'wb') as pkl_file: pickle.dump(low_power_time_str, pkl_file) return low_power_time_str def main(): logger = "start_stage_one " parser = argparse.ArgumentParser() parser.add_argument('--config', '-c', nargs=1) parser.add_argument('--delete', '-d', help='Deletes all app and service data', action='store_true') args = parser.parse_args() if args.config is not None: global SERVICES SERVICES = app_api.Services(args.config[0]) else: SERVICES = app_api.Services(CONFIG_PATH) #print(f"{logger} info: Begining stage_one") if args.delete: #print(f"{logger} info: Deleting app and service data.") delete_all_info(logger) else: register_all_apps(logger) init_tel(logger) create_mode(logger, "transmission") low_power_time_str = store_transmission_json(logger) create_mode_schedule(logger, "transmission") create_mode(logger, "low_power") store_low_power_json(logger, low_power_time_str) create_mode_schedule(logger, "low_power") create_mode(logger, "station-keeping") create_mode_schedule(logger, "station-keeping") activate_mode(logger, "station-keeping") store_latest_json(logger) print("\n** Welcome to spaceDB **\n" + '-'* 25) if __name__ == "__main__": main()
py
1a4b641ec00f054a92b2864866f0d7e7d6d53ec4
# -*- coding: utf-8 -*- """ Tencent is pleased to support the open source community by making BK-BASE 蓝鲸基础平台 available. Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. BK-BASE 蓝鲸基础平台 is licensed under the MIT License. License for BK-BASE 蓝鲸基础平台: -------------------------------------------------------------------- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from __future__ import absolute_import, print_function, unicode_literals import math from lifecycle.metrics.app_code import get_app_code_count from lifecycle.metrics.cost import ( get_cost_score, get_hdfs_capacity, get_tspider_capacity, ) from lifecycle.metrics.heat import ( get_heat_score, get_query_count, get_rd_query_list_group, ) from lifecycle.metrics.importance import ( biz_data_correct, get_asset_value_score, get_biz_info, get_dataset_info, get_heat_range_importance_score, get_project_score, ) from lifecycle.metrics.range import get_node_info, get_range_info, get_range_score # 数据敏感度 SENSITIVITY_DICT = {"public": 0, "private": 1, "confidential": 2, "topsecret": 3} # 存储评分阈值 THRESHOLD = 20.41 class Entity(object): def _get_data(self): return {} def __getattr__(self, item): """ get the attribute of a object when the attribute can not be gotten directly :param item: :return: """ if item in self._get_data(): return self._get_data()[item] super(Entity, self).__getattr__(item) class Project(Entity): def __init__(self, project_id): self.project_id = project_id self.get_project_score() def get_project_score(self): """ get project score and project active :return: """ self.project_score, self.active = get_project_score(self.project_id) class BkBiz(Entity): def __init__(self, biz_config): self.biz_config = biz_config self.biz_data_correct() def _get_data(self): return self.biz_config def biz_data_correct(self): """ correct biz attributes :return: """ if self.biz_config: ( self.bip_grade_id, self.oper_state, self.app_important_level, self.oper_state_name, self.bip_grade_name, self.app_important_level_name, ) = biz_data_correct(self.biz_config) else: self.bip_grade_id = None self.oper_state = None self.app_important_level = None self.oper_state_name = None self.bip_grade_name = None self.app_important_level_name = None @property def is_bip(self): """ is_bip :return: """ return True if self.biz_config.get("BizV1.IsBip") == "是" else False @property def biz_score(self): """ biz_score :return: """ if ( self.oper_state is None and self.bip_grade_id is None and self.app_important_level is None ): return 0 return ( self.oper_state + self.bip_grade_id + self.app_important_level + 1 if self.is_bip is True else self.oper_state + self.bip_grade_id + self.app_important_level ) class Range(Entity): def __init__(self, dataset_id, dataset_type): self.dataset_id = dataset_id self.dataset_type = dataset_type self.biz_count = 1 self.project_count = 1 self.weighted_node_count = 0.0 self.node_count_list = "[]" self.node_count = 0 self.depth = 0 self.range_score = 0.0 self.normalized_range_score = 13.1 self.app_code_count = 0 self.range_related_dict = self.get_range_info() self.set_range_dict() def get_range_info(self): return get_range_info(self.dataset_id, self.dataset_type) def get_biz_count(self): """ get the count of bizs which apply the dataset if the dataset does not have lineage, biz_count=1 (the biz of itslef) :return: """ biz_list = self.range_related_dict.get("data", {}).get("bk_biz_id", []) return len(biz_list[0].get("@groupby")) if biz_list else 1 def get_project_count(self): """ get the count of projects which apply the dataset if the dataset does not have lineage, project_count=1 (the project of itslef) :return: """ project_list = self.range_related_dict.get("data", {}).get("project_count", []) return ( project_list[0].get("count") if project_list and project_list[0].get("count") > 0 else 1 ) def get_node_info(self): """ get the successor node info :return: """ return get_node_info(self.dataset_id, self.dataset_type) def get_range_score(self): """ get range_score & normalized_range_score :return: """ return get_range_score( self.weighted_node_count, self.biz_count, self.project_count ) def get_app_code_count(self): """ get the count of apps which query the dataset :return: """ return get_app_code_count(self.dataset_id) def set_range_dict(self): if self.range_related_dict: self.biz_count = self.get_biz_count() self.project_count = self.get_project_count() ( self.weighted_node_count, self.node_count_list, self.node_count, self.depth, ) = self.get_node_info() self.range_score, self.normalized_range_score = self.get_range_score() self.app_code_count = self.get_app_code_count() class Heat(Entity): def __init__(self, heat_config): self.heat_config = heat_config self.queue_service_count = 0 self.heat_score = 0.0 def _get_data(self): return self.heat_config def set_heat_score(self): self.heat_score = get_heat_score( self.heat_config["dataset_id"], self.heat_config["dataset_type"], self.query_count, ) class StorageCapacity(Entity): def __init__(self, dataset_id, dataset_type): self.dataset_id = dataset_id self.dataset_type = dataset_type self.set_hdfs_capacity() self.set_tspider_capacity() self.set_total_capacity() self.set_log_capacity() self.set_capacity_score(THRESHOLD) def set_hdfs_capacity(self): if self.dataset_type == "result_table": self.hdfs_capacity = get_hdfs_capacity(self.dataset_id) else: self.hdfs_capacity = 0 def set_tspider_capacity(self): if self.dataset_type == "result_table": self.tspider_capacity = get_tspider_capacity(self.dataset_id) else: self.tspider_capacity = 0 def set_total_capacity(self): """ get total capacity (hdfs_capacity + tspider_capacity) :return: """ self.total_capacity = self.hdfs_capacity + self.tspider_capacity def set_log_capacity(self): """ get log capacity :return: """ if self.total_capacity: self.log_capacity = math.log(self.total_capacity) else: self.log_capacity = -1 def set_capacity_score(self, threshold): """ get capacity_score :return: """ if self.log_capacity and self.log_capacity >= 0: self.capacity_score = ( 99.99 if self.log_capacity / float(threshold) * 100 > 99.99 else round(self.log_capacity / float(threshold) * 100, 2) ) else: self.capacity_score = -1 class DataSet(Entity): def __init__(self, dataset_config): self.dataset_config = dataset_config self.heat_score = 0 self.normalized_range_score = 0 self.importance_score = 0 self.asset_value_score = 0 self.query_count = 0 def _get_data(self): return self.dataset_config def set_dataset_info(self): """ get the dataset attributes :return: """ ( self.biz_id, self.project_id, self.sensitivity, self.generate_type, ) = get_dataset_info(self.data_set_id, self.data_set_type) def get_storage_capacity(self): return StorageCapacity( self.dataset_config["data_set_id"], self.dataset_config["data_set_type"] ) @property def storage_capacity(self): """ storage_capacity stucture :return: """ if not hasattr(self, "_storage_capacity"): self._storage_capacity = self.get_storage_capacity() return self._storage_capacity def get_biz(self): """ get the biz structure :return: """ biz_dict = get_biz_info(self.biz_id) return BkBiz(biz_dict) @property def biz(self): """ biz structure :return: """ if not hasattr(self, "_biz"): self._biz = self.get_biz() return self._biz def get_range(self): """ get the range structure :return: """ return Range( self.dataset_config["data_set_id"], self.dataset_config["data_set_type"] ) @property def range(self): """ range structure :return: """ if not hasattr(self, "_range"): self._range = self.get_range() return self._range def get_heat(self): """ get the heat structure :return: """ return Heat( { "dataset_id": self.dataset_config["data_set_id"], "dataset_type": self.dataset_config["data_set_type"], "query_count": self.query_count, } ) @property def heat(self): """ heat structure :return: """ if not hasattr(self, "_heat"): self._heat = self.get_heat() return self._heat def get_project(self): """ get the project structure :return: """ return Project(self.project_id) @property def project(self): """ project structure :return: """ if not hasattr(self, "_project"): self._project = self.get_project() return self._project @property def dataset_score(self): """ dataset_score :return: """ return ( SENSITIVITY_DICT.get(self.sensitivity, 0) + 1 if self.generate_type == "user" else SENSITIVITY_DICT.get(self.sensitivity, 0) ) @property def id(self): """ id :return: """ return "{}_{}".format(self.data_set_id, self.data_set_type) def get_importance_score(self, norm_score=18): """ get importance_score :param norm_score: normalized_score :return: """ importance_score = round( (self.dataset_score + self.biz.biz_score + self.project.project_score) / float(norm_score) * 100, 2, ) importance_score = importance_score if importance_score <= 99.99 else 99.99 self.importance_score = importance_score return importance_score def set_heat_range_importance_score(self): """ get heat_score & range_score & importance_score :return: """ ( self.heat_score, self.normalized_range_score, self.importance_score, ) = get_heat_range_importance_score(self.id) def get_importance_dict(self, norm_score=18): """ get all properties related to importance :param norm_score: normalized_score :return: """ self.set_dataset_info() if self.biz_id and self.sensitivity and self.generate_type: return { "id": self.id, "data_set_id": self.data_set_id, "data_set_type": self.data_set_type, "dataset_score": self.dataset_score, "biz_score": self.biz.biz_score, "is_bip": self.biz.is_bip, "oper_state_name": self.biz.oper_state_name, "oper_state": self.biz.oper_state, "bip_grade_name": self.biz.bip_grade_name, "bip_grade_id": self.biz.bip_grade_id, "app_important_level_name": self.biz.app_important_level_name, "app_important_level": self.biz.app_important_level, "project_score": self.project.project_score, "active": self.project.active, "importance_score": self.get_importance_score(norm_score), } else: return {} def get_asset_value_score(self): self.asset_value_score = round( (self.importance_score + self.heat_score + self.normalized_range_score) / 3.0, 2, ) @property def target_id(self): return self.dataset_config["data_set_id"] @property def target_type(self): return ( "access_raw_data" if self.dataset_config["data_set_type"] == "raw_data" else self.dataset_config["data_set_type"] ) def get_asset_value_dict(self): """ get all properties related to asset_value :return: """ self.set_heat_range_importance_score() if ( self.heat_score is not None and self.normalized_range_score is not None and self.importance_score is not None ): self.get_asset_value_score() return { "id": self.id, "data_set_id": self.data_set_id, "data_set_type": self.data_set_type, "range_id": self.id, "normalized_range_score": self.normalized_range_score, "heat_id": self.id, "heat_score": self.heat_score, "importance_id": self.id, "importance_score": self.importance_score, "asset_value_score": self.asset_value_score, "target_id": self.target_id, "target_type": self.target_type, } else: return {} def get_storage_capacity_dict(self): """ get all properties related to storage_capacity :return: """ return { "id": self.id, "hdfs_capacity": self.storage_capacity.hdfs_capacity, "tspider_capacity": self.storage_capacity.tspider_capacity, "total_capacity": self.storage_capacity.total_capacity, "log_capacity": self.storage_capacity.log_capacity, "capacity_score": self.storage_capacity.capacity_score, } def get_cost_dict(self): """ get all properties related to cost :return: """ return { "id": self.id, "target_id": self.target_id, "target_type": self.target_type, "capacity_id": self.id, "capacity_score": self.storage_capacity.capacity_score, } def set_cost_score(self): """ get cost_score :return: """ self.cost_score = get_cost_score(self.id) def set_asset_value_score_via_erp(self): """ get asset_value_score :return: """ self.asset_value_score = get_asset_value_score(self.id) @property def assetvalue_to_cost(self): if self.cost_score and self.cost_score != -1: return ( round(self.asset_value_score / float(self.cost_score), 2) if self.cost_score >= 0 else -1.0 ) else: return -1.0 def get_lifecycle_dict(self): """ get all properties related to lifecycle :return: """ self.set_asset_value_score_via_erp() self.set_cost_score() if self.asset_value_score is not None and self.cost_score is not None: return { "id": self.id, "data_set_id": self.data_set_id, "data_set_type": self.data_set_type, "target_id": self.target_id, "target_type": self.target_type, "range_id": self.id, "heat_id": self.id, "importance_id": self.id, "asset_value_id": self.id, "cost_id": self.id, "assetvalue_to_cost": self.assetvalue_to_cost, } else: return {} def get_range_dict(self): """ get all properties related to range :return: """ return { "id": self.id, "data_set_id": self.data_set_id, "data_set_type": self.data_set_type, "biz_count": self.range.biz_count, "project_count": self.range.project_count, "depth": self.range.depth, "node_count_list": self.range.node_count_list, "node_count": self.range.node_count, "weighted_node_count": self.range.weighted_node_count, "app_code_count": self.range.app_code_count, "range_score": self.range.range_score, "normalized_range_score": self.range.normalized_range_score, } def set_query_count(self): self.query_count = get_query_count( self.data_set_id, self.data_set_type, self.query_dataset_dict ) def get_heat_dict(self): """ get all properties related to heat :return: """ self.set_query_count() self.heat.set_heat_score() return { "data_set_id": self.data_set_id, "data_set_type": self.data_set_type, "id": self.id, "query_count": self.heat.query_count, "queue_service_count": self.heat.queue_service_count, "heat_score": self.heat.heat_score, } def get_day_query_count_dict(self, query_config_dict): """ get the query_count_list of dataset per day :return: """ heat = self.get_heat() heat.set_heat_score() metric_dict = { "queue_service_count": heat.queue_service_count, "app_query_count": query_config_dict["app_query_count"], "day_query_count": query_config_dict["day_query_count"], "query_count": heat.query_count, "statistics_timestamp": query_config_dict["timestamp"], "heat_score": heat.heat_score, } dimension_dict = { "app_code": query_config_dict["app_code"], "statistics_time": query_config_dict["time_str"], } return metric_dict, dimension_dict def get_day_query_count_list(self): """ get the query_count_list of dataset per day, the query_count of rd is gotten by the clean rts of rd :return: """ day_query_count_list = [] if ( self.data_set_type == "result_table" and self.data_set_id in self.day_query_rt_list ): for each_day in self.day_query_rt_dict[self.data_set_id]["query_list"]: self.query_count = self.day_query_rt_dict[self.data_set_id][ "query_count" ] metric_dict, dimension_dict = self.get_day_query_count_dict(each_day) day_query_count_list.append( {"metric_dict": metric_dict, "dimension_dict": dimension_dict} ) elif self.data_set_type == "raw_data": rd_query_list_group = get_rd_query_list_group( self.data_set_id, self.day_query_rt_list, self.day_query_rt_dict ) for key, group in rd_query_list_group: sum_query_count = 0 for g in group: sum_query_count += g.get("day_query_count") g["app_query_count"] = 0 g["day_query_count"] = sum_query_count self.query_count = 0 metric_dict, dimension_dict = self.get_day_query_count_dict(g) day_query_count_list.append( {"metric_dict": metric_dict, "dimension_dict": dimension_dict} ) return day_query_count_list
py
1a4b6458ba95375ad37ff7210545fa85b54fe36e
# -*- coding: utf-8 -*- from smore import swagger def schema_definition_helper(name, schema, **kwargs): """Definition helper that allows using a marshmallow :class:`Schema <marshmallow.Schema>` to provide Swagger metadata. :param type schema: A marshmallow Schema class. """ return swagger.schema2jsonschema(schema) def setup(spec): spec.register_definition_helper(schema_definition_helper)
py
1a4b65948f8bc872876a9f949c221daceccb91af
import json import requests from .constants import HTTP_STATUS_CODE, ERROR_CODE, URL from .errors import (BadRequestError, GatewayError, ServerError) from . import resources from types import ModuleType def capitalize_camel_case(string): return "".join(map(str.capitalize, string.split('_'))) # Create a dict of resource classes RESOURCE_CLASSES = {} for name, module in resources.__dict__.items(): if isinstance(module, ModuleType) and \ capitalize_camel_case(name) in module.__dict__: RESOURCE_CLASSES[name] = module.__dict__[capitalize_camel_case(name)] class Client: """TapPayment client class""" DEFAULTS = { 'base_url': URL.BASE_URL, } def __init__(self, session=None, api_token=None, **options): """ Initialize a Client object with session, optional auth handler, and options """ self.session = session or requests.Session() self.api_token = api_token self.auth = None self.base_url = self.DEFAULTS['base_url'] # intializes each resource # injecting this client object into the constructor for name, Klass in RESOURCE_CLASSES.items(): setattr(self, name, Klass(self)) def _update_header(self, options): token_header = {'Authorization': 'Bearer ' + self.api_token } if 'headers' not in options: options['headers'] = {} options['headers'].update({'Content-type': 'application/json'}) options['headers'].update(token_header) return options def request(self, method, path, **options): """ Dispatches a request to the TapPayment HTTP API """ options = self._update_header(options) url = "{}{}".format(self.base_url, path) print(url) response = getattr(self.session, method)(url, auth=self.auth, **options) print(response.json()) if ((response.status_code >= HTTP_STATUS_CODE.OK) and (response.status_code < HTTP_STATUS_CODE.REDIRECT)): return response.json() else: msg = "" code = "" json_response = response.json() if 'error' in json_response: if 'description' in json_response['error']: msg = json_response['error']['description'] if 'code' in json_response['error']: code = str(json_response['error']['code']) if str.upper(code) == ERROR_CODE.BAD_REQUEST_ERROR: raise BadRequestError(msg) elif str.upper(code) == ERROR_CODE.GATEWAY_ERROR: raise GatewayError(msg) elif str.upper(code) == ERROR_CODE.SERVER_ERROR: raise ServerError(msg) else: raise ServerError(msg) def get(self, path, params, **options): """ Parses GET request options and dispatches a request """ return self.request('get', path, params=params, **options) def post(self, path, data, **options): """ Parses POST request options and dispatches a request """ data, options = self._update_request(data, options) return self.request('post', path, data=data, **options) def patch(self, path, data, **options): """ Parses PATCH request options and dispatches a request """ data, options = self._update_request(data, options) return self.request('patch', path, data=data, **options) def delete(self, path, data, **options): """ Parses DELETE request options and dispatches a request """ data, options = self._update_request(data, options) return self.request('delete', path, data=data, **options) def put(self, path, data, **options): """ Parses PUT request options and dispatches a request """ data, options = self._update_request(data, options) return self.request('put', path, data=data, **options) def _update_request(self, data, options): """ Updates The resource data and header options """ data = json.dumps(data) token_header = {'Authorization': 'Bearer ' + self.api_token } if 'headers' not in options: options['headers'] = {} options['headers'].update({'Content-type': 'application/json'}) options['headers'].update(token_header) return data, options
py
1a4b6646dd0bd8272706d826b68e2b4dc428ef28
#!/usr/bin/env python # # Copyright 2009 Facebook # # 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. """Implementations of various third-party authentication schemes. All the classes in this file are class Mixins designed to be used with web.py RequestHandler classes. The primary methods for each service are authenticate_redirect(), authorize_redirect(), and get_authenticated_user(). The former should be called to redirect the user to, e.g., the OpenID authentication page on the third party service, and the latter should be called upon return to get the user data from the data returned by the third party service. They all take slightly different arguments due to the fact all these services implement authentication and authorization slightly differently. See the individual service classes below for complete documentation. Example usage for Google OpenID:: class GoogleHandler(tornado.web.RequestHandler, tornado.auth.GoogleMixin): @tornado.web.asynchronous def get(self): if self.get_argument("openid.mode", None): self.get_authenticated_user(self.async_callback(self._on_auth)) return self.authenticate_redirect() def _on_auth(self, user): if not user: raise tornado.web.HTTPError(500, "Google auth failed") # Save the user with, e.g., set_secure_cookie() """ from __future__ import absolute_import, division, with_statement import base64 import binascii import hashlib import hmac import logging import time import urllib import urlparse import uuid from tornado import httpclient from tornado import escape from tornado.httputil import url_concat from tornado.util import bytes_type, b class OpenIdMixin(object): """Abstract implementation of OpenID and Attribute Exchange. See GoogleMixin below for example implementations. """ def authenticate_redirect(self, callback_uri=None, ax_attrs=["name", "email", "language", "username"]): """Returns the authentication URL for this service. After authentication, the service will redirect back to the given callback URI. We request the given attributes for the authenticated user by default (name, email, language, and username). If you don't need all those attributes for your app, you can request fewer with the ax_attrs keyword argument. """ callback_uri = callback_uri or self.request.uri args = self._openid_args(callback_uri, ax_attrs=ax_attrs) self.redirect(self._OPENID_ENDPOINT + "?" + urllib.urlencode(args)) def get_authenticated_user(self, callback, http_client=None): """Fetches the authenticated user data upon redirect. This method should be called by the handler that receives the redirect from the authenticate_redirect() or authorize_redirect() methods. """ # Verify the OpenID response via direct request to the OP args = dict((k, v[-1]) for k, v in self.request.arguments.iteritems()) args["openid.mode"] = u"check_authentication" url = self._OPENID_ENDPOINT if http_client is None: http_client = self.get_auth_http_client() http_client.fetch(url, self.async_callback( self._on_authentication_verified, callback), method="POST", body=urllib.urlencode(args)) def _openid_args(self, callback_uri, ax_attrs=[], oauth_scope=None): url = urlparse.urljoin(self.request.full_url(), callback_uri) args = { "openid.ns": "http://specs.openid.net/auth/2.0", "openid.claimed_id": "http://specs.openid.net/auth/2.0/identifier_select", "openid.identity": "http://specs.openid.net/auth/2.0/identifier_select", "openid.return_to": url, "openid.realm": urlparse.urljoin(url, '/'), "openid.mode": "checkid_setup", } if ax_attrs: args.update({ "openid.ns.ax": "http://openid.net/srv/ax/1.0", "openid.ax.mode": "fetch_request", }) ax_attrs = set(ax_attrs) required = [] if "name" in ax_attrs: ax_attrs -= set(["name", "firstname", "fullname", "lastname"]) required += ["firstname", "fullname", "lastname"] args.update({ "openid.ax.type.firstname": "http://axschema.org/namePerson/first", "openid.ax.type.fullname": "http://axschema.org/namePerson", "openid.ax.type.lastname": "http://axschema.org/namePerson/last", }) known_attrs = { "email": "http://axschema.org/contact/email", "language": "http://axschema.org/pref/language", "username": "http://axschema.org/namePerson/friendly", } for name in ax_attrs: args["openid.ax.type." + name] = known_attrs[name] required.append(name) args["openid.ax.required"] = ",".join(required) if oauth_scope: args.update({ "openid.ns.oauth": "http://specs.openid.net/extensions/oauth/1.0", "openid.oauth.consumer": self.request.host.split(":")[0], "openid.oauth.scope": oauth_scope, }) return args def _on_authentication_verified(self, callback, response): if response.error or b("is_valid:true") not in response.body: logging.warning("Invalid OpenID response: %s", response.error or response.body) callback(None) return # Make sure we got back at least an email from attribute exchange ax_ns = None for name in self.request.arguments.iterkeys(): if name.startswith("openid.ns.") and \ self.get_argument(name) == u"http://openid.net/srv/ax/1.0": ax_ns = name[10:] break def get_ax_arg(uri): if not ax_ns: return u"" prefix = "openid." + ax_ns + ".type." ax_name = None for name in self.request.arguments.iterkeys(): if self.get_argument(name) == uri and name.startswith(prefix): part = name[len(prefix):] ax_name = "openid." + ax_ns + ".value." + part break if not ax_name: return u"" return self.get_argument(ax_name, u"") email = get_ax_arg("http://axschema.org/contact/email") name = get_ax_arg("http://axschema.org/namePerson") first_name = get_ax_arg("http://axschema.org/namePerson/first") last_name = get_ax_arg("http://axschema.org/namePerson/last") username = get_ax_arg("http://axschema.org/namePerson/friendly") locale = get_ax_arg("http://axschema.org/pref/language").lower() user = dict() name_parts = [] if first_name: user["first_name"] = first_name name_parts.append(first_name) if last_name: user["last_name"] = last_name name_parts.append(last_name) if name: user["name"] = name elif name_parts: user["name"] = u" ".join(name_parts) elif email: user["name"] = email.split("@")[0] if email: user["email"] = email if locale: user["locale"] = locale if username: user["username"] = username claimed_id = self.get_argument("openid.claimed_id", None) if claimed_id: user["claimed_id"] = claimed_id callback(user) def get_auth_http_client(self): """Returns the AsyncHTTPClient instance to be used for auth requests. May be overridden by subclasses to use an http client other than the default. """ return httpclient.AsyncHTTPClient() class OAuthMixin(object): """Abstract implementation of OAuth. See TwitterMixin and FriendFeedMixin below for example implementations. """ def authorize_redirect(self, callback_uri=None, extra_params=None, http_client=None): """Redirects the user to obtain OAuth authorization for this service. Twitter and FriendFeed both require that you register a Callback URL with your application. You should call this method to log the user in, and then call get_authenticated_user() in the handler you registered as your Callback URL to complete the authorization process. This method sets a cookie called _oauth_request_token which is subsequently used (and cleared) in get_authenticated_user for security purposes. """ if callback_uri and getattr(self, "_OAUTH_NO_CALLBACKS", False): raise Exception("This service does not support oauth_callback") if http_client is None: http_client = self.get_auth_http_client() if getattr(self, "_OAUTH_VERSION", "1.0a") == "1.0a": http_client.fetch( self._oauth_request_token_url(callback_uri=callback_uri, extra_params=extra_params), self.async_callback( self._on_request_token, self._OAUTH_AUTHORIZE_URL, callback_uri)) else: http_client.fetch( self._oauth_request_token_url(), self.async_callback( self._on_request_token, self._OAUTH_AUTHORIZE_URL, callback_uri)) def get_authenticated_user(self, callback, http_client=None): """Gets the OAuth authorized user and access token on callback. This method should be called from the handler for your registered OAuth Callback URL to complete the registration process. We call callback with the authenticated user, which in addition to standard attributes like 'name' includes the 'access_key' attribute, which contains the OAuth access you can use to make authorized requests to this service on behalf of the user. """ request_key = escape.utf8(self.get_argument("oauth_token")) oauth_verifier = self.get_argument("oauth_verifier", None) request_cookie = self.get_cookie("_oauth_request_token") if not request_cookie: logging.warning("Missing OAuth request token cookie") callback(None) return self.clear_cookie("_oauth_request_token") cookie_key, cookie_secret = [base64.b64decode(escape.utf8(i)) for i in request_cookie.split("|")] if cookie_key != request_key: logging.info((cookie_key, request_key, request_cookie)) logging.warning("Request token does not match cookie") callback(None) return token = dict(key=cookie_key, secret=cookie_secret) if oauth_verifier: token["verifier"] = oauth_verifier if http_client is None: http_client = self.get_auth_http_client() http_client.fetch(self._oauth_access_token_url(token), self.async_callback(self._on_access_token, callback)) def _oauth_request_token_url(self, callback_uri=None, extra_params=None): consumer_token = self._oauth_consumer_token() url = self._OAUTH_REQUEST_TOKEN_URL args = dict( oauth_consumer_key=escape.to_basestring(consumer_token["key"]), oauth_signature_method="HMAC-SHA1", oauth_timestamp=str(int(time.time())), oauth_nonce=escape.to_basestring(binascii.b2a_hex(uuid.uuid4().bytes)), oauth_version=getattr(self, "_OAUTH_VERSION", "1.0a"), ) if getattr(self, "_OAUTH_VERSION", "1.0a") == "1.0a": if callback_uri == "oob": args["oauth_callback"] = "oob" elif callback_uri: args["oauth_callback"] = urlparse.urljoin( self.request.full_url(), callback_uri) if extra_params: args.update(extra_params) signature = _oauth10a_signature(consumer_token, "GET", url, args) else: signature = _oauth_signature(consumer_token, "GET", url, args) args["oauth_signature"] = signature return url + "?" + urllib.urlencode(args) def _on_request_token(self, authorize_url, callback_uri, response): if response.error: raise Exception("Could not get request token") request_token = _oauth_parse_response(response.body) data = (base64.b64encode(request_token["key"]) + b("|") + base64.b64encode(request_token["secret"])) self.set_cookie("_oauth_request_token", data) args = dict(oauth_token=request_token["key"]) if callback_uri == "oob": self.finish(authorize_url + "?" + urllib.urlencode(args)) return elif callback_uri: args["oauth_callback"] = urlparse.urljoin( self.request.full_url(), callback_uri) self.redirect(authorize_url + "?" + urllib.urlencode(args)) def _oauth_access_token_url(self, request_token): consumer_token = self._oauth_consumer_token() url = self._OAUTH_ACCESS_TOKEN_URL args = dict( oauth_consumer_key=escape.to_basestring(consumer_token["key"]), oauth_token=escape.to_basestring(request_token["key"]), oauth_signature_method="HMAC-SHA1", oauth_timestamp=str(int(time.time())), oauth_nonce=escape.to_basestring(binascii.b2a_hex(uuid.uuid4().bytes)), oauth_version=getattr(self, "_OAUTH_VERSION", "1.0a"), ) if "verifier" in request_token: args["oauth_verifier"] = request_token["verifier"] if getattr(self, "_OAUTH_VERSION", "1.0a") == "1.0a": signature = _oauth10a_signature(consumer_token, "GET", url, args, request_token) else: signature = _oauth_signature(consumer_token, "GET", url, args, request_token) args["oauth_signature"] = signature return url + "?" + urllib.urlencode(args) def _on_access_token(self, callback, response): if response.error: logging.warning("Could not fetch access token") callback(None) return access_token = _oauth_parse_response(response.body) self._oauth_get_user(access_token, self.async_callback( self._on_oauth_get_user, access_token, callback)) def _oauth_get_user(self, access_token, callback): raise NotImplementedError() def _on_oauth_get_user(self, access_token, callback, user): if not user: callback(None) return user["access_token"] = access_token callback(user) def _oauth_request_parameters(self, url, access_token, parameters={}, method="GET"): """Returns the OAuth parameters as a dict for the given request. parameters should include all POST arguments and query string arguments that will be sent with the request. """ consumer_token = self._oauth_consumer_token() base_args = dict( oauth_consumer_key=escape.to_basestring(consumer_token["key"]), oauth_token=escape.to_basestring(access_token["key"]), oauth_signature_method="HMAC-SHA1", oauth_timestamp=str(int(time.time())), oauth_nonce=escape.to_basestring(binascii.b2a_hex(uuid.uuid4().bytes)), oauth_version=getattr(self, "_OAUTH_VERSION", "1.0a"), ) args = {} args.update(base_args) args.update(parameters) if getattr(self, "_OAUTH_VERSION", "1.0a") == "1.0a": signature = _oauth10a_signature(consumer_token, method, url, args, access_token) else: signature = _oauth_signature(consumer_token, method, url, args, access_token) base_args["oauth_signature"] = signature return base_args def get_auth_http_client(self): """Returns the AsyncHTTPClient instance to be used for auth requests. May be overridden by subclasses to use an http client other than the default. """ return httpclient.AsyncHTTPClient() class OAuth2Mixin(object): """Abstract implementation of OAuth v 2.""" def authorize_redirect(self, redirect_uri=None, client_id=None, client_secret=None, extra_params=None): """Redirects the user to obtain OAuth authorization for this service. Some providers require that you register a Callback URL with your application. You should call this method to log the user in, and then call get_authenticated_user() in the handler you registered as your Callback URL to complete the authorization process. """ args = { "redirect_uri": redirect_uri, "client_id": client_id } if extra_params: args.update(extra_params) self.redirect( url_concat(self._OAUTH_AUTHORIZE_URL, args)) def _oauth_request_token_url(self, redirect_uri=None, client_id=None, client_secret=None, code=None, extra_params=None): url = self._OAUTH_ACCESS_TOKEN_URL args = dict( redirect_uri=redirect_uri, code=code, client_id=client_id, client_secret=client_secret, ) if extra_params: args.update(extra_params) return url_concat(url, args) class TwitterMixin(OAuthMixin): """Twitter OAuth authentication. To authenticate with Twitter, register your application with Twitter at http://twitter.com/apps. Then copy your Consumer Key and Consumer Secret to the application settings 'twitter_consumer_key' and 'twitter_consumer_secret'. Use this Mixin on the handler for the URL you registered as your application's Callback URL. When your application is set up, you can use this Mixin like this to authenticate the user with Twitter and get access to their stream:: class TwitterHandler(tornado.web.RequestHandler, tornado.auth.TwitterMixin): @tornado.web.asynchronous def get(self): if self.get_argument("oauth_token", None): self.get_authenticated_user(self.async_callback(self._on_auth)) return self.authorize_redirect() def _on_auth(self, user): if not user: raise tornado.web.HTTPError(500, "Twitter auth failed") # Save the user using, e.g., set_secure_cookie() The user object returned by get_authenticated_user() includes the attributes 'username', 'name', and all of the custom Twitter user attributes describe at http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-users%C2%A0show in addition to 'access_token'. You should save the access token with the user; it is required to make requests on behalf of the user later with twitter_request(). """ _OAUTH_REQUEST_TOKEN_URL = "http://api.twitter.com/oauth/request_token" _OAUTH_ACCESS_TOKEN_URL = "http://api.twitter.com/oauth/access_token" _OAUTH_AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize" _OAUTH_AUTHENTICATE_URL = "http://api.twitter.com/oauth/authenticate" _OAUTH_NO_CALLBACKS = False _TWITTER_BASE_URL = "http://api.twitter.com/1" def authenticate_redirect(self, callback_uri=None): """Just like authorize_redirect(), but auto-redirects if authorized. This is generally the right interface to use if you are using Twitter for single-sign on. """ http = self.get_auth_http_client() http.fetch(self._oauth_request_token_url(callback_uri=callback_uri), self.async_callback( self._on_request_token, self._OAUTH_AUTHENTICATE_URL, None)) def twitter_request(self, path, callback, access_token=None, post_args=None, **args): """Fetches the given API path, e.g., "/statuses/user_timeline/btaylor" The path should not include the format (we automatically append ".json" and parse the JSON output). If the request is a POST, post_args should be provided. Query string arguments should be given as keyword arguments. All the Twitter methods are documented at http://apiwiki.twitter.com/Twitter-API-Documentation. Many methods require an OAuth access token which you can obtain through authorize_redirect() and get_authenticated_user(). The user returned through that process includes an 'access_token' attribute that can be used to make authenticated requests via this method. Example usage:: class MainHandler(tornado.web.RequestHandler, tornado.auth.TwitterMixin): @tornado.web.authenticated @tornado.web.asynchronous def get(self): self.twitter_request( "/statuses/update", post_args={"status": "Testing Tornado Web Server"}, access_token=user["access_token"], callback=self.async_callback(self._on_post)) def _on_post(self, new_entry): if not new_entry: # Call failed; perhaps missing permission? self.authorize_redirect() return self.finish("Posted a message!") """ if path.startswith('http:') or path.startswith('https:'): # Raw urls are useful for e.g. search which doesn't follow the # usual pattern: http://search.twitter.com/search.json url = path else: url = self._TWITTER_BASE_URL + path + ".json" # Add the OAuth resource request signature if we have credentials if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters( url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urllib.urlencode(args) callback = self.async_callback(self._on_twitter_request, callback) http = self.get_auth_http_client() if post_args is not None: http.fetch(url, method="POST", body=urllib.urlencode(post_args), callback=callback) else: http.fetch(url, callback=callback) def _on_twitter_request(self, callback, response): if response.error: logging.warning("Error response %s fetching %s", response.error, response.request.url) callback(None) return callback(escape.json_decode(response.body)) def _oauth_consumer_token(self): self.require_setting("twitter_consumer_key", "Twitter OAuth") self.require_setting("twitter_consumer_secret", "Twitter OAuth") return dict( key=self.settings["twitter_consumer_key"], secret=self.settings["twitter_consumer_secret"]) def _oauth_get_user(self, access_token, callback): callback = self.async_callback(self._parse_user_response, callback) self.twitter_request( "/users/show/" + escape.native_str(access_token[b("screen_name")]), access_token=access_token, callback=callback) def _parse_user_response(self, callback, user): if user: user["username"] = user["screen_name"] callback(user) class FriendFeedMixin(OAuthMixin): """FriendFeed OAuth authentication. To authenticate with FriendFeed, register your application with FriendFeed at http://friendfeed.com/api/applications. Then copy your Consumer Key and Consumer Secret to the application settings 'friendfeed_consumer_key' and 'friendfeed_consumer_secret'. Use this Mixin on the handler for the URL you registered as your application's Callback URL. When your application is set up, you can use this Mixin like this to authenticate the user with FriendFeed and get access to their feed:: class FriendFeedHandler(tornado.web.RequestHandler, tornado.auth.FriendFeedMixin): @tornado.web.asynchronous def get(self): if self.get_argument("oauth_token", None): self.get_authenticated_user(self.async_callback(self._on_auth)) return self.authorize_redirect() def _on_auth(self, user): if not user: raise tornado.web.HTTPError(500, "FriendFeed auth failed") # Save the user using, e.g., set_secure_cookie() The user object returned by get_authenticated_user() includes the attributes 'username', 'name', and 'description' in addition to 'access_token'. You should save the access token with the user; it is required to make requests on behalf of the user later with friendfeed_request(). """ _OAUTH_VERSION = "1.0" _OAUTH_REQUEST_TOKEN_URL = "https://friendfeed.com/account/oauth/request_token" _OAUTH_ACCESS_TOKEN_URL = "https://friendfeed.com/account/oauth/access_token" _OAUTH_AUTHORIZE_URL = "https://friendfeed.com/account/oauth/authorize" _OAUTH_NO_CALLBACKS = True _OAUTH_VERSION = "1.0" def friendfeed_request(self, path, callback, access_token=None, post_args=None, **args): """Fetches the given relative API path, e.g., "/bret/friends" If the request is a POST, post_args should be provided. Query string arguments should be given as keyword arguments. All the FriendFeed methods are documented at http://friendfeed.com/api/documentation. Many methods require an OAuth access token which you can obtain through authorize_redirect() and get_authenticated_user(). The user returned through that process includes an 'access_token' attribute that can be used to make authenticated requests via this method. Example usage:: class MainHandler(tornado.web.RequestHandler, tornado.auth.FriendFeedMixin): @tornado.web.authenticated @tornado.web.asynchronous def get(self): self.friendfeed_request( "/entry", post_args={"body": "Testing Tornado Web Server"}, access_token=self.current_user["access_token"], callback=self.async_callback(self._on_post)) def _on_post(self, new_entry): if not new_entry: # Call failed; perhaps missing permission? self.authorize_redirect() return self.finish("Posted a message!") """ # Add the OAuth resource request signature if we have credentials url = "http://friendfeed-api.com/v2" + path if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters( url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urllib.urlencode(args) callback = self.async_callback(self._on_friendfeed_request, callback) http = self.get_auth_http_client() if post_args is not None: http.fetch(url, method="POST", body=urllib.urlencode(post_args), callback=callback) else: http.fetch(url, callback=callback) def _on_friendfeed_request(self, callback, response): if response.error: logging.warning("Error response %s fetching %s", response.error, response.request.url) callback(None) return callback(escape.json_decode(response.body)) def _oauth_consumer_token(self): self.require_setting("friendfeed_consumer_key", "FriendFeed OAuth") self.require_setting("friendfeed_consumer_secret", "FriendFeed OAuth") return dict( key=self.settings["friendfeed_consumer_key"], secret=self.settings["friendfeed_consumer_secret"]) def _oauth_get_user(self, access_token, callback): callback = self.async_callback(self._parse_user_response, callback) self.friendfeed_request( "/feedinfo/" + access_token["username"], include="id,name,description", access_token=access_token, callback=callback) def _parse_user_response(self, callback, user): if user: user["username"] = user["id"] callback(user) class GoogleMixin(OpenIdMixin, OAuthMixin): """Google Open ID / OAuth authentication. No application registration is necessary to use Google for authentication or to access Google resources on behalf of a user. To authenticate with Google, redirect with authenticate_redirect(). On return, parse the response with get_authenticated_user(). We send a dict containing the values for the user, including 'email', 'name', and 'locale'. Example usage:: class GoogleHandler(tornado.web.RequestHandler, tornado.auth.GoogleMixin): @tornado.web.asynchronous def get(self): if self.get_argument("openid.mode", None): self.get_authenticated_user(self.async_callback(self._on_auth)) return self.authenticate_redirect() def _on_auth(self, user): if not user: raise tornado.web.HTTPError(500, "Google auth failed") # Save the user with, e.g., set_secure_cookie() """ _OPENID_ENDPOINT = "https://www.google.com/accounts/o8/ud" _OAUTH_ACCESS_TOKEN_URL = "https://www.google.com/accounts/OAuthGetAccessToken" def authorize_redirect(self, oauth_scope, callback_uri=None, ax_attrs=["name", "email", "language", "username"]): """Authenticates and authorizes for the given Google resource. Some of the available resources are: * Gmail Contacts - http://www.google.com/m8/feeds/ * Calendar - http://www.google.com/calendar/feeds/ * Finance - http://finance.google.com/finance/feeds/ You can authorize multiple resources by separating the resource URLs with a space. """ callback_uri = callback_uri or self.request.uri args = self._openid_args(callback_uri, ax_attrs=ax_attrs, oauth_scope=oauth_scope) self.redirect(self._OPENID_ENDPOINT + "?" + urllib.urlencode(args)) def get_authenticated_user(self, callback): """Fetches the authenticated user data upon redirect.""" # Look to see if we are doing combined OpenID/OAuth oauth_ns = "" for name, values in self.request.arguments.iteritems(): if name.startswith("openid.ns.") and \ values[-1] == u"http://specs.openid.net/extensions/oauth/1.0": oauth_ns = name[10:] break token = self.get_argument("openid." + oauth_ns + ".request_token", "") if token: http = self.get_auth_http_client() token = dict(key=token, secret="") http.fetch(self._oauth_access_token_url(token), self.async_callback(self._on_access_token, callback)) else: OpenIdMixin.get_authenticated_user(self, callback) def _oauth_consumer_token(self): self.require_setting("google_consumer_key", "Google OAuth") self.require_setting("google_consumer_secret", "Google OAuth") return dict( key=self.settings["google_consumer_key"], secret=self.settings["google_consumer_secret"]) def _oauth_get_user(self, access_token, callback): OpenIdMixin.get_authenticated_user(self, callback) class FacebookMixin(object): """Facebook Connect authentication. New applications should consider using `FacebookGraphMixin` below instead of this class. To authenticate with Facebook, register your application with Facebook at http://www.facebook.com/developers/apps.php. Then copy your API Key and Application Secret to the application settings 'facebook_api_key' and 'facebook_secret'. When your application is set up, you can use this Mixin like this to authenticate the user with Facebook:: class FacebookHandler(tornado.web.RequestHandler, tornado.auth.FacebookMixin): @tornado.web.asynchronous def get(self): if self.get_argument("session", None): self.get_authenticated_user(self.async_callback(self._on_auth)) return self.authenticate_redirect() def _on_auth(self, user): if not user: raise tornado.web.HTTPError(500, "Facebook auth failed") # Save the user using, e.g., set_secure_cookie() The user object returned by get_authenticated_user() includes the attributes 'facebook_uid' and 'name' in addition to session attributes like 'session_key'. You should save the session key with the user; it is required to make requests on behalf of the user later with facebook_request(). """ def authenticate_redirect(self, callback_uri=None, cancel_uri=None, extended_permissions=None): """Authenticates/installs this app for the current user.""" self.require_setting("facebook_api_key", "Facebook Connect") callback_uri = callback_uri or self.request.uri args = { "api_key": self.settings["facebook_api_key"], "v": "1.0", "fbconnect": "true", "display": "page", "next": urlparse.urljoin(self.request.full_url(), callback_uri), "return_session": "true", } if cancel_uri: args["cancel_url"] = urlparse.urljoin( self.request.full_url(), cancel_uri) if extended_permissions: if isinstance(extended_permissions, (unicode, bytes_type)): extended_permissions = [extended_permissions] args["req_perms"] = ",".join(extended_permissions) self.redirect("http://www.facebook.com/login.php?" + urllib.urlencode(args)) def authorize_redirect(self, extended_permissions, callback_uri=None, cancel_uri=None): """Redirects to an authorization request for the given FB resource. The available resource names are listed at http://wiki.developers.facebook.com/index.php/Extended_permission. The most common resource types include: * publish_stream * read_stream * email * sms extended_permissions can be a single permission name or a list of names. To get the session secret and session key, call get_authenticated_user() just as you would with authenticate_redirect(). """ self.authenticate_redirect(callback_uri, cancel_uri, extended_permissions) def get_authenticated_user(self, callback): """Fetches the authenticated Facebook user. The authenticated user includes the special Facebook attributes 'session_key' and 'facebook_uid' in addition to the standard user attributes like 'name'. """ self.require_setting("facebook_api_key", "Facebook Connect") session = escape.json_decode(self.get_argument("session")) self.facebook_request( method="facebook.users.getInfo", callback=self.async_callback( self._on_get_user_info, callback, session), session_key=session["session_key"], uids=session["uid"], fields="uid,first_name,last_name,name,locale,pic_square," "profile_url,username") def facebook_request(self, method, callback, **args): """Makes a Facebook API REST request. We automatically include the Facebook API key and signature, but it is the callers responsibility to include 'session_key' and any other required arguments to the method. The available Facebook methods are documented here: http://wiki.developers.facebook.com/index.php/API Here is an example for the stream.get() method:: class MainHandler(tornado.web.RequestHandler, tornado.auth.FacebookMixin): @tornado.web.authenticated @tornado.web.asynchronous def get(self): self.facebook_request( method="stream.get", callback=self.async_callback(self._on_stream), session_key=self.current_user["session_key"]) def _on_stream(self, stream): if stream is None: # Not authorized to read the stream yet? self.redirect(self.authorize_redirect("read_stream")) return self.render("stream.html", stream=stream) """ self.require_setting("facebook_api_key", "Facebook Connect") self.require_setting("facebook_secret", "Facebook Connect") if not method.startswith("facebook."): method = "facebook." + method args["api_key"] = self.settings["facebook_api_key"] args["v"] = "1.0" args["method"] = method args["call_id"] = str(long(time.time() * 1e6)) args["format"] = "json" args["sig"] = self._signature(args) url = "http://api.facebook.com/restserver.php?" + \ urllib.urlencode(args) http = self.get_auth_http_client() http.fetch(url, callback=self.async_callback( self._parse_response, callback)) def _on_get_user_info(self, callback, session, users): if users is None: callback(None) return callback({ "name": users[0]["name"], "first_name": users[0]["first_name"], "last_name": users[0]["last_name"], "uid": users[0]["uid"], "locale": users[0]["locale"], "pic_square": users[0]["pic_square"], "profile_url": users[0]["profile_url"], "username": users[0].get("username"), "session_key": session["session_key"], "session_expires": session.get("expires"), }) def _parse_response(self, callback, response): if response.error: logging.warning("HTTP error from Facebook: %s", response.error) callback(None) return try: json = escape.json_decode(response.body) except Exception: logging.warning("Invalid JSON from Facebook: %r", response.body) callback(None) return if isinstance(json, dict) and json.get("error_code"): logging.warning("Facebook error: %d: %r", json["error_code"], json.get("error_msg")) callback(None) return callback(json) def _signature(self, args): parts = ["%s=%s" % (n, args[n]) for n in sorted(args.keys())] body = "".join(parts) + self.settings["facebook_secret"] if isinstance(body, unicode): body = body.encode("utf-8") return hashlib.md5(body).hexdigest() def get_auth_http_client(self): """Returns the AsyncHTTPClient instance to be used for auth requests. May be overridden by subclasses to use an http client other than the default. """ return httpclient.AsyncHTTPClient() class FacebookGraphMixin(OAuth2Mixin): """Facebook authentication using the new Graph API and OAuth2.""" _OAUTH_ACCESS_TOKEN_URL = "https://graph.facebook.com/oauth/access_token?" _OAUTH_AUTHORIZE_URL = "https://graph.facebook.com/oauth/authorize?" _OAUTH_NO_CALLBACKS = False def get_authenticated_user(self, redirect_uri, client_id, client_secret, code, callback, extra_fields=None): """Handles the login for the Facebook user, returning a user object. Example usage:: class FacebookGraphLoginHandler(LoginHandler, tornado.auth.FacebookGraphMixin): @tornado.web.asynchronous def get(self): if self.get_argument("code", False): self.get_authenticated_user( redirect_uri='/auth/facebookgraph/', client_id=self.settings["facebook_api_key"], client_secret=self.settings["facebook_secret"], code=self.get_argument("code"), callback=self.async_callback( self._on_login)) return self.authorize_redirect(redirect_uri='/auth/facebookgraph/', client_id=self.settings["facebook_api_key"], extra_params={"scope": "read_stream,offline_access"}) def _on_login(self, user): logging.error(user) self.finish() """ http = self.get_auth_http_client() args = { "redirect_uri": redirect_uri, "code": code, "client_id": client_id, "client_secret": client_secret, } fields = set(['id', 'name', 'first_name', 'last_name', 'locale', 'picture', 'link']) if extra_fields: fields.update(extra_fields) http.fetch(self._oauth_request_token_url(**args), self.async_callback(self._on_access_token, redirect_uri, client_id, client_secret, callback, fields)) def _on_access_token(self, redirect_uri, client_id, client_secret, callback, fields, response): if response.error: logging.warning('Facebook auth error: %s' % str(response)) callback(None) return args = escape.parse_qs_bytes(escape.native_str(response.body)) session = { "access_token": args["access_token"][-1], "expires": args.get("expires") } self.facebook_request( path="/me", callback=self.async_callback( self._on_get_user_info, callback, session, fields), access_token=session["access_token"], fields=",".join(fields) ) def _on_get_user_info(self, callback, session, fields, user): if user is None: callback(None) return fieldmap = {} for field in fields: fieldmap[field] = user.get(field) fieldmap.update({"access_token": session["access_token"], "session_expires": session.get("expires")}) callback(fieldmap) def facebook_request(self, path, callback, access_token=None, post_args=None, **args): """Fetches the given relative API path, e.g., "/btaylor/picture" If the request is a POST, post_args should be provided. Query string arguments should be given as keyword arguments. An introduction to the Facebook Graph API can be found at http://developers.facebook.com/docs/api Many methods require an OAuth access token which you can obtain through authorize_redirect() and get_authenticated_user(). The user returned through that process includes an 'access_token' attribute that can be used to make authenticated requests via this method. Example usage:: class MainHandler(tornado.web.RequestHandler, tornado.auth.FacebookGraphMixin): @tornado.web.authenticated @tornado.web.asynchronous def get(self): self.facebook_request( "/me/feed", post_args={"message": "I am posting from my Tornado application!"}, access_token=self.current_user["access_token"], callback=self.async_callback(self._on_post)) def _on_post(self, new_entry): if not new_entry: # Call failed; perhaps missing permission? self.authorize_redirect() return self.finish("Posted a message!") """ url = "https://graph.facebook.com" + path all_args = {} if access_token: all_args["access_token"] = access_token all_args.update(args) if all_args: url += "?" + urllib.urlencode(all_args) callback = self.async_callback(self._on_facebook_request, callback) http = self.get_auth_http_client() if post_args is not None: http.fetch(url, method="POST", body=urllib.urlencode(post_args), callback=callback) else: http.fetch(url, callback=callback) def _on_facebook_request(self, callback, response): if response.error: logging.warning("Error response %s fetching %s", response.error, response.request.url) callback(None) return callback(escape.json_decode(response.body)) def get_auth_http_client(self): """Returns the AsyncHTTPClient instance to be used for auth requests. May be overridden by subclasses to use an http client other than the default. """ return httpclient.AsyncHTTPClient() def _oauth_signature(consumer_token, method, url, parameters={}, token=None): """Calculates the HMAC-SHA1 OAuth signature for the given request. See http://oauth.net/core/1.0/#signing_process """ parts = urlparse.urlparse(url) scheme, netloc, path = parts[:3] normalized_url = scheme.lower() + "://" + netloc.lower() + path base_elems = [] base_elems.append(method.upper()) base_elems.append(normalized_url) base_elems.append("&".join("%s=%s" % (k, _oauth_escape(str(v))) for k, v in sorted(parameters.items()))) base_string = "&".join(_oauth_escape(e) for e in base_elems) key_elems = [escape.utf8(consumer_token["secret"])] key_elems.append(escape.utf8(token["secret"] if token else "")) key = b("&").join(key_elems) hash = hmac.new(key, escape.utf8(base_string), hashlib.sha1) return binascii.b2a_base64(hash.digest())[:-1] def _oauth10a_signature(consumer_token, method, url, parameters={}, token=None): """Calculates the HMAC-SHA1 OAuth 1.0a signature for the given request. See http://oauth.net/core/1.0a/#signing_process """ parts = urlparse.urlparse(url) scheme, netloc, path = parts[:3] normalized_url = scheme.lower() + "://" + netloc.lower() + path base_elems = [] base_elems.append(method.upper()) base_elems.append(normalized_url) base_elems.append("&".join("%s=%s" % (k, _oauth_escape(str(v))) for k, v in sorted(parameters.items()))) base_string = "&".join(_oauth_escape(e) for e in base_elems) key_elems = [escape.utf8(urllib.quote(consumer_token["secret"], safe='~'))] key_elems.append(escape.utf8(urllib.quote(token["secret"], safe='~') if token else "")) key = b("&").join(key_elems) hash = hmac.new(key, escape.utf8(base_string), hashlib.sha1) return binascii.b2a_base64(hash.digest())[:-1] def _oauth_escape(val): if isinstance(val, unicode): val = val.encode("utf-8") return urllib.quote(val, safe="~") def _oauth_parse_response(body): p = escape.parse_qs(body, keep_blank_values=False) token = dict(key=p[b("oauth_token")][0], secret=p[b("oauth_token_secret")][0]) # Add the extra parameters the Provider included to the token special = (b("oauth_token"), b("oauth_token_secret")) token.update((k, p[k][0]) for k in p if k not in special) return token
py
1a4b666b31eb721082bc964386a1ab0600c8c98b
import demoDay21_recsys_music.hyj.config_hyj as conf import pandas as pd import demoDay21_recsys_music.hyj.gen_cf_data_hyj as gen import tensorflow as tf import numpy as np data=gen.user_item_socre(nrows=500) # 定义label stay_seconds/total_timelen>0.9 -> 1 data['label']=data['score'].apply(lambda x:1 if x>=0.9 else 0) # 关联用户信息和item信息到data user_profile=conf.user_profile() music_meta=conf.music_data() # data数据结构 # user_id item_id score label gender age salary province item_name total_timelen location tags # 0 0000066b1be6f28ad5e40d47b8d3e51c 426100349 1.280 1 女 26-35 10000-20000 香港 刘德华 - 回家的路 2015央视春晚 现场版 250 港台 - # 1 000072fc29132acaf20168c589269e1c 426100349 1.276 1 女 36-45 5000-10000 湖北 刘德华 - 回家的路 2015央视春晚 现场版 250 港台 - # 2 000074ec4874ab7d99d543d0ce419118 426100349 1.084 1 男 36-45 2000-5000 宁夏 刘德华 - 回家的路 2015央视春晚 现场版 250 港台 - data=data.merge(user_profile,how='inner',on='user_id').merge(music_meta,how='inner',on='item_id') ''' 定义特征X''' #用户特征 user_feat=['age','gender','salary','province'] # 物品特征 item_feat=['location','total_timelen'] item_text_feat=['item_name','tags'] # 交叉特征 watch_feat=['stay_seconds','score','hour'] # 离散和连续特征 dispersed_feat=user_feat+['location'] continue_feat=['score'] # 获取Y (label) labels=data['label'] del data['label'] # 离散特征-one-hot处理 # df数据结构 : # age_0-18 age_19-25 age_26-35 age_36-45 age_46-100 gender_女 gender_男 salary_0-2000 salary_10000-20000 salary_2000-5000 ... province_香港 province_黑龙江 location_- location_亚洲 location_国内 location_日韩 location_日韩,日本 location_日韩,韩国 location_欧美 location_港台 # 0 0 0 1 0 0 1 0 0 1 0 ... 1 0 0 0 0 0 0 0 0 1 # 1 0 0 0 1 0 1 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 1 # 2 0 0 0 1 0 0 1 0 0 1 ... 0 0 0 0 0 0 0 0 0 1 # 3 0 1 0 0 0 1 0 0 0 1 ... 0 0 0 0 0 0 0 0 0 1 # 4 0 0 0 1 0 0 1 0 1 0 ... 0 0 0 0 0 0 0 0 0 1 # get_dummies one-hot处理: 将每列展开成多列,有值的为1,否则为0(根据每列的所有取值,如用户性别男 gender取值有男女 则展开为gender_女 0 gender_男 1) df=pd.get_dummies(data[dispersed_feat]) # 离散特征数组 # one-hot数据结构 # Index(['age_0-18', 'age_19-25', 'age_26-35', 'age_36-45', 'age_46-100', # 'gender_女', 'gender_男', 'salary_0-2000', 'salary_10000-20000', # 'salary_2000-5000', 'salary_20000-100000', 'salary_5000-10000', # 'province_上海', 'province_云南', 'province_内蒙古', 'province_北京', # 'province_台湾', 'province_吉林', 'province_四川', 'province_天津', # 'province_宁夏', 'province_安徽', 'province_山东', 'province_山西', # 'province_广东', 'province_广西', 'province_新疆', 'province_江苏', # 'province_江西', 'province_河北', 'province_河南', 'province_浙江', # 'province_海南', 'province_湖北', 'province_湖南', 'province_澳门', # 'province_甘肃', 'province_福建', 'province_西藏', 'province_贵州', # 'province_辽宁', 'province_重庆', 'province_陕西', 'province_青海', # 'province_香港', 'province_黑龙江', 'location_-', 'location_亚洲', # 'location_国内', 'location_日韩', 'location_日韩,日本', 'location_日韩,韩国', # 'location_欧美', 'location_港台'], # dtype='object') one_hot_cols=df.columns # 连续特征不做one-hot 直接存储 df[continue_feat]=data[continue_feat] df['label']=labels pre='F:\\00-data\\nn\\' df_file=pre+'music_data.csv' df.to_csv(df_file,index=False) print('df to csv done') chunks = pd.read_csv(df_file,iterator = True,chunksize=1000) chunk = chunks.get_chunk(5) print(chunk) print(np.array(chunk)) # 定义超参数 sample_num=len(df) x_col_num=len(df.columns)-1 y_class_num=2 x=tf.placeholder(dtype=tf.float32,shape=[None,x_col_num],name='X') y=tf.placeholder(dtype=tf.float32,shape=[None,y_class_num],name='Y') max_epoches=10000 learning_rate=0.01 batch_size=50 seed=0 n_hidden=30 del df # 定义权重和偏置变量 w={'h1':tf.Variable(tf.random_normal([x_col_num,n_hidden],seed=seed)), 'res':tf.Variable(tf.random_normal([n_hidden,y_class_num],seed=seed)) } b={'h1':tf.Variable(tf.random_normal([1,n_hidden],seed=seed)), 'res':tf.Variable(tf.random_normal([1,y_class_num],seed=seed)) } # 创建模型 def multilayer(x,w,b): # 隐藏层net值 h_1=tf.add(tf.matmul(x,w['h1']),b['h1']) # 隐藏层out值 h1_out=tf.sigmoid(h_1) # 输出层net值 res_net=tf.add(tf.matmul(h1_out,w['res']),b['res']) return res_net,res_net,h1_out,h_1 # 为正向传播、误差、梯度和更新计算创建计算图 y_hat,y_net,h_out,h_net=multilayer(x,w,b) ''' # softmax_cross_entropy_with_logits 表示先求softmax,再求交叉熵 等价于 loss2 = (-tf.reduce_sum(y*tf.log(tf.clip_by_value(softmax(y_hat),1e-10,1.0)))) clip_by_value防止y_hat取对数为0 ''' loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=y_hat)) # 梯度优化器 optimizer=tf.train.AdamOptimizer().minimize(loss) # 变量初始化器 init=tf.global_variables_initializer() # 读取数据 def data_gen(filename): f_queue=tf.train.string_input_producer(filename) reader=tf.TextLineReader(skip_header_lines=1) _,value=reader.read(f_queue) record_defaults=[[0.0] for _ in range(x_col_num+1)] data=tf.decode_csv(value,record_defaults=record_defaults) print(type(data)) feats=tf.stack(tf.gather_nd(data,indices=[[i] for i in range(x_col_num)])) label=data[-1] dad=10*batch_size cap=20*batch_size feat_batch,label_batch=tf.train.shuffle_batch([feats,label],batch_size=batch_size,min_after_dequeue=dad,capacity=cap) return feat_batch,label_batch def gen_data(feat_batch,label_batch): with tf.Session() as sess: coord=tf.train.Coordinator() threads=tf.train.start_queue_runners(coord=coord) feats,labels=sess.run([feat_batch,label_batch]) # for _ in range(5): coord.request_stop() coord.join(threads) return feats,labels auc_bool_arr=tf.equal(tf.argmax(y,1),tf.argmax(y_hat,1)) auc=tf.reduce_mean(tf.cast(auc_bool_arr,tf.float32)) # 保存模型 saver = tf.train.Saver() def train(): for i in range(5): coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) # 每次训练batch_size的样本量 batch_xs, batch_ys = data_gen([df_file]) train_auc(batch_xs, batch_ys) coord.request_stop() coord.join(threads) # try: # while not coord.should_stop(): # # except tf.errors.OutOfRangeError: # print('read done') # finally: # pass # Wait for threads to finish. def train_auc(batch_xs, batch_ys): print(batch_xs.shape) batch_xs_val, batch_ys_val = sess.run([batch_xs, batch_ys]) print(batch_xs_val[-1], batch_ys_val[-1]) batch_ys2 = [] for j in batch_ys_val: if j < 1.0: batch_ys2.append([1., 0.]) else: batch_ys2.append([0., 1.]) y_hat_out, _, loss_val, auc_val = sess.run([y_hat, optimizer, loss, auc], feed_dict={x: batch_xs_val, y: batch_ys2}) print('loss %s,auc %s' % (loss_val, auc_val)) with tf.Session() as sess: sess.run(init) # writer=tf.summary.FileWriter('graphs',graph=sess.graph) for epoch in range(20): loss_avg=0. loss_avg2=0. # 总的批次数 num_of_batch=int(sample_num/batch_size) print('epoch %s'%epoch) train() saver.save(sess,save_path=pre+'nn_softmax_model.ckpt') print('main done')
py
1a4b66be6027b68539da999f616fa514067016d1
# -*- encoding: utf-8 -*- """ Copyright (c) 2019 - present AppSeed.us """ from django.contrib.auth.decorators import login_required from django.shortcuts import render, get_object_or_404, redirect from django.template import loader from django.http import HttpResponse from django import template from .forms import * from .models import * from django.core.paginator import Paginator from django.shortcuts import render from django.contrib import messages import os from django.shortcuts import render, redirect, get_object_or_404 # from .models import Document # from .forms import DocumentForm from .helper_functions import similarity from datetime import datetime @login_required(login_url="/login/") def index(request): context = {} context['segment'] = 'index' html_template = loader.get_template('index.html') return HttpResponse(html_template.render(context, request)) def file_uploader_view(request): msg = None if request.method == "POST": form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): form.save() msg = 'User created.' return redirect("/documents/repository/") else: msg = 'Form is not valid' else: form = UploadFileForm() return render(request, "uploader.html", {"form": form, "msg": msg}) def checker_view(request): msg = None if request.method == "POST": form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): instance = form.save() msg = 'User created.' uploaded_file_name = instance.file.name print(f'name of file = {uploaded_file_name}') percentages = [] directory = os.listdir("core/media/documents/") if len(directory) != 1: print(f'dirtype = {type(directory)}') for file in directory: filename = os.fsdecode(file) print(f'name of FILE4 = {file}') if filename.endswith(".docx") and uploaded_file_name != 'documents/' + filename: print(f'name of file2 = {filename}') percentage = similarity(uploaded_file_name, filename) percentages.append(percentage) actual_percentage = round(float(sum(percentages) / len(percentages) - 1)) print(f'Actual %age = {actual_percentage}') return render(request, 'result.html', {'result': actual_percentage}) else: form = UploadFileForm() messages.error(request, "Sorry! No file to check against.") return render(request, "checker.html", {"form": form, "msg": msg}) else: msg = 'Form is not valid' else: form = UploadFileForm() return render(request, "checker.html", {"form": form, "msg": msg}) def uploads_view(request): uploads = Uploads.objects.all() paginator = Paginator(uploads, 5) page_number = request.GET.get('page') page_obj = paginator.get_page(page_number) return render(request, "uploads.html", {'page_obj': page_obj}) @login_required(login_url="/login/") def pages(request): context = {} # All resource paths end in .html. # Pick out the html file name from the url. And load that template. try: load_template = request.path.split('/')[-1] context['segment'] = load_template html_template = loader.get_template(load_template) return HttpResponse(html_template.render(context, request)) except template.TemplateDoesNotExist: html_template = loader.get_template('page-404.html') return HttpResponse(html_template.render(context, request)) except: html_template = loader.get_template('page-500.html') return HttpResponse(html_template.render(context, request))
py
1a4b678716e365ba68a43b0b80fd85485459852a
import sys import pytest import textwrap import subprocess import numpy as np import numpy.core._multiarray_tests as _multiarray_tests from numpy import array, arange, nditer, all from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_raises, HAS_REFCOUNT, suppress_warnings ) def iter_multi_index(i): ret = [] while not i.finished: ret.append(i.multi_index) i.iternext() return ret def iter_indices(i): ret = [] while not i.finished: ret.append(i.index) i.iternext() return ret def iter_iterindices(i): ret = [] while not i.finished: ret.append(i.iterindex) i.iternext() return ret @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts") def test_iter_refcount(): # Make sure the iterator doesn't leak # Basic a = arange(6) dt = np.dtype('f4').newbyteorder() rc_a = sys.getrefcount(a) rc_dt = sys.getrefcount(dt) with nditer(a, [], [['readwrite', 'updateifcopy']], casting='unsafe', op_dtypes=[dt]) as it: assert_(not it.iterationneedsapi) assert_(sys.getrefcount(a) > rc_a) assert_(sys.getrefcount(dt) > rc_dt) # del 'it' it = None assert_equal(sys.getrefcount(a), rc_a) assert_equal(sys.getrefcount(dt), rc_dt) # With a copy a = arange(6, dtype='f4') dt = np.dtype('f4') rc_a = sys.getrefcount(a) rc_dt = sys.getrefcount(dt) it = nditer(a, [], [['readwrite']], op_dtypes=[dt]) rc2_a = sys.getrefcount(a) rc2_dt = sys.getrefcount(dt) it2 = it.copy() assert_(sys.getrefcount(a) > rc2_a) assert_(sys.getrefcount(dt) > rc2_dt) it = None assert_equal(sys.getrefcount(a), rc2_a) assert_equal(sys.getrefcount(dt), rc2_dt) it2 = None assert_equal(sys.getrefcount(a), rc_a) assert_equal(sys.getrefcount(dt), rc_dt) del it2 # avoid pyflakes unused variable warning def test_iter_best_order(): # The iterator should always find the iteration order # with increasing memory addresses # Test the ordering for 1-D to 5-D shapes for shape in [(5,), (3, 4), (2, 3, 4), (2, 3, 4, 3), (2, 3, 2, 2, 3)]: a = arange(np.prod(shape)) # Test each combination of positive and negative strides for dirs in range(2**len(shape)): dirs_index = [slice(None)]*len(shape) for bit in range(len(shape)): if ((2**bit) & dirs): dirs_index[bit] = slice(None, None, -1) dirs_index = tuple(dirs_index) aview = a.reshape(shape)[dirs_index] # C-order i = nditer(aview, [], [['readonly']]) assert_equal([x for x in i], a) # Fortran-order i = nditer(aview.T, [], [['readonly']]) assert_equal([x for x in i], a) # Other order if len(shape) > 2: i = nditer(aview.swapaxes(0, 1), [], [['readonly']]) assert_equal([x for x in i], a) def test_iter_c_order(): # Test forcing C order # Test the ordering for 1-D to 5-D shapes for shape in [(5,), (3, 4), (2, 3, 4), (2, 3, 4, 3), (2, 3, 2, 2, 3)]: a = arange(np.prod(shape)) # Test each combination of positive and negative strides for dirs in range(2**len(shape)): dirs_index = [slice(None)]*len(shape) for bit in range(len(shape)): if ((2**bit) & dirs): dirs_index[bit] = slice(None, None, -1) dirs_index = tuple(dirs_index) aview = a.reshape(shape)[dirs_index] # C-order i = nditer(aview, order='C') assert_equal([x for x in i], aview.ravel(order='C')) # Fortran-order i = nditer(aview.T, order='C') assert_equal([x for x in i], aview.T.ravel(order='C')) # Other order if len(shape) > 2: i = nditer(aview.swapaxes(0, 1), order='C') assert_equal([x for x in i], aview.swapaxes(0, 1).ravel(order='C')) def test_iter_f_order(): # Test forcing F order # Test the ordering for 1-D to 5-D shapes for shape in [(5,), (3, 4), (2, 3, 4), (2, 3, 4, 3), (2, 3, 2, 2, 3)]: a = arange(np.prod(shape)) # Test each combination of positive and negative strides for dirs in range(2**len(shape)): dirs_index = [slice(None)]*len(shape) for bit in range(len(shape)): if ((2**bit) & dirs): dirs_index[bit] = slice(None, None, -1) dirs_index = tuple(dirs_index) aview = a.reshape(shape)[dirs_index] # C-order i = nditer(aview, order='F') assert_equal([x for x in i], aview.ravel(order='F')) # Fortran-order i = nditer(aview.T, order='F') assert_equal([x for x in i], aview.T.ravel(order='F')) # Other order if len(shape) > 2: i = nditer(aview.swapaxes(0, 1), order='F') assert_equal([x for x in i], aview.swapaxes(0, 1).ravel(order='F')) def test_iter_c_or_f_order(): # Test forcing any contiguous (C or F) order # Test the ordering for 1-D to 5-D shapes for shape in [(5,), (3, 4), (2, 3, 4), (2, 3, 4, 3), (2, 3, 2, 2, 3)]: a = arange(np.prod(shape)) # Test each combination of positive and negative strides for dirs in range(2**len(shape)): dirs_index = [slice(None)]*len(shape) for bit in range(len(shape)): if ((2**bit) & dirs): dirs_index[bit] = slice(None, None, -1) dirs_index = tuple(dirs_index) aview = a.reshape(shape)[dirs_index] # C-order i = nditer(aview, order='A') assert_equal([x for x in i], aview.ravel(order='A')) # Fortran-order i = nditer(aview.T, order='A') assert_equal([x for x in i], aview.T.ravel(order='A')) # Other order if len(shape) > 2: i = nditer(aview.swapaxes(0, 1), order='A') assert_equal([x for x in i], aview.swapaxes(0, 1).ravel(order='A')) def test_iter_best_order_multi_index_1d(): # The multi-indices should be correct with any reordering a = arange(4) # 1D order i = nditer(a, ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(0,), (1,), (2,), (3,)]) # 1D reversed order i = nditer(a[::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(3,), (2,), (1,), (0,)]) def test_iter_best_order_multi_index_2d(): # The multi-indices should be correct with any reordering a = arange(6) # 2D C-order i = nditer(a.reshape(2, 3), ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]) # 2D Fortran-order i = nditer(a.reshape(2, 3).copy(order='F'), ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2)]) # 2D reversed C-order i = nditer(a.reshape(2, 3)[::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(1, 0), (1, 1), (1, 2), (0, 0), (0, 1), (0, 2)]) i = nditer(a.reshape(2, 3)[:, ::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(0, 2), (0, 1), (0, 0), (1, 2), (1, 1), (1, 0)]) i = nditer(a.reshape(2, 3)[::-1, ::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(1, 2), (1, 1), (1, 0), (0, 2), (0, 1), (0, 0)]) # 2D reversed Fortran-order i = nditer(a.reshape(2, 3).copy(order='F')[::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(1, 0), (0, 0), (1, 1), (0, 1), (1, 2), (0, 2)]) i = nditer(a.reshape(2, 3).copy(order='F')[:, ::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(0, 2), (1, 2), (0, 1), (1, 1), (0, 0), (1, 0)]) i = nditer(a.reshape(2, 3).copy(order='F')[::-1, ::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(1, 2), (0, 2), (1, 1), (0, 1), (1, 0), (0, 0)]) def test_iter_best_order_multi_index_3d(): # The multi-indices should be correct with any reordering a = arange(12) # 3D C-order i = nditer(a.reshape(2, 3, 2), ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (0, 2, 0), (0, 2, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (1, 2, 0), (1, 2, 1)]) # 3D Fortran-order i = nditer(a.reshape(2, 3, 2).copy(order='F'), ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(0, 0, 0), (1, 0, 0), (0, 1, 0), (1, 1, 0), (0, 2, 0), (1, 2, 0), (0, 0, 1), (1, 0, 1), (0, 1, 1), (1, 1, 1), (0, 2, 1), (1, 2, 1)]) # 3D reversed C-order i = nditer(a.reshape(2, 3, 2)[::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (1, 2, 0), (1, 2, 1), (0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (0, 2, 0), (0, 2, 1)]) i = nditer(a.reshape(2, 3, 2)[:, ::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(0, 2, 0), (0, 2, 1), (0, 1, 0), (0, 1, 1), (0, 0, 0), (0, 0, 1), (1, 2, 0), (1, 2, 1), (1, 1, 0), (1, 1, 1), (1, 0, 0), (1, 0, 1)]) i = nditer(a.reshape(2, 3, 2)[:,:, ::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(0, 0, 1), (0, 0, 0), (0, 1, 1), (0, 1, 0), (0, 2, 1), (0, 2, 0), (1, 0, 1), (1, 0, 0), (1, 1, 1), (1, 1, 0), (1, 2, 1), (1, 2, 0)]) # 3D reversed Fortran-order i = nditer(a.reshape(2, 3, 2).copy(order='F')[::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(1, 0, 0), (0, 0, 0), (1, 1, 0), (0, 1, 0), (1, 2, 0), (0, 2, 0), (1, 0, 1), (0, 0, 1), (1, 1, 1), (0, 1, 1), (1, 2, 1), (0, 2, 1)]) i = nditer(a.reshape(2, 3, 2).copy(order='F')[:, ::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(0, 2, 0), (1, 2, 0), (0, 1, 0), (1, 1, 0), (0, 0, 0), (1, 0, 0), (0, 2, 1), (1, 2, 1), (0, 1, 1), (1, 1, 1), (0, 0, 1), (1, 0, 1)]) i = nditer(a.reshape(2, 3, 2).copy(order='F')[:,:, ::-1], ['multi_index'], [['readonly']]) assert_equal(iter_multi_index(i), [(0, 0, 1), (1, 0, 1), (0, 1, 1), (1, 1, 1), (0, 2, 1), (1, 2, 1), (0, 0, 0), (1, 0, 0), (0, 1, 0), (1, 1, 0), (0, 2, 0), (1, 2, 0)]) def test_iter_best_order_c_index_1d(): # The C index should be correct with any reordering a = arange(4) # 1D order i = nditer(a, ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [0, 1, 2, 3]) # 1D reversed order i = nditer(a[::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [3, 2, 1, 0]) def test_iter_best_order_c_index_2d(): # The C index should be correct with any reordering a = arange(6) # 2D C-order i = nditer(a.reshape(2, 3), ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [0, 1, 2, 3, 4, 5]) # 2D Fortran-order i = nditer(a.reshape(2, 3).copy(order='F'), ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [0, 3, 1, 4, 2, 5]) # 2D reversed C-order i = nditer(a.reshape(2, 3)[::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [3, 4, 5, 0, 1, 2]) i = nditer(a.reshape(2, 3)[:, ::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [2, 1, 0, 5, 4, 3]) i = nditer(a.reshape(2, 3)[::-1, ::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [5, 4, 3, 2, 1, 0]) # 2D reversed Fortran-order i = nditer(a.reshape(2, 3).copy(order='F')[::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [3, 0, 4, 1, 5, 2]) i = nditer(a.reshape(2, 3).copy(order='F')[:, ::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [2, 5, 1, 4, 0, 3]) i = nditer(a.reshape(2, 3).copy(order='F')[::-1, ::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [5, 2, 4, 1, 3, 0]) def test_iter_best_order_c_index_3d(): # The C index should be correct with any reordering a = arange(12) # 3D C-order i = nditer(a.reshape(2, 3, 2), ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) # 3D Fortran-order i = nditer(a.reshape(2, 3, 2).copy(order='F'), ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [0, 6, 2, 8, 4, 10, 1, 7, 3, 9, 5, 11]) # 3D reversed C-order i = nditer(a.reshape(2, 3, 2)[::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5]) i = nditer(a.reshape(2, 3, 2)[:, ::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [4, 5, 2, 3, 0, 1, 10, 11, 8, 9, 6, 7]) i = nditer(a.reshape(2, 3, 2)[:,:, ::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10]) # 3D reversed Fortran-order i = nditer(a.reshape(2, 3, 2).copy(order='F')[::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [6, 0, 8, 2, 10, 4, 7, 1, 9, 3, 11, 5]) i = nditer(a.reshape(2, 3, 2).copy(order='F')[:, ::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [4, 10, 2, 8, 0, 6, 5, 11, 3, 9, 1, 7]) i = nditer(a.reshape(2, 3, 2).copy(order='F')[:,:, ::-1], ['c_index'], [['readonly']]) assert_equal(iter_indices(i), [1, 7, 3, 9, 5, 11, 0, 6, 2, 8, 4, 10]) def test_iter_best_order_f_index_1d(): # The Fortran index should be correct with any reordering a = arange(4) # 1D order i = nditer(a, ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [0, 1, 2, 3]) # 1D reversed order i = nditer(a[::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [3, 2, 1, 0]) def test_iter_best_order_f_index_2d(): # The Fortran index should be correct with any reordering a = arange(6) # 2D C-order i = nditer(a.reshape(2, 3), ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [0, 2, 4, 1, 3, 5]) # 2D Fortran-order i = nditer(a.reshape(2, 3).copy(order='F'), ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [0, 1, 2, 3, 4, 5]) # 2D reversed C-order i = nditer(a.reshape(2, 3)[::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [1, 3, 5, 0, 2, 4]) i = nditer(a.reshape(2, 3)[:, ::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [4, 2, 0, 5, 3, 1]) i = nditer(a.reshape(2, 3)[::-1, ::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [5, 3, 1, 4, 2, 0]) # 2D reversed Fortran-order i = nditer(a.reshape(2, 3).copy(order='F')[::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [1, 0, 3, 2, 5, 4]) i = nditer(a.reshape(2, 3).copy(order='F')[:, ::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [4, 5, 2, 3, 0, 1]) i = nditer(a.reshape(2, 3).copy(order='F')[::-1, ::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [5, 4, 3, 2, 1, 0]) def test_iter_best_order_f_index_3d(): # The Fortran index should be correct with any reordering a = arange(12) # 3D C-order i = nditer(a.reshape(2, 3, 2), ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [0, 6, 2, 8, 4, 10, 1, 7, 3, 9, 5, 11]) # 3D Fortran-order i = nditer(a.reshape(2, 3, 2).copy(order='F'), ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) # 3D reversed C-order i = nditer(a.reshape(2, 3, 2)[::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [1, 7, 3, 9, 5, 11, 0, 6, 2, 8, 4, 10]) i = nditer(a.reshape(2, 3, 2)[:, ::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [4, 10, 2, 8, 0, 6, 5, 11, 3, 9, 1, 7]) i = nditer(a.reshape(2, 3, 2)[:,:, ::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [6, 0, 8, 2, 10, 4, 7, 1, 9, 3, 11, 5]) # 3D reversed Fortran-order i = nditer(a.reshape(2, 3, 2).copy(order='F')[::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10]) i = nditer(a.reshape(2, 3, 2).copy(order='F')[:, ::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [4, 5, 2, 3, 0, 1, 10, 11, 8, 9, 6, 7]) i = nditer(a.reshape(2, 3, 2).copy(order='F')[:,:, ::-1], ['f_index'], [['readonly']]) assert_equal(iter_indices(i), [6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5]) def test_iter_no_inner_full_coalesce(): # Check no_inner iterators which coalesce into a single inner loop for shape in [(5,), (3, 4), (2, 3, 4), (2, 3, 4, 3), (2, 3, 2, 2, 3)]: size = np.prod(shape) a = arange(size) # Test each combination of forward and backwards indexing for dirs in range(2**len(shape)): dirs_index = [slice(None)]*len(shape) for bit in range(len(shape)): if ((2**bit) & dirs): dirs_index[bit] = slice(None, None, -1) dirs_index = tuple(dirs_index) aview = a.reshape(shape)[dirs_index] # C-order i = nditer(aview, ['external_loop'], [['readonly']]) assert_equal(i.ndim, 1) assert_equal(i[0].shape, (size,)) # Fortran-order i = nditer(aview.T, ['external_loop'], [['readonly']]) assert_equal(i.ndim, 1) assert_equal(i[0].shape, (size,)) # Other order if len(shape) > 2: i = nditer(aview.swapaxes(0, 1), ['external_loop'], [['readonly']]) assert_equal(i.ndim, 1) assert_equal(i[0].shape, (size,)) def test_iter_no_inner_dim_coalescing(): # Check no_inner iterators whose dimensions may not coalesce completely # Skipping the last element in a dimension prevents coalescing # with the next-bigger dimension a = arange(24).reshape(2, 3, 4)[:,:, :-1] i = nditer(a, ['external_loop'], [['readonly']]) assert_equal(i.ndim, 2) assert_equal(i[0].shape, (3,)) a = arange(24).reshape(2, 3, 4)[:, :-1,:] i = nditer(a, ['external_loop'], [['readonly']]) assert_equal(i.ndim, 2) assert_equal(i[0].shape, (8,)) a = arange(24).reshape(2, 3, 4)[:-1,:,:] i = nditer(a, ['external_loop'], [['readonly']]) assert_equal(i.ndim, 1) assert_equal(i[0].shape, (12,)) # Even with lots of 1-sized dimensions, should still coalesce a = arange(24).reshape(1, 1, 2, 1, 1, 3, 1, 1, 4, 1, 1) i = nditer(a, ['external_loop'], [['readonly']]) assert_equal(i.ndim, 1) assert_equal(i[0].shape, (24,)) def test_iter_dim_coalescing(): # Check that the correct number of dimensions are coalesced # Tracking a multi-index disables coalescing a = arange(24).reshape(2, 3, 4) i = nditer(a, ['multi_index'], [['readonly']]) assert_equal(i.ndim, 3) # A tracked index can allow coalescing if it's compatible with the array a3d = arange(24).reshape(2, 3, 4) i = nditer(a3d, ['c_index'], [['readonly']]) assert_equal(i.ndim, 1) i = nditer(a3d.swapaxes(0, 1), ['c_index'], [['readonly']]) assert_equal(i.ndim, 3) i = nditer(a3d.T, ['c_index'], [['readonly']]) assert_equal(i.ndim, 3) i = nditer(a3d.T, ['f_index'], [['readonly']]) assert_equal(i.ndim, 1) i = nditer(a3d.T.swapaxes(0, 1), ['f_index'], [['readonly']]) assert_equal(i.ndim, 3) # When C or F order is forced, coalescing may still occur a3d = arange(24).reshape(2, 3, 4) i = nditer(a3d, order='C') assert_equal(i.ndim, 1) i = nditer(a3d.T, order='C') assert_equal(i.ndim, 3) i = nditer(a3d, order='F') assert_equal(i.ndim, 3) i = nditer(a3d.T, order='F') assert_equal(i.ndim, 1) i = nditer(a3d, order='A') assert_equal(i.ndim, 1) i = nditer(a3d.T, order='A') assert_equal(i.ndim, 1) def test_iter_broadcasting(): # Standard NumPy broadcasting rules # 1D with scalar i = nditer([arange(6), np.int32(2)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 6) assert_equal(i.shape, (6,)) # 2D with scalar i = nditer([arange(6).reshape(2, 3), np.int32(2)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 6) assert_equal(i.shape, (2, 3)) # 2D with 1D i = nditer([arange(6).reshape(2, 3), arange(3)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 6) assert_equal(i.shape, (2, 3)) i = nditer([arange(2).reshape(2, 1), arange(3)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 6) assert_equal(i.shape, (2, 3)) # 2D with 2D i = nditer([arange(2).reshape(2, 1), arange(3).reshape(1, 3)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 6) assert_equal(i.shape, (2, 3)) # 3D with scalar i = nditer([np.int32(2), arange(24).reshape(4, 2, 3)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 24) assert_equal(i.shape, (4, 2, 3)) # 3D with 1D i = nditer([arange(3), arange(24).reshape(4, 2, 3)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 24) assert_equal(i.shape, (4, 2, 3)) i = nditer([arange(3), arange(8).reshape(4, 2, 1)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 24) assert_equal(i.shape, (4, 2, 3)) # 3D with 2D i = nditer([arange(6).reshape(2, 3), arange(24).reshape(4, 2, 3)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 24) assert_equal(i.shape, (4, 2, 3)) i = nditer([arange(2).reshape(2, 1), arange(24).reshape(4, 2, 3)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 24) assert_equal(i.shape, (4, 2, 3)) i = nditer([arange(3).reshape(1, 3), arange(8).reshape(4, 2, 1)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 24) assert_equal(i.shape, (4, 2, 3)) # 3D with 3D i = nditer([arange(2).reshape(1, 2, 1), arange(3).reshape(1, 1, 3), arange(4).reshape(4, 1, 1)], ['multi_index'], [['readonly']]*3) assert_equal(i.itersize, 24) assert_equal(i.shape, (4, 2, 3)) i = nditer([arange(6).reshape(1, 2, 3), arange(4).reshape(4, 1, 1)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 24) assert_equal(i.shape, (4, 2, 3)) i = nditer([arange(24).reshape(4, 2, 3), arange(12).reshape(4, 1, 3)], ['multi_index'], [['readonly']]*2) assert_equal(i.itersize, 24) assert_equal(i.shape, (4, 2, 3)) def test_iter_itershape(): # Check that allocated outputs work with a specified shape a = np.arange(6, dtype='i2').reshape(2, 3) i = nditer([a, None], [], [['readonly'], ['writeonly', 'allocate']], op_axes=[[0, 1, None], None], itershape=(-1, -1, 4)) assert_equal(i.operands[1].shape, (2, 3, 4)) assert_equal(i.operands[1].strides, (24, 8, 2)) i = nditer([a.T, None], [], [['readonly'], ['writeonly', 'allocate']], op_axes=[[0, 1, None], None], itershape=(-1, -1, 4)) assert_equal(i.operands[1].shape, (3, 2, 4)) assert_equal(i.operands[1].strides, (8, 24, 2)) i = nditer([a.T, None], [], [['readonly'], ['writeonly', 'allocate']], order='F', op_axes=[[0, 1, None], None], itershape=(-1, -1, 4)) assert_equal(i.operands[1].shape, (3, 2, 4)) assert_equal(i.operands[1].strides, (2, 6, 12)) # If we specify 1 in the itershape, it shouldn't allow broadcasting # of that dimension to a bigger value assert_raises(ValueError, nditer, [a, None], [], [['readonly'], ['writeonly', 'allocate']], op_axes=[[0, 1, None], None], itershape=(-1, 1, 4)) # Test bug that for no op_axes but itershape, they are NULLed correctly i = np.nditer([np.ones(2), None, None], itershape=(2,)) def test_iter_broadcasting_errors(): # Check that errors are thrown for bad broadcasting shapes # 1D with 1D assert_raises(ValueError, nditer, [arange(2), arange(3)], [], [['readonly']]*2) # 2D with 1D assert_raises(ValueError, nditer, [arange(6).reshape(2, 3), arange(2)], [], [['readonly']]*2) # 2D with 2D assert_raises(ValueError, nditer, [arange(6).reshape(2, 3), arange(9).reshape(3, 3)], [], [['readonly']]*2) assert_raises(ValueError, nditer, [arange(6).reshape(2, 3), arange(4).reshape(2, 2)], [], [['readonly']]*2) # 3D with 3D assert_raises(ValueError, nditer, [arange(36).reshape(3, 3, 4), arange(24).reshape(2, 3, 4)], [], [['readonly']]*2) assert_raises(ValueError, nditer, [arange(8).reshape(2, 4, 1), arange(24).reshape(2, 3, 4)], [], [['readonly']]*2) # Verify that the error message mentions the right shapes try: nditer([arange(2).reshape(1, 2, 1), arange(3).reshape(1, 3), arange(6).reshape(2, 3)], [], [['readonly'], ['readonly'], ['writeonly', 'no_broadcast']]) raise AssertionError('Should have raised a broadcast error') except ValueError as e: msg = str(e) # The message should contain the shape of the 3rd operand assert_(msg.find('(2,3)') >= 0, 'Message "%s" doesn\'t contain operand shape (2,3)' % msg) # The message should contain the broadcast shape assert_(msg.find('(1,2,3)') >= 0, 'Message "%s" doesn\'t contain broadcast shape (1,2,3)' % msg) try: nditer([arange(6).reshape(2, 3), arange(2)], [], [['readonly'], ['readonly']], op_axes=[[0, 1], [0, np.newaxis]], itershape=(4, 3)) raise AssertionError('Should have raised a broadcast error') except ValueError as e: msg = str(e) # The message should contain "shape->remappedshape" for each operand assert_(msg.find('(2,3)->(2,3)') >= 0, 'Message "%s" doesn\'t contain operand shape (2,3)->(2,3)' % msg) assert_(msg.find('(2,)->(2,newaxis)') >= 0, ('Message "%s" doesn\'t contain remapped operand shape' + '(2,)->(2,newaxis)') % msg) # The message should contain the itershape parameter assert_(msg.find('(4,3)') >= 0, 'Message "%s" doesn\'t contain itershape parameter (4,3)' % msg) try: nditer([np.zeros((2, 1, 1)), np.zeros((2,))], [], [['writeonly', 'no_broadcast'], ['readonly']]) raise AssertionError('Should have raised a broadcast error') except ValueError as e: msg = str(e) # The message should contain the shape of the bad operand assert_(msg.find('(2,1,1)') >= 0, 'Message "%s" doesn\'t contain operand shape (2,1,1)' % msg) # The message should contain the broadcast shape assert_(msg.find('(2,1,2)') >= 0, 'Message "%s" doesn\'t contain the broadcast shape (2,1,2)' % msg) def test_iter_flags_errors(): # Check that bad combinations of flags produce errors a = arange(6) # Not enough operands assert_raises(ValueError, nditer, [], [], []) # Too many operands assert_raises(ValueError, nditer, [a]*100, [], [['readonly']]*100) # Bad global flag assert_raises(ValueError, nditer, [a], ['bad flag'], [['readonly']]) # Bad op flag assert_raises(ValueError, nditer, [a], [], [['readonly', 'bad flag']]) # Bad order parameter assert_raises(ValueError, nditer, [a], [], [['readonly']], order='G') # Bad casting parameter assert_raises(ValueError, nditer, [a], [], [['readonly']], casting='noon') # op_flags must match ops assert_raises(ValueError, nditer, [a]*3, [], [['readonly']]*2) # Cannot track both a C and an F index assert_raises(ValueError, nditer, a, ['c_index', 'f_index'], [['readonly']]) # Inner iteration and multi-indices/indices are incompatible assert_raises(ValueError, nditer, a, ['external_loop', 'multi_index'], [['readonly']]) assert_raises(ValueError, nditer, a, ['external_loop', 'c_index'], [['readonly']]) assert_raises(ValueError, nditer, a, ['external_loop', 'f_index'], [['readonly']]) # Must specify exactly one of readwrite/readonly/writeonly per operand assert_raises(ValueError, nditer, a, [], [[]]) assert_raises(ValueError, nditer, a, [], [['readonly', 'writeonly']]) assert_raises(ValueError, nditer, a, [], [['readonly', 'readwrite']]) assert_raises(ValueError, nditer, a, [], [['writeonly', 'readwrite']]) assert_raises(ValueError, nditer, a, [], [['readonly', 'writeonly', 'readwrite']]) # Python scalars are always readonly assert_raises(TypeError, nditer, 1.5, [], [['writeonly']]) assert_raises(TypeError, nditer, 1.5, [], [['readwrite']]) # Array scalars are always readonly assert_raises(TypeError, nditer, np.int32(1), [], [['writeonly']]) assert_raises(TypeError, nditer, np.int32(1), [], [['readwrite']]) # Check readonly array a.flags.writeable = False assert_raises(ValueError, nditer, a, [], [['writeonly']]) assert_raises(ValueError, nditer, a, [], [['readwrite']]) a.flags.writeable = True # Multi-indices available only with the multi_index flag i = nditer(arange(6), [], [['readonly']]) assert_raises(ValueError, lambda i:i.multi_index, i) # Index available only with an index flag assert_raises(ValueError, lambda i:i.index, i) # GotoCoords and GotoIndex incompatible with buffering or no_inner def assign_multi_index(i): i.multi_index = (0,) def assign_index(i): i.index = 0 def assign_iterindex(i): i.iterindex = 0 def assign_iterrange(i): i.iterrange = (0, 1) i = nditer(arange(6), ['external_loop']) assert_raises(ValueError, assign_multi_index, i) assert_raises(ValueError, assign_index, i) assert_raises(ValueError, assign_iterindex, i) assert_raises(ValueError, assign_iterrange, i) i = nditer(arange(6), ['buffered']) assert_raises(ValueError, assign_multi_index, i) assert_raises(ValueError, assign_index, i) assert_raises(ValueError, assign_iterrange, i) # Can't iterate if size is zero assert_raises(ValueError, nditer, np.array([])) def test_iter_slice(): a, b, c = np.arange(3), np.arange(3), np.arange(3.) i = nditer([a, b, c], [], ['readwrite']) with i: i[0:2] = (3, 3) assert_equal(a, [3, 1, 2]) assert_equal(b, [3, 1, 2]) assert_equal(c, [0, 1, 2]) i[1] = 12 assert_equal(i[0:2], [3, 12]) def test_iter_assign_mapping(): a = np.arange(24, dtype='f8').reshape(2, 3, 4).T it = np.nditer(a, [], [['readwrite', 'updateifcopy']], casting='same_kind', op_dtypes=[np.dtype('f4')]) with it: it.operands[0][...] = 3 it.operands[0][...] = 14 assert_equal(a, 14) it = np.nditer(a, [], [['readwrite', 'updateifcopy']], casting='same_kind', op_dtypes=[np.dtype('f4')]) with it: x = it.operands[0][-1:1] x[...] = 14 it.operands[0][...] = -1234 assert_equal(a, -1234) # check for no warnings on dealloc x = None it = None def test_iter_nbo_align_contig(): # Check that byte order, alignment, and contig changes work # Byte order change by requesting a specific dtype a = np.arange(6, dtype='f4') au = a.byteswap().newbyteorder() assert_(a.dtype.byteorder != au.dtype.byteorder) i = nditer(au, [], [['readwrite', 'updateifcopy']], casting='equiv', op_dtypes=[np.dtype('f4')]) with i: # context manager triggers UPDATEIFCOPY on i at exit assert_equal(i.dtypes[0].byteorder, a.dtype.byteorder) assert_equal(i.operands[0].dtype.byteorder, a.dtype.byteorder) assert_equal(i.operands[0], a) i.operands[0][:] = 2 assert_equal(au, [2]*6) del i # should not raise a warning # Byte order change by requesting NBO a = np.arange(6, dtype='f4') au = a.byteswap().newbyteorder() assert_(a.dtype.byteorder != au.dtype.byteorder) with nditer(au, [], [['readwrite', 'updateifcopy', 'nbo']], casting='equiv') as i: # context manager triggers UPDATEIFCOPY on i at exit assert_equal(i.dtypes[0].byteorder, a.dtype.byteorder) assert_equal(i.operands[0].dtype.byteorder, a.dtype.byteorder) assert_equal(i.operands[0], a) i.operands[0][:] = 12345 i.operands[0][:] = 2 assert_equal(au, [2]*6) # Unaligned input a = np.zeros((6*4+1,), dtype='i1')[1:] a.dtype = 'f4' a[:] = np.arange(6, dtype='f4') assert_(not a.flags.aligned) # Without 'aligned', shouldn't copy i = nditer(a, [], [['readonly']]) assert_(not i.operands[0].flags.aligned) assert_equal(i.operands[0], a) # With 'aligned', should make a copy with nditer(a, [], [['readwrite', 'updateifcopy', 'aligned']]) as i: assert_(i.operands[0].flags.aligned) # context manager triggers UPDATEIFCOPY on i at exit assert_equal(i.operands[0], a) i.operands[0][:] = 3 assert_equal(a, [3]*6) # Discontiguous input a = arange(12) # If it is contiguous, shouldn't copy i = nditer(a[:6], [], [['readonly']]) assert_(i.operands[0].flags.contiguous) assert_equal(i.operands[0], a[:6]) # If it isn't contiguous, should buffer i = nditer(a[::2], ['buffered', 'external_loop'], [['readonly', 'contig']], buffersize=10) assert_(i[0].flags.contiguous) assert_equal(i[0], a[::2]) def test_iter_array_cast(): # Check that arrays are cast as requested # No cast 'f4' -> 'f4' a = np.arange(6, dtype='f4').reshape(2, 3) i = nditer(a, [], [['readwrite']], op_dtypes=[np.dtype('f4')]) with i: assert_equal(i.operands[0], a) assert_equal(i.operands[0].dtype, np.dtype('f4')) # Byte-order cast '<f4' -> '>f4' a = np.arange(6, dtype='<f4').reshape(2, 3) with nditer(a, [], [['readwrite', 'updateifcopy']], casting='equiv', op_dtypes=[np.dtype('>f4')]) as i: assert_equal(i.operands[0], a) assert_equal(i.operands[0].dtype, np.dtype('>f4')) # Safe case 'f4' -> 'f8' a = np.arange(24, dtype='f4').reshape(2, 3, 4).swapaxes(1, 2) i = nditer(a, [], [['readonly', 'copy']], casting='safe', op_dtypes=[np.dtype('f8')]) assert_equal(i.operands[0], a) assert_equal(i.operands[0].dtype, np.dtype('f8')) # The memory layout of the temporary should match a (a is (48,4,16)) # except negative strides get flipped to positive strides. assert_equal(i.operands[0].strides, (96, 8, 32)) a = a[::-1,:, ::-1] i = nditer(a, [], [['readonly', 'copy']], casting='safe', op_dtypes=[np.dtype('f8')]) assert_equal(i.operands[0], a) assert_equal(i.operands[0].dtype, np.dtype('f8')) assert_equal(i.operands[0].strides, (96, 8, 32)) # Same-kind cast 'f8' -> 'f4' -> 'f8' a = np.arange(24, dtype='f8').reshape(2, 3, 4).T with nditer(a, [], [['readwrite', 'updateifcopy']], casting='same_kind', op_dtypes=[np.dtype('f4')]) as i: assert_equal(i.operands[0], a) assert_equal(i.operands[0].dtype, np.dtype('f4')) assert_equal(i.operands[0].strides, (4, 16, 48)) # Check that WRITEBACKIFCOPY is activated at exit i.operands[0][2, 1, 1] = -12.5 assert_(a[2, 1, 1] != -12.5) assert_equal(a[2, 1, 1], -12.5) a = np.arange(6, dtype='i4')[::-2] with nditer(a, [], [['writeonly', 'updateifcopy']], casting='unsafe', op_dtypes=[np.dtype('f4')]) as i: assert_equal(i.operands[0].dtype, np.dtype('f4')) # Even though the stride was negative in 'a', it # becomes positive in the temporary assert_equal(i.operands[0].strides, (4,)) i.operands[0][:] = [1, 2, 3] assert_equal(a, [1, 2, 3]) def test_iter_array_cast_errors(): # Check that invalid casts are caught # Need to enable copying for casts to occur assert_raises(TypeError, nditer, arange(2, dtype='f4'), [], [['readonly']], op_dtypes=[np.dtype('f8')]) # Also need to allow casting for casts to occur assert_raises(TypeError, nditer, arange(2, dtype='f4'), [], [['readonly', 'copy']], casting='no', op_dtypes=[np.dtype('f8')]) assert_raises(TypeError, nditer, arange(2, dtype='f4'), [], [['readonly', 'copy']], casting='equiv', op_dtypes=[np.dtype('f8')]) assert_raises(TypeError, nditer, arange(2, dtype='f8'), [], [['writeonly', 'updateifcopy']], casting='no', op_dtypes=[np.dtype('f4')]) assert_raises(TypeError, nditer, arange(2, dtype='f8'), [], [['writeonly', 'updateifcopy']], casting='equiv', op_dtypes=[np.dtype('f4')]) # '<f4' -> '>f4' should not work with casting='no' assert_raises(TypeError, nditer, arange(2, dtype='<f4'), [], [['readonly', 'copy']], casting='no', op_dtypes=[np.dtype('>f4')]) # 'f4' -> 'f8' is a safe cast, but 'f8' -> 'f4' isn't assert_raises(TypeError, nditer, arange(2, dtype='f4'), [], [['readwrite', 'updateifcopy']], casting='safe', op_dtypes=[np.dtype('f8')]) assert_raises(TypeError, nditer, arange(2, dtype='f8'), [], [['readwrite', 'updateifcopy']], casting='safe', op_dtypes=[np.dtype('f4')]) # 'f4' -> 'i4' is neither a safe nor a same-kind cast assert_raises(TypeError, nditer, arange(2, dtype='f4'), [], [['readonly', 'copy']], casting='same_kind', op_dtypes=[np.dtype('i4')]) assert_raises(TypeError, nditer, arange(2, dtype='i4'), [], [['writeonly', 'updateifcopy']], casting='same_kind', op_dtypes=[np.dtype('f4')]) def test_iter_scalar_cast(): # Check that scalars are cast as requested # No cast 'f4' -> 'f4' i = nditer(np.float32(2.5), [], [['readonly']], op_dtypes=[np.dtype('f4')]) assert_equal(i.dtypes[0], np.dtype('f4')) assert_equal(i.value.dtype, np.dtype('f4')) assert_equal(i.value, 2.5) # Safe cast 'f4' -> 'f8' i = nditer(np.float32(2.5), [], [['readonly', 'copy']], casting='safe', op_dtypes=[np.dtype('f8')]) assert_equal(i.dtypes[0], np.dtype('f8')) assert_equal(i.value.dtype, np.dtype('f8')) assert_equal(i.value, 2.5) # Same-kind cast 'f8' -> 'f4' i = nditer(np.float64(2.5), [], [['readonly', 'copy']], casting='same_kind', op_dtypes=[np.dtype('f4')]) assert_equal(i.dtypes[0], np.dtype('f4')) assert_equal(i.value.dtype, np.dtype('f4')) assert_equal(i.value, 2.5) # Unsafe cast 'f8' -> 'i4' i = nditer(np.float64(3.0), [], [['readonly', 'copy']], casting='unsafe', op_dtypes=[np.dtype('i4')]) assert_equal(i.dtypes[0], np.dtype('i4')) assert_equal(i.value.dtype, np.dtype('i4')) assert_equal(i.value, 3) # Readonly scalars may be cast even without setting COPY or BUFFERED i = nditer(3, [], [['readonly']], op_dtypes=[np.dtype('f8')]) assert_equal(i[0].dtype, np.dtype('f8')) assert_equal(i[0], 3.) def test_iter_scalar_cast_errors(): # Check that invalid casts are caught # Need to allow copying/buffering for write casts of scalars to occur assert_raises(TypeError, nditer, np.float32(2), [], [['readwrite']], op_dtypes=[np.dtype('f8')]) assert_raises(TypeError, nditer, 2.5, [], [['readwrite']], op_dtypes=[np.dtype('f4')]) # 'f8' -> 'f4' isn't a safe cast if the value would overflow assert_raises(TypeError, nditer, np.float64(1e60), [], [['readonly']], casting='safe', op_dtypes=[np.dtype('f4')]) # 'f4' -> 'i4' is neither a safe nor a same-kind cast assert_raises(TypeError, nditer, np.float32(2), [], [['readonly']], casting='same_kind', op_dtypes=[np.dtype('i4')]) def test_iter_object_arrays_basic(): # Check that object arrays work obj = {'a':3,'b':'d'} a = np.array([[1, 2, 3], None, obj, None], dtype='O') if HAS_REFCOUNT: rc = sys.getrefcount(obj) # Need to allow references for object arrays assert_raises(TypeError, nditer, a) if HAS_REFCOUNT: assert_equal(sys.getrefcount(obj), rc) i = nditer(a, ['refs_ok'], ['readonly']) vals = [x_[()] for x_ in i] assert_equal(np.array(vals, dtype='O'), a) vals, i, x = [None]*3 if HAS_REFCOUNT: assert_equal(sys.getrefcount(obj), rc) i = nditer(a.reshape(2, 2).T, ['refs_ok', 'buffered'], ['readonly'], order='C') assert_(i.iterationneedsapi) vals = [x_[()] for x_ in i] assert_equal(np.array(vals, dtype='O'), a.reshape(2, 2).ravel(order='F')) vals, i, x = [None]*3 if HAS_REFCOUNT: assert_equal(sys.getrefcount(obj), rc) i = nditer(a.reshape(2, 2).T, ['refs_ok', 'buffered'], ['readwrite'], order='C') with i: for x in i: x[...] = None vals, i, x = [None]*3 if HAS_REFCOUNT: assert_(sys.getrefcount(obj) == rc-1) assert_equal(a, np.array([None]*4, dtype='O')) def test_iter_object_arrays_conversions(): # Conversions to/from objects a = np.arange(6, dtype='O') i = nditer(a, ['refs_ok', 'buffered'], ['readwrite'], casting='unsafe', op_dtypes='i4') with i: for x in i: x[...] += 1 assert_equal(a, np.arange(6)+1) a = np.arange(6, dtype='i4') i = nditer(a, ['refs_ok', 'buffered'], ['readwrite'], casting='unsafe', op_dtypes='O') with i: for x in i: x[...] += 1 assert_equal(a, np.arange(6)+1) # Non-contiguous object array a = np.zeros((6,), dtype=[('p', 'i1'), ('a', 'O')]) a = a['a'] a[:] = np.arange(6) i = nditer(a, ['refs_ok', 'buffered'], ['readwrite'], casting='unsafe', op_dtypes='i4') with i: for x in i: x[...] += 1 assert_equal(a, np.arange(6)+1) #Non-contiguous value array a = np.zeros((6,), dtype=[('p', 'i1'), ('a', 'i4')]) a = a['a'] a[:] = np.arange(6) + 98172488 i = nditer(a, ['refs_ok', 'buffered'], ['readwrite'], casting='unsafe', op_dtypes='O') with i: ob = i[0][()] if HAS_REFCOUNT: rc = sys.getrefcount(ob) for x in i: x[...] += 1 if HAS_REFCOUNT: assert_(sys.getrefcount(ob) == rc-1) assert_equal(a, np.arange(6)+98172489) def test_iter_common_dtype(): # Check that the iterator finds a common data type correctly i = nditer([array([3], dtype='f4'), array([0], dtype='f8')], ['common_dtype'], [['readonly', 'copy']]*2, casting='safe') assert_equal(i.dtypes[0], np.dtype('f8')) assert_equal(i.dtypes[1], np.dtype('f8')) i = nditer([array([3], dtype='i4'), array([0], dtype='f4')], ['common_dtype'], [['readonly', 'copy']]*2, casting='safe') assert_equal(i.dtypes[0], np.dtype('f8')) assert_equal(i.dtypes[1], np.dtype('f8')) i = nditer([array([3], dtype='f4'), array(0, dtype='f8')], ['common_dtype'], [['readonly', 'copy']]*2, casting='same_kind') assert_equal(i.dtypes[0], np.dtype('f4')) assert_equal(i.dtypes[1], np.dtype('f4')) i = nditer([array([3], dtype='u4'), array(0, dtype='i4')], ['common_dtype'], [['readonly', 'copy']]*2, casting='safe') assert_equal(i.dtypes[0], np.dtype('u4')) assert_equal(i.dtypes[1], np.dtype('u4')) i = nditer([array([3], dtype='u4'), array(-12, dtype='i4')], ['common_dtype'], [['readonly', 'copy']]*2, casting='safe') assert_equal(i.dtypes[0], np.dtype('i8')) assert_equal(i.dtypes[1], np.dtype('i8')) i = nditer([array([3], dtype='u4'), array(-12, dtype='i4'), array([2j], dtype='c8'), array([9], dtype='f8')], ['common_dtype'], [['readonly', 'copy']]*4, casting='safe') assert_equal(i.dtypes[0], np.dtype('c16')) assert_equal(i.dtypes[1], np.dtype('c16')) assert_equal(i.dtypes[2], np.dtype('c16')) assert_equal(i.dtypes[3], np.dtype('c16')) assert_equal(i.value, (3, -12, 2j, 9)) # When allocating outputs, other outputs aren't factored in i = nditer([array([3], dtype='i4'), None, array([2j], dtype='c16')], [], [['readonly', 'copy'], ['writeonly', 'allocate'], ['writeonly']], casting='safe') assert_equal(i.dtypes[0], np.dtype('i4')) assert_equal(i.dtypes[1], np.dtype('i4')) assert_equal(i.dtypes[2], np.dtype('c16')) # But, if common data types are requested, they are i = nditer([array([3], dtype='i4'), None, array([2j], dtype='c16')], ['common_dtype'], [['readonly', 'copy'], ['writeonly', 'allocate'], ['writeonly']], casting='safe') assert_equal(i.dtypes[0], np.dtype('c16')) assert_equal(i.dtypes[1], np.dtype('c16')) assert_equal(i.dtypes[2], np.dtype('c16')) def test_iter_copy_if_overlap(): # Ensure the iterator makes copies on read/write overlap, if requested # Copy not needed, 1 op for flag in ['readonly', 'writeonly', 'readwrite']: a = arange(10) i = nditer([a], ['copy_if_overlap'], [[flag]]) with i: assert_(i.operands[0] is a) # Copy needed, 2 ops, read-write overlap x = arange(10) a = x[1:] b = x[:-1] with nditer([a, b], ['copy_if_overlap'], [['readonly'], ['readwrite']]) as i: assert_(not np.shares_memory(*i.operands)) # Copy not needed with elementwise, 2 ops, exactly same arrays x = arange(10) a = x b = x i = nditer([a, b], ['copy_if_overlap'], [['readonly', 'overlap_assume_elementwise'], ['readwrite', 'overlap_assume_elementwise']]) with i: assert_(i.operands[0] is a and i.operands[1] is b) with nditer([a, b], ['copy_if_overlap'], [['readonly'], ['readwrite']]) as i: assert_(i.operands[0] is a and not np.shares_memory(i.operands[1], b)) # Copy not needed, 2 ops, no overlap x = arange(10) a = x[::2] b = x[1::2] i = nditer([a, b], ['copy_if_overlap'], [['readonly'], ['writeonly']]) assert_(i.operands[0] is a and i.operands[1] is b) # Copy needed, 2 ops, read-write overlap x = arange(4, dtype=np.int8) a = x[3:] b = x.view(np.int32)[:1] with nditer([a, b], ['copy_if_overlap'], [['readonly'], ['writeonly']]) as i: assert_(not np.shares_memory(*i.operands)) # Copy needed, 3 ops, read-write overlap for flag in ['writeonly', 'readwrite']: x = np.ones([10, 10]) a = x b = x.T c = x with nditer([a, b, c], ['copy_if_overlap'], [['readonly'], ['readonly'], [flag]]) as i: a2, b2, c2 = i.operands assert_(not np.shares_memory(a2, c2)) assert_(not np.shares_memory(b2, c2)) # Copy not needed, 3 ops, read-only overlap x = np.ones([10, 10]) a = x b = x.T c = x i = nditer([a, b, c], ['copy_if_overlap'], [['readonly'], ['readonly'], ['readonly']]) a2, b2, c2 = i.operands assert_(a is a2) assert_(b is b2) assert_(c is c2) # Copy not needed, 3 ops, read-only overlap x = np.ones([10, 10]) a = x b = np.ones([10, 10]) c = x.T i = nditer([a, b, c], ['copy_if_overlap'], [['readonly'], ['writeonly'], ['readonly']]) a2, b2, c2 = i.operands assert_(a is a2) assert_(b is b2) assert_(c is c2) # Copy not needed, 3 ops, write-only overlap x = np.arange(7) a = x[:3] b = x[3:6] c = x[4:7] i = nditer([a, b, c], ['copy_if_overlap'], [['readonly'], ['writeonly'], ['writeonly']]) a2, b2, c2 = i.operands assert_(a is a2) assert_(b is b2) assert_(c is c2) def test_iter_op_axes(): # Check that custom axes work # Reverse the axes a = arange(6).reshape(2, 3) i = nditer([a, a.T], [], [['readonly']]*2, op_axes=[[0, 1], [1, 0]]) assert_(all([x == y for (x, y) in i])) a = arange(24).reshape(2, 3, 4) i = nditer([a.T, a], [], [['readonly']]*2, op_axes=[[2, 1, 0], None]) assert_(all([x == y for (x, y) in i])) # Broadcast 1D to any dimension a = arange(1, 31).reshape(2, 3, 5) b = arange(1, 3) i = nditer([a, b], [], [['readonly']]*2, op_axes=[None, [0, -1, -1]]) assert_equal([x*y for (x, y) in i], (a*b.reshape(2, 1, 1)).ravel()) b = arange(1, 4) i = nditer([a, b], [], [['readonly']]*2, op_axes=[None, [-1, 0, -1]]) assert_equal([x*y for (x, y) in i], (a*b.reshape(1, 3, 1)).ravel()) b = arange(1, 6) i = nditer([a, b], [], [['readonly']]*2, op_axes=[None, [np.newaxis, np.newaxis, 0]]) assert_equal([x*y for (x, y) in i], (a*b.reshape(1, 1, 5)).ravel()) # Inner product-style broadcasting a = arange(24).reshape(2, 3, 4) b = arange(40).reshape(5, 2, 4) i = nditer([a, b], ['multi_index'], [['readonly']]*2, op_axes=[[0, 1, -1, -1], [-1, -1, 0, 1]]) assert_equal(i.shape, (2, 3, 5, 2)) # Matrix product-style broadcasting a = arange(12).reshape(3, 4) b = arange(20).reshape(4, 5) i = nditer([a, b], ['multi_index'], [['readonly']]*2, op_axes=[[0, -1], [-1, 1]]) assert_equal(i.shape, (3, 5)) def test_iter_op_axes_errors(): # Check that custom axes throws errors for bad inputs # Wrong number of items in op_axes a = arange(6).reshape(2, 3) assert_raises(ValueError, nditer, [a, a], [], [['readonly']]*2, op_axes=[[0], [1], [0]]) # Out of bounds items in op_axes assert_raises(ValueError, nditer, [a, a], [], [['readonly']]*2, op_axes=[[2, 1], [0, 1]]) assert_raises(ValueError, nditer, [a, a], [], [['readonly']]*2, op_axes=[[0, 1], [2, -1]]) # Duplicate items in op_axes assert_raises(ValueError, nditer, [a, a], [], [['readonly']]*2, op_axes=[[0, 0], [0, 1]]) assert_raises(ValueError, nditer, [a, a], [], [['readonly']]*2, op_axes=[[0, 1], [1, 1]]) # Different sized arrays in op_axes assert_raises(ValueError, nditer, [a, a], [], [['readonly']]*2, op_axes=[[0, 1], [0, 1, 0]]) # Non-broadcastable dimensions in the result assert_raises(ValueError, nditer, [a, a], [], [['readonly']]*2, op_axes=[[0, 1], [1, 0]]) def test_iter_copy(): # Check that copying the iterator works correctly a = arange(24).reshape(2, 3, 4) # Simple iterator i = nditer(a) j = i.copy() assert_equal([x[()] for x in i], [x[()] for x in j]) i.iterindex = 3 j = i.copy() assert_equal([x[()] for x in i], [x[()] for x in j]) # Buffered iterator i = nditer(a, ['buffered', 'ranged'], order='F', buffersize=3) j = i.copy() assert_equal([x[()] for x in i], [x[()] for x in j]) i.iterindex = 3 j = i.copy() assert_equal([x[()] for x in i], [x[()] for x in j]) i.iterrange = (3, 9) j = i.copy() assert_equal([x[()] for x in i], [x[()] for x in j]) i.iterrange = (2, 18) next(i) next(i) j = i.copy() assert_equal([x[()] for x in i], [x[()] for x in j]) # Casting iterator with nditer(a, ['buffered'], order='F', casting='unsafe', op_dtypes='f8', buffersize=5) as i: j = i.copy() assert_equal([x[()] for x in j], a.ravel(order='F')) a = arange(24, dtype='<i4').reshape(2, 3, 4) with nditer(a, ['buffered'], order='F', casting='unsafe', op_dtypes='>f8', buffersize=5) as i: j = i.copy() assert_equal([x[()] for x in j], a.ravel(order='F')) @pytest.mark.parametrize("dtype", np.typecodes["All"]) @pytest.mark.parametrize("loop_dtype", np.typecodes["All"]) @pytest.mark.filterwarnings("ignore::numpy.ComplexWarning") def test_iter_copy_casts(dtype, loop_dtype): # Ensure the dtype is never flexible: if loop_dtype.lower() == "m": loop_dtype = loop_dtype + "[ms]" elif np.dtype(loop_dtype).itemsize == 0: loop_dtype = loop_dtype + "50" # Make things a bit more interesting by requiring a byte-swap as well: arr = np.ones(1000, dtype=np.dtype(dtype).newbyteorder()) try: expected = arr.astype(loop_dtype) except Exception: # Some casts are not possible, do not worry about them return it = np.nditer((arr,), ["buffered", "external_loop", "refs_ok"], op_dtypes=[loop_dtype], casting="unsafe") if np.issubdtype(np.dtype(loop_dtype), np.number): # Casting to strings may be strange, but for simple dtypes do not rely # on the cast being correct: assert_array_equal(expected, np.ones(1000, dtype=loop_dtype)) it_copy = it.copy() res = next(it) del it res_copy = next(it_copy) del it_copy assert_array_equal(res, expected) assert_array_equal(res_copy, expected) def test_iter_copy_casts_structured(): # Test a complicated structured dtype for casting, as it requires # both multiple steps and a more complex casting setup. # Includes a structured -> unstructured (any to object), and many other # casts, which cause this to require all steps in the casting machinery # one level down as well as the iterator copy (which uses NpyAuxData clone) in_dtype = np.dtype([("a", np.dtype("i,")), ("b", np.dtype(">i,<i,>d,S17,>d,(3)f,O,i1"))]) out_dtype = np.dtype([("a", np.dtype("O")), ("b", np.dtype(">i,>i,S17,>d,>U3,(3)d,i1,O"))]) arr = np.ones(1000, dtype=in_dtype) it = np.nditer((arr,), ["buffered", "external_loop", "refs_ok"], op_dtypes=[out_dtype], casting="unsafe") it_copy = it.copy() res1 = next(it) del it res2 = next(it_copy) del it_copy expected = arr["a"].astype(out_dtype["a"]) assert_array_equal(res1["a"], expected) assert_array_equal(res2["a"], expected) for field in in_dtype["b"].names: # Note that the .base avoids the subarray field expected = arr["b"][field].astype(out_dtype["b"][field].base) assert_array_equal(res1["b"][field], expected) assert_array_equal(res2["b"][field], expected) def test_iter_allocate_output_simple(): # Check that the iterator will properly allocate outputs # Simple case a = arange(6) i = nditer([a, None], [], [['readonly'], ['writeonly', 'allocate']], op_dtypes=[None, np.dtype('f4')]) assert_equal(i.operands[1].shape, a.shape) assert_equal(i.operands[1].dtype, np.dtype('f4')) def test_iter_allocate_output_buffered_readwrite(): # Allocated output with buffering + delay_bufalloc a = arange(6) i = nditer([a, None], ['buffered', 'delay_bufalloc'], [['readonly'], ['allocate', 'readwrite']]) with i: i.operands[1][:] = 1 i.reset() for x in i: x[1][...] += x[0][...] assert_equal(i.operands[1], a+1) def test_iter_allocate_output_itorder(): # The allocated output should match the iteration order # C-order input, best iteration order a = arange(6, dtype='i4').reshape(2, 3) i = nditer([a, None], [], [['readonly'], ['writeonly', 'allocate']], op_dtypes=[None, np.dtype('f4')]) assert_equal(i.operands[1].shape, a.shape) assert_equal(i.operands[1].strides, a.strides) assert_equal(i.operands[1].dtype, np.dtype('f4')) # F-order input, best iteration order a = arange(24, dtype='i4').reshape(2, 3, 4).T i = nditer([a, None], [], [['readonly'], ['writeonly', 'allocate']], op_dtypes=[None, np.dtype('f4')]) assert_equal(i.operands[1].shape, a.shape) assert_equal(i.operands[1].strides, a.strides) assert_equal(i.operands[1].dtype, np.dtype('f4')) # Non-contiguous input, C iteration order a = arange(24, dtype='i4').reshape(2, 3, 4).swapaxes(0, 1) i = nditer([a, None], [], [['readonly'], ['writeonly', 'allocate']], order='C', op_dtypes=[None, np.dtype('f4')]) assert_equal(i.operands[1].shape, a.shape) assert_equal(i.operands[1].strides, (32, 16, 4)) assert_equal(i.operands[1].dtype, np.dtype('f4')) def test_iter_allocate_output_opaxes(): # Specifying op_axes should work a = arange(24, dtype='i4').reshape(2, 3, 4) i = nditer([None, a], [], [['writeonly', 'allocate'], ['readonly']], op_dtypes=[np.dtype('u4'), None], op_axes=[[1, 2, 0], None]) assert_equal(i.operands[0].shape, (4, 2, 3)) assert_equal(i.operands[0].strides, (4, 48, 16)) assert_equal(i.operands[0].dtype, np.dtype('u4')) def test_iter_allocate_output_types_promotion(): # Check type promotion of automatic outputs i = nditer([array([3], dtype='f4'), array([0], dtype='f8'), None], [], [['readonly']]*2+[['writeonly', 'allocate']]) assert_equal(i.dtypes[2], np.dtype('f8')) i = nditer([array([3], dtype='i4'), array([0], dtype='f4'), None], [], [['readonly']]*2+[['writeonly', 'allocate']]) assert_equal(i.dtypes[2], np.dtype('f8')) i = nditer([array([3], dtype='f4'), array(0, dtype='f8'), None], [], [['readonly']]*2+[['writeonly', 'allocate']]) assert_equal(i.dtypes[2], np.dtype('f4')) i = nditer([array([3], dtype='u4'), array(0, dtype='i4'), None], [], [['readonly']]*2+[['writeonly', 'allocate']]) assert_equal(i.dtypes[2], np.dtype('u4')) i = nditer([array([3], dtype='u4'), array(-12, dtype='i4'), None], [], [['readonly']]*2+[['writeonly', 'allocate']]) assert_equal(i.dtypes[2], np.dtype('i8')) def test_iter_allocate_output_types_byte_order(): # Verify the rules for byte order changes # When there's just one input, the output type exactly matches a = array([3], dtype='u4').newbyteorder() i = nditer([a, None], [], [['readonly'], ['writeonly', 'allocate']]) assert_equal(i.dtypes[0], i.dtypes[1]) # With two or more inputs, the output type is in native byte order i = nditer([a, a, None], [], [['readonly'], ['readonly'], ['writeonly', 'allocate']]) assert_(i.dtypes[0] != i.dtypes[2]) assert_equal(i.dtypes[0].newbyteorder('='), i.dtypes[2]) def test_iter_allocate_output_types_scalar(): # If the inputs are all scalars, the output should be a scalar i = nditer([None, 1, 2.3, np.float32(12), np.complex128(3)], [], [['writeonly', 'allocate']] + [['readonly']]*4) assert_equal(i.operands[0].dtype, np.dtype('complex128')) assert_equal(i.operands[0].ndim, 0) def test_iter_allocate_output_subtype(): # Make sure that the subtype with priority wins class MyNDArray(np.ndarray): __array_priority__ = 15 # subclass vs ndarray a = np.array([[1, 2], [3, 4]]).view(MyNDArray) b = np.arange(4).reshape(2, 2).T i = nditer([a, b, None], [], [['readonly'], ['readonly'], ['writeonly', 'allocate']]) assert_equal(type(a), type(i.operands[2])) assert_(type(b) is not type(i.operands[2])) assert_equal(i.operands[2].shape, (2, 2)) # If subtypes are disabled, we should get back an ndarray. i = nditer([a, b, None], [], [['readonly'], ['readonly'], ['writeonly', 'allocate', 'no_subtype']]) assert_equal(type(b), type(i.operands[2])) assert_(type(a) is not type(i.operands[2])) assert_equal(i.operands[2].shape, (2, 2)) def test_iter_allocate_output_errors(): # Check that the iterator will throw errors for bad output allocations # Need an input if no output data type is specified a = arange(6) assert_raises(TypeError, nditer, [a, None], [], [['writeonly'], ['writeonly', 'allocate']]) # Allocated output should be flagged for writing assert_raises(ValueError, nditer, [a, None], [], [['readonly'], ['allocate', 'readonly']]) # Allocated output can't have buffering without delayed bufalloc assert_raises(ValueError, nditer, [a, None], ['buffered'], ['allocate', 'readwrite']) # Must specify at least one input assert_raises(ValueError, nditer, [None, None], [], [['writeonly', 'allocate'], ['writeonly', 'allocate']], op_dtypes=[np.dtype('f4'), np.dtype('f4')]) # If using op_axes, must specify all the axes a = arange(24, dtype='i4').reshape(2, 3, 4) assert_raises(ValueError, nditer, [a, None], [], [['readonly'], ['writeonly', 'allocate']], op_dtypes=[None, np.dtype('f4')], op_axes=[None, [0, np.newaxis, 1]]) # If using op_axes, the axes must be within bounds assert_raises(ValueError, nditer, [a, None], [], [['readonly'], ['writeonly', 'allocate']], op_dtypes=[None, np.dtype('f4')], op_axes=[None, [0, 3, 1]]) # If using op_axes, there can't be duplicates assert_raises(ValueError, nditer, [a, None], [], [['readonly'], ['writeonly', 'allocate']], op_dtypes=[None, np.dtype('f4')], op_axes=[None, [0, 2, 1, 0]]) # Not all axes may be specified if a reduction. If there is a hole # in op_axes, this is an error. a = arange(24, dtype='i4').reshape(2, 3, 4) assert_raises(ValueError, nditer, [a, None], ["reduce_ok"], [['readonly'], ['readwrite', 'allocate']], op_dtypes=[None, np.dtype('f4')], op_axes=[None, [0, np.newaxis, 2]]) def test_iter_remove_axis(): a = arange(24).reshape(2, 3, 4) i = nditer(a, ['multi_index']) i.remove_axis(1) assert_equal([x for x in i], a[:, 0,:].ravel()) a = a[::-1,:,:] i = nditer(a, ['multi_index']) i.remove_axis(0) assert_equal([x for x in i], a[0,:,:].ravel()) def test_iter_remove_multi_index_inner_loop(): # Check that removing multi-index support works a = arange(24).reshape(2, 3, 4) i = nditer(a, ['multi_index']) assert_equal(i.ndim, 3) assert_equal(i.shape, (2, 3, 4)) assert_equal(i.itviews[0].shape, (2, 3, 4)) # Removing the multi-index tracking causes all dimensions to coalesce before = [x for x in i] i.remove_multi_index() after = [x for x in i] assert_equal(before, after) assert_equal(i.ndim, 1) assert_raises(ValueError, lambda i:i.shape, i) assert_equal(i.itviews[0].shape, (24,)) # Removing the inner loop means there's just one iteration i.reset() assert_equal(i.itersize, 24) assert_equal(i[0].shape, tuple()) i.enable_external_loop() assert_equal(i.itersize, 24) assert_equal(i[0].shape, (24,)) assert_equal(i.value, arange(24)) def test_iter_iterindex(): # Make sure iterindex works buffersize = 5 a = arange(24).reshape(4, 3, 2) for flags in ([], ['buffered']): i = nditer(a, flags, buffersize=buffersize) assert_equal(iter_iterindices(i), list(range(24))) i.iterindex = 2 assert_equal(iter_iterindices(i), list(range(2, 24))) i = nditer(a, flags, order='F', buffersize=buffersize) assert_equal(iter_iterindices(i), list(range(24))) i.iterindex = 5 assert_equal(iter_iterindices(i), list(range(5, 24))) i = nditer(a[::-1], flags, order='F', buffersize=buffersize) assert_equal(iter_iterindices(i), list(range(24))) i.iterindex = 9 assert_equal(iter_iterindices(i), list(range(9, 24))) i = nditer(a[::-1, ::-1], flags, order='C', buffersize=buffersize) assert_equal(iter_iterindices(i), list(range(24))) i.iterindex = 13 assert_equal(iter_iterindices(i), list(range(13, 24))) i = nditer(a[::1, ::-1], flags, buffersize=buffersize) assert_equal(iter_iterindices(i), list(range(24))) i.iterindex = 23 assert_equal(iter_iterindices(i), list(range(23, 24))) i.reset() i.iterindex = 2 assert_equal(iter_iterindices(i), list(range(2, 24))) def test_iter_iterrange(): # Make sure getting and resetting the iterrange works buffersize = 5 a = arange(24, dtype='i4').reshape(4, 3, 2) a_fort = a.ravel(order='F') i = nditer(a, ['ranged'], ['readonly'], order='F', buffersize=buffersize) assert_equal(i.iterrange, (0, 24)) assert_equal([x[()] for x in i], a_fort) for r in [(0, 24), (1, 2), (3, 24), (5, 5), (0, 20), (23, 24)]: i.iterrange = r assert_equal(i.iterrange, r) assert_equal([x[()] for x in i], a_fort[r[0]:r[1]]) i = nditer(a, ['ranged', 'buffered'], ['readonly'], order='F', op_dtypes='f8', buffersize=buffersize) assert_equal(i.iterrange, (0, 24)) assert_equal([x[()] for x in i], a_fort) for r in [(0, 24), (1, 2), (3, 24), (5, 5), (0, 20), (23, 24)]: i.iterrange = r assert_equal(i.iterrange, r) assert_equal([x[()] for x in i], a_fort[r[0]:r[1]]) def get_array(i): val = np.array([], dtype='f8') for x in i: val = np.concatenate((val, x)) return val i = nditer(a, ['ranged', 'buffered', 'external_loop'], ['readonly'], order='F', op_dtypes='f8', buffersize=buffersize) assert_equal(i.iterrange, (0, 24)) assert_equal(get_array(i), a_fort) for r in [(0, 24), (1, 2), (3, 24), (5, 5), (0, 20), (23, 24)]: i.iterrange = r assert_equal(i.iterrange, r) assert_equal(get_array(i), a_fort[r[0]:r[1]]) def test_iter_buffering(): # Test buffering with several buffer sizes and types arrays = [] # F-order swapped array arrays.append(np.arange(24, dtype='c16').reshape(2, 3, 4).T.newbyteorder().byteswap()) # Contiguous 1-dimensional array arrays.append(np.arange(10, dtype='f4')) # Unaligned array a = np.zeros((4*16+1,), dtype='i1')[1:] a.dtype = 'i4' a[:] = np.arange(16, dtype='i4') arrays.append(a) # 4-D F-order array arrays.append(np.arange(120, dtype='i4').reshape(5, 3, 2, 4).T) for a in arrays: for buffersize in (1, 2, 3, 5, 8, 11, 16, 1024): vals = [] i = nditer(a, ['buffered', 'external_loop'], [['readonly', 'nbo', 'aligned']], order='C', casting='equiv', buffersize=buffersize) while not i.finished: assert_(i[0].size <= buffersize) vals.append(i[0].copy()) i.iternext() assert_equal(np.concatenate(vals), a.ravel(order='C')) def test_iter_write_buffering(): # Test that buffering of writes is working # F-order swapped array a = np.arange(24).reshape(2, 3, 4).T.newbyteorder().byteswap() i = nditer(a, ['buffered'], [['readwrite', 'nbo', 'aligned']], casting='equiv', order='C', buffersize=16) x = 0 with i: while not i.finished: i[0] = x x += 1 i.iternext() assert_equal(a.ravel(order='C'), np.arange(24)) def test_iter_buffering_delayed_alloc(): # Test that delaying buffer allocation works a = np.arange(6) b = np.arange(1, dtype='f4') i = nditer([a, b], ['buffered', 'delay_bufalloc', 'multi_index', 'reduce_ok'], ['readwrite'], casting='unsafe', op_dtypes='f4') assert_(i.has_delayed_bufalloc) assert_raises(ValueError, lambda i:i.multi_index, i) assert_raises(ValueError, lambda i:i[0], i) assert_raises(ValueError, lambda i:i[0:2], i) def assign_iter(i): i[0] = 0 assert_raises(ValueError, assign_iter, i) i.reset() assert_(not i.has_delayed_bufalloc) assert_equal(i.multi_index, (0,)) with i: assert_equal(i[0], 0) i[1] = 1 assert_equal(i[0:2], [0, 1]) assert_equal([[x[0][()], x[1][()]] for x in i], list(zip(range(6), [1]*6))) def test_iter_buffered_cast_simple(): # Test that buffering can handle a simple cast a = np.arange(10, dtype='f4') i = nditer(a, ['buffered', 'external_loop'], [['readwrite', 'nbo', 'aligned']], casting='same_kind', op_dtypes=[np.dtype('f8')], buffersize=3) with i: for v in i: v[...] *= 2 assert_equal(a, 2*np.arange(10, dtype='f4')) def test_iter_buffered_cast_byteswapped(): # Test that buffering can handle a cast which requires swap->cast->swap a = np.arange(10, dtype='f4').newbyteorder().byteswap() i = nditer(a, ['buffered', 'external_loop'], [['readwrite', 'nbo', 'aligned']], casting='same_kind', op_dtypes=[np.dtype('f8').newbyteorder()], buffersize=3) with i: for v in i: v[...] *= 2 assert_equal(a, 2*np.arange(10, dtype='f4')) with suppress_warnings() as sup: sup.filter(np.ComplexWarning) a = np.arange(10, dtype='f8').newbyteorder().byteswap() i = nditer(a, ['buffered', 'external_loop'], [['readwrite', 'nbo', 'aligned']], casting='unsafe', op_dtypes=[np.dtype('c8').newbyteorder()], buffersize=3) with i: for v in i: v[...] *= 2 assert_equal(a, 2*np.arange(10, dtype='f8')) def test_iter_buffered_cast_byteswapped_complex(): # Test that buffering can handle a cast which requires swap->cast->copy a = np.arange(10, dtype='c8').newbyteorder().byteswap() a += 2j i = nditer(a, ['buffered', 'external_loop'], [['readwrite', 'nbo', 'aligned']], casting='same_kind', op_dtypes=[np.dtype('c16')], buffersize=3) with i: for v in i: v[...] *= 2 assert_equal(a, 2*np.arange(10, dtype='c8') + 4j) a = np.arange(10, dtype='c8') a += 2j i = nditer(a, ['buffered', 'external_loop'], [['readwrite', 'nbo', 'aligned']], casting='same_kind', op_dtypes=[np.dtype('c16').newbyteorder()], buffersize=3) with i: for v in i: v[...] *= 2 assert_equal(a, 2*np.arange(10, dtype='c8') + 4j) a = np.arange(10, dtype=np.clongdouble).newbyteorder().byteswap() a += 2j i = nditer(a, ['buffered', 'external_loop'], [['readwrite', 'nbo', 'aligned']], casting='same_kind', op_dtypes=[np.dtype('c16')], buffersize=3) with i: for v in i: v[...] *= 2 assert_equal(a, 2*np.arange(10, dtype=np.clongdouble) + 4j) a = np.arange(10, dtype=np.longdouble).newbyteorder().byteswap() i = nditer(a, ['buffered', 'external_loop'], [['readwrite', 'nbo', 'aligned']], casting='same_kind', op_dtypes=[np.dtype('f4')], buffersize=7) with i: for v in i: v[...] *= 2 assert_equal(a, 2*np.arange(10, dtype=np.longdouble)) def test_iter_buffered_cast_structured_type(): # Tests buffering of structured types # simple -> struct type (duplicates the value) sdt = [('a', 'f4'), ('b', 'i8'), ('c', 'c8', (2, 3)), ('d', 'O')] a = np.arange(3, dtype='f4') + 0.5 i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt) vals = [np.array(x) for x in i] assert_equal(vals[0]['a'], 0.5) assert_equal(vals[0]['b'], 0) assert_equal(vals[0]['c'], [[(0.5)]*3]*2) assert_equal(vals[0]['d'], 0.5) assert_equal(vals[1]['a'], 1.5) assert_equal(vals[1]['b'], 1) assert_equal(vals[1]['c'], [[(1.5)]*3]*2) assert_equal(vals[1]['d'], 1.5) assert_equal(vals[0].dtype, np.dtype(sdt)) # object -> struct type sdt = [('a', 'f4'), ('b', 'i8'), ('c', 'c8', (2, 3)), ('d', 'O')] a = np.zeros((3,), dtype='O') a[0] = (0.5, 0.5, [[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]], 0.5) a[1] = (1.5, 1.5, [[1.5, 1.5, 1.5], [1.5, 1.5, 1.5]], 1.5) a[2] = (2.5, 2.5, [[2.5, 2.5, 2.5], [2.5, 2.5, 2.5]], 2.5) if HAS_REFCOUNT: rc = sys.getrefcount(a[0]) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt) vals = [x.copy() for x in i] assert_equal(vals[0]['a'], 0.5) assert_equal(vals[0]['b'], 0) assert_equal(vals[0]['c'], [[(0.5)]*3]*2) assert_equal(vals[0]['d'], 0.5) assert_equal(vals[1]['a'], 1.5) assert_equal(vals[1]['b'], 1) assert_equal(vals[1]['c'], [[(1.5)]*3]*2) assert_equal(vals[1]['d'], 1.5) assert_equal(vals[0].dtype, np.dtype(sdt)) vals, i, x = [None]*3 if HAS_REFCOUNT: assert_equal(sys.getrefcount(a[0]), rc) # single-field struct type -> simple sdt = [('a', 'f4')] a = np.array([(5.5,), (8,)], dtype=sdt) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes='i4') assert_equal([x_[()] for x_ in i], [5, 8]) # make sure multi-field struct type -> simple doesn't work sdt = [('a', 'f4'), ('b', 'i8'), ('d', 'O')] a = np.array([(5.5, 7, 'test'), (8, 10, 11)], dtype=sdt) assert_raises(TypeError, lambda: ( nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes='i4'))) # struct type -> struct type (field-wise copy) sdt1 = [('a', 'f4'), ('b', 'i8'), ('d', 'O')] sdt2 = [('d', 'u2'), ('a', 'O'), ('b', 'f8')] a = np.array([(1, 2, 3), (4, 5, 6)], dtype=sdt1) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) assert_equal([np.array(x_) for x_ in i], [np.array((1, 2, 3), dtype=sdt2), np.array((4, 5, 6), dtype=sdt2)]) def test_iter_buffered_cast_structured_type_failure_with_cleanup(): # make sure struct type -> struct type with different # number of fields fails sdt1 = [('a', 'f4'), ('b', 'i8'), ('d', 'O')] sdt2 = [('b', 'O'), ('a', 'f8')] a = np.array([(1, 2, 3), (4, 5, 6)], dtype=sdt1) for intent in ["readwrite", "readonly", "writeonly"]: # If the following assert fails, the place where the error is raised # within nditer may change. That is fine, but it may make sense for # a new (hard to design) test to replace it. The `simple_arr` is # designed to require a multi-step cast (due to having fields). assert np.can_cast(a.dtype, sdt2, casting="unsafe") simple_arr = np.array([1, 2], dtype="i,i") # requires clean up with pytest.raises(ValueError): nditer((simple_arr, a), ['buffered', 'refs_ok'], [intent, intent], casting='unsafe', op_dtypes=["f,f", sdt2]) def test_buffered_cast_error_paths(): with pytest.raises(ValueError): # The input is cast into an `S3` buffer np.nditer((np.array("a", dtype="S1"),), op_dtypes=["i"], casting="unsafe", flags=["buffered"]) # The `M8[ns]` is cast into the `S3` output it = np.nditer((np.array(1, dtype="i"),), op_dtypes=["S1"], op_flags=["writeonly"], casting="unsafe", flags=["buffered"]) with pytest.raises(ValueError): with it: buf = next(it) buf[...] = "a" # cannot be converted to int. @pytest.mark.skipif(not HAS_REFCOUNT, reason="PyPy seems to not hit this.") def test_buffered_cast_error_paths_unraisable(): # The following gives an unraisable error. Pytest sometimes captures that # (depending python and/or pytest version). So with Python>=3.8 this can # probably be cleaned out in the future to check for # pytest.PytestUnraisableExceptionWarning: code = textwrap.dedent(""" import numpy as np it = np.nditer((np.array(1, dtype="i"),), op_dtypes=["S1"], op_flags=["writeonly"], casting="unsafe", flags=["buffered"]) buf = next(it) buf[...] = "a" del buf, it # Flushing only happens during deallocate right now. """) res = subprocess.check_output([sys.executable, "-c", code], stderr=subprocess.STDOUT, text=True) assert "ValueError" in res def test_iter_buffered_cast_subarray(): # Tests buffering of subarrays # one element -> many (copies it to all) sdt1 = [('a', 'f4')] sdt2 = [('a', 'f8', (3, 2, 2))] a = np.zeros((6,), dtype=sdt1) a['a'] = np.arange(6) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) for x, count in zip(i, list(range(6))): assert_(np.all(x['a'] == count)) # one element -> many -> back (copies it to all) sdt1 = [('a', 'O', (1, 1))] sdt2 = [('a', 'O', (3, 2, 2))] a = np.zeros((6,), dtype=sdt1) a['a'][:, 0, 0] = np.arange(6) i = nditer(a, ['buffered', 'refs_ok'], ['readwrite'], casting='unsafe', op_dtypes=sdt2) with i: assert_equal(i[0].dtype, np.dtype(sdt2)) count = 0 for x in i: assert_(np.all(x['a'] == count)) x['a'][0] += 2 count += 1 assert_equal(a['a'], np.arange(6).reshape(6, 1, 1)+2) # many -> one element -> back (copies just element 0) sdt1 = [('a', 'O', (3, 2, 2))] sdt2 = [('a', 'O', (1,))] a = np.zeros((6,), dtype=sdt1) a['a'][:, 0, 0, 0] = np.arange(6) i = nditer(a, ['buffered', 'refs_ok'], ['readwrite'], casting='unsafe', op_dtypes=sdt2) with i: assert_equal(i[0].dtype, np.dtype(sdt2)) count = 0 for x in i: assert_equal(x['a'], count) x['a'] += 2 count += 1 assert_equal(a['a'], np.arange(6).reshape(6, 1, 1, 1)*np.ones((1, 3, 2, 2))+2) # many -> one element -> back (copies just element 0) sdt1 = [('a', 'f8', (3, 2, 2))] sdt2 = [('a', 'O', (1,))] a = np.zeros((6,), dtype=sdt1) a['a'][:, 0, 0, 0] = np.arange(6) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) count = 0 for x in i: assert_equal(x['a'], count) count += 1 # many -> one element (copies just element 0) sdt1 = [('a', 'O', (3, 2, 2))] sdt2 = [('a', 'f4', (1,))] a = np.zeros((6,), dtype=sdt1) a['a'][:, 0, 0, 0] = np.arange(6) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) count = 0 for x in i: assert_equal(x['a'], count) count += 1 # many -> matching shape (straightforward copy) sdt1 = [('a', 'O', (3, 2, 2))] sdt2 = [('a', 'f4', (3, 2, 2))] a = np.zeros((6,), dtype=sdt1) a['a'] = np.arange(6*3*2*2).reshape(6, 3, 2, 2) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) count = 0 for x in i: assert_equal(x['a'], a[count]['a']) count += 1 # vector -> smaller vector (truncates) sdt1 = [('a', 'f8', (6,))] sdt2 = [('a', 'f4', (2,))] a = np.zeros((6,), dtype=sdt1) a['a'] = np.arange(6*6).reshape(6, 6) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) count = 0 for x in i: assert_equal(x['a'], a[count]['a'][:2]) count += 1 # vector -> bigger vector (pads with zeros) sdt1 = [('a', 'f8', (2,))] sdt2 = [('a', 'f4', (6,))] a = np.zeros((6,), dtype=sdt1) a['a'] = np.arange(6*2).reshape(6, 2) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) count = 0 for x in i: assert_equal(x['a'][:2], a[count]['a']) assert_equal(x['a'][2:], [0, 0, 0, 0]) count += 1 # vector -> matrix (broadcasts) sdt1 = [('a', 'f8', (2,))] sdt2 = [('a', 'f4', (2, 2))] a = np.zeros((6,), dtype=sdt1) a['a'] = np.arange(6*2).reshape(6, 2) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) count = 0 for x in i: assert_equal(x['a'][0], a[count]['a']) assert_equal(x['a'][1], a[count]['a']) count += 1 # vector -> matrix (broadcasts and zero-pads) sdt1 = [('a', 'f8', (2, 1))] sdt2 = [('a', 'f4', (3, 2))] a = np.zeros((6,), dtype=sdt1) a['a'] = np.arange(6*2).reshape(6, 2, 1) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) count = 0 for x in i: assert_equal(x['a'][:2, 0], a[count]['a'][:, 0]) assert_equal(x['a'][:2, 1], a[count]['a'][:, 0]) assert_equal(x['a'][2,:], [0, 0]) count += 1 # matrix -> matrix (truncates and zero-pads) sdt1 = [('a', 'f8', (2, 3))] sdt2 = [('a', 'f4', (3, 2))] a = np.zeros((6,), dtype=sdt1) a['a'] = np.arange(6*2*3).reshape(6, 2, 3) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) count = 0 for x in i: assert_equal(x['a'][:2, 0], a[count]['a'][:, 0]) assert_equal(x['a'][:2, 1], a[count]['a'][:, 1]) assert_equal(x['a'][2,:], [0, 0]) count += 1 def test_iter_buffering_badwriteback(): # Writing back from a buffer cannot combine elements # a needs write buffering, but had a broadcast dimension a = np.arange(6).reshape(2, 3, 1) b = np.arange(12).reshape(2, 3, 2) assert_raises(ValueError, nditer, [a, b], ['buffered', 'external_loop'], [['readwrite'], ['writeonly']], order='C') # But if a is readonly, it's fine nditer([a, b], ['buffered', 'external_loop'], [['readonly'], ['writeonly']], order='C') # If a has just one element, it's fine too (constant 0 stride, a reduction) a = np.arange(1).reshape(1, 1, 1) nditer([a, b], ['buffered', 'external_loop', 'reduce_ok'], [['readwrite'], ['writeonly']], order='C') # check that it fails on other dimensions too a = np.arange(6).reshape(1, 3, 2) assert_raises(ValueError, nditer, [a, b], ['buffered', 'external_loop'], [['readwrite'], ['writeonly']], order='C') a = np.arange(4).reshape(2, 1, 2) assert_raises(ValueError, nditer, [a, b], ['buffered', 'external_loop'], [['readwrite'], ['writeonly']], order='C') def test_iter_buffering_string(): # Safe casting disallows shrinking strings a = np.array(['abc', 'a', 'abcd'], dtype=np.bytes_) assert_equal(a.dtype, np.dtype('S4')) assert_raises(TypeError, nditer, a, ['buffered'], ['readonly'], op_dtypes='S2') i = nditer(a, ['buffered'], ['readonly'], op_dtypes='S6') assert_equal(i[0], b'abc') assert_equal(i[0].dtype, np.dtype('S6')) a = np.array(['abc', 'a', 'abcd'], dtype=np.unicode_) assert_equal(a.dtype, np.dtype('U4')) assert_raises(TypeError, nditer, a, ['buffered'], ['readonly'], op_dtypes='U2') i = nditer(a, ['buffered'], ['readonly'], op_dtypes='U6') assert_equal(i[0], u'abc') assert_equal(i[0].dtype, np.dtype('U6')) def test_iter_buffering_growinner(): # Test that the inner loop grows when no buffering is needed a = np.arange(30) i = nditer(a, ['buffered', 'growinner', 'external_loop'], buffersize=5) # Should end up with just one inner loop here assert_equal(i[0].size, a.size) @pytest.mark.slow def test_iter_buffered_reduce_reuse(): # large enough array for all views, including negative strides. a = np.arange(2*3**5)[3**5:3**5+1] flags = ['buffered', 'delay_bufalloc', 'multi_index', 'reduce_ok', 'refs_ok'] op_flags = [('readonly',), ('readwrite', 'allocate')] op_axes_list = [[(0, 1, 2), (0, 1, -1)], [(0, 1, 2), (0, -1, -1)]] # wrong dtype to force buffering op_dtypes = [float, a.dtype] def get_params(): for xs in range(-3**2, 3**2 + 1): for ys in range(xs, 3**2 + 1): for op_axes in op_axes_list: # last stride is reduced and because of that not # important for this test, as it is the inner stride. strides = (xs * a.itemsize, ys * a.itemsize, a.itemsize) arr = np.lib.stride_tricks.as_strided(a, (3, 3, 3), strides) for skip in [0, 1]: yield arr, op_axes, skip for arr, op_axes, skip in get_params(): nditer2 = np.nditer([arr.copy(), None], op_axes=op_axes, flags=flags, op_flags=op_flags, op_dtypes=op_dtypes) with nditer2: nditer2.operands[-1][...] = 0 nditer2.reset() nditer2.iterindex = skip for (a2_in, b2_in) in nditer2: b2_in += a2_in.astype(np.int_) comp_res = nditer2.operands[-1] for bufsize in range(0, 3**3): nditer1 = np.nditer([arr, None], op_axes=op_axes, flags=flags, op_flags=op_flags, buffersize=bufsize, op_dtypes=op_dtypes) with nditer1: nditer1.operands[-1][...] = 0 nditer1.reset() nditer1.iterindex = skip for (a1_in, b1_in) in nditer1: b1_in += a1_in.astype(np.int_) res = nditer1.operands[-1] assert_array_equal(res, comp_res) def test_iter_no_broadcast(): # Test that the no_broadcast flag works a = np.arange(24).reshape(2, 3, 4) b = np.arange(6).reshape(2, 3, 1) c = np.arange(12).reshape(3, 4) nditer([a, b, c], [], [['readonly', 'no_broadcast'], ['readonly'], ['readonly']]) assert_raises(ValueError, nditer, [a, b, c], [], [['readonly'], ['readonly', 'no_broadcast'], ['readonly']]) assert_raises(ValueError, nditer, [a, b, c], [], [['readonly'], ['readonly'], ['readonly', 'no_broadcast']]) class TestIterNested: def test_basic(self): # Test nested iteration basic usage a = arange(12).reshape(2, 3, 2) i, j = np.nested_iters(a, [[0], [1, 2]]) vals = [list(j) for _ in i] assert_equal(vals, [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]) i, j = np.nested_iters(a, [[0, 1], [2]]) vals = [list(j) for _ in i] assert_equal(vals, [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11]]) i, j = np.nested_iters(a, [[0, 2], [1]]) vals = [list(j) for _ in i] assert_equal(vals, [[0, 2, 4], [1, 3, 5], [6, 8, 10], [7, 9, 11]]) def test_reorder(self): # Test nested iteration basic usage a = arange(12).reshape(2, 3, 2) # In 'K' order (default), it gets reordered i, j = np.nested_iters(a, [[0], [2, 1]]) vals = [list(j) for _ in i] assert_equal(vals, [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]) i, j = np.nested_iters(a, [[1, 0], [2]]) vals = [list(j) for _ in i] assert_equal(vals, [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11]]) i, j = np.nested_iters(a, [[2, 0], [1]]) vals = [list(j) for _ in i] assert_equal(vals, [[0, 2, 4], [1, 3, 5], [6, 8, 10], [7, 9, 11]]) # In 'C' order, it doesn't i, j = np.nested_iters(a, [[0], [2, 1]], order='C') vals = [list(j) for _ in i] assert_equal(vals, [[0, 2, 4, 1, 3, 5], [6, 8, 10, 7, 9, 11]]) i, j = np.nested_iters(a, [[1, 0], [2]], order='C') vals = [list(j) for _ in i] assert_equal(vals, [[0, 1], [6, 7], [2, 3], [8, 9], [4, 5], [10, 11]]) i, j = np.nested_iters(a, [[2, 0], [1]], order='C') vals = [list(j) for _ in i] assert_equal(vals, [[0, 2, 4], [6, 8, 10], [1, 3, 5], [7, 9, 11]]) def test_flip_axes(self): # Test nested iteration with negative axes a = arange(12).reshape(2, 3, 2)[::-1, ::-1, ::-1] # In 'K' order (default), the axes all get flipped i, j = np.nested_iters(a, [[0], [1, 2]]) vals = [list(j) for _ in i] assert_equal(vals, [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]) i, j = np.nested_iters(a, [[0, 1], [2]]) vals = [list(j) for _ in i] assert_equal(vals, [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11]]) i, j = np.nested_iters(a, [[0, 2], [1]]) vals = [list(j) for _ in i] assert_equal(vals, [[0, 2, 4], [1, 3, 5], [6, 8, 10], [7, 9, 11]]) # In 'C' order, flipping axes is disabled i, j = np.nested_iters(a, [[0], [1, 2]], order='C') vals = [list(j) for _ in i] assert_equal(vals, [[11, 10, 9, 8, 7, 6], [5, 4, 3, 2, 1, 0]]) i, j = np.nested_iters(a, [[0, 1], [2]], order='C') vals = [list(j) for _ in i] assert_equal(vals, [[11, 10], [9, 8], [7, 6], [5, 4], [3, 2], [1, 0]]) i, j = np.nested_iters(a, [[0, 2], [1]], order='C') vals = [list(j) for _ in i] assert_equal(vals, [[11, 9, 7], [10, 8, 6], [5, 3, 1], [4, 2, 0]]) def test_broadcast(self): # Test nested iteration with broadcasting a = arange(2).reshape(2, 1) b = arange(3).reshape(1, 3) i, j = np.nested_iters([a, b], [[0], [1]]) vals = [list(j) for _ in i] assert_equal(vals, [[[0, 0], [0, 1], [0, 2]], [[1, 0], [1, 1], [1, 2]]]) i, j = np.nested_iters([a, b], [[1], [0]]) vals = [list(j) for _ in i] assert_equal(vals, [[[0, 0], [1, 0]], [[0, 1], [1, 1]], [[0, 2], [1, 2]]]) def test_dtype_copy(self): # Test nested iteration with a copy to change dtype # copy a = arange(6, dtype='i4').reshape(2, 3) i, j = np.nested_iters(a, [[0], [1]], op_flags=['readonly', 'copy'], op_dtypes='f8') assert_equal(j[0].dtype, np.dtype('f8')) vals = [list(j) for _ in i] assert_equal(vals, [[0, 1, 2], [3, 4, 5]]) vals = None # writebackifcopy - using context manager a = arange(6, dtype='f4').reshape(2, 3) i, j = np.nested_iters(a, [[0], [1]], op_flags=['readwrite', 'updateifcopy'], casting='same_kind', op_dtypes='f8') with i, j: assert_equal(j[0].dtype, np.dtype('f8')) for x in i: for y in j: y[...] += 1 assert_equal(a, [[0, 1, 2], [3, 4, 5]]) assert_equal(a, [[1, 2, 3], [4, 5, 6]]) # writebackifcopy - using close() a = arange(6, dtype='f4').reshape(2, 3) i, j = np.nested_iters(a, [[0], [1]], op_flags=['readwrite', 'updateifcopy'], casting='same_kind', op_dtypes='f8') assert_equal(j[0].dtype, np.dtype('f8')) for x in i: for y in j: y[...] += 1 assert_equal(a, [[0, 1, 2], [3, 4, 5]]) i.close() j.close() assert_equal(a, [[1, 2, 3], [4, 5, 6]]) def test_dtype_buffered(self): # Test nested iteration with buffering to change dtype a = arange(6, dtype='f4').reshape(2, 3) i, j = np.nested_iters(a, [[0], [1]], flags=['buffered'], op_flags=['readwrite'], casting='same_kind', op_dtypes='f8') assert_equal(j[0].dtype, np.dtype('f8')) for x in i: for y in j: y[...] += 1 assert_equal(a, [[1, 2, 3], [4, 5, 6]]) def test_0d(self): a = np.arange(12).reshape(2, 3, 2) i, j = np.nested_iters(a, [[], [1, 0, 2]]) vals = [list(j) for _ in i] assert_equal(vals, [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]) i, j = np.nested_iters(a, [[1, 0, 2], []]) vals = [list(j) for _ in i] assert_equal(vals, [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]]) i, j, k = np.nested_iters(a, [[2, 0], [], [1]]) vals = [] for x in i: for y in j: vals.append([z for z in k]) assert_equal(vals, [[0, 2, 4], [1, 3, 5], [6, 8, 10], [7, 9, 11]]) def test_iter_nested_iters_dtype_buffered(self): # Test nested iteration with buffering to change dtype a = arange(6, dtype='f4').reshape(2, 3) i, j = np.nested_iters(a, [[0], [1]], flags=['buffered'], op_flags=['readwrite'], casting='same_kind', op_dtypes='f8') with i, j: assert_equal(j[0].dtype, np.dtype('f8')) for x in i: for y in j: y[...] += 1 assert_equal(a, [[1, 2, 3], [4, 5, 6]]) def test_iter_reduction_error(): a = np.arange(6) assert_raises(ValueError, nditer, [a, None], [], [['readonly'], ['readwrite', 'allocate']], op_axes=[[0], [-1]]) a = np.arange(6).reshape(2, 3) assert_raises(ValueError, nditer, [a, None], ['external_loop'], [['readonly'], ['readwrite', 'allocate']], op_axes=[[0, 1], [-1, -1]]) def test_iter_reduction(): # Test doing reductions with the iterator a = np.arange(6) i = nditer([a, None], ['reduce_ok'], [['readonly'], ['readwrite', 'allocate']], op_axes=[[0], [-1]]) # Need to initialize the output operand to the addition unit with i: i.operands[1][...] = 0 # Do the reduction for x, y in i: y[...] += x # Since no axes were specified, should have allocated a scalar assert_equal(i.operands[1].ndim, 0) assert_equal(i.operands[1], np.sum(a)) a = np.arange(6).reshape(2, 3) i = nditer([a, None], ['reduce_ok', 'external_loop'], [['readonly'], ['readwrite', 'allocate']], op_axes=[[0, 1], [-1, -1]]) # Need to initialize the output operand to the addition unit with i: i.operands[1][...] = 0 # Reduction shape/strides for the output assert_equal(i[1].shape, (6,)) assert_equal(i[1].strides, (0,)) # Do the reduction for x, y in i: # Use a for loop instead of ``y[...] += x`` # (equivalent to ``y[...] = y[...].copy() + x``), # because y has zero strides we use for the reduction for j in range(len(y)): y[j] += x[j] # Since no axes were specified, should have allocated a scalar assert_equal(i.operands[1].ndim, 0) assert_equal(i.operands[1], np.sum(a)) # This is a tricky reduction case for the buffering double loop # to handle a = np.ones((2, 3, 5)) it1 = nditer([a, None], ['reduce_ok', 'external_loop'], [['readonly'], ['readwrite', 'allocate']], op_axes=[None, [0, -1, 1]]) it2 = nditer([a, None], ['reduce_ok', 'external_loop', 'buffered', 'delay_bufalloc'], [['readonly'], ['readwrite', 'allocate']], op_axes=[None, [0, -1, 1]], buffersize=10) with it1, it2: it1.operands[1].fill(0) it2.operands[1].fill(0) it2.reset() for x in it1: x[1][...] += x[0] for x in it2: x[1][...] += x[0] assert_equal(it1.operands[1], it2.operands[1]) assert_equal(it2.operands[1].sum(), a.size) def test_iter_buffering_reduction(): # Test doing buffered reductions with the iterator a = np.arange(6) b = np.array(0., dtype='f8').byteswap().newbyteorder() i = nditer([a, b], ['reduce_ok', 'buffered'], [['readonly'], ['readwrite', 'nbo']], op_axes=[[0], [-1]]) with i: assert_equal(i[1].dtype, np.dtype('f8')) assert_(i[1].dtype != b.dtype) # Do the reduction for x, y in i: y[...] += x # Since no axes were specified, should have allocated a scalar assert_equal(b, np.sum(a)) a = np.arange(6).reshape(2, 3) b = np.array([0, 0], dtype='f8').byteswap().newbyteorder() i = nditer([a, b], ['reduce_ok', 'external_loop', 'buffered'], [['readonly'], ['readwrite', 'nbo']], op_axes=[[0, 1], [0, -1]]) # Reduction shape/strides for the output with i: assert_equal(i[1].shape, (3,)) assert_equal(i[1].strides, (0,)) # Do the reduction for x, y in i: # Use a for loop instead of ``y[...] += x`` # (equivalent to ``y[...] = y[...].copy() + x``), # because y has zero strides we use for the reduction for j in range(len(y)): y[j] += x[j] assert_equal(b, np.sum(a, axis=1)) # Iterator inner double loop was wrong on this one p = np.arange(2) + 1 it = np.nditer([p, None], ['delay_bufalloc', 'reduce_ok', 'buffered', 'external_loop'], [['readonly'], ['readwrite', 'allocate']], op_axes=[[-1, 0], [-1, -1]], itershape=(2, 2)) with it: it.operands[1].fill(0) it.reset() assert_equal(it[0], [1, 2, 1, 2]) # Iterator inner loop should take argument contiguity into account x = np.ones((7, 13, 8), np.int8)[4:6,1:11:6,1:5].transpose(1, 2, 0) x[...] = np.arange(x.size).reshape(x.shape) y_base = np.arange(4*4, dtype=np.int8).reshape(4, 4) y_base_copy = y_base.copy() y = y_base[::2,:,None] it = np.nditer([y, x], ['buffered', 'external_loop', 'reduce_ok'], [['readwrite'], ['readonly']]) with it: for a, b in it: a.fill(2) assert_equal(y_base[1::2], y_base_copy[1::2]) assert_equal(y_base[::2], 2) def test_iter_buffering_reduction_reuse_reduce_loops(): # There was a bug triggering reuse of the reduce loop inappropriately, # which caused processing to happen in unnecessarily small chunks # and overran the buffer. a = np.zeros((2, 7)) b = np.zeros((1, 7)) it = np.nditer([a, b], flags=['reduce_ok', 'external_loop', 'buffered'], op_flags=[['readonly'], ['readwrite']], buffersize=5) with it: bufsizes = [x.shape[0] for x, y in it] assert_equal(bufsizes, [5, 2, 5, 2]) assert_equal(sum(bufsizes), a.size) def test_iter_writemasked_badinput(): a = np.zeros((2, 3)) b = np.zeros((3,)) m = np.array([[True, True, False], [False, True, False]]) m2 = np.array([True, True, False]) m3 = np.array([0, 1, 1], dtype='u1') mbad1 = np.array([0, 1, 1], dtype='i1') mbad2 = np.array([0, 1, 1], dtype='f4') # Need an 'arraymask' if any operand is 'writemasked' assert_raises(ValueError, nditer, [a, m], [], [['readwrite', 'writemasked'], ['readonly']]) # A 'writemasked' operand must not be readonly assert_raises(ValueError, nditer, [a, m], [], [['readonly', 'writemasked'], ['readonly', 'arraymask']]) # 'writemasked' and 'arraymask' may not be used together assert_raises(ValueError, nditer, [a, m], [], [['readonly'], ['readwrite', 'arraymask', 'writemasked']]) # 'arraymask' may only be specified once assert_raises(ValueError, nditer, [a, m, m2], [], [['readwrite', 'writemasked'], ['readonly', 'arraymask'], ['readonly', 'arraymask']]) # An 'arraymask' with nothing 'writemasked' also doesn't make sense assert_raises(ValueError, nditer, [a, m], [], [['readwrite'], ['readonly', 'arraymask']]) # A writemasked reduction requires a similarly smaller mask assert_raises(ValueError, nditer, [a, b, m], ['reduce_ok'], [['readonly'], ['readwrite', 'writemasked'], ['readonly', 'arraymask']]) # But this should work with a smaller/equal mask to the reduction operand np.nditer([a, b, m2], ['reduce_ok'], [['readonly'], ['readwrite', 'writemasked'], ['readonly', 'arraymask']]) # The arraymask itself cannot be a reduction assert_raises(ValueError, nditer, [a, b, m2], ['reduce_ok'], [['readonly'], ['readwrite', 'writemasked'], ['readwrite', 'arraymask']]) # A uint8 mask is ok too np.nditer([a, m3], ['buffered'], [['readwrite', 'writemasked'], ['readonly', 'arraymask']], op_dtypes=['f4', None], casting='same_kind') # An int8 mask isn't ok assert_raises(TypeError, np.nditer, [a, mbad1], ['buffered'], [['readwrite', 'writemasked'], ['readonly', 'arraymask']], op_dtypes=['f4', None], casting='same_kind') # A float32 mask isn't ok assert_raises(TypeError, np.nditer, [a, mbad2], ['buffered'], [['readwrite', 'writemasked'], ['readonly', 'arraymask']], op_dtypes=['f4', None], casting='same_kind') def _is_buffered(iterator): try: iterator.itviews except ValueError: return True return False @pytest.mark.parametrize("a", [np.zeros((3,), dtype='f8'), np.zeros((9876, 3*5), dtype='f8')[::2, :], np.zeros((4, 312, 124, 3), dtype='f8')[::2, :, ::2, :]]) def test_iter_writemasked(a): # Note, the slicing above is to ensure that nditer cannot combine multiple # axes into one. The repetition is just to make things a bit more # interesting. shape = a.shape reps = shape[-1] // 3 msk = np.empty(shape, dtype=bool) msk[...] = [True, True, False] * reps # When buffering is unused, 'writemasked' effectively does nothing. # It's up to the user of the iterator to obey the requested semantics. it = np.nditer([a, msk], [], [['readwrite', 'writemasked'], ['readonly', 'arraymask']]) with it: for x, m in it: x[...] = 1 # Because we violated the semantics, all the values became 1 assert_equal(a, np.broadcast_to([1, 1, 1] * reps, shape)) # Even if buffering is enabled, we still may be accessing the array # directly. it = np.nditer([a, msk], ['buffered'], [['readwrite', 'writemasked'], ['readonly', 'arraymask']]) # @seberg: I honestly don't currently understand why a "buffered" iterator # would end up not using a buffer for the small array here at least when # "writemasked" is used, that seems confusing... Check by testing for # actual memory overlap! is_buffered = True with it: for x, m in it: x[...] = 2.5 if np.may_share_memory(x, a): is_buffered = False if not is_buffered: # Because we violated the semantics, all the values became 2.5 assert_equal(a, np.broadcast_to([2.5, 2.5, 2.5] * reps, shape)) else: # For large sizes, the iterator may be buffered: assert_equal(a, np.broadcast_to([2.5, 2.5, 1] * reps, shape)) a[...] = 2.5 # If buffering will definitely happening, for instance because of # a cast, only the items selected by the mask will be copied back from # the buffer. it = np.nditer([a, msk], ['buffered'], [['readwrite', 'writemasked'], ['readonly', 'arraymask']], op_dtypes=['i8', None], casting='unsafe') with it: for x, m in it: x[...] = 3 # Even though we violated the semantics, only the selected values # were copied back assert_equal(a, np.broadcast_to([3, 3, 2.5] * reps, shape)) def test_iter_writemasked_decref(): # force casting (to make it interesting) by using a structured dtype. arr = np.arange(10000).astype(">i,O") original = arr.copy() mask = np.random.randint(0, 2, size=10000).astype(bool) it = np.nditer([arr, mask], ['buffered', "refs_ok"], [['readwrite', 'writemasked'], ['readonly', 'arraymask']], op_dtypes=["<i,O", "?"]) singleton = object() if HAS_REFCOUNT: count = sys.getrefcount(singleton) for buf, mask_buf in it: buf[...] = (3, singleton) del buf, mask_buf, it # delete everything to ensure corrrect cleanup if HAS_REFCOUNT: # The buffer would have included additional items, they must be # cleared correctly: assert sys.getrefcount(singleton) - count == np.count_nonzero(mask) assert_array_equal(arr[~mask], original[~mask]) assert (arr[mask] == np.array((3, singleton), arr.dtype)).all() del arr if HAS_REFCOUNT: assert sys.getrefcount(singleton) == count def test_iter_non_writable_attribute_deletion(): it = np.nditer(np.ones(2)) attr = ["value", "shape", "operands", "itviews", "has_delayed_bufalloc", "iterationneedsapi", "has_multi_index", "has_index", "dtypes", "ndim", "nop", "itersize", "finished"] for s in attr: assert_raises(AttributeError, delattr, it, s) def test_iter_writable_attribute_deletion(): it = np.nditer(np.ones(2)) attr = [ "multi_index", "index", "iterrange", "iterindex"] for s in attr: assert_raises(AttributeError, delattr, it, s) def test_iter_element_deletion(): it = np.nditer(np.ones(3)) try: del it[1] del it[1:2] except TypeError: pass except Exception: raise AssertionError def test_iter_allocated_array_dtypes(): # If the dtype of an allocated output has a shape, the shape gets # tacked onto the end of the result. it = np.nditer(([1, 3, 20], None), op_dtypes=[None, ('i4', (2,))]) for a, b in it: b[0] = a - 1 b[1] = a + 1 assert_equal(it.operands[1], [[0, 2], [2, 4], [19, 21]]) # Check the same (less sensitive) thing when `op_axes` with -1 is given. it = np.nditer(([[1, 3, 20]], None), op_dtypes=[None, ('i4', (2,))], flags=["reduce_ok"], op_axes=[None, (-1, 0)]) for a, b in it: b[0] = a - 1 b[1] = a + 1 assert_equal(it.operands[1], [[0, 2], [2, 4], [19, 21]]) # Make sure this works for scalars too it = np.nditer((10, 2, None), op_dtypes=[None, None, ('i4', (2, 2))]) for a, b, c in it: c[0, 0] = a - b c[0, 1] = a + b c[1, 0] = a * b c[1, 1] = a / b assert_equal(it.operands[2], [[8, 12], [20, 5]]) def test_0d_iter(): # Basic test for iteration of 0-d arrays: i = nditer([2, 3], ['multi_index'], [['readonly']]*2) assert_equal(i.ndim, 0) assert_equal(next(i), (2, 3)) assert_equal(i.multi_index, ()) assert_equal(i.iterindex, 0) assert_raises(StopIteration, next, i) # test reset: i.reset() assert_equal(next(i), (2, 3)) assert_raises(StopIteration, next, i) # test forcing to 0-d i = nditer(np.arange(5), ['multi_index'], [['readonly']], op_axes=[()]) assert_equal(i.ndim, 0) assert_equal(len(i), 1) i = nditer(np.arange(5), ['multi_index'], [['readonly']], op_axes=[()], itershape=()) assert_equal(i.ndim, 0) assert_equal(len(i), 1) # passing an itershape alone is not enough, the op_axes are also needed with assert_raises(ValueError): nditer(np.arange(5), ['multi_index'], [['readonly']], itershape=()) # Test a more complex buffered casting case (same as another test above) sdt = [('a', 'f4'), ('b', 'i8'), ('c', 'c8', (2, 3)), ('d', 'O')] a = np.array(0.5, dtype='f4') i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes=sdt) vals = next(i) assert_equal(vals['a'], 0.5) assert_equal(vals['b'], 0) assert_equal(vals['c'], [[(0.5)]*3]*2) assert_equal(vals['d'], 0.5) def test_object_iter_cleanup(): # see gh-18450 # object arrays can raise a python exception in ufunc inner loops using # nditer, which should cause iteration to stop & cleanup. There were bugs # in the nditer cleanup when decref'ing object arrays. # This test would trigger valgrind "uninitialized read" before the bugfix. assert_raises(TypeError, lambda: np.zeros((17000, 2), dtype='f4') * None) # this more explicit code also triggers the invalid access arr = np.arange(np.BUFSIZE * 10).reshape(10, -1).astype(str) oarr = arr.astype(object) oarr[:, -1] = None assert_raises(TypeError, lambda: np.add(oarr[:, ::-1], arr[:, ::-1])) # followup: this tests for a bug introduced in the first pass of gh-18450, # caused by an incorrect fallthrough of the TypeError class T: def __bool__(self): raise TypeError("Ambiguous") assert_raises(TypeError, np.logical_or.reduce, np.array([T(), T()], dtype='O')) def test_object_iter_cleanup_reduce(): # Similar as above, but a complex reduction case that was previously # missed (see gh-18810). # The following array is special in that it cannot be flattened: arr = np.array([[None, 1], [-1, -1], [None, 2], [-1, -1]])[::2] with pytest.raises(TypeError): np.sum(arr) @pytest.mark.parametrize("arr", [ np.ones((8000, 4, 2), dtype=object)[:, ::2, :], np.ones((8000, 4, 2), dtype=object, order="F")[:, ::2, :], np.ones((8000, 4, 2), dtype=object)[:, ::2, :].copy("F")]) def test_object_iter_cleanup_large_reduce(arr): # More complicated calls are possible for large arrays: out = np.ones(8000, dtype=np.intp) # force casting with `dtype=object` res = np.sum(arr, axis=(1, 2), dtype=object, out=out) assert_array_equal(res, np.full(8000, 4, dtype=object)) def test_iter_too_large(): # The total size of the iterator must not exceed the maximum intp due # to broadcasting. Dividing by 1024 will keep it small enough to # give a legal array. size = np.iinfo(np.intp).max // 1024 arr = np.lib.stride_tricks.as_strided(np.zeros(1), (size,), (0,)) assert_raises(ValueError, nditer, (arr, arr[:, None])) # test the same for multiindex. That may get more interesting when # removing 0 dimensional axis is allowed (since an iterator can grow then) assert_raises(ValueError, nditer, (arr, arr[:, None]), flags=['multi_index']) def test_iter_too_large_with_multiindex(): # When a multi index is being tracked, the error is delayed this # checks the delayed error messages and getting below that by # removing an axis. base_size = 2**10 num = 1 while base_size**num < np.iinfo(np.intp).max: num += 1 shape_template = [1, 1] * num arrays = [] for i in range(num): shape = shape_template[:] shape[i * 2] = 2**10 arrays.append(np.empty(shape)) arrays = tuple(arrays) # arrays are now too large to be broadcast. The different modes test # different nditer functionality with or without GIL. for mode in range(6): with assert_raises(ValueError): _multiarray_tests.test_nditer_too_large(arrays, -1, mode) # but if we do nothing with the nditer, it can be constructed: _multiarray_tests.test_nditer_too_large(arrays, -1, 7) # When an axis is removed, things should work again (half the time): for i in range(num): for mode in range(6): # an axis with size 1024 is removed: _multiarray_tests.test_nditer_too_large(arrays, i*2, mode) # an axis with size 1 is removed: with assert_raises(ValueError): _multiarray_tests.test_nditer_too_large(arrays, i*2 + 1, mode) def test_writebacks(): a = np.arange(6, dtype='f4') au = a.byteswap().newbyteorder() assert_(a.dtype.byteorder != au.dtype.byteorder) it = nditer(au, [], [['readwrite', 'updateifcopy']], casting='equiv', op_dtypes=[np.dtype('f4')]) with it: it.operands[0][:] = 100 assert_equal(au, 100) # do it again, this time raise an error, it = nditer(au, [], [['readwrite', 'updateifcopy']], casting='equiv', op_dtypes=[np.dtype('f4')]) try: with it: assert_equal(au.flags.writeable, False) it.operands[0][:] = 0 raise ValueError('exit context manager on exception') except: pass assert_equal(au, 0) assert_equal(au.flags.writeable, True) # cannot reuse i outside context manager assert_raises(ValueError, getattr, it, 'operands') it = nditer(au, [], [['readwrite', 'updateifcopy']], casting='equiv', op_dtypes=[np.dtype('f4')]) with it: x = it.operands[0] x[:] = 6 assert_(x.flags.writebackifcopy) assert_equal(au, 6) assert_(not x.flags.writebackifcopy) x[:] = 123 # x.data still valid assert_equal(au, 6) # but not connected to au it = nditer(au, [], [['readwrite', 'updateifcopy']], casting='equiv', op_dtypes=[np.dtype('f4')]) # reentering works with it: with it: for x in it: x[...] = 123 it = nditer(au, [], [['readwrite', 'updateifcopy']], casting='equiv', op_dtypes=[np.dtype('f4')]) # make sure exiting the inner context manager closes the iterator with it: with it: for x in it: x[...] = 123 assert_raises(ValueError, getattr, it, 'operands') # do not crash if original data array is decrefed it = nditer(au, [], [['readwrite', 'updateifcopy']], casting='equiv', op_dtypes=[np.dtype('f4')]) del au with it: for x in it: x[...] = 123 # make sure we cannot reenter the closed iterator enter = it.__enter__ assert_raises(RuntimeError, enter) def test_close_equivalent(): ''' using a context amanger and using nditer.close are equivalent ''' def add_close(x, y, out=None): addop = np.add it = np.nditer([x, y, out], [], [['readonly'], ['readonly'], ['writeonly','allocate']]) for (a, b, c) in it: addop(a, b, out=c) ret = it.operands[2] it.close() return ret def add_context(x, y, out=None): addop = np.add it = np.nditer([x, y, out], [], [['readonly'], ['readonly'], ['writeonly','allocate']]) with it: for (a, b, c) in it: addop(a, b, out=c) return it.operands[2] z = add_close(range(5), range(5)) assert_equal(z, range(0, 10, 2)) z = add_context(range(5), range(5)) assert_equal(z, range(0, 10, 2)) def test_close_raises(): it = np.nditer(np.arange(3)) assert_equal (next(it), 0) it.close() assert_raises(StopIteration, next, it) assert_raises(ValueError, getattr, it, 'operands') def test_close_parameters(): it = np.nditer(np.arange(3)) assert_raises(TypeError, it.close, 1) @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts") def test_warn_noclose(): a = np.arange(6, dtype='f4') au = a.byteswap().newbyteorder() with suppress_warnings() as sup: sup.record(RuntimeWarning) it = np.nditer(au, [], [['readwrite', 'updateifcopy']], casting='equiv', op_dtypes=[np.dtype('f4')]) del it assert len(sup.log) == 1 @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts") @pytest.mark.parametrize(["in_dtype", "buf_dtype"], [("i", "O"), ("O", "i"), # most simple cases ("i,O", "O,O"), # structured partially only copying O ("O,i", "i,O"), # structured casting to and from O ]) @pytest.mark.parametrize("steps", [1, 2, 3]) def test_partial_iteration_cleanup(in_dtype, buf_dtype, steps): value = 123 # relies on python cache (leak-check will still find it) arr = np.full(int(np.BUFSIZE * 2.5), value).astype(in_dtype) count = sys.getrefcount(value) it = np.nditer(arr, op_dtypes=[np.dtype(buf_dtype)], flags=["buffered", "external_loop", "refs_ok"], casting="unsafe") for step in range(steps): # The iteration finishes in 3 steps, the first two are partial next(it) # Note that resetting does not free references del it assert count == sys.getrefcount(value) # Repeat the test with `iternext` it = np.nditer(arr, op_dtypes=[np.dtype(buf_dtype)], flags=["buffered", "external_loop", "refs_ok"], casting="unsafe") for step in range(steps): it.iternext() del it # should ensure cleanup assert count == sys.getrefcount(value) @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts") @pytest.mark.parametrize(["in_dtype", "buf_dtype"], [("O", "i"), # most simple cases ("O,i", "i,O"), # structured casting to and from O ]) def test_partial_iteration_error(in_dtype, buf_dtype): value = 123 # relies on python cache (leak-check will still find it) arr = np.full(int(np.BUFSIZE * 2.5), value).astype(in_dtype) if in_dtype == "O": arr[int(np.BUFSIZE * 1.5)] = None else: arr[int(np.BUFSIZE * 1.5)]["f0"] = None count = sys.getrefcount(value) it = np.nditer(arr, op_dtypes=[np.dtype(buf_dtype)], flags=["buffered", "external_loop", "refs_ok"], casting="unsafe") with pytest.raises(TypeError): # pytest.raises seems to have issues with the error originating # in the for loop, so manually unravel: next(it) next(it) # raises TypeError # Repeat the test with `iternext` after resetting, the buffers should # already be cleared from any references, so resetting is sufficient. it.reset() with pytest.raises(TypeError): it.iternext() it.iternext() assert count == sys.getrefcount(value) def test_debug_print(capfd): """ Matches the expected output of a debug print with the actual output. Note that the iterator dump should not be considered stable API, this test is mainly to ensure the print does not crash. Currently uses a subprocess to avoid dealing with the C level `printf`s. """ # the expected output with all addresses and sizes stripped (they vary # and/or are platform dependend). expected = """ ------ BEGIN ITERATOR DUMP ------ | Iterator Address: | ItFlags: BUFFER REDUCE REUSE_REDUCE_LOOPS | NDim: 2 | NOp: 2 | IterSize: 50 | IterStart: 0 | IterEnd: 50 | IterIndex: 0 | Iterator SizeOf: | BufferData SizeOf: | AxisData SizeOf: | | Perm: 0 1 | DTypes: | DTypes: dtype('float64') dtype('int32') | InitDataPtrs: | BaseOffsets: 0 0 | Operands: | Operand DTypes: dtype('int64') dtype('float64') | OpItFlags: | Flags[0]: READ CAST ALIGNED | Flags[1]: READ WRITE CAST ALIGNED REDUCE | | BufferData: | BufferSize: 50 | Size: 5 | BufIterEnd: 5 | REDUCE Pos: 0 | REDUCE OuterSize: 10 | REDUCE OuterDim: 1 | Strides: 8 4 | Ptrs: | REDUCE Outer Strides: 40 0 | REDUCE Outer Ptrs: | ReadTransferFn: | ReadTransferData: | WriteTransferFn: | WriteTransferData: | Buffers: | | AxisData[0]: | Shape: 5 | Index: 0 | Strides: 16 8 | Ptrs: | AxisData[1]: | Shape: 10 | Index: 0 | Strides: 80 0 | Ptrs: ------- END ITERATOR DUMP ------- """.strip().splitlines() arr1 = np.arange(100, dtype=np.int64).reshape(10, 10)[:, ::2] arr2 = np.arange(5.) it = np.nditer((arr1, arr2), op_dtypes=["d", "i4"], casting="unsafe", flags=["reduce_ok", "buffered"], op_flags=[["readonly"], ["readwrite"]]) it.debug_print() res = capfd.readouterr().out res = res.strip().splitlines() assert len(res) == len(expected) for res_line, expected_line in zip(res, expected): # The actual output may have additional pointers listed that are # stripped from the example output: assert res_line.startswith(expected_line.strip())
py
1a4b68398a4946f1b6da3b60c8e939936d91eca7
# -*- coding: utf-8 -*- """ Created on Mon Jun 28 23:36:19 2021 @author: Bruno Ferrari """ _path="C:/Users/Bruno Ferrari/Documents/Bruno/2019/2s/MC/artigos revisão/Artigos Mes/GD/" import pandas as pd import numpy as np import bgraph from concurrent.futures import ThreadPoolExecutor def gera_txt(nome, G): with open((_path+'bdp/dbdp_instances/GraphData/{inst}.txt').format(inst=nome), 'w') as arq: arq.write(str(G.v1()) + "\n") arq.write(str(G.v2()) + "\n") arq.write(str(G.edges())) def gera_grafo(path): graph_data = pd.read_csv(path) graph_data_exp = graph_data.iloc[:,0].str.split(" ",expand=True) n_1 = int(graph_data_exp.iloc[0,0]) n_2 = int(graph_data_exp.iloc[0,1]) graph_edges = [] for key, row in enumerate(graph_data.iloc[1:n_1+1, 0]): for adj in row.split(" ")[2:]: #if int(adj) not in graph_adj_nodes_incre: graph_edges.append((key, int(adj))) order_1 = dict(zip(range(0,n_1),graph_data_exp.iloc[1:n_1+1,1].astype(int).values + 1)) order_2 = dict(zip(range(n_1,n_1+n_2),graph_data_exp.iloc[n_1+1:n_1+1+n_2,1].astype(int).values + 1)) New = bgraph.BGraph() New.v1(list(order_1.keys())) New.v2(list(order_2.keys())) New.edges(graph_edges) return New # Descosidera vertices adicionais (ie com linha == 0) def gera_grafo_2(path): graph_data = pd.read_csv(path) graph_data_exp = graph_data.iloc[:,0].str.split(" ",expand=True) n_1 = int(graph_data_exp.iloc[0,0]) aux = (graph_data_exp[1:n_1+1].set_index(0).loc['1']) graph_edges = [(int(v), int(u)) for v in aux.iloc[:,0] for u in aux.set_index(1).loc[v] if u != None] New = bgraph.BGraph() New.v1(np.unique(np.array(np.matrix(graph_edges)[:,0])).tolist()) New.v2(np.unique(np.array(np.matrix(graph_edges)[:,1])).tolist()) New.edges(graph_edges) return New def gera_features(output): for o in output: _aux_deg = list(o.degree().values()) yield (o.n_v(), #vertices o.n_edge(), #arestas abs(o.n_v1() - o.n_v2()), #diff nos np.mean(_aux_deg), #mean deg np.std(_aux_deg), #std deg np.median(_aux_deg), #median deg o.density(), #graph density o.n_v1(), o.n_v2(), np.min(_aux_deg)) #aux feat #df_results = pd.read_excel(_path+"bdp/dbdp_instances/stallman_reduced.xlsx", sheet_name="Sheet1") df_results = pd.read_excel(_path+"bdp/dbdp_instances/instance.xlsx", sheet_name="instances").replace(regex=".txt", value="") output_list = [] for i in range(130)[::10]: with ThreadPoolExecutor(6) as ex: output = list(ex.map(lambda x: gera_grafo_2((_path+"bdp/dbdp_instances/instances/{name}.txt").format(name=x)), list(df_results['Instance'][i-10:i]))) with ThreadPoolExecutor(6) as ex: ex.map(lambda x: gera_txt(x[0], x[1]), zip(df_results['Instance'][i-10:i].replace(regex="inc", value=""), output)) output_list.append(pd.DataFrame(gera_features(output))) if not(i % 20): with pd.ExcelWriter(_path+"bdp/dbdp_instances/metafeat3.xlsx") as writer: pd.concat([df_results.replace(regex="inc", value=""), pd.concat(output_list, axis=0).reset_index(drop=True)],axis=1).to_excel(writer, sheet_name="results", index=False)
py
1a4b696f7e8ed2abef2c984cd8faeae504d66ee7
# Deepforest Preprocessing model """The preprocessing module is used to reshape data into format suitable for training or prediction. For example cutting large tiles into smaller images. """ import os import numpy as np import pandas as pd try: import slidingwindow from PIL import Image except: pass def image_name_from_path(image_path): """Convert path to image name for use in indexing.""" image_name = os.path.basename(image_path) image_name = os.path.splitext(image_name)[0] return image_name def compute_windows(numpy_image, patch_size, patch_overlap): """Create a sliding window object from a raster tile. Args: numpy_image (array): Raster object as numpy array to cut into crops Returns: windows (list): a sliding windows object """ if patch_overlap > 1: raise ValueError("Patch overlap {} must be between 0 - 1".format(patch_overlap)) # Generate overlapping sliding windows windows = slidingwindow.generate(numpy_image, slidingwindow.DimOrder.HeightWidthChannel, patch_size, patch_overlap) return (windows) def select_annotations(annotations, windows, index, allow_empty=False): """Select annotations that overlap with selected image crop. Args: image_name (str): Name of the image in the annotations file to lookup. annotations_file: path to annotations file in the format -> image_path, xmin, ymin, xmax, ymax, label windows: A sliding window object (see compute_windows) index: The index in the windows object to use a crop bounds allow_empty (bool): If True, allow window crops that have no annotations to be included Returns: selected_annotations: a pandas dataframe of annotations """ # Window coordinates - with respect to tile window_xmin, window_ymin, w, h = windows[index].getRect() window_xmax = window_xmin + w window_ymax = window_ymin + h # buffer coordinates a bit to grab boxes that might start just against # the image edge. Don't allow boxes that start and end after the offset offset = 40 selected_annotations = annotations[(annotations.xmin > (window_xmin - offset)) & (annotations.xmin < (window_xmax)) & (annotations.xmax > (window_xmin)) & (annotations.ymin > (window_ymin - offset)) & (annotations.xmax < (window_xmax + offset)) & (annotations.ymin < (window_ymax)) & (annotations.ymax > (window_ymin)) & (annotations.ymax < (window_ymax + offset))].copy() # change the image name image_name = os.path.splitext("{}".format(annotations.image_path.unique()[0]))[0] image_basename = os.path.splitext(image_name)[0] selected_annotations.image_path = "{}_{}.png".format(image_basename, index) # If no matching annotations, return a line with the image name, but no records if selected_annotations.empty: if allow_empty: selected_annotations = pd.DataFrame( ["{}_{}.png".format(image_basename, index)], columns=["image_path"]) selected_annotations["xmin"] = "" selected_annotations["ymin"] = "" selected_annotations["xmax"] = "" selected_annotations["ymax"] = "" selected_annotations["label"] = "" else: return None else: # update coordinates with respect to origin selected_annotations.xmax = (selected_annotations.xmin - window_xmin) + ( selected_annotations.xmax - selected_annotations.xmin) selected_annotations.xmin = (selected_annotations.xmin - window_xmin) selected_annotations.ymax = (selected_annotations.ymin - window_ymin) + ( selected_annotations.ymax - selected_annotations.ymin) selected_annotations.ymin = (selected_annotations.ymin - window_ymin) # cut off any annotations over the border. selected_annotations.loc[selected_annotations.xmin < 0, "xmin"] = 0 selected_annotations.loc[selected_annotations.xmax > w, "xmax"] = w selected_annotations.loc[selected_annotations.ymin < 0, "ymin"] = 0 selected_annotations.loc[selected_annotations.ymax > h, "ymax"] = h return selected_annotations def save_crop(base_dir, image_name, index, crop): """Save window crop as image file to be read by PIL. Filename should match the image_name + window index """ # create dir if needed if not os.path.exists(base_dir): os.makedirs(base_dir) im = Image.fromarray(crop) image_basename = os.path.splitext(image_name)[0] filename = "{}/{}_{}.png".format(base_dir, image_basename, index) im.save(filename) return filename def split_raster(path_to_raster, annotations_file, base_dir=".", patch_size=400, patch_overlap=0.05, allow_empty=False): """Divide a large tile into smaller arrays. Each crop will be saved to file. Args: path_to_raster: (str): Path to a tile that can be read by rasterio on disk annotations_file (str): Path to annotations file (with column names) data in the format -> image_path, xmin, ymin, xmax, ymax, label base_dir (str): Where to save the annotations and image crops relative to current working dir patch_size (int): Maximum dimensions of square window patch_overlap (float): Percent of overlap among windows 0->1 allow_empty: If True, include images with no annotations to be included in the dataset Returns: A pandas dataframe with annotations file for training. """ # Load raster as image raster = Image.open(path_to_raster) numpy_image = np.array(raster) # Check that its 3 band bands = numpy_image.shape[2] if not bands == 3: raise IOError("Input file {} has {} bands. DeepForest only accepts 3 band RGB " "rasters in the order (height, width, channels). " "If the image was cropped and saved as a .jpg, " "please ensure that no alpha channel was used.".format( path_to_raster, bands)) # Check that patch size is greater than image size height = numpy_image.shape[0] width = numpy_image.shape[1] if any(np.array([height, width]) < patch_size): raise ValueError("Patch size of {} is larger than the image dimensions {}".format( patch_size, [height, width])) # Compute sliding window index windows = compute_windows(numpy_image, patch_size, patch_overlap) # Get image name for indexing image_name = os.path.basename(path_to_raster) # Load annotations file and coerce dtype annotations = pd.read_csv(annotations_file) # open annotations file image_annotations = annotations[annotations.image_path == image_name].copy() # Sanity checks if image_annotations.empty: raise ValueError( "No image names match between the file:{} and the image_path: {}. " "Reminder that image paths should be the relative " "path (e.g. 'image_name.tif'), not the full path " "(e.g. path/to/dir/image_name.tif)".format(annotations_file, image_name)) if not annotations.shape[1] == 6: raise ValueError("Annotations file has {} columns, should have " "format image_path, xmin, ymin, xmax, ymax, label".format( annotations.shape[1])) annotations_files = [] for index, window in enumerate(windows): #Crop image crop = numpy_image[windows[index].indices()] # Find annotations, image_name is the basename of the path crop_annotations = select_annotations(image_annotations, windows, index, allow_empty) # If empty images not allowed, select annotations returns None if crop_annotations is not None: # save annotations annotations_files.append(crop_annotations) # save image crop save_crop(base_dir, image_name, index, crop) if len(annotations_files) == 0: raise ValueError( "Input file has no overlapping annotations and allow_empty is {}".format( allow_empty)) annotations_files = pd.concat(annotations_files) # Checkpoint csv files, useful for parallelization # Use filename of the raster path to save the annotations image_basename = os.path.splitext(image_name)[0] file_path = image_basename + ".csv" file_path = os.path.join(base_dir, file_path) annotations_files.to_csv(file_path, index=False, header=False) return annotations_files
py
1a4b6bb5119b01060e2cb4005e8de3a417e4ca0c
url = 'https://data.gov.tw/dataset/116285' import pandas as pd dataset = pd.read_json('53a72b2dcfdd9ecae43afda4b86089be_export.json', encoding='utf-8') dataset.columns dataset['縣市'] = dataset['醫事機構地址'].str.slice(0, 3) dataset[(dataset['成人口罩剩餘數']+dataset['兒童口罩剩餘數'])<50]
py
1a4b6f7233941895795387c74f6106b341b5edda
# pip allows us to install packages from the pypi.org official website and use in our programs import pyjokes joke = pyjokes.get_joke("en", "neutral") print(joke)
py
1a4b707232ae09de50297383c31fbcdc5298154a
# # Copyright (c) 2020 by Delphix. All rights reserved. # ####################################################################################################################### """ CommandFactory class contains all commands required to perform couchbase and OS related operations These are a list of commands which are being used in this project. Have segregated both types of commands into two classes DatabaseCommand and OSCommand. CommandFactory is the actual class through which the command string will be returned. In the last section of this file, we have created small tests for all these commands with dummy values. Through which we can see the actual command is going to execute. All methods are decorated to @staticmethod, so no need to create an object of the class, we can use the direct class name to use any command method """ ####################################################################################################################### class OSCommand(object): def __init__(self): pass @staticmethod def find_binary_path(): return "echo $COUCHBASE_PATH" @staticmethod def find_install_path(binary_path): return "find {binary_path} -name couchbase-server".format(binary_path=binary_path) @staticmethod def find_shell_path(binary_path): return "find {binary_path} -name couchbase-cli".format(binary_path=binary_path) @staticmethod def get_process(): return "ps -ef" @staticmethod def make_directory(directory_path): return "mkdir -p {directory_path}".format(directory_path=directory_path) @staticmethod def change_permission(directory_path): return "chmod -R 775 {directory_path}".format(directory_path=directory_path) @staticmethod def get_config_directory(mount_path): return "{mount_path}/.delphix".format(mount_path=mount_path) @staticmethod def read_file(filename): return "cat {filename}".format(filename=filename) @staticmethod def check_file(file_path): return "[ -f {file_path} ] && echo 'Found'".format(file_path=file_path) @staticmethod def write_file(filename, data): return "echo {data} > {filename}".format(filename=filename, data=data) @staticmethod def get_ip_of_hostname(): return "hostname -i" @staticmethod def check_directory(dir_path): return "[ -d {dir_path} ] && echo 'Found'".format(dir_path=dir_path) @staticmethod def delete_file(filename): return "rm -f {filename}".format(filename=filename) @staticmethod def get_dlpx_bin(): return "echo $DLPX_BIN_JQ" @staticmethod def unmount_file_system(mount_path): return "sudo /bin/umount {mount_path}".format(mount_path=mount_path) class DatabaseCommand(object): def __init__(self): pass @staticmethod def start_couchbase(install_path): return "{install_path} \-- -noinput -detached .".format(install_path=install_path) @staticmethod def get_version(install_path): return "{install_path} --version".format(install_path=install_path) @staticmethod def get_data_directory(couchbase_base_dir): return "cat {couchbase_base_dir}/etc/couchbase/static_config|grep path_config_datadir".format( couchbase_base_dir=couchbase_base_dir) @staticmethod def stop_couchbase(install_path): return "{install_path} -k".format(install_path=install_path) @staticmethod def cluster_init(shell_path, hostname, port, username, cluster_ramsize, cluster_name, cluster_index_ramsize, cluster_fts_ramsize, cluster_eventing_ramsize, cluster_analytics_ramsize, additional_services ): return "{shell_path} cluster-init --cluster {hostname}:{port} --cluster-username {username} --cluster-password $password --cluster-ramsize {cluster_ramsize} --cluster-name {cluster_name} --cluster-index-ramsize {cluster_index_ramsize} --cluster-fts-ramsize {cluster_fts_ramsize} --cluster-eventing-ramsize {cluster_eventing_ramsize} --cluster-analytics-ramsize {cluster_analytics_ramsize} --services data,index,{additional_services}".format( shell_path=shell_path, hostname=hostname, username=username, port=port, cluster_ramsize=cluster_ramsize, cluster_name=cluster_name, cluster_index_ramsize=cluster_index_ramsize, cluster_fts_ramsize=cluster_fts_ramsize, cluster_eventing_ramsize=cluster_eventing_ramsize, cluster_analytics_ramsize=cluster_analytics_ramsize, additional_services=additional_services ) @staticmethod def cluster_setting(shell_path, hostname, port, username, cluster_ramsize, cluster_name, cluster_index_ramsize, cluster_fts_ramsize, cluster_eventing_ramsize, cluster_analytics_ramsize): return "{shell_path} setting-cluster -c {hostname}:{port} -u {username} -p $password --cluster-ramsize {cluster_ramsize} --cluster-name {cluster_name} --cluster-index-ramsize {cluster_index_ramsize} --cluster-fts-ramsize {cluster_fts_ramsize} --cluster-eventing-ramsize {cluster_eventing_ramsize} --cluster-analytics-ramsize {cluster_analytics_ramsize}".format( shell_path=shell_path, hostname=hostname, port=port, username=username, cluster_ramsize=cluster_ramsize, cluster_name=cluster_name, cluster_index_ramsize=cluster_index_ramsize, cluster_fts_ramsize=cluster_fts_ramsize, cluster_eventing_ramsize=cluster_eventing_ramsize, cluster_analytics_ramsize=cluster_analytics_ramsize ) @staticmethod def xdcr_setup(shell_path, source_hostname, source_port, source_username, hostname, port, username, cluster_name): return "{shell_path} xdcr-setup --cluster {source_hostname}:{source_port} --username {source_username} --password $source_password --create --xdcr-hostname {hostname}:{port} --xdcr-username {username} --xdcr-password $password --xdcr-cluster-name {cluster_name}".format( shell_path=shell_path, source_hostname=source_hostname, source_port=source_port, source_username=source_username, hostname=hostname, port=port, username=username, cluster_name=cluster_name ) @staticmethod def xdcr_replicate(shell_path, source_hostname, source_port, source_username, source_bucket_name, target_bucket_name, cluster_name, hostname, port, username): return "{shell_path} xdcr-replicate --cluster {source_hostname}:{source_port} --username {source_username} --password $source_password --create --xdcr-from-bucket {source_bucket_name} --xdcr-to-bucket {target_bucket_name} --xdcr-cluster-name {cluster_name}".format( shell_path=shell_path, source_hostname=source_hostname, source_port=source_port, source_username=source_username, source_bucket_name=source_bucket_name, target_bucket_name=target_bucket_name, cluster_name=cluster_name ) @staticmethod def get_replication_uuid(shell_path, source_hostname, source_port, source_username): return "{shell_path} xdcr-setup --cluster {source_hostname}:{source_port} --username {source_username} --password $source_password --list".format( shell_path=shell_path, source_hostname=source_hostname, source_port=source_port, source_username=source_username, ) @staticmethod def get_stream_id(shell_path, source_hostname, source_port, source_username, cluster_name): return "{shell_path} xdcr-replicate --cluster {source_hostname}:{source_port} --username {source_username} --password $source_password --xdcr-cluster-name {cluster_name} --list".format( shell_path=shell_path, source_hostname=source_hostname, source_port=source_port, source_username=source_username, cluster_name=cluster_name ) @staticmethod def pause_replication(shell_path, source_hostname, source_port, source_username, cluster_name, id): return "{shell_path} xdcr-replicate --cluster {source_hostname}:{source_port} --username {source_username} --password $source_password --xdcr-cluster-name {cluster_name} --pause --xdcr-replicator={id}".format( shell_path=shell_path, source_hostname=source_hostname, source_port=source_port, source_username=source_username, cluster_name=cluster_name, id=id ) @staticmethod def resume_replication(shell_path, source_hostname, source_port, source_username, cluster_name, id): return "{shell_path} xdcr-replicate --cluster {source_hostname}:{source_port} --username {source_username} --password $source_password --xdcr-cluster-name {cluster_name} --resume --xdcr-replicator={id}".format( shell_path=shell_path, source_hostname=source_hostname, source_port=source_port, source_username=source_username, cluster_name=cluster_name, id=id ) @staticmethod def delete_replication(shell_path, source_hostname, source_port, source_username, id, cluster_name): return "{shell_path} xdcr-replicate --cluster {source_hostname}:{source_port} --username {source_username} --password $source_password --delete --xdcr-replicator {id} --xdcr-cluster-name {cluster_name}".format( shell_path=shell_path, source_hostname=source_hostname, source_port=source_port, source_username=source_username, id=id, cluster_name=cluster_name ) @staticmethod def xdcr_delete(shell_path, source_hostname, source_port, source_username, hostname, port, username, cluster_name): return "{shell_path} xdcr-setup --cluster {source_hostname}:{source_port} --username {source_username} --password $source_password --delete --xdcr-hostname {hostname}:{port} --xdcr-username {username} --xdcr-password $password --xdcr-cluster-name {cluster_name}".format( shell_path=shell_path, source_hostname=source_hostname, source_port=source_port, source_username=source_username, hostname=hostname, port=port, username=username, cluster_name=cluster_name ) @staticmethod def get_source_bucket_list(shell_path, source_hostname, source_port, source_username): return "{shell_path} bucket-list --cluster {source_hostname}:{source_port} --username {source_username} --password $password".format( shell_path=shell_path, source_hostname=source_hostname, source_port=source_port, source_username=source_username, ) @staticmethod def get_status(shell_path, hostname, port, username): return "{shell_path} server-info --cluster {hostname}:{port} --username {username} --password $password".format( shell_path=shell_path, hostname=hostname, port=port, username=username ) @staticmethod def node_init(shell_path, port, username, mount_path): return "{shell_path} node-init --cluster 127.0.0.1:{port} --username {username} --password $password --node-init-data-path {mount_path}/data --node-init-index-path {mount_path}/data --node-init-analytics-path {mount_path}/data --node-init-hostname 127.0.0.1".format( shell_path=shell_path, port=port, username=username, mount_path=mount_path ) @staticmethod def bucket_edit(shell_path, hostname, port, username, bucket_name, flush_value): return "{shell_path} bucket-edit --cluster {hostname}:{port} --username {username} --password $password --bucket={bucket_name} --enable-flush {flush_value}".format( shell_path=shell_path, hostname=hostname, port=port, username=username, bucket_name=bucket_name, flush_value=flush_value ) @staticmethod def bucket_edit_ramquota(shell_path, hostname, port, username, bucket_name, ramsize): return "{shell_path} bucket-edit --cluster {hostname}:{port} --username {username} --password $password --bucket={bucket_name} --bucket-ramsize {ramsize}".format( shell_path=shell_path, hostname=hostname, port=port, username=username, bucket_name=bucket_name, ramsize=ramsize ) @staticmethod def bucket_delete(shell_path, hostname, port, username, bucket_name): return "{shell_path} bucket-delete --cluster {hostname}:{port} --username {username} --password $password --bucket={bucket_name}".format( shell_path=shell_path, hostname=hostname, port=port, username=username, bucket_name=bucket_name ) @staticmethod def bucket_flush(shell_path, hostname, port, username, bucket_name): return "echo 'Yes' | {shell_path} bucket-flush --cluster {hostname}:{port} --username {username} --password $password --bucket={bucket_name}".format( shell_path=shell_path, hostname=hostname, port=port, username=username, bucket_name=bucket_name ) @staticmethod def bucket_create(shell_path, hostname, port, username, bucket_name, ramsize, evictionpolicy): return "{shell_path} bucket-create --cluster 127.0.0.1:{port} --username {username} --password $password --bucket {bucket_name} --bucket-type couchbase --bucket-ramsize {ramsize} --bucket-replica 0 --bucket-eviction-policy {evictionpolicy} --compression-mode passive --conflict-resolution sequence --wait".format( shell_path=shell_path, port=port, username=username, bucket_name=bucket_name, ramsize=ramsize, evictionpolicy=evictionpolicy ) @staticmethod def bucket_list(shell_path, hostname, port, username): return "{shell_path} bucket-list --cluster {hostname}:{port} --username {username} --password $password".format( shell_path=shell_path, hostname=hostname, port=port, username=username, ) @staticmethod def get_indexes_name(base_path, hostname, port, username, index): return "{base_path}/cbq -e {hostname}:{port} -u {username} -p $password -q=true -s=\"SELECT name FROM system:indexes where keyspace_id = {index} AND state = 'deferred'\"".format( base_path=base_path, hostname=hostname, port=port, username=username, index=index ) @staticmethod def build_index(base_path, hostname, port, username, index_name): return "{base_path}/cbq -e {hostname}:{port} -u {username} -p $password -q=true -s=echo \"BUILD INDEX ON {index_name}\" {index_name}".format( base_path=base_path, hostname=hostname, port=port, username=username, index_name=index_name ) @staticmethod def is_build_completed(base_path, hostname, port, username, index): return "{base_path}/cbq -e {hostname}:{port} -u {username} -p $password -q=true -s=\"SELECT COUNT(*) as unbuilt FROM system:indexes WHERE keyspace_id ={index} AND state <> 'online'".format( base_path=base_path, hostname=hostname, port=port, username=username, index=index ) @staticmethod def cb_backup_full(base_path, backup_location, backup_repo, hostname, port, username, csv_bucket_list): return "{base_path}/cbbackupmgr restore --archive {backup_location} --repo {backup_repo} --cluster couchbase://{hostname}:{port} --username {username} --password $password --force-updates --no-progress-bar --include-buckets {csv_bucket_list}".format( base_path=base_path, backup_location=backup_location, backup_repo=backup_repo, hostname=hostname, port=port, username=username, csv_bucket_list=csv_bucket_list ) @staticmethod def monitor_replication(source_username, source_hostname, source_port, bucket_name, uuid): return "curl --silent -u {source_username}:$password http://{source_hostname}:{source_port}/pools/default/buckets/{bucket_name}/stats/replications%2F{uuid}%2F{bucket_name}%2F{bucket_name}%2Fchanges_left".format( source_username=source_username, source_hostname=source_hostname, source_port=source_port, bucket_name=bucket_name, uuid=uuid, ) @staticmethod def server_info(shell_path, hostname, port, username): return "{shell_path} server-info --cluster {hostname}:{port} --username {username} --password $password ".format( shell_path=shell_path, hostname=hostname, port=port, username=username ) class CommandFactory(DatabaseCommand, OSCommand): def __init__(self): DatabaseCommand.__init__(self) OSCommand.__init__(self) if __name__ == "__main__": print("\n****Test Above Commands With Dummy Values****\n") install_path = "DummyInstallPath" binary_path = "/opt/couchbase/bin" shell_path = "/opt/couchbase/bin" hostname = "hostname" port = "8091" username = "USER" cluster_name = "delphix_cluster" cluster_ramsize = "200" cluster_index_ramsize = "300" cluster_fts_ramsize = "400" cluster_eventing_ramsize = "500" cluster_analytics_ramsize = "600" additional_services = "query,index,data" source_hostname = "source_hostname" source_port = "source_port" source_username = "source_username" source_bucket_name = "source_bucket_name" target_bucket_name = "target_bucket_name" uuid = "12345678" directory_path = "/mnt/provision/test/directory_path" mount_path = "/mnt/provision/mount_path" bucket_name = "sample" flush_value = "0" ramsize = "100" evictionpolicy = "evictionpolicy" base_path = "base_path" index = "index" index_name = "index_name" backup_location = "backup_location" backup_repo = "backup_repo" csv_bucket_list = "csv_bucket_list" filename = "filename" file_path = "test" data = "data" hostname = "192.168.1.14" dir_path = "/var/tmp" DLPX_BIN_JQ = "/var/tmp" print "find_install_path: ", CommandFactory.find_install_path(binary_path), "\n" print "find_binary_path: ", CommandFactory.find_binary_path(), "\n" print "find_install_path: ", CommandFactory.find_install_path(binary_path), "\n" print "get_process: ", CommandFactory.get_process(), "\n" print "get_version: ", CommandFactory.get_version(install_path), "\n" print "start_couchbase: ", CommandFactory.start_couchbase(install_path), "\n" print "get_data_directory: ", CommandFactory.get_data_directory("couchbase_base_dir"), "\n" print "install_path: ", CommandFactory.stop_couchbase(install_path), "\n" print "cluster_init: ", CommandFactory.cluster_init(shell_path, hostname, port, username, cluster_ramsize, cluster_name, cluster_index_ramsize, cluster_fts_ramsize, cluster_eventing_ramsize, cluster_analytics_ramsize, additional_services), "\n" print "cluster_setting: ", CommandFactory.cluster_setting(shell_path, hostname, port, username, cluster_ramsize, cluster_name, cluster_index_ramsize, cluster_fts_ramsize, cluster_eventing_ramsize, cluster_analytics_ramsize), "\n" print "xdcr_setup: ", CommandFactory.xdcr_setup(shell_path, source_hostname, source_port, source_username, hostname, port, username, cluster_name), "\n" print "xdcr_replicate: ", CommandFactory.xdcr_replicate(shell_path, source_hostname, source_port, source_username, source_bucket_name, target_bucket_name, cluster_name), "\n" print "get_replication_uuid :", CommandFactory.get_replication_uuid(shell_path, source_hostname, source_port, source_username), "\n" print "get_stream_id:", CommandFactory.get_stream_id(shell_path, source_hostname, source_port, source_username, cluster_name), "\n" print "pause_replication:", CommandFactory.pause_replication(shell_path, source_hostname, source_port, source_username, cluster_name, uuid), "\n" print "resume_replication:", CommandFactory.resume_replication(shell_path, source_hostname, source_port, source_username, cluster_name, uuid), "\n" print "delete_replication:", CommandFactory.delete_replication(shell_path, source_hostname, source_port, source_username, uuid, cluster_name), "\n" print "xdcr_delete:", CommandFactory.xdcr_delete(shell_path, source_hostname, source_port, source_username, hostname, port, username, cluster_name), "\n" print "get_source_bucket_list:", CommandFactory.get_source_bucket_list(shell_path, source_hostname, source_port, source_username), "\n" print "get_status: ", CommandFactory.get_status(shell_path, hostname, port, username), "\n" print "change_permission: ", CommandFactory.change_permission(directory_path), "\n" print "make_directory: ", CommandFactory.make_directory(directory_path), "\n" print "get_config_directory: ", CommandFactory.get_config_directory(mount_path), "\n" print "node_init:", CommandFactory.node_init(shell_path, port, username, mount_path), "\n" print "bucket_edit: ", CommandFactory.bucket_edit(shell_path, hostname, port, username, bucket_name, flush_value), "\n" print "bucket_edit_ramquota: ", CommandFactory.bucket_edit_ramquota(shell_path, hostname, port, username, bucket_name, ramsize), "\n" print "bucket_delete: ", CommandFactory.bucket_delete(shell_path, hostname, port, username, bucket_name), "\n" print "bucket_flush: ", CommandFactory.bucket_flush(shell_path, hostname, port, username, bucket_name), "\n" print "bucket_create: ", CommandFactory.bucket_create(shell_path, hostname, port, username, bucket_name, ramsize, evictionpolicy), "\n" print "bucket_list: ", CommandFactory.bucket_list(shell_path, hostname, port, username), "\n" print "get_indexes_name: ", CommandFactory.get_indexes_name(base_path, hostname, port, username, index), "\n" print "build_index: ", CommandFactory.build_index(base_path, hostname, port, username, index_name), "\n" print "is_build_completed: ", CommandFactory.is_build_completed(base_path, hostname, port, username, index), "\n" print "cb_backup_full: ", CommandFactory.cb_backup_full(base_path, backup_location, backup_repo, hostname, port, username, csv_bucket_list), "\n" print "monitor_replication: ", CommandFactory.monitor_replication(source_username, source_hostname, source_port, bucket_name, uuid), "\n" print "server_info: ", CommandFactory.server_info(shell_path, hostname, port, username), "\n" print "read_file: ", CommandFactory.read_file(filename), "\n" print "write_file: ", CommandFactory.write_file(filename, data), "\n" print "check_file: ", CommandFactory.check_file(file_path), "\n" print "get_ip_of_hostname: ", CommandFactory.get_ip_of_hostname(hostname), "\n" print "check_directory: ", CommandFactory.check_directory(dir_path), "\n" print "delete_file: ", CommandFactory.delete_file(filename), "\n" print "get_dlpx_bin: ", CommandFactory.get_dlpx_bin(), "\n" print "unmount_file_system: ", CommandFactory.unmount_file_system(mount_path), "\n"
py
1a4b711953ffc7d99e2a01b79aeaa951c3e590a2
import json import dml import prov.model import datetime import uuid import pandas as pd class topCertifiedCompanies(dml.Algorithm): contributor = 'ashwini_gdukuray_justini_utdesai' reads = ['ashwini_gdukuray_justini_utdesai.topCompanies', 'ashwini_gdukuray_justini_utdesai.masterList'] writes = ['ashwini_gdukuray_justini_utdesai.topCertCompanies'] @staticmethod def execute(trial=False): '''Retrieve some data sets (not using the API here for the sake of simplicity).''' startTime = datetime.datetime.now() # Set up the database connection. client = dml.pymongo.MongoClient() repo = client.repo repo.authenticate('ashwini_gdukuray_justini_utdesai', 'ashwini_gdukuray_justini_utdesai') masterList = repo['ashwini_gdukuray_justini_utdesai.masterList'] topCompanies = repo['ashwini_gdukuray_justini_utdesai.topCompanies'] masterListDF = pd.DataFrame(list(masterList.find())) topCompaniesDF = pd.DataFrame(list(topCompanies.find())) topCompaniesDF = topCompaniesDF.rename(index=str, columns={'Firm': 'Business Name'}) # create a more uniform ID businessIDs = [] for index, row in topCompaniesDF.iterrows(): busName = row['Business Name'] cleanedText = busName.upper().strip().replace(' ','').replace('.','').replace(',','').replace('-','') businessIDs.append(cleanedText) topCompaniesDF['B_ID'] = pd.Series(businessIDs, index=topCompaniesDF.index) merged = pd.merge(masterListDF, topCompaniesDF, how='inner', on=['B_ID']) #print(merged['B_ID']) #records = json.loads(topCompaniesDF.T.to_json()).values() repo.dropCollection("topCertCompanies") repo.createCollection("topCertCompanies") repo['ashwini_gdukuray_justini_utdesai.topCertCompanies'].insert_many(merged.to_dict('records')) repo['ashwini_gdukuray_justini_utdesai.topCertCompanies'].metadata({'complete': True}) print(repo['ashwini_gdukuray_justini_utdesai.topCertCompanies'].metadata()) repo.logout() endTime = datetime.datetime.now() return {"start": startTime, "end": endTime} @staticmethod def provenance(doc=prov.model.ProvDocument(), startTime=None, endTime=None): ''' Create the provenance document describing everything happening in this script. Each run of the script will generate a new document describing that invocation event. ''' # Set up the database connection. client = dml.pymongo.MongoClient() repo = client.repo repo.authenticate('ashwini_gdukuray_justini_utdesai', 'ashwini_gdukuray_justini_utdesai') doc.add_namespace('alg', 'http://datamechanics.io/algorithm/') # The scripts are in <folder>#<filename> format. doc.add_namespace('dat', 'http://datamechanics.io/data/') # The data sets are in <user>#<collection> format. doc.add_namespace('ont', 'http://datamechanics.io/ontology#') # 'Extension', 'DataResource', 'DataSet', 'Retrieval', 'Query', or 'Computation'. doc.add_namespace('log', 'http://datamechanics.io/log/') # The event log. this_script = doc.agent('alg:ashwini_gdukuray_justini_utdesai#topCertifiedCompanies', {prov.model.PROV_TYPE: prov.model.PROV['SoftwareAgent'], 'ont:Extension': 'py'}) topCompanies = doc.entity('dat:ashwini_gdukuray_justini_utdesai#topCompanies', {'prov:label': '311, Service Requests', prov.model.PROV_TYPE: 'ont:DataResource', 'ont:Extension': 'json'}) masterList = doc.entity('dat:ashwini_gdukuray_justini_utdesai#masterList', {'prov:label': '311, Service Requests', prov.model.PROV_TYPE: 'ont:DataResource', 'ont:Extension': 'json'}) topCertCompanies = doc.entity('dat:ashwini_gdukuray_justini_utdesai#topCertCompanies', {'prov:label': '311, Service Requests', prov.model.PROV_TYPE: 'ont:DataResource', 'ont:Extension': 'json'}) act = doc.activity('log:uuid' + str(uuid.uuid4()), startTime, endTime) doc.wasAssociatedWith(act, this_script) doc.usage(act, topCompanies, startTime, None, {prov.model.PROV_TYPE: 'ont:Retrieval', 'ont:Query': '?type=Animal+Found&$select=type,latitude,longitude,OPEN_DT' } ) doc.usage(act, masterList, startTime, None, {prov.model.PROV_TYPE: 'ont:Retrieval', 'ont:Query': '?type=Animal+Found&$select=type,latitude,longitude,OPEN_DT' } ) doc.wasAttributedTo(topCertCompanies, this_script) doc.wasGeneratedBy(topCertCompanies, act, endTime) doc.wasDerivedFrom(topCertCompanies, topCompanies, act, act, act) doc.wasDerivedFrom(topCertCompanies, masterList, act, act, act) repo.logout() return doc ''' # This is example code you might use for debugging this module. # Please remove all top-level function calls before submitting. example.execute() doc = example.provenance() print(doc.get_provn()) print(json.dumps(json.loads(doc.serialize()), indent=4)) ''' ## eof
py
1a4b730fcaa1541e21729685cd5f9a267773459e
"""Miscellaneous goodies for psycopg2 This module is a generic place used to hold little helper functions and classes untill a better place in the distribution is found. """ # psycopg/extras.py - miscellaneous extra goodies for psycopg # # Copyright (C) 2003-2010 Federico Di Gregorio <[email protected]> # # psycopg2 is free software: you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # In addition, as a special exception, the copyright holders give # permission to link this program with the OpenSSL library (or with # modified versions of OpenSSL that use the same license as OpenSSL), # and distribute linked combinations including the two. # # You must obey the GNU Lesser General Public License in all respects for # all of the code used other than OpenSSL. # # psycopg2 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 Lesser General Public # License for more details. import os import sys import time import warnings import re as regex try: import logging except: logging = None import psycopg2 from psycopg2 import extensions as _ext from psycopg2.extensions import cursor as _cursor from psycopg2.extensions import connection as _connection from psycopg2.extensions import adapt as _A from psycopg2.extensions import b class DictCursorBase(_cursor): """Base class for all dict-like cursors.""" def __init__(self, *args, **kwargs): if 'row_factory' in kwargs: row_factory = kwargs['row_factory'] del kwargs['row_factory'] else: raise NotImplementedError( "DictCursorBase can't be instantiated without a row factory.") _cursor.__init__(self, *args, **kwargs) self._query_executed = 0 self._prefetch = 0 self.row_factory = row_factory def fetchone(self): if self._prefetch: res = _cursor.fetchone(self) if self._query_executed: self._build_index() if not self._prefetch: res = _cursor.fetchone(self) return res def fetchmany(self, size=None): if self._prefetch: res = _cursor.fetchmany(self, size) if self._query_executed: self._build_index() if not self._prefetch: res = _cursor.fetchmany(self, size) return res def fetchall(self): if self._prefetch: res = _cursor.fetchall(self) if self._query_executed: self._build_index() if not self._prefetch: res = _cursor.fetchall(self) return res def next(self): if self._prefetch: res = _cursor.fetchone(self) if res is None: raise StopIteration() if self._query_executed: self._build_index() if not self._prefetch: res = _cursor.fetchone(self) if res is None: raise StopIteration() return res class DictConnection(_connection): """A connection that uses `DictCursor` automatically.""" def cursor(self, name=None): if name is None: return _connection.cursor(self, cursor_factory=DictCursor) else: return _connection.cursor(self, name, cursor_factory=DictCursor) class DictCursor(DictCursorBase): """A cursor that keeps a list of column name -> index mappings.""" def __init__(self, *args, **kwargs): kwargs['row_factory'] = DictRow DictCursorBase.__init__(self, *args, **kwargs) self._prefetch = 1 def execute(self, query, vars=None): self.index = {} self._query_executed = 1 return _cursor.execute(self, query, vars) def callproc(self, procname, vars=None): self.index = {} self._query_executed = 1 return _cursor.callproc(self, procname, vars) def _build_index(self): if self._query_executed == 1 and self.description: for i in range(len(self.description)): self.index[self.description[i][0]] = i self._query_executed = 0 class DictRow(list): """A row object that allow by-colmun-name access to data.""" __slots__ = ('_index',) def __init__(self, cursor): self._index = cursor.index self[:] = [None] * len(cursor.description) def __getitem__(self, x): if not isinstance(x, (int, slice)): x = self._index[x] return list.__getitem__(self, x) def __setitem__(self, x, v): if not isinstance(x, (int, slice)): x = self._index[x] list.__setitem__(self, x, v) def items(self): return list(self.iteritems()) def keys(self): return self._index.keys() def values(self): return tuple(self[:]) def has_key(self, x): return x in self._index def get(self, x, default=None): try: return self[x] except: return default def iteritems(self): for n, v in self._index.iteritems(): yield n, list.__getitem__(self, v) def iterkeys(self): return self._index.iterkeys() def itervalues(self): return list.__iter__(self) def copy(self): return dict(self.iteritems()) def __contains__(self, x): return x in self._index # grop the crusty Py2 methods if sys.version_info[0] > 2: items = iteritems; del iteritems keys = iterkeys; del iterkeys values = itervalues; del itervalues del has_key class RealDictConnection(_connection): """A connection that uses `RealDictCursor` automatically.""" def cursor(self, name=None): if name is None: return _connection.cursor(self, cursor_factory=RealDictCursor) else: return _connection.cursor(self, name, cursor_factory=RealDictCursor) class RealDictCursor(DictCursorBase): """A cursor that uses a real dict as the base type for rows. Note that this cursor is extremely specialized and does not allow the normal access (using integer indices) to fetched data. If you need to access database rows both as a dictionary and a list, then use the generic `DictCursor` instead of `!RealDictCursor`. """ def __init__(self, *args, **kwargs): kwargs['row_factory'] = RealDictRow DictCursorBase.__init__(self, *args, **kwargs) self._prefetch = 0 def execute(self, query, vars=None): self.column_mapping = [] self._query_executed = 1 return _cursor.execute(self, query, vars) def callproc(self, procname, vars=None): self.column_mapping = [] self._query_executed = 1 return _cursor.callproc(self, procname, vars) def _build_index(self): if self._query_executed == 1 and self.description: for i in range(len(self.description)): self.column_mapping.append(self.description[i][0]) self._query_executed = 0 class RealDictRow(dict): """A `!dict` subclass representing a data record.""" __slots__ = ('_column_mapping') def __init__(self, cursor): dict.__init__(self) self._column_mapping = cursor.column_mapping def __setitem__(self, name, value): if type(name) == int: name = self._column_mapping[name] return dict.__setitem__(self, name, value) class NamedTupleConnection(_connection): """A connection that uses `NamedTupleCursor` automatically.""" def cursor(self, *args, **kwargs): kwargs['cursor_factory'] = NamedTupleCursor return _connection.cursor(self, *args, **kwargs) class NamedTupleCursor(_cursor): """A cursor that generates results as `~collections.namedtuple`. `!fetch*()` methods will return named tuples instead of regular tuples, so their elements can be accessed both as regular numeric items as well as attributes. >>> nt_cur = conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) >>> rec = nt_cur.fetchone() >>> rec Record(id=1, num=100, data="abc'def") >>> rec[1] 100 >>> rec.data "abc'def" """ Record = None def execute(self, query, vars=None): self.Record = None return _cursor.execute(self, query, vars) def executemany(self, query, vars): self.Record = None return _cursor.executemany(self, vars) def callproc(self, procname, vars=None): self.Record = None return _cursor.callproc(self, procname, vars) def fetchone(self): t = _cursor.fetchone(self) if t is not None: nt = self.Record if nt is None: nt = self.Record = self._make_nt() return nt(*t) def fetchmany(self, size=None): ts = _cursor.fetchmany(self, size) nt = self.Record if nt is None: nt = self.Record = self._make_nt() return [nt(*t) for t in ts] def fetchall(self): ts = _cursor.fetchall(self) nt = self.Record if nt is None: nt = self.Record = self._make_nt() return [nt(*t) for t in ts] def __iter__(self): # Invoking _cursor.__iter__(self) goes to infinite recursion, # so we do pagination by hand while 1: recs = self.fetchmany(self.itersize) if not recs: return for rec in recs: yield rec try: from collections import namedtuple except ImportError, _exc: def _make_nt(self): raise self._exc else: def _make_nt(self, namedtuple=namedtuple): return namedtuple("Record", [d[0] for d in self.description or ()]) class LoggingConnection(_connection): """A connection that logs all queries to a file or logger__ object. .. __: http://docs.python.org/library/logging.html """ def initialize(self, logobj): """Initialize the connection to log to `!logobj`. The `!logobj` parameter can be an open file object or a Logger instance from the standard logging module. """ self._logobj = logobj if logging and isinstance(logobj, logging.Logger): self.log = self._logtologger else: self.log = self._logtofile def filter(self, msg, curs): """Filter the query before logging it. This is the method to overwrite to filter unwanted queries out of the log or to add some extra data to the output. The default implementation just does nothing. """ return msg def _logtofile(self, msg, curs): msg = self.filter(msg, curs) if msg: self._logobj.write(msg + os.linesep) def _logtologger(self, msg, curs): msg = self.filter(msg, curs) if msg: self._logobj.debug(msg) def _check(self): if not hasattr(self, '_logobj'): raise self.ProgrammingError( "LoggingConnection object has not been initialize()d") def cursor(self, name=None): self._check() if name is None: return _connection.cursor(self, cursor_factory=LoggingCursor) else: return _connection.cursor(self, name, cursor_factory=LoggingCursor) class LoggingCursor(_cursor): """A cursor that logs queries using its connection logging facilities.""" def execute(self, query, vars=None): try: return _cursor.execute(self, query, vars) finally: self.connection.log(self.query, self) def callproc(self, procname, vars=None): try: return _cursor.callproc(self, procname, vars) finally: self.connection.log(self.query, self) class MinTimeLoggingConnection(LoggingConnection): """A connection that logs queries based on execution time. This is just an example of how to sub-class `LoggingConnection` to provide some extra filtering for the logged queries. Both the `inizialize()` and `filter()` methods are overwritten to make sure that only queries executing for more than ``mintime`` ms are logged. Note that this connection uses the specialized cursor `MinTimeLoggingCursor`. """ def initialize(self, logobj, mintime=0): LoggingConnection.initialize(self, logobj) self._mintime = mintime def filter(self, msg, curs): t = (time.time() - curs.timestamp) * 1000 if t > self._mintime: return msg + os.linesep + " (execution time: %d ms)" % t def cursor(self, name=None): self._check() if name is None: return _connection.cursor(self, cursor_factory=MinTimeLoggingCursor) else: return _connection.cursor(self, name, cursor_factory=MinTimeLoggingCursor) class MinTimeLoggingCursor(LoggingCursor): """The cursor sub-class companion to `MinTimeLoggingConnection`.""" def execute(self, query, vars=None): self.timestamp = time.time() return LoggingCursor.execute(self, query, vars) def callproc(self, procname, vars=None): self.timestamp = time.time() return LoggingCursor.execute(self, procname, vars) # a dbtype and adapter for Python UUID type try: import uuid class UUID_adapter(object): """Adapt Python's uuid.UUID__ type to PostgreSQL's uuid__. .. __: http://docs.python.org/library/uuid.html .. __: http://www.postgresql.org/docs/8.4/static/datatype-uuid.html """ def __init__(self, uuid): self._uuid = uuid def prepare(self, conn): pass def getquoted(self): return "'"+str(self._uuid)+"'::uuid" __str__ = getquoted def register_uuid(oids=None, conn_or_curs=None): """Create the UUID type and an uuid.UUID adapter.""" if not oids: oid1 = 2950 oid2 = 2951 elif type(oids) == list: oid1, oid2 = oids else: oid1 = oids oid2 = 2951 def parseUUIDARRAY(data, cursor): if data is None: return None elif data == '{}': return [] else: return [((len(x) > 0 and x != 'NULL') and uuid.UUID(x) or None) for x in data[1:-1].split(',')] _ext.UUID = _ext.new_type((oid1, ), "UUID", lambda data, cursor: data and uuid.UUID(data) or None) _ext.UUIDARRAY = _ext.new_type((oid2,), "UUID[]", parseUUIDARRAY) _ext.register_type(_ext.UUID, conn_or_curs) _ext.register_type(_ext.UUIDARRAY, conn_or_curs) _ext.register_adapter(uuid.UUID, UUID_adapter) return _ext.UUID except ImportError, e: def register_uuid(oid=None): """Create the UUID type and an uuid.UUID adapter. This is a fake function that will always raise an error because the import of the uuid module failed. """ raise e # a type, dbtype and adapter for PostgreSQL inet type class Inet(object): """Wrap a string to allow for correct SQL-quoting of inet values. Note that this adapter does NOT check the passed value to make sure it really is an inet-compatible address but DOES call adapt() on it to make sure it is impossible to execute an SQL-injection by passing an evil value to the initializer. """ def __init__(self, addr): self.addr = addr def __repr__(self): return "%s(%r)" % (self.__class__.__name__, self.addr) def prepare(self, conn): self._conn = conn def getquoted(self): obj = _A(self.addr) if hasattr(obj, 'prepare'): obj.prepare(self._conn) return obj.getquoted() + b("::inet") def __conform__(self, foo): if foo is _ext.ISQLQuote: return self def __str__(self): return str(self.addr) def register_inet(oid=None, conn_or_curs=None): """Create the INET type and an Inet adapter.""" if not oid: oid = 869 _ext.INET = _ext.new_type((oid, ), "INET", lambda data, cursor: data and Inet(data) or None) _ext.register_type(_ext.INET, conn_or_curs) return _ext.INET def register_tstz_w_secs(oids=None, conn_or_curs=None): """The function used to register an alternate type caster for :sql:`TIMESTAMP WITH TIME ZONE` to deal with historical time zones with seconds in the UTC offset. These are now correctly handled by the default type caster, so currently the function doesn't do anything. """ warnings.warn("deprecated", DeprecationWarning) import select from psycopg2.extensions import POLL_OK, POLL_READ, POLL_WRITE from psycopg2 import OperationalError def wait_select(conn): """Wait until a connection or cursor has data available. The function is an example of a wait callback to be registered with `~psycopg2.extensions.set_wait_callback()`. This function uses `!select()` to wait for data available. """ while 1: state = conn.poll() if state == POLL_OK: break elif state == POLL_READ: select.select([conn.fileno()], [], []) elif state == POLL_WRITE: select.select([], [conn.fileno()], []) else: raise OperationalError("bad state from poll: %s" % state) class HstoreAdapter(object): """Adapt a Python dict to the hstore syntax.""" def __init__(self, wrapped): self.wrapped = wrapped def prepare(self, conn): self.conn = conn # use an old-style getquoted implementation if required if conn.server_version < 90000: self.getquoted = self._getquoted_8 def _getquoted_8(self): """Use the operators available in PG pre-9.0.""" if not self.wrapped: return b("''::hstore") adapt = _ext.adapt rv = [] for k, v in self.wrapped.iteritems(): k = adapt(k) k.prepare(self.conn) k = k.getquoted() if v is not None: v = adapt(v) v.prepare(self.conn) v = v.getquoted() else: v = b('NULL') # XXX this b'ing is painfully inefficient! rv.append(b("(") + k + b(" => ") + v + b(")")) return b("(") + b('||').join(rv) + b(")") def _getquoted_9(self): """Use the hstore(text[], text[]) function.""" if not self.wrapped: return b("''::hstore") k = _ext.adapt(self.wrapped.keys()) k.prepare(self.conn) v = _ext.adapt(self.wrapped.values()) v.prepare(self.conn) return b("hstore(") + k.getquoted() + b(", ") + v.getquoted() + b(")") getquoted = _getquoted_9 _re_hstore = regex.compile(r""" # hstore key: # a string of normal or escaped chars "((?: [^"\\] | \\. )*)" \s*=>\s* # hstore value (?: NULL # the value can be null - not catched # or a quoted string like the key | "((?: [^"\\] | \\. )*)" ) (?:\s*,\s*|$) # pairs separated by comma or end of string. """, regex.VERBOSE) @classmethod def parse(self, s, cur, _bsdec=regex.compile(r"\\(.)")): """Parse an hstore representation in a Python string. The hstore is represented as something like:: "a"=>"1", "b"=>"2" with backslash-escaped strings. """ if s is None: return None rv = {} start = 0 for m in self._re_hstore.finditer(s): if m is None or m.start() != start: raise psycopg2.InterfaceError( "error parsing hstore pair at char %d" % start) k = _bsdec.sub(r'\1', m.group(1)) v = m.group(2) if v is not None: v = _bsdec.sub(r'\1', v) rv[k] = v start = m.end() if start < len(s): raise psycopg2.InterfaceError( "error parsing hstore: unparsed data after char %d" % start) return rv @classmethod def parse_unicode(self, s, cur): """Parse an hstore returning unicode keys and values.""" if s is None: return None s = s.decode(_ext.encodings[cur.connection.encoding]) return self.parse(s, cur) @classmethod def get_oids(self, conn_or_curs): """Return the lists of OID of the hstore and hstore[] types. """ if hasattr(conn_or_curs, 'execute'): conn = conn_or_curs.connection curs = conn_or_curs else: conn = conn_or_curs curs = conn_or_curs.cursor() # Store the transaction status of the connection to revert it after use conn_status = conn.status # column typarray not available before PG 8.3 typarray = conn.server_version >= 80300 and "typarray" or "NULL" rv0, rv1 = [], [] # get the oid for the hstore curs.execute("""\ SELECT t.oid, %s FROM pg_type t JOIN pg_namespace ns ON typnamespace = ns.oid WHERE typname = 'hstore'; """ % typarray) for oids in curs: rv0.append(oids[0]) rv1.append(oids[1]) # revert the status of the connection as before the command if (conn_status != _ext.STATUS_IN_TRANSACTION and conn.isolation_level != _ext.ISOLATION_LEVEL_AUTOCOMMIT): conn.rollback() return tuple(rv0), tuple(rv1) def register_hstore(conn_or_curs, globally=False, unicode=False, oid=None): """Register adapter and typecaster for `!dict`\-\ |hstore| conversions. :param conn_or_curs: a connection or cursor: the typecaster will be registered only on this object unless *globally* is set to `!True` :param globally: register the adapter globally, not only on *conn_or_curs* :param unicode: if `!True`, keys and values returned from the database will be `!unicode` instead of `!str`. The option is not available on Python 3 :param oid: the OID of the |hstore| type if known. If not, it will be queried on *conn_or_curs* The connection or cursor passed to the function will be used to query the database and look for the OID of the |hstore| type (which may be different across databases). If querying is not desirable (e.g. with :ref:`asynchronous connections <async-support>`) you may specify it in the *oid* parameter (it can be found using a query such as :sql:`SELECT 'hstore'::regtype::oid;`). Note that, when passing a dictionary from Python to the database, both strings and unicode keys and values are supported. Dictionaries returned from the database have keys/values according to the *unicode* parameter. The |hstore| contrib module must be already installed in the database (executing the ``hstore.sql`` script in your ``contrib`` directory). Raise `~psycopg2.ProgrammingError` if the type is not found. .. versionchanged:: 2.4 added the *oid* parameter. If not specified, the typecaster is installed also if |hstore| is not installed in the :sql:`public` schema. """ if oid is None: oid = HstoreAdapter.get_oids(conn_or_curs) if oid is None or not oid[0]: raise psycopg2.ProgrammingError( "hstore type not found in the database. " "please install it from your 'contrib/hstore.sql' file") else: oid = oid[0] # for the moment we don't have a HSTOREARRAY if isinstance(oid, int): oid = (oid,) # create and register the typecaster if sys.version_info[0] < 3 and unicode: cast = HstoreAdapter.parse_unicode else: cast = HstoreAdapter.parse HSTORE = _ext.new_type(oid, "HSTORE", cast) _ext.register_type(HSTORE, not globally and conn_or_curs or None) _ext.register_adapter(dict, HstoreAdapter) class CompositeCaster(object): """Helps conversion of a PostgreSQL composite type into a Python object. The class is usually created by the `register_composite()` function. .. attribute:: name The name of the PostgreSQL type. .. attribute:: oid The oid of the PostgreSQL type. .. attribute:: type The type of the Python objects returned. If :py:func:`collections.namedtuple()` is available, it is a named tuple with attributes equal to the type components. Otherwise it is just the `!tuple` object. .. attribute:: attnames List of component names of the type to be casted. .. attribute:: atttypes List of component type oids of the type to be casted. """ def __init__(self, name, oid, attrs): self.name = name self.oid = oid self.attnames = [ a[0] for a in attrs ] self.atttypes = [ a[1] for a in attrs ] self._create_type(name, self.attnames) self.typecaster = _ext.new_type((oid,), name, self.parse) def parse(self, s, curs): if s is None: return None tokens = self.tokenize(s) if len(tokens) != len(self.atttypes): raise psycopg2.DataError( "expecting %d components for the type %s, %d found instead", (len(self.atttypes), self.name, len(self.tokens))) attrs = [ curs.cast(oid, token) for oid, token in zip(self.atttypes, tokens) ] return self._ctor(*attrs) _re_tokenize = regex.compile(r""" \(? ([,\)]) # an empty token, representing NULL | \(? " ((?: [^"] | "")*) " [,)] # or a quoted string | \(? ([^",\)]+) [,\)] # or an unquoted string """, regex.VERBOSE) _re_undouble = regex.compile(r'(["\\])\1') @classmethod def tokenize(self, s): rv = [] for m in self._re_tokenize.finditer(s): if m is None: raise psycopg2.InterfaceError("can't parse type: %r", s) if m.group(1): rv.append(None) elif m.group(2): rv.append(self._re_undouble.sub(r"\1", m.group(2))) else: rv.append(m.group(3)) return rv def _create_type(self, name, attnames): try: from collections import namedtuple except ImportError: self.type = tuple self._ctor = lambda *args: tuple(args) else: self.type = namedtuple(name, attnames) self._ctor = self.type @classmethod def _from_db(self, name, conn_or_curs): """Return a `CompositeCaster` instance for the type *name*. Raise `ProgrammingError` if the type is not found. """ if hasattr(conn_or_curs, 'execute'): conn = conn_or_curs.connection curs = conn_or_curs else: conn = conn_or_curs curs = conn_or_curs.cursor() # Store the transaction status of the connection to revert it after use conn_status = conn.status # Use the correct schema if '.' in name: schema, tname = name.split('.', 1) else: tname = name schema = 'public' # get the type oid and attributes curs.execute("""\ SELECT t.oid, attname, atttypid FROM pg_type t JOIN pg_namespace ns ON typnamespace = ns.oid JOIN pg_attribute a ON attrelid = typrelid WHERE typname = %s and nspname = %s ORDER BY attnum; """, (tname, schema)) recs = curs.fetchall() # revert the status of the connection as before the command if (conn_status != _ext.STATUS_IN_TRANSACTION and conn.isolation_level != _ext.ISOLATION_LEVEL_AUTOCOMMIT): conn.rollback() if not recs: raise psycopg2.ProgrammingError( "PostgreSQL type '%s' not found" % name) type_oid = recs[0][0] type_attrs = [ (r[1], r[2]) for r in recs ] return CompositeCaster(tname, type_oid, type_attrs) def register_composite(name, conn_or_curs, globally=False): """Register a typecaster to convert a composite type into a tuple. :param name: the name of a PostgreSQL composite type, e.g. created using the |CREATE TYPE|_ command :param conn_or_curs: a connection or cursor used to find the type oid and components; the typecaster is registered in a scope limited to this object, unless *globally* is set to `!True` :param globally: if `!False` (default) register the typecaster only on *conn_or_curs*, otherwise register it globally :return: the registered `CompositeCaster` instance responsible for the conversion """ caster = CompositeCaster._from_db(name, conn_or_curs) _ext.register_type(caster.typecaster, not globally and conn_or_curs or None) return caster __all__ = filter(lambda k: not k.startswith('_'), locals().keys())
py
1a4b737fec1d681ebbbca37da052af084b539d1c
# Copyright (C) 2020 The Electrum developers # Copyright (C) 2021 The DECENOMY Core Developers # Distributed under the MIT software license, see the accompanying # file LICENCE or http://www.opensource.org/licenses/mit-license.php from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QWidget, QVBoxLayout, QGridLayout, QLabel, QListWidget, QListWidgetItem from electrum.i18n import _ from electrum.network import Network from electrum.bip39_recovery import account_discovery from electrum.logging import get_logger from .util import WindowModalDialog, MessageBoxMixin, TaskThread, Buttons, CancelButton, OkButton _logger = get_logger(__name__) class Bip39RecoveryDialog(WindowModalDialog): ROLE_ACCOUNT = Qt.UserRole def __init__(self, parent: QWidget, get_account_xpub, on_account_select): self.get_account_xpub = get_account_xpub self.on_account_select = on_account_select WindowModalDialog.__init__(self, parent, _('BIP39 Recovery')) self.setMinimumWidth(400) vbox = QVBoxLayout(self) self.content = QVBoxLayout() self.content.addWidget(QLabel(_('Scanning common paths for existing accounts...'))) vbox.addLayout(self.content) self.ok_button = OkButton(self) self.ok_button.clicked.connect(self.on_ok_button_click) self.ok_button.setEnabled(False) vbox.addLayout(Buttons(CancelButton(self), self.ok_button)) self.finished.connect(self.on_finished) self.show() self.thread = TaskThread(self) self.thread.finished.connect(self.deleteLater) # see #3956 self.thread.add(self.recovery, self.on_recovery_success, None, self.on_recovery_error) def on_finished(self): self.thread.stop() def on_ok_button_click(self): item = self.list.currentItem() account = item.data(self.ROLE_ACCOUNT) self.on_account_select(account) def recovery(self): network = Network.get_instance() coroutine = account_discovery(network, self.get_account_xpub) return network.run_from_another_thread(coroutine) def on_recovery_success(self, accounts): self.clear_content() if len(accounts) == 0: self.content.addWidget(QLabel(_('No existing accounts found.'))) return self.content.addWidget(QLabel(_('Choose an account to restore.'))) self.list = QListWidget() for account in accounts: item = QListWidgetItem(account['description']) item.setData(self.ROLE_ACCOUNT, account) self.list.addItem(item) self.list.clicked.connect(lambda: self.ok_button.setEnabled(True)) self.content.addWidget(self.list) def on_recovery_error(self, exc_info): self.clear_content() self.content.addWidget(QLabel(_('Error: Account discovery failed.'))) _logger.error(f"recovery error", exc_info=exc_info) def clear_content(self): for i in reversed(range(self.content.count())): self.content.itemAt(i).widget().setParent(None)
py
1a4b73c964177fc2d508f34c1419fd7d77608da3
r"""OS routines for NT or Posix depending on what system we're on. This exports: - all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc. - os.path is one of the modules posixpath, or ntpath - os.name is 'posix', 'nt', 'os2', 'ce' or 'riscos' - os.curdir is a string representing the current directory ('.' or ':') - os.pardir is a string representing the parent directory ('..' or '::') - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\') - os.extsep is the extension separator ('.' or '/') - os.altsep is the alternate pathname separator (None or '/') - os.pathsep is the component separator used in $PATH etc - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n') - os.defpath is the default search path for executables - os.devnull is the file path of the null device ('/dev/null', etc.) Programs that import and use 'os' stand a better chance of being portable between different platforms. Of course, they must then only use functions that are defined by all platforms (e.g., unlink and opendir), and leave all pathname manipulation to os.path (e.g., split and join). """ #' import sys, errno _names = sys.builtin_module_names # Note: more names are added to __all__ later. __all__ = ["altsep", "curdir", "pardir", "sep", "extsep", "pathsep", "linesep", "defpath", "name", "path", "devnull", "SEEK_SET", "SEEK_CUR", "SEEK_END"] def _get_exports_list(module): try: return list(module.__all__) except AttributeError: return [n for n in dir(module) if n[0] != '_'] if 'posix' in _names: name = 'posix' linesep = '\n' from posix import * try: from posix import _exit except ImportError: pass import posixpath as path import posix __all__.extend(_get_exports_list(posix)) del posix elif 'nt' in _names: name = 'nt' linesep = '\r\n' from nt import * try: from nt import _exit except ImportError: pass import ntpath as path import nt __all__.extend(_get_exports_list(nt)) del nt elif 'os2' in _names: name = 'os2' linesep = '\r\n' from os2 import * try: from os2 import _exit except ImportError: pass if sys.version.find('EMX GCC') == -1: import ntpath as path else: import os2emxpath as path from _emx_link import link import os2 __all__.extend(_get_exports_list(os2)) del os2 elif 'ce' in _names: name = 'ce' linesep = '\r\n' from ce import * try: from ce import _exit except ImportError: pass # We can use the standard Windows path. import ntpath as path import ce __all__.extend(_get_exports_list(ce)) del ce elif 'riscos' in _names: name = 'riscos' linesep = '\n' from riscos import * try: from riscos import _exit except ImportError: pass import riscospath as path import riscos __all__.extend(_get_exports_list(riscos)) del riscos else: raise ImportError, 'no os specific module found' sys.modules['os.path'] = path from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep, devnull) del _names # Python uses fixed values for the SEEK_ constants; they are mapped # to native constants if necessary in posixmodule.c SEEK_SET = 0 SEEK_CUR = 1 SEEK_END = 2 #' # Super directory utilities. # (Inspired by Eric Raymond; the doc strings are mostly his) def makedirs(name, mode=0777): """makedirs(path [, mode=0777]) Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. This is recursive. """ head, tail = path.split(name) if not tail: head, tail = path.split(head) if head and tail and not path.exists(head): try: makedirs(head, mode) except OSError, e: # be happy if someone already created the path if e.errno != errno.EEXIST: raise if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists return mkdir(name, mode) def removedirs(name): """removedirs(path) Super-rmdir; remove a leaf directory and all empty intermediate ones. Works like rmdir except that, if the leaf directory is successfully removed, directories corresponding to rightmost path segments will be pruned away until either the whole path is consumed or an error occurs. Errors during this latter phase are ignored -- they generally mean that a directory was not empty. """ rmdir(name) head, tail = path.split(name) if not tail: head, tail = path.split(head) while head and tail: try: rmdir(head) except error: break head, tail = path.split(head) def renames(old, new): """renames(old, new) Super-rename; create directories as necessary and delete any left empty. Works like rename, except creation of any intermediate directories needed to make the new pathname good is attempted first. After the rename, directories corresponding to rightmost path segments of the old name will be pruned until either the whole path is consumed or a nonempty directory is found. Note: this function can fail with the new directory structure made if you lack permissions needed to unlink the leaf directory or file. """ head, tail = path.split(new) if head and tail and not path.exists(head): makedirs(head) rename(old, new) head, tail = path.split(old) if head and tail: try: removedirs(head) except error: pass __all__.extend(["makedirs", "removedirs", "renames"]) def walk(top, topdown=True, onerror=None, followlinks=False): """Directory tree generator. For each directory in the directory tree rooted at top (including top itself, but excluding '.' and '..'), yields a 3-tuple dirpath, dirnames, filenames dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists are just names, with no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name). If optional arg 'topdown' is true or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top down). If topdown is false, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom up). When topdown is true, the caller can modify the dirnames list in-place (e.g., via del or slice assignment), and walk will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, or to impose a specific order of visiting. Modifying dirnames when topdown is false is ineffective, since the directories in dirnames have already been generated by the time dirnames itself is generated. No matter the value of topdown, the list of subdirectories is retrieved before the tuples for the directory and its subdirectories are generated. By default errors from the os.listdir() call are ignored. If optional arg 'onerror' is specified, it should be a function; it will be called with one argument, an os.error instance. It can report the error to continue with the walk, or raise the exception to abort the walk. Note that the filename is available as the filename attribute of the exception object. By default, os.walk does not follow symbolic links to subdirectories on systems that support them. In order to get this functionality, set the optional argument 'followlinks' to true. Caution: if you pass a relative pathname for top, don't change the current working directory between resumptions of walk. walk never changes the current directory, and assumes that the client doesn't either. Example: import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print root, "consumes", print sum([getsize(join(root, name)) for name in files]), print "bytes in", len(files), "non-directory files" if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories """ islink, join, isdir = path.islink, path.join, path.isdir # We may not have read permission for top, in which case we can't # get a list of the files the directory contains. os.path.walk # always suppressed the exception then, rather than blow up for a # minor reason when (say) a thousand readable directories are still # left to visit. That logic is copied here. try: # Note that listdir and error are globals in this module due # to earlier import-*. names = listdir(top) except error, err: if onerror is not None: onerror(err) return dirs, nondirs = [], [] for name in names: if isdir(join(top, name)): dirs.append(name) else: nondirs.append(name) if topdown: yield top, dirs, nondirs for name in dirs: new_path = join(top, name) if followlinks or not islink(new_path): for x in walk(new_path, topdown, onerror, followlinks): yield x if not topdown: yield top, dirs, nondirs __all__.append("walk") # Make sure os.environ exists, at least try: environ except NameError: environ = {} def execl(file, *args): """execl(file, *args) Execute the executable file with argument list args, replacing the current process. """ execv(file, args) def execle(file, *args): """execle(file, *args, env) Execute the executable file with argument list args and environment env, replacing the current process. """ env = args[-1] execve(file, args[:-1], env) def execlp(file, *args): """execlp(file, *args) Execute the executable file (which is searched for along $PATH) with argument list args, replacing the current process. """ execvp(file, args) def execlpe(file, *args): """execlpe(file, *args, env) Execute the executable file (which is searched for along $PATH) with argument list args and environment env, replacing the current process. """ env = args[-1] execvpe(file, args[:-1], env) def execvp(file, args): """execvp(file, args) Execute the executable file (which is searched for along $PATH) with argument list args, replacing the current process. args may be a list or tuple of strings. """ _execvpe(file, args) def execvpe(file, args, env): """execvpe(file, args, env) Execute the executable file (which is searched for along $PATH) with argument list args and environment env , replacing the current process. args may be a list or tuple of strings. """ _execvpe(file, args, env) __all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"]) def _execvpe(file, args, env=None): if env is not None: func = execve argrest = (args, env) else: func = execv argrest = (args,) env = environ head, tail = path.split(file) if head: func(file, *argrest) return if 'PATH' in env: envpath = env['PATH'] else: envpath = defpath PATH = envpath.split(pathsep) saved_exc = None saved_tb = None for dir in PATH: fullname = path.join(dir, file) try: func(fullname, *argrest) except error, e: tb = sys.exc_info()[2] if (e.errno != errno.ENOENT and e.errno != errno.ENOTDIR and saved_exc is None): saved_exc = e saved_tb = tb if saved_exc: raise error, saved_exc, saved_tb raise error, e, tb # Change environ to automatically call putenv() if it exists try: # This will fail if there's no putenv putenv except NameError: pass else: import UserDict # Fake unsetenv() for Windows # not sure about os2 here but # I'm guessing they are the same. if name in ('os2', 'nt'): def unsetenv(key): putenv(key, "") if name == "riscos": # On RISC OS, all env access goes through getenv and putenv from riscosenviron import _Environ elif name in ('os2', 'nt'): # Where Env Var Names Must Be UPPERCASE # But we store them as upper case class _Environ(UserDict.IterableUserDict): def __init__(self, environ): UserDict.UserDict.__init__(self) data = self.data for k, v in environ.items(): data[k.upper()] = v def __setitem__(self, key, item): putenv(key, item) self.data[key.upper()] = item def __getitem__(self, key): return self.data[key.upper()] try: unsetenv except NameError: def __delitem__(self, key): del self.data[key.upper()] else: def __delitem__(self, key): unsetenv(key) del self.data[key.upper()] def clear(self): for key in self.data.keys(): unsetenv(key) del self.data[key] def pop(self, key, *args): unsetenv(key) return self.data.pop(key.upper(), *args) def has_key(self, key): return key.upper() in self.data def __contains__(self, key): return key.upper() in self.data def get(self, key, failobj=None): return self.data.get(key.upper(), failobj) def update(self, dict=None, **kwargs): if dict: try: keys = dict.keys() except AttributeError: # List of (key, value) for k, v in dict: self[k] = v else: # got keys # cannot use items(), since mappings # may not have them. for k in keys: self[k] = dict[k] if kwargs: self.update(kwargs) def copy(self): return dict(self) else: # Where Env Var Names Can Be Mixed Case class _Environ(UserDict.IterableUserDict): def __init__(self, environ): UserDict.UserDict.__init__(self) self.data = environ def __setitem__(self, key, item): putenv(key, item) self.data[key] = item def update(self, dict=None, **kwargs): if dict: try: keys = dict.keys() except AttributeError: # List of (key, value) for k, v in dict: self[k] = v else: # got keys # cannot use items(), since mappings # may not have them. for k in keys: self[k] = dict[k] if kwargs: self.update(kwargs) try: unsetenv except NameError: pass else: def __delitem__(self, key): unsetenv(key) del self.data[key] def clear(self): for key in self.data.keys(): unsetenv(key) del self.data[key] def pop(self, key, *args): unsetenv(key) return self.data.pop(key, *args) def copy(self): return dict(self) environ = _Environ(environ) def getenv(key, default=None): """Get an environment variable, return None if it doesn't exist. The optional second argument can specify an alternate default.""" return environ.get(key, default) __all__.append("getenv") def _exists(name): return name in globals() # Supply spawn*() (probably only for Unix) if _exists("fork") and not _exists("spawnv") and _exists("execv"): P_WAIT = 0 P_NOWAIT = P_NOWAITO = 1 # XXX Should we support P_DETACH? I suppose it could fork()**2 # and close the std I/O streams. Also, P_OVERLAY is the same # as execv*()? def _spawnvef(mode, file, args, env, func): # Internal helper; func is the exec*() function to use pid = fork() if not pid: # Child try: if env is None: func(file, args) else: func(file, args, env) except: _exit(127) else: # Parent if mode == P_NOWAIT: return pid # Caller is responsible for waiting! while 1: wpid, sts = waitpid(pid, 0) if WIFSTOPPED(sts): continue elif WIFSIGNALED(sts): return -WTERMSIG(sts) elif WIFEXITED(sts): return WEXITSTATUS(sts) else: raise error, "Not stopped, signaled or exited???" def spawnv(mode, file, args): """spawnv(mode, file, args) -> integer Execute file with arguments from args in a subprocess. If mode == P_NOWAIT return the pid of the process. If mode == P_WAIT return the process's exit code if it exits normally; otherwise return -SIG, where SIG is the signal that killed it. """ return _spawnvef(mode, file, args, None, execv) def spawnve(mode, file, args, env): """spawnve(mode, file, args, env) -> integer Execute file with arguments from args in a subprocess with the specified environment. If mode == P_NOWAIT return the pid of the process. If mode == P_WAIT return the process's exit code if it exits normally; otherwise return -SIG, where SIG is the signal that killed it. """ return _spawnvef(mode, file, args, env, execve) # Note: spawnvp[e] is't currently supported on Windows def spawnvp(mode, file, args): """spawnvp(mode, file, args) -> integer Execute file (which is looked for along $PATH) with arguments from args in a subprocess. If mode == P_NOWAIT return the pid of the process. If mode == P_WAIT return the process's exit code if it exits normally; otherwise return -SIG, where SIG is the signal that killed it. """ return _spawnvef(mode, file, args, None, execvp) def spawnvpe(mode, file, args, env): """spawnvpe(mode, file, args, env) -> integer Execute file (which is looked for along $PATH) with arguments from args in a subprocess with the supplied environment. If mode == P_NOWAIT return the pid of the process. If mode == P_WAIT return the process's exit code if it exits normally; otherwise return -SIG, where SIG is the signal that killed it. """ return _spawnvef(mode, file, args, env, execvpe) if _exists("spawnv"): # These aren't supplied by the basic Windows code # but can be easily implemented in Python def spawnl(mode, file, *args): """spawnl(mode, file, *args) -> integer Execute file with arguments from args in a subprocess. If mode == P_NOWAIT return the pid of the process. If mode == P_WAIT return the process's exit code if it exits normally; otherwise return -SIG, where SIG is the signal that killed it. """ return spawnv(mode, file, args) def spawnle(mode, file, *args): """spawnle(mode, file, *args, env) -> integer Execute file with arguments from args in a subprocess with the supplied environment. If mode == P_NOWAIT return the pid of the process. If mode == P_WAIT return the process's exit code if it exits normally; otherwise return -SIG, where SIG is the signal that killed it. """ env = args[-1] return spawnve(mode, file, args[:-1], env) __all__.extend(["spawnv", "spawnve", "spawnl", "spawnle",]) if _exists("spawnvp"): # At the moment, Windows doesn't implement spawnvp[e], # so it won't have spawnlp[e] either. def spawnlp(mode, file, *args): """spawnlp(mode, file, *args) -> integer Execute file (which is looked for along $PATH) with arguments from args in a subprocess with the supplied environment. If mode == P_NOWAIT return the pid of the process. If mode == P_WAIT return the process's exit code if it exits normally; otherwise return -SIG, where SIG is the signal that killed it. """ return spawnvp(mode, file, args) def spawnlpe(mode, file, *args): """spawnlpe(mode, file, *args, env) -> integer Execute file (which is looked for along $PATH) with arguments from args in a subprocess with the supplied environment. If mode == P_NOWAIT return the pid of the process. If mode == P_WAIT return the process's exit code if it exits normally; otherwise return -SIG, where SIG is the signal that killed it. """ env = args[-1] return spawnvpe(mode, file, args[:-1], env) __all__.extend(["spawnvp", "spawnvpe", "spawnlp", "spawnlpe",]) # Supply popen2 etc. (for Unix) if _exists("fork"): if not _exists("popen2"): def popen2(cmd, mode="t", bufsize=-1): """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments will be passed directly to the program without shell intervention (as with os.spawnv()). If 'cmd' is a string it will be passed to the shell (as with os.system()). If 'bufsize' is specified, it sets the buffer size for the I/O pipes. The file objects (child_stdin, child_stdout) are returned.""" import warnings msg = "os.popen2 is deprecated. Use the subprocess module." warnings.warn(msg, DeprecationWarning, stacklevel=2) import subprocess PIPE = subprocess.PIPE p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring), bufsize=bufsize, stdin=PIPE, stdout=PIPE, close_fds=True) return p.stdin, p.stdout __all__.append("popen2") if not _exists("popen3"): def popen3(cmd, mode="t", bufsize=-1): """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments will be passed directly to the program without shell intervention (as with os.spawnv()). If 'cmd' is a string it will be passed to the shell (as with os.system()). If 'bufsize' is specified, it sets the buffer size for the I/O pipes. The file objects (child_stdin, child_stdout, child_stderr) are returned.""" import warnings msg = "os.popen3 is deprecated. Use the subprocess module." warnings.warn(msg, DeprecationWarning, stacklevel=2) import subprocess PIPE = subprocess.PIPE p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring), bufsize=bufsize, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) return p.stdin, p.stdout, p.stderr __all__.append("popen3") if not _exists("popen4"): def popen4(cmd, mode="t", bufsize=-1): """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments will be passed directly to the program without shell intervention (as with os.spawnv()). If 'cmd' is a string it will be passed to the shell (as with os.system()). If 'bufsize' is specified, it sets the buffer size for the I/O pipes. The file objects (child_stdin, child_stdout_stderr) are returned.""" import warnings msg = "os.popen4 is deprecated. Use the subprocess module." warnings.warn(msg, DeprecationWarning, stacklevel=2) import subprocess PIPE = subprocess.PIPE p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring), bufsize=bufsize, stdin=PIPE, stdout=PIPE, stderr=subprocess.STDOUT, close_fds=True) return p.stdin, p.stdout __all__.append("popen4") import copy_reg as _copy_reg def _make_stat_result(tup, dict): return stat_result(tup, dict) def _pickle_stat_result(sr): (type, args) = sr.__reduce__() return (_make_stat_result, args) try: _copy_reg.pickle(stat_result, _pickle_stat_result, _make_stat_result) except NameError: # stat_result may not exist pass def _make_statvfs_result(tup, dict): return statvfs_result(tup, dict) def _pickle_statvfs_result(sr): (type, args) = sr.__reduce__() return (_make_statvfs_result, args) try: _copy_reg.pickle(statvfs_result, _pickle_statvfs_result, _make_statvfs_result) except NameError: # statvfs_result may not exist pass
py
1a4b7535ca63bb6cb37406ee772d7fb0e83ff636
class Config: main_page = 'https://antycaptcha.amberteam.pl:5443/' ex_url_sub_dir = 'exercises/exercise' stf_url_sub_dir = 'stf/' ex_url_param = '?seed=' test_pass_text = 'OK. Good answer' test_fail_text = 'NOT OK.' main_page_title = 'AntyCaptcha' __author__ = 'GiSDeCain'
py
1a4b7575a66e08bbe934364c368dcdf5de784d8a
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models, migrations def make_many_types(apps, schema_editor): """ Adds the Author object in Book.author to the many-to-many relationship in Book.authors """ Organization = apps.get_model('organization', 'Organization') for organization in Organization.objects.all(): if organization.organization_type: organization.organization_types.add(organization.organization_type) if organization.investor_type: organization.investor_types.add(organization.investor_type) class Migration(migrations.Migration): dependencies = [ ('organization', '0015_auto_20160405_1139'), ] operations = [ migrations.RunPython(make_many_types), ]
py
1a4b75da3ba3f3f8baf3497cf2aca0e35c7a5ff6
#!/bin/python3 import math import random import re import sys # Complete the solve function below. def solve(meal_cost, tip_percent, tax_percent): tip=meal_cost*tip_percent/100 tax= meal_cost *tax_percent/100 total =tip +tax+ meal_cost return round(total) if __name__ == '__main__': meal_cost = float(input()) tip_percent = int(input()) tax_percent = int(input()) print(solve(meal_cost, tip_percent, tax_percent))
py
1a4b7757d309344a015c5c07fb51218e8110d3fa
# Generated by Django 2.0.7 on 2018-08-20 09:44 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('users', '0005_auto_20180812_1854'), ] operations = [ migrations.AlterField( model_name='user', name='gender', field=models.CharField(choices=[('male', 'Male'), ('female', 'Female'), ('not-specified', 'Not-specified')], max_length=80, null=True), ), ]
py
1a4b777a4c112d2614835f7b4f4ff504b6b2a0f2
import logging from typing import Text, Any, Dict, Optional, List from rasa.core.constants import DEFAULT_REQUEST_TIMEOUT from rasa.core.nlg.generator import NaturalLanguageGenerator from rasa.core.trackers import DialogueStateTracker, EventVerbosity from rasa.utils.endpoints import EndpointConfig import os logger = logging.getLogger(__name__) NLG_QUERY = """ query( $template: StringOrListOfStrings! $arguments: Any $tracker: ConversationInput $channel: NlgRequestChannel ) { getResponse( template: $template arguments: $arguments tracker: $tracker channel: $channel ) { text metadata ...on QuickReplyPayload { buttons { title, type, ...on WebUrlButton { url } ...on PostbackButton { payload } } } ...on ImagePayload { image } ...on CustomPayload { buttons { title, type, ...on WebUrlButton { url } ...on PostbackButton { payload } }, elements, attachment, image, custom } } } """ def nlg_response_format_spec(): """Expected response schema for an NLG endpoint. Used for validation of the response returned from the NLG endpoint.""" return { "type": "object", "properties": { "text": {"type": ["string", "null"]}, "buttons": {"type": ["array", "null"], "items": {"type": "object"}}, "elements": {"type": ["array", "null"], "items": {"type": "object"}}, "attachment": {"type": ["object", "null"]}, "image": {"type": ["string", "null"]}, }, } def nlg_request_format_spec(): """Expected request schema for requests sent to an NLG endpoint.""" return { "type": "object", "properties": { "template": {"type": "string"}, "arguments": {"type": "object"}, "tracker": { "type": "object", "properties": { "sender_id": {"type": "string"}, "slots": {"type": "object"}, "latest_message": {"type": "object"}, "latest_event_time": {"type": "number"}, "paused": {"type": "boolean"}, "events": {"type": "array"}, }, }, "channel": {"type": "object", "properties": {"name": {"type": "string"}}}, }, } def nlg_request_format( template_name: Text, tracker: DialogueStateTracker, output_channel: Text, **kwargs: Any, ) -> Dict[Text, Any]: """Create the json body for the NLG json body for the request.""" tracker_state = tracker.current_state(EventVerbosity.ALL) return { "template": template_name, "arguments": kwargs, "tracker": tracker_state, "channel": {"name": output_channel}, } class GraphQLNaturalLanguageGenerator(NaturalLanguageGenerator): """Like Rasa's CallbackNLG, but queries Botfront's GraphQL endpoint""" def __init__(self, **kwargs) -> None: endpoint_config = kwargs.get("endpoint_config") self.nlg_endpoint = endpoint_config async def generate( self, template_name: Text, tracker: DialogueStateTracker, output_channel: Text, **kwargs: Any, ) -> List[Dict[Text, Any]]: fallback_language_slot = tracker.slots.get("fallback_language") fallback_language = fallback_language_slot.initial_value if fallback_language_slot else None language = tracker.latest_message.metadata.get("language") or fallback_language body = nlg_request_format( template_name, tracker, output_channel, **kwargs, language=language, projectId=os.environ.get("BF_PROJECT_ID"), ) logger.debug( "Requesting NLG for {} from {}." "".format(template_name, self.nlg_endpoint.url) ) try: if "graphql" in self.nlg_endpoint.url: from sgqlc.endpoint.http import HTTPEndpoint response = HTTPEndpoint(self.nlg_endpoint.url)(NLG_QUERY, body) response = response["data"]["getResponse"] else: response = await self.nlg_endpoint.request( method="post", json=body, timeout=DEFAULT_REQUEST_TIMEOUT ) response = response[0] # legacy route, use first message in seq except Exception as e: logger.error("NLG web endpoint returned an invalid response: {}".format(e)) return {"text": template_name} if self.validate_response(response): return response else: logger.error("NLG web endpoint returned an invalid response.") return {"text": template_name} @staticmethod def validate_response(content: Optional[Dict[Text, Any]]) -> bool: """Validate the NLG response. Raises exception on failure.""" from jsonschema import validate from jsonschema import ValidationError try: if content is None or content == "": # means the endpoint did not want to respond with anything return True else: validate(content, nlg_response_format_spec()) return True except ValidationError as e: e.message += ( ". Failed to validate NLG response from API, make sure your " "response from the NLG endpoint is valid. " "For more information about the format please consult the " "`nlg_response_format_spec` function from this same module: " "https://github.com/RasaHQ/rasa/blob/master/rasa/core/nlg/callback.py#L12" ) raise e
py
1a4b77afd606baf5ee1f64fceb577dcffeeb4022
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function import sys import os from .tools import logger, run_process, try_to_wrap_executable, find_output_arg, execute, check_program from .constants import CC, CXX, WASI_SYSROOT, STUBS_SYSTEM_LIB, STUBS_SYSTEM_PREAMBLE def run(args): main_program = CXX if args[0].endswith("wasmc++") else CC check_program(main_program) if '--version' in args: print('''wasienv (wasienv gcc/clang-like replacement)''') return 0 if len(args) == 1 and args[0] == '-v': # -v with no inputs # autoconf likes to see 'GNU' in the output to enable shared object support print('wasienv (wasienv gcc/clang-like replacement + linker emulating GNU ld)', file=sys.stderr) code = run_process([main_program, '-v'], check=False).returncode return code has_target = any([arg.startswith("--target") for arg in args]) # Flags decided by following: https://github.com/wasienv/wasienv/pull/8 args.append("--no-standard-libraries") args.append("-Wl,--export-all") args.append("-Wl,--no-entry") if not has_target: args.append("--target=wasm32-unknown-unknown") proc_args = [main_program]+args[1:] return_code = run_process(proc_args, check=False) target, outargs = find_output_arg(args) if target: try_to_wrap_executable(target) return return_code if __name__ == '__main__': execute(run)
py
1a4b78024e1a3216eacb2159b4460a23282febc6
#!/usr/bin/env python # -*- coding: utf-8 -*- import io import os import sys import platform from shutil import rmtree from setuptools import find_packages, setup, Command if platform.system() == 'Windows': import py2exe NAME = 'sysl' DESCRIPTION = 'System specification language with compiler and code generator' URL = 'https://github.com/anz-bank/sysl' EMAIL = '[email protected]' AUTHOR = 'ANZ' REQUIRED = [ 'httplib2', 'urllib3==1.24.2', 'openpyxl', 'plantuml', 'protobuf', 'pylint', 'PyYAML', 'requests', 'six' ] here = os.path.abspath(os.path.dirname(__file__)) with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f: long_description = '\n' + f.read() about = {} with open(os.path.join(here, 'src', NAME, '__version__.py')) as f: exec(f.read(), about) setup( name=NAME, version=about['__version__'], description=DESCRIPTION, long_description=long_description, long_description_content_type='text/markdown', author=AUTHOR, author_email=EMAIL, url=URL, package_dir={'': 'src'}, packages=find_packages('src', exclude=('tests',)), entry_points={ 'console_scripts': [ 'sysl=sysl.core.__main__:main', 'reljam=sysl.reljam.reljam:main', ], }, install_requires=REQUIRED, include_package_data=True, license='Apache 2.0', classifiers=[ 'License :: OSI Approved :: Apache Software License', 'Programming Language :: Python', 'Programming Language :: Python :: 2.7', ], extras_require={ 'dev': [ 'pytest', 'flake8', ] }, # py2exe options={'py2exe': { 'bundle_files': 1, 'dll_excludes': ['w9xpopen.exe', 'libstdc++-6.dll', 'libgcc_s_dw2-1.dll'] }}, console=[{ 'script': 'src/sysl/core/__main__.py', 'dest_base': 'sysl', 'icon_resources': [(1, 'docs/favicon.ico')] }, { 'script': 'src/sysl/reljam/__main__.py', 'dest_base': 'reljam', 'icon_resources': [(1, 'docs/favicon.ico')] }], zipfile=None )
py
1a4b7926441770707739e1d6299d9865068653fe
"""Provide access to Python's configuration information. The specific configuration variables available depend heavily on the platform and configuration. The values may be retrieved using get_config_var(name), and the list of variables is available via get_config_vars().keys(). Additional convenience functions are also available. Written by: Fred L. Drake, Jr. Email: <[email protected]> """ __revision__ = "$Id$" import os import re import string import sys from distutils.errors import DistutilsPlatformError # These are needed in a couple of spots, so just compute them once. PREFIX = os.path.normpath(sys.prefix) EXEC_PREFIX = os.path.normpath(sys.exec_prefix) # Path to the base directory of the project. On Windows the binary may # live in project/PCBuild9. If we're dealing with an x64 Windows build, # it'll live in project/PCbuild/amd64. if sys.executable: project_base = os.path.dirname(os.path.abspath(sys.executable)) else: # sys.executable can be empty if argv[0] has been changed and Python is # unable to retrieve the real program name project_base = os.getcwd() if os.name == "nt" and "pcbuild" in project_base[-8:].lower(): project_base = os.path.abspath(os.path.join(project_base, os.path.pardir)) # PC/VS7.1 if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower(): project_base = os.path.abspath(os.path.join(project_base, os.path.pardir, os.path.pardir)) # PC/AMD64 if os.name == "nt" and "\\pcbuild\\amd64" in project_base[-14:].lower(): project_base = os.path.abspath(os.path.join(project_base, os.path.pardir, os.path.pardir)) # set for cross builds if "_PYTHON_PROJECT_BASE" in os.environ: # this is the build directory, at least for posix project_base = os.path.normpath(os.environ["_PYTHON_PROJECT_BASE"]) # python_build: (Boolean) if true, we're either building Python or # building an extension with an un-installed Python, so we use # different (hard-wired) directories. # Setup.local is available for Makefile builds including VPATH builds, # Setup.dist is available on Windows def _python_build(): for fn in ("Setup.dist", "Setup.local"): if os.path.isfile(os.path.join(project_base, "Modules", fn)): return True return False python_build = _python_build() def get_python_version(): """Return a string containing the major and minor Python version, leaving off the patchlevel. Sample return values could be '1.5' or '2.2'. """ return sys.version[:3] def get_python_inc(plat_specific=0, prefix=None): """Return the directory containing installed Python header files. If 'plat_specific' is false (the default), this is the path to the non-platform-specific header files, i.e. Python.h and so on; otherwise, this is the path to platform-specific header files (namely pyconfig.h). If 'prefix' is supplied, use it instead of sys.prefix or sys.exec_prefix -- i.e., ignore 'plat_specific'. """ if prefix is None: prefix = plat_specific and EXEC_PREFIX or PREFIX if os.name == "posix": if python_build: if sys.executable: buildir = os.path.dirname(sys.executable) else: # sys.executable can be empty if argv[0] has been changed # and Python is unable to retrieve the real program name buildir = os.getcwd() if plat_specific: # python.h is located in the buildir inc_dir = buildir else: # the source dir is relative to the buildir srcdir = os.path.abspath(os.path.join(buildir, get_config_var('srcdir'))) # Include is located in the srcdir inc_dir = os.path.join(srcdir, "Include") return inc_dir return os.path.join(prefix, "include", "python" + get_python_version()) elif os.name == "nt": return os.path.join(prefix, "include") elif os.name == "os2": return os.path.join(prefix, "Include") else: raise DistutilsPlatformError( "I don't know where Python installs its C header files " "on platform '%s'" % os.name) def get_python_lib(plat_specific=0, standard_lib=0, prefix=None): """Return the directory containing the Python library (standard or site additions). If 'plat_specific' is true, return the directory containing platform-specific modules, i.e. any module from a non-pure-Python module distribution; otherwise, return the platform-shared library directory. If 'standard_lib' is true, return the directory containing standard Python library modules; otherwise, return the directory for site-specific modules. If 'prefix' is supplied, use it instead of sys.prefix or sys.exec_prefix -- i.e., ignore 'plat_specific'. """ if prefix is None: prefix = plat_specific and EXEC_PREFIX or PREFIX if os.name == "posix": libpython = os.path.join(prefix, "lib", "python" + get_python_version()) if standard_lib: return libpython else: return os.path.join(libpython, "site-packages") elif os.name == "nt": if standard_lib: return os.path.join(prefix, "Lib") else: if get_python_version() < "2.2": return prefix else: return os.path.join(prefix, "Lib", "site-packages") elif os.name == "os2": if standard_lib: return os.path.join(prefix, "Lib") else: return os.path.join(prefix, "Lib", "site-packages") else: raise DistutilsPlatformError( "I don't know where Python installs its library " "on platform '%s'" % os.name) def customize_compiler(compiler): """Do any platform-specific customization of a CCompiler instance. Mainly needed on Unix, so we can plug in the information that varies across Unices and is stored in Python's Makefile. """ if compiler.compiler_type == "unix": if sys.platform == "darwin": # Perform first-time customization of compiler-related # config vars on OS X now that we know we need a compiler. # This is primarily to support Pythons from binary # installers. The kind and paths to build tools on # the user system may vary significantly from the system # that Python itself was built on. Also the user OS # version and build tools may not support the same set # of CPU architectures for universal builds. global _config_vars # Use get_config_var() to ensure _config_vars is initialized. if not get_config_var('CUSTOMIZED_OSX_COMPILER'): import _osx_support _osx_support.customize_compiler(_config_vars) _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True' (cc, cxx, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \ get_config_vars('CC', 'CXX', 'CFLAGS', 'CCSHARED', 'LDSHARED', 'SO', 'AR', 'ARFLAGS') if 'CC' in os.environ: newcc = os.environ['CC'] if (sys.platform == 'darwin' and 'LDSHARED' not in os.environ and ldshared.startswith(cc)): # On OS X, if CC is overridden, use that as the default # command for LDSHARED as well ldshared = newcc + ldshared[len(cc):] cc = newcc if 'CXX' in os.environ: cxx = os.environ['CXX'] if 'LDSHARED' in os.environ: ldshared = os.environ['LDSHARED'] if 'CPP' in os.environ: cpp = os.environ['CPP'] else: cpp = cc + " -E" # not always if 'LDFLAGS' in os.environ: ldshared = ldshared + ' ' + os.environ['LDFLAGS'] if 'CFLAGS' in os.environ: cflags = cflags + ' ' + os.environ['CFLAGS'] ldshared = ldshared + ' ' + os.environ['CFLAGS'] if 'CPPFLAGS' in os.environ: cpp = cpp + ' ' + os.environ['CPPFLAGS'] cflags = cflags + ' ' + os.environ['CPPFLAGS'] ldshared = ldshared + ' ' + os.environ['CPPFLAGS'] if 'AR' in os.environ: ar = os.environ['AR'] if 'ARFLAGS' in os.environ: archiver = ar + ' ' + os.environ['ARFLAGS'] else: archiver = ar + ' ' + ar_flags cc_cmd = cc + ' ' + cflags compiler.set_executables( preprocessor=cpp, compiler=cc_cmd, compiler_so=cc_cmd + ' ' + ccshared, compiler_cxx=cxx, linker_so=ldshared, linker_exe=cc, archiver=archiver) compiler.shared_lib_extension = so_ext def get_config_h_filename(): """Return full pathname of installed pyconfig.h file.""" if python_build: if os.name == "nt": inc_dir = os.path.join(project_base, "PC") else: inc_dir = project_base else: inc_dir = get_python_inc(plat_specific=1) if get_python_version() < '2.2': config_h = 'config.h' else: # The name of the config.h file changed in 2.2 config_h = 'pyconfig.h' return os.path.join(inc_dir, config_h) def get_makefile_filename(): """Return full pathname of installed Makefile from the Python build.""" if python_build: return os.path.join(project_base, "Makefile") lib_dir = get_python_lib(plat_specific=1, standard_lib=1) return os.path.join(lib_dir, "config", "Makefile") def parse_config_h(fp, g=None): """Parse a config.h-style file. A dictionary containing name/value pairs is returned. If an optional dictionary is passed in as the second argument, it is used instead of a new dictionary. """ if g is None: g = {} define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n") undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n") # while 1: line = fp.readline() if not line: break m = define_rx.match(line) if m: n, v = m.group(1, 2) try: v = int(v) except ValueError: pass g[n] = v else: m = undef_rx.match(line) if m: g[m.group(1)] = 0 return g # Regexes needed for parsing Makefile (and similar syntaxes, # like old-style Setup files). _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)") _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)") _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}") def parse_makefile(fn, g=None): """Parse a Makefile-style file. A dictionary containing name/value pairs is returned. If an optional dictionary is passed in as the second argument, it is used instead of a new dictionary. """ from distutils.text_file import TextFile fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1) if g is None: g = {} done = {} notdone = {} while 1: line = fp.readline() if line is None: # eof break m = _variable_rx.match(line) if m: n, v = m.group(1, 2) v = v.strip() # `$$' is a literal `$' in make tmpv = v.replace('$$', '') if "$" in tmpv: notdone[n] = v else: try: v = int(v) except ValueError: # insert literal `$' done[n] = v.replace('$$', '$') else: done[n] = v # do variable interpolation here while notdone: for name in notdone.keys(): value = notdone[name] m = _findvar1_rx.search(value) or _findvar2_rx.search(value) if m: n = m.group(1) found = True if n in done: item = str(done[n]) elif n in notdone: # get it on a subsequent round found = False elif n in os.environ: # do it like make: fall back to environment item = os.environ[n] else: done[n] = item = "" if found: after = value[m.end():] value = value[:m.start()] + item + after if "$" in after: notdone[name] = value else: try: value = int(value) except ValueError: done[name] = value.strip() else: done[name] = value del notdone[name] else: # bogus variable reference; just drop it since we can't deal del notdone[name] fp.close() # strip spurious spaces for k, v in done.items(): if isinstance(v, str): done[k] = v.strip() # save the results in the global dictionary g.update(done) return g def expand_makefile_vars(s, vars): """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in 'string' according to 'vars' (a dictionary mapping variable names to values). Variables not present in 'vars' are silently expanded to the empty string. The variable values in 'vars' should not contain further variable expansions; if 'vars' is the output of 'parse_makefile()', you're fine. Returns a variable-expanded version of 's'. """ # This algorithm does multiple expansion, so if vars['foo'] contains # "${bar}", it will expand ${foo} to ${bar}, and then expand # ${bar}... and so forth. This is fine as long as 'vars' comes from # 'parse_makefile()', which takes care of such expansions eagerly, # according to make's variable expansion semantics. while 1: m = _findvar1_rx.search(s) or _findvar2_rx.search(s) if m: (beg, end) = m.span() s = s[0:beg] + vars.get(m.group(1)) + s[end:] else: break return s _config_vars = None def _init_posix(): """Initialize the module as appropriate for POSIX systems.""" # _sysconfigdata is generated at build time, see the sysconfig module from _sysconfigdata import build_time_vars global _config_vars _config_vars = {} _config_vars.update(build_time_vars) def _init_nt(): """Initialize the module as appropriate for NT""" g = {} # set basic install directories g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) # XXX hmmm.. a normal install puts include files here g['INCLUDEPY'] = get_python_inc(plat_specific=0) g['SO'] = '.pyd' g['EXE'] = ".exe" g['VERSION'] = get_python_version().replace(".", "") g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable)) global _config_vars _config_vars = g def _init_os2(): """Initialize the module as appropriate for OS/2""" g = {} # set basic install directories g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) # XXX hmmm.. a normal install puts include files here g['INCLUDEPY'] = get_python_inc(plat_specific=0) g['SO'] = '.pyd' g['EXE'] = ".exe" global _config_vars _config_vars = g def get_config_vars(*args): """With no arguments, return a dictionary of all configuration variables relevant for the current platform. Generally this includes everything needed to build extensions and install both pure modules and extensions. On Unix, this means every variable defined in Python's installed Makefile; on Windows and Mac OS it's a much smaller set. With arguments, return a list of values that result from looking up each argument in the configuration variable dictionary. """ global _config_vars if _config_vars is None: func = globals().get("_init_" + os.name) if func: func() else: _config_vars = {} # Normalized versions of prefix and exec_prefix are handy to have; # in fact, these are the standard versions used most places in the # Distutils. _config_vars['prefix'] = PREFIX _config_vars['exec_prefix'] = EXEC_PREFIX # OS X platforms require special customization to handle # multi-architecture, multi-os-version installers if sys.platform == 'darwin': import _osx_support _osx_support.customize_config_vars(_config_vars) if args: vals = [] for name in args: vals.append(_config_vars.get(name)) return vals else: return _config_vars def get_config_var(name): """Return the value of a single variable using the dictionary returned by 'get_config_vars()'. Equivalent to get_config_vars().get(name) """ return get_config_vars().get(name)
py
1a4b79e6ae1ca9444ab3827decbcc6847f33d27c
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import print_function import copy import warnings import paddle from paddle.fluid.framework import dygraph_only from paddle.fluid import compiler from .role_maker import UserDefinedRoleMaker, PaddleCloudRoleMaker, RoleMakerBase from .strategy_compiler import StrategyCompiler from .distributed_strategy import DistributedStrategy from .meta_optimizer_factory import MetaOptimizerFactory from .runtime_factory import RuntimeFactory from paddle.fluid.wrapped_decorator import wrap_decorator from paddle.fluid.dygraph import parallel_helper def _inited_runtime_handler_(func): def __impl__(*args, **kwargs): cls = args[0] if cls._runtime_handle is None: raise ValueError("Fleet can not find suitable runtime handler") return func(*args, **kwargs) return __impl__ def _is_non_distributed_check_(func): def __impl__(*args, **kwargs): cls = args[0] if cls._role_maker is not None and cls._role_maker._is_non_distributed( ) is True: warnings.warn( "%s() function doesn't work when use non_distributed fleet." % (func.__name__)) return return func(*args, **kwargs) return __impl__ inited_runtime_handler = wrap_decorator(_inited_runtime_handler_) is_non_distributed_check = wrap_decorator(_is_non_distributed_check_) class Fleet(object): """ Unified API for distributed training of PaddlePaddle Please reference the https://github.com/PaddlePaddle/FleetX for details Returns: Fleet: A Fleet instance Example for collective training: .. code-block:: python import paddle paddle.enable_static() import paddle.distributed.fleet as fleet fleet.init(is_collective=True) strategy = fleet.DistributedStrategy() optimizer = paddle.optimizer.SGD(learning_rate=0.001) optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) # do distributed training Example for parameter server training: .. code-block:: python import paddle paddle.enable_static() import paddle.distributed.fleet as fleet strategy = fleet.DistributedStrategy() fleet.init(strategy=strategy) optimizer = paddle.optimizer.SGD(learning_rate=0.001) optimizer = fleet.distributed_optimizer(optimizer) if fleet.is_first_worker(): print("this is first worker") print("current node index: {}".format(fleet.worker_index())) print("total number of worker num: {}".format(fleet.worker_num())) if fleet.is_worker(): print("this is worker") print("worker endpoints: {}".format(fleet.worker_endpoints(to_string=True))) print("server num: {}".format(fleet.server_num())) print("server endpoints: {}".format(fleet.server_endpoints(to_string=True))) if fleet.is_server(): print("this is server") fleet.stop_worker() """ def __init__(self): self._role_maker = None self.strategy_compiler = None self._is_collective = False self._runtime_handle = None self._util = None self._context = {} def init(self, role_maker=None, is_collective=False, strategy=None): """ Initialize role_maker in Fleet. This function is responsible for the distributed architecture what you want to run your code behind. Args: role_maker (RoleMakerBase, optional): A ``RoleMakerBase`` containing the configuration of environment variables related to distributed training.If you did not initialize the rolemaker by yourself, it will be automatically initialized to PaddleRoleMaker. The default value is None. is_collective (Boolean, optional): A ``Boolean`` variable determines whether the program runs on the CPU or GPU. False means set distributed training using CPU, and True means GPU.The default value is False.The default value is False. strategy (DistributedStrategy): Extra properties for distributed training. For details, please refer to paddle.distributed.fleet.DistributedStrategy. Default: None. Returns: None Examples1: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() Examples2: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init(is_collective=True) Examples3: .. code-block:: python import paddle.distributed.fleet as fleet role = fleet.PaddleCloudRoleMaker() fleet.init(role) Examples4: .. code-block:: python import paddle.distributed.fleet as fleet strategy = fleet.DistributedStrategy() fleet.init(strategy=strategy) """ if strategy is None: strategy = DistributedStrategy() self._user_defined_strategy = copy.deepcopy(strategy) if role_maker is None: if isinstance(is_collective, bool): self._is_collective = is_collective self._role_maker = PaddleCloudRoleMaker( is_collective=self._is_collective) else: raise ValueError( "`is_collective` should be instance of `bool`, but got {}". format(type(is_collective))) else: if isinstance(role_maker, RoleMakerBase): self._role_maker = role_maker else: raise ValueError( "`role_maker` should be subclass of `RoleMakerBase`, but got {}". format(type(role_maker))) self._role_maker._generate_role() import paddle.distributed.fleet as fleet fleet.util._set_role_maker(self._role_maker) self.strategy_compiler = StrategyCompiler() if self._role_maker._is_non_distributed() and self._is_collective: if paddle.fluid.core.is_compiled_with_cuda(): gpus_num = paddle.fluid.core.get_cuda_device_count() if gpus_num != 1: raise ValueError( "CUDA_VISIBLE_DEVICES shoule be set only 1 card if you use `python` to launch fleet program." ) if paddle.fluid.framework.in_dygraph_mode(): if self.worker_num() == 1: return if parallel_helper._is_parallel_ctx_initialized(): warnings.warn( "The dygraph parallel environment has been initialized.") else: paddle.distributed.init_parallel_env() def is_first_worker(self): """ Check whether the node is the first instance of worker. Returns: bool: True if this is the first node of worker, False if not. Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() fleet.is_first_worker() """ return self._role_maker._is_first_worker() def worker_index(self): """ Get current worker index. Returns: int: node id Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() fleet.worker_index() """ return self._role_maker._worker_index() def worker_num(self): """ Get current total worker number. Returns: int: worker numbers Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() fleet.worker_num() """ return self._role_maker._worker_num() def is_worker(self): """ Check whether the node is an instance of worker. Returns: bool: True if this is a node of worker, False if not. Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() fleet.is_worker() """ return self._role_maker._is_worker() def worker_endpoints(self, to_string=False): """ Get current worker endpoints, such as ["127.0.0.1:1001", "127.0.0.1:1002"]. Returns: list/string: server endpoints Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() fleet.worker_endpoints() """ if to_string: return ",".join(self._role_maker._get_trainer_endpoints()) else: return self._role_maker._get_trainer_endpoints() def server_num(self): """ Get current total worker number. Returns: int: server number Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() fleet.server_num() """ return len(self._role_maker._get_pserver_endpoints()) def server_index(self): """ Get current server index. Returns: int: node id Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() fleet.server_index() """ return self._role_maker._server_index() def server_endpoints(self, to_string=False): """ Get current server endpoints, such as ["127.0.0.1:1001", "127.0.0.1:1002"]. Returns: list/string: server endpoints Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() fleet.server_endpoints() """ if to_string: return ",".join(self._role_maker._get_pserver_endpoints()) else: return self._role_maker._get_pserver_endpoints() def is_server(self): """ Check whether the node is an instance of server. Returns: bool: True if this is a node of server, False if not. Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() fleet.is_server() """ return self._role_maker._is_server( ) or self._role_maker._is_heter_worker() def barrier_worker(self): """ barrier all workers Returns: None """ self._role_maker._barrier("worker") @is_non_distributed_check @inited_runtime_handler def init_worker(self): """ initialize `Communicator` for parameter server training. Returns: None Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() # build net # fleet.distributed_optimizer(...) fleet.init_worker() """ self._runtime_handle._init_worker() @is_non_distributed_check @inited_runtime_handler def init_server(self, *args, **kwargs): """ init_server executor to initialize startup program, if the `args` is not empty, it will run load_persistables for increment training. Returns: None Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() # build net # fleet.distributed_optimizer(...) fleet.init_server() """ self._runtime_handle._init_server(*args, **kwargs) @is_non_distributed_check @inited_runtime_handler def run_server(self): """ run server will run pserver main program with executor. Returns: None Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() # build net # fleet.distributed_optimizer(...) if fleet.is_server(): fleet.init_server() """ self._runtime_handle._run_server() @is_non_distributed_check @inited_runtime_handler def stop_worker(self): """ stop `Communicator` and give training complete notice to parameter server. Returns: None Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() # build net # fleet.distributed_optimizer(...) fleet.init_server() """ self._runtime_handle._stop_worker() def save_inference_model(self, executor, dirname, feeded_var_names, target_vars, main_program=None, export_for_deployment=True): """ save inference model for inference. Returns: None Examples: .. code-block:: python import paddle.distributed.fleet as fleet fleet.init() # build net # fleet.distributed_optimizer(...) fleet.init_server() """ self._runtime_handle._save_inference_model( executor, dirname, feeded_var_names, target_vars, main_program, export_for_deployment) def save_persistables(self, executor, dirname, main_program=None, mode=1): """ saves all persistable tensors from :code:`main_program` to the folder :code:`dirname`. You can refer to The :code:`dirname` is used to specify the folder where persistable tensors are going to be saved. If you would like to save tensors in separate files, set :code:`filename` None. Args: executor(Executor): The executor to run for saving persistable tensors. You can refer to :ref:`api_guide_executor_en` for more details. dirname(str, optional): The saving directory path. When you need to save the parameter to the memory, set it to None. main_program(Program, optional): The program whose persistbale tensors will be saved. Default: None. Returns: None Examples: .. code-block:: text import paddle paddle.enable_static() import paddle.distributed.fleet as fleet fleet.init() # build net # fleet.distributed_optimizer(...) exe = paddle.static.Executor(paddle.CPUPlace()) fleet.save_persistables(exe, "dirname", paddle.static.default_main_program()) """ self._runtime_handle._save_persistables(executor, dirname, main_program, mode) def distributed_optimizer(self, optimizer, strategy=None): """ Optimizer for distributed training. For the distributed training, this method would rebuild a new instance of DistributedOptimizer. Which has basic Optimizer function and special features for distributed training. Args: optimizer(Optimizer): The executor to run for init server. strategy(DistributedStrategy): Extra properties for distributed optimizer. It is recommended to use DistributedStrategy in fleet.init(). The strategy here is for compatibility. If the strategy in fleet.distributed_optimizer() is not None, then it will overwrite the DistributedStrategy in fleet.init(), which will take effect in distributed training. Returns: Fleet: instance of fleet. Examples: .. code-block:: python import paddle import paddle.distributed.fleet as fleet fleet.init(is_collective=True) strategy = fleet.DistributedStrategy() optimizer = paddle.optimizer.SGD(learning_rate=0.001) optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) """ self.user_defined_optimizer = optimizer if strategy is not None: warnings.warn( "It is recommended to use DistributedStrategy " "in fleet.init(). The strategy here is only for compatibility. " "If the strategy in fleet.distributed_optimizer() is " "not None, then it will overwrite the DistributedStrategy in fleet.init(), " "which will take effect in distributed training.") self._user_defined_strategy = copy.deepcopy(strategy) self._context = {} return self @dygraph_only def distributed_model(self, model): """ Return distributed data parallel model (Only work in dygraph mode) Args: model (Layer): the user-defind model which inherits Layer. Returns: distributed data parallel model which inherits Layer. Examples: .. code-block:: python import paddle import paddle.nn as nn from paddle.distributed import fleet class LinearNet(nn.Layer): def __init__(self): super(LinearNet, self).__init__() self._linear1 = nn.Linear(10, 10) self._linear2 = nn.Linear(10, 1) def forward(self, x): return self._linear2(self._linear1(x)) # 1. initialize fleet environment fleet.init(is_collective=True) # 2. create layer & optimizer layer = LinearNet() loss_fn = nn.MSELoss() adam = paddle.optimizer.Adam( learning_rate=0.001, parameters=layer.parameters()) # 3. get data_parallel model using fleet adam = fleet.distributed_optimizer(adam) dp_layer = fleet.distributed_model(layer) # 4. run layer inputs = paddle.randn([10, 10], 'float32') outputs = dp_layer(inputs) labels = paddle.randn([10, 1], 'float32') loss = loss_fn(outputs, labels) print("loss:", loss.numpy()) loss.backward() adam.step() adam.clear_grad() """ assert model is not None self.model = paddle.DataParallel( model, comm_buffer_size=self._user_defined_strategy.fuse_grad_size_in_MB, last_comm_buffer_size=self._user_defined_strategy. last_comm_group_size_MB) return self.model @dygraph_only def state_dict(self): """ Get state dict information from optimizer. (Only work in dygraph mode) Returns: state_dict(dict) : dict contains all the Tensor used by optimizer Examples: .. code-block:: python import numpy as np import paddle from paddle.distributed import fleet fleet.init(is_collective=True) value = np.arange(26).reshape(2, 13).astype("float32") a = paddle.to_tensor(value) layer = paddle.nn.Linear(13, 5) adam = paddle.optimizer.Adam(learning_rate=0.01, parameters=layer.parameters()) adam = fleet.distributed_optimizer(adam) dp_layer = fleet.distributed_model(layer) state_dict = adam.state_dict() """ # imitate target optimizer retrieval return self.user_defined_optimizer.state_dict() @dygraph_only def set_state_dict(self, state_dict): """ Load optimizer state dict. (Only work in dygraph mode) Args: state_dict(dict) : Dict contains all the Tensor needed by optimizer Returns: None Examples: .. code-block:: python import numpy as np import paddle from paddle.distributed import fleet fleet.init(is_collective=True) value = np.arange(26).reshape(2, 13).astype("float32") a = paddle.to_tensor(value) layer = paddle.nn.Linear(13, 5) adam = paddle.optimizer.Adam(learning_rate=0.01, parameters=layer.parameters()) adam = fleet.distributed_optimizer(adam) dp_layer = fleet.distributed_model(layer) state_dict = adam.state_dict() paddle.save(state_dict, "paddle_dy") para_state_dict = paddle.load("paddle_dy") adam.set_state_dict(para_state_dict) """ # imitate target optimizer retrieval return self.user_defined_optimizer.set_state_dict(state_dict) @dygraph_only def set_lr(self, value): """ Set the value of the learning rate manually in the optimizer. (Only work in dygraph mode) Args: value (float|Tensor): the value of learning rate Returns: None Examples: .. code-block:: python import numpy as np import paddle from paddle.distributed import fleet fleet.init(is_collective=True) value = np.arange(26).reshape(2, 13).astype("float32") a = paddle.to_tensor(value) layer = paddle.nn.Linear(13, 5) adam = paddle.optimizer.Adam(learning_rate=0.01, parameters=layer.parameters()) adam = fleet.distributed_optimizer(adam) dp_layer = fleet.distributed_model(layer) lr_list = [0.2, 0.3, 0.4, 0.5, 0.6] for i in range(5): adam.set_lr(lr_list[i]) lr = adam.get_lr() print("current lr is {}".format(lr)) # Print: # current lr is 0.2 # current lr is 0.3 # current lr is 0.4 # current lr is 0.5 # current lr is 0.6 """ # imitate target optimizer retrieval return self.user_defined_optimizer.set_lr(value) @dygraph_only def get_lr(self): """ Get current step learning rate. (Only work in dygraph mode) Returns: float: The learning rate of the current step. Examples: .. code-block:: python import numpy as np import paddle from paddle.distributed import fleet fleet.init(is_collective=True) value = np.arange(26).reshape(2, 13).astype("float32") a = paddle.to_tensor(value) layer = paddle.nn.Linear(13, 5) adam = paddle.optimizer.Adam(learning_rate=0.01, parameters=layer.parameters()) adam = fleet.distributed_optimizer(adam) dp_layer = fleet.distributed_model(layer) lr = adam.get_lr() print(lr) # 0.01 """ # imitate target optimizer retrieval return self.user_defined_optimizer.get_lr() @dygraph_only def step(self): """ Execute the optimizer once. (Only work in dygraph mode) Returns: None Examples: .. code-block:: python import paddle import paddle.nn as nn from paddle.distributed import fleet class LinearNet(nn.Layer): def __init__(self): super(LinearNet, self).__init__() self._linear1 = nn.Linear(10, 10) self._linear2 = nn.Linear(10, 1) def forward(self, x): return self._linear2(self._linear1(x)) # 1. initialize fleet environment fleet.init(is_collective=True) # 2. create layer & optimizer layer = LinearNet() loss_fn = nn.MSELoss() adam = paddle.optimizer.Adam( learning_rate=0.001, parameters=layer.parameters()) # 3. get data_parallel model using fleet adam = fleet.distributed_optimizer(adam) dp_layer = fleet.distributed_model(layer) # 4. run layer inputs = paddle.randn([10, 10], 'float32') outputs = dp_layer(inputs) labels = paddle.randn([10, 1], 'float32') loss = loss_fn(outputs, labels) print("loss:", loss.numpy()) loss.backward() adam.step() adam.clear_grad() """ # imitate target optimizer retrieval return self.user_defined_optimizer.step() @dygraph_only def clear_grad(self): """ Clear the gradients of all optimized parameters for model. (Only work in dygraph mode) Returns: None Examples: .. code-block:: python import paddle import paddle.nn as nn from paddle.distributed import fleet class LinearNet(nn.Layer): def __init__(self): super(LinearNet, self).__init__() self._linear1 = nn.Linear(10, 10) self._linear2 = nn.Linear(10, 1) def forward(self, x): return self._linear2(self._linear1(x)) # 1. initialize fleet environment fleet.init(is_collective=True) # 2. create layer & optimizer layer = LinearNet() loss_fn = nn.MSELoss() adam = paddle.optimizer.Adam( learning_rate=0.001, parameters=layer.parameters()) # 3. get data_parallel model using fleet adam = fleet.distributed_optimizer(adam) dp_layer = fleet.distributed_model(layer) # 4. run layer inputs = paddle.randn([10, 10], 'float32') outputs = dp_layer(inputs) labels = paddle.randn([10, 1], 'float32') loss = loss_fn(outputs, labels) print("loss:", loss.numpy()) loss.backward() adam.step() adam.clear_grad() """ # imitate target optimizer retrieval return self.user_defined_optimizer.clear_grad() def _final_strategy(self): if "valid_strategy" not in self._context: print( "WARNING: You may need to call minimize function before this function is called" ) return {} else: return self._context["valid_strategy"] def _get_applied_meta_list(self): if "applied_meta_list" not in self._context: print( "WARNING: You may need to call minimize function before _get_applied_meta_list called" ) return [] else: return self._context["applied_meta_list"] def _get_applied_graph_list(self): if "applied_graph_list" not in self._context: print( "WARNING: You may need to call minimize function before _get_applied_graph_list called" ) return [] else: return self._context["applied_graph_list"] def minimize(self, loss, startup_program=None, parameter_list=None, no_grad_set=None): """ Add distributed operations to minimize ``loss`` by updating ``parameter_list``. Args: loss (Tensor): A ``Tensor`` containing the value to minimize. startup_program (Program, optional): :ref:`api_fluid_Program` for initializing parameters in ``parameter_list``. The default value is None, at this time :ref:`api_fluid_default_startup_program` will be used. parameter_list (Iterable, optional): Iterable of ``Tensor`` or ``Tensor.name`` to update to minimize ``loss``. The default value is None, at this time all parameters will be updated. no_grad_set (set, optional): Set of ``Tensor`` or ``Tensor.name`` that don't need to be updated. The default value is None. Returns: tuple: tuple (optimize_ops, params_grads), A list of operators appended by minimize and a list of (param, grad) tensor pairs, param is ``Parameter``, grad is the gradient value corresponding to the parameter. The returned tuple can be passed to ``fetch_list`` in ``Executor.run()`` to indicate program pruning. If so, the program will be pruned by ``feed`` and ``fetch_list`` before run, see details in ``Executor``. Examples: .. code-block:: python import paddle paddle.enable_static() import paddle.distributed.fleet as fleet import paddle.nn.functional as F hid_dim = 10 label_dim = 2 input_x = paddle.static.data(name='x', shape=[None, 13], dtype='float32') input_y = paddle.static.data(name='y', shape=[None, 1], dtype='int64') fc_1 = paddle.static.nn.fc(x=input_x, size=hid_dim, activation='tanh') fc_2 = paddle.static.nn.fc(x=fc_1, size=hid_dim, activation='tanh') prediction = paddle.static.nn.fc(x=[fc_2], size=label_dim, activation='softmax') cost = F.cross_entropy(input=prediction, label=input_y) avg_cost = paddle.mean(x=cost) fleet.init(is_collective=True) strategy = fleet.DistributedStrategy() optimizer = paddle.optimizer.SGD(learning_rate=0.001) optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) optimizer.minimize(avg_cost) # for more examples, please reference https://github.com/PaddlePaddle/FleetX """ context = {} context["user_defined_strategy"] = copy.deepcopy( self._user_defined_strategy) if paddle.fluid.framework.in_dygraph_mode(): # imitate target optimizer retrieval target_opt = self.user_defined_optimizer self._context = context return target_opt.minimize(loss) # cache original feed forward program self.origin_main_program = loss.block.program context["origin_main_program"] = self.origin_main_program context["loss"] = loss if startup_program == None: self.origin_startup_program = \ paddle.static.default_startup_program().clone(for_test=False) startup_program = paddle.static.default_startup_program() else: self.origin_startup_program = \ startup_program.clone(for_test=False) context["origin_startup_program"] = startup_program context["role_maker"] = self._role_maker # compile time distributed_optimizer_list = \ MetaOptimizerFactory()._get_valid_meta_optimizers( self.user_defined_optimizer) context["user_defined_strategy"] = copy.deepcopy( self._user_defined_strategy) copy_user_defined_strategy = copy.deepcopy(self._user_defined_strategy) # trigger the auto-parallel in very strict condition # strategy = DistributedStrategy() # strategy.auto = True # optimizer = paddle.optimizer.SGD(learning_rate=0.1) # optimizer = fleet.distributed_optimizer(optimizer, strategy) if copy_user_defined_strategy._is_strict_auto(): # turn on all the strategy for each optimizer for opt in distributed_optimizer_list: opt._enable_strategy(copy_user_defined_strategy, context) valid_optimizer_list = [] valid_graph_optimizer_list = [] can_not_apply_optimizer_list = [] # recall meta optimizers for ranking for opt in distributed_optimizer_list: opt._set_basic_info(loss, self._role_maker, self.user_defined_optimizer, copy_user_defined_strategy) if opt._can_apply() and not opt._is_graph_out(): valid_optimizer_list.append(opt) elif opt._can_apply() and opt._is_graph_out(): valid_graph_optimizer_list.append(opt) else: can_not_apply_optimizer_list.append(opt) # combine recalled meta optimizers to be a valid meta optimizer meta_optimizer, graph_optimizer = \ self.strategy_compiler.generate_optimizer( loss, self._role_maker, self.user_defined_optimizer, copy_user_defined_strategy, valid_optimizer_list, valid_graph_optimizer_list) valid_strategy = self.strategy_compiler._get_valid_strategy( copy_user_defined_strategy, can_not_apply_optimizer_list) context["valid_strategy"] = copy.deepcopy(valid_strategy) applied_meta_list = self.strategy_compiler._get_applied_meta_list() applied_graph_list = self.strategy_compiler._get_applied_graph_list() context['applied_meta_list'] = applied_meta_list context['applied_graph_list'] = applied_graph_list self._context = context self.valid_strategy = valid_strategy self.valid_strategy._enable_env() optimize_ops = [] params_grads = [] if self._role_maker._is_non_distributed() and not self._is_collective: if self._runtime_handle is None: self._runtime_handle = RuntimeFactory()._create_runtime(context) compiled_program = compiler.CompiledProgram( self.origin_main_program).with_data_parallel( loss_name=loss.name, share_vars_from=None) loss.block.program._graph = compiled_program return self.user_defined_optimizer.minimize( loss, startup_program, parameter_list, no_grad_set=no_grad_set) if meta_optimizer: optimize_ops, params_grads = meta_optimizer.minimize( loss, startup_program, parameter_list, no_grad_set=no_grad_set) default_program = paddle.static.default_main_program() if id(default_program) != id(loss.block.program): paddle.fluid.framework.switch_main_program(loss.block.program) else: optimize_ops, params_grads = self.user_defined_optimizer.minimize( loss, startup_program, parameter_list, no_grad_set=no_grad_set) context["program_optimize_ops"] = optimize_ops context["program_params_grads"] = params_grads if graph_optimizer: optimize_ops, params_grads = graph_optimizer.minimize( loss, startup_program, parameter_list, no_grad_set=no_grad_set) # since we do not encourage users to use graph operations # if a graph optimizer takes effect, mostly # optimizers_ops and params_grads are None # i.e. users can not modify current computation graph anymore context["graph_optimize_ops"] = optimize_ops context["graph_optimize_grads"] = params_grads if self._runtime_handle is None: self._runtime_handle = RuntimeFactory()._create_runtime(context) import paddle.distributed.fleet as fleet fleet.util._set_strategy(context["valid_strategy"]) return optimize_ops, params_grads
py
1a4b7a498718d61a5f4578cf1ba8e5f748f7e195
# Examples - Simple from dataclasses import dataclass from flask import Flask from miko import Manager app = Flask(__name__) manager = Manager() @dataclass class User: name: str comment: str @app.get("/") def index(): return manager.render( "index.html", user=User( "麻弓=タイム", "小さい胸は貴重なのよっ、ステータスなのよっ!?" ), logged_in=True ) app.run()
py
1a4b7a8f254cad8783906d6b06baffc1a904900e
r""" Yang-Baxter Graphs """ #***************************************************************************** # Copyright (C) 2009 Franco Saliola <[email protected]> # # Distributed under the terms of the GNU General Public License (GPL) # # This code 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. # # The full text of the GPL is available at: # # http://www.gnu.org/licenses/ #***************************************************************************** from sage.graphs.digraph import DiGraph from sage.structure.sage_object import SageObject from sage.misc.lazy_attribute import lazy_attribute from sage.combinat.partition import Partition from sage.combinat.permutation import Permutation def YangBaxterGraph(partition=None, root=None, operators=None): r""" Construct the Yang-Baxter graph from ``root`` by repeated application of ``operators``, or the Yang-Baxter graph associated to ``partition``. INPUT: The user needs to provide either ``partition`` or both ``root`` and ``operators``, where - ``partition`` -- a partition of a positive integer - ``root`` -- the root vertex - ``operator`` - a function that maps vertices `u` to a list of tuples of the form `(v, l)` where `v` is a successor of `u` and `l` is the label of the edge from `u` to `v`. OUTPUT: - Either: - :class:`YangBaxterGraph_partition` - if partition is defined - :class:`YangBaxterGraph_generic` - if partition is ``None`` EXAMPLES: The Yang-Baxter graph defined by a partition `[p_1,\dots,p_k]` is the labelled directed graph with vertex set obtained by bubble-sorting `(p_k-1,p_k-2,\dots,0,\dots,p_1-1,p_1-2,\dots,0)`; there is an arrow from `u` to `v` labelled by `i` if `v` is obtained by swapping the `i`-th and `(i+1)`-th elements of `u`. For example, if the partition is `[3,1]`, then we begin with `(0,2,1,0)` and generate all tuples obtained from it by swapping two adjacent entries if they are increasing:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: bubbleswaps = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(0,2,1,0), operators=bubbleswaps); Y Yang-Baxter graph with root vertex (0, 2, 1, 0) sage: Y.vertices() [(2, 0, 1, 0), (2, 1, 0, 0), (0, 2, 1, 0)] The ``partition`` keyword is a shorthand for the above construction. :: sage: Y = YangBaxterGraph(partition=[3,1]); Y Yang-Baxter graph of [3, 1], with top vertex (0, 2, 1, 0) sage: Y.vertices() [(0, 2, 1, 0), (2, 0, 1, 0), (2, 1, 0, 0)] The permutahedron can be realized as a Yang-Baxter graph. :: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: swappers = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(1,2,3,4), operators=swappers); Y Yang-Baxter graph with root vertex (1, 2, 3, 4) sage: Y.plot() Graphics object consisting of 97 graphics primitives The Cayley graph of a finite group can be realized as a Yang-Baxter graph. :: sage: def left_multiplication_by(g): ... return lambda h : h*g sage: G = CyclicPermutationGroup(4) sage: operators = [ left_multiplication_by(gen) for gen in G.gens() ] sage: Y = YangBaxterGraph(root=G.identity(), operators=operators); Y Yang-Baxter graph with root vertex () sage: Y.plot(edge_labels=False) Graphics object consisting of 9 graphics primitives sage: G = SymmetricGroup(4) sage: operators = [left_multiplication_by(gen) for gen in G.gens()] sage: Y = YangBaxterGraph(root=G.identity(), operators=operators); Y Yang-Baxter graph with root vertex () sage: Y.plot(edge_labels=False) Graphics object consisting of 96 graphics primitives AUTHORS: - Franco Saliola (2009-04-23) """ if partition is None: return YangBaxterGraph_generic(root=root, operators=operators) else: return YangBaxterGraph_partition(partition=Partition(partition)) ##### General class for Yang-Baxter Graphs ################################ class YangBaxterGraph_generic(SageObject): def __init__(self, root, operators): r""" A class to model the Yang-Baxter graph defined by ``root`` and ``operators``. INPUT: - ``root`` -- the root vertex of the graph - ``operators`` -- a list of callables that map vertices to (new) vertices. .. NOTE:: This is a lazy implementation: the digraph is only computed when it is needed. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops); Y Yang-Baxter graph with root vertex (1, 0, 2, 1, 0) sage: loads(dumps(Y)) == Y True AUTHORS: - Franco Saliola (2009-04-23) """ self._root = root self._operators = operators def _successors(self, u): r""" Return a list of tuples for the form `(op(u), op)`, where op is one of the operators defining ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: Y._successors((1,0,2,1,0)) [((1, 2, 0, 1, 0), Swap-if-increasing at position 1)] """ successors = set() for op in self._operators: v = op(u) if v != u: successors.add((v, op)) return list(successors) def __repr__(self): r""" EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(2)] sage: Y = YangBaxterGraph(root=(1,2,3), operators=ops) sage: Y.__repr__() 'Yang-Baxter graph with root vertex (1, 2, 3)' """ return "Yang-Baxter graph with root vertex %s" % (self._root,) @lazy_attribute def _digraph(self): r""" Constructs the underlying digraph and stores the result as an attribute. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(2)] sage: Y = YangBaxterGraph(root=(1,2,3), operators=ops) sage: Y._digraph Digraph on 6 vertices """ digraph = DiGraph() digraph.add_vertex(self._root) queue = [self._root] while queue: u = queue.pop() for (v, l) in self._successors(u): if v not in digraph: queue.append(v) digraph.add_edge(u, v, l) return digraph def __hash__(self): r""" TESTS:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(2)] sage: Y = YangBaxterGraph(root=(1,2,3), operators=ops) sage: hash(Y) 1028420699 # 32-bit 7656306018247013467 # 64-bit """ # TODO: this is ugly but unavoidable: the Yang Baxter graphs are being # used in containers but are mutable. return hash(self._digraph.copy(immutable=True)) def __eq__(self, other): r""" EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y1 = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: Y2 = YangBaxterGraph(root=(2,0,2,1,0), operators=ops) sage: Y3 = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: Y1.__eq__(Y2) False sage: Y2.__eq__(Y2) True sage: Y1.__eq__(Y1) True sage: Y3.__eq__(Y1) True sage: Y3.__eq__(Y2) False """ return type(self) is type(other) and self._digraph.__eq__(other._digraph) def __ne__(self, other): r""" Test non-equality. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y1 = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: Y2 = YangBaxterGraph(root=(2,0,2,1,0), operators=ops) sage: Y3 = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: Y1.__ne__(Y2) True sage: Y2.__ne__(Y2) False sage: Y1.__ne__(Y1) False sage: Y3.__ne__(Y1) False sage: Y3.__ne__(Y2) True """ return not self.__eq__(other) def __iter__(self): r""" Returns an iterator of the vertices in ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: uniq(Y.__iter__()) [(1, 0, 2, 1, 0), (1, 2, 0, 1, 0), (1, 2, 1, 0, 0), (2, 1, 0, 1, 0), (2, 1, 1, 0, 0)] """ return self._digraph.vertex_iterator() def __len__(self): r""" Returns the number of vertices in ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: Y.__len__() 5 sage: ops = [SwapIncreasingOperator(i) for i in range(5)] sage: Y = YangBaxterGraph(root=(0,1,0,2,1,0), operators=ops) sage: Y.__len__() 16 """ return self._digraph.num_verts() def __copy__(self): r""" Returns a copy of ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops); Y Yang-Baxter graph with root vertex (1, 0, 2, 1, 0) sage: B = copy(Y); B Yang-Baxter graph with root vertex (1, 0, 2, 1, 0) sage: Y is B False sage: Y == B True """ from copy import copy Y = self.__class__(self._root, self._operators) Y._digraph = copy(self._digraph) return Y def _edges_in_bfs(self): r""" Returns an iterator of the edges of the digraph traversed in a breadth-first search of the vertices beginning at ``self.root()``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: list(Y._edges_in_bfs()) [((1, 0, 2, 1, 0), (1, 2, 0, 1, 0), Swap-if-increasing at position 1), ((1, 2, 0, 1, 0), (1, 2, 1, 0, 0), Swap-if-increasing at position 2), ((1, 2, 0, 1, 0), (2, 1, 0, 1, 0), Swap-if-increasing at position 0), ((2, 1, 0, 1, 0), (2, 1, 1, 0, 0), Swap-if-increasing at position 2)] """ digraph = self._digraph seen = {} queue = [self._root] seen[self._root] = True while queue: u = queue.pop() for w in digraph.neighbor_out_iterator(u): if w not in seen: seen[w] = True queue.append(w) yield (u,w,digraph.edge_label(u,w)) def root(self): r""" Returns the root vertex of ``self``. If ``self`` is the Yang-Baxter graph of the partition `[p_1,p_2,\dots,p_k]`, then this is the vertex `(p_k-1,p_k-2,\dots,0,\dots,p_1-1,p_1-2,\dots,0)`. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: Y.root() (1, 0, 2, 1, 0) sage: Y = YangBaxterGraph(root=(0,1,0,2,1,0), operators=ops) sage: Y.root() (0, 1, 0, 2, 1, 0) sage: Y = YangBaxterGraph(root=(1,0,3,2,1,0), operators=ops) sage: Y.root() (1, 0, 3, 2, 1, 0) sage: Y = YangBaxterGraph(partition=[3,2]) sage: Y.root() (1, 0, 2, 1, 0) """ return self._root def successors(self, v): r""" Return the successors of the vertex ``v``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: Y.successors(Y.root()) [(1, 2, 0, 1, 0)] sage: Y.successors((1, 2, 0, 1, 0)) [(1, 2, 1, 0, 0), (2, 1, 0, 1, 0)] """ return [a for (a,b) in self._successors(v)] def plot(self, *args, **kwds): r""" Plots ``self`` as a digraph. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: Y.plot() Graphics object consisting of 16 graphics primitives sage: Y.plot(edge_labels=False) Graphics object consisting of 11 graphics primitives """ if "edge_labels" not in kwds: kwds["edge_labels"] = True if "vertex_labels" not in kwds: kwds["vertex_labels"] = True return self._digraph.plot(*args, **kwds) def vertices(self): r""" Returns the vertices of ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(0,2,1,0), operators=ops) sage: Y.vertices() [(2, 0, 1, 0), (2, 1, 0, 0), (0, 2, 1, 0)] """ return list(self) def edges(self): r""" Returns the (labelled) edges of ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(0,2,1,0), operators=ops) sage: Y.edges() [((0, 2, 1, 0), (2, 0, 1, 0), Swap-if-increasing at position 0), ((2, 0, 1, 0), (2, 1, 0, 0), Swap-if-increasing at position 1)] """ return self._digraph.edges() def vertex_relabelling_dict(self, v, relabel_operator): r""" Return a dictionary pairing vertices ``u`` of ``self`` with the object obtained from ``v`` by applying the ``relabel_operator`` along a path from the root to ``u``. Note that the root is paired with ``v``. INPUT: - ``v`` -- an object - ``relabel_operator`` -- function mapping a vertex and a label to the image of the vertex OUTPUT: - dictionary pairing vertices with the corresponding image of ``v`` EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(0,2,1,0), operators=ops) sage: def relabel_operator(op, u): ... i = op.position() ... return u[:i] + u[i:i+2][::-1] + u[i+2:] sage: Y.vertex_relabelling_dict((1,2,3,4), relabel_operator) {(0, 2, 1, 0): (1, 2, 3, 4), (2, 0, 1, 0): (2, 1, 3, 4), (2, 1, 0, 0): (2, 3, 1, 4)} """ relabelling = {self._root:v} for (u,w,i) in self._edges_in_bfs(): relabelling[w] = relabel_operator(i, relabelling[u]) return relabelling def relabel_vertices(self, v, relabel_operator, inplace=True): r""" Relabel the vertices ``u`` of ``self`` by the object obtained from ``u`` by applying the ``relabel_operator`` to ``v`` along a path from ``self.root()`` to ``u``. Note that the ``self.root()`` is paired with ``v``. INPUT: - ``v`` -- tuple, Permutation, CombinatorialObject - ``inplace`` -- if ``True``, modifies ``self``; otherwise returns a modified copy of ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(0,2,1,0), operators=ops) sage: def relabel_op(op, u): ... i = op.position() ... return u[:i] + u[i:i+2][::-1] + u[i+2:] sage: d = Y.relabel_vertices((1,2,3,4), relabel_op, inplace=False); d Yang-Baxter graph with root vertex (1, 2, 3, 4) sage: Y.vertices() [(2, 0, 1, 0), (2, 1, 0, 0), (0, 2, 1, 0)] sage: e = Y.relabel_vertices((1,2,3,4), relabel_op); e sage: Y.vertices() [(2, 1, 3, 4), (1, 2, 3, 4), (2, 3, 1, 4)] """ from copy import copy relabelling = self.vertex_relabelling_dict(v, relabel_operator) Y = self if inplace else copy(self) Y._root = relabelling[Y._root] Y._digraph.relabel(relabelling, inplace=True) if inplace is False: return Y def relabel_edges(self, edge_dict, inplace=True): r""" Relabel the edges of ``self``. INPUT: - ``edge_dict`` -- a dictionary keyed by the (unlabelled) edges. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(0,2,1,0), operators=ops) sage: def relabel_op(op, u): ... i = op.position() ... return u[:i] + u[i:i+2][::-1] + u[i+2:] sage: Y.edges() [((0, 2, 1, 0), (2, 0, 1, 0), Swap-if-increasing at position 0), ((2, 0, 1, 0), (2, 1, 0, 0), Swap-if-increasing at position 1)] sage: d = {((0,2,1,0),(2,0,1,0)):17, ((2,0,1,0),(2,1,0,0)):27} sage: Y.relabel_edges(d, inplace=False).edges() [((0, 2, 1, 0), (2, 0, 1, 0), 17), ((2, 0, 1, 0), (2, 1, 0, 0), 27)] sage: Y.edges() [((0, 2, 1, 0), (2, 0, 1, 0), Swap-if-increasing at position 0), ((2, 0, 1, 0), (2, 1, 0, 0), Swap-if-increasing at position 1)] sage: Y.relabel_edges(d, inplace=True) sage: Y.edges() [((0, 2, 1, 0), (2, 0, 1, 0), 17), ((2, 0, 1, 0), (2, 1, 0, 0), 27)] """ if inplace: Y = self else: from copy import copy Y = copy(self) digraph = Y._digraph for (u,v,i) in digraph.edges(): digraph.set_edge_label(u,v,edge_dict[u,v]) if not inplace: return Y ##### Yang-Baxter Graphs defined by a partition ########################### class YangBaxterGraph_partition(YangBaxterGraph_generic): def __init__(self, partition): r""" A class to model the Yang-Baxter graph of a partition. The Yang-Baxter graph defined by a partition `[p_1,\dots,p_k]` is the labelled directed graph with vertex set obtained by bubble-sorting `(p_k-1,p_k-2,\dots,0,\dots,p_1-1,p_1-2,\dots,0)`; there is an arrow from `u` to `v` labelled by `i` if `v` is obtained by swapping the `i`-th and `(i+1)`-th elements of `u`. .. note:: This is a lazy implementation: the digraph is only computed when it is needed. EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,2,1]); Y Yang-Baxter graph of [3, 2, 1], with top vertex (0, 1, 0, 2, 1, 0) sage: loads(dumps(Y)) == Y True AUTHORS: - Franco Saliola (2009-04-23) """ self._partition = partition beta = sorted(self._partition, reverse=True) root = tuple(sum(map(range,beta), []))[::-1] operators = [SwapIncreasingOperator(i) for i in range(sum(partition)-1)] super(YangBaxterGraph_partition, self).__init__(root, operators) def __repr__(self): r""" EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,2]) sage: Y.__repr__() 'Yang-Baxter graph of [3, 2], with top vertex (1, 0, 2, 1, 0)' """ return "Yang-Baxter graph of %s, with top vertex %s" % (self._partition, self._root) def __copy__(self): r""" Returns a copy of ``self``. EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,2]); Y Yang-Baxter graph of [3, 2], with top vertex (1, 0, 2, 1, 0) sage: B = copy(Y); B Yang-Baxter graph of [3, 2], with top vertex (1, 0, 2, 1, 0) sage: Y is B False sage: Y == B True """ from copy import copy Y = self.__class__(self._partition) Y._digraph = copy(self._digraph) return Y @lazy_attribute def _digraph(self): r""" Constructs the underlying digraph and stores the result as an attribute. EXAMPLES:: sage: Y = YangBaxterGraph(partition=[2,1]) sage: Y._digraph Digraph on 2 vertices sage: Y.edges() [((0, 1, 0), (1, 0, 0), Swap positions 0 and 1)] """ digraph = super(YangBaxterGraph_partition, self)._digraph for (u,v,op) in digraph.edges(): digraph.set_edge_label(u,v,SwapOperator(op.position())) return digraph @lazy_attribute def _vertex_ordering(self): r""" Returns a list of the vertices of ``self``, sorted using Pythons ``sorted`` method. EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,2]) sage: Y._vertex_ordering [(1, 0, 2, 1, 0), (1, 2, 0, 1, 0), (1, 2, 1, 0, 0), (2, 1, 0, 1, 0), (2, 1, 1, 0, 0)] """ return sorted(self._digraph.vertices()) def __iter__(self): r""" Iterate over the vertices ``self``. .. NOTE:: The vertices are first sorted using Python's sorted command. EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,2]) sage: list(Y.__iter__()) [(1, 0, 2, 1, 0), (1, 2, 0, 1, 0), (1, 2, 1, 0, 0), (2, 1, 0, 1, 0), (2, 1, 1, 0, 0)] """ for v in self._vertex_ordering: yield v def _swap_operator(self, operator, u): r""" Return the image of ``u`` under ``operator``. INPUT: - ``i`` -- positive integer between 1 and len(u)-1, inclusive - ``u`` -- tuple, list, permutation, CombinatorialObject, .... EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,1]) sage: from sage.combinat.yang_baxter_graph import SwapOperator sage: ops = [SwapOperator(i) for i in range(3)] sage: [Y._swap_operator(op, (1,2,3,4)) for op in ops] [(2, 1, 3, 4), (1, 3, 2, 4), (1, 2, 4, 3)] sage: [Y._swap_operator(op, [4,3,2,1]) for op in ops] [[3, 4, 2, 1], [4, 2, 3, 1], [4, 3, 1, 2]] sage: [Y._swap_operator(op, Permutation([1,2,3,4])) for op in ops] [[2, 1, 3, 4], [1, 3, 2, 4], [1, 2, 4, 3]] """ return operator(u) def vertex_relabelling_dict(self, v): r""" Return a dictionary pairing vertices ``u`` of ``self`` with the object obtained from ``v`` by applying transpositions corresponding to the edges labels along a path from the root to ``u``. Note that the root is paired with ``v``. INPUT: - ``v`` -- an object OUTPUT: - dictionary pairing vertices with the corresponding image of ``v`` EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,1]) sage: Y.vertex_relabelling_dict((1,2,3,4)) {(0, 2, 1, 0): (1, 2, 3, 4), (2, 0, 1, 0): (2, 1, 3, 4), (2, 1, 0, 0): (2, 3, 1, 4)} sage: Y.vertex_relabelling_dict((4,3,2,1)) {(0, 2, 1, 0): (4, 3, 2, 1), (2, 0, 1, 0): (3, 4, 2, 1), (2, 1, 0, 0): (3, 2, 4, 1)} """ return super(YangBaxterGraph_partition, self).vertex_relabelling_dict(v, self._swap_operator) def relabel_vertices(self, v, inplace=True): r""" Relabel the vertices of ``self`` with the object obtained from ``v`` by applying the transpositions corresponding to the edge labels along some path from the root to the vertex. INPUT: - ``v`` -- tuple, Permutation, CombinatorialObject - ``inplace`` -- if ``True``, modifies ``self``; otherwise returns a modified copy of ``self``. EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,1]); Y Yang-Baxter graph of [3, 1], with top vertex (0, 2, 1, 0) sage: d = Y.relabel_vertices((1,2,3,4), inplace=False); d Digraph on 3 vertices sage: Y.vertices() [(0, 2, 1, 0), (2, 0, 1, 0), (2, 1, 0, 0)] sage: e = Y.relabel_vertices((1,2,3,4)); e sage: Y.vertices() [(1, 2, 3, 4), (2, 1, 3, 4), (2, 3, 1, 4)] """ relabelling = self.vertex_relabelling_dict(v) if inplace: Y = self Y._root = relabelling[Y._root] Y._digraph.relabel(relabelling, inplace=inplace) Y._vertex_ordering = sorted(Y._digraph.vertices()) return else: from copy import copy Y = copy(self) Y._root = relabelling[Y._root] return Y._digraph.relabel(relabelling, inplace=inplace) ##### Some Yang-Baxter operators ########################################## class SwapOperator(SageObject): def __init__(self, i): r""" The operator that swaps the items in positions ``i`` and ``i+1``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapOperator sage: s3 = SwapOperator(3) sage: s3 == loads(dumps(s3)) True """ self._position = i def __hash__(self): r""" TESTS:: sage: from sage.combinat.yang_baxter_graph import SwapOperator sage: s = [SwapOperator(i) for i in range(3)] sage: map(hash, s) [0, 1, 2] """ return hash(self._position) def __cmp__(self, other): r""" Compare two swap operators. The comparison is done by comparing the positions. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapOperator sage: s = [SwapOperator(i) for i in range(3)] sage: s[0] == s[0] True sage: s[1] < s[0] False sage: s[1] < s[2] True """ if type(self) is type(other): return cmp(self._position, other._position) else: return cmp(type(self), type(other)) def __repr__(self): r""" Representation string. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapOperator sage: s3 = SwapOperator(3) sage: s3.__repr__() 'Swap positions 3 and 4' """ return "Swap positions %s and %s" % (self._position, self._position+1) def __str__(self): r""" A short str representation (used, for example, in labelling edges of graphs). EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapOperator sage: s3 = SwapOperator(3) sage: s3.__str__() '3' """ return "%s" % self._position def __call__(self, u): r""" Return the object obtained from swapping the items in positions ``i`` and ``i+1`` of ``u``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapOperator sage: s3 = SwapOperator(3) sage: s3((1,2,3,4,5)) (1, 2, 3, 5, 4) sage: s3([1,2,3,4,5]) [1, 2, 3, 5, 4] """ i = self._position if isinstance(u, Permutation): return Permutation(u[:i] + u[i:i+2][::-1] + u[i+2:]) return type(u)(u[:i] + u[i:i+2][::-1] + u[i+2:]) def position(self): r""" ``self`` is the operator that swaps positions ``i`` and ``i+1``. This method returns ``i``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapOperator sage: s3 = SwapOperator(3) sage: s3.position() 3 """ return self._position class SwapIncreasingOperator(SwapOperator): def __repr__(self): r""" Representation string. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: s3 = SwapIncreasingOperator(3) sage: s3.__repr__() 'Swap-if-increasing at position 3' """ return "Swap-if-increasing at position %s" % self._position def __call__(self, u): r""" Returns a copy of ``u`` with ``u[i-1]`` and ``u[i]`` swapped if ``u[i-1] > u[i]``; otherwise returns ``u``. INPUT: - ``i`` -- positive integer between ``1`` and ``len(u)-1``, inclusive - ``u`` -- tuple, list, permutation, CombinatorialObject, .... EXAMPLES:: sage: Y = YangBaxterGraph(partition=[2,2]) sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: operators = [SwapIncreasingOperator(i) for i in range(3)] sage: [op((1,2,3,4)) for op in operators] [(2, 1, 3, 4), (1, 3, 2, 4), (1, 2, 4, 3)] sage: [op([4,3,2,1]) for op in operators] [[4, 3, 2, 1], [4, 3, 2, 1], [4, 3, 2, 1]] sage: [op(Permutation([1,3,2,4])) for op in operators] [[3, 1, 2, 4], [1, 3, 2, 4], [1, 3, 4, 2]] """ i = self._position j = i+1 if u[i] < u[j]: v = list(u) (v[j], v[i]) = (v[i], v[j]) if isinstance(u, Permutation): return Permutation(v) return type(u)(v) else: return u
py
1a4b7b48cae6cde77adcdc5a229d89a7d4d9df35
from django.db import models # Create your models here. class Destination: id: int name: str img: str desc: str price: int
py
1a4b7cb838bb85a90bbc4d216b0942dcbf698cc6
from django.contrib import admin from .models import Vehicle, Vehicle_version, trademark # Register your models here. class DetalleVehiculos(admin.ModelAdmin): list_display=('name','trademark','publication_status') list_filter= ('trademark','publication_status') search_fields = ('name','trademark') prepopulated_fields = {"v_slug": ("name",)} class trademarkl(admin.ModelAdmin): model = Vehicle list_display = ('version','vehicle','trademark2') search_fields = ('version','vehicle__name','vehicle__trademark__name') list_filter = ('vehicle__trademark',) def trademark2(self,obj): return obj.vehicle.trademark trademark2.short_description = 'Trademark' admin.site.register(Vehicle,DetalleVehiculos) admin.site.register(Vehicle_version,trademarkl) admin.site.register(trademark)
py
1a4b7d1615190ba56e402058156f847e4149e9a5
# coding: utf-8 """ Kubernetes No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) # noqa: E501 The version of the OpenAPI document: release-1.24 Generated by: https://openapi-generator.tech """ import pprint import re # noqa: F401 import six from kubernetes.client.configuration import Configuration class V1CSIStorageCapacityList(object): """NOTE: This class is auto generated by OpenAPI Generator. Ref: https://openapi-generator.tech Do not edit the class manually. """ """ Attributes: openapi_types (dict): The key is attribute name and the value is attribute type. attribute_map (dict): The key is attribute name and the value is json key in definition. """ openapi_types = { 'api_version': 'str', 'items': 'list[V1CSIStorageCapacity]', 'kind': 'str', 'metadata': 'V1ListMeta' } attribute_map = { 'api_version': 'apiVersion', 'items': 'items', 'kind': 'kind', 'metadata': 'metadata' } def __init__(self, api_version=None, items=None, kind=None, metadata=None, local_vars_configuration=None): # noqa: E501 """V1CSIStorageCapacityList - a model defined in OpenAPI""" # noqa: E501 if local_vars_configuration is None: local_vars_configuration = Configuration() self.local_vars_configuration = local_vars_configuration self._api_version = None self._items = None self._kind = None self._metadata = None self.discriminator = None if api_version is not None: self.api_version = api_version self.items = items if kind is not None: self.kind = kind if metadata is not None: self.metadata = metadata @property def api_version(self): """Gets the api_version of this V1CSIStorageCapacityList. # noqa: E501 APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources # noqa: E501 :return: The api_version of this V1CSIStorageCapacityList. # noqa: E501 :rtype: str """ return self._api_version @api_version.setter def api_version(self, api_version): """Sets the api_version of this V1CSIStorageCapacityList. APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources # noqa: E501 :param api_version: The api_version of this V1CSIStorageCapacityList. # noqa: E501 :type: str """ self._api_version = api_version @property def items(self): """Gets the items of this V1CSIStorageCapacityList. # noqa: E501 Items is the list of CSIStorageCapacity objects. # noqa: E501 :return: The items of this V1CSIStorageCapacityList. # noqa: E501 :rtype: list[V1CSIStorageCapacity] """ return self._items @items.setter def items(self, items): """Sets the items of this V1CSIStorageCapacityList. Items is the list of CSIStorageCapacity objects. # noqa: E501 :param items: The items of this V1CSIStorageCapacityList. # noqa: E501 :type: list[V1CSIStorageCapacity] """ if self.local_vars_configuration.client_side_validation and items is None: # noqa: E501 raise ValueError("Invalid value for `items`, must not be `None`") # noqa: E501 self._items = items @property def kind(self): """Gets the kind of this V1CSIStorageCapacityList. # noqa: E501 Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501 :return: The kind of this V1CSIStorageCapacityList. # noqa: E501 :rtype: str """ return self._kind @kind.setter def kind(self, kind): """Sets the kind of this V1CSIStorageCapacityList. Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501 :param kind: The kind of this V1CSIStorageCapacityList. # noqa: E501 :type: str """ self._kind = kind @property def metadata(self): """Gets the metadata of this V1CSIStorageCapacityList. # noqa: E501 :return: The metadata of this V1CSIStorageCapacityList. # noqa: E501 :rtype: V1ListMeta """ return self._metadata @metadata.setter def metadata(self, metadata): """Sets the metadata of this V1CSIStorageCapacityList. :param metadata: The metadata of this V1CSIStorageCapacityList. # noqa: E501 :type: V1ListMeta """ self._metadata = metadata def to_dict(self): """Returns the model properties as a dict""" result = {} for attr, _ in six.iteritems(self.openapi_types): value = getattr(self, attr) if isinstance(value, list): result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() elif isinstance(value, dict): result[attr] = dict(map( lambda item: (item[0], item[1].to_dict()) if hasattr(item[1], "to_dict") else item, value.items() )) else: result[attr] = value return result def to_str(self): """Returns the string representation of the model""" return pprint.pformat(self.to_dict()) def __repr__(self): """For `print` and `pprint`""" return self.to_str() def __eq__(self, other): """Returns true if both objects are equal""" if not isinstance(other, V1CSIStorageCapacityList): return False return self.to_dict() == other.to_dict() def __ne__(self, other): """Returns true if both objects are not equal""" if not isinstance(other, V1CSIStorageCapacityList): return True return self.to_dict() != other.to_dict()
py
1a4b7dcf876c6645cdb875ad4f8a432588126e96
import logging import os import tempfile from gensim import corpora from gensim.parsing.preprocessing import STOPWORDS from pprint import pprint # pretty-printer from collections import defaultdict class Indexer: def __init__(self): logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) self.TEMP_FOLDER = tempfile.gettempdir() print('Folder "{}" will be used to save temporary dictionary and corpus.'.format(self.TEMP_FOLDER)) self.documents = None def set_documents(self, documents): if documents is None: self.documents = ["Human machine interface for lab abc computer applications", "A survey of user opinion of computer system response time", "The EPS user interface management system", "System and human system engineering testing of EPS", "Relation of user perceived response time to error measurement", "The generation of random binary unordered trees", "The intersection graph of paths in trees", "Graph minors IV Widths of trees and well quasi ordering", "Graph minors A survey"] else: self.documents = documents def remove_stopwords(self): # remove common words and tokenize texts = [[word for word in document.lower().split() if word not in STOPWORDS] for document in self.documents] return texts def clean_low_freq_words(self, texts): # remove words that appear only once frequency = defaultdict(int) for text in texts: for token in text: frequency[token] += 1 texts = [[token for token in text if frequency[token] > 1] for text in texts] pprint(texts) if __name__ == "__main__": indexer = Indexer() indexer.set_documents(None) texts = indexer.remove_stopwords() indexer.clean_low_freq_words(texts)
py
1a4b7e65b1dff583a600a2833af0dbfa86e857f4
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # # http://www.apache.org/licenses/LICENSE-2.0 # # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. from aliyunsdkcore.request import RpcRequest from aliyunsdkwaf_openapi.endpoint import endpoint_data class CreateProtectionModuleRuleRequest(RpcRequest): def __init__(self): RpcRequest.__init__(self, 'waf-openapi', '2019-09-10', 'CreateProtectionModuleRule','waf') self.set_method('POST') if hasattr(self, "endpoint_map"): setattr(self, "endpoint_map", endpoint_data.getEndpointMap()) if hasattr(self, "endpoint_regional"): setattr(self, "endpoint_regional", endpoint_data.getEndpointRegional()) def get_DefenseType(self): # String return self.get_query_params().get('DefenseType') def set_DefenseType(self, DefenseType): # String self.add_query_param('DefenseType', DefenseType) def get_Rule(self): # String return self.get_query_params().get('Rule') def set_Rule(self, Rule): # String self.add_query_param('Rule', Rule) def get_InstanceId(self): # String return self.get_query_params().get('InstanceId') def set_InstanceId(self, InstanceId): # String self.add_query_param('InstanceId', InstanceId) def get_Domain(self): # String return self.get_query_params().get('Domain') def set_Domain(self, Domain): # String self.add_query_param('Domain', Domain)
py
1a4b7ea3f5f85eeda3db1a99b8d7014dbc48f7d2
from src.utils import * import numpy as np class SO3: #  tolerance criterion TOL = 1e-8 Id = torch.eye(3).cuda().float() dId = torch.eye(3).cuda().double() @classmethod # cls: class def exp(cls, phi): angle = phi.norm(dim=1, keepdim=True) mask = angle[:, 0] < cls.TOL dim_batch = phi.shape[0] Id = cls.Id.expand(dim_batch, 3, 3) axis = phi[~mask] / angle[~mask] c = angle[~mask].cos().unsqueeze(2)# add a 1 on second dimension s = angle[~mask].sin().unsqueeze(2) Rot = phi.new_empty(dim_batch, 3, 3) Rot[mask] = Id[mask] + SO3.wedge(phi[mask]) Rot[~mask] = c*Id[~mask] + \ (1-c)*cls.bouter(axis, axis) + s*cls.wedge(axis) return Rot @classmethod def log(cls, Rot): dim_batch = Rot.shape[0] Id = cls.Id.expand(dim_batch, 3, 3) cos_angle = (0.5 * cls.btrace(Rot) - 0.5).clamp(-1., 1.) #min:-1, max:+1 # Clip cos(angle) to its proper domain to avoid NaNs from rounding # errors angle = cos_angle.acos() mask = angle < cls.TOL if mask.sum() == 0: angle = angle.unsqueeze(1).unsqueeze(1) return cls.vee((0.5 * angle/angle.sin())*(Rot-Rot.transpose(1, 2))) elif mask.sum() == dim_batch: # If angle is close to zero, use first-order Taylor expansion return cls.vee(Rot - Id) phi = cls.vee(Rot - Id) angle = angle phi[~mask] = cls.vee((0.5 * angle[~mask]/angle[~mask].sin()).unsqueeze( 1).unsqueeze(2)*(Rot[~mask] - Rot[~mask].transpose(1, 2))) return phi @staticmethod def vee(Phi): return torch.stack((Phi[:, 2, 1], Phi[:, 0, 2], Phi[:, 1, 0]), dim=1) @staticmethod def wedge(phi): dim_batch = phi.shape[0] zero = phi.new_zeros(dim_batch) return torch.stack((zero, -phi[:, 2], phi[:, 1], phi[:, 2], zero, -phi[:, 0], -phi[:, 1], phi[:, 0], zero), 1).view(dim_batch, 3, 3) @classmethod def from_rpy(cls, roll, pitch, yaw):# rotation sequence z y' x'' return cls.rotz(yaw).bmm(cls.roty(pitch).bmm(cls.rotx(roll)))# tensor.bmm: batch matrix-matrix product,bij * bjk = bik @classmethod def rotx(cls, angle_in_radians): c = angle_in_radians.cos() s = angle_in_radians.sin() mat = c.new_zeros((c.shape[0], 3, 3))# is it the dim of c or (c.shape[0],3,3), should be latter one mat[:, 0, 0] = 1 mat[:, 1, 1] = c mat[:, 2, 2] = c mat[:, 1, 2] = -s mat[:, 2, 1] = s return mat @classmethod def roty(cls, angle_in_radians): c = angle_in_radians.cos() s = angle_in_radians.sin() mat = c.new_zeros((c.shape[0], 3, 3)) mat[:, 1, 1] = 1 mat[:, 0, 0] = c mat[:, 2, 2] = c mat[:, 0, 2] = s mat[:, 2, 0] = -s return mat @classmethod def rotz(cls, angle_in_radians): c = angle_in_radians.cos() s = angle_in_radians.sin() mat = c.new_zeros((c.shape[0], 3, 3)) mat[:, 2, 2] = 1 mat[:, 0, 0] = c mat[:, 1, 1] = c mat[:, 0, 1] = -s mat[:, 1, 0] = s return mat @classmethod def isclose(cls, x, y): return (x-y).abs() < cls.TOL @classmethod def to_rpy(cls, Rots): """Convert a rotation matrix to RPY Euler angles.""" pitch = torch.atan2(-Rots[:, 2, 0], torch.sqrt(Rots[:, 0, 0]**2 + Rots[:, 1, 0]**2)) yaw = pitch.new_empty(pitch.shape) roll = pitch.new_empty(pitch.shape) near_pi_over_two_mask = cls.isclose(pitch, np.pi / 2.) near_neg_pi_over_two_mask = cls.isclose(pitch, -np.pi / 2.) remainder_inds = ~(near_pi_over_two_mask | near_neg_pi_over_two_mask) yaw[near_pi_over_two_mask] = 0 roll[near_pi_over_two_mask] = torch.atan2( Rots[near_pi_over_two_mask, 0, 1], Rots[near_pi_over_two_mask, 1, 1]) yaw[near_neg_pi_over_two_mask] = 0. roll[near_neg_pi_over_two_mask] = -torch.atan2( Rots[near_neg_pi_over_two_mask, 0, 1], Rots[near_neg_pi_over_two_mask, 1, 1]) sec_pitch = 1/pitch[remainder_inds].cos() remainder_mats = Rots[remainder_inds] yaw = torch.atan2(remainder_mats[:, 1, 0] * sec_pitch, remainder_mats[:, 0, 0] * sec_pitch) roll = torch.atan2(remainder_mats[:, 2, 1] * sec_pitch, remainder_mats[:, 2, 2] * sec_pitch) rpys = torch.cat([roll.unsqueeze(dim=1), pitch.unsqueeze(dim=1), yaw.unsqueeze(dim=1)], dim=1) return rpys @classmethod def from_quaternion(cls, quat, ordering='wxyz'): """Form a rotation matrix from a unit length quaternion. Valid orderings are 'xyzw' and 'wxyz'. """ if ordering is 'xyzw': qx = quat[:, 0] qy = quat[:, 1] qz = quat[:, 2] qw = quat[:, 3] elif ordering is 'wxyz': qw = quat[:, 0] qx = quat[:, 1] qy = quat[:, 2] qz = quat[:, 3] # Form the matrix mat = quat.new_empty(quat.shape[0], 3, 3) qx2 = qx * qx qy2 = qy * qy qz2 = qz * qz mat[:, 0, 0] = 1. - 2. * (qy2 + qz2) mat[:, 0, 1] = 2. * (qx * qy - qw * qz) mat[:, 0, 2] = 2. * (qw * qy + qx * qz) mat[:, 1, 0] = 2. * (qw * qz + qx * qy) mat[:, 1, 1] = 1. - 2. * (qx2 + qz2) mat[:, 1, 2] = 2. * (qy * qz - qw * qx) mat[:, 2, 0] = 2. * (qx * qz - qw * qy) mat[:, 2, 1] = 2. * (qw * qx + qy * qz) mat[:, 2, 2] = 1. - 2. * (qx2 + qy2) return mat @classmethod def to_quaternion(cls, Rots, ordering='wxyz'): """Convert a rotation matrix to a unit length quaternion. Valid orderings are 'xyzw' and 'wxyz'. """ tmp = 1 + Rots[:, 0, 0] + Rots[:, 1, 1] + Rots[:, 2, 2] tmp[tmp < 0] = 0 qw = 0.5 * torch.sqrt(tmp) qx = qw.new_empty(qw.shape[0]) qy = qw.new_empty(qw.shape[0]) qz = qw.new_empty(qw.shape[0]) near_zero_mask = qw.abs() < cls.TOL if near_zero_mask.sum() > 0: cond1_mask = near_zero_mask * \ (Rots[:, 0, 0] > Rots[:, 1, 1])*(Rots[:, 0, 0] > Rots[:, 2, 2]) cond1_inds = cond1_mask.nonzero() if len(cond1_inds) > 0: cond1_inds = cond1_inds.squeeze() R_cond1 = Rots[cond1_inds].view(-1, 3, 3) #.view() is subset of .reshape() d = 2. * torch.sqrt(1. + R_cond1[:, 0, 0] - R_cond1[:, 1, 1] - R_cond1[:, 2, 2]).view(-1) qw[cond1_inds] = (R_cond1[:, 2, 1] - R_cond1[:, 1, 2]) / d qx[cond1_inds] = 0.25 * d qy[cond1_inds] = (R_cond1[:, 1, 0] + R_cond1[:, 0, 1]) / d qz[cond1_inds] = (R_cond1[:, 0, 2] + R_cond1[:, 2, 0]) / d cond2_mask = near_zero_mask * (Rots[:, 1, 1] > Rots[:, 2, 2]) cond2_inds = cond2_mask.nonzero() if len(cond2_inds) > 0: cond2_inds = cond2_inds.squeeze() R_cond2 = Rots[cond2_inds].view(-1, 3, 3) d = 2. * torch.sqrt(1. + R_cond2[:, 1, 1] - R_cond2[:, 0, 0] - R_cond2[:, 2, 2]).squeeze() tmp = (R_cond2[:, 0, 2] - R_cond2[:, 2, 0]) / d qw[cond2_inds] = tmp qx[cond2_inds] = (R_cond2[:, 1, 0] + R_cond2[:, 0, 1]) / d qy[cond2_inds] = 0.25 * d qz[cond2_inds] = (R_cond2[:, 2, 1] + R_cond2[:, 1, 2]) / d cond3_mask = near_zero_mask & cond1_mask.logical_not() & cond2_mask.logical_not() cond3_inds = cond3_mask if len(cond3_inds) > 0: R_cond3 = Rots[cond3_inds].view(-1, 3, 3) d = 2. * \ torch.sqrt(1. + R_cond3[:, 2, 2] - R_cond3[:, 0, 0] - R_cond3[:, 1, 1]).squeeze() qw[cond3_inds] = (R_cond3[:, 1, 0] - R_cond3[:, 0, 1]) / d qx[cond3_inds] = (R_cond3[:, 0, 2] + R_cond3[:, 2, 0]) / d qy[cond3_inds] = (R_cond3[:, 2, 1] + R_cond3[:, 1, 2]) / d qz[cond3_inds] = 0.25 * d far_zero_mask = near_zero_mask.logical_not() far_zero_inds = far_zero_mask if len(far_zero_inds) > 0: R_fz = Rots[far_zero_inds] d = 4. * qw[far_zero_inds] qx[far_zero_inds] = (R_fz[:, 2, 1] - R_fz[:, 1, 2]) / d qy[far_zero_inds] = (R_fz[:, 0, 2] - R_fz[:, 2, 0]) / d qz[far_zero_inds] = (R_fz[:, 1, 0] - R_fz[:, 0, 1]) / d # Check ordering last if ordering is 'xyzw': quat = torch.stack([qx, qy, qz, qw], dim=1) elif ordering is 'wxyz': quat = torch.stack([qw, qx, qy, qz], dim=1) return quat @classmethod def normalize(cls, Rots): U, _, V = torch.svd(Rots) S = cls.Id.clone().repeat(Rots.shape[0], 1, 1) S[:, 2, 2] = torch.det(U) * torch.det(V) return U.bmm(S).bmm(V.transpose(1, 2)) @classmethod def dnormalize(cls, Rots): U, _, V = torch.svd(Rots) S = cls.dId.clone().repeat(Rots.shape[0], 1, 1) S[:, 2, 2] = torch.det(U) * torch.det(V) return U.bmm(S).bmm(V.transpose(1, 2)) @classmethod def qmul(cls, q, r, ordering='wxyz'): """ Multiply quaternion(s) q with quaternion(s) r. """ terms = cls.bouter(r, q) w = terms[:, 0, 0] - terms[:, 1, 1] - terms[:, 2, 2] - terms[:, 3, 3] x = terms[:, 0, 1] + terms[:, 1, 0] - terms[:, 2, 3] + terms[:, 3, 2] y = terms[:, 0, 2] + terms[:, 1, 3] + terms[:, 2, 0] - terms[:, 3, 1] z = terms[:, 0, 3] - terms[:, 1, 2] + terms[:, 2, 1] + terms[:, 3, 0] xyz = torch.stack((x, y, z), dim=1) xyz[w < 0] *= -1 w[w < 0] *= -1 if ordering == 'wxyz': q = torch.cat((w.unsqueeze(1), xyz), dim=1) else: q = torch.cat((xyz, w.unsqueeze(1)), dim=1) return q / q.norm(dim=1, keepdim=True) @staticmethod def sinc(x): return x.sin() / x @classmethod def qexp(cls, xi, ordering='wxyz'): """ Convert exponential maps to quaternions. """ theta = xi.norm(dim=1, keepdim=True) w = (0.5*theta).cos() xyz = 0.5*cls.sinc(0.5*theta/np.pi)*xi return torch.cat((w, xyz), 1) @classmethod def qlog(cls, q, ordering='wxyz'): """ Applies the log map to quaternions. """ n = 0.5*torch.norm(q[:, 1:], p=2, dim=1, keepdim=True) n = torch.clamp(n, min=1e-8) q = q[:, 1:] * torch.acos(torch.clamp(q[:, :1], min=-1.0, max=1.0)) r = q / n return r @classmethod def qinv(cls, q, ordering='wxyz'): "Quaternion inverse" r = torch.empty_like(q) if ordering == 'wxyz': r[:, 1:4] = -q[:, 1:4] r[:, 0] = q[:, 0] else: r[:, :3] = -q[:, :3] r[:, 3] = q[:, 3] return r @classmethod def qnorm(cls, q): "Quaternion normalization" return q / q.norm(dim=1, keepdim=True) @classmethod def qinterp(cls, qs, t, t_int): idxs = np.searchsorted(t, t_int) idxs0 = idxs-1 idxs0[idxs0 < 0] = 0 idxs1 = idxs idxs1[idxs1 == t.shape[0]] = t.shape[0] - 1 q0 = qs[idxs0] q1 = qs[idxs1] tau = torch.zeros_like(t_int) dt = (t[idxs1]-t[idxs0])[idxs0 != idxs1] tau[idxs0 != idxs1] = (t_int-t[idxs0])[idxs0 != idxs1]/dt return cls.slerp(q0, q1, tau) @classmethod def slerp(cls, q0, q1, tau, DOT_THRESHOLD = 0.9995): """Spherical linear interpolation.""" dot = (q0*q1).sum(dim=1) q1[dot < 0] = -q1[dot < 0] dot[dot < 0] = -dot[dot < 0] q = torch.zeros_like(q0) tmp = q0 + tau.unsqueeze(1) * (q1 - q0) tmp = tmp[dot > DOT_THRESHOLD] q[dot > DOT_THRESHOLD] = tmp / tmp.norm(dim=1, keepdim=True) theta_0 = dot.acos() sin_theta_0 = theta_0.sin() theta = theta_0 * tau sin_theta = theta.sin() s0 = (theta.cos() - dot * sin_theta / sin_theta_0).unsqueeze(1) s1 = (sin_theta / sin_theta_0).unsqueeze(1) q[dot < DOT_THRESHOLD] = ((s0 * q0) + (s1 * q1))[dot < DOT_THRESHOLD] return q / q.norm(dim=1, keepdim=True) @staticmethod def bouter(vec1, vec2): """batch outer product""" return torch.einsum('bi, bj -> bij', vec1, vec2) @staticmethod def btrace(mat): """batch matrix trace""" return torch.einsum('bii -> b', mat) class CPUSO3: # tolerance criterion TOL = 1e-8 Id = torch.eye(3) @classmethod def qmul(cls, q, r): """ Multiply quaternion(s) q with quaternion(s) r. """ # Compute outer product terms = cls.outer(r, q) w = terms[0, 0] - terms[1, 1] - terms[2, 2] - terms[3, 3] x = terms[0, 1] + terms[1, 0] - terms[2, 3] + terms[3, 2] y = terms[0, 2] + terms[1, 3] + terms[2, 0] - terms[3, 1] z = terms[0, 3] - terms[1, 2] + terms[2, 1] + terms[3, 0] return torch.stack((w, x, y, z)) @staticmethod def outer(a, b): return torch.einsum('i, j -> ij', a, b)
py
1a4b7ef14f6a6c90db756ae6275cd3139b431c62
""" 自动取消(订单,拼团)异步任务 """ import datetime import os # import sentry_sdk from django.utils.timezone import make_aware # from sentry_sdk.integrations.celery import CeleryIntegration from django_redis import get_redis_connection from config.services import get_receipt_by_shop_id, get_msg_notify_by_shop_id from groupon.constant import GrouponStatus, GrouponAttendStatus from order.constant import OrderPayType, OrderRefundType, OrderType from groupon.services import ( get_shop_groupon_by_id, get_shop_groupon_attend_by_id, list_paid_details_by_groupon_attend_id ) from order.selectors import ( list_waitting_order_by_groupon_attend_id, list_unpaid_order_by_groupon_attend_id, ) from order.services import ( cancel_order, # direct_pay, refund_order, get_order_by_shop_id_and_id, direct_pay) from promotion.events import GrouponEvent from promotion.services import publish_product_promotion, get_product_promotion, PRODUCT_PROMOTION_KEY # from .celery_tplmsg_task import ( # GrouponOrderFailAttendTplMsg, # GrouponOrderRefundFailTplMsg, # GrouponOrderSuccessAttendTplMsg, # ) from celery_tasks.main import app # sentry_sdk.init(SENTRY_DSN, integrations=[CeleryIntegration()]) @app.task(bind=True, name="auto_cancel_order") def auto_cancel_order(self, shop_id, order_id): """ 超时未支付(15min)自动取消订单 """ success, _ = cancel_order(shop_id, order_id) if success: return order = get_order_by_shop_id_and_id(shop_id, order_id) if not order: return elif order.order_type == OrderType.GROUPON: auto_validate_groupon_attend.apply_async( args=[order.shop_id, order.groupon_attend_id] ) @app.task(bind=True, name="auto_publish_groupon") def auto_publish_groupon(self, shop_id, groupon_id): """ 自动发布拼团事件 """ now = make_aware(datetime.datetime.now()) success, groupon = get_shop_groupon_by_id(shop_id, groupon_id) if not success: print("Groupon [id={}] publish failed: {}".format(groupon_id, groupon)) return if groupon.status != GrouponStatus.ON: print( "Groupon [id={}] publish failed: 状态错误{}".format( groupon_id, groupon.status ) ) return if groupon.to_datetime < now: print( "Groupon [id={}] publish failed: 已过期{}".format( groupon_id, groupon.to_datetime ) ) return content = { "id": groupon.id, "price": round(float(groupon.price), 2), "to_datetime": groupon.to_datetime.strftime("%Y-%m-%d %H:%M:%S"), "groupon_type": groupon.groupon_type, "success_size": groupon.success_size, "quantity_limit": groupon.quantity_limit, "succeeded_count": groupon.succeeded_count, "success_limit": groupon.success_limit, "succeeded_quantity": int(round(groupon.succeeded_quantity)), } event = GrouponEvent(content) ttl = (groupon.to_datetime - now).total_seconds() publish_product_promotion( groupon.shop_id, groupon.product_id, event, ttl=int(ttl) ) print("Groupon [id={}] publish success".format(groupon.id)) @app.task(bind=True, name="auto_expire_groupon") def auto_expire_groupon(self, shop_id, groupon_id): """ 拼团自动过期 """ success, groupon = get_shop_groupon_by_id(shop_id, groupon_id) if not success: print("Groupon [id={}] expire failed: {}".format(groupon_id, groupon)) return if groupon.status == GrouponStatus.EXPIRED: print( "Groupon [id={}] expire failed: 状态错误{}".format( groupon_id, groupon.status ) ) return # 任务提前10s过期算作提前 if groupon.to_datetime - make_aware(datetime.datetime.now()) > datetime.timedelta( seconds=10 ): print( "Groupon [id={}] expire failed: 未到过期时间{}".format( groupon_id, make_aware(datetime.datetime.now()) ) ) return groupon.set_expired() groupon.save() print("Groupon [id={}] expire failed".format(groupon_id)) @app.task(bind=True, name="auto_validate_groupon_attend") def auto_validate_groupon_attend( self, shop_id: int, groupon_attend_id: int, force: bool = False ): """ 自动验证拼团参与,如果满员,走订单直接支付 """ success, groupon_attend = get_shop_groupon_attend_by_id( shop_id, groupon_attend_id, for_update=True ) if not success: raise ValueError(groupon_attend) if groupon_attend.size < groupon_attend.success_size: print("拼团验证: 拼团参与{}还未满员".format(groupon_attend_id)) return if groupon_attend.status != GrouponAttendStatus.WAITTING: raise ValueError( "拼团验证: 拼团参与{}状态错误{}".format(groupon_attend_id, groupon_attend.status) ) paid_attend_details = list_paid_details_by_groupon_attend_id(groupon_attend.id) if len(paid_attend_details) < groupon_attend.size and not force: print( "拼团验证: 拼团参与{}还在等待团员支付,当前支付人数{}".format( groupon_attend_id, len(paid_attend_details) ) ) return waitting_orders = list_waitting_order_by_groupon_attend_id(groupon_attend.id) if len(waitting_orders) != len(paid_attend_details): raise ValueError( "拼团验证: 拼团参与{}付款人数{}和订单人数{}不匹配".format( groupon_attend_id, len(paid_attend_details), len(waitting_orders) ) ) promotion = get_product_promotion(shop_id, groupon_attend.groupon.product_id) pattern = PRODUCT_PROMOTION_KEY.format( shop_id=shop_id, product_id=groupon_attend.groupon.product_id ) groupon_attend.set_succeeded() groupon_attend.groupon.succeeded_count += 1 redis_conn = get_redis_connection("subscribe") redis_conn.hset(pattern, "succeeded_count", groupon_attend.groupon.succeeded_count) for waitting_order in waitting_orders: if promotion and isinstance(promotion, GrouponEvent): quantity = int( round(float(waitting_order.amount_net) / float(promotion.price)) ) groupon_attend.groupon.succeeded_quantity += quantity redis_conn.hset( pattern, "succeeded_quantity", int(groupon_attend.groupon.succeeded_quantity), ) direct_pay(waitting_order) print("拼团验证: 拼团参与{}成团成功".format(groupon_attend_id)) groupon_attend.save() # 拼团成功, 发送拼团成功的模板消息 msg_notify = get_msg_notify_by_shop_id(shop_id) # if msg_notify.group_success_wx: # for waitting_order in waitting_orders: # GrouponOrderSuccessAttendTplMsg.send(order_id=waitting_order.id) @app.task(bind=True, name="auto_fail_groupon_attend") def auto_fail_groupon_attend(self, shop_id: int, groupon_attend_id: int, reason: str): """ 拼团参与自动失败 """ success, groupon_attend = get_shop_groupon_attend_by_id( shop_id, groupon_attend_id, for_update=True ) if not success: raise ValueError(groupon_attend) if groupon_attend.status != GrouponAttendStatus.WAITTING: print("拼团失败: 拼团参与{}状态错误{}".format(groupon_attend_id, groupon_attend.status)) return paid_attend_details = list_paid_details_by_groupon_attend_id( groupon_attend.id ) waitting_orders = list_waitting_order_by_groupon_attend_id( groupon_attend.id ) if len(waitting_orders) != len(paid_attend_details): raise ValueError( "拼团失败: 拼团参与{}付款人数{}和订单人数{}不匹配".format( groupon_attend_id, len(paid_attend_details), len(waitting_orders) ) ) groupon_attend.set_failed(reason) groupon_attend.save() # 拼团中订单自动退款 map_refund_order = {True: [], False: []} for waitting_order in waitting_orders: refund_type = ( OrderRefundType.WEIXIN_JSAPI_REFUND if waitting_order.pay_type == OrderPayType.WEIXIN_JSAPI else OrderRefundType.UNDERLINE_REFUND ) success, _ = refund_order( waitting_order.shop.id, waitting_order, refund_type ) map_refund_order[success].append(waitting_order.id) # 未支付订单自动取消 unpaid_orders = list_unpaid_order_by_groupon_attend_id( groupon_attend.id ) for unpaid_order in unpaid_orders: cancel_order(unpaid_order.shop_id, unpaid_order.id) print( "拼团失败: 拼团参与{},退款成功{},退款失败".format( groupon_attend_id, len(map_refund_order.get(True)), len(map_refund_order.get(False)), ) ) # 拼团失败, 发送拼团失败退款的模板消息 msg_notify = get_msg_notify_by_shop_id(shop_id) # if msg_notify.group_failed_wx: # for order_id in map_refund_order.get(True): # GrouponOrderFailAttendTplMsg.send(order_id=order_id) # for order_id in map_refund_order.get(False): # GrouponOrderRefundFailTplMsg.send(order_id=order_id)
py
1a4b7fd358bbd6e9463815ef2f68a395ab87a838
# -*- coding: utf-8 -*- """Example for gam.AdditiveModel and PolynomialSmoother This example was written as a test case. The data generating process is chosen so the parameters are well identified and estimated. Created on Fri Nov 04 13:45:43 2011 Author: Josef Perktold """ from statsmodels.compat.python import lrange import numpy as np from statsmodels.sandbox.gam import AdditiveModel from statsmodels.regression.linear_model import OLS np.random.seed(8765993) #seed is chosen for nice result, not randomly #other seeds are pretty off in the prediction #DGP: simple polynomial order = 3 sigma_noise = 0.5 nobs = 1000 #1000 #with 1000, OLS and Additivemodel aggree in params at 2 decimals lb, ub = -3.5, 4#2.5 x1 = np.linspace(lb, ub, nobs) x2 = np.sin(2*x1) x = np.column_stack((x1/x1.max()*2, x2)) exog = (x[:,:,None]**np.arange(order+1)[None, None, :]).reshape(nobs, -1) idx = lrange((order+1)*2) del idx[order+1] exog_reduced = exog[:,idx] #remove duplicate constant y_true = exog.sum(1) / 2. z = y_true #alias check d = x y = y_true + sigma_noise * np.random.randn(nobs) example = 1 if example == 1: m = AdditiveModel(d) m.fit(y) y_pred = m.results.predict(d) for ss in m.smoothers: print(ss.params) res_ols = OLS(y, exog_reduced).fit() print(res_ols.params) #from numpy.testing import assert_almost_equal #assert_almost_equal(y_pred, res_ols.fittedvalues, 3) if example > 0: import matplotlib.pyplot as plt plt.figure() plt.plot(exog) y_pred = m.results.mu# + m.results.alpha #m.results.predict(d) plt.figure() plt.subplot(2,2,1) plt.plot(y, '.', alpha=0.25) plt.plot(y_true, 'k-', label='true') plt.plot(res_ols.fittedvalues, 'g-', label='OLS', lw=2, alpha=-.7) plt.plot(y_pred, 'r-', label='AM') plt.legend(loc='upper left') plt.title('gam.AdditiveModel') counter = 2 for ii, xx in zip(['z', 'x1', 'x2'], [z, x[:,0], x[:,1]]): sortidx = np.argsort(xx) #plt.figure() plt.subplot(2, 2, counter) plt.plot(xx[sortidx], y[sortidx], '.', alpha=0.25) plt.plot(xx[sortidx], y_true[sortidx], 'k.', label='true', lw=2) plt.plot(xx[sortidx], y_pred[sortidx], 'r.', label='AM') plt.legend(loc='upper left') plt.title('gam.AdditiveModel ' + ii) counter += 1 plt.show()
py
1a4b7ffc4c07beb0d1511140c8d4e587ec0e848e
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import numpy as np import torch from . import BaseWrapperDataset class PrependTokenDataset(BaseWrapperDataset): def __init__(self, dataset, token=None, pad_token=None): super().__init__(dataset) self.token = token self.pad_token = pad_token if token is not None: self._sizes = np.array(dataset.sizes) + 1 else: self._sizes = dataset.sizes def __getitem__(self, idx): item = self.dataset[idx] if self.token is not None: if self.pad_token is not None: # char ngram data item_len = item.size(1) item = torch.cat([item.new([[self.token] + [self.pad_token]*(item_len-1)]), item]) else: item = torch.cat([item.new([self.token]), item]) return item @property def sizes(self): return self._sizes def num_tokens(self, index): n = self.dataset.num_tokens(index) if self.token is not None: n += 1 return n def size(self, index): n = self.dataset.size(index) if self.token is not None: n += 1 return n
py
1a4b8007488a48db561261c149c46003af44cc19
import json import os from maidfiddler.util.util import BASE_DIR from maidfiddler.util.config import CONFIG from maidfiddler.util.logger import logger import random current_translation = {} def tr(obj): return tr_str(obj.whatsThis()) def tr_str(original): if "translation" not in current_translation: return original parts = original.split(".") cur = current_translation["translation"] for arg in parts: if arg not in cur: return get_original(original, parts) cur = cur[arg] return cur MINIFY = None def get_original(s, parts): global MINIFY if MINIFY is None: logger.debug("Fetching MINIFY") MINIFY = CONFIG.getboolean("Developer", "minify-untranslated-tags", fallback=True) return s if not MINIFY else parts[-1] def load_translation(name): global current_translation path = os.path.join(BASE_DIR, "translations", name) logger.debug(f"TL path: {path}") if not os.path.isfile(path): return with open(path, "r", encoding="utf-8-sig") as tl_file: current_translation = json.load(tl_file) if "translation" not in current_translation: logger.warning("translation invalid") current_translation = {} return def get_random_title(): if "titles" not in current_translation or len(current_translation["titles"]) == 0: return None return random.choice(current_translation["titles"]) def get_language_name(path): if not os.path.isfile(path): return None try: with open(path, "r", encoding="utf-8-sig") as tl_file: tl = json.load(tl_file) return tl["info"]["language"] except: return None
py
1a4b80b6bca00741507081ebe496cb7552af369a
""" A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a non-empty string containing only digits, determine the total number of ways to decode it. The answer is guaranteed to fit in a 32-bit integer. Example 1: Input: s = "12" Output: 2 Explanation: It could be decoded as "AB" (1 2) or "L" (12). """ class Solution91: pass
py
1a4b817355b90f698520a15696dd373701579b20
import pandas as pd import glob import csv import os import seaborn as sns import matplotlib.pyplot as plt from builtins import any class CrystalBall: def __init__(self, list_of_csvs:list, csvname_to_colnames_list:dict, csvname_to_IDs:dict, csvname_to_nonIDs:dict, all_IDs:list, all_nonIDs:list, csvname_to_one_ID:list): # get list of all files in current directory that end in .csv self.list_of_csvs = list_of_csvs # create dictionary where csvname maps to colnames self.csvname_to_colnames_list = csvname_to_colnames_list # create dictionary where csvname maps to colnames that have the substring "ID" self.csvname_to_IDs = csvname_to_IDs # create dictionary where csvname maps to colnames that do not have the substring "ID" self.csvname_to_nonIDs = csvname_to_nonIDs # create list of only unique IDs self.all_IDs = all_IDs # create list of only unique nonIDs self.all_nonIDs = all_nonIDs # create list of all column names (IDs + nonIDs) self.all_colnames = list(all_IDs.union(all_nonIDs)) # create dictionary that maps out relationship, one csvname to one ID self.csvname_to_one_ID = csvname_to_one_ID @classmethod def run(self, rel_dir): """ Initialize the Crystal Ball object for a given directory that contains the CSVs. Parameters ---------- rel_dir : str - A string that contains the relative directory, which contains the CSVs to analyze. Returns -------- CrystalBall - CrystalBall that has all class variables initialized by this run script. Examples -------- .. code-block:: python relative_directory = './folder1/folder2' crystalBall = CrystalBall.run(relative_directory) """ rel_dir = rel_dir + '/*.csv' list_of_csvs = sorted(glob.glob(rel_dir)) csvname_to_colnames_list = {} csvname_to_IDs = {} csvname_to_nonIDs = {} all_IDs = set() all_nonIDs = set() csvname_to_one_ID = [] for csv_name in list_of_csvs: with open(csv_name, "rt") as f: reader = csv.reader(f) try: col_names = next(reader) csvname_to_colnames_list[csv_name] = col_names ids = [] non_ids = [] for col_name in col_names: if 'ID' in col_name or 'Id' in col_name: csvname_to_one_ID.append([os.path.split(csv_name)[1], col_name]) ids.append(col_name) else: non_ids.append(col_name) csvname_to_IDs[csv_name] = ids csvname_to_nonIDs[csv_name] = non_ids all_IDs.update(ids) all_nonIDs.update(non_ids) continue except StopIteration: continue except: continue return CrystalBall(list_of_csvs, csvname_to_colnames_list, csvname_to_IDs, csvname_to_nonIDs, all_IDs, all_nonIDs, csvname_to_one_ID) def contains(self, keywords: list, all_colnames: list=None) -> list: """ Check if keywords exist in all_colnames. - Determine whether a keyword (substring) exists in a given list of column names (strings). - Note: This search is case sensitive! Parameters ---------- keywords : list[str] - List of key words that the user is interested in all_colnames : list[str] - List of column names of a table, or for many tables. - If no argument is provided, this function will use the column names generated when the run method was called. Returns ------- list - Each index corresponds to a keyword. - For each index, True if substring exists in list of strings, otherwise False. Examples -------- >>> colnames = ['id', 'name', 'title'] >>> cb.contains(['name'], colnames) [True] >>> cb.contains(['Name'], colnames) [False] >>> cb.contains(['name', 'Name'], colnames) [True, False] """ if all_colnames is None: return [any(keyword in colname for colname in self.all_colnames) for keyword in keywords] else: return [any(keyword in colname for colname in all_colnames) for keyword in keywords] def featureSearch(self, keywords: list, all_colnames: list=None, mode: str='UNION') -> list: """ Find the columns that contain the keywords. - Find features (column names) that contain the substrings specified in keywords. - Note: This search is case sensitive! Parameters ---------- keywords : list[str] - List of key words that the user is interested in colnames : list[str] - List of column names of a table, or for many tables. - If no argument is provided, this function will use the column names generated when the run method was called. Returns -------- DataFrame - DataFrame will contain all features (column names) that contains one/all substrings found in keywords. - DataFrame will be sorted in alphabetical order. Examples (update example, outputs a DataFrame instead of a list) -------- >>> colnames = ['id', 'name', 'nameType', 'subSpeciesName', 'title'] >>> cb.featureSearch(['name'], colnames) ['name', 'nameType'] >>> cb.featureSearch(['Name'], colnames) ['subSpeciesName'] >>> cb.featureSearch(['name', 'Name'], colnames) ['name', 'nameType', 'subSpeciesName'] """ ##implement INTERSECTION mode later def search(keywords, colnames): suggested_colnames = set() for colname in colnames: for keyword in keywords: if keyword in colname: suggested_colnames.add(colname) return pd.DataFrame( {'featureName': sorted(list(suggested_colnames))}) if type(keywords) is not list: raise Exception('keywords argument expects a list') if mode is 'UNION': if all_colnames is None: return search(keywords, self.all_colnames) else: return search(keywords, all_colnames) elif mode is "INTERSECTION": print('to implement later') def tableSearch(self, keywords, csvname_to_colnames_list=None, mode='UNION'): """ Find the tables that contain the keywords. - Find tables that contain column names which have the substrings specified in keywords. - Note: This search is case sensitive! Parameters ---------- keywords : list[str] - List of key words that the user is interested in csvname_to_colnames_list : dict[str] = list - Dictionary that maps a string (table name) to a list of column names it contains. - If no argument is provided, this function will use the dictionary generated when the run method was called. mode : str - If mode is UNION, then return all tables that contain at least one keyword. - If mode is INTERSECTION, then return all tables that contain all the keywords. Returns -------- list[str] - List will contain all tables that contain a match with keywords. - List will be sorted in alphabetical order. Examples -------- >>> csvname_to_colnames_list = {'table1': ['colName1', 'colName2'], 'table2':['colName3', 'colName4']} >>> cb.tableSearch(['colName1'], csvname_to_colnames_list) ['table1'] >>> cb.tableSearch(['colName3'], csvname_to_colnames_list) ['table2'] >>> cb.tableSearch(['colName1', 'colName2'], csvname_to_colnames_list) ['table1', 'table2'] """ def columnNamesContainKeyword(keyword, colname_list): return any(keyword in colname for colname in colname_list) if mode is 'UNION': if csvname_to_colnames_list is None: return list(filter(lambda x: x is not None, [key if False not in [True if any(keyword in colname for colname in self.csvname_to_colnames_list[key]) else False for keyword in keywords] else None for key in self.csvname_to_colnames_list])) else: return list(filter(lambda x: x is not None, [key if False not in [True if any(keyword in colname for colname in csvname_to_colnames_list[key]) else False for keyword in keywords] else None for key in csvname_to_colnames_list])) elif mode is 'INTERSECTION': csv_matches = [] if csvname_to_colnames_list is None: for csvname in self.csvname_to_colnames_list: keyword_checklist = [] for keyword in keywords: keyword_checklist.append(columnNamesContainKeyword(keyword, self.csvname_to_colnames_list[csvname])) if False not in keyword_checklist: csv_matches.append(csvname) return sorted(csv_matches) else: print("implement later") def openTable(self, rel_dir, indices=None, encoding='utf-8'): """ Open the csv that is referenced by the given relative directory. Parameters ---------- rel_dir : str - A path to the table that is relative to where the user is running Crystal Ball. indices : list[int] - Sets the (multi)index by columns represented by their numerical integer-locations. Returns -------- DataFrame - The DataFrame containing the contents of the csv. Examples -------- (link juptyer notebook) """ df = pd.read_csv(rel_dir, engine='python', encoding=encoding , error_bad_lines=False) if indices is not None: df.set_index(list(df.columns[indices]), inplace=True) return df def subTable(self, supertable, chosen_index:list, chosen_columns:list): """ Create a subtable from a supertable. Parameters ---------- supertable : DataFrame - Table from which to select chosen_columns from in order to form a subtable chosen_index : list[str] - The column names that will form the new (multi)index for the subtable. chosen_columns : list[str] - The column names that will form the new columns for the subtable. Returns -------- DataFrame - DataFrame (the newly-formed subtable) that will have the (multi)index and columns specified in the arguments. Examples -------- (link juptyer notebook) """ ## chosen_columns should default to empty list # if len(chosen_columns) == 0: # use all the columns from supertable combined = chosen_index.copy() combined.extend(chosen_columns) subtable = supertable[combined].set_index(chosen_index) return subtable def mergeTables(self, tables:list): """ Sequentially merge a list of tables that all share a common index. - Merge defaults to using inner joins over the index. Parameters ---------- tables : list[DataFrame] - Contains a list of DataFrames that will be merged sequentially. Returns -------- DataFrame - Table that results from sequentially merging the DataFrames given in the argument. Examples -------- (link juptyer notebook) """ # replace sequential mergeing with concat... # TO IMPLEMENT LATER: other types of joins, merging by non-index def chooseLargestString(string_list): largest_string = string_list[0] for string in string_list: if len(string) > len(largest_string): largest_string = string return largest_string if len(tables) < 2: raise Exception("need at least two tables in order to merge") num_of_dropped_rows = 0 max_num_of_rows = max(len(tables[0]), len(tables[1])) current_merge = tables[0].merge(tables[1], how='inner', left_index=True, right_index=True) diff = max_num_of_rows - len(current_merge) max_num_of_rows = len(current_merge) num_of_dropped_rows += diff index_names = [tables[0].index.name, tables[1].index.name] if len(tables) - 2 > 0: for i in range(2, len(tables)): current_merge = current_merge.merge(table[i], how='inner', left_index=True, right_index=True) diff = max_num_of_rows - len(current_merge) max_num_of_rows = len(current_merge) num_of_dropped_rows += diff index_names.append(tables[i].index.name) print('Number of Dropped Rows: ',num_of_dropped_rows) current_merge.index.name = chooseLargestString(index_names) # CHECK FOR MULTI INDEX CASE, WHETHER THE ABOVE LINE BREAKS return current_merge def analyzeRelationships(self, to_analyze:list, visualize=True): """ Analyze basic stats of one or more different indexes. By comparing boxplots, you should be able to determine which indices are related. Parameters ---------- to_analyze : list[list[str, Series]] - A list of lists. The later should be of length two, in which the 0th index stores the table name and the 1st index contains a Series. - The Series should contain the values of the column derived from the table associated with the name stored in the 0th index. Returns -------- DataFrame - Table that contains basic stats about each given Series. Examples -------- (link juptyer notebook) """ descriptions = [] boxplot_data = [] boxplot_xtick_labels = [] for pair in to_analyze: new_name = pair[1].name + ' from ' + pair[0] descriptions.append(pair[1].describe().rename(new_name)) boxplot_data.append(pair[1]) boxplot_xtick_labels.append(new_name) # add labels to the quartile ranges for exact measurment. if visualize: g = sns.boxplot(data=boxplot_data) g.set( title='Relationship Analysis', xlabel='Features', ylabel='Numerical Values', xticklabels=boxplot_xtick_labels ) plt.xticks(rotation=-10) description_table = pd.concat(descriptions, axis=1) return description_table def compareRelationship(self, to_analyze1, to_analyze2, visualize=False): """ Compare and contrast the difference between two Series. By comparing boxplots, you should be able to determine which indices are related. Parameters ---------- to_analyze1 : list[str, Series] - A list that contains the name of the first table, and the contents of a specifc column from that table as a Series. to_analyze2 : list[str, Series] - A list that contains the name of the second table, and the contents of a specifc column from that table as a Series. Returns -------- DataFrame - Table that contains basic stats about each given Series, as well as a third column that contains the difference between the stats. Examples -------- (link juptyer notebook) """ descriptions = [] boxplot_data = [] boxplot_xtick_labels = [] new_name = to_analyze1[1].name + ' from ' + to_analyze1[0] description1 = to_analyze1[1].describe().rename(new_name) descriptions.append(description1) boxplot_data.append(to_analyze1[1]) boxplot_xtick_labels.append(new_name) new_name = to_analyze2[1].name + ' from ' + to_analyze2[0] description2 = to_analyze2[1].describe().rename(new_name) descriptions.append(description2) boxplot_data.append(to_analyze2[1]) boxplot_xtick_labels.append(new_name) if visualize: g = sns.boxplot(data=boxplot_data) g.set( title='Relationship Analysis', xlabel='Features', ylabel='Numerical Values', xticklabels=boxplot_xtick_labels ) plt.xticks(rotation=-10) diff_description = abs(description1 - description2) diff_description.name = "Difference" descriptions.append(diff_description) description_table = pd.concat(descriptions, axis=1) return description_table def export(self, df_to_export, write_to, export_type="CSV"): """ Exports contents of dataframe to relative location specified by write_to parameter. - Default export type is CSV Parameters ---------- df_to_export : DataFrame - DataFrame whose contents will be exported into a specifed location. write_to : str - Relative location (including file) that you will write into. export_type : str - Format that contents of df_to_export will be exported as. Returns -------- None Examples -------- (link juptyer notebook) """ if export_type is "CSV": df_to_export.to_csv(write_to, encoding='utf-8', index=True, index_label=df_to_export.index.name) else: print('implemnt sql format') # to implement later # featureSearch should return a dictionary, where key is the index and value is the name of the feature # this makes it easier for people to select the feature they want # search function should also have an 'is_exact' option, to make search more precise. # check if a lower case letter surrounds either side of the keyword, implies that it is an interjection # create a function that let's you index into a python list with another list. useful for selecting many names at once # from featureSearch result # format output of lists better... can actually use DataFrame for formatting # Print out "You have found the following column names/table names/etc." to make it easier for people to # understand what they are seeing.