src
stringlengths
721
1.04M
from django.conf.urls import include, url from . import views urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^populated_cities', views.populated_cities_query, name ='populated_cities'), url(r'^start_tour_cities', views.start_tour_cities, name ='start_tour_cities'), url(r'^stop_tour_cities', views.stop_tour_cities, name ='stop_tour_cities'), url(r'^premierLeague_stadiums', views.premierLeague_stadiums_query, name ='premierLeague_stadiums'), url(r'^start_tour_stadiums', views.start_tour_stadiums, name ='start_tour_stadiums'), url(r'^stop_tour_stadiums', views.stop_tour_stadiums, name ='stop_tour_stadiums'), url(r'^longest_rivers', views.longest_rivers_query, name ='longest_rivers'), url(r'^tour_experience', views.tour_experience, name ='tour_experience'), url(r'^line_track_experience', views.line_track_experience, name ='line_track_experience'), url(r'^stop_experience', views.stop_experience, name ='stop_experience'), url(r'^spanish_airports', views.spanish_airports_query, name ='spanish_airports'), url(r'^start_tour_airports', views.start_tour_airports, name ='start_tour_airports'), url(r'^stop_tour_airports', views.stop_tour_airports, name ='stop_tour_airports'), url(r'^summer_olympic_games_aux', views.olympic_games_query_aux, name ='summer_olympic_games_aux'), url(r'^summer_olympic_games', views.olympic_games_query, name ='summer_olympic_games'), url(r'^try_demo', views.try_demo, name ='try_demo'), url(r'^start_lleida_tour', views.start_lleida_tour, name ='start_lleida_tour'), url(r'^start_bayern_tour', views.start_bayern_tour, name ='start_bayern_tour'), url(r'^start_barcelona92', views.start_barcelona92, name ='start_barcelona92'), url(r'^stop_tour_demo', views.stop_tour_demo, name ='stop_tour_demo'), url(r'^clear_KML_folder', views.clear_KML_folder, name ='clear_KML_folder'), url(r'^stop_current_tour', views.stop_current_tour, name ='stop_current_tour'), url(r'^relaunch_LG', views.relaunch_LG, name ='relaunch_LG'), url(r'^clear_LG_cache', views.clear_LG_cache, name ='clear_LG_cache') ]
from django.db import models from django.contrib.auth.models import User CAMPAIGN_TYPE = ( ('S', 'Standard'), ('P', 'Mini-Site'), ) STATUS = ( ('A', 'Active'), ('I', 'Inactive'), ) PCAMPAIGN_LINK = ( ('FB', 'Facebook Page'), ('TW', 'Twitter Page'), ('NOTE', 'Send us a Note'), ('WEB', 'Visit Website'), ) THEME_LOOKUP = { 'a': 'Black', 'b': 'Blue', 'c': 'Dark Gray', 'd': 'Light Gray', 'e': 'Yellow', } PCAMPAIGN_LINK_TEXT_LOOKUP = { 'FB': 'Facebook Page', 'TW': 'Twitter Page', 'NOTE': 'Send us a Note', 'WEB': 'Visit Website', } class Campaign(models.Model): user = models.ForeignKey(User, db_index=True) name = models.CharField(max_length=80,) data = models.CharField(max_length=7089,) campaign_type = models.CharField(max_length=1, choices=CAMPAIGN_TYPE, default='S') premium_title = models.CharField(max_length=40, default='', null=True, blank=True) premium_theme = models.CharField(max_length=1, default='', null=True, blank=True) premium_header_theme = models.CharField(max_length=1, default='', null=True, blank=True) status = models.CharField(max_length=1, choices=STATUS, default='A') updated_at = models.DateTimeField(auto_now=True) created_at = models.DateTimeField(auto_now_add=True) def d_status(self): for k,v in STATUS: if k == self.status: return v return 'N/A' def d_campaign_type(self): for k,v in CAMPAIGN_TYPE: if k == self.campaign_type: return v return 'N/A' def d_premium_theme(self): if self.premium_theme in THEME_LOOKUP: return THEME_LOOKUP[self.premium_theme] else: return 'N/A' def d_premium_header_theme(self): if self.premium_header_theme in THEME_LOOKUP: return THEME_LOOKUP[self.premium_header_theme] else: return 'N/A' class Note(models.Model): campaign = models.ForeignKey(Campaign, db_index=True) ip = models.CharField(max_length=39,) email = models.EmailField(max_length=75) note = models.CharField(max_length=1024) user_agent = models.CharField(max_length=255) created_at = models.DateTimeField(auto_now_add=True) class Premium(models.Model): campaign = models.ForeignKey(Campaign, db_index=True) seq = models.PositiveSmallIntegerField(default=1) name = models.CharField(max_length=10, choices=PCAMPAIGN_LINK) val = models.CharField(max_length=255, null=True, blank=True) updated_at = models.DateTimeField(auto_now=True) created_at = models.DateTimeField(auto_now_add=True) def d_name(self): if self.name in PCAMPAIGN_LINK_TEXT_LOOKUP: return PCAMPAIGN_LINK_TEXT_LOOKUP[self.name] else: return 'N/A' class Scan(models.Model): campaign = models.ForeignKey(Campaign, db_index=True) ip = models.CharField(max_length=39,) refer = models.CharField(max_length=255) user_agent = models.CharField(max_length=255) created_at = models.DateTimeField(auto_now_add=True) class Click(models.Model): campaign = models.ForeignKey(Campaign, db_index=True) click_type = models.CharField(max_length=10, choices=PCAMPAIGN_LINK) ip = models.CharField(max_length=39,) refer = models.CharField(max_length=255) user_agent = models.CharField(max_length=255) created_at = models.DateTimeField(auto_now_add=True) def d_click_type(self): if self.click_type in PCAMPAIGN_LINK_TEXT_LOOKUP: return PCAMPAIGN_LINK_TEXT_LOOKUP[self.click_type] else: return 'N/A'
from pupa.scrape import Person, Scraper from openstates.utils import LXMLMixin from .util import get_client, get_url, backoff, SESSION_SITE_IDS HOMEPAGE_URLS = { "lower": ( "http://www.house.ga.gov/Representatives/en-US/" "member.aspx?Member={code}&Session={sid}" ), "upper": ( "http://www.senate.ga.gov/SENATORS/en-US/" "member.aspx?Member={code}&Session={sid}" ), } class GAPersonScraper(Scraper, LXMLMixin): sservice = get_client("Members").service ssource = get_url("Members") def clean_list(self, dirty_list): new_list = [] for x in dirty_list: if x is None: new_list.append(x) else: new_list.append(x.strip()) return new_list def scrape_homepage(self, url, kwargs): url = url.format(**kwargs) page = self.lxmlize(url) images = page.xpath("//img[contains(@src, 'SiteCollectionImages')]") if len(images) != 1: raise Exception return url, images[0].attrib["src"] def scrape_session(self, session, chambers): sid = SESSION_SITE_IDS[session] members = backoff(self.sservice.GetMembersBySession, sid)["MemberListing"] seen_guids = [] for member in members: guid = member["Id"] member_info = backoff(self.sservice.GetMember, guid) # If a member switches chambers during the session, they may # appear twice. Skip the duplicate record accordingly. if guid in seen_guids: self.warning( "Skipping duplicate record of {}".format( member_info["Name"]["Last"] ) ) continue else: seen_guids.append(guid) # Check to see if the member has vacated; skip if so. # A member can have multiple services for a given session, # if they switched chambers. Filter these down to just the # active service. try: (legislative_service,) = [ service for service in member_info["SessionsInService"][ "LegislativeService" ] if service["Session"]["Id"] == sid and service["DateVacated"] is None ] except ValueError: self.info( "Skipping retired member {}".format(member_info["Name"]["Last"]) ) continue nick_name, first_name, middle_name, last_name = ( member_info["Name"][x] for x in ["Nickname", "First", "Middle", "Last"] ) first_name = nick_name if nick_name else first_name if middle_name: full_name = "%s %s %s" % (first_name, middle_name, last_name) else: full_name = "%s %s" % (first_name, last_name) party = legislative_service["Party"] if party == "Democrat": party = "Democratic" elif party.strip() == "": party = "other" chamber, district = ( legislative_service["District"][x] for x in ["Type", "Number"] ) chamber = {"House": "lower", "Senate": "upper"}[chamber] url, photo = self.scrape_homepage( HOMEPAGE_URLS[chamber], {"code": guid, "sid": sid} ) legislator = Person( name=full_name, district=str(district), party=party, primary_org=chamber, image=photo, ) legislator.extras = { "family_name": last_name, "given_name": first_name, "guid": guid, } if ( member_info["Address"]["Street"] is not None and member_info["Address"]["Street"].strip() ): capitol_address_info = { k: v.strip() for k, v in dict(member_info["Address"]).items() if k in ["Street", "City", "State", "Zip"] } capitol_address = "{Street}\n{City}, {State} {Zip}".format( **capitol_address_info ) legislator.add_contact_detail( type="address", value=capitol_address, note="Capitol Address" ) else: self.warning( "Could not find full capitol address for {}".format(full_name) ) capitol_contact_info = self.clean_list( [member_info["Address"][x] for x in ["Email", "Phone", "Fax"]] ) # Sometimes email is set to a long cryptic string. # If it doesn't have a @ character, simply set it to None # examples: # 01X5dvct3G1lV6RQ7I9o926Q==&c=xT8jBs5X4S7ZX2TOajTx2W7CBprTaVlpcvUvHEv78GI= # 01X5dvct3G1lV6RQ7I9o926Q==&c=eSH9vpfdy3XJ989Gpw4MOdUa3n55NTA8ev58RPJuzA8= if capitol_contact_info[0] and "@" not in capitol_contact_info[0]: capitol_contact_info[0] = None if capitol_contact_info[0]: # Site was hacked in the past assert "quickrxdrugs@yahoo.com" not in capitol_contact_info[0] if capitol_contact_info[1]: legislator.add_contact_detail( type="voice", value=capitol_contact_info[1], note="Capitol Address" ) if capitol_contact_info[2]: legislator.add_contact_detail( type="fax", value=capitol_contact_info[2], note="Capitol Address" ) if capitol_contact_info[0]: legislator.add_contact_detail( type="email", value=capitol_contact_info[0], note="Capitol Address" ) if ( member_info["DistrictAddress"]["Street"] is not None and member_info["DistrictAddress"]["Street"].strip() ): district_address_info = { k: v.strip() for k, v in dict(member_info["DistrictAddress"]).items() if k in ["Street", "City", "State", "Zip"] } district_address = "{Street}\n{City}, {State} {Zip}".format( **district_address_info ) legislator.add_contact_detail( type="address", value=district_address, note="District Address" ) else: self.warning( "Could not find full district address for {}".format(full_name) ) district_contact_info = self.clean_list( [member_info["DistrictAddress"][x] for x in ["Email", "Phone", "Fax"]] ) # Same issue with district email. See above comment if district_contact_info[0] and "@" not in district_contact_info[0]: district_contact_info[0] = None if district_contact_info[0]: # Site was hacked in the past assert "quickrxdrugs@yahoo.com" not in district_contact_info[0] if district_contact_info[1]: legislator.add_contact_detail( type="voice", value=district_contact_info[1], note="District Address", ) if district_contact_info[2]: legislator.add_contact_detail( type="fax", value=district_contact_info[2], note="District Address" ) if district_contact_info[0]: legislator.add_contact_detail( type="email", value=district_contact_info[0], note="District Address", ) legislator.add_link(url) legislator.add_source(self.ssource) legislator.add_source( HOMEPAGE_URLS[chamber].format(**{"code": guid, "sid": sid}) ) yield legislator def scrape(self, session=None, chamber=None): if not session: session = self.latest_session() self.info("no session specified, using %s", session) chambers = [chamber] if chamber is not None else ["upper", "lower"] yield from self.scrape_session(session, chambers)
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- from typing import Any, AsyncIterable, Callable, Dict, Generic, Optional, TypeVar import warnings from azure.core.async_paging import AsyncItemPaged, AsyncList from azure.core.exceptions import ClientAuthenticationError, HttpResponseError, ResourceExistsError, ResourceNotFoundError, map_error from azure.core.pipeline import PipelineResponse from azure.core.pipeline.transport import AsyncHttpResponse, HttpRequest from azure.mgmt.core.exceptions import ARMErrorFormat from ... import models as _models T = TypeVar('T') ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]] class DeletedWebAppsOperations: """DeletedWebAppsOperations async operations. You should not instantiate this class directly. Instead, you should create a Client instance that instantiates it for you and attaches it as an attribute. :ivar models: Alias to model classes used in this operation group. :type models: ~azure.mgmt.web.v2020_06_01.models :param client: Client for service requests. :param config: Configuration of service client. :param serializer: An object model serializer. :param deserializer: An object model deserializer. """ models = _models def __init__(self, client, config, serializer, deserializer) -> None: self._client = client self._serialize = serializer self._deserialize = deserializer self._config = config def list( self, **kwargs: Any ) -> AsyncIterable["_models.DeletedWebAppCollection"]: """Get all deleted apps for a subscription. Description for Get all deleted apps for a subscription. :keyword callable cls: A custom type or function that will be passed the direct response :return: An iterator like instance of either DeletedWebAppCollection or the result of cls(response) :rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.web.v2020_06_01.models.DeletedWebAppCollection] :raises: ~azure.core.exceptions.HttpResponseError """ cls = kwargs.pop('cls', None) # type: ClsType["_models.DeletedWebAppCollection"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-06-01" accept = "application/json" def prepare_request(next_link=None): # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') if not next_link: # Construct URL url = self.list.metadata['url'] # type: ignore path_format_arguments = { 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') request = self._client.get(url, query_parameters, header_parameters) else: url = next_link query_parameters = {} # type: Dict[str, Any] request = self._client.get(url, query_parameters, header_parameters) return request async def extract_data(pipeline_response): deserialized = self._deserialize('DeletedWebAppCollection', pipeline_response) list_of_elem = deserialized.value if cls: list_of_elem = cls(list_of_elem) return deserialized.next_link or None, AsyncList(list_of_elem) async def get_next(next_link=None): request = prepare_request(next_link) pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: error = self._deserialize.failsafe_deserialize(_models.DefaultErrorResponse, response) map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) return pipeline_response return AsyncItemPaged( get_next, extract_data ) list.metadata = {'url': '/subscriptions/{subscriptionId}/providers/Microsoft.Web/deletedSites'} # type: ignore def list_by_location( self, location: str, **kwargs: Any ) -> AsyncIterable["_models.DeletedWebAppCollection"]: """Get all deleted apps for a subscription at location. Description for Get all deleted apps for a subscription at location. :param location: :type location: str :keyword callable cls: A custom type or function that will be passed the direct response :return: An iterator like instance of either DeletedWebAppCollection or the result of cls(response) :rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.web.v2020_06_01.models.DeletedWebAppCollection] :raises: ~azure.core.exceptions.HttpResponseError """ cls = kwargs.pop('cls', None) # type: ClsType["_models.DeletedWebAppCollection"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-06-01" accept = "application/json" def prepare_request(next_link=None): # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') if not next_link: # Construct URL url = self.list_by_location.metadata['url'] # type: ignore path_format_arguments = { 'location': self._serialize.url("location", location, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') request = self._client.get(url, query_parameters, header_parameters) else: url = next_link query_parameters = {} # type: Dict[str, Any] request = self._client.get(url, query_parameters, header_parameters) return request async def extract_data(pipeline_response): deserialized = self._deserialize('DeletedWebAppCollection', pipeline_response) list_of_elem = deserialized.value if cls: list_of_elem = cls(list_of_elem) return deserialized.next_link or None, AsyncList(list_of_elem) async def get_next(next_link=None): request = prepare_request(next_link) pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: error = self._deserialize.failsafe_deserialize(_models.DefaultErrorResponse, response) map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) return pipeline_response return AsyncItemPaged( get_next, extract_data ) list_by_location.metadata = {'url': '/subscriptions/{subscriptionId}/providers/Microsoft.Web/locations/{location}/deletedSites'} # type: ignore async def get_deleted_web_app_by_location( self, location: str, deleted_site_id: str, **kwargs: Any ) -> "_models.DeletedSite": """Get deleted app for a subscription at location. Description for Get deleted app for a subscription at location. :param location: :type location: str :param deleted_site_id: The numeric ID of the deleted app, e.g. 12345. :type deleted_site_id: str :keyword callable cls: A custom type or function that will be passed the direct response :return: DeletedSite, or the result of cls(response) :rtype: ~azure.mgmt.web.v2020_06_01.models.DeletedSite :raises: ~azure.core.exceptions.HttpResponseError """ cls = kwargs.pop('cls', None) # type: ClsType["_models.DeletedSite"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-06-01" accept = "application/json" # Construct URL url = self.get_deleted_web_app_by_location.metadata['url'] # type: ignore path_format_arguments = { 'location': self._serialize.url("location", location, 'str'), 'deletedSiteId': self._serialize.url("deleted_site_id", deleted_site_id, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') request = self._client.get(url, query_parameters, header_parameters) pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: map_error(status_code=response.status_code, response=response, error_map=error_map) error = self._deserialize.failsafe_deserialize(_models.DefaultErrorResponse, response) raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) deserialized = self._deserialize('DeletedSite', pipeline_response) if cls: return cls(pipeline_response, deserialized, {}) return deserialized get_deleted_web_app_by_location.metadata = {'url': '/subscriptions/{subscriptionId}/providers/Microsoft.Web/locations/{location}/deletedSites/{deletedSiteId}'} # type: ignore
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ ODB++ attribute parser """ __all__ = ["parse_attributes_from_line", "parse_attributes"] def parse_attributes_from_line(line): """ Given a pre-stripped line from a line record file, parse the attribute section. Returns a dict of numeric attribute values. Example: parse_attributes_from_line("P -30.9595 3.8107 0 P 0 8 0;0=0,2=0") => {0: 0, 2: 0} """ attribute_str = line.partition(";")[2].strip() return parse_attributes(attribute_str) if attribute_str else {} def parse_attributes(attribute_str): """ Given the attribute part from a line record file, parse the attribute section. Returns a dict of numeric attribute values. Example: parse_attributes("0=0,2=0") => {0: 0, 2: 0} """ # Split into individual key/value pairs attrs = (s.strip() for s in attribute_str.split(",")) # Split each k/v pair into individual parts part_attrs = ( attr.partition("=") if "=" in attr else (attr, None, True) for attr in attrs) # Create dict of ints return { int(attr[0]): int(attr[2]) if not isinstance(attr[2], bool) else attr[2] for attr in part_attrs }
from __future__ import print_function from show3d_balls import * import argparse import os import random import numpy as np import torch import torch.nn as nn import torch.nn.parallel import torch.backends.cudnn as cudnn import torch.optim as optim import torch.utils.data import torchvision.datasets as dset import torchvision.transforms as transforms import torchvision.utils as vutils from torch.autograd import Variable from datasets import PartDataset from pointnet import PointGen, PointGenC, PointNetAE import torch.nn.functional as F import matplotlib.pyplot as plt #showpoints(np.random.randn(2500,3), c1 = np.random.uniform(0,1,size = (2500))) parser = argparse.ArgumentParser() parser.add_argument('--model', type=str, default = '', help='model path') opt = parser.parse_args() print (opt) ae = PointNetAE(num_points = 2048) ae.load_state_dict(torch.load(opt.model)) dataset = PartDataset(root = 'shapenetcore_partanno_segmentation_benchmark_v0', class_choice = ['Chair'], classification = True, npoints = 2048) dataloader = torch.utils.data.DataLoader(dataset, batch_size=64, shuffle=True, num_workers=1) ae.cuda() i,data = enumerate(dataloader, 0).next() points, _ = data points = Variable(points) bs = points.size()[0] points = points.transpose(2,1) points = points.cuda() gen = ae(points) point_np = gen.transpose(2,1).cpu().data.numpy() #showpoints(points.transpose(2,1).cpu().data.numpy()) showpoints(point_np) #sim_noise = Variable(torch.randn(1000, 100)) #points = gen(sim_noise) #point_np = points.transpose(2,1).data.numpy() #print(point_np.shape) #np.savez('gan.npz', points = point_np)
# https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/582/week-4-january-22nd-january-28th/3614/ # A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and # going in the bottom-right direction until reaching the matrix's end. # For example, the matrix diagonal starting from mat[2][0], where mat is a 6 x 3 matrix, includes cells mat[2][0], mat[3][1], and mat[4][2]. # Given an m x n matrix mat of integers, sort each matrix diagonal in ascending order and return the resulting matrix. class Solution: def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]: if not mat or not mat[0]: return mat for d in range (1- len(mat), len(mat[0])): delta = (-1)* min(d,0) y= delta; x = d+delta diag = [] while y<len(mat) and x< len(mat[0]): diag.append(mat[y][x]) y = y+1 x = x+1 diag.sort() y= delta x = d+delta while y<len(mat) and x< len(mat[0]): mat[y][x]=diag[y-delta] y = y+1 x = x+1 return mat s = Solution() print(s.diagonalSort([[3,3,1,1],[2,2,1,2],[1,1,1,2]]))
# # known-value-test: Test against test vectors # Yawning Angel (yawning at schwanenlied dot me) # # Depends on obfsproxy for some utility stuff. # import obfsproxy.transports.obfs3_dh as obfs3_dh # # Test keypair x/X: # # The test vector specifies "... 756e" for x but this forces the UniformDH # code to return p - X as the public key, and more importantly that's what # the original material I used ends with. # x = int( """6f59 2d67 6f53 6874 746f 2068 6e6b 776f 2073 6874 2065 6167 6574 202e 6f59 2d67 6f53 6874 746f 2068 7369 7420 6568 6720 7461 2e65 5920 676f 532d 746f 6f68 6874 6920 2073 6874 2065 656b 2079 6e61 2064 7567 7261 6964 6e61 6f20 2066 6874 2065 6167 6574 202e 6150 7473 202c 7270 7365 6e65 2c74 6620 7475 7275 2c65 6120 6c6c 6120 6572 6f20 656e 6920 206e 6f59 2d67 6f53 6874 746f 2e68 4820 2065 6e6b 776f 2073 6877 7265 2065 6874 2065 6c4f 2064 6e4f 7365 6220 6f72 656b 7420 7268 756f""".replace(' ','').replace('\n',''), 16) x_str = obfs3_dh.int_to_bytes(x, 192) X = int( """76a3 d17d 5c55 b03e 865f a3e8 2679 90a7 24ba a24b 0bdd 0cc4 af93 be8d e30b e120 d553 3c91 bf63 ef92 3b02 edcb 84b7 4438 3f7d e232 cca6 eb46 d07c ad83 dcaa 317f becb c68c a13e 2c40 19e6 a365 3106 7450 04ae cc0b e1df f0a7 8733 fb0e 7d5c b7c4 97ca b77b 1331 bf34 7e5f 3a78 47aa 0bc0 f4bc 6414 6b48 407f ed7b 931d 1697 2d25 fb4d a5e6 dc07 4ce2 a58d aa8d e762 4247 cdf2 ebe4 e4df ec6d 5989 aac7 78c8 7559 d321 3d60 40d4 111c e3a2 acae 19f9 ee15 3250 9e03 7f69 b252 fdc3 0243 cbbc e9d0""".replace(' ','').replace('\n',''), 16) X_str = obfs3_dh.int_to_bytes(X, 192) # # Test keypair y/Y # y = int( """7365 6220 6f72 656b 7420 7268 756f 6867 6f20 2066 6c6f 2c64 6120 646e 7720 6568 6572 5420 6568 2079 6873 6c61 206c 7262 6165 206b 6874 6f72 6775 2068 6761 6961 2e6e 4820 2065 6e6b 776f 2073 6877 7265 2065 6854 7965 6820 7661 2065 7274 646f 6520 7261 6874 7327 6620 6569 646c 2c73 6120 646e 7720 6568 6572 5420 6568 2079 7473 6c69 206c 7274 6165 2064 6874 6d65 202c 6e61 2064 6877 2079 6f6e 6f20 656e 6320 6e61 6220 6865 6c6f 2064 6854 6d65 6120 2073 6854 7965 7420 6572 6461 0a2e""".replace(' ','').replace('\n',''), 16) y_str = obfs3_dh.int_to_bytes(y, 192) Y = int( """d04e 156e 554c 37ff d7ab a749 df66 2350 1e4f f446 6cb1 2be0 5561 7c1a 3687 2237 36d2 c3fd ce9e e0f9 b277 7435 0849 112a a5ae b1f1 2681 1c9c 2f3a 9cb1 3d2f 0c3a 7e6f a2d3 bf71 baf5 0d83 9171 534f 227e fbb2 ce42 27a3 8c25 abdc 5ba7 fc43 0111 3a2c b206 9c9b 305f aac4 b72b f21f ec71 578a 9c36 9bca c84e 1a7d cf07 54e3 42f5 bc8f e491 7441 b882 5443 5e2a baf2 97e9 3e1e 5796 8672 d45b d7d4 c8ba 1bc3 d314 889b 5bc3 d3e4 ea33 d4f2 dfdd 34e5 e5a7 2ff2 4ee4 6316 d475 7dad 0936 6a0b 66b3""".replace(' ','').replace('\n',''), 16) Y_str = obfs3_dh.int_to_bytes(Y, 192) # # Shared secret: x + Y/y + X # xYyX = int( """78af af5f 457f 1fdb 832b ebc3 9764 4a33 038b e9db a10c a2ce 4a07 6f32 7f3a 0ce3 151d 477b 869e e7ac 4677 5529 2ad8 a77d b9bd 87ff bbc3 9955 bcfb 03b1 5838 88c8 fd03 7834 ff3f 401d 463c 10f8 99aa 6378 4451 40b7 f838 6a7d 509e 7b9d b19b 677f 062a 7a1a 4e15 0960 4d7a 0839 ccd5 da61 73e1 0afd 9eab 6dda 7453 9d60 493c a37f a5c9 8cd9 640b 409c d8bb 3be2 bc51 36fd 42e7 64fc 3f3c 0ddb 8db3 d87a bcf2 e659 8d2b 101b ef7a 56f5 0ebc 658f 9df1 287d a813 5954 3e77 e4a4 cfa7 598a 4152 e4c0""".replace(' ','').replace('\n',''), 16) xYyX_str = obfs3_dh.int_to_bytes(xYyX, 192) # # Test my shit # import uniformdh as ssl_dh ssl_x = ssl_dh.UniformDH(x_str) if ssl_x.get_public() == X_str: print("X - Public key - MATCH") else: print("X - Public key - MISMATCH") ssl_y = ssl_dh.UniformDH(y_str) if ssl_y.get_public() == Y_str: print("Y - Public key - MATCH") else: print("Y - Public key - MISMATCH") ssl_x_Y = ssl_x.get_secret(ssl_y.get_public()) ssl_y_X = ssl_y.get_secret(ssl_x.get_public()) if ssl_x_Y == ssl_y_X: print(" Shared secret - CONSISTENT") else: print(" Shared secret - CONSISTENT") if ssl_x_Y == xYyX_str: print(" Shared secret - MATCH") else: print(" Shared secret - MISMATCH")
#!/usr/bin/env python # -*- coding: utf-8 -*- # # OpenBCI - framework for Brain-Computer Interfaces based on EEG signal # Project was initiated by Magdalena Michalska and Krzysztof Kulewski # as part of their MSc theses at the University of Warsaw. # Copyright (C) 2008-2009 Krzysztof Kulewski and Magdalena Michalska # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # # Author: # Mateusz Kruszyński <mateusz.kruszynski@gmail.com> from tags import tagger import time TAGGER = tagger.get_tagger() import random def run(): while True: i = raw_input() print("LEN i: "+str(len(i))) if len(i) == 0: i = "XYZ" i = i.strip() t= time.time() print("SEND TAG name"+i+" with time: "+repr(t)) TAGGER.send_tag(t, t+3.0*random.random()+1.0, i, {'a':1, 'b':2.2, 'c':'napis_jakis', 'annotation':"ANNOTACJA JAKAS"}) if __name__ == "__main__": print("Type anything to send tag!!!") run()
# Copyright (c) 2015, Ecole Polytechnique Federale de Lausanne, Blue Brain Project # All rights reserved. # # This file is part of NeuroM <https://github.com/BlueBrain/NeuroM> # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. Neither the name of the copyright holder nor the names of # its contributors may be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. '''Mathematical and geometrical functions used to compute morphometrics''' import math from itertools import combinations import numpy as np from neurom.core.dataformat import COLS def vector(p1, p2): '''compute vector between two 3D points Args: p1, p2: indexable objects with indices 0, 1, 2 corresponding to 3D cartesian coordinates. Returns: 3-vector from p1 - p2 ''' return np.subtract(p1[COLS.XYZ], p2[COLS.XYZ]) def linear_interpolate(p1, p2, fraction): '''Returns the point p satisfying: p1 + fraction * (p2 - p1)''' return np.array((p1[0] + fraction * (p2[0] - p1[0]), p1[1] + fraction * (p2[1] - p1[1]), p1[2] + fraction * (p2[2] - p1[2]))) def interpolate_radius(r1, r2, fraction): '''Calculate the radius that corresponds to a point P that lies at a fraction of the length of a cut cone P1P2 where P1, P2 are the centers of the circles that bound the shape with radii r1 and r2 respectively. Args: r1: float Radius of the first node of the segment. r2: float Radius of the second node of the segment fraction: float The fraction at which the interpolated radius is calculated. Returns: float The interpolated radius. Note: The fraction is assumed from point P1, not from point P2. ''' def f(a, b, c): ''' Returns the length of the interpolated radius calculated using similar triangles. ''' return a + c * (b - a) return f(r2, r1, 1. - fraction) if r1 > r2 else f(r1, r2, fraction) def path_fraction_id_offset(points, fraction, relative_offset=False): '''Find the segment which corresponds to the fraction of the path length along the piecewise linear curve which is constructed from the set of points. Args: points: an iterable of indexable objects with indices 0, 1, 2 correspoding to 3D cartesian coordinates fraction: path length fraction (0.0 <= fraction <= 1.0) relative_offset: return absolute or relative segment distance Returns: (segment ID, segment offset) pair. ''' if not (0. <= fraction <= 1.0): raise ValueError("Invalid fraction: %.3f" % fraction) pts = np.array(points)[:, COLS.XYZ] lengths = np.linalg.norm(np.diff(pts, axis=0), axis=1) cum_lengths = np.cumsum(lengths) offset = cum_lengths[-1] * fraction seg_id = np.argmin(cum_lengths < offset) if seg_id > 0: offset -= cum_lengths[seg_id - 1] if relative_offset: offset /= lengths[seg_id] return seg_id, offset def path_fraction_point(points, fraction): '''Computes the point which corresponds to the fraction of the path length along the piecewise linear curve which is constructed from the set of points. Args: points: an iterable of indexable objects with indices 0, 1, 2 correspoding to 3D cartesian coordinates fraction: path length fraction (0 <= fraction <= 1) Returns: The 3D coordinates of the aforementioned point ''' seg_id, offset = path_fraction_id_offset(points, fraction, relative_offset=True) return linear_interpolate(points[seg_id], points[seg_id + 1], offset) def scalar_projection(v1, v2): '''compute the scalar projection of v1 upon v2 Args: v1, v2: iterable indices 0, 1, 2 corresponding to cartesian coordinates Returns: 3-vector of the projection of point p onto the direction of v ''' return np.dot(v1, v2) / np.linalg.norm(v2) def vector_projection(v1, v2): '''compute the vector projection of v1 upon v2 Args: v1, v2: iterable indices 0, 1, 2 corresponding to cartesian coordinates Returns: 3-vector of the projection of point p onto the direction of v ''' return scalar_projection(v1, v2) * v2 / np.linalg.norm(v2) def dist_point_line(p, l1, l2): '''compute the orthogonal distance between from the line that goes through the points l1, l2 and the point p Args: p, l1, l2 : iterable point indices 0, 1, 2 corresponding to cartesian coordinates ''' cross_prod = np.cross(l2 - l1, p - l1) return np.linalg.norm(cross_prod) / np.linalg.norm(l2 - l1) def point_dist2(p1, p2): '''compute the square of the euclidian distance between two 3D points Args: p1, p2: indexable objects with indices 0, 1, 2 corresponding to 3D cartesian coordinates. Returns: The square of the euclidian distance between the points. ''' v = vector(p1, p2) return np.dot(v, v) def point_dist(p1, p2): '''compute the euclidian distance between two 3D points Args: p1, p2: indexable objects with indices 0, 1, 2 corresponding to 3D cartesian coordinates. Returns: The euclidian distance between the points. ''' return np.sqrt(point_dist2(p1, p2)) def angle_3points(p0, p1, p2): ''' compute the angle in radians between three 3D points Calculated as the angle between p1-p0 and p2-p0. Args: p0, p1, p2: indexable objects with indices 0, 1, 2 corresponding to 3D cartesian coordinates. Returns: Angle in radians between (p1-p0) and (p2-p0). 0.0 if p0==p1 or p0==p2. ''' vec1 = vector(p1, p0) vec2 = vector(p2, p0) return math.atan2(np.linalg.norm(np.cross(vec1, vec2)), np.dot(vec1, vec2)) def polygon_diameter(points): ''' Compute the maximun euclidian distance between any two points in a list of points ''' return max(point_dist(p0, p1) for (p0, p1) in combinations(points, 2)) def average_points_dist(p0, p_list): """ Computes the average distance between a list of points and a given point p0. """ return np.mean(list(point_dist(p0, p1) for p1 in p_list)) def path_distance(points): """ Compute the path distance from given set of points """ vecs = np.diff(points, axis=0)[:, :3] d2 = [np.dot(p, p) for p in vecs] return np.sum(np.sqrt(d2)) def segment_length(seg): '''Return the length of a segment. Returns: Euclidian distance between centres of points in seg ''' return point_dist(seg[0], seg[1]) def segment_length2(seg): '''Return the square of the length of a segment. Returns: Square of Euclidian distance between centres of points in seg ''' return point_dist2(seg[0], seg[1]) def segment_radius(seg): '''Return the mean radius of a segment Returns: arithmetic mean of the radii of the points in seg ''' return (seg[0][COLS.R] + seg[1][COLS.R]) / 2. def segment_x_coordinate(seg): '''Return the mean x coordinate of a segment Returns: arithmetic mean of the x coordinates of the points in seg ''' return (seg[0][COLS.X] + seg[1][COLS.X]) / 2. def segment_y_coordinate(seg): '''Return the mean y coordinate of a segment Returns: arithmetic mean of the y coordinates of the points in seg ''' return (seg[0][COLS.Y] + seg[1][COLS.Y]) / 2. def segment_z_coordinate(seg): '''Return the mean z coordinate of a segment Returns: arithmetic mean of the z coordinates of the points in seg ''' return (seg[0][COLS.Z] + seg[1][COLS.Z]) / 2. def segment_radial_dist(seg, pos): '''Return the radial distance of a tree segment to a given point The radial distance is the euclidian distance between the mid-point of the segment and the point in question. Parameters: seg: tree segment pos: origin to which distances are measured. It must have at lease 3 components. The first 3 components are (x, y, z). ''' return point_dist(pos, np.divide(np.add(seg[0], seg[1]), 2.0)) def segment_area(seg): '''Compute the surface area of a segment. Approximated as a conical frustum. Does not include the surface area of the bounding circles. ''' r0 = seg[0][COLS.R] r1 = seg[1][COLS.R] h2 = point_dist2(seg[0], seg[1]) return math.pi * (r0 + r1) * math.sqrt((r0 - r1) ** 2 + h2) def segment_volume(seg): '''Compute the volume of a segment. Approximated as a conical frustum. ''' r0 = seg[0][COLS.R] r1 = seg[1][COLS.R] h = point_dist(seg[0], seg[1]) return math.pi * h * ((r0 * r0) + (r0 * r1) + (r1 * r1)) / 3.0 def taper_rate(p0, p1): '''Compute the taper rate between points p0 and p1 Args: p0, p1: iterables with first 4 components containing (x, y, z, r) Returns: The taper rate, defined as the absolute value of the difference in the diameters of p0 and p1 divided by the euclidian distance between them. ''' return 2 * abs(p0[COLS.R] - p1[COLS.R]) / point_dist(p0, p1) def segment_taper_rate(seg): '''Compute the taper rate of a segment Returns: The taper rate, defined as the absolute value of the difference in the diameters of the segment's two points divided by the euclidian distance between them. ''' return taper_rate(seg[0], seg[1]) def pca(points): ''' Estimate the principal components of the covariance on the given point cloud Input A numpy array of points of the form ((x1,y1,z1), (x2, y2, z2)...) Ouptut Eigenvalues and respective eigenvectors ''' return np.linalg.eig(np.cov(points.transpose())) def sphere_area(r): ''' Compute the area of a sphere with radius r ''' return 4. * math.pi * r ** 2 # Useful alias for path_distance section_length = path_distance def principal_direction_extent(points): '''Calculate the extent of a set of 3D points. The extent is defined as the maximum distance between the projections on the principal directions of the covariance matrix of the points. Parameter: points : a 2D numpy array of points Returns: extents : the extents for each of the eigenvectors of the cov matrix eigs : eigenvalues of the covariance matrix eigv : respective eigenvectors of the covariance matrix ''' # center the points around 0.0 points = np.copy(points) points -= np.mean(points, axis=0) # principal components _, eigv = pca(points) extent = np.zeros(3) for i in range(eigv.shape[1]): # orthogonal projection onto the direction of the v component scalar_projs = np.sort(np.array([np.dot(p, eigv[:, i]) for p in points])) extent[i] = scalar_projs[-1] if scalar_projs[0] < 0.: extent -= scalar_projs[0] return extent
XXXXXXXXX XXXXX XXXXXX XXXXXX XXXXX XXXXXXXXXXXXXXXX XXXXX XXXXXXXXXXXXX XXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXX XXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXX XXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXX X XXXXXXXXXXXXXXX XXXXXXX XXXXX XXXXXXXXXXXXXXXXXXX XXXX XXXXXXXXXXXXXXXXXX XXXXXXXXX XXXXXXXXXXXXXX XXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXX XXXX XXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXX XXXX XXXXXX XXXXXXX XXXXXXXXXXX XX XXX XXXX XXXX XXXXXXX XXXXXX XXXX XXX XXX XXXXX XXX XXXXXXX XXXX XXX XXXXX XXX XXXX XXXXXXXXXX XX XXXXX XXXXXXXXXXX XXXXXXXXXX XXXX XXXX XXXXXXXXXXX XXXXXX XX XX XXXXXX XX XXXXXXXXX XXX XXXXXXXXXX XXX XXXX XXXXXXXXXXX XXXX XXXXXXX XXXXX XXXXXXXXXX XXXXX XXXXXXX XXXXX XXXXXXXX XXX XXXXXXXXXXXXXXX XXXXXX XXXXXXXXXX XXXXXX XXXXXXXXX XXXX XXXXXXXXXXXXXXX XXXX XXXXXXXXXXXXXXXXXXXXXXX XXXX XXXXXXXXXXXXXX XXXX XXXXXXXXXXXX XXXX XXXXXXXXXXXXXXXXXX XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXX XXXXXXXXXXXX XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXX XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXX XXXXXX XXXXXX XXXX XXXXXXXXXXXXXXXXX XXXXXXXXX XXXXX XX XXX XX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXX XXXX XXXXXXXXXXX XXXXX XXX XXX XXXXXXXXXX XXX XXXXXXXXXXXX XXXXXXXXXXXXX XXXXX XXX X XXXX XXXXX XX XX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXX XX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXX XXXXXX XXX XXXXXXXXXXXX XX XXXXXXXXXXXXXXX XX XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX XXX XXXXXXX XX XX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXX XXXXXX XXXXXXXXXXXXX XXXXXXXXXX XX XXXXXXXX XXXXX XXX XX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXX XXXXXX XXXXXX XXXXXXXXXX XXXXXXX XXXXXXX
import tempfile import pytest from django.core.management import call_command from postix.core.models import ListConstraint @pytest.yield_fixture def sample_member_file_ccc(): with tempfile.NamedTemporaryFile() as t: t.write( b"""chaos_number first_name last_name state 2 bezahlt 4 A B Verzug 5 C D bezahlt 8 E F bezahlt 11 G H ruhend 14 I J ruhend 23 K L bezahlt """ ) t.seek(0) yield t.name @pytest.yield_fixture def sample_member_file_incremental_update_ccc(): with tempfile.NamedTemporaryFile() as t: t.write( b"""chaos_number first_name last_name state 2 bezahlt 4 A B Verzug 8 E Y bezahlt 11 G H ruhend 14 I J ruhend 23 K L ruhend 42 M N bezahlt 43 O P Verzug """ ) t.seek(0) yield t.name @pytest.mark.django_db def test_member_import_ccc(sample_member_file_ccc): call_command('import_member', sample_member_file_ccc) lc = ListConstraint.objects.get(confidential=True, name='Mitglieder') assert set((e.identifier, e.name) for e in lc.entries.all()) == { ('2', ' '), ('5', 'C D'), ('8', 'E F'), ('23', 'K L'), } @pytest.mark.django_db def test_member_import_ccc_update( sample_member_file_ccc, sample_member_file_incremental_update_ccc ): call_command('import_member', sample_member_file_ccc) lc = ListConstraint.objects.get(confidential=True, name='Mitglieder') call_command('import_member', sample_member_file_incremental_update_ccc) assert set((e.identifier, e.name) for e in lc.entries.all()) == { ('2', ' '), ( '5', 'C D', ), # got removed from the file, but we don't detect that so we can apply partial lists as well ('8', 'E Y'), # name changed :) ('42', 'M N'), } @pytest.yield_fixture def sample_member_file_local(): with tempfile.NamedTemporaryFile() as t: t.write( b"""CHAOSNR;NAME 1;foo 2;bar """ ) t.seek(0) yield t.name @pytest.mark.django_db def test_member_import_local(sample_member_file_local): call_command('import_member', sample_member_file_local, prefix='BLN') lc = ListConstraint.objects.get(confidential=True, name='Mitglieder') assert set((e.identifier, e.name) for e in lc.entries.all()) == { ('BLN-1', 'foo'), ('BLN-2', 'bar'), }
# Copyright (c) 2012-2013, Mark Peek <mark@peek.org> # All rights reserved. # # See LICENSE file for full license. from aws import Action service_name = 'AWS ElastiCache' prefix = 'elasticache' AddTagsToResource = Action(prefix, 'AddTagsToResource') AuthorizeCacheSecurityGroupIngress = \ Action(prefix, 'AuthorizeCacheSecurityGroupIngress') CopySnapshot = Action(prefix, 'CopySnapshot') CreateCacheCluster = Action(prefix, 'CreateCacheCluster') CreateCacheParameterGroup = Action(prefix, 'CreateCacheParameterGroup') CreateCacheSecurityGroup = Action(prefix, 'CreateCacheSecurityGroup') CreateCacheSubnetGroup = Action(prefix, 'CreateCacheSubnetGroup') CreateReplicationGroup = Action(prefix, 'CreateReplicationGroup') CreateSnapshot = Action(prefix, 'CreateSnapshot') DeleteCacheCluster = Action(prefix, 'DeleteCacheCluster') DeleteCacheParameterGroup = Action(prefix, 'DeleteCacheParameterGroup') DeleteCacheSecurityGroup = Action(prefix, 'DeleteCacheSecurityGroup') DeleteCacheSubnetGroup = Action(prefix, 'DeleteCacheSubnetGroup') DeleteReplicationGroup = Action(prefix, 'DeleteReplicationGroup') DeleteSnapshot = Action(prefix, 'DeleteSnapshot') DescribeCacheClusters = Action(prefix, 'DescribeCacheClusters') DescribeCacheEngineVersions = \ Action(prefix, 'DescribeCacheEngineVersions') DescribeCacheParameterGroups = \ Action(prefix, 'DescribeCacheParameterGroups') DescribeCacheParameters = Action(prefix, 'DescribeCacheParameters') DescribeCacheSecurityGroups = \ Action(prefix, 'DescribeCacheSecurityGroups') DescribeCacheSubnetGroups = \ Action(prefix, 'DescribeCacheSubnetGroups') DescribeEngineDefaultParameters = \ Action(prefix, 'DescribeEngineDefaultParameters') DescribeEvents = Action(prefix, 'DescribeEvents') DescribeReplicationGroups = Action(prefix, 'DescribeReplicationGroups') DescribeReservedCacheNodes = \ Action(prefix, 'DescribeReservedCacheNodes') DescribeReservedCacheNodesOfferings = \ Action(prefix, 'DescribeReservedCacheNodesOfferings') DescribeSnapshots = Action(prefix, 'DescribeSnapshots') ListTagsForResource = Action(prefix, 'ListTagsForResource') ModifyCacheCluster = Action(prefix, 'ModifyCacheCluster') ModifyCacheParameterGroup = Action(prefix, 'ModifyCacheParameterGroup') ModifyCacheSubnetGroup = Action(prefix, 'ModifyCacheSubnetGroup') ModifyReplicationGroup = Action(prefix, 'ModifyReplicationGroup') PurchaseReservedCacheNodesOffering = \ Action(prefix, 'PurchaseReservedCacheNodesOffering') RebootCacheCluster = Action(prefix, 'RebootCacheCluster') RemoveTagsFromResource = Action(prefix, 'RemoveTagsFromResource') ResetCacheParameterGroup = Action(prefix, 'ResetCacheParameterGroup') RevokeCacheSecurityGroupIngress = \ Action(prefix, 'RevokeCacheSecurityGroupIngress')
# -*- coding: utf-8 -*- ############################################################################## # # Author: Yannick Vaucher # Copyright 2012 Camptocamp SA # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # ############################################################################## {"name": "Multicurrency revaluation", "version": "6.1", "category": "Finance", "description": """ =========================== Multicurrency revaluation =========================== The *Multicurrency revaluation* module allows you generate automatically multicurrency revaluation journal entries. You will also find here a Revaluation report Note that an extra aggregation by currency on general ledger & partner ledger (from module : *account_financial_report*) has been added in order to get more details. --------------- Main Features --------------- * A checkbox *Allow currency revaluation* on accounts. * A wizard to generate the revaluation journal entries. It adjusts account balance having *Allow currency revaluation* checked. * A wizard to print a report of revaluation. The report uses webkit report system. --------------- Configuration --------------- Due to the various legislation according the country, in the Company settings you can set the way you want to generate revaluation journal entries. Please, find below adviced account settings for 3 countries : For UK (Revaluation) ==================== (l10n_uk Chart of account) :: LOSS GAIN Revaluation account [7700] [7700] Provision B.S account [ ] [ ] Provision P&L account [ ] [ ] For CH (Provision) ================== (l10n_ch Chart of account) :: LOSS GAIN Revaluation account [ ] [ ] Provision B.S account [2331] [2331] Provision P&L account [3906] [4906] For FR ====== (l10n_fr Chart of account) :: LOSS GAIN Revaluation account [ 476] [ 477] Provision B.S account [1515] [ ] Provision P&L account [6865] [ ] """, "author": "Camptocamp", "license": 'AGPL-3', "depends": ["base", "account", "account_reversal", "base_headers_webkit"], "init_xml": ["res_company_view.xml", "res_currency_view.xml", "account_view.xml", "wizard/wizard_currency_revaluation_view.xml", "wizard/print_currency_unrealized_report_view.xml"], "update_xml": ['report/report.xml'], #"test": ["test/currency_revaluation.yml"], "demo_xml": [], 'installable': False, "active": False, # 'certificate': 'certificate', } # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
"""dqutils.dq3.charsmall - The dictionary of small size characters. """ CHARMAP = { 0x00:"", 0x01:" ", 0x02:"/", 0x03:"゜", 0x04:"゛", 0x05:":", 0x06:"Ex", 0x07:"Lv", 0x08:"―", 0x09:"*", 0x0A:"ど", 0x0B:".", 0x0C:"あ", 0x0D:"い", 0x0E:"う", 0x0F:"え", 0x10:"お", 0x11:"か", 0x12:"き", 0x13:"く", 0x14:"け", 0x15:"こ", 0x16:"さ", 0x17:"し", 0x18:"す", 0x19:"せ", 0x1A:"そ", 0x1B:"た", 0x1C:"ち", 0x1D:"つ", 0x1E:"て", 0x1F:"と", 0x20:"な", 0x21:"に", 0x22:"ぬ", 0x23:"ね", 0x24:"の", 0x25:"は", 0x26:"ひ", 0x27:"ふ", 0x28:"へ", 0x29:"ほ", 0x2A:"ま", 0x2B:"み", 0x2C:"む", 0x2D:"め", 0x2E:"も", 0x2F:"や", 0x30:"ゆ", 0x31:"よ", 0x32:"ら", 0x33:"り", 0x34:"る", 0x35:"れ", 0x36:"ろ", 0x37:"わ", 0x38:"を", 0x39:"ん", 0x3A:"ぁ", 0x3B:"ぃ", 0x3C:"ぅ", 0x3D:"ぇ", 0x3E:"ぉ", 0x3F:"っ", 0x40:"ゃ", 0x41:"ゅ", 0x42:"ょ", 0x43:"ア", 0x44:"イ", 0x45:"ウ", 0x46:"エ", 0x47:"オ", 0x48:"カ", 0x49:"キ", 0x4A:"ク", 0x4B:"ケ", 0x4C:"コ", 0x4D:"サ", 0x4E:"シ", 0x4F:"ス", 0x50:"セ", 0x51:"ソ", 0x52:"タ", 0x53:"チ", 0x54:"ツ", 0x55:"テ", 0x56:"ト", 0x57:"ナ", 0x58:"ニ", 0x59:"ヌ", 0x5A:"ネ", 0x5B:"ノ", 0x5C:"ハ", 0x5D:"ヒ", 0x5E:"フ", 0x5F:"ヘ", 0x60:"ホ", 0x61:"マ", 0x62:"ミ", 0x63:"ム", 0x64:"メ", 0x65:"モ", 0x66:"ヤ", 0x67:"ユ", 0x68:"ヨ", 0x69:"ラ", 0x6A:"リ", 0x6B:"ル", 0x6C:"レ", 0x6D:"ロ", 0x6E:"ワ", 0x6F:"ヲ", 0x70:"ン", 0x71:"ァ", 0x72:"ィ", 0x73:"ゥ", 0x74:"ェ", 0x75:"ォ", 0x76:"ッ", 0x77:"ャ", 0x78:"ュ", 0x79:"ョ", 0x7A:"E", 0x7B:" ", 0x7C:"★", 0x7D:"ー", 0x7E:"?", 0x7F:"!", 0x80:"▼", 0x81:"〓", 0x82:"→", 0x83:"。", 0x84:".", 0x85:"「", 0x86:"…", 0x87:"0", 0x88:"1", 0x89:"2", 0x8A:"3", 0x8B:"4", 0x8C:"5", 0x8D:"6", 0x8E:"7", 0x8F:"8", 0x90:"9", 0x91:"A", 0x92:"B", 0x93:"C", 0x94:"D", 0x95:"E", 0x96:"F", 0x97:"G", 0x98:"H", 0x99:"I", 0x9A:"J", 0x9B:"K", 0x9C:"L", 0x9D:"M", 0x9E:"N", 0x9F:"O", 0xA0:"P", 0xA1:"Q", 0xA2:"R", 0xA3:"S", 0xA4:"T", 0xA5:"U", 0xA6:"V", 0xA7:"W", 0xA8:"X", 0xA9:"Y", 0xAA:"Z", #0xAB:"[AB]", #0xAC:"[AC]", #0xAD:"[AD]", #0xAE:"[AE]", #0xAF:"[AF]", #0xB0:"[B0]", #0xB1:"[B1]", #0xB2:"[B2]", #0xB3:"[B3]", #0xB4:"[B4]", #0xB5:"[B5]", #0xB6:"[B6]", #0xB7:"[B7]", #0xB8:"[B8]", #0xB9:"[B9]", #0xBA:"[BA]", #0xBB:"[BB]", #0xBC:"[BC]", #0xBD:"[BD]", #0xBE:"[BE]", #0xBF:"[BF]", #0xC0:"[C0]", #0xC1:"[C1]", #0xC2:"[C2]", #0xC3:"[C3]", #0xC4:"[C4]", #0xC5:"[C5]", #0xC6:"[C6]", #0xC7:"[C7]", #0xC8:"[C8]", 0xC9:"が", 0xCA:"ぎ", 0xCB:"ぐ", 0xCC:"げ", 0xCD:"ご", 0xCE:"ざ", 0xCF:"じ", 0xD0:"ず", 0xD1:"ぜ", 0xD2:"ぞ", 0xD3:"だ", 0xD4:"ぢ", 0xD5:"づ", 0xD6:"で", 0xD7:"ど", 0xD8:"ば", 0xD9:"び", 0xDA:"ぶ", 0xDB:"べ", 0xDC:"ぼ", 0xDD:"ヴ", 0xDE:"ガ", 0xDF:"ギ", 0xE0:"グ", 0xE1:"ゲ", 0xE2:"ゴ", 0xE3:"ザ", 0xE4:"ジ", 0xE5:"ズ", 0xE6:"ゼ", 0xE7:"ゾ", 0xE8:"ダ", 0xE9:"ヂ", 0xEA:"ヅ", 0xEB:"デ", 0xEC:"ド", 0xED:"バ", 0xEE:"ビ", 0xEF:"ブ", 0xF0:"べ", 0xF1:"ボ", 0xF2:"ぱ", 0xF3:"ぴ", 0xF4:"ぷ", 0xF5:"ぺ", 0xF6:"ぽ", 0xF7:"パ", 0xF8:"ピ", 0xF9:"プ", 0xFA:"ペ", 0xFB:"ポ", 0xFC:"~", 0xFD:"+", 0xFE:"ぬ", 0xFF:"J", }
# Copyright 2010 The Go Authors. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. # This is the server part of the package dashboard. # It must be run by App Engine. from google.appengine.api import mail from google.appengine.api import memcache from google.appengine.api import taskqueue from google.appengine.api import urlfetch from google.appengine.api import users from google.appengine.ext import db from google.appengine.ext import webapp from google.appengine.ext.webapp import template from google.appengine.ext.webapp.util import run_wsgi_app import datetime import logging import os import re import sets import urllib2 # local imports from auth import auth import toutf8 import const template.register_template_library('toutf8') # Storage model for package info recorded on server. class Package(db.Model): path = db.StringProperty() web_url = db.StringProperty() # derived from path count = db.IntegerProperty() # grand total week_count = db.IntegerProperty() # rolling weekly count day_count = db.TextProperty(default='') # daily count last_install = db.DateTimeProperty() # data contributed by gobuilder info = db.StringProperty() ok = db.BooleanProperty() last_ok = db.DateTimeProperty() def get_day_count(self): counts = {} if not self.day_count: return counts for d in str(self.day_count).split('\n'): date, count = d.split(' ') counts[date] = int(count) return counts def set_day_count(self, count): days = [] for day, count in count.items(): days.append('%s %d' % (day, count)) days.sort(reverse=True) days = days[:28] self.day_count = '\n'.join(days) def inc(self): count = self.get_day_count() today = str(datetime.date.today()) count[today] = count.get(today, 0) + 1 self.set_day_count(count) self.update_week_count(count) self.count += 1 def update_week_count(self, count=None): if count is None: count = self.get_day_count() total = 0 today = datetime.date.today() for i in range(7): day = str(today - datetime.timedelta(days=i)) if day in count: total += count[day] self.week_count = total # PackageDaily kicks off the daily package maintenance cron job # and serves the associated task queue. class PackageDaily(webapp.RequestHandler): def get(self): # queue a task to update each package with a week_count > 0 keys = Package.all(keys_only=True).filter('week_count >', 0) for key in keys: taskqueue.add(url='/package/daily', params={'key': key.name()}) def post(self): # update a single package (in a task queue) def update(key): p = Package.get_by_key_name(key) if not p: return p.update_week_count() p.put() key = self.request.get('key') if not key: return db.run_in_transaction(update, key) class Project(db.Model): name = db.StringProperty(indexed=True) descr = db.StringProperty() web_url = db.StringProperty() package = db.ReferenceProperty(Package) category = db.StringProperty(indexed=True) tags = db.ListProperty(str) approved = db.BooleanProperty(indexed=True) re_bitbucket = re.compile(r'^(bitbucket\.org/[a-z0-9A-Z_.\-]+/[a-zA-Z0-9_.\-]+)(/[a-z0-9A-Z_.\-/]+)?$') re_googlecode = re.compile(r'^[a-z0-9\-]+\.googlecode\.com/(svn|hg|git)(/[a-z0-9A-Z_.\-/]+)?$') re_github = re.compile(r'^github\.com/[a-z0-9A-Z_.\-]+(/[a-z0-9A-Z_.\-]+)+$') re_launchpad = re.compile(r'^launchpad\.net/([a-z0-9A-Z_.\-]+(/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(/[a-z0-9A-Z_.\-/]+)?$') def vc_to_web(path): if re_bitbucket.match(path): m = re_bitbucket.match(path) check_url = 'http://' + m.group(1) + '/?cmd=heads' web = 'http://' + m.group(1) + '/' elif re_github.match(path): m = re_github_web.match(path) check_url = 'https://raw.github.com/' + m.group(1) + '/' + m.group(2) + '/master/' web = 'http://github.com/' + m.group(1) + '/' + m.group(2) + '/' elif re_googlecode.match(path): m = re_googlecode.match(path) check_url = 'http://'+path if not m.group(2): # append / after bare '/hg' or '/git' check_url += '/' web = 'http://code.google.com/p/' + path[:path.index('.')] elif re_launchpad.match(path): check_url = web = 'https://'+path else: return False, False return web, check_url re_bitbucket_web = re.compile(r'bitbucket\.org/([a-z0-9A-Z_.\-]+)/([a-z0-9A-Z_.\-]+)') re_googlecode_web = re.compile(r'code.google.com/p/([a-z0-9\-]+)') re_github_web = re.compile(r'github\.com/([a-z0-9A-Z_.\-]+)/([a-z0-9A-Z_.\-]+)') re_launchpad_web = re.compile(r'launchpad\.net/([a-z0-9A-Z_.\-]+(/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(/[a-z0-9A-Z_.\-/]+)?') re_striphttp = re.compile(r'https?://(www\.)?') def find_googlecode_vcs(path): # Perform http request to path/hg or path/git to check if they're # using mercurial or git. Otherwise, assume svn. for vcs in ['git', 'hg']: try: response = urlfetch.fetch('http://'+path+vcs, deadline=1) if response.status_code == 200: return vcs except: pass return 'svn' def web_to_vc(url): url = re_striphttp.sub('', url) m = re_bitbucket_web.match(url) if m: return 'bitbucket.org/'+m.group(1)+'/'+m.group(2) m = re_github_web.match(url) if m: return 'github.com/'+m.group(1)+'/'+m.group(2) m = re_googlecode_web.match(url) if m: path = m.group(1)+'.googlecode.com/' vcs = find_googlecode_vcs(path) return path + vcs m = re_launchpad_web.match(url) if m: return m.group(0) return False MaxPathLength = 100 CacheTimeout = 3600 class PackagePage(webapp.RequestHandler): def get(self): if self.request.get('fmt') == 'json': return self.json() html = memcache.get('view-package') if not html: tdata = {} q = Package.all().filter('week_count >', 0) q.order('-week_count') tdata['by_week_count'] = q.fetch(50) q = Package.all() q.order('-last_install') tdata['by_time'] = q.fetch(20) q = Package.all() q.order('-count') tdata['by_count'] = q.fetch(100) path = os.path.join(os.path.dirname(__file__), 'package.html') html = template.render(path, tdata) memcache.set('view-package', html, time=CacheTimeout) self.response.headers['Content-Type'] = 'text/html; charset=utf-8' self.response.out.write(html) def json(self): json = memcache.get('view-package-json') if not json: q = Package.all() s = '{"packages": [' sep = '' for r in q.fetch(1000): s += '%s\n\t{"path": "%s", "last_install": "%s", "count": "%s"}' % (sep, r.path, r.last_install, r.count) sep = ',' s += '\n]}\n' json = s memcache.set('view-package-json', json, time=CacheTimeout) self.response.set_status(200) self.response.headers['Content-Type'] = 'text/plain; charset=utf-8' self.response.out.write(json) def can_get_url(self, url): try: urllib2.urlopen(urllib2.Request(url)) return True except: return False def is_valid_package_path(self, path): return (re_bitbucket.match(path) or re_googlecode.match(path) or re_github.match(path) or re_launchpad.match(path)) def record_pkg(self, path): # sanity check string if not path or len(path) > MaxPathLength or not self.is_valid_package_path(path): return False # look in datastore key = 'pkg-' + path p = Package.get_by_key_name(key) if p is None: # not in datastore - verify URL before creating web, check_url = vc_to_web(path) if not web: logging.error('unrecognized path: %s', path) return False if not self.can_get_url(check_url): logging.error('cannot get %s', check_url) return False p = Package(key_name = key, path = path, count = 0, web_url = web) if auth(self.request): # builder updating package metadata p.info = self.request.get('info') p.ok = self.request.get('ok') == "true" if p.ok: p.last_ok = datetime.datetime.utcnow() else: # goinstall reporting an install p.inc() p.last_install = datetime.datetime.utcnow() # update package object p.put() return True def post(self): path = self.request.get('path') ok = db.run_in_transaction(self.record_pkg, path) if ok: self.response.set_status(200) self.response.out.write('ok') else: logging.error('invalid path in post: %s', path) self.response.set_status(500) self.response.out.write('not ok') class ProjectPage(webapp.RequestHandler): def get(self): admin = users.is_current_user_admin() if self.request.path == "/project/login": self.redirect(users.create_login_url("/project")) elif self.request.path == "/project/logout": self.redirect(users.create_logout_url("/project")) elif self.request.path == "/project/edit" and admin: self.edit() elif self.request.path == "/project/assoc" and admin: self.assoc() else: self.list() def assoc(self): projects = Project.all() for p in projects: if p.package: continue path = web_to_vc(p.web_url) if not path: continue pkg = Package.get_by_key_name("pkg-"+path) if not pkg: self.response.out.write('no: %s %s<br>' % (p.web_url, path)) continue p.package = pkg p.put() self.response.out.write('yes: %s %s<br>' % (p.web_url, path)) def post(self): if self.request.path == "/project/edit": self.edit(True) else: data = dict(map(lambda x: (x, self.request.get(x)), ["name","descr","web_url"])) if reduce(lambda x, y: x or not y, data.values(), False): data["submitMsg"] = "You must complete all the fields." self.list(data) return p = Project.get_by_key_name("proj-"+data["name"]) if p is not None: data["submitMsg"] = "A project by this name already exists." self.list(data) return p = Project(key_name="proj-"+data["name"], **data) p.put() path = os.path.join(os.path.dirname(__file__), 'project-notify.txt') mail.send_mail( sender=const.mail_from, to=const.mail_submit_to, subject=const.mail_submit_subject, body=template.render(path, {'project': p})) self.list({"submitMsg": "Your project has been submitted."}) def list(self, additional_data={}): cache_key = 'view-project-data' tag = self.request.get('tag', None) if tag: cache_key += '-'+tag data = memcache.get(cache_key) admin = users.is_current_user_admin() if admin or not data: projects = Project.all().order('category').order('name') if not admin: projects = projects.filter('approved =', True) projects = list(projects) tags = sets.Set() for p in projects: for t in p.tags: tags.add(t) if tag: projects = filter(lambda x: tag in x.tags, projects) data = {} data['tag'] = tag data['tags'] = tags data['projects'] = projects data['admin']= admin if not admin: memcache.set(cache_key, data, time=CacheTimeout) for k, v in additional_data.items(): data[k] = v self.response.headers['Content-Type'] = 'text/html; charset=utf-8' path = os.path.join(os.path.dirname(__file__), 'project.html') self.response.out.write(template.render(path, data)) def edit(self, save=False): if save: name = self.request.get("orig_name") else: name = self.request.get("name") p = Project.get_by_key_name("proj-"+name) if not p: self.response.out.write("Couldn't find that Project.") return if save: if self.request.get("do") == "Delete": p.delete() else: pkg_name = self.request.get("package", None) if pkg_name: pkg = Package.get_by_key_name("pkg-"+pkg_name) if pkg: p.package = pkg.key() for f in ['name', 'descr', 'web_url', 'category']: setattr(p, f, self.request.get(f, None)) p.approved = self.request.get("approved") == "1" p.tags = filter(lambda x: x, self.request.get("tags", "").split(",")) p.put() memcache.delete('view-project-data') self.redirect('/project') return # get all project categories and tags cats, tags = sets.Set(), sets.Set() for r in Project.all(): cats.add(r.category) for t in r.tags: tags.add(t) self.response.headers['Content-Type'] = 'text/html; charset=utf-8' path = os.path.join(os.path.dirname(__file__), 'project-edit.html') self.response.out.write(template.render(path, { "taglist": tags, "catlist": cats, "p": p, "tags": ",".join(p.tags) })) def redirect(self, url): self.response.set_status(302) self.response.headers.add_header("Location", url) def main(): app = webapp.WSGIApplication([ ('/package', PackagePage), ('/package/daily', PackageDaily), ('/project.*', ProjectPage), ], debug=True) run_wsgi_app(app) if __name__ == '__main__': main()
# GPL # Original Authors: Evan J. Rosky (syrux), Chichiri, Jace Priester # import bpy import random import math from bpy.types import ( Operator, Menu, ) from mathutils import ( Vector, Quaternion, ) # ################### Globals #################### # doprots = True # Datas in which we will build the new discombobulated mesh nPolygons = [] nVerts = [] Verts = [] Polygons = [] dVerts = [] dPolygons = [] i_prots = [] # index of the top polygons on which we'll generate the doodads i_dood_type = [] # type of doodad (given by index of the doodad obj) # ############### Utility Functions ############### # def randnum(a, b): return random.random() * (b - a) + a def randVertex(a, b, c, d, Verts): """Return a vector of a random vertex on a quad-polygon""" i = random.randint(1, 2) A, B, C, D = 0, 0, 0, 0 if(a == 1): A, B, C, D = a, b, c, d else: A, B, C, D = a, d, c, b i = randnum(0.1, 0.9) vecAB = Verts[B] - Verts[A] E = Verts[A] + vecAB * i vecDC = Verts[C] - Verts[D] F = Verts[D] + vecDC * i i = randnum(0.1, 0.9) vecEF = F - E O = E + vecEF * i return O # ################## Protusions #################### # def fill_older_datas(verts, polygon): """ Specifically coded to be called by the function addProtusionToPolygon, its sets up a tuple which contains the vertices from the base and the top of the protusions. """ temp_vertices = [] temp_vertices.append(verts[polygon[0]].copy()) temp_vertices.append(verts[polygon[1]].copy()) temp_vertices.append(verts[polygon[2]].copy()) temp_vertices.append(verts[polygon[3]].copy()) temp_vertices.append(verts[polygon[0]].copy()) temp_vertices.append(verts[polygon[1]].copy()) temp_vertices.append(verts[polygon[2]].copy()) temp_vertices.append(verts[polygon[3]].copy()) return temp_vertices def extrude_top(temp_vertices, normal, height): """ This function extrude the polygon composed of the four first members of the tuple temp_vertices along the normal multiplied by the height of the extrusion. """ j = 0 while j < 3: temp_vertices[0][j] += normal[j] * height temp_vertices[1][j] += normal[j] * height temp_vertices[2][j] += normal[j] * height temp_vertices[3][j] += normal[j] * height j += 1 def scale_top(temp_vertices, center, normal, height, scale_ratio): """ This function scale the polygon composed of the four first members of the tuple temp_vertices. """ vec1 = [0, 0, 0] vec2 = [0, 0, 0] vec3 = [0, 0, 0] vec4 = [0, 0, 0] j = 0 while j < 3: center[j] += normal[j] * height vec1[j] = temp_vertices[0][j] - center[j] vec2[j] = temp_vertices[1][j] - center[j] vec3[j] = temp_vertices[2][j] - center[j] vec4[j] = temp_vertices[3][j] - center[j] temp_vertices[0][j] = center[j] + vec1[j] * (1 - scale_ratio) temp_vertices[1][j] = center[j] + vec2[j] * (1 - scale_ratio) temp_vertices[2][j] = center[j] + vec3[j] * (1 - scale_ratio) temp_vertices[3][j] = center[j] + vec4[j] * (1 - scale_ratio) j += 1 def add_prot_polygons(temp_vertices): """ Specifically coded to be called by addProtusionToPolygon, this function put the data from the generated protusion at the end the tuples Verts and Polygons, which will later used to generate the final mesh. """ global Verts global Polygons global i_prots findex = len(Verts) Verts += temp_vertices polygontop = [findex + 0, findex + 1, findex + 2, findex + 3] polygon1 = [findex + 0, findex + 1, findex + 5, findex + 4] polygon2 = [findex + 1, findex + 2, findex + 6, findex + 5] polygon3 = [findex + 2, findex + 3, findex + 7, findex + 6] polygon4 = [findex + 3, findex + 0, findex + 4, findex + 7] Polygons.append(polygontop) i_prots.append(len(Polygons) - 1) Polygons.append(polygon1) Polygons.append(polygon2) Polygons.append(polygon3) Polygons.append(polygon4) def addProtusionToPolygon(obpolygon, verts, minHeight, maxHeight, minTaper, maxTaper): """Create a protusion from the polygon "obpolygon" of the original object and use several values sent by the user. It calls in this order the following functions: - fill_older_data; - extrude_top; - scale_top; - add_prot_polygons; """ # some useful variables polygon = obpolygon.vertices tVerts = list(fill_older_datas(verts, polygon)) # list of temp vertices height = randnum(minHeight, maxHeight) # height of generated protusion scale_ratio = randnum(minTaper, maxTaper) # extrude the top polygon extrude_top(tVerts, obpolygon.normal, height) # Now, we scale, the top polygon along its normal scale_top(tVerts, GetPolyCentroid(obpolygon, verts), obpolygon.normal, height, scale_ratio) # Finally, we add the protusions to the list of polygons add_prot_polygons(tVerts) # ################# Divide a polygon ############### # def divide_one(list_polygons, list_vertices, verts, polygon, findex): """ called by divide_polygon, to generate a polygon from one polygon, maybe I could simplify this process """ temp_vertices = [] temp_vertices.append(verts[polygon[0]].copy()) temp_vertices.append(verts[polygon[1]].copy()) temp_vertices.append(verts[polygon[2]].copy()) temp_vertices.append(verts[polygon[3]].copy()) list_vertices += temp_vertices list_polygons.append([findex + 0, findex + 1, findex + 2, findex + 3]) def divide_two(list_polygons, list_vertices, verts, polygon, findex): """ called by divide_polygon, to generate two polygons from one polygon and add them to the list of polygons and vertices which form the discombobulated mesh """ temp_vertices = [] temp_vertices.append(verts[polygon[0]].copy()) temp_vertices.append(verts[polygon[1]].copy()) temp_vertices.append(verts[polygon[2]].copy()) temp_vertices.append(verts[polygon[3]].copy()) temp_vertices.append((verts[polygon[0]] + verts[polygon[1]]) / 2) temp_vertices.append((verts[polygon[2]] + verts[polygon[3]]) / 2) list_vertices += temp_vertices list_polygons.append([findex + 0, findex + 4, findex + 5, findex + 3]) list_polygons.append([findex + 1, findex + 2, findex + 5, findex + 4]) def divide_three(list_polygons, list_vertices, verts, polygon, findex, center): """ called by divide_polygon, to generate three polygons from one polygon and add them to the list of polygons and vertices which form the discombobulated mesh """ temp_vertices = [] temp_vertices.append(verts[polygon[0]].copy()) temp_vertices.append(verts[polygon[1]].copy()) temp_vertices.append(verts[polygon[2]].copy()) temp_vertices.append(verts[polygon[3]].copy()) temp_vertices.append((verts[polygon[0]] + verts[polygon[1]]) / 2) temp_vertices.append((verts[polygon[2]] + verts[polygon[3]]) / 2) temp_vertices.append((verts[polygon[1]] + verts[polygon[2]]) / 2) temp_vertices.append(center.copy()) list_vertices += temp_vertices list_polygons.append([findex + 0, findex + 4, findex + 5, findex + 3]) list_polygons.append([findex + 1, findex + 6, findex + 7, findex + 4]) list_polygons.append([findex + 6, findex + 2, findex + 5, findex + 7]) def divide_four(list_polygons, list_vertices, verts, polygon, findex, center): """ called by divide_polygon, to generate four polygons from one polygon and add them to the list of polygons and vertices which form the discombobulated mesh """ temp_vertices = [] temp_vertices.append(verts[polygon[0]].copy()) temp_vertices.append(verts[polygon[1]].copy()) temp_vertices.append(verts[polygon[2]].copy()) temp_vertices.append(verts[polygon[3]].copy()) temp_vertices.append((verts[polygon[0]] + verts[polygon[1]]) / 2) temp_vertices.append((verts[polygon[2]] + verts[polygon[3]]) / 2) temp_vertices.append((verts[polygon[1]] + verts[polygon[2]]) / 2) temp_vertices.append(center.copy()) temp_vertices.append((verts[polygon[0]] + verts[polygon[3]]) / 2) temp_vertices.append(center.copy()) list_vertices += temp_vertices list_polygons.append([findex + 0, findex + 4, findex + 7, findex + 8]) list_polygons.append([findex + 1, findex + 6, findex + 7, findex + 4]) list_polygons.append([findex + 6, findex + 2, findex + 5, findex + 7]) list_polygons.append([findex + 8, findex + 7, findex + 5, findex + 3]) def dividepolygon(obpolygon, verts, number): """Divide the poly into the wanted number of polygons""" global nPolygons global nVerts poly = obpolygon.vertices if(number == 1): divide_one(nPolygons, nVerts, verts, poly, len(nVerts)) elif(number == 2): divide_two(nPolygons, nVerts, verts, poly, len(nVerts)) elif(number == 3): divide_three(nPolygons, nVerts, verts, poly, len(nVerts), GetPolyCentroid(obpolygon, verts)) elif(number == 4): divide_four(nPolygons, nVerts, verts, poly, len(nVerts), GetPolyCentroid(obpolygon, verts)) # ################## Discombobulate ################ # def GetPolyCentroid(obpolygon, allvertcoords): centroid = Vector((0, 0, 0)) for vindex in obpolygon.vertices: centroid += Vector(allvertcoords[vindex]) centroid /= len(obpolygon.vertices) return centroid def division(obpolygons, verts, sf1, sf2, sf3, sf4): """Function to divide each of the selected polygons""" divide = [] if (sf1): divide.append(1) if (sf2): divide.append(2) if (sf3): divide.append(3) if (sf4): divide.append(4) for poly in obpolygons: if(poly.select is True and len(poly.vertices) == 4): a = random.randint(0, len(divide) - 1) dividepolygon(poly, verts, divide[a]) def protusion(obverts, obpolygons, minHeight, maxHeight, minTaper, maxTaper): """function to generate the protusions""" verts = [] for vertex in obverts: verts.append(vertex.co) for polygon in obpolygons: if(polygon.select is True): if(len(polygon.vertices) == 4): addProtusionToPolygon(polygon, verts, minHeight, maxHeight, minTaper, maxTaper) def test_v2_near_v1(v1, v2): if (v1.x - 0.1 <= v2.x <= v1.x + 0.1 and v1.y - 0.1 <= v2.y <= v1.y + 0.1 and v1.z - 0.1 <= v2.z <= v1.z + 0.1): return True return False def angle_between_nor(nor_orig, nor_result): angle = math.acos(nor_orig.dot(nor_result)) axis = nor_orig.cross(nor_result).normalized() q = Quaternion() q.x = axis.x * math.sin(angle / 2) q.y = axis.y * math.sin(angle / 2) q.z = axis.z * math.sin(angle / 2) q.w = math.cos(angle / 2) return q def doodads(object1, mesh1, dmin, dmax): """function to generate the doodads""" global dVerts global dPolygons i = 0 # on parcoure cette boucle pour ajouter des doodads a toutes les polygons # english translation: this loops adds doodads to all polygons while(i < len(object1.data.polygons)): if object1.data.polygons[i].select is False: continue doods_nbr = random.randint(dmin, dmax) j = 0 while(j <= doods_nbr): origin_dood = randVertex(object1.data.polygons[i].vertices[0], object1.data.polygons[i].vertices[1], object1.data.polygons[i].vertices[2], object1.data.polygons[i].vertices[3], Verts) type_dood = random.randint(0, len(bpy.context.scene.discomb.DISC_doodads) - 1) polygons_add = [] verts_add = [] # First we have to apply scaling and rotation to the mesh bpy.ops.object.select_pattern(pattern=bpy.context.scene.discomb.DISC_doodads[type_dood], extend=False) bpy.context.scene.objects.active = bpy.data.objects[bpy.context.scene.discomb.DISC_doodads[type_dood]] bpy.ops.object.transform_apply(rotation=True, scale=True) for polygon in bpy.data.objects[bpy.context.scene.discomb.DISC_doodads[type_dood]].data.polygons: polygons_add.append(polygon.vertices) for vertex in bpy.data.objects[bpy.context.scene.discomb.DISC_doodads[type_dood]].data.vertices: verts_add.append(vertex.co.copy()) normal_original_polygon = object1.data.polygons[i].normal nor_def = Vector((0.0, 0.0, 1.0)) qr = nor_def.rotation_difference(normal_original_polygon.normalized()) if(test_v2_near_v1(nor_def, -normal_original_polygon)): qr = Quaternion((0.0, 0.0, 0.0, 0.0)) # qr = angle_between_nor(nor_def, normal_original_polygon) for vertex in verts_add: vertex.rotate(qr) vertex += origin_dood findex = len(dVerts) for polygon in polygons_add: dPolygons.append([polygon[0] + findex, polygon[1] + findex, polygon[2] + findex, polygon[3] + findex]) i_dood_type.append(bpy.data.objects[bpy.context.scene.discomb.DISC_doodads[type_dood]].name) for vertex in verts_add: dVerts.append(vertex) j += 1 i += 5 def protusions_repeat(object1, mesh1, r_prot): for j in i_prots: if j < len(object1.data.polygons): object1.data.polygons[j].select = True else: print("Warning: hit end of polygons in object1") # add material to discombobulated mesh def setMatProt(discObj, origObj, sideProtMat, topProtMat): # First we put the materials in their slots bpy.ops.object.select_pattern(pattern=discObj.name, extend=False) bpy.context.scene.objects.active = bpy.data.objects[discObj.name] try: origObj.material_slots[topProtMat] origObj.material_slots[sideProtMat] except: return bpy.ops.object.material_slot_add() bpy.ops.object.material_slot_add() discObj.material_slots[0].material = origObj.material_slots[topProtMat].material discObj.material_slots[1].material = origObj.material_slots[sideProtMat].material # Then we assign materials to protusions for polygon in discObj.data.polygons: if polygon.index in i_prots: polygon.material_index = 0 else: polygon.material_index = 1 def setMatDood(doodObj): # First we add the materials slots bpy.ops.object.select_pattern(pattern=doodObj.name, extend=False) bpy.context.scene.objects.active = doodObj for name in bpy.context.scene.discomb.DISC_doodads: try: bpy.ops.object.material_slot_add() doodObj.material_slots[-1].material = bpy.data.objects[name].material_slots[0].material for polygon in doodObj.data.polygons: if i_dood_type[polygon.index] == name: polygon.material_index = len(doodObj.material_slots) - 1 except: print() def clean_doodads(): current_doodads = list(bpy.context.scene.discomb.DISC_doodads) for name in current_doodads: if name not in bpy.data.objects: bpy.context.scene.discomb.DISC_doodads.remove(name) def discombobulate(minHeight, maxHeight, minTaper, maxTaper, sf1, sf2, sf3, sf4, dmin, dmax, r_prot, sideProtMat, topProtMat, isLast): global doprots global nVerts global nPolygons global Verts global Polygons global dVerts global dPolygons global i_prots bpy.ops.object.mode_set(mode="OBJECT") # start by cleaning up doodads that don't exist anymore clean_doodads() # Create the discombobulated mesh mesh = bpy.data.meshes.new("tmp") object = bpy.data.objects.new("tmp", mesh) bpy.context.scene.objects.link(object) # init final verts and polygons tuple nPolygons = [] nVerts = [] Polygons = [] Verts = [] dPolygons = [] dVerts = [] origObj = bpy.context.active_object # There we collect the rotation, translation and scaling datas from the original mesh to_translate = bpy.context.active_object.location to_scale = bpy.context.active_object.scale to_rotate = bpy.context.active_object.rotation_euler # First, we collect all the informations we will need from the previous mesh obverts = bpy.context.active_object.data.vertices obpolygons = bpy.context.active_object.data.polygons verts = [] for vertex in obverts: verts.append(vertex.co) division(obpolygons, verts, sf1, sf2, sf3, sf4) # Fill in the discombobulated mesh with the new polygons mesh.from_pydata(nVerts, [], nPolygons) mesh.update(calc_edges=True) # Reload the datas bpy.ops.object.select_all(action="DESELECT") bpy.ops.object.select_pattern(pattern=object.name, extend=False) bpy.context.scene.objects.active = bpy.data.objects[object.name] obverts = bpy.context.active_object.data.vertices obpolygons = bpy.context.active_object.data.polygons protusion(obverts, obpolygons, minHeight, maxHeight, minTaper, maxTaper) # Fill in the discombobulated mesh with the new polygons mesh1 = bpy.data.meshes.new("discombobulated_object") object1 = bpy.data.objects.new("discombobulated_mesh", mesh1) bpy.context.scene.objects.link(object1) mesh1.from_pydata(Verts, [], Polygons) mesh1.update(calc_edges=True) # Set the material's of discombobulated object setMatProt(object1, origObj, sideProtMat, topProtMat) bpy.ops.object.select_pattern(pattern=object1.name, extend=False) bpy.context.scene.objects.active = bpy.data.objects[object1.name] bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.normals_make_consistent(inside=False) bpy.ops.mesh.select_all(action='DESELECT') bpy.ops.object.mode_set(mode='OBJECT') # if(bpy.context.scene.repeatprot): protusions_repeat(object1, mesh1, r_prot) if(len(bpy.context.scene.discomb.DISC_doodads) != 0 and bpy.context.scene.discomb.dodoodads and isLast): doodads(object1, mesh1, dmin, dmax) mesh2 = bpy.data.meshes.new("dood_mesh") object2 = bpy.data.objects.new("dood_obj", mesh2) bpy.context.scene.objects.link(object2) mesh2.from_pydata(dVerts, [], dPolygons) mesh2.update(calc_edges=True) setMatDood(object2) object2.location = to_translate object2.rotation_euler = to_rotate object2.scale = to_scale bpy.ops.object.select_pattern(pattern=object.name, extend=False) bpy.context.scene.objects.active = bpy.data.objects[object.name] bpy.ops.object.delete() bpy.ops.object.select_pattern(pattern=object1.name, extend=False) bpy.context.scene.objects.active = bpy.data.objects[object1.name] bpy.context.scene.update() # translate, scale and rotate discombobulated results object1.location = to_translate object1.rotation_euler = to_rotate object1.scale = to_scale # set all polys to selected. this allows recursive discombobulating. for poly in mesh1.polygons: poly.select = True # ### Operators for selecting and deselecting an object as a doodad ### # class chooseDoodad(Operator): bl_idname = "object.discombobulate_set_doodad" bl_label = "Discombobulate set doodad object" bl_description = ("Save the Active Object as Doodad \n" "Object has to be quads only") bl_options = {'REGISTER'} @classmethod def poll(cls, context): obj = bpy.context.active_object if (obj is not None and obj.type == "MESH"): mesh = obj.data for polygon in mesh.polygons: is_ok = len(polygon.vertices) if is_ok != 4: return False return True return False def execute(self, context): obj_name = bpy.context.active_object.name msg = "Object with this name already saved" if obj_name not in bpy.context.scene.discomb.DISC_doodads: bpy.context.scene.discomb.DISC_doodads.append(obj_name) msg = "Saved Doodad object: {}".format(obj_name) self.report({'INFO'}, message=msg) def invoke(self, context, event): self.execute(context) return {'FINISHED'} class unchooseDoodad(Operator): bl_idname = "object.discombobulate_unset_doodad" bl_label = "Discombobulate unset doodad object" bl_description = "Remove the saved Doodad Object(s)" bl_options = {'REGISTER'} remove_all = bpy.props.BoolProperty( name="Remove all Doodads", default=False, ) def execute(self, context): msg = ("No doodads to remove") doodadery = bpy.context.scene.discomb.DISC_doodads if len(doodadery) > 0: if not self.remove_all: name = bpy.context.active_object.name if name in doodadery: bpy.context.scene.discomb.DISC_doodads.remove(name) msg = ("Removed Doodad object: {}".format(name)) else: bpy.context.scene.discomb.DISC_doodads[:] = [] msg = "Removed all Doodads" else: msg = "No Doodads to Remove" self.report({'INFO'}, message=msg) def invoke(self, context, event): self.execute(context) return {'FINISHED'} # ################## Interpolygon ################## # class discombobulator(Operator): bl_idname = "object.discombobulate" bl_label = "Discombobulate" bl_description = "Apply" bl_options = {'REGISTER', 'UNDO'} def execute(self, context): scn = context.scene.discomb i = 0 while i < bpy.context.scene.discomb.repeatprot: isLast = False if i == scn.repeatprot - 1: isLast = True discombobulate(scn.minHeight, scn.maxHeight, scn.minTaper, scn.maxTaper, scn.subpolygon1, scn.subpolygon2, scn.subpolygon3, scn.subpolygon4, scn.mindoodads, scn.maxdoodads, scn.repeatprot, scn.sideProtMat, scn.topProtMat, isLast) i += 1 return {'FINISHED'} class discombobulator_dodads_list(Menu): bl_idname = "object.discombobulator_dodad_list" bl_label = "List of saved Doodads" bl_description = "List of the saved Doodad Object Names" bl_options = {'REGISTER'} def draw(self, context): layout = self.layout doodle = len(bpy.context.scene.discomb.DISC_doodads) layout.label("Saved doodads : {}".format(doodle)) layout.separator() if doodle > 0: for name in bpy.context.scene.discomb.DISC_doodads: layout.label(text=name) class discombob_help(Menu): bl_idname = "help.discombobulator" bl_label = "Usage Information" bl_description = "Help" bl_options = {'REGISTER'} def draw(self, context): layout = self.layout layout.label(text="Usage Information:", icon="INFO") layout.separator() layout.label(text="Quads only, not Triangles or Ngons", icon="ERROR") layout.label("Works only with Mesh object that have faces") layout.separator() layout.label("Select a face or faces") layout.label("Press Discombobulate to create greebles") layout.label("In object mode, still needs a selection in Edit Mode") layout.separator() layout.label("Doodads - additional objects layered on the mesh surface") layout.label("(Similar to dupliverts - but as one separate object)") layout.separator() layout.label(text="Limitations:", icon="MOD_EXPLODE") layout.label("Be careful with the repeat protusions setting") layout.label("(Runs reqursively)") layout.label("If possible, avoid using on a high polycount base mesh") layout.label("(It can run out of memory and take a long time to compute)") class VIEW3D_OT_tools_discombobulate(Operator): bl_idname = "discombobulate.ops" bl_label = "Discombobulator" bl_description = ("Easily add sci-fi details to a surface \n" "Needs an existing active Mesh with Faces") bl_options = {'REGISTER'} executing = False @classmethod def poll(cls, context): return (context.active_object is not None and context.active_object.type == "MESH") def draw(self, context): layout = self.layout row = layout.row() row.menu('help.discombobulator', icon='INFO') box = layout.box() box.label("Protusions settings") row = box.row() row.prop(context.scene.discomb, 'doprots') row = box.row() row.prop(context.scene.discomb, 'minHeight') row = box.row() row.prop(context.scene.discomb, 'maxHeight') row = box.row() row.prop(context.scene.discomb, 'minTaper') row = box.row() row.prop(context.scene.discomb, 'maxTaper') row = box.row() col1 = row.column(align=True) col1.prop(context.scene.discomb, "subpolygon1") col2 = row.column(align=True) col2.prop(context.scene.discomb, "subpolygon2") col3 = row.column(align=True) col3.prop(context.scene.discomb, "subpolygon3") col4 = row.column(align=True) col4.prop(context.scene.discomb, "subpolygon4") row = box.row() row.prop(context.scene.discomb, "repeatprot") box = layout.box() box.label("Doodads settings") row = box.row() is_doodad = context.scene.discomb.dodoodads row.prop(context.scene.discomb, 'dodoodads') row = box.row() row.enabled = is_doodad row.prop(context.scene.discomb, "mindoodads") row = box.row() row.enabled = is_doodad row.prop(context.scene.discomb, "maxdoodads") row = box.row() row.enabled = is_doodad row.operator("object.discombobulate_set_doodad", text="Pick doodad") row = box.row() splits = row.split(0.5) splits.enabled = is_doodad splits.operator("object.discombobulate_unset_doodad", text="Remove active doodad").remove_all = False splits.operator("object.discombobulate_unset_doodad", text="Remove all doodads").remove_all = True col = box.column(align=True) doodle = len(bpy.context.scene.discomb.DISC_doodads) col.enabled = (True if doodle > 0 else False) col.menu("object.discombobulator_dodad_list", text="List of saved Doodads ({})".format(doodle)) box = layout.box() box.label("Materials settings") row = box.row() row.prop(context.scene.discomb, 'topProtMat') row = box.row() row.prop(context.scene.discomb, "sideProtMat") def invoke(self, context, event): return context.window_manager.invoke_props_dialog(self, width=300) def check(self, context): return not self.executing def execute(self, context): self.executing = True bpy.ops.object.discombobulate('INVOKE_DEFAULT') return {'FINISHED'}
#------------------------------------------------------------------------------ # Copyright (c) 2013, Nucleic Development Team. # # Distributed under the terms of the Modified BSD License. # # The full license is in the file COPYING.txt, distributed with this software. #------------------------------------------------------------------------------ from atom.api import Typed from enaml.widgets.stack_item import ProxyStackItem from .QtGui import QFrame from .q_single_widget_layout import QSingleWidgetLayout from .qt_container import QtContainer from .qt_widget import QtWidget class QStackItem(QFrame): """ A QFrame subclass which acts as an item QStack. """ def __init__(self, *args, **kwargs): """ Initialize a QStackItem. Parameters ---------- *args, **kwargs The position and keyword arguments required to initialize a QWidget. """ super(QStackItem, self).__init__(*args, **kwargs) self._stack_widget = None self.setLayout(QSingleWidgetLayout()) def stackWidget(self): """ Get the stack widget for this stack item. Returns ------- result : QWidget or None The stack widget being managed by this item. """ return self._stack_widget def setStackWidget(self, widget): """ Set the stack widget for this stack item. Parameters ---------- widget : QWidget The QWidget to use as the stack widget in this item. """ self._stack_widget = widget self.layout().setWidget(widget) class QtStackItem(QtWidget, ProxyStackItem): """ A Qt implementation of an Enaml ProxyStackItem. """ #: A reference to the widget created by the proxy. widget = Typed(QStackItem) #-------------------------------------------------------------------------- # Initialization API #-------------------------------------------------------------------------- def create_widget(self): """ Create the underlying QStackItem widget. """ self.widget = QStackItem(self.parent_widget()) def init_layout(self): """ Initialize the layout for the underyling widget. """ super(QtStackItem, self).init_layout() self.widget.setStackWidget(self.stack_widget()) #-------------------------------------------------------------------------- # Utility Methods #-------------------------------------------------------------------------- def stack_widget(self): """ Find and return the stack widget child for this widget. """ d = self.declaration.stack_widget() if d is not None: return d.proxy.widget #-------------------------------------------------------------------------- # Child Events #-------------------------------------------------------------------------- def child_added(self, child): """ Handle the child added event for a QtStackItem. """ super(QtStackItem, self).child_added(child) if isinstance(child, QtContainer): self.widget.setStackWidget(self.stack_widget()) def child_removed(self, child): """ Handle the child added event for a QtStackItem. """ super(QtStackItem, self).child_removed(child) if isinstance(child, QtContainer): self.widget.setStackWidget(self.stack_widget()) #-------------------------------------------------------------------------- # Widget Update Methods #-------------------------------------------------------------------------- def set_visible(self, visible): """ An overridden visibility setter. This setter disables changing visibility on the widget since the visibility is controlled entirely by the parent stack. """ pass
# User Configuration variable settings for pitimolo # Purpose - Motion Detection Security Cam # Updated - 20-Jun-2017 IMPORTANT - Required for pi-timolo.py ver 6.71 or Greater # Done by - Claude Pageau configTitle = "pi-timolo ver 6.71 Default Settings" configName = "config.py" #====================================== # pi-timolo.py ver 6.71 Settings #====================================== # Logging and Debug Settings # -------------------------- # Note - Set verbose to False if script is run in background or from /etc/rc.local verbose = True # default= True Sends logging Info to Console. False if running script as daeman logDataToFile = False # default= False True logs diagnostic data to a disk file for review debug = False # default= False True = debug mode returns pixel average data for tuning imageTestPrint = False # default= False Set to True to print one image and exit (useful for aligning camera) # Image Settings # -------------- imageNamePrefix = 'cam1-' # default= 'cam1-' for all image file names. Eg garage- imageWidth = 1024 # default= 1024 Full Size Image Width in px imageHeight = 768 # default= 768 Full Size Image Height in px imageFormat = ".jpg" # default = ".jpg" image Formats .jpeg .png .gif .bmp imageJpegQuality = 20 # default = 20 Valid jpg encoder quality values 1(high) - 40(low) imageVFlip = False # default= False True Flips image Vertically imageHFlip = False # default= False True Flips image Horizontally imageRotation = 0 # Default= 0 Rotate image. Valid values: 0, 90, 180, 270 noNightShots = False # default= False True=No Night Images (Motion or Timelapse) noDayShots = False # default= False True=No Day Images (Motion or Timelapse) imagePreview = False # default= False True=Preview image on connected RPI Monitor useVideoPort = False # default= False True=Use the video port to capture motion images (faster than the image port). # Date/Time Settings for Displaying info Directly on Images # --------------------------------------------------------- showDateOnImage = True # default= True False=Do Not display date/time text on images showTextFontSize = 18 # default= 18 Size of image Font in pixel height showTextBottom = True # Location of image Text True=Bottom False=Top showTextWhite = True # Colour of image Text True=White False=Black showTextWhiteNight = True # default=True Changes night text to white. Useful if night needs white text instead of black # Low Light Twilight and Night Settings # ------------------------------------- nightTwilightThreshold = 90 # default= 90 dayPixAve where twilight starts (framerate_range shutter) nightDarkThreshold = 50 # default= 50 dayPixAve where camera variable shutter long exposure starts nightBlackThreshold = 4 # default= 4 dayPixAve where almost no light so Max settings used nightSleepSec = 30 # default= 30 Sec - Time period to allow camera to calculate low light AWB nightMaxShutSec = 5.9 # default= 5.9 Sec Highest V1 Cam shutter for long exposures V2=10 Sec. nightMaxISO = 800 # default= 800 Night ISO setting for Long Exposure below nightThreshold nightDarkAdjust = 4.7 # default= 4.7 Factor to fine tune nightDarkThreshold brightness (lower is brighter) # Motion Detect Settings # ---------------------- motionOn = True # default= True True=Turns Motion Detect On, False=Off motionDir = "media/motion" # default= "media/motion" Folder Path for Motion Detect Image Storage motionPrefix = "mo-" # default= "mo-" Prefix for all Motion Detect images motionCamSleep = 0.7 # default= 0.7 Sec of day sleep so camera can measure AWB before taking photo motionStreamOn = False # default= False True=Use video stream thread for Faster motion detect best on quad core motionStreamStopSec = 0.5 # default= 0.5 seconds Time to close stream thread motionAverage = 20 # default= 20 Num Images to Average for motion verification: 1=last image only, 100=Med, 300=High, Etc. motionThreshold = 50 # default= 50 (1-200) How much a pixel has to change to be counted motionSensitivity = 100 # default= 100 Number of changed pixels to trigger motion image motionVideoOn = False # default= False True=Take a video clip rather than image motionVideoTimer = 10 # default= 10 seconds of video clip to take if Motion Detected motionQuickTLOn = False # default= False True=Take a quick time lapse sequence rather than a single image (overrides motionVideoOn) motionQuickTLTimer = 10 # default= 10 Duration in seconds of quick time lapse sequence after initial motion detected motionQuickTLInterval = 0 # default= 0 seconds between each Quick time lapse image. 0 is fast as possible motionForce = 3600 # default= 3600 seconds (1 hr) Force single motion image if no Motion Detected in specified seconds. motionNumOn = True # default= True True=filenames by sequenced Number False=filenames by date/time motionNumRecycle = True # default= True when NumMax reached restart at NumStart instead of exiting motionNumStart = 1000 # default= 1000 Start 0f motion number sequence motionNumMax = 500 # default= 0 Max number of motion images desired. 0=Continuous motionDotsOn = True # default= True Displays motion loop progress dots if verbose=True False=Non motionDotsMax = 100 # default= 100 Number of motion dots before starting new line if motionDotsOn=True motionSubDirMaxHours = 0 # 0=off or specify Max Hrs to create new sub-folder if HrsMax exceeded motionSubDirMaxFiles = 0 # 0=off or specify Max Files to create new sub-folder if FilesMax exceeded motionRecentMax = 10 # 0=off Maintain specified number of most recent files in motionRecentDir motionRecentDir = "media/recent/motion" # default= "media/recent/motion" Location of motionRecent files createLockFile = False # default= False True=Create pi-timolo.sync file whenever images saved. # Lock File is used to indicate motion images have been added # so sync.sh can sync in background via sudo crontab -e # Time Lapse Settings # ------------------- timelapseOn = True # default= False True=Turn timelapse On, False=Off timelapseDir = "media/timelapse" # default= "media/timelapse" Storage Folder Path for Time Lapse Image Storage timelapsePrefix = "tl-" # default= "tl-" Prefix for All timelapse images with this prefix timelapseCamSleep = 4.0 # default= 4.0 seconds day sleep so camera can measure AWB before taking photo timelapseTimer = 300 # default= 300 (5 min) Seconds between timelapse images timelapseNumOn = True # default= True filenames Sequenced by Number False=filenames by date/time timelapseNumRecycle = True # default= True Restart Numbering at NumStart False= Surpress Timelapse at NumMax timelapseNumStart = 1000 # default= 1000 Start of timelapse number sequence timelapseNumMax = 2000 # default= 2000 Max number of timelapse images desired. 0=Continuous timelapseExitSec = 0 # default= 0 seconds Surpress Timelapse after specified Seconds 0=Continuous timelapseMaxFiles = 0 # 0=off or specify MaxFiles to maintain then oldest are deleted default=0 (off) timelapseSubDirMaxHours = 0 # 0=off or specify MaxHours - Creates New dated sub-folder if MaxHours exceeded timelapseSubDirMaxFiles = 0 # 0=off or specify MaxFiles - Creates New dated sub-folder if MaxFiles exceeded timelapseRecentMax = 10 # 0=off or specify number of most recent files to save in timelapseRecentDir timelapseRecentDir = "media/recent/timelapse" # default= "media/recent/timelapse" location of timelapseRecent files # Video Repeat Mode (suppresses Timelapse and Motion Settings) # ----------------- videoRepeatOn = False # Turn on Video Repeat Mode IMPORTANT Overrides timelapse and motion videoPath = "media/videos" # default= media/videos Storage folder path for videos videoPrefix = "vid-" # prefix for video filenames videoDuration = 120 # default= 120 seconds for each video recording videoTimer = 60 # default= 60 minutes Run Recording Session then Exit 0=Continuous videoNumOn = False # default= True True=filenames by sequence Number False=filenames by date/time videoNumRecycle = False # default= False when NumMax reached restart at NumStart instead of exiting videoNumStart = 100 # default= 100 Start of video filename number sequence videoNumMax = 20 # default= 20 Max number of videos desired. 0=Continuous # Manage Disk Space Settings #--------------------------- spaceTimerHrs = 0 # default= 0 0=off or specify hours frequency to perform free disk space check spaceFreeMB = 500 # default= 500 Target Free space in MB Required. spaceMediaDir = '/home/pi/pi-timolo/media' # default= '/home/pi/pi-timolo/media' Starting point for directory walk spaceFileExt = 'jpg' # default= 'jpg' File extension to Delete Oldest Files #====================================== # webserver.py Settings #====================================== # Web Server settings # ------------------- web_server_port = 8080 # default= 8080 Web server access port eg http://192.168.1.100:8080 web_server_root = "media" # default= "media" webserver root path to webserver image/video sub-folders web_page_title = "Pi-Timolo Media" # web page title that browser show (not displayed on web page) web_page_refresh_on = True # False=Off (never) Refresh True=On (per seconds below) web_page_refresh_sec = "180" # default= "180" seconds to wait for web page refresh seconds (three minutes) web_page_blank = False # True Starts left image with a blank page until a right menu item is selected # False displays second list[1] item since first may be in progress # Left iFrame Image Settings # -------------------------- web_image_height = "768" # default= "768" px height of images to display in iframe web_iframe_width_usage = "75%" # Left Pane - Sets % of total screen width allowed for iframe. Rest for right list web_iframe_width = "100%" # Desired frame width to display images. can be eg percent "80%" or px "1280" web_iframe_height = "100%" # Desired frame height to display images. Scroll bars if image larger (percent or px) # Right Side Files List # --------------------- web_max_list_entries = 0 # 0 = All or Specify Max right side file entries to show (must be > 1) web_list_height = web_image_height # Right List - side menu height in px (link selection) web_list_by_datetime = True # True=datetime False=filename web_list_sort_descending = True # reverse sort order (filename or datetime per web_list_by_datetime setting # ---------------------------------------------- End of User Variables -----------------------------------------------------
'''functions to work with contrasts for multiple tests contrast matrices for comparing all pairs, all levels to reference level, ... extension to 2-way groups in progress TwoWay: class for bringing two-way analysis together and try out various helper functions Idea for second part - get all transformation matrices to move in between different full rank parameterizations - standardize to one parameterization to get all interesting effects. - multivariate normal distribution - exploit or expand what we have in LikelihoodResults, cov_params, f_test, t_test, example: resols_dropf_full.cov_params(C2) - connect to new multiple comparison for contrast matrices, based on multivariate normal or t distribution (Hothorn, Bretz, Westfall) ''' from numpy.testing import assert_equal import numpy as np #next 3 functions copied from multicomp.py def contrast_allpairs(nm): '''contrast or restriction matrix for all pairs of nm variables Parameters ---------- nm : int Returns ------- contr : ndarray, 2d, (nm*(nm-1)/2, nm) contrast matrix for all pairwise comparisons ''' contr = [] for i in range(nm): for j in range(i+1, nm): contr_row = np.zeros(nm) contr_row[i] = 1 contr_row[j] = -1 contr.append(contr_row) return np.array(contr) def contrast_all_one(nm): '''contrast or restriction matrix for all against first comparison Parameters ---------- nm : int Returns ------- contr : ndarray, 2d, (nm-1, nm) contrast matrix for all against first comparisons ''' contr = np.column_stack((np.ones(nm-1), -np.eye(nm-1))) return contr def contrast_diff_mean(nm): '''contrast or restriction matrix for all against mean comparison Parameters ---------- nm : int Returns ------- contr : ndarray, 2d, (nm-1, nm) contrast matrix for all against mean comparisons ''' return np.eye(nm) - np.ones((nm,nm))/nm def signstr(x, noplus=False): if x in [-1,0,1]: if not noplus: return '+' if np.sign(x)>=0 else '-' else: return '' if np.sign(x)>=0 else '-' else: return str(x) def contrast_labels(contrasts, names, reverse=False): if reverse: sl = slice(None, None, -1) else: sl = slice(None) labels = [''.join(['%s%s' % (signstr(c, noplus=True),v) for c,v in zip(row, names)[sl] if c != 0]) for row in contrasts] return labels def contrast_product(names1, names2, intgroup1=None, intgroup2=None, pairs=False): '''build contrast matrices for products of two categorical variables this is an experimental script and should be converted to a class Parameters ---------- names1, names2 : lists of strings contains the list of level labels for each categorical variable intgroup1, intgroup2 : ndarrays TODO: this part not tested, finished yet categorical variable Notes ----- This creates a full rank matrix. It does not do all pairwise comparisons, parameterization is using contrast_all_one to get differences with first level. ? does contrast_all_pairs work as a plugin to get all pairs ? ''' n1 = len(names1) n2 = len(names2) names_prod = ['%s_%s' % (i,j) for i in names1 for j in names2] ee1 = np.zeros((1,n1)) ee1[0,0] = 1 if not pairs: dd = np.r_[ee1, -contrast_all_one(n1)] else: dd = np.r_[ee1, -contrast_allpairs(n1)] contrast_prod = np.kron(dd[1:], np.eye(n2)) names_contrast_prod0 = contrast_labels(contrast_prod, names_prod, reverse=True) names_contrast_prod = [''.join(['%s%s' % (signstr(c, noplus=True),v) for c,v in zip(row, names_prod)[::-1] if c != 0]) for row in contrast_prod] ee2 = np.zeros((1,n2)) ee2[0,0] = 1 #dd2 = np.r_[ee2, -contrast_all_one(n2)] if not pairs: dd2 = np.r_[ee2, -contrast_all_one(n2)] else: dd2 = np.r_[ee2, -contrast_allpairs(n2)] contrast_prod2 = np.kron(np.eye(n1), dd2[1:]) names_contrast_prod2 = [''.join(['%s%s' % (signstr(c, noplus=True),v) for c,v in zip(row, names_prod)[::-1] if c != 0]) for row in contrast_prod2] if (intgroup1 is not None) and (intgroup1 is not None): d1, _ = dummy_1d(intgroup1) d2, _ = dummy_1d(intgroup2) dummy = dummy_product(d1, d2) else: dummy = None return (names_prod, contrast_prod, names_contrast_prod, contrast_prod2, names_contrast_prod2, dummy) def dummy_1d(x, varname=None): '''dummy variable for id integer groups Parameters ---------- x : ndarray, 1d categorical variable, requires integers if varname is None varname : str name of the variable used in labels for category levels Returns ------- dummy : ndarray, 2d array of dummy variables, one column for each level of the category (full set) labels : list[str] labels for the columns, i.e. levels of each category Notes ----- use tools.categorical instead for more more options See Also -------- statsmodels.tools.categorical Examples -------- >>> x = np.array(['F', 'F', 'M', 'M', 'F', 'F', 'M', 'M', 'F', 'F', 'M', 'M'], dtype='|S1') >>> dummy_1d(x, varname='gender') (array([[1, 0], [1, 0], [0, 1], [0, 1], [1, 0], [1, 0], [0, 1], [0, 1], [1, 0], [1, 0], [0, 1], [0, 1]]), ['gender_F', 'gender_M']) ''' if varname is None: #assumes integer labels = ['level_%d' % i for i in range(x.max() + 1)] return (x[:,None]==np.arange(x.max()+1)).astype(int), labels else: grouplabels = np.unique(x) labels = [varname + '_%s' % str(i) for i in grouplabels] return (x[:,None]==grouplabels).astype(int), labels def dummy_product(d1, d2, method='full'): '''dummy variable from product of two dummy variables Parameters ---------- d1, d2 : ndarray two dummy variables, assumes full set for methods 'drop-last' and 'drop-first' method : {'full', 'drop-last', 'drop-first'} 'full' returns the full product, encoding of intersection of categories. The drop methods provide a difference dummy encoding: (constant, main effects, interaction effects). The first or last columns of the dummy variable (i.e. levels) are dropped to get full rank dummy matrix. Returns ------- dummy : ndarray dummy variable for product, see method ''' if method == 'full': dd = (d1[:,:,None]*d2[:,None,:]).reshape(d1.shape[0],-1) elif method == 'drop-last': #same as SAS transreg d12rl = dummy_product(d1[:,:-1], d2[:,:-1]) dd = np.column_stack((np.ones(d1.shape[0], int), d1[:,:-1], d2[:,:-1],d12rl)) #Note: dtype int should preserve dtype of d1 and d2 elif method == 'drop-first': d12r = dummy_product(d1[:,1:], d2[:,1:]) dd = np.column_stack((np.ones(d1.shape[0], int), d1[:,1:], d2[:,1:],d12r)) else: raise ValueError('method not recognized') return dd def dummy_limits(d): '''start and endpoints of groups in a sorted dummy variable array helper function for nested categories Examples -------- >>> d1 = np.array([[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1]]) >>> dummy_limits(d1) (array([0, 4, 8]), array([ 4, 8, 12])) get group slices from an array >>> [np.arange(d1.shape[0])[b:e] for b,e in zip(*dummy_limits(d1))] [array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([ 8, 9, 10, 11])] >>> [np.arange(d1.shape[0])[b:e] for b,e in zip(*dummy_limits(d1))] [array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([ 8, 9, 10, 11])] ''' nobs, nvars = d.shape start1, col1 = np.nonzero(np.diff(d,axis=0)==1) end1, col1_ = np.nonzero(np.diff(d,axis=0)==-1) cc = np.arange(nvars) #print(cc, np.r_[[0], col1], np.r_[col1_, [nvars-1]] if ((not (np.r_[[0], col1] == cc).all()) or (not (np.r_[col1_, [nvars-1]] == cc).all())): raise ValueError('dummy variable is not sorted') start = np.r_[[0], start1+1] end = np.r_[end1+1, [nobs]] return start, end def dummy_nested(d1, d2, method='full'): '''unfinished and incomplete mainly copy past dummy_product dummy variable from product of two dummy variables Parameters ---------- d1, d2 : ndarray two dummy variables, d2 is assumed to be nested in d1 Assumes full set for methods 'drop-last' and 'drop-first'. method : {'full', 'drop-last', 'drop-first'} 'full' returns the full product, which in this case is d2. The drop methods provide an effects encoding: (constant, main effects, subgroup effects). The first or last columns of the dummy variable (i.e. levels) are dropped to get full rank encoding. Returns ------- dummy : ndarray dummy variable for product, see method ''' if method == 'full': return d2 start1, end1 = dummy_limits(d1) start2, end2 = dummy_limits(d2) first = np.in1d(start2, start1) last = np.in1d(end2, end1) equal = (first == last) col_dropf = ~first*~equal col_dropl = ~last*~equal if method == 'drop-last': d12rl = dummy_product(d1[:,:-1], d2[:,:-1]) dd = np.column_stack((np.ones(d1.shape[0], int), d1[:,:-1], d2[:,col_dropl])) #Note: dtype int should preserve dtype of d1 and d2 elif method == 'drop-first': d12r = dummy_product(d1[:,1:], d2[:,1:]) dd = np.column_stack((np.ones(d1.shape[0], int), d1[:,1:], d2[:,col_dropf])) else: raise ValueError('method not recognized') return dd, col_dropf, col_dropl class DummyTransform(object): '''Conversion between full rank dummy encodings y = X b + u b = C a a = C^{-1} b y = X C a + u define Z = X C, then y = Z a + u contrasts: R_b b = r R_a a = R_b C a = r where R_a = R_b C Here C is the transform matrix, with dot_left and dot_right as the main methods, and the same for the inverse transform matrix, C^{-1} Note: - The class was mainly written to keep left and right straight. - No checking is done. - not sure yet if method names make sense ''' def __init__(self, d1, d2): '''C such that d1 C = d2, with d1 = X, d2 = Z should be (x, z) in arguments ? ''' self.transf_matrix = np.linalg.lstsq(d1, d2, rcond=-1)[0] self.invtransf_matrix = np.linalg.lstsq(d2, d1, rcond=-1)[0] def dot_left(self, a): ''' b = C a ''' return np.dot(self.transf_matrix, a) def dot_right(self, x): ''' z = x C ''' return np.dot(x, self.transf_matrix) def inv_dot_left(self, b): ''' a = C^{-1} b ''' return np.dot(self.invtransf_matrix, b) def inv_dot_right(self, z): ''' x = z C^{-1} ''' return np.dot(z, self.invtransf_matrix) def groupmean_d(x, d): '''groupmeans using dummy variables Parameters ---------- x : array_like, ndim data array, tested for 1,2 and 3 dimensions d : ndarray, 1d dummy variable, needs to have the same length as x in axis 0. Returns ------- groupmeans : ndarray, ndim-1 means for each group along axis 0, the levels of the groups are the last axis Notes ----- This will be memory intensive if there are many levels in the categorical variable, i.e. many columns in the dummy variable. In this case it is recommended to use a more efficient version. ''' x = np.asarray(x) ## if x.ndim == 1: ## nvars = 1 ## else: nvars = x.ndim + 1 sli = [slice(None)] + [None]*(nvars-2) + [slice(None)] return (x[...,None] * d[sli]).sum(0)*1./d.sum(0) class TwoWay(object): '''a wrapper class for two way anova type of analysis with OLS currently mainly to bring things together Notes ----- unclear: adding multiple test might assume block design or orthogonality This estimates the full dummy version with OLS. The drop first dummy representation can be recovered through the transform method. TODO: add more methods, tests, pairwise, multiple, marginal effects try out what can be added for userfriendly access. missing: ANOVA table ''' def __init__(self, endog, factor1, factor2, varnames=None): self.nobs = factor1.shape[0] if varnames is None: vname1 = 'a' vname2 = 'b' else: vname1, vname1 = varnames self.d1, self.d1_labels = d1, d1_labels = dummy_1d(factor1, vname1) self.d2, self.d2_labels = d2, d2_labels = dummy_1d(factor2, vname2) self.nlevel1 = nlevel1 = d1.shape[1] self.nlevel2 = nlevel2 = d2.shape[1] #get product dummies res = contrast_product(d1_labels, d2_labels) prodlab, C1, C1lab, C2, C2lab, _ = res self.prod_label, self.C1, self.C1_label, self.C2, self.C2_label, _ = res dp_full = dummy_product(d1, d2, method='full') dp_dropf = dummy_product(d1, d2, method='drop-first') self.transform = DummyTransform(dp_full, dp_dropf) #estimate the model self.nvars = dp_full.shape[1] self.exog = dp_full self.resols = sm.OLS(endog, dp_full).fit() self.params = self.resols.params #get transformed parameters, (constant, main, interaction effect) self.params_dropf = self.transform.inv_dot_left(self.params) self.start_interaction = 1 + (nlevel1 - 1) + (nlevel2 - 1) self.n_interaction = self.nvars - self.start_interaction #convert to cached property def r_nointer(self): '''contrast/restriction matrix for no interaction ''' nia = self.n_interaction R_nointer = np.hstack((np.zeros((nia, self.nvars-nia)), np.eye(nia))) #inter_direct = resols_full_dropf.tval[-nia:] R_nointer_transf = self.transform.inv_dot_right(R_nointer) self.R_nointer_transf = R_nointer_transf return R_nointer_transf def ttest_interaction(self): '''ttests for no-interaction terms are zero ''' #use self.r_nointer instead nia = self.n_interaction R_nointer = np.hstack((np.zeros((nia, self.nvars-nia)), np.eye(nia))) #inter_direct = resols_full_dropf.tval[-nia:] R_nointer_transf = self.transform.inv_dot_right(R_nointer) self.R_nointer_transf = R_nointer_transf t_res = self.resols.t_test(R_nointer_transf) return t_res def ftest_interaction(self): '''ttests for no-interaction terms are zero ''' R_nointer_transf = self.r_nointer() return self.resols.f_test(R_nointer_transf) def ttest_conditional_effect(self, factorind): if factorind == 1: return self.resols.t_test(self.C1), self.C1_label else: return self.resols.t_test(self.C2), self.C2_label def summary_coeff(self): from statsmodels.iolib import SimpleTable params_arr = self.params.reshape(self.nlevel1, self.nlevel2) stubs = self.d1_labels headers = self.d2_labels title = 'Estimated Coefficients by factors' table_fmt = dict( data_fmts = ["%#10.4g"]*self.nlevel2) return SimpleTable(params_arr, headers, stubs, title=title, txt_fmt=table_fmt) # --------------- tests # TODO: several tests still missing, several are in the example with print class TestContrastTools(object): def __init__(self): self.v1name = ['a0', 'a1', 'a2'] self.v2name = ['b0', 'b1'] self.d1 = np.array([[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1]]) def test_dummy_1d(self): x = np.array(['F', 'F', 'M', 'M', 'F', 'F', 'M', 'M', 'F', 'F', 'M', 'M'], dtype='|S1') d, labels = (np.array([[1, 0], [1, 0], [0, 1], [0, 1], [1, 0], [1, 0], [0, 1], [0, 1], [1, 0], [1, 0], [0, 1], [0, 1]]), ['gender_F', 'gender_M']) res_d, res_labels = dummy_1d(x, varname='gender') assert_equal(res_d, d) assert_equal(res_labels, labels) def test_contrast_product(self): res_cp = contrast_product(self.v1name, self.v2name) res_t = [0]*6 res_t[0] = ['a0_b0', 'a0_b1', 'a1_b0', 'a1_b1', 'a2_b0', 'a2_b1'] res_t[1] = np.array([[-1., 0., 1., 0., 0., 0.], [ 0., -1., 0., 1., 0., 0.], [-1., 0., 0., 0., 1., 0.], [ 0., -1., 0., 0., 0., 1.]]) res_t[2] = ['a1_b0-a0_b0', 'a1_b1-a0_b1', 'a2_b0-a0_b0', 'a2_b1-a0_b1'] res_t[3] = np.array([[-1., 1., 0., 0., 0., 0.], [ 0., 0., -1., 1., 0., 0.], [ 0., 0., 0., 0., -1., 1.]]) res_t[4] = ['a0_b1-a0_b0', 'a1_b1-a1_b0', 'a2_b1-a2_b0'] for ii in range(5): np.testing.assert_equal(res_cp[ii], res_t[ii], err_msg=str(ii)) def test_dummy_limits(self): b,e = dummy_limits(self.d1) assert_equal(b, np.array([0, 4, 8])) assert_equal(e, np.array([ 4, 8, 12])) if __name__ == '__main__': tt = TestContrastTools() tt.test_contrast_product() tt.test_dummy_1d() tt.test_dummy_limits() import statsmodels.api as sm examples = ['small', 'large', None][1] v1name = ['a0', 'a1', 'a2'] v2name = ['b0', 'b1'] res_cp = contrast_product(v1name, v2name) print(res_cp) y = np.arange(12) x1 = np.arange(12)//4 x2 = np.arange(12)//2 % 2 if 'small' in examples: d1, d1_labels = dummy_1d(x1) d2, d2_labels = dummy_1d(x2) if 'large' in examples: x1 = np.repeat(x1, 5, axis=0) x2 = np.repeat(x2, 5, axis=0) nobs = x1.shape[0] d1, d1_labels = dummy_1d(x1) d2, d2_labels = dummy_1d(x2) dd_full = dummy_product(d1, d2, method='full') dd_dropl = dummy_product(d1, d2, method='drop-last') dd_dropf = dummy_product(d1, d2, method='drop-first') #Note: full parameterization of dummies is orthogonal #np.eye(6)*10 in "large" example print((np.dot(dd_full.T, dd_full) == np.diag(dd_full.sum(0))).all()) #check that transforms work #generate 3 data sets with the 3 different parameterizations effect_size = [1., 0.01][1] noise_scale = [0.001, 0.1][0] noise = noise_scale * np.random.randn(nobs) beta = effect_size * np.arange(1,7) ydata_full = (dd_full * beta).sum(1) + noise ydata_dropl = (dd_dropl * beta).sum(1) + noise ydata_dropf = (dd_dropf * beta).sum(1) + noise resols_full_full = sm.OLS(ydata_full, dd_full).fit() resols_full_dropf = sm.OLS(ydata_full, dd_dropf).fit() params_f_f = resols_full_full.params params_f_df = resols_full_dropf.params resols_dropf_full = sm.OLS(ydata_dropf, dd_full).fit() resols_dropf_dropf = sm.OLS(ydata_dropf, dd_dropf).fit() params_df_f = resols_dropf_full.params params_df_df = resols_dropf_dropf.params tr_of = np.linalg.lstsq(dd_dropf, dd_full, rcond=-1)[0] tr_fo = np.linalg.lstsq(dd_full, dd_dropf, rcond=-1)[0] print(np.dot(tr_fo, params_df_df) - params_df_f) print(np.dot(tr_of, params_f_f) - params_f_df) transf_f_df = DummyTransform(dd_full, dd_dropf) print(np.max(np.abs((dd_full - transf_f_df.inv_dot_right(dd_dropf))))) print(np.max(np.abs((dd_dropf - transf_f_df.dot_right(dd_full))))) print(np.max(np.abs((params_df_df - transf_f_df.inv_dot_left(params_df_f))))) np.max(np.abs((params_f_df - transf_f_df.inv_dot_left(params_f_f)))) prodlab, C1, C1lab, C2, C2lab,_ = contrast_product(v1name, v2name) print('\ntvalues for no effect of factor 1') print('each test is conditional on a level of factor 2') print(C1lab) print(resols_dropf_full.t_test(C1).tvalue) print('\ntvalues for no effect of factor 2') print('each test is conditional on a level of factor 1') print(C2lab) print(resols_dropf_full.t_test(C2).tvalue) #covariance matrix of restrictions C2, note: orthogonal resols_dropf_full.cov_params(C2) #testing for no interaction effect R_noint = np.hstack((np.zeros((2,4)), np.eye(2))) inter_direct = resols_full_dropf.tvalues[-2:] inter_transf = resols_full_full.t_test(transf_f_df.inv_dot_right(R_noint)).tvalue print(np.max(np.abs((inter_direct - inter_transf)))) #now with class version tw = TwoWay(ydata_dropf, x1, x2) print(tw.ttest_interaction().tvalue) print(tw.ttest_interaction().pvalue) print(tw.ftest_interaction().fvalue) print(tw.ftest_interaction().pvalue) print(tw.ttest_conditional_effect(1)[0].tvalue) print(tw.ttest_conditional_effect(2)[0].tvalue) print(tw.summary_coeff()) ''' documentation for early examples while developing - some have changed already >>> y = np.arange(12) >>> y array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) >>> x1 = np.arange(12)//4 >>> x1 array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2]) >>> x2 = np.arange(12)//2%2 >>> x2 array([0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]) >>> d1 = dummy_1d(x1) >>> d1 array([[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1]]) >>> d2 = dummy_1d(x2) >>> d2 array([[1, 0], [1, 0], [0, 1], [0, 1], [1, 0], [1, 0], [0, 1], [0, 1], [1, 0], [1, 0], [0, 1], [0, 1]]) >>> d12 = dummy_product(d1, d2) >>> d12 array([[1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1]]) >>> d12rl = dummy_product(d1[:,:-1], d2[:,:-1]) >>> np.column_stack((np.ones(d1.shape[0]), d1[:,:-1], d2[:,:-1],d12rl)) array([[ 1., 1., 0., 1., 1., 0.], [ 1., 1., 0., 1., 1., 0.], [ 1., 1., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0., 0.], [ 1., 0., 1., 1., 0., 1.], [ 1., 0., 1., 1., 0., 1.], [ 1., 0., 1., 0., 0., 0.], [ 1., 0., 1., 0., 0., 0.], [ 1., 0., 0., 1., 0., 0.], [ 1., 0., 0., 1., 0., 0.], [ 1., 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0., 0.]]) ''' #nprod = ['%s_%s' % (i,j) for i in ['a0', 'a1', 'a2'] for j in ['b0', 'b1']] #>>> [''.join(['%s%s' % (signstr(c),v) for c,v in zip(row, nprod) if c != 0]) # for row in np.kron(dd[1:], np.eye(2))] ''' >>> nprod = ['%s_%s' % (i,j) for i in ['a0', 'a1', 'a2'] for j in ['b0', 'b1']] >>> nprod ['a0_b0', 'a0_b1', 'a1_b0', 'a1_b1', 'a2_b0', 'a2_b1'] >>> [''.join(['%s%s' % (signstr(c),v) for c,v in zip(row, nprod) if c != 0]) for row in np.kron(dd[1:], np.eye(2))] ['-a0b0+a1b0', '-a0b1+a1b1', '-a0b0+a2b0', '-a0b1+a2b1'] >>> [''.join(['%s%s' % (signstr(c),v) for c,v in zip(row, nprod)[::-1] if c != 0]) for row in np.kron(dd[1:], np.eye(2))] ['+a1_b0-a0_b0', '+a1_b1-a0_b1', '+a2_b0-a0_b0', '+a2_b1-a0_b1'] >>> np.r_[[[1,0,0,0,0]],contrast_all_one(5)] array([[ 1., 0., 0., 0., 0.], [ 1., -1., 0., 0., 0.], [ 1., 0., -1., 0., 0.], [ 1., 0., 0., -1., 0.], [ 1., 0., 0., 0., -1.]]) >>> idxprod = [(i,j) for i in range(3) for j in range(2)] >>> idxprod [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)] >>> np.array(idxprod).reshape(2,3,2,order='F')[:,:,0] array([[0, 1, 2], [0, 1, 2]]) >>> np.array(idxprod).reshape(2,3,2,order='F')[:,:,1] array([[0, 0, 0], [1, 1, 1]]) >>> dd3_ = np.r_[[[0,0,0]],contrast_all_one(3)] pairwise contrasts and reparameterization dd = np.r_[[[1,0,0,0,0]],-contrast_all_one(5)] >>> dd array([[ 1., 0., 0., 0., 0.], [-1., 1., 0., 0., 0.], [-1., 0., 1., 0., 0.], [-1., 0., 0., 1., 0.], [-1., 0., 0., 0., 1.]]) >>> np.dot(dd.T, np.arange(5)) array([-10., 1., 2., 3., 4.]) >>> np.round(np.linalg.inv(dd.T)).astype(int) array([[1, 1, 1, 1, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]) >>> np.round(np.linalg.inv(dd)).astype(int) array([[1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 0, 1, 0, 0], [1, 0, 0, 1, 0], [1, 0, 0, 0, 1]]) >>> dd array([[ 1., 0., 0., 0., 0.], [-1., 1., 0., 0., 0.], [-1., 0., 1., 0., 0.], [-1., 0., 0., 1., 0.], [-1., 0., 0., 0., 1.]]) >>> ddinv=np.round(np.linalg.inv(dd.T)).astype(int) >>> np.dot(ddinv, np.arange(5)) array([10, 1, 2, 3, 4]) >>> np.dot(dd, np.arange(5)) array([ 0., 1., 2., 3., 4.]) >>> np.dot(dd, 5+np.arange(5)) array([ 5., 1., 2., 3., 4.]) >>> ddinv2 = np.round(np.linalg.inv(dd)).astype(int) >>> np.dot(ddinv2, np.arange(5)) array([0, 1, 2, 3, 4]) >>> np.dot(ddinv2, 5+np.arange(5)) array([ 5, 11, 12, 13, 14]) >>> np.dot(ddinv2, [5, 0, 0 , 1, 2]) array([5, 5, 5, 6, 7]) >>> np.dot(ddinv2, dd) array([[ 1., 0., 0., 0., 0.], [ 0., 1., 0., 0., 0.], [ 0., 0., 1., 0., 0.], [ 0., 0., 0., 1., 0.], [ 0., 0., 0., 0., 1.]]) >>> dd3 = -np.r_[[[1,0,0]],contrast_all_one(3)] >>> dd2 = -np.r_[[[1,0]],contrast_all_one(2)] >>> np.kron(np.eye(3), dd2) array([[-1., 0., 0., 0., 0., 0.], [-1., 1., 0., 0., 0., 0.], [ 0., 0., -1., 0., 0., 0.], [ 0., 0., -1., 1., 0., 0.], [ 0., 0., 0., 0., -1., 0.], [ 0., 0., 0., 0., -1., 1.]]) >>> dd2 array([[-1., 0.], [-1., 1.]]) >>> np.kron(np.eye(3), dd2[1:]) array([[-1., 1., 0., 0., 0., 0.], [ 0., 0., -1., 1., 0., 0.], [ 0., 0., 0., 0., -1., 1.]]) >>> np.kron(dd[1:], np.eye(2)) array([[-1., 0., 1., 0., 0., 0.], [ 0., -1., 0., 1., 0., 0.], [-1., 0., 0., 0., 1., 0.], [ 0., -1., 0., 0., 0., 1.]]) d_ = np.r_[[[1,0,0,0,0]],contrast_all_one(5)] >>> d_ array([[ 1., 0., 0., 0., 0.], [ 1., -1., 0., 0., 0.], [ 1., 0., -1., 0., 0.], [ 1., 0., 0., -1., 0.], [ 1., 0., 0., 0., -1.]]) >>> np.round(np.linalg.pinv(d_)).astype(int) array([[ 1, 0, 0, 0, 0], [ 1, -1, 0, 0, 0], [ 1, 0, -1, 0, 0], [ 1, 0, 0, -1, 0], [ 1, 0, 0, 0, -1]]) >>> np.linalg.inv(d_).astype(int) array([[ 1, 0, 0, 0, 0], [ 1, -1, 0, 0, 0], [ 1, 0, -1, 0, 0], [ 1, 0, 0, -1, 0], [ 1, 0, 0, 0, -1]]) group means >>> sli = [slice(None)] + [None]*(3-2) + [slice(None)] >>> (np.column_stack((y, x1, x2))[...,None] * d1[sli]).sum(0)*1./d1.sum(0) array([[ 1.5, 5.5, 9.5], [ 0. , 1. , 2. ], [ 0.5, 0.5, 0.5]]) >>> [(z[:,None] * d1).sum(0)*1./d1.sum(0) for z in np.column_stack((y, x1, x2)).T] [array([ 1.5, 5.5, 9.5]), array([ 0., 1., 2.]), array([ 0.5, 0.5, 0.5])] >>> '''
## \file ## \ingroup tutorial_roofit ## \notebook ## Basic functionality: adding boxes with parameters to RooPlots and decorating with arrows, etc... ## ## \macro_code ## ## \author Clemens Lange, Wouter Verkerke (C++ version) import ROOT # Set up model # --------------------- # Create observables x = ROOT.RooRealVar("x", "x", -10, 10) # Create Gaussian sigma = ROOT.RooRealVar("sigma", "sigma", 1, 0.1, 10) mean = ROOT.RooRealVar("mean", "mean", -3, -10, 10) gauss = ROOT.RooGaussian("gauss", "gauss", x, mean, sigma) # Generate a sample of 1000 events with sigma=3 data = gauss.generate(ROOT.RooArgSet(x), 1000) # Fit pdf to data gauss.fitTo(data) # Plot p.d.f. and data # ------------------------------------- # Overlay projection of gauss on data frame = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title( "RooPlot with decorations"), ROOT.RooFit.Bins(40)) data.plotOn(frame) gauss.plotOn(frame) # Add box with pdf parameters # ----------------------------------------------------- # Left edge of box starts at 55% of Xaxis) gauss.paramOn(frame, ROOT.RooFit.Layout(0.55)) # Add box with data statistics # ------------------------------------------------------- # X size of box is from 55% to 99% of Xaxis range, of box is at 80% of # Yaxis range) data.statOn(frame, ROOT.RooFit.Layout(0.55, 0.99, 0.8)) # Add text and arrow # ----------------------------------- # Add text to frame txt = ROOT.TText(2, 100, "Signal") txt.SetTextSize(0.04) txt.SetTextColor(ROOT.kRed) frame.addObject(txt) # Add arrow to frame arrow = ROOT.TArrow(2, 100, -1, 50, 0.01, "|>") arrow.SetLineColor(ROOT.kRed) arrow.SetFillColor(ROOT.kRed) arrow.SetLineWidth(3) frame.addObject(arrow) # Persist frame with all decorations in ROOT file # --------------------------------------------------------------------------------------------- f = ROOT.TFile("rf106_plotdecoration.root", "RECREATE") frame.Write() f.Close() # To read back and plot frame with all decorations in clean root session do # root> ROOT.TFile f("rf106_plotdecoration.root") # root> xframe.Draw() c = ROOT.TCanvas("rf106_plotdecoration", "rf106_plotdecoration", 600, 600) ROOT.gPad.SetLeftMargin(0.15) frame.GetYaxis().SetTitleOffset(1.6) frame.Draw() c.SaveAs("rf106_plotdecoration.png")
import nifty import numpy import nifty.segmentation import nifty.graph.rag import nifty.graph.agglo import vigra import matplotlib.pyplot as plt from random import shuffle #import fastfilters numpy.random.seed(32) Objective = nifty.graph.opt.lifted_multicut.PixelWiseLmcObjective2D class PlmcObjective2D(nifty.graph.opt.lifted_multicut.PixelWiseLmcObjective2D): def __init__(self,raw, affinities, weights, offsets): self.raw = numpy.require(raw,dtype='float32') self.affinities = affinities self.weights = weights self.offsets = offsets super(PlmcObjective2D, self).__init__(weights, offsets) def proposals_from_raw(self): proposals = [] for sigma in (1.0, 3.0, 5.0): raw = self.raw hmap = vigra.filters.hessianOfGaussianEigenvalues(raw, 5.0)[:,:,0] seg,nseg = vigra.analysis.watersheds(1.0*hmap) proposals.append(seg) #plt.imshow(nifty.segmentation.markBoundaries(raw/255.0, seg, color=(1,0,0))) #plt.show() return proposals def proposal_from_raw_agglo(self): proposals = [] for sigma in (1.0, 3.0, 5.0): grow_map = vigra.filters.hessianOfGaussianEigenvalues(self.raw, sigma)[:,:,0] overseg,nseg = vigra.analysis.watersheds(grow_map.astype('float32')) rag = nifty.graph.rag.gridRag(overseg) edge_features, node_features = nifty.graph.rag.accumulateMeanAndLength( rag, grow_map, [512,512],0) meanEdgeStrength = edge_features[:,0] edgeSizes = edge_features[:,1] nodeSizes = node_features[:,1] for size_reg in (0.1,0.2,0.4,0.8): # cluster-policy nnodes = rag.numberOfNodes//300 nnodes = min(nnodes, 1000) clusterPolicy = nifty.graph.agglo.edgeWeightedClusterPolicy( graph=rag, edgeIndicators=meanEdgeStrength, edgeSizes=edgeSizes, nodeSizes=nodeSizes, numberOfNodesStop=nnodes, sizeRegularizer=size_reg) # run agglomerative clustering agglomerativeClustering = nifty.graph.agglo.agglomerativeClustering(clusterPolicy) agglomerativeClustering.run() nodeSeg = agglomerativeClustering.result() # convert graph segmentation # to pixel segmentation seg = nifty.graph.rag.projectScalarNodeDataToPixels(rag, nodeSeg) #plt.imshow(nifty.segmentation.segmentOverlay(self.raw, seg, showBoundaries=False)) #plt.show() proposals.append(seg) return proposals def proposal_from_local_agglo(self, hmap): proposals = [] hmap0 = vigra.filters.gaussianSmoothing(hmap, 0.1) for sigma in (1.0, 3.0, 5.0): hmap1 = vigra.filters.gaussianSmoothing(hmap, sigma) grow_map = hmap0 + 0.05*hmap1 overseg,nseg = vigra.analysis.watersheds(grow_map.astype('float32')) rag = nifty.graph.rag.gridRag(overseg) edge_features, node_features = nifty.graph.rag.accumulateMeanAndLength( rag, hmap, [512,512],0) meanEdgeStrength = edge_features[:,0] edgeSizes = edge_features[:,1] nodeSizes = node_features[:,1] for size_reg in (0.1,0.2,0.4,0.8): # cluster-policy clusterPolicy = nifty.graph.agglo.edgeWeightedClusterPolicy( graph=rag, edgeIndicators=meanEdgeStrength, edgeSizes=edgeSizes, nodeSizes=nodeSizes, numberOfNodesStop=rag.numberOfNodes//10, sizeRegularizer=size_reg) # run agglomerative clustering agglomerativeClustering = nifty.graph.agglo.agglomerativeClustering(clusterPolicy) agglomerativeClustering.run() nodeSeg = agglomerativeClustering.result() # convert graph segmentation # to pixel segmentation seg = nifty.graph.rag.projectScalarNodeDataToPixels(rag, nodeSeg) #plt.imshow(nifty.segmentation.segmentOverlay(self.raw, seg, showBoundaries=False)) #plt.show() proposals.append(seg) return proposals def downsample_by_two(self): def impl(raw, weights, affinities, offsets): shape = weights.shape[0:2] new_shape = [s//2 for s in shape] new_raw = vigra.sampling.resize(raw.astype('float32'), new_shape) n_offsets = offsets.shape[0] new_offsets = offsets.astype('float')/2.0 new_weight_dict = dict() new_affinity_dict = dict() def f(o): if(o>0.0 and o<1.0): return 1 elif(o<0.0 and o>-1.0): return -1 else: return int(round(o)) for i_offset in range(n_offsets): weights_channel = weights[:,:,i_offset] affinity_channel = affinities[:,:,i_offset] new_weights_channel = vigra.sampling.resize(weights_channel.astype('float32'), new_shape) new_affinity_channel = vigra.sampling.resize(affinity_channel.astype('float32'), new_shape) offset = offsets[i_offset,:] nx,ny = new_offsets[i_offset,:] nx,ny = f(nx), f(ny) if (nx,ny) in new_weight_dict: new_weight_dict[(nx,ny)] += new_weights_channel new_affinity_dict[(nx,ny)] += new_affinity_channel else: new_weight_dict[(nx,ny)] = new_weights_channel new_affinity_dict[(nx,ny)] = new_affinity_channel print(offset,(nx,ny)) new_offsets = [ ] new_weights = [ ] new_affinities = [ ] for key in new_weight_dict.keys(): new_offsets.append(key) new_weights.append(new_weight_dict[key]) new_affinities.append(new_affinity_dict[key]) new_weights = numpy.array(new_weights) new_affinities = numpy.array(new_affinities) new_offsets = numpy.array(new_offsets) return new_raw, numpy.rollaxis(new_weights,0,3), numpy.rollaxis(new_affinities,0,3), new_offsets#numpy.swapaxes(new_offsets,0,1) new_raw, new_weights,new_affinities, new_offsets = impl(raw=self.raw,weights=self.weights, affinities=self.affinities, offsets=self.offsets) return PlmcObjective2D(raw=new_raw, affinities=new_affinities, weights=new_weights, offsets=new_offsets) def local_affinities_to_pixel(affinities, offsets): shape = affinities.shape[0:2] offset_dict = dict() for i in range(offsets.shape[0]): x,y = offsets[i,:] key = int(x),int(y) offset_dict[key] = i local_edges = [ (-1, 0), ( 1, 0), ( 0,-1), ( 0, 1) ] acc = numpy.zeros(shape) for local_edge in local_edges: #print("find",local_edge) if local_edge in offset_dict: acc += affinities[:,:, offset_dict[local_edge]] else: o_local_edge = tuple([-1*e for e in local_edge]) #print("missing",local_edge) if o_local_edge in offset_dict: #print(" using: ",o_local_edge) o_channel = affinities[:,:, offset_dict[o_local_edge]] padded_o_channel = numpy.pad(o_channel, 1, mode='reflect') if local_edge == (0,1): acc += padded_o_channel[1:shape[0]+1, 2:shape[1]+2] elif local_edge == (1,0): acc += padded_o_channel[2:shape[0]+2, 1:shape[1]+1] elif local_edge == (0,-1): acc += padded_o_channel[1:shape[0]+1, 0:shape[1]] elif local_edge == (1,0): acc += padded_o_channel[0:shape[0], 1:shape[1]+1] else: raise RuntimeError("todo") return acc def make_pixel_wise(affinities, offsets): shape = affinities.shape[0:2] big_shape = tuple([2*s for s in shape]) padding_size = int(numpy.abs(offsets).max())*2 acc = numpy.zeros(shape) for i in range(offsets.shape[0]): print(i) affinity_channel = affinities[:, :, i] affinity_channel = vigra.sampling.resize(affinity_channel, big_shape) padded_affinity_channel = numpy.pad(affinity_channel, padding_size, mode='reflect') sx = padding_size - offsets[i,0] sy = padding_size - offsets[i,1] p_affinity = padded_affinity_channel[sx: sx+big_shape[0], sy: sy+big_shape[0]] sigma = 0.3*numpy.sum(offsets[i,:]**2)**0.5 print("sigma",sigma) p_affinity = vigra.filters.gaussianSmoothing(p_affinity, sigma) acc += numpy.array(vigra.sampling.resize(p_affinity, shape)) return acc def solve_single_scale(objective, best_l=None): shape = objective.shape class Fuse(object): def __init__(self,objective, best_l=None): self.objective = objective self.best_l = best_l self.best_e = None if self.best_l is not None: self.best_e = objective.evaluate(best_l) G = nifty.graph.UndirectedGraph CCObj = G.LiftedMulticutObjective greedySolverFactory = CCObj.liftedMulticutGreedyAdditiveFactory() klSolverFactory = CCObj.liftedMulticutKernighanLinFactory() solverFactory = CCObj.chainedSolversFactory([greedySolverFactory, greedySolverFactory]) self.fm = nifty.graph.opt.lifted_multicut.PixelWiseLmcConnetedComponentsFusion2D( objective=self.objective, solver_factory=solverFactory) def fuse_with(self, labels): labels = numpy.squeeze(labels) labels = numpy.require(labels, requirements=['C']) if labels.ndim == 2: if self.best_l is None: self.best_l = labels else: #print("fuuuuu") self.best_l = self.fm.fuse( labels, numpy.require(self.best_l,requirements=['C']) ) else: labels = numpy.concatenate([self.best_l[:,:,None], labels],axis=2) self.best_l = self.fm.fuse(labels) self.best_e = objective.evaluate(self.best_l) print(self.best_e) fuse_inf = Fuse(objective=objective, best_l=best_l) local = local_affinities_to_pixel(objective.affinities, objective.offsets) def seeded_watersheds(sigma): #print("thesigma",sigma) hmap1 = vigra.filters.gaussianSmoothing(local, 0.2) hmap2 = vigra.filters.gaussianSmoothing(local, sigma) hmap1 += 0.03*hmap2 #print(nifty.segmentation.seededWatersheds) seg = nifty.segmentation.seededWatersheds(hmap1, method='edge_weighted', acc='interpixel') return seg def refine_watershed(labels,r, sigma): hmap1 = vigra.filters.gaussianSmoothing(local, 0.2) hmap2 = vigra.filters.gaussianSmoothing(local, sigma) hmap1 += 0.03*hmap2 zeros = numpy.zeros_like(labels) boundaries = skimage.segmentation.mark_boundaries(zeros, labels.astype('uint32'))[:,:,0]*255 #print(boundaries.min(),boundaries.max()) boundaries = vigra.filters.discDilation(boundaries.astype('uint8'),r).squeeze() new_seeds = labels + 1 where_b = numpy.where(boundaries==1) new_seeds[boundaries==255] = 0 seg,nseg = vigra.analysis.watersheds(hmap1.astype('float32'), seeds=new_seeds.astype('uint32')) seg = nifty.segmentation.connectedComponents(seg) return seg def refiner(labels,r): grid = numpy.arange(labels.size) + labels.max() + 1 grid = grid.reshape(labels.shape) zeros = numpy.zeros_like(labels) boundaries = skimage.segmentation.mark_boundaries(zeros, labels.astype('uint32'))[:,:,0]*255 #print(boundaries.min(),boundaries.max()) boundaries = vigra.filters.discDilation(boundaries.astype('uint8'),r).squeeze() new_seeds = labels.copy() where_mask = boundaries==255 new_seeds[where_mask] = grid[where_mask] return new_seeds proposals = [] proposals += objective.proposals_from_raw() proposals += objective.proposal_from_local_agglo(local) proposals += objective.proposal_from_raw_agglo() proposals += [seeded_watersheds(s) for s in (1.0, 2.0, 3.0)] #shuffle(proposals) print("fuabsf") for proposal in proposals: print("fuse with prop") fuse_inf.fuse_with(proposal) if False: print("refine watershed") for r in (1,2,3,4,5): for s in (1.0, 2.0, 3.0,5.0): p = refine_watershed(fuse_inf.best_l,r=r,sigma=s) fuse_inf.fuse_with(p) else: for r in (1,2,3,4): while(True): print("buja",r) best_e = float(fuse_inf.best_e) fuse_inf.fuse_with(refiner(fuse_inf.best_l, r=2)) if fuse_inf.best_e >= best_e: break #sys.exit() if True: for ps in (1,2,3,4): print("multi shiftey", ps) # shift for i in range(10): print("Si",i) proposals = [] best_e = float(fuse_inf.best_e) padded = numpy.pad(fuse_inf.best_l+1, ps+1, mode='constant', constant_values=0) for x in range(-ps,ps+1): for y in range(-ps,ps+1): labels = padded[ ps + x : ps + x + shape[0], ps + y : ps + y + shape[1] ] #labels = nifty.segmentation.connectedComponents(prop) proposals.append(labels[:,:,None]) if len(proposals) >= 6: proposals = numpy.concatenate(proposals, axis=2) fuse_inf.fuse_with(proposals) proposals = [] if len(proposals) >= 1: proposals = numpy.concatenate(proposals, axis=2) fuse_inf.fuse_with(proposals) if(fuse_inf.best_e >= best_e): break print("shiftey done ") else: print("shiftey") # shift ps = 2 for i in range(10): print("Si",i) proposals = [] best_e = float(fuse_inf.best_e) padded = numpy.pad(fuse_inf.best_l+1, ps+1, mode='constant', constant_values=0) for x in range(-ps,ps): for y in range(-ps,ps): labels = padded[ ps + x : ps + x + shape[0], ps + y : ps + y + shape[1] ] #labels = nifty.segmentation.connectedComponents(prop) proposals.append(labels) shuffle(proposals) for labels in proposals: fuse_inf.fuse_with(labels) if(fuse_inf.best_e >= best_e): break print("shiftey done ") return fuse_inf.best_l def solve_pyramid(objective, best_l=None): G = nifty.graph.UndirectedGraph CCObj = G.LiftedMulticutObjective solverFactory = CCObj.liftedMulticutGreedyAdditiveFactory() fm = nifty.graph.opt.lifted_multicut.PixelWiseLmcConnetedComponentsFusion2D(objective=objective, solver_factory=solverFactory) shape = objective.shape best_e = None if best_l is not None: best_e = objective.evaluate(best_l) # make a pyramid current = objective pyramid = [current] #while(current.shape[0]!=64): # print("jay") # current = current.downsample_by_two() # pyramid.append(current) #pyramid = reversed(pyramid) old_res = None for obj in pyramid: init = None if old_res is not None: print(old_res.shape) print('\n\n\n\n') init = vigra.sampling.resize(old_res.astype('float32'), obj.shape ,0).astype('int') old_res = solve_single_scale(obj, init) res = old_res return res def affinities_to_weights(affinities, offsets, beta=0.5): eps = 0.00001 affinities = numpy.clip(affinities, eps, 1.0-eps) weights = numpy.log((1.0-affinities)/(affinities)) + numpy.log((1.0-beta)/(beta)) return weights def affinities_to_better_weights(affinities, offsets, beta=0.5): weights = affinities.copy() eps = 0.00001 affinities = numpy.clip(affinities, eps, 1.0-eps) weights = numpy.log((1.0-affinities)/(affinities)) + numpy.log((1.0-beta)/(beta)) # long range weights[:,:,:] = -1.0*(affinities[:,:,:]-0.5) # local weighs weights[:,:,0] = 1.0 - affinities[:,:,0] weights[:,:,1] = 1.0 - affinities[:,:,1] weights *= numpy.sum(offsets**2,1)**0.5 return weights def affinities_lmc(raw, affinities, offsets, beta=0.5): # convert affinities to weights weights = affinities_to_better_weights(affinities=affinities, offsets=offsets, beta=0.5) #w = numpy.sum(offsets**2,axis=1) #weights *= w #weights[:,:,0] = 0 #weights[:,:,1] = 0 objective = PlmcObjective2D(raw=raw, affinities=affinities, weights=weights, offsets=offsets) return solve_pyramid(objective) if __name__ == "__main__": # load weighs and raw path_affinities = "/home/tbeier/nice_p/isbi_test_default.h5" #path_affinities = "/home/tbeier/nice_probs/isbi_test_default.h5" offsets = numpy.array([ [-1,0],[0,-1], [-9,0],[0,-9],[-9,-9],[9,-9], [-9,-4],[-4,-9],[4,-9],[9,-4], [-27,0],[0,-27],[-27,-27],[27,-27] ]) import h5py f5_affinities = h5py.File(path_affinities) affinities = f5_affinities['data'] z = 8 # get once slice affinities = numpy.rollaxis(affinities[:,z,:,:],0,3) affinities = numpy.require(affinities, requirements=['C']) import skimage.io #raw_path = "/home/tbeier/src/nifty/src/python/examples/multicut/NaturePaperDataUpl/ISBI2012/raw_test.tif" raw_path = '/home/tbeier/src/nifty/mysandbox/NaturePaperDataUpl/ISBI2012/raw_test.tif' raw = skimage.io.imread(raw_path) raw = raw[z,:,:] #raw = raw[200:64+200, 200:64+200] #affinities = affinities[200:64+200, 200:64+200,:] #t = 0.2 #affinities[affinities >= t ] = 1 #affinities[affinities < t ] = 0 print(raw.shape, affinities.shape) if False: import matplotlib.pyplot as plt for x in range(offsets.shape[0]): fig = plt.figure() ax1 = fig.add_subplot(2,1,1) ax1.imshow(raw) ax2 = fig.add_subplot(2,1,2) ax2.imshow(affinities[:,:,x]) plt.show() sys.exit() res = affinities_lmc(raw=raw, affinities=affinities, offsets=offsets, beta=0.5) plt.imshow(nifty.segmentation.segmentOverlay(raw, res, showBoundaries=False)) plt.show() plt.imshow(nifty.segmentation.markBoundaries(raw, res, color=(1,0,0))) plt.show()
#!/usr/bin/env python #************************************************************************ # Codelet Tuning Infrastructure # Copyright (C) 2010-2015 Intel Corporation, CEA, GENCI, and UVSQ # # 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/>. #************************************************************************ # Authors: Franck Talbart, Mathieu Bordet, Nicolas Petit # This script generates the dynamic MAQAO CQA schema import cti, ctr from cti_hapi import entry, util import json, sys, os, subprocess uid = cti.CTI_UID(sys.argv[1]) path_plugin = ctr.ctr_plugin_get_path_by_uid(cti.CTR_ENTRY_PLUGIN, uid) #--------------------------------------------------------------------------- def update_file(filename, cmd, result): infile, outfile = entry.load_defaults(uid) if not outfile["init"].params.has_key(result["name"]): file_input = open(filename) ji = json.load(file_input) with open(filename, 'w') as outfile: ji[cmd]["params"].insert(4, result) json.dump(ji, outfile, indent=4) #--------------------------------------------------------------------------- maqao_bin = os.path.join(cti.cti_plugin_config_get_value(cti.THIRD_PARTY_DIR), "maqao", "maqao") sys.stdout.flush() child = subprocess.Popen([maqao_bin, "cqa", "-cti", "-ext"], shell=False) output = child.communicate() rc = child.returncode if rc != 0: raise Exception("Error with MAQAO CQA while getting the JSON file") file_js = open("x86.json") jr = json.load(file_js) result = { cti.META_ATTRIBUTE_NAME: "matrix_loop_results", cti.META_ATTRIBUTE_TYPE: "MATRIX", cti.META_ATTRIBUTE_DESC: "MAQAO CQA loop results.", cti.META_ATTRIBUTE_LONG_DESC: "The MAQAO CQA loop results from the associated compilation.", cti.META_ATTRIBUTE_MATRIX_COLUMN_NAMES: [], cti.META_ATTRIBUTE_MATRIX_COLUMN_TYPES: [], cti.META_ATTRIBUTE_MATRIX_COLUMN_DESCS: [], cti.META_ATTRIBUTE_MATRIX_COLUMN_LONG_DESCS: [], cti.META_ATTRIBUTE_OPTIONAL: True, cti.META_ATTRIBUTE_MATRIX_GENERATED: True } for archi_k, archi_v in jr.items(): #Moving the metrics into a list in order to sort them metric_list = [] for metric_k, metric_v in archi_v.items(): metric_list.append({ cti.META_ATTRIBUTE_NAME: metric_k, cti.META_ATTRIBUTE_TYPE: metric_v["type"], cti.META_ATTRIBUTE_DESC: metric_v["description"] }) sorted_metrics = sorted(metric_list, key=lambda k: k[cti.META_ATTRIBUTE_NAME]) for m in sorted_metrics: param = util.column_name_replace(m[cti.META_ATTRIBUTE_NAME].strip()) result[cti.META_ATTRIBUTE_MATRIX_COLUMN_NAMES].append("%s_%s" % (archi_k, param)) result[cti.META_ATTRIBUTE_MATRIX_COLUMN_TYPES].append(m[cti.META_ATTRIBUTE_TYPE]) result[cti.META_ATTRIBUTE_MATRIX_COLUMN_DESCS].append(m[cti.META_ATTRIBUTE_NAME]) result[cti.META_ATTRIBUTE_MATRIX_COLUMN_LONG_DESCS].append(m[cti.META_ATTRIBUTE_DESC]) if archi_k == "common": #Adding port balancing column result[cti.META_ATTRIBUTE_MATRIX_COLUMN_NAMES].append("%s_port_balancing" % archi_k) result[cti.META_ATTRIBUTE_MATRIX_COLUMN_TYPES].append("TEXT") result[cti.META_ATTRIBUTE_MATRIX_COLUMN_DESCS].append("The port balancing") result[cti.META_ATTRIBUTE_MATRIX_COLUMN_LONG_DESCS].append("The port balancing.") #Adding multiple path column result[cti.META_ATTRIBUTE_MATRIX_COLUMN_NAMES].append("%s_is_multiple_path" % archi_k) result[cti.META_ATTRIBUTE_MATRIX_COLUMN_TYPES].append("BOOLEAN") result[cti.META_ATTRIBUTE_MATRIX_COLUMN_DESCS].append("Is multiple path") result[cti.META_ATTRIBUTE_MATRIX_COLUMN_LONG_DESCS].append("True if the loop has multiple path.") update_file(os.path.join(path_plugin, cti.cti_plugin_config_get_value(cti.PLUGIN_DEFAULT_INPUT_FILENAME)), "update", result) update_file(os.path.join(path_plugin, cti.cti_plugin_config_get_value(cti.PLUGIN_DEFAULT_OUTPUT_FILENAME)), "init", result) os.remove("x86.json")
#!/usr/bin/python # Program to detect periodic payments and spot gaps in them import argparse import csv import datetime import os import qsutils # See notes in finconv.py for config file format secs_per_day = 24 * 60 * 60 def finperiodic_setup(app_data, input_format): return ['payee'], {} def finperiodic_row(timestamp, row, output_rows, scratch): timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S") payee = row['payee'] amount = row.get('amount', row.get('debits', row.get('credits'))) if payee in scratch: scratch[payee][timestamp] = amount else: scratch[payee] = {timestamp: amount} def finperiodic_tidyup(columns, rows, scratch): for payee, transactions in scratch.iteritems(): # print payee, transactions dates = sorted(transactions.keys()) prev = dates[0] intervals = [] for when in dates[1:]: interval = int((when-prev).total_seconds() / secs_per_day) if interval > 0: # ignore further transactions on the same day intervals.append(interval) prev = when if len(intervals) > 1: counts = {} for interval in intervals: counts[interval] = counts.get(interval, 0) + 1 print payee for k in sorted(counts.keys()): print " ", k, counts[k] total = sum(counts) approx_weekly = sum(counts[6:8]) / total approx_monthly = sum(counts[26:34]) / total print "approx_weekly", approx_weekly print "approx_monthly", approx_monthly return None, None def main(): parser = qsutils.program_argparser() parser.add_argument("input_files", nargs='*') args = parser.parse_args() config = qsutils.program_load_config(args) parser.add_argument("-o", "--output") parser.add_argument("-f", "--format", default=None) parser.add_argument("input_file") args = parser.parse_args() # todo: deduce format of input file; should normally be financisto, or have similar data qsutils.process_fin_csv({'args': args, 'config': qsutils.load_config( args.verbose, None, None, qsutils.program_load_config(args), *args.config or ())}, finperiodic_setup, finperiodic_row, finperiodic_tidyup) if __name__ == "__main__": main()
#!/usr/bin/env python2 # - * - coding=utf-8 - * - # Copyright (c) 2015, Sergiusz Bazanski <q3k@q3k.org> # Copyright (c) 2015, Remigiusz Marcinkiewicz <enleth@enleth.com> # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. import datetime import enum import json import re from sqlalchemy.orm import subqueryload_all from flask import g from webapp import app, db, mc, cache_enabled import directory class APIKey(db.Model): id = db.Column(db.Integer, primary_key=True) secret = db.Column(db.String(64)) member = db.Column(db.Integer, db.ForeignKey("member.id")) description = db.Column(db.Text) class MemberTransfer(db.Model): __tablename__ = "member_transfer" id = db.Column(db.Integer, primary_key=True) member = db.Column(db.Integer, db.ForeignKey("member.id")) transfer_id = db.Column(db.Integer, db.ForeignKey("transfer.id")) year = db.Column(db.Integer) month = db.Column(db.Integer) transfer = db.relationship("Transfer", backref="member_transfers") def __init__(self, _id, year, month, transfer): self.id = _id self.year = year self.month = month self.transfer = transfer class PaymentStatus(enum.Enum): never_paid = 1 # never paid membership fees unpaid = 2 # more than 3 fees unapid okay = 3 # fees paid class PaymentPolicy(enum.Enum): normal = "Normal" extended = "Extended Grace Period" potato = "Potato" disabled = "Disabled" class MembershipType(enum.Enum): fatty = "Fatty" starving = "Starving" class Member(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True) type = db.Column(db.Enum("starving", "fatty", name="member_types")) transfers = db.relationship("MemberTransfer",order_by=[db.asc(MemberTransfer.year), db.asc(MemberTransfer.month)]) # old field active = db.Column(db.Boolean) api_keys = db.relationship("APIKey") join_year = db.Column(db.Integer) join_month = db.Column(db.Integer) ldap_username = db.Column(db.String(64), unique=True) # Normal - standard 3 months grace period # Extended Grace Period - do not shut off account after grace period # Potato - do not ever shut off account, report falsified payment status # Disabled - manual disable override, regardless of payment extra payment_policy = db.Column(db.Enum(*[p.value for p in PaymentPolicy.__members__.values()], name='payment_policy_types')) preferred_email = db.Column(db.String(64)) def mt_covers(self, mt): """For transfer view - given an mt, should we rowspan?""" if mt not in self.transfers: return None ix = self.transfers.index(mt) if ix != 0: # check if the previous mt was covered by the same transfer if self.transfers[ix-1].transfer.uid == mt.transfer.uid: return None # check how many next mts use the same transfer rowspan = 0 for ix2 in range(ix+1, len(self.transfers)): if self.transfers[ix2].transfer.uid == mt.transfer.uid: rowspan += 1 else: break if rowspan == 0: return None else: return rowspan + 1 @classmethod def get_members(kls, deep=False): """Gets all members as an SQLAlchemy query. @param(deep) - whether to do a subqueryload_all and load all transfer data """ if deep: return kls.query.options(subqueryload_all(kls.transfers, MemberTransfer.transfer)).order_by(kls.username) else: return kls.query.order_by(kls.username) def _yearmonth_increment(self, ym): y, m = ym y2, m2 = y, m+1 if m2 > 12: y2 += 1 m2 = 1 return (y2, m2) def _yearmonth_scalar(self, ym): y, m = ym return y * 12 + (m - 1) def _get_status_uncached(self): now_date = datetime.datetime.now() now = now_date.year * 12 + (now_date.month - 1) del now_date status = {} status['ldap_username'] = self.ldap_username status['username'] = self.username status['type'] = self.type status['payment_policy'] = self.payment_policy # First check - did we actually get any transfers? if not self.transfers or self.transfers[0].transfer.uid == app.config['DUMMY_TRANSFER_UID']: status['payment_status'] = PaymentStatus.never_paid.value status['months_due'] = None status['last_paid'] = (None, None) if self.join_year is not None and self.join_month is not None: status['joined'] = (self.join_year, self.join_month) status['next_unpaid'] = self._yearmonth_increment(status['joined']) else: status['joined'] = (None, None) status['next_unpaid'] = (None, None) status['left'] = False self._apply_judgement(status) return status # Use the join date from SQL, if available if self.join_year is not None and self.join_month is not None: joined = (self.join_year, self.join_month) else: joined = (self.transfers[0].year, self.transfers[0].month) joined_scalar = self._yearmonth_scalar(joined) status['joined'] = joined most_recent_transfer = (0, 0) unpaid_months = 0 # Iterate over all payments and figure out how much months are unpaid previous_transfer = (0, 0) previous_uid = None active_payment = True for mt in self.transfers: this_transfer = (mt.year, mt.month) this_scalar = self._yearmonth_scalar(this_transfer) this_uid = mt.transfer.uid previous_scalar = self._yearmonth_scalar(previous_transfer) most_recent_scalar = self._yearmonth_scalar(most_recent_transfer) # Is this transfer a „not a member anymore” transfer? if this_uid == app.config['DUMMY_TRANSFER_UID']: active_payment = False continue # Is this the first transfer? See if it was done on time if previous_uid is None: unpaid_months += (this_scalar - joined_scalar) # Apply any missing payments if active_payment and previous_uid is not None: unpaid_months += (this_scalar - previous_scalar) - 1 # Is this the most recent payment? if this_scalar > most_recent_scalar: most_recent_scalar = this_scalar most_recent_transfer = this_transfer active_payment = True previous_transfer = this_transfer previous_uid = this_uid # Apply missing payments from now if active_payment: previous_scalar = self._yearmonth_scalar(previous_transfer) unpaid_months += (now - previous_scalar) status['months_due'] = unpaid_months status['payment_status'] = PaymentStatus.okay.value if unpaid_months < 4 else PaymentStatus.unpaid.value status['last_paid'] = most_recent_transfer status['left'] = not active_payment if not active_payment: status['next_unpaid'] = (None, None) else: status['next_unpaid'] = self._yearmonth_increment(status['last_paid']) self._apply_judgement(status) return status def get_list_email(self): if self.preferred_email: return self.preferred_email return '{}@hackerspace.pl'.format(self.ldap_username) def get_contact_email(self): if self.preferred_email: return self.preferred_email mra = directory.get_member_fields(g.ldap, self.ldap_username, 'mailRoutingAddress') mra = mra['mailRoutingAddress'] if mra: return mra else: return '{}@hackerspace.pl'.format(self.ldap_username) def get_status(self): """It's better to call this after doing a full select of data.""" cache_key = 'kasownik-payment_status-{}'.format(self.username) cache_data = mc.get(cache_key) if cache_data and cache_enabled: data = json.loads(cache_data) return data else: cache_data = self._get_status_uncached() mc.set(cache_key, json.dumps(cache_data)) return cache_data def _apply_judgement(self, status): """Check your priviledge, you cisnormative shitlord!""" if status['left']: status['judgement'] = False return policy = status['payment_policy'] if policy == 'Normal': if status['payment_status'] == PaymentStatus.okay.value and status['last_paid'][0] is not None: status['judgement'] = True else: status['judgement'] = False elif policy == 'Extended Grace Period': status['judgement'] = True elif policy == 'Potato': status['judgement'] = True status['months_due'] = 0 else: status['judgement'] = False def get_months_due(self): status = self.get_status() return status['months_due'] def get_last_paid(self): status = self.get_status() return status['last_paid'] def get_next_unpaid(self): status = self.get_status() return status['next_unpaid'] def __init__(self, _id, _username, _type, _active): self.id = _id self.username = _username self.type = _type self.active = _active now_date = datetime.datetime.now() self.join_year = now_date.year self.join_month = now_date.month self.ldap_username = _username self.payment_policy = PaymentPolicy.normal.value class Transfer(db.Model): id = db.Column(db.Integer, primary_key=True) uid = db.Column(db.String(128)) account_from = db.Column(db.String(32)) name_from = db.Column(db.String(256)) amount = db.Column(db.Integer) title = db.Column(db.String(256)) date = db.Column(db.Date) ignore = db.Column(db.Boolean) def __init__(self, _id, _uid, _account_from, _name_from, _amount, _title, _date, _ignore): self.id = _id self.uid = _uid self.account_from = _account_from self.name_from = _name_from self.amount = _amount self.title = _title self.date = _date self.ignore = _ignore def get_short_uid(self): return self.uid[:16] def parse_title(self): m = re.match(ur"^([a-z0-9\-_\.]+) *\- *(fatty|starving|superfatty) *\- *([0-9a-z\-_ąężźćóżłśń \(\),/\.]+$)", self.title.strip().lower()) if not m: return (None, None, None) member, _type, title = m.group(1), m.group(2), m.group(3) if title in [u"składka", u"opłata", u"opłata miesięczna", "skladka"]: return (member, _type, None) return member, _type, title MATCH_OK, MATCH_WRONG_TYPE, MATCH_NO_USER, MATCH_UNPARSEABLE = range(4) def get_matchability(self): title = self.parse_title() if not title[0]: return self.MATCH_UNPARSEABLE, self.title member_name = title[0] member = Member.query.filter_by(username=member_name).first() if not member: return self.MATCH_NO_USER, member_name if (title[1] == 'starving' and self.amount > 50) or (title[1] == 'fatty' and self.amount > 100): return self.MATCH_WRONG_TYPE, member if title[2]: return self.MATCH_WRONG_TYPE, member return self.MATCH_OK, member
# Copyright (c) 2011 Citrix Systems, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from sqlalchemy import * from migrate import * from nova import log as logging meta = MetaData() # Table stub-definitions # Just for the ForeignKey and column creation to succeed, these are not the # actual definitions of instances or services. # volumes = Table('volumes', meta, Column('id', Integer(), primary_key=True, nullable=False), ) # # New Tables # flavors = Table('sm_flavors', meta, Column('created_at', DateTime(timezone=False)), Column('updated_at', DateTime(timezone=False)), Column('deleted_at', DateTime(timezone=False)), Column('deleted', Boolean(create_constraint=True, name=None)), Column('id', Integer(), primary_key=True, nullable=False), Column('label', String(length=255, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False)), Column('description', String(length=255, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False)), ) backend = Table('sm_backend_config', meta, Column('created_at', DateTime(timezone=False)), Column('updated_at', DateTime(timezone=False)), Column('deleted_at', DateTime(timezone=False)), Column('deleted', Boolean(create_constraint=True, name=None)), Column('id', Integer(), primary_key=True, nullable=False), Column('flavor_id', Integer(), ForeignKey('sm_flavors.id'), nullable=False), Column('sr_uuid', String(length=255, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False)), Column('sr_type', String(length=255, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False)), Column('config_params', String(length=2047, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False)), ) sm_vol = Table('sm_volume', meta, Column('created_at', DateTime(timezone=False)), Column('updated_at', DateTime(timezone=False)), Column('deleted_at', DateTime(timezone=False)), Column('deleted', Boolean(create_constraint=True, name=None)), Column('id', Integer(), ForeignKey('volumes.id'), primary_key=True, nullable=False), Column('backend_id', Integer(), ForeignKey('sm_backend_config.id'), nullable=False), Column('vdi_uuid', String(length=255, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False)), ) # # Tables to alter # # (none currently) def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; # bind migrate_engine to your metadata meta.bind = migrate_engine for table in (flavors, backend, sm_vol): try: table.create() except Exception: logging.info(repr(table))
import gdal class GDALCalculateArea: """ Problem: How do we compute zonal stats from a raster given only a bounding box from geoserver? Geoserver Bounding Box = [LEFT,BOTTOM,RIGHT,TOP] So, corner coordinates = A:(LEFT,TOP) B:(LEFT,BOTTOM) C:(RIGHT,BOTTOM) D:(RIGHT,TOP) * ------------------------> * | A | | D | ___|__________________|____ | | | | | | | | | | | | | | | | | | | ___|__________________|____ v B | | C * | | Main restriction is that GDAL cannot read negative dimensions, so our only option is to read left to right and top to bottom. (A) will be our origin, (B) will be our row extent and (D) will be our column extent. From there, GDAL will return a 2D array of values that we can math and science with. Note: GDAL reads the value in the top-left corner of a cell as the value of the entire cell. This is hardcoded into GDAL and cannot be changed (unless you want to learn C) """ def __init__(self, filepath): #Set GeoTiff driver self.driver = gdal.GetDriverByName("GTiff") self.driver.Register() #Open raster and read number of rows, columns, bands self.dataset = gdal.Open(filepath) self.cols = self.dataset.RasterXSize self.rows = self.dataset.RasterYSize self.allBands = self.dataset.RasterCount self.band = self.dataset.GetRasterBand(1) #Get raster georeference info self.transform = self.dataset.GetGeoTransform() self.xOrigin = self.transform[0] self.yOrigin = self.transform[3] self.pixelWidth = self.transform[1] self.pixelHeight = self.transform[5] #Take bbox and make a dictionary tailored to this class #out of ift def get_bbox_corner_coords(self, boundingbox): bbox = boundingbox keys = ['origin','bottomleft','bottomright','topright'] bbox_tuples = [(bbox[0],bbox[3]), (bbox[0],bbox[1]), (bbox[2],bbox[1]), (bbox[2],bbox[3])] return dict(zip(keys,bbox_tuples)) def compute_x_offset(self, xcoord): return int((xcoord - self.xOrigin)/ self.pixelWidth) def compute_y_offset(self, ycoord): return int((ycoord - self.yOrigin)/ self.pixelHeight) def compute_pixel_extent(self): keys = ['pixels_x','pixels_y'] coords = self.get_bbox_corner_coords(bbox) originX = coords['origin'][0] originY = coords['origin'][1] extentX = coords['topright'][0] - originX extentY = coords['bottomleft'][1] - originY pixelExtentX = int(extentX / self.pixelWidth) pixelExtentY = int(extentY / self.pixelHeight) return dict(zip(keys,[pixelExtentX,pixelExtentY])) def get_raster_attrs(self): return self.band.ReadAsArray(0,0,self.cols,self.rows) def get_zone_mean(self, dataset): data = dataset flat = [int(a) for b in data for a in b] length = len(flat) mean = reduce(lambda x, y: x + y, flat)/ length return mean def get_cell_value(self, xcoord, ycoord): x = self.compute_x_offset(xcoord) y = self.compute_y_offset(ycoord) data = self.band.ReadAsArray(x,y,1,1) return data[0,0] def get_raster_zone(self, boundingbox): coords = self.get_bbox_corner_coords(boundingbox) pixelExtents = self.compute_pixel_extent() originX = self.compute_x_offset(coords['origin'][0]) originY = self.compute_y_offset(coords['origin'][1]) extentX = pixelExtents['pixels_x'] extentY = pixelExtents['pixels_y'] data = self.band.ReadAsArray(originX,originY,extentX,extentY) return data def get_mean(self, geom): if len(geom) = 2: xcoord = geom[0] ycoord = geom[1] return self.get_cell_value(xcoord,ycoord) elif len(geom) = 4: data = self.get_raster_zone(geom) return self.get_zone_mean(data) else: error_message = """Sorry, this function only supports point geometries in the form [x,y] and bounding box queries in the form [left,bottom,right,top].""" return error_message
from objects.orobject import OrObject from collections import namedtuple from libbuiltin import typeof from objects.number import Number import objects.constants as constants import sys overrides = { "stdin": "", # oranj script - "count": "q", # in `who` "digits": "n", # csplit "exit": "x", "extract": "x", "zip": "z", "gzip": "z", "compress": "z", "literal": "N", # ls } class CLIArgs: def __init__(self): self.mandatory = [] self.short = {} self.long = {} self.dump = None self.kwdump = None def shortarg(arg): return [arg[0], arg[0].swapcase] + (overrides[arg] if arg in overrides else []) def getargs(intp, arglist): args = CLIArgs() for i in arglist: if i[0] == "ARG": args.mandatory.append(i[1]) args.long[i[1]] = "str" for short in shortarg(i[1]): if short not in args.short: args.short[short] = i[1] break elif i[0] == "DEFARG": args.long[i[1]] = typeof(intp.run(i[2])) for short in shortarg(i[1]): if short not in args.short: args.short[short] = i[1] break elif i[0] == "UNWRAPABLE": args.mandatory.append(i[1]) if not args.dump: args.dump = i[1] elif i[0] == "UNWRAPABLEKW": if not args.kwdump: args.kwdump = i[1] return args def evalval(type, val): if type == "int": return Number(val, intonlystr=True) elif type == "num": return Number(val) elif type == "list": return OrObject.from_py(val.split(",")) elif type == "bool": return constants.true elif type == "str": return OrObject.from_py(val) else: raise TypeError, "`%s` type not supported for command-line \ arguments" % type def parseargs(args, schema): passargs = {} takingopts = True errors = False setval = "" if schema.dump: passargs[schema.dump] = [] # Not OrObjected yet if schema.kwdump: passargs[schema.kwdump] = {} # Also not OO'd yet for i in args: if i.startswith("--") and len(i) > 2 and takingopts: kw, t, val = i[2:].partition("=") if kw not in schema.long and not schema.kwdump: print "ERROR: Unknown option `%s`" % kw errors = True continue elif schema.kwdump: passargs[schema.kwdump][kw] = evalval("str", val) continue if kw in schema.mandatory: schema.mandatory.remove(kw) passargs[kw] = evalval(schema.long[kw], val) elif i == "--" and takingopts: takingopts = False elif i.startswith("-") and takingopts: key = i[1:2] val = i[2:] if key not in schema.short: print "ERROR: Unknown option `%s`" % key errors = True continue elif schema.kwdump: setval = ":kwdump:" continue if schema.short[key] in schema.mandatory: schema.mandatory.remove(schema.short[key]) if schema.long[schema.short[key]] == "bool": passargs[schema.short[key]] = constants.true elif val: passargs[schema.short[key]] = evalval(schema.long[schema.short[key]], val) else: setval = schema.short[key] elif setval: if setval == ":kwdump:": passargs[schema.kwdump][setval] = evalval("str", val) else: passargs[setval] = evalval(schema.long[setval], i) setval = "" else: try: kw = schema.mandatory[0] except IndexError: print "ERROR: Too many arguments" errors = True continue if kw == schema.dump: passargs[schema.dump].append(i) takingopts = False continue passargs[kw] = evalval(schema.long[kw], i) schema.mandatory.pop(0) if schema.dump: passargs[schema.dump] = OrObject.from_py(passargs[schema.dump]) if schema.kwdump: passargs[schema.kwdump] = OrObject.from_py(passargs[schema.kwdump]) if len(schema.mandatory) and schema.mandatory[0] != schema.dump: m = len(schema.mandatory) - (1 if schema.dump in schema.mandatory else 0) print "Arguments Missing: " + ", ".join(map(lambda x: "`%s`"%x, schema.mandatory)) print "ERROR: Missing %d arguments; consult --help for command syntax" % m errors = True if setval: print "ERROR: Expecting value for argument `%s`" % setval errors = True if errors: sys.exit(1) return passargs def run_main(intp, args): if "--help" in args or "-h" in args or "-?" in args \ and "$$help" in intp.curr: __help = intp.curr["$$help"] if typeof(__help) == "str": print __help else: __help() elif "--version" in args and "$$version" in intp.curr: __version = intp.curr["$$version"] if typeof(__version) == "str": print __version else: __version() else: main = intp.curr["$$main"] schema = getargs(intp, main.arglist) kwargs = parseargs(args, schema) main(**kwargs) def run(intp, args, wrapfn=lambda fn, i, glob: fn()): if "$$main" in intp.curr: wrapfn(lambda: run_main(intp, args), intp, globals()) #run_main(intp, args) return
from toee import * import tpdp def OnBeginSpellCast( spell ): print "Ray of Cluminess OnBeginSpellCast" print "spell.target_list=", spell.target_list print "spell.caster=", spell.caster, " caster.level= ", spell.caster_level def OnSpellEffect( spell ): print "Ray of Clumsiness OnSpellEffect" def OnBeginRound( spell ): print "Ray of Clumsiness OnBeginRound" def OnBeginProjectile( spell, projectile, index_of_target ): print "Ray of Clumsiness OnBeginProjectile" #spell.proj_partsys_id = game.particles( 'sp-Ray of Enfeeblement', projectile ) projectile.obj_set_int( obj_f_projectile_part_sys_id, game.particles( 'sp-Ray of Enfeeblement', projectile ) ) def OnEndProjectile( spell, projectile, index_of_target ): print "Ray of Clumsiness OnEndProjectile" target_item = spell.target_list[0] dam_bonus = min( 5, spell.caster_level / 2 ) dam_amount = spell.roll_dice_with_metamagic(1, 6, dam_bonus) dam_amount = -dam_amount print "amount=", dam_amount spell.duration = 10 * spell.caster_level game.particles_end( projectile.obj_get_int( obj_f_projectile_part_sys_id ) ) if spell.caster.perform_touch_attack( target_item.obj ) & D20CAF_HIT: target_item.obj.float_mesfile_line( 'mes\\spell.mes', 20022, tf_red ) target_item.obj.condition_add_with_args( 'sp-Cats Grace', spell.id, spell.duration, dam_amount ) target_item.partsys_id = game.particles( 'sp-Ray of Enfeeblement-Hit', target_item.obj ) else: # missed target_item.obj.float_mesfile_line( 'mes\\spell.mes', 30007 ) game.particles( 'Fizzle', target_item.obj ) spell.target_list.remove_target( target_item.obj ) spell.spell_end( spell.id ) def OnEndSpellCast( spell ): print "Ray of Clumsiness OnEndSpellCast"
#! /usr/bin/env python # -*- coding: utf-8 -*- # # Interpreter version: python 2.7 # # Imports ===================================================================== import transaction from BTrees.OOBTree import OOBTree from BTrees.OOBTree import OOTreeSet from BTrees.OOBTree import intersection from zeo_connector import transaction_manager from zeo_connector.examples import DatabaseHandler import settings # Exceptions ================================================================== class InvalidType(Exception): """ Raised in case that object you are trying to store doesn't have required interface. """ class UnindexableObject(Exception): """ Raised in case, that object doesn't have at least one attribute set. """ # Functions & classes ========================================================= class StorageHandler(DatabaseHandler): """ Object database with indexing by the object attributes. Each stored object is required to have following properties: - indexes (list of strings) - project_key (string) For example:: class Person(Persistent): def __init__(self, name, surname): self.name = name self.surname = surname @property def indexes(self): return [ "name", "surname", ] @property def project_key(self): return PROJECT_KEY Note: I suggest to use properties, because that way the values are not stored in database, but constructed at request by the property methods. """ def __init__(self, project_key, conf_path=settings.ZEO_CLIENT_PATH): """ Constructor. Args: project_key (str): Project key which is used for the root of DB. conf_path (str): Path to the client zeo configuration file. Default :attr:`.settings.ZEO_CLIENT_PATH`. """ super(self.__class__, self).__init__( conf_path=conf_path, project_key=project_key ) @transaction_manager def _zeo_key(self, key, new_type=OOBTree): """ Get key from the :attr:`zeo` database root. If the key doesn't exist, create it by calling `new_type` argument. Args: key (str): Key in the root dict. new_type (func/obj): Object/function returning the new instance. Returns: obj: Stored object, or `new_type`. """ zeo_key = self.zeo.get(key, None) if zeo_key is None: zeo_key = new_type() self.zeo[key] = zeo_key return zeo_key def _get_db_fields(self, obj): """ Return list of database dictionaries, which are used as indexes for each attributes. Args: cached (bool, default True): Use cached connection to database. Returns: list: List of OOBTree's for each item in :attr:`.COMMON_FIELDS`. """ for field in obj.indexes: yield field, self._zeo_key(field) def _check_obj_properties(self, pub, name="pub"): """ Make sure, that `pub` has the right interface. Args: pub (obj): Instance which will be checked. name (str): Name of the instance. Used in exception. Default `pub`. Raises: InvalidType: When the `pub` is not instance of `obj_type`. """ if not hasattr(pub, "indexes"): raise InvalidType("`%s` doesn't have .indexes property!" % name) if not pub.indexes: raise InvalidType("`%s.indexes` is not set!" % name) if not hasattr(pub, "project_key"): raise InvalidType( "`%s` doesn't have .project_key property!" % name ) if not pub.project_key: raise InvalidType("`%s.project_key` is not set!" % name) def _put_into_indexes(self, obj): """ Put publication into all indexes. Attr: obj (obj): Indexable object. Raises: UnindexableObject: When there is no index (property) which can be used to index `obj` in database. """ no_of_used_indexes = 0 for field_name, db_index in list(self._get_db_fields(obj)): attr_value = getattr(obj, field_name) if attr_value is None: # index only by set attributes continue container = db_index.get(attr_value, None) if container is None: container = OOTreeSet() db_index[attr_value] = container container.insert(obj) no_of_used_indexes += 1 # make sure that atleast one `attr_value` was used if no_of_used_indexes <= 0: raise UnindexableObject( "You have to use atleast one of the identificators!" ) def store_object(self, obj): """ Save `obj` into database and into proper indexes. Attr: obj (obj): Indexable object. Raises: InvalidType: When the `obj` doesn't have right properties. Unindexableobjlication: When there is no indexes defined. """ self._check_obj_properties(obj) with transaction.manager: self._put_into_indexes(obj) def _get_subset_matches(self, query): """ Yield publications, at indexes defined by `query` property values. Args: query (obj): Object implementing proper interface. Yields: list: List of matching publications. """ for field_name, db_index in self._get_db_fields(query): attr = getattr(query, field_name) if attr is None: # don't use unset attributes continue results = db_index.get(attr, OOTreeSet()) if results: yield results def search_objects(self, query): """ Return list of objects which match all properties that are set (``not None``) using AND operator to all of them. Example: result = storage_handler.search_objects( DBPublication(isbn="azgabash") ) Args: query (obj): Object implementing proper interface with some of the properties set. Returns: list: List of matching objects or ``[]`` if no match was found. Raises: InvalidType: When the `query` doesn't implement required properties. """ self._check_obj_properties(query, "query") # AND operator between results final_result = None for result in self._get_subset_matches(query): if final_result is None: final_result = result continue final_result = intersection(final_result, result) # if no result is found, `final_result` is None, and I want [] if not final_result: return [] return list(final_result)
# pkmin.py # # Copyright (C) 2014 Leo Osvald (leo.osvald@gmail.com) # # This file is part of PKMin. # # PKMin 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. # # PKMin 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 YOUR PROGRAM NAME. If not, see <http://www.gnu.org/licenses/>. from __future__ import print_function import argparse import binascii import struct import subprocess import sys from collections import namedtuple from getpass import getpass from hashlib import pbkdf2_hmac from os.path import commonprefix verbosity = 0 _PGP_VERSION_OCTET = b"\x04" # Paperkey-related constants _PAPERKEY_FMT_VERSION_OCTET = b"\x00" # paperkey format version length _FINGERPRINT_OCTET_CNT = 20 # the current version of OpenPGP uses 20 octets _SECRET_LEN_OCTET_CNT = 2 # length preceding each key printed by paperkey _SECRET_LEN_PACK_FMT = ">H" # format passed to pack struct.(un)pack _CRC_OCTET_CNT = 3 # trailing checksum length produced by paperkey _CRC_FAKED_OCTETS = b"\x00" * _CRC_OCTET_CNT # a fake CRC24 _SECRET_COMMON_PREFIX_OCTET_CNT = 13 # likely shared with 1st subkey (E) _SECRET_PLAINTEXT_TAG = b"\x00" _SECRET_PLAINTEXT_CHKSUM_OCTET_CNT = 2 _SECRET_PLAINTEXT_CHKSUM_PACK_FMT = ">H" # format passed to pack struct.(un)pack _SECRET_S2K_TAGS = (b"\xFE", b"\xFF") _SECRET_S2K_MODE_ITER_SALTED_OCTET = b"\x03" _SECRET_S2K_SALT_IND = 4 # index at which the non-stripped S2K salt begins _SECRET_S2K_SALT_OCTET_CNT = 8 def _reversed_dict(d): return dict((v, k) for k, v in d.items()) class S2KCountCodec(object): @property def _expbias(self): return 6 @property def _shift(self): return 4 @property def _mask(self): return (1 << self._shift) - 1 def decode(self, octet): c = ord(octet) return (1 + self._mask + (c & self._mask)) << ( (c >> self._shift) + self._expbias) @classmethod def _to_bytes(cls, ordinal): return bytes(bytearray([ordinal])) def encode(self, count): # TODO: reduce time complexity to O(1) by reverse-engineering decode() lo, hi = 0x00, 0xFF while lo < hi: # round up octet using binary search mid = (lo + hi - 1) // 2 mid_count = self.decode(self._to_bytes(mid)) if mid_count >= count: hi = mid else: lo = mid + 1 return self._to_bytes(hi) S2K_CIPHER_ALGOS = { # 'IDEA': b"\x01", # discouraged as it is broken # '3DES': b"\x02", # discouraged as it is broken 'CAST5': b"\x03", # 'BLOWFISH': b"\x04", # discouraged as there is a class of known weak keys 'AES128': b"\x07", 'AES192': b"\x08", 'AES256': b"\x09", 'TWOFISH': b"\x0A", } S2K_CIPHER_OCTETS = _reversed_dict(S2K_CIPHER_ALGOS) S2K_DIGEST_ALGOS = { # 'MD5': b"\x01", # deprecated 'SHA1': b"\x02", 'RIPEMD160': b"\x03", 'SHA256': b"\x08", 'SHA384': b"\x09", 'SHA512': b"\x0A", 'SHA224': b"\x0B", } S2K_DIGEST_OCTETS = _reversed_dict(S2K_DIGEST_ALGOS) S2K = namedtuple('S2K', ['cipher_algo', 'digest_algo', 'count']) S2K_DEFAULTS = S2K( S2K_CIPHER_ALGOS['CAST5'], S2K_DIGEST_ALGOS['SHA1'], S2KCountCodec().encode(65536), ) class ForwardCompatError(Exception): def __init__(self, msg_suffix): Exception.__init__(self, "not forward-compatible with " + msg_suffix) class ExternalProgramError(Exception): def __init__(self, msg, stderr, returncode): Exception.__init__(self, "%s%s" % ( msg, " (exit code = %d)" % returncode if verbosity > 1 else "", )) self.returncode = returncode self.stderr = stderr def _info(*objs): if verbosity >= 0: print(*objs, file=sys.stderr) def _warn(*objs): _info("warning:", *objs) def _create_recovery_reminder(option, arg=None): def to_option_args(arg): if arg is None: return option try: # converts to "[option arg...]" if iterable but not string if not isinstance(arg, str): return " ".join("%s %s" % (option, arg) for arg in arg) except TypeError: pass return to_option_args((arg,)) return '\n (REMEMBER to pass "%s" in the recovery)' % to_option_args(arg) def _uppercase_hexlify(octets): return binascii.hexlify(octets).upper() def _compute_checksum(octets, count): return sum(bytearray(octets)) & ((1 << (8 * count)) - 1) def minify(octets): if octets[0] != _PAPERKEY_FMT_VERSION_OCTET: raise ForwardCompatError("specified paperkey - need format version 00") fingerprints = [] secrets = [] ind = 1 ind_to = len(octets) - _CRC_OCTET_CNT while ind < ind_to: if octets[ind] != _PGP_VERSION_OCTET: raise ForwardCompatError( "OpenPGP (sub)key version - need version 04" ) ind += 1 fingerprint = str(octets[ind : ind + _FINGERPRINT_OCTET_CNT]) if verbosity > 1: _info("Fingerprint:", _uppercase_hexlify(fingerprint)) fingerprints.append(fingerprint) ind += _FINGERPRINT_OCTET_CNT secret_len = struct.unpack( _SECRET_LEN_PACK_FMT, octets[ind : ind + _SECRET_LEN_OCTET_CNT], )[0] assert secret_len ind += _SECRET_LEN_OCTET_CNT secret = octets[ind : ind + secret_len] ind += secret_len if verbosity > 1: _info("Prefix:", _uppercase_hexlify( secret[:_SECRET_COMMON_PREFIX_OCTET_CNT] )) _info("Extracted (sub)key length %d" % secret_len) # strip off checksum for non-encrypted keys if secret[0] == _SECRET_PLAINTEXT_TAG: secret = secret[:-_SECRET_PLAINTEXT_CHKSUM_OCTET_CNT] if verbosity > 1: _info("Secret checksum: %04X" % _compute_checksum( secret, _SECRET_PLAINTEXT_CHKSUM_OCTET_CNT, )) secrets.append(secret) if not secrets: raise ValueError("Invalid secret key data.") scp = bytes(commonprefix(secrets)) if verbosity > 0: _info("Secret common prefix:", _uppercase_hexlify(scp)) if len(secrets) > 2: raise NotImplementedError( "Multiple subkeys not supported (found %d)" % (len(secrets) - 1), ) if len(scp) < _SECRET_COMMON_PREFIX_OCTET_CNT: _warn( "sub(key)s do not share a common prefix of length %d" % ( _SECRET_COMMON_PREFIX_OCTET_CNT, ) + _create_recovery_reminder("--prefix-length", len(scp)) ) # Warn if any redundancy found outside secret common prefix matching_octet_cnt = sum(b1 ^ b2 == 0 for b1, b2 in zip(*map( lambda s: s[:_SECRET_COMMON_PREFIX_OCTET_CNT], map(bytearray, secrets), ))) - len(scp) if matching_octet_cnt: _warn("%d octets match after secret common prefix %s" % ( matching_octet_cnt, _uppercase_hexlify(scp) )) def strip_s2k_part(secret): scp_strip_cnt, secret_len0 = 0, len(secret) cipher_algo, mode, digest_algo, count = None, None, None, None ret = lambda: ( secret, scp_strip_cnt, S2K(cipher_algo, digest_algo, count) if digest_algo else None, ) if not any(secret.startswith(tag) for tag in _SECRET_S2K_TAGS): return ret() first_octet, secret = secret[0], secret[1:] try: # avoid multiple "if len(secret)" by catching IndexError cipher_algo, secret = secret[0], secret[1:] mode, secret = secret[0], secret[1:] if mode != _SECRET_S2K_MODE_ITER_SALTED_OCTET: raise NotImplementedError("only --s2k-mode 3 keys supported") digest_algo, secret = secret[0], secret[1:] # force raising IndexError if prefix too short count, secret = ( secret[_SECRET_S2K_SALT_OCTET_CNT], secret[: _SECRET_S2K_SALT_OCTET_CNT] + secret[_SECRET_S2K_SALT_OCTET_CNT + 1 :], ) except IndexError: raise ValueError("Invalid secret key - incomplete S2K part") finally: scp_strip_cnt = min(secret_len0 - len(secret), len(scp)) if _SECRET_S2K_SALT_IND < scp_strip_cnt and ( len(scp) <= _SECRET_S2K_SALT_IND + _SECRET_S2K_SALT_OCTET_CNT ): # handle the case when common prefix does not contain S2K count scp_strip_cnt -= 1 return ret() # Strip S2K only from the subkey only (or key if no subkeys), # and strip common prefix from the remaining (sub)keys # Note that the opposite wouldn't work in case of --export-secret-subkeys, # since the first subkey would have a different, non-standard, S2K part # and S2K part of the 2nd would not be fully contained in the common prefix: # __ __ <- secret common prefix of length <= 2 # F? ?? 6? ?? ?? <- s2k mode octet (#3) is 0x64-0x6E (experimental) # F? ?? 0? ?? ?? ... <- s2k mode octet (#3) is 0x00, 0x01 or 0x03 secrets[-1], last_scp_strip_cnt, s2k = strip_s2k_part(secrets[-1]) secrets[-1] = secrets[-1][len(scp) - last_scp_strip_cnt :] secrets[:-1] = map(lambda s: s[len(scp):], secrets[:-1]) scp, min_scp_strip_cnt, _ = strip_s2k_part(scp) # strip S2K part from SCP secret_lens = map(len, secrets) if len(set(secret_lens)) != 1: len_diffs = [ sub_len - secret_lens[0] - (last_scp_strip_cnt - min_scp_strip_cnt) for sub_len in secret_lens[1:] ] _warn( "(Sub)key lengths not unique; |subkey| - |key| = %s" % len_diffs + _create_recovery_reminder("--length-diff", len_diffs) ) del secret_lens # Remind for non-default S2K options if not last_scp_strip_cnt: _warn("key not encrypted" + _create_recovery_reminder("--plaintext")) else: crr = _create_recovery_reminder # reduce verbosity by aliasing if s2k.digest_algo != S2K_DEFAULTS.digest_algo: _warn( "did not use the %s secret-to-key digest algorithm" % ( S2K_DIGEST_OCTETS[S2K_DEFAULTS.digest_algo] ) + crr("--s2k-digest-algo", S2K_DIGEST_OCTETS[s2k.digest_algo]) ) if s2k.cipher_algo != S2K_DEFAULTS.cipher_algo: _warn( "did not use the %s secret-to-key cipher algorithm" % ( S2K_CIPHER_OCTETS[S2K_DEFAULTS.cipher_algo] ) + crr("--s2k-cipher-algo", S2K_CIPHER_OCTETS[s2k.cipher_algo]) ) if s2k.count != S2K_DEFAULTS.count: count_codec = S2KCountCodec() _warn( "passphrase was not mangled %d times" % ( count_codec.decode(S2K_DEFAULTS.count) ) + crr("--s2k-count", count_codec.decode(s2k.count)) ) del crr out = scp for secret in secrets: out += secret return out, fingerprints, s2k def unminify(octets, fingerprints, s2k, len_diffs, implicit_scp_len): if len(len_diffs) != len(fingerprints): raise ValueError("length diffs inconsistent with found fingerprints") if not fingerprints: raise ValueError("no (sub)key to unminify") subkey_cnt = len(len_diffs) # number of (sub)keys if subkey_cnt > 2: raise NotImplementedError( "Multiple subkeys not supported (requested %d)" % subkey_cnt ) scp_len = implicit_scp_len if s2k: # scp_len = max(scp_len - _SECRET_S2K_SALT_IND - ( # scp_len > _SECRET_S2K_SALT_IND + _SECRET_S2K_SALT_OCTET_CNT), 0) # s2k_outside_scp_cnt = max(_SECRET_S2K_SALT_OCTET_CNT - scp_len, 0) if scp_len < _SECRET_S2K_SALT_IND + _SECRET_S2K_SALT_OCTET_CNT: scp_len = max(scp_len - _SECRET_S2K_SALT_IND, 0) s2k_outside_scp_cnt = _SECRET_S2K_SALT_OCTET_CNT - scp_len else: scp_len -= _SECRET_S2K_SALT_IND if scp_len > _SECRET_S2K_SALT_OCTET_CNT: # if S2K count not in scp scp_len -= 1 s2k_outside_scp_cnt = 0 if verbosity > 1: _info("%d octets of S2K salt outside secret common prefix" % ( s2k_outside_scp_cnt )) secret_suffix_sum = len(octets) - scp_len secret_len_avg = (secret_suffix_sum - sum(len_diffs)) / subkey_cnt if (scp_len + sum(len_diffs) + secret_len_avg * subkey_cnt != len(octets)) or secret_suffix_sum < 0: raise ValueError("length diffs inconsistent with common prefix length") del secret_suffix_sum # Strip off (part of) S2K salt from the last subkey (if present) if s2k: s2k_salt = octets[: min(scp_len, _SECRET_S2K_SALT_OCTET_CNT)] if s2k_outside_scp_cnt: last_secret_ind = secret_len_avg + len_diffs[-1] s2k_salt += octets[ -last_secret_ind : -last_secret_ind + s2k_outside_scp_cnt ] octets = ( octets[:-last_secret_ind] + octets[-last_secret_ind + s2k_outside_scp_cnt :] ) if verbosity > 0: _info("S2K salt: ", _uppercase_hexlify(s2k_salt)) s2k_part_len_max = 4 + _SECRET_S2K_SALT_OCTET_CNT + 1 last_prefix = octets[:implicit_scp_len] if s2k is None else ( _SECRET_S2K_TAGS[0] + s2k.cipher_algo + _SECRET_S2K_MODE_ITER_SALTED_OCTET + s2k.digest_algo + s2k_salt + s2k.count ) secret_prefixes = [ last_prefix[:implicit_scp_len] for i in range(subkey_cnt - 1) ] + [last_prefix] if verbosity > 2: _info("Explicit Common Prefix length:", scp_len) _info("Prefixes:", map(_uppercase_hexlify, secret_prefixes)) ind = scp_len out = bytearray(_PAPERKEY_FMT_VERSION_OCTET) for len_diff, fp, secret_prefix in zip( len_diffs, fingerprints, secret_prefixes ): out += _PGP_VERSION_OCTET out += fp if verbosity > 1: _info("Fingerprint:", _uppercase_hexlify(fp)) secret_suffix_len = secret_len_avg + len_diff secret = secret_prefix + octets[ind : ind + secret_suffix_len] ind += secret_suffix_len if secret[0] == _SECRET_PLAINTEXT_TAG: checksum = _compute_checksum( secret, _SECRET_PLAINTEXT_CHKSUM_OCTET_CNT, ) if verbosity > 1: _info("Secret checksum: %04X" % checksum) secret_suffix_len += _SECRET_PLAINTEXT_CHKSUM_OCTET_CNT secret += struct.pack(_SECRET_PLAINTEXT_CHKSUM_PACK_FMT, checksum) out += struct.pack(_SECRET_LEN_PACK_FMT, len(secret)) out += secret if verbosity > 1: _info("Recovered (sub)key of length", len(secret)) out += _CRC_FAKED_OCTETS # dump fake CRC24 and use --ignore-crc-error return out def _parse_fingerprints(pgp_out): return [ # TODO: is there a more machine-friendly way to retrieve fingerprints? "".join(line.partition("=")[-1].split()) for line in pgp_out.split("\n") if "fingerprint" in line and not line.startswith("uid") ] def _quiet_check_output(args, msg, input=None, error_filter=lambda line: True): """Run command with arguments and return its output as a byte string, with stderr captured but suppressed (unlike subprocess.check_output). If the return code was non-zero it raises ExternalProgramError.""" p = subprocess.Popen( args, stdin=(subprocess.PIPE if input is not None else None), stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) p_out, p_err = p.communicate(input) if p.returncode: raise ExternalProgramError( msg, "\n".join(filter(error_filter, p_err.rstrip().split("\n"))), p.returncode, ) return p_out def _xorify(octets, otp): if len(octets) != len(otp): raise ValueError if not (bytes == type(octets) == type(otp)): raise TypeError return bytes(bytearray( octet ^ offset for octet, offset in zip(bytearray(octets), bytearray(otp)) )) def otp(octets, fingerprints, nonce, iter_cnt, key_factory): salt = b"".join(fingerprints) + nonce return _xorify(octets, key_factory(len(octets), salt, iter_cnt)) def _create_pbkdf2_key_factory(passphrase_factory): return lambda key_len, salt, iter_cnt: ( pbkdf2_hmac('sha512', passphrase_factory(), salt, iter_cnt, key_len) ) def _read_passphrase(): while True: pp1, pp2 = ( getpass("Enter OTP passhrase: "), getpass("Repeat the OTP passhrase: "), ) if pp1 == pp2: return pp1 def main(args, otp_key_factory=_create_pbkdf2_key_factory(_read_passphrase)): global verbosity parser = argparse.ArgumentParser() parser.add_argument( "key", nargs='?', help="the secret key to (un)minify (will be passed to gpg)", ) parser.add_argument( "-v", "--verbose", dest='verbosity', help="increase output verbosity (can be repeated)", action='count', default=0, ) parser.add_argument( "-q", "--quiet", action='store_true', help="do not write any errors, warnings or info to stderr", ) coding_group = parser.add_mutually_exclusive_group() coding_group.add_argument( "-a", "--ascii", action='store_true', help="use base64 coding instead of raw (binary)", ) coding_group.add_argument( "-x", "--hex", action='store_true', help="use base16 coding instead of raw (binary)", ) parser.add_argument( "-p", "--otp", action='store_true', help="use PBKDF2-based one-time pad", ) parser.add_argument( "-i", metavar="N_ITER", type=int, default=65536, help="use that many iterations in PBKDF2", ) parser.add_argument( "-s", metavar="SALT_SUFFIX", dest='nonce', type=bytes, default="", help="append OTP salt suffix to fingerprints (use as nonce)", ) min_unmin_group = parser.add_mutually_exclusive_group() min_unmin_group.add_argument( "-r", dest='min_key_file', nargs='?', type=argparse.FileType('rb'), const='-', # make stdin the implicit argument help="recover the minified key from the path or stdin", ) min_unmin_group.add_argument( "--secret-key", metavar="FILE", help="read the key to minify from this file (not stdin)", ) parser.add_argument( "--paperkey-path", metavar="PATH", default="paperkey", help="the path to the paperkey program", ) parser.add_argument( "--gpg-path", metavar="PATH", default="gpg", help="the path to the gpg program", ) recov_args = parser.add_argument_group("recovery options") recov_args.add_argument( "--pubring", metavar="PATH", help="public keyring used to unminify the key", ) recov_args.add_argument( "--fingerprint", metavar="HEX", dest='fingerprints', action='append', help="(sub)key fingerprint (bypasses gpg, can be repeated)", ) recov_args.add_argument( "--length-diff", metavar="DIFF", type=int, help="|subkey| - |key| (use only if warned)", ) recov_args.add_argument( "--prefix-length", metavar="N", type=int, default=_SECRET_COMMON_PREFIX_OCTET_CNT, help="secret common prefix length (use only if warned)", ) recov_args.add_argument( "--plaintext", action='store_true', help="interpret the key to unminify as plaintext", ) recov_args.add_argument( "--s2k-cipher-algo", choices=S2K_CIPHER_ALGOS, default=S2K_CIPHER_OCTETS[S2K_DEFAULTS.cipher_algo], help="cipher algorithm that was used to encrypt the key", ) recov_args.add_argument( "--s2k-digest-algo", choices=S2K_DIGEST_ALGOS, default=S2K_DIGEST_OCTETS[S2K_DEFAULTS.digest_algo], help="digest algorithm that was used to encrypt the key", ) recov_args.add_argument( "--s2k-count", metavar="N_ITER", type=int, default=S2KCountCodec().decode(S2K_DEFAULTS.count), help="number of times the key passphrase was mangled", ) args = parser.parse_args(args) def get_arg(name, default_value): arg = getattr(args, name) return arg if arg is not None else default_value verbosity = args.verbosity if not args.quiet else -1 def export_fingerprints(): pgp_out = _quiet_check_output([ args.gpg_path, "--fingerprint", "--fingerprint", args.key, ], "gpg failed to match the key") if sum(map(lambda s: s.startswith("uid"), pgp_out.split("\n"))) != 1: raise parser.error("no unique match for the key: " + args.key) return _parse_fingerprints(pgp_out) if args.min_key_file: if not (args.key or (args.fingerprints and args.pubring)): parser.error("not specified the key to unminify") fps = args.fingerprints if fps is None: fps = export_fingerprints() if verbosity > 0: for fp in fps: _info("Parsed fingerprint:", fp) fps = map(binascii.unhexlify, fps) subkey_cnt = len(fps) - 1 len_diffs = [0] + ( [args.length_diff] if get_arg('length_diff', 0) else [0] * subkey_cnt ) if verbosity > 1: _info("Secret length differences:", ' '.join(map(str, len_diffs))) s2k = None if args.plaintext else S2K( cipher_algo=S2K_CIPHER_ALGOS[args.s2k_cipher_algo], digest_algo=S2K_DIGEST_ALGOS[args.s2k_digest_algo], count=S2KCountCodec().encode(args.s2k_count), ) octets = get_arg('min_key_file', sys.stdin).read() if args.ascii: octets = binascii.a2b_base64(octets) elif args.hex: octets = binascii.a2b_hex(octets) if args.otp: octets = otp(octets, fps, args.nonce, args.i, otp_key_factory) octets = unminify(octets, fps, s2k, len_diffs, args.prefix_length) sys.stdout.write(_quiet_check_output( [ args.paperkey_path, "--pubring", args.pubring, "--ignore-crc-error", ], input=octets, msg="failed to recover secret key using paperkey", error_filter=( lambda l: not l.startswith("CRC of secret does not match") ), )) else: def export_secret_key(): export_fingerprints() # ensure unique return _quiet_check_output([ args.gpg_path, "--export-secret-key", args.key ], "failed to export the secret key: " + args.key) octets, fps, s2k = minify(_quiet_check_output( [args.paperkey_path] + ( ["--secret-key", args.secret_key] if args.secret_key else [] ) + ["--output-type", "raw"], "failed to extract secret part of the key using paperkey", input=export_secret_key() if args.key else None, )) if args.otp: octets = otp(octets, fps, args.nonce, args.i, otp_key_factory) if args.ascii: print(binascii.b2a_base64(octets)) elif args.hex: print(binascii.b2a_hex(octets)) else: sys.stdout.write(octets) if __name__ == '__main__': def _error(exit_code, *objs): _info("%s:" % __file__, *objs) sys.exit(exit_code) try: main(sys.argv[1:]) except ExternalProgramError as e: if verbosity > 0: _info(e.stderr) _error(1, e) except ForwardCompatError as e: _error(3, e) except NotImplementedError as e: _info( """Note: storing multiple subkeys is redundant and thus discouraged. Back up only the master key (with an encryption subkey), then encrypt other subkeys using the master key. """ ) _error(4, e) except Exception as e: _error(5, e)
# -*- coding: utf-8 -*- import json from nose.tools import eq_, ok_ from rest_framework.exceptions import ParseError from django.contrib.auth.models import AnonymousUser from django.test.client import RequestFactory from django.test.utils import override_settings import mkt from mkt.constants.applications import DEVICE_CHOICES_IDS from mkt.constants.features import FeatureProfile from mkt.search.filters import (DeviceTypeFilter, ProfileFilter, PublicAppsFilter, PublicSearchFormFilter, RegionFilter, SearchQueryFilter, SortingFilter, ValidAppsFilter) from mkt.search.forms import TARAKO_CATEGORIES_MAPPING from mkt.search.views import SearchView from mkt.site.tests import TestCase from mkt.webapps.indexers import WebappIndexer class FilterTestsBase(TestCase): def setUp(self): super(FilterTestsBase, self).setUp() self.req = RequestFactory().get('/') self.req.user = AnonymousUser() self.view_class = SearchView def _filter(self, req=None, data=None): req = req or RequestFactory().get('/', data=data or {}) req.user = AnonymousUser() queryset = WebappIndexer.search() for filter_class in self.filter_classes: queryset = filter_class().filter_queryset(req, queryset, self.view_class) return queryset.to_dict() class TestQueryFilter(FilterTestsBase): filter_classes = [SearchQueryFilter] def test_q(self): qs = self._filter(data={'q': 'search terms'}) # Spot check a few queries. should = (qs['query']['function_score']['query']['bool']['should']) ok_({'match': {'name': {'query': 'search terms', 'boost': 4, 'slop': 1, 'type': 'phrase'}}} in should) ok_({'prefix': {'name': {'boost': 1.5, 'value': 'search terms'}}} in should) ok_({'match': {'name_l10n_english': {'query': 'search terms', 'boost': 2.5}}} in should) ok_({'match': {'description_l10n_english': {'query': 'search terms', 'boost': 0.6, 'analyzer': 'english_analyzer', 'type': 'phrase'}}} in should) def test_fuzzy_single_word(self): qs = self._filter(data={'q': 'term'}) should = (qs['query']['function_score']['query']['bool']['should']) ok_({'fuzzy': {'tags': {'prefix_length': 1, 'value': 'term'}}} in should) def test_no_fuzzy_multi_word(self): qs = self._filter(data={'q': 'search terms'}) qs_str = json.dumps(qs) ok_('fuzzy' not in qs_str) @override_settings(ES_USE_PLUGINS=True) def test_polish_analyzer(self): """ Test that the polish analyzer is included correctly since it is an exception to the rest b/c it is a plugin. """ with self.activate(locale='pl'): qs = self._filter(data={'q': u'próba'}) should = (qs['query']['function_score']['query']['bool']['should']) ok_({'match': {'name_l10n_polish': {'query': u'pr\xf3ba', 'boost': 2.5}}} in should) ok_({'match': {'description_l10n_polish': {'query': u'pr\xf3ba', 'boost': 0.6, 'analyzer': 'polish', 'type': 'phrase'}}} in should) class TestFormFilter(FilterTestsBase): filter_classes = [PublicSearchFormFilter] def test_category(self): qs = self._filter(data={'cat': 'games'}) ok_({'terms': {'category': ['games']}} in qs['query']['filtered']['filter']['bool']['must']) def test_tag(self): qs = self._filter(data={'tag': 'tarako'}) ok_({'term': {'tags': 'tarako'}} in qs['query']['filtered']['filter']['bool']['must']) def test_tarako_categories(self): qs = self._filter(data={'cat': 'tarako-lifestyle'}) ok_({'terms': {'category': TARAKO_CATEGORIES_MAPPING['tarako-lifestyle']}} in qs['query']['filtered']['filter']['bool']['must']) qs = self._filter(data={'cat': 'tarako-games'}) ok_({'terms': {'category': TARAKO_CATEGORIES_MAPPING['tarako-games']}} in qs['query']['filtered']['filter']['bool']['must']) qs = self._filter(data={'cat': 'tarako-tools'}) ok_({'terms': {'category': TARAKO_CATEGORIES_MAPPING['tarako-tools']}} in qs['query']['filtered']['filter']['bool']['must']) def test_app_type(self): qs = self._filter(data={'app_type': ['hosted']}) ok_({'terms': {'app_type': [1]}} in qs['query']['filtered']['filter']['bool']['must']) def test_app_type_packaged(self): """Test packaged also includes privileged.""" qs = self._filter(data={'app_type': ['packaged']}) ok_({'terms': {'app_type': [2, 3]}} in qs['query']['filtered']['filter']['bool']['must']) def test_manifest_url(self): url = 'http://hy.fr/manifest.webapp' qs = self._filter(data={'manifest_url': url}) ok_({'term': {'manifest_url': url}} in qs['query']['filtered']['filter']['bool']['must']) def test_offline(self): """Ensure we are filtering by offline-capable apps.""" qs = self._filter(data={'offline': 'True'}) ok_({'term': {'is_offline': True}} in qs['query']['filtered']['filter']['bool']['must']) def test_online(self): """Ensure we are filtering by apps that require online access.""" qs = self._filter(data={'offline': 'False'}) ok_({'term': {'is_offline': False}} in qs['query']['filtered']['filter']['bool']['must']) def test_offline_and_online(self): """Ensure we are not filtering by offline/online by default.""" # Pass any form values other than 'offline' to create the dict. qs = self._filter(data={'cat': 'games'}) ok_({'term': {'is_offline': True}} not in qs['query']['filtered']['filter']['bool']['must']) ok_({'term': {'is_offline': False}} not in qs['query']['filtered']['filter']['bool']['must']) def test_languages(self): qs = self._filter(data={'languages': 'fr'}) ok_({'terms': {'supported_locales': ['fr']}} in qs['query']['filtered']['filter']['bool']['must']) qs = self._filter(data={'languages': 'ar,en-US'}) ok_({'terms': {'supported_locales': ['ar', 'en-US']}} in qs['query']['filtered']['filter']['bool']['must']) def test_author(self): qs = self._filter(data={'author': 'Mozilla LABS'}) ok_({'term': {'author.raw': u'mozilla labs'}} in qs['query']['filtered']['filter']['bool']['must']) def test_installs_allowed_from(self): qs = self._filter(data={'installs_allowed_from': '*'}) ok_({'term': {'installs_allowed_from': u'*'}} in qs['query']['filtered']['filter']['bool']['must']) # Test that we don't filter by this field if not provided. qs = self._filter() ok_('installs_allowed_from' not in json.dumps(qs), "Unexpected 'installs_allowed_from' in query") def test_premium_types(self): def ptype(p): return mkt.ADDON_PREMIUM_API_LOOKUP.get(p) # Test a single premium type. qs = self._filter(data={'premium_types': ['free']}) ok_({'terms': {'premium_type': [ptype('free')]}} in qs['query']['filtered']['filter']['bool']['must']) # Test many premium types. qs = self._filter(data={'premium_types': ['free', 'free-inapp']}) ok_({'terms': {'premium_type': [ptype('free'), ptype('free-inapp')]}} in qs['query']['filtered']['filter']['bool']['must']) # Test a non-existent premium type. with self.assertRaises(ParseError): self._filter(data={'premium_types': ['free', 'platinum']}) def test_device(self): qs = self._filter(data={'dev': 'desktop'}) ok_({'term': {'device': DEVICE_CHOICES_IDS['desktop']}} in qs['query']['filtered']['filter']['bool']['must']) def test_no_device_with_device_type(self): """Test that providing a device type w/o device doesn't filter.""" qs = self._filter(data={'dev': '', 'device': 'firefoxos'}) ok_('filtered' not in qs['query'].keys()) class TestPublicAppsFilter(FilterTestsBase): filter_classes = [PublicAppsFilter] def test_status(self): qs = self._filter(self.req) ok_({'term': {'status': mkt.STATUS_PUBLIC}} in qs['query']['filtered']['filter']['bool']['must']) ok_({'term': {'is_disabled': False}} in qs['query']['filtered']['filter']['bool']['must']) class TestValidAppsFilter(FilterTestsBase): filter_classes = [ValidAppsFilter] def test_status(self): qs = self._filter(self.req) ok_({'terms': {'status': mkt.VALID_STATUSES}} in qs['query']['filtered']['filter']['bool']['must']) ok_({'term': {'is_disabled': False}} in qs['query']['filtered']['filter']['bool']['must']) class TestDeviceTypeFilter(FilterTestsBase): filter_classes = [DeviceTypeFilter] def test_no_filters(self): qs = self._filter(self.req) ok_('filtered' not in qs['query'].keys()) def test_mobile(self): self.req.MOBILE = True qs = self._filter(self.req) ok_({'term': {'uses_flash': False}} in qs['query']['filtered']['filter']['bool']['must']) def test_gaia(self): self.req.GAIA = True qs = self._filter(self.req) ok_({'term': {'uses_flash': False}} in qs['query']['filtered']['filter']['bool']['must']) def test_tablet(self): self.req.TABLET = True qs = self._filter(self.req) ok_('filtered' not in qs['query'].keys()) def test_device_in_querystring(self): qs = self._filter(data={'dev': 'desktop'}) ok_({'term': {'device': 1}} in qs['query']['filtered']['filter']['bool']['must']) qs = self._filter(data={'dev': 'android', 'device': 'mobile'}) ok_({'term': {'device': 2}} in qs['query']['filtered']['filter']['bool']['must']) qs = self._filter(data={'dev': 'android', 'device': 'tablet'}) ok_({'term': {'device': 3}} in qs['query']['filtered']['filter']['bool']['must']) qs = self._filter(data={'dev': 'firefoxos'}) ok_({'term': {'device': 4}} in qs['query']['filtered']['filter']['bool']['must']) class TestRegionFilter(FilterTestsBase): filter_classes = [RegionFilter] def test_no_region_default(self): qs = self._filter(self.req) ok_({'term': {'region_exclusions': mkt.regions.RESTOFWORLD.id}} in qs['query']['filtered']['filter']['bool']['must_not']) def test_region(self): self.req.REGION = mkt.regions.BRA qs = self._filter(self.req) ok_({'term': {'region_exclusions': mkt.regions.BRA.id}} in qs['query']['filtered']['filter']['bool']['must_not']) class TestProfileFilter(FilterTestsBase): filter_classes = [ProfileFilter] def profile_qs(self, disabled_features=None): if disabled_features is None: disabled_features = {} profile = FeatureProfile().fromkeys(FeatureProfile(), True) for feature in disabled_features: profile[feature] = False return {'pro': profile.to_signature(), 'dev': 'firefoxos'} def test_filter_all_features_present(self): qs = self._filter(data=self.profile_qs()) ok_('filtered' not in qs['query'].keys()) def test_filter_one_feature_present(self): qs = self._filter(data=self.profile_qs(disabled_features=['sms'])) ok_({'term': {'features.has_sms': True}} in qs['query']['filtered']['filter']['bool']['must_not']) def test_filter_one_feature_present_desktop(self): data = self.profile_qs(disabled_features=['sms']) data['dev'] = 'desktop' qs = self._filter(data=data) ok_('filtered' not in qs['query'].keys()) def test_filter_multiple_features_present(self): qs = self._filter( data=self.profile_qs(disabled_features=['sms', 'apps'])) ok_({'term': {'features.has_sms': True}} in qs['query']['filtered']['filter']['bool']['must_not']) ok_({'term': {'features.has_apps': True}} in qs['query']['filtered']['filter']['bool']['must_not']) class TestSortingFilter(FilterTestsBase): filter_classes = [SortingFilter] def test_sort(self): for api_sort, es_sort in SortingFilter.DEFAULT_SORTING.items(): qs = self._filter(data={'sort': [api_sort]}) if es_sort.startswith('-'): ok_({es_sort[1:]: {'order': 'desc'}} in qs['sort'], qs) else: eq_([es_sort], qs['sort'], qs) def test_sort_multiple(self): qs = self._filter(data={'sort': ['rating', 'created']}) ok_({'bayesian_rating': {'order': 'desc'}} in qs['sort']) ok_({'created': {'order': 'desc'}} in qs['sort']) def test_sort_regional(self): """Popularity and trending use regional sorting for mature regions.""" req = RequestFactory().get('/') req.REGION = mkt.regions.BRA # Default empty query searches use popularity. qs = self._filter(req) ok_({'popularity_%s' % mkt.regions.BRA.id: {'order': 'desc'}} in qs['sort']) # Popularity. req = RequestFactory().get('/', data={'sort': ['popularity']}) req.REGION = mkt.regions.BRA qs = self._filter(req) ok_({'popularity_%s' % mkt.regions.BRA.id: {'order': 'desc'}} in qs['sort']) # Trending. req = RequestFactory().get('/', data={'sort': ['trending']}) req.REGION = mkt.regions.BRA qs = self._filter(req) ok_({'trending_%s' % mkt.regions.BRA.id: {'order': 'desc'}} in qs['sort']) class TestCombinedFilter(FilterTestsBase): """ Basic test to ensure that when filters are combined they result in the expected query structure. """ filter_classes = [SearchQueryFilter, PublicSearchFormFilter, PublicAppsFilter, SortingFilter] def test_combined(self): qs = self._filter(data={'q': 'test', 'cat': 'games', 'sort': 'trending'}) ok_(qs['query']['filtered']['query']['function_score']) ok_(qs['query']['filtered']['filter']) must = qs['query']['filtered']['filter']['bool']['must'] ok_({'terms': {'category': ['games']}} in must) ok_({'term': {'status': 4}} in must) ok_({'term': {'is_disabled': False}} in must) ok_({'trending': {'order': 'desc'}} in qs['sort']) query = qs['query']['filtered']['query'] ok_({'field_value_factor': {'field': 'boost'}} in query['function_score']['functions']) ok_({'match': {'name_l10n_english': {'boost': 2.5, 'query': u'test'}}} in query['function_score']['query']['bool']['should'])
# See LICENCE for the source code licence. # (c) 2010 Dan Saul from Base import Base SAVE_STATUS_SAVED = 0 # We can close this without losing data. SAVE_STATUS_NOT_SAVED = 1 # If we close this we will lose data, but we can save. SAVE_STATUS_NOT_SAVED_NEED_PATH = 2 # If we close this we will lose data, but we can save, however need the save path. SAVE_STATUS_SAVING = 3 # In the progress of saving. SAVE_STATUS_UNSAVABLE = 4 # If we close this we will lose data, we are not able to save however. SAVE_STATUS_RANGE = range(SAVE_STATUS_SAVED,SAVE_STATUS_UNSAVABLE+1) BUS_INTERFACE_NAME = "org.ude.components.client" BUS_INTERFACE_NAME_PID_FORMAT = BUS_INTERFACE_NAME+"_{0}" BUS_OBJECT_PATH = "/org/ude/components/client" import logging,os logger = logging.getLogger('Client') logger.setLevel(logging.DEBUG) __fh = logging.FileHandler('runtime.log') __fh.setLevel(logging.DEBUG) __ch = logging.StreamHandler() __ch.setLevel(logging.ERROR) __formatter = logging.Formatter('%(asctime)s %(name)s[%(process)d:%(levelname)s] %(filename)s::%(funcName)s() - %(message)s') __fh.setFormatter(__formatter) __ch.setFormatter(__formatter) logger.addHandler(__fh) logger.addHandler(__ch) #http://docs.python.org/library/logging.html#http-handler eventually...
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html import logging from scrapy.conf import settings from scrapy.exceptions import DropItem import rethinkdb as r #class PushfeedPipeline(object): # def process_item(self, item, spider): # return item class RethinkdbPipeline(object): """docstring for RethinkdbPipeline""" def __init__(self): r.connect(settings['RETHINKDB_SERVER'], settings['RETHINKDB_PORT']).repl() self.db = r.db(settings['RETHINKDB_DB']).table(settings['RETHINKDB_TABLE']) def process_item(self, item, spider): for data in item: if not data: raise DropItem data = dict(title=item['title'][0], description=item['description'][0], date=item['date'][0], link=item['link'][0], img=item['img'][0]) self.db.insert(data).run() logging.log(logging.INFO,"Feed added to rethinkdb database!") return item
# Copyright 2013 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS-IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Page objects used in functional tests for U-MOOC.""" __author__ = 'John Orr (jorr@google.com)' from selenium.webdriver.common import action_chains from selenium.webdriver.common import by from selenium.webdriver.support import expected_conditions as ec from selenium.webdriver.support import select from selenium.webdriver.support import wait class PageObject(object): """Superclass to hold shared logic used by page objects.""" def __init__(self, tester): self._tester = tester def find_element_by_css_selector(self, selector): return self._tester.driver.find_element_by_css_selector(selector) def find_element_by_id(self, elt_id): return self._tester.driver.find_element_by_id(elt_id) def find_element_by_link_text(self, text): return self._tester.driver.find_element_by_link_text(text) def find_element_by_name(self, name): return self._tester.driver.find_element_by_name(name) def expect_status_message_to_be(self, value): wait.WebDriverWait(self._tester.driver, 15).until( ec.text_to_be_present_in_element( (by.By.ID, 'formStatusMessage'), value)) class EditorPageObject(PageObject): """Page object for pages which wait for the editor to finish loading.""" def __init__(self, tester): super(EditorPageObject, self).__init__(tester) def successful_butter_bar(driver): form_status_message = driver.find_element_by_id('formStatusMessage') return 'Success.' in form_status_message.text or ( not form_status_message.is_displayed()) wait.WebDriverWait(self._tester.driver, 15).until(successful_butter_bar) def set_status(self, status): select.Select(self.find_element_by_name( 'is_draft')).select_by_visible_text(status) return self def click_save(self, link_text='Save', status_message='Saved'): self.find_element_by_link_text(link_text).click() self.expect_status_message_to_be(status_message) return self def _close_and_return_to(self, continue_page): self.find_element_by_link_text('Close').click() return continue_page(self._tester) class DashboardEditor(EditorPageObject): """A base class for the editors accessed from the Dashboard.""" def click_close(self): return self._close_and_return_to(DashboardPage) class RootPage(PageObject): """Page object to model the interactions with the root page.""" def load(self, base_url): self._tester.driver.get(base_url + '/') return self def click_login(self): self.find_element_by_link_text('Login').click() return LoginPage(self._tester) def click_dashboard(self): self.find_element_by_link_text('Dashboard').click() return DashboardPage(self._tester) def click_admin(self): self.find_element_by_link_text('Admin').click() return AdminPage(self._tester) def click_announcements(self): self.find_element_by_link_text('Announcements').click() return AnnouncementsPage(self._tester) def click_register(self): self.find_element_by_link_text('Register').click() return RegisterPage(self._tester) class RegisterPage(PageObject): """Page object to model the registration page.""" def enroll(self, name): enroll = self.find_element_by_name('form01') enroll.send_keys(name) enroll.submit() return RegisterPage(self._tester) def verify_enrollment(self): self._tester.assertTrue( 'Thank you for registering' in self.find_element_by_css_selector( 'p.top_content').text) return self def click_course(self): self.find_element_by_link_text('Course').click() return RootPage(self._tester) class AnnouncementsPage(PageObject): """Page object to model the announcements page.""" def click_add_new(self): self.find_element_by_css_selector( '#gcb-add-announcement > button').click() return AnnouncementsEditorPage(self._tester) def verify_announcement(self, title=None, date=None, body=None): """Verify that the announcement has the given fields.""" if title: self._tester.assertEquals( title, self._tester.driver.find_elements_by_css_selector( 'div.gcb-aside h2')[0].text) if date: self._tester.assertEquals( date, self._tester.driver.find_elements_by_css_selector( 'div.gcb-aside p')[0].text) if body: self._tester.assertEquals( body, self._tester.driver.find_elements_by_css_selector( 'div.gcb-aside p')[1].text) return self class AnnouncementsEditorPage(EditorPageObject): """Page to model the announcements editor.""" def enter_fields(self, title=None, date=None, body=None): """Enter title, date, and body into the announcement form.""" if title: title_el = self.find_element_by_name('title') title_el.clear() title_el.send_keys(title) if date: date_el = self.find_element_by_name('date') date_el.clear() date_el.send_keys(date) if body: body_el = self.find_element_by_name('html') body_el.clear() body_el.send_keys(body) return self def click_close(self): return self._close_and_return_to(AnnouncementsPage) class LoginPage(PageObject): """Page object to model the interactions with the login page.""" def login(self, login, admin=False): email = self._tester.driver.find_element_by_id('email') email.clear() email.send_keys(login) if admin: self.find_element_by_id('admin').click() self.find_element_by_id('submit-login').click() return RootPage(self._tester) class DashboardPage(PageObject): """Page object to model the interactions with the dashboard landing page.""" def load(self, base_url, name): self._tester.driver.get('/'.join([base_url, name, 'dashboard'])) return self def verify_read_only_course(self): self._tester.assertEquals( 'Read-only course.', self.find_element_by_id('gcb-alerts-bar').text) return self def verify_selected_tab(self, tab_text): tab = self.find_element_by_link_text(tab_text) self._tester.assertEquals('selected', tab.get_attribute('class')) def verify_not_publicly_available(self): self._tester.assertEquals( 'The course is not publicly available.', self.find_element_by_id('gcb-alerts-bar').text) return self def click_import(self): self.find_element_by_css_selector('#import_course').click() return Import(self._tester) def click_add_unit(self): self.find_element_by_css_selector('#add_unit > button').click() return AddUnit(self._tester) def click_add_assessment(self): self.find_element_by_css_selector('#add_assessment > button').click() return AddAssessment(self._tester) def click_add_link(self): self.find_element_by_css_selector('#add_link > button').click() return AddLink(self._tester) def click_add_lesson(self): self.find_element_by_css_selector('#add_lesson > button').click() return AddLesson(self._tester) def click_organize(self): self.find_element_by_css_selector('#edit_unit_lesson').click() return Organize(self._tester) def click_assets(self): self.find_element_by_link_text('Assets').click() return AssetsPage(self._tester) def verify_course_outline_contains_unit(self, unit_title): self.find_element_by_link_text(unit_title) return self class AssetsPage(PageObject): """Page object for the dashboard's assets tab.""" def click_upload(self): self.find_element_by_link_text('Upload').click() return AssetsEditorPage(self._tester) def verify_image_file_by_name(self, name): self.find_element_by_link_text(name) # throw exception if not found return self def verify_no_image_file_by_name(self, name): self.find_element_by_link_text(name) # throw exception if not found return self def click_edit_image(self, name): self.find_element_by_link_text( name).parent.find_element_by_link_text('[Edit]').click() return ImageEditorPage(self._tester) class AssetsEditorPage(DashboardEditor): """Page object for upload image page.""" def select_file(self, path): self.find_element_by_name('file').send_keys(path) return self def click_upload_and_expect_saved(self): self.find_element_by_link_text('Upload').click() self.expect_status_message_to_be('Saved.') # Page automatically redirects after successful save. wait.WebDriverWait(self._tester.driver, 15).until( ec.title_contains('Assets')) return AssetsPage(self._tester) class ImageEditorPage(EditorPageObject): """Page object for the dashboard's view/delete image page.""" def click_delete(self): self.find_element_by_link_text('Delete').click() return self def confirm_delete(self): self._tester.driver.switch_to_alert().accept() return AssetsPage(self._tester) class AddUnit(DashboardEditor): """Page object to model the dashboard's add unit editor.""" def __init__(self, tester): super(AddUnit, self).__init__(tester) self.expect_status_message_to_be('New unit has been created and saved.') def set_title(self, title): title_el = self.find_element_by_name('title') title_el.clear() title_el.send_keys(title) return self class Import(DashboardEditor): """Page object to model the dashboard's unit/lesson organizer.""" pass class AddAssessment(DashboardEditor): """Page object to model the dashboard's assessment editor.""" def __init__(self, tester): super(AddAssessment, self).__init__(tester) self.expect_status_message_to_be( 'New assessment has been created and saved.') class AddLink(DashboardEditor): """Page object to model the dashboard's link editor.""" def __init__(self, tester): super(AddLink, self).__init__(tester) self.expect_status_message_to_be( 'New link has been created and saved.') class AddLesson(DashboardEditor): """Page object to model the dashboard's lesson editor.""" RTE_EDITOR_ID = 'gcbRteField-0_editor' RTE_TEXTAREA_ID = 'gcbRteField-0' def __init__(self, tester): super(AddLesson, self).__init__(tester) self.expect_status_message_to_be( 'New lesson has been created and saved.') def click_rich_text(self): el = self.find_element_by_css_selector('div.rte-control') self._tester.assertEqual('Rich Text', el.text) el.click() wait.WebDriverWait(self._tester.driver, 15).until( ec.element_to_be_clickable((by.By.ID, AddLesson.RTE_EDITOR_ID))) return self def click_plain_text(self): el = self.find_element_by_css_selector('div.rte-control') self._tester.assertEqual('<HTML>', el.text) el.click() return self def send_rte_text(self, text): self.find_element_by_id('gcbRteField-0_editor').send_keys(text) return self def select_rte_custom_tag_type(self, option_text): """Select the given option from the custom content type selector.""" self._ensure_rte_iframe_ready_and_switch_to_it() select_tag = self.find_element_by_name('tag') for option in select_tag.find_elements_by_tag_name('option'): if option.text == option_text: option.click() break else: self._tester.fail('No option "%s" found' % option_text) wait.WebDriverWait(self._tester.driver, 15).until( ec.element_to_be_clickable( (by.By.PARTIAL_LINK_TEXT, 'Close'))) self._tester.driver.switch_to_default_content() return self def click_rte_add_custom_tag(self): self.find_element_by_link_text( 'Insert Google U-MOOC component').click() return self def doubleclick_rte_element(self, elt_css_selector): self._tester.driver.switch_to_frame(AddLesson.RTE_EDITOR_ID) target = self.find_element_by_css_selector(elt_css_selector) action_chains.ActionChains( self._tester.driver).double_click(target).perform() self._tester.driver.switch_to_default_content() return self def _ensure_rte_iframe_ready_and_switch_to_it(self): wait.WebDriverWait(self._tester.driver, 15).until( ec.frame_to_be_available_and_switch_to_it('modal-editor-iframe')) # Ensure inputEx has initialized too wait.WebDriverWait(self._tester.driver, 15).until( ec.element_to_be_clickable( (by.By.PARTIAL_LINK_TEXT, 'Close'))) def set_rte_lightbox_field(self, field_css_selector, value): self._ensure_rte_iframe_ready_and_switch_to_it() field = self.find_element_by_css_selector(field_css_selector) field.clear() field.send_keys(value) self._tester.driver.switch_to_default_content() return self def ensure_rte_lightbox_field_has_value(self, field_css_selector, value): self._ensure_rte_iframe_ready_and_switch_to_it() self._tester.assertEqual( value, self.find_element_by_css_selector( field_css_selector).get_attribute('value')) self._tester.driver.switch_to_default_content() return self def click_rte_save(self): self._ensure_rte_iframe_ready_and_switch_to_it() self.find_element_by_link_text('Save').click() self._tester.driver.switch_to_default_content() return self def ensure_objectives_textarea_matches(self, text): self._tester.assertEqual(text, self.find_element_by_id( AddLesson.RTE_TEXTAREA_ID).get_attribute('value')) return self class Organize(DashboardEditor): """Page object to model the dashboard's unit/lesson organizer.""" pass class AdminPage(PageObject): """Page object to model the interactions with the admimn landing page.""" def click_add_course(self): self.find_element_by_id('add_course').click() return AddCourseEditorPage(self._tester) def click_settings(self): self.find_element_by_link_text('Settings').click() return AdminSettingsPage(self._tester) class AdminSettingsPage(PageObject): """Page object for the admin settings.""" def click_override_admin_user_emails(self): self._tester.driver.find_elements_by_css_selector( 'button.btn btn-primary')[0].click() return ConfigPropertyOverridePage(self._tester) def verify_admin_user_emails_contains(self, email): self._tester.assertTrue( email in self._tester.driver.find_elements_by_css_selector( 'table.gcb-config tr')[1].find_elements_by_css_selector( 'td')[1].text) class ConfigPropertyOverridePage(EditorPageObject): """Page object for the admin property override editor.""" def set_value(self, value): self.find_element_by_name('value').send_keys(value) return self def click_close(self): return self._close_and_return_to(AdminSettingsPage) class AddCourseEditorPage(EditorPageObject): """Page object for the dashboards' add course page.""" def set_fields(self, name=None, title=None, email=None): """Populate the fields in the add course page.""" name_el = self.find_element_by_name('name') title_el = self.find_element_by_name('title') email_el = self.find_element_by_name('admin_email') name_el.clear() title_el.clear() email_el.clear() if name: name_el.send_keys(name) if title: title_el.send_keys(title) if email: email_el.send_keys(email) return self def click_close(self): return self._close_and_return_to(AdminPage)
# coding=utf-8 r""" This code was generated by \ / _ _ _| _ _ | (_)\/(_)(_|\/| |(/_ v1.0.0 / / """ from tests import IntegrationTestCase from tests.holodeck import Request from twilio.base.exceptions import TwilioException from twilio.http.response import Response class TrustProductsChannelEndpointAssignmentTestCase(IntegrationTestCase): def test_create_request(self): self.holodeck.mock(Response(500, '')) with self.assertRaises(TwilioException): self.client.trusthub.v1.trust_products("BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .trust_products_channel_endpoint_assignment.create(channel_endpoint_type="channel_endpoint_type", channel_endpoint_sid="ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") values = { 'ChannelEndpointType': "channel_endpoint_type", 'ChannelEndpointSid': "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", } self.holodeck.assert_has_request(Request( 'post', 'https://trusthub.twilio.com/v1/TrustProducts/BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ChannelEndpointAssignments', data=values, )) def test_create_response(self): self.holodeck.mock(Response( 201, ''' { "sid": "RAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "trust_product_sid": "BUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "channel_endpoint_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "channel_endpoint_type": "phone-number", "date_created": "2019-07-31T02:34:41Z", "url": "https://trusthub.twilio.com/v1/TrustProducts/BUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ChannelEndpointAssignments/RAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" } ''' )) actual = self.client.trusthub.v1.trust_products("BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .trust_products_channel_endpoint_assignment.create(channel_endpoint_type="channel_endpoint_type", channel_endpoint_sid="ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") self.assertIsNotNone(actual) def test_list_request(self): self.holodeck.mock(Response(500, '')) with self.assertRaises(TwilioException): self.client.trusthub.v1.trust_products("BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .trust_products_channel_endpoint_assignment.list() self.holodeck.assert_has_request(Request( 'get', 'https://trusthub.twilio.com/v1/TrustProducts/BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ChannelEndpointAssignments', )) def test_read_empty_response(self): self.holodeck.mock(Response( 200, ''' { "results": [], "meta": { "page": 0, "page_size": 50, "first_page_url": "https://trusthub.twilio.com/v1/TrustProducts/BUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ChannelEndpointAssignments?PageSize=50&Page=0", "previous_page_url": null, "url": "https://trusthub.twilio.com/v1/TrustProducts/BUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ChannelEndpointAssignments?PageSize=50&Page=0", "next_page_url": null, "key": "results" } } ''' )) actual = self.client.trusthub.v1.trust_products("BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .trust_products_channel_endpoint_assignment.list() self.assertIsNotNone(actual) def test_read_full_response(self): self.holodeck.mock(Response( 200, ''' { "results": [ { "sid": "RAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "trust_product_sid": "BUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "channel_endpoint_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "channel_endpoint_type": "phone-number", "date_created": "2019-07-31T02:34:41Z", "url": "https://trusthub.twilio.com/v1/TrustProducts/BUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ChannelEndpointAssignments/RAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" } ], "meta": { "page": 0, "page_size": 50, "first_page_url": "https://trusthub.twilio.com/v1/TrustProducts/BUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ChannelEndpointAssignments?ChannelEndpointSid=PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&PageSize=50&Page=0", "previous_page_url": null, "url": "https://trusthub.twilio.com/v1/TrustProducts/BUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ChannelEndpointAssignments?ChannelEndpointSid=PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&PageSize=50&Page=0", "next_page_url": null, "key": "results" } } ''' )) actual = self.client.trusthub.v1.trust_products("BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .trust_products_channel_endpoint_assignment.list() self.assertIsNotNone(actual) def test_fetch_request(self): self.holodeck.mock(Response(500, '')) with self.assertRaises(TwilioException): self.client.trusthub.v1.trust_products("BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .trust_products_channel_endpoint_assignment("RAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").fetch() self.holodeck.assert_has_request(Request( 'get', 'https://trusthub.twilio.com/v1/TrustProducts/BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ChannelEndpointAssignments/RAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', )) def test_fetch_response(self): self.holodeck.mock(Response( 200, ''' { "sid": "RAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "trust_product_sid": "BUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "channel_endpoint_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "channel_endpoint_type": "phone-number", "date_created": "2019-07-31T02:34:41Z", "url": "https://trusthub.twilio.com/v1/TrustProducts/BUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/ChannelEndpointAssignments/RAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" } ''' )) actual = self.client.trusthub.v1.trust_products("BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .trust_products_channel_endpoint_assignment("RAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").fetch() self.assertIsNotNone(actual) def test_delete_request(self): self.holodeck.mock(Response(500, '')) with self.assertRaises(TwilioException): self.client.trusthub.v1.trust_products("BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .trust_products_channel_endpoint_assignment("RAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").delete() self.holodeck.assert_has_request(Request( 'delete', 'https://trusthub.twilio.com/v1/TrustProducts/BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ChannelEndpointAssignments/RAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', )) def test_delete_response(self): self.holodeck.mock(Response( 204, None, )) actual = self.client.trusthub.v1.trust_products("BUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .trust_products_channel_endpoint_assignment("RAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").delete() self.assertTrue(actual)
#!/usr/bin/python # -*- coding: utf-8 -*- __author__ = "Stefan Mauerberger" __copyright__ = "Copyright (C) 2017 Stefan Mauerberger" __license__ = "GPLv3" ''' Save a plot of the discretization as PGF file ''' import numpy as np from matplotlib import pyplot as plt from plotting import rcParams, prepare_map from gptt import read_station_file, ListPairs from reference import dt_obs plt.rcParams.update(rcParams) # Read station coordinates all_stations = read_station_file('../dat/stations.dat') # Read pseudo data pseudo_data = np.genfromtxt('../dat/pseudo_data.dat', dtype=dt_obs) # Instantiate pairs = ListPairs(pseudo_data, all_stations) # Stations stations = pairs.stations # Discretization points = pairs.points # Prepare map m = prepare_map() # Plot station locations m.scatter(stations['lon'], stations['lat'], lw=0, color='g', latlon=True) # Plot discretization m.scatter(points['lon'], points['lat'], lw=0, marker='.', s=4, latlon=True, color='g', rasterized=True) plt.savefig('../fig_discretization.pgf')
import sys import argparse from itertools import izip def parseArgument(): # Parse the input parser=argparse.ArgumentParser(description=\ "Get the positions with the top scores and their scores") parser.add_argument("--bedFileNameList", required=True,\ help='File with list of bed files') parser.add_argument("--signalFileNameList", required=True,\ help='File with list of signal files, ordered in the same way as bedFileNameList') parser.add_argument("--numSignalsMerged", type=int, default=2, required=False,\ help='Number of signals per merged peak file') parser.add_argument("--outputFileNamePrefixList", required=True,\ help='File with list of output name prefixes, where there are numSignalsMerged per bed/signal file, ordered in the same way as bedFileNameList') parser.add_argument("--scriptFileName", required=True,\ help='Output file name for script') options = parser.parse_args() return options def makeGetMergedPeakSignalsScript(options): # Make a script that will get signals for the merged peaks and record them according to bin indexes bedFileNameListFile = open(options.bedFileNameList) signalFileNameListFile = open(options.signalFileNameList) outputFileNamePrefixListFile = open(options.outputFileNamePrefixList) scriptFile = open(options.scriptFileName) for bedFileNameStr, signalFileNameStr in izip((bedFileNameListFile, signalFileNameListFile)): # Iterate through the merged peak files and create lines in the script for each bedFileName = bedFileNameStr.strip() signalFileName = signalFileNameStr.strip() for i in range(options.numSignalsMerged): # Iterate through the merged signals and create a separate file for each outputFileNamePrefix = outputFileNamePrefixListFile.readline().strip() for j in range(22): # Iterate through the chromosomes and make a separate file for each chrom = "chr" + str(j) outputFileName = "{0}.{1}.{2}\n".format(outputFileNamePrefix, chrom, "txt") col = str(i + 4) scriptString = "{0} {1} {2} {3} {4} {5} {6}\n".format("paste", bedFileName, signalFileName, "| grep -P'", + chrom + "\\t' | sort -u -k1,1 -k2,2n -k3,3n | cut -f1," + col, "| awk '{print NR, \"\\t\", $0}' | awk 'BEGIN{OFS=\"\\t\"} {print $2,$1,$1+1,$3}' >", outputFileName) scriptFile.write(scriptString) if __name__ == "__main__": options = parseArgument() makeGetMergedPeakSignalsScript(options)
# -*- coding: utf-8 -*- from operator import attrgetter from pyangbind.lib.yangtypes import RestrictedPrecisionDecimalType from pyangbind.lib.yangtypes import RestrictedClassType from pyangbind.lib.yangtypes import TypedListType from pyangbind.lib.yangtypes import YANGBool from pyangbind.lib.yangtypes import YANGListType from pyangbind.lib.yangtypes import YANGDynClass from pyangbind.lib.yangtypes import ReferenceType from pyangbind.lib.base import PybindBase from collections import OrderedDict from decimal import Decimal from bitarray import bitarray import six # PY3 support of some PY2 keywords (needs improved) if six.PY3: import builtins as __builtin__ long = int elif six.PY2: import __builtin__ from . import sid class prefix_sid(PybindBase): """ This class was auto-generated by the PythonClass plugin for PYANG from YANG module openconfig-network-instance - based on the path /network-instances/network-instance/protocols/protocol/isis/levels/level/link-state-database/lsp/tlvs/tlv/extended-ipv4-reachability/prefixes/prefix/subTLVs/subTLVs/prefix-sid. Each member element of the container is represented as a class variable - with a specific YANG type. YANG Description: This container defines segment routing extensions for prefixes. """ __slots__ = ("_path_helper", "_extmethods", "__sid") _yang_name = "prefix-sid" _pybind_generated_by = "container" def __init__(self, *args, **kwargs): self._path_helper = False self._extmethods = False self.__sid = YANGDynClass( base=YANGListType( False, sid.sid, yang_name="sid", parent=self, is_container="list", user_ordered=False, path_helper=self._path_helper, yang_keys="False", extensions=None, ), is_container="list", yang_name="sid", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions=None, namespace="http://openconfig.net/yang/network-instance", defining_module="openconfig-network-instance", yang_type="list", is_config=False, ) load = kwargs.pop("load", None) if args: if len(args) > 1: raise TypeError("cannot create a YANG container with >1 argument") all_attr = True for e in self._pyangbind_elements: if not hasattr(args[0], e): all_attr = False break if not all_attr: raise ValueError("Supplied object did not have the correct attributes") for e in self._pyangbind_elements: nobj = getattr(args[0], e) if nobj._changed() is False: continue setmethod = getattr(self, "_set_%s" % e) if load is None: setmethod(getattr(args[0], e)) else: setmethod(getattr(args[0], e), load=load) def _path(self): if hasattr(self, "_parent"): return self._parent._path() + [self._yang_name] else: return [ "network-instances", "network-instance", "protocols", "protocol", "isis", "levels", "level", "link-state-database", "lsp", "tlvs", "tlv", "extended-ipv4-reachability", "prefixes", "prefix", "subTLVs", "subTLVs", "prefix-sid", ] def _get_sid(self): """ Getter method for sid, mapped from YANG variable /network_instances/network_instance/protocols/protocol/isis/levels/level/link_state_database/lsp/tlvs/tlv/extended_ipv4_reachability/prefixes/prefix/subTLVs/subTLVs/prefix_sid/sid (list) YANG Description: Prefix Segment-ID list. IGP-Prefix Segment is an IGP segment attached to an IGP prefix. An IGP-Prefix Segment is global (unless explicitly advertised otherwise) within the SR/IGP domain. """ return self.__sid def _set_sid(self, v, load=False): """ Setter method for sid, mapped from YANG variable /network_instances/network_instance/protocols/protocol/isis/levels/level/link_state_database/lsp/tlvs/tlv/extended_ipv4_reachability/prefixes/prefix/subTLVs/subTLVs/prefix_sid/sid (list) If this variable is read-only (config: false) in the source YANG file, then _set_sid is considered as a private method. Backends looking to populate this variable should do so via calling thisObj._set_sid() directly. YANG Description: Prefix Segment-ID list. IGP-Prefix Segment is an IGP segment attached to an IGP prefix. An IGP-Prefix Segment is global (unless explicitly advertised otherwise) within the SR/IGP domain. """ if hasattr(v, "_utype"): v = v._utype(v) try: t = YANGDynClass( v, base=YANGListType( False, sid.sid, yang_name="sid", parent=self, is_container="list", user_ordered=False, path_helper=self._path_helper, yang_keys="False", extensions=None, ), is_container="list", yang_name="sid", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions=None, namespace="http://openconfig.net/yang/network-instance", defining_module="openconfig-network-instance", yang_type="list", is_config=False, ) except (TypeError, ValueError): raise ValueError( { "error-string": """sid must be of a type compatible with list""", "defined-type": "list", "generated-type": """YANGDynClass(base=YANGListType(False,sid.sid, yang_name="sid", parent=self, is_container='list', user_ordered=False, path_helper=self._path_helper, yang_keys='False', extensions=None), is_container='list', yang_name="sid", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions=None, namespace='http://openconfig.net/yang/network-instance', defining_module='openconfig-network-instance', yang_type='list', is_config=False)""", } ) self.__sid = t if hasattr(self, "_set"): self._set() def _unset_sid(self): self.__sid = YANGDynClass( base=YANGListType( False, sid.sid, yang_name="sid", parent=self, is_container="list", user_ordered=False, path_helper=self._path_helper, yang_keys="False", extensions=None, ), is_container="list", yang_name="sid", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions=None, namespace="http://openconfig.net/yang/network-instance", defining_module="openconfig-network-instance", yang_type="list", is_config=False, ) sid = __builtin__.property(_get_sid) _pyangbind_elements = OrderedDict([("sid", sid)]) from . import sid class prefix_sid(PybindBase): """ This class was auto-generated by the PythonClass plugin for PYANG from YANG module openconfig-network-instance-l2 - based on the path /network-instances/network-instance/protocols/protocol/isis/levels/level/link-state-database/lsp/tlvs/tlv/extended-ipv4-reachability/prefixes/prefix/subTLVs/subTLVs/prefix-sid. Each member element of the container is represented as a class variable - with a specific YANG type. YANG Description: This container defines segment routing extensions for prefixes. """ __slots__ = ("_path_helper", "_extmethods", "__sid") _yang_name = "prefix-sid" _pybind_generated_by = "container" def __init__(self, *args, **kwargs): self._path_helper = False self._extmethods = False self.__sid = YANGDynClass( base=YANGListType( False, sid.sid, yang_name="sid", parent=self, is_container="list", user_ordered=False, path_helper=self._path_helper, yang_keys="False", extensions=None, ), is_container="list", yang_name="sid", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions=None, namespace="http://openconfig.net/yang/network-instance", defining_module="openconfig-network-instance", yang_type="list", is_config=False, ) load = kwargs.pop("load", None) if args: if len(args) > 1: raise TypeError("cannot create a YANG container with >1 argument") all_attr = True for e in self._pyangbind_elements: if not hasattr(args[0], e): all_attr = False break if not all_attr: raise ValueError("Supplied object did not have the correct attributes") for e in self._pyangbind_elements: nobj = getattr(args[0], e) if nobj._changed() is False: continue setmethod = getattr(self, "_set_%s" % e) if load is None: setmethod(getattr(args[0], e)) else: setmethod(getattr(args[0], e), load=load) def _path(self): if hasattr(self, "_parent"): return self._parent._path() + [self._yang_name] else: return [ "network-instances", "network-instance", "protocols", "protocol", "isis", "levels", "level", "link-state-database", "lsp", "tlvs", "tlv", "extended-ipv4-reachability", "prefixes", "prefix", "subTLVs", "subTLVs", "prefix-sid", ] def _get_sid(self): """ Getter method for sid, mapped from YANG variable /network_instances/network_instance/protocols/protocol/isis/levels/level/link_state_database/lsp/tlvs/tlv/extended_ipv4_reachability/prefixes/prefix/subTLVs/subTLVs/prefix_sid/sid (list) YANG Description: Prefix Segment-ID list. IGP-Prefix Segment is an IGP segment attached to an IGP prefix. An IGP-Prefix Segment is global (unless explicitly advertised otherwise) within the SR/IGP domain. """ return self.__sid def _set_sid(self, v, load=False): """ Setter method for sid, mapped from YANG variable /network_instances/network_instance/protocols/protocol/isis/levels/level/link_state_database/lsp/tlvs/tlv/extended_ipv4_reachability/prefixes/prefix/subTLVs/subTLVs/prefix_sid/sid (list) If this variable is read-only (config: false) in the source YANG file, then _set_sid is considered as a private method. Backends looking to populate this variable should do so via calling thisObj._set_sid() directly. YANG Description: Prefix Segment-ID list. IGP-Prefix Segment is an IGP segment attached to an IGP prefix. An IGP-Prefix Segment is global (unless explicitly advertised otherwise) within the SR/IGP domain. """ if hasattr(v, "_utype"): v = v._utype(v) try: t = YANGDynClass( v, base=YANGListType( False, sid.sid, yang_name="sid", parent=self, is_container="list", user_ordered=False, path_helper=self._path_helper, yang_keys="False", extensions=None, ), is_container="list", yang_name="sid", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions=None, namespace="http://openconfig.net/yang/network-instance", defining_module="openconfig-network-instance", yang_type="list", is_config=False, ) except (TypeError, ValueError): raise ValueError( { "error-string": """sid must be of a type compatible with list""", "defined-type": "list", "generated-type": """YANGDynClass(base=YANGListType(False,sid.sid, yang_name="sid", parent=self, is_container='list', user_ordered=False, path_helper=self._path_helper, yang_keys='False', extensions=None), is_container='list', yang_name="sid", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions=None, namespace='http://openconfig.net/yang/network-instance', defining_module='openconfig-network-instance', yang_type='list', is_config=False)""", } ) self.__sid = t if hasattr(self, "_set"): self._set() def _unset_sid(self): self.__sid = YANGDynClass( base=YANGListType( False, sid.sid, yang_name="sid", parent=self, is_container="list", user_ordered=False, path_helper=self._path_helper, yang_keys="False", extensions=None, ), is_container="list", yang_name="sid", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions=None, namespace="http://openconfig.net/yang/network-instance", defining_module="openconfig-network-instance", yang_type="list", is_config=False, ) sid = __builtin__.property(_get_sid) _pyangbind_elements = OrderedDict([("sid", sid)])
"""Integration tests for Hover""" import json import re from unittest import TestCase from lexicon.tests.providers.integration_tests import IntegrationTestsV2 class HoverProviderTests(TestCase, IntegrationTestsV2): """TestCase for Hover""" provider_name = "hover" domain = "novuslex.com" domain_id = "dom1127777" hoverauth = "0123456789abcdef0123456789abcdef" hover_session = "0123456789abcdef0123456789abcdef" def _filter_post_data_parameters(self): return ["username", "password"] def _filter_headers(self): return ["Cookie"] def _filter_query_parameters(self): return ["hover_session", "hoverauth"] def _replace_auth(self, cookie): cookie = re.sub( "hover_session=.*;", f"hover_session={self.hover_session};", cookie ) cookie = re.sub("hoverauth=.*;", f"hoverauth={self.hoverauth};", cookie) return cookie def _filter_response(self, response): if "basestring" not in globals(): basestring = str if "set-cookie" in response["headers"]: if isinstance(response["headers"]["set-cookie"], basestring): response["headers"]["set-cookie"] = self._replace_auth( response["headers"]["set-cookie"] ) else: for i, cookie in enumerate(response["headers"]["set-cookie"]): response["headers"]["set-cookie"][i] = self._replace_auth(cookie) try: filtered_body = json.loads(response["body"]["string"].decode("UTF-8")) except ValueError: # Body is not json during authentication, so we let it through. # Helper function _request in hover.py will raise exception when # response is not json and it should be. pass else: # filter out my personal contact information if "contact" in filtered_body: del filtered_body["contact"] # if the response is listing all my domains then return a mock response if "domains" in filtered_body and len(filtered_body["domains"]) > 1: filtered_body["domains"] = [ { "id": self.domain_id, "domain_name": self.domain, "status": "active", } ] response["body"]["string"] = json.dumps(filtered_body).encode("UTF-8") return response
"""sc2league URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url, include from django.contrib import admin from rest_framework import routers from sc2league_server.seasons import views as season_views router = routers.DefaultRouter() router.register('seasons', season_views.SeasonViewSet) urlpatterns = [ url('^', include(router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]
#! /usr/bin/env python # -*- encoding: utf-8 -*- # vim:fenc=utf-8: from mole.event import Event from mole.action import Action, ActionSyntaxError sort_asc = lambda x,y:cmp(x,y) sort_des = lambda x,y:cmp(y,x) class ActionSort(Action): """This action sort values in pipeline. :param `fields`: a :class:`list` with the fields names to used as sort key. """ REQUIRE_PARSER = True def __init__(self, fields): self.fields = fields if self.fields is None: raise ActionSyntaxError("No fields provided") def _sort(self, evx, evy): for field in self.fields: if field[0] == "-": field = field[1:] sfunc = sort_des else: sfunc = sort_asc if evx[field] and not evy[field]: return -1 if not evx[field] and evy[field]: return 1 if evx[field] == evy[field]: continue return sfunc(evx[field],evy[field]) return 0 def __call__(self, pipeline): return sorted(pipeline,self._sort)
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. # # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is # regenerated. # -------------------------------------------------------------------------- from .sub_resource_py3 import SubResource class StorageAccountInformation(SubResource): """Azure Storage account information. Variables are only populated by the server, and will be ignored when sending a request. :ivar id: The resource identifier. :vartype id: str :ivar name: The resource name. :vartype name: str :ivar type: The resource type. :vartype type: str :ivar suffix: The optional suffix for the storage account. :vartype suffix: str """ _validation = { 'id': {'readonly': True}, 'name': {'readonly': True}, 'type': {'readonly': True}, 'suffix': {'readonly': True}, } _attribute_map = { 'id': {'key': 'id', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'type': {'key': 'type', 'type': 'str'}, 'suffix': {'key': 'properties.suffix', 'type': 'str'}, } def __init__(self, **kwargs) -> None: super(StorageAccountInformation, self).__init__(**kwargs) self.suffix = None
# # rpclib - Copyright (C) Rpclib contributors. # # This library 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 2.1 of the License, or (at your option) any later version. # # This library 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. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 # """This module contains the utility methods that convert an ElementTree hierarchy to python dicts and vice versa. """ from lxml import etree from rpclib.util.odict import odict def root_dict_to_etree(d): """Converts a dictionary to an xml hiearchy. Just like a valid xml document, the dictionary must have a single element. The format of the child dictionaries is the same as :func:`dict_to_etree`. """ assert len(d) == 1 key, = d.keys() retval = etree.Element(key) for val in d.values(): break if isinstance(val, dict) or isinstance(val, odict): dict_to_etree(val, retval) else: for a in val: dict_to_etree(a, retval) return retval def dict_to_etree(d, parent): """Takes a the dict whose value is either None or an instance of dict, odict or an iterable. The iterables can contain either other dicts/odicts or str/unicode instances. """ for k, v in d.items(): if v is None or len(v) == 0: etree.SubElement(parent, k) elif isinstance(v, dict) or isinstance(v, odict): child = etree.SubElement(parent, k) dict_to_etree(v, child) else: for e in v: child=etree.SubElement(parent, k) if isinstance(e, dict) or isinstance(e, odict): dict_to_etree(e, child) else: child.text=str(e) def root_etree_to_dict(element, iterable=(list, list.append)): """Takes an xml root element and returns the corresponding dict. The second argument is a pair of iterable type and the function used to add elements to the iterable. The xml attributes are ignored. """ return {element.tag: iterable[0]([etree_to_dict(element, iterable)])} def etree_to_dict(element, iterable=(list, list.append)): """Takes an xml root element and returns the corresponding dict. The second argument is a pair of iterable type and the function used to add elements to the iterable. The xml attributes are ignored. """ if (element.text is None) or element.text.isspace(): retval = odict() for elt in element: if not (elt.tag in retval): retval[elt.tag] = iterable[0]() iterable[1](retval[elt.tag], etree_to_dict(elt, iterable)) else: retval = element.text return retval def etree_strip_namespaces(element): """Removes any namespace information form the given element recursively.""" retval = etree.Element(element.tag.rpartition('}')[-1]) retval.text = element.text for a in element.attrib: retval.attrib[a.rpartition('}')[-1]] = element.attrib[a] for e in element: retval.append(etree_strip_namespaces(e)) return retval
from django.shortcuts import get_object_or_404, render from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse from django.views import generic from .models import Choice, Question class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_question_list' def get_queryset(self): """Return the last five published questions.""" return Question.objects.order_by('-pub_date')[:5] class DetailView(generic.DetailView): model = Question template_name = 'polls/detail.html' class ResultsView(generic.DetailView): model = Question template_name = 'polls/results.html' def vote(request, question_id): p = get_object_or_404(Question, pk=question_id) try: selected_choice = p.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): # Redisplay the question voting form. return render(request, 'polls/detail.html', { 'question': p, 'error_message': "You didn't select a choice.", }) else: selected_choice.votes += 1 selected_choice.save() # Always return an HttpResponseRedirect after successfully dealing # with POST data. This prevents data from being posted twice if a # user hits the Back button. return HttpResponseRedirect(reverse('polls:results', args=(p.id,))) def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context)
# -*- coding: utf-8 -*- # pylint: disable=too-many-arguments,redefined-outer-name import copy import os import pytest import psutil import gevent from gevent import Greenlet from raiden.app import App from raiden.api.rest import RestAPI, APIServer from raiden.api.python import RaidenAPI from raiden.raiden_service import RaidenService from raiden.network.discovery import Discovery from raiden.tests.utils.apitestcontext import ApiTestContext def wait_for_listening_port(port_number, tries=10, sleep=0.1): for _ in range(tries): gevent.sleep(sleep) connections = psutil.net_connections() for conn in connections: if conn.status == 'LISTEN' and conn.laddr[1] == port_number: return raise RuntimeError('{port} is not bound'.format(port_number)) # TODO: Figure out why this fixture can't work as session scoped # What happens is that after one test is done, in the next one # the server is no longer running even though the teardown has not # been invoked. @pytest.fixture def api_backend(rest_api_port_number): # Initializing it without raiden_service.api here since that is a # function scope fixture. We will inject it to rest_api object later rest_api = RestAPI(None) api_server = APIServer(rest_api) api_server.flask_app.config['SERVER_NAME'] = 'localhost:{}'.format(rest_api_port_number) # TODO: Find out why tests fail with debug=True server = Greenlet.spawn( api_server.run, rest_api_port_number, debug=False, use_evalex=False, ) # Fixes flaky test, were requests are done prior to the server initializing # the listening socket. # https://github.com/raiden-network/raiden/issues/389#issuecomment-305551563 wait_for_listening_port(rest_api_port_number) yield api_server, rest_api server.kill(block=True, timeout=10) @pytest.fixture def api_raiden_service( monkeypatch, api_backend, api_test_context, blockchain_services, transport_class, max_unresponsive_time, send_ping_time, reveal_timeout, raiden_udp_ports, tmpdir): blockchain = blockchain_services[0] config = copy.deepcopy(App.default_config) config['port'] = raiden_udp_ports[0] config['host'] = '127.0.0.1' config['privatekey_hex'] = blockchain.private_key.encode('hex') config['send_ping_time'] = send_ping_time config['max_unresponsive_time'] = max_unresponsive_time config['reveal_timeout'] = reveal_timeout config['database_path'] = os.path.join(tmpdir.strpath, 'database.db') raiden_service = RaidenService( blockchain, blockchain.private_key, transport_class, Discovery(), config ) api = RaidenAPI(raiden_service) monkeypatch.setattr(api, 'get_channel_list', api_test_context.query_channels) monkeypatch.setattr(api, 'get_tokens_list', api_test_context.query_tokens) monkeypatch.setattr(api, 'open', api_test_context.open_channel) monkeypatch.setattr(api, 'deposit', api_test_context.deposit) monkeypatch.setattr(api, 'close', api_test_context.close) monkeypatch.setattr(api, 'settle', api_test_context.settle) monkeypatch.setattr(api, 'get_channel', api_test_context.get_channel) monkeypatch.setattr(api, 'get_network_events', api_test_context.get_network_events) monkeypatch.setattr(api, 'get_token_network_events', api_test_context.get_token_network_events) monkeypatch.setattr(api, 'get_channel_events', api_test_context.get_channel_events) monkeypatch.setattr(api, 'transfer', api_test_context.transfer) monkeypatch.setattr(api, 'token_swap', api_test_context.token_swap) monkeypatch.setattr(api, 'expect_token_swap', api_test_context.expect_token_swap) monkeypatch.setattr(api, 'connect_token_network', api_test_context.connect) monkeypatch.setattr(api, 'leave_token_network', api_test_context.leave) # also make sure that the test server's raiden_api uses this mock # raiden service _, raiden_api = api_backend monkeypatch.setattr(raiden_api, 'raiden_api', api) return raiden_service @pytest.fixture def api_test_context(reveal_timeout): return ApiTestContext(reveal_timeout)
from lixian import XunleiClient from lixian_commands.util import * from lixian_cli_parser import * from lixian_config import get_config import lixian_help from getpass import getpass @command_line_parser(help=lixian_help.login) @with_parser(parse_login) @with_parser(parse_logging) def login(args): if args.cookies == '-': args._args['cookies'] = None if len(args) < 1: args.username = args.username or XunleiClient(cookie_path=args.cookies, login=False).get_username() or get_config('username') or raw_input('ID: ') args.password = args.password or get_config('password') or getpass('Password: ') elif len(args) == 1: args.username = args.username or XunleiClient(cookie_path=args.cookies, login=False).get_username() or get_config('username') args.password = args[0] if args.password == '-': args.password = getpass('Password: ') elif len(args) == 2: args.username, args.password = list(args) if args.password == '-': args.password = getpass('Password: ') elif len(args) == 3: args.username, args.password, args.cookies = list(args) if args.password == '-': args.password = getpass('Password: ') elif len(args) > 3: raise RuntimeError('Too many arguments') if not args.username: raise RuntimeError("What's your name?") if args.cookies: print 'Saving login session to', args.cookies else: print 'Testing login without saving session' client = XunleiClient(args.username, args.password, args.cookies)
from slm_lab.agent import net from slm_lab.agent.algorithm import policy_util from slm_lab.agent.algorithm.sarsa import SARSA from slm_lab.agent.algorithm.dqn import DQN from slm_lab.lib import logger, util from slm_lab.lib.decorator import lab_api import numpy as np import torch logger = logger.get_logger(__name__) class HydraDQN(DQN): '''Multi-task DQN with separate state and action processors per environment''' @lab_api def init_nets(self, global_nets=None): '''Initialize nets with multi-task dimensions, and set net params''' # NOTE: Separate init from MultitaskDQN despite similarities so that this implementation can support arbitrary sized state and action heads (e.g. multiple layers) self.state_dims = in_dims = [body.state_dim for body in self.agent.nanflat_body_a] self.action_dims = out_dims = [body.action_dim for body in self.agent.nanflat_body_a] if global_nets is None: NetClass = getattr(net, self.net_spec['type']) self.net = NetClass(self.net_spec, in_dims, out_dims) self.target_net = NetClass(self.net_spec, in_dims, out_dims) self.net_names = ['net', 'target_net'] else: util.set_attr(self, global_nets) self.net_names = list(global_nets.keys()) self.post_init_nets() self.online_net = self.target_net self.eval_net = self.target_net @lab_api def calc_pdparam(self, xs, evaluate=True, net=None): ''' Calculate pdparams for multi-action by chunking the network logits output ''' pdparam = SARSA.calc_pdparam(self, xs, evaluate=evaluate, net=net) return pdparam @lab_api def space_act(self, state_a): '''Non-atomizable act to override agent.act(), do a single pass on the entire state_a instead of composing act() via iteration''' # gather and flatten states = [] for eb, body in util.ndenumerate_nonan(self.agent.body_a): state = state_a[eb] if self.normalize_state: state = policy_util.update_online_stats_and_normalize_state(body, state) states.append(state) xs = [torch.from_numpy(state).float() for state in states] pdparam = self.calc_pdparam(xs, evaluate=False) # use multi-policy. note arg change action_a, action_pd_a = self.action_policy(states, self, self.agent.nanflat_body_a, pdparam) for idx, body in enumerate(self.agent.nanflat_body_a): body.action_tensor, body.action_pd = action_a[idx], action_pd_a[idx] # used for body.action_pd_update later return action_a.cpu().numpy() @lab_api def space_sample(self): '''Samples a batch per body, which may experience different environment''' batch = {k: [] for k in self.body.memory.data_keys} for body in self.agent.nanflat_body_a: body_batch = body.memory.sample() if self.normalize_state: body_batch = policy_util.normalize_states_and_next_states(body, body_batch) body_batch = util.to_torch_batch(body_batch, self.net.device, body.memory.is_episodic) for k, arr in batch.items(): arr.append(body_batch[k]) return batch def calc_q_loss(self, batch): '''Compute the Q value loss for Hydra network by apply the singleton logic on generalized aggregate.''' q_preds = torch.stack(self.net.wrap_eval(batch['states'])) act_q_preds = q_preds.gather(-1, torch.stack(batch['actions']).long().unsqueeze(-1)).squeeze(-1) # Use online_net to select actions in next state online_next_q_preds = torch.stack(self.online_net.wrap_eval(batch['next_states'])) # Use eval_net to calculate next_q_preds for actions chosen by online_net next_q_preds = torch.stack(self.eval_net.wrap_eval(batch['next_states'])) max_next_q_preds = online_next_q_preds.gather(-1, next_q_preds.argmax(dim=-1, keepdim=True)).squeeze(-1) max_q_targets = torch.stack(batch['rewards']) + self.gamma * (1 - torch.stack(batch['dones'])) * max_next_q_preds q_loss = self.net.loss_fn(act_q_preds, max_q_targets) # TODO use the same loss_fn but do not reduce yet for body in self.agent.nanflat_body_a: if 'Prioritized' in util.get_class_name(body.memory): # PER errors = torch.abs(max_q_targets - act_q_preds) body.memory.update_priorities(errors) return q_loss @lab_api def space_train(self): ''' Completes one training step for the agent if it is time to train. i.e. the environment timestep is greater than the minimum training timestep and a multiple of the training_frequency. Each training step consists of sampling n batches from the agent's memory. For each of the batches, the target Q values (q_targets) are computed and a single training step is taken k times Otherwise this function does nothing. ''' if util.in_eval_lab_modes(): self.body.flush() return np.nan clock = self.body.env.clock # main clock tick = util.s_get(self, 'aeb_space.clock').get(clock.max_tick_unit) self.to_train = (tick > self.training_start_step and tick % self.training_frequency == 0) if self.to_train == 1: total_loss = torch.tensor(0.0, device=self.net.device) for _ in range(self.training_epoch): batch = self.space_sample() for _ in range(self.training_batch_epoch): loss = self.calc_q_loss(batch) self.net.training_step(loss=loss, lr_clock=clock) total_loss += loss loss = total_loss / (self.training_epoch * self.training_batch_epoch) # reset self.to_train = 0 for body in self.agent.nanflat_body_a: body.flush() logger.debug(f'Trained {self.name} at epi: {clock.epi}, total_t: {clock.total_t}, t: {clock.t}, total_reward so far: {self.body.memory.total_reward}, loss: {loss:g}') return loss.item() else: return np.nan
# this one doesn't crawl the web, it just and only # takes xml from import os import psycopg2 import uni_to_beta import xml import xmlrpc connect_manu = psycopg2.connect("dbname=manu_tironis user=quellen password=quellen") manu_cursor = connect_manu.cursor() def get_raw_text(): pass def translate(raw_text): uni_to_beta.st(raw_text) pass def get_trigrams(translated): """ :param translated: str :return: """ tokens = translated.split() trigrams = [] for index, word in enumerate(tokens[:-2]): trigram = " ".join(tokens[index], tokens[index + 1], tokens[index + 2]) trigrams.append(trigram) # and now save /the planet:/ by calling bulk save # FIXME FINISH ME!! manu_cursor.executemany('insert into trigrams values (%s') def main(): for directory in os.listdir('here'): for subdir in directory: for file in subdir: if file.endswith("_gk.xml"): xml = file.open.read() raw_text = get_raw_text(xml) translated = translate(raw_text) get_trigrams(translated)
# Copyright (C) 2010 Google Inc. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following disclaimer # in the documentation and/or other materials provided with the # distribution. # * Neither the name of Google Inc. nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """Chromium Mac implementation of the Port interface.""" import logging import signal from webkitpy.layout_tests.port import base _log = logging.getLogger(__name__) class MacPort(base.Port): SUPPORTED_VERSIONS = ('snowleopard', 'lion', 'retina', 'mountainlion', 'mavericks') port_name = 'mac' # FIXME: We treat Retina (High-DPI) devices as if they are running # a different operating system version. This is lame and should be fixed. # Note that the retina versions fallback to the non-retina versions and so no # baselines are shared between retina versions; this keeps the fallback graph as a tree # and maximizes the number of baselines we can share that way. # We also currently only support Retina on 10.8; we need to either upgrade to 10.9 or support both. FALLBACK_PATHS = {} FALLBACK_PATHS['mavericks'] = ['mac'] FALLBACK_PATHS['mountainlion'] = ['mac-mountainlion'] + FALLBACK_PATHS['mavericks'] FALLBACK_PATHS['retina'] = ['mac-retina'] + FALLBACK_PATHS['mountainlion'] FALLBACK_PATHS['lion'] = ['mac-lion'] + FALLBACK_PATHS['mountainlion'] FALLBACK_PATHS['snowleopard'] = ['mac-snowleopard'] + FALLBACK_PATHS['lion'] DEFAULT_BUILD_DIRECTORIES = ('xcodebuild', 'out') CONTENT_SHELL_NAME = 'Content Shell' BUILD_REQUIREMENTS_URL = 'https://code.google.com/p/chromium/wiki/MacBuildInstructions' @classmethod def determine_full_port_name(cls, host, options, port_name): if port_name.endswith('mac'): if host.platform.os_version in ('future',): version = 'mavericks' else: version = host.platform.os_version if host.platform.is_highdpi(): version = 'retina' return port_name + '-' + version return port_name def __init__(self, host, port_name, **kwargs): super(MacPort, self).__init__(host, port_name, **kwargs) self._version = port_name[port_name.index('mac-') + len('mac-'):] assert self._version in self.SUPPORTED_VERSIONS def _modules_to_search_for_symbols(self): return [self._build_path('ffmpegsumo.so')] def check_build(self, needs_http, printer): result = super(MacPort, self).check_build(needs_http, printer) if result: _log.error('For complete Mac build requirements, please see:') _log.error('') _log.error(' http://code.google.com/p/chromium/wiki/MacBuildInstructions') return result def operating_system(self): return 'mac' # # PROTECTED METHODS # def _wdiff_missing_message(self): return 'wdiff is not installed; please install from MacPorts or elsewhere' def path_to_apache(self): return '/usr/sbin/httpd' def path_to_apache_config_file(self): config_file_name = 'apache2-httpd-' + self._apache_version() + '.conf' return self._filesystem.join(self.layout_tests_dir(), 'http', 'conf', config_file_name) def _path_to_driver(self, configuration=None): # FIXME: make |configuration| happy with case-sensitive file systems. return self._build_path_with_configuration(configuration, self.driver_name() + '.app', 'Contents', 'MacOS', self.driver_name()) def _path_to_helper(self): binary_name = 'layout_test_helper' return self._build_path(binary_name) def _path_to_wdiff(self): return 'wdiff'
# Licensed under the GNU General Public License Version 2 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Copyright (C) 2009 # Muayyad Saleh Alsadi <alsadi@ojuba.org> """ This is a module for dealing with removable media NOTE: releasing (unmounting and unlocking) is done when media is destructed """ class MediaDevice(object): """ You should just use acquire() to get the mount point (the implementation is supposed to be smart enough to return mount point when it's already mounted) You don't need to manually call release(), just destruct MediaDevice object and the implementation should do that if needed. """ def __init__(self, media_id): """ media_id argument is the implementation-specific id, provided by MediaManager. """ self._unmount_needed = False self._unlock_needed = False raise NotImplementedError def __del__(self): """ destruct the object, unmount and unlock if needed. no need to re-implement this method when you derive from this class. """ if self._unmount_needed: self.unmount() if self._unlock_needed: self.unlock() def is_removable(self): raise NotImplementedError def is_mounted(self): raise NotImplementedError def is_locked(self): raise NotImplementedError def get_mount_point(self): """return the mount point or None if not mounted""" raise NotImplementedError def lock(self): """return True if lock is successfully acquired.""" raise NotImplementedError def unlock(self): """return True if it was able to release the lock successfully.""" raise NotImplementedError def mount(self): """ mount the device and return the mount point. If it's already mounted, just return the mount point. """ raise NotImplementedError def unmount(self): """unmount the device and return True.""" raise NotImplementedError # no need to re-implement the following methods when you derive from this class def acquire(self): """ return the mount point, lock and mount the device if needed """ self.lock() return self.mount() def release(self): """ unmount and release lock. no need to call this method, just destruct the object. """ self.unlock() return self.unmount() class MediaManager (object): """Just iterate over an instance of this class to get MediaDevice objects""" def __init__(self): raise NotImplementedError def __iter__(self): raise NotImplementedError
#!/usr/bin/env python2 # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>' __docformat__ = 'restructuredtext en' from PyQt5.Qt import QAction from calibre.gui2.actions import InterfaceAction from calibre.gui2.dialogs.quickview import Quickview from calibre.gui2 import error_dialog, gprefs from calibre.gui2.widgets import LayoutButton class QuickviewButton(LayoutButton): # {{{ def __init__(self, gui, quickview_manager): self.qv = quickview_manager qaction = quickview_manager.qaction LayoutButton.__init__(self, I('quickview.png'), _('Quickview'), parent=gui, shortcut=qaction.shortcut().toString()) self.toggled.connect(self.update_state) self.action_toggle = qaction self.action_toggle.triggered.connect(self.toggle) self.action_toggle.changed.connect(self.update_shortcut) def update_state(self, checked): if checked: self.set_state_to_hide() self.qv._show_quickview() else: self.set_state_to_show() self.qv._hide_quickview() def save_state(self): gprefs['quickview visible'] = bool(self.isChecked()) def restore_state(self): if gprefs.get('quickview visible', False): self.toggle() # }}} current_qv_action_pi = None def set_quickview_action_plugin(pi): global current_qv_action_pi current_qv_action_pi = pi def get_quickview_action_plugin(): return current_qv_action_pi class ShowQuickviewAction(InterfaceAction): name = 'Quickview' action_spec = (_('Quickview'), 'quickview.png', None, None) dont_add_to = frozenset(['context-menu-device']) action_type = 'current' current_instance = None def genesis(self): self.gui.keyboard.register_shortcut('Toggle Quickview', _('Toggle Quickview'), description=_('Open/close the Quickview panel/window'), default_keys=('Q',), action=self.qaction, group=self.action_spec[0]) self.focus_action = QAction(self.gui) self.gui.addAction(self.focus_action) self.gui.keyboard.register_shortcut('Focus To Quickview', _('Focus to Quickview'), description=_('Move the focus to the Quickview panel/window'), default_keys=('Shift+Q',), action=self.focus_action, group=self.action_spec[0]) self.focus_action.triggered.connect(self.focus_quickview) self.focus_bl_action = QAction(self.gui) self.gui.addAction(self.focus_bl_action) self.gui.keyboard.register_shortcut('Focus from Quickview', _('Focus from Quickview to the book list'), description=_('Move the focus from Quickview to the book list'), default_keys=('Shift+Alt+Q',), action=self.focus_bl_action, group=self.action_spec[0]) self.focus_bl_action.triggered.connect(self.focus_booklist) self.focus_refresh_action = QAction(self.gui) self.gui.addAction(self.focus_refresh_action) self.gui.keyboard.register_shortcut('Refresh from Quickview', _('Refresh Quickview'), description=_('Refresh the information shown in the Quickview pane'), action=self.focus_refresh_action, group=self.action_spec[0]) self.focus_refresh_action.triggered.connect(self.refill_quickview) self.search_action = QAction(self.gui) self.gui.addAction(self.search_action) self.gui.keyboard.register_shortcut('Search from Quickview', _('Search from Quickview'), description=_('Search for the currently selected Quickview item'), default_keys=('Shift+S',), action=self.search_action, group=self.action_spec[0]) self.search_action.triggered.connect(self.search_quickview) self.search_action.changed.connect(self.set_search_shortcut) self.menuless_qaction.changed.connect(self.set_search_shortcut) self.qv_button = QuickviewButton(self.gui, self) def initialization_complete(self): set_quickview_action_plugin(self) def _hide_quickview(self): ''' This is called only from the QV button toggle ''' if self.current_instance: if not self.current_instance.is_closed: self.current_instance._reject() self.current_instance = None def _show_quickview(self, *args): ''' This is called only from the QV button toggle ''' if self.current_instance: if not self.current_instance.is_closed: self.current_instance._reject() self.current_instance = None if self.gui.current_view() is not self.gui.library_view: error_dialog(self.gui, _('No quickview available'), _('Quickview is not available for books ' 'on the device.')).exec_() return self.qv_button.set_state_to_hide() index = self.gui.library_view.currentIndex() self.current_instance = Quickview(self.gui, index) self.current_instance.reopen_after_dock_change.connect(self.open_quickview) self.set_search_shortcut() self.current_instance.show() self.current_instance.quickview_closed.connect(self.qv_button.set_state_to_show) def set_search_shortcut(self): if self.current_instance and not self.current_instance.is_closed: self.current_instance.addAction(self.focus_bl_action) self.current_instance.set_shortcuts(self.search_action.shortcut().toString(), self.menuless_qaction.shortcut().toString()) def open_quickview(self): ''' QV moved from/to dock. Close and reopen the pane/window. Also called when QV is closed and the user asks to move the focus ''' if self.current_instance and not self.current_instance.is_closed: self.current_instance.reject() self.current_instance = None self.qaction.triggered.emit() def refill_quickview(self): ''' Called when the columns shown in the QV pane might have changed. ''' if self.current_instance and not self.current_instance.is_closed: self.current_instance.refill() def refresh_quickview(self, idx): ''' Called when the data shown in the QV pane might have changed. ''' if self.current_instance and not self.current_instance.is_closed: self.current_instance.refresh(idx) def change_quickview_column(self, idx): ''' Called from the column header context menu to change the QV query column ''' self.focus_quickview() self.current_instance.slave(idx) def library_changed(self, db): ''' If QV is open, close it then reopen it so the columns are correct ''' if self.current_instance and not self.current_instance.is_closed: self.current_instance.reject() self.qaction.triggered.emit() def focus_quickview(self): ''' Used to move the focus to the QV books table. Open QV if needed ''' if not self.current_instance or self.current_instance.is_closed: self.open_quickview() else: self.current_instance.set_focus() def focus_booklist(self): self.gui.activateWindow() self.gui.library_view.setFocus() def search_quickview(self): if not self.current_instance or self.current_instance.is_closed: return self.current_instance.do_search()
""" ------------------------ GILDAS CLASS file reader ------------------------ Read a CLASS file into an :class:`pyspeckit.spectrum.ObsBlock` """ from __future__ import print_function from astropy.extern.six.moves import xrange from astropy.extern.six import iteritems try: import astropy.io.fits as pyfits except ImportError: import pyfits import numpy import numpy as np from numpy import pi from astropy import log # from astropy.time import Time from astropy import units as u import pyspeckit import sys import re try: from astropy.utils.console import ProgressBar except ImportError: ProgressBar = lambda x: None ProgressBar.update = lambda x: None import struct import time # 'range' is needed as a keyword irange = range def print_timing(func): """ Prints execution time of decorated function. Included here because CLASS files can take a little while to read; this should probably be replaced with a progressbar """ def wrapper(*arg,**kwargs): t1 = time.time() res = func(*arg,**kwargs) t2 = time.time() log.info('%s took %0.5g s' % (func.func_name, (t2-t1))) return res wrapper.__doc__ = func.__doc__ return wrapper """ Specification: http://iram.fr/IRAMFR/GILDAS/doc/html/class-html/node58.html """ filetype_dict = {'1A ':'Multiple_IEEE','1 ':'Multiple_Vax','1B ':'Multiple_EEEI', '2A ':'v2','2 ':'v2','2B ':'v2', '9A ':'Single_IEEE','9 ':'Single_Vax','9B ':'Single_EEEI'} fileversion_dict = {'1A ':'v1', '2A ':'v2'} record_lengths = {'1A': 512, '2A': 1024*4} header_id_numbers = {0: 'USER CODE', -1: 'COMMENT', -2: 'GENERAL', -3: 'POSITION', -4: 'SPECTRO', -5: 'BASELINE', -6: 'HISTORY', # -8: 'SWITCH', -10: 'DRIFT', -14: 'CALIBRATION', } header_id_lengths = {-2: 9, # may really be 10? -3: 17, -4: 17, -5: None, # variable length -6: 3, # variable length -14: 25, } # from packages/classic/lib/classic_mod.f90 filedescv2_nw1=14 """ GENERAL integer(kind=obsnum_length) :: num ! [ ] Observation number integer(kind=4) :: ver ! [ ] Version number integer(kind=4) :: teles(3) ! [ ] Telescope name integer(kind=4) :: dobs ! [MJD-60549] Date of observation integer(kind=4) :: dred ! [MJD-60549] Date of reduction integer(kind=4) :: typec ! [ code] Type of coordinates integer(kind=4) :: kind ! [ code] Type of data integer(kind=4) :: qual ! [ code] Quality of data integer(kind=4) :: subscan ! [ ] Subscan number integer(kind=obsnum_length) :: scan ! [ ] Scan number ! Written in the entry real(kind=8) :: ut ! 1-2 [ rad] UT of observation real(kind=8) :: st ! 3-4 [ rad] LST of observation real(kind=4) :: az ! 5 [ rad] Azimuth real(kind=4) :: el ! 6 [ rad] Elevation real(kind=4) :: tau ! 7 [neper] Opacity real(kind=4) :: tsys ! 8 [ K] System temperature real(kind=4) :: time ! 9 [ s] Integration time ! Not in this section in file integer(kind=4) :: xunit ! [ code] X unit (if X coordinates section is present) ! NOT in data --- character(len=12) :: cdobs ! [string] Duplicate of dobs character(len=12) :: cdred ! [string] Duplicate of dred """ keys_lengths = { 'unknown': [ ( 'NUM' ,1,'int32'), # Observation number ( 'VER' ,1,'int32'), # Version number ( 'TELES' ,3,'|S12') , # Telescope name ( 'DOBS' ,1,'int32'), # Date of observation ( 'DRED' ,1,'int32'), # Date of reduction ( 'TYPEC' ,1,'int32'), # Type of coordinates ( 'KIND' ,1,'int32'), # Type of data ( 'QUAL' ,1,'int32'), # Quality of data ( 'SCAN' ,1,'int32'), # Scan number ( 'SUBSCAN' ,1,'int32'), # Subscan number ], 'COMMENT': [ # -1 ('LTEXT',1,'int32'), # integer(kind=4) :: ltext ! Length of comment ('CTEXT',1024/4,'|S1024'), # character ctext*1024 ! Comment string ], 'GENERAL': [ # -2 ( 'UT' ,2,'float64'), # rad UT of observation ( 'ST' ,2,'float64'), # rad LST of observation ( 'AZ' ,1,'float32'), # rad Azimuth ( 'EL' ,1,'float32'), # rad Elevation ( 'TAU' ,1,'float32'), # neper Opacity ( 'TSYS' ,1,'float32'), # K System temperature ( 'TIME' ,1,'float32'), # s Integration time # XUNIT should not be there? #( 'XUNIT' ,1,'int32'), # code X unit (if xcoord_sec is present) ] , 'POSITION': [ # -3 ('SOURC',3,'|S12') , # [ ] Source name ('EPOCH',1,'float32'), # [ ] Epoch of coordinates ('LAM' ,2,'float64'), #[rad] Lambda ('BET' ,2,'float64'), #[rad] Beta ('LAMOF',1,'float32'), # [rad] Offset in Lambda ('BETOF',1,'float32'), # [rad] Offset in Beta ('PROJ' ,1,'int32') , # [rad] Projection system ('SL0P' ,1,'float64'), # lambda of descriptive system # MAY NOT EXIST IN OLD CLASS ('SB0P' ,1,'float64'), # beta of descriptive system # MAY NOT EXIST IN OLD CLASS ('SK0P' ,1,'float64'), # angle of descriptive system # MAY NOT EXIST IN OLD CLASS ], 'SPECTRO': [ # -4 #('align' ,1,'int32'), # [ ] Alignment padding ('LINE' ,3,'|S12'), # [ ] Line name ('RESTF' ,2,'float64'), # [ MHz] Rest frequency ('NCHAN' ,1,'int32'), # [ ] Number of channels ('RCHAN' ,1,'float32'), # [ ] Reference channels ('FRES' ,1,'float32'), # [ MHz] Frequency resolution ('FOFF' ,1,'float32'), # [ MHz] Frequency offset ('VRES' ,1,'float32'), # [km/s] Velocity resolution ('VOFF' ,1,'float32'), # [km/s] Velocity at reference channel ('BAD' ,1,'float32'), # [ ] Blanking value #('ALIGN_1',1,'int32'), # [ ] Alignment padding ('IMAGE' ,2,'float64'), # [ MHz] Image frequency #('ALIGN_2',1,'int32'), # [ ] Alignment padding ('VTYPE' ,1,'int32'), # [code] Type of velocity ('DOPPLER',2,'float64'), # [ ] Doppler factor = -V/c (CLASS convention) ], 'CALIBRATION': [ # -14 ('ALIGN',1,'int32'), # BUFFER (it's a zero - it is not declared in the docs!!!!) ('BEEFF',1,'float32'), # [ ] Beam efficiency ('FOEFF',1,'float32'), # [ ] Forward efficiency ('GAINI',1,'float32'), # [ ] Image/Signal gain ratio ('H2OMM',1,'float32'), # [ mm] Water vapor content ('PAMB',1,'float32'), # [ hPa] Ambient pressure ('TAMB',1,'float32'), # [ K] Ambient temperature ('TATMS',1,'float32'), # [ K] Atmosphere temp. in signal band ('TCHOP',1,'float32'), # [ K] Chopper temperature ('TCOLD',1,'float32'), # [ K] Cold load temperature ('TAUS',1,'float32'), # [neper] Opacity in signal band ('TAUI',1,'float32'), # [neper] Opacity in image band ('TATMI',1,'float32'), # [ K] Atmosphere temp. in image band ('TREC',1,'float32'), # [ K] Receiver temperature ('CMODE',1,'int32'), # [ code] Calibration mode ('ATFAC',1,'float32'), # [ ] Applied calibration factor ('ALTI',1,'float32'), # [ m] Site elevation ('COUNT',3,'3float32'), # [count] Power of Atm., Chopp., Cold ('LCALOF',1,'float32'), # [ rad] Longitude offset for sky measurement ('BCALOF',1,'float32'), # [ rad] Latitude offset for sky measurement ('GEOLONG',1,'float64'), # [ rad] Geographic longitude of observatory # MAY NOT EXIST IN OLD CLASS ('GEOLAT',1,'float64'), # [ rad] Geographic latitude of observatory # MAY NOT EXIST IN OLD CLASS ], 'BASELINE':[ ('DEG',1,'int32'), #! [ ] Degree of last baseline ('SIGFI',1,'float32'), #! [Int. unit] Sigma ('AIRE',1,'float32'), #! [Int. unit] Area under windows ('NWIND',1,'int32'), #! [ ] Number of line windows # WARNING: These should probably have 'n', the second digit, = NWIND # The docs are really unclear about this, they say "W1(MWIND)" ('W1MWIND',1,'float32'), #! [km/s] Lower limits of windows ('W2MWIND',1,'float32'), #! [km/s] Upper limits of windows ('SINUS',3,'float32'), #![] Sinus baseline results ], 'DRIFT':[ # 16? ('FREQ',1,'float64') , #! [ MHz] Rest frequency real(kind=8) :: ('WIDTH',1,'float32'), #! [ MHz] Bandwidth real(kind=4) :: ('NPOIN',1,'int32') , #! [ ] Number of data points integer(kind=4) :: ('RPOIN',1,'float32'), #! [ ] Reference point real(kind=4) :: ('TREF',1,'float32') , #! [ ?] Time at reference real(kind=4) :: ('AREF',1,'float32') , #! [ rad] Angular offset at ref. real(kind=4) :: ('APOS',1,'float32') , #! [ rad] Position angle of drift real(kind=4) :: ('TRES',1,'float32') , #! [ ?] Time resolution real(kind=4) :: ('ARES',1,'float32') , #! [ rad] Angular resolution real(kind=4) :: ('BAD',1,'float32') , #! [ ] Blanking value real(kind=4) :: ('CTYPE',1,'int32') , #! [code] Type of offsets integer(kind=4) :: ('CIMAG',1,'float64'), #! [ MHz] Image frequency real(kind=8) :: ('COLLA',1,'float32'), #! [ ?] Collimation error Az real(kind=4) :: ('COLLE',1,'float32'), #! [ ?] Collimation error El real(kind=4) :: ], } def _read_bytes(f, n): '''Read the next `n` bytes (from idlsave)''' return f.read(n) """ Warning: UNCLEAR what endianness should be! Numpy seemed to get it right, and I think numpy assumes NATIVE endianness """ def _read_byte(f): '''Read a single byte (from idlsave)''' return numpy.uint8(struct.unpack('=B', f.read(4)[:1])[0]) def _read_int16(f): '''Read a signed 16-bit integer (from idlsave)''' return numpy.int16(struct.unpack('=h', f.read(4)[2:4])[0]) def _read_int32(f): '''Read a signed 32-bit integer (from idlsave)''' return numpy.int32(struct.unpack('=i', f.read(4))[0]) def _read_int64(f): '''Read a signed 64-bit integer ''' return numpy.int64(struct.unpack('=q', f.read(8))[0]) def _read_float32(f): '''Read a 32-bit float (from idlsave)''' return numpy.float32(struct.unpack('=f', f.read(4))[0]) def _align_32(f): '''Align to the next 32-bit position in a file (from idlsave)''' pos = f.tell() if pos % 4 != 0: f.seek(pos + 4 - pos % 4) return def _read_word(f,length): if length > 0: chars = _read_bytes(f, length) _align_32(f) else: chars = None return chars def _read_int(f): return struct.unpack('i',f.read(4)) def is_ascii(s): try: s.decode('ascii') return True except UnicodeDecodeError: return False except UnicodeEncodeError: return False def is_all_null(s): return all(x=='\x00' for x in s) """ from clic_file.f90: v1, v2 integer(kind=4) :: bloc ! 1 : observation address [records] integer(kind=8) :: bloc ! 1- 2: observation address [records] integer(kind=4) :: bloc ! 1 : block read from index integer(kind=4) :: num ! 2 : observation number integer(kind=4) :: word ! 3 : address offset [4-bytes] integer(kind=4) :: num ! 2 : number read integer(kind=4) :: ver ! 3 : observation version integer(kind=4) :: ver ! 4 : observation version integer(kind=4) :: ver ! 3 : version read from index integer(kind=4) :: sourc(3) ! 4- 6: source name integer(kind=8) :: num ! 5- 6: observation number character(len=12) :: csour ! 4- 6: source read from index integer(kind=4) :: line(3) ! 7- 9: line name integer(kind=4) :: sourc(3) ! 7- 9: source name character(len=12) :: cline ! 7- 9: line read from index integer(kind=4) :: teles(3) ! 10-12: telescope name integer(kind=4) :: line(3) ! 10-12: line name character(len=12) :: ctele ! 10-12: telescope read from index integer(kind=4) :: dobs ! 13 : observation date [class_date] integer(kind=4) :: teles(3) ! 13-15: telescope name integer(kind=4) :: dobs ! 13 : date obs. read from index integer(kind=4) :: dred ! 14 : reduction date [class_date] integer(kind=4) :: dobs ! 16 : observation date [class_date] integer(kind=4) :: dred ! 14 : date red. read from index real(kind=4) :: off1 ! 15 : lambda offset [radian] integer(kind=4) :: dred ! 17 : reduction date [class_date] real(kind=4) :: off1 ! 15 : read offset 1 real(kind=4) :: off2 ! 16 : beta offset [radian] real(kind=4) :: off1 ! 18 : lambda offset [radian] real(kind=4) :: off2 ! 16 : read offset 2 integer(kind=4) :: typec ! 17 : coordinates types real(kind=4) :: off2 ! 19 : beta offset [radian] integer(kind=4) :: type ! 17 : type of read offsets integer(kind=4) :: kind ! 18 : data kind integer(kind=4) :: typec ! 20 : coordinates types integer(kind=4) :: kind ! 18 : type of observation integer(kind=4) :: qual ! 19 : data quality integer(kind=4) :: kind ! 21 : data kind integer(kind=4) :: qual ! 19 : Quality read from index integer(kind=4) :: scan ! 20 : scan number integer(kind=4) :: qual ! 22 : data quality integer(kind=4) :: scan ! 20 : Scan number read from index integer(kind=4) :: proc ! 21 : procedure type integer(kind=4) :: scan ! 23 : scan number real(kind=4) :: posa ! 21 : Position angle integer(kind=4) :: itype ! 22 : observation type integer(kind=4) :: proc ! 24 : procedure type integer(kind=4) :: subscan ! 22 : Subscan number real(kind=4) :: houra ! 23 : hour angle [radian] integer(kind=4) :: itype ! 25 : observation type integer(kind=4) :: pad(10) ! 23-32: Pad to 32 words integer(kind=4) :: project ! 24 : project name real(kind=4) :: houra ! 26 : hour angle [radian] integer(kind=4) :: pad1 ! 25 : unused word integer(kind=4) :: project(2) ! 27 : project name integer(kind=4) :: bpc ! 26 : baseline bandpass cal status integer(kind=4) :: bpc ! 29 : baseline bandpass cal status integer(kind=4) :: ic ! 27 : instrumental cal status integer(kind=4) :: ic ! 30 : instrumental cal status integer(kind=4) :: recei ! 28 : receiver number integer(kind=4) :: recei ! 31 : receiver number real(kind=4) :: ut ! 29 : UT [s] real(kind=4) :: ut ! 32 : UT [s] integer(kind=4) :: pad2(3) ! 30-32: padding to 32 4-bytes word equivalently integer(kind=obsnum_length) :: num ! [ ] Observation number integer(kind=4) :: ver ! [ ] Version number integer(kind=4) :: teles(3) ! [ ] Telescope name integer(kind=4) :: dobs ! [MJD-60549] Date of observation integer(kind=4) :: dred ! [MJD-60549] Date of reduction integer(kind=4) :: typec ! [ code] Type of coordinates integer(kind=4) :: kind ! [ code] Type of data integer(kind=4) :: qual ! [ code] Quality of data integer(kind=4) :: subscan ! [ ] Subscan number integer(kind=obsnum_length) :: scan ! [ ] Scan number """ """ index.f90: call conv%read%i8(data(1), indl%bloc, 1) ! bloc call conv%read%i4(data(3), indl%word, 1) ! word call conv%read%i8(data(4), indl%num, 1) ! num call conv%read%i4(data(6), indl%ver, 1) ! ver call conv%read%cc(data(7), indl%csour, 3) ! csour call conv%read%cc(data(10),indl%cline, 3) ! cline call conv%read%cc(data(13),indl%ctele, 3) ! ctele call conv%read%i4(data(16),indl%dobs, 1) ! dobs call conv%read%i4(data(17),indl%dred, 1) ! dred call conv%read%r4(data(18),indl%off1, 1) ! off1 call conv%read%r4(data(19),indl%off2, 1) ! off2 call conv%read%i4(data(20),indl%type, 1) ! type call conv%read%i4(data(21),indl%kind, 1) ! kind call conv%read%i4(data(22),indl%qual, 1) ! qual call conv%read%r4(data(23),indl%posa, 1) ! posa call conv%read%i8(data(24),indl%scan, 1) ! scan call conv%read%i4(data(26),indl%subscan,1) ! subscan if (isv3) then call conv%read%r8(data(27),indl%ut, 1) ! ut else """ def _read_indices(f, file_description): #if file_description['version'] in (1,2): # extension_positions = (file_description['aex']-1)*file_description['reclen']*4 # all_indices = {extension: # [_read_index(f, # filetype=file_description['version'], # entry=ii, # #position=position, # ) # for ii in range(file_description['lex1'])] # for extension,position in enumerate(extension_positions) # if position > 0 # } #elif file_description['version'] == 1: extension_positions = ((file_description['aex'].astype('int64')-1) *file_description['reclen']*4) all_indices = [_read_index(f, filetype=file_description['version'], # 1-indexed files entry_number=ii+1, file_description=file_description, ) for ii in range(file_description['xnext']-1)] #else: # raise ValueError("Invalid file version {0}".format(file_description['version'])) return all_indices def _find_index(entry_number, file_description, return_position=False): if file_description['gex'] == 10: kex=(entry_number-1)/file_description['lex1'] + 1 else: # exponential growth: #kex = gi8_dicho(file_description['nex'], file_description['lexn'], entry_number) - 1 kex = len([xx for xx in file_description['lexn'] if xx<entry_number]) ken = entry_number - file_description['lexn'][kex-1] #! Find ken (relative entry number in the extension, starts from 1) #ken = entry_num - file%desc%lexn(kex-1) kb = ((ken-1)*file_description['lind'])/file_description['reclen'] #kb = ((ken-1)*file%desc%lind)/file%desc%reclen ! In the extension, the # ! relative record position (as an offset, starts from 0) where the # ! Entry Index starts. NB: there can be a non-integer number of Entry # ! Indexes per record # Subtract 1: 'aex' is 1-indexed kbl = (file_description['aex'][kex-1]+kb)-1 # kbl = file%desc%aex(kex)+kb ! The absolute record number where the Entry Index goes k = ((ken-1)*file_description['lind']) % file_description['reclen'] #k = mod((ken-1)*file%desc%lind,file%desc%reclen)+1 ! = in the record, the # ! first word of the Entry Index of the entry number 'entry_num' if return_position: return (kbl*file_description['reclen']+k)*4 else: return kbl,k def _read_index(f, filetype='v1', DEBUG=False, clic=False, position=None, entry_number=None, file_description=None): if position is not None: f.seek(position) if entry_number is not None: indpos = _find_index(entry_number, file_description, return_position=True) f.seek(indpos) x0 = f.tell() if filetype in ('1A ','v1', 1): log.debug('Index filetype 1A') index = { "XBLOC":_read_int32(f), "XNUM":_read_int32(f), "XVER":_read_int32(f), "XSOURC":_read_word(f,12), "XLINE":_read_word(f,12), "XTEL":_read_word(f,12), "XDOBS":_read_int32(f), "XDRED":_read_int32(f), "XOFF1":_read_float32(f),# first offset (real, radians) "XOFF2":_read_float32(f),# second offset (real, radians) "XTYPE":_read_int32(f),# coordinate system ('EQ'', 'GA', 'HO') "XKIND":_read_int32(f),# Kind of observation (0: spectral, 1: continuum, ) "XQUAL":_read_int32(f),# Quality (0-9) "XSCAN":_read_int32(f),# Scan number } index['BLOC'] = index['XBLOC'] # v2 compatibility index['WORD'] = 1 # v2 compatibility index['SOURC'] = index['CSOUR'] = index['XSOURC'] index['DOBS'] = index['CDOBS'] = index['XDOBS'] index['CTELE'] = index['XTEL'] index['LINE'] = index['XLINE'] index['OFF1'] = index['XOFF1'] index['OFF2'] = index['XOFF2'] index['QUAL'] = index['XQUAL'] index['SCAN'] = index['XSCAN'] index['KIND'] = index['XKIND'] if clic: # use header set up in clic nextchunk = { "XPROC":_read_int32(f),# "procedure type" "XITYPE":_read_int32(f),# "XHOURANG":_read_float32(f),# "XPROJNAME":_read_int32(f),# "XPAD1":_read_int32(f), "XBPC" :_read_int32(f), "XIC" :_read_int32(f), "XRECEI" :_read_int32(f), "XUT":_read_float32(f), "XPAD2":numpy.fromfile(f,count=3,dtype='int32') # BLANK is NOT ALLOWED!!! It is a special KW } else: nextchunk = {"XPOSA":_read_float32(f), "XSUBSCAN":_read_int32(f), 'XPAD2': numpy.fromfile(f,count=10,dtype='int32'), } nextchunk['SUBSCAN'] = nextchunk['XSUBSCAN'] nextchunk['POSA'] = nextchunk['XPOSA'] index.update(nextchunk) if (f.tell() - x0 != 128): missed_bits = (f.tell()-x0) X = f.read(128-missed_bits) if DEBUG: print("read_index missed %i bits: %s" % (128-missed_bits,X)) #raise IndexError("read_index did not successfully read 128 bytes at %i. Read %i bytes." % (x0,f.tell()-x0)) if any(not is_ascii(index[x]) for x in ('XSOURC','XLINE','XTEL')): raise ValueError("Invalid index read from {0}.".format(x0)) elif filetype in ('2A ','v2', 2): log.debug('Index filetype 2A') index = { "BLOC" : _read_int64(f) , #(data(1), 1) ! bloc "WORD" : _read_int32(f) , #(data(3), 1) ! word "NUM" : _read_int64(f) , #(data(4), 1) ! num "VER" : _read_int32(f) , #(data(6), 1) ! ver "CSOUR" : _read_word(f,12), #(data(7), 3) ! csour "CLINE" : _read_word(f,12), #(data(10), 3) ! cline "CTELE" : _read_word(f,12), #(data(13), 3) ! ctele "DOBS" : _read_int32(f) , #(data(16), 1) ! dobs "DRED" : _read_int32(f) , #(data(17), 1) ! dred "OFF1" : _read_float32(f), #(data(18), 1) ! off1 "OFF2" : _read_float32(f), #(data(19), 1) ! off2 "TYPE" : _read_int32(f) , #(data(20), 1) ! type "KIND" : _read_int32(f) , #(data(21), 1) ! kind "QUAL" : _read_int32(f) , #(data(22), 1) ! qual "POSA" : _read_float32(f), #(data(23), 1) ! posa "SCAN" : _read_int64(f) , #(data(24), 1) ! scan "SUBSCAN": _read_int32(f) , #(data(26), 1) ! subscan } #last24bits = f.read(24) #log.debug("Read 24 bits: '{0}'".format(last24bits)) if any((is_all_null(index[x]) or not is_ascii(index[x])) for x in ('CSOUR','CLINE','CTELE')): raise ValueError("Invalid index read from {0}.".format(x0)) index['SOURC'] = index['XSOURC'] = index['CSOUR'] index['LINE'] = index['XLINE'] = index['CLINE'] index['XKIND'] = index['KIND'] try: index['DOBS'] = index['XDOBS'] = index['CDOBS'] except KeyError: index['CDOBS'] = index['XDOBS'] = index['DOBS'] else: raise NotImplementedError("Filetype {0} not implemented.".format(filetype)) # from kernel/lib/gsys/date.f90: gag_julda class_dobs = index['DOBS'] index['DOBS'] = ((class_dobs + 365*2025)/365.2425 + 1) # SLOW #index['DATEOBS'] = Time(index['DOBS'], format='jyear') #index['DATEOBSS'] = index['DATEOBS'].iso log.debug("Indexing finished at {0}".format(f.tell())) return index def _read_header(f, type=0, position=None): """ Read a header entry from a CLASS file (helper function) """ if position is not None: f.seek(position) if type in keys_lengths: hdrsec = [(x[0],numpy.fromfile(f,count=1,dtype=x[2])[0]) for x in keys_lengths[type]] return dict(hdrsec) else: return {} raise ValueError("Unrecognized type {0}".format(type)) def _read_first_record(f): f.seek(0) filetype = f.read(4) if fileversion_dict[filetype] == 'v1': return _read_first_record_v1(f) else: return _read_first_record_v2(f) def _read_first_record_v1(f, record_length_words=128): r""" Position & Parameter & Fortran Kind & Purpose \\ \hline 1 & {\tt code} & Character*4 & File code \\ 2 & {\tt next} & Integer*4 & Next free record \\ 3 & {\tt lex} & Integer*4 & Length of first extension (number of entries) \\ 4 & {\tt nex} & Integer*4 & Number of extensions \\ 5 & {\tt xnext} & Integer*4 & Next available entry number \\ 6:2*{\tt reclen} & {\tt ex(:)} & Integer*4 & Array of extension addresses from classic_mod.f90: integer(kind=4) :: code ! 1 File code integer(kind=4) :: next ! 2 Next free record integer(kind=4) :: lex ! 3 Extension length (number of entries) integer(kind=4) :: nex ! 4 Number of extensions integer(kind=4) :: xnext ! 5 Next available entry number integer(kind=4) :: aex(mex_v1) ! 6:256 Extension addresses from old (<dec2013) class, file.f90: read(ilun,rec=1,err=11,iostat=ier) ibx%code,ibx%next, & & ibx%ilex,ibx%imex,ibx%xnext also uses filedesc_v1tov2 from classic/lib/file.f90 """ # OLD NOTES # hdr = header # hdr.update(obshead) # re-overwrite things # hdr.update({'OBSNUM':obsnum,'RECNUM':spcount}) # hdr.update({'RA':hdr['LAM']/pi*180,'DEC':hdr['BET']/pi*180}) # hdr.update({'RAoff':hdr['LAMOF']/pi*180,'DECoff':hdr['BETOF']/pi*180}) # hdr.update({'OBJECT':hdr['SOURC'].strip()}) # hdr.update({'BUNIT':'Tastar'}) # hdr.update({'EXPOSURE':hdr['TIME']}) f.seek(0) file_description = { 'code': f.read(4), 'next': _read_int32(f), 'lex': _read_int32(f), 'nex': _read_int32(f), 'xnext': _read_int32(f), 'gex': 10., 'vind': 1, # classic_vind_v1 packages/classic/lib/classic_mod.f90 'version': 1, 'nextrec': 3, 'nextword': 1, 'lind': 32, #classic_lind_v1 packages/classic/lib/classic_mod.f90 'kind': 'unknown', 'flags': 0, } file_description['reclen'] = record_length_words # should be 128w = 512 bytes ex = np.fromfile(f, count=(record_length_words*2-5), dtype='int32') file_description['ex'] = ex[ex!=0] file_description['nextrec'] = file_description['next'] # this can't be... file_description['lex1'] = file_description['lex'] # number of entries file_description['lexn'] = (np.arange(file_description['nex']+1) * file_description['lex1']) file_description['nentries'] = np.sum(file_description['lexn']) file_description['aex'] = file_description['ex'][:file_description['nex']] #file_description['version'] = fileversion_dict[file_description['code']] assert f.tell() == 1024 # Something is not quite right with the 'ex' parsing #assert len(file_description['ex']) == file_description['nex'] return file_description def _read_first_record_v2(f): r""" packages/classic/lib/file.f90 Position & Parameter & Fortran Kind & Purpose & Unit \\ \hline 1 & {\tt code} & Character*4 & File code & - \\ 2 & {\tt reclen} & Integer*4 & Record length & words \\ 3 & {\tt kind} & Integer*4 & File kind & - \\ 4 & {\tt vind} & Integer*4 & Index version & - \\ 5 & {\tt lind} & Integer*4 & Index length & words \\ 6 & {\tt flags} & Integer*4 & Bit flags. \#1: single or multiple, & - \\ & & & \#2-32: provision (0-filled) & \\ \hline 7:8 & {\tt xnext} & Integer*8 & Next available entry number & - \\ 9:10 & {\tt nextrec} & Integer*8 & Next record which contains free space & record \\ 11 & {\tt nextword} & Integer*4 & Next free word in this record & word \\ \hline 12 & {\tt lex1} & Integer*4 & Length of first extension index & entries \\ 13 & {\tt nex} & Integer*4 & Number of extensions & - \\ 14 & {\tt gex} & Integer*4 & Extension growth rule & - \\ 15:{\tt reclen} & {\tt aex(:)} & Integer*8 & Array of extension addresses & record """ f.seek(0) file_description = { 'code': f.read(4), 'reclen': _read_int32(f), 'kind': _read_int32(f), 'vind': _read_int32(f), 'lind': _read_int32(f), 'flags': _read_int32(f), 'xnext': _read_int64(f), 'nextrec': _read_int64(f), 'nextword': _read_int32(f), 'lex1': _read_int32(f), 'nex': _read_int32(f), 'gex': _read_int32(f), } file_description['lexn'] = [0] if file_description['gex'] == 10: for ii in range(1, file_description['nex']+1): file_description['lexn'].append(file_description['lexn'][-1]+file_description['lex1']) else: #! Exponential growth. Only growth with mantissa 2.0 is supported for ii in range(1, file_description['nex']): # I don't know what the fortran does here!!! # ahh, maybe 2_8 means int(2, dtype='int64') nent = int(file_description['lex1'] * 2**(ii-1)) #nent = int(file%desc%lex1,kind=8) * 2_8**(iex-1) file_description['lexn'].append(file_description['lexn'][-1]+nent) #file%desc%lexn(iex) = file%desc%lexn(iex-1) + nent file_description['nentries'] = np.sum(file_description['lexn']) record_length_words = file_description['reclen'] aex = numpy.fromfile(f, count=(record_length_words-15)/2, dtype='int64') file_description['aex'] = aex[aex!=0] assert len(file_description['aex']) == file_description['nex'] file_description['version'] = 2 return file_description def gi8_dicho(ninp,lexn,xval,ceil=True): """ ! @ public ! Find ival such as ! X(ival-1) < xval <= X(ival) (ceiling mode) ! or ! X(ival) <= xval < X(ival+1) (floor mode) ! for input data ordered. Use a dichotomic search for that. call gi8_dicho(nex,file%desc%lexn,entry_num,.true.,kex,error) """ #integer(kind=size_length), intent(in) :: np ! Number of input points #integer(kind=8), intent(in) :: x(np) ! Input ordered Values #integer(kind=8), intent(in) :: xval ! The value we search for #logical, intent(in) :: ceil ! Ceiling or floor mode? #integer(kind=size_length), intent(out) :: ival ! Position in the array #logical, intent(inout) :: error ! Logical error flag iinf = 1 isup = ninp #! Ceiling mode while isup > (iinf+1): imid = int(np.floor((isup + iinf)/2.)) if (lexn[imid-1] < xval): iinf = imid else: isup = imid ival = isup return ival def _read_obshead(f, file_description, position=None): if file_description['version'] == 1: return _read_obshead_v1(f, position=position) if file_description['version'] == 2: return _read_obshead_v2(f, position=position) else: raise ValueError("Invalid file version {0}.". format(file_description['version'])) def _read_obshead_v2(f, position=None): """ ! Version 2 (public) integer(kind=4), parameter :: entrydescv2_nw1=11 ! Number of words, in 1st part integer(kind=4), parameter :: entrydescv2_nw2=5 ! Number of words for 1 section in 2nd part type classic_entrydesc_t sequence integer(kind=4) :: code ! 1 : code observation icode integer(kind=4) :: version ! 2 : observation version integer(kind=4) :: nsec ! 3 : number of sections integer(kind=4) :: pad1 ! - : memory padding (not in data) integer(kind=8) :: nword ! 4- 5: number of words integer(kind=8) :: adata ! 6- 7: data address integer(kind=8) :: ldata ! 8- 9: data length integer(kind=8) :: xnum ! 10-11: entry number ! Out of the 'sequence' block: integer(kind=4) :: msec ! Not in data: maximum number of sections the ! Observation Index can hold integer(kind=4) :: pad2 ! Memory padding for 8 bytes alignment integer(kind=4) :: seciden(classic_maxsec) ! Section Numbers (on disk: 1 to ed%nsec) integer(kind=8) :: secleng(classic_maxsec) ! Section Lengths (on disk: 1 to ed%nsec) integer(kind=8) :: secaddr(classic_maxsec) ! Section Addresses (on disk: 1 to ed%nsec) end type classic_entrydesc_t """ if position is not None: f.seek(position) else: position = f.tell() IDcode = f.read(4) if IDcode.strip() != '2': raise IndexError("Observation Header reading failure at {0}. " "Record does not appear to be an observation header.". format(position)) f.seek(position) entrydescv2_nw1=11 entrydescv2_nw2=5 obshead = { 'CODE': f.read(4), 'VERSION': _read_int32(f), 'NSEC': _read_int32(f), #'_blank': _read_int32(f), 'NWORD': _read_int64(f), 'ADATA': _read_int64(f), 'LDATA': _read_int64(f), 'XNUM': _read_int64(f), #'MSEC': _read_int32(f), #'_blank2': _read_int32(f), } section_numbers = np.fromfile(f, count=obshead['NSEC'], dtype='int32') section_lengths = np.fromfile(f, count=obshead['NSEC'], dtype='int64') section_addresses = np.fromfile(f, count=obshead['NSEC'], dtype='int64') return obshead['XNUM'],obshead,dict(zip(section_numbers,section_addresses)) def _read_obshead_v1(f, position=None, verbose=False): """ Read the observation header of a CLASS file (helper function for read_class; should not be used independently) """ if position is not None: f.seek(position) IDcode = f.read(4) if IDcode.strip() != '2': raise IndexError("Observation Header reading failure at {0}. " "Record does not appear to be an observation header.". format(f.tell() - 4)) (nblocks, nbyteob, data_address, nheaders, data_length, obindex, nsec, obsnum) = numpy.fromfile(f, count=8, dtype='int32') if verbose: print("nblocks,nbyteob,data_address,data_length,nheaders,obindex,nsec,obsnum",nblocks,nbyteob,data_address,data_length,nheaders,obindex,nsec,obsnum) print("DATA_LENGTH: ",data_length) seccodes = numpy.fromfile(f,count=nsec,dtype='int32') # Documentation says addresses then length: It is apparently wrong seclen = numpy.fromfile(f,count=nsec,dtype='int32') secaddr = numpy.fromfile(f,count=nsec,dtype='int32') if verbose: print("Section codes, addresses, lengths: ",seccodes,secaddr,seclen) hdr = {'NBLOCKS':nblocks, 'NBYTEOB':nbyteob, 'DATAADDR':data_address, 'DATALEN':data_length, 'NHEADERS':nheaders, 'OBINDEX':obindex, 'NSEC':nsec, 'OBSNUM':obsnum} #return obsnum,seccodes return obsnum,hdr,dict(zip(seccodes,secaddr)) # THIS IS IN READ_OBSHEAD!!! # def _read_preheader(f): # """ # Not entirely clear what this is, but it is stuff that precedes the actual data # # Looks something like this: # array([ 1, -2, -3, -4, -14, # 9, 17, 18, 25, 55, # 64, 81, 99, -1179344801, 979657591, # # -2, -3, -4, -14 indicate the 4 header types # 9,17,18,25 *MAY* indicate the number of bytes in each # # # HOW is it indicated how many entries there are? # """ # # 13 comes from counting 1, -2,....99 above # numbers = np.fromfile(f, count=13, dtype='int32') # sections = [n for n in numbers if n in header_id_numbers] # return sections def downsample_1d(myarr,factor,estimator=np.mean, weight=None): """ Downsample a 1D array by averaging over *factor* pixels. Crops right side if the shape is not a multiple of factor. This code is pure numpy and should be fast. keywords: estimator - default to mean. You can downsample by summing or something else if you want a different estimator (e.g., downsampling error: you want to sum & divide by sqrt(n)) weight: np.ndarray An array of weights to use for the downsampling. If None, assumes uniform 1 """ if myarr.ndim != 1: raise ValueError("Only works on 1d data. Says so in the title.") xs = myarr.size crarr = myarr[:xs-(xs % int(factor))] if weight is None: dsarr = estimator(np.concatenate([[crarr[i::factor] for i in range(factor)]]),axis=0) else: dsarr = estimator(np.concatenate([[crarr[i::factor]*weight[i::factor] for i in range(factor)]]),axis=0) warr = estimator(np.concatenate([[weight[i::factor] for i in range(factor)]]),axis=0) dsarr = dsarr/warr return dsarr # unit test def test_downsample1d(): data = np.arange(10) weight = np.ones(10) weight[5]=0 assert np.all(downsample_1d(data, 2, weight=weight, estimator=np.mean) == np.array([ 0.5, 2.5, 4. , 6.5, 8.5])) def read_observation(f, obsid, file_description=None, indices=None, my_memmap=None, memmap=True): if isinstance(f, str): f = open(f,'rb') opened = True if memmap: my_memmap = numpy.memmap(filename, offset=0, dtype='float32', mode='r') else: my_memmap = None elif my_memmap is None and memmap: raise ValueError("Must pass in a memmap object if passing in a file object.") else: opened = False if file_description is None: file_description = _read_first_record(f) if indices is None: indices = _read_indices(f, file_description) index = indices[obsid] obs_position = (index['BLOC']-1)*file_description['reclen']*4 + (index['WORD']-1)*4 obsnum,obshead,sections = _read_obshead(f, file_description, position=obs_position) header = obshead datastart = 0 for section_id,section_address in iteritems(sections): # Section addresses are 1-indexed byte addresses # in the current "block" sec_position = obs_position + (section_address-1)*4 temp_hdr = _read_header(f, type=header_id_numbers[section_id], position=sec_position) header.update(temp_hdr) datastart = max(datastart,f.tell()) hdr = header hdr.update(obshead) # re-overwrite things hdr.update({'OBSNUM':obsnum,'RECNUM':obsid}) hdr.update({'RA':hdr['LAM']/pi*180,'DEC':hdr['BET']/pi*180}) hdr.update({'RAoff':hdr['LAMOF']/pi*180,'DECoff':hdr['BETOF']/pi*180}) hdr.update({'OBJECT':hdr['SOURC'].strip()}) hdr.update({'BUNIT':'Tastar'}) hdr.update({'EXPOSURE':float(hdr['TIME'])}) hdr['HDRSTART'] = obs_position hdr['DATASTART'] = datastart hdr.update(indices[obsid]) # Apparently the data are still valid in this case? #if hdr['XNUM'] != obsid+1: # log.error("The spectrum read was {0} but {1} was requested.". # format(hdr['XNUM']-1, obsid)) if hdr['KIND'] == 1: # continuum nchan = hdr['NPOIN'] elif 'NCHAN' in hdr: nchan = hdr['NCHAN'] else: log.error("No NCHAN in header. This is not a spectrum.") import ipdb; ipdb.set_trace() # There may be a 1-channel offset? CHECK!!! # (changed by 1 pixel - October 14, 2014) # (changed back - October 21, 2014 - I think the ends are just bad, but not # zero.) f.seek(datastart-1) spec = _read_spectrum(f, position=datastart-1, nchan=nchan, memmap=memmap, my_memmap=my_memmap) if opened: f.close() return spec, hdr def _read_spectrum(f, position, nchan, my_memmap=None, memmap=True): if position != f.tell(): log.warn("Reading data from {0}, but the file is wound " "to {1}.".format(position, f.tell())) if memmap: here = position #spectrum = numpy.memmap(filename, offset=here, dtype='float32', # mode='r', shape=(nchan,)) spectrum = my_memmap[here/4:here/4+nchan] f.seek(here+nchan*4) else: f.seek(position) spectrum = numpy.fromfile(f,count=nchan,dtype='float32') return spectrum def _spectrum_from_header(fileobj, header, memmap=None): return _read_spectrum(fileobj, position=header['DATASTART'], nchan=header['NCHAN'] if 'NCHAN' in hdr else hdr['NPOIN'], my_memmap=memmap) def clean_header(header): newheader = {} for k in header: if not isinstance(header[k], (int, float, str)): if isinstance(header[k], np.ndarray) and header[k].size > 1: if header[k].size > 10: raise ValueError("Large array being put in header. That's no good. key={0}".format(k)) for ii,val in enumerate(header[k]): newheader[k[:7]+str(ii)] = val else: newheader[k[:8]] = str(header[k]) else: newheader[k[:8]] = header[k] return newheader class ClassObject(object): def __init__(self, filename, verbose=False): t0 = time.time() self._file = open(filename, 'rb') self.file_description = _read_first_record(self._file) self.allind = _read_indices(self._file, self.file_description) self._data = np.memmap(self._file, dtype='float32', mode='r') if verbose: log.info("Setting _spectra") self._spectra = LazyItem(self) t1 = time.time() if verbose: log.info("Setting posang. t={0}".format(t1-t0)) self.set_posang() t2 = time.time() if verbose: log.info("Identifying otf scans. t={0}".format(t2-t1)) self._identify_otf_scans(verbose=verbose) t3 = time.time() #self._load_all_spectra() if verbose: log.info("Loaded CLASS object with {3} indices. Time breakdown:" " {0}s for indices, " "{1}s for posang, and {2}s for OTF scan identification" .format(t1-t0, t2-t1, t3-t2, len(self.allind))) def __repr__(self): s = "\n".join(["{k}: {v}".format(k=k,v=v) for k,v in iteritems(self.getinfo())]) return "ClassObject({id}) with {nspec} entries\n".format(id=id(self), nspec=len(self.allind)) + s def getinfo(self, allsources=False): info = dict( tels = self.tels, lines = self.lines, scans = self.scans, sources = self.sources if allsources else self.sci_sources, ) return info def set_posang(self): h0 = self.headers[0] for h in self.headers: dx = h['OFF1'] - h0['OFF1'] dy = h['OFF2'] - h0['OFF2'] h['COMPPOSA'] = np.arctan2(dy,dx)*180/np.pi h0 = h def _identify_otf_scans(self, verbose=False): h0 = self.allind[0] st = 0 otfscan = 0 posangs = [h['COMPPOSA'] for h in self.allind] if verbose: pb = ProgressBar(len(self.allind)) for ii,h in enumerate(self.allind): if (h['SCAN'] != h0['SCAN'] or h['SOURC'] != h0['SOURC']): h0['FIRSTSCAN'] = st cpa = np.median(posangs[st:ii]) for hh in self.allind[st:ii]: hh['SCANPOSA'] = cpa % 180 st = ii if h['SCAN'] == h0['SCAN']: h0['OTFSCAN'] = otfscan otfscan += 1 h['OTFSCAN'] = otfscan else: otfscan = 0 h['OTFSCAN'] = otfscan else: h['OTFSCAN'] = otfscan if verbose: pb.update(ii) def listscans(self, source=None, telescope=None, out=sys.stdout): minid=0 scan = -1 sourc = "" #tel = '' minoff1,maxoff1 = np.inf,-np.inf minoff2,maxoff2 = np.inf,-np.inf ttlangle,nangle = 0.0,0 print("{entries:15s} {SOURC:12s} {XTEL:12s} {SCAN:>8s} {SUBSCAN:>8s} " "[ {RAmin:>12s}, {RAmax:>12s} ] " "[ {DECmin:>12s}, {DECmax:>12s} ] " "{angle:>12s} {SCANPOSA:>12s} {OTFSCAN:>8s} {TSYS:>8s} {UTD:>12s}" .format(entries='Scans', SOURC='Source', XTEL='Telescope', SCAN='Scan', SUBSCAN='Subscan', RAmin='min(RA)', RAmax='max(RA)', DECmin='min(DEC)', DECmax='max(DEC)', SCANPOSA='Scan PA', angle='Angle', OTFSCAN='OTFscan', TSYS='TSYS', UTD='UTD'), file=out) data_rows = [] for ii,row in enumerate(self.headers): if (row['SCAN'] == scan and row['SOURC'] == sourc #and row['XTEL'] == tel ): minoff1 = min(minoff1, row['OFF1']) maxoff1 = max(maxoff1, row['OFF1']) minoff2 = min(minoff2, row['OFF2']) maxoff2 = max(maxoff2, row['OFF2']) ttlangle += np.arctan2(row['OFF2'] - prevrow['OFF2'], row['OFF1'] - prevrow['OFF1'])%np.pi nangle += 1 prevrow = row else: if scan == -1: scan = row['SCAN'] sourc = row['SOURC'] #tel = row['XTEL'] prevrow = row continue ok = True if source is not None: if isinstance(source, (list,tuple)): ok = ok and any(re.search((s), prevrow['SOURC']) for s in source) else: ok = ok and re.search((source), prevrow['SOURC']) if telescope is not None: ok = ok and re.search((telescope), prevrow['XTEL']) if ok: data = dict(RAmin=minoff1*180/np.pi*3600, RAmax=maxoff1*180/np.pi*3600, DECmin=minoff2*180/np.pi*3600, DECmax=maxoff2*180/np.pi*3600, angle=(ttlangle/nangle)*180/np.pi if nangle>0 else 0, e0=minid, e1=ii-1, #TSYS=row['TSYS'] if 'TSYS' in row else '--', UTD=row['DOBS']+row['UT'] if 'UT' in row else -99, **prevrow) print("{e0:7d}-{e1:7d} {SOURC:12s} {XTEL:12s} {SCAN:8d} {SUBSCAN:8d} " "[ {RAmin:12f}, {RAmax:12f} ] " "[ {DECmin:12f}, {DECmax:12f} ] " "{angle:12.1f} {SCANPOSA:12.1f} {OTFSCAN:8d}" " {TSYS:>8.1f} {UTD:12f}". format(**data), file=out) data_rows.append(data) minoff1,maxoff1 = np.inf,-np.inf minoff2,maxoff2 = np.inf,-np.inf ttlangle,nangle = 0.0,0 scan = row['SCAN'] sourc = row['SOURC'] #tel = row['XTEL'] minid = ii return data @property def tels(self): if hasattr(self,'_tels'): return self._tels else: self._tels = set([h['XTEL'] for h in self.allind]) return self._tels @property def sources(self): if hasattr(self,'_source'): return self._source else: self._source = set([h['SOURC'] for h in self.allind]) return self._source @property def scans(self): if hasattr(self,'_scan'): return self._scan else: self._scan = set([h['SCAN'] for h in self.allind]) return self._scan @property def sci_sources(self): return set([s for s in self.sources if s[:4] not in ('SKY-', 'TSYS', 'TCAL', 'TREC', 'HOT-', 'COLD')]) @property def lines(self): if hasattr(self,'_lines'): return self._lines else: self._lines = set([h['LINE'] for h in self.allind]) return self._lines def _load_all_spectra(self, indices=None): if indices is None: indices = range(self.file_description['xnext']-1) if hasattr(self, '_loaded_indices'): indices_set = set(indices) indices_to_load = (indices_set.difference(self._loaded_indices)) self._loaded_indices = self._loaded_indices.union(indices_set) if any(indices_to_load): pb = ProgressBar(len(indices_to_load)) for ii,k in enumerate(xrange(indices_to_load)): self._spectra[k] pb.update(ii) else: self._loaded_indices = set(indices) self._spectra.load_all() @property def spectra(self): return [x[0] for x in self._spectra] @property def headers(self): return [self._spectra[ii][1] if ii in self._spectra else x for ii,x in enumerate(self.allind)] def select_spectra(self, all=None, line=None, linere=None, linereflags=re.IGNORECASE, number=None, scan=None, offset=None, source=None, sourcere=None, sourcereflags=re.IGNORECASE, range=None, quality=None, telescope=None, telescopere=None, telescopereflags=re.IGNORECASE, subscan=None, entry=None, posang=None, #observed=None, #reduced=None, frequency=None, section=None, user=None, include_old_versions=False, ): """ Parameters ---------- include_old_versions: bool Include spectra with XVER numbers <0? These are CLASS spectra that have been "overwritten" (re-reduced?) """ if entry is not None and len(entry)==2: return irange(entry[0], entry[1]) if frequency is not None: self._load_all_spectra() sel = [(re.search(re.escape(line), h['LINE'], re.IGNORECASE) if line is not None else True) and (re.search(linere, h['LINE'], linereflags) if linere is not None else True) and (h['SCAN'] == scan if scan is not None else True) and ((h['OFF1'] == offset or h['OFF2'] == offset) if offset is not None else True) and (re.search(re.escape(source), h['CSOUR'], re.IGNORECASE) if source is not None else True) and (re.search(sourcere, h['CSOUR'], sourcereflags) if sourcere is not None else True) and (h['OFF1']>range[0] and h['OFF1'] < range[1] and h['OFF2']>range[2] and h['OFF2'] < range[3] if range is not None and len(range)==4 else True) and (h['QUAL'] == quality if quality is not None else True) and (re.search(re.escape(telescope), h['CTELE'], re.IGNORECASE) if telescope is not None else True) and (re.search(telescopere, h['CTELE'], telescopereflags) if telescopere is not None else True) and (h['SUBSCAN']==subscan if subscan is not None else True) and (h['NUM'] >= number[0] and h['NUM'] < number[1] if number is not None else True) and ('RESTF' in h and # Need to check that it IS a spectrum: continuum data can't be accessed this way h['RESTF'] > frequency[0] and h['RESTF'] < frequency[1] if frequency is not None and len(frequency)==2 else True) and (h['COMPPOSA']%180 > posang[0] and h['COMPPOSA']%180 < posang[1] if posang is not None and len(posang)==2 else True) and (h['XVER'] > 0 if not include_old_versions else True) for h in self.headers ] return [ii for ii,k in enumerate(sel) if k] def get_spectra(self, progressbar=True, **kwargs): selected_indices = self.select_spectra(**kwargs) if not any(selected_indices): raise ValueError("Selection yielded empty.") self._spectra.load(selected_indices, progressbar=progressbar) return [self._spectra[ii] for ii in selected_indices] def get_pyspeckit_spectra(self, progressbar=True, **kwargs): spdata = self.get_spectra(progressbar=progressbar, **kwargs) spectra = [pyspeckit.Spectrum(data=data, xarr=make_axis(header), header=clean_header(header)) for data,header in spdata] return spectra def read_observations(self, observation_indices, progressbar=True): self._spectra.load(observation_indices, progressbar=progressbar) return [self._spectra[ii] for ii in observation_indices] @print_timing def read_class(filename, downsample_factor=None, sourcename=None, telescope=None, posang=None, verbose=False, flag_array=None): """ Read a binary class file. Based on the `GILDAS CLASS file type Specification <http://iram.fr/IRAMFR/GILDAS/doc/html/class-html/node58.html>`_ Parameters ---------- filename: str downsample_factor: None or int Factor by which to downsample data by averaging. Useful for overresolved data. sourcename: str or list of str Source names to match to the data (uses regex) telescope: str or list of str 'XTEL' or 'TELE' parameters: the telescope & instrument flag_array: np.ndarray An array with the same shape as the data used to flag out (remove) data when downsampling. True = flag out """ classobj = ClassObject(filename) if not isinstance(sourcename, (list,tuple)): sourcename = [sourcename] if not isinstance(telescope, (list,tuple)): telescope = [telescope] spectra,headers = [],[] if verbose: log.info("Reading...") selection = [ii for source in sourcename for tel in telescope for ii in classobj.select_spectra(sourcere=source, telescope=tel, posang=posang)] sphdr = classobj.read_observations(selection) if len(sphdr) == 0: return None spec,hdr = zip(*sphdr) spectra += spec headers += hdr indexes = headers weight = ~flag_array if flag_array is not None else None if downsample_factor is not None: if verbose: log.info("Downsampling...") spectra = [downsample_1d(spec, downsample_factor, weight=weight) for spec in ProgressBar(spectra)] headers = [downsample_header(h, downsample_factor) for h in ProgressBar(headers)] return spectra,headers,indexes def downsample_header(hdr, downsample_factor): for k in ('NCHAN','NPOIN','DATALEN'): if k in hdr: hdr[k] = hdr[k] / downsample_factor # maybe wrong? h['RCHAN'] = (h['RCHAN']-1) / downsample_factor + 1 scalefactor = 1./downsample_factor hdr['RCHAN'] = (hdr['RCHAN']-1)*scalefactor + 0.5 + scalefactor/2. for kw in ['FRES','VRES']: if kw in hdr: hdr[kw] *= downsample_factor return hdr def make_axis(header,imagfreq=False): """ Create a :class:`pyspeckit.spectrum.units.SpectroscopicAxis` from the CLASS "header" """ from .. import units rest_frequency = header.get('RESTF') xunits = 'MHz' nchan = header.get('NCHAN') voff = header.get('VOFF') foff = header.get('FOFF') doppler = header.get('DOPPLER') fres = header.get('FRES') refchan = header.get('RCHAN') imfreq = header.get('IMAGE') if foff in (None, 0.0) and voff not in (None, 0.0): # Radio convention foff = -voff/2.997924580e5 * rest_frequency if not imagfreq: xarr = rest_frequency + foff + (numpy.arange(1, nchan+1) - refchan) * fres XAxis = units.SpectroscopicAxis(xarr,unit='MHz',refX=rest_frequency*u.MHz) else: xarr = imfreq - (numpy.arange(1, nchan+1) - refchan) * fres XAxis = units.SpectroscopicAxis(xarr,unit='MHz',refX=imfreq*u.MHz) return XAxis @print_timing def class_to_obsblocks(filename, telescope, line, datatuple=None, source=None, imagfreq=False, DEBUG=False, **kwargs): """ Load an entire CLASS observing session into a list of ObsBlocks based on matches to the 'telescope', 'line' and 'source' names Parameters ---------- filename : string The Gildas CLASS data file to read the spectra from. telescope : list List of telescope names to be matched. line : list List of line names to be matched. source : list (optional) List of source names to be matched. Defaults to None. imagfreq : bool Create a SpectroscopicAxis with the image frequency. """ if datatuple is None: spectra,header,indexes = read_class(filename,DEBUG=DEBUG, **kwargs) else: spectra,header,indexes = datatuple obslist = [] lastscannum = -1 spectrumlist = None for sp,hdr,ind in zip(spectra,header,indexes): hdr.update(ind) # this is slow but necessary... H = pyfits.Header() for k,v in iteritems(hdr): if hasattr(v,"__len__") and not isinstance(v,str): if len(v) > 1: for ii,vv in enumerate(v): H.update(k[:7]+str(ii),vv) else: H.update(k,v[0]) elif pyfits.Card._comment_FSC_RE.match(str(v)) is not None: H.update(k,v) scannum = hdr['SCAN'] if 'XTEL' in hdr and hdr['XTEL'].strip() not in telescope: continue if hdr['LINE'].strip() not in line: continue if (source is not None) and (hdr['SOURC'].strip() not in source): continue hdr.update({'RESTFREQ':hdr.get('RESTF')}) H.update('RESTFREQ',hdr.get('RESTF')) #print "Did not skip %s,%s. Scannum, last: %i,%i" % (hdr['XTEL'],hdr['LINE'],scannum,lastscannum) if scannum != lastscannum: lastscannum = scannum if spectrumlist is not None: obslist.append(pyspeckit.ObsBlock(spectrumlist)) xarr = make_axis(hdr,imagfreq=imagfreq) spectrumlist = [( pyspeckit.Spectrum(xarr=xarr, header=H, data=sp))] else: spectrumlist.append( pyspeckit.Spectrum(xarr=xarr, header=H, data=sp)) return obslist class LazyItem(object): """ Simple lazy spectrum-retriever wrapper """ def __init__(self, parent): self.parent = parent self.sphdr = {} self.nind = len(self.parent.allind) self.nloaded = 0 def __repr__(self): return ("Set of {0} spectra & headers, {1} loaded" " ({2:0.2f}%)".format(self.nind, self.nloaded, (float(self.nloaded)/self.nind)*100)) def load_all(self, progressbar=True): self.load(range(self.nind)) def load(self, indices, progressbar=True): pb = ProgressBar(len(indices)) counter = 0 for k in indices: self[k] counter += 1 pb.update(counter) def __getitem__(self, key): if key in self.sphdr: return self.sphdr[key] elif isinstance(key, slice): return [self[k] for k in xrange(key.start or 0, key.end or len(self.parent.allind), key.step or 1)] else: sphd = read_observation(self.parent._file, key, file_description=self.parent.file_description, indices=self.parent.allind, my_memmap=self.parent._data) # Update the header with OTFSCAN and POSANG info sphd[1].update(self.parent.allind[key]) self.sphdr[key] = sphd self.nloaded += 1 return sphd def __iter__(self): return self.next() def __next__(self): for k in self.spheader: yield self.spheader[k] def __contains__(self, key): return key in self.sphdr @print_timing def class_to_spectra(filename, datatuple=None, **kwargs): """ Load each individual spectrum within a CLASS file into a list of Spectrum objects """ if datatuple is None: spectra,header,indexes = read_class(filename, **kwargs) else: spectra,header,indexes = datatuple spectrumlist = [] for sp,hdr,ind in zip(spectra,header,indexes): hdr.update(ind) xarr = make_axis(hdr) spectrumlist.append( pyspeckit.Spectrum(xarr=xarr, header=hdr, data=sp)) return pyspeckit.Spectra(spectrumlist) def tests(): """ Tests are specific to the machine on which this code was developed. """ fn1 = '/Users/adam/work/bolocam/hht/class_003.smt' #fn1 = '/Users/adam/work/bolocam/hht/class_001.smt' #fn1 = '/Users/adam/work/bolocam/hht/test_SMT-F1M-VU-20824-073.cls' #fn2 = '/Users/adam/work/bolocam/hht/test_SMT-F1M-VU-79472+203.cls' #F1 = read_class(fn1)#,DEBUG=True) #F2 = read_class(fn2) n2hp = class_to_obsblocks(fn1,telescope=['SMT-F1M-HU','SMT-F1M-VU'],line=['N2HP(3-2)','N2H+(3-2)']) hcop = class_to_obsblocks(fn1,telescope=['SMT-F1M-HL','SMT-F1M-VL'],line=['HCOP(3-2)','HCO+(3-2)'])
# Copyright (c) 2011-2021 Berkeley Model United Nations. All rights reserved. # Use of this source code is governed by a BSD License (see LICENSE). import csv from django.conf import settings from django.conf.urls import url from django.contrib import admin from django.urls import reverse from django.http import HttpResponse, HttpResponseRedirect from googleapiclient.discovery import build from google.oauth2 import service_account from huxley.core.models import CommitteeFeedback class CommitteeFeedbackAdmin(admin.ModelAdmin): search_fields = ('committee__name', ) def get_rows(self): rows = [] rows.append([ 'Committee', 'General Rating', 'General Comment', 'Chair 1', 'Chair 1 Rating', 'Chair 1 Comment', 'Chair 2 Name', 'Chair 2 Rating', 'Chair 2 Comment', 'Chair 3 Name', 'Chair 3 Rating', 'Chair 3 Comment', 'Chair 4 Name', 'Chair 4 Rating', 'Chair 4 Comment', 'Chair 5 Name', 'Chair 5 Rating', 'Chair 5 Comment', 'Chair 6 Name', 'Chair 6 Rating', 'Chair 6 Comment', 'Chair 7 Name', 'Chair 7 Rating', 'Chair 7 Comment', 'Chair 8 Name', 'Chair 8 Rating', 'Chair 8 Comment', 'Chair 9 Name', 'Chair 9 Rating', 'Chair 9 Comment', 'Chair 10 Name', 'Chair 10 Rating', 'Chair 10 Comment', 'Perception of Berkeley', 'Money Spent' ]) for feedback in CommitteeFeedback.objects.all().order_by( 'committee__name'): rows.append([ feedback.committee.name, feedback.rating, feedback.comment, feedback.chair_1_name, feedback.chair_1_rating, feedback.chair_1_comment, feedback.chair_2_name, feedback.chair_2_rating, feedback.chair_2_comment, feedback.chair_3_name, feedback.chair_3_rating, feedback.chair_3_comment, feedback.chair_4_name, feedback.chair_4_rating, feedback.chair_4_comment, feedback.chair_5_name, feedback.chair_5_rating, feedback.chair_5_comment, feedback.chair_6_name, feedback.chair_6_rating, feedback.chair_6_comment, feedback.chair_7_name, feedback.chair_7_rating, feedback.chair_7_comment, feedback.chair_8_name, feedback.chair_8_rating, feedback.chair_8_comment, feedback.chair_9_name, feedback.chair_9_rating, feedback.chair_9_comment, feedback.chair_10_name, feedback.chair_10_rating, feedback.chair_10_comment, feedback.berkeley_perception, feedback.money_spent ]) return rows def list(self, request): '''Return a CSV file containing all committee feedback.''' feedbacks = HttpResponse(content_type='text/csv') feedbacks[ 'Content-Disposition'] = 'attachment; filename="feedback.csv"' writer = csv.writer(feedbacks) for row in self.get_rows(): writer.writerow(row) return feedbacks def sheets(self, request): if settings.SHEET_ID: SHEET_RANGE = 'Feedback!A1:AI' # Store credentials creds = service_account.Credentials.from_service_account_file( settings.SERVICE_ACCOUNT_FILE, scopes=settings.SCOPES) data = self.get_rows() body = { 'values': data, } service = build('sheets', 'v4', credentials=creds) response = service.spreadsheets().values().clear( spreadsheetId=settings.SHEET_ID, range=SHEET_RANGE, ).execute() response = service.spreadsheets().values().update( spreadsheetId=settings.SHEET_ID, range=SHEET_RANGE, valueInputOption='USER_ENTERED', body=body).execute() return HttpResponseRedirect( reverse('admin:core_committeefeedback_changelist')) def get_urls(self): return super(CommitteeFeedbackAdmin, self).get_urls() + [ url(r'list', self.admin_site.admin_view(self.list), name='core_committeefeedback_list'), url( r'sheets', self.admin_site.admin_view(self.sheets), name='core_committeefeedback_sheets', ), ]
"""Service calling related helpers.""" import functools import logging # pylint: disable=unused-import from typing import Optional # NOQA import voluptuous as vol from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant # NOQA from homeassistant.exceptions import TemplateError from homeassistant.loader import get_component import homeassistant.helpers.config_validation as cv HASS = None # type: Optional[HomeAssistant] CONF_SERVICE = 'service' CONF_SERVICE_TEMPLATE = 'service_template' CONF_SERVICE_ENTITY_ID = 'entity_id' CONF_SERVICE_DATA = 'data' CONF_SERVICE_DATA_TEMPLATE = 'data_template' _LOGGER = logging.getLogger(__name__) def service(domain, service_name): """Decorator factory to register a service.""" def register_service_decorator(action): """Decorator to register a service.""" HASS.services.register(domain, service_name, functools.partial(action, HASS)) return action return register_service_decorator def call_from_config(hass, config, blocking=False, variables=None, validate_config=True): """Call a service based on a config hash.""" if validate_config: try: config = cv.SERVICE_SCHEMA(config) except vol.Invalid as ex: _LOGGER.error("Invalid config for calling service: %s", ex) return if CONF_SERVICE in config: domain_service = config[CONF_SERVICE] else: try: config[CONF_SERVICE_TEMPLATE].hass = hass domain_service = config[CONF_SERVICE_TEMPLATE].render(variables) domain_service = cv.service(domain_service) except TemplateError as ex: _LOGGER.error('Error rendering service name template: %s', ex) return except vol.Invalid as ex: _LOGGER.error('Template rendered invalid service: %s', domain_service) return domain, service_name = domain_service.split('.', 1) service_data = dict(config.get(CONF_SERVICE_DATA, {})) def _data_template_creator(value): """Recursive template creator helper function.""" if isinstance(value, list): for idx, element in enumerate(value): value[idx] = _data_template_creator(element) return value if isinstance(value, dict): for key, element in value.items(): value[key] = _data_template_creator(element) return value value.hass = hass return value.render(variables) if CONF_SERVICE_DATA_TEMPLATE in config: for key, value in config[CONF_SERVICE_DATA_TEMPLATE].items(): service_data[key] = _data_template_creator(value) if CONF_SERVICE_ENTITY_ID in config: service_data[ATTR_ENTITY_ID] = config[CONF_SERVICE_ENTITY_ID] hass.services.call(domain, service_name, service_data, blocking) def extract_entity_ids(hass, service_call): """Helper method to extract a list of entity ids from a service call. Will convert group entity ids to the entity ids it represents. """ if not (service_call.data and ATTR_ENTITY_ID in service_call.data): return [] group = get_component('group') # Entity ID attr can be a list or a string service_ent_id = service_call.data[ATTR_ENTITY_ID] if isinstance(service_ent_id, str): return group.expand_entity_ids(hass, [service_ent_id]) return [ent_id for ent_id in group.expand_entity_ids(hass, service_ent_id)]
#-*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # ############################################################################## from openerp.addons.base_status.base_stage import base_stage from openerp.addons.crm import crm from datetime import datetime from openerp.osv import fields,osv from openerp.tools.translate import _ import binascii import time from openerp import tools from openerp.tools import html2plaintext class project_issue_version(osv.osv): _name = "project.issue.version" _order = "name desc" _columns = { 'name': fields.char('Version Number', size=32, required=True), 'active': fields.boolean('Active', required=False), } _defaults = { 'active': 1, } project_issue_version() _ISSUE_STATE = [('draft', 'New'), ('open', 'In Progress'), ('cancel', 'Cancelled'), ('done', 'Done'), ('pending', 'Pending')] class project_issue(base_stage, osv.osv): _name = "project.issue" _description = "Project Issue" _order = "priority, create_date desc" _inherit = ['mail.thread', 'ir.needaction_mixin'] _track = { 'state': { 'project_issue.mt_issue_new': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'new', 'project_issue.mt_issue_closed': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'done', 'project_issue.mt_issue_started': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'open', }, 'stage_id': { 'project_issue.mt_issue_stage': lambda self, cr, uid, obj, ctx=None: obj['state'] not in ['new', 'done', 'open'], }, 'kanban_state': { 'project_issue.mt_issue_blocked': lambda self, cr, uid, obj, ctx=None: obj['kanban_state'] == 'blocked', }, } def create(self, cr, uid, vals, context=None): if context is None: context = {} if not context.get('default_project_id', False) and vals.get('project_id', False): ctx = context.copy() ctx['default_project_id'] = vals['project_id'] vals['stage_id'] = self._get_default_stage_id(cr, uid, context=ctx) return super(project_issue, self).create(cr, uid, vals, context=context) def _get_default_project_id(self, cr, uid, context=None): """ Gives default project by checking if present in the context """ return self._resolve_project_id_from_context(cr, uid, context=context) def _get_default_stage_id(self, cr, uid, context=None): """ Gives default stage_id """ project_id = self._get_default_project_id(cr, uid, context=context) return self.stage_find(cr, uid, [], project_id, [('state', '=', 'draft')], context=context) def _resolve_project_id_from_context(self, cr, uid, context=None): """ Returns ID of project based on the value of 'default_project_id' context key, or None if it cannot be resolved to a single project. """ if context is None: context = {} if type(context.get('default_project_id')) in (int, long): return context.get('default_project_id') if isinstance(context.get('default_project_id'), basestring): project_name = context['default_project_id'] project_ids = self.pool.get('project.project').name_search(cr, uid, name=project_name, context=context) if len(project_ids) == 1: return int(project_ids[0][0]) return None def _read_group_stage_ids(self, cr, uid, ids, domain, read_group_order=None, access_rights_uid=None, context=None): access_rights_uid = access_rights_uid or uid stage_obj = self.pool.get('project.task.type') order = stage_obj._order # lame hack to allow reverting search, should just work in the trivial case if read_group_order == 'stage_id desc': order = "%s desc" % order # retrieve section_id from the context and write the domain # - ('id', 'in', 'ids'): add columns that should be present # - OR ('case_default', '=', True), ('fold', '=', False): add default columns that are not folded # - OR ('project_ids', 'in', project_id), ('fold', '=', False) if project_id: add project columns that are not folded search_domain = [] project_id = self._resolve_project_id_from_context(cr, uid, context=context) if project_id: search_domain += ['|', ('project_ids', '=', project_id)] search_domain += [('id', 'in', ids)] # perform search stage_ids = stage_obj._search(cr, uid, search_domain, order=order, access_rights_uid=access_rights_uid, context=context) result = stage_obj.name_get(cr, access_rights_uid, stage_ids, context=context) # restore order of the search result.sort(lambda x,y: cmp(stage_ids.index(x[0]), stage_ids.index(y[0]))) fold = {} for stage in stage_obj.browse(cr, access_rights_uid, stage_ids, context=context): fold[stage.id] = stage.fold or False return result, fold def _compute_day(self, cr, uid, ids, fields, args, context=None): """ @param cr: the current row, from the database cursor, @param uid: the current user’s ID for security checks, @param ids: List of Openday’s IDs @return: difference between current date and log date @param context: A standard dictionary for contextual values """ cal_obj = self.pool.get('resource.calendar') res_obj = self.pool.get('resource.resource') res = {} for issue in self.browse(cr, uid, ids, context=context): res[issue.id] = {} for field in fields: duration = 0 ans = False hours = 0 date_create = datetime.strptime(issue.create_date, "%Y-%m-%d %H:%M:%S") if field in ['working_hours_open','day_open']: if issue.date_open: date_open = datetime.strptime(issue.date_open, "%Y-%m-%d %H:%M:%S") ans = date_open - date_create date_until = issue.date_open #Calculating no. of working hours to open the issue if issue.project_id.resource_calendar_id: hours = cal_obj.interval_hours_get(cr, uid, issue.project_id.resource_calendar_id.id, date_create, date_open) elif field in ['working_hours_close','day_close']: if issue.date_closed: date_close = datetime.strptime(issue.date_closed, "%Y-%m-%d %H:%M:%S") date_until = issue.date_closed ans = date_close - date_create #Calculating no. of working hours to close the issue if issue.project_id.resource_calendar_id: hours = cal_obj.interval_hours_get(cr, uid, issue.project_id.resource_calendar_id.id, date_create, date_close) elif field in ['days_since_creation']: if issue.create_date: days_since_creation = datetime.today() - datetime.strptime(issue.create_date, "%Y-%m-%d %H:%M:%S") res[issue.id][field] = days_since_creation.days continue elif field in ['inactivity_days']: res[issue.id][field] = 0 if issue.date_action_last: inactive_days = datetime.today() - datetime.strptime(issue.date_action_last, '%Y-%m-%d %H:%M:%S') res[issue.id][field] = inactive_days.days continue if ans: resource_id = False if issue.user_id: resource_ids = res_obj.search(cr, uid, [('user_id','=',issue.user_id.id)]) if resource_ids and len(resource_ids): resource_id = resource_ids[0] duration = float(ans.days) if issue.project_id and issue.project_id.resource_calendar_id: duration = float(ans.days) * 24 new_dates = cal_obj.interval_min_get(cr, uid, issue.project_id.resource_calendar_id.id, date_create, duration, resource=resource_id) no_days = [] date_until = datetime.strptime(date_until, '%Y-%m-%d %H:%M:%S') for in_time, out_time in new_dates: if in_time.date not in no_days: no_days.append(in_time.date) if out_time > date_until: break duration = len(no_days) if field in ['working_hours_open','working_hours_close']: res[issue.id][field] = hours else: res[issue.id][field] = abs(float(duration)) return res def _hours_get(self, cr, uid, ids, field_names, args, context=None): task_pool = self.pool.get('project.task') res = {} for issue in self.browse(cr, uid, ids, context=context): progress = 0.0 if issue.task_id: progress = task_pool._hours_get(cr, uid, [issue.task_id.id], field_names, args, context=context)[issue.task_id.id]['progress'] res[issue.id] = {'progress' : progress} return res def on_change_project(self, cr, uid, ids, project_id, context=None): return {} def _get_issue_task(self, cr, uid, ids, context=None): issues = [] issue_pool = self.pool.get('project.issue') for task in self.pool.get('project.task').browse(cr, uid, ids, context=context): issues += issue_pool.search(cr, uid, [('task_id','=',task.id)]) return issues def _get_issue_work(self, cr, uid, ids, context=None): issues = [] issue_pool = self.pool.get('project.issue') for work in self.pool.get('project.task.work').browse(cr, uid, ids, context=context): if work.task_id: issues += issue_pool.search(cr, uid, [('task_id','=',work.task_id.id)]) return issues _columns = { 'id': fields.integer('ID', readonly=True), 'name': fields.char('Issue', size=128, required=True), 'active': fields.boolean('Active', required=False), 'create_date': fields.datetime('Creation Date', readonly=True,select=True), 'write_date': fields.datetime('Update Date', readonly=True), 'days_since_creation': fields.function(_compute_day, string='Days since creation date', \ multi='compute_day', type="integer", help="Difference in days between creation date and current date"), 'date_deadline': fields.date('Deadline'), 'section_id': fields.many2one('crm.case.section', 'Sales Team', \ select=True, help='Sales team to which Case belongs to.\ Define Responsible user and Email account for mail gateway.'), 'partner_id': fields.many2one('res.partner', 'Contact', select=1), 'company_id': fields.many2one('res.company', 'Company'), 'description': fields.text('Private Note'), 'state': fields.related('stage_id', 'state', type="selection", store=True, selection=_ISSUE_STATE, string="Status", readonly=True, help='The status is set to \'Draft\', when a case is created.\ If the case is in progress the status is set to \'Open\'.\ When the case is over, the status is set to \'Done\'.\ If the case needs to be reviewed then the status is \ set to \'Pending\'.'), 'kanban_state': fields.selection([('normal', 'Normal'),('blocked', 'Blocked'),('done', 'Ready for next stage')], 'Kanban State', track_visibility='onchange', help="A Issue's kanban state indicates special situations affecting it:\n" " * Normal is the default situation\n" " * Blocked indicates something is preventing the progress of this issue\n" " * Ready for next stage indicates the issue is ready to be pulled to the next stage", readonly=True, required=False), 'email_from': fields.char('Email', size=128, help="These people will receive email.", select=1), 'email_cc': fields.char('Watchers Emails', size=256, help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"), 'date_open': fields.datetime('Opened', readonly=True,select=True), # Project Issue fields 'date_closed': fields.datetime('Closed', readonly=True,select=True), 'date': fields.datetime('Date'), 'channel_id': fields.many2one('crm.case.channel', 'Channel', help="Communication channel."), 'categ_ids': fields.many2many('project.category', string='Tags'), 'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority', select=True), 'version_id': fields.many2one('project.issue.version', 'Version'), 'stage_id': fields.many2one ('project.task.type', 'Stage', track_visibility='onchange', domain="['&', ('fold', '=', False), ('project_ids', '=', project_id)]"), 'project_id':fields.many2one('project.project', 'Project', track_visibility='onchange'), 'duration': fields.float('Duration'), 'task_id': fields.many2one('project.task', 'Task', domain="[('project_id','=',project_id)]"), 'day_open': fields.function(_compute_day, string='Days to Open', \ multi='compute_day', type="float", store=True), 'day_close': fields.function(_compute_day, string='Days to Close', \ multi='compute_day', type="float", store=True), 'user_id': fields.many2one('res.users', 'Assigned to', required=False, select=1, track_visibility='onchange'), 'working_hours_open': fields.function(_compute_day, string='Working Hours to Open the Issue', \ multi='compute_day', type="float", store=True), 'working_hours_close': fields.function(_compute_day, string='Working Hours to Close the Issue', \ multi='compute_day', type="float", store=True), 'inactivity_days': fields.function(_compute_day, string='Days since last action', \ multi='compute_day', type="integer", help="Difference in days between last action and current date"), 'color': fields.integer('Color Index'), 'user_email': fields.related('user_id', 'email', type='char', string='User Email', readonly=True), 'date_action_last': fields.datetime('Last Action', readonly=1), 'date_action_next': fields.datetime('Next Action', readonly=1), 'progress': fields.function(_hours_get, string='Progress (%)', multi='hours', group_operator="avg", help="Computed as: Time Spent / Total Time.", store = { 'project.issue': (lambda self, cr, uid, ids, c={}: ids, ['task_id'], 10), 'project.task': (_get_issue_task, ['progress'], 10), 'project.task.work': (_get_issue_work, ['hours'], 10), }), } _defaults = { 'active': 1, 'partner_id': lambda s, cr, uid, c: s._get_default_partner(cr, uid, c), 'email_from': lambda s, cr, uid, c: s._get_default_email(cr, uid, c), 'stage_id': lambda s, cr, uid, c: s._get_default_stage_id(cr, uid, c), 'section_id': lambda s, cr, uid, c: s._get_default_section_id(cr, uid, c), 'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c), 'priority': crm.AVAILABLE_PRIORITIES[2][0], 'kanban_state': 'normal', } _group_by_full = { 'stage_id': _read_group_stage_ids } def set_priority(self, cr, uid, ids, priority, *args): """Set lead priority """ return self.write(cr, uid, ids, {'priority' : priority}) def set_high_priority(self, cr, uid, ids, *args): """Set lead priority to high """ return self.set_priority(cr, uid, ids, '1') def set_normal_priority(self, cr, uid, ids, *args): """Set lead priority to normal """ return self.set_priority(cr, uid, ids, '3') def convert_issue_task(self, cr, uid, ids, context=None): if context is None: context = {} case_obj = self.pool.get('project.issue') data_obj = self.pool.get('ir.model.data') task_obj = self.pool.get('project.task') result = data_obj._get_id(cr, uid, 'project', 'view_task_search_form') res = data_obj.read(cr, uid, result, ['res_id']) id2 = data_obj._get_id(cr, uid, 'project', 'view_task_form2') id3 = data_obj._get_id(cr, uid, 'project', 'view_task_tree2') if id2: id2 = data_obj.browse(cr, uid, id2, context=context).res_id if id3: id3 = data_obj.browse(cr, uid, id3, context=context).res_id for bug in case_obj.browse(cr, uid, ids, context=context): new_task_id = task_obj.create(cr, uid, { 'name': bug.name, 'partner_id': bug.partner_id.id, 'description':bug.description, 'date_deadline': bug.date, 'project_id': bug.project_id.id, # priority must be in ['0','1','2','3','4'], while bug.priority is in ['1','2','3','4','5'] 'priority': str(int(bug.priority) - 1), 'user_id': bug.user_id.id, 'planned_hours': 0.0, }) vals = { 'task_id': new_task_id, 'stage_id': self.stage_find(cr, uid, [bug], bug.project_id.id, [('state', '=', 'pending')], context=context), } message = _("Project issue <b>converted</b> to task.") self.message_post(cr, uid, [bug.id], body=message, context=context) case_obj.write(cr, uid, [bug.id], vals, context=context) return { 'name': _('Tasks'), 'view_type': 'form', 'view_mode': 'form,tree', 'res_model': 'project.task', 'res_id': int(new_task_id), 'view_id': False, 'views': [(id2,'form'),(id3,'tree'),(False,'calendar'),(False,'graph')], 'type': 'ir.actions.act_window', 'search_view_id': res['res_id'], 'nodestroy': True } def copy(self, cr, uid, id, default=None, context=None): issue = self.read(cr, uid, id, ['name'], context=context) if not default: default = {} default = default.copy() default.update(name=_('%s (copy)') % (issue['name'])) return super(project_issue, self).copy(cr, uid, id, default=default, context=context) def write(self, cr, uid, ids, vals, context=None): #Update last action date every time the user change the stage, the state or send a new email logged_fields = ['stage_id', 'state', 'message_ids'] if any([field in vals for field in logged_fields]): vals['date_action_last'] = time.strftime('%Y-%m-%d %H:%M:%S') return super(project_issue, self).write(cr, uid, ids, vals, context) def onchange_task_id(self, cr, uid, ids, task_id, context=None): if not task_id: return {'value': {}} task = self.pool.get('project.task').browse(cr, uid, task_id, context=context) return {'value': {'user_id': task.user_id.id, }} def case_reset(self, cr, uid, ids, context=None): """Resets case as draft """ res = super(project_issue, self).case_reset(cr, uid, ids, context) self.write(cr, uid, ids, {'date_open': False, 'date_closed': False}) return res # ------------------------------------------------------- # Stage management # ------------------------------------------------------- def set_kanban_state_blocked(self, cr, uid, ids, context=None): return self.write(cr, uid, ids, {'kanban_state': 'blocked'}, context=context) def set_kanban_state_normal(self, cr, uid, ids, context=None): return self.write(cr, uid, ids, {'kanban_state': 'normal'}, context=context) def set_kanban_state_done(self, cr, uid, ids, context=None): return self.write(cr, uid, ids, {'kanban_state': 'done'}, context=context) def stage_find(self, cr, uid, cases, section_id, domain=[], order='sequence', context=None): """ Override of the base.stage method Parameter of the stage search taken from the issue: - type: stage type must be the same or 'both' - section_id: if set, stages must belong to this section or be a default case """ if isinstance(cases, (int, long)): cases = self.browse(cr, uid, cases, context=context) # collect all section_ids section_ids = [] if section_id: section_ids.append(section_id) for task in cases: if task.project_id: section_ids.append(task.project_id.id) # OR all section_ids and OR with case_default search_domain = [] if section_ids: search_domain += [('|')] * (len(section_ids)-1) for section_id in section_ids: search_domain.append(('project_ids', '=', section_id)) search_domain += list(domain) # perform search, return the first found stage_ids = self.pool.get('project.task.type').search(cr, uid, search_domain, order=order, context=context) if stage_ids: return stage_ids[0] return False def case_cancel(self, cr, uid, ids, context=None): """ Cancels case """ self.case_set(cr, uid, ids, 'cancelled', {'active': True}, context=context) return True def case_escalate(self, cr, uid, ids, context=None): cases = self.browse(cr, uid, ids) for case in cases: data = {} if case.project_id.project_escalation_id: data['project_id'] = case.project_id.project_escalation_id.id if case.project_id.project_escalation_id.user_id: data['user_id'] = case.project_id.project_escalation_id.user_id.id if case.task_id: self.pool.get('project.task').write(cr, uid, [case.task_id.id], {'project_id': data['project_id'], 'user_id': False}) else: raise osv.except_osv(_('Warning!'), _('You cannot escalate this issue.\nThe relevant Project has not configured the Escalation Project!')) self.case_set(cr, uid, ids, 'draft', data, context=context) return True # ------------------------------------------------------- # Mail gateway # ------------------------------------------------------- def message_new(self, cr, uid, msg, custom_values=None, context=None): """ Overrides mail_thread message_new that is called by the mailgateway through message_process. This override updates the document according to the email. """ if custom_values is None: custom_values = {} if context is None: context = {} context['state_to'] = 'draft' desc = html2plaintext(msg.get('body')) if msg.get('body') else '' custom_values.update({ 'name': msg.get('subject') or _("No Subject"), 'description': desc, 'email_from': msg.get('from'), 'email_cc': msg.get('cc'), 'user_id': False, }) if msg.get('priority'): custom_values['priority'] = msg.get('priority') res_id = super(project_issue, self).message_new(cr, uid, msg, custom_values=custom_values, context=context) return res_id def message_update(self, cr, uid, ids, msg, update_vals=None, context=None): """ Overrides mail_thread message_update that is called by the mailgateway through message_process. This method updates the document according to the email. """ if isinstance(ids, (str, int, long)): ids = [ids] if update_vals is None: update_vals = {} # Update doc values according to the message if msg.get('priority'): update_vals['priority'] = msg.get('priority') # Parse 'body' to find values to update maps = { 'cost': 'planned_cost', 'revenue': 'planned_revenue', 'probability': 'probability', } for line in msg.get('body', '').split('\n'): line = line.strip() res = tools.command_re.match(line) if res and maps.get(res.group(1).lower(), False): key = maps.get(res.group(1).lower()) update_vals[key] = res.group(2).lower() return super(project_issue, self).message_update(cr, uid, ids, msg, update_vals=update_vals, context=context) class project(osv.osv): _inherit = "project.project" def _get_alias_models(self, cr, uid, context=None): return [('project.task', "Tasks"), ("project.issue", "Issues")] def _issue_count(self, cr, uid, ids, field_name, arg, context=None): res = dict.fromkeys(ids, 0) issue_ids = self.pool.get('project.issue').search(cr, uid, [('project_id', 'in', ids)]) for issue in self.pool.get('project.issue').browse(cr, uid, issue_ids, context): res[issue.project_id.id] += 1 return res _columns = { 'project_escalation_id' : fields.many2one('project.project','Project Escalation', help='If any issue is escalated from the current Project, it will be listed under the project selected here.', states={'close':[('readonly',True)], 'cancelled':[('readonly',True)]}), 'issue_count': fields.function(_issue_count, type='integer'), } def _check_escalation(self, cr, uid, ids, context=None): project_obj = self.browse(cr, uid, ids[0], context=context) if project_obj.project_escalation_id: if project_obj.project_escalation_id.id == project_obj.id: return False return True _constraints = [ (_check_escalation, 'Error! You cannot assign escalation to the same project!', ['project_escalation_id']) ] project() class account_analytic_account(osv.osv): _inherit = 'account.analytic.account' _description = 'Analytic Account' _columns = { 'use_issues' : fields.boolean('Issues', help="Check this field if this project manages issues"), } def on_change_template(self, cr, uid, ids, template_id, context=None): res = super(account_analytic_account, self).on_change_template(cr, uid, ids, template_id, context=context) if template_id and 'value' in res: template = self.browse(cr, uid, template_id, context=context) res['value']['use_issues'] = template.use_issues return res def _trigger_project_creation(self, cr, uid, vals, context=None): if context is None: context = {} res = super(account_analytic_account, self)._trigger_project_creation(cr, uid, vals, context=context) return res or (vals.get('use_issues') and not 'project_creation_in_progress' in context) account_analytic_account() class project_project(osv.osv): _inherit = 'project.project' _defaults = { 'use_issues': True } # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
# 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 collections import pretend from warehouse import csp class TestCSPTween: def test_csp_policy(self): response = pretend.stub(headers={}) handler = pretend.call_recorder(lambda request: response) settings = { "csp": { "default-src": ["*"], "style-src": ["'self'", "example.net"], }, } registry = pretend.stub(settings=settings) tween = csp.content_security_policy_tween_factory(handler, registry) request = pretend.stub( path="/project/foobar/", find_service=pretend.call_recorder( lambda *args, **kwargs: settings["csp"] ), ) assert tween(request) is response assert response.headers == { "Content-Security-Policy": "default-src *; style-src 'self' example.net", } def test_csp_policy_default(self): response = pretend.stub(headers={}) handler = pretend.call_recorder(lambda request: response) registry = pretend.stub(settings={}) tween = csp.content_security_policy_tween_factory(handler, registry) request = pretend.stub( path="/path/to/nowhere/", find_service=pretend.raiser(ValueError), ) assert tween(request) is response assert response.headers == {} def test_csp_policy_debug_disables(self): response = pretend.stub(headers={}) handler = pretend.call_recorder(lambda request: response) settings = { "csp": { "default-src": ["*"], "style-src": ["'self'", "example.net"], }, } registry = pretend.stub(settings=settings) tween = csp.content_security_policy_tween_factory(handler, registry) request = pretend.stub( path="/_debug_toolbar/foo/", find_service=pretend.call_recorder( lambda *args, **kwargs: settings["csp"] ), ) assert tween(request) is response assert response.headers == {} def test_csp_policy_inject(self): response = pretend.stub(headers={}) def handler(request): request.find_service("csp")["default-src"].append("example.com") return response settings = { "csp": { "default-src": ["*"], "style-src": ["'self'"], }, } registry = pretend.stub(settings=settings) tween = csp.content_security_policy_tween_factory(handler, registry) request = pretend.stub( path="/example", find_service=pretend.call_recorder( lambda *args, **kwargs: settings["csp"] ), ) assert tween(request) is response assert response.headers == { "Content-Security-Policy": "default-src * example.com; style-src 'self'", } def test_csp_policy_default_inject(self): settings = collections.defaultdict(list) response = pretend.stub(headers={}) registry = pretend.stub(settings=settings) def handler(request): request.find_service("csp")["default-src"].append("example.com") return response tween = csp.content_security_policy_tween_factory(handler, registry) request = pretend.stub( path="/path/to/nowhere/", find_service=pretend.call_recorder( lambda *args, **kwargs: settings ), ) assert tween(request) is response assert response.headers == { "Content-Security-Policy": "default-src example.com" } def test_devel_csp(self): settings = { "csp": { "script-src": ["{request.scheme}://{request.host}"], } } response = pretend.stub(headers={}) registry = pretend.stub(settings=settings) handler = pretend.call_recorder(lambda request: response) tween = csp.content_security_policy_tween_factory(handler, registry) request = pretend.stub( scheme="https", host="example.com", path="/path/to/nowhere", find_service=pretend.call_recorder( lambda *args, **kwargs: settings["csp"], ), ) assert tween(request) is response assert response.headers == { "Content-Security-Policy": "script-src https://example.com", } class TestCSPPolicy: def test_create(self): policy = csp.CSPPolicy({"foo": ["bar"]}) assert isinstance(policy, collections.defaultdict) def test_merge(self): policy = csp.CSPPolicy({"foo": ["bar"]}) policy.merge({ "foo": ["baz"], "something": ["else"], }) assert policy == { "foo": ["bar", "baz"], "something": ["else"], } def test_includeme(): config = pretend.stub( register_service_factory=pretend.call_recorder( lambda fact, name: None), add_settings=pretend.call_recorder(lambda settings: None), add_tween=pretend.call_recorder(lambda tween: None), registry=pretend.stub(settings={ "camo.url": "camo.url.value", "statuspage.url": "https://2p66nmmycsj3.statuspage.io", }), ) csp.includeme(config) assert config.register_service_factory.calls == [ pretend.call(csp.csp_factory, name="csp") ] assert config.add_tween.calls == [ pretend.call("warehouse.csp.content_security_policy_tween_factory"), ] assert config.add_settings.calls == [ pretend.call({ "csp": { "base-uri": ["'self'"], "block-all-mixed-content": [], "connect-src": [ "'self'", "https://2p66nmmycsj3.statuspage.io", ], "default-src": ["'none'"], "font-src": ["'self'", "fonts.gstatic.com"], "form-action": ["'self'"], "frame-ancestors": ["'none'"], "frame-src": ["'none'"], "img-src": [ "'self'", "camo.url.value", "https://secure.gravatar.com", ], "referrer": ["origin-when-cross-origin"], "reflected-xss": ["block"], "script-src": ["'self'", "www.google-analytics.com"], "style-src": ["'self'", "fonts.googleapis.com"], }, }) ] class TestFactory: def test_copy(self): settings = { "csp": { "foo": "bar", }, } request = pretend.stub( registry=pretend.stub( settings=settings ) ) result = csp.csp_factory(None, request) assert isinstance(result, csp.CSPPolicy) assert result == settings["csp"] # ensure changes to factory result don't propagate back to the # settings result["baz"] = "foo" assert result == {"foo": "bar", "baz": "foo"} assert settings == {"csp": {"foo": "bar"}} def test_default(self): request = pretend.stub( registry=pretend.stub( settings={} ) ) result = csp.csp_factory(None, request) assert isinstance(result, csp.CSPPolicy) assert result == {}
import psycopg2 as dbapi2 class Author: def __init__(self,id, name, lastname, birthdate, nationality, penname,description,picture): self.id= id self.name= name self.lastname=lastname self.birthdate=birthdate self.nationality=nationality self.penname = penname self.description = description self.picture=picture author1 = Author(None,"Ernest","Hemingway",1899,"American",None,None,"https://raw.githubusercontent.com/itucsdb1612/itucsdb1612/master/wiki_screenshots/sahalemre/Pic/hemingway.jpg") author2 = Author(None,"Samuel","Clemens",1835,"American","Mark Twain",None,"https://raw.githubusercontent.com/itucsdb1612/itucsdb1612/master/wiki_screenshots/sahalemre/Pic/marktwain.jpg") author3 = Author(None,"Metehan","Gültekin",1994,"Turkish",None,None,"https://raw.githubusercontent.com/itucsdb1612/itucsdb1612/master/wiki_screenshots/sahalemre/Pic/mete.jpg") author4 = Author(None,"Ilay","Köksal",1995,"Turkish",None,None,"https://raw.githubusercontent.com/itucsdb1612/itucsdb1612/master/wiki_screenshots/sahalemre/Pic/ilay.jpg") def insertAuthor(dsn,author): with dbapi2.connect(dsn) as connection: cursor = connection.cursor() statement = """ INSERT INTO AUTHORS (NAME, LASTNAME, BIRTHDATE, NATIONALITY, PENNAME, DESCRIPTION,PICTURE) VALUES (%s,%s,%s,%s,%s,%s,%s)""" cursor.execute(statement,(author.name,author.lastname,author.birthdate,author.nationality,author.penname,author.description,author.picture)) cursor.close() def selectAuthor(dsn): with dbapi2.connect(dsn) as connection: cursor = connection.cursor() statement = """SELECT * FROM AUTHORS ORDER BY ID ASC""" cursor.execute(statement) authors = cursor.fetchall() return authors cursor.close() def selectAuthorbyLastName(dsn,lastname): with dbapi2.connect(dsn) as connection: cursor = connection.cursor() statement = """SELECT * FROM AUTHORS WHERE LASTNAME = %s""" cursor.execute(statement,(lastname)) authors = cursor.fetchall() return authors cursor.close() def selectAuthorbyId(dsn,selectid): with dbapi2.connect(dsn) as connection: cursor = connection.cursor() statement = """SELECT * FROM AUTHORS WHERE ID = %s""" cursor.execute(statement,[selectid]) authors = cursor.fetchall() return authors cursor.close() def deleteAuthor(dsn,deleteid): with dbapi2.connect(dsn) as connection: cursor = connection.cursor() statement = """ DELETE FROM AUTHORS WHERE ID = %s""" cursor.execute(statement,(deleteid)) cursor.close() def updateAuthor(dsn,updateid,newauthor): with dbapi2.connect(dsn) as connection: cursor = connection.cursor() statement = """ UPDATE AUTHORS SET NAME = %s, LASTNAME = %s, BIRTHDATE = %s, NATIONALITY = %s, PENNAME = %s DESCRIPTION = %s PICTURE = %s WHERE ID = %s""" cursor.execute(statement,(newauthor.name,newauthor.lastname,newauthor.birthdate,newauthor.nationality,newauthor.penname,newauthor.description,newauthor.picture,updateid)) cursor.close() def getBooksofAuthor(dsn,authorid): with dbapi2.connect(dsn) as connection: cursor = connection.cursor() statement = """ SELECT TITLE FROM BOOKS WHERE AUTHORID = %s""" cursor.execute(statement,(authorid,)) books = cursor.fetchall() cursor.close() return books
import random import traceback import itertools from base64 import b64decode as bd from couchpotato.core.event import addEvent, fireEvent from couchpotato.core.helpers.encoding import toUnicode, ss, tryUrlencode from couchpotato.core.helpers.variable import tryInt, splitString from couchpotato.core.logger import CPLog from couchpotato.core.media.movie.providers.base import MovieProvider from couchpotato.environment import Env log = CPLog(__name__) autoload = 'TheMovieDb' class TheMovieDb(MovieProvider): http_time_between_calls = .35 configuration = { 'images': { 'secure_base_url': 'https://image.tmdb.org/t/p/', }, } ak = ['ZjdmNTE3NzU4NzdlMGJiNjcwMzUyMDk1MmIzYzc4NDA=', 'ZTIyNGZlNGYzZmVjNWY3YjU1NzA2NDFmN2NkM2RmM2E=', 'YTNkYzExMWU2NjEwNWY2Mzg3ZTk5MzkzODEzYWU0ZDU=', 'ZjZiZDY4N2ZmYTYzY2QyODJiNmZmMmM2ODc3ZjI2Njk='] languages = [ 'en' ] default_language = 'en' def __init__(self): addEvent('info.search', self.search, priority = 1) addEvent('movie.search', self.search, priority = 1) addEvent('movie.info', self.getInfo, priority = 1) addEvent('movie.info_by_tmdb', self.getInfo) addEvent('app.load', self.config) def config(self): # Reset invalid key if self.conf('api_key') == '9b939aee0aaafc12a65bf448e4af9543': self.conf('api_key', '') languages = self.getLanguages() # languages should never be empty, the first language is the default language used for all the description details self.default_language = languages[0] # en is always downloaded and it is the fallback if 'en' in languages: languages.remove('en') # default language has a special management languages.remove(self.default_language) self.languages = languages configuration = self.request('configuration') if configuration: self.configuration = configuration def search(self, q, limit = 3): """ Find movie by name """ if self.isDisabled(): return False log.debug('Searching for movie: %s', q) raw = None try: name_year = fireEvent('scanner.name_year', q, single = True) raw = self.request('search/movie', { 'query': name_year.get('name', q), 'year': name_year.get('year'), 'search_type': 'ngram' if limit > 1 else 'phrase' }, return_key = 'results') except: log.error('Failed searching TMDB for "%s": %s', (q, traceback.format_exc())) results = [] if raw: try: nr = 0 for movie in raw: parsed_movie = self.parseMovie(movie, extended = False) if parsed_movie: results.append(parsed_movie) nr += 1 if nr == limit: break log.info('Found: %s', [result['titles'][0] + ' (' + str(result.get('year', 0)) + ')' for result in results]) return results except SyntaxError as e: log.error('Failed to parse XML response: %s', e) return False return results def getInfo(self, identifier = None, extended = True, **kwargs): if not identifier: return {} result = self.parseMovie({ 'id': identifier }, extended = extended) return result or {} def parseMovie(self, movie, extended = True): # Do request, append other items movie = self.request('movie/%s' % movie.get('id'), { 'language': self.conf('preferred_language').upper(), 'append_to_response': 'alternative_titles' + (',images,casts' if extended else ''), }) if not movie: return movie_default = movie if self.default_language == 'en' else self.request('movie/%s' % movie.get('id'), { 'append_to_response': 'alternative_titles' + (',images,casts' if extended else ''), 'language': self.default_language }) movie_default = movie_default or movie movie_others = [ self.request('movie/%s' % movie.get('id'), { 'append_to_response': 'alternative_titles' + (',images,casts' if extended else ''), 'language': language }) for language in self.languages] if self.languages else [] # Images poster = self.getImage(movie, type = 'poster', size = 'w154') poster_original = self.getImage(movie, type = 'poster', size = 'original') backdrop_original = self.getImage(movie, type = 'backdrop', size = 'original') extra_thumbs = self.getMultImages(movie, type = 'backdrops', size = 'original') if extended else [] images = { 'poster': [poster] if poster else [], #'backdrop': [backdrop] if backdrop else [], 'poster_original': [poster_original] if poster_original else [], 'backdrop_original': [backdrop_original] if backdrop_original else [], 'actors': {}, 'extra_thumbs': extra_thumbs } # Genres try: genres = [genre.get('name') for genre in movie.get('genres', [])] except: genres = [] # 1900 is the same as None year = str(movie.get('release_date') or '')[:4] if not movie.get('release_date') or year == '1900' or year.lower() == 'none': year = None # Gather actors data actors = {} if extended: # Full data cast = movie.get('casts', {}).get('cast', []) for cast_item in cast: try: actors[toUnicode(cast_item.get('name'))] = toUnicode(cast_item.get('character')) images['actors'][toUnicode(cast_item.get('name'))] = self.getImage(cast_item, type = 'profile', size = 'original') except: log.debug('Error getting cast info for %s: %s', (cast_item, traceback.format_exc())) movie_data = { 'type': 'movie', 'via_tmdb': True, 'tmdb_id': movie.get('id'), 'alternate_titles': [m['title'] for m in movie['alternative_titles']['titles']], 'titles': [toUnicode(movie_default.get('title') or movie.get('title'))], 'original_title': movie.get('original_title'), 'images': images, 'imdb': movie.get('imdb_id'), 'runtime': movie.get('runtime'), 'released': str(movie.get('release_date')), 'year': tryInt(year, None), 'plot': movie_default.get('overview') or movie.get('overview'), 'genres': genres, 'collection': getattr(movie.get('belongs_to_collection'), 'name', None), 'actor_roles': actors } movie_data = dict((k, v) for k, v in movie_data.items() if v) # Add alternative names movies = [ movie ] + movie_others if movie == movie_default else [ movie, movie_default ] + movie_others movie_titles = [ self.getTitles(movie) for movie in movies ] all_titles = sorted(list(itertools.chain.from_iterable(movie_titles))) alternate_titles = movie_data['titles'] for title in all_titles: if title and title not in alternate_titles and title.lower() != 'none' and title is not None: alternate_titles.append(title) movie_data['titles'] = alternate_titles return movie_data def getImage(self, movie, type = 'poster', size = 'poster'): image_url = '' try: path = movie.get('%s_path' % type) if path: image_url = '%s%s%s' % (self.configuration['images']['secure_base_url'], size, path) except: log.debug('Failed getting %s.%s for "%s"', (type, size, ss(str(movie)))) return image_url def getMultImages(self, movie, type = 'backdrops', size = 'original'): image_urls = [] try: for image in movie.get('images', {}).get(type, [])[1:5]: image_urls.append(self.getImage(image, 'file', size)) except: log.debug('Failed getting %s.%s for "%s"', (type, size, ss(str(movie)))) return image_urls def request(self, call = '', params = {}, return_key = None): params = dict((k, v) for k, v in params.items() if v) params = tryUrlencode(params) try: url = 'https://api.themoviedb.org/3/%s?api_key=%s%s' % (call, self.getApiKey(), '&%s' % params if params else '') data = self.getJsonData(url, show_error = False) except: log.debug('Movie not found: %s, %s', (call, params)) data = None if data and return_key and return_key in data: data = data.get(return_key) return data def isDisabled(self): if self.getApiKey() == '': log.error('No API key provided.') return True return False def getApiKey(self): key = self.conf('api_key') return bd(random.choice(self.ak)) if key == '' else key def getLanguages(self): languages = splitString(Env.setting('languages', section = 'core')) if len(languages): return languages return [ 'en' ] def getTitles(self, movie): # add the title to the list title = toUnicode(movie.get('title')) titles = [title] if title else [] # add the original_title to the list alternate_title = toUnicode(movie.get('original_title')) if alternate_title and alternate_title not in titles: titles.append(alternate_title) # Add alternative titles alternate_titles = movie.get('alternative_titles', {}).get('titles', []) for alt in alternate_titles: alt_name = toUnicode(alt.get('title')) if alt_name and alt_name not in titles and alt_name.lower() != 'none' and alt_name is not None: titles.append(alt_name) return titles; config = [{ 'name': 'themoviedb', 'groups': [ { 'tab': 'searcher', 'name': 'searcher', 'options': [ { 'name': 'preferred_language', 'label': 'Preferred langauge code', 'description': 'Please provide your language code. It will be used for providers supporting altnerate title searching.', 'default': 'en', 'placeholder': 'en|de|fr...', }, ], }, { 'tab': 'providers', 'name': 'tmdb', 'label': 'TheMovieDB', 'hidden': True, 'description': 'Used for all calls to TheMovieDB.', 'options': [ { 'name': 'api_key', 'default': '', 'label': 'Api Key', }, ], }, ], }]
"""Labeled Faces in the Wild (LFW) dataset This dataset is a collection of JPEG pictures of famous people collected over the internet, all details are available on the official website: http://vis-www.cs.umass.edu/lfw/ """ # Copyright (c) 2011 Olivier Grisel <olivier.grisel@ensta.org> # License: BSD 3 clause from os import listdir, makedirs, remove from os.path import dirname, join, exists, isdir import logging from distutils.version import LooseVersion import numpy as np import joblib from joblib import Memory from .base import get_data_home, _fetch_remote, RemoteFileMetadata from ..utils import Bunch logger = logging.getLogger(__name__) # The original data can be found in: # http://vis-www.cs.umass.edu/lfw/lfw.tgz ARCHIVE = RemoteFileMetadata( filename='lfw.tgz', url='https://ndownloader.figshare.com/files/5976018', checksum=('055f7d9c632d7370e6fb4afc7468d40f' '970c34a80d4c6f50ffec63f5a8d536c0')) # The original funneled data can be found in: # http://vis-www.cs.umass.edu/lfw/lfw-funneled.tgz FUNNELED_ARCHIVE = RemoteFileMetadata( filename='lfw-funneled.tgz', url='https://ndownloader.figshare.com/files/5976015', checksum=('b47c8422c8cded889dc5a13418c4bc2a' 'bbda121092b3533a83306f90d900100a')) # The original target data can be found in: # http://vis-www.cs.umass.edu/lfw/pairsDevTrain.txt', # http://vis-www.cs.umass.edu/lfw/pairsDevTest.txt', # http://vis-www.cs.umass.edu/lfw/pairs.txt', TARGETS = ( RemoteFileMetadata( filename='pairsDevTrain.txt', url='https://ndownloader.figshare.com/files/5976012', checksum=('1d454dada7dfeca0e7eab6f65dc4e97a' '6312d44cf142207be28d688be92aabfa')), RemoteFileMetadata( filename='pairsDevTest.txt', url='https://ndownloader.figshare.com/files/5976009', checksum=('7cb06600ea8b2814ac26e946201cdb30' '4296262aad67d046a16a7ec85d0ff87c')), RemoteFileMetadata( filename='pairs.txt', url='https://ndownloader.figshare.com/files/5976006', checksum=('ea42330c62c92989f9d7c03237ed5d59' '1365e89b3e649747777b70e692dc1592')), ) # # Common private utilities for data fetching from the original LFW website # local disk caching, and image decoding. # def _check_fetch_lfw(data_home=None, funneled=True, download_if_missing=True): """Helper function to download any missing LFW data""" data_home = get_data_home(data_home=data_home) lfw_home = join(data_home, "lfw_home") if not exists(lfw_home): makedirs(lfw_home) for target in TARGETS: target_filepath = join(lfw_home, target.filename) if not exists(target_filepath): if download_if_missing: logger.info("Downloading LFW metadata: %s", target.url) _fetch_remote(target, dirname=lfw_home) else: raise IOError("%s is missing" % target_filepath) if funneled: data_folder_path = join(lfw_home, "lfw_funneled") archive = FUNNELED_ARCHIVE else: data_folder_path = join(lfw_home, "lfw") archive = ARCHIVE if not exists(data_folder_path): archive_path = join(lfw_home, archive.filename) if not exists(archive_path): if download_if_missing: logger.info("Downloading LFW data (~200MB): %s", archive.url) _fetch_remote(archive, dirname=lfw_home) else: raise IOError("%s is missing" % archive_path) import tarfile logger.debug("Decompressing the data archive to %s", data_folder_path) tarfile.open(archive_path, "r:gz").extractall(path=lfw_home) remove(archive_path) return lfw_home, data_folder_path def _load_imgs(file_paths, slice_, color, resize): """Internally used to load images""" # import PIL only when needed from ..externals._pilutil import imread, imresize # compute the portion of the images to load to respect the slice_ parameter # given by the caller default_slice = (slice(0, 250), slice(0, 250)) if slice_ is None: slice_ = default_slice else: slice_ = tuple(s or ds for s, ds in zip(slice_, default_slice)) h_slice, w_slice = slice_ h = (h_slice.stop - h_slice.start) // (h_slice.step or 1) w = (w_slice.stop - w_slice.start) // (w_slice.step or 1) if resize is not None: resize = float(resize) h = int(resize * h) w = int(resize * w) # allocate some contiguous memory to host the decoded image slices n_faces = len(file_paths) if not color: faces = np.zeros((n_faces, h, w), dtype=np.float32) else: faces = np.zeros((n_faces, h, w, 3), dtype=np.float32) # iterate over the collected file path to load the jpeg files as numpy # arrays for i, file_path in enumerate(file_paths): if i % 1000 == 0: logger.debug("Loading face #%05d / %05d", i + 1, n_faces) # Checks if jpeg reading worked. Refer to issue #3594 for more # details. img = imread(file_path) if img.ndim is 0: raise RuntimeError("Failed to read the image file %s, " "Please make sure that libjpeg is installed" % file_path) face = np.asarray(img[slice_], dtype=np.float32) face /= 255.0 # scale uint8 coded colors to the [0.0, 1.0] floats if resize is not None: face = imresize(face, resize) if not color: # average the color channels to compute a gray levels # representation face = face.mean(axis=2) faces[i, ...] = face return faces # # Task #1: Face Identification on picture with names # def _fetch_lfw_people(data_folder_path, slice_=None, color=False, resize=None, min_faces_per_person=0): """Perform the actual data loading for the lfw people dataset This operation is meant to be cached by a joblib wrapper. """ # scan the data folder content to retain people with more that # `min_faces_per_person` face pictures person_names, file_paths = [], [] for person_name in sorted(listdir(data_folder_path)): folder_path = join(data_folder_path, person_name) if not isdir(folder_path): continue paths = [join(folder_path, f) for f in sorted(listdir(folder_path))] n_pictures = len(paths) if n_pictures >= min_faces_per_person: person_name = person_name.replace('_', ' ') person_names.extend([person_name] * n_pictures) file_paths.extend(paths) n_faces = len(file_paths) if n_faces == 0: raise ValueError("min_faces_per_person=%d is too restrictive" % min_faces_per_person) target_names = np.unique(person_names) target = np.searchsorted(target_names, person_names) faces = _load_imgs(file_paths, slice_, color, resize) # shuffle the faces with a deterministic RNG scheme to avoid having # all faces of the same person in a row, as it would break some # cross validation and learning algorithms such as SGD and online # k-means that make an IID assumption indices = np.arange(n_faces) np.random.RandomState(42).shuffle(indices) faces, target = faces[indices], target[indices] return faces, target, target_names def fetch_lfw_people(data_home=None, funneled=True, resize=0.5, min_faces_per_person=0, color=False, slice_=(slice(70, 195), slice(78, 172)), download_if_missing=True, return_X_y=False): """Load the Labeled Faces in the Wild (LFW) people dataset \ (classification). Download it if necessary. ================= ======================= Classes 5749 Samples total 13233 Dimensionality 5828 Features real, between 0 and 255 ================= ======================= Read more in the :ref:`User Guide <labeled_faces_in_the_wild_dataset>`. Parameters ---------- data_home : optional, default: None Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in '~/scikit_learn_data' subfolders. funneled : boolean, optional, default: True Download and use the funneled variant of the dataset. resize : float, optional, default 0.5 Ratio used to resize the each face picture. min_faces_per_person : int, optional, default None The extracted dataset will only retain pictures of people that have at least `min_faces_per_person` different pictures. color : boolean, optional, default False Keep the 3 RGB channels instead of averaging them to a single gray level channel. If color is True the shape of the data has one more dimension than the shape with color = False. slice_ : optional Provide a custom 2D slice (height, width) to extract the 'interesting' part of the jpeg files and avoid use statistical correlation from the background download_if_missing : optional, True by default If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site. return_X_y : boolean, default=False. If True, returns ``(dataset.data, dataset.target)`` instead of a Bunch object. See below for more information about the `dataset.data` and `dataset.target` object. .. versionadded:: 0.20 Returns ------- dataset : dict-like object with the following attributes: dataset.data : numpy array of shape (13233, 2914) Each row corresponds to a ravelled face image of original size 62 x 47 pixels. Changing the ``slice_`` or resize parameters will change the shape of the output. dataset.images : numpy array of shape (13233, 62, 47) Each row is a face image corresponding to one of the 5749 people in the dataset. Changing the ``slice_`` or resize parameters will change the shape of the output. dataset.target : numpy array of shape (13233,) Labels associated to each face image. Those labels range from 0-5748 and correspond to the person IDs. dataset.DESCR : string Description of the Labeled Faces in the Wild (LFW) dataset. (data, target) : tuple if ``return_X_y`` is True .. versionadded:: 0.20 """ lfw_home, data_folder_path = _check_fetch_lfw( data_home=data_home, funneled=funneled, download_if_missing=download_if_missing) logger.debug('Loading LFW people faces from %s', lfw_home) # wrap the loader in a memoizing function that will return memmaped data # arrays for optimal memory usage if LooseVersion(joblib.__version__) < LooseVersion('0.12'): # Deal with change of API in joblib m = Memory(cachedir=lfw_home, compress=6, verbose=0) else: m = Memory(location=lfw_home, compress=6, verbose=0) load_func = m.cache(_fetch_lfw_people) # load and memoize the pairs as np arrays faces, target, target_names = load_func( data_folder_path, resize=resize, min_faces_per_person=min_faces_per_person, color=color, slice_=slice_) X = faces.reshape(len(faces), -1) module_path = dirname(__file__) with open(join(module_path, 'descr', 'lfw.rst')) as rst_file: fdescr = rst_file.read() if return_X_y: return X, target # pack the results as a Bunch instance return Bunch(data=X, images=faces, target=target, target_names=target_names, DESCR=fdescr) # # Task #2: Face Verification on pairs of face pictures # def _fetch_lfw_pairs(index_file_path, data_folder_path, slice_=None, color=False, resize=None): """Perform the actual data loading for the LFW pairs dataset This operation is meant to be cached by a joblib wrapper. """ # parse the index file to find the number of pairs to be able to allocate # the right amount of memory before starting to decode the jpeg files with open(index_file_path, 'rb') as index_file: split_lines = [ln.decode().strip().split('\t') for ln in index_file] pair_specs = [sl for sl in split_lines if len(sl) > 2] n_pairs = len(pair_specs) # iterating over the metadata lines for each pair to find the filename to # decode and load in memory target = np.zeros(n_pairs, dtype=np.int) file_paths = list() for i, components in enumerate(pair_specs): if len(components) == 3: target[i] = 1 pair = ( (components[0], int(components[1]) - 1), (components[0], int(components[2]) - 1), ) elif len(components) == 4: target[i] = 0 pair = ( (components[0], int(components[1]) - 1), (components[2], int(components[3]) - 1), ) else: raise ValueError("invalid line %d: %r" % (i + 1, components)) for j, (name, idx) in enumerate(pair): try: person_folder = join(data_folder_path, name) except TypeError: person_folder = join(data_folder_path, str(name, 'UTF-8')) filenames = list(sorted(listdir(person_folder))) file_path = join(person_folder, filenames[idx]) file_paths.append(file_path) pairs = _load_imgs(file_paths, slice_, color, resize) shape = list(pairs.shape) n_faces = shape.pop(0) shape.insert(0, 2) shape.insert(0, n_faces // 2) pairs.shape = shape return pairs, target, np.array(['Different persons', 'Same person']) def fetch_lfw_pairs(subset='train', data_home=None, funneled=True, resize=0.5, color=False, slice_=(slice(70, 195), slice(78, 172)), download_if_missing=True): """Load the Labeled Faces in the Wild (LFW) pairs dataset (classification). Download it if necessary. ================= ======================= Classes 5749 Samples total 13233 Dimensionality 5828 Features real, between 0 and 255 ================= ======================= In the official `README.txt`_ this task is described as the "Restricted" task. As I am not sure as to implement the "Unrestricted" variant correctly, I left it as unsupported for now. .. _`README.txt`: http://vis-www.cs.umass.edu/lfw/README.txt The original images are 250 x 250 pixels, but the default slice and resize arguments reduce them to 62 x 47. Read more in the :ref:`User Guide <labeled_faces_in_the_wild_dataset>`. Parameters ---------- subset : optional, default: 'train' Select the dataset to load: 'train' for the development training set, 'test' for the development test set, and '10_folds' for the official evaluation set that is meant to be used with a 10-folds cross validation. data_home : optional, default: None Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in '~/scikit_learn_data' subfolders. funneled : boolean, optional, default: True Download and use the funneled variant of the dataset. resize : float, optional, default 0.5 Ratio used to resize the each face picture. color : boolean, optional, default False Keep the 3 RGB channels instead of averaging them to a single gray level channel. If color is True the shape of the data has one more dimension than the shape with color = False. slice_ : optional Provide a custom 2D slice (height, width) to extract the 'interesting' part of the jpeg files and avoid use statistical correlation from the background download_if_missing : optional, True by default If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site. Returns ------- The data is returned as a Bunch object with the following attributes: data : numpy array of shape (2200, 5828). Shape depends on ``subset``. Each row corresponds to 2 ravel'd face images of original size 62 x 47 pixels. Changing the ``slice_``, ``resize`` or ``subset`` parameters will change the shape of the output. pairs : numpy array of shape (2200, 2, 62, 47). Shape depends on ``subset`` Each row has 2 face images corresponding to same or different person from the dataset containing 5749 people. Changing the ``slice_``, ``resize`` or ``subset`` parameters will change the shape of the output. target : numpy array of shape (2200,). Shape depends on ``subset``. Labels associated to each pair of images. The two label values being different persons or the same person. DESCR : string Description of the Labeled Faces in the Wild (LFW) dataset. """ lfw_home, data_folder_path = _check_fetch_lfw( data_home=data_home, funneled=funneled, download_if_missing=download_if_missing) logger.debug('Loading %s LFW pairs from %s', subset, lfw_home) # wrap the loader in a memoizing function that will return memmaped data # arrays for optimal memory usage if LooseVersion(joblib.__version__) < LooseVersion('0.12'): # Deal with change of API in joblib m = Memory(cachedir=lfw_home, compress=6, verbose=0) else: m = Memory(location=lfw_home, compress=6, verbose=0) load_func = m.cache(_fetch_lfw_pairs) # select the right metadata file according to the requested subset label_filenames = { 'train': 'pairsDevTrain.txt', 'test': 'pairsDevTest.txt', '10_folds': 'pairs.txt', } if subset not in label_filenames: raise ValueError("subset='%s' is invalid: should be one of %r" % ( subset, list(sorted(label_filenames.keys())))) index_file_path = join(lfw_home, label_filenames[subset]) # load and memoize the pairs as np arrays pairs, target, target_names = load_func( index_file_path, data_folder_path, resize=resize, color=color, slice_=slice_) module_path = dirname(__file__) with open(join(module_path, 'descr', 'lfw.rst')) as rst_file: fdescr = rst_file.read() # pack the results as a Bunch instance return Bunch(data=pairs.reshape(len(pairs), -1), pairs=pairs, target=target, target_names=target_names, DESCR=fdescr)
# Copyright 2012 OpenStack Foundation # # 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. """ To run these tests against a live database: 1. Modify the file ``keystone/tests/unit/config_files/backend_sql.conf`` to use the connection for your live database. 2. Set up a blank, live database 3. Run the tests using:: tox -e py27 -- keystone.tests.unit.test_sql_upgrade WARNING:: Your database will be wiped. Do not do this against a database with valuable data as all data will be lost. """ import copy import json import uuid from migrate.versioning import api as versioning_api from oslo_config import cfg from oslo_db import exception as db_exception from oslo_db.sqlalchemy import migration from oslo_db.sqlalchemy import session as db_session import six from sqlalchemy.engine import reflection import sqlalchemy.exc from sqlalchemy import schema from keystone.assignment.backends import sql as assignment_sql from keystone.common import sql from keystone.common.sql import migrate_repo from keystone.common.sql import migration_helpers from keystone.contrib import federation from keystone.contrib import revoke from keystone import exception from keystone.tests import unit as tests from keystone.tests.unit import default_fixtures from keystone.tests.unit.ksfixtures import database CONF = cfg.CONF DEFAULT_DOMAIN_ID = CONF.identity.default_domain_id # NOTE(morganfainberg): This should be updated when each DB migration collapse # is done to mirror the expected structure of the DB in the format of # { <DB_TABLE_NAME>: [<COLUMN>, <COLUMN>, ...], ... } INITIAL_TABLE_STRUCTURE = { 'credential': [ 'id', 'user_id', 'project_id', 'blob', 'type', 'extra', ], 'domain': [ 'id', 'name', 'enabled', 'extra', ], 'endpoint': [ 'id', 'legacy_endpoint_id', 'interface', 'region', 'service_id', 'url', 'extra', ], 'group': [ 'id', 'domain_id', 'name', 'description', 'extra', ], 'group_domain_metadata': [ 'group_id', 'domain_id', 'data', ], 'group_project_metadata': [ 'group_id', 'project_id', 'data', ], 'policy': [ 'id', 'type', 'blob', 'extra', ], 'project': [ 'id', 'name', 'extra', 'description', 'enabled', 'domain_id', ], 'role': [ 'id', 'name', 'extra', ], 'service': [ 'id', 'type', 'extra', ], 'token': [ 'id', 'expires', 'extra', 'valid', 'trust_id', 'user_id', ], 'trust': [ 'id', 'trustor_user_id', 'trustee_user_id', 'project_id', 'impersonation', 'deleted_at', 'expires_at', 'extra', ], 'trust_role': [ 'trust_id', 'role_id', ], 'user': [ 'id', 'name', 'extra', 'password', 'enabled', 'domain_id', 'default_project_id', ], 'user_domain_metadata': [ 'user_id', 'domain_id', 'data', ], 'user_group_membership': [ 'user_id', 'group_id', ], 'user_project_metadata': [ 'user_id', 'project_id', 'data', ], } INITIAL_EXTENSION_TABLE_STRUCTURE = { 'revocation_event': [ 'id', 'domain_id', 'project_id', 'user_id', 'role_id', 'trust_id', 'consumer_id', 'access_token_id', 'issued_before', 'expires_at', 'revoked_at', 'audit_id', 'audit_chain_id', ], } EXTENSIONS = {'federation': federation, 'revoke': revoke} class SqlMigrateBase(tests.SQLDriverOverrides, tests.TestCase): def initialize_sql(self): self.metadata = sqlalchemy.MetaData() self.metadata.bind = self.engine def config_files(self): config_files = super(SqlMigrateBase, self).config_files() config_files.append(tests.dirs.tests_conf('backend_sql.conf')) return config_files def repo_package(self): return sql def setUp(self): super(SqlMigrateBase, self).setUp() database.initialize_sql_session() conn_str = CONF.database.connection if (conn_str != tests.IN_MEM_DB_CONN_STRING and conn_str.startswith('sqlite') and conn_str[10:] == tests.DEFAULT_TEST_DB_FILE): # Override the default with a DB that is specific to the migration # tests only if the DB Connection string is the same as the global # default. This is required so that no conflicts occur due to the # global default DB already being under migrate control. This is # only needed if the DB is not-in-memory db_file = tests.dirs.tmp('keystone_migrate_test.db') self.config_fixture.config( group='database', connection='sqlite:///%s' % db_file) # create and share a single sqlalchemy engine for testing self.engine = sql.get_engine() self.Session = db_session.get_maker(self.engine, autocommit=False) self.initialize_sql() self.repo_path = migration_helpers.find_migrate_repo( self.repo_package()) self.schema = versioning_api.ControlledSchema.create( self.engine, self.repo_path, self.initial_db_version) # auto-detect the highest available schema version in the migrate_repo self.max_version = self.schema.repository.version().version def tearDown(self): sqlalchemy.orm.session.Session.close_all() meta = sqlalchemy.MetaData() meta.bind = self.engine meta.reflect(self.engine) with self.engine.begin() as conn: inspector = reflection.Inspector.from_engine(self.engine) metadata = schema.MetaData() tbs = [] all_fks = [] for table_name in inspector.get_table_names(): fks = [] for fk in inspector.get_foreign_keys(table_name): if not fk['name']: continue fks.append( schema.ForeignKeyConstraint((), (), name=fk['name'])) table = schema.Table(table_name, metadata, *fks) tbs.append(table) all_fks.extend(fks) for fkc in all_fks: conn.execute(schema.DropConstraint(fkc)) for table in tbs: conn.execute(schema.DropTable(table)) sql.cleanup() super(SqlMigrateBase, self).tearDown() def select_table(self, name): table = sqlalchemy.Table(name, self.metadata, autoload=True) s = sqlalchemy.select([table]) return s def assertTableExists(self, table_name): try: self.select_table(table_name) except sqlalchemy.exc.NoSuchTableError: raise AssertionError('Table "%s" does not exist' % table_name) def assertTableDoesNotExist(self, table_name): """Asserts that a given table exists cannot be selected by name.""" # Switch to a different metadata otherwise you might still # detect renamed or dropped tables try: temp_metadata = sqlalchemy.MetaData() temp_metadata.bind = self.engine sqlalchemy.Table(table_name, temp_metadata, autoload=True) except sqlalchemy.exc.NoSuchTableError: pass else: raise AssertionError('Table "%s" already exists' % table_name) def upgrade(self, *args, **kwargs): self._migrate(*args, **kwargs) def downgrade(self, *args, **kwargs): self._migrate(*args, downgrade=True, **kwargs) def _migrate(self, version, repository=None, downgrade=False, current_schema=None): repository = repository or self.repo_path err = '' version = versioning_api._migrate_version(self.schema, version, not downgrade, err) if not current_schema: current_schema = self.schema changeset = current_schema.changeset(version) for ver, change in changeset: self.schema.runchange(ver, change, changeset.step) self.assertEqual(self.schema.version, version) def assertTableColumns(self, table_name, expected_cols): """Asserts that the table contains the expected set of columns.""" self.initialize_sql() table = self.select_table(table_name) actual_cols = [col.name for col in table.columns] # Check if the columns are equal, but allow for a different order, # which might occur after an upgrade followed by a downgrade self.assertItemsEqual(expected_cols, actual_cols, '%s table' % table_name) @property def initial_db_version(self): return getattr(self, '_initial_db_version', 0) class SqlUpgradeTests(SqlMigrateBase): _initial_db_version = migrate_repo.DB_INIT_VERSION def test_blank_db_to_start(self): self.assertTableDoesNotExist('user') def test_start_version_db_init_version(self): version = migration.db_version(sql.get_engine(), self.repo_path, migrate_repo.DB_INIT_VERSION) self.assertEqual( version, migrate_repo.DB_INIT_VERSION, 'DB is not at version %s' % migrate_repo.DB_INIT_VERSION) def test_two_steps_forward_one_step_back(self): """You should be able to cleanly undo and re-apply all upgrades. Upgrades are run in the following order:: Starting with the initial version defined at keystone.common.migrate_repo.DB_INIT_VERSION INIT +1 -> INIT +2 -> INIT +1 -> INIT +2 -> INIT +3 -> INIT +2 ... ^---------------------^ ^---------------------^ Downgrade to the DB_INIT_VERSION does not occur based on the requirement that the base version be DB_INIT_VERSION + 1 before migration can occur. Downgrade below DB_INIT_VERSION + 1 is no longer supported. DB_INIT_VERSION is the number preceding the release schema version from two releases prior. Example, Juno releases with the DB_INIT_VERSION being 35 where Havana (Havana was two releases before Juno) release schema version is 36. The migrate utility requires the db must be initialized under version control with the revision directly before the first version to be applied. """ for x in range(migrate_repo.DB_INIT_VERSION + 1, self.max_version + 1): self.upgrade(x) downgrade_ver = x - 1 # Don't actually downgrade to the init version. This will raise # a not-implemented error. if downgrade_ver != migrate_repo.DB_INIT_VERSION: self.downgrade(x - 1) self.upgrade(x) def test_upgrade_add_initial_tables(self): self.upgrade(migrate_repo.DB_INIT_VERSION + 1) self.check_initial_table_structure() def check_initial_table_structure(self): for table in INITIAL_TABLE_STRUCTURE: self.assertTableColumns(table, INITIAL_TABLE_STRUCTURE[table]) # Ensure the default domain was properly created. default_domain = migration_helpers.get_default_domain() meta = sqlalchemy.MetaData() meta.bind = self.engine domain_table = sqlalchemy.Table('domain', meta, autoload=True) session = self.Session() q = session.query(domain_table) refs = q.all() self.assertEqual(1, len(refs)) for k in default_domain.keys(): self.assertEqual(default_domain[k], getattr(refs[0], k)) def test_downgrade_to_db_init_version(self): self.upgrade(self.max_version) if self.engine.name == 'mysql': self._mysql_check_all_tables_innodb() self.downgrade(migrate_repo.DB_INIT_VERSION + 1) self.check_initial_table_structure() meta = sqlalchemy.MetaData() meta.bind = self.engine meta.reflect(self.engine) initial_table_set = set(INITIAL_TABLE_STRUCTURE.keys()) table_set = set(meta.tables.keys()) # explicitly remove the migrate_version table, this is not controlled # by the migration scripts and should be exempt from this check. table_set.remove('migrate_version') self.assertSetEqual(initial_table_set, table_set) # Downgrade to before Havana's release schema version (036) is not # supported. A NotImplementedError should be raised when attempting to # downgrade. self.assertRaises(NotImplementedError, self.downgrade, migrate_repo.DB_INIT_VERSION) def insert_dict(self, session, table_name, d, table=None): """Naively inserts key-value pairs into a table, given a dictionary.""" if table is None: this_table = sqlalchemy.Table(table_name, self.metadata, autoload=True) else: this_table = table insert = this_table.insert().values(**d) session.execute(insert) session.commit() def test_region_migration(self): self.assertTableDoesNotExist('region') self.upgrade(37) self.assertTableExists('region') self.downgrade(36) self.assertTableDoesNotExist('region') def test_assignment_table_migration(self): def create_base_data(session): domain_table = sqlalchemy.Table('domain', self.metadata, autoload=True) user_table = sqlalchemy.Table('user', self.metadata, autoload=True) group_table = sqlalchemy.Table('group', self.metadata, autoload=True) role_table = sqlalchemy.Table('role', self.metadata, autoload=True) project_table = sqlalchemy.Table( 'project', self.metadata, autoload=True) base_data = {} # Create a Domain base_data['domain'] = {'id': uuid.uuid4().hex, 'name': uuid.uuid4().hex, 'enabled': True} session.execute(domain_table.insert().values(base_data['domain'])) # Create another Domain base_data['domain2'] = {'id': uuid.uuid4().hex, 'name': uuid.uuid4().hex, 'enabled': True} session.execute(domain_table.insert().values(base_data['domain2'])) # Create a Project base_data['project'] = {'id': uuid.uuid4().hex, 'name': uuid.uuid4().hex, 'domain_id': base_data['domain']['id'], 'extra': "{}"} session.execute( project_table.insert().values(base_data['project'])) # Create another Project base_data['project2'] = {'id': uuid.uuid4().hex, 'name': uuid.uuid4().hex, 'domain_id': base_data['domain']['id'], 'extra': "{}"} session.execute( project_table.insert().values(base_data['project2'])) # Create a User base_data['user'] = {'id': uuid.uuid4().hex, 'name': uuid.uuid4().hex, 'domain_id': base_data['domain']['id'], 'password': uuid.uuid4().hex, 'enabled': True, 'extra': "{}"} session.execute(user_table.insert().values(base_data['user'])) # Create a Group base_data['group'] = {'id': uuid.uuid4().hex, 'name': uuid.uuid4().hex, 'domain_id': base_data['domain']['id'], 'extra': "{}"} session.execute(group_table.insert().values(base_data['group'])) # Create roles base_data['roles'] = [] for _ in range(9): role = {'id': uuid.uuid4().hex, 'name': uuid.uuid4().hex} session.execute(role_table.insert().values(role)) base_data['roles'].append(role) return base_data def populate_grants(session, base_data): user_project_table = sqlalchemy.Table( 'user_project_metadata', self.metadata, autoload=True) user_domain_table = sqlalchemy.Table( 'user_domain_metadata', self.metadata, autoload=True) group_project_table = sqlalchemy.Table( 'group_project_metadata', self.metadata, autoload=True) group_domain_table = sqlalchemy.Table( 'group_domain_metadata', self.metadata, autoload=True) # Grant a role to user on project grant = {'user_id': base_data['user']['id'], 'project_id': base_data['project']['id'], 'data': json.dumps( {'roles': [{'id': base_data['roles'][0]['id']}]})} session.execute(user_project_table.insert().values(grant)) # Grant two roles to user on project2 grant = {'user_id': base_data['user']['id'], 'project_id': base_data['project2']['id'], 'data': json.dumps( {'roles': [{'id': base_data['roles'][1]['id']}, {'id': base_data['roles'][2]['id']}]})} session.execute(user_project_table.insert().values(grant)) # Grant role to group on project grant = {'group_id': base_data['group']['id'], 'project_id': base_data['project']['id'], 'data': json.dumps( {'roles': [{'id': base_data['roles'][3]['id']}]})} session.execute(group_project_table.insert().values(grant)) # Grant two roles to group on project2 grant = {'group_id': base_data['group']['id'], 'project_id': base_data['project2']['id'], 'data': json.dumps( {'roles': [{'id': base_data['roles'][4]['id']}, {'id': base_data['roles'][5]['id']}]})} session.execute(group_project_table.insert().values(grant)) # Grant two roles to group on domain, one inherited, one not grant = {'group_id': base_data['group']['id'], 'domain_id': base_data['domain']['id'], 'data': json.dumps( {'roles': [{'id': base_data['roles'][6]['id']}, {'id': base_data['roles'][7]['id'], 'inherited_to': 'projects'}]})} session.execute(group_domain_table.insert().values(grant)) # Grant inherited role to user on domain grant = {'user_id': base_data['user']['id'], 'domain_id': base_data['domain']['id'], 'data': json.dumps( {'roles': [{'id': base_data['roles'][8]['id'], 'inherited_to': 'projects'}]})} session.execute(user_domain_table.insert().values(grant)) # Grant two non-inherited roles to user on domain2, using roles # that are also assigned to other actors/targets grant = {'user_id': base_data['user']['id'], 'domain_id': base_data['domain2']['id'], 'data': json.dumps( {'roles': [{'id': base_data['roles'][6]['id']}, {'id': base_data['roles'][7]['id']}]})} session.execute(user_domain_table.insert().values(grant)) session.commit() def check_grants(session, base_data): user_project_table = sqlalchemy.Table( 'user_project_metadata', self.metadata, autoload=True) user_domain_table = sqlalchemy.Table( 'user_domain_metadata', self.metadata, autoload=True) group_project_table = sqlalchemy.Table( 'group_project_metadata', self.metadata, autoload=True) group_domain_table = sqlalchemy.Table( 'group_domain_metadata', self.metadata, autoload=True) s = sqlalchemy.select([user_project_table.c.data]).where( (user_project_table.c.user_id == base_data['user']['id']) & (user_project_table.c.project_id == base_data['project']['id'])) r = session.execute(s) data = json.loads(r.fetchone()['data']) self.assertEqual(1, len(data['roles'])) self.assertIn({'id': base_data['roles'][0]['id']}, data['roles']) s = sqlalchemy.select([user_project_table.c.data]).where( (user_project_table.c.user_id == base_data['user']['id']) & (user_project_table.c.project_id == base_data['project2']['id'])) r = session.execute(s) data = json.loads(r.fetchone()['data']) self.assertEqual(2, len(data['roles'])) self.assertIn({'id': base_data['roles'][1]['id']}, data['roles']) self.assertIn({'id': base_data['roles'][2]['id']}, data['roles']) s = sqlalchemy.select([group_project_table.c.data]).where( (group_project_table.c.group_id == base_data['group']['id']) & (group_project_table.c.project_id == base_data['project']['id'])) r = session.execute(s) data = json.loads(r.fetchone()['data']) self.assertEqual(1, len(data['roles'])) self.assertIn({'id': base_data['roles'][3]['id']}, data['roles']) s = sqlalchemy.select([group_project_table.c.data]).where( (group_project_table.c.group_id == base_data['group']['id']) & (group_project_table.c.project_id == base_data['project2']['id'])) r = session.execute(s) data = json.loads(r.fetchone()['data']) self.assertEqual(2, len(data['roles'])) self.assertIn({'id': base_data['roles'][4]['id']}, data['roles']) self.assertIn({'id': base_data['roles'][5]['id']}, data['roles']) s = sqlalchemy.select([group_domain_table.c.data]).where( (group_domain_table.c.group_id == base_data['group']['id']) & (group_domain_table.c.domain_id == base_data['domain']['id'])) r = session.execute(s) data = json.loads(r.fetchone()['data']) self.assertEqual(2, len(data['roles'])) self.assertIn({'id': base_data['roles'][6]['id']}, data['roles']) self.assertIn({'id': base_data['roles'][7]['id'], 'inherited_to': 'projects'}, data['roles']) s = sqlalchemy.select([user_domain_table.c.data]).where( (user_domain_table.c.user_id == base_data['user']['id']) & (user_domain_table.c.domain_id == base_data['domain']['id'])) r = session.execute(s) data = json.loads(r.fetchone()['data']) self.assertEqual(1, len(data['roles'])) self.assertIn({'id': base_data['roles'][8]['id'], 'inherited_to': 'projects'}, data['roles']) s = sqlalchemy.select([user_domain_table.c.data]).where( (user_domain_table.c.user_id == base_data['user']['id']) & (user_domain_table.c.domain_id == base_data['domain2']['id'])) r = session.execute(s) data = json.loads(r.fetchone()['data']) self.assertEqual(2, len(data['roles'])) self.assertIn({'id': base_data['roles'][6]['id']}, data['roles']) self.assertIn({'id': base_data['roles'][7]['id']}, data['roles']) def check_assignments(session, base_data): def check_assignment_type(refs, type): for ref in refs: self.assertEqual(type, ref.type) assignment_table = sqlalchemy.Table( 'assignment', self.metadata, autoload=True) refs = session.query(assignment_table).all() self.assertEqual(11, len(refs)) q = session.query(assignment_table) q = q.filter_by(actor_id=base_data['user']['id']) q = q.filter_by(target_id=base_data['project']['id']) refs = q.all() self.assertEqual(1, len(refs)) self.assertEqual(base_data['roles'][0]['id'], refs[0].role_id) self.assertFalse(refs[0].inherited) check_assignment_type(refs, assignment_sql.AssignmentType.USER_PROJECT) q = session.query(assignment_table) q = q.filter_by(actor_id=base_data['user']['id']) q = q.filter_by(target_id=base_data['project2']['id']) refs = q.all() self.assertEqual(2, len(refs)) role_ids = [base_data['roles'][1]['id'], base_data['roles'][2]['id']] self.assertIn(refs[0].role_id, role_ids) self.assertIn(refs[1].role_id, role_ids) self.assertFalse(refs[0].inherited) self.assertFalse(refs[1].inherited) check_assignment_type(refs, assignment_sql.AssignmentType.USER_PROJECT) q = session.query(assignment_table) q = q.filter_by(actor_id=base_data['group']['id']) q = q.filter_by(target_id=base_data['project']['id']) refs = q.all() self.assertEqual(1, len(refs)) self.assertEqual(base_data['roles'][3]['id'], refs[0].role_id) self.assertFalse(refs[0].inherited) check_assignment_type(refs, assignment_sql.AssignmentType.GROUP_PROJECT) q = session.query(assignment_table) q = q.filter_by(actor_id=base_data['group']['id']) q = q.filter_by(target_id=base_data['project2']['id']) refs = q.all() self.assertEqual(2, len(refs)) role_ids = [base_data['roles'][4]['id'], base_data['roles'][5]['id']] self.assertIn(refs[0].role_id, role_ids) self.assertIn(refs[1].role_id, role_ids) self.assertFalse(refs[0].inherited) self.assertFalse(refs[1].inherited) check_assignment_type(refs, assignment_sql.AssignmentType.GROUP_PROJECT) q = session.query(assignment_table) q = q.filter_by(actor_id=base_data['group']['id']) q = q.filter_by(target_id=base_data['domain']['id']) refs = q.all() self.assertEqual(2, len(refs)) role_ids = [base_data['roles'][6]['id'], base_data['roles'][7]['id']] self.assertIn(refs[0].role_id, role_ids) self.assertIn(refs[1].role_id, role_ids) if refs[0].role_id == base_data['roles'][7]['id']: self.assertTrue(refs[0].inherited) self.assertFalse(refs[1].inherited) else: self.assertTrue(refs[1].inherited) self.assertFalse(refs[0].inherited) check_assignment_type(refs, assignment_sql.AssignmentType.GROUP_DOMAIN) q = session.query(assignment_table) q = q.filter_by(actor_id=base_data['user']['id']) q = q.filter_by(target_id=base_data['domain']['id']) refs = q.all() self.assertEqual(1, len(refs)) self.assertEqual(base_data['roles'][8]['id'], refs[0].role_id) self.assertTrue(refs[0].inherited) check_assignment_type(refs, assignment_sql.AssignmentType.USER_DOMAIN) q = session.query(assignment_table) q = q.filter_by(actor_id=base_data['user']['id']) q = q.filter_by(target_id=base_data['domain2']['id']) refs = q.all() self.assertEqual(2, len(refs)) role_ids = [base_data['roles'][6]['id'], base_data['roles'][7]['id']] self.assertIn(refs[0].role_id, role_ids) self.assertIn(refs[1].role_id, role_ids) self.assertFalse(refs[0].inherited) self.assertFalse(refs[1].inherited) check_assignment_type(refs, assignment_sql.AssignmentType.USER_DOMAIN) self.upgrade(37) session = self.Session() self.assertTableDoesNotExist('assignment') base_data = create_base_data(session) populate_grants(session, base_data) check_grants(session, base_data) session.commit() session.close() self.upgrade(40) session = self.Session() self.assertTableExists('assignment') self.assertTableDoesNotExist('user_project_metadata') self.assertTableDoesNotExist('group_project_metadata') self.assertTableDoesNotExist('user_domain_metadata') self.assertTableDoesNotExist('group_domain_metadata') check_assignments(session, base_data) session.close() self.downgrade(37) session = self.Session() self.assertTableDoesNotExist('assignment') check_grants(session, base_data) session.close() def test_limited_trusts_upgrade(self): # make sure that the remaining_uses column is created self.upgrade(41) self.assertTableColumns('trust', ['id', 'trustor_user_id', 'trustee_user_id', 'project_id', 'impersonation', 'deleted_at', 'expires_at', 'extra', 'remaining_uses']) def test_limited_trusts_downgrade(self): # make sure that the remaining_uses column is removed self.upgrade(41) self.downgrade(40) self.assertTableColumns('trust', ['id', 'trustor_user_id', 'trustee_user_id', 'project_id', 'impersonation', 'deleted_at', 'expires_at', 'extra']) def test_limited_trusts_downgrade_trusts_cleanup(self): # make sure that only trusts with unlimited uses are kept in the # downgrade self.upgrade(41) session = self.Session() limited_trust = { 'id': uuid.uuid4().hex, 'trustor_user_id': uuid.uuid4().hex, 'trustee_user_id': uuid.uuid4().hex, 'project_id': uuid.uuid4().hex, 'impersonation': True, 'remaining_uses': 5 } consumed_trust = { 'id': uuid.uuid4().hex, 'trustor_user_id': uuid.uuid4().hex, 'trustee_user_id': uuid.uuid4().hex, 'project_id': uuid.uuid4().hex, 'impersonation': True, 'remaining_uses': 0 } unlimited_trust = { 'id': uuid.uuid4().hex, 'trustor_user_id': uuid.uuid4().hex, 'trustee_user_id': uuid.uuid4().hex, 'project_id': uuid.uuid4().hex, 'impersonation': True, 'remaining_uses': None } self.insert_dict(session, 'trust', limited_trust) self.insert_dict(session, 'trust', consumed_trust) self.insert_dict(session, 'trust', unlimited_trust) trust_table = sqlalchemy.Table( 'trust', self.metadata, autoload=True) # we should have 3 trusts in base self.assertEqual(3, session.query(trust_table).count()) session.close() self.downgrade(40) session = self.Session() trust_table = sqlalchemy.Table( 'trust', self.metadata, autoload=True) # Now only one trust remains ... self.assertEqual(1, session.query(trust_table.columns.id).count()) # ... and this trust is the one that was not limited in uses self.assertEqual( unlimited_trust['id'], session.query(trust_table.columns.id).one()[0]) def test_upgrade_service_enabled_cols(self): """Migration 44 added `enabled` column to `service` table.""" self.upgrade(44) # Verify that there's an 'enabled' field. exp_cols = ['id', 'type', 'extra', 'enabled'] self.assertTableColumns('service', exp_cols) def test_downgrade_service_enabled_cols(self): """Check columns when downgrade to migration 43. The downgrade from migration 44 removes the `enabled` column from the `service` table. """ self.upgrade(44) self.downgrade(43) exp_cols = ['id', 'type', 'extra'] self.assertTableColumns('service', exp_cols) def test_upgrade_service_enabled_data(self): """Migration 44 has to migrate data from `extra` to `enabled`.""" def add_service(**extra_data): service_id = uuid.uuid4().hex service = { 'id': service_id, 'type': uuid.uuid4().hex, 'extra': json.dumps(extra_data), } self.insert_dict(session, 'service', service) return service_id self.upgrade(43) session = self.Session() # Different services with expected enabled and extra values, and a # description. random_attr_name = uuid.uuid4().hex random_attr_value = uuid.uuid4().hex random_attr = {random_attr_name: random_attr_value} random_attr_str = "%s='%s'" % (random_attr_name, random_attr_value) random_attr_enabled_false = {random_attr_name: random_attr_value, 'enabled': False} random_attr_enabled_false_str = 'enabled=False,%s' % random_attr_str services = [ # Some values for True. (add_service(), (True, {}), 'no enabled'), (add_service(enabled=True), (True, {}), 'enabled=True'), (add_service(enabled='true'), (True, {}), "enabled='true'"), (add_service(**random_attr), (True, random_attr), random_attr_str), (add_service(enabled=None), (True, {}), 'enabled=None'), # Some values for False. (add_service(enabled=False), (False, {}), 'enabled=False'), (add_service(enabled='false'), (False, {}), "enabled='false'"), (add_service(enabled='0'), (False, {}), "enabled='0'"), (add_service(**random_attr_enabled_false), (False, random_attr), random_attr_enabled_false_str), ] session.close() self.upgrade(44) session = self.Session() # Verify that the services have the expected values. self.metadata.clear() service_table = sqlalchemy.Table('service', self.metadata, autoload=True) def fetch_service(service_id): cols = [service_table.c.enabled, service_table.c.extra] f = service_table.c.id == service_id s = sqlalchemy.select(cols).where(f) ep = session.execute(s).fetchone() return ep.enabled, json.loads(ep.extra) for service_id, exp, msg in services: exp_enabled, exp_extra = exp enabled, extra = fetch_service(service_id) self.assertEqual(exp_enabled, enabled, msg) self.assertEqual(exp_extra, extra, msg) def test_downgrade_service_enabled_data(self): """Downgrade from migration 44 migrates data. Downgrade from migration 44 migrates data from `enabled` to `extra`. Any disabled services have 'enabled': False put into 'extra'. """ def add_service(enabled=True, **extra_data): service_id = uuid.uuid4().hex service = { 'id': service_id, 'type': uuid.uuid4().hex, 'extra': json.dumps(extra_data), 'enabled': enabled } self.insert_dict(session, 'service', service) return service_id self.upgrade(44) session = self.Session() # Insert some services using the new format. # We'll need a service entry since it's the foreign key for services. service_id = add_service(True) new_service = (lambda enabled, **extra_data: add_service(enabled, **extra_data)) # Different services with expected extra values, and a # description. services = [ # True tests (new_service(True), {}, 'enabled'), (new_service(True, something='whatever'), {'something': 'whatever'}, "something='whatever'"), # False tests (new_service(False), {'enabled': False}, 'enabled=False'), (new_service(False, something='whatever'), {'enabled': False, 'something': 'whatever'}, "enabled=False, something='whatever'"), ] session.close() self.downgrade(43) session = self.Session() # Verify that the services have the expected values. self.metadata.clear() service_table = sqlalchemy.Table('service', self.metadata, autoload=True) def fetch_service(service_id): cols = [service_table.c.extra] f = service_table.c.id == service_id s = sqlalchemy.select(cols).where(f) ep = session.execute(s).fetchone() return json.loads(ep.extra) for service_id, exp_extra, msg in services: extra = fetch_service(service_id) self.assertEqual(exp_extra, extra, msg) def test_upgrade_endpoint_enabled_cols(self): """Migration 42 added `enabled` column to `endpoint` table.""" self.upgrade(42) # Verify that there's an 'enabled' field. exp_cols = ['id', 'legacy_endpoint_id', 'interface', 'region', 'service_id', 'url', 'extra', 'enabled'] self.assertTableColumns('endpoint', exp_cols) def test_downgrade_endpoint_enabled_cols(self): """Check columns when downgrade from migration 41. The downgrade from migration 42 removes the `enabled` column from the `endpoint` table. """ self.upgrade(42) self.downgrade(41) exp_cols = ['id', 'legacy_endpoint_id', 'interface', 'region', 'service_id', 'url', 'extra'] self.assertTableColumns('endpoint', exp_cols) def test_upgrade_endpoint_enabled_data(self): """Migration 42 has to migrate data from `extra` to `enabled`.""" def add_service(): service_id = uuid.uuid4().hex service = { 'id': service_id, 'type': uuid.uuid4().hex } self.insert_dict(session, 'service', service) return service_id def add_endpoint(service_id, **extra_data): endpoint_id = uuid.uuid4().hex endpoint = { 'id': endpoint_id, 'interface': uuid.uuid4().hex[:8], 'service_id': service_id, 'url': uuid.uuid4().hex, 'extra': json.dumps(extra_data) } self.insert_dict(session, 'endpoint', endpoint) return endpoint_id self.upgrade(41) session = self.Session() # Insert some endpoints using the old format where `enabled` is in # `extra` JSON. # We'll need a service entry since it's the foreign key for endpoints. service_id = add_service() new_ep = lambda **extra_data: add_endpoint(service_id, **extra_data) # Different endpoints with expected enabled and extra values, and a # description. random_attr_name = uuid.uuid4().hex random_attr_value = uuid.uuid4().hex random_attr = {random_attr_name: random_attr_value} random_attr_str = "%s='%s'" % (random_attr_name, random_attr_value) random_attr_enabled_false = {random_attr_name: random_attr_value, 'enabled': False} random_attr_enabled_false_str = 'enabled=False,%s' % random_attr_str endpoints = [ # Some values for True. (new_ep(), (True, {}), 'no enabled'), (new_ep(enabled=True), (True, {}), 'enabled=True'), (new_ep(enabled='true'), (True, {}), "enabled='true'"), (new_ep(**random_attr), (True, random_attr), random_attr_str), (new_ep(enabled=None), (True, {}), 'enabled=None'), # Some values for False. (new_ep(enabled=False), (False, {}), 'enabled=False'), (new_ep(enabled='false'), (False, {}), "enabled='false'"), (new_ep(enabled='0'), (False, {}), "enabled='0'"), (new_ep(**random_attr_enabled_false), (False, random_attr), random_attr_enabled_false_str), ] session.close() self.upgrade(42) session = self.Session() # Verify that the endpoints have the expected values. self.metadata.clear() endpoint_table = sqlalchemy.Table('endpoint', self.metadata, autoload=True) def fetch_endpoint(endpoint_id): cols = [endpoint_table.c.enabled, endpoint_table.c.extra] f = endpoint_table.c.id == endpoint_id s = sqlalchemy.select(cols).where(f) ep = session.execute(s).fetchone() return ep.enabled, json.loads(ep.extra) for endpoint_id, exp, msg in endpoints: exp_enabled, exp_extra = exp enabled, extra = fetch_endpoint(endpoint_id) # NOTE(henry-nash): Different databases may return enabled as a # real boolean of 0/1 - so we use assertEqual not assertIs here. self.assertEqual(exp_enabled, enabled, msg) self.assertEqual(exp_extra, extra, msg) def test_downgrade_endpoint_enabled_data(self): """Downgrade from migration 42 migrates data. Downgrade from migration 42 migrates data from `enabled` to `extra`. Any disabled endpoints have 'enabled': False put into 'extra'. """ def add_service(): service_id = uuid.uuid4().hex service = { 'id': service_id, 'type': uuid.uuid4().hex } self.insert_dict(session, 'service', service) return service_id def add_endpoint(service_id, enabled, **extra_data): endpoint_id = uuid.uuid4().hex endpoint = { 'id': endpoint_id, 'interface': uuid.uuid4().hex[:8], 'service_id': service_id, 'url': uuid.uuid4().hex, 'extra': json.dumps(extra_data), 'enabled': enabled } self.insert_dict(session, 'endpoint', endpoint) return endpoint_id self.upgrade(42) session = self.Session() # Insert some endpoints using the new format. # We'll need a service entry since it's the foreign key for endpoints. service_id = add_service() new_ep = (lambda enabled, **extra_data: add_endpoint(service_id, enabled, **extra_data)) # Different endpoints with expected extra values, and a # description. endpoints = [ # True tests (new_ep(True), {}, 'enabled'), (new_ep(True, something='whatever'), {'something': 'whatever'}, "something='whatever'"), # False tests (new_ep(False), {'enabled': False}, 'enabled=False'), (new_ep(False, something='whatever'), {'enabled': False, 'something': 'whatever'}, "enabled=False, something='whatever'"), ] session.close() self.downgrade(41) session = self.Session() # Verify that the endpoints have the expected values. self.metadata.clear() endpoint_table = sqlalchemy.Table('endpoint', self.metadata, autoload=True) def fetch_endpoint(endpoint_id): cols = [endpoint_table.c.extra] f = endpoint_table.c.id == endpoint_id s = sqlalchemy.select(cols).where(f) ep = session.execute(s).fetchone() return json.loads(ep.extra) for endpoint_id, exp_extra, msg in endpoints: extra = fetch_endpoint(endpoint_id) self.assertEqual(exp_extra, extra, msg) def test_upgrade_region_non_unique_description(self): """Test upgrade to migration 43. This migration should occur with no unique constraint on the region description column. Create two regions with the same description. """ def add_region(): region_uuid = uuid.uuid4().hex region = { 'id': region_uuid, 'description': '' } self.insert_dict(session, 'region', region) return region_uuid self.upgrade(43) session = self.Session() # Write one region to the database add_region() # Write another region to the database with the same description add_region() def test_upgrade_region_unique_description(self): """Test upgrade to migration 43. This test models a migration where there is a unique constraint on the description column. Create two regions with the same description. """ def add_region(table): region_uuid = uuid.uuid4().hex region = { 'id': region_uuid, 'description': '' } self.insert_dict(session, 'region', region, table=table) return region_uuid def get_metadata(): meta = sqlalchemy.MetaData() meta.bind = self.engine return meta # Migrate to version 42 self.upgrade(42) session = self.Session() region_table = sqlalchemy.Table('region', get_metadata(), autoload=True) # create the unique constraint and load the new version of the # reflection cache idx = sqlalchemy.Index('description', region_table.c.description, unique=True) idx.create(self.engine) region_unique_table = sqlalchemy.Table('region', get_metadata(), autoload=True) add_region(region_unique_table) self.assertEqual(1, session.query(region_unique_table).count()) # verify the unique constraint is enforced self.assertRaises( # FIXME (I159): Since oslo_db wraps all the database exceptions # into more specific exception objects, we should catch both of # sqlalchemy and oslo_db exceptions. If an old oslo_db version # is installed, IntegrityError is raised. If >=0.4.0 version of # oslo_db is installed, DBError is raised. # When the global requirements is updated with # the version fixes exceptions wrapping, IntegrityError must be # removed from the tuple. # NOTE(henry-nash): The above re-creation of the (now erased from # history) unique constraint doesn't appear to work well with the # Postgresql SQA driver, leading to it throwing a ValueError, so # we also catch that here. (sqlalchemy.exc.IntegrityError, db_exception.DBError, ValueError), add_region, table=region_unique_table) # migrate to 43, unique constraint should be dropped session.close() self.upgrade(43) session = self.Session() # reload the region table from the schema region_nonunique = sqlalchemy.Table('region', get_metadata(), autoload=True) self.assertEqual(1, session.query(region_nonunique).count()) # Write a second region to the database with the same description add_region(region_nonunique) self.assertEqual(2, session.query(region_nonunique).count()) def test_id_mapping(self): self.upgrade(50) self.assertTableDoesNotExist('id_mapping') self.upgrade(51) self.assertTableExists('id_mapping') self.downgrade(50) self.assertTableDoesNotExist('id_mapping') def test_region_url_upgrade(self): self.upgrade(52) self.assertTableColumns('region', ['id', 'description', 'parent_region_id', 'extra', 'url']) def test_region_url_downgrade(self): self.upgrade(52) self.downgrade(51) self.assertTableColumns('region', ['id', 'description', 'parent_region_id', 'extra']) def test_region_url_cleanup(self): # make sure that the url field is dropped in the downgrade self.upgrade(52) session = self.Session() beta = { 'id': uuid.uuid4().hex, 'description': uuid.uuid4().hex, 'parent_region_id': uuid.uuid4().hex, 'url': uuid.uuid4().hex } acme = { 'id': uuid.uuid4().hex, 'description': uuid.uuid4().hex, 'parent_region_id': uuid.uuid4().hex, 'url': None } self.insert_dict(session, 'region', beta) self.insert_dict(session, 'region', acme) region_table = sqlalchemy.Table('region', self.metadata, autoload=True) self.assertEqual(2, session.query(region_table).count()) session.close() self.downgrade(51) session = self.Session() self.metadata.clear() region_table = sqlalchemy.Table('region', self.metadata, autoload=True) self.assertEqual(2, session.query(region_table).count()) region = session.query(region_table)[0] self.assertRaises(AttributeError, getattr, region, 'url') def test_endpoint_region_upgrade_columns(self): self.upgrade(53) self.assertTableColumns('endpoint', ['id', 'legacy_endpoint_id', 'interface', 'service_id', 'url', 'extra', 'enabled', 'region_id']) region_table = sqlalchemy.Table('region', self.metadata, autoload=True) self.assertEqual(region_table.c.id.type.length, 255) self.assertEqual(region_table.c.parent_region_id.type.length, 255) endpoint_table = sqlalchemy.Table('endpoint', self.metadata, autoload=True) self.assertEqual(endpoint_table.c.region_id.type.length, 255) def test_endpoint_region_downgrade_columns(self): self.upgrade(53) self.downgrade(52) self.assertTableColumns('endpoint', ['id', 'legacy_endpoint_id', 'interface', 'service_id', 'url', 'extra', 'enabled', 'region']) region_table = sqlalchemy.Table('region', self.metadata, autoload=True) self.assertEqual(region_table.c.id.type.length, 64) self.assertEqual(region_table.c.parent_region_id.type.length, 64) endpoint_table = sqlalchemy.Table('endpoint', self.metadata, autoload=True) self.assertEqual(endpoint_table.c.region.type.length, 255) def test_endpoint_region_migration(self): self.upgrade(52) session = self.Session() _small_region_name = '0' * 30 _long_region_name = '0' * 255 _clashing_region_name = '0' * 70 def add_service(): service_id = uuid.uuid4().hex service = { 'id': service_id, 'type': uuid.uuid4().hex } self.insert_dict(session, 'service', service) return service_id def add_endpoint(service_id, region): endpoint_id = uuid.uuid4().hex endpoint = { 'id': endpoint_id, 'interface': uuid.uuid4().hex[:8], 'service_id': service_id, 'url': uuid.uuid4().hex, 'region': region } self.insert_dict(session, 'endpoint', endpoint) return endpoint_id _service_id_ = add_service() add_endpoint(_service_id_, region=_long_region_name) add_endpoint(_service_id_, region=_long_region_name) add_endpoint(_service_id_, region=_clashing_region_name) add_endpoint(_service_id_, region=_small_region_name) add_endpoint(_service_id_, region=None) # upgrade to 53 session.close() self.upgrade(53) session = self.Session() self.metadata.clear() region_table = sqlalchemy.Table('region', self.metadata, autoload=True) self.assertEqual(1, session.query(region_table). filter_by(id=_long_region_name).count()) self.assertEqual(1, session.query(region_table). filter_by(id=_clashing_region_name).count()) self.assertEqual(1, session.query(region_table). filter_by(id=_small_region_name).count()) endpoint_table = sqlalchemy.Table('endpoint', self.metadata, autoload=True) self.assertEqual(5, session.query(endpoint_table).count()) self.assertEqual(2, session.query(endpoint_table). filter_by(region_id=_long_region_name).count()) self.assertEqual(1, session.query(endpoint_table). filter_by(region_id=_clashing_region_name).count()) self.assertEqual(1, session.query(endpoint_table). filter_by(region_id=_small_region_name).count()) # downgrade to 52 session.close() self.downgrade(52) session = self.Session() self.metadata.clear() region_table = sqlalchemy.Table('region', self.metadata, autoload=True) self.assertEqual(1, session.query(region_table).count()) self.assertEqual(1, session.query(region_table). filter_by(id=_small_region_name).count()) endpoint_table = sqlalchemy.Table('endpoint', self.metadata, autoload=True) self.assertEqual(5, session.query(endpoint_table).count()) self.assertEqual(2, session.query(endpoint_table). filter_by(region=_long_region_name).count()) self.assertEqual(1, session.query(endpoint_table). filter_by(region=_clashing_region_name).count()) self.assertEqual(1, session.query(endpoint_table). filter_by(region=_small_region_name).count()) def test_add_actor_id_index(self): self.upgrade(53) self.upgrade(54) table = sqlalchemy.Table('assignment', self.metadata, autoload=True) index_data = [(idx.name, idx.columns.keys()) for idx in table.indexes] self.assertIn(('ix_actor_id', ['actor_id']), index_data) def test_token_user_id_and_trust_id_index_upgrade(self): self.upgrade(54) self.upgrade(55) table = sqlalchemy.Table('token', self.metadata, autoload=True) index_data = [(idx.name, idx.columns.keys()) for idx in table.indexes] self.assertIn(('ix_token_user_id', ['user_id']), index_data) self.assertIn(('ix_token_trust_id', ['trust_id']), index_data) def test_token_user_id_and_trust_id_index_downgrade(self): self.upgrade(55) self.downgrade(54) table = sqlalchemy.Table('token', self.metadata, autoload=True) index_data = [(idx.name, idx.columns.keys()) for idx in table.indexes] self.assertNotIn(('ix_token_user_id', ['user_id']), index_data) self.assertNotIn(('ix_token_trust_id', ['trust_id']), index_data) def test_remove_actor_id_index(self): self.upgrade(54) self.downgrade(53) table = sqlalchemy.Table('assignment', self.metadata, autoload=True) index_data = [(idx.name, idx.columns.keys()) for idx in table.indexes] self.assertNotIn(('ix_actor_id', ['actor_id']), index_data) def test_project_parent_id_upgrade(self): self.upgrade(61) self.assertTableColumns('project', ['id', 'name', 'extra', 'description', 'enabled', 'domain_id', 'parent_id']) def test_project_parent_id_downgrade(self): self.upgrade(61) self.downgrade(60) self.assertTableColumns('project', ['id', 'name', 'extra', 'description', 'enabled', 'domain_id']) def test_project_parent_id_cleanup(self): # make sure that the parent_id field is dropped in the downgrade self.upgrade(61) session = self.Session() domain = {'id': uuid.uuid4().hex, 'name': uuid.uuid4().hex, 'enabled': True} acme = { 'id': uuid.uuid4().hex, 'description': uuid.uuid4().hex, 'domain_id': domain['id'], 'name': uuid.uuid4().hex, 'parent_id': None } beta = { 'id': uuid.uuid4().hex, 'description': uuid.uuid4().hex, 'domain_id': domain['id'], 'name': uuid.uuid4().hex, 'parent_id': acme['id'] } self.insert_dict(session, 'domain', domain) self.insert_dict(session, 'project', acme) self.insert_dict(session, 'project', beta) proj_table = sqlalchemy.Table('project', self.metadata, autoload=True) self.assertEqual(2, session.query(proj_table).count()) session.close() self.downgrade(60) session = self.Session() self.metadata.clear() proj_table = sqlalchemy.Table('project', self.metadata, autoload=True) self.assertEqual(2, session.query(proj_table).count()) project = session.query(proj_table)[0] self.assertRaises(AttributeError, getattr, project, 'parent_id') def test_drop_assignment_role_fk(self): self.upgrade(61) self.assertTrue(self.does_fk_exist('assignment', 'role_id')) self.upgrade(62) if self.engine.name != 'sqlite': # sqlite does not support FK deletions (or enforcement) self.assertFalse(self.does_fk_exist('assignment', 'role_id')) self.downgrade(61) self.assertTrue(self.does_fk_exist('assignment', 'role_id')) def does_fk_exist(self, table, fk_column): inspector = reflection.Inspector.from_engine(self.engine) for fk in inspector.get_foreign_keys(table): if fk_column in fk['constrained_columns']: return True return False def test_drop_region_url_upgrade(self): self.upgrade(63) self.assertTableColumns('region', ['id', 'description', 'parent_region_id', 'extra']) def test_drop_region_url_downgrade(self): self.upgrade(63) self.downgrade(62) self.assertTableColumns('region', ['id', 'description', 'parent_region_id', 'extra', 'url']) def test_drop_domain_fk(self): self.upgrade(63) self.assertTrue(self.does_fk_exist('group', 'domain_id')) self.assertTrue(self.does_fk_exist('user', 'domain_id')) self.upgrade(64) if self.engine.name != 'sqlite': # sqlite does not support FK deletions (or enforcement) self.assertFalse(self.does_fk_exist('group', 'domain_id')) self.assertFalse(self.does_fk_exist('user', 'domain_id')) self.downgrade(63) self.assertTrue(self.does_fk_exist('group', 'domain_id')) self.assertTrue(self.does_fk_exist('user', 'domain_id')) def populate_user_table(self, with_pass_enab=False, with_pass_enab_domain=False): # Populate the appropriate fields in the user # table, depending on the parameters: # # Default: id, name, extra # pass_enab: Add password, enabled as well # pass_enab_domain: Add password, enabled and domain as well # this_table = sqlalchemy.Table("user", self.metadata, autoload=True) for user in default_fixtures.USERS: extra = copy.deepcopy(user) extra.pop('id') extra.pop('name') if with_pass_enab: password = extra.pop('password', None) enabled = extra.pop('enabled', True) ins = this_table.insert().values( {'id': user['id'], 'name': user['name'], 'password': password, 'enabled': bool(enabled), 'extra': json.dumps(extra)}) else: if with_pass_enab_domain: password = extra.pop('password', None) enabled = extra.pop('enabled', True) extra.pop('domain_id') ins = this_table.insert().values( {'id': user['id'], 'name': user['name'], 'domain_id': user['domain_id'], 'password': password, 'enabled': bool(enabled), 'extra': json.dumps(extra)}) else: ins = this_table.insert().values( {'id': user['id'], 'name': user['name'], 'extra': json.dumps(extra)}) self.engine.execute(ins) def populate_tenant_table(self, with_desc_enab=False, with_desc_enab_domain=False): # Populate the appropriate fields in the tenant or # project table, depending on the parameters # # Default: id, name, extra # desc_enab: Add description, enabled as well # desc_enab_domain: Add description, enabled and domain as well, # plus use project instead of tenant # if with_desc_enab_domain: # By this time tenants are now projects this_table = sqlalchemy.Table("project", self.metadata, autoload=True) else: this_table = sqlalchemy.Table("tenant", self.metadata, autoload=True) for tenant in default_fixtures.TENANTS: extra = copy.deepcopy(tenant) extra.pop('id') extra.pop('name') if with_desc_enab: desc = extra.pop('description', None) enabled = extra.pop('enabled', True) ins = this_table.insert().values( {'id': tenant['id'], 'name': tenant['name'], 'description': desc, 'enabled': bool(enabled), 'extra': json.dumps(extra)}) else: if with_desc_enab_domain: desc = extra.pop('description', None) enabled = extra.pop('enabled', True) extra.pop('domain_id') ins = this_table.insert().values( {'id': tenant['id'], 'name': tenant['name'], 'domain_id': tenant['domain_id'], 'description': desc, 'enabled': bool(enabled), 'extra': json.dumps(extra)}) else: ins = this_table.insert().values( {'id': tenant['id'], 'name': tenant['name'], 'extra': json.dumps(extra)}) self.engine.execute(ins) def _mysql_check_all_tables_innodb(self): database = self.engine.url.database connection = self.engine.connect() # sanity check total = connection.execute("SELECT count(*) " "from information_schema.TABLES " "where TABLE_SCHEMA='%(database)s'" % dict(database=database)) self.assertTrue(total.scalar() > 0, "No tables found. Wrong schema?") noninnodb = connection.execute("SELECT table_name " "from information_schema.TABLES " "where TABLE_SCHEMA='%(database)s' " "and ENGINE!='InnoDB' " "and TABLE_NAME!='migrate_version'" % dict(database=database)) names = [x[0] for x in noninnodb] self.assertEqual([], names, "Non-InnoDB tables exist") connection.close() class VersionTests(SqlMigrateBase): _initial_db_version = migrate_repo.DB_INIT_VERSION def test_core_initial(self): """Get the version before migrated, it's the initial DB version.""" version = migration_helpers.get_db_version() self.assertEqual(migrate_repo.DB_INIT_VERSION, version) def test_core_max(self): """When get the version after upgrading, it's the new version.""" self.upgrade(self.max_version) version = migration_helpers.get_db_version() self.assertEqual(self.max_version, version) def test_extension_not_controlled(self): """When get the version before controlling, raises DbMigrationError.""" self.assertRaises(db_exception.DbMigrationError, migration_helpers.get_db_version, extension='federation') def test_extension_initial(self): """When get the initial version of an extension, it's 0.""" for name, extension in six.iteritems(EXTENSIONS): abs_path = migration_helpers.find_migrate_repo(extension) migration.db_version_control(sql.get_engine(), abs_path) version = migration_helpers.get_db_version(extension=name) self.assertEqual(0, version, 'Migrate version for %s is not 0' % name) def test_extension_migrated(self): """When get the version after migrating an extension, it's not 0.""" for name, extension in six.iteritems(EXTENSIONS): abs_path = migration_helpers.find_migrate_repo(extension) migration.db_version_control(sql.get_engine(), abs_path) migration.db_sync(sql.get_engine(), abs_path) version = migration_helpers.get_db_version(extension=name) self.assertTrue( version > 0, "Version for %s didn't change after migrated?" % name) def test_extension_downgraded(self): """When get the version after downgrading an extension, it is 0.""" for name, extension in six.iteritems(EXTENSIONS): abs_path = migration_helpers.find_migrate_repo(extension) migration.db_version_control(sql.get_engine(), abs_path) migration.db_sync(sql.get_engine(), abs_path) version = migration_helpers.get_db_version(extension=name) self.assertTrue( version > 0, "Version for %s didn't change after migrated?" % name) migration.db_sync(sql.get_engine(), abs_path, version=0) version = migration_helpers.get_db_version(extension=name) self.assertEqual(0, version, 'Migrate version for %s is not 0' % name) def test_unexpected_extension(self): """The version for an extension that doesn't exist raises ImportError. """ extension_name = uuid.uuid4().hex self.assertRaises(ImportError, migration_helpers.get_db_version, extension=extension_name) def test_unversioned_extension(self): """The version for extensions without migrations raise an exception. """ self.assertRaises(exception.MigrationNotProvided, migration_helpers.get_db_version, extension='admin_crud') def test_initial_with_extension_version_None(self): """When performing a default migration, also migrate extensions.""" migration_helpers.sync_database_to_version(extension=None, version=None) for table in INITIAL_EXTENSION_TABLE_STRUCTURE: self.assertTableColumns(table, INITIAL_EXTENSION_TABLE_STRUCTURE[table]) def test_initial_with_extension_version_max(self): """When migrating to max version, do not migrate extensions.""" migration_helpers.sync_database_to_version(extension=None, version=self.max_version) for table in INITIAL_EXTENSION_TABLE_STRUCTURE: self.assertTableDoesNotExist(table)
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import requests import time def alos_ql_downloader(): """Функция скачивает квиклуки для ALOS, используя в качестве исходных данных ссылки из 2х .txt-листов: main_links_filepath = основной файл со ссылками, auxl_links_filepath = резервный (на случай 404) файл со ссылками По окончанию в файл report_file записывается список ссылок от незагруженных квиклуков.""" dir = os.path.dirname(__file__) main_links_filepath = os.path.join(dir, 'ALOS_data', 'AL04B.txt') auxl_links_filepath = os.path.join(dir, 'ALOS_data', 'AL05B.txt') report_file = os.path.join(dir, 'ALOS_data', u'Отчёт.txt') dst_dir = os.path.join(dir, 'ALOS_data', 'Downloads') if not os.path.exists(dst_dir): os.mkdir(dst_dir) err_links_list = [] with open(main_links_filepath) as f1: main_links_list = [line.strip() for line in f1.readlines()] with open(auxl_links_filepath) as f2: auxl_links_list = [line.strip() for line in f2.readlines()] for x in range(len(main_links_list) - 1): print('Скачиваем файл {} из {}'.format(x + 1, len(main_links_list) - 1)) filename = main_links_list[x].rsplit('/')[-1] assert filename == auxl_links_list[x].rsplit('/')[-1] if filename not in os.listdir(dst_dir): with open(os.path.join(dst_dir, filename), 'wb') as img_f: r = requests.get(auxl_links_list[x]) try: r.raise_for_status() img_f.write(r.content) print('{0} quicklooks downloaded'.format(x + 1)) time.sleep(0) except requests.HTTPError: r = requests.get(main_links_list[x]) try: r.raise_for_status() img_f.write(r.content) print('{0} quicklooks downloaded'.format(x + 1)) time.sleep(0) except requests.HTTPError: err_links_list.append(auxl_links_list[x]) else: print('{} уже скачан, пропускаем'.format(filename)) with open(report_file, 'w') as rf: rf.writelines(err_links_list) alos_ql_downloader()
# japanese_theme_base.py --- Traditional colors of Japan def parse_csv(s): result = dict() def modifier(x): if x[0] == "#": return x[1:] else: return result[x] ls = s.splitlines() for l in ls: ss = l.split(",") result[ss[0]] = modifier(ss[1]) return result import sys def replace_colors(csv, in_stream=sys.stdin, out_stream=sys.stdout): s = in_stream.read() t = s.format(**csv) out_stream.write(t) class MyString (str): def __init__(self, s): self.upcase = s.upper() def print_colors(csv, fmt, out_stream=sys.stdout): s = "" for k, v in csv.items(): s += fmt.format(MyString(k), MyString(v),n='\n') out_stream.write(s) if __name__ == '__main__': s = "" fmt = "" filename = sys.argv[1] if len(sys.argv) > 2: fmt = sys.argv[2] with open(filename) as f: s = f.read() if s: csv = parse_csv(s) if fmt: print_colors(csv, fmt) else: replace_colors(csv)
from django.conf.urls import url from views import BucketListsView, \ BucketListCreateView, BucketListUpdateView, \ BucketListDeleteView, BucketListDetailView, \ BucketListItemCreateView, BucketListItemUpdateView, \ BucketListItemDoneView, BucketListItemDeleteView urlpatterns = [ # bucket list routes: url(r'^bucketlists/$', BucketListsView.as_view(), name='bucketlists'), url(r'^bucketlists/create/$', BucketListCreateView.as_view(), name='bucketlist_create'), url(r'^bucketlists/(?P<pk>[0-9]+)/update/$', BucketListUpdateView.as_view(), name='bucketlist_update'), url(r'^bucketlists/(?P<pk>[0-9]+)/delete/$', BucketListDeleteView.as_view(), name='bucketlist_delete'), url(r'^bucketlists/(?P<pk>[0-9]+)/$', BucketListDetailView.as_view(), name='bucketlist_details'), # bucket list item routes: url(r'^bucketlists/(?P<pk>[0-9]+)/items/create$', BucketListItemCreateView.as_view(), name='bucketlist_item_create'), url(r'^bucketlists/(?P<pk>[0-9]+)/items/(?P<item_pk>[0-9]+)/update/$', BucketListItemUpdateView.as_view(), name='bucketlist_item_update'), url(r'^bucketlists/(?P<pk>[0-9]+)/items/(?P<item_pk>[0-9]+)/done/$', BucketListItemDoneView.as_view(), name='bucketlist_item_done'), url(r'^bucketlists/(?P<pk>[0-9]+)/items/(?P<item_pk>[0-9]+)/delete/$', BucketListItemDeleteView.as_view(), name='bucketlist_item_delete'), ]
from typing import Tuple, Optional from pathlib import Path import numpy as np import tensorflow as tf import gpflow from gpflow.config import default_float from gpflow.utilities import print_summary from gpflow.utilities import set_trainable from gpflow.likelihoods import Likelihood import tensorflow as tf import tensorflow_probability as tfp tfd = tfp.distributions from sklearn.preprocessing import StandardScaler import matplotlib.pyplot as plt # Pretty much based on this example given on Stack Overflow (with the change of having f and g independent): # https://stackoverflow.com/questions/57597349/likelihoods-combining-multiple-latent-gps-in-gpflow class MultiLatentLikelihood(Likelihood): def __init__(self, num_latent=1, **kwargs): super().__init__(**kwargs) self.num_latent = num_latent def _transform(self, F): return [F[:, i] for i in range(self.num_latent)] def predict_mean_and_var(self, Fmu, Fvar): return super().predict_mean_and_var(self._transform(Fmu), self._transform(Fvar)) def predict_density(self, Fmu, Fvar, Y): return super().predict_density(self._transform(Fmu), self._transform(Fvar), Y) def variational_expectations(self, Fmu, Fvar, Y): return super().variational_expectations(self._transform(Fmu), self._transform(Fvar), Y) class HeteroscedasticGaussian(MultiLatentLikelihood): r""" When using this class, num_latent must be 2. It does not support multi-output (num_output will be 1) """ def __init__(self, transform=tf.nn.softplus, **kwargs): super().__init__(num_latent=2, **kwargs) self.transform = transform def Y_given_F(self, F, G): mu = tf.squeeze(F) sigma = self.transform(tf.squeeze(G)) Y_given_F = tfd.Normal(mu, sigma) return Y_given_F def log_prob(self, F, G, Y): return self.Y_given_F(F, G).log_prob(Y) #was log_prob def conditional_mean(self, F, G): return self.Y_given_F(F, G).mean() def conditional_variance(self, F, G): return self.Y_given_F(F, G).variance() # Load the data D = np.loadtxt('mcycle.csv', delimiter=',') X = D[:, 1:2] Y = D[:, 2:] N = X.shape[0] # Store original data x0 = X.copy() y0 = Y.copy() # Force into Nx1 format # X = x[:, None].reshape(N,1) # y = y[:, None].reshape(N,1) # standardize X_scaler = StandardScaler().fit(X) y_scaler = StandardScaler().fit(Y) X = X_scaler.transform(X) Y = y_scaler.transform(Y) # Kernel kern_list = [gpflow.kernels.Matern32(variance=3., lengthscale=1.), gpflow.kernels.Matern32(variance=3., lengthscale=1.)] kernel = gpflow.kernels.SeparateIndependent(kern_list) # Inducing points (we hack this through SVGP, because the SpearateIndependent support in plain # VGP was broken). We simply put an inducing point at every data point. Xu = X.copy() inducing_variables = gpflow.inducing_variables.mo_inducing_variables.\ SharedIndependentInducingVariables(gpflow.inducing_variables.InducingPoints(Xu)) # The model model = gpflow.models.SVGP(kernel=kernel, likelihood=HeteroscedasticGaussian(), inducing_variable=inducing_variables, num_latent=2) # Set trainable (everything except the 'inducing' points, because we want the full model). set_trainable(model, True) set_trainable(model.inducing_variable.inducing_variable_shared.Z,False) # Print model gpflow.utilities.print_summary(model) # Optimize parameters o = gpflow.optimizers.Scipy() @tf.function(autograph=False) def objective(): return -model.elbo((X,Y)) o.minimize(objective, variables=model.trainable_variables) # Set up plotting def plot(m, ax=None): if ax is None: fig, ax = plt.subplots(1, 1, figsize=(6, 6)) mu, var = m.predict_y(X) ax.plot(X, mu) ax.plot(X, mu + tf.sqrt(var)) ax.plot(X, mu - tf.sqrt(var)) ax.plot(X, Y, '+') # Plot model plot(model) # Print model gpflow.utilities.print_summary(model) # NLPD on training data -tf.reduce_mean(model.predict_log_density((X, Y))) # Load data D = np.loadtxt('mcycle.csv', delimiter=',') X = D[:, 1:2] Y = D[:, 2:] N = X.shape[0] # Standardize X_scaler = StandardScaler().fit(X) y_scaler = StandardScaler().fit(Y) Xall = X_scaler.transform(X) Yall = y_scaler.transform(Y) # Load cross-validation indices cvind = np.loadtxt('cvind.csv').astype(int) # 10-fold cross-validation setup nt = np.floor(cvind.shape[0] / 10).astype(int) cvind = np.reshape(cvind[:10 * nt], (10, nt)) # The model def run_fold(X, Y, XT, YT): # Kernel kern_list = [gpflow.kernels.Matern32(variance=3., lengthscale=1.), gpflow.kernels.Matern32(variance=3., lengthscale=1.)] kernel = gpflow.kernels.SeparateIndependent(kern_list) # Inducing points (we hack this through SVGP, because the SpearateIndependent support in plain # VGP was broken). We simply put an inducing point at every data point. Xu = X.copy() inducing_variables = gpflow.inducing_variables.mo_inducing_variables. \ SharedIndependentInducingVariables(gpflow.inducing_variables.InducingPoints(Xu)) # The model model = gpflow.models.SVGP(kernel=kernel, likelihood=HeteroscedasticGaussian(), inducing_variable=inducing_variables, num_latent=2) # Set trainable (everything except the 'inducing' points, because we want the full model). set_trainable(model, True) set_trainable(model.inducing_variable.inducing_variable_shared.Z, False) # Optimize parameters o = gpflow.optimizers.Scipy() @tf.function(autograph=False) def objective(): return -model.elbo((X, Y)) o.minimize(objective, variables=model.trainable_variables) # Plot model plot(model) # Print model gpflow.utilities.print_summary(model, fmt='notebook') # Return NLPD return -tf.reduce_mean(model.predict_log_density((XT, YT))).numpy() # Array of NLPDs nlpd = [] # Run for each fold for fold in np.arange(10): # Get training and test indices test = cvind[fold, :] train = np.setdiff1d(cvind, test) # Set training and test data X = Xall[train, :] Y = Yall[train, :] XT = Xall[test, :] YT = Yall[test, :] # . Run results res = run_fold(X, Y, XT, YT) nlpd.append(res) print(res) print(np.mean(nlpd)) print(np.std(nlpd))
import datetime import email import re import time import os from email.header import decode_header, make_header from imaplib import ParseFlags class Message(): def __init__(self, mailbox, uid): self.uid = uid self.mailbox = mailbox self.gmail = mailbox.gmail if mailbox else None self.message = None self.headers = {} self.subject = None self.body = None self.html = None self.to = None self.fr = None self.cc = None self.delivered_to = None self.sent_at = None self.flags = [] self.labels = [] self.thread_id = None self.thread = [] self.message_id = None self.attachments = None def is_read(self): return ('\\Seen' in self.flags) def read(self): flag = '\\Seen' self.gmail.imap.uid('STORE', self.uid, '+FLAGS', flag) if flag not in self.flags: self.flags.append(flag) def unread(self): flag = '\\Seen' self.gmail.imap.uid('STORE', self.uid, '-FLAGS', flag) if flag in self.flags: self.flags.remove(flag) def is_starred(self): return ('\\Flagged' in self.flags) def star(self): flag = '\\Flagged' self.gmail.imap.uid('STORE', self.uid, '+FLAGS', flag) if flag not in self.flags: self.flags.append(flag) def unstar(self): flag = '\\Flagged' self.gmail.imap.uid('STORE', self.uid, '-FLAGS', flag) if flag in self.flags: self.flags.remove(flag) def is_draft(self): return ('\\Draft' in self.flags) def has_label(self, label): full_label = '%s' % label return (full_label in self.labels) def add_label(self, label): full_label = '%s' % label self.gmail.imap.uid('STORE', self.uid, '+X-GM-LABELS', full_label) if full_label not in self.labels: self.labels.append(full_label) def remove_label(self, label): full_label = '%s' % label self.gmail.imap.uid('STORE', self.uid, '-X-GM-LABELS', full_label) if full_label in self.labels: self.labels.remove(full_label) def is_deleted(self): return ('\\Deleted' in self.flags) def delete(self): flag = '\\Deleted' self.gmail.imap.uid('STORE', self.uid, '+FLAGS', flag) if flag not in self.flags: self.flags.append(flag) trash = '[Gmail]/Trash' if '[Gmail]/Trash' in self.gmail.labels() else '[Gmail]/Bin' if self.mailbox.name not in ['[Gmail]/Bin', '[Gmail]/Trash']: self.move_to(trash) # def undelete(self): # flag = '\\Deleted' # self.gmail.imap.uid('STORE', self.uid, '-FLAGS', flag) # if flag in self.flags: self.flags.remove(flag) def move_to(self, name): self.gmail.copy(self.uid, name, self.mailbox.name) if name not in ['[Gmail]/Bin', '[Gmail]/Trash']: self.delete() def archive(self): self.move_to('[Gmail]/All Mail') def parse_headers(self, message): hdrs = {} for hdr in message.keys(): hdrs[hdr] = message[hdr] return hdrs def parse_flags(self, headers): return list(ParseFlags(headers)) # flags = re.search(r'FLAGS \(([^\)]*)\)', headers).groups(1)[0].split(' ') def parse_labels(self, headers): if re.search(r'X-GM-LABELS \(([^\)]+)\)', headers): labels = re.search(r'X-GM-LABELS \(([^\)]+)\)', headers).groups(1)[0].split(' ') return map(lambda l: l.replace('"', '').decode("string_escape"), labels) else: return list() def parse_subject(self, encoded_subject): dh = decode_header(encoded_subject) default_charset = 'ASCII' return ''.join([ unicode(t[0], t[1] or default_charset) for t in dh ]) def parse(self, raw_message): raw_headers = raw_message[0] raw_email = raw_message[1] self.message = email.message_from_string(raw_email) self.headers = self.parse_headers(self.message) self.to = self.message['to'] self.fr = self.message['from'] self.delivered_to = self.message['delivered_to'] self.subject = self.parse_subject(self.message['subject']) if self.message.get_content_maintype() == "multipart": for content in self.message.walk(): if content.get_content_type() == "text/plain": self.body = content.get_payload(decode=True) elif content.get_content_type() == "text/html": self.html = content.get_payload(decode=True) elif self.message.get_content_maintype() == "text": self.body = self.message.get_payload() self.sent_at = datetime.datetime.fromtimestamp(time.mktime(email.utils.parsedate_tz(self.message['date'])[:9])) self.flags = self.parse_flags(raw_headers) self.labels = self.parse_labels(raw_headers) if re.search(r'X-GM-THRID (\d+)', raw_headers): self.thread_id = re.search(r'X-GM-THRID (\d+)', raw_headers).groups(1)[0] if re.search(r'X-GM-MSGID (\d+)', raw_headers): self.message_id = re.search(r'X-GM-MSGID (\d+)', raw_headers).groups(1)[0] # Parse attachments into attachment objects array for this message self.attachments = [ Attachment(attachment) for attachment in self.message._payload if not isinstance(attachment, basestring) and attachment.get('Content-Disposition') is not None ] def fetch(self): if not self.message: response, results = self.gmail.imap.uid('FETCH', self.uid, '(BODY.PEEK[] FLAGS X-GM-THRID X-GM-MSGID X-GM-LABELS)') self.parse(results[0]) return self.message # returns a list of fetched messages (both sent and received) in chronological order def fetch_thread(self): self.fetch() original_mailbox = self.mailbox self.gmail.use_mailbox(original_mailbox.name) # fetch and cache messages from inbox or other received mailbox response, results = self.gmail.imap.uid('SEARCH', None, '(X-GM-THRID ' + self.thread_id + ')') received_messages = {} uids = results[0].split(' ') if response == 'OK': for uid in uids: received_messages[uid] = Message(original_mailbox, uid) self.gmail.fetch_multiple_messages(received_messages) self.mailbox.messages.update(received_messages) # fetch and cache messages from 'sent' self.gmail.use_mailbox('[Gmail]/Sent Mail') response, results = self.gmail.imap.uid('SEARCH', None, '(X-GM-THRID ' + self.thread_id + ')') sent_messages = {} uids = results[0].split(' ') if response == 'OK': for uid in uids: sent_messages[uid] = Message(self.gmail.mailboxes['[Gmail]/Sent Mail'], uid) self.gmail.fetch_multiple_messages(sent_messages) self.gmail.mailboxes['[Gmail]/Sent Mail'].messages.update(sent_messages) self.gmail.use_mailbox(original_mailbox.name) # combine and sort sent and received messages return sorted(dict(received_messages.items() + sent_messages.items()).values(), key=lambda m: m.sent_at) class Attachment: def __init__(self, attachment): self.name = attachment.get_filename() # Raw file data self.payload = attachment.get_payload(decode=True) # Filesize in kilobytes self.size = int(round(len(self.payload)/1000.0)) def save(self, path=None): if path is None: # Save as name of attachment if there is no path specified path = self.name elif os.path.isdir(path): # If the path is a directory, save as name of attachment in that directory path = os.path.join(path, self.name) with open(path, 'wb') as f: f.write(self.payload)
''' Created on Jul 12, 2012 @author: gillesB ''' from Window import Window from Workarea import Workarea class WindowTools(object): """ Some utilities for the windows """ @staticmethod def get_active_window(): """ Returns the active window :return: the active window :rtype: Window """ XID = Workarea._root_window.get_full_property(Workarea.atom("_NET_ACTIVE_WINDOW"), 0).value[0] return Window(XID) @staticmethod def print_window_info(keybinding, param): """ Prints some information of the currently active window. :param keybinding: :type keybinding: :param param: :type param: """ window = WindowTools.get_active_window() assert isinstance(window, Window) window_geometry = window.get_geometry() print "Screen resolution: " print "Workarea width and height: ", Workarea.get_workarea_width(), Workarea.get_workarea_height() print "Window title: ", window.get_name() print "Window width and height", window_geometry["width"], window_geometry["height"] , "+ frame size: ", window.get_frame_extents() print "Window position", window_geometry["x"], window_geometry["y"] @staticmethod def get_normal_windows_on_current_desktop(): """ Returns all 'normal' windows which are visible on the current desktop. :return: all 'normal' windows which are visible on the current desktop :rtype: list[Window] """ def m_get_window_from_XID(XID): return Window(XID) def f_normal_window(window): if WindowTools.window_is_on_current_desktop(window) and WindowTools.window_is_window_type_normal(window): return True return False XIDs = Workarea.get_all_XIDs() windows = map(m_get_window_from_XID, XIDs) filtered_windows = filter(f_normal_window, windows) filtered_windows.reverse() return filtered_windows @staticmethod def window_is_on_current_desktop(window): """ Returns True if window is on current desktop, False otherwise :param window: :type window: Window :return: True if window is on current desktop, False otherwise :rtype: bool """ if Workarea.get_current_desktop() == window.get_desktop_id(): return True return False @staticmethod def window_is_window_type_normal(window): """ Returns True if window is a normal window, False otherwise :param window: :type window: Window :return: True if window is a normal window, False otherwise :rtype: bool """ window_type = window.get_window_type() if (window_type == Workarea.atom("_NET_WM_WINDOW_TYPE_NORMAL") or (window_type is None and window.get_transient_for() is None)): return True return False def __init__(self, params): ''' Constructor '''
#! /usr/bin/env python # This script generates input files used to run MCMC from the shell prompt. # Preamble # -------- # To correctly execute this script, one needs to set the correct paths # to the source code. The paths are given as if the Python session # runs from a 'run/' folder at the same level than the repo, as in: # rootdir/ # |-- MCcubed/ # `-- run/ # Alternatively, edit the paths from this script to adjust to your # working directory. # Import the necessary modules: import sys import numpy as np # Import the modules from the MCcubed package: sys.path.append("../MCcubed/") import MCcubed as mc3 # Import the modeling function: sys.path.append("../MCcubed/examples/models/") from quadratic import quad # Create a synthetic dataset using a quadratic polynomial curve: x = np.linspace(0, 10, 1000) # Independent model variable p0 = [3, -2.4, 0.5] # True-underlying model parameters y = quad(p0, x) # Noiseless model uncert = np.sqrt(np.abs(y)) # Data points uncertainty error = np.random.normal(0, uncert) # Noise for the data data = y + error # Noisy data set # data.npz contains the data and uncertainty arrays: mc3.utils.savebin([data, uncert], 'data.npz') # indp.npz contains the list of additional arguments for the model: mc3.utils.savebin([x], 'indp.npz')
#!/usr/bin/env python3 #******************************************************************************** # DynoSprite - scripts/sprite2asm.py # Copyright (c) 2013-2014, Richard Goedeken # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #******************************************************************************** import re import sys import copy # ************************************************************************************************* # Assembly language output classes # ************************************************************************************************* class AsmMetrics: def __init__(self): self.cycles = 0 self.bytes = 0 def __add__(self, other): self.cycles += other.cycles self.bytes += other.bytes return self regA = 1 regB = 2 regD = 3 regE = 4 regF = 8 regW = 12 regQ = 15 regX = 16 regY = 32 regU = 64 regS = 128 regName = { regA:"a", regB:"b", regD:"d", regE:"e", regF:"f", regW:"w", regQ:"q", regX:"x", regY:"y", regU:"u", regS:"s" } class AsmRegisters: def __init__(self): self.valid = 0 self.values = { } def Invalidate(self, regnum): self.valid &= ~regnum def SetValue(self, regnum, value): self.valid |= regnum if regnum == regD: self.values[regA] = value >> 8 self.values[regB] = value & 0xff elif regnum == regW: self.values[regE] = value >> 8 self.values[regF] = value & 0xff elif regnum == regQ: self.values[regA] = (value >> 24) & 0xff self.values[regB] = (value >> 16) & 0xff self.values[regE] = (value >> 8) & 0xff self.values[regF] = (value & 0xff) else: self.values[regnum] = value def GetValue(self, regnum): if regnum == regD: return ((self.values[regA] << 8) + self.values[regB]) elif regnum == regW: return ((self.values[regE] << 8) + self.values[regF]) elif regnum == regQ: return ((self.values[regA] << 24) + (self.values[regB] << 16) + (self.values[regE] << 8) + self.values[regF]) else: return self.values[regnum] def IsValid(self, regnum): return bool((self.valid & regnum) == regnum) class AsmStream: def __init__(self, name, regState=None): self.name = name if name == None: self.text = "" else: self.text = "*" * 60 + f"\n* {name}:\n" + "*" * 60 + f"\n{name}\n" self.metrics = AsmMetrics() if regState == None: self.reg = AsmRegisters() else: self.reg = regState def __add__(self, other): self.metrics += other.metrics self.text += other.text self.reg = other.reg return self def emit_comment(self, text): self.text += " * " + text + "\n" def emit_label(self, text): self.text += f"{text}\n" def emit_op(self, op, reg, comment, cycles6809, cycles6309, bytes): # generate string for this instruction line str = " " + op if reg != "": str += " " * (12 - len(op)) + reg if comment != "": str += " " * (24 - len(reg)) + "* " + comment str += "\n" # update member variables self.text += str if CPU == 6309: self.metrics.cycles += cycles6309 else: self.metrics.cycles += cycles6809 self.metrics.bytes += bytes def gen_loadimm_accum(self, regnum, value, comment): if self.reg.IsValid(regnum) and self.reg.GetValue(regnum) == value: # print("gen_loadimm_accum warning: register '%s' loading value 0x%x it already contains" % (regName[regnum], value)) return # optimization: if possible, demote this accumulator load instruction to a smaller size if regnum == regQ and self.reg.IsValid(regD) and self.reg.GetValue(regD) == ((value >> 16) & 0xffff): regnum = regW value = value & 0xffff elif regnum == regQ and self.reg.IsValid(regW) and self.reg.GetValue(regW) == (value & 0xffff): regnum = regD value = value >> 16 if regnum == regW and self.reg.IsValid(regE) and self.reg.GetValue(regE) == ((value >> 8) & 0xff): regnum = regF value = value & 0xff elif regnum == regW and self.reg.IsValid(regF) and self.reg.GetValue(regF) == (value & 0xff): regnum = regE value = value >> 8 if regnum == regD and self.reg.IsValid(regA) and self.reg.GetValue(regA) == ((value >> 8) & 0xff): regnum = regB value = value & 0xff elif regnum == regD and self.reg.IsValid(regB) and self.reg.GetValue(regB) == (value & 0xff): regnum = regA value = value >> 8 # handle register Q loads separately if regnum == regQ: self.emit_op("ldq", (f"#${value:08x}"), comment, 5, 5, 5) self.reg.SetValue(regQ, value) return # sanity check on register to load if regnum != regA and regnum != regB and regnum != regD and regnum != regE and regnum != regF and regnum != regW: raise Exception(f"invalid accumulator register '{regName[regnum]}' give to gen_loadimm_accum") # if we know the previous value of the register that we're loading, then we may be able to save some # bytes/cycles by modifying it with a clr/com/neg/inc/dec instruction instead of loading it if self.reg.IsValid(regnum): oldval = self.reg.GetValue(regnum) else: oldval = None if value == 0 and (regnum == regA or regnum == regB): self.emit_op(f"clr{regName[regnum]}", "", comment, 2, 1, 1) elif value == 0 and CPU == 6309 and (regnum == regD or regnum == regE or regnum == regF or regnum == regW): self.emit_op(f"clr{regName[regnum]}", "", comment, 3, 2, 2) elif oldval == 255 - value and (regnum == regA or regnum == regB): self.emit_op(f"com{regName[regnum]}", "", comment + f" ({regName[regnum]} = ~${oldval:02x} = ${value:02x})", 2, 1, 1) elif oldval == 255 - value and CPU == 6309 and (regnum == regE or regnum == regF): self.emit_op(f"com{regName[regnum]}", "", comment + f" ({regName[regnum]} = ~${oldval:02x} = ${value:02x})", 3, 2, 2) elif oldval == 65535 - value and CPU == 6309 and (regnum == regD or regnum == regW): self.emit_op(f"com{regName[regnum]}", "", comment + f" ({regName[regnum]} = ~${oldval:04x} = ${value:04x})", 3, 2, 2) elif oldval == (256 - value) & 0xff and (regnum == regA or regnum == regB): self.emit_op(f"neg{regName[regnum]}", "", comment + f" ({regName[regnum]} = -${oldval:02x} = ${value:02x})", 2, 1, 1) elif oldval == (65536 - value) & 0xffff and CPU == 6309 and regnum == regD: self.emit_op(f"neg{regName[regnum]}", "", comment + f" ({regName[regnum]} = -${oldval:04x} = ${value:04x})", 3, 2, 2) elif oldval == (value - 1) & 0xff and (regnum == regA or regnum == regB): self.emit_op(f"inc{regName[regnum]}", "", comment + f" ({regName[regnum]} = ${oldval:02x}+1 = ${value:02x})", 2, 1, 1) elif oldval == (value - 1) & 0xff and CPU == 6309 and (regnum == regE or regnum == regF): self.emit_op(f"inc{regName[regnum]}", "", comment + f" ({regName[regnum]} = ${oldval:02x}+1 = ${value:02x})", 3, 2, 2) elif oldval == (value - 1) & 0xffff and CPU == 6309 and (regnum == regD or regnum == regW): self.emit_op(f"inc{regName[regnum]}", "", comment + f" ({regName[regnum]} = ${oldval:04x}+1 = ${value:04x})", 3, 2, 2) elif oldval == (value + 1) & 0xff and (regnum == regA or regnum == regB): self.emit_op(f"dec{regName[regnum]}", "", comment + f" ({regName[regnum]} = ${oldval:02x}-1 = ${value:02x})", 2, 1, 1) elif oldval == (value + 1) & 0xff and CPU == 6309 and (regnum == regE or regnum == regF): self.emit_op(f"dec{regName[regnum]}", "", comment + f" ({regName[regnum]} = ${oldval:02x}-1 = ${value:02x})", 3, 2, 2) elif oldval == (value + 1) & 0xffff and CPU == 6309 and (regnum == regD or regnum == regW): self.emit_op(f"dec{regName[regnum]}", "", comment + f" ({regName[regnum]} = ${oldval:04x}-1 = ${value:04x})", 3, 2, 2) else: # we must do a full register load instruction if regnum == regD: self.emit_op("ldd", (f"#${value:04x}"), comment, 3, 3, 3) elif regnum == regW: self.emit_op("ldw", (f"#${value:04x}"), comment, 4, 4, 4) elif regnum == regE or regnum == regF: self.emit_op(f"ld{regName[regnum]}", (f"#${value:02x}"), comment, 3, 3, 3) else: self.emit_op(f"ld{regName[regnum]}", (f"#${value:02x}"), comment, 2, 2, 2) self.reg.SetValue(regnum, value) def gen_loadstore_indexed(self, bLoad, regLdSt, regIdx, offset, comment): opcode = "{}{}".format({False:"st",True:"ld"}[bLoad], regName[regLdSt]) if offset == 0: operands = f",{regName[regIdx]}" else: operands = f"{int(offset)},{regName[regIdx]}" if regLdSt == regA or regLdSt == regB: cycles = 4 bytes = 2 elif regLdSt == regE or regLdSt == regF: cycles = 5 bytes = 3 elif regLdSt == regD or regLdSt == regU or regLdSt == regX: cycles = 5 bytes = 2 elif regLdSt == regS or regLdSt == regW or regLdSt == regY: cycles = 6 bytes = 3 elif regLdSt == regQ: cycles = 8 bytes = 3 else: raise Exception(f"Unsupported register {int(regLdSt)}") if offset == 0: cycles += 0 bytes += 0 elif offset < 16 and offset >= -16: cycles += 1 bytes += 0 elif offset < 128 and offset >= -128: cycles += 1 bytes += 1 else: if CPU == 6309: cycles += 3 else: cycles += 4 # only 3 if 6309 bytes += 2 self.emit_op(opcode, operands, comment, cycles, cycles, bytes) if bLoad: self.reg.Invalidate(regLdSt) def gen_loadeffaddr_offset(self, regDst, offset, regSrc, comment): opcode = f"lea{regName[regDst]}" if offset == 0: operands = f",{regName[regSrc]}" else: operands = f"{int(offset)},{regName[regSrc]}" cycles6309 = 4 cycles6809 = 4 bytes = 2 if offset == 0: pass elif offset < 16 and offset >= -16: cycles6309 += 1 cycles6809 += 1 bytes += 0 elif offset < 128 and offset >= -128: cycles6309 += 1 cycles6809 += 1 bytes += 1 else: cycles6309 += 3 cycles6809 += 4 bytes += 2 self.emit_op(opcode, operands, comment, cycles6309, cycles6809, bytes) # ************************************************************************************************* # Sprite class: object definition, parsing, pre/post processing # ************************************************************************************************* class Sprite: def __init__(self, name): # member variables which are read from sprite file self.name = name self.width = 0 self.height = 0 self.hasSinglePixelPos = False self.hasRowPointerArray = False self.matrix = [] self.hotspot = (0, 0) # member variables which are calculated self.numPixels = 0 self.numSavedBytes = 0 self.rowStripList = [] self.originXsprite = 0 # Index of pixel column is sprite which contains the hotspot. # Note that the difference between originXsprite and originXcode must be an even number self.originXcode = 0 # Index of pixel column in sprite which will be written into left pixel (MSB) of # the byte to which the destination pointer is pointing when DrawLeft is called. # When DrawRight is called, this pixel will be written into the right (LSB) of the destination byte self.chunkHint = sys.maxsize # Chunk to process rows - this is a compromise between performance and self.funcErase = AsmStream(f"Erase_{name}") self.funcDraw = [ None, None ] def ReadInputLine(self, line): pivot = line.find('=') if pivot != -1: key = line[0:pivot].strip().lower() value = line[pivot+1:].strip() if key == "width": self.width = int(value) elif key == "height": self.height = int(value) elif key == "singlepixelposition": self.hasSinglePixelPos = (value.lower() == "true") elif key == "rowpointerarray": self.hasRowPointerArray = (value.lower() == "true") elif key == "hotspot": coords = [ int(v) for v in value[1:-1].split(',') ] self.hotspot = (coords[0], coords[1]) elif key == "chunkhint": self.chunkHint = int(value) else: print(f"illegal line in Sprite '{self.name}' definition: {line}") else: rowpix = line.split() if len(rowpix) == self.width: self.matrix.append([-1 if val == "-" else int(val,16) for val in rowpix]) else: print(f"illegal line in Sprite '{self.name}' definition: {line}") def FinishDefinition(self): # check that we loaded all rows of the matrix if len(self.matrix) != self.height: print(f"Sprite [{self.name}] error: Matrix height {int(len(self.matrix))} doesn't match sprite height {int(self.height)}") # create one or two draw functions if self.hasSinglePixelPos: self.funcDraw[0] = AsmStream(f"DrawLeft_{self.name}") self.funcDraw[1] = AsmStream(f"DrawRight_{self.name}") else: self.funcDraw[0] = AsmStream(f"Draw_{self.name}") def Process1_PreCalc(self): # analyze each row and make list of non-transparent strips (consecutive pixels) for y in range(self.height): stripList = [] stripStart = None for x in range(self.width): isTransparent = (self.matrix[y][x] == -1) if not isTransparent: self.numPixels += 1 if not isTransparent and stripStart == None: # start new strip here stripStart = x elif isTransparent and stripStart != None: # strip ends here stripLen = x - stripStart stripList.append((stripStart, stripLen)) stripStart = None # handle strip which ends on last column of row if stripStart != None: stripLen = self.width - stripStart stripList.append((stripStart, stripLen)) # append this strip array to the row list self.rowStripList.append(stripList) # calculate OriginX self.originXsprite = self.hotspot[0] self.originXcode = ((self.width >> 1) & ~1) | (self.originXsprite & 1) # ************************************************************************************************* # Sprite class: Erase function generation # ************************************************************************************************* def Process2_GenErase(self): # print input conditions self.funcErase.emit_comment("Input: X = Pointer to buffer containing saved pixel data") self.funcErase.emit_comment(" Y = Pointer to graphics memory") if CPU == 6309: self.funcErase.emit_comment("Trashed: X,Y,D,W") else: self.funcErase.emit_comment("Trashed: X,Y,D") # this gives the offset (relative to X pointer) from which newly restored bytes will be loaded SrcPtrOffNew = 0 # the lineAdvance parameter gives the offset in rows to the line in graphics memory which we are writing lineAdvance = 0 # the DstCenterOff holds the difference between the center of the sprite and the current destination pointer (Y) # this is only non-zero when using TFM instructions for 6309 code generation DstCenterOff = 0 # iterate over all of the lines for y in range(self.height): stripList = self.rowStripList[y] if len(stripList) == 0: # fixme handle hasRowPointerArray here and other places below lineAdvance += 1 continue # generate a list of all the byte offsets which must be stored byteList = [] for x in range(self.width): if self.matrix[y][x] == -1: continue byteOffL = (x - self.originXcode) >> 1 byteOffR = (x - self.originXcode + 1) >> 1 if len(byteList) == 0 or byteList[-1] != byteOffL: byteList.append(byteOffL) if self.hasSinglePixelPos and byteList[-1] != byteOffR: byteList.append(byteOffR) # now, generate a list of byte strips which must be copied byteStripList = [] byteStart = byteList[0] byteLen = 1 for offX in byteList[1:]: if offX == byteStart + byteLen: byteLen += 1 continue byteStripList.append((byteStart, byteLen)) byteStart = offX byteLen = 1 byteStripList.append((byteStart, byteLen)) # figure out how many bytes to restore in total for strip in byteStripList: self.numSavedBytes += strip[1] # handle each byte strip separately, and for each strip, decide whether to copy the strip # using accumulator load/store instructions, or (6309-only) using TFM instruction for stripIdx in range(len(byteStripList)): strip = byteStripList[stripIdx] # first, try the erase operation with load/store accumulator instructions asmStripAccum = AsmStream(None, copy.deepcopy(self.funcErase.reg)) SrcPtrOffAccum = SrcPtrOffNew lineAdvanceAccum = lineAdvance DstCenterOffAccum = DstCenterOff # accum: figure out how many individual load/store operations we might do before hitting a TFM or the next line numLoadCommands = 0 nextTFMStart = None for idx in range(stripIdx,len(byteStripList)): if CPU == 6309: if strip[1] == 7 or strip[1] >= 9: # this strip will probably be done with TFM, so we will need to adjust destination (leay) # at this point nextTFMStart = strip[0] break numLoadCommands += (strip[1] >> 2) + ((strip[1] & 2) >> 1) + (strip[1] & 1) else: numLoadCommands += (strip[1] >> 1) + (strip[1] & 1) # accum: if necessary, advance the source (X) pointer if SrcPtrOffAccum + strip[1] > 64: bytesToAdvance = SrcPtrOffAccum + 16 asmStripAccum.gen_loadeffaddr_offset(regX, bytesToAdvance, regX, "") SrcPtrOffAccum = -16 # accum: if necessary, advance the destination (Y) pointer across lines if lineAdvanceAccum > 0 and numLoadCommands > 2: if nextTFMStart == None: nextDstCenterOff = 0 else: nextDstCenterOff = nextTFMStart bytesToAdvance = (256 * lineAdvanceAccum) - DstCenterOffAccum + nextDstCenterOff asmStripAccum.gen_loadeffaddr_offset(regY, bytesToAdvance, regY, "") lineAdvanceAccum = 0 DstCenterOffAccum = nextDstCenterOff # accum: emit code to store each bytes containing pixels in this strip if CPU == 6309: # accum: copy strip in 32-bit dwords while strip[1] >= 4: asmStripAccum.gen_loadstore_indexed(True, regQ, regX, SrcPtrOffAccum, "") SrcPtrOffAccum += 4 asmStripAccum.gen_loadstore_indexed(False, regQ, regY, strip[0] + 256*lineAdvanceAccum - DstCenterOffAccum, "") strip = (strip[0] + 4, strip[1] - 4) # accum: copy strip in words while strip[1] >= 2: asmStripAccum.gen_loadstore_indexed(True, regD, regX, SrcPtrOffAccum, "") SrcPtrOffAccum += 2 asmStripAccum.gen_loadstore_indexed(False, regD, regY, strip[0] + 256*lineAdvanceAccum - DstCenterOffAccum, "") strip = (strip[0] + 2, strip[1] - 2) # if strip is odd length, copy a byte if strip[1] == 1: asmStripAccum.gen_loadstore_indexed(True, regA, regX, SrcPtrOffAccum, "") SrcPtrOffAccum += 1 asmStripAccum.gen_loadstore_indexed(False, regA, regY, strip[0] + 256*lineAdvanceAccum - DstCenterOffAccum, "") strip = (strip[0] + 1, strip[1] - 1) # then try the erase operation with TFM instructions if CPU == 6309: asmStripTfm = AsmStream(None, copy.deepcopy(self.funcErase.reg)) SrcPtrOffTfm = SrcPtrOffNew lineAdvanceTfm = lineAdvance DstCenterOffTfm = DstCenterOff strip = byteStripList[stripIdx] # tfm: if necessary, advance the source (X) pointer if SrcPtrOffTfm != 0: bytesToAdvance = SrcPtrOffTfm asmStripTfm.gen_loadeffaddr_offset(regX, bytesToAdvance, regX, "") SrcPtrOffTfm = 0 # tfm: if necessary, advance the destination (Y) pointer to start of copy output region if lineAdvanceTfm > 0 or DstCenterOffTfm != strip[0]: bytesToAdvance = (256 * lineAdvanceTfm) - DstCenterOffTfm + strip[0] asmStripTfm.gen_loadeffaddr_offset(regY, bytesToAdvance, regY, "") lineAdvanceTfm = 0 DstCenterOffTfm = strip[0] # tfm: load number of bytes to copy in W register asmStripTfm.gen_loadimm_accum(regW, strip[1], "") # tfm: do the copy cycles = 6 + 3 * strip[1] asmStripTfm.emit_op("tfm", "x+,y+", "", cycles, cycles, 3) asmStripTfm.reg.SetValue(regW, 0) # tfm: source pointer was advanced, so still at 0, so no need to update SrcPtrOffTfm # tfm: destination pointer was advanced, so update DstCenterOffTfm DstCenterOffTfm += strip[1] # finally, decide whether the Accumulator or TFM methods was faster, and renconcile our state accordingly if CPU == 6309 and asmStripTfm.metrics.cycles < asmStripAccum.metrics.cycles: self.funcErase += asmStripTfm SrcPtrOffNew = SrcPtrOffTfm lineAdvance = lineAdvanceTfm DstCenterOff = DstCenterOffTfm else: self.funcErase += asmStripAccum SrcPtrOffNew = SrcPtrOffAccum lineAdvance = lineAdvanceAccum DstCenterOff = DstCenterOffAccum # now we loop back and do the remaining strips in this row # next iteration will be on the next line lineAdvance += 1 # dump out return instruction self.funcErase.emit_op("rts", "", "", 5, 4, 1) # ************************************************************************************************* # Sprite class: Draw function generation # ************************************************************************************************* def Process3_GenDraw(self, funcNum): # funcNum 0 is for Draw/DrawLeft, funcNum 1 if for DrawRight funcDraw = self.funcDraw[funcNum] # print input conditions funcDraw.emit_comment("Input: X = Pointer to graphics memory") funcDraw.emit_comment(" Y = Pointer to buffer for storing background pixel data") if CPU == 6309: funcDraw.emit_comment("Trashed: X,Y,D,W,U") else: funcDraw.emit_comment("Trashed: X,Y,D,U") # start by generating the Byte Command Strip List for all of the rows byteStripsByRow = [ ] for y in range(self.height): stripList = self.rowStripList[y] if len(stripList) == 0: byteStripsByRow.append([]) continue # generate a list of all the byte offsets which must be stored (same as in Erase function) byteStoreList = [] for x in range(self.width): if self.matrix[y][x] == -1: continue byteOffL = (x - self.originXcode) >> 1 byteOffR = (x - self.originXcode + 1) >> 1 if len(byteStoreList) == 0 or byteStoreList[-1] != byteOffL: byteStoreList.append(byteOffL) if self.hasSinglePixelPos and byteStoreList[-1] != byteOffR: byteStoreList.append(byteOffR) # now generate a list of byte commands for the given draw routine # - Command 0: ignore (no Write routines modify either nibble in byte) # - Command 1: store only (other Write routine modifies byte) # - Command 2: store and write 1 nibble # - Command 3: store and write both nibbles byteCmds = [ ] # value is (command number, value to store, mask) byteOffStart = (0 - self.originXcode) >> 1 byteOffEnd = (self.width - self.originXcode) >> 1 for offX in range(byteOffStart,byteOffEnd+1): pixNibL = ((offX << 1) + self.originXcode - funcNum) pixNibR = pixNibL + 1 nibToWrite = 0 valToWrite = 0 maskToWrite = 0xff if pixNibL >= 0 and pixNibL < self.width and self.matrix[y][pixNibL] != -1: nibToWrite += 1 valToWrite = (self.matrix[y][pixNibL] << 4) maskToWrite = (maskToWrite & 0x0f) if pixNibR >= 0 and pixNibR < self.width and self.matrix[y][pixNibR] != -1: nibToWrite += 1 valToWrite += self.matrix[y][pixNibR] maskToWrite = (maskToWrite & 0xf0) # generate the command if offX not in byteStoreList: if nibToWrite != 0: raise Exception(f"Logical error: byte offset {int(offX)} not in store list, but nibToWrite is {int(nibToWrite)}") byteCmds.append((0, 0, 0)) continue if nibToWrite == 0: byteCmds.append((1, 0, 0)) continue if nibToWrite == 1: byteCmds.append((2, valToWrite, maskToWrite)) continue byteCmds.append((3, valToWrite, 0)) # convert the list of byte commands into a list of strips with consecutive non-command-0 bytes # byteStrips is list of tuples: (byteOffsetStart, [ (command number, value to store, mask), ... ] ) numBytes = byteOffEnd - byteOffStart + 1 byteStrips = [ ] byteCmdStart = None thisStrip = [ ] for idx in range(numBytes+1): byteOff = byteOffStart + idx if idx == numBytes or byteCmds[idx][0] == 0: # command 0 - ignore this byte if byteCmdStart != None: byteStrips.append((byteCmdStart, thisStrip)) byteCmdStart = None thisStrip = [ ] continue # command 1, 2, or 3 if byteCmdStart == None: byteCmdStart = byteOff thisStrip.append(byteCmds[idx]) byteStripsByRow.append(byteStrips) # process the next row # build some custom data structures for the algorithm corresponding with the target CPU if CPU == 6309: pass else: # now we will generate lists of bytes or words (pure command3) which may be written in a line # we will also calculate a 'probability' for each byte/word, so that we will prefer those bytes/words # which are more likely to be written. this is to minimize cross-row loads self.byteWriteProbByRow = [ ] self.wordWriteProbByRow = [ ] for y in range(self.height): byteStrips = byteStripsByRow[y] if len(byteStrips) == 0: self.byteWriteProbByRow.append({}) self.wordWriteProbByRow.append({}) continue byteWriteCntByVal = { } wordWriteCntByVal = { } totalBytes = 0 totalWords = 0 # iterate over all of the strips for (offX,byteCmds) in byteStrips: bytesInStrip = len(byteCmds) # start with bytes (they're easier) if (bytesInStrip & 1) == 1: for idx in range(0, bytesInStrip, 2): # we only care about command-3 bytes if byteCmds[idx][0] == 3: writeVal = byteCmds[idx][1] if writeVal in byteWriteCntByVal: byteWriteCntByVal[writeVal] += 1 else: byteWriteCntByVal[writeVal] = 1 totalBytes += 1 # next do words for even-length strips, or words on even byte boundary in odd-length strip if bytesInStrip > 1: for idx in range(0, bytesInStrip-1, 2): # we only care about words containing both command-3 bytes if byteCmds[idx][0] == 3 and byteCmds[idx+1][0] == 3: writeVal = (byteCmds[idx][1] << 8) + byteCmds[idx+1][1] if writeVal in wordWriteCntByVal: wordWriteCntByVal[writeVal] += 1 else: wordWriteCntByVal[writeVal] = 1 totalWords += 1 # finally, do words on odd-byte boundary in an odd-length strip if bytesInStrip > 1 and (bytesInStrip & 1) == 1: # do all words on an odd byte boundary for idx in range(1, bytesInStrip-1, 2): # we only care about words containing both command-3 bytes if byteCmds[idx][0] == 3 and byteCmds[idx+1][0] == 3: writeVal = (byteCmds[idx][1] << 8) + byteCmds[idx+1][1] if writeVal in wordWriteCntByVal: wordWriteCntByVal[writeVal] += 1 else: wordWriteCntByVal[writeVal] = 1 totalWords += 1 # now generate dictionaries containing writeValue ==> probability byteWriteProb = { } wordWriteProb = { } for val in byteWriteCntByVal: byteWriteProb[val] = float(byteWriteCntByVal[val]) / totalBytes for val in wordWriteCntByVal: wordWriteProb[val] = float(wordWriteCntByVal[val]) / totalWords # add these dictionaries to the row lists self.byteWriteProbByRow.append(byteWriteProb) self.wordWriteProbByRow.append(wordWriteProb) # main loop of Draw generator: process each line separately # YPtrOffNew gives the offset (relative to Y pointer) to which background graphic bytes will be saved self.YPtrOffNew = 0 # the lineAdvance parameter gives the offset in rows to the line in graphics memory which we are writing self.lineAdvance = 0 # but this is only used for the 6809. For the 6309, we always use "addr U,X" to advance one row if CPU == 6309: funcDraw.emit_op("ldu","#256","", 3, 3, 3); # now we will generate optimized assembly code for each row regState = AsmRegisters() for y in range(self.height): # get list of byte command strips for this row, and count total number of bytes to save byteStrips = byteStripsByRow[y] totalBytesToSave = 0 if len(byteStrips) == 0: # fixme handle hasRowPointerArray here and other places below self.lineAdvance += 1 continue for cmdStrip in byteStrips: totalBytesToSave += len(cmdStrip[1]) # set a flag if we will only store one byte or one word for this row bSingleWriteOp = totalBytesToSave < 2 or (totalBytesToSave == 2 and len(byteStrips) == 1) # advance the Y pointer if necessary if self.YPtrOffNew + totalBytesToSave > 64: bytesToAdvance = self.YPtrOffNew + 16 funcDraw.gen_loadeffaddr_offset(regY, bytesToAdvance, regY, "") self.YPtrOffNew = -16 # advance the X pointer if necessary if CPU == 6309: if self.lineAdvance == 1: funcDraw.emit_op("addr", "u,x", "", 4, 4, 3) elif self.lineAdvance > 1: funcDraw.gen_loadeffaddr_offset(regX, 256*self.lineAdvance, regX, "") self.lineAdvance = 0 else: if self.lineAdvance > 0 and not bSingleWriteOp: funcDraw.gen_loadeffaddr_offset(regX, 256*self.lineAdvance, regX, "") self.lineAdvance = 0 # fixme save the YPtrOffNew here if hasRowPointerArray is true if self.hasRowPointerArray: funcDraw.emit_label(f"Row{int(y)}_{funcDraw.name}") # call the specific row handler method for the current CPU if CPU == 6309: rowAsm = self.RowDraw6309(y, regState, byteStrips) else: rowAsm = self.RowDraw6809(y, regState, byteStrips) # update our draw function with the best Asm code for this row, and update our register state tracking variable funcDraw += rowAsm regState = rowAsm.reg # next iteration will be on the next line self.YPtrOffNew += totalBytesToSave self.lineAdvance += 1 # dump out return instruction funcDraw.emit_op("rts", "", "", 5, 4, 1) # ************************************************************************************************* # Sprite class: Draw function row generation for 6309 # ************************************************************************************************* def RowDraw6309(self, y, regState, byteStrips): # generate a byte command list, which is a list of tuples: (srcOffset, dstOffset, command, value, mask) byteOffCmdList = [] dstOffset = 0 for strip in byteStrips: off0 = strip[0] byteCmds = strip[1] for byteIdx in range(len(byteCmds)): thisCmd = byteCmds[byteIdx] byteOffCmdList.append((off0+byteIdx, dstOffset, thisCmd[0], thisCmd[1], thisCmd[2])) dstOffset += 1 # in block of n commands, exhaustively search all permutations of store/write commands, and return best result # we choose n = 12 as a compromise for compile time performance and runtime performance layoutDict = { 1:[], 2:[], 4:[] } results = [self.Permute6309StoreLayouts(regState, layoutDict, byteOffCmdList[ii:ii + self.chunkHint], 4) for ii in range(0, len(byteOffCmdList), self.chunkHint)] return_value = results[0] for ii in range(1, len(results)): return_value += results[ii] return return_value def Permute6309StoreLayouts(self, regState, layoutDict, byteOffCmdList, searchSize): cmdListLen = len(byteOffCmdList) if cmdListLen == 0: # this is a leaf node, so we need to emit the code for handling the Store operations startAsm = AsmStream(None, regState) writeByteList = [ ] layoutCopy = { 1:copy.copy(layoutDict[1]), 2:copy.copy(layoutDict[2]), 4:copy.copy(layoutDict[4]) } return self.Permute6309StoreCodeGen(startAsm, layoutCopy, writeByteList, 4, None) if searchSize == 4: # search for DWORDs that we can store bFoundCandidates = False for cmdIdx in range(cmdListLen-3): # are these bytes consecutive? if byteOffCmdList[cmdIdx+1][0] != byteOffCmdList[cmdIdx][0] + 1: continue if byteOffCmdList[cmdIdx+2][0] != byteOffCmdList[cmdIdx][0] + 2: continue if byteOffCmdList[cmdIdx+3][0] != byteOffCmdList[cmdIdx][0] + 3: continue # can't use Q if the last 2 bytes require AND/OR (command 2) if byteOffCmdList[cmdIdx+2][2] == 2 or byteOffCmdList[cmdIdx+3][2] == 2: continue # otherwise, this position is a candidate for Store operation with 32-bit Q accumulator DWordCmdList = [] for i in range(4): DWordCmdList.append((byteOffCmdList[cmdIdx+i][2], byteOffCmdList[cmdIdx+i][3], byteOffCmdList[cmdIdx+i][4])) layoutDict[4].append((byteOffCmdList[cmdIdx][0], byteOffCmdList[cmdIdx][1], DWordCmdList)) newByteOffCmdList = byteOffCmdList[:cmdIdx] + byteOffCmdList[cmdIdx+4:] # recurse down into this case trialAsm = self.Permute6309StoreLayouts(regState, layoutDict, newByteOffCmdList, 4) # revert our layoutDict changes del layoutDict[4][-1] # update our bestAsm if not bFoundCandidates: bFoundCandidates = True bestAsm = trialAsm else: bestAsm = self.BestResult(bestAsm, trialAsm) continue # if we found any DWORD candidates during this function search, then just return the best result if bFoundCandidates: return bestAsm # otherwise our search continues with WORDs searchSize = 2 if searchSize == 2: # search for WORDs that we can store bFoundCandidates = False for cmdIdx in range(cmdListLen-1): # are these bytes consecutive? if byteOffCmdList[cmdIdx+1][0] != byteOffCmdList[cmdIdx][0] + 1: continue # otherwise, this position is a candidate for Store operation with 16-bit D accumulator WordCmdList = [] for i in range(2): WordCmdList.append((byteOffCmdList[cmdIdx+i][2], byteOffCmdList[cmdIdx+i][3], byteOffCmdList[cmdIdx+i][4])) layoutDict[2].append((byteOffCmdList[cmdIdx][0], byteOffCmdList[cmdIdx][1], WordCmdList)) newByteOffCmdList = byteOffCmdList[:cmdIdx] + byteOffCmdList[cmdIdx+2:] # recurse down into this case trialAsm = self.Permute6309StoreLayouts(regState, layoutDict, newByteOffCmdList, 2) # revert our layoutDict changes del layoutDict[2][-1] # update our bestAsm if not bFoundCandidates: bFoundCandidates = True bestAsm = trialAsm else: bestAsm = self.BestResult(bestAsm, trialAsm) continue # if we found any WORD candidates during this function search, then just return the best result if bFoundCandidates: return bestAsm # otherwise our search continues with BYTEs searchSize = 1 # search size must be 1 if searchSize != 1: raise Exception("Error: searchSize != 1 at end of Permute6309StoreLayouts!") # recurse over all possible orderings of BYTEs to store bFoundCandidates = False for cmdIdx in range(cmdListLen): layoutDict[1].append(byteOffCmdList[cmdIdx]) newByteOffCmdList = byteOffCmdList[:cmdIdx] + byteOffCmdList[cmdIdx+1:] # recurse down into this case trialAsm = self.Permute6309StoreLayouts(regState, layoutDict, newByteOffCmdList, 1) # revert our layoutDict changes del layoutDict[1][-1] # update our bestAsm if not bFoundCandidates: bFoundCandidates = True bestAsm = trialAsm else: bestAsm = self.BestResult(bestAsm, trialAsm) continue # return the best result return bestAsm def Permute6309StoreCodeGen(self, bestRowAsm, layoutDict, writeByteList, storeSize, scratchReg): if storeSize == 1 and len(layoutDict[1]) == 0: # this is a leaf node, so we need to permute across all possible variations of Write operations # start by make a copy of our list of bytes to write, and then sorting it writeByteListCopy = copy.copy(writeByteList) writeByteListCopy.sort() # it will be sorted primarily by the first element in the tuple, which is offX # now call a function to permute all possible write commands layoutDict = { 1:[], 2:[], 4:[] } return self.Permute6309WriteLayouts(bestRowAsm, layoutDict, writeByteListCopy, 4) if storeSize == 4: # if there are no 4-byte stores left, then recurse to handle 2-byte stores if len(layoutDict[4]) == 0: return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 2, None) # store the first DWORD in the list store4Cmds = layoutDict[4].pop(0) offX = store4Cmds[0] offY = self.YPtrOffNew + store4Cmds[1] bestRowAsm.gen_loadstore_indexed(True, regQ, regX, offX, "") # ldq off,x bestRowAsm.gen_loadstore_indexed(False, regQ, regY, offY, "") # if this 4-byte store contains command-2 bytes in the lower 2 positions, then crash if store4Cmds[2][2][0] == 2 or store4Cmds[2][3][0] == 2: raise Exception("Error: word in 32-bit store command contains Command-2 bytes in low WORD!") # if this 4-byte store contains no command-2 bytes, just add to the WriteByteList and continue with next DWORD if store4Cmds[2][0][0] != 2 and store4Cmds[2][1][0] != 2: for byteIdx in range(4): cmd = store4Cmds[2][byteIdx] if cmd[0] == 3: writeByteList.append((offX+byteIdx, cmd[1])) return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 4, None) # generate AND/OR instructions to handle command-2 bytes in A and/or B registers self.GenerateCommand2RegisterOps(store4Cmds[2][0], store4Cmds[2][1], bestRowAsm) # if all 4 bytes are going to be written, then we should write them now (faster) if store4Cmds[2][0][0] > 1 and store4Cmds[2][1][0] > 1 and store4Cmds[2][2][0] == 3 and store4Cmds[2][3][0] == 3: if store4Cmds[2][0][0] == 3: bestRowAsm.gen_loadimm_accum(regA, store4Cmds[2][0][1], "") elif store4Cmds[2][1][0] == 3: bestRowAsm.gen_loadimm_accum(regB, store4Cmds[2][1][1], "") wordLoad = (store4Cmds[2][2][1] << 8) + store4Cmds[2][3][1] bestRowAsm.gen_loadimm_accum(regW, wordLoad, "") bestRowAsm.gen_loadstore_indexed(False, regQ, regX, offX, "") # stq off,x # fixme optimize: permute all possible byte/word stores with valid accumulators now return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 4, None) # put lower 2 bytes into write list, we will deal with them later for byteIdx in range(2,4): cmd = store4Cmds[2][byteIdx] if cmd[0] == 3: writeByteList.append((offX+byteIdx, cmd[1])) # if only one of the 2 upper bytes needs to be written, the write it and continue if store4Cmds[2][0][0] == 1: # only write B bestRowAsm.gen_loadstore_indexed(False, regB, regX, offX+1, "") # stb off,x return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 4, None) if store4Cmds[2][1][0] == 1: # only write A bestRowAsm.gen_loadstore_indexed(False, regA, regX, offX, "") # sta off,x return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 4, None) # if both upper bytes are command-2, write D and continue if store4Cmds[2][0][0] == 2 and store4Cmds[2][1][0] == 2: bestRowAsm.gen_loadstore_indexed(False, regD, regX, offX, "") # std off,x return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 4, None) # now we're in a situation where one byte is ready to write (command-2), but the other is command-3 # we will write them both now with reg D if store4Cmds[2][0][0] == 3: bestRowAsm.gen_loadimm_accum(regA, store4Cmds[2][0][1], "") else: bestRowAsm.gen_loadimm_accum(regB, store4Cmds[2][1][1], "") bestRowAsm.gen_loadstore_indexed(False, regD, regX, offX, "") # std off,x return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 4, None) if storeSize == 2: # if there are no 2-byte stores left, then recurse to handle 1-byte stores if len(layoutDict[2]) == 0: return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 1, None) # store the first WORD in the list store2Cmds = layoutDict[2].pop(0) offX = store2Cmds[0] offY = self.YPtrOffNew + store2Cmds[1] bestRowAsm.gen_loadstore_indexed(True, regD, regX, offX, "") # ldd off,x bestRowAsm.gen_loadstore_indexed(False, regD, regY, offY, "") # if this 2-byte store contains no command-2 bytes, just add to the WriteByteList and continue with next DWORD if store2Cmds[2][0][0] != 2 and store2Cmds[2][1][0] != 2: for byteIdx in range(2): cmd = store2Cmds[2][byteIdx] if cmd[0] == 3: writeByteList.append((offX+byteIdx, cmd[1])) return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 2, None) # generate AND/OR instructions to handle command-2 bytes in A and/or B registers self.GenerateCommand2RegisterOps(store2Cmds[2][0], store2Cmds[2][1], bestRowAsm) # if only one of the 2 upper bytes needs to be written, the write it and continue if store2Cmds[2][0][0] == 1: # only write B bestRowAsm.gen_loadstore_indexed(False, regB, regX, offX+1, "") # stb off,x return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 2, None) if store2Cmds[2][1][0] == 1: # only write A bestRowAsm.gen_loadstore_indexed(False, regA, regX, offX, "") # sta off,x return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 2, None) # if both upper bytes are command-2, write D and continue if store2Cmds[2][0][0] == 2 and store2Cmds[2][1][0] == 2: bestRowAsm.gen_loadstore_indexed(False, regD, regX, offX, "") # std off,x return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 2, None) # now we're in a situation where one byte is ready to write (command-2), but the other is command-3 # we will write them both now with reg D if store2Cmds[2][0][0] == 3: bestRowAsm.gen_loadimm_accum(regA, store2Cmds[2][0][1], "") else: bestRowAsm.gen_loadimm_accum(regB, store2Cmds[2][1][1], "") bestRowAsm.gen_loadstore_indexed(False, regD, regX, offX, "") # std off,x # fixme optimize: permute all possible byte/word stores with valid accumulators now return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 2, None) # store size must be 1 if storeSize != 1: raise Exception("Error: storeSize != 1 at end of Permute6309StoreCodeGen!") # figure out whiche register to use as scratch for the byte stores if scratchReg == None: if not bestRowAsm.reg.IsValid(regA): scratchReg = regA elif not bestRowAsm.reg.IsValid(regB): scratchReg = regB else: # both A and B are valid, so permute over 2 possible cases and return best trial1Asm = copy.deepcopy(bestRowAsm) layoutCopy = { 1:copy.copy(layoutDict[1]), 2:copy.copy(layoutDict[2]), 4:copy.copy(layoutDict[4]) } trial1Asm = self.Permute6309StoreCodeGen(trial1Asm, layoutCopy, copy.copy(writeByteList), 1, regA) # then try out the second case trial2Asm = bestRowAsm trial2Asm = self.Permute6309StoreCodeGen(trial2Asm, layoutDict, writeByteList, 1, regB) # now choose the best option return self.BestResult(trial1Asm, trial2Asm) # store each BYTE in the list while len(layoutDict[1]) > 0: store1Cmd = layoutDict[1].pop(0) offX = store1Cmd[0] offY = self.YPtrOffNew + store1Cmd[1] bestRowAsm.gen_loadstore_indexed(True, scratchReg, regX, offX, "") bestRowAsm.gen_loadstore_indexed(False, scratchReg, regY, offY, "") # if this is Command-1 then we're done if store1Cmd[2] == 1: continue # if this is Command-2 then update and write if store1Cmd[2] == 2: # we don't need to clear bits with AND mask if nybble we're writing is 15 if (store1Cmd[3] | store1Cmd[4]) != 0xff: bestRowAsm.emit_op(f"and{regName[scratchReg]}", (f"#${store1Cmd[4]:02x}"), "", 2, 2, 2) # we don't need to write nybble with OR if we're writing 0 if store1Cmd[3] != 0: bestRowAsm.emit_op(f"or{regName[scratchReg]}", (f"#${store1Cmd[3]:02x}"), "", 2, 2, 2) bestRowAsm.gen_loadstore_indexed(False, scratchReg, regX, offX, "") continue # if this is Command-3, add it to the byte write list and continue if store1Cmd[2] != 3: raise Exception("Error: command number for byte store != 3 at end of Permute6309StoreCodeGen!") writeByteList.append((offX, store1Cmd[3])) # now the store operations are all completed, so we will recurse into ourself to start the writes return self.Permute6309StoreCodeGen(bestRowAsm, layoutDict, writeByteList, 1, scratchReg) def BestResult(self, trial1Asm, trial2Asm): if trial1Asm.metrics.cycles < trial2Asm.metrics.cycles or (trial1Asm.metrics.cycles == trial2Asm.metrics.cycles and trial1Asm.metrics.bytes <= trial2Asm.metrics.bytes): return trial1Asm else: return trial2Asm def GenerateCommand2RegisterOps(self, byteCmd1, byteCmd2, rowAsm): if byteCmd1[0] == 2 and byteCmd2[0] == 2: byteSplit = False # we don't need to clear bits with AND mask if nybble we're writing is 15 if (byteCmd1[1] | byteCmd1[2]) != 0xff: rowAsm.emit_op("anda", (f"#${byteCmd1[2]:02x}"), "", 2, 2, 2) else: byteSplit = True if (byteCmd2[1] | byteCmd2[2]) != 0xff: rowAsm.emit_op("andb", (f"#${byteCmd2[2]:02x}"), "", 2, 2, 2) else: byteSplit = True if byteSplit: # we don't need to write nybble with OR if we're writing 0 if byteCmd1[1] != 0: rowAsm.emit_op("ora", (f"#${byteCmd1[1]:02x}"), "", 2, 2, 2) if byteCmd2[1] != 0: rowAsm.emit_op("orb", (f"#${byteCmd2[1]:02x}"), "", 2, 2, 2) else: wordAdd = (byteCmd1[1] << 8) + byteCmd2[1] if wordAdd != 0: rowAsm.emit_op("addd", f"#${wordAdd:04x}", "", 4, 3, 3) elif byteCmd1[0] == 2: # we don't need to clear bits with AND mask if nybble we're writing is 15 if (byteCmd1[1] | byteCmd1[2]) != 0xff: rowAsm.emit_op("anda", (f"#${byteCmd1[2]:02x}"), "", 2, 2, 2) # we don't need to write nybble with OR if we're writing 0 if byteCmd1[1] != 0: rowAsm.emit_op("ora", (f"#${byteCmd1[1]:02x}"), "", 2, 2, 2) elif byteCmd2[0] == 2: # we don't need to clear bits with AND mask if nybble we're writing is 15 if (byteCmd2[1] | byteCmd2[2]) != 0xff: rowAsm.emit_op("andb", (f"#${byteCmd2[2]:02x}"), "", 2, 2, 2) # we don't need to write nybble with OR if we're writing 0 if byteCmd2[1] != 0: rowAsm.emit_op("orb", (f"#${byteCmd2[1]:02x}"), "", 2, 2, 2) def Permute6309WriteLayouts(self, startAsm, layoutDict, writeByteList, writeSize): writeListLen = len(writeByteList) if writeListLen == 0: layoutDict = { 1:copy.copy(layoutDict[1]), 2:copy.copy(layoutDict[2]), 4:copy.copy(layoutDict[4]) } # this is a leaf node, so we need to emit code to perform Write operations according to the order in our layout for write4Bytes in layoutDict[4]: startAsm.gen_loadimm_accum(regQ, write4Bytes[1], "") startAsm.gen_loadstore_indexed(False, regQ, regX, write4Bytes[0], "") # stq off,x # handle WORD write operations with D, W registers and BYTE write operations with A,B registers for write2Bytes in layoutDict[2][:]: if write2Bytes[1] == startAsm.reg.GetValue(regD): layoutDict[2].remove(write2Bytes) startAsm.gen_loadstore_indexed(False, regD, regX, write2Bytes[0], "") # std off,x elif write2Bytes[1] == startAsm.reg.GetValue(regW): layoutDict[2].remove(write2Bytes) startAsm.gen_loadstore_indexed(False, regW, regX, write2Bytes[0], "") # stw off,x for write1Bytes in layoutDict[1][:]: if write1Bytes[1] == startAsm.reg.GetValue(regA): layoutDict[1].remove(write1Bytes) startAsm.gen_loadstore_indexed(False, regA, regX, write1Bytes[0], "") # sta off,x elif write1Bytes[1] == startAsm.reg.GetValue(regB): layoutDict[1].remove(write1Bytes) startAsm.gen_loadstore_indexed(False, regB, regX, write1Bytes[0], "") # stb off,x for write2Bytes in layoutDict[2]: startAsm.gen_loadimm_accum(regD, write2Bytes[1], "") startAsm.gen_loadstore_indexed(False, regD, regX, write2Bytes[0], "") # std off,x # handle BYTE write operations with A/B registers for write1Bytes in layoutDict[1][:]: if write1Bytes[1] == startAsm.reg.GetValue(regA): layoutDict[1].remove(write1Bytes) startAsm.gen_loadstore_indexed(False, regA, regX, write1Bytes[0], "") # sta off,x elif write1Bytes[1] == startAsm.reg.GetValue(regB): layoutDict[1].remove(write1Bytes) startAsm.gen_loadstore_indexed(False, regB, regX, write1Bytes[0], "") # stb off,x for write1Bytes in layoutDict[1]: startAsm.gen_loadimm_accum(regA, write1Bytes[1], "") startAsm.gen_loadstore_indexed(False, regA, regX, write1Bytes[0], "") # sta off,x return startAsm if writeSize == 4: # search for DWORDs that we can write bFoundCandidates = False for writeIdx in range(writeListLen-3): # are these bytes consecutive? if writeByteList[writeIdx+1][0] != writeByteList[writeIdx][0] + 1: continue if writeByteList[writeIdx+2][0] != writeByteList[writeIdx][0] + 2: continue if writeByteList[writeIdx+3][0] != writeByteList[writeIdx][0] + 3: continue # this position is a candidate for Write operation with 32-bit Q accumulator DWordValue = (writeByteList[writeIdx][1] << 24) + (writeByteList[writeIdx+1][1] << 16) + (writeByteList[writeIdx+2][1] << 8) + writeByteList[writeIdx+3][1] layoutDict[4].append((writeByteList[writeIdx][0], DWordValue)) newWriteByteList = writeByteList[:writeIdx] + writeByteList[writeIdx+4:] # recurse down into this case trialAsm = self.Permute6309WriteLayouts(copy.deepcopy(startAsm), layoutDict, newWriteByteList, 4) # revert our layoutDict changes del layoutDict[4][-1] # update our bestAsm case if not bFoundCandidates: bestAsm = trialAsm # since this is our first candidate, it is currently the best bFoundCandidates = True else: bestAsm = self.BestResult(bestAsm, trialAsm) continue # if we found any DWORD candidates during this function search, then just return the best result if bFoundCandidates: return bestAsm # otherwise our search continues with WORDs writeSize = 2 if writeSize == 2: # search for WORDs that we can write bFoundCandidates = False for writeIdx in range(writeListLen-1): # are these bytes consecutive? if writeByteList[writeIdx+1][0] != writeByteList[writeIdx][0] + 1: continue # this position is a candidate for Write operation with 16-bit D accumulator WordValue = (writeByteList[writeIdx][1] << 8) + writeByteList[writeIdx+1][1] layoutDict[2].append((writeByteList[writeIdx][0], WordValue)) newWriteByteList = writeByteList[:writeIdx] + writeByteList[writeIdx+2:] # recurse down into this case trialAsm = self.Permute6309WriteLayouts(copy.deepcopy(startAsm), layoutDict, newWriteByteList, 2) # revert our layoutDict changes del layoutDict[2][-1] # update our bestAsm case if not bFoundCandidates: bestAsm = trialAsm # since this is our first candidate, it is currently the best bFoundCandidates = True else: bestAsm = self.BestResult(bestAsm, trialAsm) continue # if we found any WORD candidates during this function search, then just return the best result if bFoundCandidates: return bestAsm # otherwise our search continues with BYTEs writeSize = 1 # write size must be 1 if writeSize != 1: raise Exception("Error: writeSize != 1 at end of Permute6309WriteLayouts!") # if there are no bytes to write, recurse into ourselves to emit the Write operation code if len(writeByteList) == 0: return self.Permute6309WriteLayouts(startAsm, layoutDict, writeByteList, 1) # try out all possible permutations of byte writes bFoundCandidates = False for writeIdx in range(writeListLen): writeByteCmd = writeByteList[writeIdx] layoutDict[1].append(writeByteCmd) newWriteByteList = writeByteList[:writeIdx] + writeByteList[writeIdx+1:] # recurse down into this case trialAsm = self.Permute6309WriteLayouts(copy.deepcopy(startAsm), layoutDict, newWriteByteList, 1) # revert our layoutDict changes del layoutDict[1][-1] # update our bestAsm case if not bFoundCandidates: bestAsm = trialAsm # since this is our first candidate, it is currently the best bFoundCandidates = True else: bestAsm = self.BestResult(bestAsm, trialAsm) continue # we are completely done; return the best result return bestAsm # ************************************************************************************************* # Sprite class: Draw function row generation for 6809 # ************************************************************************************************* def RowDraw6809(self, y, regState, byteStrips): # iterate through all permutations of byte/word layouts for strips to find fastest one layoutList = [ ] return self.PermuteByteStripLayouts(y, regState, layoutList, byteStrips) def PermuteByteStripLayouts(self, rowNum, regState, layoutList, remainingCmdStrips): # if we are at a leaf, then we have a complete row layout to turn into assembly code if len(remainingCmdStrips) == 0: rowAsm = self.GenRowCode(rowNum, copy.deepcopy(regState), layoutList) return rowAsm # otherwise we have more pixel strips to permute, so we will recurse activeByteCmdStrip = remainingCmdStrips[0] nextByteCmdStrips = remainingCmdStrips[1:] # if there is only one layout choice, then recurse into trivial case bytesInStrip = len(activeByteCmdStrip[1]) if bytesInStrip == 1 or (bytesInStrip & 1) == 0: layoutList.append((activeByteCmdStrip, None)) bestAsm = self.PermuteByteStripLayouts(rowNum, regState, layoutList, nextByteCmdStrips) layoutList.pop() return bestAsm # otherwise, iterate through each possible position for single byte in odd-length strip bestAsm = None for idx in range(0, bytesInStrip, 2): singleByteOffX = activeByteCmdStrip[0] + idx layoutList.append((activeByteCmdStrip, singleByteOffX)) trialAsm = self.PermuteByteStripLayouts(rowNum, regState, layoutList, nextByteCmdStrips) layoutList.pop() if bestAsm == None or trialAsm.metrics.cycles < bestAsm.metrics.cycles: #if bestAsm != None: # fixme debug # print("%s: row layout with (cyc=%i,bytes=%i) is better than (cyc=%i,bytes=%i)" % (self.name, trialAsm.metrics.cycles, trialAsm.metrics.bytes, bestAsm.metrics.cycles, bestAsm.metrics.bytes)) bestAsm = trialAsm # return the best one return bestAsm def GenRowCode(self, rowNum, regState, layoutList): rowAsm = AsmStream(None, regState) # generate cmdBytesToStore and cmdWordsToStore lists cmdBytesToStore = [] cmdWordsToStore = [] cmdBytesToWrite = [] cmdWordsToWrite = [] offY = self.YPtrOffNew for ((stripOffX,stripByteCmds), singleByteOffX) in layoutList: numByteCmds = len(stripByteCmds) offX = stripOffX while offX < stripOffX+numByteCmds: numLeft = stripOffX+numByteCmds - offX # do we process a single byte? if numLeft == 1 or singleByteOffX == offX: byteCmd = stripByteCmds[offX-stripOffX] cmdBytesToStore.append((offX, offY, byteCmd)) offX += 1 offY += 1 continue # otherwise we must process a word byteCmd1 = stripByteCmds[offX-stripOffX] byteCmd2 = stripByteCmds[offX-stripOffX+1] cmdWordsToStore.append((offX, offY, byteCmd1, byteCmd2)) offX += 2 offY += 2 # use U to store all words with no command2 bytes idx = 0 while idx < len(cmdWordsToStore): (offX,offY,byteCmd1,byteCmd2) = cmdWordsToStore[idx] if byteCmd1[0] != 2 and byteCmd2[0] != 2: # emit code to store the background word with U rowAsm.gen_loadstore_indexed(True, regU, regX, offX + 256*self.lineAdvance, "") # ldu off,x rowAsm.gen_loadstore_indexed(False, regU, regY, offY, "") # move this command word into the cmdWordsToWrite or cmdBytesToWrite list if byteCmd1[0] == 1: cmdWordsToStore.pop(idx) cmdBytesToWrite.append((offX+1,offY+1,byteCmd2)) elif byteCmd2[0] == 1: cmdWordsToStore.pop(idx) cmdBytesToWrite.append((offX,offY,byteCmd1)) else: cmdWordsToWrite.append(cmdWordsToStore.pop(idx)) continue # we can't save this word right now, so skip it idx += 1 # write all of the pure Command3 words which match D (if it's valid) if rowAsm.reg.IsValid(regD): idx = 0 while idx < len(cmdWordsToWrite): (offX,offY,byteCmd1,byteCmd2) = cmdWordsToWrite[idx] if byteCmd1[1] == rowAsm.reg.GetValue(regA) and byteCmd2[1] == rowAsm.reg.GetValue(regB): rowAsm.gen_loadstore_indexed(False, regD, regX, offX + 256*self.lineAdvance, "") # std off,x cmdWordsToWrite.pop(idx) continue idx += 1 # decide which byte register (A or B) to use as scratch. # preserve either register (A or B) which matches a Command-3 byte if not rowAsm.reg.IsValid(regA): scratchReg = regA elif not rowAsm.reg.IsValid(regB): scratchReg = regB else: scratchReg = regB for (offX,offY,(byteCmdNum,byteCmdVal,byteCmdMask)) in cmdBytesToStore + cmdBytesToWrite: if byteCmdNum != 3: continue if byteCmdVal == rowAsm.reg.GetValue(regB): scratchReg = regA # use the scratch register to store all bytes, and write all Command2 bytes while len(cmdBytesToStore) > 0: (offX,offY,byteCmd) = cmdBytesToStore.pop(0) rowAsm.gen_loadstore_indexed(True, scratchReg, regX, offX + 256*self.lineAdvance, "") rowAsm.gen_loadstore_indexed(False, scratchReg, regY, offY, "") if byteCmd[0] == 2: # we don't need to clear bits with AND mask if nybble we're writing is 15 if (byteCmd[1] | byteCmd[2]) != 0xff: rowAsm.emit_op(f"and{regName[scratchReg]}", (f"#${byteCmd[2]:02x}"), "", 2, 2, 2) if byteCmd[1] != 0: rowAsm.emit_op(f"or{regName[scratchReg]}", (f"#${byteCmd[1]:02x}"), "", 2, 2, 2) rowAsm.gen_loadstore_indexed(False, scratchReg, regX, offX + 256*self.lineAdvance, "") elif byteCmd[0] == 3: cmdBytesToWrite.append((offX,offY,byteCmd)) # write all Command3 bytes which match a valid register idx = 0 while idx < len(cmdBytesToWrite): (offX,offY,byteCmd) = cmdBytesToWrite[idx] if byteCmd[0] != 3: raise Exception(f"Error: byte command {int(byteCmd[0])} is in the cmdBytesToWrite list!") if rowAsm.reg.IsValid(regA) and byteCmd[1] == rowAsm.reg.GetValue(regA): rowAsm.gen_loadstore_indexed(False, regA, regX, offX + 256*self.lineAdvance, "") # sta off,x cmdBytesToWrite.pop(idx) continue elif rowAsm.reg.IsValid(regB) and byteCmd[1] == rowAsm.reg.GetValue(regB): rowAsm.gen_loadstore_indexed(False, regB, regX, offX + 256*self.lineAdvance, "") # stb off,x cmdBytesToWrite.pop(idx) continue idx += 1 # fixme (micro-op): if there is a word in cmdWordsToStore which contains a command-3 byte which # is part of the future lonelyBytes list, then put that word at end of list # use D (trashing it) to Store and Write the remaining unstored words (which all must have a Command2 byte) while len(cmdWordsToStore) > 0: (offX,offY,byteCmd1,byteCmd2) = cmdWordsToStore.pop(0) rowAsm.gen_loadstore_indexed(True, regD, regX, offX + 256*self.lineAdvance, "") # ldd off,x rowAsm.gen_loadstore_indexed(False, regD, regY, offY, "") if byteCmd1[0] == 2 and byteCmd2[0] == 2: byteSplit = False # we don't need to clear bits with AND mask if nybble we're writing is 15 if (byteCmd1[1] | byteCmd1[2]) != 0xff: rowAsm.emit_op("anda", (f"#${byteCmd1[2]:02x}"), "", 2, 2, 2) else: byteSplit = True if (byteCmd2[1] | byteCmd2[2]) != 0xff: rowAsm.emit_op("andb", (f"#${byteCmd2[2]:02x}"), "", 2, 2, 2) else: byteSplit = True if byteSplit: # we don't need to write nybble with OR if we're writing 0 if byteCmd1[1] != 0: rowAsm.emit_op("ora", (f"#${byteCmd1[1]:02x}"), "", 2, 2, 2) if byteCmd2[1] != 0: rowAsm.emit_op("orb", (f"#${byteCmd2[1]:02x}"), "", 2, 2, 2) else: wordAdd = (byteCmd1[1] << 8) + byteCmd2[1] if wordAdd != 0: rowAsm.emit_op("addd", f"#${wordAdd:04x}", "", 4, 3, 3) rowAsm.gen_loadstore_indexed(False, regD, regX, offX + 256*self.lineAdvance, "") # std off,x elif byteCmd1[0] == 2: # we don't need to clear bits with AND mask if nybble we're writing is 15 if (byteCmd1[1] | byteCmd1[2]) != 0xff: rowAsm.emit_op("anda", (f"#${byteCmd1[2]:02x}"), "", 2, 2, 2) # we don't need to write nybble with OR if we're writing 0 if byteCmd1[1] != 0: rowAsm.emit_op("ora", (f"#${byteCmd1[1]:02x}"), "", 2, 2, 2) if byteCmd2[0] == 1: rowAsm.gen_loadstore_indexed(False, regA, regX, offX + 256*self.lineAdvance, "") # sta off,x else: # assert: byteCmd2[0] == 3 rowAsm.gen_loadimm_accum(regB, byteCmd2[1], "") rowAsm.gen_loadstore_indexed(False, regD, regX, offX + 256* self.lineAdvance, "") # std off,x elif byteCmd2[0] == 2: # we don't need to clear bits with AND mask if nybble we're writing is 15 if (byteCmd2[1] | byteCmd2[2]) != 0xff: rowAsm.emit_op("andb", (f"#${byteCmd2[2]:02x}"), "", 2, 2, 2) # we don't need to write nybble with OR if we're writing 0 if byteCmd2[1] != 0: rowAsm.emit_op("orb", (f"#${byteCmd2[1]:02x}"), "", 2, 2, 2) if byteCmd1[0] == 1: rowAsm.gen_loadstore_indexed(False, regB, regX, offX+1 + 256*self.lineAdvance, "") # stb off,x else: # assert: byteCmd1[0] == 3 rowAsm.gen_loadimm_accum(regA, byteCmd1[1], "") rowAsm.gen_loadstore_indexed(False, regD, regX, offX + 256*self.lineAdvance, "") # std off,x else: raise Exception("Error: word in cmdWordsToStore contains no Command-2 bytes!") # assert that only command3 bytes/words are remaining to be written, and all bytes/words have been stored if len(cmdBytesToStore) > 0 or len(cmdWordsToStore) > 0: raise Exception("internal error: unstored bytes/words remaining!"); for (offX,offY,byteCmd) in cmdBytesToWrite: if byteCmd[0] != 3: raise Exception(f"internal error: command-{int(byteCmd[0])} byte to write") for (offX,offY,byteCmd1,byteCmd2) in cmdWordsToWrite: if byteCmd1[0] != 3 or byteCmd2[0] != 3: raise Exception(f"internal error: command-({int(byteCmd1[0])},{int(byteCmd2[0])}) bytes to write in word") # emit byte writes for any bytes which match our current register values if rowAsm.reg.IsValid(regA) or rowAsm.reg.IsValid(regB): idx = 0 while idx < len(cmdBytesToWrite): (offX,offY,byteCmd) = cmdBytesToWrite[idx] matchReg = None if rowAsm.reg.IsValid(regA) and byteCmd[1] == rowAsm.reg.GetValue(regA): matchReg = regA elif rowAsm.reg.IsValid(regB) and byteCmd[1] == rowAsm.reg.GetValue(regB): matchReg = regB if matchReg == None: idx += 1 continue # we found a match. Write 2 pixels rowAsm.gen_loadstore_indexed(False, matchReg, regX, offX + 256*self.lineAdvance, "") # pop this byte command out of the Write list, and reiterate with the same idx to get the next byte command cmdBytesToWrite.pop(idx) # make lists of unique byte and word values to write uniqByteValues = [] uniqWordValues = [] for (offX,offY,byteCmd) in cmdBytesToWrite: val = byteCmd[1] if val not in uniqByteValues: uniqByteValues.append(val) for (offX,offY,byteCmd1,byteCmd2) in cmdWordsToWrite: val = (byteCmd1[1] << 8) + byteCmd2[1] if val not in uniqWordValues: uniqWordValues.append(val) # permute across all orderings of word writes to minimize number of loads score,wordOrder = self.PermuteWordWriteOrder(rowNum, rowAsm.reg, uniqWordValues, [ ]) # we need a scratch register to use while writing bytes that don't match words # choose one which doesn't destroy a useful register for the first word if not rowAsm.reg.IsValid(regA): scratchReg = regA elif not rowAsm.reg.IsValid(regB): scratchReg = regB else: scratchReg = regB if len(wordOrder) > 0 and (wordOrder[0] & 0xff) == rowAsm.reg.GetValue(regB): scratchReg = regA # make list of byte values to write which don't match any bytes in any word lonelyByteVals = [ ] for (offX,offY,byteCmd) in cmdBytesToWrite: matchWord = False for (offXW, offYW, byteCmd1W, byteCmd2W) in cmdWordsToWrite: if byteCmd[1] == byteCmd1W[1] or byteCmd[1] == byteCmd2W[1]: matchWord = True # if this byte doesn't match any words to write, add it to lonely byte list if not matchWord and byteCmd[1] not in lonelyByteVals: lonelyByteVals.append(byteCmd[1]) # fixme (micro-op): if len(cmdWordsToWrite) == 0, then search in lonelyByteVals for a byte # which may be written in the following row. If found, put at end of lonelyByteVals list # emit byte writes for those bytes which don't match any bytes in any word for lonelyVal in lonelyByteVals: rowAsm.gen_loadimm_accum(scratchReg, lonelyVal, "") idx = 0 while idx < len(cmdBytesToWrite): (offX,offY,byteCmd) = cmdBytesToWrite[idx] # if this byte to write doesn't match the lonely byte value we're writing, then skip it if byteCmd[1] != lonelyVal: idx += 1 continue # otherwise we will emit this byte write now rowAsm.gen_loadstore_indexed(False, scratchReg, regX, offX + 256*self.lineAdvance, "") # pop this byte command out of the Write list, and reiterate with the same idx to get the next byte command cmdBytesToWrite.pop(idx) # emit all words write (and matching byte writes) while len(cmdWordsToWrite) > 0: # pop command and write the word (offXW, offYW, byteCmd1W, byteCmd2W) = cmdWordsToWrite.pop(0) loadA = (not rowAsm.reg.IsValid(regA)) or (byteCmd1W[1] != rowAsm.reg.GetValue(regA)) loadB = (not rowAsm.reg.IsValid(regB)) or (byteCmd2W[1] != rowAsm.reg.GetValue(regB)) if loadA and loadB: wordVal = (byteCmd1W[1] << 8) + byteCmd2W[1] rowAsm.gen_loadimm_accum(regD, wordVal, "") elif loadA: rowAsm.gen_loadimm_accum(regA, byteCmd1W[1], "") elif loadB: rowAsm.gen_loadimm_accum(regB, byteCmd2W[1], "") rowAsm.gen_loadstore_indexed(False, regD, regX, offXW + 256*self.lineAdvance, "") # std off,x # write any matching bytes idx = 0 while idx < len(cmdBytesToWrite): (offX,offY,byteCmd) = cmdBytesToWrite[idx] matchReg = None if byteCmd[1] == rowAsm.reg.GetValue(regA): matchReg = regA elif byteCmd[1] == rowAsm.reg.GetValue(regB): matchReg = regB if matchReg == None: idx += 1 continue # we found a match. Write 2 pixels rowAsm.gen_loadstore_indexed(False, matchReg, regX, offX + 256*self.lineAdvance, "") # pop this byte command out of the Write list, and reiterate with the same idx to get the next byte command cmdBytesToWrite.pop(idx) # assert that there is nothing left to do if len(cmdBytesToWrite) != 0: raise Exception("Error: remaining cmdBytesToWrite after everything is done!") if len(cmdWordsToWrite) != 0: raise Exception("Error: remaining cmdWordsToWrite after everything is done!") # return the generated assembly language code return rowAsm def PermuteWordWriteOrder(self, rowNum, regState, uniqWordValues, wordOrder): # if we are at a leaf, calculate the score and return if len(uniqWordValues) == 0: score = 0.0 if len(wordOrder) > 0: # give 1 point if a byte in the first word matches up with an existing regA/regB known value firstWordWriteVal = wordOrder[0] if regState.IsValid(regA) and regState.GetValue(regA) == (firstWordWriteVal >> 8): score += 1.0 elif regState.IsValid(regB) and regState.GetValue(regB) == (firstWordWriteVal & 0xff): score += 1.0 # increase the score by the probability that this word (or a sub-byte) will be useful to the next row lastVal = wordOrder[-1] if rowNum < self.height - 1: wordWriteProb = self.wordWriteProbByRow[rowNum+1] byteWriteProb = self.byteWriteProbByRow[rowNum+1] if lastVal in wordWriteProb: score += wordWriteProb[lastVal] byteProb = 0.0 if (lastVal >> 8) in byteWriteProb: byteProb = byteWriteProb[lastVal >> 8] if (lastVal & 0xff) in byteWriteProb: byteProb = max(byteProb, byteWriteProb[lastVal & 0xff]) score += byteProb # give 1 point for each byte load that we can avoid when advancing to next word to write for idx in range(len(wordOrder)-1): if (wordOrder[idx] & 0xff00) == (wordOrder[idx+1] & 0xff00): score += 1.0 if (wordOrder[idx] & 0xff) == (wordOrder[idx+1] & 0xff): score += 1.0 return (score,copy.copy(wordOrder)) # otherwise, try all possible orderings and keep track of the one with the best score bestScore = 0 bestOrder = None for idx in range(len(uniqWordValues)): nextWord = uniqWordValues.pop(idx) wordOrder.append(nextWord) tryScore,tryOrder = self.PermuteWordWriteOrder(rowNum, regState, uniqWordValues, wordOrder) if bestOrder == None or tryScore > bestScore: bestScore = tryScore bestOrder = tryOrder wordOrder.pop() uniqWordValues.insert(idx, nextWord) return (bestScore, bestOrder) # ************************************************************************************************* # Application object: high-level processing, statistics gathering, final assembly dump # ************************************************************************************************* class App: def __init__(self, spriteFilename, asmFilename): self.spriteFilename = spriteFilename self.asmFilename = asmFilename self.spriteList = [] self.groupNumber = None def ReadInput(self): curSprite = None spritetext = open(self.spriteFilename).read() for line in spritetext.split("\n"): # remove comments and whitespace from line pivot = line.find("*") if pivot != -1: line = line[:pivot] line = line.strip() if len(line) < 1: continue if line[0] == '[' and line[-1] == ']': # new sprite definiton if curSprite != None: curSprite.FinishDefinition() newSpriteName = line[1:-1] curSprite = Sprite(newSpriteName) self.spriteList.append(curSprite) continue if curSprite == None: pivot = line.find('=') if pivot != -1: key = line[0:pivot].strip().lower() value = line[pivot+1:].strip() if key == "group": self.groupNumber = int(value) continue print(f"Warning: ignore line before sprite section: {line}") continue curSprite.ReadInputLine(line) if curSprite != None: curSprite.FinishDefinition() def PrintRow(self, RowName, Values, datatype): if len(RowName) < 16: RowName += " " * (16 - len(RowName)) else: RowName = RowName[:16] print(RowName, end=' ') for val in Values: if val == None: s = "" elif datatype == str: s = val elif datatype == int: s = str(val) elif datatype == float: s = f"{val:.2f}" else: raise Exception("Invalid data type") if len(s) >= 8: print(s[:8], end=' ') else: print(" " * (8 - len(s)) + s, end=' ') print() def Calculate(self): for sprite in self.spriteList: sprite.Process1_PreCalc() sprite.Process2_GenErase() sprite.Process3_GenDraw(0) if sprite.hasSinglePixelPos: sprite.Process3_GenDraw(1) # calculate and print statistics for each sprite Names = [] Pixels = [] Storage = [] MaxCycles = [] CyclesPerPix = [] EraseBytes = [] EraseCycles = [] DrawLBytes = [] DrawLCycles = [] DrawRBytes = [] DrawRCycles = [] TotalErase = 0 TotalDrawL = 0 TotalDrawR = 0 # add data to lists for sprite in self.spriteList: name = sprite.name # skip blank sprites if sprite.numPixels == 0: continue Names.append(name) Pixels.append(sprite.numPixels) Storage.append(sprite.numSavedBytes) EraseBytes.append(sprite.funcErase.metrics.bytes) EraseCycles.append(sprite.funcErase.metrics.cycles) DrawLBytes.append(sprite.funcDraw[0].metrics.bytes) DrawLCycles.append(sprite.funcDraw[0].metrics.cycles) if sprite.hasSinglePixelPos: DrawRBytes.append(sprite.funcDraw[1].metrics.bytes) DrawRCycles.append(sprite.funcDraw[1].metrics.cycles) MaxDrawCycles = max(sprite.funcDraw[0].metrics.cycles, sprite.funcDraw[1].metrics.cycles) else: DrawRBytes.append(None) DrawRCycles.append(None) MaxDrawCycles = sprite.funcDraw[0].metrics.cycles myMaxCycles = MaxDrawCycles + sprite.funcErase.metrics.cycles MaxCycles.append(myMaxCycles) CyclesPerPix.append(float(myMaxCycles) / float(sprite.numPixels)) TotalErase += sprite.funcErase.metrics.bytes TotalDrawL += sprite.funcDraw[0].metrics.bytes if sprite.hasSinglePixelPos: TotalDrawR += sprite.funcDraw[1].metrics.bytes # print summary numSprites = len(self.spriteList) print(f"Total number of sprites: {int(numSprites)}") print(f"Total Erase code bytes: {int(TotalErase)}") print(f"Total Draw Left code bytes: {int(TotalDrawL)}") print(f"Total Draw Right code bytes: {int(TotalDrawR)}") print() # last column should be averages Names.append("Average") Pixels.append(sum(Pixels) / numSprites) Storage.append(sum(Storage) / numSprites) MaxCycles.append(sum(MaxCycles) / numSprites) CyclesPerPix.append(sum(CyclesPerPix) / float(numSprites)) EraseBytes.append(sum(EraseBytes) / numSprites) EraseCycles.append(sum(EraseCycles) / numSprites) DrawLBytes.append(sum(DrawLBytes) / numSprites) DrawLCycles.append(sum(DrawLCycles) / numSprites) ValidDrawRBytes = [val for val in DrawRBytes if val is not None] ValidDrawRCycles = [val for val in DrawRCycles if val is not None] if len(ValidDrawRBytes) > 0: DrawRBytes.append(sum(ValidDrawRBytes) / len(ValidDrawRBytes)) DrawRCycles.append(sum(ValidDrawRCycles) / len(ValidDrawRCycles)) # print tables numCols = len(Names) for startIdx in range(0, numCols, 8): endIdx = min(startIdx+8, numCols); self.PrintRow("Sprite Name", Names[startIdx:endIdx], str) self.PrintRow("Pixels", Pixels[startIdx:endIdx], int) self.PrintRow("Storage Bytes", Storage[startIdx:endIdx], int) self.PrintRow("Max Cycles", MaxCycles[startIdx:endIdx], int) self.PrintRow("Cycles/pixel", CyclesPerPix[startIdx:endIdx], float) print("**************Erase:") self.PrintRow("Code bytes", EraseBytes[startIdx:endIdx], int) self.PrintRow("Clock cycles", EraseCycles[startIdx:endIdx], int) print("**********Draw_Left:") self.PrintRow("Code bytes", DrawLBytes[startIdx:endIdx], int) self.PrintRow("Clock cycles", DrawLCycles[startIdx:endIdx], int) if len(ValidDrawRBytes) > 0: print("*********Draw_Right:") self.PrintRow("Code bytes", DrawRBytes[startIdx:endIdx], int) self.PrintRow("Clock cycles", DrawRCycles[startIdx:endIdx], int) print() def WriteAsm(self): # make sure we have a group number if self.groupNumber == None: raise Exception(f"No group number was given in input file {self.spriteFilename}") # open output file for writing f = open(self.asmFilename, "w") origin = 0 # dump out the draw/erase routines bHasDrawRight = False for sprite in self.spriteList: # drawLeft length = sprite.funcDraw[0].metrics.bytes f.write(f"* (Origin: ${origin:04X} Length: {int(length)} bytes)\n") f.write(sprite.funcDraw[0].text + "\n") origin += length # drawRight if sprite.hasSinglePixelPos: length = sprite.funcDraw[1].metrics.bytes f.write(f"* (Origin: ${origin:04X} Length: {int(length)} bytes)\n") f.write(sprite.funcDraw[1].text + "\n") origin += length bHasDrawRight = True # erase length = sprite.funcErase.metrics.bytes f.write(f"* (Origin: ${origin:04X} Length: {int(length)} bytes)\n") f.write(sprite.funcErase.text + "\n") origin += length # at the end, write the Sprite Descriptor Table f.write(f"\nNumberOfSprites\n fcb {int(len(self.spriteList))}\n") f.write("SpriteDescriptorTable\n") for sprite in self.spriteList: f.write(f" * {sprite.name}\n") p = str(sprite.width) f.write(f" fcb {p}{' ' * (24 - len(p))}* width\n") p = str(sprite.height) f.write(f" fcb {p}{' ' * (24 - len(p))}* height\n") p = str((sprite.originXcode - sprite.originXsprite)//2) f.write(f" fcb {p}{' ' * (24 - len(p))}* offsetX\n") p = str(-sprite.hotspot[1]) f.write(f" fcb {p}{' ' * (24 - len(p))}* offsetY\n") f.write(" fcb 0 * cpLeft\n") f.write(" fcb 0 * cpRight\n") f.write(" fcb 0 * cpErase\n") p = str(sprite.numSavedBytes) f.write(f" fdb {p}{' ' * (24 - len(p))}* storeBytes\n") p = str(sprite.funcDraw[0].metrics.bytes) f.write(f" fdb {p}{' ' * (24 - len(p))}* length of drawLeft in bytes\n") if sprite.hasSinglePixelPos: p = str(sprite.funcDraw[1].metrics.bytes) f.write(f" fdb {p}{' ' * (24 - len(p))}* length of drawRight in bytes\n") else: f.write(" fdb 0 * length of drawRight in bytes\n") p = str(sprite.funcErase.metrics.bytes) f.write(f" fdb {p}{' ' * (24 - len(p))}* length of erase in bytes\n") f.write(" fcb 0 * res1\n") # ************************************************************************************************* # main function for standard script execution # ************************************************************************************************* if __name__ == "__main__": if len(sys.argv) != 4: print(f"Usage: {sys.argv[0]} <InputSpriteFile> <OutputAsmFile> <6809 | 6309>") sys.exit(1) # set CPU type global CPU CPU = int(sys.argv[3]) # run the app myApp = App(sys.argv[1], sys.argv[2]) myApp.ReadInput() myApp.Calculate() myApp.WriteAsm()
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models, migrations class Migration(migrations.Migration): dependencies = [ ] operations = [ migrations.CreateModel( name='Attendance', fields=[ ('id', models.AutoField(serialize=False, verbose_name=b'id', primary_key=True)), ('user_pic_url', models.TextField()), ('name', models.TextField()), ('people', models.IntegerField()), ('create_date', models.DateField()), ], options={ 'db_table': 'attendance', }, ), migrations.CreateModel( name='Rsvp', fields=[ ('id', models.AutoField(serialize=False, verbose_name=b'id', primary_key=True)), ('message', models.TextField()), ('deadline', models.DateField()), ], options={ 'db_table': 'rsvp', }, ), migrations.CreateModel( name='UserRsvp', fields=[ ('id', models.AutoField(serialize=False, verbose_name=b'id', primary_key=True)), ('user', models.IntegerField()), ('rsvp', models.ForeignKey(to='rsvp.Rsvp')), ], options={ 'db_table': 'user_rsvp', }, ), migrations.AddField( model_name='attendance', name='rsvp', field=models.ForeignKey(to='rsvp.Rsvp'), ), ]
import copy import random from datetime import datetime import pytz from fedlearner.common import metrics, common from fedlearner.data_join.common import convert_to_str from fedlearner.common.common import convert_to_datetime class MetricStats: def __init__(self, raw_data_options, metric_tags): self._tags = copy.deepcopy(metric_tags) self._stat_fields = raw_data_options.optional_fields self._sample_ratio = common.Config.RAW_DATA_METRICS_SAMPLE_RATE def emit_metric(self, item): if random.random() < self._sample_ratio: tags = copy.deepcopy(self._tags) for field in self._stat_fields: value = convert_to_str(getattr(item, field, '#None#')) tags[field] = value tags['example_id'] = convert_to_str(item.example_id) tags['event_time'] = convert_to_datetime(item.event_time, True) \ .isoformat(timespec='microseconds') tags['process_time'] = datetime.now(tz=pytz.utc) \ .isoformat(timespec='microseconds') metrics.emit_store(name='input_data', value=0, tags=tags, index_type='raw_data')
# initial helloworld attempt! import argparse # main def main(): parser = argparse.ArgumentParser() parser.add_argument('filearg', help='XML file') parser.add_argument('-macro', '-m', nargs='+', dest='macarg', help='macro string arg') args = parser.parse_args() print(args) #args_init() ''' Defines all options in tool --seg, -s : segment, only one segment can be defined per argument (--seg=SEGMENT_KEY, -s SEGMENT_KEY) --txt, -t : text, multiple text entries per argument, at least one key required (--txt TEXT_KEY [TEXT_KEY [...]]) --lst, -l : list, key required, --entry subargument optional. If entry key(s) not specified, all entries will be printed. --entry, -e : entry (--lst LIST_KEY [--entry ENTRY_KEY [ENTRY_KEY [...]]) ''' def args_init(): # create top level parser parser = argparse.ArgumentParser(prog='txtgen', description='text preset generator/assembler') parser.add_argument('--store', '-s', dest='segargs', action='append', nargs='+') subparsers = parser.add_subparsers(title='', description='', help='sub-command help') # create text element parser textarg = subparsers.add_parser('text', aliases=['txt', 't'], help='text help') textarg.add_argument(action='append', nargs='+') #textarg.set_defaults(func=textfunc) # create list element parser # create list entry element parser # should parse for key value, but know if it's a seg_*, txt_*, lst_*, # entry_*key=value based on the type of command args = parser.parse_args() print(args.strargs) # entry point if __name__ == '__main__': main() """ parsing of chained args algorithm: args - array of arguments, already parsed from the command line node - current xml node to search for arguments find-key-value(args, key, value, node): value=search key-value in (node) if key-value not found: child = node.tail() search key-value in (child): else: go to next argument in args disp(value) find-key-value(args.next()) python textgen [xmlfile [seg SEG_KEY [--txt | -t [TXT_KEY]] [--lst | -l [LST_KEY] [--entry | -e [ENTRY_KEY]]] ] ] """
#!/usr/bin/env python3 import string, os, time, json, re, unicodedata, html.parser, requests # Bon appetit cafe hours api url url = "http://legacy.cafebonappetit.com/api/2/cafes" # How many cafeterias you want to parse (in order) totalCafes = 2393 # What our file should be named fileName = "data.json" # Parser to clean up the string names h = html.parser.HTMLParser() # Time when we started the script start = time.time() def clean(stringToClean): # Remove beginning and ending whitespace string = stringToClean.strip() # Replace html cleanString = h.unescape(string) # Replace unicode return unicodedata.normalize('NFKD', cleanString) # Finds the cafeteria id and name def getBonAppMenuData(url, id): response = requests.get(url, params={'cafe': id}) data = response.json() try: # Grab cafe Id from JSON cafeId = list(data["cafes"].keys())[0] # Grab cafe name from JSON cafeName = data["cafes"][cafeId]["name"] # We want to display titles at least. We can do without location. # Grab cafe city from JSON cafeCity = data["cafes"][cafeId].get("city", "") # Grab cafe state from JSON cafeState = data["cafes"][cafeId].get("state", "") # Formatting city and state strings # Both empty if cafeCity == "" and cafeState == "": cafeLoc = "No location listed" # Only city elif cafeCity != "" and cafeState == "": cafeLoc = clean(cafeCity) # Only State elif cafeCity == "" and cafeState != "": cafeLoc = clean(cafeState) # City and State else: cafeLoc = clean(cafeCity) + ", " + clean(cafeState) # Clean up the cafe name cafeName = clean(cafeName) # Construct the full return string print(cafeId + ") " + cafeName + " in " + cafeLoc) return {'id':cafeId, 'label':cafeName, 'desc':cafeLoc} except: print('[Skipping. Moving on...]') pass # Round numbers to a decimal point def num2str(num, precision): return "%0.*f" % (precision, num) # Get the outfile's size def calculateFileSize(): return str(os.path.getsize(fileName)) def main(): # Our constructed JSON data responseData = [] # Loop through the "known" amount of cafes for num in range(1, totalCafes): data = getBonAppMenuData(url, num) # Append if it's not null if data is not None: responseData.append(data) time.sleep(3) # Write our output to a file with open(fileName, 'w') as outfile: # Output the data into a file json.dump(responseData, outfile) # Save the runtime endTime = time.time() - start; print('') print('File: ' + fileName) print('Size: ' + calculateFileSize() + ' bytes') print('This took ' + num2str(endTime, 2) + ' seconds\n') if __name__ == '__main__': main()
from datetime import datetime def convert_daynum_to_friendly(day_num): friendly_string = "" lastdigit = int(str(day_num[-1])) if lastdigit == 1 and day_num != 11: friendly_string = "st" if lastdigit == 2 and day_num != 12: friendly_string = "nd" if lastdigit == 3 and day_num != 13: friendly_string = "rd" else: friendly_string = "th" return "%s%s" % (day_num, friendly_string) def get_time_now(): time_now = datetime.now().time() time_hours = time_now.strftime("%I").lstrip('0') time_minutes = time_now.strftime("%M") ampm = time_now.strftime("%p").lower() return "%s:%s %s" % (time_hours, time_minutes, ampm) def get_date_now(): date_now = datetime.now() day = date_now.strftime("%A") day_number = convert_daynum_to_friendly(date_now.strftime("%d")) month_name = date_now.strftime("%B") formatted_date = "%s %s %s" % (day, day_number, month_name) return formatted_date def get_now_zulu(): date_now = datetime.now() zulu_date = "%sZ" % date_now.strftime("%Y-%m-%d") return zulu_date def convert_zulu_to_dayname(day): dayname_from_zulu = datetime.strptime(day[:-1], "%Y-%m-%d").strftime("%a") return dayname_from_zulu
''' Converts Program model to a image (graph) ''' import pygraphviz as pgv def stmts_to_str(title, types, ss): l = [title] if types: l.append(', '.join(['%s: %s' % x for x in types])) for (v, e) in ss: ls = str(e) ls = ls.replace(r'\n', r'\\n') ls = ls.replace(r'\r', r'\\r') ls = ls.replace(r'\t', r'\\t') l.append('%s := %s' % (v, ls)) ml = max([len(x) for x in l]) l.insert(2 if types else 1, '-' * ml) return '\n'.join(l) def create_graph(pm): G = pgv.AGraph(directed=True) for name, fnc in list(pm.fncs.items()): fnclab = 'fun %s (%s) : %s --- ' % ( fnc.name, ', '.join(['%s : %s' % x for x in fnc.params]), fnc.rettype) types = list(fnc.types.items()) for loc in fnc.locs(): fnclabel = fnclab if loc == fnc.initloc else '' label = stmts_to_str('%sL%s' % (fnclabel, loc,), types, fnc.exprs(loc)) types = None G.add_node('%s-%s' % (name, loc), label=label, shape='rectangle', fontname='monospace') for loc in fnc.locs(): locs = '%s-%s' % (name, loc) loc2 = fnc.trans(loc, True) locs2 = '%s-%s' % (name, loc2) if loc2: G.add_edge(locs, locs2, label='True') loc2 = fnc.trans(loc, False) locs2 = '%s-%s' % (name, loc2) if loc2: G.add_edge(locs, locs2, label='False') G.layout('dot') return G
# http://stackoverflow.com/a/1336640 # http://creativecommons.org/licenses/by-sa/3.0/ import logging # now we patch Python code to add color support to logging.StreamHandler def add_coloring_to_emit_windows(fn): # add methods we need to the class def _out_handle(self): import ctypes return ctypes.windll.kernel32.GetStdHandle(self.STD_OUTPUT_HANDLE) out_handle = property(_out_handle) def _set_color(self, code): import ctypes # Constants from the Windows API self.STD_OUTPUT_HANDLE = -11 hdl = ctypes.windll.kernel32.GetStdHandle(self.STD_OUTPUT_HANDLE) ctypes.windll.kernel32.SetConsoleTextAttribute(hdl, code) setattr(logging.StreamHandler, '_set_color', _set_color) def new(*args): FOREGROUND_BLUE = 0x0001 # text color contains blue. FOREGROUND_GREEN = 0x0002 # text color contains green. FOREGROUND_RED = 0x0004 # text color contains red. FOREGROUND_INTENSITY = 0x0008 # text color is intensified. FOREGROUND_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED # winbase.h STD_INPUT_HANDLE = -10 STD_OUTPUT_HANDLE = -11 STD_ERROR_HANDLE = -12 # wincon.h FOREGROUND_BLACK = 0x0000 FOREGROUND_BLUE = 0x0001 FOREGROUND_GREEN = 0x0002 FOREGROUND_CYAN = 0x0003 FOREGROUND_RED = 0x0004 FOREGROUND_MAGENTA = 0x0005 FOREGROUND_YELLOW = 0x0006 FOREGROUND_GREY = 0x0007 FOREGROUND_INTENSITY = 0x0008 # foreground color is intensified. BACKGROUND_BLACK = 0x0000 BACKGROUND_BLUE = 0x0010 BACKGROUND_GREEN = 0x0020 BACKGROUND_CYAN = 0x0030 BACKGROUND_RED = 0x0040 BACKGROUND_MAGENTA = 0x0050 BACKGROUND_YELLOW = 0x0060 BACKGROUND_GREY = 0x0070 BACKGROUND_INTENSITY = 0x0080 # background color is intensified. levelno = args[1].levelno if (levelno >= 50): color = BACKGROUND_YELLOW | FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY elif (levelno >= 40): color = FOREGROUND_RED | FOREGROUND_INTENSITY elif (levelno >= 30): color = FOREGROUND_YELLOW | FOREGROUND_INTENSITY elif (levelno >= 20): color = FOREGROUND_GREEN elif (levelno >= 10): color = FOREGROUND_MAGENTA else: color = FOREGROUND_WHITE args[0]._set_color(color) ret = fn(*args) args[0]._set_color(FOREGROUND_WHITE) # print "after" return ret return new def add_coloring_to_emit_ansi(fn): # add methods we need to the class def new(*args): levelno = args[1].levelno if (levelno >= 50): color = '\x1b[31m' # red elif (levelno >= 40): color = '\x1b[31m' # red elif (levelno >= 30): color = '\x1b[33m' # yellow elif (levelno >= 20): color = '\x1b[32m' # green elif (levelno >= 10): color = '\x1b[35m' # pink else: color = '\x1b[0m' # normal if not args[1].msg: color = '\x1b[31m' args[1].msg = 'NoneType' args[1].msg = color + str(args[1].msg) + '\x1b[0m' # normal # print "after" return fn(*args) return new import platform if platform.system() == 'Windows': # Windows does not support ANSI escapes and we are using API calls to set the console color logging.StreamHandler.emit = add_coloring_to_emit_windows(logging.StreamHandler.emit) else: # all non-Windows platforms are supporting ANSI escapes so we use them logging.StreamHandler.emit = add_coloring_to_emit_ansi(logging.StreamHandler.emit) # log = logging.getLogger() # log.addFilter(log_filter()) # //hdlr = logging.StreamHandler() # //hdlr.setFormatter(formatter())
# Under MIT License, see LICENSE.txt from typing import Dict from Util import Position class Ball: def __init__(self, position=Position()): self._position = position self._velocity = Position() def update(self, new_dict: Dict): self.position = new_dict['position'] self.velocity = new_dict['velocity'] def is_moving_fast(self, fast_speed = 600.0): # mm/s return fast_speed < self.velocity.norm def is_mobile(self, immobile_speed = 300.0): # mm/s return immobile_speed < self.velocity.norm def is_immobile(self): return not self.is_mobile() @property def position(self) -> Position: return self._position @position.setter def position(self, value): assert isinstance(value, Position) self._position = value @property def velocity(self) -> Position: return self._velocity @velocity.setter def velocity(self, value): assert isinstance(value, Position) self._velocity = value
# Author: Jason Lu # 猜数字 age_of_oldboy = 56 guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("Yes, you got it.") elif guess_age > age_of_oldboy: print("think smaller...") else: print("think bigger...") print("===============for语句================") count = 0 age_of_oldboy = 56 for i in range(3): guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("Yes, you got it.") break elif guess_age > age_of_oldboy: print("think smaller...") else: print("think bigger...") count += 1 else: print("you have tried too many times...fuck off") for i in range(10): print('i:', i) for i in range(0, 10, 2): print('i: ', i) for i in range(0, 100): if i < 50: print('i < 50') else: continue print("===============while语句================") count = 0 age_of_oldboy = 100 while count < 3: int_guess_age = int(input(">>guess age:")) if int_guess_age == age_of_oldboy: break elif int_guess_age < age_of_oldboy: print('think bigger') else: print('think smaller') count += 1 else: print('You have tried too many times...fuck off')
import os import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk from cv_core import engine, util from cv_gtk3.main_window import MainWindowFunctions from cv_gtk3.setting import GUISettings from cv_gtk3.signal_handlers import handlers from cv_gtk3.gtk_util import GTKUtilities class CardvaultGTK(MainWindowFunctions): """ Main UI class for the GTK interface """ def __init__(self): # Start engine (without config file) self.engine = engine.CardvaultEngine() # Load Glade files glade_files = ['mainwindow.glade', 'search.glade', 'overlays.glade'] self.ui = Gtk.Builder() for file in glade_files: self.ui.add_from_string(GTKUtilities.load_ui_resource(file)) # Set pages for the ui to use GUISettings.pages = { "search": self.ui.get_object("searchView"), } # Verify that cache directories exist if not os.path.isdir(util.CoreConfig.cache_path): os.mkdir(util.CoreConfig.cache_path) if not os.path.isdir(util.CoreConfig.icon_cache_path): os.mkdir(util.CoreConfig.icon_cache_path) # Load single mana icons GTKUtilities.mana_icons = GTKUtilities.load_icon_cache(os.path.join(GTKUtilities.resources_path, 'mana')) # Load the the pre constructed icon cache GTKUtilities.precon_icon_cache = GTKUtilities.load_icon_cache(util.CoreConfig.icon_cache_path) # Call constructor of superclasses MainWindowFunctions.__init__(self, self.ui) # Create Signal handlers and connect them to the UI self.handlers = handlers.Handlers(self) self.ui.connect_signals(self.handlers) # Initialize starting view self.ui.get_object('mainWindow').show_all() self.hide_initial_widgets() self.switch_page('search') def main(): CardvaultGTK() Gtk.main() if __name__ == '__main__': main()
import theano import theano.tensor as T import mlbase.network as N import h5py import numpy as np import mlbase.layers.activation as act import mlbase.loaddata as l from mlbase.layers import * import mlbase.cost as cost from skimage.measure import block_reduce def test_generative(): import mlbase.cost as cost import mlbase.layers.activation as act import h5py network = N.Network() network.debug = True network.setInput(N.RawInput((1, 28,28))) network.append(N.Conv2d(feature_map_multiplier=32)) network.append(act.Relu()) network.append(N.Pooling()) network.append(N.Conv2d(feature_map_multiplier=2)) network.append(act.Relu()) network.append(N.Pooling()) network.append(UpConv2d(feature_map_multiplier=2)) network.append(act.Relu()) network.append(UpConv2d(feature_map_multiplier=32)) network.append(act.Relu()) #network.append(N.Flatten()) #network.append(N.FullConn(input_feature=1152, output_feature=1152*2)) #network.append(N.Relu()) #network.append(N.FullConn(input_feature=1152*2, output_feature=10)) #network.append(N.SoftMax()) network.costFunction = cost.ImageSSE network.inputOutputType = (T.tensor4(), T.tensor4(),) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) #network.train(trX, trY) #print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) network.train(trX, trX) print(np.sum((teX - network.predict(teX)) * (teX - network.predict(teX)))) # the following is the piece to load model and predict a image. #import mlbase.networkhelper as N #import mlbase.cost as cost #import theano.tensor as T #import mlbase.loaddata as l #from PIL import Image # #n = N.Network() #n.loadFromFile('/hdd/home/yueguan/workspace/sesame-paste-noodle-dev/expdata/saved_model_LAST') #n.costFunction = cost.ImageSSE #n.inputOutputType = (T.tensor4(), T.tensor4(),) # #n.build(reload=True) # #trX, trY, teX, teY = l.load_mnist() #result = n.predict(trX[0:1]) #result = (result > 0).astype(float)*255.0 # # #im = Image.fromarray(result[0][0]) #if im.mode != 'RGB': # im = im.convert('RGB') # #im.save('result.jpg') def test_resnet(): import mlbase.network as N import h5py network = N.Network() network.debug = True network.setInput(N.RawInput((1,28,28))) network.append(N.Conv2d(feature_map_multiplier=32)) network.append(ResLayer()) network.append(ResLayer()) network.append(ResLayer()) network.append(ResLayer(increase_dim=True)) network.append(ResLayer()) network.append(ResLayer()) network.append(ResLayer()) network.append(ResLayer(increase_dim=True)) network.append(ResLayer()) network.append(ResLayer()) network.append(ResLayer()) network.append(N.GlobalPooling()) network.append(N.FullConn(input_feature=128, output_feature=10)) network.append(N.SoftMax()) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) def test_deeper(): import h5py network = N.Network() network.debug = True network.setInput(N.RawInput((1,28,28))) network.append(N.Conv2d(feature_map_multiplier=32)) for _ in range(3): network.append(ResLayer()) network.append(ResLayer(increase_dim=True)) for _ in range(3): network.append(ResLayer()) network.append(ResLayer(increase_dim=True)) for _ in range(3): network.append(ResLayer()) network.append(N.GlobalPooling()) network.append(N.FullConn(input_feature=128, output_feature=10)) network.append(N.SoftMax()) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) def test_binaryinput(): network = N.Network() network.debug = True network.setInput(RawInput((1, 28,28))) network.append(Conv2d(filter_size=(3,3), input_feature=1, output_feature=32)) network.append(Relu()) network.append(Pooling((2,2))) network.append(Binarize()) network.append(Conv2d(filter_size=(3,3), input_feature=32, output_feature=64)) network.append(Relu()) network.append(Pooling((2,2))) network.append(Binarize()) network.append(Conv2d(filter_size=(3,3), input_feature=64, output_feature=128)) network.append(Relu()) network.append(Pooling((2,2))) network.append(Flatten()) network.append(FullConn(input_feature=1152, output_feature=1152*2)) network.append(Relu()) network.append(FullConn(input_feature=1152*2, output_feature=10)) network.append(SoftMax()) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) def test_binaryweight(): network = N.Network() network.debug = True network.setInput(RawInput((1, 28,28))) network.append(Conv2d(feature_map_multiplier=32)) network.append(Relu()) network.append(Pooling((2,2))) network.append(Binarize()) network.append(Conv2d(feature_map_multiplier=2)) network.append(Relu()) network.append(Pooling((2,2))) network.append(Binarize()) network.append(BinaryConv2d(feature_map_multiplier=2)) network.append(Relu()) network.append(Pooling((2,2))) network.append(Flatten()) network.append(FullConn(input_feature=1152, output_feature=1152*2)) network.append(Relu()) network.append(FullConn(input_feature=1152*2, output_feature=10)) network.append(SoftMax()) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) def test_unet(): n = N.Network() def unet_dag(): x1 = DAGPlan.input() y1 = Relu(Conv2d(Relu(Conv2d(x1)))) x2 = Pooling(y1) y2 = Relu(Conv2d(Relu(Conv2d(x2)))) x3 = Pooling(y2) y3 = Relu(Conv2d(Relu(Conv2d(x3)))) #x4 = y2 // conv.UpConv2d(y3) x4 = CropConcat(y2, UpConv2d(y3)) y4 = Relu(Conv2d(Relu(Conv2d(x4)))) #x5 = y1 // conv.UpConv2d(y4) x5 = CropConcat(y1, UpConv2d(y4)) y5 = Relu(Conv2d(Relu(Conv2d(x5)))) return y5 dagplan = unet_dag() class UNet(Layer, metaclass=DAG, dag=dagplan, yaml_tag=u'!UNet', type_name='UNet'): pass n.setInput(RawInput((1, 420//2, 580//2))) n.append(Conv2d(feature_map_multiplier=4)) n.append(Relu()) n.append(UNet()) n.append(Conv2d(output_feature=1)) n.batchSize = 32 n.costFunction = cost.ImageDice n.inputOutputType = (T.tensor4(), T.tensor4(),) n.build() trX, trY, teX = l.load_kaggle_ultrasound() trX = block_reduce(trX, block_size=(1,1,2,2), func=np.mean) trY = block_reduce(trY, block_size=(1,1,2,2), func=np.mean) teX = block_reduce(teX, block_size=(1,1,2,2), func=np.mean) trX = trX[:]/255.0 trY = trY[:]/255.0 teX = teX[:]/255.0 for i in range(5000): print(i) n.train(trX, trX[:,:,:208, :288]) #n.train(trX, trX) #print(np.sum((teX - network.predict(teX)) * (teX - network.predict(teX)))) def test_seqlayer(): network = N.Network() network.debug = True class ConvNN(layer.Layer, metaclass=compose.SeqLayer, seq=[Conv2d, act.Relu, pooling.Pooling], yaml_tag=u'!ConvNN', type_name='ConvNN'): def __init__(self, feature_map_multiplier=1): super().__init__() self.bases[0] = Conv2d(feature_map_multiplier=feature_map_multiplier) network.setInput(RawInput((1, 28,28))) network.append(ConvNN(feature_map_multiplier=32)) network.append(ConvNN(feature_map_multiplier=2)) network.append(ConvNN(feature_map_multiplier=2)) network.append(reshape.Flatten()) network.append(fullconn.FullConn(input_feature=1152, output_feature=1152*2)) network.append(act.Relu()) network.append(fullconn.FullConn(input_feature=1152*2, output_feature=10)) network.append(output.SoftMax()) network.build() trX, trY, teX, teY = l.load_mnist() for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) def testload(): n = N.Network() n.loadFromFile() n.saveToFile('testmodel') def test_maxout(): network = N.Network() network.setInput(RawInput((1, 28,28))) network.append(conv.Conv2d(filter_size=(3,3), feature_map_multiplier=128)) network.append(pooling.FeaturePooling(4)) network.append(pooling.Pooling((2,2))) network.append(conv.Conv2d(filter_size=(3,3), feature_map_multiplier=8)) network.append(pooling.FeaturePooling(4)) network.append(pooling.Pooling((2,2))) network.append(conv.Conv2d(filter_size=(3,3), feature_map_multiplier=8)) network.append(pooling.FeaturePooling(4)) network.append(pooling.GlobalPooling()) network.append(fullconn.FullConn(input_feature=128, output_feature=10)) network.append(output.SoftMax()) network.build() trX, trY, teX, teY = l.load_mnist() for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) def test_globalpooling(): network = N.Network() network.debug = True network.setInput(RawInput((1, 28,28))) network.append(conv.Conv2d(filter_size=(3,3), feature_map_multiplier=32)) network.append(bn.BatchNormalization()) network.append(act.Relu()) network.append(polling.Pooling((2,2))) network.append(conv.Conv2d(filter_size=(3,3), feature_map_multiplier=2)) network.append(bn.BatchNormalization()) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(conv.Conv2d(filter_size=(3,3), feature_map_multiplier=2)) network.append(bn.BatchNormalization()) network.append(act.Relu()) network.append(pooling.GlobalPooling()) network.append(fullconn.FullConn(input_feature=128, output_feature=10)) network.append(output.SoftMax()) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) def test5(): network = N.Network() network.debug = True network.setInput(RawInput((1, 28,28))) network.append(conv.Conv2d(filter_size=(3,3), feature_map_multiplier=32)) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(conv.Conv2d(filter_size=(3,3), feature_map_multiplier=2)) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(conv.Conv2d(filter_size=(3,3), feature_map_multiplier=2)) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(reshape.Flatten()) network.append(fullconn.FullConn(input_feature=1152, output_feature=1152*2)) network.append(act.Relu()) network.append(fullconn.FullConn(input_feature=1152*2, output_feature=10)) network.append(output.SoftMax()) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) def testbn(): network = N.Network() network.debug = True network.setSaveInterval(10) network.setInput(RawInput((1, 28,28))) network.append(conv.Conv2d(filter_size=(3,3), input_feature=1, output_feature=32)) network.append(N.BatchNormalization()) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(conv.Conv2d(filter_size=(3,3), input_feature=32, output_feature=64)) network.append(N.BatchNormalization()) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(conv.Conv2d(filter_size=(3,3), input_feature=64, output_feature=128)) network.append(N.BatchNormalization()) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(reshape.Flatten()) network.append(fullconn.FullConn(input_feature=1152, output_feature=1152*2)) network.append(act.Relu()) network.append(fullconn.FullConn(input_feature=1152*2, output_feature=10)) network.append(output.SoftMax()) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) def test(): network = N.Network() network.debug = True network.setInput(RawInput((28,28))) network.append(conv.Conv2d(filter_size=(3,3), input_feature=1, output_feature=32)) network.append(act.Relu()) network.append(conv.Conv2d(filter_size=(2,2), input_feature=32, output_feature=32, subsample=(2,2),border='valid')) network.append(conv.Conv2d(filter_size=(3,3), input_feature=32, output_feature=64)) network.append(act.Relu()) network.append(conv.Conv2d(filter_size=(2,2), input_feature=64, output_feature=64, subsample=(2,2),border='valid')) network.append(conv.Conv2d(filter_size=(3,3), input_feature=64, output_feature=128)) network.append(act.Relu()) network.append(conv.Conv2d(filter_size=(2,2), input_feature=128, output_feature=128, subsample=(2,2),border='valid')) network.append(reshape.Flatten()) network.append(fullconn.FullConn(input_feature=1152, output_feature=1152*2)) network.append(act.Relu()) network.append(fullconn.FullConn(input_feature=1152*2, output_feature=10)) network.append(output.SoftMax()) network.build() print(network) f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) def test4(): network = N.Network() network.debug = True network.setInput(RawInput((28,28))) network.append(conv.Conv2d(filter_size=(3,3), input_feature=1, output_feature=32)) network.append(act.Relu()) network.append(conv.Conv2d(filter_size=(2,2), input_feature=32, output_feature=32, subsample=(2,2),border='valid')) network.append(conv.Conv2d(filter_size=(3,3), input_feature=32, output_feature=64)) network.append(act.Relu()) network.append(conv.Conv2d(filter_size=(2,2), input_feature=64, output_feature=64, subsample=(2,2),border='valid')) network.append(conv.Conv2d(filter_size=(3,3), input_feature=64, output_feature=128)) network.append(act.Relu()) network.append(conv.Conv2d(filter_size=(2,2), input_feature=128, output_feature=128, subsample=(2,2),border='valid')) network.append(reshape.Flatten()) network.append(fullconn.FullConn(input_feature=1152, output_feature=1152*2)) network.append(act.Relu()) network.append(fullconn.FullConn(input_feature=1152*2, output_feature=10)) network.append(output.SoftMax()) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) # test3() def test3(): network = N.Network() network.debug = True network.setInput(RawInput((28,28))) network.append(conv.Conv2d(filter_size=(3,3), input_feature=1, output_feature=32)) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(conv.Conv2d(filter_size=(3,3), input_feature=32, output_feature=64)) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(conv.Conv2d(filter_size=(3,3), input_feature=64, output_feature=128)) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(reshape.Flatten()) network.append(fullconn.FullConn(input_feature=1152, output_feature=1152*2)) network.append(act.Relu()) network.append(fullconn.FullConn(input_feature=1152*2, output_feature=10)) network.append(output.SoftMax()) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) # test2() def test2(): network = N.Network() network.debug = True #network.setInput(RawInput((1, 28,28))) #network.append(conv.Conv2d(feature_map_multiplier=32)) #network.append(act.Relu()) #network.append(pooling.Pooling()) #network.append(conv.Conv2d(feature_map_multiplier=2)) #network.append(act.Relu()) #network.append(pooling.Pooling()) #network.append(conv.Conv2d(feature_map_multiplier=2)) #network.append(act.Relu()) #network.append(pooling.Pooling()) #network.append(reshape.Flatten()) #network.append(fullconn.FullConn(input_feature=1152, output_feature=1152*2)) #network.append(act.Relu()) #network.append(fullconn.FullConn(input_feature=1152*2, output_feature=10)) #network.append(output.SoftMax()) li = RawInput((1, 28,28)) network.setInput(li) lc1 = conv.Conv2d(feature_map_multiplier=32) la1 = act.Relu() lp1 = pooling.Pooling() lc2 = conv.Conv2d(feature_map_multiplier=2) la2 = act.Relu() lp2 = pooling.Pooling() lc3 = conv.Conv2d(feature_map_multiplier=2) la3 = act.Relu() lp3 = pooling.Pooling() lf = reshape.Flatten() lfc1 = fullconn.FullConn(input_feature=1152, output_feature=1152*2) la4 = act.Relu() lfc2 = fullconn.FullConn(input_feature=1152*2, output_feature=10) lsm = output.SoftMax() network.connect(li, lc1) network.connect(lc1, la1) network.connect(la1, lp1) network.connect(lp1, lc2) network.connect(lc2, la2) network.connect(la2, lp2) network.connect(lp2, lc3) network.connect(lc3, la3) network.connect(la3, lp3) network.connect(lp3, lf) network.connect(lf, lfc1) network.connect(lfc1, la4) network.connect(la4, lfc2) network.connect(lfc2, lsm) network.build() trX, trY, teX, teY = l.load_mnist() for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1))) # test1(): def test1(): network = N.Network() network.debug = True network.setInput((28,28)) network.append(conv.Conv2d(filter=(3,3), input_feature=1, output_feature=32)) network.append(act.Relu()) network.append(conv.Conv2d(filter=(3,3), input_feature=32, output_feature=32)) network.append(act.Relu()) network.append(conv.Conv2d(filter=(3,3), input_feature=32, output_feature=32)) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(conv.Conv2d(filter=(3,3), input_feature=32, output_feature=64)) network.append(act.Relu()) network.append(conv.Conv2d(filter=(3,3), input_feature=64, output_feature=64)) network.append(act.Relu()) network.append(conv.Conv2d(filter=(3,3), input_feature=64, output_feature=64)) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(conv.Conv2d(filter=(3,3), input_feature=64, output_feature=128)) network.append(act.Relu()) network.append(conv.Conv2d(filter=(3,3), input_feature=128, output_feature=128)) network.append(act.Relu()) network.append(conv.Conv2d(filter=(3,3), input_feature=128, output_feature=128)) network.append(act.Relu()) network.append(pooling.Pooling((2,2))) network.append(reshape.Flatten()) network.append(fullconn.FullConn(input_feature=1152, output_feature=1152*2)) network.append(act.Relu()) network.append(fullconn.FullConn(input_feature=1152*2, output_feature=10)) network.append(output.SoftMax()) #network.setCost(N.CategoryCrossEntropy) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == network.predict(teX))) def test_mlp(): n = N.Network() n.setInput(RawInput((1, 28, 28))) n.append(Flatten()) n.append(FullConn(feature_map_multiplier=2)) n.append(Elu()) n.append(FullConn(output_feature=10)) n.append(output.SoftMax()) n.build() trX, trY, teX, teY = l.load_mnist() for i in range(100): print(i) n.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(n.predict(teX), axis=1))) def test_show_internal(): n = N.Network() n.setInput(RawInput((1, 28, 28))) n.append(Flatten(), "flatten") n.append(FullConn(feature_map_multiplier=2), "fc1") n.append(Elu(), "layer1") n.append(FullConn(output_feature=10), "fc2") n.append(output.SoftMax(), "layer2") n.build() trX, trY, teX, teY = l.load_mnist() for i in range(100): print(i) n.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(n.predict(teX), axis=1))) n.predict(teX, stub=["layer1", "layer2"]) if __name__ == "__main__": test_mlp()
from datetime import timedelta, datetime from airflow import DAG from airflow.contrib.operators.bigquery_operator import BigQueryOperator from airflow.contrib.operators.bigquery_to_gcs import BigQueryToCloudStorageOperator from airflow.contrib.operators.gcs_to_bq import GoogleCloudStorageToBigQueryOperator from dags.support import schemas seven_days_ago = datetime.combine(datetime.today() - timedelta(7), datetime.min.time()) default_args = { 'owner': 'airflow', 'depends_on_past': False, 'start_date': seven_days_ago, 'email': ['alex@vanboxel.be'], 'email_on_failure': False, 'email_on_retry': False, 'retries': 1, 'retry_delay': timedelta(minutes=30), } with DAG('v1_8_bigquery', schedule_interval=timedelta(days=1), default_args=default_args) as dag: bq_extract_one_day = BigQueryOperator( task_id='bq_extract_one_day', bql='gcp_smoke/gsob_extract_day.sql', destination_dataset_table= '{{var.value.gcq_dataset}}.gsod_partition{{ ds_nodash }}', write_disposition='WRITE_TRUNCATE', bigquery_conn_id='gcp_smoke', use_legacy_sql=False ) bq2gcp_avro = BigQueryToCloudStorageOperator( task_id='bq2gcp_avro', source_project_dataset_table='{{var.value.gcq_dataset}}.gsod_partition{{ ds_nodash }}', destination_cloud_storage_uris=[ 'gs://{{var.value.gcs_bucket}}/{{var.value.gcs_root}}/gcp_smoke_bq/bq_to_gcp_avro/{{ ds_nodash }}/part-*.avro' ], export_format='AVRO', bigquery_conn_id='gcp_smoke', ) bq2gcp_override = BigQueryToCloudStorageOperator( task_id='bq2gcp_override', source_project_dataset_table='{{var.value.gcq_dataset}}.gsod_partition{{ ds_nodash }}', destination_cloud_storage_uris=[ 'gs://{{var.value.gcs_bucket}}/{{var.value.gcs_root}}/gcp_smoke_bq/bq_to_gcp_avro/99999999/part-*.avro' ], export_format='AVRO', bigquery_conn_id='gcp_smoke', ) gcs2bq_avro_auto_schema = GoogleCloudStorageToBigQueryOperator( task_id='gcs2bq_avro_auto_schema', bucket='{{var.value.gcs_bucket}}', source_objects=[ '{{var.value.gcs_root}}/gcp_smoke_bq/bq_to_gcp_avro/{{ ds_nodash }}/part-*' ], destination_project_dataset_table='{{var.value.gcq_tempset}}.avro_auto_schema{{ ds_nodash }}', source_format='AVRO', create_disposition='CREATE_IF_NEEDED', write_disposition='WRITE_TRUNCATE', google_cloud_storage_conn_id='gcp_smoke', bigquery_conn_id='gcp_smoke' ) gcs2bq_avro_with_schema = GoogleCloudStorageToBigQueryOperator( task_id='gcs2bq_avro_with_schema', bucket='{{var.value.gcs_bucket}}', source_objects=[ '{{var.value.gcs_root}}/gcp_smoke_bq/bq_to_gcp_avro/{{ ds_nodash }}/part-*' ], destination_project_dataset_table='{{var.value.gcq_tempset}}.avro_with_schema{{ ds_nodash }}', source_format='AVRO', schema_fields=schemas.gsob(), create_disposition='CREATE_IF_NEEDED', write_disposition='WRITE_TRUNCATE', google_cloud_storage_conn_id='gcp_smoke', bigquery_conn_id='gcp_smoke' ) bq_extract_one_day >> bq2gcp_avro >> bq2gcp_override bq2gcp_avro >> gcs2bq_avro_auto_schema bq2gcp_avro >> gcs2bq_avro_with_schema
import numpy def obtain_matrix_from_quaternion(q: numpy.ndarray) -> numpy.ndarray: if q.size != 4: raise ValueError('Wrong number of elements on the array (q.size != 4)!') m = numpy.zeros((3, 3), dtype=numpy.float64) m[0, 0] = 1.0 - 2.0 * q[2] * q[2] - 2.0 * q[3] * q[3] m[0, 1] = 2.0 * q[1] * q[2] - 2.0 * q[3] * q[0] m[0, 2] = 2.0 * q[1] * q[3] + 2.0 * q[2] * q[0] m[1, 0] = 2.0 * q[1] * q[2] + 2.0 * q[3] * q[0] m[1, 1] = 1.0 - 2.0 * q[1] * q[1] - 2.0 * q[3] * q[3] m[1, 2] = 2.0 * q[2] * q[3] - 2.0 * q[1] * q[0] m[2, 0] = 2.0 * q[1] * q[3] - 2.0 * q[2] * q[0] m[2, 1] = 2.0 * q[2] * q[3] + 2.0 * q[1] * q[0] m[2, 2] = 1.0 - 2.0 * q[1] * q[1] - 2.0 * q[2] * q[2] return m def obtain_angle_vector_from_matrix(m: numpy.ndarray) -> tuple: v = numpy.array([ m[2, 1] - m[1, 2], m[0, 2] - m[2, 0], m[1, 0] - m[0, 1] ], dtype=numpy.float64) a = numpy.linalg.norm(v) v /= a a = numpy.arcsin(a / 2.0) return (a, v) def obtain_quaternion_from_angle_vector(a: float, v: numpy.ndarray) -> numpy.ndarray: v_magnitude = numpy.linalg.norm(v) q = numpy.array((1.0, 0.0, 0.0, 0.0), dtype=numpy.float64) if v_magnitude > 0.0 and abs(a) > 0.0: q = numpy.hstack((numpy.cos(a / 2.0), numpy.sin(a / 2.0) * v / v_magnitude)) return q def calculate_hamilton_product(q1: numpy.ndarray, q2: numpy.ndarray) -> numpy.ndarray: p = numpy.zeros((4), dtype=numpy.float64) p[0] = q1[0] * q2[0] - q1[1] * q2[1] - q1[2] * q2[2] - q1[3] * q2[3] p[1] = q1[0] * q2[1] + q1[1] * q2[0] + q1[2] * q2[3] - q1[3] * q2[2] p[2] = q1[0] * q2[2] + q1[2] * q2[0] - q1[1] * q2[3] + q1[3] * q2[1] p[3] = q1[0] * q2[3] + q1[3] * q2[0] + q1[1] * q2[2] - q1[2] * q2[1] return p def obtain_quaternion_conjugate(q: numpy.ndarray)->numpy.ndarray: p = q.copy() p[1:] *= -1 return p def calculate_rotated_vector_by_quaternion( v: numpy.ndarray, q: numpy.ndarray ) -> numpy.ndarray: p = numpy.hstack((0.0, v)) p = calculate_hamilton_product( calculate_hamilton_product(q, p), obtain_quaternion_conjugate(q) ) return p[1:] def calculate_rotation_matrix_from_bases( b1: numpy.ndarray, b2: numpy.ndarray, b3: numpy.ndarray, v1: numpy.ndarray, v2: numpy.ndarray, v3: numpy.ndarray, ) -> numpy.ndarray: b1n = b1 / numpy.linalg.norm(b1) b2n = b2 / numpy.linalg.norm(b2) b3n = b3 / numpy.linalg.norm(b3) v1n = v1 / numpy.linalg.norm(v1) v2n = v2 / numpy.linalg.norm(v2) v3n = v3 / numpy.linalg.norm(v3) V = numpy.matmul(v1n.reshape(3, 1), b1n.reshape((1, 3))) \ + numpy.matmul(v2n.reshape(3, 1), b2n.reshape((1, 3))) \ + numpy.matmul(v3n.reshape(3, 1), b3n.reshape((1, 3))) U, Q, W = numpy.linalg.svd(V) R = numpy.matmul(U, W) return R
#!/usr/bin/python ################################################################ # LiveQ - An interactive volunteering computing batch system # Copyright (C) 2013 Ioannis Charalampidis # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ################################################################ # ---------- import sys sys.path.append("../liveq-common") # ---------- import time import logging from agent.io.jobmanagers import JobManagers from agent.config import Config from liveq.exceptions import ConfigException from liveq.reporting.postmortem import PostMortem from liveq import handleSIGINT, exit # Prepare runtime configuration runtimeConfig = { } # Load configuration try: Config.fromFile( "config/agent.conf.local", runtimeConfig ) except ConfigException as e: print("ERROR Configuration exception: %s" % e) exit(1) # Hook sigint -> Shutdown handleSIGINT() # Setup post-mortem PostMortem.addGlobalConfig("global", Config) PostMortem.addGlobalInfo("version", "2.0") # Prepare post-mortem from subprocess import Popen, PIPE pm = PostMortem() p = Popen(["C:\\windows\\system32\\help.exe"], stdout=PIPE) pm.addProcess("C:\\windows\\system32\\help.exe", p, stdout=True) time.sleep(2) pm.complete() print pm.sections a = str(pm.sections) print pm.toBuffer() b = pm.toBuffer() print "dump=%i, compress=%i" % (len(a),len(b)) # EXIT exit(0) # Banner logging.info("Starting agent tests %s" % Config.UUID) # Login to the server jobmanagers = JobManagers( Config.SERVER_CHANNEL ) def hsFunction(channel): logging.info("Sending handshake to %s" % channel.name) channel.send('handshake', { 'version': 2, 'slots': 0, 'free_slots': 0, 'group': 'debug' }) jobmanagers.handshakeFn(hsFunction) # Pick JIDs while True: jobmanagers.process(0.5) print "--- Agent: %s" % jobmanagers.jid()
""" MIT License Copyright (c) 2016 Sully Chen (https://github.com/SullyChen/Autopilot-TensorFlow) 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 tensorflow as tf import scipy g = tf.Graph() def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) def conv2d(x, W, stride): return tf.nn.conv2d(x, W, strides=[1, stride, stride, 1], padding='VALID') with g.as_default(): x = tf.placeholder(tf.float32, shape=[None, 66, 200, 3]) y_ = tf.placeholder(tf.float32, shape=[None, 1]) x_image = x #first convolutional layer W_conv1 = weight_variable([5, 5, 3, 24]) b_conv1 = bias_variable([24]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1, 2) + b_conv1) #second convolutional layer W_conv2 = weight_variable([5, 5, 24, 36]) b_conv2 = bias_variable([36]) h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2, 2) + b_conv2) #third convolutional layer W_conv3 = weight_variable([5, 5, 36, 48]) b_conv3 = bias_variable([48]) h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3, 2) + b_conv3) #fourth convolutional layer W_conv4 = weight_variable([3, 3, 48, 64]) b_conv4 = bias_variable([64]) h_conv4 = tf.nn.relu(conv2d(h_conv3, W_conv4, 1) + b_conv4) #fifth convolutional layer W_conv5 = weight_variable([3, 3, 64, 64]) b_conv5 = bias_variable([64]) h_conv5 = tf.nn.relu(conv2d(h_conv4, W_conv5, 1) + b_conv5) #FCL 1 W_fc1 = weight_variable([1152, 1164]) b_fc1 = bias_variable([1164]) h_conv5_flat = tf.reshape(h_conv5, [-1, 1152]) h_fc1 = tf.nn.relu(tf.matmul(h_conv5_flat, W_fc1) + b_fc1) keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) #FCL 2 W_fc2 = weight_variable([1164, 100]) b_fc2 = bias_variable([100]) h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob) #FCL 3 W_fc3 = weight_variable([100, 50]) b_fc3 = bias_variable([50]) h_fc3 = tf.nn.relu(tf.matmul(h_fc2_drop, W_fc3) + b_fc3) h_fc3_drop = tf.nn.dropout(h_fc3, keep_prob) #FCL 3 W_fc4 = weight_variable([50, 10]) b_fc4 = bias_variable([10]) h_fc4 = tf.nn.relu(tf.matmul(h_fc3_drop, W_fc4) + b_fc4) h_fc4_drop = tf.nn.dropout(h_fc4, keep_prob) #Output W_fc5 = weight_variable([10, 1]) b_fc5 = bias_variable([1]) y = tf.multiply(tf.atan(tf.matmul(h_fc4_drop, W_fc5) + b_fc5), 2) #scale the atan output
# Copyright (c) 2010-2014, GEM Foundation. # # NRML is free software: you can redistribute it and/or modify it # under the terms of the GNU Affero General Public License as published # by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # NRML 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 Affero General Public License # along with NRML. If not, see <http://www.gnu.org/licenses/>. import cStringIO from contextlib import contextmanager from xml.sax.saxutils import escape, quoteattr import numpy # this is needed by the doctests, don't remove it @contextmanager def floatformat(fmt_string): """ Context manager to change the default format string for the function :func:`openquake.commonlib.writers.scientificformat`. :param fmt_string: the format to use; for instance '%13.9E' """ fmt_defaults = scientificformat.__defaults__ scientificformat.__defaults__ = (fmt_string,) + fmt_defaults[1:] try: yield finally: scientificformat.__defaults__ = fmt_defaults zeroset = set(['E', '-', '+', '.', '0']) def scientificformat(value, fmt='%13.9E', sep=' ', sep2=':'): """ :param value: the value to convert into a string :param fmt: the formatting string to use for float values :param sep: separator to use for vector-like values :param sep2: second separator to use for matrix-like values Convert a float or an array into a string by using the scientific notation and a fixed precision (by default 10 decimal digits). For instance: >>> scientificformat(-0E0) '0.000000000E+00' >>> scientificformat(-0.004) '-4.000000000E-03' >>> scientificformat([0.004]) '4.000000000E-03' >>> scientificformat([0.01, 0.02], '%10.6E') '1.000000E-02 2.000000E-02' >>> scientificformat([[0.1, 0.2], [0.3, 0.4]], '%4.1E') '1.0E-01:2.0E-01 3.0E-01:4.0E-01' """ if isinstance(value, basestring): return value elif isinstance(value, (int, long)): return str(value) elif hasattr(value, '__len__'): return sep.join((scientificformat(f, fmt, sep2) for f in value)) elif isinstance(value, float): fmt_value = fmt % value if set(fmt_value) <= zeroset: # '-0.0000000E+00' is converted into '0.0000000E+00 fmt_value = fmt_value.replace('-', '') return fmt_value return str(value) class StreamingXMLWriter(object): """ A stream-based XML writer. The typical usage is something like this:: with StreamingXMLWriter(output_file) as writer: writer.start_tag('root') for node in nodegenerator(): writer.serialize(node) writer.end_tag('root') """ def __init__(self, stream, indent=4, encoding='utf-8', nsmap=None): """ :param stream: the stream or a file where to write the XML :param int indent: the indentation to use in the XML (default 4 spaces) """ self.stream = stream self.indent = indent self.encoding = encoding self.indentlevel = 0 self.nsmap = nsmap def shorten(self, tag): """ Get the short representation of a fully qualified tag :param str tag: a (fully qualified or not) XML tag """ if tag.startswith('{'): ns, _tag = tag.rsplit('}') tag = self.nsmap.get(ns[1:], '') + _tag return tag def _write(self, text): """Write text by respecting the current indentlevel""" if not isinstance(text, str): text = text.encode(self.encoding, 'xmlcharrefreplace') spaces = ' ' * (self.indent * self.indentlevel) self.stream.write(spaces + text.strip() + '\n') def emptyElement(self, name, attrs): """Add an empty element (may have attributes)""" attr = ' '.join('%s=%s' % (n, quoteattr(scientificformat(v))) for n, v in sorted(attrs.iteritems())) self._write('<%s %s/>' % (name, attr)) def start_tag(self, name, attrs=None): """Open an XML tag""" if not attrs: self._write('<%s>' % name) else: self._write('<' + name) for (name, value) in sorted(attrs.items()): self._write( ' %s=%s' % (name, quoteattr(scientificformat(value)))) self._write('>') self.indentlevel += 1 def end_tag(self, name): """Close an XML tag""" self.indentlevel -= 1 self._write('</%s>' % name) def serialize(self, node): """Serialize a node object (typically an ElementTree object)""" if self.nsmap is not None: tag = self.shorten(node.tag) else: tag = node.tag if not node and node.text is None: self.emptyElement(tag, node.attrib) return self.start_tag(tag, node.attrib) if node.text is not None: self._write(escape(scientificformat(node.text).strip())) for subnode in node: self.serialize(subnode) self.end_tag(tag) def __enter__(self): """Write the XML declaration""" self._write('<?xml version="1.0" encoding="%s"?>\n' % self.encoding) return self def __exit__(self, etype, exc, tb): """Close the XML document""" pass def tostring(node, indent=4): """ Convert a node into an XML string by using the StreamingXMLWriter. This is useful for testing purposes. :param node: a node object (typically an ElementTree object) :param indent: the indentation to use in the XML (default 4 spaces) """ out = cStringIO.StringIO() writer = StreamingXMLWriter(out, indent) writer.serialize(node) return out.getvalue() def save_csv(dest, header_rows, sep=',', fmt='%12.8E', mode='wb'): """ :param dest: destination filename :param header_rows: header + rows to save :param sep: separator to use (default comma) :param fmt: formatting string (default '%12.8E') :param mode: file open mode (default 'wb') """ with open(dest, mode) as f: for row in header_rows: f.write(sep.join(scientificformat(col, fmt) for col in row) + '\n') return dest # recursive function used internally by build_header def _build_header(dtype, root): header = [] if dtype.fields is None: if not root: return [] return [root + (str(dtype), dtype.shape)] for field in dtype.fields: dt = dtype.fields[field][0] if dt.subdtype is None: # nested header.extend(_build_header(dt, root + (field,))) else: numpytype = str(dt.subdtype[0]) header.append(root + (field, numpytype, dt.shape)) return header def build_header(dtype): """ Convert a numpy nested dtype into a list of strings suitable as header of csv file. >>> imt_dt = numpy.dtype([('PGA', float, 3), ('PGV', float, 4)]) >>> build_header(imt_dt) ['PGV:float64:4', 'PGA:float64:3'] >>> gmf_dt = numpy.dtype([('A', imt_dt), ('B', imt_dt), ... ('idx', numpy.uint32)]) >>> build_header(gmf_dt) ['A-PGV:float64:4', 'A-PGA:float64:3', 'B-PGV:float64:4', 'B-PGA:float64:3', 'idx:uint32:'] """ header = _build_header(dtype, ()) h = [] for col in header: name = '-'.join(col[:-2]) numpytype = col[-2] shape = col[-1] h.append(':'.join([name, numpytype, ':'.join(map(str, shape))])) return h def extract_from(data, fields): """ Extract data from numpy arrays with nested records. >>> imt_dt = numpy.dtype([('PGA', float, 3), ('PGV', float, 4)]) >>> a = numpy.array([([1, 2, 3], [4, 5, 6, 7])], imt_dt) >>> extract_from(a, ['PGA']) array([[ 1., 2., 3.]]) >>> gmf_dt = numpy.dtype([('A', imt_dt), ('B', imt_dt), ... ('idx', numpy.uint32)]) >>> b = numpy.array([(([1, 2, 3], [4, 5, 6, 7]), ... ([1, 2, 4], [3, 5, 6, 7]), 8)], gmf_dt) >>> extract_from(b, ['idx']) array([8], dtype=uint32) >>> extract_from(b, ['B', 'PGV']) array([[ 3., 5., 6., 7.]]) """ for f in fields: data = data[f] return data def write_csv(dest, data, sep=',', fmt='%12.8E', header=None): """ :param dest: destination filename :param data: array to save :param sep: separator to use (default comma) :param fmt: formatting string (default '%12.8E') :param header: optional list with the names of the columns to display """ try: # see if data is a composite numpy array data.dtype.fields except AttributeError: # not a composite array header = header or [] else: header = header or build_header(data.dtype) with open(dest, 'wb') as f: if header: f.write(sep.join(header) + '\n') all_fields = [col.split(':', 1)[0].split('-') for col in header] for record in data: row = [] for fields in all_fields: row.append(extract_from(record, fields)) f.write(sep.join(scientificformat(col, fmt) for col in row) + '\n') else: for row in data: f.write(sep.join(scientificformat(col, fmt) for col in row) + '\n') return dest
import json import time from calendar import timegm from datetime import datetime, timedelta from django.conf import settings from django.core import signing from django.urls import reverse from django.test import RequestFactory import jwt import mock from freezegun import freeze_time from rest_framework.exceptions import AuthenticationFailed from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView from rest_framework_jwt.views import refresh_jwt_token from olympia import core from olympia.amo.templatetags.jinja_helpers import absolutify from olympia.amo.tests import ( APITestClient, TestCase, WithDynamicEndpoints, user_factory) from olympia.api.authentication import ( JWTKeyAuthentication, WebTokenAuthentication) from olympia.api.tests.test_jwt_auth import JWTAuthKeyTester class JWTKeyAuthTestView(APIView): """ This is an example of a view that would be protected by JWTKeyAuthentication, used in TestJWTKeyAuthProtectedView below. """ permission_classes = [IsAuthenticated] authentication_classes = [JWTKeyAuthentication] def get(self, request): return Response('some get response') def post(self, request): return Response({'user_pk': request.user.pk}) class TestJWTKeyAuthentication(JWTAuthKeyTester, TestCase): client_class = APITestClient def setUp(self): super(TestJWTKeyAuthentication, self).setUp() self.factory = RequestFactory() self.auth = JWTKeyAuthentication() self.user = user_factory(read_dev_agreement=datetime.now()) def request(self, token): return self.factory.get('/', HTTP_AUTHORIZATION='JWT {}'.format(token)) def _create_token(self, api_key=None): if api_key is None: api_key = self.create_api_key(self.user) return self.create_auth_token(api_key.user, api_key.key, api_key.secret) def test_get_user(self): core.set_remote_addr('15.16.23.42') user, _ = self.auth.authenticate(self.request(self._create_token())) assert user == self.user assert user.last_login_ip == '15.16.23.42' self.assertCloseToNow(user.last_login) def test_wrong_type_for_iat(self): api_key = self.create_api_key(self.user) # Manually create a broken payload where 'iat' is a string containing # a timestamp.. issued_at = int(time.mktime(datetime.utcnow().timetuple())) payload = { 'iss': api_key.key, 'iat': unicode(issued_at), 'exp': unicode( issued_at + settings.MAX_APIKEY_JWT_AUTH_TOKEN_LIFETIME), } token = self.encode_token_payload(payload, api_key.secret) core.set_remote_addr('1.2.3.4') with self.assertRaises(AuthenticationFailed) as ctx: self.auth.authenticate(self.request(token)) assert ctx.exception.detail == ( 'Wrong type for one or more keys in payload') def test_unknown_issuer(self): api_key = self.create_api_key(self.user) payload = self.auth_token_payload(self.user, api_key.key) payload['iss'] = 'non-existant-issuer' token = self.encode_token_payload(payload, api_key.secret) with self.assertRaises(AuthenticationFailed) as ctx: self.auth.authenticate(self.request(token)) assert ctx.exception.detail == 'Unknown JWT iss (issuer).' def test_deleted_user(self): in_the_past = self.days_ago(42) self.user.update( last_login_ip='48.15.16.23', last_login=in_the_past, deleted=True) with self.assertRaises(AuthenticationFailed) as ctx: self.auth.authenticate(self.request(self._create_token())) assert ctx.exception.detail == 'User account is disabled.' self.user.reload() assert self.user.last_login == in_the_past assert self.user.last_login_ip == '48.15.16.23' def test_user_has_not_read_agreement(self): self.user.update(read_dev_agreement=None) with self.assertRaises(AuthenticationFailed) as ctx: self.auth.authenticate(self.request(self._create_token())) assert ctx.exception.detail == 'User has not read developer agreement.' @mock.patch('olympia.api.jwt_auth.jwt_decode_handler') def test_decode_authentication_failed(self, jwt_decode_handler): jwt_decode_handler.side_effect = AuthenticationFailed with self.assertRaises(AuthenticationFailed) as ctx: self.auth.authenticate(self.request('whatever')) assert ctx.exception.detail == 'Incorrect authentication credentials.' @mock.patch('olympia.api.jwt_auth.jwt_decode_handler') def test_decode_expired_signature(self, jwt_decode_handler): jwt_decode_handler.side_effect = jwt.ExpiredSignature with self.assertRaises(AuthenticationFailed) as ctx: self.auth.authenticate(self.request('whatever')) assert ctx.exception.detail == 'Signature has expired.' @mock.patch('olympia.api.jwt_auth.jwt_decode_handler') def test_decode_decoding_error(self, jwt_decode_handler): jwt_decode_handler.side_effect = jwt.DecodeError with self.assertRaises(AuthenticationFailed) as ctx: self.auth.authenticate(self.request('whatever')) assert ctx.exception.detail == 'Error decoding signature.' @mock.patch('olympia.api.jwt_auth.jwt_decode_handler') def test_decode_invalid_token(self, jwt_decode_handler): jwt_decode_handler.side_effect = jwt.InvalidTokenError with self.assertRaises(AuthenticationFailed) as ctx: self.auth.authenticate(self.request('whatever')) assert ctx.exception.detail == 'Invalid JWT Token.' def test_refuse_refreshable_tokens(self): # We should not accept refreshable tokens. api_key = self.create_api_key(self.user) payload = self.auth_token_payload(self.user, api_key.key) payload['orig_iat'] = timegm(payload['iat'].utctimetuple()) token = self.encode_token_payload(payload, api_key.secret) with self.assertRaises(AuthenticationFailed) as ctx: self.auth.authenticate(self.request(token)) assert ctx.exception.detail == ( "API key based tokens are not refreshable, don't include " "`orig_iat` in their payload.") def test_cant_refresh_token(self): # Developers generate tokens, not us, they should not be refreshable, # the refresh implementation does not even know how to decode them. api_key = self.create_api_key(self.user) payload = self.auth_token_payload(self.user, api_key.key) payload['orig_iat'] = timegm(payload['iat'].utctimetuple()) token = self.encode_token_payload(payload, api_key.secret) request = self.factory.post('/lol-refresh', {'token': token}) response = refresh_jwt_token(request) response.render() assert response.status_code == 400 data = json.loads(response.content) assert data == {'non_field_errors': ['Error decoding signature.']} class TestJWTKeyAuthProtectedView( WithDynamicEndpoints, JWTAuthKeyTester, TestCase): client_class = APITestClient def setUp(self): super(TestJWTKeyAuthProtectedView, self).setUp() self.endpoint(JWTKeyAuthTestView) self.client.logout_api() # just to be sure! self.user = user_factory(read_dev_agreement=datetime.now()) def request(self, method, *args, **kw): handler = getattr(self.client, method) return handler(reverse('test-dynamic-endpoint'), *args, **kw) def jwt_request(self, token, method, *args, **kw): return self.request(method, HTTP_AUTHORIZATION='JWT {}'.format(token), *args, **kw) def test_get_requires_auth(self): res = self.request('get') assert res.status_code == 401, res.content def test_post_requires_auth(self): res = self.request('post', {}) assert res.status_code == 401, res.content def test_can_post_with_jwt_header(self): api_key = self.create_api_key(self.user) token = self.create_auth_token(api_key.user, api_key.key, api_key.secret) res = self.jwt_request(token, 'post', {}) assert res.status_code == 200, res.content data = json.loads(res.content) assert data['user_pk'] == self.user.pk def test_api_key_must_be_active(self): api_key = self.create_api_key(self.user, is_active=None) token = self.create_auth_token(api_key.user, api_key.key, api_key.secret) res = self.jwt_request(token, 'post', {}) assert res.status_code == 401, res.content class TestWebTokenAuthentication(TestCase): client_class = APITestClient def setUp(self): super(TestWebTokenAuthentication, self).setUp() self.auth = WebTokenAuthentication() self.factory = RequestFactory() self.user = user_factory(read_dev_agreement=datetime.now()) def _authenticate(self, token): url = absolutify('/api/v3/whatever/') prefix = WebTokenAuthentication.auth_header_prefix request = self.factory.post( url, HTTP_HOST='testserver', HTTP_AUTHORIZATION='{0} {1}'.format(prefix, token)) return self.auth.authenticate(request) def test_success(self): token = self.client.generate_api_token(self.user) user, _ = self._authenticate(token) assert user == self.user def test_authenticate_header(self): request = self.factory.post('/api/v3/whatever/') assert (self.auth.authenticate_header(request) == 'bearer realm="api"') def test_wrong_header_only_prefix(self): request = self.factory.post( '/api/v3/whatever/', HTTP_AUTHORIZATION=WebTokenAuthentication.auth_header_prefix) with self.assertRaises(AuthenticationFailed) as exp: self.auth.authenticate(request) assert exp.exception.detail['code'] == 'ERROR_INVALID_HEADER' assert exp.exception.detail['detail'] == ( 'Invalid Authorization header. No credentials provided.') def test_wrong_header_too_many_spaces(self): request = self.factory.post( '/api/v3/whatever/', HTTP_AUTHORIZATION='{} foo bar'.format( WebTokenAuthentication.auth_header_prefix)) with self.assertRaises(AuthenticationFailed) as exp: self.auth.authenticate(request) assert exp.exception.detail['code'] == 'ERROR_INVALID_HEADER' assert exp.exception.detail['detail'] == ( 'Invalid Authorization header. ' 'Credentials string should not contain spaces.') def test_no_token(self): request = self.factory.post('/api/v3/whatever/') self.auth.authenticate(request) is None def test_expired_token(self): old_date = datetime.now() - timedelta( seconds=settings.SESSION_COOKIE_AGE + 1) with freeze_time(old_date): token = self.client.generate_api_token(self.user) with self.assertRaises(AuthenticationFailed) as exp: self._authenticate(token) assert exp.exception.detail['code'] == 'ERROR_SIGNATURE_EXPIRED' assert exp.exception.detail['detail'] == 'Signature has expired.' def test_still_valid_token(self): not_so_old_date = datetime.now() - timedelta( seconds=settings.SESSION_COOKIE_AGE - 30) with freeze_time(not_so_old_date): token = self.client.generate_api_token(self.user) assert self._authenticate(token)[0] == self.user def test_bad_token(self): token = 'garbage' with self.assertRaises(AuthenticationFailed) as exp: self._authenticate(token) assert exp.exception.detail['code'] == 'ERROR_DECODING_SIGNATURE' assert exp.exception.detail['detail'] == 'Error decoding signature.' def test_user_id_is_none(self): token = self.client.generate_api_token(self.user, user_id=None) with self.assertRaises(AuthenticationFailed): self._authenticate(token) def test_no_user_id_in_payload(self): data = { 'auth_hash': self.user.get_session_auth_hash(), } token = signing.dumps(data, salt=WebTokenAuthentication.salt) with self.assertRaises(AuthenticationFailed): self._authenticate(token) def test_no_auth_hash_in_payload(self): data = { 'user_id': self.user.pk, } token = signing.dumps(data, salt=WebTokenAuthentication.salt) with self.assertRaises(AuthenticationFailed): self._authenticate(token) def test_user_deleted(self): self.user.delete() token = self.client.generate_api_token(self.user) with self.assertRaises(AuthenticationFailed): self._authenticate(token) def test_invalid_user_not_found(self): token = self.client.generate_api_token(self.user, user_id=-1) with self.assertRaises(AuthenticationFailed): self._authenticate(token) def test_invalid_user_other_user(self): user2 = user_factory(read_dev_agreement=datetime.now()) token = self.client.generate_api_token(self.user, user_id=user2.pk) with self.assertRaises(AuthenticationFailed): self._authenticate(token) def test_wrong_auth_id(self): token = self.client.generate_api_token(self.user) self.user.update(auth_id=self.user.auth_id + 42) with self.assertRaises(AuthenticationFailed): self._authenticate(token) def test_make_sure_token_is_decodable(self): token = self.client.generate_api_token(self.user) # A token is really a string containing the json dict, # a timestamp and a signature, separated by ':'. The base64 encoding # lacks padding, which is why we need to use signing.b64_decode() which # handles that for us. data = json.loads(signing.b64_decode(token.split(':')[0])) assert data['user_id'] == self.user.pk assert data['auth_hash'] == self.user.get_session_auth_hash()
from __future__ import division import os from pymongo import MongoClient import numpy as np from moca.helpers import read_memefile, get_total_sequences from moca.helpers.db import create_binary_pickle, encode_image#, unpickle_numpy_array from moca.helpers import get_max_occuring_bases, search_files #from moca.helpers import read_centrimo_txt #from moca.helpers import read_centrimo_stats from moca.helpers.seqstats import get_center_enrichment, get_motif_evalue, perform_t_test, get_pearson_corr from moca.helpers.seqstats import get_flanking_scores, remove_flanking_scores, perform_OLS __root_dir__ = '/media/data1/encode_analysis' flank_length = 5 COUNT_TYPE = 'counts' client = MongoClient() db = client.moca_encode_tf db.encode_tf_stats.remove() for d in os.listdir(__root_dir__): results = db.tf_metadata.find({'@id': '/experiments/{}/'.format(d)}) meme_file = os.path.join(__root_dir__, d, 'moca_output', 'meme_out', 'meme.txt') centrimo_dir = os.path.join(__root_dir__, d, 'moca_output', 'centrimo_out') if not os.path.isfile(meme_file): print 'Skipping {}'.format(d) continue meme_info = read_memefile(meme_file) total_sequences = get_total_sequences(meme_file) for i in range(0, meme_info['total_motifs']): record = meme_info['motif_records'][i] max_occur = get_max_occuring_bases(record, max_count=1, count_type=COUNT_TYPE) motif_freq = [] for position in max_occur: motif_freq.append(position[0][1]) motif_freq = np.asarray(motif_freq) fimo_sample = os.path.join(os.path.dirname(meme_file), 'fimo_out_{}'.format(i+1)) fimo_random = os.path.join(os.path.dirname(meme_file), 'fimo_random_{}'.format(i+1)) if not os.path.isfile(os.path.join(fimo_sample, 'gerp.mean.txt')): db.encode_tf_stats.insert_one({ 'encode_id': d, 'motif_number': i+1, 'motif_missing_error': True}) continue motif_enrichment = meme_info['motif_occurrences']['motif{}'.format(i+1)]/total_sequences centrimo_dir = os.path.abspath(centrimo_dir) centrimo_txt = os.path.join(centrimo_dir, 'centrimo.txt') enrichment_info = get_center_enrichment(centrimo_txt, i+1) center_enrichment = enrichment_info['enrichment'] center_enrichment_pval = enrichment_info['enrichment_pval'] motif_evalue = get_motif_evalue(record) motif_logo = os.path.join(os.path.dirname(meme_file), 'logo{}.png'.format(i+1)) motif_logorc = os.path.join(os.path.dirname(meme_file), 'logo_rc{}.png'.format(i+1)) motif_logo_encoded = encode_image(motif_logo) motif_logorc_encoded = encode_image(motif_logorc) moca_plot = os.path.join(os.path.dirname(meme_file), 'moca_plots', 'moca{}.png'.format(i+1)) moca_plotrc = os.path.join(os.path.dirname(meme_file), 'moca_plots', 'moca{}_rc.png'.format(i+1)) moca_plot_encoded = encode_image(moca_plot) moca_plotrc_encoded = encode_image(moca_plot) if os.stat(os.path.join(fimo_sample, 'gerp.mean.txt')).st_size == 0: db.encode_tf_stats.insert_one({ 'encode_id': d, 'motif_number': i+1, 'center_enrichment': center_enrichment, 'center_enrichment_pval': center_enrichment_pval, 'motif_evalue': motif_evalue, 'motif_enrichment': motif_enrichment, 'motif_logo': motif_logo_encoded, 'motif_logorc': motif_logorc_encoded, 'moca_plot': moca_plot_encoded, 'moca_plotrc': moca_plotrc_encoded, 'no_fimo_hit_sample': True}) continue gerp_mean_sample = np.loadtxt(os.path.join(fimo_sample, 'gerp.mean.txt')).tolist() phylop_mean_sample = np.loadtxt(os.path.join(fimo_sample, 'phylop.mean.txt')).tolist() delta_phylop_ttest = perform_t_test(remove_flanking_scores(phylop_mean_sample, flank_length), get_flanking_scores(phylop_mean_sample, flank_length)) p_delta_phylop = delta_phylop_ttest['one_sided_pval'] delta_phylop = delta_phylop_ttest['delta'] delta_gerp_ttest = perform_t_test(remove_flanking_scores(gerp_mean_sample, flank_length), get_flanking_scores(gerp_mean_sample, flank_length)) p_delta_gerp = delta_gerp_ttest['one_sided_pval'] delta_gerp = delta_gerp_ttest['delta'] phylop_sample_ols = perform_OLS(remove_flanking_scores(phylop_mean_sample, flank_length), motif_freq) gerp_sample_ols = perform_OLS(remove_flanking_scores(gerp_mean_sample, flank_length), motif_freq) phylop_sample_fit = phylop_sample_ols['regression_fit'] gerp_sample_fit = gerp_sample_ols['regression_fit'] corr_phylop_sample = get_pearson_corr(motif_freq, remove_flanking_scores(phylop_mean_sample, flank_length)) corr_gerp_sample = get_pearson_corr(motif_freq, remove_flanking_scores(gerp_mean_sample, flank_length)) r_phylop_sample, r_phylop_sample_pval = corr_phylop_sample r_gerp_sample, r_gerp_sample_pval = corr_gerp_sample r2_phylop_sample = phylop_sample_fit.rsquared r2_gerp_sample = gerp_sample_fit.rsquared #gerp_raw= np.loadtxt(os.path.join(fimo_sample, 'gerp.raw.txt')) #phylop_raw = np.loadtxt(os.path.join(fimo_sample, 'phylop.raw.txt')) if os.stat(os.path.join(fimo_random, 'gerp.mean.txt')).st_size == 0: db.encode_tf_stats.insert_one({ 'encode_id': d, 'motif_number': i+1, 'center_enrichment': center_enrichment, 'center_enrichment_pval': center_enrichment_pval, 'motif_evalue': motif_evalue, 'motif_enrichment': motif_enrichment, 'phylop_mean_sample': phylop_mean_sample, 'gerp_mean_sample': gerp_mean_sample, 'r2_phylop_sample': r2_phylop_sample, 'r_phylop_sample': r_phylop_sample, 'r_phylop_sample_pval': r_phylop_sample_pval, 'r2_gerp_sample': r2_gerp_sample, 'r_gerp_sample': r_gerp_sample, 'r_gerp_sample_pval': r_gerp_sample_pval, 'motif_logo': motif_logo_encoded, 'motif_logorc': motif_logorc_encoded, 'moca_plot': moca_plot_encoded, 'moca_plotrc': moca_plotrc_encoded, 'no_fimo_hit_control': True}) continue gerp_mean_control = np.loadtxt(os.path.join(fimo_random, 'gerp.mean.txt')).tolist() phylop_mean_control = np.loadtxt(os.path.join(fimo_random, 'phylop.mean.txt')).tolist() phylop_control_ols = perform_OLS(remove_flanking_scores(phylop_mean_control, flank_length), motif_freq) gerp_control_ols = perform_OLS(remove_flanking_scores(gerp_mean_control, flank_length), motif_freq) phylop_control_fit = phylop_control_ols['regression_fit'] gerp_control_fit = gerp_control_ols['regression_fit'] corr_phylop_control = get_pearson_corr(motif_freq, remove_flanking_scores(phylop_mean_control, flank_length)) corr_gerp_control = get_pearson_corr(motif_freq, remove_flanking_scores(gerp_mean_control, flank_length)) r_phylop_control, r_phylop_control_pval = corr_phylop_control r_gerp_control, r_gerp_control_pval = corr_gerp_control r2_phylop_control = phylop_control_fit.rsquared r2_gerp_control = gerp_control_fit.rsquared db.encode_tf_stats.insert_one({ 'encode_id': d, 'motif_number': i+1, 'center_enrichment': center_enrichment, 'center_enrichment_pval': center_enrichment_pval, 'motif_evalue': motif_evalue, 'motif_enrichment': motif_enrichment, 'phylop_mean_sample': phylop_mean_sample, 'gerp_mean_sample': gerp_mean_sample, 'r2_phylop_sample': r2_phylop_sample, 'r_phylop_sample': r_phylop_sample, 'r_phylop_sample_pval': r_phylop_sample_pval, 'r2_gerp_sample': r2_gerp_sample, 'r_gerp_sample': r_gerp_sample, 'r_gerp_sample_pval': r_gerp_sample_pval, #'gerp_raw': create_binary_pickle(gerp_raw), #'phylop_raw': create_binary_pickle(phylop_raw), 'phylop_mean_control': phylop_mean_control, 'gerp_mean_control': gerp_mean_control, 'delta_phylop_over_control': delta_phylop, 'delta_phylop_pval': p_delta_phylop, 'delta_gerp_over_control': delta_gerp, 'delta_gerp_pval': p_delta_gerp, 'r2_phylop_control': r2_phylop_control, 'r_phylop_control': r_phylop_control, 'r_phylop_control_pval': r_phylop_control_pval, 'r2_gerp_control': r2_gerp_control, 'r_gerp_control_pval': r_gerp_control_pval, 'motif_logo': motif_logo_encoded, 'motif_logorc': motif_logorc_encoded, 'moca_plot': moca_plot_encoded, 'moca_plotrc': moca_plotrc_encoded, })
# 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 random from rally.common.i18n import _ from rally import exceptions from rally.plugins.openstack.services.image import image from rally.plugins.openstack.services.storage import block from rally.task import atomic from rally.task import utils as bench_utils CONF = block.CONF class CinderMixin(object): def _get_client(self): return self._clients.cinder(self.version) def _update_resource(self, resource): try: manager = getattr(resource, "manager", None) if manager: res = manager.get(resource.id) else: if isinstance(resource, block.Volume): attr = "volumes" elif isinstance(resource, block.VolumeSnapshot): attr = "volume_snapshots" elif isinstance(resource, block.VolumeBackup): attr = "backups" res = getattr(self._get_client(), attr).get(resource.id) except Exception as e: if getattr(e, "code", getattr(e, "http_status", 400)) == 404: raise exceptions.GetResourceNotFound(resource=resource) raise exceptions.GetResourceFailure(resource=resource, err=e) return res def _wait_available_volume(self, volume): return bench_utils.wait_for_status( volume, ready_statuses=["available"], update_resource=self._update_resource, timeout=CONF.benchmark.cinder_volume_create_timeout, check_interval=CONF.benchmark.cinder_volume_create_poll_interval ) def list_volumes(self, detailed=True): """List all volumes.""" aname = "cinder_v%s.list_volumes" % self.version with atomic.ActionTimer(self, aname): return self._get_client().volumes.list(detailed) def get_volume(self, volume_id): """Get target volume information.""" aname = "cinder_v%s.get_volume" % self.version with atomic.ActionTimer(self, aname): return self._get_client().volumes.get(volume_id) def delete_volume(self, volume): """Delete target volume.""" aname = "cinder_v%s.delete_volume" % self.version with atomic.ActionTimer(self, aname): self._get_client().volumes.delete(volume) bench_utils.wait_for_status( volume, ready_statuses=["deleted"], check_deletion=True, update_resource=self._update_resource, timeout=CONF.benchmark.cinder_volume_delete_timeout, check_interval=(CONF.benchmark .cinder_volume_delete_poll_interval) ) def extend_volume(self, volume, new_size): """Extend the size of the specified volume.""" if isinstance(new_size, dict): new_size = random.randint(new_size["min"], new_size["max"]) aname = "cinder_v%s.extend_volume" % self.version with atomic.ActionTimer(self, aname): self._get_client().volumes.extend(volume, new_size) return self._wait_available_volume(volume) def list_snapshots(self, detailed=True): """Get a list of all snapshots.""" aname = "cinder_v%s.list_snapshots" % self.version with atomic.ActionTimer(self, aname): return (self._get_client() .volume_snapshots.list(detailed)) def set_metadata(self, volume, sets=10, set_size=3): """Set volume metadata. :param volume: The volume to set metadata on :param sets: how many operations to perform :param set_size: number of metadata keys to set in each operation :returns: A list of keys that were set """ key = "cinder_v%s.set_%s_metadatas_%s_times" % (self.version, set_size, sets) with atomic.ActionTimer(self, key): keys = [] for i in range(sets): metadata = {} for j in range(set_size): key = self.generate_random_name() keys.append(key) metadata[key] = self.generate_random_name() self._get_client().volumes.set_metadata(volume, metadata) return keys def delete_metadata(self, volume, keys, deletes=10, delete_size=3): """Delete volume metadata keys. Note that ``len(keys)`` must be greater than or equal to ``deletes * delete_size``. :param volume: The volume to delete metadata from :param deletes: how many operations to perform :param delete_size: number of metadata keys to delete in each operation :param keys: a list of keys to choose deletion candidates from """ if len(keys) < deletes * delete_size: raise exceptions.InvalidArgumentsException( "Not enough metadata keys to delete: " "%(num_keys)s keys, but asked to delete %(num_deletes)s" % {"num_keys": len(keys), "num_deletes": deletes * delete_size}) # make a shallow copy of the list of keys so that, when we pop # from it later, we don't modify the original list. keys = list(keys) random.shuffle(keys) action_name = ("cinder_v%s.delete_%s_metadatas_%s_times" % (self.version, delete_size, deletes)) with atomic.ActionTimer(self, action_name): for i in range(deletes): to_del = keys[i * delete_size:(i + 1) * delete_size] self._get_client().volumes.delete_metadata(volume, to_del) def update_readonly_flag(self, volume, read_only): """Update the read-only access mode flag of the specified volume. :param volume: The UUID of the volume to update. :param read_only: The value to indicate whether to update volume to read-only access mode. :returns: A tuple of http Response and body """ aname = "cinder_v%s.update_readonly_flag" % self.version with atomic.ActionTimer(self, aname): return self._get_client().volumes.update_readonly_flag( volume, read_only) def upload_volume_to_image(self, volume, force=False, container_format="bare", disk_format="raw"): """Upload the given volume to image. Returns created image. :param volume: volume object :param force: flag to indicate whether to snapshot a volume even if it's attached to an instance :param container_format: container format of image. Acceptable formats: ami, ari, aki, bare, and ovf :param disk_format: disk format of image. Acceptable formats: ami, ari, aki, vhd, vmdk, raw, qcow2, vdi and iso :returns: Returns created image object """ aname = "cinder_v%s.upload_volume_to_image" % self.version with atomic.ActionTimer(self, aname): resp, img = self._get_client().volumes.upload_to_image( volume, force, self.generate_random_name(), container_format, disk_format) # NOTE (e0ne): upload_to_image changes volume status to uploading # so we need to wait until it will be available. volume = self._wait_available_volume(volume) image_id = img["os-volume_upload_image"]["image_id"] glance = image.Image(self._clients) image_inst = glance.get_image(image_id) image_inst = bench_utils.wait_for_status( image_inst, ready_statuses=["active"], update_resource=glance.get_image, timeout=CONF.benchmark.glance_image_create_timeout, check_interval=(CONF.benchmark .glance_image_create_poll_interval) ) return image_inst def create_qos(self, specs): """Create a qos specs. :param specs: A dict of key/value pairs to be set :rtype: :class:'QoSSpecs' """ aname = "cinder_v%s.create_qos" % self.version name = self.generate_random_name() with atomic.ActionTimer(self, aname): return self._get_client().qos_specs.create(name, specs) def list_qos(self, search_opts=None): """Get a list of all qos specs. :param search_opts: search options :rtype: list of :class: 'QoSpecs' """ aname = "cinder_v%s.list_qos" % self.version with atomic.ActionTimer(self, aname): return self._get_client().qos_specs.list(search_opts) def get_qos(self, qos_id): """Get a specific qos specs. :param qos_id: The ID of the :class: 'QoSSpecs' to get :rtype: :class: 'QoSSpecs' """ aname = "cinder_v%s.get_qos" % self.version with atomic.ActionTimer(self, aname): return self._get_client().qos_specs.get(qos_id) def set_qos(self, qos_id, set_specs_args): """Add/Update keys in qos specs. :param qos_id: The ID of the :class:`QoSSpecs` to get :param set_specs_args: A dict of key/value pairs to be set :rtype: class 'cinderclient.apiclient.base.DictWithMeta' {"qos_specs": set_specs_args} """ aname = "cinder_v%s.set_qos" % self.version with atomic.ActionTimer(self, aname): return self._get_client().qos_specs.set_keys(qos_id, set_specs_args) def qos_associate_type(self, qos_specs, vol_type_id): """Associate qos specs from volume type. :param qos_specs: The qos specs to be associated with :param vol_type_id: The volume type id to be associated with :returns: base on client response return True if the request has been accepted or not """ aname = "cinder_v%s.qos_associate_type" % self.version with atomic.ActionTimer(self, aname): tuple_res = self._get_client().qos_specs.associate(qos_specs, vol_type_id) return (tuple_res[0].status_code == 202) def qos_disassociate_type(self, qos_specs, vol_type_id): """Disassociate qos specs from volume type. :param qos_specs: The qos specs to be disassociated with :param vol_type_id: The volume type id to be disassociated with :returns: base on client response return True if the request has been accepted or not """ aname = "cinder_v%s.qos_disassociate_type" % self.version with atomic.ActionTimer(self, aname): tuple_res = self._get_client().qos_specs.disassociate(qos_specs, vol_type_id) return (tuple_res[0].status_code == 202) def delete_snapshot(self, snapshot): """Delete the given snapshot. Returns when the snapshot is actually deleted. :param snapshot: snapshot object """ aname = "cinder_v%s.delete_snapshot" % self.version with atomic.ActionTimer(self, aname): self._get_client().volume_snapshots.delete(snapshot) bench_utils.wait_for_status( snapshot, ready_statuses=["deleted"], check_deletion=True, update_resource=self._update_resource, timeout=CONF.benchmark.cinder_volume_delete_timeout, check_interval=(CONF.benchmark .cinder_volume_delete_poll_interval) ) def delete_backup(self, backup): """Delete the given backup. Returns when the backup is actually deleted. :param backup: backup instance """ aname = "cinder_v%s.delete_backup" % self.version with atomic.ActionTimer(self, aname): self._get_client().backups.delete(backup) bench_utils.wait_for_status( backup, ready_statuses=["deleted"], check_deletion=True, update_resource=self._update_resource, timeout=CONF.benchmark.cinder_volume_delete_timeout, check_interval=(CONF.benchmark .cinder_volume_delete_poll_interval) ) def restore_backup(self, backup_id, volume_id=None): """Restore the given backup. :param backup_id: The ID of the backup to restore. :param volume_id: The ID of the volume to restore the backup to. """ aname = "cinder_v%s.restore_backup" % self.version with atomic.ActionTimer(self, aname): restore = self._get_client().restores.restore(backup_id, volume_id) restored_volume = self._get_client().volumes.get(restore.volume_id) return self._wait_available_volume(restored_volume) def list_backups(self, detailed=True): """Return user volume backups list. :param detailed: True if detailed information about backup should be listed """ aname = "cinder_v%s.list_backups" % self.version with atomic.ActionTimer(self, aname): return self._get_client().backups.list(detailed) def list_transfers(self, detailed=True, search_opts=None): """Get a list of all volume transfers. :param detailed: If True, detailed information about transfer should be listed :param search_opts: Search options to filter out volume transfers :returns: list of :class:`VolumeTransfer` """ aname = "cinder_v%s.list_transfers" % self.version with atomic.ActionTimer(self, aname): return self._get_client().transfers.list(detailed, search_opts) def get_volume_type(self, volume_type): """get details of volume_type. :param volume_type: The ID of the :class:`VolumeType` to get :returns: :class:`VolumeType` """ aname = "cinder_v%s.get_volume_type" % self.version with atomic.ActionTimer(self, aname): return self._get_client().volume_types.get(volume_type) def delete_volume_type(self, volume_type): """delete a volume type. :param volume_type: Name or Id of the volume type :returns: base on client response return True if the request has been accepted or not """ aname = "cinder_v%s.delete_volume_type" % self.version with atomic.ActionTimer(self, aname): tuple_res = self._get_client().volume_types.delete( volume_type) return (tuple_res[0].status_code == 202) def set_volume_type_keys(self, volume_type, metadata): """Set extra specs on a volume type. :param volume_type: The :class:`VolumeType` to set extra spec on :param metadata: A dict of key/value pairs to be set :returns: extra_specs if the request has been accepted """ aname = "cinder_v%s.set_volume_type_keys" % self.version with atomic.ActionTimer(self, aname): return volume_type.set_keys(metadata) def transfer_create(self, volume_id, name=None): """Create a volume transfer. :param name: The name of created transfer :param volume_id: The ID of the volume to transfer :rtype: VolumeTransfer """ name = name or self.generate_random_name() aname = "cinder_v%s.transfer_create" % self.version with atomic.ActionTimer(self, aname): return self._get_client().transfers.create(volume_id, name=name) def transfer_accept(self, transfer_id, auth_key): """Accept a volume transfer. :param transfer_id: The ID of the transfer to accept. :param auth_key: The auth_key of the transfer. :rtype: VolumeTransfer """ aname = "cinder_v%s.transfer_accept" % self.version with atomic.ActionTimer(self, aname): return self._get_client().transfers.accept(transfer_id, auth_key) def create_encryption_type(self, volume_type, specs): """Create encryption type for a volume type. Default: admin only. :param volume_type: the volume type on which to add an encryption type :param specs: the encryption type specifications to add :return: an instance of :class: VolumeEncryptionType """ aname = "cinder_v%s.create_encryption_type" % self.version with atomic.ActionTimer(self, aname): return self._get_client().volume_encryption_types.create( volume_type, specs) def get_encryption_type(self, volume_type): """Get the volume encryption type for the specified volume type. :param volume_type: the volume type to query :return: an instance of :class: VolumeEncryptionType """ aname = "cinder_v%s.get_encryption_type" % self.version with atomic.ActionTimer(self, aname): return self._get_client().volume_encryption_types.get( volume_type) def list_encryption_type(self, search_opts=None): """List all volume encryption types. :param search_opts: Options used when search for encryption types :return: a list of :class: VolumeEncryptionType instances """ aname = "cinder_v%s.list_encryption_type" % self.version with atomic.ActionTimer(self, aname): return self._get_client().volume_encryption_types.list( search_opts) def delete_encryption_type(self, volume_type): """Delete the encryption type information for the specified volume type. :param volume_type: the volume type whose encryption type information must be deleted """ aname = "cinder_v%s.delete_encryption_type" % self.version with atomic.ActionTimer(self, aname): resp = self._get_client().volume_encryption_types.delete( volume_type) if (resp[0].status_code != 202): raise exceptions.RallyException( _("EncryptionType Deletion Failed")) def update_encryption_type(self, volume_type, specs): """Update the encryption type information for the specified volume type. :param volume_type: the volume type whose encryption type information must be updated :param specs: the encryption type specifications to update :return: an instance of :class: VolumeEncryptionType """ aname = "cinder_v%s.update_encryption_type" % self.version with atomic.ActionTimer(self, aname): return self._get_client().volume_encryption_types.update( volume_type, specs) class UnifiedCinderMixin(object): @staticmethod def _unify_backup(backup): return block.VolumeBackup(id=backup.id, name=backup.name, volume_id=backup.volume_id, status=backup.status) @staticmethod def _unify_transfer(transfer): auth_key = transfer.auth_key if hasattr(transfer, "auth_key") else None return block.VolumeTransfer(id=transfer.id, name=transfer.name, volume_id=transfer.volume_id, auth_key=auth_key) @staticmethod def _unify_qos(qos): return block.QoSSpecs(id=qos.id, name=qos.name, specs=qos.specs) @staticmethod def _unify_encryption_type(encryption_type): return block.VolumeEncryptionType( id=encryption_type.encryption_id, volume_type_id=encryption_type.volume_type_id) def delete_volume(self, volume): """Delete a volume.""" self._impl.delete_volume(volume) def set_metadata(self, volume, sets=10, set_size=3): """Update/Set a volume metadata. :param volume: The updated/setted volume. :param sets: how many operations to perform :param set_size: number of metadata keys to set in each operation :returns: A list of keys that were set """ return self._impl.set_metadata(volume, sets=sets, set_size=set_size) def delete_metadata(self, volume, keys, deletes=10, delete_size=3): """Delete volume metadata keys. Note that ``len(keys)`` must be greater than or equal to ``deletes * delete_size``. :param volume: The volume to delete metadata from :param deletes: how many operations to perform :param delete_size: number of metadata keys to delete in each operation :param keys: a list of keys to choose deletion candidates from """ self._impl.delete_metadata(volume, keys=keys, deletes=10, delete_size=3) def update_readonly_flag(self, volume, read_only): """Update the read-only access mode flag of the specified volume. :param volume: The UUID of the volume to update. :param read_only: The value to indicate whether to update volume to read-only access mode. :returns: A tuple of http Response and body """ return self._impl.update_readonly_flag(volume, read_only=read_only) def upload_volume_to_image(self, volume, force=False, container_format="bare", disk_format="raw"): """Upload the given volume to image. Returns created image. :param volume: volume object :param force: flag to indicate whether to snapshot a volume even if it's attached to an instance :param container_format: container format of image. Acceptable formats: ami, ari, aki, bare, and ovf :param disk_format: disk format of image. Acceptable formats: ami, ari, aki, vhd, vmdk, raw, qcow2, vdi and iso :returns: Returns created image object """ return self._impl.upload_volume_to_image( volume, force=force, container_format=container_format, disk_format=disk_format) def create_qos(self, specs): """Create a qos specs. :param specs: A dict of key/value pairs to be set :rtype: :class:'QoSSpecs' """ return self._unify_qos(self._impl.create_qos(specs)) def list_qos(self, search_opts=None): """Get a list of all qos specs. :param search_opts: search options :rtype: list of :class: 'QoSpecs' """ return [self._unify_qos(qos) for qos in self._impl.list_qos(search_opts)] def get_qos(self, qos_id): """Get a specific qos specs. :param qos_id: The ID of the :class: 'QoSSpecs' to get :rtype: :class: 'QoSSpecs' """ return self._unify_qos(self._impl.get_qos(qos_id)) def set_qos(self, qos, set_specs_args): """Add/Update keys in qos specs. :param qos: The instance of the :class:`QoSSpecs` to set :param set_specs_args: A dict of key/value pairs to be set :rtype: :class: 'QoSSpecs' """ self._impl.set_qos(qos.id, set_specs_args) return self._unify_qos(qos) def qos_associate_type(self, qos_specs, vol_type_id): """Associate qos specs from volume type. :param qos_specs: The qos specs to be associated with :param vol_type_id: The volume type id to be associated with """ self._impl.qos_associate_type(qos_specs, vol_type_id) return self._unify_qos(qos_specs) def qos_disassociate_type(self, qos_specs, vol_type_id): """Disassociate qos specs from volume type. :param qos_specs: The qos specs to be disassociated with :param vol_type_id: The volume type id to be disassociated with """ self._impl.qos_disassociate_type(qos_specs, vol_type_id) return self._unify_qos(qos_specs) def delete_snapshot(self, snapshot): """Delete the given backup. Returns when the backup is actually deleted. :param backup: backup instance """ self._impl.delete_snapshot(snapshot) def delete_backup(self, backup): """Delete a volume backup.""" self._impl.delete_backup(backup) def list_backups(self, detailed=True): """Return user volume backups list.""" return [self._unify_backup(backup) for backup in self._impl.list_backups(detailed=detailed)] def list_transfers(self, detailed=True, search_opts=None): """Get a list of all volume transfers. :param detailed: If True, detailed information about transfer should be listed :param search_opts: Search options to filter out volume transfers :returns: list of :class:`VolumeTransfer` """ return [self._unify_transfer(transfer) for transfer in self._impl.list_transfers( detailed=detailed, search_opts=search_opts)] def get_volume_type(self, volume_type): """get details of volume_type. :param volume_type: The ID of the :class:`VolumeType` to get :returns: :class:`VolumeType` """ return self._impl.get_volume_type(volume_type) def delete_volume_type(self, volume_type): """delete a volume type. :param volume_type: Name or Id of the volume type :returns: base on client response return True if the request has been accepted or not """ return self._impl.delete_volume_type(volume_type) def set_volume_type_keys(self, volume_type, metadata): """Set extra specs on a volume type. :param volume_type: The :class:`VolumeType` to set extra spec on :param metadata: A dict of key/value pairs to be set :returns: extra_specs if the request has been accepted """ return self._impl.set_volume_type_keys(volume_type, metadata) def transfer_create(self, volume_id, name=None): """Creates a volume transfer. :param name: The name of created transfer :param volume_id: The ID of the volume to transfer. :returns: Return the created transfer. """ return self._unify_transfer( self._impl.transfer_create(volume_id, name=name)) def transfer_accept(self, transfer_id, auth_key): """Accept a volume transfer. :param transfer_id: The ID of the transfer to accept. :param auth_key: The auth_key of the transfer. :returns: VolumeTransfer """ return self._unify_transfer( self._impl.transfer_accept(transfer_id, auth_key=auth_key)) def create_encryption_type(self, volume_type, specs): """Create encryption type for a volume type. Default: admin only. :param volume_type: the volume type on which to add an encryption type :param specs: the encryption type specifications to add :return: an instance of :class: VolumeEncryptionType """ return self._unify_encryption_type( self._impl.create_encryption_type(volume_type, specs=specs)) def get_encryption_type(self, volume_type): """Get the volume encryption type for the specified volume type. :param volume_type: the volume type to query :return: an instance of :class: VolumeEncryptionType """ return self._unify_encryption_type( self._impl.get_encryption_type(volume_type)) def list_encryption_type(self, search_opts=None): """List all volume encryption types. :param search_opts: Options used when search for encryption types :return: a list of :class: VolumeEncryptionType instances """ return [self._unify_encryption_type(encryption_type) for encryption_type in self._impl.list_encryption_type( search_opts=search_opts)] def delete_encryption_type(self, volume_type): """Delete the encryption type information for the specified volume type. :param volume_type: the volume type whose encryption type information must be deleted """ return self._impl.delete_encryption_type(volume_type) def update_encryption_type(self, volume_type, specs): """Update the encryption type information for the specified volume type. :param volume_type: the volume type whose encryption type information must be updated :param specs: the encryption type specifications to update :return: an instance of :class: VolumeEncryptionType """ return self._impl.update_encryption_type(volume_type, specs=specs)
# Copyright (C) 2018 DataArt # # 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 devicehive import NetworkError, ApiResponseError, SubscriptionError def test_save(test): test.only_admin_implementation() device_hive_api = test.device_hive_api() name = test.generate_id('n-s', test.NETWORK_ENTITY) description = '%s-description' % name network = device_hive_api.create_network(name, description) name = test.generate_id('n-s', test.NETWORK_ENTITY) description = '%s-description' % name network.name = name network.description = description network.save() network_1 = device_hive_api.get_network(network.id) network.remove() try: network.save() assert False except NetworkError: pass try: network_1.save() assert False except ApiResponseError as api_response_error: assert api_response_error.code == 404 def test_remove(test): test.only_admin_implementation() device_hive_api = test.device_hive_api() name = test.generate_id('n-r', test.NETWORK_ENTITY) description = '%s-description' % name network = device_hive_api.create_network(name, description) network_1 = device_hive_api.get_network(network.id) network.remove() assert not network.id assert not network.name assert not network.description try: network.remove() assert False except NetworkError: pass try: network_1.remove() assert False except ApiResponseError as api_response_error: assert api_response_error.code == 404 # ========================================================================== name = test.generate_id('n-r', test.NETWORK_ENTITY) description = '%s-description' % name network = device_hive_api.create_network(name, description) device_id = test.generate_id('n-r', test.DEVICE_ENTITY) device_hive_api.put_device(device_id, network_id=network.id) try: network.remove() assert False except ApiResponseError as api_response_error: assert api_response_error.code == 400 device = device_hive_api.get_device(device_id) assert device.id == device_id network.remove(force=True) try: device_hive_api.get_device(device_id) assert False except ApiResponseError as api_response_error: assert api_response_error.code == 404 def test_subscribe_insert_commands(test): test.only_admin_implementation() def init_data(handler): device_id = test.generate_id('n-s-i-c', test.DEVICE_ENTITY) network_name = test.generate_id('n-s-i-c', test.NETWORK_ENTITY) description = '%s-description' % network_name network = handler.api.create_network(network_name, description) command_names = ['%s-name-%s' % (device_id, i) for i in range(2)] device = handler.api.put_device(device_id, network_id=network.id) return device, network, command_names, [] def send_data(handler, device, command_names): for command_name in command_names: command = device.send_command(command_name) handler.data['command_ids'].append(command.id) def set_handler_data(handler, device, network, command_names, command_ids): handler.data['device'] = device handler.data['network'] = network handler.data['command_names'] = command_names handler.data['command_ids'] = command_ids def handle_connect(handler): device, network, command_names, command_ids = init_data(handler) set_handler_data(handler, device, network, command_names, command_ids) send_data(handler, device, command_names) handler.data['subscription'] = network.subscribe_insert_commands() def handle_command_insert(handler, command): assert command.id in handler.data['command_ids'] handler.data['command_ids'].remove(command.id) if handler.data['command_ids']: return handler.data['subscription'].remove() handler.data['device'].remove() handler.data['network'].remove() handler.disconnect() test.run(handle_connect, handle_command_insert) def handle_connect(handler): device, network, command_names, command_ids = init_data(handler) command_name = command_names[:1] set_handler_data(handler, device, network, command_names, command_ids) send_data(handler, device, command_name) handler.data['subscription'] = network.subscribe_insert_commands( names=command_name) def handle_command_insert(handler, command): assert command.id == handler.data['command_ids'][0] handler.data['subscription'].remove() handler.data['device'].remove() handler.data['network'].remove() handler.disconnect() test.run(handle_connect, handle_command_insert) def handle_connect(handler): network_name = test.generate_id('n-s-i-c', test.NETWORK_ENTITY) description = '%s-description' % network_name network = handler.api.create_network(network_name, description) device_id = test.generate_id('n-s-i-c', test.DEVICE_ENTITY) device = handler.api.put_device(device_id, network_id=network.id) command_name = '%s-name-1' % device_id command = device.send_command(command_name) set_handler_data(handler, device, network, [command_name], [command.id]) handler.data['subscription'] = network.subscribe_insert_commands() def handle_command_insert(handler, command): assert command.id == handler.data['command_ids'][0] handler.data['subscription'].remove() handler.data['device'].remove() handler.data['network'].remove() handler.disconnect() test.run(handle_connect, handle_command_insert) def handle_connect(handler): network_name = test.generate_id('n-s-i-c', test.NETWORK_ENTITY) description = '%s-description' % network_name network = handler.api.create_network(network_name, description) network_1 = handler.api.get_network(network.id) network.remove() try: network_1.subscribe_insert_commands() assert False except ApiResponseError as api_response_error: assert api_response_error.code == 404 test.run(handle_connect) def test_unsubscribe_insert_commands(test): test.only_admin_implementation() def handle_connect(handler): name = test.generate_id('n-u-i-c', test.NETWORK_ENTITY) description = '%s-description' % name network = handler.api.create_network(name, description) subscription = network.subscribe_insert_commands() subscription.remove() try: subscription.remove() assert False except SubscriptionError: pass network.remove() test.run(handle_connect) def test_subscribe_update_commands(test): test.only_admin_implementation() def init_data(handler): device_id = test.generate_id('n-s-u-c', test.DEVICE_ENTITY) network_name = test.generate_id('n-s-u-c', test.NETWORK_ENTITY) description = '%s-description' % network_name network = handler.api.create_network(network_name, description) command_names = ['%s-name-%s' % (device_id, i) for i in range(2)] device = handler.api.put_device(device_id, network_id=network.id) return device, network, command_names, [] def send_data(handler, device, command_names): for command_name in command_names: command = device.send_command(command_name) handler.data['command_ids'].append(command.id) command.status = 'status' command.save() def set_handler_data(handler, device, network, command_names, command_ids): handler.data['device'] = device handler.data['network'] = network handler.data['command_names'] = command_names handler.data['command_ids'] = command_ids def handle_connect(handler): device, network, command_names, command_ids = init_data(handler) set_handler_data(handler, device, network, command_names, command_ids) send_data(handler, device, command_names) handler.data['subscription'] = network.subscribe_update_commands() def handle_command_update(handler, command): assert command.id in handler.data['command_ids'] handler.data['command_ids'].remove(command.id) if handler.data['command_ids']: return handler.data['subscription'].remove() handler.data['device'].remove() handler.data['network'].remove() handler.disconnect() test.run(handle_connect, handle_command_update=handle_command_update) def handle_connect(handler): device, network, command_names, command_ids = init_data(handler) command_name = command_names[:1] set_handler_data(handler, device, network, command_names, command_ids) send_data(handler, device, command_name) handler.data['subscription'] = network.subscribe_update_commands( names=command_name) def handle_command_update(handler, command): assert command.id == handler.data['command_ids'][0] handler.data['subscription'].remove() handler.data['device'].remove() handler.data['network'].remove() handler.disconnect() test.run(handle_connect, handle_command_update=handle_command_update) def handle_connect(handler): network_name = test.generate_id('n-s-u-c', test.NETWORK_ENTITY) description = '%s-description' % network_name network = handler.api.create_network(network_name, description) device_id = test.generate_id('n-s-u-c', test.DEVICE_ENTITY) device = handler.api.put_device(device_id, network_id=network.id) command_name = '%s-name-1' % device_id command = device.send_command(command_name) command.status = 'status' command.save() set_handler_data(handler, device, network, [command_name], [command.id]) handler.data['subscription'] = network.subscribe_update_commands() def handle_command_update(handler, command): assert command.id == handler.data['command_ids'][0] handler.data['subscription'].remove() handler.data['device'].remove() handler.data['network'].remove() handler.disconnect() test.run(handle_connect, handle_command_update=handle_command_update) def handle_connect(handler): network_name = test.generate_id('n-s-u-c', test.NETWORK_ENTITY) description = '%s-description' % network_name network = handler.api.create_network(network_name, description) network_1 = handler.api.get_network(network.id) network.remove() try: network_1.subscribe_update_commands() assert False except ApiResponseError as api_response_error: assert api_response_error.code == 404 test.run(handle_connect) def test_unsubscribe_update_commands(test): test.only_admin_implementation() def handle_connect(handler): name = test.generate_id('n-u-u-c', test.NETWORK_ENTITY) description = '%s-description' % name network = handler.api.create_network(name, description) subscription = network.subscribe_update_commands() subscription.remove() try: subscription.remove() assert False except SubscriptionError: pass network.remove() test.run(handle_connect) def test_subscribe_notifications(test): test.only_admin_implementation() def init_data(handler): device_id = test.generate_id('n-s-n', test.DEVICE_ENTITY) network_name = test.generate_id('n-s-n', test.NETWORK_ENTITY) description = '%s-description' % network_name network = handler.api.create_network(network_name, description) notification_names = ['%s-name-%s' % (device_id, i) for i in range(2)] device = handler.api.put_device(device_id, network_id=network.id) return device, network, notification_names, [] def send_data(handler, device, notification_names): for notification_name in notification_names: notification = device.send_notification(notification_name) handler.data['notification_ids'].append(notification.id) def set_handler_data(handler, device, network, notification_names, notification_ids): handler.data['device'] = device handler.data['network'] = network handler.data['notification_names'] = notification_names handler.data['notification_ids'] = notification_ids def handle_connect(handler): device, network, notification_names, notification_ids = init_data( handler) set_handler_data(handler, device, network, notification_names, notification_ids) send_data(handler, device, notification_names) handler.data['subscription'] = network.subscribe_notifications() def handle_notification(handler, notification): assert notification.id in handler.data['notification_ids'] handler.data['notification_ids'].remove(notification.id) if handler.data['notification_ids']: return handler.data['subscription'].remove() handler.data['device'].remove() handler.data['network'].remove() handler.disconnect() test.run(handle_connect, handle_notification=handle_notification) def handle_connect(handler): device, network, notification_names, notification_ids = init_data( handler) notification_name = notification_names[:1] set_handler_data(handler, device, network, notification_names, notification_ids) send_data(handler, device, notification_name) handler.data['subscription'] = network.subscribe_notifications( names=notification_name) def handle_notification(handler, notification): assert notification.id == handler.data['notification_ids'][0] handler.data['subscription'].remove() handler.data['device'].remove() handler.data['network'].remove() handler.disconnect() test.run(handle_connect, handle_notification=handle_notification) def handle_connect(handler): network_name = test.generate_id('n-s-n', test.NETWORK_ENTITY) description = '%s-description' % network_name network = handler.api.create_network(network_name, description) device_id = test.generate_id('n-s-n', test.DEVICE_ENTITY) device = handler.api.put_device(device_id, network_id=network.id) notification_name = '%s-name-1' % device_id notification = device.send_notification(notification_name) set_handler_data(handler, device, network, [notification_name], [notification.id]) handler.data['subscription'] = network.subscribe_notifications() def handle_notification(handler, notification): assert notification.id == handler.data['notification_ids'][0] handler.data['subscription'].remove() handler.data['device'].remove() handler.data['network'].remove() handler.disconnect() test.run(handle_connect, handle_notification=handle_notification) def handle_connect(handler): network_name = test.generate_id('n-s-n', test.NETWORK_ENTITY) description = '%s-description' % network_name network = handler.api.create_network(network_name, description) network_1 = handler.api.get_network(network.id) network.remove() try: network_1.subscribe_notifications() assert False except ApiResponseError as api_response_error: assert api_response_error.code == 404 test.run(handle_connect) def test_unsubscribe_notifications(test): test.only_admin_implementation() def handle_connect(handler): name = test.generate_id('n-u-n', test.NETWORK_ENTITY) description = '%s-description' % name network = handler.api.create_network(name, description) subscription = network.subscribe_notifications() subscription.remove() try: subscription.remove() assert False except SubscriptionError: pass network.remove() test.run(handle_connect)
""" Created on 22/03/2013 @author: thom """ import random import math import logging from rdkit.Chem import AllChem as Chem from ULPS import Float_t import config class Kinetics2D(object): @classmethod def get_ke(cls, m, x, y): return 0.5 * m * (x * x + y * y) @classmethod def get_speed(cls, x, y): return math.sqrt(x * x + y * y) @classmethod def radial_to_xyz(cls, theta=None, r=None): """Always returns a 2-D x,y""" if theta is None: theta = random.uniform(0, 2.0 * math.pi) if r is None: r = random.uniform(0, 1) y = math.sin(theta) * r x = math.cos(theta) * r return x, y @classmethod def xyz_to_radial(cls, x, y): """Always returns a 2-D theta,r""" r = math.hypot(x, y) theta = math.atan2(y, x) return theta, r @classmethod def get_distance(cls, l1, l2): return math.sqrt(sum([(_l1 - _l2) * (_l1 - _l2) for _l1, _l2 in zip(l1, l2)])) @classmethod def get_CM_energy(cls, mols): """Return KE of Centre of Mass: _ke = 1/2mv^2, where mv for the centre of mass = sum (mi * vi) for all particles i :param mols: list of Molecule""" total_mass = sum([mol.get_mass() for mol in mols]) return cls.get_ke(total_mass, *cls.get_CM_velocity(mols)) @classmethod def get_CM_velocity(cls, mols): """Return the momentum (mdx,mdy) of the centre of mass for these particles""" cm_momentum = [0, 0] total_mass = sum([mol.get_mass() for mol in mols]) for mol in mols: cm_momentum += mol.get_velocity() * mol.get_mass() CM_velocity = cm_momentum / total_mass logging.debug("CM velocity = {}".format(CM_velocity)) return CM_velocity # for mol in mols: # cm_momentum[0] += mol.get_mass() * mol.get_velocity()[0] # cm_momentum[1] += mol.get_mass() * mol.get_velocity()[1] # return [mv / total_mass for mv in cm_momentum] @classmethod def inelastic_collision(cls, reactant_mols, product_mols, energy_delta): """Determine velocities of product molecules following a collision of reactant molecules, for between one and three product molecules. Model as a collision, followed by an explosion, meaning that the total momentum of the system is conserved - if two particles, each has equal and opposite momentum in CoM frame Assume an impulse, or force, splitting the particles apart, acting equally on each particle Then impulse J = mv2-mv1 and so momentum change will be the same for all particles Implies that for two particles, equal and opposite mv in CoM frame, and for three particles, mv arranged in equilateral triangle Post-conditions: 1. Sum in_mass = Sum out_mass - although #in_molecules ne #out_molecules 2. Vector speed and direction of CoM remains constant 3. in_KE + in_PE + in_IE = Sum out_KE + out_PE + out_IE or in_KE - delta_KE = out_KE :param reactant_mols: reactants - must have total KE > 0 :type reactant_mols: list of Molecule :param product_mols: products of reaction - must be 1, 2 or 3 products only :type product_mols: list of Molecule :param energy_delta: final KE = initial KE - energy_delta """ def total_mv(mv): totals = [0, 0] for mv_ in mv: for dim in range(len(totals)): totals[dim] += mv_[dim] return totals if len(product_mols) < 1 or len(product_mols) > 3: raise ValueError() logging.debug("reactant_mols = {}, product_mols = {}".format([Chem.MolToSmiles(mol) for mol in reactant_mols], [Chem.MolToSmiles(mol) for mol in product_mols])) in_v = [mol.get_velocity() for mol in reactant_mols] in_mass = [mol.get_mass() for mol in reactant_mols] in_mv = [[m * v_ for v_ in v] for m, v in zip(in_mass, in_v)] in_ke = sum([mol.get_kinetic_energy() for mol in reactant_mols]) in_ie = sum([mol.get_internal_energy() for mol in reactant_mols]) # Velocity of centre of mass after collision # Momentums add to zero in the CoM frame out_mass = [mol.get_mass() for mol in product_mols] cm_in_v = cls.get_CM_velocity(reactant_mols) cm_in_radial_v = cls.xyz_to_radial(*cm_in_v) # Bound energy_of_collision to above zero (rounding errors for small values) # consistent sense with that in discover_reaction - final_PE = initial_PE + energy_delta => final_KE = initial_KE - energy_delta energy_of_collision = max(0, in_ke + in_ie - energy_delta - cls.get_CM_energy(reactant_mols)) if energy_of_collision <= 0: raise ValueError out_v_in_CoM_frame = [] if len(out_mass) == 1: # One out particle is stationary in out_CoM frame IE = energy_of_collision # inelastic collision -> loss of KE -> must go to IE out_v_in_CoM_frame.append([0, 0]) elif len(out_mass) == 2: ke_in_CM_frame = random.uniform(0, energy_of_collision) IE = energy_of_collision - ke_in_CM_frame mv = math.sqrt((2.0 * ke_in_CM_frame * out_mass[0] * out_mass[1]) / (out_mass[0] + out_mass[1])) out_v_in_CoM_frame.append(cls.radial_to_xyz(cm_in_radial_v[0] + math.pi * 0.5, mv)) out_v_in_CoM_frame.append(cls.radial_to_xyz(cm_in_radial_v[0] + math.pi * 1.5, mv)) elif len(out_mass) == 3: # Sum of vector momentums = 0, and in centre of momentum frame arranged as equilateral triangle, side mv # Must then convert to velocities by dividing by particle mass, which means no longer equilateral...but unimportant, as only needed equilateral to initially arrange ke_in_CM_frame = random.uniform(0, energy_of_collision) # The energy of the collision - over and above the energy of the centre of mass, which is invariant IE = energy_of_collision - ke_in_CM_frame mv = math.sqrt((2.0 * ke_in_CM_frame * out_mass[0] * out_mass[1] * out_mass[2]) / (out_mass[0] * out_mass[1] + out_mass[1] * out_mass[2] + out_mass[0] * out_mass[2])) out_v_in_CoM_frame.append(cls.radial_to_xyz(cm_in_radial_v[0] + math.pi / 3.0, mv)) out_v_in_CoM_frame.append(cls.radial_to_xyz(cm_in_radial_v[0] - math.pi / 3.0, mv)) out_v_in_CoM_frame.append(cls.radial_to_xyz(cm_in_radial_v[0] + math.pi, mv)) # Now convert from momentums to velocities by scaling by 1/mass out_v_in_CoM_frame = [[mv_component / mass for mv_component in particle_mv] for particle_mv, mass in zip(out_v_in_CoM_frame, out_mass)] # Finally convert back from CoM frame to lab frame out_v = [[v_ + cm_v_ for v_, cm_v_ in zip(v, cm_in_v)] for v in out_v_in_CoM_frame] ######################### # Confirm post-conditions # 1. Mass assert Float_t.almost_equal(sum(in_mass), sum(out_mass)) # 2. Momentum out_mv = [[m * v_ for v_ in v] for m, v in zip(out_mass, out_v)] in_mv_total = total_mv(in_mv) out_mv_total = total_mv(out_mv) logging.debug("IN MV = {}, OUT MV = {}".format(in_mv_total, out_mv_total)) for in_, out_ in zip(in_mv_total, out_mv_total): assert Float_t.almost_equal(in_, out_) # 3. Energy out_ke = sum([cls.get_ke(m, *v) for m, v in zip(out_mass, out_v)]) logging.debug("IN_KE + IN_IE = {}+{} = {}, OUT_KE + DELTA + IE = {} + {} + {} = {}".format(in_ke, in_ie, in_ke + in_ie, out_ke, energy_delta, IE, out_ke + energy_delta + IE)) assert Float_t.almost_equal(in_ke + in_ie, out_ke + energy_delta + IE, max_diff=config.EnergyTolerance) return out_v, IE
"""Interface to a series of frequently used mirth shell commands. Project setup.py defines entry points for the functions below. """ import argparse import getpass import os import shutil import tempfile from pheme.util.config import Config from pheme.warehouse.mirth_channel_transform import TransformManager CHANNELS = ('PHEME_hl7_obx_insert', 'PHEME_http_receiver', 'PHEME_hl7_visit_insert', 'PHEME_batchfile_consumer', 'PHEME_hl7_dx_insert', 'PHEME_hl7_obr_insert', 'dump_to_disk',) class MirthShell(object): """Sets up and executes common tasks via the mirth shell We have a few common patterns, such as deploying test and production versions of the mirth channels. This manages pulling in configuration details and setting up the necessary paths to make it scriptable. """ def __init__(self): self.config = Config() self.mirth_home = self.config.get('mirth', 'mirth_home') self.mirth_system_user = self.config.get('mirth', 'mirth_system_user') def write_script(self, script_file, imports=[], exports=[],): """Generate script for mirth shell to run :param script_file: An open file handle to a writable file :param imports: The mirth channel(s) XML export to import :param exports: The list of (channel name, output path) to export. Writes the necessary mirth shell instructions to the script_file so that the file can be executed via the mirth shell. NB - this takes the liberty of relaxing the 0600 mode settings hardcoded in NamedTemporaryFile so mirth shell will have read access if running as a different user. Codetemplates are also imported or exported (if imports or exports are defined) into the same directory as the first named import / export in a file named "codetemplates.xml". """ for channel in imports: assert(os.path.exists(channel)) script_file.write("import %s force\n" % channel) for channel, output in exports: script_file.write("export %s %s\n" % (channel, output)) if imports: codetemplates = os.path.join(os.path.dirname(imports[0]), "codetemplates.xml") script_file.write("importcodetemplates %s\n" % codetemplates) script_file.write("deploy\n") script_file.write("status\n") if exports: codetemplates = os.path.join(os.path.dirname(exports[0][1]), "codetemplates.xml") script_file.write("exportcodetemplates %s\n" % codetemplates) script_file.flush() script_file.seek(0) os.chmod(script_file.name, 0644) def execute_script(self, script): "Execute the given script via the mirth shell" try: orig_dir = os.getcwd() os.chdir(self.mirth_home) sudo = '' if getpass.getuser() != self.mirth_system_user: sudo = 'sudo -H -u %s' % self.mirth_system_user command = "%s ./mccommand -s %s" %\ (sudo, script.name) print command os.system(command) except: raise finally: os.chdir(orig_dir) def transform_channels(): """Apply default transform to PHEME channels""" doc = """ Mirth channels can be easily exported in XML format. This utility provides a mechanims to alter an export for subsequent import. Useful for altering details such as database name and user authentication. NB - values defined in the project configuration file will be used unless provided as optional arguments. See `pheme.util.config.Config` """ config = Config() ap = argparse.ArgumentParser(description=doc) ap.add_argument("-d", "--database", dest="db", default=config.get('warehouse', 'database'), help="name of database (overrides " "[warehouse]database)") ap.add_argument("-u", "--user", dest="user", default=config.get('warehouse', 'database_user'), help="database user (overrides " "[warehouse]database_user)") ap.add_argument("-p", "--password", dest="password", default=config.get('warehouse', 'database_password'), help="database password (overrides [warehouse]" "database_password)") ap.add_argument("--input_dir", dest="input_dir", default=config.get('warehouse', 'input_dir'), help="filesystem directory for channel to poll " "(overrides [warehouse]input_dir)") ap.add_argument("--output_dir", dest="output_dir", default=config.get('warehouse', 'output_dir'), help="filesystem directory for channel output " "(overrides [warehouse]output_dir)") ap.add_argument("--error_dir", dest="error_dir", default=config.get('warehouse', 'error_dir'), help="filesystem directory for channel errors " "(overrides [warehouse]error_dir)") ap.add_argument("source_directory", help="directory containing source channel " "definition files") ap.add_argument("target_directory", help="directory to write transformed channel " "definition files") args = ap.parse_args() source_dir = os.path.realpath(args.source_directory) target_dir = os.path.realpath(args.target_directory) transformer = TransformManager(src=None, target_dir=target_dir, options=args) for c in CHANNELS: transformer.src = os.path.join(source_dir, '%s.xml' % c) transformer() # no transformation on codetemplates at this time - but the # importer expects the codetemplates.xml file to be in the same # directory, so copy it over. shutil.copy(os.path.join(source_dir, 'codetemplates.xml'), target_dir) def deploy_channels(): """Entry point to deploy the channels to mirth on localhost""" ap = argparse.ArgumentParser(description="deploy known PHEME channels " "and code templates to Mirth Connect") ap.add_argument("deploy_directory", help="directory containing channel definition files") args = ap.parse_args() path = os.path.realpath(args.deploy_directory) imports = [os.path.join(path, '%s.xml' % c) for c in CHANNELS] ms = MirthShell() with tempfile.NamedTemporaryFile('w') as script_file: ms.write_script(script_file, imports=imports) ms.execute_script(script_file) def export_channels(): """Entry point to export the PHEME channels to named directory""" ap = argparse.ArgumentParser(description="export known PHEME channels " "and code templates from Mirth Connect") ap.add_argument("export_directory", help="directory for exported files") args = ap.parse_args() path = os.path.realpath(args.export_directory) if not os.path.isdir(path): raise argparse.ArgumentTypeError("can't access %s" % path) exports = [] for c in CHANNELS: exports.append((c, os.path.join(path, '%s.xml' % c))) ms = MirthShell() with tempfile.NamedTemporaryFile('w') as script_file: ms.write_script(script_file, exports=exports) ms.execute_script(script_file)
#!/usr/bin/python """ Fill color Takes 3 sets of arguments --file <filename> --base R,G,B # color to replace --fill R,G,B # color to fill """ import argparse import os import shutil import random def get_args(): parser = argparse.ArgumentParser() parser.add_argument('file', type=str, help='input voxel model file') parser.add_argument('--base', nargs=3, type=int, help='base color to replace. R G B', required=True) parser.add_argument('--fill', nargs=3, type=int, help='color to replace with. R G B', required=True) args = parser.parse_args() return args def check_infile(args): if not os.path.exists(args.file): print "File %s does not exist." % (args.file,) return False return True def get_outfile(args): # get an unused filename to save backup to outfile = '%s.old' % (args.file) _outfile = outfile while os.path.exists(outfile): outfile = _outfile + str(random.randrange(10000)) return outfile def process(args): # save old file shutil.copy2(args.file, get_outfile(args)) # read file with open(args.file, 'r') as f: data = f.readlines() # split data into voxel lines and metadata metadata = [] voxels = [] found = False for line in data: if not line: continue if found: voxels.append(line.strip()) else: metadata.append(line.strip()) if 'voxels' in line: found = True # split into RGBA components voxels = [[int(c) for c in v.split()] for v in voxels] # splice in new RGBA for vox in voxels: if vox[-3:] == args.base: vox[-3:] = args.fill # reassemble string voxels = [map(str, v) for v in voxels] voxels = [' '.join(v) for v in voxels] # recombine metadata and voxel lines data = metadata + voxels # convert to string data += [''] data = '\n'.join(data) # write to file with open(args.file, 'w') as f: f.write(data) def run(): args = get_args() if not check_infile(args): return process(args) if __name__ == '__main__': run()
import os, sys, zipfile, time from modulegraph.find_modules import PY_SUFFIXES from modulegraph.modulegraph import os_listdir import macholib.util def os_path_islink(path): """ os.path.islink with zipfile support. Luckily zipfiles cannot contain symlink, therefore the implementation is trivial. """ return os.path.islink(path) def os_readlink(path): """ os.readlink with zipfile support. Luckily zipfiles cannot contain symlink, therefore the implementation is trivial. """ return os.readlink(path) def os_path_isdir(path): """ os.path.isdir that understands zipfiles. Assumes that you're checking a path the is the result of os_listdir and might give false positives otherwise. """ while path.endswith('/') and path != '/': path = path[:-1] zf, zp = path_to_zip(path) if zf is None: return os.path.isdir(zp) else: zip = zipfile.ZipFile(zf) try: info = zip.getinfo(zp) except KeyError: return True else: # Not quite true, you can store information about directories in # zipfiles, but those have a lash at the end of the filename return False def copy_resource(source, destination, dry_run=0): """ Copy a resource file into the application bundle """ if os.path.isdir(source): # XXX: This is wrong, need to call ourselves recursively if not dry_run: if not os.path.exists(destination): os.mkdir(destination) for fn in os_listdir(source): copy_resource(os.path.join(source, fn), os.path.join(destination, fn), dry_run=dry_run) else: copy_file_data(source, destination, dry_run=dry_run) def copy_file_data(source, destination, dry_run=0): zf, zp = path_to_zip(source) if zf is None: data = open(zp,'rb').read() else: data = get_zip_data(zf, zp) if not dry_run: fp = open(destination, 'wb') fp.write(data) fp.close() def get_zip_data(path_to_zip, path_in_zip): zf = zipfile.ZipFile(path_to_zip) return zf.read(path_in_zip) def path_to_zip(path): """ Returns (pathtozip, pathinzip). If path isn't in a zipfile pathtozip will be None """ orig_path = path from distutils.errors import DistutilsFileError if os.path.exists(path): return (None, path) else: rest = '' while not os.path.exists(path): path, r = os.path.split(path) if not path: raise DistutilsFileError("File doesn't exist: %s"%(orig_path,)) rest = os.path.join(r, rest) if not os.path.isfile(path): # Directory really doesn't exist raise DistutilsFileError("File doesn't exist: %s"%(orig_path,)) try: zf = zipfile.ZipFile(path) except zipfile.BadZipfile: raise DistutilsFileError("File doesn't exist: %s"%(orig_path,)) if rest.endswith('/'): rest = rest[:-1] return path, rest def get_mtime(path, mustExist=True): """ Get mtime of a path, even if it is inside a zipfile """ try: return os.stat(path).st_mtime except os.error: from distutils.errors import DistutilsFileError try: path, rest = path_to_zip(path) except DistutilsFileError: if not mustExist: return -1 raise zf = zipfile.ZipFile(path) info = zf.getinfo(rest) return time.mktime(info.date_time + (0, 0, 0)) def newer(source, target): """ distutils.dep_utils.newer with zipfile support """ msource = get_mtime(source) mtarget = get_mtime(target, mustExist=False) return msource > mtarget def is_python_package(path): """Returns whether `path` is a python package (has a __init__.py(c|o) file). """ if os_path_isdir(path): for p in os_listdir(path): if p.startswith('__init__.') and p[8:] in {'.py', '.pyc', '.pyo'}: return True return False def make_exec(path): mask = os.umask(0) os.umask(mask) os.chmod(path, os.stat(path).st_mode | (0o111 & ~mask)) def makedirs(path): if not os.path.exists(path): os.makedirs(path) def mergecopy(src, dest): return macholib.util.mergecopy(src, dest) def mergetree(src, dst, condition=None, copyfn=mergecopy): """Recursively merge a directory tree using mergecopy().""" return macholib.util.mergetree(src, dst, condition=condition, copyfn=copyfn) def move(src, dst): return macholib.util.move(src, dst) LOADER = """ def __load(): import imp, os, sys, os.path ext = %r library_path = os.environ['LIBRARYPATH'] dynload_path = os.path.join(library_path, 'lib-dynload') ext = os.path.join(dynload_path, ext) if os.path.exists(ext): mod = imp.load_dynamic(__name__, ext) else: raise ImportError(repr(ext) + " not found") __load() del __load """ def make_loader(fn): return LOADER % fn def byte_compile(py_files, optimize=0, force=0, target_dir=None, verbose=1, dry_run=0, direct=None): if direct is None: direct = (__debug__ and optimize == 0) # "Indirect" byte-compilation: write a temporary script and then # run it with the appropriate flags. if not direct: from tempfile import mktemp from distutils.util import execute, spawn script_name = mktemp(".py") if verbose: print("writing byte-compilation script '%s'" % script_name) if not dry_run: script = open(script_name, "w") script.write(""" from pluginbuilder.util import byte_compile from modulegraph.modulegraph import * files = [ """) for f in py_files: script.write(repr(f) + ",\n") script.write("]\n") script.write(""" byte_compile(files, optimize=%r, force=%r, target_dir=%r, verbose=%r, dry_run=0, direct=1) """ % (optimize, force, target_dir, verbose)) script.close() cmd = [sys.executable, script_name] if optimize == 1: cmd.insert(1, "-O") elif optimize == 2: cmd.insert(1, "-OO") spawn(cmd, verbose=verbose, dry_run=dry_run) execute(os.remove, (script_name,), "removing %s" % script_name, verbose=verbose, dry_run=dry_run) else: from py_compile import compile from distutils.dir_util import mkpath for mod in py_files: # Terminology from the py_compile module: # cfile - byte-compiled file # dfile - purported source filename (same as 'file' by default) if mod.filename == mod.identifier: cfile = os.path.basename(mod.filename) dfile = cfile + (__debug__ and 'c' or 'o') else: cfile = mod.identifier.replace('.', os.sep) if mod.packagepath: dfile = cfile + os.sep + '__init__.py' + (__debug__ and 'c' or 'o') else: dfile = cfile + '.py' + (__debug__ and 'c' or 'o') if target_dir: cfile = os.path.join(target_dir, dfile) if force or newer(mod.filename, cfile): if verbose: print("byte-compiling %s to %s" % (mod.filename, dfile)) if not dry_run: mkpath(os.path.dirname(cfile)) suffix = os.path.splitext(mod.filename)[1] if suffix in ('.py', '.pyw'): zfile, pth = path_to_zip(mod.filename) if zfile is None: compile(mod.filename, cfile, dfile) else: fn = dfile + '.py' open(fn, 'wb').write(get_zip_data(zfile, pth)) compile(mod.filename, cfile, dfile) os.unlink(fn) elif suffix in PY_SUFFIXES: # Minor problem: This will happily copy a file # <mod>.pyo to <mod>.pyc or <mod>.pyc to # <mod>.pyo, but it does seem to work. copy_file_data(mod.filename, cfile) else: raise RuntimeError \ ("Don't know how to handle %r" % mod.filename) else: if verbose: print("skipping byte-compilation of %s to %s" % \ (mod.filename, dfile)) SCMDIRS = {'CVS', '.svn', '.hg', '.git'} def skipscm(ofn): fn = os.path.basename(ofn) if fn in SCMDIRS: return False return True def iter_platform_files(path, is_platform_file=macholib.util.is_platform_file): """ Iterate over all of the platform files in a directory """ for root, dirs, files in os.walk(path): for fn in files: fn = os.path.join(root, fn) if is_platform_file(fn): yield fn def copy_tree(src, dst, preserve_mode=1, preserve_times=1, preserve_symlinks=0, update=0, verbose=0, dry_run=0, condition=None): """ Copy an entire directory tree 'src' to a new location 'dst'. Both 'src' and 'dst' must be directory names. If 'src' is not a directory, raise DistutilsFileError. If 'dst' does not exist, it is created with 'mkpath()'. The end result of the copy is that every file in 'src' is copied to 'dst', and directories under 'src' are recursively copied to 'dst'. Return the list of files that were copied or might have been copied, using their output name. The return value is unaffected by 'update' or 'dry_run': it is simply the list of all files under 'src', with the names changed to be under 'dst'. 'preserve_mode' and 'preserve_times' are the same as for 'copy_file'; note that they only apply to regular files, not to directories. If 'preserve_symlinks' is true, symlinks will be copied as symlinks (on platforms that support them!); otherwise (the default), the destination of the symlink will be copied. 'update' and 'verbose' are the same as for 'copy_file'. """ assert isinstance(src, str), repr(src) assert isinstance(dst, str), repr(dst) from distutils.dir_util import mkpath from distutils.file_util import copy_file from distutils.dep_util import newer from distutils.errors import DistutilsFileError from distutils import log if condition is None: condition = skipscm if not dry_run and not os_path_isdir(src): raise DistutilsFileError("cannot copy tree '%s': not a directory" % src) try: names = os_listdir(src) except os.error as xxx_todo_changeme: (errno, errstr) = xxx_todo_changeme.args if dry_run: names = [] else: raise DistutilsFileError("error listing files in '%s': %s" % (src, errstr)) if not dry_run: mkpath(dst) outputs = [] for n in names: src_name = os.path.join(src, n) dst_name = os.path.join(dst, n) if (condition is not None) and (not condition(src_name)): continue if preserve_symlinks and os_path_islink(src_name): link_dest = os_readlink(src_name) log.info("linking %s -> %s", dst_name, link_dest) if not dry_run: if update and not newer(src, dst_name): pass else: if os_path_islink(dst_name): os.remove(dst_name) os.symlink(link_dest, dst_name) outputs.append(dst_name) elif os_path_isdir(src_name): outputs.extend( copy_tree(src_name, dst_name, preserve_mode, preserve_times, preserve_symlinks, update, dry_run=dry_run, condition=condition)) else: copy_file(src_name, dst_name, preserve_mode, preserve_times, update, dry_run=dry_run) outputs.append(dst_name) return outputs
# Importing helper class for setting up a reachability planning problem from hpp.corbaserver.rbprm.rbprmbuilder import Builder # Importing Gepetto viewer helper class from hpp.gepetto import Viewer rootJointType = 'freeflyer' packageName = 'hpp-rbprm-corba' meshPackageName = 'hpp-rbprm-corba' # URDF file describing the trunk of the robot HyQ urdfName = 'hyq_trunk_large' # URDF files describing the reachable workspace of each limb of HyQ urdfNameRom = ['hyq_lhleg_rom','hyq_lfleg_rom','hyq_rfleg_rom','hyq_rhleg_rom'] urdfSuffix = "" srdfSuffix = "" # Creating an instance of the helper class, and loading the robot rbprmBuilder = Builder () rbprmBuilder.loadModel(urdfName, urdfNameRom, rootJointType, meshPackageName, packageName, urdfSuffix, srdfSuffix) rbprmBuilder.setJointBounds ("base_joint_xyz", [-2,5, -1, 1, 0.3, 4]) # The following lines set constraint on the valid configurations: # a configuration is valid only if all limbs can create a contact ... rbprmBuilder.setFilter(['hyq_rhleg_rom', 'hyq_lfleg_rom', 'hyq_rfleg_rom','hyq_lhleg_rom']) rbprmBuilder.setAffordanceFilter('hyq_rhleg_rom', ['Support']) rbprmBuilder.setAffordanceFilter('hyq_rfleg_rom', ['Support',]) rbprmBuilder.setAffordanceFilter('hyq_lhleg_rom', ['Support']) rbprmBuilder.setAffordanceFilter('hyq_lfleg_rom', ['Support',]) # We also bound the rotations of the torso. rbprmBuilder.boundSO3([-0.4,0.4,-3,3,-3,3]) # Creating an instance of HPP problem solver and the viewer from hpp.corbaserver.rbprm.problem_solver import ProblemSolver ps = ProblemSolver( rbprmBuilder ) r = Viewer (ps) # Setting initial and goal configurations q_init = rbprmBuilder.getCurrentConfig (); q_init [0:3] = [-2, 0, 0.63]; rbprmBuilder.setCurrentConfig (q_init); r (q_init) q_goal = q_init [::] q_goal [0:3] = [3, 0, 0.63]; r (q_goal) # Choosing a path optimizer ps.addPathOptimizer("RandomShortcut") ps.setInitialConfig (q_init) ps.addGoalConfig (q_goal) from hpp.corbaserver.affordance.affordance import AffordanceTool afftool = AffordanceTool () afftool.loadObstacleModel (packageName, "darpa", "planning", r) #~ afftool.visualiseAffordances('Support', r, [0.25, 0.5, 0.5]) # Choosing RBPRM shooter and path validation methods. # Note that the standard RRT algorithm is used. ps.client.problem.selectConFigurationShooter("RbprmShooter") ps.client.problem.selectPathValidation("RbprmPathValidation",0.05) # Solve the problem t = ps.solve () if isinstance(t, list): t = t[0]* 3600000 + t[1] * 60000 + t[2] * 1000 + t[3] # Playing the computed path from hpp.gepetto import PathPlayer pp = PathPlayer (rbprmBuilder.client.basic, r) q_far = q_init [::] q_far [0:3] = [-2, -3, 0.63]; r(q_far) for i in range(1,10): rbprmBuilder.client.basic.problem.optimizePath(i) from hpp.corbaserver import Client from hpp.corbaserver.robot import Robot as Parent class Robot (Parent): rootJointType = 'freeflyer' packageName = 'hpp-rbprm-corba' meshPackageName = 'hpp-rbprm-corba' # URDF file describing the trunk of the robot HyQ urdfName = 'hyq_trunk_large' urdfSuffix = "" srdfSuffix = "" def __init__ (self, robotName, load = True): Parent.__init__ (self, robotName, self.rootJointType, load) self.tf_root = "base_footprint" self.client.basic = Client () self.load = load #DEMO code to play root path and final contact plan cl = Client() cl.problem.selectProblem("rbprm_path") rbprmBuilder2 = Robot ("toto") ps2 = ProblemSolver( rbprmBuilder2 ) cl.problem.selectProblem("default") cl.problem.movePathToProblem(3,"rbprm_path",rbprmBuilder.getAllJointNames()) r2 = Viewer (ps2) r2(q_far)
class Stack(): def __init__(self, arg = []): self.Q1 = arg self.Q2 = [] def stack_empty(self): if len(self.Q1) == 0 and len(self.Q2) == 0: return True else: return False def push(self, x): if self.stack_empty() is True: self.Q1.append(x) elif len(self.Q1) != 0: self.Q1.append(x) else: self.Q2.append(x) def pop(self): val = None if self.stack_empty() == True: print "underflow" elif len(self.Q1) != 0: while len(self.Q1) != 0: if len(self.Q1) == 1: val = self.Q1[0] else: self.Q2.append(self.Q1[0]) self.Q1.remove(self.Q1[0]) else: while len(self.Q2) != 0: if len(self.Q2) == 1: val = self.Q2[0] else: self.Q1.append(self.Q2[0]) self.Q2.remove(self.Q2[0]) return val def show_stack(self): print "Queue 1 :", self.Q1 print "Queue 2 :", self.Q2 #----------------------- input_num = ['a', 'b', 'c'] stk = Stack(input_num) stk.pop() stk.show_stack() stk.pop() stk.show_stack() stk.push('d') stk.show_stack() stk.pop() stk.show_stack()
# -*- coding: utf-8 -*- # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. """TaskReschedule tracks rescheduled task instances.""" from sqlalchemy import Column, ForeignKeyConstraint, Index, Integer, String, asc from airflow.models.base import Base, ID_LEN from airflow.utils.db import provide_session from airflow.utils.sqlalchemy import UtcDateTime class TaskReschedule(Base): """ TaskReschedule tracks rescheduled task instances. """ __tablename__ = "task_reschedule" id = Column(Integer, primary_key=True) task_id = Column(String(ID_LEN), nullable=False) dag_id = Column(String(ID_LEN), nullable=False) execution_date = Column(UtcDateTime, nullable=False) try_number = Column(Integer, nullable=False) start_date = Column(UtcDateTime, nullable=False) end_date = Column(UtcDateTime, nullable=False) duration = Column(Integer, nullable=False) reschedule_date = Column(UtcDateTime, nullable=False) __table_args__ = ( Index('idx_task_reschedule_dag_task_date', dag_id, task_id, execution_date, unique=False), ForeignKeyConstraint([task_id, dag_id, execution_date], ['task_instance.task_id', 'task_instance.dag_id', 'task_instance.execution_date'], name='task_reschedule_dag_task_date_fkey', ondelete='CASCADE') ) def __init__(self, task, execution_date, try_number, start_date, end_date, reschedule_date): self.dag_id = task.dag_id self.task_id = task.task_id self.execution_date = execution_date self.try_number = try_number self.start_date = start_date self.end_date = end_date self.reschedule_date = reschedule_date self.duration = (self.end_date - self.start_date).total_seconds() @staticmethod @provide_session def find_for_task_instance(task_instance, session): """ Returns all task reschedules for the task instance and try number, in ascending order. :param task_instance: the task instance to find task reschedules for :type task_instance: airflow.models.TaskInstance """ TR = TaskReschedule return ( session .query(TR) .filter(TR.dag_id == task_instance.dag_id, TR.task_id == task_instance.task_id, TR.execution_date == task_instance.execution_date, TR.try_number == task_instance.try_number) .order_by(asc(TR.id)) .all() )
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models, migrations class Migration(migrations.Migration): dependencies = [ ('uwadmin', '0004_auto_20150318_0034'), ] operations = [ migrations.AddField( model_name='publishrequest', name='approved_at', field=models.DateTimeField(default=None, null=True, db_index=True, blank=True), preserve_default=True, ), migrations.AddField( model_name='publishrequest', name='source_text', field=models.ForeignKey(related_name='source_publish_requests', to='uwadmin.LangCode', null=True), preserve_default=True, ), migrations.AddField( model_name='publishrequest', name='source_version', field=models.CharField(max_length=10, blank=True), preserve_default=True, ), migrations.AlterField( model_name='publishrequest', name='language', field=models.ForeignKey(related_name='publish_requests', to='uwadmin.LangCode'), preserve_default=True, ), ]
# # 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. # """A word-counting workflow.""" from __future__ import absolute_import import argparse import logging import re from past.builtins import unicode import apache_beam as beam from apache_beam.io import ReadFromText from apache_beam.io import WriteToText from apache_beam.metrics import Metrics from apache_beam.metrics.metric import MetricsFilter from apache_beam.options.pipeline_options import PipelineOptions from apache_beam.options.pipeline_options import SetupOptions class WordExtractingDoFn(beam.DoFn): """Parse each line of input text into words.""" def __init__(self): # TODO(BEAM-6158): Revert the workaround once we can pickle super() on py3. # super(WordExtractingDoFn, self).__init__() beam.DoFn.__init__(self) self.words_counter = Metrics.counter(self.__class__, 'words') self.word_lengths_counter = Metrics.counter(self.__class__, 'word_lengths') self.word_lengths_dist = Metrics.distribution( self.__class__, 'word_len_dist') self.empty_line_counter = Metrics.counter(self.__class__, 'empty_lines') def process(self, element): """Returns an iterator over the words of this element. The element is a line of text. If the line is blank, note that, too. Args: element: the element being processed Returns: The processed element. """ text_line = element.strip() if not text_line: self.empty_line_counter.inc(1) words = re.findall(r'[\w\']+', text_line, re.UNICODE) for w in words: self.words_counter.inc() self.word_lengths_counter.inc(len(w)) self.word_lengths_dist.update(len(w)) return words def run(argv=None, save_main_session=True): """Main entry point; defines and runs the wordcount pipeline.""" parser = argparse.ArgumentParser() parser.add_argument('--input', dest='input', default='gs://dataflow-samples/shakespeare/kinglear.txt', help='Input file to process.') parser.add_argument('--output', dest='output', required=True, help='Output file to write results to.') known_args, pipeline_args = parser.parse_known_args(argv) # We use the save_main_session option because one or more DoFn's in this # workflow rely on global context (e.g., a module imported at module level). pipeline_options = PipelineOptions(pipeline_args) pipeline_options.view_as(SetupOptions).save_main_session = save_main_session p = beam.Pipeline(options=pipeline_options) # Read the text file[pattern] into a PCollection. lines = p | 'read' >> ReadFromText(known_args.input) # Count the occurrences of each word. def count_ones(word_ones): (word, ones) = word_ones return (word, sum(ones)) counts = (lines | 'split' >> (beam.ParDo(WordExtractingDoFn()) .with_output_types(unicode)) | 'pair_with_one' >> beam.Map(lambda x: (x, 1)) | 'group' >> beam.GroupByKey() | 'count' >> beam.Map(count_ones)) # Format the counts into a PCollection of strings. def format_result(word_count): (word, count) = word_count return '%s: %d' % (word, count) output = counts | 'format' >> beam.Map(format_result) # Write the output using a "Write" transform that has side effects. # pylint: disable=expression-not-assigned output | 'write' >> WriteToText(known_args.output) result = p.run() result.wait_until_finish() # Do not query metrics when creating a template which doesn't run if (not hasattr(result, 'has_job') # direct runner or result.has_job): # not just a template creation empty_lines_filter = MetricsFilter().with_name('empty_lines') query_result = result.metrics().query(empty_lines_filter) if query_result['counters']: empty_lines_counter = query_result['counters'][0] logging.info('number of empty lines: %d', empty_lines_counter.result) word_lengths_filter = MetricsFilter().with_name('word_len_dist') query_result = result.metrics().query(word_lengths_filter) if query_result['distributions']: word_lengths_dist = query_result['distributions'][0] logging.info('average word length: %d', word_lengths_dist.result.mean) if __name__ == '__main__': logging.getLogger().setLevel(logging.INFO) run()
# ##### BEGIN GPL LICENSE BLOCK ##### # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # ##### END GPL LICENSE BLOCK ##### # <pep8-80 compliant> bl_info = { "name": "BioVision Motion Capture (BVH) format", "author": "Campbell Barton", "blender": (2, 74, 0), "location": "File > Import-Export", "description": "Import-Export BVH from armature objects", "warning": "", "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/" "Scripts/Import-Export/MotionCapture_BVH", "support": 'OFFICIAL', "category": "Import-Export"} if "bpy" in locals(): import importlib if "import_bvh" in locals(): importlib.reload(import_bvh) if "export_bvh" in locals(): importlib.reload(export_bvh) import bpy from bpy.props import (StringProperty, FloatProperty, IntProperty, BoolProperty, EnumProperty, ) from bpy_extras.io_utils import (ImportHelper, ExportHelper, orientation_helper_factory, axis_conversion, ) ImportBVHOrientationHelper = orientation_helper_factory("ImportBVHOrientationHelper", axis_forward='-Z', axis_up='Y') class ImportBVH(bpy.types.Operator, ImportHelper, ImportBVHOrientationHelper): """Load a BVH motion capture file""" bl_idname = "import_anim.bvh" bl_label = "Import BVH" bl_options = {'REGISTER', 'UNDO'} filename_ext = ".bvh" filter_glob = StringProperty(default="*.bvh", options={'HIDDEN'}) target = EnumProperty(items=( ('ARMATURE', "Armature", ""), ('OBJECT', "Object", ""), ), name="Target", description="Import target type", default='ARMATURE') global_scale = FloatProperty( name="Scale", description="Scale the BVH by this value", min=0.0001, max=1000000.0, soft_min=0.001, soft_max=100.0, default=1.0, ) frame_start = IntProperty( name="Start Frame", description="Starting frame for the animation", default=1, ) use_fps_scale = BoolProperty( name="Scale FPS", description=("Scale the framerate from the BVH to " "the current scenes, otherwise each " "BVH frame maps directly to a Blender frame"), default=False, ) use_cyclic = BoolProperty( name="Loop", description="Loop the animation playback", default=False, ) rotate_mode = EnumProperty( name="Rotation", description="Rotation conversion", items=(('QUATERNION', "Quaternion", "Convert rotations to quaternions"), ('NATIVE', "Euler (Native)", ("Use the rotation order " "defined in the BVH file")), ('XYZ', "Euler (XYZ)", "Convert rotations to euler XYZ"), ('XZY', "Euler (XZY)", "Convert rotations to euler XZY"), ('YXZ', "Euler (YXZ)", "Convert rotations to euler YXZ"), ('YZX', "Euler (YZX)", "Convert rotations to euler YZX"), ('ZXY', "Euler (ZXY)", "Convert rotations to euler ZXY"), ('ZYX', "Euler (ZYX)", "Convert rotations to euler ZYX"), ), default='NATIVE', ) def execute(self, context): keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob", )) global_matrix = axis_conversion(from_forward=self.axis_forward, from_up=self.axis_up, ).to_4x4() keywords["global_matrix"] = global_matrix from . import import_bvh return import_bvh.load(self, context, **keywords) class ExportBVH(bpy.types.Operator, ExportHelper): """Save a BVH motion capture file from an armature""" bl_idname = "export_anim.bvh" bl_label = "Export BVH" filename_ext = ".bvh" filter_glob = StringProperty( default="*.bvh", options={'HIDDEN'}, ) global_scale = FloatProperty( name="Scale", description="Scale the BVH by this value", min=0.0001, max=1000000.0, soft_min=0.001, soft_max=100.0, default=1.0, ) frame_start = IntProperty( name="Start Frame", description="Starting frame to export", default=0, ) frame_end = IntProperty( name="End Frame", description="End frame to export", default=0, ) rotate_mode = EnumProperty( name="Rotation", description="Rotation conversion", items=(('NATIVE', "Euler (Native)", "Use the rotation order defined in the BVH file"), ('XYZ', "Euler (XYZ)", "Convert rotations to euler XYZ"), ('XZY', "Euler (XZY)", "Convert rotations to euler XZY"), ('YXZ', "Euler (YXZ)", "Convert rotations to euler YXZ"), ('YZX', "Euler (YZX)", "Convert rotations to euler YZX"), ('ZXY', "Euler (ZXY)", "Convert rotations to euler ZXY"), ('ZYX', "Euler (ZYX)", "Convert rotations to euler ZYX"), ), default='NATIVE', ) root_transform_only = BoolProperty( name="Root Translation Only", description="Only write out translation channels for the root bone", default=False, ) @classmethod def poll(cls, context): obj = context.object return obj and obj.type == 'ARMATURE' def invoke(self, context, event): self.frame_start = context.scene.frame_start self.frame_end = context.scene.frame_end return super().invoke(context, event) def execute(self, context): if self.frame_start == 0 and self.frame_end == 0: self.frame_start = context.scene.frame_start self.frame_end = context.scene.frame_end keywords = self.as_keywords(ignore=("check_existing", "filter_glob")) from . import export_bvh return export_bvh.save(self, context, **keywords) def menu_func_import(self, context): self.layout.operator(ImportBVH.bl_idname, text="Motion Capture (.bvh)") def menu_func_export(self, context): self.layout.operator(ExportBVH.bl_idname, text="Motion Capture (.bvh)") def register(): bpy.utils.register_module(__name__) bpy.types.INFO_MT_file_import.append(menu_func_import) bpy.types.INFO_MT_file_export.append(menu_func_export) def unregister(): bpy.utils.unregister_module(__name__) bpy.types.INFO_MT_file_import.remove(menu_func_import) bpy.types.INFO_MT_file_export.remove(menu_func_export) if __name__ == "__main__": register()
#!/usr/bin/env python # Copyright (c) 2012 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. import os import StringIO import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from testing_support.super_mox import SuperMoxTestBase from testing_support import trial_dir import gclient_utils import subprocess2 class GclientUtilBase(SuperMoxTestBase): def setUp(self): super(GclientUtilBase, self).setUp() gclient_utils.sys.stdout.flush = lambda: None self.mox.StubOutWithMock(subprocess2, 'Popen') self.mox.StubOutWithMock(subprocess2, 'communicate') class CheckCallAndFilterTestCase(GclientUtilBase): class ProcessIdMock(object): def __init__(self, test_string): self.stdout = StringIO.StringIO(test_string) self.pid = 9284 # pylint: disable=no-self-use def wait(self): return 0 def _inner(self, args, test_string): cwd = 'bleh' gclient_utils.sys.stdout.write( '________ running \'boo foo bar\' in \'bleh\'\n') for i in test_string: gclient_utils.sys.stdout.write(i) # pylint: disable=no-member subprocess2.Popen( args, cwd=cwd, stdout=subprocess2.PIPE, stderr=subprocess2.STDOUT, bufsize=0).AndReturn(self.ProcessIdMock(test_string)) os.getcwd() self.mox.ReplayAll() compiled_pattern = gclient_utils.re.compile(r'a(.*)b') line_list = [] capture_list = [] def FilterLines(line): line_list.append(line) assert isinstance(line, str), type(line) match = compiled_pattern.search(line) if match: capture_list.append(match.group(1)) gclient_utils.CheckCallAndFilterAndHeader( args, cwd=cwd, always=True, filter_fn=FilterLines) self.assertEquals(line_list, ['ahah', 'accb', 'allo', 'addb']) self.assertEquals(capture_list, ['cc', 'dd']) def testCheckCallAndFilter(self): args = ['boo', 'foo', 'bar'] test_string = 'ahah\naccb\nallo\naddb\n' self._inner(args, test_string) self.checkstdout('________ running \'boo foo bar\' in \'bleh\'\n' 'ahah\naccb\nallo\naddb\n' '________ running \'boo foo bar\' in \'bleh\'\nahah\naccb\nallo\naddb' '\n') class SplitUrlRevisionTestCase(GclientUtilBase): def testSSHUrl(self): url = "ssh://test@example.com/test.git" rev = "ac345e52dc" out_url, out_rev = gclient_utils.SplitUrlRevision(url) self.assertEquals(out_rev, None) self.assertEquals(out_url, url) out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) self.assertEquals(out_rev, rev) self.assertEquals(out_url, url) url = "ssh://example.com/test.git" out_url, out_rev = gclient_utils.SplitUrlRevision(url) self.assertEquals(out_rev, None) self.assertEquals(out_url, url) out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) self.assertEquals(out_rev, rev) self.assertEquals(out_url, url) url = "ssh://example.com/git/test.git" out_url, out_rev = gclient_utils.SplitUrlRevision(url) self.assertEquals(out_rev, None) self.assertEquals(out_url, url) out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) self.assertEquals(out_rev, rev) self.assertEquals(out_url, url) rev = "test-stable" out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) self.assertEquals(out_rev, rev) self.assertEquals(out_url, url) url = "ssh://user-name@example.com/~/test.git" out_url, out_rev = gclient_utils.SplitUrlRevision(url) self.assertEquals(out_rev, None) self.assertEquals(out_url, url) out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) self.assertEquals(out_rev, rev) self.assertEquals(out_url, url) url = "ssh://user-name@example.com/~username/test.git" out_url, out_rev = gclient_utils.SplitUrlRevision(url) self.assertEquals(out_rev, None) self.assertEquals(out_url, url) out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) self.assertEquals(out_rev, rev) self.assertEquals(out_url, url) url = "git@github.com:dart-lang/spark.git" out_url, out_rev = gclient_utils.SplitUrlRevision(url) self.assertEquals(out_rev, None) self.assertEquals(out_url, url) out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) self.assertEquals(out_rev, rev) self.assertEquals(out_url, url) def testSVNUrl(self): url = "svn://example.com/test" rev = "ac345e52dc" out_url, out_rev = gclient_utils.SplitUrlRevision(url) self.assertEquals(out_rev, None) self.assertEquals(out_url, url) out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) self.assertEquals(out_rev, rev) self.assertEquals(out_url, url) class GClientUtilsTest(trial_dir.TestCase): def testHardToDelete(self): # Use the fact that tearDown will delete the directory to make it hard to do # so. l1 = os.path.join(self.root_dir, 'l1') l2 = os.path.join(l1, 'l2') l3 = os.path.join(l2, 'l3') f3 = os.path.join(l3, 'f3') os.mkdir(l1) os.mkdir(l2) os.mkdir(l3) gclient_utils.FileWrite(f3, 'foo') os.chmod(f3, 0) os.chmod(l3, 0) os.chmod(l2, 0) os.chmod(l1, 0) def testUpgradeToHttps(self): values = [ ['', ''], [None, None], ['foo', 'https://foo'], ['http://foo', 'https://foo'], ['foo/', 'https://foo/'], ['ssh-svn://foo', 'ssh-svn://foo'], ['ssh-svn://foo/bar/', 'ssh-svn://foo/bar/'], ['codereview.chromium.org', 'https://codereview.chromium.org'], ['codereview.chromium.org/', 'https://codereview.chromium.org/'], ['http://foo:10000', 'http://foo:10000'], ['http://foo:10000/bar', 'http://foo:10000/bar'], ['foo:10000', 'http://foo:10000'], ['foo:', 'https://foo:'], ] for content, expected in values: self.assertEquals( expected, gclient_utils.UpgradeToHttps(content)) def testParseCodereviewSettingsContent(self): values = [ ['# bleh\n', {}], ['\t# foo : bar\n', {}], ['Foo:bar', {'Foo': 'bar'}], ['Foo:bar:baz\n', {'Foo': 'bar:baz'}], [' Foo : bar ', {'Foo': 'bar'}], [' Foo : bar \n', {'Foo': 'bar'}], ['a:b\n\rc:d\re:f', {'a': 'b', 'c': 'd', 'e': 'f'}], ['an_url:http://value/', {'an_url': 'http://value/'}], [ 'CODE_REVIEW_SERVER : http://r/s', {'CODE_REVIEW_SERVER': 'https://r/s'} ], ['VIEW_VC:http://r/s', {'VIEW_VC': 'https://r/s'}], ] for content, expected in values: self.assertEquals( expected, gclient_utils.ParseCodereviewSettingsContent(content)) if __name__ == '__main__': import unittest unittest.main() # vim: ts=2:sw=2:tw=80:et: