text
string
size
int64
token_count
int64
from utilities.getStore import getStore filePath = "./entities/bill/store.json" productFilePath = "./entities/product/store.json" def getProduct(name): for product in getStore(productFilePath): if product["name"] == name: return product def getProductBillsAverage(name): productPriceSummation = 0 productAmount = 0 for bill in getStore(filePath): for sell in bill: if sell["name"] == name: productAmount += 1 productPriceSummation += sell["amount"] * getProduct( name )["price"] return productPriceSummation / (productAmount if productAmount else 1) def getSellsAverage(bills): # print("The average of sells has being") allProductsPricesSummation = 0 allSellsAmount = 0 for bill in bills: allSellsAmount += 1 for sell in bill: allProductsPricesSummation = sell["amount"] * getProduct(sell["name"])["price"] print(f"The average of sells has being {allProductsPricesSummation / allSellsAmount}") def getProductsAverage(): print("The average of sells for each product") for product in getStore(productFilePath): print(f"For {product['name']} the average sells are {getProductBillsAverage(product['name'])}") def getHighestSellWithBill(bills): maximumBillIndex = 0 maximumBill = 0 for billIndex in range(len(bills)): billSummation = 0 for sell in bills[billIndex]: billSummation += sell["amount"] * getProduct( sell["name"] )["price"] if billSummation > maximumBill: maximumBill = billSummation maximumBillIndex = billIndex print(f"The highest sell is {maximumBill}") print(f"For the following bill {bills[maximumBillIndex]}") def main(): bills = getStore(filePath) getSellsAverage(bills) getProductsAverage() getHighestSellWithBill(bills)
1,974
596
#discord.pyのインポート from asyncio import sleep import discord client = discord.Client() #BOTログイン処理 @client.event async def on_ready(): print('Logged in as') print(client.user.name) print(client.user.id) print('------') await client.change_presence(game=discord.Game(name='!delchat *')) # BOT動作プログラム @client.event async def on_message(message): # 送り主がBotだった場合反応したくないので if client.user != message.author: # 削除コマンド if message.content.startswith("!delchat "): #役職比較 if discord.utils.get(message.author.roles, name="admin"): # メッセージを格納 delcmd = message.content # 入力メッセージのリスト化 delcmd_ = delcmd.split() # 入力メッセージのint化 delcmd_int = int(delcmd_[1]) # 入力メッセージの単語数 delcmd_c = len(delcmd_) if delcmd_c == 2 and delcmd_int <= 50 and delcmd_int > 1: # メッセージ取得 msgs = [msg async for msg in client.logs_from(message.channel, limit=(delcmd_int+1))] await client.delete_messages(msgs) delmsg = await client.send_message(message.channel, '削除が完了しました') await sleep(5) await client.delete_message(delmsg) else: # エラーメッセージを送ります delmsg = await client.send_message(message.channel, "コマンドが間違っています。[!delchat *] *:2~50") await sleep(5) await client.delete_message(delmsg) else: # エラーメッセージを送ります delmsg = await client.send_message(message.channel, "admin権限がありません。") await sleep(5) await client.delete_message(delmsg) client.run("***")
1,826
650
# Generated by Django 3.2.11 on 2022-01-08 03:50 from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ ('api', '0001_initial'), ] operations = [ migrations.CreateModel( name='Element', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=100)), ('description', models.TextField()), ], ), migrations.CreateModel( name='Pokemon', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=100)), ('image', models.CharField(max_length=700)), ('id_pokedex', models.IntegerField()), ('description', models.TextField()), ('id_element', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='element_pokemons', to='api.element')), ], ), ]
1,191
348
#!/usr/bin/python ########################################################################### # # Copyright 2019 Dell, 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 sys import time import json import ast import cli_client as cc from rpipe_utils import pipestr from scripts.render_cli import show_cli_output import urllib3 urllib3.disable_warnings() def generate_ipprefix_uri(args, delete): _action = "PERMIT" _mode = set_name = ge = le = _maskrange_length = _ip_prefix = '' ge_val = le_val = prefix_exits = le_exits = ge_exits = is_error = i = 0 for arg in args: if "permit" == arg: _action = "PERMIT" elif "deny" == arg: _action = "DENY" elif "prefix-list" == arg: set_name = args[i+1] if len(args) > 4: _ip_prefix = args[i+3] prefix_exits = 1 elif "ge" == arg: ge_exits = 1 ge_val = int(args[i+1]) ge = args[i+1] elif "le" == arg: le_exits = 1 le_val = int(args[i+1]) le = args[i+1] elif "ip" == arg: _mode = "IPV4" max = "32" elif "ipv6" == arg: _mode = "IPV6" max = "128" else: temp = 1 i = i + 1 if prefix_exits: _prefix, _mask = _ip_prefix.split("/") mask_val = int(_mask) if (ge_exits == 0 and le_exits == 0): _maskrange_length = "exact" elif (ge_exits == 1 and le_exits == 0): if (ge_val <= mask_val): is_error = 1 _maskrange_length = ge + ".." + max elif (ge_exits == 0 and le_exits == 1): if (mask_val > le_val): is_error = 1 _maskrange_length = _mask+".."+le else: if ((ge_val <= mask_val) or (mask_val > le_val) or (ge_val > le_val)): is_error = 1 _maskrange_length = ge+".."+le if is_error: print ("%Error: Invalid prefix range, make sure: len < ge <= le") exit(1) if delete: keypath = cc.Path('/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/prefix-sets/prefix-set={prefix_list_name}/prefixes/prefix={prefix}%2F{mask},{masklength_range}', prefix_list_name=set_name, prefix=_prefix, mask=_mask, masklength_range=_maskrange_length) body = None else: keypath = cc.Path('/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/prefix-sets') body = {"openconfig-routing-policy:prefix-sets":{"prefix-set":[{"name": set_name,"config":{"name": set_name, "mode": _mode},"prefixes":{"prefix":[{"ip-prefix": _ip_prefix,"masklength-range": _maskrange_length,"config": { "ip-prefix": _ip_prefix,"masklength-range": _maskrange_length,"openconfig-routing-policy-ext:action": _action}}]}}]}} else: keypath = cc.Path('/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/prefix-sets/prefix-set={prefix_list_name}', prefix_list_name=set_name) body = None return keypath, body def invoke(func, args): body = None aa = cc.ApiClient() if func == 'ip_prefix_create': keypath, body = generate_ipprefix_uri(args, 0) return aa.patch(keypath, body) elif func == 'ip_prefix_delete': keypath, body = generate_ipprefix_uri(args, 1) return aa.delete(keypath) elif func == 'ip_prefix_show_all': keypath = cc.Path('/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/prefix-sets') return aa.get(keypath) elif func == 'ip_prefix_show_specific': keypath = cc.Path('/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/prefix-sets/prefix-set={name}',name=args[1]) return aa.get(keypath) elif func == 'ipv6_prefix_show_all': keypath = cc.Path('/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/prefix-sets') return aa.get(keypath) elif func == 'ipv6_prefix_show_specific': keypath = cc.Path('/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/prefix-sets/prefix-set={name}',name=args[1]) return aa.get(keypath) else: return aa.cli_not_implemented(func) def run(func, args): try: response = invoke(func,args) if response.ok(): if response.content is not None: # Get Command Output api_response = response.content if api_response is None: print("Failed") return #print api_response show_cli_output(args[0], api_response) else: print response.error_message() return except Exception as e: print "%Error: " + str(e) return if __name__ == '__main__': pipestr().write(sys.argv) run(sys.argv[1], sys.argv[2:])
5,468
1,792
import os import time exec_count = 100 cmds = [] cmds.append("tar -cPzf /opt/web.tar.gz /opt/web/ /opt/soft") cmds.append("rm -f /opt/web.tar.gz") for i in range(exec_count): for cmd in cmds: if os.system(cmd) != 0: break time.sleep(1)
268
111
from .help import help_handler from .pre import pre_handler from .now import now_handler from .filter import filter_handler
123
32
import utils.fileutils as futils inp = futils.read_list("../data/day6.txt") nums_dict = dict() group_size, res_count = 0, 0 for line in inp: if line == "": # res_count += len(nums_set) for k, v in nums_dict.items(): if v == group_size: res_count += 1 nums_dict = dict() group_size = 0 continue group_size += 1 for ch in line: nums_dict[ch] = 1 + nums_dict.get(ch, 0) for k, v in nums_dict.items(): if v == group_size: res_count += 1 print("Sum of counts: {0}".format(res_count))
581
220
from . import types from . import dep from . import RuntimeModel from . import Metadata class Property(object): def __init__(self, name, description, namespace='', visible=False, readonly=False, define_name=None, long_description=None): ''' :param name: Name of the property (string) :param type: PropertyType describing the type of property :param visible: Boolean, whether the property is visible in the UI or not ''' self.name = name self.type = type self.dependencies = [] self.generation = [] self.visible = visible self.readonly = readonly self.category='' self.subcategory='' self.namespace = namespace self.id = '.'.join((str(namespace).upper(), self.name)) self.label = description self.description = long_description self.defaultvalue = '' self.transient = False self.parent = None self.mode = None self.define_name = define_name if define_name else name self.is_advanced = False self.allowedconflicts = [] self.generate_if_hidden = True def set_parent_module(self, mod): self.parent = mod # Set default category to name of module if not self.category: self.category = self.parent.name def set_namespace(self, namespace): ''' Sets the namespace on a property :param namespace: :return: ''' self.namespace = namespace self.id = '.'.join((str(namespace).upper(), self.name, 'PROP')) def set_visibility(self, visible): self.visible = visible def set_readonly(self, readonly): self.readonly = readonly def add_dependency(self, dependency): self.dependencies.append(dependency) def get_dependencies(self): return self.dependencies def generateXML(self): ''' Generate the Studio XML for this property :return: etree.Element containing the Studio XML describing the property ''' print("Not able to gen XML from base property!") print(self.name) print(self.type) return None class StringProperty(Property): ''' Property which can take on a string value ''' def __init__(self, name, description, namespace='', visible=False, readonly=False, define_name=None, long_description=None): Property.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name, long_description=long_description) self.id = '.'.join((str(namespace).upper(), self.name, 'STRING')) def set_namespace(self, namespace): self.id = '.'.join((str(namespace).upper(), self.name, 'STRING')) class ArrayProperty(Property): ''' Property which can take on an array value ''' def __init__(self, name, description, namespace='', visible=False, readonly=False, define_name=None, long_description=None): Property.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name, long_description=long_description) self.id = '.'.join((str(namespace).upper(), self.name, 'ARRAY')) def set_namespace(self, namespace): self.id = '.'.join((str(namespace).upper(), self.name, 'ARRAY')) class IntegerProperty(Property): ''' Property which can take on integer values ''' def __init__(self, name, description, min, max, default, namespace='', visible=False, readonly=False, define_name=None, long_description=None): Property.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name, long_description=long_description) self.min = int(min) self.max = int(max) self.defaultvalue = int(default) self.id = '.'.join((str(namespace).upper(), self.name, 'INT')) self.format = None def set_namespace(self, namespace): self.id = '.'.join((str(namespace).upper(), self.name, 'INT')) def set_format(self, format): self.format = format class Enum: ''' Container class for an item inside of an EnumProperty ''' def __init__(self, value, index, define_value=None, visible=True): self.value = value self.visible = visible self.index = index if define_value is not None: self.define_value = define_value else: self.define_value = value class EnumProperty(Property): ''' Property allowing a selection from a list of options ''' def __init__(self, name, description, namespace='', visible=False, readonly=False, define_name=None, long_description=None): Property.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name, long_description=long_description) self.values = {} self.id = '.'.join((str(namespace).upper(), self.name, 'ENUM')) def set_namespace(self, namespace): self.id = '.'.join((str(namespace).upper(), self.name, 'ENUM')) def add_enum(self, value, define_value=None, visible=True): ''' Add an option to the selection list :param value: String value for the option (visible in UI) :param visible: Whether or not this option will be visible in the UI :param define_value: Name which will be generated as #def value :return: None ''' self.values[len(self.values.keys())] = Enum(value, len(self.values.keys()), define_value=define_value, visible=visible) class ModeProperty(EnumProperty): def __init__(self, name, description, namespace='', visible=False, readonly=False, define_name=None, long_description=None, hide_properties=True): Property.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name, long_description=long_description) self.values = {} self.id = '.'.join((str(namespace).upper(), self.name, 'ENUM')) self.hide_properties = hide_properties class BoolProperty(EnumProperty): ''' Property allowing you to select a binary setting ''' def __init__(self, name, description, namespace='', visible=False, readonly=False, define_name=None, long_description=None): EnumProperty.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name, long_description=long_description) self.id = '.'.join((str(namespace).upper(), self.name, 'BOOL')) self.add_enum('False', define_value="0") self.add_enum('True', define_value="1") def set_namespace(self, namespace): self.id = '.'.join((str(namespace).upper(), self.name, 'BOOL')) def set_default_to_false(self): self.defaultvalue = 'False' def set_default_to_true(self): self.defaultvalue = 'True' class CheckboxProperty(Property): ''' Property allowing you to select a binary setting using a checkbox ''' def __init__(self, name, description, namespace='', visible=False, readonly=False, define_name=None): Property.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name) self.id = '.'.join((str(namespace).upper(), self.name, 'CHECKBOX')) def set_namespace(self, namespace): self.id = '.'.join((str(namespace).upper(), self.name, 'CHECKBOX')) class ModuleProperty(EnumProperty): ''' Property allowing you to select a peripheral available on the current chip ''' def __init__(self, name, description, namespace='', visible=False, readonly=False, define_name=None, long_description=None): EnumProperty.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name, long_description=long_description) self.id = '.'.join((str(namespace).upper(), self.name, 'MOD')) self.allowedModules = [] self.inherit_options = False self.define_value_prefix = '' self.owned_mode = None self.define_name_postfix = '' def set_namespace(self, namespace): self.id = '.'.join((str(namespace).upper(), self.name, 'MOD')) def add_allowed_module(self, module_namespace): ''' Adds a module 'namespace' to the allowed modules for this property :param module_namespace: regular expression for which modules can be selected by this property :return: None ''' self.allowedModules.append(module_namespace) def mask_with_module_list(self, module_name_list): ''' Updates the list of allowed modules for this property by comparing a list with the property's allowed modules. :param module_name_list: list of module names available on this part :return: ''' self.values = {} self.add_enum('None') for mod_name in module_name_list: for allowed_mod in self.allowedModules: if mod_name.rstrip('0123456789') == allowed_mod: define_value = mod_name self.add_enum(mod_name, define_value=define_value) class PinProperty(EnumProperty): ''' Property allowing you to select any GPIO pin available ''' def __init__(self, name, description, namespace='', visible=False, readonly=False, define_name=None, disabled_label=None, long_description=None): EnumProperty.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name, long_description=long_description) self.referenced_module = None self.referenced_route = None self.em4 = False self.id = '.'.join((str(namespace).upper(), self.name, 'PIN')) if not disabled_label: disabled_label = 'Disabled' self.disabled_label = disabled_label self.add_enum(disabled_label, define_value="Disabled") def set_namespace(self, namespace): self.id = '.'.join((str(namespace).upper(), self.name, 'PIN')) def mask_with_pin_list(self, pin_list): ''' Updates the available enum values with the values from pin_list :param pin_list: list of pin names available on the part :return: None ''' self.values={} self.add_enum(self.disabled_label, define_value="Disabled") for pin in pin_list: self.add_enum(pin) def set_reference_route(self, route): self.referenced_route = route def set_reference_module(self, module): self.referenced_module = module def set_reference(self, module, route): self.referenced_module = module self.referenced_route = route class PRSChannelProperty(EnumProperty): """ Property allowing you to select PRS channel available from the PRS module """ def __init__(self, name, description, channel_count, custom_name="", namespace='', visible=False, readonly=False, define_name=None, long_description=None, gpio=True): EnumProperty.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name, long_description=long_description) self.add_enum("Disabled") self.channel_count = channel_count self.custom_name = custom_name self.gpio = gpio for i in range(channel_count): self.add_enum("CH" + str(i), define_value=str(i)) class AportBusProperty(EnumProperty): """ APORT bus select """ def __init__(self, name, description, signal=None, define_name_prefix=None, define_value_prefix=None, namespace='', visible=True, readonly=False, define_name=None): EnumProperty.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name) self.signal = signal self.define_name_prefix = define_name_prefix self.define_value_prefix = define_value_prefix self.extra_enums = [] self.bus_props = {} def add_extra_enum(self, value, define_value=None): self.extra_enums.append((value, define_value)) def mask_with_bus_list(self, bus_list, superset=False): ''' Updates the list of allowed buses for this property. :param bus_list: list of buses available on this part :return: ''' self.values = {} # Find existing referenced bus property bus_property_prefix = "{}_".format(self.define_name_prefix.lower()) for bus_name in bus_list: aportname = busname_to_aportname(self.parent.name, bus_name) # Add bus to bus dropdown self.add_enum("APORT bus {}".format(bus_name), define_value="APORT{}".format(aportname)) # Add channel dropdown for bus bus_property_name = bus_property_prefix + bus_name bus_prop = self.bus_props.get(bus_property_name) if not bus_prop: bus_prop = PinProperty(bus_property_name, "Channel on APORT bus {} ({})".format(bus_name, self.signal), define_name=self.define_name_prefix, visible=False) bus_prop.set_reference(self.parent.name, "{}_{}".format(self.signal, bus_name)) bus_prop.category = self.category bus_prop.subcategory = self.subcategory self.parent.add_property(bus_prop) self.bus_props[bus_property_name] = bus_prop else: bus_prop.set_visibility(False) # Add extra values to bus dropdown for value, define_value in self.extra_enums: self.add_enum(value, define_value=define_value) class AportScanProperty(EnumProperty): scan_props = {} """ APORT scan select """ def __init__(self, name, description, define_name_prefix=None, define_value_prefix=None, namespace='', visible=True, readonly=False, define_name=None): EnumProperty.__init__(self, name, description, namespace=namespace, visible=visible, readonly=readonly, define_name=define_name) self.define_name_prefix = define_name_prefix self.define_value_prefix = define_value_prefix self.scan_mask = None self.start = 0 self.end = 0 def attach_to_scan_mask(self, scan_mask_property): self.scan_mask = scan_mask_property def set_range(self, start, end): self.start = start self.end = end def mask_with_bus_list(self, bus_list, superset=False): ''' Updates the list of allowed buses for this property. :param bus_list: list of buses available on this part :return: ''' self.values = {} if not self.scan_props.get(self.parent.name): self.scan_props[self.parent.name] = {} bus_channels = {} aports = {} updated_scan_props = [] # print(bus_list) for signal, buses in bus_list.items(): for bus_name, routes in buses.items(): aport = busname_to_aportname(self.parent.name, bus_name)[:-1] bus_name = bus_name[:-1] aports[bus_name] = aport if bus_name not in bus_channels: bus_channels[bus_name] = set() bus_channels[bus_name] = bus_channels[bus_name] | set(routes) for name, route_prop in self.scan_props[self.parent.name].items(): # Hide props by default route_prop.set_visibility(False) for bus, routes in bus_channels.items(): channels_available = [False, False, False, False] for route in sorted(routes, key=lambda r: r.number): channels_available[int(route.number / 8)] = True for i in range(4): start = i * 8 end = (i + 1) * 8 - 1 if channels_available[i]: self.add_enum("APORT bus {} channel {}-{}".format(bus, start, end), "APORT{}CH{}TO{}".format(aports[bus], start, end)) else: self.add_enum("APORT bus {} channel {}-{} (no pins available)".format(bus, start, end), "APORT{}CH{}TO{}".format(aports[bus], start, end)) if superset: for route in sorted(routes, key=lambda r: r.number): route_prop_name = "{}_{}_ch{}".format(self.name.lower().rsplit('_', 1)[0], bus, route.number) route_prop = self.scan_props[self.parent.name].get(route_prop_name) if not route_prop: route_prop = CheckboxProperty(route_prop_name, "Enable channel {}".format(route.number), namespace=self.parent.namespace, visible=False) channel_range_start = int(route.number / 8) * 8 channel_range_end = channel_range_start + 7 route_prop.category = self.category route_prop.subcategory = "APORT bus {} channel {}-{}".format(bus, channel_range_start, channel_range_end) self.scan_props[self.parent.name][route_prop_name] = route_prop self.parent.add_property(route_prop) else: for route in sorted(routes, key=lambda r: r.number): route_prop_name = "{}_{}_ch{}".format(self.name.lower().rsplit('_', 1)[0], bus, route.number) route_prop = self.scan_props[self.parent.name].get(route_prop_name) route_prop.label = "Enable channel {} ({})".format(route.number, route.padName) route_prop.set_visibility(True) updated_scan_props.append(route_prop) if not superset: return updated_scan_props class AportScanMaskProperty(IntegerProperty): """ APORT scan mask """ def __init__(self, name, description, min, max, default, namespace='', visible=False, readonly=False, define_name=None): IntegerProperty.__init__(self, name, description, min, max, default, namespace, visible, readonly, define_name) self.channel_selectors = [] self.other_scan_masks = [] self.set_format("0x{:08X}UL") self.channel_start = 0 def add_channel_selector(self, channel_selector): self.channel_selectors.append(channel_selector) class AportBondedMaskProperty(IntegerProperty): """ APORT bonded mask """ def __init__(self, name, description, min, max, default, namespace='', visible=False, readonly=False, define_name=None): IntegerProperty.__init__(self, name, description, min, max, default, namespace, visible, readonly, define_name) self.set_format("0x{:08X}UL") self.channel_start = 0 self.aport = "1" self.input_props = [] def mask_with_bus_list(self, bus_list, superset=False): ''' Updates the list of allowed buses for this property. :param bus_list: list of buses available on this part :return: ''' updated_inputs = [] bus_channels = {} if not superset: for signal, buses in bus_list.items(): for bus_name, routes in buses.items(): bus_name = bus_name[:-1] if bus_name not in bus_channels: bus_channels[bus_name] = set() bus_channels[bus_name] = bus_channels[bus_name] | set(routes) for route in sorted(bus_channels[aportname_to_busname(self.parent.name, self.aport)], key=lambda r: r.number): route_prop = self.input_props[int(route.number) % 32] route_prop.label = "Enable channel {} ({})".format(route.number, route.padName) route_prop.set_visibility(True) updated_inputs.append(route_prop) return updated_inputs class StudioModule(object): """docstring for StudioModule""" def __init__(self, basename, modules): super(StudioModule, self).__init__() self.basename = basename self.modules = {} for m in modules: # Allow both lists of frameworks and single framework to be specified if isinstance(m.frameworks,list): for framework in m.frameworks: self.modules[framework.value] = m.name else: self.modules[m.frameworks.value] = m.name def getModuleId(self, framework): print ("%s: %s" % (self.basename, self.modules.keys())) if framework not in self.modules: return None return "%s.%s" % (self.basename, self.modules[framework]) def __str__(self): return self.basename class Module(object): ''' Class for describing a HALConfig module or device peripheral. A module is basically a collection of properties. ''' def __init__(self, name, core=False, visible=False, namespace=None): # Name is a required argument self.name = name self.displayname = name # namespace defaults to module base (i.e. module without the instance number) if namespace: self.namespace = namespace else: self.namespace = name.rstrip('0123456789') # No description by default self.description = "" # Core signifies a module contributed by the die self.core = core # Visible controls whether the module shows up in the UI self.visible = visible # List of properties on this module self.properties = [] # Category is the category where to put the module on the UI. Default for core is 'Core'. self.category = ' Peripherals' if core else " HAL" # Define generated with the module being active (selected) or not self.enabled_define = None # Compatibility of module self.compatibility = dep.Dependency() # Association with an on-chip peripheral self.peripheral = None # Studio module specifier self.studio_module = None # By default, module has no custom name property self.has_custom_name = False self.model = None self.family = None # Contribute 'standard' properties for every module, allowing SDKs to take control in a hwconf doc # (even though they shouldn't) inuse = BoolProperty('usedbysdk', 'SDK is taking control over this module', visible=False) self.add_property(inuse) hidden = BoolProperty('hiddenbysdk', 'SDK is hiding this module', visible=False) self.add_property(hidden) hidden = BoolProperty('showadvanced', 'Show advanced options', visible=False) self.add_property(hidden) forceenable = BoolProperty('forceenable', 'Forcefully enabled in model', visible=False) self.add_property(forceenable) owner = StringProperty('owner', 'Owned by', visible=True, readonly=True) owner.transient = True self.add_property(owner) if self.core and (self.namespace != self.name): # Add custom name property if this is a parameterized core module (e.g. USARTn, TIMERn...) customname = StringProperty('customname', 'Custom name', visible=True, readonly=False) self.add_property(customname) self.has_custom_name = True def __str__(self): if self.studio_module: return str(self.studio_module) return "none" def add_property(self, prop): ''' Add a property to this module :type prop: Property :param prop: property to add :return: None ''' # Regular list append for now # TODO: change to property merge on properties with same ID prop.set_namespace(self.namespace) prop.set_parent_module(self) self.properties.append(prop) def load_halconfig_model(self, available_module_names_list, family=None): ''' Load a HAL config model :param model: a HAL config model :param family: a halconfig_dependency Family object describing for which family this module is loaded or str containing family name :return: None ''' if not family: raise ValueError("Family is not set") if isinstance(family, str): self.family = dep.Family(family_str=family) else: self.family = family self.family.available_mods = available_module_names_list if hasattr(self.model, 'compatibility'): self.compatibility = self.model.compatibility if hasattr(self.model, "peripheral"): self.peripheral = self.model.peripheral if hasattr(self.model, "category"): self.category = self.model.category if hasattr(self.model, "displayname"): self.displayname = self.model.displayname if hasattr(self.model, "description"): self.description = self.model.description if hasattr(self.model, "studio_module"): self.studio_module = StudioModule(self.model.studio_module["basename"], \ self.model.studio_module["modules"]) if hasattr(self.model, 'modes'): mode_prop = ModeProperty(self.model.modes["define"], "mode", visible=True, hide_properties=self.model.modes.get('hide_properties', True)) for val in self.model.modes["values"]: if isinstance(val, types.EnumValue): if val.dependency: if val.dependency.applies_to(family=self.family): mode_prop.add_enum(val.display_name, define_value=val.define_value) else: mode_prop.add_enum(val.display_name, define_value=val.define_value) else: mode_prop.add_enum(val) self.add_property(mode_prop) if hasattr(self.model, 'enable'): self.enabled_define = self.model.enable["define"] for prop, options in self.model.options.items(): current_opt_set = None # If one property has several option elements, iterate to find which option element has the correct dependency if isinstance(options, list): for opt in options: # Skip documentation option if opt.get("documentation"): continue if opt.get("dependency"): if opt.get("dependency").applies_to_family(self.family): if opt.get("dependency").applies_to_module(self.name): current_opt_set = opt break else: if options.get("dependency"): if options.get("dependency").applies_to_family(self.family): if options.get("dependency").applies_to_module(self.name): current_opt_set = options else: current_opt_set = options if current_opt_set is not None: self._load_halconfig_property(prop, current_opt_set, self.family, self.model) self.post_load() def _load_halconfig_property(self, prop, opts, family, model): """ :param prop: a HAL config property :param opts: dictionary containing a set of options for current prop :param family: a halconfig_dependency Family object describing for which family this module is loaded :return: None """ prop_obj = None extra_properties = [] if opts['type'] == 'enable': self.enabled_define = prop elif opts['type'] == 'boolean': prop_obj = BoolProperty(prop, opts['description'], visible=True) elif opts['type'] == 'integer': prop_obj = IntegerProperty(prop, opts['description'], opts['min'], opts['max'], 0, visible=True) elif isinstance(opts['type'], str) and 'int' in opts['type'] and '_t' in opts['type']: prop_obj = IntegerProperty(prop, opts['description'], opts['min'], opts['max'], 0, visible=True) elif opts['type'] == 'string': prop_obj = StringProperty(prop, opts['description'], visible=True) elif opts['type'] == 'array': prop_obj = ArrayProperty(prop, opts['description'], visible=True) elif opts['type'] == 'enum': if opts['values']: prop_obj = EnumProperty(prop, opts['description'], visible=True) for val in opts['values']: if isinstance(val, types.EnumValue): if val.dependency: if val.dependency.applies_to_family(family=family): prop_obj.add_enum(val.display_name, define_value=val.define_value) else: prop_obj.add_enum(val.display_name, define_value=val.define_value) else: prop_obj.add_enum(val) elif isinstance(opts['type'], types.Pin): prop_obj = PinProperty(prop, opts['description'], visible=True, disabled_label=opts['type'].disabled_label) if opts['type'].signal: # Pin is connected to a PORTIO signal prop_obj.set_reference(self.name, opts['type'].signal) if opts['type'].em4: prop_obj.em4 = True elif isinstance(opts['type'], types.Peripheral): prop_obj = ModuleProperty(prop, opts['description'], visible=True) for filter in opts['type'].filter: prop_obj.add_allowed_module(filter) prop_obj.inherit_options = opts['type'].inherit_options prop_obj.define_value_prefix = opts['type'].define_value_prefix prop_obj.define_name_postfix = opts['type'].define_name_postfix if hasattr(opts['type'], 'mode'): prop_obj.owned_mode = opts['type'].mode elif isinstance(opts['type'], types.PinArray): prop_obj = IntegerProperty(opts['type'].count_define, opts['description'], opts['type'].min, opts['type'].max, opts['type'].default, visible=True) init_string = "" for i in range(opts['type'].min, opts['type'].max): visible = True if i < opts['type'].default else False item_property = PinProperty(opts['type'].item_define.replace("%n", str(i)), opts['type'].item_description.replace("%n", str(i)), visible=visible) if opts.get('allowedconflicts') is not None: item_property.allowedconflicts = opts['allowedconflicts'] if visible: init_string += ("{{ {0}, {1} }}, ".format(opts['type'].item_port_define.replace("%n", str(i)), opts['type'].item_pin_define.replace("%n", str(i)))) extra_properties.append(item_property) if init_string: # Strip last comma space from default value init_string = init_string[:-2] init_property = ArrayProperty(opts['type'].init_define, "{} init".format(prop), visible=False) init_property.defaultvalue = init_string init_property.transient = True extra_properties.append(init_property) elif isinstance(opts['type'], types.PRSChannelLocation): prs_chan_count = Metadata.get_prs_chan_with_gpio_count(family.get_name()) prop_obj = PRSChannelProperty(opts['type'].define, opts['description'], prs_chan_count, custom_name=opts['type'].custom_name, gpio=opts['type'].gpio, visible=True) if dep.Dependency(platform=dep.Platform.SERIES0).applies_to_family(family): # Make PRS dropdown readonly on Series 0, since changing it will affect unrelated modules that # also use PRS. Users will have to use PORTIO view to select PRS location. readonly = True else: readonly = False if opts['type'].gpio: disabled_property = StringProperty( "prs_disabled_chn_{}_pin".format(opts['type'].custom_name if opts['type'].custom_name else ""), "PRS channel output pin", visible=True, readonly=True, long_description="No PRS channel selected") if opts.get('category') is not None: disabled_property.category = opts['category'] if opts.get('subcategory') is not None: disabled_property.subcategory = opts['subcategory'] extra_properties.append(disabled_property) for i in range(prs_chan_count): item_property = PinProperty(opts['type'].name + str(i), opts['type'].output_description.replace("%n", str(i)), visible=False, readonly=readonly, define_name=opts['type'].name) if dep.Dependency(platform=dep.Platform.SERIES2).applies_to_family(family): item_property.set_reference("PRS", "ASYNCH" + str(i)) else: item_property.set_reference("PRS", "CH" + str(i)) if opts.get('category') is not None: item_property.category = opts['category'] if opts.get('subcategory') is not None: item_property.subcategory = opts['subcategory'] extra_properties.append(item_property) elif isinstance(opts['type'], types.AportSingleChannel): obj = opts['type'] prop_obj = AportBusProperty(obj.define, opts['description'], signal=obj.signal, define_name_prefix=obj.define_name_prefix, define_value_prefix=obj.define_value_prefix) for val in obj.extra_values: if isinstance(val, types.EnumValue): if val.dependency: if val.dependency.applies_to_family(family=family): prop_obj.add_extra_enum(val.display_name, define_value=val.define_value) else: prop_obj.add_extra_enum(val.display_name, define_value=val.define_value) else: prop_obj.add_extra_enum(val) elif isinstance(opts['type'], types.AportScanMode): obj = opts['type'] prop_obj = AportScanMaskProperty(prop, opts['description'], 0, 0xFFFFFFFF, 0, visible=True, readonly=True) prop_obj.channel_start = obj.channel_start define_prefix = prop.rsplit('_', 1)[0] range_start = int(obj.channel_start / 8) for i in range(range_start, range_start + 4): start = i * 8 end = (i + 1) * 8 - 1 input_number = "{}TO{}".format(start, end) input_name = "{}_INPUT{}".format(define_prefix, input_number) input_prop = AportScanProperty(input_name, "Input {} to {}".format(start, end), define_value_prefix=obj.define_value_prefix.replace("%n", input_number)) if opts.get('mode') is not None: input_prop.mode = opts['mode'] input_prop.set_range(start, end) if opts.get('subcategory') is not None: input_prop.subcategory = opts['subcategory'] if opts.get('category') is not None: input_prop.category = opts['category'] input_prop.attach_to_scan_mask(prop_obj) prop_obj.add_channel_selector(input_prop) extra_properties.append(input_prop) for i in range(obj.channel_start, obj.channel_start + 32): pin_prop = PinProperty("{}_INPUT{}".format(define_prefix, i), "Input {}".format(i), visible=True, readonly=True) pin_prop.category = opts['category'] + " Pinout" pin_prop.transient = True extra_properties.append(pin_prop) for p in self.properties: if isinstance(p, AportScanMaskProperty): prop_obj.other_scan_masks.append(p) p.other_scan_masks.append(prop_obj) elif isinstance(opts['type'], types.AportBondedMode): obj = opts['type'] prop_obj = AportBondedMaskProperty(prop, opts['description'], 0, 0xFFFFFFFF, 0, visible=True, readonly=True) prop_obj.channel_start = obj.channel_start prop_obj.aport = obj.aport define_prefix = prop.rsplit('_', 1)[0] for i in range(obj.channel_start, obj.channel_start + 32): input_prop_name = "{}_{}_ch{}".format(prop_obj.name.lower().rsplit('_', 1)[0], aportname_to_busname(self.name, obj.aport), i % 32) input_prop = CheckboxProperty(input_prop_name, "Enable channel {}".format(i), namespace=self.namespace, visible=False) input_prop.category = opts['category'] input_prop.subcategory = opts['subcategory'] extra_properties.append(input_prop) prop_obj.input_props.append(input_prop) pin_prop = PinProperty("{}_INPUT{}".format(define_prefix, i), "Input {}".format(i), visible=True, readonly=True) pin_prop.category = opts['category'] + " Pinout" pin_prop.transient = True extra_properties.append(pin_prop) else: print("ERROR: unknown property type {} in HAL config model for {}".format(opts['type'], model.name)) if prop_obj is not None: if opts.get('mode') is not None: prop_obj.mode = opts['mode'] if opts.get('generate_if_hidden') is not None: prop_obj.generate_if_hidden = opts['generate_if_hidden'] # Hiding properties that don't belong to the default mode mode_prop = next((prop for prop in self.get_properties() if isinstance(prop, ModeProperty)), None) if mode_prop and mode_prop.hide_properties: if hasattr(prop_obj, 'mode'): if isinstance(prop_obj.mode, list): prop_obj.set_visibility(True if mode_prop.values[0].define_value in prop_obj.mode else False) elif prop_obj.mode: prop_obj.set_visibility(True if prop_obj.mode == mode_prop.values[0].define_value else False) # If _model specifically states visibility, this overrides hiding by default mode if opts.get("visible") is not None: prop_obj.set_visibility(opts['visible']) if opts.get("advanced", False): # Hide advanced properties by default prop_obj.is_advanced = opts.get("advanced", False) prop_obj.set_visibility(False) if opts.get("readonly") is not None: prop_obj.set_readonly(opts['readonly']) if opts.get('defaultValue') is not None: prop_obj.defaultvalue = opts['defaultValue'] if opts.get('overrideDefaultValue') is not None: f = family.get_name().lower() for override_for, value in opts.get('overrideDefaultValue').items(): if f.startswith(override_for.lower()): prop_obj.defaultvalue = value if opts.get('longdescription') is not None: prop_obj.description = opts['longdescription'] elif opts.get("default") is not None: prop_obj.defaultvalue = opts['default'] if opts.get('subcategory') is not None: prop_obj.subcategory = opts['subcategory'] if opts.get('category') is not None: prop_obj.category = opts['category'] if opts.get('allowedconflicts') is not None: prop_obj.allowedconflicts = opts['allowedconflicts'] self.add_property(prop_obj) for property in extra_properties: self.add_property(property) def get_property(self, name): """ Look up property on this module :param name: Regular expression needing to match the name of the property :return: Property if found, None elsewhere """ return next((x for x in self.properties if name == x.name), None) def get_properties(self): ''' :return: Collection of properties in this module ''' return self.properties def activate_runtime(self, state): # Install default hooks for prop in self.properties: if isinstance(prop, ModuleProperty): if prop.inherit_options: RuntimeModel.set_change_handler(prop, RuntimeModel.owning_module_property_callback, on_enable=True) if isinstance(prop, ModeProperty): RuntimeModel.set_change_handler(prop, RuntimeModel.module_mode_callback) if isinstance(prop, PinProperty): if prop.referenced_route is None: RuntimeModel.set_change_handler(prop, RuntimeModel.pin_selection_callback) else: RuntimeModel.configure_route_handler(prop, state) if isinstance(prop, AportBusProperty): RuntimeModel.configure_aport_single_route_handler(prop, state) if isinstance(prop, AportScanMaskProperty): RuntimeModel.configure_aport_scan(prop, state) if isinstance(prop, AportBondedMaskProperty): RuntimeModel.configure_aport_bonded(prop, state) if prop.name == "owner": RuntimeModel.set_change_handler(prop, RuntimeModel.owner_changed_callback) if prop.name == "usedbysdk": RuntimeModel.set_change_handler(prop, RuntimeModel.module_usedbysdk_callback) if prop.name == "hiddenbysdk": RuntimeModel.set_change_handler(prop, RuntimeModel.module_hiddenbysdk_callback) if prop.name == "showadvanced": RuntimeModel.set_change_handler(prop, RuntimeModel.module_showadvanced_callback) if isinstance(prop, PRSChannelProperty): RuntimeModel.set_change_handler(prop, RuntimeModel.prs_channel_changed_callback, on_enable=True) RuntimeModel.set_enable_handler(self, RuntimeModel.module_enabled_callback) # Install user hooks self.set_runtime_hooks() def set_runtime_hooks(self): """ To be overridden by the implementing HAL Config module :return: None """ pass def post_load(self): """ To be overridden by the implementing HAL Config module :return: None """ pass def get_property(mod, property_name): """ Get a property model object by searching for property name :param mod: module on which to look for the property :param property_name: name of the property :return: ExporterModel.Property (or superclass) if found, None else. """ if mod is None: return None prop = mod.get_property(property_name) return prop def override_module(module_list, old, new): """ Override a module in the module_list with another instance :param old: :param new: :return: """ if old.name != new.name: print("ERROR: Not replacing module with same module") return for k,v in enumerate(module_list): if v == old: module_list[k] = new def mask_peripheral_selectors_with_module_list(module_list, module_names): for module_name, module in module_list.items(): for property in module.properties: if isinstance(property, ModuleProperty): property.mask_with_module_list(list(module_names)) def busname_to_aportname(module_name, busname): if 'IDAC' in module_name: idx = 1 elif len(busname) > 2: idx = 0 else: idx = ord(busname[0].upper()) - 64 aportname = "{}{}".format(idx, busname[-1]) return aportname def aportname_to_busname(module_name, aportname): if len(aportname) == 2: diff = aportname[1] aportname = aportname[0] else: diff = '' if 'IDAC' in module_name: busname = 'C' elif aportname == '0': busname = module_name else: busname = chr(ord(aportname) + 16) return "{}{}".format(busname, diff) def clear(): AportScanProperty.scan_props = {}
45,165
12,260
# -*- coding: utf-8 -*- # Generated by the protocol buffer compiler. DO NOT EDIT! # source: tests/schematics_proto3_tests.proto import sys _b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) from google.protobuf.internal import enum_type_wrapper from google.protobuf import descriptor as _descriptor from google.protobuf import message as _message from google.protobuf import reflection as _reflection from google.protobuf import symbol_database as _symbol_database # @@protoc_insertion_point(imports) _sym_db = _symbol_database.Default() from google.protobuf import timestamp_pb2 as google_dot_protobuf_dot_timestamp__pb2 from google.protobuf import wrappers_pb2 as google_dot_protobuf_dot_wrappers__pb2 DESCRIPTOR = _descriptor.FileDescriptor( name='tests/schematics_proto3_tests.proto', package='schematics_proto3.tests', syntax='proto3', serialized_options=None, serialized_pb=_b('\n#tests/schematics_proto3_tests.proto\x12\x17schematics_proto3.tests\x1a\x1fgoogle/protobuf/timestamp.proto\x1a\x1egoogle/protobuf/wrappers.proto\"e\n\x06Nested\x12\x34\n\x05inner\x18\x01 \x01(\x0b\x32%.schematics_proto3.tests.Nested.Inner\x12\r\n\x05other\x18\x02 \x01(\t\x1a\x16\n\x05Inner\x12\r\n\x05value\x18\x01 \x01(\t\">\n\rWrappedDouble\x12-\n\x07wrapped\x18\x01 \x01(\x0b\x32\x1c.google.protobuf.DoubleValue\"<\n\x0cWrappedFloat\x12,\n\x07wrapped\x18\x01 \x01(\x0b\x32\x1b.google.protobuf.FloatValue\"<\n\x0cWrappedInt64\x12,\n\x07wrapped\x18\x01 \x01(\x0b\x32\x1b.google.protobuf.Int64Value\">\n\rWrappedUInt64\x12-\n\x07wrapped\x18\x01 \x01(\x0b\x32\x1c.google.protobuf.UInt64Value\"<\n\x0cWrappedInt32\x12,\n\x07wrapped\x18\x01 \x01(\x0b\x32\x1b.google.protobuf.Int32Value\">\n\rWrappedUInt32\x12-\n\x07wrapped\x18\x01 \x01(\x0b\x32\x1c.google.protobuf.UInt32Value\":\n\x0bWrappedBool\x12+\n\x07wrapped\x18\x01 \x01(\x0b\x32\x1a.google.protobuf.BoolValue\">\n\rWrappedString\x12-\n\x07wrapped\x18\x01 \x01(\x0b\x32\x1c.google.protobuf.StringValue\"<\n\x0cWrappedBytes\x12,\n\x07wrapped\x18\x01 \x01(\x0b\x32\x1b.google.protobuf.BytesValue\"6\n\tTimestamp\x12)\n\x05value\x18\x01 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\">\n\x11RepeatedTimestamp\x12)\n\x05value\x18\x01 \x03(\x0b\x32\x1a.google.protobuf.Timestamp\"w\n\x0eOneOfTimestamp\x12.\n\x06value1\x18\x01 \x01(\x0b\x32\x1c.google.protobuf.StringValueH\x00\x12,\n\x06value2\x18\x02 \x01(\x0b\x32\x1a.google.protobuf.TimestampH\x00\x42\x07\n\x05inner\"\x17\n\x06\x44ouble\x12\r\n\x05value\x18\x01 \x01(\x01\"\x16\n\x05\x46loat\x12\r\n\x05value\x18\x01 \x01(\x02\"\x16\n\x05Int64\x12\r\n\x05value\x18\x01 \x01(\x03\"\x17\n\x06UInt64\x12\r\n\x05value\x18\x01 \x01(\x04\"\x16\n\x05Int32\x12\r\n\x05value\x18\x01 \x01(\x05\"\x17\n\x06UInt32\x12\r\n\x05value\x18\x01 \x01(\r\"\x15\n\x04\x42ool\x12\r\n\x05value\x18\x01 \x01(\x08\"\x17\n\x06String\x12\r\n\x05value\x18\x01 \x01(\t\"\x16\n\x05\x42ytes\x12\r\n\x05value\x18\x01 \x01(\x0c\"\"\n\x11RepeatedPrimitive\x12\r\n\x05value\x18\x01 \x03(\t\"f\n\x0eRepeatedNested\x12<\n\x05inner\x18\x01 \x03(\x0b\x32-.schematics_proto3.tests.RepeatedNested.Inner\x1a\x16\n\x05Inner\x12\r\n\x05value\x18\x01 \x01(\t\"=\n\x0fRepeatedWrapped\x12*\n\x05value\x18\x01 \x03(\x0b\x32\x1b.google.protobuf.Int32Value\"=\n\x0eOneOfPrimitive\x12\x10\n\x06value1\x18\x01 \x01(\x04H\x00\x12\x10\n\x06value2\x18\x02 \x01(\tH\x00\x42\x07\n\x05inner\"\x9c\x01\n\x0bOneOfNested\x12<\n\x06value1\x18\x01 \x01(\x0b\x32*.schematics_proto3.tests.OneOfNested.InnerH\x00\x12.\n\x06value2\x18\x02 \x01(\x0b\x32\x1c.google.protobuf.StringValueH\x00\x1a\x16\n\x05Inner\x12\r\n\x05value\x18\x01 \x01(\tB\x07\n\x05inner\":\n\nSimpleEnum\x12,\n\x05value\x18\x01 \x01(\x0e\x32\x1d.schematics_proto3.tests.Enum\"<\n\x0cRepeatedEnum\x12,\n\x05value\x18\x01 \x03(\x0e\x32\x1d.schematics_proto3.tests.Enum\"u\n\tOneOfEnum\x12.\n\x06value1\x18\x01 \x01(\x0b\x32\x1c.google.protobuf.StringValueH\x00\x12/\n\x06value2\x18\x02 \x01(\x0e\x32\x1d.schematics_proto3.tests.EnumH\x00\x42\x07\n\x05inner**\n\x04\x45num\x12\x0b\n\x07UNKNOWN\x10\x00\x12\t\n\x05\x46IRST\x10\x01\x12\n\n\x06SECOND\x10\x02\x62\x06proto3') , dependencies=[google_dot_protobuf_dot_timestamp__pb2.DESCRIPTOR,google_dot_protobuf_dot_wrappers__pb2.DESCRIPTOR,]) _ENUM = _descriptor.EnumDescriptor( name='Enum', full_name='schematics_proto3.tests.Enum', filename=None, file=DESCRIPTOR, values=[ _descriptor.EnumValueDescriptor( name='UNKNOWN', index=0, number=0, serialized_options=None, type=None), _descriptor.EnumValueDescriptor( name='FIRST', index=1, number=1, serialized_options=None, type=None), _descriptor.EnumValueDescriptor( name='SECOND', index=2, number=2, serialized_options=None, type=None), ], containing_type=None, serialized_options=None, serialized_start=1922, serialized_end=1964, ) _sym_db.RegisterEnumDescriptor(_ENUM) Enum = enum_type_wrapper.EnumTypeWrapper(_ENUM) UNKNOWN = 0 FIRST = 1 SECOND = 2 _NESTED_INNER = _descriptor.Descriptor( name='Inner', full_name='schematics_proto3.tests.Nested.Inner', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.Nested.Inner.value', index=0, number=1, type=9, cpp_type=9, label=1, has_default_value=False, default_value=_b("").decode('utf-8'), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=208, serialized_end=230, ) _NESTED = _descriptor.Descriptor( name='Nested', full_name='schematics_proto3.tests.Nested', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='inner', full_name='schematics_proto3.tests.Nested.inner', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='other', full_name='schematics_proto3.tests.Nested.other', index=1, number=2, type=9, cpp_type=9, label=1, has_default_value=False, default_value=_b("").decode('utf-8'), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[_NESTED_INNER, ], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=129, serialized_end=230, ) _WRAPPEDDOUBLE = _descriptor.Descriptor( name='WrappedDouble', full_name='schematics_proto3.tests.WrappedDouble', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='wrapped', full_name='schematics_proto3.tests.WrappedDouble.wrapped', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=232, serialized_end=294, ) _WRAPPEDFLOAT = _descriptor.Descriptor( name='WrappedFloat', full_name='schematics_proto3.tests.WrappedFloat', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='wrapped', full_name='schematics_proto3.tests.WrappedFloat.wrapped', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=296, serialized_end=356, ) _WRAPPEDINT64 = _descriptor.Descriptor( name='WrappedInt64', full_name='schematics_proto3.tests.WrappedInt64', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='wrapped', full_name='schematics_proto3.tests.WrappedInt64.wrapped', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=358, serialized_end=418, ) _WRAPPEDUINT64 = _descriptor.Descriptor( name='WrappedUInt64', full_name='schematics_proto3.tests.WrappedUInt64', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='wrapped', full_name='schematics_proto3.tests.WrappedUInt64.wrapped', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=420, serialized_end=482, ) _WRAPPEDINT32 = _descriptor.Descriptor( name='WrappedInt32', full_name='schematics_proto3.tests.WrappedInt32', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='wrapped', full_name='schematics_proto3.tests.WrappedInt32.wrapped', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=484, serialized_end=544, ) _WRAPPEDUINT32 = _descriptor.Descriptor( name='WrappedUInt32', full_name='schematics_proto3.tests.WrappedUInt32', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='wrapped', full_name='schematics_proto3.tests.WrappedUInt32.wrapped', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=546, serialized_end=608, ) _WRAPPEDBOOL = _descriptor.Descriptor( name='WrappedBool', full_name='schematics_proto3.tests.WrappedBool', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='wrapped', full_name='schematics_proto3.tests.WrappedBool.wrapped', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=610, serialized_end=668, ) _WRAPPEDSTRING = _descriptor.Descriptor( name='WrappedString', full_name='schematics_proto3.tests.WrappedString', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='wrapped', full_name='schematics_proto3.tests.WrappedString.wrapped', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=670, serialized_end=732, ) _WRAPPEDBYTES = _descriptor.Descriptor( name='WrappedBytes', full_name='schematics_proto3.tests.WrappedBytes', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='wrapped', full_name='schematics_proto3.tests.WrappedBytes.wrapped', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=734, serialized_end=794, ) _TIMESTAMP = _descriptor.Descriptor( name='Timestamp', full_name='schematics_proto3.tests.Timestamp', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.Timestamp.value', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=796, serialized_end=850, ) _REPEATEDTIMESTAMP = _descriptor.Descriptor( name='RepeatedTimestamp', full_name='schematics_proto3.tests.RepeatedTimestamp', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.RepeatedTimestamp.value', index=0, number=1, type=11, cpp_type=10, label=3, has_default_value=False, default_value=[], message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=852, serialized_end=914, ) _ONEOFTIMESTAMP = _descriptor.Descriptor( name='OneOfTimestamp', full_name='schematics_proto3.tests.OneOfTimestamp', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value1', full_name='schematics_proto3.tests.OneOfTimestamp.value1', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='value2', full_name='schematics_proto3.tests.OneOfTimestamp.value2', index=1, number=2, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ _descriptor.OneofDescriptor( name='inner', full_name='schematics_proto3.tests.OneOfTimestamp.inner', index=0, containing_type=None, fields=[]), ], serialized_start=916, serialized_end=1035, ) _DOUBLE = _descriptor.Descriptor( name='Double', full_name='schematics_proto3.tests.Double', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.Double.value', index=0, number=1, type=1, cpp_type=5, label=1, has_default_value=False, default_value=float(0), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1037, serialized_end=1060, ) _FLOAT = _descriptor.Descriptor( name='Float', full_name='schematics_proto3.tests.Float', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.Float.value', index=0, number=1, type=2, cpp_type=6, label=1, has_default_value=False, default_value=float(0), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1062, serialized_end=1084, ) _INT64 = _descriptor.Descriptor( name='Int64', full_name='schematics_proto3.tests.Int64', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.Int64.value', index=0, number=1, type=3, cpp_type=2, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1086, serialized_end=1108, ) _UINT64 = _descriptor.Descriptor( name='UInt64', full_name='schematics_proto3.tests.UInt64', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.UInt64.value', index=0, number=1, type=4, cpp_type=4, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1110, serialized_end=1133, ) _INT32 = _descriptor.Descriptor( name='Int32', full_name='schematics_proto3.tests.Int32', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.Int32.value', index=0, number=1, type=5, cpp_type=1, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1135, serialized_end=1157, ) _UINT32 = _descriptor.Descriptor( name='UInt32', full_name='schematics_proto3.tests.UInt32', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.UInt32.value', index=0, number=1, type=13, cpp_type=3, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1159, serialized_end=1182, ) _BOOL = _descriptor.Descriptor( name='Bool', full_name='schematics_proto3.tests.Bool', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.Bool.value', index=0, number=1, type=8, cpp_type=7, label=1, has_default_value=False, default_value=False, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1184, serialized_end=1205, ) _STRING = _descriptor.Descriptor( name='String', full_name='schematics_proto3.tests.String', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.String.value', index=0, number=1, type=9, cpp_type=9, label=1, has_default_value=False, default_value=_b("").decode('utf-8'), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1207, serialized_end=1230, ) _BYTES = _descriptor.Descriptor( name='Bytes', full_name='schematics_proto3.tests.Bytes', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.Bytes.value', index=0, number=1, type=12, cpp_type=9, label=1, has_default_value=False, default_value=_b(""), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1232, serialized_end=1254, ) _REPEATEDPRIMITIVE = _descriptor.Descriptor( name='RepeatedPrimitive', full_name='schematics_proto3.tests.RepeatedPrimitive', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.RepeatedPrimitive.value', index=0, number=1, type=9, cpp_type=9, label=3, has_default_value=False, default_value=[], message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1256, serialized_end=1290, ) _REPEATEDNESTED_INNER = _descriptor.Descriptor( name='Inner', full_name='schematics_proto3.tests.RepeatedNested.Inner', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.RepeatedNested.Inner.value', index=0, number=1, type=9, cpp_type=9, label=1, has_default_value=False, default_value=_b("").decode('utf-8'), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=208, serialized_end=230, ) _REPEATEDNESTED = _descriptor.Descriptor( name='RepeatedNested', full_name='schematics_proto3.tests.RepeatedNested', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='inner', full_name='schematics_proto3.tests.RepeatedNested.inner', index=0, number=1, type=11, cpp_type=10, label=3, has_default_value=False, default_value=[], message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[_REPEATEDNESTED_INNER, ], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1292, serialized_end=1394, ) _REPEATEDWRAPPED = _descriptor.Descriptor( name='RepeatedWrapped', full_name='schematics_proto3.tests.RepeatedWrapped', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.RepeatedWrapped.value', index=0, number=1, type=11, cpp_type=10, label=3, has_default_value=False, default_value=[], message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1396, serialized_end=1457, ) _ONEOFPRIMITIVE = _descriptor.Descriptor( name='OneOfPrimitive', full_name='schematics_proto3.tests.OneOfPrimitive', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value1', full_name='schematics_proto3.tests.OneOfPrimitive.value1', index=0, number=1, type=4, cpp_type=4, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='value2', full_name='schematics_proto3.tests.OneOfPrimitive.value2', index=1, number=2, type=9, cpp_type=9, label=1, has_default_value=False, default_value=_b("").decode('utf-8'), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ _descriptor.OneofDescriptor( name='inner', full_name='schematics_proto3.tests.OneOfPrimitive.inner', index=0, containing_type=None, fields=[]), ], serialized_start=1459, serialized_end=1520, ) _ONEOFNESTED_INNER = _descriptor.Descriptor( name='Inner', full_name='schematics_proto3.tests.OneOfNested.Inner', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.OneOfNested.Inner.value', index=0, number=1, type=9, cpp_type=9, label=1, has_default_value=False, default_value=_b("").decode('utf-8'), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=208, serialized_end=230, ) _ONEOFNESTED = _descriptor.Descriptor( name='OneOfNested', full_name='schematics_proto3.tests.OneOfNested', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value1', full_name='schematics_proto3.tests.OneOfNested.value1', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='value2', full_name='schematics_proto3.tests.OneOfNested.value2', index=1, number=2, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[_ONEOFNESTED_INNER, ], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ _descriptor.OneofDescriptor( name='inner', full_name='schematics_proto3.tests.OneOfNested.inner', index=0, containing_type=None, fields=[]), ], serialized_start=1523, serialized_end=1679, ) _SIMPLEENUM = _descriptor.Descriptor( name='SimpleEnum', full_name='schematics_proto3.tests.SimpleEnum', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.SimpleEnum.value', index=0, number=1, type=14, cpp_type=8, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1681, serialized_end=1739, ) _REPEATEDENUM = _descriptor.Descriptor( name='RepeatedEnum', full_name='schematics_proto3.tests.RepeatedEnum', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value', full_name='schematics_proto3.tests.RepeatedEnum.value', index=0, number=1, type=14, cpp_type=8, label=3, has_default_value=False, default_value=[], message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ ], serialized_start=1741, serialized_end=1801, ) _ONEOFENUM = _descriptor.Descriptor( name='OneOfEnum', full_name='schematics_proto3.tests.OneOfEnum', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='value1', full_name='schematics_proto3.tests.OneOfEnum.value1', index=0, number=1, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='value2', full_name='schematics_proto3.tests.OneOfEnum.value2', index=1, number=2, type=14, cpp_type=8, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], serialized_options=None, is_extendable=False, syntax='proto3', extension_ranges=[], oneofs=[ _descriptor.OneofDescriptor( name='inner', full_name='schematics_proto3.tests.OneOfEnum.inner', index=0, containing_type=None, fields=[]), ], serialized_start=1803, serialized_end=1920, ) _NESTED_INNER.containing_type = _NESTED _NESTED.fields_by_name['inner'].message_type = _NESTED_INNER _WRAPPEDDOUBLE.fields_by_name['wrapped'].message_type = google_dot_protobuf_dot_wrappers__pb2._DOUBLEVALUE _WRAPPEDFLOAT.fields_by_name['wrapped'].message_type = google_dot_protobuf_dot_wrappers__pb2._FLOATVALUE _WRAPPEDINT64.fields_by_name['wrapped'].message_type = google_dot_protobuf_dot_wrappers__pb2._INT64VALUE _WRAPPEDUINT64.fields_by_name['wrapped'].message_type = google_dot_protobuf_dot_wrappers__pb2._UINT64VALUE _WRAPPEDINT32.fields_by_name['wrapped'].message_type = google_dot_protobuf_dot_wrappers__pb2._INT32VALUE _WRAPPEDUINT32.fields_by_name['wrapped'].message_type = google_dot_protobuf_dot_wrappers__pb2._UINT32VALUE _WRAPPEDBOOL.fields_by_name['wrapped'].message_type = google_dot_protobuf_dot_wrappers__pb2._BOOLVALUE _WRAPPEDSTRING.fields_by_name['wrapped'].message_type = google_dot_protobuf_dot_wrappers__pb2._STRINGVALUE _WRAPPEDBYTES.fields_by_name['wrapped'].message_type = google_dot_protobuf_dot_wrappers__pb2._BYTESVALUE _TIMESTAMP.fields_by_name['value'].message_type = google_dot_protobuf_dot_timestamp__pb2._TIMESTAMP _REPEATEDTIMESTAMP.fields_by_name['value'].message_type = google_dot_protobuf_dot_timestamp__pb2._TIMESTAMP _ONEOFTIMESTAMP.fields_by_name['value1'].message_type = google_dot_protobuf_dot_wrappers__pb2._STRINGVALUE _ONEOFTIMESTAMP.fields_by_name['value2'].message_type = google_dot_protobuf_dot_timestamp__pb2._TIMESTAMP _ONEOFTIMESTAMP.oneofs_by_name['inner'].fields.append( _ONEOFTIMESTAMP.fields_by_name['value1']) _ONEOFTIMESTAMP.fields_by_name['value1'].containing_oneof = _ONEOFTIMESTAMP.oneofs_by_name['inner'] _ONEOFTIMESTAMP.oneofs_by_name['inner'].fields.append( _ONEOFTIMESTAMP.fields_by_name['value2']) _ONEOFTIMESTAMP.fields_by_name['value2'].containing_oneof = _ONEOFTIMESTAMP.oneofs_by_name['inner'] _REPEATEDNESTED_INNER.containing_type = _REPEATEDNESTED _REPEATEDNESTED.fields_by_name['inner'].message_type = _REPEATEDNESTED_INNER _REPEATEDWRAPPED.fields_by_name['value'].message_type = google_dot_protobuf_dot_wrappers__pb2._INT32VALUE _ONEOFPRIMITIVE.oneofs_by_name['inner'].fields.append( _ONEOFPRIMITIVE.fields_by_name['value1']) _ONEOFPRIMITIVE.fields_by_name['value1'].containing_oneof = _ONEOFPRIMITIVE.oneofs_by_name['inner'] _ONEOFPRIMITIVE.oneofs_by_name['inner'].fields.append( _ONEOFPRIMITIVE.fields_by_name['value2']) _ONEOFPRIMITIVE.fields_by_name['value2'].containing_oneof = _ONEOFPRIMITIVE.oneofs_by_name['inner'] _ONEOFNESTED_INNER.containing_type = _ONEOFNESTED _ONEOFNESTED.fields_by_name['value1'].message_type = _ONEOFNESTED_INNER _ONEOFNESTED.fields_by_name['value2'].message_type = google_dot_protobuf_dot_wrappers__pb2._STRINGVALUE _ONEOFNESTED.oneofs_by_name['inner'].fields.append( _ONEOFNESTED.fields_by_name['value1']) _ONEOFNESTED.fields_by_name['value1'].containing_oneof = _ONEOFNESTED.oneofs_by_name['inner'] _ONEOFNESTED.oneofs_by_name['inner'].fields.append( _ONEOFNESTED.fields_by_name['value2']) _ONEOFNESTED.fields_by_name['value2'].containing_oneof = _ONEOFNESTED.oneofs_by_name['inner'] _SIMPLEENUM.fields_by_name['value'].enum_type = _ENUM _REPEATEDENUM.fields_by_name['value'].enum_type = _ENUM _ONEOFENUM.fields_by_name['value1'].message_type = google_dot_protobuf_dot_wrappers__pb2._STRINGVALUE _ONEOFENUM.fields_by_name['value2'].enum_type = _ENUM _ONEOFENUM.oneofs_by_name['inner'].fields.append( _ONEOFENUM.fields_by_name['value1']) _ONEOFENUM.fields_by_name['value1'].containing_oneof = _ONEOFENUM.oneofs_by_name['inner'] _ONEOFENUM.oneofs_by_name['inner'].fields.append( _ONEOFENUM.fields_by_name['value2']) _ONEOFENUM.fields_by_name['value2'].containing_oneof = _ONEOFENUM.oneofs_by_name['inner'] DESCRIPTOR.message_types_by_name['Nested'] = _NESTED DESCRIPTOR.message_types_by_name['WrappedDouble'] = _WRAPPEDDOUBLE DESCRIPTOR.message_types_by_name['WrappedFloat'] = _WRAPPEDFLOAT DESCRIPTOR.message_types_by_name['WrappedInt64'] = _WRAPPEDINT64 DESCRIPTOR.message_types_by_name['WrappedUInt64'] = _WRAPPEDUINT64 DESCRIPTOR.message_types_by_name['WrappedInt32'] = _WRAPPEDINT32 DESCRIPTOR.message_types_by_name['WrappedUInt32'] = _WRAPPEDUINT32 DESCRIPTOR.message_types_by_name['WrappedBool'] = _WRAPPEDBOOL DESCRIPTOR.message_types_by_name['WrappedString'] = _WRAPPEDSTRING DESCRIPTOR.message_types_by_name['WrappedBytes'] = _WRAPPEDBYTES DESCRIPTOR.message_types_by_name['Timestamp'] = _TIMESTAMP DESCRIPTOR.message_types_by_name['RepeatedTimestamp'] = _REPEATEDTIMESTAMP DESCRIPTOR.message_types_by_name['OneOfTimestamp'] = _ONEOFTIMESTAMP DESCRIPTOR.message_types_by_name['Double'] = _DOUBLE DESCRIPTOR.message_types_by_name['Float'] = _FLOAT DESCRIPTOR.message_types_by_name['Int64'] = _INT64 DESCRIPTOR.message_types_by_name['UInt64'] = _UINT64 DESCRIPTOR.message_types_by_name['Int32'] = _INT32 DESCRIPTOR.message_types_by_name['UInt32'] = _UINT32 DESCRIPTOR.message_types_by_name['Bool'] = _BOOL DESCRIPTOR.message_types_by_name['String'] = _STRING DESCRIPTOR.message_types_by_name['Bytes'] = _BYTES DESCRIPTOR.message_types_by_name['RepeatedPrimitive'] = _REPEATEDPRIMITIVE DESCRIPTOR.message_types_by_name['RepeatedNested'] = _REPEATEDNESTED DESCRIPTOR.message_types_by_name['RepeatedWrapped'] = _REPEATEDWRAPPED DESCRIPTOR.message_types_by_name['OneOfPrimitive'] = _ONEOFPRIMITIVE DESCRIPTOR.message_types_by_name['OneOfNested'] = _ONEOFNESTED DESCRIPTOR.message_types_by_name['SimpleEnum'] = _SIMPLEENUM DESCRIPTOR.message_types_by_name['RepeatedEnum'] = _REPEATEDENUM DESCRIPTOR.message_types_by_name['OneOfEnum'] = _ONEOFENUM DESCRIPTOR.enum_types_by_name['Enum'] = _ENUM _sym_db.RegisterFileDescriptor(DESCRIPTOR) Nested = _reflection.GeneratedProtocolMessageType('Nested', (_message.Message,), dict( Inner = _reflection.GeneratedProtocolMessageType('Inner', (_message.Message,), dict( DESCRIPTOR = _NESTED_INNER, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.Nested.Inner) )) , DESCRIPTOR = _NESTED, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.Nested) )) _sym_db.RegisterMessage(Nested) _sym_db.RegisterMessage(Nested.Inner) WrappedDouble = _reflection.GeneratedProtocolMessageType('WrappedDouble', (_message.Message,), dict( DESCRIPTOR = _WRAPPEDDOUBLE, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.WrappedDouble) )) _sym_db.RegisterMessage(WrappedDouble) WrappedFloat = _reflection.GeneratedProtocolMessageType('WrappedFloat', (_message.Message,), dict( DESCRIPTOR = _WRAPPEDFLOAT, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.WrappedFloat) )) _sym_db.RegisterMessage(WrappedFloat) WrappedInt64 = _reflection.GeneratedProtocolMessageType('WrappedInt64', (_message.Message,), dict( DESCRIPTOR = _WRAPPEDINT64, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.WrappedInt64) )) _sym_db.RegisterMessage(WrappedInt64) WrappedUInt64 = _reflection.GeneratedProtocolMessageType('WrappedUInt64', (_message.Message,), dict( DESCRIPTOR = _WRAPPEDUINT64, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.WrappedUInt64) )) _sym_db.RegisterMessage(WrappedUInt64) WrappedInt32 = _reflection.GeneratedProtocolMessageType('WrappedInt32', (_message.Message,), dict( DESCRIPTOR = _WRAPPEDINT32, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.WrappedInt32) )) _sym_db.RegisterMessage(WrappedInt32) WrappedUInt32 = _reflection.GeneratedProtocolMessageType('WrappedUInt32', (_message.Message,), dict( DESCRIPTOR = _WRAPPEDUINT32, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.WrappedUInt32) )) _sym_db.RegisterMessage(WrappedUInt32) WrappedBool = _reflection.GeneratedProtocolMessageType('WrappedBool', (_message.Message,), dict( DESCRIPTOR = _WRAPPEDBOOL, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.WrappedBool) )) _sym_db.RegisterMessage(WrappedBool) WrappedString = _reflection.GeneratedProtocolMessageType('WrappedString', (_message.Message,), dict( DESCRIPTOR = _WRAPPEDSTRING, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.WrappedString) )) _sym_db.RegisterMessage(WrappedString) WrappedBytes = _reflection.GeneratedProtocolMessageType('WrappedBytes', (_message.Message,), dict( DESCRIPTOR = _WRAPPEDBYTES, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.WrappedBytes) )) _sym_db.RegisterMessage(WrappedBytes) Timestamp = _reflection.GeneratedProtocolMessageType('Timestamp', (_message.Message,), dict( DESCRIPTOR = _TIMESTAMP, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.Timestamp) )) _sym_db.RegisterMessage(Timestamp) RepeatedTimestamp = _reflection.GeneratedProtocolMessageType('RepeatedTimestamp', (_message.Message,), dict( DESCRIPTOR = _REPEATEDTIMESTAMP, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.RepeatedTimestamp) )) _sym_db.RegisterMessage(RepeatedTimestamp) OneOfTimestamp = _reflection.GeneratedProtocolMessageType('OneOfTimestamp', (_message.Message,), dict( DESCRIPTOR = _ONEOFTIMESTAMP, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.OneOfTimestamp) )) _sym_db.RegisterMessage(OneOfTimestamp) Double = _reflection.GeneratedProtocolMessageType('Double', (_message.Message,), dict( DESCRIPTOR = _DOUBLE, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.Double) )) _sym_db.RegisterMessage(Double) Float = _reflection.GeneratedProtocolMessageType('Float', (_message.Message,), dict( DESCRIPTOR = _FLOAT, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.Float) )) _sym_db.RegisterMessage(Float) Int64 = _reflection.GeneratedProtocolMessageType('Int64', (_message.Message,), dict( DESCRIPTOR = _INT64, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.Int64) )) _sym_db.RegisterMessage(Int64) UInt64 = _reflection.GeneratedProtocolMessageType('UInt64', (_message.Message,), dict( DESCRIPTOR = _UINT64, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.UInt64) )) _sym_db.RegisterMessage(UInt64) Int32 = _reflection.GeneratedProtocolMessageType('Int32', (_message.Message,), dict( DESCRIPTOR = _INT32, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.Int32) )) _sym_db.RegisterMessage(Int32) UInt32 = _reflection.GeneratedProtocolMessageType('UInt32', (_message.Message,), dict( DESCRIPTOR = _UINT32, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.UInt32) )) _sym_db.RegisterMessage(UInt32) Bool = _reflection.GeneratedProtocolMessageType('Bool', (_message.Message,), dict( DESCRIPTOR = _BOOL, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.Bool) )) _sym_db.RegisterMessage(Bool) String = _reflection.GeneratedProtocolMessageType('String', (_message.Message,), dict( DESCRIPTOR = _STRING, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.String) )) _sym_db.RegisterMessage(String) Bytes = _reflection.GeneratedProtocolMessageType('Bytes', (_message.Message,), dict( DESCRIPTOR = _BYTES, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.Bytes) )) _sym_db.RegisterMessage(Bytes) RepeatedPrimitive = _reflection.GeneratedProtocolMessageType('RepeatedPrimitive', (_message.Message,), dict( DESCRIPTOR = _REPEATEDPRIMITIVE, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.RepeatedPrimitive) )) _sym_db.RegisterMessage(RepeatedPrimitive) RepeatedNested = _reflection.GeneratedProtocolMessageType('RepeatedNested', (_message.Message,), dict( Inner = _reflection.GeneratedProtocolMessageType('Inner', (_message.Message,), dict( DESCRIPTOR = _REPEATEDNESTED_INNER, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.RepeatedNested.Inner) )) , DESCRIPTOR = _REPEATEDNESTED, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.RepeatedNested) )) _sym_db.RegisterMessage(RepeatedNested) _sym_db.RegisterMessage(RepeatedNested.Inner) RepeatedWrapped = _reflection.GeneratedProtocolMessageType('RepeatedWrapped', (_message.Message,), dict( DESCRIPTOR = _REPEATEDWRAPPED, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.RepeatedWrapped) )) _sym_db.RegisterMessage(RepeatedWrapped) OneOfPrimitive = _reflection.GeneratedProtocolMessageType('OneOfPrimitive', (_message.Message,), dict( DESCRIPTOR = _ONEOFPRIMITIVE, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.OneOfPrimitive) )) _sym_db.RegisterMessage(OneOfPrimitive) OneOfNested = _reflection.GeneratedProtocolMessageType('OneOfNested', (_message.Message,), dict( Inner = _reflection.GeneratedProtocolMessageType('Inner', (_message.Message,), dict( DESCRIPTOR = _ONEOFNESTED_INNER, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.OneOfNested.Inner) )) , DESCRIPTOR = _ONEOFNESTED, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.OneOfNested) )) _sym_db.RegisterMessage(OneOfNested) _sym_db.RegisterMessage(OneOfNested.Inner) SimpleEnum = _reflection.GeneratedProtocolMessageType('SimpleEnum', (_message.Message,), dict( DESCRIPTOR = _SIMPLEENUM, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.SimpleEnum) )) _sym_db.RegisterMessage(SimpleEnum) RepeatedEnum = _reflection.GeneratedProtocolMessageType('RepeatedEnum', (_message.Message,), dict( DESCRIPTOR = _REPEATEDENUM, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.RepeatedEnum) )) _sym_db.RegisterMessage(RepeatedEnum) OneOfEnum = _reflection.GeneratedProtocolMessageType('OneOfEnum', (_message.Message,), dict( DESCRIPTOR = _ONEOFENUM, __module__ = 'tests.schematics_proto3_tests_pb2' # @@protoc_insertion_point(class_scope:schematics_proto3.tests.OneOfEnum) )) _sym_db.RegisterMessage(OneOfEnum) # @@protoc_insertion_point(module_scope)
48,837
19,293
from rest_framework.serializers import ModelSerializer from accounts.models import Account class AccountSerializer(ModelSerializer): class Meta: model = Account fields = [ 'account_id', 'limit', ]
241
59
# coding: utf-8 from __future__ import division from __future__ import print_function from __future__ import absolute_import from __future__ import unicode_literals import os import logging import datetime import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from config import SAVING_DIR from config import SEED from visual import set_plot_config set_plot_config() from utils.log import set_logger from utils.log import flush from utils.log import print_line from utils.evaluation import evaluate_minuit from problem.higgs import HiggsConfigTesOnly as Config from problem.higgs import get_minimizer from problem.higgs import get_minimizer_no_nuisance from problem.higgs import get_generators_torch from problem.higgs import HiggsNLL as NLLComputer from ..common import N_BINS def do_iter(config, model, i_iter, valid_generator, test_generator, root_dir, n_bins=N_BINS): logger = logging.getLogger() directory = os.path.join(root_dir, model.name, f"iter_{i_iter}") os.makedirs(directory, exist_ok=True) logger.info(f"saving dir = {directory}") logger.info('Generate testing data') X_test, y_test, w_test = test_generator.generate(*config.TRUE, n_samples=config.N_TESTING_SAMPLES, no_grad=True) logger.info('Set up NLL computer') compute_summaries = model.summary_computer(n_bins=n_bins) compute_nll = NLLComputer(compute_summaries, valid_generator, X_test, w_test, config=config) basic_check(compute_nll, config) basic_contourplot(compute_nll, config, directory) # MINIMIZE NLL logger.info('Prepare minuit minimizer') minimizer = get_minimizer(compute_nll, config.CALIBRATED, config.CALIBRATED_ERROR) some_dict = evaluate_minuit(minimizer, config.TRUE, directory, suffix="") # FOCUSED contour plot nll_func = lambda mu, tes : compute_nll(tes, config.TRUE.jes, config.TRUE.les, mu) x = minimizer.values[3] y = minimizer.values[0] x_err = minimizer.errors[3] y_err = minimizer.errors[0] focused_contour(x, y, x_err, y_err, nll_func, directory, xlabel="mu", ylabel='tes') nll_func = lambda mu, jes : compute_nll(config.TRUE.tes, jes, config.TRUE.les, mu) x = minimizer.values[3] y = minimizer.values[1] x_err = minimizer.errors[3] y_err = minimizer.errors[1] focused_contour(x, y, x_err, y_err, nll_func, directory, xlabel="mu", ylabel='jes') nll_func = lambda mu, les : compute_nll(config.TRUE.tes, config.TRUE.jes, les, mu) x = minimizer.values[3] y = minimizer.values[2] x_err = minimizer.errors[3] y_err = minimizer.errors[2] focused_contour(x, y, x_err, y_err, nll_func, directory, xlabel="mu", ylabel='les') def basic_check(compute_nll, config): logger = logging.getLogger() nll = compute_nll(*config.CALIBRATED) logger.info(f"Calib nll = {nll}") nll = compute_nll(*config.TRUE) logger.info(f"TRUE nll = {nll}") def basic_contourplot(compute_nll, config, directory): logger = logging.getLogger() ARRAY_SIZE = 10 # MESH NLL logger.info(f"basic mu-tes contour plot...") mu_array = np.linspace(0.5, 1.5, ARRAY_SIZE) tes_array = np.linspace(0.95, 1.05, ARRAY_SIZE) mu_mesh, tes_mesh = np.meshgrid(mu_array, tes_array) nll_func = lambda mu, tes : compute_nll(tes, config.TRUE.jes, config.TRUE.les, mu) nll_mesh = np.array([nll_func(mu, tes) for mu, tes in zip(mu_mesh.ravel(), tes_mesh.ravel())]).reshape(mu_mesh.shape) plot_contour(mu_mesh, tes_mesh, nll_mesh, directory, xlabel="mu", ylabel="tes") logger.info(f"basic mu-jes contour plot...") jes_array = np.linspace(0.95, 1.05, ARRAY_SIZE) mu_mesh, jes_mesh = np.meshgrid(mu_array, jes_array) nll_func = lambda mu, jes : compute_nll(config.TRUE.tes, jes, config.TRUE.les, mu) nll_mesh = np.array([nll_func(mu, jes) for mu, jes in zip(mu_mesh.ravel(), jes_mesh.ravel())]).reshape(mu_mesh.shape) plot_contour(mu_mesh, jes_mesh, nll_mesh, directory, xlabel="mu", ylabel="jes") logger.info(f"basic mu-les contour plot...") les_array = np.linspace(0.95, 1.05, ARRAY_SIZE) mu_mesh, les_mesh = np.meshgrid(mu_array, les_array) nll_func = lambda mu, les : compute_nll(config.TRUE.tes, config.TRUE.jes, les, mu) nll_mesh = np.array([nll_func(mu, les) for mu, les in zip(mu_mesh.ravel(), les_mesh.ravel())]).reshape(mu_mesh.shape) plot_contour(mu_mesh, les_mesh, nll_mesh, directory, xlabel="mu", ylabel="les") logger.info(f"basic tes-jes contour plot...") tes_mesh, jes_mesh = np.meshgrid(tes_array, jes_array) nll_func = lambda tes, jes : compute_nll(tes, jes, config.TRUE.les, config.TRUE.mu) nll_mesh = np.array([nll_func(tes, jes) for tes, jes in zip(tes_mesh.ravel(), jes_mesh.ravel())]).reshape(tes_mesh.shape) plot_contour(tes_mesh, jes_mesh, nll_mesh, directory, xlabel="tes", ylabel="jes") def plot_contour(x, y, z, directory, xlabel="mu", ylabel="tes"): logger = logging.getLogger() fig, ax = plt.subplots() CS = ax.contour(x, y, z) ax.clabel(CS, inline=1, fontsize=10) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) now = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S\n") fig.title(now) fname = f"{xlabel}-{ylabel}_contour_plot.png" path = os.path.join(directory, fname) plt.savefig(path) plt.clf() plt.close(fig) logger.info(f"saved at {path}") def focused_contour(x, y, x_err, y_err, nll_func, directory, xlabel="mu", ylabel='tes'): logger = logging.getLogger() ARRAY_SIZE = 10 logger.info(f"focused {xlabel}-{ylabel} contour plot...") x_array = np.linspace(x-3*x_err, x+3*x_err, ARRAY_SIZE) y_array = np.linspace(y-3*y_err, y+3*y_err, ARRAY_SIZE) x_mesh, y_mesh = np.meshgrid(x_array, y_array) z_mesh = np.array([nll_func(x, y) for x, y in zip(x_mesh.ravel(), y_mesh.ravel())]).reshape(x_mesh.shape) fig, ax = plt.subplots() CS = ax.contour(x_mesh, y_mesh, z_mesh) ax.clabel(CS, inline=1, fontsize=10) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) now = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S\n") fig.title(now) fname = f"{xlabel}-{ylabel}_focused_contour_plot.png" path = os.path.join(directory, fname) plt.savefig(path) plt.clf() plt.close(fig) logger.info(f"saved at {path}")
6,320
2,546
# -*- coding: utf-8 -*- # Generated by Django 1.11 on 2018-09-21 17:17 from __future__ import unicode_literals import django.contrib.postgres.fields from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ ('dashboard', '0011_auto_20180727_1800'), ] operations = [ migrations.CreateModel( name='Fibermap', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('ra_obs', django.contrib.postgres.fields.ArrayField(base_field=models.FloatField(), size=None)), ('dec_obs', django.contrib.postgres.fields.ArrayField(base_field=models.FloatField(), size=None)), ('fiber', django.contrib.postgres.fields.ArrayField(base_field=models.FloatField(), size=None)), ('objtype', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=15), size=None)), ('exposure', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='fibermap_exposure', to='dashboard.Exposure')), ], ), migrations.RemoveField( model_name='qa', name='job', ), migrations.DeleteModel( name='QA', ), ]
1,376
432
from .private.HMC804x import _RohdeSchwarzHMC804x from qcodes.utils.deprecate import deprecate_moved_to_qcd @deprecate_moved_to_qcd(alternative="qcodes_contrib_drivers.drivers.RohdeSchwarz.HMC8042.RohdeSchwarzHMC8042") class RohdeSchwarzHMC8042(_RohdeSchwarzHMC804x): """ This is the qcodes driver for the Rohde & Schwarz HMC8042 Power Supply """ def __init__(self, name, address, **kwargs): super().__init__(name, address, num_channels=2, **kwargs)
476
199
# Multiples -- Part I - Write code that prints all the odd numbers from 1 to 1000. Use the for loop and don't use a list to do this exercise for i in range(1, 1000, 2): print(i) # Multiples -- Part II - Create another program that prints all the multiples of 5 from 5 to 1,000,000 for m in range(5, 1000000, 5): print(m) # Sum List -- Create a program that prints the sum of all the values in the list: a = [1, 2, 5, 10, 255, 3] a = [1, 2, 5, 10, 255, 3] b = sum(a) print(b) # Average List -- Create a program that prints the average of the values in the list: c = [1, 2, 5, 10, 255, 3] c = [1, 2, 5, 10, 255, 3] dSum = sum(c) eLen = len(c) fAvg = (dSum / eLen) print(fAvg)
682
306
#!/usr/bin/env python3 """ Demo example to test CFG-VALSET ublox message - generation 9 @author: mgesteiro """ import sys import time from serial import Serial, SerialException, SerialTimeoutException from pyubx2 import ( UBXMessage, GET, SET, VALSET_RAM, UBXMessageError, UBXTypeError, UBXParseError, ) def message_valsetuart1baudrate_set(baudrate): """ Function to generate a CFG-VALSET CFG-UART1-BAUDRATE set UBX message """ # https://www.u-blox.com/en/docs/UBX-18010854#page=86&zoom=auto,-74,499 # CFG-UART1-BAUDRATE Key = 0x40520001 return UBXMessage( "CFG", "CFG-VALSET", SET, payload=b"\x00" + VALSET_RAM # version + int(0).to_bytes(2, byteorder="little", signed=False) # layers + 0x40520001 .to_bytes(4, byteorder="little", signed=False) # reserved0 + baudrate.to_bytes(4, byteorder="little", signed=False), # key # value ) def message_valsetuart1baudrate_response(): """ Function to generate a ACK-ACK-ACK UBX message """ # https://www.u-blox.com/en/docs/UBX-18010854#page=52&zoom=auto,-74,379 return UBXMessage("ACK", "ACK-ACK", GET, clsID=0x06, msgID=0x8A) if __name__ == "__main__": PORTNAME = "/dev/tty.usbserial-A50285BI" BAUDRATE = 230400 try: print("\nBuilding CFG-UART1-BAUDRATE VALSET message:") msg = message_valsetuart1baudrate_set(BAUDRATE) print(f" GENERATED: {msg.serialize().hex()}") print( " EXPECTED: b562068a0c00000100000100524000840300b7ef" + " (Note: valid for 230400 baudrate)" ) print(f" {msg}\n") print(f"This demo will now set your module's UART1 to {BAUDRATE} (only in RAM)") try: input("press <ENTER> to continue, CTRL-C to abort!\n") except KeyboardInterrupt: print("\nExecution aborted.\n") sys.exit(0) sport = Serial(PORTNAME, BAUDRATE, timeout=2) time.sleep(0.250) # stabilize print( f"Sending set message to {PORTNAME} at {BAUDRATE} " + "(edit the code to change these values)\n" ) sport.flushInput() sport.write(msg.serialize()) print("Receiving response ...") raw = sport.read(512) START = raw.find(b"\xB5\x62") data = raw[START : START + 10] # expected ACK msg = message_valsetuart1baudrate_response() print(f" RECEIVED: {data.hex()}") print(f" EXPECTED: {msg.serialize().hex()}") print(f" {UBXMessage.parse(data)}\n") except ( UBXMessageError, UBXTypeError, UBXParseError, SerialException, SerialTimeoutException, ) as err: print(f"Something broke 💥🤷‍♂️: {err}\n")
2,817
1,104
import pygame import time import sys, random pygame.init() yellow = (255, 255, 102) green = (0, 255, 0) black = (0,0,0) width = 1280 height = 720 gameDisplay = pygame.display.set_mode((width, height)) pygame.display.set_caption('Snake Game By Saswat Samal') clock = pygame.time.Clock() snake_block = 10 snake_speed = 15 font_style = pygame.font.SysFont("ubuntu", 25) score_font = pygame.font.SysFont("ubuntu", 20) def main_menu(): while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: gameLoop() gameDisplay.fill(black) main_menu_message = font_style.render('Press anywhere to start the game' , True , (255,255,255)) font_pos = main_menu_message.get_rect(center=(width//2, height//2)) gameDisplay.blit(main_menu_message , font_pos) pygame.display.update() def gameScore(score): value = score_font.render("Your Score: " + str(score), True, green) gameDisplay.blit(value, [width/2, 0]) def our_snake(snake_block, snake_list): for x in snake_list: pygame.draw.rect(gameDisplay, green, [x[0], x[1], snake_block, snake_block]) def message(msg, color): mesg = font_style.render(msg, True, color) gameDisplay.blit(mesg, [width / 6, height / 3]) def gameLoop(): game_over = False game_close = False x1 = width / 2 y1 = height / 2 x1_change = 0 y1_change = 0 snake_List = [] Length_of_snake = 1 foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0 while not game_over: while game_close == True: gameDisplay.fill(black) message("Game Over! Press P to Play Again and Press Q to Quit the game. ", green) gameScore(Length_of_snake - 1) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_p: gameLoop() for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -snake_block y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = snake_block y1_change = 0 elif event.key == pygame.K_UP: y1_change = -snake_block x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = snake_block x1_change = 0 if x1 >= width or x1 < 0 or y1 >= height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change gameDisplay.fill(black) pygame.draw.rect(gameDisplay, yellow, [foodx, foody, snake_block, snake_block]) snake_Head = [] snake_Head.append(x1) snake_Head.append(y1) snake_List.append(snake_Head) if len(snake_List) > Length_of_snake: del snake_List[0] for x in snake_List[:-1]: if x == snake_Head: game_close = True our_snake(snake_block, snake_List) gameScore(Length_of_snake - 1) pygame.display.update() if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0 Length_of_snake += 1 clock.tick(snake_speed) pygame.quit() quit() main_menu()
3,996
1,401
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot = True) sess = tf.InteractiveSession() x = tf.placeholder(tf.float32, [None, 784]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) y = tf.nn.softmax(tf.matmul(x, W) + b) y_ = tf.placeholder(tf.float32, [None, 10]) # 真实概率 cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices = [1])) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) tf.global_variables_initializer().run() for i in range(10000): batch_xs, batch_ys = mnist.train.next_batch(100) train_step.run({x: batch_xs, y_: batch_ys}) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))
918
395
# Generated by Django 2.2.20 on 2021-05-27 14:53 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('analyzer', '0005_auto_20210526_1755'), ] operations = [ migrations.AddField( model_name='graphitevariable', name='function', field=models.CharField(choices=[('A', 'avg'), ('MA', 'max'), ('MI', 'min')], default='A', max_length=12), ), ]
465
171
# -*- coding: utf-8 -*- """ Solution to Project Euler problem X Author: Jaime Liew https://github.com/jaimeliew1/Project_Euler_Solutions """ import math def isTri(n): return ((math.sqrt(1+8*n)-1)/2).is_integer() def isSqr(n): return (math.sqrt(n)).is_integer() def isPent(n): return ((1+math.sqrt(1+24*n))/6).is_integer() def isHex(n): return ((1+math.sqrt(1+8*n))/4).is_integer() def isHept(n): return ((3+math.sqrt(9+40*n))/10).is_integer() def isOct(n): return ((2+math.sqrt(4+12*n))/6).is_integer() isPoly = [isTri, isSqr, isPent, isHex,isHept,isOct] class Jnum: id = 0 #each nth bit is 1 if it is an nGon number n = 0 isMultiPoly = False def __init__(self, num): self.n = num for i in (f(num) for f in isPoly): self.id = (self.id << 1) | i if bin(self.id).count('1') > 1: self.isMultiPoly = True def __eq__(self,other): return self.n == other.n def __ne__(self,other): return self.n != other.n def checkThisSet(thisSet,depth,maxDepth, numSet): for q in (q for q in numSet if q not in thisSet): workingBit = 0 qIsCandidate = True if str(thisSet[-1].n)[2:] == str(q.n)[:2]: #if cyclical workingBit = 0 for i in (thisSet + [q]): if workingBit & (i.id) == 0: workingBit |= (i.id) else: qIsCandidate = False break else: qIsCandidate = False if qIsCandidate: if depth == maxDepth-1: if str(thisSet[0].n)[:2] == str(q.n)[2:]: #if cyclical back to start return list(thisSet + [q]) else: return [Jnum(0)] furtherTesting = checkThisSet(list(thisSet + [q]),depth +1, maxDepth, numSet) if furtherTesting != [Jnum(0)]: return furtherTesting return [Jnum(0)] def run(): ### generate set of possible candidates numSet = [] for i in range(1000, 10000): a = Jnum(i) if a.id != 0: if a.isMultiPoly: temp = a for k, bit in enumerate(bin(a.id)[2:].zfill(6)[::-1]): if bit == '1': temp.id = 1<<k numSet.append(Jnum(a.n)) numSet[-1].id = 1<<k else: numSet.append(a) #print("there are ",len(numSet)," candidate numbers.\n") ### Recursive search loop for i in numSet: currentSet = checkThisSet(list([i]), 1, 6, numSet) if currentSet != [Jnum(0)]: break Sum = 0 for i in currentSet: #print(i.n, bin(i.id)[2:].zfill(6)) Sum += i.n return Sum if __name__ == "__main__": print(run())
2,865
1,048
from shop.models import Product from django.shortcuts import render_to_response from django.template import RequestContext def index_view(request): products = Product.objects.all() return render_to_response("cuescience_shop/index.html", RequestContext(request, {"products": products}))
302
84
import numpy as np from qrogue.game.logic.actors import StateVector stv = StateVector([1 / np.sqrt(2), 0 + 0j, 0 + 0j, 1 / np.sqrt(2)]) #stv.extend(1) print(stv)
163
76
from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from .models import Image, Comment, Like from .serializers import ImageSerializer, CommentSerializer, LikeSerializer class Feed(APIView): def get(self, request, format=None): user = request.user follwoing_users = user.following.all() image_list = [] for following_user in follwoing_users: user_images = following_user.images.all()[:2] for image in user_images: image_list.append(image) sorted_list = sorted(image_list, key=lambda image:image.created_at, reverse=True) serializer = ImageSerializer(sorted_list, many=True) return Response(serializer.data) class LikeImage(APIView): def post(self, request, image_id, format=None): try: found_image = Image.objects.get(id=image_id) except Image.DoesNotExist : return Response(status=status.HTTP_404_NOT_FOUND) try: pre_exisiting_like = Like.objects.get( creator=request.user, image=found_image ) return Response(status=status.HTTP_304_NOT_MODIFIED) except Like.DoesNotExist: new_like = Like.objects.create( creator=request.user, image=found_image ) new_like.save() return Response(status=status.HTTP_201_CREATED) class UnLikeImage(APIView): def delete(self, request, image_id, format=None): user = request.user try: found_image = Image.objects.get(id=image_id) except: return Response(status=status.HTTP_404_NOT_FOUND) try: pre_existing_like = Like.objects.get( creator=user, image=found_image ) pre_existing_like.delete() return Response(status=status.HTTP_204_NO_CONTENT) except Like.DoesNotExist: return Response(status=status.HTTP_304_NOT_MODIFIED) class CommentOnImage(APIView): def post(self, request, image_id, format=None): user = request.user try: found_image = Image.objects.get(id=image_id) except Image.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = CommentSerializer(data=request.data) if serializer.is_valid(): serializer.save(creator=user, image=found_image) return Response(data=serializer.data, status=status.HTTP_201_CREATED) else: return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST) class Comment(APIView): def delete(self, request, comment_id, format=None): s user = request.user try: comment = Comment.objects.get(id=comment_id, creator=user) comment.delete() return Response(status=status.HTTP_204_NO_CONTENT) except Comment.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND)
3,190
934
#!/usr/bin/env python """ Compute the comparison of the analytic and experimental PSD matrices. This will generate Figure 1. This is probably the only example that will run in a reasonable time without a GPU. For more details on the method see https://arxiv.org/abs/2106.13785. """ import numpy as np import matplotlib.pyplot as plt from bilby.core.utils import create_white_noise, create_frequency_series from scipy.signal.windows import tukey from scipy.interpolate import interp1d from tqdm.auto import trange from coarse_psd_matrix.utils import ( compute_psd_matrix, create_parser, fetch_psd_data, ) from coarse_psd_matrix.plotting import plot_psd_matrix from matplotlib import rcParams rcParams["font.family"] = "serif" rcParams["font.serif"] = "Computer Modern Roman" rcParams["font.size"] = 20 rcParams["text.usetex"] = True rcParams["grid.alpha"] = 0 if __name__ == "__main__": parser = create_parser() args = parser.parse_args() interferometer = args.interferometer outdir = args.outdir duration = args.duration medium_duration = args.medium_duration sampling_frequency = args.sampling_frequency low_frequency = args.low_frequency tukey_alpha = args.tukey_alpha minimum_frequency = 480 maximum_frequency = 530 event = args.event data = fetch_psd_data( interferometer_name=interferometer, event=event, duration=duration, sampling_frequency=sampling_frequency, low_frequency=low_frequency, tukey_alpha=tukey_alpha, medium_duration=medium_duration, outdir=outdir, ) svd = compute_psd_matrix( interferometer_name=interferometer, event=event, duration=duration, sampling_frequency=sampling_frequency, low_frequency=low_frequency, tukey_alpha=tukey_alpha, minimum_frequency=minimum_frequency, maximum_frequency=maximum_frequency, medium_duration=medium_duration, outdir=outdir, ) psd = data["medium_psd"][: sampling_frequency // 2 * medium_duration + 1] original_frequencies = create_frequency_series( duration=medium_duration, sampling_frequency=sampling_frequency ) new_frequencies = create_frequency_series( duration=256, sampling_frequency=sampling_frequency ) psd = interp1d(original_frequencies, psd)(new_frequencies) short_window = tukey(duration * sampling_frequency, tukey_alpha) short_window /= np.mean(short_window ** 2) ** 0.5 analytic_psd_matrix = (svd[0] * svd[1]) @ svd[2] estimated_psd_matrix = np.zeros_like(analytic_psd_matrix) nfft = duration * sampling_frequency start_idx = minimum_frequency * duration stop_idx = maximum_frequency * duration n_average = 1024 * 1024 // 64 for _ in trange(n_average): white_noise, frequencies = create_white_noise( sampling_frequency=2048, duration=256 ) coloured_noise = white_noise * psd ** 0.5 td_noise = np.fft.irfft(coloured_noise).reshape((-1, nfft)) fd_noise = np.fft.rfft(td_noise * short_window) reduced_noise = fd_noise[:, start_idx : stop_idx + 1] estimated_psd_matrix += np.einsum( "ki,kj->ij", reduced_noise, reduced_noise.conjugate() ) / 2 total_averages = n_average * len(reduced_noise) estimated_psd_matrix /= total_averages rcParams["font.family"] = "serif" rcParams["font.serif"] = "Computer Modern Roman" rcParams["font.size"] = 20 rcParams["text.usetex"] = True rcParams["grid.alpha"] = 0 fig, axes = plt.subplots(nrows=2, figsize=(10, 16)) kwargs = dict( minimum_frequency=minimum_frequency, maximum_frequency=maximum_frequency, duration=duration, vmin=-53, vmax=-41.8, tick_step=10, ) plot_psd_matrix(estimated_psd_matrix, axes[0], **kwargs) plot_psd_matrix(analytic_psd_matrix, axes[1], **kwargs) axes[0].text(-25, 190, "(a)") axes[1].text(-25, 190, "(b)") plt.tight_layout() plt.savefig(f"{outdir}/zoom_{tukey_alpha}.pdf") if tukey_alpha == 0.1: plt.savefig("figure_1.pdf") plt.close()
4,195
1,479
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import pymysql class WebSpiderPipeline(object): def init_insert_db(self,key,table_name): pass def process_item(self, item, spider): # print(item['name']) connection = pymysql.connect(host='127.0.0.1', user='root', password='123456', db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) try: with connection.cursor() as cursor: # Create a new record if item['type'] == 'toutiao': insert_sql = """INSERT INTO `dongqiudi` (`id`, `name`,`url`,`time`,`comment`,`image`) VALUES (%s, %s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE id=VALUES(id), name=VALUES(name), url=VALUES (url), time=VALUES (time), comment=VALUES (comment), image=VALUES (image)""" cursor.execute(insert_sql, (item['id'], item['name'], item['url'], item['time'], item['comment'], item['image'])) elif item['type'] == 'rank': insert_sql = """INSERT INTO `rank` (`rank`,`team_avatar`,`team_name`,`round`,`win`,`draw`,`lost`,`goal`,`fumble`,`GD`,`integral`,`rel`,`rel_name`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE rank=VALUES (rank), team_avatar=VALUES (team_avatar), team_name=VALUES (team_name), round=VALUES (round), win=VALUES (win), draw=VALUES (draw), lost=VALUES (lost), goal=VALUES (goal), fumble=VALUES (fumble), GD=VALUES (GD), integral=VALUES (integral), rel=VALUES (rel), rel_name=VALUES (rel_name) """ cursor.execute(insert_sql, (item['rank'], item['team_avatar'], item['team_name'], item['round'], item['win'], item['draw'],item['lost'],item['goal'],item['fumble'],item['GD'],item['integral'],item['rel'],item['rel_name'])) elif item['type'] == 'goal': insert_sql = """INSERT INTO `player_goal_rank` (`rank`,`data`,`player_avatar`,`player_name`,`team_avatar`,`team_name`,`rel`,`rel_name`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE rank=VALUES (rank), data=VALUES (data), player_avatar=VALUES (player_avatar), player_name=VALUES (player_name), team_avatar=VALUES (team_avatar), team_name=VALUES (team_name), rel=VALUES (rel), rel_name=VALUES (rel_name) """ cursor.execute(insert_sql, (item['rank'], item['data'], item['player_avatar'], item['player_name'],item['team_avatar'], item['team_name'], item['rel'], item['rel_name'])) elif item['type'] == 'assist': insert_sql = """INSERT INTO `player_assist_rank` (`rank`,`data`,`player_avatar`,`player_name`,`team_avatar`,`team_name`,`rel`,`rel_name`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE rank=VALUES (rank), data=VALUES (data), player_avatar=VALUES (player_avatar), player_name=VALUES (player_name), team_avatar=VALUES (team_avatar), team_name=VALUES (team_name), rel=VALUES (rel), rel_name=VALUES (rel_name) """ cursor.execute(insert_sql, (item['rank'], item['data'], item['player_avatar'], item['player_name'],item['team_avatar'], item['team_name'], item['rel'], item['rel_name'])) # connection is not autocommit by default. So you must commit to save # your changes. connection.commit() finally: connection.close() pass
5,581
1,393
"""mixin to add destinations to config attributes""" class AddDestinationMixin(object): """stuff""" def add_destination_chooser(self, destination_chooser_mappings): """adds destination_choosers to config""" pass
239
72
#!/usr/bin/env python3 """ Description: ------------ This Python script (assumes Python3) downloads boundary conditions files from AWS S3 to a target directory for the requested date range. Remarks: -------- (1) Jiawei Zhuang found that it is much faster to issue aws s3 cp commands from a bash script than a Python script. Therefore, in this routine we create a bash script with all of the download commands that will be executed by the main routine. """ # Imports import os import sys import subprocess # Exit with error if we are not using Python3 assert sys.version_info.major >= 3, "ERROR: Python 3 is required to run download_bc.py!" # Define global variables DATA_DOWNLOAD_SCRIPT = "./auto_generated_download_script.sh" def list_missing_files(start_date, end_date, destination): """ Creates list of BC files in date range that do not already exist at destination. Args: ----- start_date : str Initial date of simulation. end_date : str Final date of simulation. destination : str Target directory for downloaded files """ missing_files = [] start_str = str(start_date) start_year = start_str[:4] start_month = start_str[4:6] start_day = start_str[6:8] end_str = str(end_date) end_year = end_str[:4] end_month = end_str[4:6] end_day = end_str[6:8] month_days = [31, [28, 29], 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] file_prefix = "GEOSChem.BoundaryConditions." file_suffix = "_0000z.nc4" for year in range(int(start_year), int(end_year) + 1): # skip years with definite no data if year < 2018: print( "Skipping BC data download for ", str(year), ": no data from this year" ) continue init_month = 1 final_month = 12 if year == int(start_year): # only get desired months from incomplete years init_month = int(start_month) if year == int(end_year): final_month = int(end_month) for month in range(init_month, final_month + 1): # skip months with definite no data if year == 2018 and month < 4: print( "Skipping BC data download for ", str(year), "/0", str(month), ": no data from this month", ) continue # add 0 to month string if necessary month_prefix = "0" if month < 10 else "" init_day = 1 final_day = month_days[month - 1] # leap day if month == 2: if year % 4 == 0: final_day = final_day[1] else: final_day = final_day[0] if month == int(start_month) and year == int(start_year): # only get desired days from incomplete months init_day = int(start_day) if month == int(end_month) and year == int(end_year): final_day = int(end_day) for day in range(init_day, final_day + 1): # add 0 to day string if necessary day_prefix = "0" if day < 10 else "" # check if file for this day already exists file_name = ( file_prefix + str(year) + month_prefix + str(month) + day_prefix + str(day) + file_suffix ) # add file to download list if needed if not os.path.exists(destination + "/" + file_name): missing_files.append(file_name) return missing_files def create_download_script(paths, destination): """ Creates a data download script to obtain missing files Args: ----- paths : dict Output of function list_missing_files. """ # Create the data download script with open(DATA_DOWNLOAD_SCRIPT, "w") as f: # Write shebang line to script print("#!/bin/bash\n", file=f) print("# This script was generated by download_bc.py\n", file=f) cmd_prefix = "aws s3 cp --only-show-errors --request-payer=requester " remote_root = "s3://imi-boundary-conditions/" # make destination if needed if not os.path.exists(destination): os.mkdir(destination) # Write download commands for only the missing data files for path in paths: cmd = cmd_prefix + remote_root + path + " " + destination print(cmd, file=f) print(file=f) # Close file and make it executable f.close() os.chmod(DATA_DOWNLOAD_SCRIPT, 0o755) def download_the_data(start_date, end_date, destination): """ Downloads required boundary conditions files from AWS. Args: ----- start_date : str Initial date of simulation. end_date : str Final date of simulation. destination : str Target directory for downloaded files """ # Get a list of missing data paths paths = list_missing_files(start_date, end_date, destination) # Create script to download missing files from AWS S3 create_download_script(paths, destination) # Run the data download script and return the status # Remove the file afterwards status = subprocess.call(DATA_DOWNLOAD_SCRIPT) os.remove(DATA_DOWNLOAD_SCRIPT) # Raise an exception if the data was not successfully downloaded if status != 0: err_msg = "Error downloading data from AWS!" raise Exception(err_msg) def main(): """ Main program. Gets command-line arguments and calls function download_the_data to initiate a data-downloading process. Calling sequence: ----------------- ./download_data.py start_date end_date destination Example call: ------------- ./download_data.py 20200501 20200531 /home/ubuntu/ExtData/BoundaryConditions """ download_the_data(sys.argv[1], sys.argv[2], sys.argv[3]) if __name__ == "__main__": main()
6,298
1,813
from __future__ import absolute_import, unicode_literals # This will make sure the app is always imported when # Django starts so that shared_task will use this app. # import pymysql # # pymysql.install_as_MySQLdb()
216
66
____notmain: BeginFunc 0 ; EndFunc ;
39
19
""" Ejercicio para operación entre currencies """ """ Representación del currency""" class Curency: def __init__(self, name, symbol, factor): self.name = name self.symbol = symbol self.factor = factor # No me queda muy claro el uso de esta función, sirve para mostrar puntualmente qué? # def __repr__(self): # info = self.name # info2 = self.symbol # return info, info2 euro = Curency("Euro","EU","3.2") print(euro)
483
161
import os from IPython import get_ipython # need this fix first os.environ["LD_PRELOAD"] = "" os.system("apt remove libtcmalloc-minimal4") os.system("apt install libtcmalloc-minimal4") os.environ["LD_PRELOAD"] = "/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.3.0" os.system("dpkg -L libtcmalloc-minimal4") # then install blender url = "https://download.blender.org/release/Blender2.83/blender-2.83.0-linux64.tar.xz" os.system(f"curl {url} | tar xJ") os.system("ln -s /content/blender-2.83.0-linux64/blender /usr/local/bin/blender") # show result get_ipython().system("blender -v")
587
242
from django.db import models from django_countries.fields import CountryField from localflavor.us.models import USStateField from base.models import EONBaseModel class Amenity(EONBaseModel): """Common amenities, i.e. Pool, Lake, Hiking Trail, Primitive Camping, etc.""" name = models.CharField(max_length=50) code = models.CharField(max_length=5) description = models.TextField(null=True, blank=True) def __str__(self): return self.name class Park(EONBaseModel): """General Park Information Utilized for Reference""" PARK_TYPE_STATE = 'state' PARK_TYPE_NATIONAL = 'national' PARK_TYPE_CITY = 'city' PARK_TYPE_OTHER = 'other' PARK_TYPE_CHOICES = ( (PARK_TYPE_STATE, 'State'), (PARK_TYPE_NATIONAL, 'National'), (PARK_TYPE_CITY, 'City'), (PARK_TYPE_OTHER, 'Other') ) park_type = models.CharField(max_length=20, choices=PARK_TYPE_CHOICES) name = models.CharField(max_length=100) description = models.TextField(null=True, blank=True) photos = models.ManyToManyField('base.Photo', through='ParkPhoto') address_one = models.CharField(max_length=50) address_two = models.CharField(max_length=50, null=True, blank=True) city = models.CharField(max_length=50) state = USStateField(blank=True, null=True) country = CountryField() postal_code = models.CharField(blank=True, null=True, max_length=20) amenities = models.ManyToManyField(Amenity, through='ParkAmenity') topic = models.ForeignKey('base.Topic', on_delete=models.SET_NULL, null=True) def __str__(self): return '{name} - {park_type}'.format( name=self.name, park_type=self.get_park_type_display(), ) class ParkAmenity(EONBaseModel): park = models.ForeignKey(Park, on_delete=models.CASCADE) amenity = models.ForeignKey(Amenity, on_delete=models.CASCADE) additional_info = models.TextField(blank=True) class ParkPhoto(EONBaseModel): photo = models.ForeignKey('base.Photo', on_delete=models.CASCADE, related_name='park_photos') park = models.ForeignKey(Park, on_delete=models.DO_NOTHING, related_name='park_photos') def __str__(self): return '{park_name} - Photo:{photo_name}'.format( park_name=self.park.name, photo_name=self.photo.name, )
2,352
808
#!/usr/bin/env python2.7 import sys reload(sys) sys.setdefaultencoding("utf-8") from flask import Flask, render_template, request, jsonify import argparse import datetime import os, sys import requests from socket import error as SocketError import errno import json import pika import uuid app = Flask(__name__) @app.route("/book", methods=["GET"]) def book(): starttime=request.args.get('starttime', '') endtime=request.args.get('endtime', '') user_name=request.args.get('user_name', '') user_email=request.args.get('user_email', '') room_name=request.args.get('room_name', '') if starttime is None or endtime is None or user_name is None or user_email is None or room_name is None: return "no parameter provided to book request\n" data = { "cmd": "book", "data": {"starttime": starttime, "endtime": endtime, "user_name": user_name, "user_email": user_email, "room_name": room_name} } message = json.dumps(data) return send_message_to_queue(message) @app.route("/dispo", methods=["GET"]) def dispo(): key=request.args.get('key', '') sys.stderr.write( "key: "+str(key)+'\r\n') if key is not None and str(key) is not "": data = { "cmd": "dispo", "data": {"key": key} } message = json.dumps(data) return send_message_to_queue(message) return "no parameter provided to dispo request\n" def on_response(ch, method, props, body): global corr_id global response if corr_id == props.correlation_id: response = body def send_message_to_queue(message): global corr_id global response global connection global channel global callback_queue response=None connection = pika.BlockingConnection(pika.ConnectionParameters(host=rabbitmq,port=int(rabbitmq_port),heartbeat_interval=30)) channel = connection.channel() result=channel.queue_declare(exclusive=True) callback_queue = result.method.queue channel.basic_consume(on_response, no_ack=True, queue=callback_queue) corr_id=str(uuid.uuid4()) response = None corr_id = str(uuid.uuid4()) channel.basic_publish( exchange='', routing_key="rpc_queue", properties=pika.BasicProperties( reply_to = callback_queue, correlation_id = corr_id), body=message) print(" [x] Sent data to RabbitMQ") while response is None: connection.process_data_events() print(" [x] Get response from RabbitMQ") print "response: "+str(response) return response if __name__ == '__main__': from argparse import ArgumentParser parser = ArgumentParser("Room Finder Dispo Service") parser.add_argument("-r","--rabbitmq", help="IP or hostname for rabbitmq server, e.g. 'rabbit.domain.com'.") parser.add_argument("-p","--port", help="tcp port for rabitmq server, e.g. '2765'.") args = parser.parse_args() rabbitmq = args.rabbitmq if (rabbitmq == None): rabbitmq = os.getenv("roomfinder_rabbitmq_server") if (rabbitmq == None): get_rabbitmq_server = raw_input("What is the rabbitmq server IP or hostname? ") rabbitmq = get_rabbitmq_server rabbitmq_port = args.port if (rabbitmq_port == None): rabbitmq_port = os.getenv("roomfinder_rabbitmq_port") if (rabbitmq_port == None): get_rabbitmq_port = raw_input("What is the rabbitmq TCP port? ") rabbitmq_port = get_rabbitmq_port try: app.run(host='0.0.0.0', port=int("5000")) except: try: app.run(host='0.0.0.0', port=int("5000")) except: print "Dispo web services error"
3,891
1,246
import numpy as np from ComponentStructural import ComponentStructural from ..subsystems import SubsystemStructural class Component3D(ComponentStructural): pass
167
42
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler import logging from utils import TELEGRAM_TOKEN from handlers import start, ask_new_url, get_url, get_description, cancel from handlers import URL_URL, URL_DESCRIPTION logging.basicConfig(format='%(levelname)s - %(message)s', level=logging.DEBUG) logger = logging.getLogger(__name__) updater = None def start_bot(): global updater updater = Updater(TELEGRAM_TOKEN, use_context=True) dispatcher = updater.dispatcher dispatcher.add_handler(CommandHandler('start', start)) conversation_url_handler = ConversationHandler( entry_points=[CommandHandler('url', ask_new_url)], states={ URL_URL: [MessageHandler(Filters.text, get_url)], URL_DESCRIPTION: [MessageHandler(Filters.text, get_description)], }, fallbacks=[MessageHandler(Filters.command, cancel)] ) dispatcher.add_handler(conversation_url_handler) updater.start_polling(timeout=30) updater.idle() start_bot()
1,063
341
#!/usr/bin/env python """ Simple surface plot example """ from gr import * from math import * x = [-2 + i * 0.5 for i in range(0, 29)] y = [-7 + i * 0.5 for i in range(0, 29)] z = list(range(0, 841)) for i in range(0, 29): for j in range(0, 29): r1 = sqrt((x[j] - 5)**2 + y[i]**2) r2 = sqrt((x[j] + 5)**2 + y[i]**2) z[i * 29 - 1 + j] = (exp(cos(r1)) + exp(cos(r2)) - 0.9) * 25 setcharheight(24.0/500) settextalign(TEXT_HALIGN_CENTER, TEXT_VALIGN_TOP) textext(0.5, 0.9, "Surface Example") (tbx, tby) = inqtextext(0.5, 0.9, "Surface Example") fillarea(tbx, tby) setwindow(-2, 12, -7, 7) setspace(-80, 200, 45, 70) setcharheight(14.0/500) axes3d(1, 0, 20, -2, -7, -80, 2, 0, 2, -0.01) axes3d(0, 1, 0, 12, -7, -80, 0, 2, 0, 0.01) titles3d("X-Axis", "Y-Axis", "Z-Axis") surface(x, y, z, 3) surface(x, y, z, 1) updatews()
841
493
# function that will be distributed def inc(x): return x + 2 # wrapper function, uses the dask client object def main(context, x=1, y=2): context.logger.info(f"params: x={x},y={y}") print(f"params: x={x},y={y}") x = context.dask_client.submit(inc, x) print(x) print(x.result()) context.log_result("y", x.result())
344
129
"""Main functionalities.""" from .image.image import (Image, RawDataContainer) from .image.color_format import AVAILABLE_FORMATS from .parser.factory import ParserFactory import cv2 as cv import os def load_image(file_path, color_format, width): try: image = Image.from_file(file_path) parser = ParserFactory.create_object( determine_color_format(color_format)) except Exception as e: print(type(e).__name__, e) image = parser.parse(image.data_buffer, determine_color_format(color_format), width) return image def get_displayable(image): if image.color_format is None: raise Exception("Image should be already parsed!") parser = ParserFactory.create_object(image.color_format) return parser.get_displayable(image) def determine_color_format(format_string): if format_string in AVAILABLE_FORMATS.keys(): return AVAILABLE_FORMATS[format_string] else: raise NotImplementedError( "Provided string is not name of supported format.") def save_image_as_file(image, file_path): directory = file_path.replace('\\', '/') if directory.rfind('/') == -1: directory = './' else: directory = directory[:directory.rfind("/")] if not os.path.isdir(directory): os.makedirs(directory) try: cv.imwrite(file_path, cv.cvtColor(image, cv.COLOR_RGB2BGR)) except Exception as e: print(type(e).__name__, e)
1,495
464
"""demo URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.10/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url, include from django.conf import settings from django.contrib import admin from django.views.generic import TemplateView import debug_toolbar from rockband import rocking_urls # from movies import urls as movie_urls from async import async_urls urlpatterns = [ url(r'^$', TemplateView.as_view(template_name="index.html")), url(r'^admin/', admin.site.urls), # Rock band urls url(r'^bands/', include(rocking_urls)), # asynchronous demo app url(r'^async/', include(async_urls)), # url(r'$movies/', include(movie_urls)) # Django auth views url('^', include('django.contrib.auth.urls')), ] # For the debug toolbar if settings.DEBUG: urlpatterns += [ url(r'^__debug__/', include(debug_toolbar.urls)), ]
1,423
460
from django.urls import path from .views import teacherregister,studentregister,login_view,logout from . import views from .views import ( ClassroomCreateView,ClassroomListView,ClassroomDetailView,ClassroomUpdateView,ClassroomDeleteView, SubjectCreateView,SubjectListView,SubjectDetailView,SubjectUpdateView,SubjectDeleteView, ClassMemberCreateView,ClassMemberListView,ClassMemberDetailView,ClassMemberUpdateView,ClassMemberDeleteView, TimetableCreateView,TimetableListView,TimetableDetailView,TimetableUpdateView,TimetableDeleteView,CrudView,chatroom ) urlpatterns = [ path('', views.index, name='index'), path('health', views.health, name='health'), path('404', views.handler404, name='404'), path('500', views.handler500, name='500'), path('signup/teacher', teacherregister,name='register-teacher'), path('signup/student', studentregister,name='register-student'), path('accounts/login/', login_view, name='login'), path('logout/', logout,name='logout'), #Classroom path('classroom/new', ClassroomCreateView.as_view(),name='classroom-create'), path('classroom_list', ClassroomListView.as_view(),name='classroom-list'), path('classroom/<str:pk>/', ClassroomDetailView.as_view(),name='classroom-detail'), path('classroom/<str:pk>/update', ClassroomUpdateView.as_view(),name='classroom-update'), path('classroom/<str:pk>/delete', ClassroomDeleteView.as_view(),name='classroom-delete'), #path('Classroom/<int:pk>/image',ChildImageUpdateView.as_view(),name='Classroom-image'), #Subject path('subject/new', SubjectCreateView.as_view(),name='subject-create'), path('subject_list', SubjectListView.as_view(),name='subject-list'), path('subject/<int:pk>/', SubjectDetailView.as_view(),name='subject-detail'), path('subject/<int:pk>/update', SubjectUpdateView.as_view(),name='subject-update'), path('subject/<int:pk>/delete', SubjectDeleteView.as_view(),name='subject-delete'), # Class Members path('classmember/new', ClassMemberCreateView.as_view(),name='classmember-create'), path('classmember_list', ClassMemberListView.as_view(),name='classmember-list'), path('classmember/<str:pk>/', ClassMemberDetailView.as_view(),name='classmember-detail'), path('classmember/<str:pk>/update', ClassMemberUpdateView.as_view(),name='classmember-update'), path('classmember/<str:pk>/delete', ClassMemberDeleteView.as_view(),name='classmember-delete'), # TimeTable path('timetable/new', TimetableCreateView.as_view(),name='timetable-create'), path('timetable_list', TimetableListView.as_view(),name='timetable-list'), path('timetable/<int:pk>/', TimetableDetailView.as_view(),name='timetable-detail'), path('timetable/<int:pk>/update', TimetableUpdateView.as_view(),name='timetable-update'), path('timetable/<int:pk>/delete', TimetableDeleteView.as_view(),name='timetable-delete'), # chatroom path('chat/new',chatroom,name='chatroom'), path('crud/',CrudView.as_view(), name='crud_ajax'), ]
3,478
955
#!/usr/bin/env python """ CLI tool to runs various tasks related to QA. """ import os import time from pathlib import Path import sys import traceback import json import yaml import uuid import datetime import click from .run import RunContext from .runners import runners, Job, JobGroup from .runners.lsf import LsfPriority from .conventions import batch_dir, batch_dir, make_batch_dir, make_batch_conf_dir, make_hash from .conventions import serialize_config, deserialize_config, get_settings from .utils import PathType, entrypoint_module, load_tuning_search from .utils import save_outputs_manifest, total_storage from .utils import redirect_std_streams from .utils import getenvs from .api import url_to_dir, print_url from .api import get_outputs, notify_qa_database, serialize_paths from .iterators import iter_inputs, iter_parameters from .config import config_has_error, ignore_config_errors from .config import project, project_root, subproject, config from .config import default_batches_files, get_default_database, default_batch_label, default_platform from .config import get_default_configuration, default_input_type from .config import commit_id, outputs_commit, artifacts_commit, root_qatools, artifacts_commit_root, outputs_commit_root from .config import user, is_ci, on_windows @click.group() @click.pass_context @click.option('--platform', default=default_platform) @click.option('--configuration', '--config', '-c', 'configurations', multiple=True, help="Will be passed to the run function") @click.option('--label', '-l', default=default_batch_label, help="Gives tuning experiments a name.") @click.option('--tuning', default=None, help="Extra parameters for tuning (JSON)") @click.option('--tuning-filepath', type=PathType(), default=None, help="File with extra parameters for tuning") @click.option('--dryrun', is_flag=True, help="Only show the commands that would be executed") @click.option('--share', is_flag=True, help="Show outputs in QA-Board, doesn't just save them locally.") @click.option('--database', type=PathType(), help="Input database location") @click.option('--type', 'input_type', default=default_input_type, help="How we define inputs") @click.option('--offline', is_flag=True, help="Do not notify QA-Board about run statuses.") def qa(ctx, platform, configurations, label, tuning, tuning_filepath, dryrun, share, database, input_type, offline): """Entrypoint to running your algo, launching batchs...""" # We want all paths to be relative to top-most qaboard.yaml # it should be located at the root of the git repository if config_has_error and not ignore_config_errors: click.secho('Please fix the error(s) above in qaboard.yaml', fg='red', err=True, bold=True) exit(1) # Click passes `ctx.obj` to downstream commands, we can use it as a scratchpad # http://click.pocoo.org/6/complex/ ctx.obj = {} will_show_help = '-h' in sys.argv or '--help' in sys.argv noop_command = 'get' in sys.argv or 'init' in sys.argv if root_qatools and root_qatools != Path().resolve() and not will_show_help and not noop_command: ctx.obj['previous_cwd'] = os.getcwd() click.echo(click.style("Working directory changed to: ", fg='blue') + click.style(str(root_qatools), fg='blue', bold=True), err=True) os.chdir(root_qatools) # We want open permissions on outputs and artifacts # it makes collaboration among mutliple users / automated tools so much easier... os.umask(0) ctx.obj['project'] = project ctx.obj['project_root'] = project_root ctx.obj['subproject'] = subproject ctx.obj['HOST'] = os.environ.get('HOST', os.environ.get('HOSTNAME')) ctx.obj['user'] = user ctx.obj['dryrun'] = dryrun ctx.obj['share'] = share ctx.obj['offline'] = offline ctx.obj['outputs_commit'] = outputs_commit ctx.obj['artifacts_commit'] = artifacts_commit # Note: to support multiple databases per project, # either use / as database, or somehow we need to hash the db in the output path. ctx.obj['raw_batch_label'] = label ctx.obj['batch_label'] = label if not share else f"@{user}| {label}" ctx.obj['platform'] = platform ctx.obj['input_type'] = input_type ctx.obj['inputs_settings'] = get_settings(input_type, config) ctx.obj['database'] = database if database else get_default_database(ctx.obj['inputs_settings']) # configuration singular is for backward compatibility to a time where there was a single str config ctx.obj['configuration'] = ':'.join(configurations) if configurations else get_default_configuration(ctx.obj['inputs_settings']) # we should refactor the str configuration away completly, and do a much simpler parsing, like # deserialize_config = lambda configurations: return [maybe_json_loads(c) for c in configurations] ctx.obj['configurations'] = deserialize_config(ctx.obj['configuration']) ctx.obj['extra_parameters'] = {} if tuning: ctx.obj['extra_parameters'] = json.loads(tuning) elif tuning_filepath: ctx.obj['tuning_filepath'] = tuning_filepath with tuning_filepath.open('r') as f: if tuning_filepath.suffix == '.yaml': ctx.obj['extra_parameters'] = yaml.load(f, Loader=yaml.SafeLoader) elif tuning_filepath.suffix == '.cde': from cde import Config ctx.obj['extra_parameters'] = Config.loads(f.read()).asdict() else: ctx.obj['extra_parameters'] = json.load(f) # batch runs will override this since batches may have different configurations ctx.obj['batch_conf_dir'] = make_batch_conf_dir(outputs_commit, ctx.obj['batch_label'], platform, ctx.obj['configurations'], ctx.obj['extra_parameters'], share) ctx.obj['batch_dir'] = make_batch_dir(outputs_commit, ctx.obj['batch_label'], platform, ctx.obj['configurations'], ctx.obj['extra_parameters'], share) # For convenience, we allow users to change environment variables using {ENV: {VAR: value}} # in configurations or tuning parameters environment_variables = {} for c in ctx.obj['configurations']: if not isinstance(c, dict): continue if 'ENV' in c: environment_variables.update(c['ENV']) if 'ENV' in ctx.obj['extra_parameters']: environment_variables.update(ctx.obj['extra_parameters']['ENV']) os.environ.update(environment_variables) # we manage stripping ansi color codes ourselfs since we redirect std streams # to both the original stream and a log file ctx.color = True # colors in log files colors will be interpreted in the UIs ctx.obj['color'] = is_ci or share @qa.command() @click.option('-i', '--input', 'input_path', type=PathType(), help='Path of the input/recording/test we should work on, relative to the database directory.') @click.option('-o', '--output', 'output_path', type=PathType(), default=None, help='Custom output directory path. If not provided, defaults to ctx.obj["batch_conf_dir"] / input_path.with_suffix('')') @click.argument('variable') @click.pass_context def get(ctx, input_path, output_path, variable): """Prints the value of the requested variable. Mostly useful for debug.""" try: output_directory = ctx.obj['batch_conf_dir'] / input_path.with_suffix('') if not output_path else output_path except: pass from .config import outputs_commit, commit_branch, artifacts_branch_root # backward compatibility if variable == "branch_ci_dir": variable = "artifacts_branch_root" if variable == "commit_ci_dir": variable = "outputs_commit" locals().update(globals()) locals().update(ctx.obj) if variable in locals(): print(locals().get(variable)) else: click.secho(f"Could not find {variable}", err=True, fg='red') exit(1) @qa.command(context_settings=dict( ignore_unknown_options=True, allow_interspersed_args=False, )) @click.pass_context @click.option('-i', '--input', 'input_path', required=True, type=PathType(), help='Path of the input/recording/test we should work on, relative to the database directory.') @click.option('-o', '--output', 'output_path', type=PathType(), default=None, help='Custom output directory path. If not provided, defaults to ctx.obj["batch_conf_dir"] / input_path.with_suffix('')') @click.option('--keep-previous', is_flag=True, help="Don't clean previous outputs before the run.") @click.option('--no-postprocess', is_flag=True, help="Don't do the postprocessing.") @click.option('--save-manifests-in-database', is_flag=True, help="Save the input and outputs manifests in the database.") @click.argument('forwarded_args', nargs=-1, type=click.UNPROCESSED) def run(ctx, input_path, output_path, keep_previous, no_postprocess, forwarded_args, save_manifests_in_database): """ Runs over a given input/recording/test and computes various success metrics and outputs. """ run_context = RunContext.from_click_run_context(ctx, config) # Usually we want to remove any files already present in the output directory. # It avoids issues with remaining state... This said, # In some cases users want to debug long, multi-stepped runs, for which they have their own caching if not keep_previous: import shutil shutil.rmtree(run_context.output_dir, ignore_errors=True) run_context.output_dir.mkdir(parents=True, exist_ok=True) with (run_context.output_dir / 'run.json').open('w') as f: json.dump({ # run_context.database is always made absolute, we keep it relative if given so "database": str(ctx.obj["database"]), "input_path": str(run_context.rel_input_path), "input_type": run_context.type, "configurations": run_context.configurations, "extra_parameters": run_context.extra_parameters, "platform": run_context.platform, }, f, sort_keys=True, indent=2, separators=(',', ': ')) # Without this, we can only log runs from `qa batch`, on linux, via LSF # this redirect is not 100% perfect, we don't get stdout from C calls # if not 'LSB_JOBID' in os.environ: # When using LSF, we usally already have incremental logs with redirect_std_streams(run_context.output_dir / 'log.txt', color=ctx.obj['color']): # Help reproduce qa runs with something copy-pastable in the logs if is_ci: from shlex import quote click.secho(' '.join(['qa', *map(quote, sys.argv[1:])]), fg='cyan', bold=True) click.echo(click.style("Outputs: ", fg='cyan') + click.style(str(run_context.output_dir), fg='cyan', bold=True), err=True) print_url(ctx) if not ctx.obj['offline']: notify_qa_database(**ctx.obj, is_pending=True, is_running=True) start = time.time() cwd = os.getcwd() try: runtime_metrics = entrypoint_module(config).run(run_context) except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() click.secho(f'[ERROR] Your `run` function raised an exception: {e}', fg='red', bold=True) try: exc_type, exc_value, exc_traceback = sys.exc_info() click.secho(''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)), fg='red') except Exception as e: # debug strange stale file errors, ideally remove this... print(f"ERROR: {e}") runtime_metrics = {'is_failed': True} if not runtime_metrics: click.secho('[WARNING] Your `run` function should return a dict with a least {"is_failed": False}', fg='yellow') runtime_metrics = {"is_failed": False} if not isinstance(runtime_metrics, dict): click.secho(f'[ERROR] Your `run` function did not return a dict, but {runtime_metrics}', fg='red', bold=True) runtime_metrics = {'is_failed': True} runtime_metrics['compute_time'] = time.time() - start # avoid issues if code in run() changes cwd if os.getcwd() != cwd: os.chdir(cwd) metrics = postprocess_(runtime_metrics, run_context, skip=no_postprocess or runtime_metrics['is_failed'], save_manifests_in_database=save_manifests_in_database) if not metrics: metrics = runtime_metrics if metrics['is_failed']: click.secho('[ERROR] The run has failed.', fg='red', err=True) click.secho(str(metrics), fg='red', bold=True) exit(1) else: click.secho(str(metrics), fg='green') def postprocess_(runtime_metrics, run_context, skip=False, save_manifests_in_database=False): """Computes computes various success metrics and outputs.""" from .utils import file_info try: if not skip: try: entrypoint_postprocess = entrypoint_module(config).postprocess except: metrics = runtime_metrics else: metrics = entrypoint_postprocess(runtime_metrics, run_context) else: metrics = runtime_metrics except: exc_type, exc_value, exc_traceback = sys.exc_info() # TODO: in case of import error because postprocess was not defined, just ignore it...? # TODO: we should provide a default postprocess function, that reads metrics.json and returns {**previous, **runtime_metrics} exc_type, exc_value, exc_traceback = sys.exc_info() click.secho(f'[ERROR] Your `postprocess` function raised an exception:', fg='red', bold=True) click.secho(''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)), fg='red') metrics = {**runtime_metrics, 'is_failed': True} if 'is_failed' not in metrics: click.secho("[Warning] The result of the `postprocess` function misses a key `is_failed` (bool)", fg='yellow') metrics['is_failed'] = False if (run_context.output_dir / 'metrics.json').exists(): with (run_context.output_dir / 'metrics.json').open('r') as f: previous_metrics = json.load(f) metrics = { **previous_metrics, **metrics, } with (run_context.output_dir / 'metrics.json').open('w') as f: json.dump(metrics, f, sort_keys=True, indent=2, separators=(',', ': ')) # To help identify if input files change, we compute and save some metadata. if is_ci or save_manifests_in_database: manifest_inputs = run_context.obj.get('manifest-inputs', [run_context.input_path]) input_files = {} for manifest_input in manifest_inputs: manifest_input = Path(manifest_input) if manifest_input.is_dir(): for idx, path in enumerate(manifest_input.rglob('*')): if idx >= 200: break if not path.is_file(): continue input_files[path.as_posix()] = file_info(path, config=config) elif manifest_input.is_file(): input_files.update({manifest_input.as_posix(): file_info(manifest_input, config=config)}) with (run_context.output_dir / 'manifest.inputs.json').open('w') as f: json.dump(input_files, f, indent=2) outputs_manifest = save_outputs_manifest(run_context.output_dir, config=config) output_data = { 'storage': total_storage(outputs_manifest), } if save_manifests_in_database: if run_context.input_path.is_file(): click.secho('WARNING: saving the manifests in the database is only implemented for inputs that are *folders*.', fg='yellow', err=True) else: from .utils import copy copy(run_context.output_dir / 'manifest.inputs.json', run_context.input_path / 'manifest.inputs.json') copy(run_context.output_dir / 'manifest.outputs.json', run_context.input_path / 'manifest.outputs.json') if not run_context.obj.get('offline') and not run_context.obj.get('dryrun'): notify_qa_database(**run_context.obj, metrics=metrics, data=output_data, is_pending=False, is_running=False) return metrics @qa.command(context_settings=dict( ignore_unknown_options=True, )) @click.pass_context @click.option('-i', '--input', 'input_path', required=True, type=PathType(), help='Path of the input/recording/test we should work on, relative to the database directory.') @click.option('-o', '--output', 'output_path', type=PathType(), default=None, help='Custom output directory path. If not provided, defaults to ctx.obj["batch_conf_dir"] / input_path.with_suffix('')') @click.argument('forwarded_args', nargs=-1, type=click.UNPROCESSED) def postprocess(ctx, input_path, output_path, forwarded_args): """Run only the post-processing, assuming results already exist.""" run_context = RunContext.from_click_run_context(ctx, config) with redirect_std_streams(run_context.output_dir / 'log.txt', color=ctx.obj['color']): click.echo(click.style("Outputs: ", fg='cyan') + click.style(str(run_context.output_dir), fg='cyan', bold=True), err=True) print_url(ctx) metrics = postprocess_({}, run_context) if metrics['is_failed']: click.secho('[ERROR] The run has failed.', fg='red', err=True, bold=True) click.secho(str(metrics), fg='red') else: click.secho(str(metrics), fg='green') @qa.command(context_settings=dict( ignore_unknown_options=True, )) @click.pass_context @click.option('-i', '--input', 'input_path', required=True, type=PathType(), help='Path of the input/recording/test we should work on, relative to the database directory.') @click.option('-o', '--output', 'output_path', type=PathType(), default=None, help='Custom output directory path. If not provided, defaults to ctx.obj["batch_conf_dir"] / input_path.with_suffix('')') def sync(ctx, input_path, output_path): """Updates the database metrics using metrics.json""" run_context = RunContext.from_click_run_context(ctx, config) if (run_context.output_dir / 'metrics.json').exists(): with (run_context.output_dir / 'metrics.json').open('r') as f: metrics = json.load(f) notify_qa_database(**ctx.obj, metrics=metrics, is_pending=False, is_running=False) click.secho(str(metrics), fg='green') @qa.command(context_settings=dict( ignore_unknown_options=True, )) @click.pass_context @click.option('--output-id', 'output_id', help='Custom output directory path. If not provided, defaults to ctx.obj["batch_conf_dir"] / input_path.with_suffix('')') def wait(ctx, output_id): from .api import get_output while True: output = get_output(output_id) click.secho("...waiting") if output["is_pending"]: time.sleep(5) continue break exit(0 if not output["is_failed"] else 1) runners_config = config.get('runners', {}) if 'default' in runners_config: default_runner = runners_config['default'] else: task_runners = [r for r in runners_config if r not in ['default', 'local']] default_runner = task_runners[0] if task_runners else 'local' lsf_config = config['lsf'] if 'lsf' in config else config.get('runners', {}).get('lsf', {}) if 'lsf' in config: default_runner = 'lsf' if default_runner == 'lsf' and os.name=='nt': default_runner = 'local' local_config = config.get('runners', {}).get('local', {}) @qa.command(context_settings=dict( ignore_unknown_options=True, )) @click.option('--batch', '-b', 'batches', multiple=True, help="We run over all inputs+configs+database in those batches") @click.option('--batches-file', 'batches_files', type=PathType(), default=default_batches_files, multiple=True, help="YAML files listing batches of inputs+configs+database.") @click.option('--tuning-search', 'tuning_search_dict', help='string containing JSON describing the tuning parameters to explore') @click.option('--tuning-search-file', type=PathType(), default=None, help='tuning file describing the tuning parameters to explore') @click.option('--no-wait', is_flag=True, help="If true, returns as soon as the jobs are sent, otherwise waits for completion.") @click.option('--list', 'list_contexts', is_flag=True, help="Print as JSON details about each run we would do.") @click.option('--list-output-dirs', is_flag=True, help="Only print the prefixes for the results of each batch we run on.") @click.option('--list-inputs', is_flag=True, help="Print to stdout a JSON with a list of the inputs we would call qa run on.") @click.option('--runner', default=default_runner, help="Run runs locally or using a task queue like Celery, LSF...") @click.option('--local-concurrency', default=os.environ.get('QA_BATCH_CONCURRENCY', local_config.get('concurrency')), type=int, help="joblib's n_jobs: 0=unlimited, 2=2 at a time, -1=#cpu-1") @click.option('--lsf-threads', default=lsf_config.get('threads', 0), type=int, help="restrict number of lsf threads to use. 0=no restriction") @click.option('--lsf-memory', default=lsf_config.get('memory', 0), help="restrict memory (MB) to use. 0=no restriction") @click.option('--lsf-queue', default=lsf_config.get('queue'), help="LSF queue (-q)") @click.option('--lsf-fast-queue', default=lsf_config.get('fast_queue', lsf_config.get('queue')), help="Fast LSF queue, for interactive jobs") @click.option('--lsf-resources', default=lsf_config.get('resources', None), help="LSF resources restrictions (-R)") @click.option('--lsf-priority', default=lsf_config.get('priority', 0), type=int, help="LSF priority (-sp)") @click.option('--action-on-existing', default=config.get('outputs', {}).get('action_on_existing', "run"), help="When there are already finished successful runs, whether to do run / postprocess (only) / sync (re-use results) / skip") @click.option('--action-on-pending', default=config.get('outputs', {}).get('action_on_pending', "wait"), help="When there are already pending runs, whether to do wait (then run) / sync (use those runs' results) / skip (don't run) / continue (run as usual, can cause races)") @click.option('--prefix-outputs-path', type=PathType(), default=None, help='Custom prefix for the outputs; they will be at $prefix/$output_path') @click.argument('forwarded_args', nargs=-1, type=click.UNPROCESSED) @click.pass_context def batch(ctx, batches, batches_files, tuning_search_dict, tuning_search_file, no_wait, list_contexts, list_output_dirs, list_inputs, runner, local_concurrency, lsf_threads, lsf_memory, lsf_queue, lsf_fast_queue, lsf_resources, lsf_priority, action_on_existing, action_on_pending, prefix_outputs_path, forwarded_args): """Run on all the inputs/tests/recordings in a given batch using the LSF cluster.""" if not batches_files: click.secho(f'WARNING: Could not find how to identify input tests.', fg='red', err=True, bold=True) click.secho(f'Consider adding to qaboard.yaml somelike like:\n```\ninputs:\n batches: batches.yaml\n```', fg='red', err=True) click.secho(f'Where batches.yaml is formatted like in http://qa-docs/docs/batches-running-on-multiple-inputs', fg='red', err=True) return if not batches: if not len(forwarded_args): click.secho(f'ERROR: you must provide a batch', fg='red', err=True, bold=True) click.secho(f'Use either `qa batch BATCH`, or `qa batch --batch BATCH_2 --batch BATCH_2`', fg='red', err=True) exit(1) single_batch, *forwarded_args = forwarded_args batches = [single_batch] print_url(ctx) existing_outputs = get_outputs(ctx.obj) command_id = str(uuid.uuid4()) # unique IDs for triggered runs makes it easier to wait/cancel them os.environ['QA_BATCH']= 'true' # triggered runs will be less verbose than with just `qa run` os.environ['QA_BATCHES_FILES'] = json.dumps([str(b) for b in batches_files]) dryrun = ctx.obj['dryrun'] or list_output_dirs or list_inputs or list_contexts should_notify_qa_database = (is_ci or ctx.obj['share']) and not (dryrun or ctx.obj['offline']) if should_notify_qa_database: command_data = { "command_created_at_datetime": datetime.datetime.utcnow().isoformat(), "argv": sys.argv, "runner": runner, **ctx.obj, } job_url = getenvs(('BUILD_URL', 'CI_JOB_URL', 'CIRCLE_BUILD_URL', 'TRAVIS_BUILD_WEB_URL')) # jenkins, gitlabCI, cirlceCI, travisCI if job_url: command_data['job_url'] = job_url if not os.environ.get('QA_BATCH_COMMAND_HIDE_LOGS'): notify_qa_database(object_type='batch', command={command_id: command_data}, **ctx.obj) tuning_search, filetype = load_tuning_search(tuning_search_dict, tuning_search_file) default_runner_options = { "type": runner, "command_id": command_id, } # Each runner should add what it cares about... # TODO: Having --runner-X prefixes makes it all a mess, but still the help text is useful # TODO: It would be nice to generate the CLI help depending on the runner that's choosen, then we could use if runner == 'lsf': default_runner_options.update({ "project": lsf_config.get('project', str(project) if project else "qaboard"), "max_threads": lsf_threads, "max_memory": lsf_memory, 'resources': lsf_resources, "queue": lsf_queue, "fast_queue": lsf_fast_queue, "user": ctx.obj['user'], }) if runner == "local": default_runner_options["concurrency"] = local_concurrency if runner == 'local' or runner == 'celery': default_runner_options["cwd"] = ctx.obj['previous_cwd'] if 'previous_cwd' in ctx.obj else os.getcwd() jobs = JobGroup(job_options=default_runner_options) inputs_iter = iter_inputs(batches, batches_files, ctx.obj['database'], ctx.obj['configurations'], ctx.obj['platform'], default_runner_options, config, ctx.obj['inputs_settings']) for run_context in inputs_iter: input_configuration_str = serialize_config(run_context.configurations) for tuning_file, tuning_hash, tuning_params in iter_parameters(tuning_search, filetype=filetype, extra_parameters=ctx.obj['extra_parameters']): if not prefix_outputs_path: batch_conf_dir = make_batch_conf_dir( outputs_commit, ctx.obj["batch_label"], run_context.platform, run_context.configurations, tuning_params, ctx.obj['share'] ) else: batch_conf_dir = outputs_commit / prefix_outputs_path if tuning_file: batch_conf_dir = batch_conf_dir / Path(tuning_file).stem from qaboard.conventions import slugify_hash input_dir = run_context.rel_input_path.with_suffix('') if len(input_dir.as_posix()) > 90: input_dir = Path(slugify_hash(input_dir.as_posix(), maxlength=90)) run_context.output_dir = batch_conf_dir / input_dir if forwarded_args: run_forwarded_args = [a for a in forwarded_args if not a in ("--keep-previous", "--no-postprocess", "--save-manifests-in-database")] if run_forwarded_args: run_context.extra_parameters = {"forwarded_args": run_forwarded_args, **tuning_params} else: run_context.extra_parameters = tuning_params else: run_context.extra_parameters = tuning_params if list_output_dirs: print(run_context.output_dir) break if list_inputs: print(run_context.input_path) break matching_existing_outputs = [o for o in existing_outputs.values() if url_to_dir(o['output_dir_url']) == run_context.output_dir] matching_existing_output = matching_existing_outputs[0] if matching_existing_outputs else None # at most 1, garanteed by database constaints is_pending = matching_existing_output['is_pending'] if matching_existing_output else False is_failed = matching_existing_output['is_failed'] if matching_existing_output else run_context.is_failed() ran_before = True if matching_existing_output else run_context.ran() should_run = not is_pending and (action_on_existing=='run' or is_failed or not ran_before) if not should_run and action_on_existing=='skip': continue if is_pending and action_on_pending == 'skip': continue if not forwarded_args: forwarded_args_cli = None else: if not on_windows: # FIXME: we assume no single quotes... forwarded_args_cli = ' '.join(f"'{a}'" for a in forwarded_args) else: from .compat import escaped_for_cli forwarded_args_cli = ' '.join(escaped_for_cli(a) for a in forwarded_args) if input_configuration_str == get_default_configuration(ctx.obj['inputs_settings']): configuration_cli = None else: # We can't use --config, or "-c A -c B" until we ensure all clients updated a version supporting it if not on_windows: configuration = input_configuration_str.replace("'", "'\"'\"'") # support single-quotes configuration_cli = f"--configuration '{configuration}'" else: from .compat import escaped_for_cli configuration_cli = f'--configuration {escaped_for_cli(input_configuration_str)}' # We could serialize properly the run_context/runner_options, and e.g. call "qa --pickled-cli" and use the CLI command below just for logs... args = [ f"qa", f'--share' if ctx.obj["share"] else None, f'--offline' if ctx.obj['offline'] else None, f'--label "{ctx.obj["raw_batch_label"]}"' if ctx.obj["raw_batch_label"] != default_batch_label else None, f'--platform "{run_context.platform}"' if run_context.platform != default_platform else None, # TODO: make it customizable in batches f'--type "{run_context.type}"' if run_context.type != default_input_type else None, f'--database "{run_context.database.as_posix()}"' if run_context.database != get_default_database(ctx.obj['inputs_settings']) else None, configuration_cli, f'--tuning-filepath "{tuning_file}"' if tuning_params else None, 'run' if should_run else action_on_existing, f'--input "{run_context.rel_input_path}"', f'--output "{run_context.output_dir}"' if prefix_outputs_path else None, forwarded_args_cli if forwarded_args_cli else None, ] command = ' '.join([arg for arg in args if arg is not None]) click.secho(command, fg='cyan', err=True) click.secho(f" {run_context.output_dir if run_context.output_dir.is_absolute else run_context.output_dir.relative_to(subproject)}", fg='blue', err=True) import re if 'QA_TESTING' in os.environ: # we want to make sure we test the current code command = re.sub('^qa', 'python -m qaboard', command) if str(subproject) != '.': command = f"cd {subproject} && {command}" run_context.command = command run_context.job_options['command_id'] = command_id job = Job(run_context) if should_notify_qa_database and not is_pending: # TODO: accumulate and send all at once to avoid 100s of requests? db_output = notify_qa_database(**{ **ctx.obj, **run_context.obj, # for now we don't want to worry about backward compatibility, and input_path being abs vs relative... "is_pending": True, }) if db_output: # Note: the ID is already in the matching job above job.id = db_output["id"] if is_pending: wait_command = f"qa wait --output-id {matching_existing_output['id']}" if action_on_pending=="sync": job.id = matching_existing_output['id'] job.run_context.command = wait_command elif action_on_pending=="wait": job.run_context.command = f"{wait_command} || {job.run_context.command}" else: assert action_on_pending=="continue" jobs.append(job) if list_contexts: print(json.dumps([serialize_paths(j.run_context.asdict()) for j in jobs], indent=2)) return if not dryrun: is_failed = jobs.start( blocking=not no_wait, qa_context=ctx.obj, ) from .gitlab import gitlab_token, update_gitlab_status if gitlab_token and jobs and is_ci and 'QABOARD_TUNING' not in os.environ: update_gitlab_status(commit_id, 'failed' if is_failed else 'success', ctx.obj["batch_label"], f"{len(jobs)} results") if is_failed and not no_wait: del os.environ['QA_BATCH'] # restore verbosity print_url(ctx, status="failure") exit(1) @qa.command() # Do we want this? we could simply use groups not defined in qatools.yaml:artifacts as paths @click.option('--file', '-f', 'files', multiple=True, help="Save specific files instead of artifacts indicated by yaml file") @click.option('--exclude', 'excluded_groups', multiple=True, help="Exclude specific artifact groups") # Do we use this? yes in the API, but let's deprecate and remove for other uses... @click.option('--out', '-o', 'artifacts_path', default='', help="Path to save artifacts in case of specified files") @click.argument('groups', nargs=-1, type=click.UNPROCESSED, default=None) @click.pass_context def save_artifacts(ctx, files, excluded_groups, artifacts_path, groups): """Save the results at a standard location""" import filecmp from .config import is_in_git_repo, qatools_config_paths from .utils import copy, file_info from .compat import cased_path click.secho(f"Saving artifacts in: {artifacts_commit}", bold=True, underline=True) artifacts = {} if files: artifacts = {f"__{f}": {"glob": f} for f in files} else: if 'artifacts' not in config: config['artifacts'] = {} # We support both qaboard.yaml and qaboard.yaml for backward compatibility with SIRC's projects # Default artifacts config['artifacts']['__qaboard.yaml'] = {"glob": ['qaboard.yaml', 'qatools.yaml']} config['artifacts']['__qatools'] = {"glob": ['qatools/*', 'qa/*']} # Handle sub-projects config['artifacts']['__sub-qaboard.yaml'] = {"glob": [str(p.relative_to(root_qatools).parent / 'qaboard.yaml') for p in qatools_config_paths]} config['artifacts']['__sub-qaboard.yaml'] = {"glob": [str(p.relative_to(root_qatools).parent / 'qatools.yaml') for p in qatools_config_paths]} config['artifacts']['__metrics.yaml'] = {"glob": config.get('outputs', {}).get('metrics')} config['artifacts']['__batches.yaml'] = {"glob": default_batches_files} config['artifacts']['__envrc'] = {"glob": ['.envrc', '**/*.envrc']} if groups: if excluded_groups: groups = [g for g in groups if g not in excluded_groups] artifacts = {g: config['artifacts'][g] for g in groups if g in config['artifacts'].keys()} else: artifacts = config['artifacts'] if 'QA_VERBOSE_VERBOSE' in os.environ: print(artifacts) if not is_in_git_repo: click.secho( "You are not in a git repository, maybe in an artifacts folder. `save_artifacts` is unavailable.", fg='yellow', dim=True) exit(1) for artifact_name, artifact_config in artifacts.items(): click.secho(f'Saving artifacts: {artifact_name}', bold=True) manifest_path = artifacts_commit / 'manifests' / f'{artifact_name}.json' manifest_path.parent.mkdir(parents=True, exist_ok=True) if manifest_path.exists(): with manifest_path.open() as f: try: manifest = json.load(f) except: manifest = {} else: manifest = {} nb_files = 0 globs = artifact_config.get('glob') if not isinstance(globs, list): globs = [globs] for g in globs: if not g: continue for path in Path('.').glob(g): path = cased_path(path) if not path.is_file(): continue if artifacts_path: destination = artifacts_commit_root / artifacts_path / path else: destination = artifacts_commit_root / path if 'QA_VERBOSE_VERBOSE' in os.environ: print(destination) if destination.exists() and filecmp.cmp(str(path), str(destination), shallow=True): # when working on subprojects, the artifact might be copied already, # but manifests are saved per-subproject if path.as_posix() not in manifest: manifest[path.as_posix()] = file_info(path, config=config) continue if 'QA_VERBOSE' in os.environ or ctx.obj['dryrun']: click.secho(str(path), dim=True) if not ctx.obj['dryrun']: copy(path, destination) manifest[path.as_posix()] = file_info(path, config=config) if not ctx.obj['dryrun']: with manifest_path.open('w') as f: json.dump(manifest, f) if nb_files > 0: click.secho(f"{nb_files} files copied") if os.name == "nt" and not ctx.obj['dryrun']: # [Samsung-SIRC specific] print("... Fixing linux file permissions") try: # Windows does not set file permissions correctly on the shared storage, # it does not respect umask 0: files are not world-writable. # Trying to each_file.chmod(0o777) does not work either # The only option is to make the call from linux. # We could save a list of paths and chmod them with their parent directories... # but to make things faster to code, we just "ssh linux chmod everything" # from qaboard.compat import windows_to_linux_path # # We can assume SSH to be present on Windows10 # ssh = f"ssh -i \\\\networkdrive\\home\\{user}\\.ssh\\id_rsa -oStrictHostKeyChecking=no" # chmod = f'{ssh} {user}@{user}-srv \'chmod -R 777 "{windows_to_linux_path(artifacts_commit)}"\'' # print(chmod) # os.system(chmod) pass except Exception as e: print(f'WARNING: {e}') # if the commit was deleted, this notification will mark it as good again notify_qa_database(object_type='commit', **ctx.obj) @qa.command() @click.pass_context @click.option('--batch', '-b', 'batches', required=True, multiple=True, help="Only check bit-accuracy for this batch of inputs+configs+database.") @click.option('--batches-file', 'batches_files', type=PathType(), default=default_batches_files, multiple=True, help="YAML file listing batches of inputs+config+database selected from the database.") def check_bit_accuracy_manifest(ctx, batches, batches_files): """ Checks the bit accuracy of the results in the current ouput directory versus the latest commit on origin/develop. """ from .bit_accuracy import is_bit_accurate commit_dir = outputs_commit if is_ci else Path() all_bit_accurate = True nb_compared = 0 for run_context in iter_inputs(batches, batches_files, ctx.obj['database'], ctx.obj['configurations'], default_platform, {}, config, ctx.obj['inputs_settings']): nb_compared += 1 if run_context.input_path.is_file(): click.secho('ERROR: check_bit_accuracy_manifest only works for inputs that are folders', fg='red', err=True) # otherwise the manifest is at # * input_path.parent / 'manifest.json' in the database # * input_path.with_suffix('') / 'manifest.json' in the results # # reference_output_directory = run_context.input_path if run_context.input_path.is_folder() else run_context.input_path.parent exit(1) batch_conf_dir = make_batch_conf_dir(Path(), ctx.obj['batch_label'], ctx.obj["platform"], run_context.configurations, ctx.obj['extra_parameters'], ctx.obj['share']) input_is_bit_accurate = is_bit_accurate(commit_dir / batch_conf_dir, run_context.database, [run_context.rel_input_path]) all_bit_accurate = all_bit_accurate and input_is_bit_accurate if not all_bit_accurate: click.secho("\nError: you are not bit-accurate versus the manifest.", fg='red', underline=True, bold=True) click.secho("Reminder: the manifest lists the expected inputs/outputs for each test. It acts as an explicit gatekeeper against changes", fg='red', dim=True) if not run_context.database.is_absolute(): click.secho("If that's what you wanted, update and commit all manifests.", fg='red') # click.secho("If that's what you wanted, update all manifests using:", fg='red') # click.secho("$ qa batch * --save-manifests-in-database", fg='red') # click.secho("$ git add # your changes", fg='red') # click.secho("$ git commit # now retry your CI", fg='red') else: click.secho("To update the manifests for all tests, run:", fg='red') click.secho("$ qa batch --save-manifests --batch *", fg='red') exit(1) if not nb_compared: click.secho("\nWARNING: Nothing was compared! It's not likely to be what you expected...", fg='yellow', underline=True, bold=True) @qa.command() @click.pass_context @click.option( "--reference", default=config.get('project', {}).get('reference_branch', 'master'), help="Branch, tag or commit used as reference." ) @click.option('--batch', '-b', 'batches', multiple=True, help="Only check bit-accuracy for those batches of inputs+configs+database.") @click.option('--batches-file', 'batches_files', type=PathType(), default=default_batches_files, multiple=True, help="YAML file listing batches of inputs+config+database selected from the database.") @click.option('--reference-platform', help="Compare against a difference platform.") def check_bit_accuracy(ctx, reference, batches, batches_files, reference_platform): """ Checks the bit accuracy of the results in the current ouput directory versus the latest commit on origin/develop. """ from .config import is_in_git_repo, commit_branch, is_ci, outputs_project_root, repo_root from .bit_accuracy import is_bit_accurate from .gitlab import lastest_successful_ci_commit from .conventions import get_commit_dirs from .git import latest_commit, git_show, git_parents if not is_in_git_repo: click.secho("You are not in a git repository, maybe in an artifacts folder. `check_bit_accuracy` is unavailable.", fg='yellow', dim=True) exit(1) if is_ci and commit_branch == reference: click.secho(f'We are on branch {reference}', fg='cyan', bold=True, err=True) click.secho(f"Comparing bit-accuracy against this commit's ({commit_id[:8]}) parents.", fg='cyan', bold=True, err=True) # It will work until we try to rebase merge requests. # We really should use Gitlab' API (or our database) to ask about previous pipelines on the branch reference_commits = git_parents(commit_id) else: click.secho(f'Comparing bit-accuracy versus the latest remote commit of {reference}', fg='cyan', bold=True, err=True) reference_commits = [latest_commit(reference)] click.secho(f"{commit_id[:8]} versus {reference_commits}.", fg='cyan', err=True) # This where the new results are located commit_dir = outputs_commit_root if is_ci else Path() if not batches: output_directories = list(p.parent.relative_to(commit_dir) for p in (commit_dir / subproject / 'output').rglob('manifest.outputs.json')) else: output_directories = [] for run_context in iter_inputs(batches, batches_files, ctx.obj['database'], ctx.obj['configurations'], default_platform, {}, config, ctx.obj['inputs_settings']): batch_conf_dir = make_batch_conf_dir(subproject, ctx.obj['batch_label'], ctx.obj["platform"], run_context.configurations, ctx.obj["extra_parameters"], ctx.obj['share']) input_path = run_context.input_path.relative_to(run_context.database) output_directory = batch_conf_dir / input_path.with_suffix('') output_directories.append(output_directory) for reference_commit in reference_commits: # if the reference commit is pending or failed, we wait or maybe pick a parent reference_commit = lastest_successful_ci_commit(reference_commit) click.secho(f'Current directory : {commit_dir}', fg='cyan', bold=True, err=True) reference_rootproject_ci_dir = outputs_project_root / get_commit_dirs(reference_commit, repo_root) click.secho(f"Reference directory: {reference_rootproject_ci_dir}", fg='cyan', bold=True, err=True) all_bit_accurate = True for o in output_directories: all_bit_accurate = is_bit_accurate(commit_dir, reference_rootproject_ci_dir, [o], reference_platform) and all_bit_accurate if not all_bit_accurate: click.secho(f"\nERROR: results are not bit-accurate to {reference_commits}.", bg='red', bold=True) if is_ci: click.secho(f"\nTo investigate, go to", fg='red', underline=True) for reference_commit in reference_commits: click.secho(f"https://qa/{project.as_posix()}/commit/{commit_id}?reference={reference_commit}&selected_views=bit_accuracy", fg='red') exit(1) from .optimize import optimize qa.add_command(optimize) # TODO: split more... # from .bit_accuracy import check_bit_accuracy, check_bit_accuracy_manifest # qa.add_command(check_bit_accuracy) # qa.add_command(check_bit_accuracy_manifest) @qa.command() @click.pass_context def init(ctx): """Provide a sample qaboard.yaml configuration.""" from .init import qa_init qa_init(ctx) def main(): from .compat import ensure_cli_backward_compatibility ensure_cli_backward_compatibility() qa(obj={}, auto_envvar_prefix='QA') if __name__ == '__main__': main()
44,634
14,258
# -*- coding: utf-8 -*- import threading import time import global_def as gd from db_reader import DbReaderDef, DbReaer from queue import Queue, Empty class DataHandlerThd(threading.Thread): def __init__(self, req_queue: Queue, rcv_queue: Queue, db_host: str, db_port: int, db_user: str, db_pw: str, db_name: str, db_char_set: str = 'utf8'): threading.Thread.__init__(self) self._db_host = db_host self._db_port = db_port self._db_user = db_user self._db_pw = db_pw self._db_name = db_name self._db_char_set = db_char_set self._req_queue = req_queue self._rcv_queue = rcv_queue self.is_run = False def _send_err_msg(self, msg: str) -> None: self._rcv_queue.put({ gd.KEY_NM_EVT: gd.EVT_TYPE_ERR, gd.KEY_NM_MSG: msg }) def _read_db(self, req: dict) -> bool: req_date = int(req.get(gd.KEY_NM_DATE, 0)) tbl_infos = req.get(gd.KEY_NM_TBL_INFOS, None) if 19900101 > req_date or 30000101 < req_date: self._send_err_msg('Invalid Date') return False if list != type(tbl_infos) or 0 == len(tbl_infos): self._send_err_msg('Invalid Table Infos1') return False db_readers = [] for reader_idx, tbl_info in enumerate(tbl_infos): tbl_nm = tbl_info.get(gd.KEY_NM_TBL_NM, None) col_nms = tbl_info.get(gd.KEY_NM_COL_NMS, []) if tbl_nm is None or 0 == len(col_nms): self._send_err_msg('Invalid Table Infos2') return False db_reader = DbReaer(reader_idx, req_date, tbl_nm, col_nms, self._db_host, self._db_port, self._db_user, self._db_pw, self._db_name, self._db_char_set) db_readers.append(db_reader) for db_reader in db_readers: db_reader.read_thd.start() is_st_read = False is_error = False while not is_st_read: for db_reader in db_readers: thd_state: int = db_reader.get_thd_state() if DbReaderDef.STATE_ERROR == thd_state: is_st_read = True is_error = True break elif DbReaderDef.STATE_READY == thd_state: break else: is_st_read = True time.sleep(0.5) if is_error: for db_reader in db_readers: db_reader.set_stop_thd() time.sleep(1) self._send_err_msg('Error in DbReaderThd1') return False # 처음에 하나씩 데이터를 읽는다 empty_reader_idxs = [] for reader_idx, db_reader in enumerate(db_readers): if not db_reader.read_next_data(): empty_reader_idxs.append(reader_idx) # 텅빈 Reader들을 목록에서 제거 for reader_idx in empty_reader_idxs: del db_readers[reader_idx] reader_cnt = len(db_readers) fin_readers = [] while 0 < reader_cnt: min_rtime_idx = -1 min_rtime = 9999999999999 find_min_ts = False is_exist_fin_readers = False for idx, db_reader in enumerate(db_readers): row: list = db_reader.last_data # 마지막 데이터가 비었을때 if row is None: thd_state = db_reader.get_thd_state() if DbReaderDef.STATE_WORKING == thd_state: time.sleep(0.5) db_reader.read_next_data() find_min_ts = False break elif DbReaderDef.STATE_FINISHED == thd_state: fin_readers.append(idx) is_exist_fin_readers = True continue elif DbReaderDef.STATE_ERROR == thd_state: self._send_err_msg('Error in DbReaderThd2') fin_readers.append(idx) is_exist_fin_readers = True continue pk_rtime = row[0] if min_rtime > pk_rtime: min_rtime = pk_rtime min_rtime_idx = idx find_min_ts = True # 가장 과거의 값을 찾았다면 if find_min_ts: target_reader: DbReaer = db_readers[min_rtime_idx] self._rcv_queue.put({ gd.KEY_NM_EVT: gd.EVT_TYPE_READ_DB, gd.KEY_NM_IDX: target_reader.reader_idx, gd.KEY_NM_DATA: target_reader.last_data }) target_reader.read_next_data() # 종료된 Reader가 생겼다면 if is_exist_fin_readers: fin_readers.sort(reverse=True) for fin_reader_idx in fin_readers: del db_readers[fin_reader_idx] reader_cnt = len(db_readers) fin_readers.clear() self._rcv_queue.put({ gd.KEY_NM_EVT: gd.EVT_TYPE_FIN }) return True def run(self): self.is_run = True while self.is_run: try: req = self._req_queue.get(True, 1) evt_type = req.get(gd.KEY_NM_EVT) if gd.EVT_TYPE_READ_DB == evt_type: print(f'Read DB Start!, data: {req}') self._read_db(req) print(f'Read DB End!, data: {req}') elif gd.EVT_TYPE_FIN == evt_type: break except Empty as em: pass except Exception as e: self.is_run = False break
5,838
1,964
import logging from rest_framework import mixins, generics, permissions, exceptions from django.conf import settings from django.utils import timezone from .serializers import CategorySerializer, TagSerializer, TodoSerializer from .models import Category, Tag, Todo logger = logging.getLogger(__name__) class MyGenericApiView(generics.GenericAPIView): # Disabling "options" method metadata_class = None def initial(self, request, *args, **kwargs): super().initial(request, *args, **kwargs) timezone.activate(request.user.profile.timezone) @staticmethod def _raise_invalid_param(param_name): raise exceptions.ParseError('parameter `{0}` is invalid'.format(param_name)) def parse_get_int(self, param_name, default=None): param = self.request.query_params.get(param_name, default) if param != default: try: param = int(param) except ValueError: self._raise_invalid_param(param_name) return param def parse_get_bool(self, param_name, default=None): param = self.parse_get_int(param_name, default) if param != default: if param not in (0, 1): self._raise_invalid_param(param_name) param = bool(param) return param # Hiding "options" from available methods @property def allowed_methods(self): methods = super().allowed_methods methods.remove('OPTIONS') return methods class CategoryList(mixins.ListModelMixin, mixins.CreateModelMixin, MyGenericApiView): serializer_class = CategorySerializer permission_classes = (permissions.IsAuthenticated,) def get_queryset(self): return Category.objects.filter(user=self.request.user) def perform_create(self, serializer): serializer.save(user=self.request.user) def get(self, request, *args, **kwargs): return self.list(request, args, kwargs) def post(self, request, *args, **kwargs): return self.create(request, *args, **kwargs) class CategoryDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, MyGenericApiView): serializer_class = CategorySerializer permission_classes = (permissions.IsAuthenticated,) def get_queryset(self): return Category.objects.filter(user=self.request.user) def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) def put(self, request, *args, **kwargs): return self.update(request, *args, partial=True, **kwargs) def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs) class TagList(mixins.ListModelMixin, mixins.CreateModelMixin, MyGenericApiView): serializer_class = TagSerializer permission_classes = (permissions.IsAuthenticated,) def get_queryset(self): return Tag.objects.filter(user=self.request.user) def perform_create(self, serializer): serializer.save(user=self.request.user) def get(self, request, *args, **kwargs): return self.list(request, args, kwargs) def post(self, request, *args, **kwargs): return self.create(request, *args, **kwargs) class TagDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, MyGenericApiView): serializer_class = TagSerializer permission_classes = (permissions.IsAuthenticated,) def get_queryset(self): return Tag.objects.filter(user=self.request.user) def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) def put(self, request, *args, **kwargs): return self.update(request, *args, partial=True, **kwargs) def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs) class TodoList(mixins.ListModelMixin, mixins.CreateModelMixin, MyGenericApiView): serializer_class = TodoSerializer permission_classes = (permissions.IsAuthenticated,) def get_queryset(self): """ Gets query according to GET params Available GET params: only_done: if specified, todos will be filtered by `todo.is_done` = only_done category: if specified todos will be filtered by this category tags: if specified todos will be filtered by this tags list only_one_day: if specified changes behaviour of by_date(see below) to show todos only for one day by_date: if specified todos will be filtered by this date, if it is equal to `None`, filters todos without deadline :return: queryset """ q = Todo.objects.filter(user=self.request.user) only_done = self.parse_get_bool('only_done') only_one_day = self.parse_get_bool('only_one_day', False) category = self.request.query_params.get('category') tags = self.request.query_params.getlist('tags') by_date = self.request.query_params.get('by_date') if only_done is not None: if only_done: q = q.filter(is_done=True) else: q = q.filter(is_done=False) if category is not None: try: category = int(category) except ValueError: raise exceptions.ParseError('parameter `category` is invalid') else: q = q.filter(category__pk=category) if tags: try: tags = list(map(int, tags)) except ValueError: raise exceptions.ParseError('parameter `tags` is invalid') else: for t in tags: q = q.filter(tags__pk=t) if by_date is not None: if by_date in ('today', 'tomorrow', 'week', 'none'): date = timezone.localtime(timezone.now()) else: try: date = timezone.datetime.strptime(by_date, settings.DATE_FORMAT) except TypeError: raise exceptions.ParseError('parameter `by_date` is invalid') date = timezone.make_aware(timezone.datetime.combine(date, timezone.datetime.max.time())) if by_date == 'tomorrow': date += timezone.timedelta(days=1) elif by_date == 'week': date += timezone.timedelta(days=6) logger.warn(str(date)) if by_date == 'none': q = q.filter(deadline__isnull=True) elif only_one_day: q = q.filter(deadline__date=date) else: q = q.filter(deadline__lte=date) return q.prefetch_related('tags') def perform_create(self, serializer): serializer.save(user=self.request.user) def get(self, request, *args, **kwargs): return self.list(request, args, kwargs) def post(self, request, *args, **kwargs): return self.create(request, *args, **kwargs) class TodoDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, MyGenericApiView): serializer_class = TodoSerializer permission_classes = (permissions.IsAuthenticated,) def get_queryset(self): return Todo.objects.filter(user=self.request.user) def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) def put(self, request, *args, **kwargs): return self.update(request, *args, partial=True, **kwargs) def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs)
7,865
2,237
# coding=utf-8 # # This file is part of Hypothesis (https://github.com/DRMacIver/hypothesis) # # Most of this work is copyright (C) 2013-2015 David R. MacIver # (david@drmaciver.com), but it contains contributions by others. See # https://github.com/DRMacIver/hypothesis/blob/master/CONTRIBUTING.rst for a # full list of people who may hold copyright, and consult the git log if you # need to determine who owns an individual contribution. # # This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at http://mozilla.org/MPL/2.0/. # # END HEADER """A module controlling settings for Hypothesis to use in falsification. Either an explicit settings object can be used or the default object on this module can be modified. """ from __future__ import division, print_function, absolute_import import os import inspect import warnings import threading from collections import namedtuple from hypothesis.errors import InvalidArgument, HypothesisDeprecationWarning from hypothesis.configuration import hypothesis_home_dir from hypothesis.utils.conventions import not_set from hypothesis.utils.dynamicvariables import DynamicVariable __all__ = [ 'settings', ] all_settings = {} _db_cache = {} class SettingsProperty(object): def __init__(self, name): self.name = name def __get__(self, obj, type=None): if obj is None: return self else: try: return obj.__dict__[self.name] except KeyError: raise AttributeError(self.name) def __set__(self, obj, value): obj.__dict__[self.name] = value def __delete__(self, obj): try: del obj.__dict__[self.name] except KeyError: raise AttributeError(self.name) @property def __doc__(self): return '\n'.join(( all_settings[self.name].description, 'default value: %r' % (getattr(settings.default, self.name),) )) default_variable = DynamicVariable(None) class SettingsMeta(type): def __init__(self, *args, **kwargs): super(SettingsMeta, self).__init__(*args, **kwargs) @property def default(self): return default_variable.value @default.setter def default(self, value): if default_variable.value is not None: raise AttributeError('Cannot assign settings.default') self._assign_default_internal(value) def _assign_default_internal(self, value): default_variable.value = value class settings(SettingsMeta('settings', (object,), {})): """A settings object controls a variety of parameters that are used in falsification. These may control both the falsification strategy and the details of the data that is generated. Default values are picked up from the settings.default object and changes made there will be picked up in newly created settings. """ _WHITELISTED_REAL_PROPERTIES = [ '_database', '_construction_complete', 'storage' ] __definitions_are_locked = False _profiles = {} def __getattr__(self, name): if name in all_settings: d = all_settings[name].default if inspect.isfunction(d): d = d() return d else: raise AttributeError('settings has no attribute %s' % (name,)) def __init__( self, parent=None, **kwargs ): self._construction_complete = False self._database = kwargs.pop('database', not_set) explicit_kwargs = list(kwargs) defaults = parent or settings.default if defaults is not None: for setting in all_settings.values(): if kwargs.get(setting.name, not_set) is not_set: kwargs[setting.name] = getattr(defaults, setting.name) if self._database is not_set: self._database = defaults.database for name, value in kwargs.items(): if name not in all_settings: raise InvalidArgument( 'Invalid argument %s' % (name,)) setattr(self, name, value) self.storage = threading.local() self._construction_complete = True for k in explicit_kwargs: deprecation = all_settings[k].deprecation if deprecation: note_deprecation(deprecation, self) def defaults_stack(self): try: return self.storage.defaults_stack except AttributeError: self.storage.defaults_stack = [] return self.storage.defaults_stack def __call__(self, test): test._hypothesis_internal_use_settings = self return test @classmethod def define_setting( cls, name, description, default, options=None, deprecation=None, ): """Add a new setting. - name is the name of the property that will be used to access the setting. This must be a valid python identifier. - description will appear in the property's docstring - default is the default value. This may be a zero argument function in which case it is evaluated and its result is stored the first time it is accessed on any given settings object. """ if settings.__definitions_are_locked: from hypothesis.errors import InvalidState raise InvalidState( 'Settings have been locked and may no longer be defined.' ) if options is not None: options = tuple(options) if default not in options: raise InvalidArgument( 'Default value %r is not in options %r' % ( default, options ) ) all_settings[name] = Setting( name, description.strip(), default, options, deprecation) setattr(settings, name, SettingsProperty(name)) @classmethod def lock_further_definitions(cls): settings.__definitions_are_locked = True def __setattr__(self, name, value): if name in settings._WHITELISTED_REAL_PROPERTIES: return object.__setattr__(self, name, value) elif name == 'database': if self._construction_complete: raise AttributeError( 'Settings objects are immutable and may not be assigned to' ' after construction.' ) else: return object.__setattr__(self, '_database', value) elif name in all_settings: if self._construction_complete: raise AttributeError( 'Settings objects are immutable and may not be assigned to' ' after construction.' ) else: setting = all_settings[name] if ( setting.options is not None and value not in setting.options ): raise InvalidArgument( 'Invalid %s, %r. Valid options: %r' % ( name, value, setting.options ) ) return object.__setattr__(self, name, value) else: raise AttributeError('No such setting %s' % (name,)) def __repr__(self): bits = [] for name in all_settings: value = getattr(self, name) bits.append('%s=%r' % (name, value)) bits.sort() return 'settings(%s)' % ', '.join(bits) @property def database(self): """An ExampleDatabase instance to use for storage of examples. May be None. If this was explicitly set at settings instantiation then that value will be used (even if it was None). If not and the database_file setting is not None this will be lazily loaded as an SQLite backed ExampleDatabase using that file the first time this property is accessed on a particular thread. """ try: if self._database is not_set and self.database_file is not None: from hypothesis.database import ExampleDatabase from hypothesis.database.backend import SQLiteBackend if self.database_file not in _db_cache: _db_cache[self.database_file] = ( ExampleDatabase( backend=SQLiteBackend(self.database_file))) return _db_cache[self.database_file] if self._database is not_set: self._database = None return self._database except AttributeError: import traceback traceback.print_exc() assert False def __enter__(self): default_context_manager = default_variable.with_value(self) self.defaults_stack().append(default_context_manager) default_context_manager.__enter__() return self def __exit__(self, *args, **kwargs): default_context_manager = self.defaults_stack().pop() return default_context_manager.__exit__(*args, **kwargs) @staticmethod def register_profile(name, settings): """registers a collection of values to be used as a settings profile. These settings can be loaded in by name. Enable different defaults for different settings. - settings is a settings object """ settings._profiles[name] = settings @staticmethod def get_profile(name): """Return the profile with the given name. - name is a string representing the name of the profile to load A InvalidArgument exception will be thrown if the profile does not exist """ try: return settings._profiles[name] except KeyError: raise InvalidArgument( "Profile '{0}' has not been registered".format( name ) ) @staticmethod def load_profile(name): """Loads in the settings defined in the profile provided If the profile does not exist an InvalidArgument will be thrown. Any setting not defined in the profile will be the library defined default for that setting """ settings._assign_default_internal(settings.get_profile(name)) Setting = namedtuple( 'Setting', ( 'name', 'description', 'default', 'options', 'deprecation')) settings.define_setting( 'min_satisfying_examples', default=5, description=""" Raise Unsatisfiable for any tests which do not produce at least this many values that pass all assume() calls and which have not exhaustively covered the search space. """ ) settings.define_setting( 'max_examples', default=200, description=""" Once this many satisfying examples have been considered without finding any counter-example, falsification will terminate. """ ) settings.define_setting( 'max_iterations', default=1000, description=""" Once this many iterations of the example loop have run, including ones which failed to satisfy assumptions and ones which produced duplicates, falsification will terminate. """ ) settings.define_setting( 'max_shrinks', default=500, description=""" Once this many successful shrinks have been performed, Hypothesis will assume something has gone a bit wrong and give up rather than continuing to try to shrink the example. """ ) settings.define_setting( 'timeout', default=60, description=""" Once this many seconds have passed, falsify will terminate even if it has not found many examples. This is a soft rather than a hard limit - Hypothesis won't e.g. interrupt execution of the called function to stop it. If this value is <= 0 then no timeout will be applied. """ ) settings.define_setting( 'derandomize', default=False, description=""" If this is True then hypothesis will run in deterministic mode where each falsification uses a random number generator that is seeded based on the hypothesis to falsify, which will be consistent across multiple runs. This has the advantage that it will eliminate any randomness from your tests, which may be preferable for some situations . It does have the disadvantage of making your tests less likely to find novel breakages. """ ) settings.define_setting( 'strict', default=os.getenv('HYPOTHESIS_STRICT_MODE') == 'true', description=""" If set to True, anything that would cause Hypothesis to issue a warning will instead raise an error. Note that new warnings may be added at any time, so running with strict set to True means that new Hypothesis releases may validly break your code. You can enable this setting temporarily by setting the HYPOTHESIS_STRICT_MODE environment variable to the string 'true'. """ ) settings.define_setting( 'database_file', default=lambda: ( os.getenv('HYPOTHESIS_DATABASE_FILE') or os.path.join(hypothesis_home_dir(), 'examples.db') ), description=""" database: An instance of hypothesis.database.ExampleDatabase that will be used to save examples to and load previous examples from. May be None in which case no storage will be used. """ ) class Verbosity(object): def __repr__(self): return 'Verbosity.%s' % (self.name,) def __init__(self, name, level): self.name = name self.level = level def __eq__(self, other): return isinstance(other, Verbosity) and ( self.level == other.level ) def __ne__(self, other): return not self.__eq__(other) def __hash__(self): return self.level def __lt__(self, other): return self.level < other.level def __le__(self, other): return self.level <= other.level def __gt__(self, other): return self.level > other.level def __ge__(self, other): return self.level >= other.level @classmethod def by_name(cls, key): result = getattr(cls, key, None) if isinstance(result, Verbosity): return result raise InvalidArgument('No such verbosity level %r' % (key,)) Verbosity.quiet = Verbosity('quiet', 0) Verbosity.normal = Verbosity('normal', 1) Verbosity.verbose = Verbosity('verbose', 2) Verbosity.debug = Verbosity('debug', 3) Verbosity.all = [ Verbosity.quiet, Verbosity.normal, Verbosity.verbose, Verbosity.debug ] ENVIRONMENT_VERBOSITY_OVERRIDE = os.getenv('HYPOTHESIS_VERBOSITY_LEVEL') if ENVIRONMENT_VERBOSITY_OVERRIDE: DEFAULT_VERBOSITY = Verbosity.by_name(ENVIRONMENT_VERBOSITY_OVERRIDE) else: DEFAULT_VERBOSITY = Verbosity.normal settings.define_setting( 'verbosity', options=Verbosity.all, default=DEFAULT_VERBOSITY, description='Control the verbosity level of Hypothesis messages', ) settings.define_setting( name='stateful_step_count', default=50, description=""" Number of steps to run a stateful program for before giving up on it breaking. """ ) settings.define_setting( 'perform_health_check', default=True, description=u""" If set to True, Hypothesis will run a preliminary health check before attempting to actually execute your test. """ ) settings.lock_further_definitions() settings.register_profile('default', settings()) settings.load_profile('default') assert settings.default is not None def note_deprecation(message, s=None): # If *either* self or the current default are non-strict # then this should be an error. This is to handle e.g. the case # where defining a new setting while non-strict updates a # profile which is strict. This should not be an error, but # using the profile here would cause it to be one. if s is None: s = settings.default assert s is not None strict = settings.default.strict and s.strict verbosity = s.verbosity warning = HypothesisDeprecationWarning(message) if strict: raise warning elif verbosity > Verbosity.quiet: warnings.warn(warning, stacklevel=3)
16,294
4,363
""" Script to generate a security pipeline for PDF files. It does the following: - Adds specified meta-data - Encrypts file Run: python3 pipeline.py """ from PyPDF2 import PdfFileWriter, PdfFileReader from PyPDF2.generic import NameObject, createStringObject def encrypt(input_pdf, output_pdf, password): pdf_writer = PdfFileWriter() pdf_reader = PdfFileReader(input_pdf) for page in range(pdf_reader.getNumPages()): pdf_writer.addPage(pdf_reader.getPage(page)) pdf_writer.encrypt(user_pwd=password, owner_pwd=None, use_128bit=True) with open(output_pdf, 'wb') as fh: pdf_writer.write(fh) def meta(input_pdf, output_pdf, value): pdf_writer = PdfFileWriter() pdf_reader = PdfFileReader(input_pdf) for page in range(pdf_reader.getNumPages()): pdf_writer.addPage(pdf_reader.getPage(page)) # pdf_writer.encrypt(user_pwd=password, owner_pwd=None, # use_128bit=True) infoDict = pdf_writer._info.getObject() infoDict.update({NameObject('/Version'): createStringObject(u'234ds2')}) info = pdf_reader.documentInfo for key in info: infoDict.update({NameObject(key): createStringObject(info[key])}) # add the grade # infoDict.update({NameObject('/Grade'): createStringObject(u'A+')}) # infoDict.update({NameObject('/Grade2'): createStringObject(u'A+')}) infoDict.update({NameObject('/Key'): createStringObject(value)}) with open(output_pdf, 'wb') as fh: pdf_writer.write(fh) if __name__ == '__main__': # path for the file to process filepath = "/Users/victor/Desktop/Apex.AI_Threat_Model_AliasRobotics.pdf" # meta-data-value meta_value = u'HitachiVentures' meta(input_pdf=filepath, output_pdf=filepath+"underNDA.pdf", value=meta_value) encrypt(input_pdf=filepath+"underNDA.pdf", output_pdf=filepath+"underNDA_encrypted.pdf", password='4l14srobotics')
2,014
689
import glob from numpy.linalg import norm import numpy as np from copy import deepcopy as copy from MergeTrack.merge_functions import read_ann,read_props from MergeTrack.ReID_net_functions import ReID_net_init, add_ReID input_images = "DAVIS/val17/" input_proposals = "DAVIS/ReID_props/" first_frame_anns = "DAVIS/val17-ff/" output_images = "DAVIS/final_results/" output_proposals = "DAVIS/final_props/" ReID_net = ReID_net_init() dataset_max_distances = [] for video_fn in sorted(glob.glob(input_images+"*/")): video_proposals = [] templates = [] for image_fn in sorted(glob.glob(video_fn+"*")): ann_fn = image_fn.replace(input_images,first_frame_anns).replace('.jpg','.png') if glob.glob(ann_fn): new_templates = read_ann(ann_fn) new_templates = add_ReID(new_templates, image_fn, ReID_net) # import json # ff_fn = image_fn.replace(input_images, "DAVIS/ff_test/").replace('.jpg', '.json') # with open(ff_fn, "r") as f: # new_templates = json.load(f) # for id, templ in enumerate(new_templates): # templ['ReID'] = np.array(templ['ReID']) # templ['id'] = id templates = templates + new_templates prop_fn = image_fn.replace(input_images,input_proposals).replace('.jpg','.json') proposals = read_props(prop_fn) video_proposals.append(proposals) ReIDs = [[prop['ReID'] for prop in props] for props in video_proposals] template_ReIDs = [templ['ReID'] for templ in templates] all_reid_distances = [np.array([[norm(c_reid - gt_reid) for c_reid in curr] for gt_reid in template_ReIDs]) for curr in ReIDs] all_reid_distances_no_inf = copy(all_reid_distances) for mat in all_reid_distances_no_inf: mat[np.isinf(mat)] = 0 max_distances = np.array([mat.max(axis=1) if mat.shape[1]>0 else np.zeros((mat.shape[0])) for mat in all_reid_distances_no_inf]).max(axis=0) print(max_distances) dataset_max_distances.append(max_distances.max()) print(np.array(dataset_max_distances).max())
1,989
754
import unittest import string_matching class StringMatchingTest(unittest.TestCase): def test_string_matching_naive(self): t = 'ababbababa' s = 'aba' self.assertEquals(string_matching.string_matching_naive(t, s), [0, 5, 7]) t = 'ababbababa' s = 'abbb' self.assertEquals(string_matching.string_matching_naive(t, s), []) def test_string_matching_rabin_karp(self): t = 'ababbababa' s = 'aba' self.assertEquals(string_matching.string_matching_rabin_karp(t, s), [0, 5, 7]) t = 'ababbababa' s = 'abbb' self.assertEquals(string_matching.string_matching_rabin_karp(t, s), []) def test_string_matching_knuth_morris_pratt(self): t = 'ababbababa' s = 'aba' self.assertEquals(string_matching.string_matching_knuth_morris_pratt(t, s), [0, 5, 7]) t = 'ababbababa' s = 'abbb' self.assertEquals(string_matching.string_matching_knuth_morris_pratt(t, s), []) def test_string_matching_boyer_moore_horspool(self): t = 'ababbababa' s = 'aba' self.assertEquals(string_matching.string_matching_boyer_moore_horspool(t, s), [0, 5, 7]) t = 'ababbababa' s = 'abbb' self.assertEquals(string_matching.string_matching_boyer_moore_horspool(t, s), []) if __name__ == '__main__': unittest.main()
1,377
534
import numpy as np import pytest import molecool_test @pytest.fixture def methane_molecule(): symbols = np.array(['C', 'H', 'H', 'H', 'H']) coordinates = np.array([ [1, 1, 1], [2.4, 1, 1], [-0.4, 1, 1], [1, 1, 2.4], [1, 1, -0.4], ]) return symbols, coordinates def test_move_methane(methane_molecule): symbols, coordinates = methane_molecule coordinates[0] += 5 def test_build_bond_list(methane_molecule): symbols, coordinates = methane_molecule bonds = molecool_test.build_bond_list(coordinates) assert len(bonds) == 4 for bond_length in bonds.values(): assert bond_length == 1.4 def test_build_bond_failure(methane_molecule): symbols, coordinates = methane_molecule with pytest.raises(ValueError): bonds = molecool_test.build_bond_list(coordinates, min_bond=-1) def test_molecular_mass(methane_molecule): symbols, coordinates = methane_molecule calculated_mass = molecool_test.calculate_molecular_mass(symbols) actual_mass = 16.04 assert pytest.approx(actual_mass, abs=1e-2) == calculated_mass def test_center_of_mass(methane_molecule): symbols, coordinates = methane_molecule center_of_mass = molecool_test.calculate_center_of_mass(symbols, coordinates) expected_center = np.array([1,1,1]) assert np.array_equal(center_of_mass, expected_center)
1,411
558
from unittest import TestCase from unittest.mock import patch, MagicMock, PropertyMock from basketball_reference_web_scraper.html import SearchPage, PlayerSearchResult class TestSearchPage(TestCase): def test_nba_aba_baa_players_content_query(self): self.assertEqual( SearchPage(html=MagicMock()).nba_aba_baa_players_content_query, '//div[@id="searches"]/div[@id="players"]', ) @patch.object(SearchPage, 'nba_aba_baa_players_content_query', new_callable=PropertyMock) def test_nba_aba_baa_players_pagination_links_query(self, mocked_query): mocked_query.return_value = "some query" self.assertEqual( SearchPage(html=MagicMock()).nba_aba_baa_players_pagination_links_query, 'some query/div[@class="search-pagination"]/a', ) @patch.object(SearchPage, 'nba_aba_baa_players_content_query', new_callable=PropertyMock) def test_nba_aba_baa_player_search_items_query(self, mocked_query): mocked_query.return_value = "some query" self.assertEqual( SearchPage(html=MagicMock()).nba_aba_baa_player_search_items_query, 'some query/div[@class="search-item"]', ) @patch.object(SearchPage, 'nba_aba_baa_players_pagination_links_query', new_callable=PropertyMock) def test_nba_aba_baa_players_pagination_links(self, mocked_query): mocked_query.return_value = "some query" html = MagicMock() links = [MagicMock(return_value="some"), MagicMock(return_value="links")] html.xpath = MagicMock(return_value=links) self.assertEqual( SearchPage(html=html).nba_aba_baa_players_pagination_links, links, ) html.xpath.asset_called_once_with("some query") @patch.object(SearchPage, 'nba_aba_baa_players_pagination_links', new_callable=PropertyMock) def test_nba_aba_baa_players_pagination_url_is_none_when_no_pagination_links(self, mocked_links): mocked_links.return_value = [] self.assertIsNone(SearchPage(html=MagicMock()).nba_aba_baa_players_pagination_url) @patch.object(SearchPage, 'nba_aba_baa_players_pagination_links', new_callable=PropertyMock) def test_nba_aba_baa_players_pagination_url_is_first_link_href_attrib_when_single_link_is_not_at_end_of_results( self, mocked_links ): link = MagicMock() link.text_content = MagicMock(return_value="jaebaebae") link.attrib = MagicMock() link.attrib.__getitem__ = MagicMock(return_value="some text content") mocked_links.return_value = [link] self.assertEqual( SearchPage(html=MagicMock()).nba_aba_baa_players_pagination_url, "some text content", ) link.attrib.__getitem__.assert_called_once_with("href") @patch.object(SearchPage, 'nba_aba_baa_players_pagination_links', new_callable=PropertyMock) def test_nba_aba_baa_players_pagination_url_is_none_when_single_link_is_at_end_of_results( self, mocked_links ): link = MagicMock() link.text_content = MagicMock(return_value="Previous 100 Results") mocked_links.return_value = [link] self.assertIsNone(SearchPage(html=MagicMock()).nba_aba_baa_players_pagination_url) link.text_content.assert_called_once_with() @patch.object(SearchPage, 'nba_aba_baa_players_pagination_links', new_callable=PropertyMock) def test_nba_aba_baa_players_pagination_url_is_second_link_href_attrib_when_multiple_links( self, mocked_links ): first_link = MagicMock() first_link.attrib = MagicMock() first_link.attrib.__getitem__ = MagicMock(return_value="some text content") second_link = MagicMock() second_link.attrib = MagicMock() second_link.attrib.__getitem__ = MagicMock(return_value="some other text content") mocked_links.return_value = [first_link, second_link] self.assertEqual( SearchPage(html=MagicMock()).nba_aba_baa_players_pagination_url, "some other text content", ) second_link.attrib.__getitem__.assert_called_once_with("href") @patch.object(SearchPage, 'nba_aba_baa_player_search_items_query', new_callable=PropertyMock) def test_nba_aba_baa_players(self, mocked_query): mocked_query.return_value = "some query" first_result = MagicMock(name="first html result") second_result = MagicMock(name="second html result") third_result = MagicMock(name="third html result") html = MagicMock() html.xpath = MagicMock(return_value=[first_result, second_result, third_result]) self.assertEqual( SearchPage(html=html).nba_aba_baa_players, [ PlayerSearchResult(html=first_result), PlayerSearchResult(html=second_result), PlayerSearchResult(html=third_result), ] )
5,004
1,622
## # This software was developed and / or modified by Raytheon Company, # pursuant to Contract DG133W-05-CQ-1067 with the US Government. # # U.S. EXPORT CONTROLLED TECHNICAL DATA # This software product contains export-restricted data whose # export/transfer/disclosure is restricted by U.S. law. Dissemination # to non-U.S. persons whether in the United States or abroad requires # an export license or other authorization. # # Contractor Name: Raytheon Company # Contractor Address: 6825 Pine Street, Suite 340 # Mail Stop B8 # Omaha, NE 68106 # 402.291.0100 # # See the AWIPS II Master Rights File ("Master Rights File.pdf") for # further licensing information. ## # ---------------------------------------------------------------------------- # This software is in the public domain, furnished "as is", without technical # support, and with no warranty, express or implied, as to its usefulness for # any purpose. # # New_WindGust_Tool # # Authors: Tom Mazza NWS Charleston, WV Created: 04/25/03 # Matthew H. Belk NWS Taunton, MA Last Modified: 06/16/03 # Mathewson FSL Modified: 3/30/04 # -change in model names to OB3 names #---------------------------------------------------------------------------- # # SOFTWARE HISTORY # # Date Ticket# Engineer Description # ------------ ---------- ----------- -------------------------- # 02/10/2016 5283 nabowle Remove NGM support. # ---------------------------------------------------------------------------- ## # This is an absolute override file, indicating that a higher priority version # of the file will completely replace a lower priority version of the file. ## ToolType = "numeric" WeatherElementEdited = "WindGust" from numpy import * # without this, the builtin max() is used from numpy import max import LogStream # You can screen the elements for which your tool will appear by using # a ScreenList. For example: #ScreenList = ["MixHgt","WindGust", "TransWind"] # Set up variables to be solicited from the user: VariableList = [ ("Momentum algorithm:", "RUC", "radio", ["RUC", "Power"]), ("Use BL Winds:", "No", "radio", ["Yes", "No"]), ("Model:", "NAM12", "radio", ["GFS80", "NAM12", "gfsLR", "RAP40"]) ] #Set up Class import SmartScript ## For available commands, see SmartScript toolName = 'WindGustFromAlgorithm' class Tool (SmartScript.SmartScript): def __init__(self, dbss): SmartScript.SmartScript.__init__(self, dbss) # Define your site ID self._SITEID = "BOX" # Required Method: Execute # Called once for each grid # Fill in the arguments you want to use -- WeatherElement1, WeatherElement2... def execute(self, Wind, MixHgt, Topo, GridTimeRange): "Determines WindGust using one of two algorithms, one from the RUC or a power relationship. This tool assumes your mixing height has already been adjusted for your surface temperatures." sounding = self.makeNumericSounding(self._model, "wind", self._modelCube, GridTimeRange, noDataError=0) ######################################################################## # If we don't have a model sounding at this point in time, or the # size of the grids do not match if sounding is None: # or sounding[0].shape != Topo.shape: LogStream.logProblem(toolName, ': cannot obtain a Wind sounding') return None # leaves current WindGust grid alone ######################################################################## # If we made it this far, split up the sounding into its component # cubes of height and wind (gh_Cube, wind_Cube) = sounding if gh_Cube is None: LogStream.logProblem(toolName, 'gh_Cube is None') return None if wind_Cube is None: LogStream.logProblem(toolName, 'wind_Cube is None') return None ######################################################################## # Convert topography from feet to meters self._topo = self.ftToM(Topo) ######################################################################## # Initialize a cube to hold BL wind grids bl_WindCube = {} ######################################################################## # Cycle through all the BL levels we have for this model for lvl in self._blCube: #################################################################### # Initialize BL wind grid for this level grid = None #################################################################### # If this is the NAM40/20 model if self._model.find('NAM40') != -1: ################################################################ # Get BL winds from other NAM40/NAM20 file tempModel = self._model.replace('NAM40', 'NAM20') ################################################################ # Try to get model BL winds for this time grid = self.getGrids(tempModel, "wind", lvl, GridTimeRange, noDataError=0) #################################################################### # Otherwise else: ################################################################ # Try to get model BL winds for this time grid = self.getGrids(self._model, "Wind", lvl, GridTimeRange, noDataError=0) #################################################################### # Add this grid to the BL wind cube - if it is valid if grid != None: ################################################################ # Store the wind speeds at this BL level bl_WindCube[lvl] = grid[0] #################################################################### # Otherwise else: ################################################################ # Store a placeholder bl_WindCube[lvl] = None ######################################################################## # Convert mixing height from ft ASL to m ASL mixHgt_m = self.ftToM(MixHgt) ######################################################################## # Make a 3D mask where the model sounding level is ABOVE the ground, # but below the Mixing Height self._mixedLayer = (gh_Cube >= self._topo) & (gh_Cube <= mixHgt_m) ######################################################################## # Method to compute WindGust using a version of the RUC technique # adapted by Matthew H. Belk (BOX). ######################################################################## # Initialize WindGust using current 10m Wind speeds - (mag, dir) WindGust = Wind[0] ######################################################################## # Move vertically through the model BL cube for lvl in self._blCube: #################################################################### # Make a mask where this BL surface is at or below the MixHgt blMask = MixHgt <= self._blHgt[lvl] #################################################################### # If there are any points in the mixed layer at this surface, and # there actually is a wind grid if any(blMask) and bl_WindCube[lvl] != None: ################################################################ # Get wind magnitude at current level - remember model winds # are in m/s and need to be in kts for comparison curMag = self.mpsToKt(bl_WindCube[lvl]) ################################################################ # Compute difference between wind at this level and SFC wind # where points are in the mixed layer deltaSpd = curMag - Wind[0] ################################################################ # Get the depth of the mixed layer to this point (m AGL) deltaZ = self._blHgt[lvl] ################################################################ # Adjust change in wind speed by a coefficient - using the # lesser of 0.5 or (deltaZ / 2000) # First get the factor, which will range from 0.5 to 1.0, # higher closer to the ground delta = max(1.0 - deltaZ/2000.0, 0.5) ################################################################ # Employ the power relationship if selected: it focuses in on # how much lower than one this factor will be (it ranges from # no less than 1 just above the surface to 0.5 lower than 1 # 1000 or more feet from the surface). The power relationship # takes this small number (between 0 and 0.5) to the second # power, which makes it smaller still. It actually first # doubles it, then squares it, then halves it again. This # causes a difference of 0 to stay 0, a difference of 0.5 to # stay at 0.5, but a difference of 0.25 will become 0.125. # This difference is then subtracted from one, to get a new, # equal or larger factor by which to multiply the potential # wind gust, to arrive at a gust potential that decreases more # slowly at first with height, then more rapidly later on, to # arrive at the same factor up at 1000 m and more above the # surface. The resulting wind gust is always equal to or # greater than using the RUC algorthm straight up. if self._algorithm == 'Power': delta = 1 - (pow((2 * (1 - delta)), 2)) / 2 ################################################################ # Adjust wind speed difference by chosen coefficient deltaSpd *= delta gustV = Wind[0] + deltaSpd ################################################################ # Make a mask where this WindGust is > current WindGust newGust = gustV > WindGust ################################################################ # Assign new WindGust where new WindGust is greater and the # surface is still within the mixed layer WindGustMask = newGust & blMask WindGust[WindGustMask] = gustV[WindGustMask] ######################################################################## # Move vertically through the model cube for i in xrange(gh_Cube.shape[0]): #################################################################### # If there are any points in the mixed layer at this surface if any(self._mixedLayer[i]): ################################################################ # Get wind magnitude at current level - remember model winds # are in m/s and need to be in kts for comparison curMag = self.mpsToKt(wind_Cube[0][i]) ################################################################ # Compute difference between wind at this level and SFC wind # where points are in the mixed layer deltaSpd = curMag - Wind[0] ################################################################ # Get the depth of the mixed layer to this point (m AGL) deltaZ = gh_Cube[i] - self._topo ################################################################ # Adjust change in wind speed by a coefficient - using the # lesser of 0.5 or (deltaZ / 2000) # First get the factor, which will range from 0.5 to 1.0, # higher closer to the ground delta = max(1.0-deltaZ/2000.0,0.5) ################################################################ # Employ the power relationship if selected: it focuses in on # how much lower than one this factor will be (it ranges from # no less than 1 just above the surface to 0.5 lower than 1 # 1000 or more feet from the surface). The power relationship # takes this small number (between 0 and 0.5) to the second # power, which makes it smaller still. It actually first # doubles it, then squares it, then halves it again. This # causes a difference of 0 to stay 0, a difference of 0.5 to # stay at 0.5, but a difference of 0.25 will become 0.125. # This difference is then subtracted from one, to get a new, # equal or larger factor by which to multiply the potential # wind gust, to arrive at a gust potential that decreases more # slowly at first with height, then more rapidly later on, to # arrive at the same factor up at 1000 feet and more above the # surface. The resulting wind gust is always equal to or # greater than using the RUC algorthm straight up. if self._algorithm == 'Power': delta = 1 - (pow((2 * (1 - delta)), 2)) / 2 ################################################################ # Adjust wind speed difference by chosen coefficient deltaSpd *= delta gustV = Wind[0] + deltaSpd ################################################################ # Make a mask where this WindGust is > current WindGust newGust = gustV > WindGust ################################################################ # Assign new WindGust where new WindGust is greater and the # surface is still within the mixed layer WindGustMask = newGust & self._mixedLayer[i] WindGust[WindGustMask] = gustV[WindGustMask] ######################################################################## # Return the computed WindGust return WindGust # Optional Methods # These methods can have the additional argument: # ToolTimeRange -- selected time range over which we are running the tool def preProcessTool(self, varDict): # Called once at beginning of Tool # Cannot have WeatherElement or Grid arguments ######################################################################## # Get site ID try: siteID=self.mutableID().siteID() except: siteID=self._SITEID ######################################################################## # Get name of chosen model - and fix it up so we can use it later on. # This will grab the latest version of the chosen model from the D2D # netCDF files. self._model = "%s_D2D_%s" % (siteID, varDict["Model:"]) ######################################################################## # Get chosen algorithm self._algorithm = varDict["Momentum algorithm:"] ######################################################################## # Get answer if we should use BL winds useBLwinds = varDict["Use BL Winds:"] ######################################################################## # Initialize a list of model levels self._modelCube = [] ######################################################################## # Determine model levels available for each model if self._model.find( 'GFS80') != -1 or \ self._model.find( 'GFS') != -1: self._modelCube = ["MB850", "MB700", "MB500", "MB400", "MB300"] self._blCube = [] elif self._model.find( 'NAM12') != -1: self._modelCube = ["MB1000", "MB950", "MB900", "MB850", "MB800", "MB750", "MB700", "MB650", "MB600", "MB550", "MB500", "MB450", "MB400", "MB350"] self._blCube = ["BL030", "BL03060", "BL6090", "BL90120", "BL12015"] elif self._model.find( 'NAM40') != -1 or \ self._model.find( 'NAM20') != -1: self._modelCube = ["MB975", "MB950", "MB925", "MB900", "MB875", "MB850", "MB825", "MB800", "MB775", "MB750", "MB725", "MB700", "MB675", "MB650", "MB625", "MB600", "MB550", "MB500", "MB450", "MB400", "MB350", "MB300"] self._blCube = ["BL030", "BL03060", "BL6090", "BL90120", "BL120150"] elif self._model.find( 'gfsLR') != -1: self._modelCube = ["MB1000", "MB850", "MB700", "MB500", "MB300"] self._blCube = [] elif self._model.find( 'RAP40') != -1: self._modelCube = ["MB1000", "MB950", "MB900", "MB850", "MB800", "MB750", "MB700", "MB650", "MB600", "MB550", "MB500", "MB450", "MB400", "MB350", "MB300"] self._blCube = ["BL030", "BL6090", "BL15018"] ######################################################################## # If we should not use the BL winds if useBLwinds is 'No': #################################################################### # Reset the levels in the BL cube so we don't do anything self._blCube = [] ######################################################################## # Determine height of all possible BL levels available for each model. # If level is not at a fixed height AGL, use the hydrostatic equation. # Assume the density of the air is 1 kg/m3 and gravity is 9.80 m/s^2. # The height will be in m AGL at the center of the layer. Remember # there are 100 Pa per 1 mb. self._blHgt = {'BL030' : (15.0 * 100.0/ 9.8), 'BL3060' : (45.0 * 100.0 / 9.8), 'BL03060' : (45.0 * 100.0 / 9.8), 'BL6090' : (75.0 * 100.0 / 9.8), 'BL90120' : (105.0 * 100.0 / 9.8), 'BL12015' : (135.0 * 100.0 / 9.8), 'BL120150': (135.0 * 100.0 / 9.8), 'BL15018' : (165.0 * 100.0 / 9.8), 'FH1829' : 1829.0, 'FH2743' : 2743.0, 'FH3658' : 3658.0 } LogStream.logDebug(toolName, ': preProcessTool complete.')
19,891
5,288
from abc import ABC from typing import List from zeep import CachingClient, Client, Settings from .exceptions import APIError class _BaseClient(ABC): """API клиент сервиса отслеживания посылок. https://tracking.pochta.ru/specification """ WSDL = '' def __init__(self, login: str, password: str, caching=True): """Инициализация API клиента сервиса отслеживания посылок. :param login: Логин от системы трекинга :param password: Пароль от системы трекинга :param caching: Флаг, позволяющий отключить кэширование в zeep """ self._login = login self._password = password zeep_client = CachingClient if caching else Client self._client = zeep_client( self.WSDL, settings=Settings(strict=False), ) class SingleTracker(_BaseClient): """Клиент для взаимодеействия с API единичной обработки запросов.""" WSDL = 'https://tracking.russianpost.ru/rtm34?wsdl' def get_history(self, barcode: str) -> dict: """ История операций над отправлением. Метод getOperationHistory используется для получения информации о конкретном отправлении. Метод возвращает подробную информацию по всем операциям, совершенным над отправлением. https://tracking.pochta.ru/specification#getOperationHistory :param barcode: Идентификатор регистрируемого почтового отправления в одном из форматов: - внутрироссийский, состоящий из 14 символов (цифровой) - международный, состоящий из 13 символов (буквенно-цифровой) в формате S10. :return: Ответ метода getOperationHistory содержит список элементов historyRecord. Каждый из них содержит информацию об одной операции над отправлением. Если над отправлением еще не зарегистрировано ни одной операции, то возвращается пустой список элементов historyRecord. """ return self._client.service.getOperationHistory( OperationHistoryRequest={ 'Barcode': barcode, 'MessageType': '0' }, AuthorizationHeader={ 'login': self._login, 'password': self._password, }, ) def get_order_events_for_mail(self, barcode: str) -> dict: """ История операций с наложенным платежом. Метод PostalOrderEventsForMail позволяет получить информацию об операциях с наложенным платежом, который связан с конкретным почтовым отправлением. https://tracking.pochta.ru/specification#PostalOrderEventsForMail :param barcode: Идентификатор регистрируемого почтового отправления в одном из форматов: - внутрироссийский, состоящий из 14 символов (цифровой); - международный, состоящий из 13 символов (буквенно-цифровой) в формате S10. :return: Список событий """ return self._client.service.PostalOrderEventsForMail( PostalOrderEventsForMailInput={ 'Barcode': barcode, }, AuthorizationHeader={ 'login': self._login, 'password': self._password, }, ) class BatchTracker(_BaseClient): """Клиент для взаимодеействия с API пакетной обработки запросов.""" WSDL = 'https://tracking.russianpost.ru/fc?wsdl' def get_ticket(self, barcodes: List[str]) -> str: """Получения билета на подготовку информации по списку идентификаторов отправлений. Метод getTicket используется для получения билета на подготовку информации по списку идентификаторов отправлений. В запросе передается список идентификаторов отправлений. При успешном вызове метод возвращает идентификатор билета. Ограничения и рекомендации по использованию: - Количество идентификаторов отправлений в одном запросе не должно превышать *3000*. - Рекомендуется выполнять первое обращение за ответом по билету не ранее, чем через 15 минут от момента выдачи билета. - В случае неготовности результата повторные обращения по тому же билету следует выполнять не чаще, чем 1 раз в 15 минут - Время хранения ответа по билету в Сервисе отслеживания составляет 32 часа. По истечении этого периода ответ удаляется. https://tracking.pochta.ru/specification раздел "Пакетная обработка" п.3 :param barcodes: Идентификаторы регистрируемых почтовогых отправлений в одном из форматов: - внутрироссийский, состоящий из 14 символов (цифровой) - международный, состоящий из 13 символов (буквенно-цифровой) в формате S10. :return: Ответ метода getTicket содержит информацию о выданном билете в объекте ticketResponse в случае успешного запроса, функция возвращает номер созданного ticket, полученного из ticketResponse.value """ # По умолчанию zeep генерирует Request старой версии, # где запрос отправляется в виде файла с метаданными # Поэтому, вручную создаём объект Request и убираем аттрибуты, относящиеся к файлу request = self._client.get_type('{http://fclient.russianpost.org}file') request.attributes.clear() items = [{'Barcode': barcode} for barcode in barcodes] response = self._client.service.getTicket( request=request(Item=items), login=self._login, password=self._password, language='RUS', ) if response['error'] is not None: raise APIError(f'Response body contains error: {response["error"]}') return response['value'] def get_response_by_ticket(self, ticket: str) -> List[dict]: """Метод используется для получения информации об отправлениях по ранее полученному билету. Вызывает метод answerByTicketRequest используемый для получения информации об отправлениях по ранее полученному билету. https://tracking.pochta.ru/specification раздел "Пакетная обработка" п.4 :param ticket: Строка, содержащая номер ticket, полученного ранее при вызове getTicket :return: Результаты пакетной обработки в виде списка словарей, содержащих результаты выполнения запроса на пакетную обработку """ response = self._client.service.getResponseByTicket( ticket=ticket, login=self._login, password=self._password, ) if response['error'] is not None: raise APIError(f'Response body contains error: {response["error"]}') return response['value']['Item']
6,705
2,032
# @Time: 2022/4/12 20:50 # @Author: chang liu # @Email: chang_liu_tamu@gmail.com # @File:4.4.Implementing_the_iterator_protocol.py ################ clean version ######################### # class Node: # def __init__(self, val): # self._value = val # self._children = [] # # def __repr__(self): # return "Node({!r})".format(self._value) # # def add_child(self, node): # self._children.append(node) # # def __iter__(self): # return iter(self._children) # # def depth_first(self): # yield self # for c in self: # yield from c.depth_first() ############# some messy version #################### class Node: def __init__(self, value): self._value = value self._children = [] def __repr__(self): return "Node({!r})".format(self._value) def add_child(self, node): self._children.append(node) def __iter__(self): return iter(self._children) # def iter(self): # return iter(self._children) def depth_first(self): return DepthFirstIterator(self) # def __iter__(self): # return DepthFirstIterator(self) class DepthFirstIterator: ''' DFS traversal ''' def __init__(self, start_node): self._node = start_node self._children_iter = None self._child_iter = None def __iter__(self): return self def __next__(self): if self._children_iter == None: self._children_iter = iter(self._node) # self._children_iter = self._node.iter() return self._node elif self._child_iter: try: following = next(self._child_iter) return following except StopIteration: self._child_iter = None return next(self) else: self._child_iter = next(self._children_iter).depth_first() return next(self) # return next(self._child_iter) root = Node(0) left = Node(1) right = Node(2) left.add_child(Node(3)) left.add_child(Node(4)) right.add_child(Node(5)) right.add_child(Node(6)) root.add_child(left) root.add_child(right) for i in root.depth_first(): print(i) # for i in root: # print(i)
2,291
731
import tensorflow as tf import tensorflow_probability as tfp import numpy as np import pickle import random from utils import gauss_cross_entropy tfk = tfp.math.psd_kernels def _add_diagonal_jitter(matrix, jitter=1e-6): return tf.linalg.set_diag(matrix, tf.linalg.diag_part(matrix) + jitter) class FGP: dtype = np.float64 def __init__(self, init_amplitude, init_length_scale, GP_joint, L_w, object_vectors_init=None, object_prior_corr=False, K_obj_normalize=False): """ GP class for FGPVAE. :param init_amplitude: :param init_length_scale: :param GP_joint: :param L_w: number of local latent channels :param object_vectors_init: initizalition for object vectors (GP-LVM) :param object_prior_corr: whether or not correlated object priors are used :param K_obj_normalize: whether or not to normalize object kernel (linear kernel) """ self.object_prior_corr = object_prior_corr self.K_obj_normalize = K_obj_normalize if GP_joint: self.amplitude = tf.Variable(initial_value=init_amplitude, name="GP_amplitude", trainable=True, dtype=self.dtype) self.length_scale = tf.Variable(initial_value=init_length_scale, name="GP_length_scale", trainable=True, dtype=self.dtype) else: self.amplitude = tf.constant(init_amplitude, dtype=self.dtype) self.length_scale = tf.constant(init_length_scale, dtype=self.dtype) # kernels self.kernel_local = tfk.ExpSinSquared(amplitude=self.amplitude, length_scale=self.length_scale, period=2*np.pi) self.kernel_global = tfk.Linear() # GP-LVM, object vectors if object_vectors_init is not None: self.object_vectors = tf.Variable(initial_value=object_vectors_init, name="GP_LVM_object_vectors", dtype=self.dtype) else: self.object_vectors = None # number of local (views/angles) channels self.L_w = L_w def build_1d_gp_local(self, X, Y, varY, X_test): """ Fits GP for local latent channels. Takes input-output dataset and returns post mean, var, marginal lhood. This is standard GP regression with heteroscedastic noise. :param X: inputs tensor (batch, npoints) :param Y: outputs tensor (batch, npoints) :param varY: outputs tensor (batch, npoints) :param X_test: (batch, ns) input points to compute post mean + var Returns: p_m: (batch, ns) post mean at X_test p_v: (batch, ns) post var at X_test logZ: (batch) marginal lhood of each dataset in batch """ # Prepare all constants batch = tf.shape(X)[0] n = tf.shape(X)[1] ns = tf.shape(X_test)[1] # K_x + \sigma_x^* K = self.kernel_local.matrix(tf.expand_dims(X, 2), tf.expand_dims(X, 2)) # (batch, n n) K = K + tf.matrix_diag(varY) # (batch, n, n) chol_K = tf.linalg.cholesky(K) # (batch, n, n) # lhood term 1/3 lhood_pi_term = tf.cast(n, dtype=self.dtype) * np.log(2 * np.pi) # lhood term 2/3 lhood_logdet_term = 2 * tf.reduce_sum(tf.log(tf.matrix_diag_part(chol_K)), 1) # (batch) # lhood term 3/3 Y = tf.expand_dims(Y, 2) iKY = tf.cholesky_solve(chol_K, Y) # (batch, n, 1) lh_quad_term = tf.matmul(tf.transpose(Y, (0,2,1)), iKY) # (batch, 1, 1) lh_quad_term = tf.reshape(lh_quad_term, [batch]) # log P(Y|X) = -1/2 * ( n log(2 pi) + Y inv(K+noise) Y + log det(K+noise)) gp_lhood = -0.5 * (lhood_pi_term + lh_quad_term + lhood_logdet_term) # Compute posterior mean and variances Ks = self.kernel_local.matrix(tf.expand_dims(X, 2), tf.expand_dims(X_test, 2)) # (batch, n, ns) Ks_t = tf.transpose(Ks, (0, 2, 1)) # (batch, ns, n) # posterior mean p_m = tf.matmul(Ks_t, iKY) p_m = tf.reshape(p_m, (batch, ns)) # posterior variance iK_Ks = tf.cholesky_solve(chol_K, Ks) # (batch, n, ns) Ks_iK_Ks = tf.reduce_sum(Ks * iK_Ks, axis=1) # (batch, ns) p_v = 1 - Ks_iK_Ks # (batch, ns) p_v = tf.reshape(p_v, (batch, ns)) return p_m, p_v, gp_lhood, K def build_1d_gp_global(self, means, vars): """ Fits GP for global latent channels. :param Y: encoder means (batch, npoints) :param varY: encoder vars (batch, npoints) Returns: p_m: (batch) posterior means p_v: (batch) post vars logZ: (batch) product of Gaussians terms """ n = tf.shape(means)[1] sigma_squared_bar = 1 / (tf.reduce_sum(tf.math.reciprocal_no_nan(vars), axis=1) + 1) mu_bar = sigma_squared_bar * tf.reduce_sum(means * tf.math.reciprocal_no_nan(vars), axis=1) lhood = tf.log(tf.sqrt(sigma_squared_bar)) + 0.5*tf.math.reciprocal_no_nan(sigma_squared_bar)*mu_bar**2 - \ 0.5*tf.cast(n, dtype=self.dtype)*tf.log(2.0*tf.cast(np.pi, dtype=self.dtype)) - \ tf.reduce_sum(tf.log(tf.sqrt(vars)), axis=1) - 0.5*tf.reduce_sum(tf.math.reciprocal_no_nan(vars)*means**2) return mu_bar, sigma_squared_bar, lhood @staticmethod def preprocess_1d_gp_global_correlated_object_priors(means, vars): """ Product of Gaussians for each global latent channel. See 2.9 in FGPVAE.tex N = nr. of digits N_t = nr. of angles for digit t :param means: (N, N_t) :param vars: (N, N_t) Returns: bar_means: \Bar{\mu} (1, N,) bar_vars: \Bar{\sigma}^2 (1, N,) C_tilde: \Tilde{C} (1, N,) """ N_t = tf.shape(means)[1] N_t = tf.cast(N_t, dtype=tf.float64) alpha = tf.reduce_sum(tf.math.reciprocal_no_nan(vars), axis=1) beta = tf.reduce_sum(means / vars, axis=1) bar_means = tf.expand_dims(beta / alpha, 0) # expand_dims to make it compatible with batching latter on bar_vars = tf.expand_dims(1 / alpha, 0) # expand_dims to make it compatible with batching latter on # C_1 = (2.0 * np.pi)**(-0.5 * N_t) * tf.reduce_prod(vars**(-0.5), axis=1) C_1 = (2.0 * np.pi) ** (-0.5 * N_t) * tf.reduce_prod(tf.sqrt(tf.math.reciprocal_no_nan(vars)), axis=1) C_2 = tf.exp(-0.5*tf.reduce_sum(means**2/vars, axis=1)) C_3 = tf.exp(0.5*beta**2 / alpha) C_4 = tf.sqrt(2*np.pi/alpha) C_tilde = tf.expand_dims(C_1*C_2*C_3*C_4, 0) # expand_dims to make it compatible with batching latter on # C_tilde = tf.clip_by_value(C_tilde, 1e-90, 100) bar_vars = tf.clip_by_value(bar_vars, 1e-3, 100) return bar_means, bar_vars, C_tilde def kernel_matrix_correlated_object_priors(self, x, y): """ Computes object kernel matrix in case correlated object priors are used. See 2.9 in FGPVAE.tex :param x: (1, N, 10) :param y: (1, N, 10) :param K_obj_normalized: whether or not to normalize (between -1 and 1) object kernel matrix (linear kernel) :return: object kernel matrix (1, N, N) """ # unpack auxiliary data if self.object_vectors is None: x_object, y_object =x[:, :, 2:], y[:, :, 2:] else: x_object = tf.gather(self.object_vectors, tf.cast(x[:, :, 0], dtype=tf.int64)) y_object = tf.gather(self.object_vectors, tf.cast(y[:, :, 0], dtype=tf.int64)) # compute kernel matrix object_matrix = self.kernel_global.matrix(x_object, y_object) if self.K_obj_normalize: # normalize object matrix obj_norm = 1 / tf.matmul(tf.math.reduce_euclidean_norm(x_object, axis=2, keepdims=True), tf.transpose(tf.math.reduce_euclidean_norm(y_object, axis=2, keepdims=True), perm=[0, 2, 1])) object_matrix = object_matrix * obj_norm return object_matrix def X_matrix(self, x): """ Computes X matrix. We need this function (instead of working directly with X) in order to support GP-LVM vectors joint optimization. :param x: (1, N, 10) :param normalized: whether or not to normalize object vectors (so that every object vector has norm 1) :return: """ # unpack auxiliary data if self.object_vectors is None: x_object = x[:, :, 2:] else: x_object = tf.gather(self.object_vectors, tf.cast(x[:, :, 0], dtype=tf.int64)) if self.K_obj_normalize: x_object = x_object / tf.math.reduce_euclidean_norm(x_object, axis=2, keepdims=True) return x_object def build_1d_gp_global_correlated_object_priors(self, X, Y, varY, X_test, C_tilde, omit_C_tilde, bayesian_reg_view, EPSILON=1e-6): """ See 2.9 in FGPVAE.tex Since using build_1d_gp_global_correlated_object_priors leads to numerical issues, we add support for fitting global GP using Bayesian linear regression view. :param X: auxiliary data, train points of GP (1, N, 10) :param Y: encoded and processed means for train points (1, N) :param varY: encoded and processed vars for train points (1, N) :param X_test: auxiliary data, test points of GP (1, N_s, 10) :param C_tilde: (1, N) :param omit_C_tilde: omit C_tilde from derivation and modify cross-entropy term instead :param bayesian_reg_view: whether or not to use Bayesian regression view to fit global GP. :param EPSILON: for numerical stability in log() :return: """ if bayesian_reg_view: p = 8 # dimension of object vectors N = tf.shape(X)[1] # get (and normalize) X and X_test X = self.X_matrix(X) # (1, N, p) X_T = tf.transpose(X, (0, 2, 1)) # (1, p, N) X_test = self.X_matrix(X_test) # (1, N_s, p) X_test_T = tf.transpose(X_test, (0, 2, 1)) # (1, p, N_s) # posterior params A = tf.matmul(X_T, tf.matmul(tf.linalg.diag(tf.math.reciprocal_no_nan(varY)), X)) + \ tf.expand_dims(tf.eye(p, dtype=tf.float64), 0) # (1, p, p) A_inv = tf.linalg.inv(_add_diagonal_jitter(A)) # (1, p, p) w_bar = tf.linalg.matvec(A_inv, tf.linalg.matvec(X_T, tf.math.reciprocal_no_nan(varY) * Y)) # (1, p) p_m = tf.linalg.matvec(X_test, w_bar) # (1, N) p_v = tf.linalg.diag_part(tf.matmul(X_test, tf.matmul(A_inv, X_test_T))) # (1, N) p_v = tf.clip_by_value(p_v, 1e-6, 100) # log GPML (marginal likelihood) lhood_pi_term = tf.cast(N, dtype=tf.float64) * np.log(2 * np.pi) # () mid_mat = tf.linalg.diag(varY) - tf.matmul(X, tf.matmul(A_inv, X_T)) # (1, N, N) Y_tilde = tf.math.reciprocal_no_nan(varY) * Y # (1, N) lhood_quad_term = tf.reduce_sum(Y_tilde * tf.linalg.matvec(mid_mat, Y_tilde), axis=1) # (1, ) A_chol = tf.linalg.cholesky(_add_diagonal_jitter(A)) # (1, p, p) lhood_logdet_term = tf.reduce_sum(tf.math.log(tf.math.sqrt(varY)), axis=1) + \ 2 * tf.reduce_sum(tf.log(tf.matrix_diag_part(A_chol)), axis=1) # (1, ) gp_lhood = -0.5 * (lhood_pi_term + lhood_quad_term + lhood_logdet_term) # (1, ) # add C_tilde terms if not omit_C_tilde: gp_lhood = gp_lhood + tf.reduce_sum(tf.log(C_tilde + EPSILON)) # (1, ) else: # Prepare all constants batch = tf.shape(X)[0] n = tf.shape(X)[1] ns = tf.shape(X_test)[1] # K_x + \sigma_x^* K = self.kernel_matrix_correlated_object_priors(X, X) # (batch, n n) K = K + tf.matrix_diag(varY) # (batch, n, n) chol_K = tf.linalg.cholesky(K) # (batch, n, n) # no cholesky_solve implementation # inv_K = tf.linalg.inv(_add_diagonal_jitter(K, 1e-2)) # lhood term 1/3 lhood_pi_term = tf.cast(n, dtype=self.dtype) * np.log(2 * np.pi) # lhood term 2/3 lhood_logdet_term = 2 * tf.reduce_sum(tf.log(tf.matrix_diag_part(chol_K)), 1) # (batch) # lhood term 3/3 Y = tf.expand_dims(Y, 2) # (batch, n, 1) iKY = tf.cholesky_solve(_add_diagonal_jitter(chol_K), Y) # (batch, n, 1) lh_quad_term = tf.matmul(tf.transpose(Y, (0, 2, 1)), iKY) # (batch, 1, 1) lh_quad_term = tf.reshape(lh_quad_term, [batch]) # no cholesky_solve implementation # iKY = tf.linalg.matvec(inv_K, Y) # lh_quad_term = tf.matmul(iKY, tf.transpose(Y, (1, 0))) # (batch, 1, 1) # lh_quad_term = tf.reshape(lh_quad_term, [batch]) # log P(Y|X) = -1/2 * ( n log(2 pi) + Y inv(K+noise) Y + log det(K+noise)) gp_lhood = -0.5 * (lhood_pi_term + lh_quad_term + lhood_logdet_term) # add C_tilde terms if not omit_C_tilde: gp_lhood = gp_lhood + tf.reduce_sum(tf.log(C_tilde + EPSILON)) # Compute posterior mean and variances Ks = self.kernel_matrix_correlated_object_priors(X, X_test) # (batch, n, ns) Ks_t = tf.transpose(Ks, (0, 2, 1)) # (batch, ns, n) # posterior mean p_m = tf.matmul(Ks_t, iKY) # no cholesky_solve implementation # p_m = tf.matmul(Ks_t, tf.expand_dims(iKY, 2)) p_m = tf.reshape(p_m, (batch, ns)) # posterior variance iK_Ks = tf.cholesky_solve(_add_diagonal_jitter(chol_K), Ks) # (batch, n, ns) Ks_iK_Ks = tf.reduce_sum(Ks * iK_Ks, axis=1) # (batch, ns) # no cholesky_solve implementation # Ks_iK_Ks = 1 - tf.linalg.diag_part(tf.matmul(Ks, tf.matmul(inv_K, Ks))) p_v = 1 - Ks_iK_Ks # (batch, ns) p_v = tf.reshape(p_v, (batch, ns)) p_v = tf.clip_by_value(p_v, 1e-6, 100) # drop first axis p_m = tf.squeeze(p_m) p_v = tf.squeeze(p_v) gp_lhood = tf.squeeze(gp_lhood) return p_m, p_v, gp_lhood def forward_pass_FGPVAE_rotated_mnist(data_batch, beta, vae, GP, N_t, clipping_qs, bayes_reg_view, omit_C_tilde, C_ma, lagrange_mult, alpha, kappa, GECO=False): """ :param data_batch: :param beta: :param vae: :param GP: :param N_t: :param clipping_qs: :param bayes_reg_view: whether or not to use Bayesian regresion view for linear kernel in global channels :param omit_C_tilde: omit C_tilde from derivation and modify cross-entropy term instead :param C_ma: average constraint from t-1 step (GECO) :param lagrange_mult: lambda from t-1 step (GECO) :param kappa: reconstruction level parameter for GECO :param alpha: moving average parameter for GECO :param GECO: whether or not to use GECO algorithm for training :return: """ images, aux_data = data_batch aux_data = tf.reshape(aux_data, (-1, N_t, 10)) L = vae.L L_w = GP.L_w w = tf.shape(images)[1] h = tf.shape(images)[2] K = tf.cast(w, dtype=tf.float64) * tf.cast(h, dtype=tf.float64) b = tf.cast(tf.shape(images)[0], dtype=tf.float64) # batch_size # ENCODER NETWORK qnet_mu, qnet_var = vae.encode(images) qnet_mu = tf.reshape(qnet_mu, (-1, N_t, L)) qnet_var = tf.reshape(qnet_var, (-1, N_t, L)) # clipping of VAE posterior variance if clipping_qs: qnet_var = tf.clip_by_value(qnet_var, 1e-3, 100) # GP p_m, p_v, lhoods_local, lhoods_global = [], [], [], [] for i in range(L_w): # fit local GPs p_m_i, p_v_i, lhood_i, K_local = GP.build_1d_gp_local(X=aux_data[:, :, 1], Y=qnet_mu[:, :, i], varY=qnet_var[:, :, i], X_test=aux_data[:, :, 1]) p_m.append(p_m_i) p_v.append(p_v_i) lhoods_local.append(lhood_i) ce_global_arr = [] for i in range(L_w, L): # fit global GPs if GP.object_prior_corr: object_aux_data_filtered = tf.transpose(aux_data[:, ::N_t, :], perm=[1, 0, 2]) bar_means, bar_vars, C_tilde = GP.preprocess_1d_gp_global_correlated_object_priors(qnet_mu[:, :, i], qnet_var[:, :, i]) p_m_i, p_v_i, lhood_i = GP.build_1d_gp_global_correlated_object_priors(object_aux_data_filtered, bar_means, bar_vars, object_aux_data_filtered, C_tilde, bayesian_reg_view=bayes_reg_view, omit_C_tilde=omit_C_tilde) if omit_C_tilde: ce_global_i = gauss_cross_entropy(p_m_i, p_v_i, bar_means, bar_vars) ce_global_arr.append(ce_global_i) else: p_m_i, p_v_i, lhood_i = GP.build_1d_gp_global(means=qnet_mu[:, :, i], vars=qnet_var[:, :, i]) # repeat p_m_i and p_v_i N_t times, since those are shared across all images within one object dataset D_t p_m_i = tf.tile(tf.expand_dims(p_m_i, 1), [1, N_t]) p_v_i = tf.tile(tf.expand_dims(p_v_i, 1), [1, N_t]) p_m.append(p_m_i) p_v.append(p_v_i) lhoods_global.append(lhood_i) p_m = tf.stack(p_m, axis=2) p_v = tf.stack(p_v, axis=2) if GP.object_prior_corr: # for local channels sum over latent channels and over digits' datasets # for global channels we only sum over latent channels (as there is only one global GP per channel) lhoods = tf.reduce_sum(lhoods_local, axis=(0, 1)) + tf.reduce_sum(lhoods_global, axis=0) # CE (cross-entropy) if omit_C_tilde: ce_global = tf.reduce_sum(ce_global_arr) ce_local = gauss_cross_entropy(p_m[:, :, :L_w], p_v[:, :, :L_w], qnet_mu[:, :, :L_w], qnet_var[:, :, :L_w]) ce_local = tf.reduce_sum(ce_local, (0, 1, 2)) # sum also over digits' datasets ce_term = ce_global + ce_local else: ce_term = gauss_cross_entropy(p_m, p_v, qnet_mu, qnet_var) ce_term = tf.reduce_sum(ce_term, (0, 1, 2)) # sum also over digits' datasets # KL part elbo_kl_part = lhoods - ce_term else: lhoods = lhoods_global + lhoods_local lhoods = tf.reduce_sum(lhoods, axis=0) # CE (cross-entropy) ce_term = gauss_cross_entropy(p_m, p_v, qnet_mu, qnet_var) ce_term = tf.reduce_sum(ce_term, (1, 2)) # KL part elbo_kl_part = lhoods - ce_term # SAMPLE epsilon = tf.random.normal(shape=tf.shape(p_m), dtype=tf.float64) latent_samples = p_m + epsilon * tf.sqrt(p_v) # DECODER NETWORK (Gaussian observational likelihood, MSE) recon_images = vae.decode(tf.reshape(latent_samples, (-1, L))) if GP.object_prior_corr: if GECO: recon_loss = tf.reduce_sum((tf.reshape(images, (-1, N_t, w, h)) - tf.reshape(recon_images, (-1, N_t, w, h))) ** 2, axis=[2, 3]) recon_loss = tf.reduce_sum(recon_loss/K - kappa**2) C_ma = alpha * C_ma + (1 - alpha) * recon_loss / b # elbo = - (1/L) * KL_term + lagrange_mult * C_ma # elbo = - (1/b) * KL_term + lagrange_mult * C_ma # elbo = - KL_term + lagrange_mult * C_ma elbo = - elbo_kl_part + lagrange_mult * (recon_loss / b + tf.stop_gradient(C_ma - recon_loss / b)) lagrange_mult = lagrange_mult * tf.exp(C_ma) else: recon_loss = tf.reduce_sum((tf.reshape(images, (-1, N_t, w, h)) - tf.reshape(recon_images, (-1, N_t, w, h))) ** 2, axis=[1, 2, 3]) recon_loss = tf.reduce_sum(recon_loss) / K elbo = -recon_loss + (beta / L) * elbo_kl_part else: if GECO: recon_loss = tf.reduce_mean((tf.reshape(images, (-1, N_t, w, h)) - tf.reshape(recon_images, (-1, N_t, w, h))) ** 2, axis=[2, 3]) N_t = tf.cast(N_t, dtype=tf.float64) C_ma = alpha * C_ma + (1 - alpha) * tf.reduce_mean(recon_loss - kappa ** 2) recon_loss = tf.reduce_sum(recon_loss - kappa ** 2) # elbo = - (1/L) * elbo_kl_part + lagrange_mult * C_ma # elbo = - (1/b) * elbo_kl_part + lagrange_mult * C_ma # elbo = - elbo_kl_part + lagrange_mult * C_ma elbo = - elbo_kl_part + lagrange_mult * (recon_loss / N_t + tf.stop_gradient(C_ma - recon_loss / N_t)) lagrange_mult = lagrange_mult * tf.exp(C_ma) else: recon_loss = tf.reduce_sum((tf.reshape(images, (-1, N_t, w, h)) - tf.reshape(recon_images, (-1, N_t, w, h))) ** 2, axis=[1, 2, 3]) # ELBO # beta plays role of sigma_gaussian_decoder here (\lambda(\sigma_y) in Casale paper) # K and L are not part of ELBO. They are used in loss objective to account for the fact that magnitudes of # reconstruction and KL terms depend on number of pixels (K) and number of latent GPs used (L), respectively recon_loss = recon_loss / K elbo = -recon_loss + (beta/L) * elbo_kl_part # average across object datasets elbo = tf.reduce_sum(elbo) elbo_kl_part = tf.reduce_sum(elbo_kl_part) recon_loss = tf.reduce_sum(recon_loss) return elbo, recon_loss, elbo_kl_part, p_m, p_v, qnet_mu, qnet_var, recon_images, latent_samples, C_ma, lagrange_mult def predict_FGPVAE_rotated_mnist(test_images, test_aux_data, train_images, train_aux_data, vae, GP, bayes_reg_view, omit_C_tilde, N_t=15, clipping_qs=False): """ Get FGPVAE predictions for rotated MNIST test data. :param test_data_batch: :param train_images: :param train_aux_data: :param vae: :param GP: :param N_t: :param clipping_qs: :return: """ L = vae.L L_w = GP.L_w w = tf.shape(train_images)[1] h = tf.shape(train_images)[2] train_aux_data = tf.reshape(train_aux_data, (-1, N_t, 10)) test_aux_data = tf.expand_dims(test_aux_data, 1) # encode train images qnet_mu, qnet_var = vae.encode(train_images) qnet_mu = tf.reshape(qnet_mu, (-1, N_t, L)) qnet_var = tf.reshape(qnet_var, (-1, N_t, L)) # clipping of VAE posterior variance if clipping_qs: qnet_var = tf.clip_by_value(qnet_var, 1e-3, 100) # GP, get latent embeddings for test images p_m, p_v = [], [] for i in range(L_w): # fit local GPs p_m_i, p_v_i, _ , _= GP.build_1d_gp_local(X=train_aux_data[:, :, 1], Y=qnet_mu[:, :, i], varY=qnet_var[:, :, i], X_test=test_aux_data[:, :, 1]) p_m.append(p_m_i) p_v.append(p_v_i) for i in range(L_w, L): # fit global GPs if GP.object_prior_corr: object_aux_data_filtered = tf.transpose(train_aux_data[:, ::N_t, :], perm=[1, 0, 2]) bar_means, bar_vars, C_tilde = GP.preprocess_1d_gp_global_correlated_object_priors(qnet_mu[:, :, i], qnet_var[:, :, i]) p_m_i, p_v_i, _ = GP.build_1d_gp_global_correlated_object_priors(object_aux_data_filtered, bar_means, bar_vars, object_aux_data_filtered, C_tilde, omit_C_tilde=omit_C_tilde, bayesian_reg_view=bayes_reg_view) else: p_m_i, p_v_i, _ = GP.build_1d_gp_global(means=qnet_mu[:, :, i], vars=qnet_var[:, :, i]) p_m.append(tf.expand_dims(p_m_i, 1)) p_v.append(tf.expand_dims(p_v_i, 1)) p_m = tf.stack(p_m, axis=2) p_v = tf.stack(p_v, axis=2) # SAMPLE epsilon = tf.random.normal(shape=tf.shape(p_m), dtype=tf.float64) latent_samples = p_m + epsilon * tf.sqrt(p_v) # decode, calculate error (Gaussian observational likelihood, MSE) recon_images = vae.decode(tf.reshape(latent_samples, (-1, L))) recon_loss = tf.reduce_mean((test_images - recon_images) ** 2) return recon_images, recon_loss def extrapolate_experiment_eval_data(mnist_path, digit, N_t, pred_angle_id=7, nr_angles=16): """ Prepare validation dataset for the extrapolate experiment. :param mnist_path: :param digit: :param N_t: how many angles do we observe for each image in test set :param pred_angle_id: which angle to leave out for prediction :param nr_angles: size of object dataset :return: """ eval_data_dict = pickle.load(open(mnist_path + 'eval_data{}_not_shuffled.p'.format(digit), 'rb')) eval_images, eval_aux_data = eval_data_dict["images"], eval_data_dict["aux_data"] pred_angle_mask = [pred_angle_id + i * nr_angles for i in range(int(len(eval_aux_data) / nr_angles))] not_pred_angle_mask = [i for i in range(len(eval_images)) if i not in pred_angle_mask] observed_images = eval_images[not_pred_angle_mask] observed_aux_data = eval_aux_data[not_pred_angle_mask] # randomly drop some observed angles if N_t < 15: digit_mask = [True]*N_t + [False]*(15-N_t) mask = [random.sample(digit_mask, len(digit_mask)) for _ in range(int(len(eval_aux_data)/nr_angles))] flatten = lambda l: [item for sublist in l for item in sublist] mask = flatten(mask) observed_images = observed_images[mask] observed_aux_data = observed_aux_data[mask] test_images = eval_images[pred_angle_mask] test_aux_data = eval_aux_data[pred_angle_mask] return observed_images, observed_aux_data, test_images, test_aux_data def latent_samples_FGPVAE(train_images, train_aux_data, vae, GP, N_t, clipping_qs=False): """ Get latent samples for training data. For t-SNE plots :) :param train_images: :param train_aux_data: :param vae: :param GP: :param clipping_qs: :return: """ train_aux_data = tf.reshape(train_aux_data, (-1, N_t, 10)) L = vae.L L_w = GP.L_w # ENCODER NETWORK qnet_mu, qnet_var = vae.encode(train_images) qnet_mu = tf.reshape(qnet_mu, (-1, N_t, L)) qnet_var = tf.reshape(qnet_var, (-1, N_t, L)) # clipping of VAE posterior variance if clipping_qs: qnet_var = tf.clip_by_value(qnet_var, 1e-3, 100) # GP p_m, p_v = [], [] for i in range(L_w): # fit local GPs p_m_i, p_v_i, _, _ = GP.build_1d_gp_local(X=train_aux_data[:, :, 1], Y=qnet_mu[:, :, i], varY=qnet_var[:, :, i], X_test=train_aux_data[:, :, 1]) p_m.append(p_m_i) p_v.append(p_v_i) for i in range(L_w, L): # fit global GPs p_m_i, p_v_i, lhood_i = GP.build_1d_gp_global(means=qnet_mu[:, :, i], vars=qnet_var[:, :, i]) # repeat p_m_i and p_v_i N_t times, since those are shared across all images within one object dataset D_t p_m_i = tf.tile(tf.expand_dims(p_m_i, 1), [1, N_t]) p_v_i = tf.tile(tf.expand_dims(p_v_i, 1), [1, N_t]) p_m.append(p_m_i) p_v.append(p_v_i) p_m = tf.stack(p_m, axis=2) p_v = tf.stack(p_v, axis=2) # SAMPLE epsilon = tf.random.normal(shape=tf.shape(p_m), dtype=tf.float64) latent_samples = p_m + epsilon * tf.sqrt(p_v) return latent_samples
29,762
11,403
from abc import ABC, abstractmethod from argparse import Namespace, ArgumentParser from spotty.commands.writers.abstract_output_writrer import AbstractOutputWriter class AbstractCommand(ABC): """Abstract class for a Spotty sub-command.""" @property @abstractmethod def name(self) -> str: """The sub-command name.""" raise NotImplementedError @property def description(self) -> str: """The sub-command description. It will be displayed in the help text.""" return '' def configure(self, parser: ArgumentParser): """Adds arguments for the sub-command.""" parser.add_argument('-d', '--debug', action='store_true', help='Show debug messages') @abstractmethod def run(self, args: Namespace, output: AbstractOutputWriter): """Runs the sub-command. Args: args: Arguments provided by argparse. output: Output writer. Raises: ValueError: If command's arguments can't be processed. """ raise NotImplementedError
1,069
283
# -*- coding: utf-8 -*- # ------------------------------------------------------------------------------ # # Copyright 2018-2019 Fetch.AI Limited # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # ------------------------------------------------------------------------------ """This module contains the tests for the protocol generator.""" import inspect import os import shutil import tempfile import yaml from aea.configurations.base import ProtocolSpecification from aea.configurations.loader import ConfigLoader from aea.protocols.generator import ProtocolGenerator CUR_PATH = os.path.dirname(inspect.getfile(inspect.currentframe())) # type: ignore class TestGenerateProtocol: """Test that the generating a protocol works correctly in correct preconditions.""" @classmethod def setup_class(cls): """Set the test up.""" # Specification cls.protocol_name = "two_party_negotiation" cls.specification_file_name = "spec.yaml" correct_specification = { "name": cls.protocol_name, "author": "fetchai", "version": "0.1.0", "license": "Apache-2.0", "description": "A protocol for negotiation over a fixed set of resources involving two parties.", "speech_acts": { "cfp": {"query": "DataModel"}, "propose": {"query": "DataModel", "price": "float"}, "accept": {}, "decline": {}, "match_accept": {}, }, } # Dump the config cls.cwd = os.getcwd() # os.mkdir(os.path.join(CUR_PATH, "temp")) cls.t = tempfile.mkdtemp() os.chdir(cls.t) # cls.path_to_specification = os.path.join(".", cls.specification_file_name) cls.path_to_specification = os.path.join(cls.t, cls.specification_file_name) yaml.safe_dump(correct_specification, open(cls.path_to_specification, "w")) # Load the config cls.config_loader = ConfigLoader( "protocol-specification_schema.json", ProtocolSpecification ) cls.protocol_specification = cls.config_loader.load( open(cls.path_to_specification) ) # Generate the protocol cls.protocol_generator = ProtocolGenerator(cls.protocol_specification, cls.t) cls.protocol_generator.generate() # Add as module # dotted_path = "packages.fetchai.protocols." + cls.protocol_name # import pdb;pdb.set_trace() # module_object = load_module(dotted_path, Path(os.path.join(cls.t, cls.protocol_name))) # import_module(dotted_path, module_object) # sys.modules[dotted_path] = module_object # def test_exit_code_equal_to_0(self): # """Test that the exit code is equal to 0.""" # from packages.fetchai.protocols.two_party_negotiation.message import TwoPartyNegotiationMessage # # from two_party_negotiation.serialization import TwoPartyNegotiationSerializer # # from two_party_negotiation.message import DataModel # assert 0 == 0 @classmethod def teardown_class(cls): """Tear the test down.""" os.chdir(cls.cwd) try: shutil.rmtree(cls.t) # os.remove(os.path.join(cls.t, cls.protocol_name)) except (OSError, IOError): pass # class TestCases(TestCase): # """Test class for the light protocol generator.""" # # def test_all_custom_data_types(self): # """Test all custom data types.""" # test_protocol_specification_path = os.path.join(CUR_PATH, "data", "all_custom.yaml") # test_protocol_template = ProtocolTemplate(test_protocol_specification_path) # test_protocol_template.load() # test_protocol_generator = ProtocolGenerator(test_protocol_template, 'tests') # test_protocol_generator.generate() # # from two_party_negotiation_protocol.message import TwoPartyNegotiationMessage # from two_party_negotiation_protocol.serialization import TwoPartyNegotiationSerializer # from two_party_negotiation_protocol.message import DataModel # from two_party_negotiation_protocol.message import Signature # # data_model = DataModel() # signature = Signature() # content_list = [data_model, signature] # # message = TwoPartyNegotiationMessage(message_id=5, target=4, performative="propose", contents=content_list) # print(str.format("message is {}", message)) # message.check_consistency() # serialized_message = TwoPartyNegotiationSerializer().encode(msg=message) # print(str.format("serialized message is {}", serialized_message)) # deserialised_message = TwoPartyNegotiationSerializer().decode(obj=serialized_message) # print(str.format("deserialized message is {}", deserialised_message)) # # assert message == deserialised_message, "Failure" # # def test_correct_functionality(self): # """End to end test of functionality.""" # test_protocol_specification_path = os.path.join(CUR_PATH, "data", "correct_spec.yaml") # test_protocol_template = ProtocolTemplate(test_protocol_specification_path) # test_protocol_template.load() # test_protocol_generator = ProtocolGenerator(test_protocol_template, 'tests') # test_protocol_generator.generate() # # from two_party_negotiation_protocol.message import TwoPartyNegotiationMessage # from two_party_negotiation_protocol.serialization import TwoPartyNegotiationSerializer # from two_party_negotiation_protocol.message import DataModel # # data_model = DataModel() # content_list = [data_model, 10.5] # # message = TwoPartyNegotiationMessage(message_id=5, target=4, performative="propose", contents=content_list) # print(str.format("message is {}", message)) # message.check_consistency() # serialized_message = TwoPartyNegotiationSerializer().encode(msg=message) # print(str.format("serialized message is {}", serialized_message)) # deserialised_message = TwoPartyNegotiationSerializer().decode(obj=serialized_message) # print(str.format("deserialized message is {}", deserialised_message)) # # assert message == deserialised_message, "Failure" # # def test_missing_name(self): # """Test missing name handling.""" # test_protocol_specification_path = os.path.join(CUR_PATH, "data", "missing_name.yaml") # test_protocol_template = ProtocolTemplate(test_protocol_specification_path) # # self.assertRaises(ProtocolSpecificationParseError, test_protocol_template.load) # # def test_missing_description(self): # """Test missing description handling.""" # test_protocol_specification_path = os.path.join(CUR_PATH, "data", "missing_description.yaml") # test_protocol_template = ProtocolTemplate(test_protocol_specification_path) # # assert test_protocol_template.load(), "Failure" # # def test_missing_speech_acts(self): # """Test missing speech acts handling.""" # test_protocol_specification_path = os.path.join(CUR_PATH, "data", "missing_speech_acts.yaml") # test_protocol_template = ProtocolTemplate(test_protocol_specification_path) # # self.assertRaises(ProtocolSpecificationParseError, test_protocol_template.load) # # def test_extra_fields(self): # """Test extra fields handling.""" # test_protocol_specification_path = os.path.join(CUR_PATH, "data", "extra_fields.yaml") # test_protocol_template = ProtocolTemplate(test_protocol_specification_path) # # assert test_protocol_template.load(), "Failure" # # def test_one_document(self): # """Test one document handling.""" # test_protocol_specification_path = os.path.join(CUR_PATH, "data", "one_document.yaml") # test_protocol_template = ProtocolTemplate(test_protocol_specification_path) # # self.assertRaises(ProtocolSpecificationParseError, test_protocol_template.load) # # def test_wrong_speech_act_type_sequence_performatives(self): # """Test wrong speech act handling.""" # test_protocol_specification_path = os.path.join(CUR_PATH, "data", "wrong_speech_act_type_sequence_performatives.yaml") # test_protocol_template = ProtocolTemplate(test_protocol_specification_path) # # self.assertRaises(ProtocolSpecificationParseError, test_protocol_template.load) # # def test_wrong_speech_act_type_dictionary_contents(self): # """Test wrong speech act dictionary contents handling.""" # test_protocol_specification_path = os.path.join(CUR_PATH, "data", "wrong_speech_act_type_dictionary_contents.yaml") # test_protocol_template = ProtocolTemplate(test_protocol_specification_path) # # self.assertRaises(ProtocolSpecificationParseError, test_protocol_template.load)
9,495
2,772
import os.path import os from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from datetime import datetime # If modifying these scopes, delete the file token.json. SCOPES = ['https://www.googleapis.com/auth/drive'] def main(): """Shows basic usage of the Drive v3 API. Prints the names and ids of the first 10 files the user has access to. """ creds = None # The file token.json stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.json'): creds = Credentials.from_authorized_user_file('token.json', SCOPES) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: print("Refresh Creds") creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('token.json', 'w') as token: token.write(creds.to_json()) service = build('drive', 'v3', credentials=creds) # Call the Drive v3 API results = service.files().list( q="mimeType = 'application/vnd.google-apps.folder' and '0ALNhV0hP-QYDUk9PVA' in parents", pageSize=100, fields="nextPageToken, files(id, name, parents)").execute() items = results.get('files', []) pic_id = '' if not items: print('No files found.') else: print('1st Files:') for item in items: if item['name']=='KIOSK Picture': pic_id = item['id'] print(u'{0} ({1}) - {2}'.format(item['name'], item['id'], item['parents'])) #print(pic_id) # Check Machine ID q_str = "mimeType = 'application/vnd.google-apps.folder' and '" + str(pic_id) +"' in parents" #print(q_str) results = service.files().list( q=q_str, #"mimeType = 'application/vnd.google-apps.folder' and '" + str(pic_id) +"' in parents", pageSize=10, fields="nextPageToken, files(id, name, parents)").execute() items = results.get('files', []) bHasBaseFolder = False sMachineID = 'Test_MachineID' sMachineID_ID = '' if not items: print('No files found.') else: print('2nd Files:') for item in items: if item['name']==sMachineID: bHasBaseFolder = True sMachineID_ID = item['id'] print(u'{0} ({1}) - {2}'.format(item['name'], item['id'], item['parents'])) if bHasBaseFolder == False: file_metadata = { 'name': sMachineID, 'mimeType': 'application/vnd.google-apps.folder', 'parents': [str(pic_id)] } file = service.files().create(body=file_metadata, fields='id').execute() sMachineID_ID = str(file.get('id')) print('Folder ID: %s' % file.get('id')) #print(sMachineID_ID) # Check Date Folder sTodayDateString = datetime.now().strftime("%Y%m%d") sTodayDate_ID = '' bHasBaseFolder = False q_str = "mimeType = 'application/vnd.google-apps.folder' and '" + str(sMachineID_ID) +"' in parents" results = service.files().list( q=q_str, pageSize=10, fields="nextPageToken, files(id, name, parents)").execute() items = results.get('files', []) if not items: print('No files found.') else: print('3nd Files:') for item in items: if item['name']==sTodayDateString: bHasBaseFolder = True sTodayDate_ID = item['id'] print(u'{0} ({1}) - {2}'.format(item['name'], item['id'], item['parents'])) if bHasBaseFolder == False: file_metadata = { 'name': sTodayDateString, 'mimeType': 'application/vnd.google-apps.folder', 'parents': [str(sMachineID_ID)] } file = service.files().create(body=file_metadata, fields='id').execute() sTodayDate_ID = str(file.get('id')) print('Folder ID: %s' % file.get('id')) #Check Test Location sTestLocation='我是測試考場(真的是測試用)' sTestLocation_ID = '' bHasBaseFolder = False q_str = "mimeType = 'application/vnd.google-apps.folder' and '" + str(sTodayDate_ID) +"' in parents" results = service.files().list( q=q_str, pageSize=10, fields="nextPageToken, files(id, name, parents)").execute() items = results.get('files', []) if not items: print('No files found.') else: print('4nd Files:') for item in items: if item['name']==sTestLocation: bHasBaseFolder = True sTestLocation_ID = item['id'] print(u'{0} ({1}) - {2}'.format(item['name'], item['id'], item['parents'])) if bHasBaseFolder == False: file_metadata = { 'name': sTestLocation, 'mimeType': 'application/vnd.google-apps.folder', 'parents': [str(sTodayDate_ID)] } file = service.files().create(body=file_metadata, fields='id').execute() sTestLocation_ID = str(file.get('id')) print('Folder ID: %s' % file.get('id')) sTestLocation_ID = CreateGoogleDriveFolder(service, sTestLocation, sTodayDate_ID) print('Check Function') print(sTestLocation_ID) def CreateGoogleDriveFolder(service, titlestring, folderid): returnfolderid='' bHasFolder = False q_str = "mimeType = 'application/vnd.google-apps.folder' and '" + str(folderid) +"' in parents" results = service.files().list( q=q_str, pageSize=10, fields="nextPageToken, files(id, name, parents)").execute() items = results.get('files', []) if not items: print('No files found.') else: for item in items: if item['name']==titlestring: bHasFolder = True returnfolderid = item['id'] print(u'{0} ({1}) - {2}'.format(item['name'], item['id'], item['parents'])) if bHasFolder == False: try: file_metadata = { 'name': titlestring, 'mimeType': 'application/vnd.google-apps.folder', 'parents': [str(folderid)] } file = service.files().create(body=file_metadata, fields='id').execute() returnfolderid = str(file.get('id')) print('Folder ID: %s' % file.get('id')) except Exception as ex: print(ex) return returnfolderid if __name__ == '__main__': main()
6,981
2,191
from .QArithmetic import * from .qft import *
46
16
"""Calculate conditional probability of a short interevent time being followed by another short interevent time, compared with the unconditional probability. This is used to test whether fault records have memory """ import os, sys from glob import glob import numpy as np import matplotlib.pyplot as plt from QuakeRates.dataman.parse_params import parse_param_file, \ get_event_sets # Define parameter files filepath = '../params' param_file_list = glob(os.path.join(filepath, '*.txt')) n_samples = 500 # Number of Monte Carlo samples of the eq chronologies half_n = int(n_samples/2) plot_dir = './plots_conditional_probs' if not os.path.exists(plot_dir): os.makedirs(plot_dir) # Define subset to take #faulting_styles = ['Reverse'] #faulting_styles = ['Normal'] #faulting_styles = ['Strike_slip'] faulting_styles = ['all'] tectonic_regions = ['all'] #tectonic_regions = ['Plate_boundary_master', 'Plate_boundary_network'] min_number_events = 10 names, event_sets, event_certainties, num_events = \ get_event_sets(param_file_list, tectonic_regions, faulting_styles, min_number_events) # Now loop over paleo-earthquake records for i, event_set in enumerate(event_sets): # Generate some chronologies event_set.gen_chronologies(n_samples, observation_end=2019, min_separation=1) print(num_events[i]) event_set.calculate_cov() # Calculate interevent times and mean as part of this # Lists to store results uncond_probs = [] cond_probs = [] for j, sample in enumerate(event_set.interevent_times.T): num_less_mean = len(np.argwhere(sample < event_set.means[j])) uncond_prob_less_mean = num_less_mean/event_set.num_events count_short = 0 for k, ie_time in enumerate(sample): if k==0: ie_time_0 = ie_time else: if ie_time < event_set.means[i] and \ ie_time_0 < event_set.means[i]: count_short += 1 ie_time_0 = ie_time cond_prob_less_mean = count_short/num_less_mean uncond_probs.append(uncond_prob_less_mean) cond_probs.append(cond_prob_less_mean) print(uncond_probs) print(cond_probs) uncond_probs = np.array(uncond_probs) cond_probs = np.array(cond_probs) probs_ratio = cond_probs/uncond_probs print(probs_ratio) plt.clf() plt.hist(probs_ratio, bins = 10, facecolor='0.6', edgecolor='0.2', density=True) figname = 'conditional_prob_ratio_histogram_%s.png' % names[i] fig_filename = os.path.join(plot_dir, figname) plt.savefig(fig_filename)
2,631
900
import time #Controls functions for the delta sleep_time = 0.5 def turn_on_vacuum(): print("Turning on vacuum pump") def pickup(): print("Picking up sample...") time.sleep(sleep_time) def drop(): print("Dropping sample...") time.sleep(sleep_time) def move_to_start(): print("Moving to Start...") time.sleep(sleep_time) def move_to_camera(): print("Moving to Camera...") time.sleep(sleep_time) def move_to_passed(): print("Moving to Pass Stack...") time.sleep(sleep_time) def move_to_failed(): print("Moving to Fail Stack...") time.sleep(sleep_time)
643
221
# From The School of AI's Move 37 Course https://www.theschool.ai/courses/move-37-course/ # Coding demo by Colin Skow # Forked from https://github.com/lazyprogrammer/machine_learning_examples/tree/master/rl # Credit goes to LazyProgrammer from __future__ import print_function, division from builtins import range # Note: you may need to update your version of future # sudo pip install -U future import numpy as np from grid_world import standard_grid from utils import print_values, print_policy # SMALL_ENOUGH is referred to by the mathematical symbol theta in equations SMALL_ENOUGH = 1e-3 GAMMA = 0.9 ALL_POSSIBLE_ACTIONS = ('U', 'D', 'L', 'R') def best_action_value(grid, V, s): # finds the highest value action (max_a) from state s, returns the action and value best_a = None best_value = float('-inf') grid.set_state(s) # loop through all possible actions to find the best current action for a in ALL_POSSIBLE_ACTIONS: transititions = grid.get_transition_probs(a) expected_v = 0 expected_r = 0 for (prob, r, state_prime) in transititions: expected_r += prob * r expected_v += prob * V[state_prime] v = expected_r + GAMMA * expected_v if v > best_value: best_value = v best_a = a return best_a, best_value def calculate_values(grid): # initialize V(s) V = {} states = grid.all_states() for s in states: V[s] = 0 # repeat until convergence # V[s] = max[a]{ sum[s',r] { p(s',r|s,a)[r + gamma*V[s']] } } while True: # biggest_change is referred to by the mathematical symbol delta in equations biggest_change = 0 for s in grid.non_terminal_states(): old_v = V[s] _, new_v = best_action_value(grid, V, s) V[s] = new_v biggest_change = max(biggest_change, np.abs(old_v - new_v)) if biggest_change < SMALL_ENOUGH: break return V def initialize_random_policy(): # policy is a lookup table for state -> action # we'll randomly choose an action and update as we learn policy = {} for s in grid.non_terminal_states(): policy[s] = np.random.choice(ALL_POSSIBLE_ACTIONS) return policy def calculate_greedy_policy(grid, V): policy = initialize_random_policy() # find a policy that leads to optimal value function for s in policy.keys(): grid.set_state(s) # loop through all possible actions to find the best current action best_a, _ = best_action_value(grid, V, s) policy[s] = best_a return policy if __name__ == '__main__': # this grid gives you a reward of -0.1 for every non-terminal state # we want to see if this will encourage finding a shorter path to the goal grid = standard_grid(obey_prob=0.8, step_cost=-0.5) # print rewards print("rewards:") print_values(grid.rewards, grid) # calculate accurate values for each square V = calculate_values(grid) # calculate the optimum policy based on our values policy = calculate_greedy_policy(grid, V) # our goal here is to verify that we get the same answer as with policy iteration print("values:") print_values(V, grid) print("policy:") print_policy(policy, grid)
3,120
1,055
#!/usr/bin/env python # -*- coding: utf-8 -*- # # GTKcsvimport.py # # Copyright 2010-2015 Jose Riguera Lopez <jriguera@gmail.com> # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """ Parse a CSV to add variables or geolocate photos. GTK User Interface. """ __program__ = "photoplace.csvimport" __author__ = "Jose Riguera Lopez <jriguera@gmail.com>" __version__ = "0.1.2" __date__ = "Dec 2014" __license__ = "Apache 2.0" __copyright__ ="(c) Jose Riguera" import os.path import csv import sys import codecs import warnings import gettext import locale warnings.filterwarnings('ignore', module='gtk') try: import pygtk pygtk.require("2.0") import gtk import gobject except Exception as e: warnings.resetwarnings() print("Warning: %s" % str(e)) print("You don't have the PyGTK 2.0 module installed") raise warnings.resetwarnings() from csvimport import * # I18N gettext support __GETTEXT_DOMAIN__ = __program__ __PACKAGE_DIR__ = os.path.abspath(os.path.dirname(__file__)) __LOCALE_DIR__ = os.path.join(__PACKAGE_DIR__, u"locale") try: if not os.path.isdir(__LOCALE_DIR__): print ("Error: Cannot locate default locale dir: '%s'." % (__LOCALE_DIR__)) __LOCALE_DIR__ = None locale.setlocale(locale.LC_ALL,"") #gettext.bindtextdomain(__GETTEXT_DOMAIN__, __LOCALE_DIR__) t = gettext.translation(__GETTEXT_DOMAIN__, __LOCALE_DIR__, fallback=False) _ = t.ugettext except Exception as e: print ("Error setting up the translations: %s" % (str(e))) _ = lambda s: unicode(s) class GTKCSVImport(object): def __init__(self, plugin, gui, userfacade, logger): object.__init__(self) self.plugin = gtk.VBox(False) self.logger = logger self.options = None self.userfacade = userfacade # 1st line hbox = gtk.HBox(False) label_name = gtk.Label() align = gtk.Alignment(0.01, 0.5, 0, 0) label_name.set_markup(_("CSV file:")) align.add(label_name) hbox.pack_start(align, False, False, 5) self.button_addfile = gtk.Button() image = gtk.Image() image.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON) self.button_addfile.set_image(image) self.button_addfile.set_tooltip_text(_("Select a CSV file to load photo's variables")) self.button_addfile.set_label(_("Select file")) self.button_addfile.connect('clicked', self._load_csv) align = gtk.Alignment(0.01, 0.5, 0, 0) align.add(self.button_addfile) hbox.pack_start(align, False, False, 5) self.plugin.pack_start(hbox, False, False, 5) # 3rd line hbox_headers = gtk.HBox(False) label_headers = gtk.Label() label_headers.set_markup(_("Headers:")) hbox_headers.pack_start(label_headers, False, False, 5) self.entry_headers = gtk.Entry(max=256) self.entry_headers.connect('focus-out-event', self._out_entry) self.entry_headers.connect('activate', self._set_entry) self.entry_headers.set_tooltip_text(_("List of column headers of the CSV file. Each header will be a variable for each photo")) self.entry_headers.set_sensitive(False) hbox_headers.pack_start(self.entry_headers, True, True, 2) label_headerid = gtk.Label() label_headerid.set_markup(_("where photo name is:")) hbox_headers.pack_start(label_headerid, False, False, 0) self.cb_headerid = gtk.combo_box_new_text() self.cb_headerid.connect("changed", self._combo_id) self.cb_headerid.set_tooltip_text(_("Name of the column to match with each photo file name. It must be one of the Headers")) self.cb_headerid.set_sensitive(False) self.cb_headerid.append_text(' ') hbox_headers.pack_start(self.cb_headerid, False, False, 5) self.plugin.pack_start(hbox_headers, False, False, 5) # 4st line self.checkbutton_geolocate = gtk.CheckButton(_("Geolocate photos using CSV headers")) self.checkbutton_geolocate.set_tooltip_text(_("It is active, it will assign the following headers to each photo. It will geotag the photos by using those headers, but, warning: GPX data will take precedence!")) self.checkbutton_geolocate.connect('toggled', self._geolocate) self.checkbutton_geolocate.set_sensitive(False) # Headers Variables self.frame = gtk.Frame() self.frame.set_label_widget(self.checkbutton_geolocate) table = gtk.Table(2, 4, True) label_lat = gtk.Label() label_lat.set_markup(_("Latitude:")) align = gtk.Alignment(1.00, 0.5, 0, 0) align.add(label_lat) table.attach(align, 0, 1, 0, 1, gtk.FILL) self.cb_lat = gtk.combo_box_new_text() self.cb_lat.connect("changed", self._combo_geolocate, CSVImport_CONFKEY_HEADER_LAT) self.cb_lat.set_tooltip_text(_("Latitude header name")) table.attach(self.cb_lat, 1, 2, 0, 1) label_lon = gtk.Label() label_lon.set_markup(_("Longitude:")) align = gtk.Alignment(1.00, 0.5, 0, 0) align.add(label_lon) table.attach(align, 2, 3, 0, 1, gtk.FILL) self.cb_lon = gtk.combo_box_new_text() self.cb_lon.connect("changed", self._combo_geolocate, CSVImport_CONFKEY_HEADER_LON) self.cb_lon.set_tooltip_text(_("Longitude header name")) table.attach(self.cb_lon, 3, 4, 0, 1) label_date = gtk.Label() label_date.set_markup(_("Time-Date:")) align = gtk.Alignment(1.00, 0.5, 0, 0) align.add(label_date) table.attach(align, 0, 1, 1, 2) self.cb_date = gtk.combo_box_new_text() self.cb_date.connect("changed", self._combo_geolocate, CSVImport_CONFKEY_HEADER_DATE) table.attach(self.cb_date, 1, 2, 1, 2) label_ele = gtk.Label() label_ele.set_markup(_("Elevation:")) align = gtk.Alignment(1.00, 0.5, 0, 0) align.add(label_ele) table.attach(align, 2, 3, 1, 2) self.cb_ele = gtk.combo_box_new_text() self.cb_ele.connect("changed", self._combo_geolocate, CSVImport_CONFKEY_HEADER_ELE) self.cb_ele.set_tooltip_text(_("Elevation header name")) table.attach(self.cb_ele, 3, 4, 1, 2) table.set_border_width(20) table.set_row_spacings(5) self.frame.add(table) self.frame.set_border_width(5) self.frame.set_sensitive(False) self.plugin.pack_start(self.frame, False, False, 5) # Button self.button_process = gtk.Button() self.button_process.set_label(_("Process")) image = gtk.Image() image.set_from_stock(gtk.STOCK_EXECUTE, gtk.ICON_SIZE_BUTTON) self.button_process.set_image(image) self.button_process.connect('clicked', self.process) align = gtk.Alignment(0.50, 0.5, 0.1, 0) align.add(self.button_process) self.plugin.pack_start(align, False, False, 0) self.button_process.set_sensitive(False) # Attributes self.rootplugin = plugin self.rootgui = gui self.window = gui.builder.get_object("window") self.events = True def _load_csv(self, widget): dialog = gtk.FileChooserDialog(title=_("Select CSV file ..."), parent=self.window, action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) ffilter = gtk.FileFilter() ffilter.set_name(_("Comma Separated Values (CSV)")) ffilter.add_pattern("*.csv") dialog.add_filter(ffilter) ffilter = gtk.FileFilter() ffilter.set_name(_("All files")) ffilter.add_pattern("*") dialog.add_filter(ffilter) filename = None if dialog.run() == gtk.RESPONSE_OK: filename = dialog.get_filename() dialog.destroy() self._set_csv(filename) def _set_csv(self, filename): if filename != None and os.path.isfile(filename): if not isinstance(filename, unicode): try: filename = unicode(filename, 'UTF-8') except: pass shortfilename = " " + os.path.basename(filename) + " " if len(shortfilename) > 150: shortfilename = shortfilename[0:150] + " ... " image = self.button_addfile.get_image() image.clear() self.button_addfile.set_label(shortfilename) self.frame.set_sensitive(True) self.checkbutton_geolocate.set_sensitive(True) self.cb_headerid.set_sensitive(True) self.entry_headers.set_sensitive(True) self.button_process.set_sensitive(True) self._init_csv(filename) self._geolocate() else: self.reset() def _init_csv(self, filename): dgettext = dict() dgettext['file'] = filename try: fd = open(filename, 'rb') except Exception as e: dgettext['error'] = str(e) msg = _("Cannot read file '%(file)s': %(error)s") self.logger.error(msg % dgettext) self.rootgui.show_dialog(_("Error"), msg, _('Please check file permisions')) else: dialect = 'excel' headers = self.options[CSVImport_CONFKEY_HEADERS] delimiter = self.options[CSVImport_CONFKEY_DELIMITER] quotechar = self.options[CSVImport_CONFKEY_QUOTECHAR] if not delimiter or not quotechar: dialect = csv.Sniffer().sniff(fd.read(10240)) delimiter = dialect.delimiter quotechar = dialect.quotechar fd.seek(0) else: dgettext['delimiter'] = delimiter dgettext['quotechar'] = quotechar has_header = csv.Sniffer().has_header(fd.read(10240)) fd.seek(0) headers_defined = False if headers: headers_defined = True else: reader = csv.DictReader(fd, dialect=dialect, delimiter=delimiter, quotechar=quotechar) if has_header: reader.next() headers = reader.fieldnames headers_defined = True self.options[CSVImport_CONFKEY_FILENAME] = filename if not headers_defined: msg = _("File has no headers") tip = _('You have to define the name of the headers.') self.rootgui.show_dialog(_("Warning"), msg, tip, gtk.MESSAGE_WARNING) else: self.entry_headers.set_text(', '.join(headers)) try: index = headers.index(self.options[CSVImport_CONFKEY_HEADER_ID]) except: index = 0 self._set_combo(self.cb_headerid, headers, CSVImport_CONFKEY_HEADER_ID, index) self.rootplugin.update_headers(headers) fd.close() def process(self, widget=None): filename = self.options[CSVImport_CONFKEY_FILENAME] if filename != None: self.rootplugin.init_csv(filename) counter = self.rootplugin.process_csv(self.userfacade.state.geophotos) self.rootplugin.logger.info(_("%d photos processed with CSV data.") % counter) self.rootplugin.end_csv() self.rootgui.reload_treeviewgeophotos() def _geolocate(self, widget=None): self.events = False value = self.checkbutton_geolocate.get_active() self.cb_date.set_sensitive(value) self.cb_ele.set_sensitive(value) self.cb_lon.set_sensitive(value) self.cb_lat.set_sensitive(value) self.options[CSVImport_CONFKEY_GEOLOCATE] = value if not value: self.options[CSVImport_CONFKEY_HEADER_LAT] = '' self.options[CSVImport_CONFKEY_HEADER_LON] = '' self.options[CSVImport_CONFKEY_HEADER_ELE] = '' self.options[CSVImport_CONFKEY_HEADER_DATE] = '' self._empty_combo(self.cb_date) self._empty_combo(self.cb_ele) self._empty_combo(self.cb_lon) self._empty_combo(self.cb_lat) else: headers = [" "] + self.options[CSVImport_CONFKEY_HEADERS] try: headers.remove(self.cb_headerid.get_active_text()) except: pass self._set_combo(self.cb_date, headers) self._set_combo(self.cb_ele, headers) self._set_combo(self.cb_lon, headers) self._set_combo(self.cb_lat, headers) counter = 0 for i in headers: item = i.lower() if i == self.options[CSVImport_CONFKEY_HEADER_LAT]: self.cb_lat.set_active(counter) elif i == self.options[CSVImport_CONFKEY_HEADER_LON]: self.cb_lon.set_active(counter) elif i == self.options[CSVImport_CONFKEY_HEADER_ELE]: self.cb_ele.set_active(counter) elif i == self.options[CSVImport_CONFKEY_HEADER_DATE]: self.cb_date.set_active(counter) elif 'lat' in item: self.cb_lat.set_active(counter) self.options[CSVImport_CONFKEY_HEADER_LAT] = i elif 'lon' in item: self.cb_lon.set_active(counter) self.options[CSVImport_CONFKEY_HEADER_LON] = i elif 'ele' in item: self.cb_ele.set_active(counter) self.options[CSVImport_CONFKEY_HEADER_ELE] = i elif 'date' in item or 'time' in item: self.cb_date.set_active(counter) self.options[CSVImport_CONFKEY_HEADER_DATE] = i counter += 1 self.events = True def _out_entry(self, widget, e): widget.set_text(', '.join(self.options[CSVImport_CONFKEY_HEADERS])) return False def _set_entry(self, widget): value = widget.get_text() items = [] try: char = None for c in [',', ';', '|', '#']: if c in value: char = c break else: raise Exception for item in value.split(char): items.append(item.strip()) if len(items) < 2: raise Exception except: msg = _("Cannot set headers") tip = _("Please, define the name of the headers to be used as variables.") self.rootgui.show_dialog(_("Error"), msg, tip) else: try: index = items.index(self.options[CSVImport_CONFKEY_HEADER_ID]) except: index = 0 self._set_combo(self.cb_headerid, items, CSVImport_CONFKEY_HEADER_ID, index) self.rootplugin.update_headers(items) self._geolocate() def _set_combo(self, cb, items=[], key=None, active=None): self.events = False cb.get_model().clear() for item in items: cb.append_text(item) if active != None: self.options[key] = items[active] cb.set_active(active) self.events = True def _empty_combo(self, cb): cb.get_model().clear() def _combo_geolocate(self, widget, key): if self.events: header = widget.get_active_text() if header in self.options[CSVImport_CONFKEY_HEADERS]: self.options[key] = header else: self.options[key] = '' def _activate_combo(self, cb, key, value, no): counter = 0 for row in cb.get_model(): if value == row[0]: if row[0] == no: cb.set_active(0) self.options[key] = '' else: cb.set_active(counter) self.options[key] = row[0] break counter += 1 def _combo_id(self, widget): if self.events: header = widget.get_active_text() self.options[CSVImport_CONFKEY_HEADER_ID] = header header_lat = self.cb_lat.get_active_text() header_lon = self.cb_lon.get_active_text() header_ele = self.cb_ele.get_active_text() header_date = self.cb_date.get_active_text() self._geolocate() self._activate_combo(self.cb_lat, CSVImport_CONFKEY_HEADER_LAT, header_lat, header) self._activate_combo(self.cb_lon, CSVImport_CONFKEY_HEADER_LON, header_lon, header) self._activate_combo(self.cb_ele, CSVImport_CONFKEY_HEADER_ELE, header_ele, header) self._activate_combo(self.cb_date, CSVImport_CONFKEY_HEADER_DATE, header_date, header) def show(self, widget=None, options=None): if widget: widget.add(self.plugin) if options: self.setup(options) self.plugin.show_all() def hide(self, reset=False): self.plugin.hide_all() if reset: self.reset() def reset(self): self.button_process.set_sensitive(False) self.checkbutton_geolocate.set_sensitive(False) self.frame.set_sensitive(False) self._empty_combo(self.cb_headerid) self.cb_headerid.set_sensitive(False) self.options[CSVImport_CONFKEY_HEADER_ID] = '' self.entry_headers.set_sensitive(False) self.entry_headers.set_text('') image = self.button_addfile.get_image() image.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON) self.button_addfile.set_image(image) self.button_addfile.set_label(_("Select file")) self.checkbutton_geolocate.set_active(False) self.options[CSVImport_CONFKEY_FILENAME] = '' self.rootplugin.update_headers() self.userfacade.state.photovariables = self.rootplugin.photovariables_old self._geolocate() self.rootgui.reload_treeviewgeophotos() self.events = True def setup(self, options): self.options = options self.cb_date.set_tooltip_text(_("Date header name. Format should be: ") + self.options[CSVImport_CONFKEY_DATE_PARSE]) if options[CSVImport_CONFKEY_GEOLOCATE]: self.checkbutton_geolocate.set_active(True) filename = options[CSVImport_CONFKEY_FILENAME] if filename: self._set_csv(filename) #self.entry_headers.set_text(', '.join(options[CSVImport_CONFKEY_HEADERS])) #EOF
19,238
6,283
# -*- coding: utf-8 -*- # Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (MPG) is # holder of all proprietary rights on this computer program. # You can only use this computer program if you have closed # a license agreement with MPG or you get the right to use the computer # program from someone who is authorized to grant you that right. # Any use of the computer program without a valid license is prohibited and # liable to prosecution. # # Copyright©2020 Max-Planck-Gesellschaft zur Förderung # der Wissenschaften e.V. (MPG). acting on behalf of its Max Planck Institute # for Intelligent Systems. All rights reserved. # # Contact: ps-license@tuebingen.mpg.de import torch import torch.nn.functional as F import numpy as np import torchgeometry as tgm from src import misc_utils, eulerangles from tqdm import tqdm def compute_afford_loss(vertices=None, scene_data=None, gen_batch=None, pen_w=0.0, no_obj_classes=None, use_semantics=False, semantics_w=0.0, **kwargs): contact_ids = gen_batch[:, :, 0] > 0.5 x = misc_utils.read_sdf(vertices, scene_data['sdf'], scene_data['grid_dim'], scene_data['grid_min'], scene_data['grid_max'], mode="bilinear").squeeze() batch_size = vertices.shape[0] device = vertices.device contact_loss = torch.sum(x[contact_ids.flatten()] ** 2) pen_loss = torch.tensor(0.0) if pen_w > 0: mask = x.lt(0).flatten().int() + (~contact_ids.flatten()).int() x_neg = torch.abs(x[mask == 2]) if len(x_neg) == 0: pen_loss = torch.tensor(0.0) else: pen_loss = pen_w * x_neg.sum() semantics_loss = torch.tensor(0.0) if use_semantics: # Read semantics x_semantics = misc_utils.read_sdf(vertices, scene_data['semantics'], scene_data['grid_dim'], scene_data['grid_min'], scene_data['grid_max'], mode="bilinear").squeeze() x_semantics = contact_ids.flatten().float() * x_semantics.unsqueeze(0) x_semantics = torch.zeros(x_semantics.shape[0], x_semantics.shape[1], no_obj_classes, device=device).scatter_( -1, x_semantics.unsqueeze(-1).type(torch.long), 1.) # Compute loss targets = gen_batch[:, :, 1:].argmax(dim=-1).type(torch.long).reshape(batch_size, -1) semantics_loss = semantics_w * F.cross_entropy(x_semantics.permute(0, 2, 1), targets, reduction='sum') return contact_loss, pen_loss, semantics_loss def eval_init_points(init_pos=None, init_ang=None, vertices=None, scene_data=None, gen_batch=None, **kwargs): with torch.no_grad(): losses = [] init_pos_batches = init_pos.split(1) for i in tqdm(range(len(init_pos_batches))): curr_init_pos = init_pos_batches[i] rot_aa = torch.cat((torch.zeros((1, 2), device=vertices.device), init_ang[i].reshape(1, 1)), dim=1) rot_mat = tgm.angle_axis_to_rotation_matrix(rot_aa.reshape(-1, 3))[:, :3, :3] curr_vertices = torch.bmm(rot_mat, vertices.permute(0, 2, 1)).permute(0, 2, 1) curr_vertices = curr_vertices + curr_init_pos contact_loss, pen_loss, semantics_loss = compute_afford_loss(vertices=curr_vertices, scene_data=scene_data, gen_batch=gen_batch, **kwargs) loss = contact_loss + pen_loss + semantics_loss losses.append(loss.item()) # Sort initial positions and orientations from best to wrost losses = np.array(losses) ids = np.argsort(losses) losses = losses[ids] init_pos = init_pos[ids] init_ang = init_ang[ids] return losses, init_pos, init_ang def init_points_culling(init_pos=None, vertices=None, scene_data=None, gen_batch=None, max_init_points=50, **kwargs): init_ang = [] angles = torch.arange(0, 2 * np.pi, np.pi / 2, device=vertices.device) angles[0] = 1e-9 for ang in angles: init_ang.append(ang * torch.ones(init_pos.shape[0], 1, device=vertices.device)) init_ang = torch.cat(init_ang).to(init_pos.device) init_pos = init_pos.repeat(angles.shape[0], 1, 1) # Shuffle rnd_ids = np.random.choice(init_pos.shape[0], init_pos.shape[0], replace=False) init_pos = init_pos[rnd_ids, :] init_ang = init_ang[rnd_ids, :] losses, init_pos, init_ang = eval_init_points(init_pos=init_pos, init_ang=init_ang, vertices=vertices.unsqueeze(0), scene_data=scene_data, gen_batch=gen_batch, **kwargs) # Select only a subset from initial points for optimization if init_pos.shape[0] > max_init_points: init_pos = init_pos[:max_init_points] init_ang = init_ang[:max_init_points] return init_pos, init_ang class opt_wrapper(object): def __init__(self, vertices=None, vertices_can=None, pelvis=None, scene_data=None, down_sample_fn=None, down_sample_fn2=None, device=None, dtype=None, pen_w=None, use_semantics=None, no_obj_classes=None, nv=None, optimizer=None, gen_batch=None, body_model=None, opt_pose=False, semantics_w=None, init_body_pose=None, pose_w=None, **kwargs): self.optimizer = optimizer self.vertices = vertices self.vertices_can = vertices_can self.pelvis = pelvis self.scene_data = scene_data self.down_sample_fn = down_sample_fn self.down_sample_fn2 = down_sample_fn2 self.device = device self.dtype = dtype self.pen_w = pen_w self.pose_w = pose_w self.semantics_w = semantics_w self.use_semantics = use_semantics self.no_obj_classes = no_obj_classes self.nv = nv self.gen_batch = gen_batch self.opt_pose = opt_pose self.body_model = body_model self.init_body_pose = init_body_pose self.R_smpl2scene = torch.tensor(eulerangles.euler2mat(np.pi / 2, 0, 0, 'sxyz'), dtype=dtype, device=device) def compute_vertices(self, t_free, y_ang, vertices=None, down_sample=True): curr_batch_size = self.vertices.shape[0] rot_aa = torch.cat((torch.zeros((curr_batch_size, 2), device=self.device), y_ang), dim=1) rot_mat = tgm.angle_axis_to_rotation_matrix(rot_aa.reshape(-1, 3))[:, :3, :3] if self.opt_pose: body_model_output = self.body_model(return_verts=True) pelvis = body_model_output.joints[:, 0, :].reshape(1, 3) vertices_local = body_model_output.vertices.squeeze() vertices_local = torch.matmul(self.R_smpl2scene, (vertices_local - pelvis).t()).t() vertices_local.unsqueeze_(0) if down_sample: vertices_local = self.down_sample_fn.forward(vertices_local.permute(0, 2, 1)) vertices_local = self.down_sample_fn2.forward(vertices_local).permute(0, 2, 1) vertices_local = torch.bmm(rot_mat, vertices_local.permute(0, 2, 1)).permute(0, 2, 1) vertices_local += t_free else: # very important to make a local copy, so that you don't change the original variable if vertices is None: vertices_local = torch.bmm(rot_mat, self.vertices.permute(0, 2, 1)).permute(0, 2, 1) else: vertices_local = torch.bmm(rot_mat, vertices.permute(0, 2, 1)).permute(0, 2, 1) vertices_local += t_free return vertices_local, rot_mat def compute_loss(self, t_free, y_ang): pose_loss = torch.tensor(0.0) if self.opt_pose: pose_loss = self.pose_w * F.mse_loss(self.body_model.body_pose, self.init_body_pose) vertices_local, rot_mat = self.compute_vertices(t_free, y_ang) contact_loss, pen_loss, semantic_loss = compute_afford_loss(vertices=vertices_local, scene_data=self.scene_data, gen_batch=self.gen_batch, pen_w=self.pen_w, no_obj_classes=self.no_obj_classes, use_semantics=self.use_semantics, semantics_w=self.semantics_w) return contact_loss, pen_loss, pose_loss, semantic_loss def create_fitting_closure(self, t_free, y_ang): def fitting_func(): self.optimizer.zero_grad() recon_loss, pen_loss, pose_loss, semantic_loss = self.compute_loss(t_free, y_ang) loss_total = recon_loss + pen_loss + pose_loss + semantic_loss loss_total.backward(retain_graph=True) return loss_total return fitting_func
9,024
2,993
from tensorflow.contrib.slim.python.slim.data import data_provider from tensorflow.contrib.slim.python.slim.data import parallel_reader
137
40
# -*- coding: UTF-8 -*- ####################################################################### # ---------------------------------------------------------------------------- # "THE BEER-WARE LICENSE" (Revision 42): # @tantrumdev wrote this file. As long as you retain this notice you # can do whatever you want with this stuff. If we meet some day, and you think # this stuff is worth it, you can buy me a beer in return. - Muad'Dib # ---------------------------------------------------------------------------- ####################################################################### # Addon Name: Placenta # Addon id: plugin.video.placenta # Addon Provider: MuadDib import json from resources.lib.modules import client URL_PATTERN = 'http://thexem.de/map/single?id=%s&origin=tvdb&season=%s&episode=%s&destination=scene' def get_scene_episode_number(tvdbid, season, episode): try: url = URL_PATTERN % (tvdbid, season, episode) r = client.request(url) r = json.loads(r) if r['result'] == 'success': data = r['data']['scene'] return data['season'], data['episode'] except: pass return season, episode
1,205
341
# import colorgram # # colors = colorgram.extract('image.jpg', 30) # rgb_colors = [] # for color in colors: # rgb_colors.append((color.rgb.r, color.rgb.g, color.rgb.b)) # # print(rgb_colors) from turtle import Turtle, Screen import random color_list = [(238, 251, 245), (250, 228, 15), (213, 12, 8), (199, 11, 36), (10, 98, 61), (5, 39, 32), (232, 228, 5), (64, 221, 157), (198, 68, 19), (32, 91, 189), (43, 212, 71), (235, 148, 38), (32, 30, 153), (242, 247, 251), (15, 22, 54), (67, 9, 49), (245, 38, 148), (14, 206, 222), (65, 203, 230), (62, 20, 10), (229, 164, 7), (226, 19, 111), (14, 154, 22), (246, 58, 14), (98, 75, 8), (248, 11, 9), (223, 140, 205), (66, 241, 160), ] tim = Turtle() scr = Screen() scr.colormode(255) tim.penup() tim.hideturtle() tim.setposition(-300, -300) for i in range(10): tim.setposition(-300, tim.ycor() + 50) for j in range(10): tim.setx(tim.xcor() + 50) tim.dot(20, random.choice(color_list)) scr.exitonclick()
1,048
613
from django import forms from .models import UserAccount, JobPost, JobPostActivity, UserProfile class FormUserCreation(forms.ModelForm): UserTypes = ((1, 'Applicants'), (2, 'Organisations')) user_type = forms.ChoiceField(choices=UserTypes, widget=forms.Select(attrs={'class': "form-control"})) user_full_name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter Full Name'})) email = forms.EmailField(max_length=250, help_text="Required. Invalid format", widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter Email ID'})) password = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'type': 'password', 'placeholder': 'Enter Password', 'minlength': '6', 'onkeyup': 'check();'})) confirm_password = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'type': 'password', 'placeholder': 'Re-enter Password', 'minlength': '6', 'onkeyup': 'check();'})) class Meta: model = UserAccount fields = ('user_type', 'user_full_name', 'email', 'password') class FormLogin(forms.ModelForm): email = forms.EmailField(max_length=250, help_text="Required. Invalid format", widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter Email ID'})) password = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'type': 'password', 'placeholder': 'Enter Password', 'minlength': '6'})) class Meta: model = UserAccount fields = ('email', 'password') class FormJobPost(forms.ModelForm): Locations = (('Mumbai', 'Mumbai'), ('Navi Mumbai', 'Navi Mumbai'), ('Pune', 'Pune')) job_types = (('Software Engineer', 'Software Engineer'), ('Database Admin', 'Database Admin'), ('DevOps', 'DevOps')) jobs_skill = (('Java', 'Java'), ('Python', 'Python'), ('C', 'C'), ('C++', 'C++')) job_location = forms.ChoiceField(choices=Locations, widget=forms.Select(attrs={'class': "form-control"})) job_type = forms.ChoiceField(choices=job_types, widget=forms.Select(attrs={'class': "form-control"})) job_skills = forms.ChoiceField(choices=jobs_skill, widget=forms.Select(attrs={'class': "form-control"})) job_title = forms.CharField(max_length=100, required=True, widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter job title'})) posted_by_email = forms.EmailField(max_length=250, help_text="Required. Invalid format", widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter Email ID', 'readonly': True})) job_description = forms.CharField(widget=forms.Textarea(attrs={'class': "form-control", 'placeholder': 'Enter Job Description'})) class Meta: model = JobPost fields = ('job_type', 'job_skills', 'job_location', 'posted_by_email', 'job_description', 'job_title') class FormApply(forms.ModelForm): email = forms.EmailField(required=True, max_length=250, help_text="Required. Invalid format", widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter Email ID', 'readonly': True})) to_email = forms.EmailField(required=True, max_length=250, help_text="Required. Invalid format", widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter Email ID', 'readonly': True})) cover_letter = forms.CharField(required=True, widget=forms.Textarea(attrs={'class': "form-control", 'placeholder': 'Cover Letter'})) post_id = forms.IntegerField(required=True, widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Post ID', 'readonly': True})) job_title = forms.CharField(required=True, widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter Job Title'})) class Meta: model = JobPostActivity fields = ('email', 'post_id') class FormUploadImage(forms.Form): user_image = forms.ImageField(widget=forms.FileInput()) class Meta: model = UserAccount fields = ('user_image', ) class FormUploadResume(forms.Form): resume = forms.FileField() class Meta: model = UserAccount fields = ('resume', ) class FormApplicantsInfo(forms.Form): Gender = (('Male', 'Male'), ('Female', 'Female'), ('None', 'None')) gender = forms.ChoiceField(choices=Gender, widget=forms.Select(attrs={'class': "form-control"})) email = forms.EmailField(max_length=250, help_text="Required. Invalid format", widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter Email ID', 'readonly': True})) gmail = forms.EmailField(max_length=250, help_text="Required. Invalid format", widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter gmail id'})) linkedin = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter Linkedin profile'})) skype_id = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter skype id'})) about_me = forms.CharField(widget=forms.Textarea(attrs={'class': "form-control", 'placeholder': 'Enter About you'})) address = forms.CharField(widget=forms.Textarea(attrs={'class': "form-control", 'placeholder': 'Enter your address'})) birthday = forms.DateField(widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter DOB in DD-MM-YYYY'})) job_title = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter Job Title'})) location = forms.CharField(widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': 'Enter Your location'})) class Meta: model = UserProfile fields = ('email', 'gmail', 'linkedin', 'skype_id', 'about_me', 'address', 'birthday', 'job_title', 'location', 'gender')
8,723
2,077
#!/usr/bin/env python #_MIT License #_ #_Copyright (c) 2017 Dan Persons (dpersonsdev@gmail.com) #_ #_Permission is hereby granted, free of charge, to any person obtaining a copy #_of this software and associated documentation files (the "Software"), to deal #_in the Software without restriction, including without limitation the rights #_to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #_copies of the Software, and to permit persons to whom the Software is #_furnished to do so, subject to the following conditions: #_ #_The above copyright notice and this permission notice shall be included in all #_copies or substantial portions of the Software. #_ #_THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #_IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #_FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #_AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #_LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #_OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #_SOFTWARE. import time from time import strftime from time import sleep from time import daylight from time import timezone from time import altzone from random import randrange from datetime import datetime import MySQLdb as mdb import json import threading import os from sys import exit import siemstress.manage #import signal class SiemTrigger: def __init__(self, db, rule): """Initialize trigger object""" self.db = db self.rule = rule self.tzone = None def watch_rule(self): """Watch a trigger rule""" # Set time zone: if daylight: self.tzone = \ str(int(float(altzone) / 60 // 60)).rjust(2, '0') + \ str(int(float(altzone) / 60 % 60)).ljust(2, '0') else: self.tzone = \ str(int(float(timezone) / 60 // 60)).rjust(2, '0') + \ str(int(float(timezone) / 60 % 60)).ljust(2, '0') if not '-' in self.tzone: self.tzone = '+' + self.tzone while True: # Check the rule: self.check_rule() # Wait until the next interval sleep(int(self.rule['time_int']) * 60) def check_rule(self): """Check a trigger rule""" # To Do: Add date_stamp_utc/int logic if not self.tzone: # Set time zone: if time.localtime().tm_isdst: self.tzone = \ str(int(float(altzone) / 60 // 60)).rjust(2, '0') + \ str(int(float(altzone) / 60 % 60)).ljust(2, '0') else: self.tzone = \ str(int(float(timezone) / 60 // 60)).rjust(2, '0') + \ str(int(float(timezone) / 60 % 60)).ljust(2, '0') if not '-' in self.tzone: self.tzone = '+' + self.tzone # Query the database: con = mdb.connect(self.db['host'], self.db['user'], self.db['password'], self.db['database']) with con: cur = con.cursor() cur.execute(self.rule['sql_query']) rows = cur.fetchall() cur.close() con.close() # Evaluate the results: if len(rows) > int(self.rule['event_limit']): idtags = json.dumps([int(row[0]) for row in rows]) datestamp = datetime.now().strftime('%Y%m%d%H%M%S') datestamputc = datetime.utcnow().strftime('%Y%m%d%H%M%S') magnitude = (((len(rows) // 2) // \ (self.rule['event_limit'] + 1) // 2) + 5) * \ ( 7 - self.rule['severity']) outstatement = 'INSERT INTO ' + \ self.rule['out_table'] + \ '(date_stamp, date_stamp_utc, t_zone, ' + \ 'source_rule, severity, source_table, event_limit, ' + \ 'event_count, magnitude, time_int, message, source_ids) ' + \ 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' # Send an event to the database: con = mdb.connect(self.db['host'], self.db['user'], self.db['password'], self.db['database']) with con: cur = con.cursor() cur.execute(outstatement, (datestamp, datestamputc, self.tzone, self.rule['rule_name'], self.rule['severity'], self.rule['source_table'], self.rule['event_limit'], len(rows), magnitude, self.rule['time_int'], self.rule['message'], idtags)) cur.close() con.close() def start_rule(db, rule, oneshot): """Initialize trigger object and start watching""" # Make sure the table exists: siemstress.manage.create_ruleevent_table(rule['out_table']) sentry = SiemTrigger(db, rule) if oneshot: sentry.check_rule() elif int(rule['time_int']) == 0: pass else: # Before starting, sleep randomly up to rule interval to stagger # database use: sleep(randrange(0, int(rule['time_int']) * 60)) sentry.watch_rule()
5,538
1,768
# Generated by Django 3.2.7 on 2021-09-12 01:29 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('authentication', '0001_initial'), ] operations = [ migrations.AddField( model_name='userprofile', name='mail', field=models.EmailField(blank=True, db_index=True, max_length=127, null=True, unique=True, verbose_name='电话'), ), migrations.AlterField( model_name='userprofile', name='telephone', field=models.CharField(blank=True, db_index=True, max_length=127, null=True, unique=True, verbose_name='电话'), ), ]
687
223
# coding: utf-8 # # Copyright 2022 :Barry-Thomas-Paul: Moss # # Licensed under the Apache License, Version 2.0 (the "License") # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http: // www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Struct Class # this is a auto generated file generated by Cheetah # Namespace: com.sun.star.util # Libre Office Version: 7.3 from ooo.oenv.env_const import UNO_NONE import typing from .time import Time as Time_604e0855 class TimeWithTimezone(object): """ Struct Class represents a combined time value with time zone. **since** LibreOffice 4.1 See Also: `API TimeWithTimezone <https://api.libreoffice.org/docs/idl/ref/structcom_1_1sun_1_1star_1_1util_1_1TimeWithTimezone.html>`_ """ __ooo_ns__: str = 'com.sun.star.util' __ooo_full_ns__: str = 'com.sun.star.util.TimeWithTimezone' __ooo_type_name__: str = 'struct' typeName: str = 'com.sun.star.util.TimeWithTimezone' """Literal Constant ``com.sun.star.util.TimeWithTimezone``""" def __init__(self, TimeInTZ: typing.Optional[Time_604e0855] = UNO_NONE, Timezone: typing.Optional[int] = 0) -> None: """ Constructor Arguments: TimeInTZ (Time, optional): TimeInTZ value. Timezone (int, optional): Timezone value. """ super().__init__() if isinstance(TimeInTZ, TimeWithTimezone): oth: TimeWithTimezone = TimeInTZ self.TimeInTZ = oth.TimeInTZ self.Timezone = oth.Timezone return kargs = { "TimeInTZ": TimeInTZ, "Timezone": Timezone, } if kargs["TimeInTZ"] is UNO_NONE: kargs["TimeInTZ"] = None self._init(**kargs) def _init(self, **kwargs) -> None: self._time_in_tz = kwargs["TimeInTZ"] self._timezone = kwargs["Timezone"] @property def TimeInTZ(self) -> Time_604e0855: """ the time (in TimeZone) """ return self._time_in_tz @TimeInTZ.setter def TimeInTZ(self, value: Time_604e0855) -> None: self._time_in_tz = value @property def Timezone(self) -> int: """ contains the time zone, as signed offset in minutes from UTC, that is east of UTC, that is the amount of minutes that should be added to UTC time to obtain the time in that timezone. To obtain UTC time from TimeInTZ, you need to subtract TimeZone minutes. """ return self._timezone @Timezone.setter def Timezone(self, value: int) -> None: self._timezone = value __all__ = ['TimeWithTimezone']
3,040
1,010
from pathlib import Path from unittest import mock import pytest from fab.build_config import AddFlags from fab.dep_tree import AnalysedFile from fab.steps.compile_fortran import CompileFortran # todo: we might have liked to reuse this from test_dep_tree from fab.util import CompiledFile @pytest.fixture def src_tree(): return { Path('src/foo.f90'): AnalysedFile(fpath=Path('src/foo.f90'), file_hash=None), Path('src/root.f90'): AnalysedFile( fpath=Path('src/root.f90'), file_deps={Path('src/a.f90'), Path('src/b.f90')}, file_hash=None), Path('src/a.f90'): AnalysedFile(fpath=Path('src/a.f90'), file_deps={Path('src/c.f90')}, file_hash=None), Path('src/b.f90'): AnalysedFile(fpath=Path('src/b.f90'), file_deps={Path('src/c.f90')}, file_hash=None), Path('src/c.f90'): AnalysedFile(fpath=Path('src/c.f90'), file_deps=set(), file_hash=None), } class Test_run(object): # todo: almost identical to the c compiler test def test_vanilla(self, src_tree): # ensure the compile passes match the build tree config = mock.Mock(workspace=Path('foo/src'), multiprocessing=False) c_compiler = CompileFortran( compiler='gcc', common_flags=['-c'], path_flags=[AddFlags(match='foo/src/*', flags=['-Dhello'])]) def foo(items, func): return [CompiledFile(af, output_fpath=None) for af in items] with mock.patch('fab.steps.Step.run_mp', side_effect=foo) as mock_run_mp: c_compiler.run(artefact_store={'build_tree': src_tree}, config=config) # 1st pass mock_run_mp.assert_any_call( items={src_tree[Path('src/foo.f90')], src_tree[Path('src/c.f90')]}, func=mock.ANY) # 2nd pass mock_run_mp.assert_any_call( items={src_tree[Path('src/a.f90')], src_tree[Path('src/b.f90')]}, func=mock.ANY) # last pass mock_run_mp.assert_called_with(items={src_tree[Path('src/root.f90')]}, func=mock.ANY)
2,025
736
""" 5. Faça um programa que receba do usuário um arquivo texto e um caracter. Mostre na tela quantas vezes aquele caractere ocorre dentro do arquivo. """ arquivo=open('CursoUdemyPython/exercicios/Lista6/arq.txt') texto=arquivo.read() carac=input('Informe um caractere: ') ca=0 for c in texto: if(c == carac): ca+=1 arquivo.close() print(f"Foi identificado {ca} deste caractere")
393
152
#--------------------------------------------------------- # Superset specific config #--------------------------------------------------------- ROW_LIMIT = 5000 SUPERSET_WEBSERVER_PORT = 8088 #--------------------------------------------------------- #--------------------------------------------------------- # Flask App Builder configuration #--------------------------------------------------------- # Your App secret key SECRET_KEY = '\2\1ulan123456\1\2\e\y\y\h' # The SQLAlchemy connection string to your database backend # This connection defines the path to the database that stores your # superset metadata (slices, connections, tables, dashboards, ...). # Note that the connection information to connect to the datasources # you want to explore are managed directly in the web UI #SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://superset:superset@localhost:5432/superset' # ------------------------------ # GLOBALS FOR APP Builder # ------------------------------ # Uncomment to setup Your App name APP_NAME = 'Insights' # Uncomment to setup an App icon APP_ICON = '/static/assets/images/qmatic_insights-logo.png' # Extract and use X-Forwarded-For/X-Forwarded-Proto headers? ENABLE_PROXY_FIX = True ENABLE_JAVASCRIPT_CONTROLS = True ''' import os from flask_appbuilder.security.manager import AUTH_OID, AUTH_REMOTE_USER, AUTH_DB, AUTH_LDAP, AUTH_OAUTH basedir = os.path.abspath(os.path.dirname(__file__)) SUPERSET_WORKERS = 8 CSRF_ENABLED = True AUTH_TYPE = AUTH_OAUTH AUTH_USER_REGISTRATION = False AUTH_USER_REGISTRATION_ROLE = "Gamma" #"Public" OAUTH_PROVIDERS = [ { 'name': 'google', 'icon': 'fa-google', 'token_key': 'access_token', 'remote_app': { 'base_url': 'https://www.googleapis.com/oauth2/v2/', 'request_token_params': { 'scope': 'email profile' }, 'request_token_url': None, 'access_token_url': 'https://accounts.google.com/o/oauth2/token', 'authorize_url': 'https://accounts.google.com/o/oauth2/auth', 'consumer_key': '996225546131-1qd2alfrrp1scf6gvkeg63mg2ku85lka.apps.googleusercontent.com', 'consumer_secret': '3fxwT-a8YA1akyuUYFfakMCz' } }, { 'name': 'slatest.qmaticcloud.com', 'icon': 'fa-google', 'token_key': 'access_token', 'remote_app': { #'base_url': 'https://slatest.qmaticcloud.com/oauth2server/oauth/', 'base_url': None, 'request_token_params': { 'scope': 'user_info', 'state': '123' }, 'request_token_url': None, 'access_token_url': 'https://slatest.qmaticcloud.com/oauth2server/oauth/token', 'authorize_url': 'https://slatest.qmaticcloud.com/oauth2server/oauth/authorize', 'consumer_key': 'businessintelligence', 'consumer_secret': 'fSmI0K1uSvnORBk3' } }, { 'name': 'msdemo.qmatic.cloud', 'icon': 'fa-google', 'token_key': 'access_token', 'remote_app': { 'base_url': None, 'request_token_params': { 'scope': 'user_info', 'state': '123' }, 'request_token_url': None, 'access_token_url': 'https://msdemo.qmatic.cloud/oauth2server/oauth/token', 'authorize_url': 'https://msdemo.qmatic.cloud/oauth2server/oauth/authorize', 'consumer_key': 'businessintelligence', 'consumer_secret': 'fSmI0K1uSvnORBk3' } } ] '''
3,704
1,191
import argparse import cv2 import re import numpy as np import string import PIL import os,glob import ntpath import time import matplotlib.pyplot as plt from PIL import Image from yad2k.models.keras_yolo import (preprocess_true_boxes, yolo_body, yolo_eval, yolo_head, yolo_loss) from yad2k.utils.draw_boxes import draw_boxes from retrain_yolo import (create_model,get_classes) import keras.backend as K from crnn.train_crnn import create_crnn_model from crnn.crnn_data_gen import * char_list = string.ascii_letters+string.digits YOLO_ANCHORS = np.array( ((0.57273, 0.677385), (1.87446, 2.06253), (3.33843, 5.47434), (7.88282, 3.52778), (9.77052, 9.16828))) class_names=['plate','no-plate'] class CarNumberDetector: def __init__(self,viden_yolo_weights_path,viden_crnn_weights_path,classes_path,out_path): self.act_model = create_crnn_model(train=False) self.act_model.load_weights(viden_crnn_weights_path)# 'viden_trained_models\\viden_crnn_14May2021.hdf5') class_names = get_classes(classes_path) #print(class_names) self.out_path=out_path #self.anchors = YOLO_ANCHORS self.yolo_model_body, self.yolo_model = create_model(YOLO_ANCHORS, class_names,load_pretrained=False,freeze_body=False) self.yolo_model_body.load_weights(viden_yolo_weights_path)#'viden_trained_models\\viden_yolo_14May2021.h5') self.yolo_outputs = yolo_head(self.yolo_model_body.output, YOLO_ANCHORS, len(class_names)) self.yolo_input_image_shape = K.placeholder(shape=(2, )) self.boxes, self.scores, self.classes = yolo_eval( self.yolo_outputs, self.yolo_input_image_shape,max_boxes=1, score_threshold=.7, iou_threshold=0.5) def extract_number(self,orig_image_url=None,image_array=None,save=False): """ This is the primary method to detect number plate on car and fetch the number. image_array is the numpy array representing original car image method returns numpy array of image with bounding box ad the extracted car_number string """ if( image_array is None and orig_image_url is None): raise ValueError("image array or url is required") if(orig_image_url is not None): image = PIL.Image.open(orig_image_url) else: image = PIL.Image.fromarray(image_array) pred_boxes,pred_box_classes,img_with_boxes = self.get_bounding_boxes(image) pred_txt='' for i, box in list(enumerate(pred_boxes)): box_class = class_names[pred_box_classes[i]] top, left, bottom, right = box pred_obj_img = image.crop((left,top,right,bottom)) pred_txt=self.get_text(pred_obj_img) # Save the image: if save: time_param= int(round(time.time() * 1000)) orig_img_name = self.out_path+pred_txt+"-"+str(time_param)+".jpg" orig_image = PIL.Image.fromarray(img_with_boxes) orig_image.save(orig_img_name) return (orig_image,pred_txt) def get_text(self,pil_image): img = pil_image.resize((128, 32), Image.BICUBIC) img = np.array(img) /255; img = np.sum(img, axis=2,keepdims=True) img = np.expand_dims(img , axis = 0) prediction = self.act_model.predict(img) # use CTC decoder out = K.get_value(K.ctc_decode(prediction, input_length=np.ones(prediction.shape[0])*prediction.shape[1],greedy=False)[0][0]) x = out[0] le= min(10,out.shape[1]) s='' for x in out: for p in range(0, le): if int(x[p]) != -1: s += char_list[int(x[p])] return s def get_bounding_boxes(self,image): image_shape = (416, 416) resized_image =image.resize(tuple(image_shape), PIL.Image.BICUBIC) image_data = np.array(resized_image, dtype='float32') image_data /= 255. image_data = np.expand_dims(image_data, 0) sess = K.get_session() out_boxes, out_scores, out_classes = sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo_model_body.input: image_data, self.yolo_input_image_shape: [image_data.shape[1], image_data.shape[2]], K.learning_phase(): 0 }) # Convert pred on 416 to actual image size resized_boxes = out_boxes/416 w,h = image.size box_resize_dim = [h,w,h,w] resized_boxes = resized_boxes * box_resize_dim orig_image_data = np.array(image, dtype='float32') orig_image_with_boxes = draw_boxes(orig_image_data, resized_boxes, out_classes, class_names, out_scores,"rand") return resized_boxes,out_classes,orig_image_with_boxes
5,038
1,781
""" =================================================================== Plot time-frequency representations on topographies for MEG sensors =================================================================== Both induced power and phase locking values are displayed. """ print(__doc__) # Authors: Alexandre Gramfort <gramfort@nmr.mgh.harvard.edu> # Denis Engemann <d.engemann@fz-juelich.de> # # License: BSD (3-clause) import numpy as np import matplotlib.pyplot as plt import mne from mne import fiff from mne.time_frequency import induced_power from mne.viz import plot_topo_power, plot_topo_phase_lock from mne.datasets import sample data_path = sample.data_path() raw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif' event_fname = data_path + '/MEG/sample/sample_audvis_raw-eve.fif' event_id, tmin, tmax = 1, -0.2, 0.5 # Setup for reading the raw data raw = fiff.Raw(raw_fname) events = mne.read_events(event_fname) include = [] raw.info['bads'] += ['MEG 2443', 'EEG 053'] # bads + 2 more # picks MEG gradiometers picks = fiff.pick_types(raw.info, meg='grad', eeg=False, eog=True, stim=False, include=include, exclude='bads') epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(grad=4000e-13, eog=150e-6)) data = epochs.get_data() # as 3D matrix layout = mne.find_layout(epochs.info, 'meg') ############################################################################### # Calculate power and phase locking value frequencies = np.arange(7, 30, 3) # define frequencies of interest n_cycles = frequencies / float(7) # different number of cycle per frequency Fs = raw.info['sfreq'] # sampling in Hz decim = 3 power, phase_lock = induced_power(data, Fs=Fs, frequencies=frequencies, n_cycles=n_cycles, n_jobs=1, use_fft=False, decim=decim, zero_mean=True) ############################################################################### # Prepare topography plots, set baseline correction parameters baseline = (None, 0) # set the baseline for induced power mode = 'ratio' # set mode for baseline rescaling ############################################################################### # Show topography of power. title = 'Induced power - MNE sample data' plot_topo_power(epochs, power, frequencies, layout, baseline=baseline, mode=mode, decim=decim, vmin=0., vmax=14, title=title) plt.show() ############################################################################### # Show topography of phase locking value (PLV) mode = None # no baseline rescaling for PLV title = 'Phase locking value - MNE sample data' plot_topo_phase_lock(epochs, phase_lock, frequencies, layout, baseline=baseline, mode=mode, decim=decim, title=title) plt.show()
2,881
929
import bluetooth import sys, os import subprocess as sp import datetime from pushBulletForBluetooth import pushNotification from makeReminderforBluetooth import Reminder import csv class blueDev: def findmyDevice(self): sendPushBullet = pushNotification() timestamp = datetime.datetime.now().strftime('%d/%m/%Y') nearby_devices = bluetooth.discover_devices(lookup_names = True) if nearby_devices is not None: print("Scanned device:") for addr, name in nearby_devices: devices = (addr.split("(")[-1]) print(devices) else: print("No device available") print() paired = sp.Popen(["bt-device", "--list"], stdin = sp.PIPE, stdout = sp.PIPE, close_fds = True) (stdout, stdin) = (paired.stdout, paired.stdin) list_of_paired_devices = stdout.readlines() list_of_paired_devices.pop(0) print("Matching devices...") for paired_device in list_of_paired_devices: pairedString = paired_device.decode() pairedSplit = pairedString.split("(")[-1] pairedDevice = pairedSplit[0:-2] for devices, name in nearby_devices: if pairedDevice == devices: print(devices, "=", pairedDevice) with open('bluetoothReminder.csv', 'r') as csvfile: readCSV = csv.reader(csvfile) for row in readCSV: if row[0] != timestamp: print("Device matched!") sendPushBullet.send() else: print("Device matched! Notification has already been sent today.") else: print(devices, "!=", pairedDevice) print("Device not matched...") def main(): bluetooth = blueDev() reminder = Reminder() reminder.makeReminder() bluetooth.findmyDevice() if __name__ == "__main__": main()
2,201
597
appendMe = '\nNew bit of information' appendFile = open('example.txt','a') appendFile.write(appendMe) appendFile.close()
127
43
from kafka import KafkaProducer TOPIC_NAME = 'items' KAFKA_SERVER = 'localhost:9092' producer = KafkaProducer(bootstrap_servers=KAFKA_SERVER) producer.send(TOPIC_NAME, b'Test Message!!!') producer.flush()
209
85
from datetime import datetime import myfitnesspal import sqlite_utils import click from . import utils @click.group() @click.version_option() def cli(): "Save data from MyFitnessPal to a SQLite database" @cli.command() @click.argument( "db_path", type=click.Path(file_okay=True, dir_okay=False, allow_dash=False), required=True, ) @click.argument( "user", type=str, required=True, ) @click.argument( "date", type=str, required=True, ) @click.option( "--measurement", multiple=True, required=True, ) def diary(db_path, user, date, measurement): "Save food, exercise, goal, and measurement entries for a given user and date" date = datetime.fromisoformat(date).date() db = sqlite_utils.Database(db_path) client = myfitnesspal.Client(user) diary_entry = utils.fetch_diary_entry(date, client, measurement) utils.save_diary_entry(db, diary_entry) utils.ensure_db_shape(db)
954
329
import numpy as np class T1Hist: def __init__( self, max_t1: float = 3000, min_t1: float = 200 ): self.max_t1 = max_t1 self.min_t1 = min_t1 def get_t1_histogram(self, t1_matrix: np.ndarray, norm_m0_matrix: np.ndarray): t1_histogram = t1_matrix.astype(float).ravel() t1_weights = norm_m0_matrix.astype(float).ravel() self.t1_histogram, self.t1_weights = self.remove_outliers( t1_histogram, t1_weights ) def remove_outliers(self, t1_histogram, t1_weights): t1_weights = t1_weights[ (t1_histogram > (self.min_t1)) & (t1_histogram < (self.max_t1)) ] t1_histogram = t1_histogram[ (t1_histogram > (self.min_t1)) & (t1_histogram < (self.max_t1)) ] return t1_histogram, t1_weights
849
352
import pluginpackageRENAME import preload import os if __name__ == "__main__": preload.preload() app = pluginpackageRENAME.app app.static_folder = os.path.join(os.getcwd(), "html") app.run()
209
76
import matplotlib.pyplot as plt import numpy as np import math import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit import json from scipy.interpolate import interp1d from data_utils import * def integral(y, x): area = 0 for xi, xj, yi, yj in zip(x[:-1], x[1:], y[:-1], y[1:]): area += (yi + yj) / 2 * abs(xj - xi) return area def preprocess(x): #x = [math.log10(_) for _ in x] x = [_/float(max(x)) for _ in x] return x def func(y, x, y_int): for y1, y2, x1, x2 in zip(y[:-1], y[1:], x[:-1], x[1:]): if y_int == y1: return x1 elif y_int == y2: return x2 elif y_int > y1 and y_int < y2: x_int = (y_int - y1) / (y2 - y1) * (x2 - x1) + x1 return x_int def draw_curve(): x = [1, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 40000] y_mean = [13.3, 29.6, 33.9, 43.8, 50.81, 67.7, 75.6, 81.5, 91.4, 95.6] plt.plot(x, y_mean, 'black') #plt.fill(x + x[::-1], y_mean + [95.6] * len(y_min), '#f4df42', alpha=.5, ec='None') plt.fill(x + x[::-1], [0] * len(y_mean) + y_mean[::-1], '#0099ff', alpha=.5, ec='None') plt.xlabel("Vocab") plt.xscale('log') plt.xlim(left=0) plt.ylim(ymin=0) plt.ylabel("Accuracy") #plt.show() plt.savefig("metrics.eps", dpi=1000, format='eps') #draw_curve() def draw_uncerntainy_curve(): x = [0, 100, 200, 500, 1000, 2000, 5000, 10000] y_max = [13.3, 51.2, 67.5, 80.4, 85.1, 87.5, 90.5, 91.4] y_mean = [13.3, 29.6, 33.9, 43.9, 50.81, 67.7, 81.5, 91.4] y_min = [13.3, 25.6, 27, 35.1, 42.4, 56, 74.1, 91.4] plt.plot(x, y_mean, 'black', label="Mean Accuracy Curve") plt.plot(x, y_min, 'black', label="Lower Accuracy Curve") plt.plot(x, y_max, 'black', label="Upper Accuracy Curve") #plt.plot(X, y, 'r.', markersize=10, label=u'Observations') #plt.plot(x, y_pred, 'b-', label=u'Prediction') plt.fill(x + x[::-1], y_min + y_max[::-1], '#0099ff', alpha=.5, ec='None', label='Accuracy Range') plt.legend(loc='lower right', prop={'size':14}) plt.xlim(left=0) plt.xlabel("Vocab") plt.ylabel("Accuracy") plt.savefig("accuracy_curve.eps", dpi=1000, format='eps') #plt.show() #plt.fill(np.concatenate([x, x[::-1]]), # np.concatenate([y_pred - 1.9600 * sigma, # (y_pred + 1.9600 * sigma)[::-1]]), # alpha=.5, fc='b', ec='None', label='95% confidence interval') #draw_uncerntainy_curve() def draw_SLU_uncerntainy_curve(): x = [0, 7, 27, 77, 100, 1778, 5134, 10000] x = [str(_) for _ in x] y_max = [13.3, 48.8, 81.3, 92.0, 94.0, 95.3, 95.8, 96.1] y_mean = [13.3, 33.4, 54.3, 77.4, 88.9, 93.5, 94.2, 96.1] y_min = [13.3, 14.2, 33.2, 46.8, 72.8, 88.4, 92.3, 96.1] plt.plot(x, y_mean, color='black', label="Mean Accuracy Curve") plt.plot(x, y_min, color='black', label="Lower Accuracy Curve") plt.plot(x, y_max, color='black', label="Upper Accuracy Curve") #plt.plot(X, y, 'r.', markersize=10, label=u'Observations') #plt.plot(x, y_pred, 'b-', label=u'Prediction') plt.fill(x + x[::-1], y_min + y_max[::-1], color='#0099ff', alpha=.5, ec='None', label='Accuracy Range') plt.xlim(left=0) plt.ylim(bottom=0) plt.legend(loc='lower right', prop={'size':14}) plt.xlabel("Vocab") plt.ylabel("Accuracy") plt.savefig("accuracy_curve.eps", dpi=1000, format='eps') #plt.show() #plt.fill(np.concatenate([x, x[::-1]]), # np.concatenate([y_pred - 1.9600 * sigma, # (y_pred + 1.9600 * sigma)[::-1]]), # alpha=.5, fc='b', ec='None', label='95% confidence interval') #draw_SLU_uncerntainy_curve() def read(string, use_str=False): string = string.strip() result = eval(string) if use_str: result = [str(_) for _ in result] else: result = [float(_) for _ in result] return result def draw_curve(): def savefig(f1, f2, f3, name): x, y = enhanced(read(f1[1]), read(f1[0])) plt.plot(x, y, 'y*', label="Frequency") x, y = enhanced(read(f2[1]), read(f2[0])) plt.plot(x, y, 'b--', label="TF-IDF") x, y = enhanced(read(f3[1]), read(f3[0])) plt.plot(x, y, 'r', label="Variational") #plt.title("{} dataset".format(name)) plt.xlabel('Vocab') plt.ylabel('Accuracy') plt.legend(loc='best') plt.xscale('log') #plt.xlim(left=0.001) #plt.show() plt.savefig("{}.eps".format(name), format="eps", dpi=1000) plt.clf() plt.rcParams.update({'font.size': 14}) file = 'results/ag_news' f1 = open(file + ".txt").readlines() f2 = open(file + "_tf_idf.txt").readlines() f3 = open(file + "_var.txt").readlines() savefig(f1, f2, f3, 'images/ag_news') file = 'results/dbpedia' f1 = open(file + ".txt").readlines() f2 = open(file + "_tf_idf.txt").readlines() f3 = open(file + "_var.txt").readlines() savefig(f1, f2, f3, 'images/dbpedia') file = 'results/yelp_review' f1 = open(file + ".txt").readlines() f2 = open(file + "_tf_idf.txt").readlines() f3 = open(file + "_var.txt").readlines() savefig(f1, f2, f3, 'images/yelp_review') #draw_curve() def compute_score(): from data_utils import * f = 'results/ag_news.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 61673) print CR(y, x) f = 'results/ag_news_tf_idf.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 61673) print CR(y, x) f = 'results/ag_news_var.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 61673) print CR(y, x) print() f = 'results/dbpedia.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 563355) print CR(y, x) f = 'results/dbpedia_tf_idf.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 563355) print CR(y, x) f = 'results/dbpedia_var.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 563355) print CR(y, x) print() f = 'results/yelp_review.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 252712) print CR(y, x) f = 'results/yelp_review_tf_idf.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 252712) print CR(y, x) f = 'results/yelp_review_var.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 252712) print CR(y, x) print() f = 'results/sogou_news.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 254495) print CR(y, x) f = 'results/sogou_news_tf_idf.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 254495) print CR(y, x) f = 'results/snli.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 42391) print CR(y, x) f = 'results/snli_var.txt' y = read(open(f).readlines()[0]) x = read(open(f).readlines()[1]) print ROC(y, x, 42391) print CR(y, x) compute_score()
6,948
3,598
#!/usr/bin/env python # -*- coding: utf-8 -*- import json from alipay.aop.api.constant.ParamConstants import * from alipay.aop.api.domain.AoiInfoDTO import AoiInfoDTO from alipay.aop.api.domain.ItemStoreDTO import ItemStoreDTO class CircleRecommendItemDTO(object): def __init__(self): self._aoi_info = None self._discount = None self._item_cover = None self._item_detail_url = None self._item_id = None self._item_label = None self._item_name = None self._item_store = None self._original_price = None self._sales_info = None self._saved_money = None self._saved_money_info = None self._sell_price = None self._sold_quantity = None self._store_id = None @property def aoi_info(self): return self._aoi_info @aoi_info.setter def aoi_info(self, value): if isinstance(value, AoiInfoDTO): self._aoi_info = value else: self._aoi_info = AoiInfoDTO.from_alipay_dict(value) @property def discount(self): return self._discount @discount.setter def discount(self, value): self._discount = value @property def item_cover(self): return self._item_cover @item_cover.setter def item_cover(self, value): self._item_cover = value @property def item_detail_url(self): return self._item_detail_url @item_detail_url.setter def item_detail_url(self, value): self._item_detail_url = value @property def item_id(self): return self._item_id @item_id.setter def item_id(self, value): self._item_id = value @property def item_label(self): return self._item_label @item_label.setter def item_label(self, value): self._item_label = value @property def item_name(self): return self._item_name @item_name.setter def item_name(self, value): self._item_name = value @property def item_store(self): return self._item_store @item_store.setter def item_store(self, value): if isinstance(value, ItemStoreDTO): self._item_store = value else: self._item_store = ItemStoreDTO.from_alipay_dict(value) @property def original_price(self): return self._original_price @original_price.setter def original_price(self, value): self._original_price = value @property def sales_info(self): return self._sales_info @sales_info.setter def sales_info(self, value): self._sales_info = value @property def saved_money(self): return self._saved_money @saved_money.setter def saved_money(self, value): self._saved_money = value @property def saved_money_info(self): return self._saved_money_info @saved_money_info.setter def saved_money_info(self, value): self._saved_money_info = value @property def sell_price(self): return self._sell_price @sell_price.setter def sell_price(self, value): self._sell_price = value @property def sold_quantity(self): return self._sold_quantity @sold_quantity.setter def sold_quantity(self, value): self._sold_quantity = value @property def store_id(self): return self._store_id @store_id.setter def store_id(self, value): self._store_id = value def to_alipay_dict(self): params = dict() if self.aoi_info: if hasattr(self.aoi_info, 'to_alipay_dict'): params['aoi_info'] = self.aoi_info.to_alipay_dict() else: params['aoi_info'] = self.aoi_info if self.discount: if hasattr(self.discount, 'to_alipay_dict'): params['discount'] = self.discount.to_alipay_dict() else: params['discount'] = self.discount if self.item_cover: if hasattr(self.item_cover, 'to_alipay_dict'): params['item_cover'] = self.item_cover.to_alipay_dict() else: params['item_cover'] = self.item_cover if self.item_detail_url: if hasattr(self.item_detail_url, 'to_alipay_dict'): params['item_detail_url'] = self.item_detail_url.to_alipay_dict() else: params['item_detail_url'] = self.item_detail_url if self.item_id: if hasattr(self.item_id, 'to_alipay_dict'): params['item_id'] = self.item_id.to_alipay_dict() else: params['item_id'] = self.item_id if self.item_label: if hasattr(self.item_label, 'to_alipay_dict'): params['item_label'] = self.item_label.to_alipay_dict() else: params['item_label'] = self.item_label if self.item_name: if hasattr(self.item_name, 'to_alipay_dict'): params['item_name'] = self.item_name.to_alipay_dict() else: params['item_name'] = self.item_name if self.item_store: if hasattr(self.item_store, 'to_alipay_dict'): params['item_store'] = self.item_store.to_alipay_dict() else: params['item_store'] = self.item_store if self.original_price: if hasattr(self.original_price, 'to_alipay_dict'): params['original_price'] = self.original_price.to_alipay_dict() else: params['original_price'] = self.original_price if self.sales_info: if hasattr(self.sales_info, 'to_alipay_dict'): params['sales_info'] = self.sales_info.to_alipay_dict() else: params['sales_info'] = self.sales_info if self.saved_money: if hasattr(self.saved_money, 'to_alipay_dict'): params['saved_money'] = self.saved_money.to_alipay_dict() else: params['saved_money'] = self.saved_money if self.saved_money_info: if hasattr(self.saved_money_info, 'to_alipay_dict'): params['saved_money_info'] = self.saved_money_info.to_alipay_dict() else: params['saved_money_info'] = self.saved_money_info if self.sell_price: if hasattr(self.sell_price, 'to_alipay_dict'): params['sell_price'] = self.sell_price.to_alipay_dict() else: params['sell_price'] = self.sell_price if self.sold_quantity: if hasattr(self.sold_quantity, 'to_alipay_dict'): params['sold_quantity'] = self.sold_quantity.to_alipay_dict() else: params['sold_quantity'] = self.sold_quantity if self.store_id: if hasattr(self.store_id, 'to_alipay_dict'): params['store_id'] = self.store_id.to_alipay_dict() else: params['store_id'] = self.store_id return params @staticmethod def from_alipay_dict(d): if not d: return None o = CircleRecommendItemDTO() if 'aoi_info' in d: o.aoi_info = d['aoi_info'] if 'discount' in d: o.discount = d['discount'] if 'item_cover' in d: o.item_cover = d['item_cover'] if 'item_detail_url' in d: o.item_detail_url = d['item_detail_url'] if 'item_id' in d: o.item_id = d['item_id'] if 'item_label' in d: o.item_label = d['item_label'] if 'item_name' in d: o.item_name = d['item_name'] if 'item_store' in d: o.item_store = d['item_store'] if 'original_price' in d: o.original_price = d['original_price'] if 'sales_info' in d: o.sales_info = d['sales_info'] if 'saved_money' in d: o.saved_money = d['saved_money'] if 'saved_money_info' in d: o.saved_money_info = d['saved_money_info'] if 'sell_price' in d: o.sell_price = d['sell_price'] if 'sold_quantity' in d: o.sold_quantity = d['sold_quantity'] if 'store_id' in d: o.store_id = d['store_id'] return o
8,392
2,737
import subprocess import time import logging import os import signal log = logging.getLogger(__name__) def runpreloadcommand(cmd,timeout,preload): newenv = dict(os.environ) newenv["LD_PRELOAD"] = preload process = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,env=newenv) processRc = None handleprocess = True counter = 0 stdout = '' stderr = '' while handleprocess: counter += 1 time.sleep(1) cout,cerr = process.communicate() stdout += cout stderr += cerr process.poll() processRc = process.returncode if processRc != None: break if counter == timeout: os.kill(process.pid, signal.SIGQUIT) if counter > timeout: os.kill(process.pid, signal.SIGKILL) processRc = -9 break return (processRc,stdout,stderr) def gsiDcapCopy(src,dest,timeout = 60): cmd = "dccp -C 3000 -d 2 -A %s %s" % (src,dest) process = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) processRc = None handleprocess = True counter = 0 stdout = '' stderr = '' while handleprocess: counter += 1 time.sleep(1) cout,cerr = process.communicate() stdout += cout stderr += cerr process.poll() processRc = process.returncode if processRc != None: break if counter == timeout: os.kill(process.pid, signal.SIGQUIT) if counter > timeout: os.kill(process.pid, signal.SIGKILL) processRc = -9 break if processRc != 0: log = logging.getLogger("gsiDcapCopy") log.error("failed to execute command '%s'" % (cmd)) return (processRc,stdout,stderr) class uploaderDcap: def __init__(self): self.remotePrefix = None self.log = logging.getLogger("uploaderGsiDcap") def _getfilepath(self,remotePath): if self.remotePrefix != None: return self.remotePrefix + remotePath else: return remotePath def exists(self,remotePath): cmd = "stat %s" % (self._getfilepath(remotePath)) timeout = 10 preload = "/usr/lib64/libpdcap.so.1" return runpreloadcommand(cmd,timeout,preload) def delete(self,remotePath): cmd = "unlink %s" % (self._getfilepath(remotePath)) timeout = 10 preload = "/usr/lib64/libpdcap.so.1" return runpreloadcommand(cmd,timeout,preload) def upload(self,localpath,remotePath): path = self._getfilepath(remotePath) return gsiDcapCopy(localpath,path) def replace(self,localpath,remotePath): path = self._getfilepath(remotePath) (rc,stdout,stderr) = self.exists(path) if rc == 0: (rc,stdout,stderr) = self.delete(path) if rc != 0: msg = "stderr={stderr}".format(stderr=stderr) log.error(msg) return (rc,stdout,stderr) rc,stdout,stderr = gsiDcapCopy(localpath,path) if rc != 0: msg = "stderr={stderr}".format(stderr=stderr) log.error(msg) return (rc,stdout,stderr) return (rc,stdout,stderr) def download(self,remotePath,localpath): rc,stdout,stderr = gsiDcapCopy(self._getfilepath(remotePath),localpath) if rc != 0: for errorLine in stderr.split('\n'): self.log.error("stderr:'%s'" % (errorLine)) return rc,stdout,stderr
3,599
1,149
# drones/serializers.py file from rest_framework import serializers from drones.models import DroneCategory, Drone, Pilot, Competition from django.contrib.auth.models import User import drones.views class UserDroneSerializer(serializers.HyperlinkedModelSerializer): """serialize the drones related to a User""" class Meta: model: Drone fields = ( 'url', 'name') class UserSerializer(serializers.HyperlinkedModelSerializer): """declare an instance of the UserDroneSerializer class""" drones = UserDroneSerializer( many=True, read_only=True) class Meta: model = User fields = ( 'url', 'pk', 'username', 'drone') class DroneCategorySerializer(serializers.HyperlinkedModelSerializer): """defines a one-to-many relationship that is read- only""" drones = serializers.HyperlinkedRelatedField( many=True, read_only=True, view_name='drone-detail', # browsable API feature ) class Meta: """model related to the serializer, and field names that we want to include in the serialization""" model = DroneCategory fields = ( 'url', 'pk', 'name', 'drones') class DroneSerializer(serializers.HyperlinkedModelSerializer): """display the drone category name""" drone_category = serializers.SlugRelatedField( queryset=DroneCategory.objects.all(), slug_field='name') # Display the owner's username (read-only) owner = serializers.ReadOnlyField(source='owner.username') class Meta: """model related to the serializer, and field names that we want to include in the serialization""" model = Drone fields = ( 'url', 'name', 'drone_category', 'owner', 'manufacturing_date', 'has_it_competed', 'inserted_timestamp', ) class CompetitionSerializer(serializers.HyperlinkedModelSerializer): """display all the details for the related Drone""" drone = DroneSerializer() class Meta: """model related to the serializer, and field names that we want to include in the serialization""" model = Competition fields = ( 'url', 'pk', 'distance_in_feet', 'distance_achievement_date', 'drone') class PilotSerializer(serializers.HyperlinkedModelSerializer): """serialize Pilot instances and serialize all the Competition instances related to the Pilot""" competitions = CompetitionSerializer(many=True, read_only=True) gender = serializers.ChoiceField(choices=Pilot.gender_choices) gender_description = serializers.CharField(source='get_gender_display', read_only=True) class Meta: """model related to the serializer, and field names that we want to include in the serialization""" model = Pilot fields = ( 'url', 'name', 'gender', 'gender_description', 'races_count', 'inserted_timestamp', 'competitions') class PilotCompetitionSerializer(serializers.ModelSerializer): """display the related Pilot name and the related Drone name""" pilot = serializers.SlugRelatedField( queryset=Pilot.objects.all(), slug_field='name') drone = serializers.SlugRelatedField( queryset=Drone.objects.all(), slug_field='name') class Meta: """model related to the serializer, and field names that we want to include in the serialization""" model = Competition fields = ( 'url', 'pk', 'distance_in_feet', 'distance_achievement_date', 'pilot', 'drone')
3,923
1,071
import learnables.py # création d'un objet Calamous class Calamous: #définition de son type et de si c'est un pokémon pokemon = True type_ = "Water" # définition de ses talents et de son genre (qui seront random) abilities = "Multicule" or "Glissade" or "Protéen" gender = "M" or "F" # création de ses niveaux levels = 1 exp = 0 needexp = 30 lootexp = 10 difupgexp = 3 # création de la possibilité de monter de niveau if adverse == 0: exp = exp + lootexp * difupgexp if levels == 1 and exp == needexp: levels = 2 needexp = 70 exp = 0 if adverse == 0: exp = exp + lootexp * difupgexp if exp == needexp: levels = levels + 1 needexp = needexp * 2 exp = 0 # définition de ses stats hp = 50 atq = 35 df = 40 spa = 60 spd = 30 spe = 60 pre = 1 esc = 1 # définition de l'apparence et des capacités qu'il pourra apprendre appearence = ('assets/personnages/Poké-avant/Calamous') # définition de valeurs plus cachées happiness = 0 # création de la possibilité de monter certaines d'entre elles if adverse == 0: happiness = happiness + 0.1 # description du pokédex desc = "Ce pokémon ne peut pas vivre hors de l'eau : sa peau sèche dès qu'elle est déshydratée trop longtemps"
1,471
552
import random import matplotlib.pyplot as plt import numpy as np from scipy import stats ############################ STATS input data ################################################ def return_nan_percentage(input_data): """ prints percentage of nan values in max. 3D sized array Parameters ---------- input_array : array max 3D array Returns ------- None """ total_size = input_data.size nan_sum = np.isnan(input_data).sum() perc = float(nan_sum / total_size) print("percentage of nan values inside dataset is: %.2f" % float(perc) + " %") # #4D example: # for i in Training_data: # return_nan_percentage(i) def describe_with_stats(input_data): flat_array = input_data.flatten() # 'omit'performs the calculations ignoring nan values nobs, minmax, mean, variance, skewness, kurtosis = stats.describe( flat_array, nan_policy="omit" ) print("Number of observations: " + str(nobs)) print("min: " + str(minmax[0])) print("max: " + str(minmax[1])) print("the mean is: " + str(mean)) print("the variance is: " + str(variance)) print("Skewness is: " + str(skewness)) print("Kurtosis: " + str(kurtosis)) print("---") # for i in Training_data_germany: # describe_with_stats(i) ############################ Derive Labels ############################################### def mask_nan_values(input_array): array_with_masked_nans = input_array.fillna(value=10000.00) return array_with_masked_nans # back to xarray with: # label_xarray = xr.DataArray(output_3D_array, dims=['time', 'latitude', 'longitude'] ) # to turn list output into a 3D array use: def list_to_array(output_list): output_3D_array = np.stack(output_list, axis=0) return output_3D_array # TODO: returns list of 2D arrays now, try to return 3D x array to save as net cdf -SEE BELOW # TODO: write test # #Example: # #create data subset of 10 of a data xarray # data_10 = data[0:10] #first 10 items to test # print(data.shape) # #call function with a threshod of 10 # output_array = binary_image_classification(data_10, T=0.5) # #show one image of the masked output images # plt.imshow(output_array[0], origin = 'lower') # #might need to change 'lower' to 'upper" # TODO: def list_of_2D_xarray_to_netcdf(): x = 0 netcdf = x return netcdf def save_plots_from_3Darray( input_array, OUTPUT_PATH, title="drought mask figure Nr:", show_plots=True ): """ saves pngs and/or prints images from 3Darrays as png files Parameters ---------- input_xarray : array 3-D input array in the format [num_samples, height, width] title: str title of the plots, number will be added according to iteration index show_plots: boolean determines if plots will be displayed as output or not Returns ------- None """ for k in range(len(input_array[0])): fig = input_array[k].plot() plt.title(title + str(k)) plt.axis("equal") plt.title("drought mask for SMI, month " + str(k)) if show_plots: plt.show() fig.figure.savefig(OUTPUT_PATH + title + str(k) + ".png", dpi=100) print(OUTPUT_PATH + "drought_mask_" + str(k) + ".png") ############################ class imbalance ###################################### # option 1, faster, combine these 2 fcts (recommended): def hide_random_values(input_value, T=0.68): if input_value == 0: if np.random.rand(1) > T: return -1 return input_value # print(hide_random_values(0)) def reduce_class_size(input_array): output_array = np.copy(input_array) for t in range(0, 472): for xy in range(0, 7171): output_array[t, xy] = hide_random_values(output_array[t, xy]) return output_array # option 2, combine these 2 fcts: def get_indices(dataset, value=0): """dataset = str(), 2D-array value = int(), value to print the indices for""" result = np.where(dataset == value) print("Tuple of arrays returned : ", result) # zip the 2 arrays (array 1: rows, array 2: columns) to get the exact coordinates listOfCoordinates = list(zip(result[0], result[1])) # iterate over the list of coordinates # for cord in listOfCoordinates: # print(cord) print(len(listOfCoordinates)) return listOfCoordinates def reduce_class_size(input_array, indices_list, T=0.78, value=int(-1)): """set entries in array to value=x, randomly and within set percentage of array list = list, list of indices (2D) T = int() , percentage to be modified returns: """ output_array = np.copy(input_array) # determine the percentage of the array that will be modified len_modifier = int(len(indices_list) * T) # select percentage T randomly from the list random_coords = random.sample(listOfCoordinates, len_modifier) # print(random_coords[:10]) # set selected entries to value print("selected indices will be set to " + str(value)) for i in random_coords: # print(labels_reshaped[i]) output_array[i] == value return output_array
5,186
1,734
def load(): with open("input") as f: for x in f: a, b, = x.strip().split("|") yield {frozenset(x) for x in a.strip().split()}, [frozenset(x) for x in b.strip().split()] def decode_signal(signal): num = {} while len(num) < 10: for x in signal.difference(num.values()): if len(x) == 2: num[1] = x elif len(x) == 3: num[7] = x elif len(x) == 4: num[4] = x elif len(x) == 7: num[8] = x elif len(x) == 6 and 4 in num and num[4].issubset(x): num[9] = x elif len(x) == 5 and 1 in num and num[1].issubset(x): num[3] = x elif len(x) == 6 and 7 in num and 9 in num and num[7].issubset(x) and num[9] != x: num[0] = x elif len(x) == 6 and 1 in num and not num[1].issubset(x): num[6] = x elif len(x) == 5 and 6 in num and x.issubset(num[6]): num[5] = x elif len(x) == 5 and 3 in num and 5 in num: num[2] = x return {v: k for k, v in num.items()} def decode_output(): result = 0 for sig, out in load(): mapping = decode_signal(sig) result += int("".join(str(mapping[x]) for x in out)) return result print(decode_output())
1,382
494
#!/usr/bin/env python # # This is the library for Grove Base Hat. # # Button Base Class # ''' ## License The MIT License (MIT) Grove Base Hat for the Raspberry Pi, used to connect grove sensors. Copyright (C) 2018 Seeed Technology Co.,Ltd. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ''' class Button(object): # event bits EV_RAW_STATUS = 1 << 0 EV_SINGLE_CLICK = 1 << 1 EV_DOUBLE_CLICK = 1 << 2 EV_LONG_PRESS = 1 << 3 EV_LEVEL_CHANGED = 1 << 4 # EV_HAS = 1 << 31 pins = [] def __init__(self, pin): self.__on_obj = None self.__on_event = None self.__event = 0 self.pins.append(pin) # To use with button array self.__index = self.pins.index(pin) def get_on_event(self): return self.__on_obj, self.__on_event def on_event(self, obj, callback): if not obj: return if not callable(callback): return self.__on_obj, self.__on_event = obj, callback def is_pressed(self): return False # call by derivate class def _send_event(self, event, pressed, tm): if not callable(self.__on_event): return evt = { 'index': self.__index, 'code' : event, 'pressed': pressed, 'time' : tm, } self.__on_event(self.__on_obj, evt)
2,469
863
# Reverse a linked list # Developer: Murillo Grubler # https://www.hackerrank.com/challenges/reverse-a-linked-list/problem # Time complexity of reverse function: O(n) class SinglyLinkedListNode: def __init__(self, node_data): self.data = node_data self.next = None class SinglyLinkedList: def __init__(self): self.head = None self.tail = None def insert_node(self, node_data): node = SinglyLinkedListNode(node_data) if not self.head: self.head = node else: self.tail.next = node self.tail = node # Complete the reverse function below. # # For your reference: # # SinglyLinkedListNode: # int data # SinglyLinkedListNode next # def reverse(head): ln = SinglyLinkedListNode(head.data) temp_node = head.next while temp_node: next_ln = ln ln = SinglyLinkedListNode(temp_node.data) ln.next = next_ln temp_node = temp_node.next return ln if __name__ == '__main__': tests = int(input()) for tests_itr in range(tests): llist_count = int(input()) llist = SinglyLinkedList() for _ in range(llist_count): llist_item = int(input()) llist.insert_node(llist_item) result = reverse(llist.head) while result: print (result.data, end=' ') result = result.next
1,402
452
import os from PIL import Image import random from functools import wraps from flask import jsonify from flask_jwt_extended import get_current_user from .artpiece import Artpiece from .exceptions import InvalidUsage from web.extensions import cache #decorator to require admin_acccess for a route def access_level_required(level): try: def outer(func): @wraps(func) def inner(*args, **kwargs): if get_current_user().role < level: raise InvalidUsage.forbidden() return func(*args, **kwargs) return inner except TypeError: raise TypeError("Specify an access level to use access_level_required decorator") return outer @cache.memoize(timeout=3600) def get_image_description(image_path): with Image.open(image_path) as image: # Exif ID 270 = ImageDescription return image.getexif().get(270) """ Return a list of images in the 'gallery' folder and their descriptions Output is list of tuples (image_location, image_description) output list is in random order for random display order every time """ def get_gallery_images(): internal_path_prefix = './web' public_gallery_path = '/static/img/gallery/' image_paths = [ public_gallery_path + filename for filename in os.listdir(internal_path_prefix + public_gallery_path) ] image_descriptions = list() for image_path in image_paths: this_image_description = get_image_description(internal_path_prefix + image_path) image_descriptions.append(this_image_description) image_metadata = list(zip(image_paths, image_descriptions)) random.shuffle(image_metadata) return image_metadata
1,737
496
import spconv import torch from torch import nn def residualBlock(channels, kernel_size=3): return spconv.SparseSequential( spconv.ConcatTable() .add(spconv.Identity()) .add(spconv.SparseSequential( nn.BatchNorm1d(channels), spconv.SubMConv3d(channels, channels, kernel_size=kernel_size), nn.Sigmoid() )), spconv.JoinTable() ) def subMVonvBlock(inChannels, outChannels, kernel_size=3, indiceKey=None): return spconv.SparseSequential( nn.BatchNorm1d(inChannels), spconv.SubMConv3d(inChannels, outChannels, kernel_size=kernel_size, indice_key=indiceKey), nn.Sigmoid() ) def convBlock(inChannels, outChannels, kernel_size=3): return nn.Sequential( nn.BatchNorm3d(inChannels), nn.Conv3d(inChannels, outChannels, kernel_size=kernel_size, stride=1, padding=1), nn.LeakyReLU() ) def kaiming_init(m): classname = m.__class__.__name__ if classname.find('Conv') != -1: torch.nn.init.kaiming_uniform_(m.weight) def toSparseInput(inputTSDF): # Construct Sparse Tensor inputTSDF = inputTSDF.permute(0,2,3,4,1) sparseMask = torch.any(torch.abs(inputTSDF) < 1, dim=4) batchSize = len(inputTSDF) spatialShape = inputTSDF.shape[1:-1] sparseIndice = sparseMask.to_sparse(inputTSDF.ndim-1).indices().permute(1, 0).contiguous().int() sparseValue = inputTSDF[sparseMask] inputData_sparse = spconv.SparseConvTensor(features=sparseValue, indices=sparseIndice, spatial_shape=spatialShape, batch_size=batchSize) return inputData_sparse def sparseFuse(inputSparseTSDF, oldSparseTSDF, inputMask, oldMask): # fuseTSDF = torch.cat((self.toDense(inputSparseTSDF), self.toDense(oldSparseTSDF)), dim=1) oldTSDF = spconv.ToDense(oldSparseTSDF).permute(0,2,3,4,1) inputTSDF = spconv.ToDense(inputSparseTSDF).permute(0,2,3,4,1) # oldTSDF[inputMask] = (oldTSDF[inputMask] * oldWeight[inputMask] + inputTSDF[inputMask] * inputWeight[inputMask]) / (oldWeight[inputMask] + inputWeight[inputMask]) batchSize = inputSparseTSDF.batch_size spatialShape = inputSparseTSDF.spatial_shape fuseMask = torch.logical_or(inputMask, oldMask) sparseIndice = fuseMask.to_sparse(oldTSDF.ndim-1).indices().permute(1, 0).contiguous().int() sparseValue = oldTSDF[fuseMask] return spconv.SparseConvTensor(features=sparseValue, indices=sparseIndice, spatial_shape=spatialShape, batch_size=batchSize)
2,528
944
#!/usr/bin/env python # # Copyright (c) 2013 In-Q-Tel, Inc/Lab41, 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 fnmatch, os, sys, time, uuid from pymongo import MongoClient def mongo_server(server, port, database, collection): # connect to the redis server try: m_server = MongoClient(server, port) m_database = m_server[database] m_collection = m_database[collection] except: print "Mongo server failure" sys.exit(0) return m_server, m_database, m_collection def process_doc(input, m_server, m_database, m_collection): matches = [] docs = [] for root, dirnames, filenames in os.walk(input): for filename in fnmatch.filter(filenames, '*.txt'): matches.append(os.path.join(root, filename)) j = 0 k = 0 for file in matches: if len(docs) % 100 == 0 and len(docs) > 0: m_collection.insert(docs) print str(j), "total docs." print str(k), "docs failed." docs = [] doc = open(file, 'r').read() try: doc = unicode(doc, "utf-8") doc = {"doc": doc} docs.append(doc) j += 1 except: k += 1 if len(docs) > 0: m_collection.insert(docs) print str(j), "total docs." print str(k), "docs failed." def print_help(): print "-i \t<input path to files> (default is /mnt/)" print "-s \t<mongo server> (default is localhost)" print "-p \t<mongo port> (default is 27017)" print "-d \t<mongo database> (default is local)" print "-c \t<mongo collection> (default is collection)" print "-h \thelp\n" sys.exit(0) def process_args(args): # default initialization input = "/mnt/" server = "localhost" port = 27017 database = "local" collection = "collection" # process args i = 0 while i < len(args): if args[i] == "-s": try: server = args[i+1] i += 1 except: print_help() elif args[i] == "-p": try: port = int(args[i+1]) i += 1 except: print_help() elif args[i] == "-d": try: database = args[i+1] i += 1 except: print_help() elif args[i] == "-c": try: collection = args[i+1] i += 1 except: print_help() elif args[i] == "-i": try: input = args[i+1] i += 1 except: print_help() else: print_help() i += 1 return input, server, port, database, collection def get_args(): args = [] for arg in sys.argv: args.append(arg) return args[1:] if __name__ == "__main__": start_time = time.time() args = get_args() input, server, port, database, collection = process_args(args) m_server, m_database, m_collection = mongo_server(server, port, database, collection) process_doc(input, m_server, m_database, m_collection) print "Took",time.time() - start_time,"seconds to complete."
3,794
1,177
import copy import logging import math import torch from torch import nn logger = logging.getLogger('global') def init_weights_normal(module, std=0.01): for m in module.modules(): if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear) or isinstance( m, nn.ConvTranspose2d): nn.init.normal_(m.weight.data, std=std) if m.bias is not None: m.bias.data.zero_() def init_weights_xavier(module): for m in module.modules(): if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear) or isinstance( m, nn.ConvTranspose2d): nn.init.xavier_normal_(m.weight.data) if m.bias is not None: m.bias.data.zero_() def init_weights_msra(module): for m in module.modules(): if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear) or isinstance( m, nn.ConvTranspose2d): nn.init.kaiming_normal_(m.weight.data) if m.bias is not None: m.bias.data.zero_() def init_bias_focal(module, cls_loss_type, init_prior, num_classes): if cls_loss_type == 'sigmoid': for m in module.modules(): if isinstance(m, nn.Conv2d): # to keep the torch random state m.bias.data.normal_(-math.log(1.0 / init_prior - 1.0), init_prior) torch.nn.init.constant_(m.bias, -math.log(1.0 / init_prior - 1.0)) elif cls_loss_type == 'softmax': for m in module.modules(): if isinstance(m, nn.Conv2d): m.bias.data.normal_(0, 0.01) for i in range(0, m.bias.data.shape[0], num_classes): fg = m.bias.data[i + 1:i + 1 + num_classes - 1] mu = torch.exp(fg).sum() m.bias.data[i] = math.log(mu * (1.0 - init_prior) / init_prior) else: raise NotImplementedError(f'{cls_loss_type} is not supported') def initialize(model, method, **kwargs): # initialize BN for m in model.modules(): if isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() # initialize Conv & FC if method == 'normal': init_weights_normal(model, **kwargs) elif method == 'msra': init_weights_msra(model) elif method == 'xavier': init_weights_xavier(model) else: raise NotImplementedError(f'{method} not supported') def initialize_from_cfg(model, cfg): if cfg is None: initialize(model, 'normal', std=0.01) return cfg = copy.deepcopy(cfg) method = cfg.pop('method') initialize(model, method, **cfg)
2,659
945
# This Python file uses the following encoding: utf-8 import os; import sys; import urllib.request; from bs4 import BeautifulSoup; import wikipedia from PyQt5.QtWebEngineWidgets import *; from PyQt5.QtGui import QIcon; from PyQt5.QtWidgets import *; from PyQt5.QtNetwork import QNetworkProxy class AudioFile(QMainWindow): def __init__(self): QMainWindow.__init__(self) proxy = QNetworkProxy() proxy.setType(1); proxy.setHostName("127.0.0.1"); proxy.setPort(9050); proxy.setApplicationProxy(proxy) toolb = QToolBar("URL"); self.lineEdit = QLineEdit(self); self.lineEdit2 = QLineEdit(self); combobox2 = QComboBox(self); self.textEdit = QTextEdit(self) def track_search(): try: soup = BeautifulSoup(urllib.request.urlopen(wikipedia.page(str(self.lineEdit.text()+" "+self.lineEdit2.text())).url).read()) except: soup = BeautifulSoup(urllib.request.urlopen(wikipedia.page(str(self.lineEdit.text()+" "+self.lineEdit2.text()+" album")).url).read()) for link in soup.find_all("td"): w = link.get_text().strip() if w[:1] == '"': self.textEdit.append(w.replace('"', "")) else: pass self.lineEdit2.clear() def save(): bandName = self.lineEdit.text(); script = self.textEdit.toPlainText() with open(os.getcwd()+"/.Pyrate/Set_List/"+bandName+".txt", "w", encoding = 'utf-8') as file: file.write(script) file.close() self.textEdit.clear(); self.lineEdit.clear() def text_changed(): nav = combobox2.currentText() if nav == "Save": save() if nav == "Album Search": track_search() combobox2.setCurrentText("") toolb.setOrientation(0x2); self.addToolBar(toolb); self.lineEdit.setObjectName(u"Artist Name"); self.lineEdit.setPlaceholderText("Artist Name"); toolb.addWidget(self.lineEdit); self.lineEdit2.setObjectName(u"Album Name"); self.lineEdit2.setPlaceholderText("Album Name"); toolb.addWidget(self.lineEdit2); combobox2.addItems(["", "Album Search", "Save"]); combobox2.currentTextChanged.connect(lambda: text_changed()); toolb.addWidget(combobox2); self.textEdit.setObjectName(u"Track List"); self.textEdit.setPlaceholderText("Track List"); self.textEdit.setAcceptRichText(False); self.setCentralWidget(self.textEdit) self.setWindowIcon(QIcon(os.getcwd()+'/.Pyrate/.images/pp.png')); self.setWindowTitle("Shanties"); self.setStyleSheet("color: #fe2023;" "background-color: #000000;" "selection-color: #ffffff;" "selection-background-color: #e01b24;"); self.lineEdit.setStyleSheet("background-color: #ffffff;" "selection-color: #000000;");self.lineEdit2.setStyleSheet("background-color: #ffffff;" "selection-color: #000000;"); self.textEdit.setStyleSheet("background-color: #ffffff;" "selection-color: #000000;") if __name__ == "__main__": app = QApplication([]) AudioFile().show() sys.exit(app.exec_())
3,000
972
__author__ = 'petriau' import requests # for HTTP requests from bs4 import BeautifulSoup # for HTML parsing url_SBM_FinanceProgress = 'http://sbm.gov.in/sbmreport/Report/Financial/SBM_StateReleaseAllocationincludingUnapproved.aspx' # Function to return HTML parsed with BeautifulSoup from a POST request URL and parameters. def parsePOSTResponse(URL, parameters=''): responseHTMLParsed = '' r = requests.post(URL, data=parameters) if r.status_code == 200: responseHTML = r.content responseHTMLParsed = BeautifulSoup(responseHTML, 'html.parser') return responseHTMLParsed listTest = [['__EVENTARGUMENT',''],['__EVENTTARGET', 'ctl00$ContentPlaceHolder1$rptr_state$ctl03$lnkbtn_stName'],['__EVENTVALIDATION',"/wEWTAL2js/IBwLq6fiEBwK4qJKGBgL7uLfDBQLMho26CAKkvMv0BAKrp/OzCAKzzOWcCQLfxNm+CQLZ25fbDALc9b7CDALYxrzSBgLK5tedAQLJrc6KBwLD2Nb1DwKAz9S2BQLD2JrzAgKAz/jyBAKUlKjOAgKvk9PyBQKUlOzLBQKvk5ewAQKNhuujBAK2ocCIDQKNhq+hBwK2oaT3BwLW5PDOBwLdiLPJCQLW5LTMCgLdiPeGBQLPqct8AqCWu5oDAs+pj/oDAqCW/9cOArTAhacLArv/sNsBArTAyaQOArv/1JcBApnX36oFAtbopuwKApnXo6gIAtbo6qkGAv7tmdUPAvHRnK0JAv7t3dICAvHRgJwEAtuMv40FAoTehsMOAtuMg4sIAoTeyoAKAtTR8eIFAquz5dgPAtTRhc0LAquz+e4CAs3DtJABAoKu9fINAs3DyPoGAoKuibEHAp7/hZEKAuGFkd0CAp7/mfsPAuGFpfMFApfxyL4FAriAoXcCl/HcqAsCuIC1tQoC4M/OkQ8Cv46O1Q0C4M/i+wQCv46iawLZlNH1CQKqnLrXDQLZlJXzDAKqnM5tAr6ri/gNAsWF0MkLUJ4OhBgatkYSQhamBAvcsSVIgC8="], ['__VIEWSTATE',"/wEPDwUKMTQwNTE3ODMyMg9kFgJmD2QWAgIDD2QWBAIfDw8WBB4EVGV4dAVMPHNwYW4gY2xhc3M9ImdseXBoaWNvbiBnbHlwaGljb24tY2lyY2xlLWFycm93LWxlZnQiPjwvc3Bhbj4gQmFjayB0byBQcmV2aW91cx4HVmlzaWJsZWcWAh4Hb25jbGljawUoamF2YXNjcmlwdDpoaXN0b3J5LmJhY2soKTsgcmV0dXJuIGZhbHNlO2QCIQ9kFgICAQ9kFggCAw8PFgIfAAUIKENlbnRlcilkZAIFDw8WAh8ABQsoMjAxNi0yMDE3KWRkAgcPFgIfAWgWBAIBDxYCHwFoFgQCAw8QZGQWAWZkAgcPEA8WBh4NRGF0YVRleHRGaWVsZAUHRmluWWVhch4ORGF0YVZhbHVlRmllbGQFB0ZpblllYXIeC18hRGF0YUJvdW5kZ2QQFQIKLS1TZWxlY3QtLQkyMDE2LTIwMTcVAgItMgkyMDE2LTIwMTcUKwMCZ2cWAQIBZAIDD2QWAgIBDw8WBB8ABRRSZWNvcmQgTm90IEZvdW5kICEhIR8BaGRkAgkPFgIeC18hSXRlbUNvdW50AiAWQgIBD2QWBmYPFQEBMWQCAQ8PFgIfAAUMQSAmIE4gSWxhbmRzZGQCBA8VCgYzNDAuMDAEMC4wMAQwLjAwBjM0MC4wMAQwLjAwBTEzLjU5BTEzLjU5BDQuMDAGMzQwLjAwBjMyNi40MWQCAg9kFgZmDxUBATJkAgEPDxYCHwAFDkFuZGhyYSBQcmFkZXNoZGQCBA8VCgc4NTk2LjY5BzY3NzIuODUEMS4xNAgxNTM3MC42OAc1NjQ5LjkzBzMzNDMuNjEHODk5My41NAU1OC41MQc5NzIwLjc1BzYzNzcuMTRkAgMPZBYGZg8VAQEzZAIBDw8WAh8ABRFBcnVuYWNoYWwgUHJhZGVzaGRkAgQPFQoHMTQ2NS44OAY5NjguNTEEMC4wMAcyNDM0LjM5BDAuMDAGMTA4LjAzBjEwOC4wMwQ0LjQ0BzI0MzQuMzkHMjMyNi4zNWQCBA9kFgZmDxUBATRkAgEPDxYCHwAFBUFzc2FtZGQCBA8VCggxNjExMC43OAQwLjAwBDAuMDAIMTYxMTAuNzgGNjg2LjE5BjkxNi4yNwcxNjAyLjQ2BDkuOTUIMTU0MjQuNjAIMTQ1MDguMzJkAgUPZBYGZg8VAQE1ZAIBDw8WAh8ABQVCaWhhcmRkAgQPFQoHNDIwMC4zNQgxMzE4Ni4zNwQwLjAwCDE3Mzg2LjcyBjY4Ni45OAcxMjI2LjgwBzE5MTMuNzgFMTEuMDEIMTY2OTkuNzQIMTU0NzIuOTRkAgYPZBYGZg8VAQE2ZAIBDw8WAh8ABQxDaGhhdHRpc2dhcmhkZAIEDxUKCC01OTYyLjUxBzk5NDcuNTcEMC4wMAczOTg1LjA2BjU3MS4xNgcxODY5LjE5BzI0NDAuMzQFNjEuMjQHMzQxMy45MQcxNTQ0LjcyZAIHD2QWBmYPFQEBN2QCAQ8PFgIfAAUMRCAmIE4gSGF2ZWxpZGQCBA8VCgQxLjQ4BDAuMDAEMC4wMAQxLjQ4BDAuMDAEMC4wMAQwLjAwBDAuMDAEMS40OAQxLjQ4ZAIID2QWBmYPFQEBOGQCAQ8PFgIfAAUDR29hZGQCBA8VCgctMzMzLjk1BDAuMDAEMC4wMActMzMzLjk1BDAuMDAHMjA5NC40OAcyMDk0LjQ4BDAuMDAHLTMzMy45NQgtMjQyOC40M2QCCQ9kFgZmDxUBATlkAgEPDxYCHwAFB0d1amFyYXRkZAIEDxUKCC00Njg4LjA0CDI4MDQ5LjI2BDAuMjMIMjMzNjEuNDUHMjAwNS4zNgc0MTc5LjAzBzYxODQuMzkFMjYuNDcIMjEzNTYuMDgIMTcxNzcuMDZkAgoPZBYGZg8VAQIxMGQCAQ8PFgIfAAUHSGFyeWFuYWRkAgQPFQoGNzc0LjQ5BzY4NzkuMDcEMi4zNQc3NjU1LjkyBjIwOC40MgU5MS42MQYzMDAuMDMEMy45Mgc3NDQ3LjUwBzczNTUuODlkAgsPZBYGZg8VAQIxMWQCAQ8PFgIfAAUQSGltYWNoYWwgUHJhZGVzaGRkAgQPFQoHNTI4My4yOAQwLjAwBTI0LjAzBzUzMDcuMzEGMzEzLjY0BjY2Ni41NgY5ODAuMjAFMTguNDcHNDk5My42Nwc0MzI3LjExZAIMD2QWBmYPFQECMTJkAgEPDxYCHwAFD0phbW11ICYgS2FzaG1pcmRkAgQPFQoHNTM5OS4zNwYyMjkuOTAEMC4wMAc1NjI5LjI3BjEwMS43MQU1MS44NQYxNTMuNTYEMi43Mwc1NTI3LjU1BzU0NzUuNzBkAg0PZBYGZg8VAQIxM2QCAQ8PFgIfAAUJSmhhcmtoYW5kZGQCBA8VCgktMTIyNzYuNjMIMTAzNTguOTYENC4xMggtMTkxMy41NQcxMzkwLjc2BzIzNTcuMjIHMzc0Ny45OAQwLjAwCC0zMzA0LjMxCC01NjYxLjUyZAIOD2QWBmYPFQECMTRkAgEPDxYCHwAFCUthcm5hdGFrYWRkAgQPFQoILTUwNDAuNjQIMTI2NzEuNTAEMC4wMAc3NjMwLjg2Bjk0OS40MwczMzA1LjYyBzQyNTUuMDUFNTUuNzYHNjY4MS40MwczMzc1LjgxZAIPD2QWBmYPFQECMTVkAgEPDxYCHwAFBktlcmFsYWRkAgQPFQoHMjg5MC45MgQwLjAwBDIuODIHMjg5My43NAYxMDcuNjkENS4xMQYxMTIuODAEMy45MAcyNzg2LjA1BzI3ODAuOTRkAhAPZBYGZg8VAQIxNmQCAQ8PFgIfAAUOTWFkaHlhIFByYWRlc2hkZAIEDxUKCS0xNTYzMy43NAgzNDIyMy41MwUyNS4wMAgxODYxNC43OQc5MzYwLjU0BzM0NzIuOTUIMTI4MzMuNDkFNjguOTQHOTI1NC4yNAc1NzgxLjI5ZAIRD2QWBmYPFQECMTdkAgEPDxYCHwAFC01haGFyYXNodHJhZGQCBA8VCggtNDMzMy4xNwgyNjQ0Ny4wOQQwLjAwCDIyMTEzLjkyBjMyNy42OAczNDg5LjAxBzM4MTYuNjkFMTcuMjYIMjE3ODYuMjMIMTgyOTcuMjNkAhIPZBYGZg8VAQIxOGQCAQ8PFgIfAAUHTWFuaXB1cmRkAgQPFQoHLTQ2Ni4yOQcyNzI3LjUwBDAuMDAHMjI2MS4yMQQwLjAwBjE1NS42MwYxNTUuNjMENi44OAcyMjYxLjIxBzIxMDUuNThkAhMPZBYGZg8VAQIxOWQCAQ8PFgIfAAUJTWVnaGFsYXlhZGQCBA8VCgcxNzI3LjY3BzQxMjIuMjQEMC4wMAc1ODQ5LjkxBjIyOS42MAYxMDguMjUGMzM3Ljg1BDUuNzgHNTYyMC4zMQc1NTEyLjA2ZAIUD2QWBmYPFQECMjBkAgEPDxYCHwAFB01pem9yYW1kZAIEDxUKBjM2NC4zMwQwLjAwBDAuMDAGMzY0LjMzBTk1LjExBTczLjgyBjE2OC45MwU0Ni4zNwYyNjkuMjMGMTk1LjQwZAIVD2QWBmYPFQECMjFkAgEPDxYCHwAFCE5hZ2FsYW5kZGQCBA8VCgYzMDIuMDMEMC4wMAQ5Ljg3BjMxMS45MAYxNzMuNDMEMi4yNwYxNzUuNzAFNTYuMzMGMTM4LjQ3BjEzNi4yMGQCFg9kFgZmDxUBAjIyZAIBDw8WAh8ABQZPZGlzaGFkZAIEDxUKCS01MDYzMS40Nwg0NTg1Ni42MQQwLjAwCC00Nzc0Ljg2Bzk1MTAuMzgHMjI4MC4zNQgxMTc5MC43MwQwLjAwCS0xNDI4NS4yNAktMTY1NjUuNTlkAhcPZBYGZg8VAQIyM2QCAQ8PFgIfAAUKUHVkdWNoZXJyeWRkAgQPFQoGNjYzLjEyBDAuMDAEMC4wMAY2NjMuMTIEMC4wMAQwLjAwBDAuMDAEMC4wMAY2NjMuMTIGNjYzLjEyZAIYD2QWBmYPFQECMjRkAgEPDxYCHwAFBlB1bmphYmRkAgQPFQoILTE2NTUuMjkHMjQ4Mi44NQQwLjAwBjgyNy41NgYxNTQuOTIGNTE4LjkwBjY3My44MwU4MS40MgY2NzIuNjMGMTUzLjczZAIZD2QWBmYPFQECMjVkAgEPDxYCHwAFCVJhamFzdGhhbmRkAgQPFQoJLTMwNTk3LjUwCDYyNzMwLjA1BDAuMDAIMzIxMzIuNTUHNjQxNC45Mwc1ODA4LjUyCDEyMjIzLjQ1BTM4LjA0CDI1NzE3LjYyCDE5OTA5LjEwZAIaD2QWBmYPFQECMjZkAgEPDxYCHwAFBlNpa2tpbWRkAgQPFQoGNTE1LjM5BjQ4MC45NgQwLjAwBjk5Ni4zNQQwLjAwBDAuMDAEMC4wMAQwLjAwBjk5Ni4zNQY5OTYuMzVkAhsPZBYGZg8VAQIyN2QCAQ8PFgIfAAUKVGFtaWwgTmFkdWRkAgQPFQoJLTI0MTEwLjAxCDI2ODUwLjk0BDAuNjIHMjc0MS41NgY0NzguMTEGMTU3Ljg5BjYzNi4wMQUyMy4yMAcyMjYzLjQ0BzIxMDUuNTVkAhwPZBYGZg8VAQIyOGQCAQ8PFgIfAAUJVGVsYW5nYW5hZGQCBA8VCgc1ODE0LjI1BDAuMDAEMC4wMAc1ODE0LjI1BjY1Ni43OAc0NjgwLjI0BzUzMzcuMDIFOTEuNzkHNTE1Ny40NwY0NzcuMjNkAh0PZBYGZg8VAQIyOWQCAQ8PFgIfAAUHVHJpcHVyYWRkAgQPFQoHMzYwNy40OAQwLjAwBDAuMDAHMzYwNy40OAU0MC4yMgYxMjguOTEGMTY5LjEzBDQuNjkHMzU2Ny4yNgczNDM4LjM2ZAIeD2QWBmYPFQECMzBkAgEPDxYCHwAFDVV0dGFyIFByYWRlc2hkZAIEDxUKCDI2OTIyLjIyCDE3ODY3LjQ0BDYuNjYINDQ3OTYuMzIHMzg1Ni44MAczNTE4LjMwBzczNzUuMTAFMTYuNDYINDA5MzkuNTIIMzc0MjEuMjFkAh8PZBYGZg8VAQIzMWQCAQ8PFgIfAAULVXR0YXJha2hhbmRkZAIEDxUKCC0xNjU4LjI3Bzg1MjkuMTMEMC4wMAc2ODcwLjg2BzEyMjAuMDkGNjQwLjc5BzE4NjAuODgFMjcuMDgHNTY1MC43Nwc1MDA5Ljk3ZAIgD2QWBmYPFQECMzJkAgEPDxYCHwAFC1dlc3QgQmVuZ2FsZGQCBA8VCgktMTYyMTkuNjcIMzI4NzUuNjAEMC4wMAgxNjY1NS45MwcxNzI3LjgxBzY2NDYuMTkHODM3NC4wMAU1MC4yOAgxNDkyOC4xMgc4MjgxLjkzZAIhD2QWAgICDxUKCS04ODYyNy40NQkzNTQyNTcuOTMFNzYuODQJMjY1NzA3LjM0CDQ2OTE3LjY3CDUxOTEwLjk5CDk4ODI4LjY3BTM3LjE5CTIxODc4OS42NAkxNjY4NzguNjRkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYDBQ9jdGwwMCRpY29uX3dvcmQFEGN0bDAwJGljb25fZXhjZWwFEmN0bDAwJGljb25fcHJpbnRlcqLkin/PLgDvwcsQ6/a18eF5HbFe"] ] paramDictionary = {key: str(value) for key, value in listTest} def merge_two_dicts(x, y): '''Given two dicts, merge them into a new dict as a shallow copy.''' z = x.copy() z.update(y) return z postParams = { # '__EVENTARGUMENT': '', # '__EVENTTARGET': 'ctl00$ContentPlaceHolder1$rptr_state$ctl03$lnkbtn_stName', # '__EVENTVALIDATION': "/wEWTAL2js/IBwLq6fiEBwK4qJKGBgL7uLfDBQLMho26CAKkvMv0BAKrp/OzCAKzzOWcCQLfxNm+CQLZ25fbDALc9b7CDALYxrzSBgLK5tedAQLJrc6KBwLD2Nb1DwKAz9S2BQLD2JrzAgKAz/jyBAKUlKjOAgKvk9PyBQKUlOzLBQKvk5ewAQKNhuujBAK2ocCIDQKNhq+hBwK2oaT3BwLW5PDOBwLdiLPJCQLW5LTMCgLdiPeGBQLPqct8AqCWu5oDAs+pj/oDAqCW/9cOArTAhacLArv/sNsBArTAyaQOArv/1JcBApnX36oFAtbopuwKApnXo6gIAtbo6qkGAv7tmdUPAvHRnK0JAv7t3dICAvHRgJwEAtuMv40FAoTehsMOAtuMg4sIAoTeyoAKAtTR8eIFAquz5dgPAtTRhc0LAquz+e4CAs3DtJABAoKu9fINAs3DyPoGAoKuibEHAp7/hZEKAuGFkd0CAp7/mfsPAuGFpfMFApfxyL4FAriAoXcCl/HcqAsCuIC1tQoC4M/OkQ8Cv46O1Q0C4M/i+wQCv46iawLZlNH1CQKqnLrXDQLZlJXzDAKqnM5tAr6ri/gNAsWF0MkLUJ4OhBgatkYSQhamBAvcsSVIgC8=", # '__VIEWSTATE': "/wEPDwUKMTQwNTE3ODMyMg9kFgJmD2QWAgIDD2QWBAIfDw8WBB4EVGV4dAVMPHNwYW4gY2xhc3M9ImdseXBoaWNvbiBnbHlwaGljb24tY2lyY2xlLWFycm93LWxlZnQiPjwvc3Bhbj4gQmFjayB0byBQcmV2aW91cx4HVmlzaWJsZWcWAh4Hb25jbGljawUoamF2YXNjcmlwdDpoaXN0b3J5LmJhY2soKTsgcmV0dXJuIGZhbHNlO2QCIQ9kFgICAQ9kFggCAw8PFgIfAAUIKENlbnRlcilkZAIFDw8WAh8ABQsoMjAxNi0yMDE3KWRkAgcPFgIfAWgWBAIBDxYCHwFoFgQCAw8QZGQWAWZkAgcPEA8WBh4NRGF0YVRleHRGaWVsZAUHRmluWWVhch4ORGF0YVZhbHVlRmllbGQFB0ZpblllYXIeC18hRGF0YUJvdW5kZ2QQFQIKLS1TZWxlY3QtLQkyMDE2LTIwMTcVAgItMgkyMDE2LTIwMTcUKwMCZ2cWAQIBZAIDD2QWAgIBDw8WBB8ABRRSZWNvcmQgTm90IEZvdW5kICEhIR8BaGRkAgkPFgIeC18hSXRlbUNvdW50AiAWQgIBD2QWBmYPFQEBMWQCAQ8PFgIfAAUMQSAmIE4gSWxhbmRzZGQCBA8VCgYzNDAuMDAEMC4wMAQwLjAwBjM0MC4wMAQwLjAwBTEzLjU5BTEzLjU5BDQuMDAGMzQwLjAwBjMyNi40MWQCAg9kFgZmDxUBATJkAgEPDxYCHwAFDkFuZGhyYSBQcmFkZXNoZGQCBA8VCgc4NTk2LjY5BzY3NzIuODUEMS4xNAgxNTM3MC42OAc1NjQ5LjkzBzMzNDMuNjEHODk5My41NAU1OC41MQc5NzIwLjc1BzYzNzcuMTRkAgMPZBYGZg8VAQEzZAIBDw8WAh8ABRFBcnVuYWNoYWwgUHJhZGVzaGRkAgQPFQoHMTQ2NS44OAY5NjguNTEEMC4wMAcyNDM0LjM5BDAuMDAGMTA4LjAzBjEwOC4wMwQ0LjQ0BzI0MzQuMzkHMjMyNi4zNWQCBA9kFgZmDxUBATRkAgEPDxYCHwAFBUFzc2FtZGQCBA8VCggxNjExMC43OAQwLjAwBDAuMDAIMTYxMTAuNzgGNjg2LjE5BjkxNi4yNwcxNjAyLjQ2BDkuOTUIMTU0MjQuNjAIMTQ1MDguMzJkAgUPZBYGZg8VAQE1ZAIBDw8WAh8ABQVCaWhhcmRkAgQPFQoHNDIwMC4zNQgxMzE4Ni4zNwQwLjAwCDE3Mzg2LjcyBjY4Ni45OAcxMjI2LjgwBzE5MTMuNzgFMTEuMDEIMTY2OTkuNzQIMTU0NzIuOTRkAgYPZBYGZg8VAQE2ZAIBDw8WAh8ABQxDaGhhdHRpc2dhcmhkZAIEDxUKCC01OTYyLjUxBzk5NDcuNTcEMC4wMAczOTg1LjA2BjU3MS4xNgcxODY5LjE5BzI0NDAuMzQFNjEuMjQHMzQxMy45MQcxNTQ0LjcyZAIHD2QWBmYPFQEBN2QCAQ8PFgIfAAUMRCAmIE4gSGF2ZWxpZGQCBA8VCgQxLjQ4BDAuMDAEMC4wMAQxLjQ4BDAuMDAEMC4wMAQwLjAwBDAuMDAEMS40OAQxLjQ4ZAIID2QWBmYPFQEBOGQCAQ8PFgIfAAUDR29hZGQCBA8VCgctMzMzLjk1BDAuMDAEMC4wMActMzMzLjk1BDAuMDAHMjA5NC40OAcyMDk0LjQ4BDAuMDAHLTMzMy45NQgtMjQyOC40M2QCCQ9kFgZmDxUBATlkAgEPDxYCHwAFB0d1amFyYXRkZAIEDxUKCC00Njg4LjA0CDI4MDQ5LjI2BDAuMjMIMjMzNjEuNDUHMjAwNS4zNgc0MTc5LjAzBzYxODQuMzkFMjYuNDcIMjEzNTYuMDgIMTcxNzcuMDZkAgoPZBYGZg8VAQIxMGQCAQ8PFgIfAAUHSGFyeWFuYWRkAgQPFQoGNzc0LjQ5BzY4NzkuMDcEMi4zNQc3NjU1LjkyBjIwOC40MgU5MS42MQYzMDAuMDMEMy45Mgc3NDQ3LjUwBzczNTUuODlkAgsPZBYGZg8VAQIxMWQCAQ8PFgIfAAUQSGltYWNoYWwgUHJhZGVzaGRkAgQPFQoHNTI4My4yOAQwLjAwBTI0LjAzBzUzMDcuMzEGMzEzLjY0BjY2Ni41NgY5ODAuMjAFMTguNDcHNDk5My42Nwc0MzI3LjExZAIMD2QWBmYPFQECMTJkAgEPDxYCHwAFD0phbW11ICYgS2FzaG1pcmRkAgQPFQoHNTM5OS4zNwYyMjkuOTAEMC4wMAc1NjI5LjI3BjEwMS43MQU1MS44NQYxNTMuNTYEMi43Mwc1NTI3LjU1BzU0NzUuNzBkAg0PZBYGZg8VAQIxM2QCAQ8PFgIfAAUJSmhhcmtoYW5kZGQCBA8VCgktMTIyNzYuNjMIMTAzNTguOTYENC4xMggtMTkxMy41NQcxMzkwLjc2BzIzNTcuMjIHMzc0Ny45OAQwLjAwCC0zMzA0LjMxCC01NjYxLjUyZAIOD2QWBmYPFQECMTRkAgEPDxYCHwAFCUthcm5hdGFrYWRkAgQPFQoILTUwNDAuNjQIMTI2NzEuNTAEMC4wMAc3NjMwLjg2Bjk0OS40MwczMzA1LjYyBzQyNTUuMDUFNTUuNzYHNjY4MS40MwczMzc1LjgxZAIPD2QWBmYPFQECMTVkAgEPDxYCHwAFBktlcmFsYWRkAgQPFQoHMjg5MC45MgQwLjAwBDIuODIHMjg5My43NAYxMDcuNjkENS4xMQYxMTIuODAEMy45MAcyNzg2LjA1BzI3ODAuOTRkAhAPZBYGZg8VAQIxNmQCAQ8PFgIfAAUOTWFkaHlhIFByYWRlc2hkZAIEDxUKCS0xNTYzMy43NAgzNDIyMy41MwUyNS4wMAgxODYxNC43OQc5MzYwLjU0BzM0NzIuOTUIMTI4MzMuNDkFNjguOTQHOTI1NC4yNAc1NzgxLjI5ZAIRD2QWBmYPFQECMTdkAgEPDxYCHwAFC01haGFyYXNodHJhZGQCBA8VCggtNDMzMy4xNwgyNjQ0Ny4wOQQwLjAwCDIyMTEzLjkyBjMyNy42OAczNDg5LjAxBzM4MTYuNjkFMTcuMjYIMjE3ODYuMjMIMTgyOTcuMjNkAhIPZBYGZg8VAQIxOGQCAQ8PFgIfAAUHTWFuaXB1cmRkAgQPFQoHLTQ2Ni4yOQcyNzI3LjUwBDAuMDAHMjI2MS4yMQQwLjAwBjE1NS42MwYxNTUuNjMENi44OAcyMjYxLjIxBzIxMDUuNThkAhMPZBYGZg8VAQIxOWQCAQ8PFgIfAAUJTWVnaGFsYXlhZGQCBA8VCgcxNzI3LjY3BzQxMjIuMjQEMC4wMAc1ODQ5LjkxBjIyOS42MAYxMDguMjUGMzM3Ljg1BDUuNzgHNTYyMC4zMQc1NTEyLjA2ZAIUD2QWBmYPFQECMjBkAgEPDxYCHwAFB01pem9yYW1kZAIEDxUKBjM2NC4zMwQwLjAwBDAuMDAGMzY0LjMzBTk1LjExBTczLjgyBjE2OC45MwU0Ni4zNwYyNjkuMjMGMTk1LjQwZAIVD2QWBmYPFQECMjFkAgEPDxYCHwAFCE5hZ2FsYW5kZGQCBA8VCgYzMDIuMDMEMC4wMAQ5Ljg3BjMxMS45MAYxNzMuNDMEMi4yNwYxNzUuNzAFNTYuMzMGMTM4LjQ3BjEzNi4yMGQCFg9kFgZmDxUBAjIyZAIBDw8WAh8ABQZPZGlzaGFkZAIEDxUKCS01MDYzMS40Nwg0NTg1Ni42MQQwLjAwCC00Nzc0Ljg2Bzk1MTAuMzgHMjI4MC4zNQgxMTc5MC43MwQwLjAwCS0xNDI4NS4yNAktMTY1NjUuNTlkAhcPZBYGZg8VAQIyM2QCAQ8PFgIfAAUKUHVkdWNoZXJyeWRkAgQPFQoGNjYzLjEyBDAuMDAEMC4wMAY2NjMuMTIEMC4wMAQwLjAwBDAuMDAEMC4wMAY2NjMuMTIGNjYzLjEyZAIYD2QWBmYPFQECMjRkAgEPDxYCHwAFBlB1bmphYmRkAgQPFQoILTE2NTUuMjkHMjQ4Mi44NQQwLjAwBjgyNy41NgYxNTQuOTIGNTE4LjkwBjY3My44MwU4MS40MgY2NzIuNjMGMTUzLjczZAIZD2QWBmYPFQECMjVkAgEPDxYCHwAFCVJhamFzdGhhbmRkAgQPFQoJLTMwNTk3LjUwCDYyNzMwLjA1BDAuMDAIMzIxMzIuNTUHNjQxNC45Mwc1ODA4LjUyCDEyMjIzLjQ1BTM4LjA0CDI1NzE3LjYyCDE5OTA5LjEwZAIaD2QWBmYPFQECMjZkAgEPDxYCHwAFBlNpa2tpbWRkAgQPFQoGNTE1LjM5BjQ4MC45NgQwLjAwBjk5Ni4zNQQwLjAwBDAuMDAEMC4wMAQwLjAwBjk5Ni4zNQY5OTYuMzVkAhsPZBYGZg8VAQIyN2QCAQ8PFgIfAAUKVGFtaWwgTmFkdWRkAgQPFQoJLTI0MTEwLjAxCDI2ODUwLjk0BDAuNjIHMjc0MS41NgY0NzguMTEGMTU3Ljg5BjYzNi4wMQUyMy4yMAcyMjYzLjQ0BzIxMDUuNTVkAhwPZBYGZg8VAQIyOGQCAQ8PFgIfAAUJVGVsYW5nYW5hZGQCBA8VCgc1ODE0LjI1BDAuMDAEMC4wMAc1ODE0LjI1BjY1Ni43OAc0NjgwLjI0BzUzMzcuMDIFOTEuNzkHNTE1Ny40NwY0NzcuMjNkAh0PZBYGZg8VAQIyOWQCAQ8PFgIfAAUHVHJpcHVyYWRkAgQPFQoHMzYwNy40OAQwLjAwBDAuMDAHMzYwNy40OAU0MC4yMgYxMjguOTEGMTY5LjEzBDQuNjkHMzU2Ny4yNgczNDM4LjM2ZAIeD2QWBmYPFQECMzBkAgEPDxYCHwAFDVV0dGFyIFByYWRlc2hkZAIEDxUKCDI2OTIyLjIyCDE3ODY3LjQ0BDYuNjYINDQ3OTYuMzIHMzg1Ni44MAczNTE4LjMwBzczNzUuMTAFMTYuNDYINDA5MzkuNTIIMzc0MjEuMjFkAh8PZBYGZg8VAQIzMWQCAQ8PFgIfAAULVXR0YXJha2hhbmRkZAIEDxUKCC0xNjU4LjI3Bzg1MjkuMTMEMC4wMAc2ODcwLjg2BzEyMjAuMDkGNjQwLjc5BzE4NjAuODgFMjcuMDgHNTY1MC43Nwc1MDA5Ljk3ZAIgD2QWBmYPFQECMzJkAgEPDxYCHwAFC1dlc3QgQmVuZ2FsZGQCBA8VCgktMTYyMTkuNjcIMzI4NzUuNjAEMC4wMAgxNjY1NS45MwcxNzI3LjgxBzY2NDYuMTkHODM3NC4wMAU1MC4yOAgxNDkyOC4xMgc4MjgxLjkzZAIhD2QWAgICDxUKCS04ODYyNy40NQkzNTQyNTcuOTMFNzYuODQJMjY1NzA3LjM0CDQ2OTE3LjY3CDUxOTEwLjk5CDk4ODI4LjY3BTM3LjE5CTIxODc4OS42NAkxNjY4NzguNjRkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYDBQ9jdGwwMCRpY29uX3dvcmQFEGN0bDAwJGljb25fZXhjZWwFEmN0bDAwJGljb25fcHJpbnRlcqLkin/PLgDvwcsQ6/a18eF5HbFe", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl01$hfd_StateId':"26", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl02$hfd_StateId':"1", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl03$hfd_StateId':"2", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl04$hfd_StateId':"3", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl05$hfd_StateId':"4", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl06$hfd_StateId':"34", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl07$hfd_StateId':"28", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl08$hfd_StateId':"5", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl09$hfd_StateId':"6", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl10$hfd_StateId':"7", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl11$hfd_StateId':"8", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl12$hfd_StateId':"9", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl13$hfd_StateId':"35", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl14$hfd_StateId':"10", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl15$hfd_StateId':"11", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl16$hfd_StateId':"12", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl17$hfd_StateId':"13", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl18$hfd_StateId':"14", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl19$hfd_StateId':"15", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl20$hfd_StateId':"16", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl21$hfd_StateId':"17", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl22$hfd_StateId':"18", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl23$hfd_StateId':"32", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl24$hfd_StateId':"19", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl25$hfd_StateId':"20", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl26$hfd_StateId':"21", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl27$hfd_StateId':"22", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl28$hfd_StateId':"36", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl29$hfd_StateId':"23", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl30$hfd_StateId':"24", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl31$hfd_StateId':"33", 'ctl00$ContentPlaceHolder1$rptr_cen$ctl32$hfd_StateId':"25" } p = merge_two_dicts(postParams, paramDictionary) componentPage = parsePOSTResponse(url_SBM_FinanceProgress, p) print(componentPage) x = 'what'
16,649
11,608
def listprime(max): prime=[True]*max prime[0]=False prime[1]=False for x in range(max/2): if prime[x]: bei=x+x while bei<max: prime[bei]=False bei+=x return prime def listprimenumber(lpn): listprimelpn=listprime(lpn) l=[] for x in range(len(listprimelpn)): if listprimelpn[x]: l.append(x) return l b=listprimenumber(100) print b count=3 for x in range(len(b)-count): sum=0 for y in range(count): sum+=b[x+y] if sum in b: print sum #if b[x+0]+b[x+1]+b[x+2] in b: # print b[x],b[x+1],b[x+2]
677
279
__author__ = 'ferrard' # --------------------------------------------------------------- # Imports # --------------------------------------------------------------- import scipy as sp import random import time # --------------------------------------------------------------- # Class - Graph # --------------------------------------------------------------- class WalkableGraph: """Graph on which we can do random walking""" # --------------------------------------------------------------- # Initialisation # --------------------------------------------------------------- def __init__(self, file_path): """ Loads a graph from file The file should have format: CityName i_1 i_2 ... i_k .... where i_j are indices of neighbouring cities (index given by index of the row) """ self._graph = [] self._cities = [] with open(file_path, 'r') as f: for line in f: city = line.split(' ')[0] neighs = [int(s) for s in line.split(' ')[1:]] self._cities.append(city) self._graph.append(neighs) self.n = len(self._cities) self._transition_matrix = self.__get_transition_matrix() # --------------------------------------------------------------- # Interface # --------------------------------------------------------------- def print(self): """ Prints the neighbourhood table of the graph """ for i in range(self.n): print(str(i) + " " + self._cities[i] + " " + str(len(self._graph[i])) + " " + str(self._graph[i])) def probs_after_k_steps(self, k): """ Prints the probability (for each city) we end up in the city after k steps """ probs = (1/self.n)*sp.ones(self.n) for i in range(k): probs = sp.dot(self._transition_matrix, probs) print("Probabilities: ") for i in range(self.n): print("\t" + self._cities[i] + ": " + str(probs[i])) return probs def random_walk(self, start_city, steps=10, time_in_city=0): """ Does a random walk through the graph, starting at given city, making "steps" random steps and waiting in each city for time_in_city seconds """ # find the index of the start-city current_city_index = None for i in range(len(self._cities)): if self._cities[i] == start_city: current_city_index = i if current_city_index is None: raise Exception("Unknown city " + start_city) # do the random walking print("Random walk with " + str(steps) + " steps. Started in " + self._cities[current_city_index]) visits = [0]*self.n for i in range(steps): visits[current_city_index] += 1 current_city_index = random.choice(self._graph[current_city_index]) print("Moved to " + self._cities[current_city_index]) time.sleep(time_in_city) visits[current_city_index] += 1 # print the statistics print("Finished random walk in: " + self._cities[current_city_index]) print("Visits of cities: ") for i in range(self.n): print("\t%s: %s (%s)" % (self._cities[i], visits[i], visits[i]/steps)) # --------------------------------------------------------------- # Implementation # --------------------------------------------------------------- def __get_transition_matrix(self): """ Gets the transition matrix of the graph """ transition_matrix = sp.zeros((self.n, self.n)) for j in range(self.n): for i in self._graph[j]: transition_matrix[i][j] = 1/len(self._graph[j]) return transition_matrix # --------------------------------------------------------------- # Main # --------------------------------------------------------------- def main(): random.seed() g = WalkableGraph('ghana.txt') g.print() print() print("Let's do some walking") k = 1000 g.random_walk("CapeCoast", k, 0) # g.probs_after_k_steps(k) if __name__ == '__main__': main()
4,189
1,166
from flask import Flask, render_template, request app = Flask(__name__) @app.route('/') def home(): return render_template('index.html') @app.route('/login', methods=['POST']) def login(): return f'<h2>Name: {request.form["name"]}</h2><br><h2>Password: {request.form["password"]}' if __name__ == '__main__': app.run()
337
122
""" Django settings for spartify project. Generated by 'django-admin startproject' using Django 2.2.12. For more information on this file, see https://docs.djangoproject.com/en/2.2/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.2/ref/settings/ """ import os from dotenv import load_dotenv load_dotenv() # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.environ.get("SECRET_KEY") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = int(os.environ.get("DEBUG", default=1)) ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS",'').split(" ") + ['*', '192.168.43.72', '192.168.0.53', '0.0.0.0'] AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', 'social_core.backends.spotify.SpotifyOAuth2', ) AUTH_USER_MODEL = 'backend.User' SOCIAL_AUTH_USER_MODEL = 'backend.User' # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'channels', 'social_django', 'backend', 'lobby' ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', # 'backend.middlewares.ApiMiddleware', ] ROOT_URLCONF = 'spartify.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'spartify.wsgi.application' ASGI_APPLICATION = 'spartify.routing.application' # Database # https://docs.djangoproject.com/en/2.2/ref/settings/#databases DATABASES = { 'default': { "ENGINE": os.environ.get("SQL_ENGINE", "django.db.backends.sqlite3"), "NAME": os.environ.get("SQL_DATABASE", os.path.join(BASE_DIR, "db.sqlite3")), "USER": os.environ.get("SQL_USER", "user"), "PASSWORD": os.environ.get("SQL_PASSWORD", "password"), "HOST": os.environ.get("SQL_HOST", "localhost"), "PORT": os.environ.get("SQL_PORT", "5432"), } } # Password validation # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/2.2/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.2/howto/static-files/ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_URL = 'staticfiles/' STATIC_ROOT = os.path.join(BASE_DIR, STATIC_URL) SOCIAL_AUTH_SPOTIFY_KEY = os.environ['SOCIAL_AUTH_SPOTIFY_KEY'] SOCIAL_AUTH_SPOTIFY_SECRET = os.environ['SOCIAL_AUTH_SPOTIFY_SECRET'] SOCIAL_AUTH_URL_NAMESPACE = 'social' SOCIAL_AUTH_SPOTIFY_SCOPE = ['user-read-email','user-read-private', 'user-read-playback-state', 'user-modify-playback-state'] # SOCIAL_AUTH_LOGIN_REDIRECT_URL = 'http://{}/complete/spotify/' % os.getenv('HOST') LOGIN_REDIRECT_URL = 'dashboard' LOGIN_URL = 'login' DEFAULT_AUTO_FIELD='django.db.models.AutoField' SOCIAL_AUTH_PIPELINE = ( 'social_core.pipeline.social_auth.social_details', 'social_core.pipeline.social_auth.social_uid', 'social_core.pipeline.social_auth.auth_allowed', 'social_core.pipeline.social_auth.social_user', 'social_core.pipeline.user.get_username', 'social_core.pipeline.user.create_user', 'social_core.pipeline.social_auth.associate_user', 'social_core.pipeline.social_auth.load_extra_data', 'social_core.pipeline.user.user_details', 'backend.pipeline.save_access_token', #save token on login, ) QUEUE_SESSION_ID = 'queue' SESSION_EXPIRE_AT_BROWSER_CLOSE = 15 LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'api_formatter': { 'format': '{username} -- {endpoint} -- {status_code:d}: {message}', 'style': '{', }, 'lobby_formatter': { 'format': '{id}--{username}: {message} -- {asctime}', 'style': '{', }, }, 'handlers': { 'api_errors': { 'class': 'logging.FileHandler', 'filename': 'logs/api_errors.log', 'formatter': 'api_formatter', 'level': 'ERROR', }, }, 'loggers':{ 'backend': { 'handlers': ['api_errors'], }, }, } REDIS_HOST = os.environ.get("REDIS_HOST", '127.0.0.1') REDIS_PORT = 6379 REDIS_DB = 0 CHANNEL_LAYERS = { 'default': { 'BACKEND': 'channels_redis.core.RedisChannelLayer', "CONFIG": { "hosts": [(REDIS_HOST, REDIS_PORT)], }, } }
6,094
2,201
#!/usr/bin/env python # # tdwggeo2csv.py # # Description: convert TDWG plant distribution files out of the box to a single # CSV file # # TODO: should create new id's for each entry and have a tdwg_code for # each so we can maintain as much data as possbible # TODO: we should probably include the original text files in bauble # and run the conversion script on build # TODO: add a notes column to geography so we carry over the extra # geography data(kew regions, notes, etc.) and so that we can add # notes to them in bauble import codecs import os import re from optparse import OptionParser # l1 - Continent, tblLevel1.txt, UTF-8 # l2 - Region, tblLevel2.txt, UTF-8 # l3 - BotanicalCountry, tblLevel4, ISO-8859-15 # l4 - BaseUnit, tblLevel4.txt, ISO-8859-15 # gazette (places), tblGazette.txt, ISO-8859-15 parser = OptionParser() parser.add_option('-d', '--directory', dest='directory', help='directory of WGS txt files', metavar='DIR') (options, args) = parser.parse_args() if not options.directory: parser.error('directory required') cwd, _dummy = os.path.split(__file__) src_dir = options.directory class Reader(object): def __init__(self, filename, encoding='utf8'): self.file = codecs.open(filename, "r", encoding) self.headers = self.file.next().strip().split('*') s = "" # sanitize the column headers for h in self.headers: h2 = h.replace(' ', '_') s += '(?P<%s>.*?)\*' % h2 s = s[:-2] + '$' self.line_rx = re.compile(s) def group(self, line): m = self.line_rx.match(line.strip()) if m is None: raise ValueError("could not match:\n%s\n%s" % (unicode(line), (unicode(s)))) return m.groupdict() def __iter__(self): return self def next(self): line = self.file.next() # remove the stupid ,00 decimals at the end of the integers #line = self.file.next().replace(',00','') return self.group(line) # converted rows organized by tdwg_code so we can resolve parents converted_rows = {} id_ctr = 1 class Row(dict): def __init__(self, id=None, name=None, tdwg_code=None, iso_code=None, parent_id=None): super(Row, self).__init__(id=id, name=name, tdwg_code=tdwg_code, iso_code=iso_code, parent_id=parent_id) columns = ['id', 'name', 'tdwg_code', 'iso_code', 'parent_id'] def __getattr__(self, item): if item in self: return self[item] else: return getattr(self, item) def __setattr__(self, key, value): self[key] = value def csv(self): s = [] for c in self.columns: if self[c] is None: #s.append('None') s.append('') elif c is 'id' or c is 'parent_id': s.append(self[c]) else: s.append('"%s"' % self[c].encode('utf8')) # s.append(quote(self[c])) return ','.join(s) def convert_level1(): global converted_data, id_ctr reader = Reader(os.path.join(src_dir, 'tblLevel1.txt'), 'utf8') for line in reader: r = Row(id=str(id_ctr), name=line['L1_continent'], tdwg_code=line['L1_code']) converted_rows[line['L1_code']] = r print(r.csv()) id_ctr += 1 def convert_level2(): global converted_data, id_ctr reader = Reader(os.path.join(src_dir, 'tblLevel2.txt'), 'utf8') for line in reader: r = Row(id=str(id_ctr), name=line['L2_region'], tdwg_code=line['L2_code'], iso_code=line['L2_ISOcode']) r.parent_id = converted_rows[line['L1_code']]['id'] converted_rows[line['L2_code']] = r print(r.csv()) id_ctr += 1 def convert_level3(): global converted_data, id_ctr reader = Reader(os.path.join(src_dir, 'tblLevel3.txt'), 'iso-8859-15') for line in reader: r = Row(id=str(id_ctr), name=line['L3_area'], tdwg_code=line['L3_code'], iso_code=line['L3_ISOcode']) #r.parent_id = converted_rows[line['L2_code']]['id'] r['parent_id'] = converted_rows[line['L2_code']]['id'] converted_rows[line['L3_code']] = r print(r.csv()) id_ctr += 1 def convert_level4(): global converted_data, id_ctr reader = Reader(os.path.join(src_dir, 'tblLevel4.txt'), 'iso-8859-15') for line in reader: # skip redundant lines from level 3 if line['L4_code'].endswith('-OO'): continue r = Row(id=str(id_ctr), name=line['L4_country'], tdwg_code=line['L4_code'], iso_code=line['L4_ISOcode']) r.parent_id = converted_rows[line['L3_code']]['id'] converted_rows[line['L4_code']] = r print(r.csv()) id_ctr += 1 def convert_gazetteer(): global converted_data, id_ctr reader = Reader(os.path.join(src_dir, 'tblGazetteer.txt'), 'iso-8859-15') for line in reader: # try to only include those things that are unique to the gazeteer if line['L3_code'] in converted_rows and \ converted_rows[line['L3_code']]['name'] == line['Gazetteer']: continue elif line['L4_code'] in converted_rows and \ converted_rows[line['L4_code']]['name'] == line['Gazetteer']: continue # TODO: create two rows, one for the gazetteer data and one for the # kew data r = Row(id=str(id_ctr), name=line['Gazetteer'], tdwg_code=line['ID']) # throw out anything that doesn't have a name, there seems # to be at least one row that doesn't have a name and is really just # a place holder for a kew region if line['Synonym'] != '': #print '%s == %s' % (line['Gazetteer'].encode('utf8'), line['Synonym'].encode('utf8')) pass if r.name == '' or line['Synonym'] != '': continue try: r.parent_id = converted_rows[line['L4_code']]['id'] except KeyError as e: try: r.parent_id = converted_rows[line['L3_code']]['id'] except KeyError as e: try: r.parent_id = converted_rows[line['L2_code']]['id'] except KeyError as e: try: r.parent_id = converted_rows[line['L1_code']]['id'] except KeyError as e: pass # add the converted rows and print out the csv line converted_rows[line['ID']] = r print(r.csv()) id_ctr += 1 def main(): global id_ctr, converted_rows print(','.join(['"%s"' % c for c in Row.columns])) convert_level1() convert_level2() convert_level3() convert_level4() convert_gazetteer() print(Row(id='%s' % id_ctr, name='Cultivated').csv()) id_ctr += 1 if __name__ == "__main__": main()
7,018
2,346
#!/usr/bin/python """ Program to read and parse haproxylogs to put them in shape to upload to DataLogger The input date schould be sorted by date, and finished the uploaded data will immediately split into TimeseriesArray, so no further data of this day could be appended """ import os import sys import gzip import logging logging.basicConfig(level=logging.DEBUG) import datetime import zlib import requests import StringIO import argparse # own modules from datalogger import DataLoggerWeb as DataLoggerWeb import tilak_haproxylog def aggregator(index_keynames, value_keynames, ts_keyname, func, interval = 60 * 5): """ aggregates some protocol data to get a consistent timeseries, with interval """ data = {} ts = None #print ts_keyname for parsts, parsdata in func(): #print parsdata #print parsdata["log_timestamp"] if ts is None: ts = parsts key = tuple((parsdata[key] for key in index_keynames)) values = tuple((int(parsdata[key]) for key in value_keynames)) if key not in data: data[key] = values else: data[key] = tuple((data[key][index] + int(values[index]) for index in range(len(values)))) if parsts > (ts + interval): for keys, values in data.items(): yield "%s\t%s\t%s" % (ts, "\t".join((str(index_key) for index_key in keys)), "\t".join((str(value_key) for value_key in values))) ts = None data = {} def parser_generator(index_keynames, value_keynames, file_obj): """ return specific parser for this set of parameters """ def inner(): """ split line into dict of fields, and append some data according to line """ for line in file_obj: logdata = tilak_haproxylog.parse_line(line) if logdata is not None: logdata["hits"] = 1 for value_key in value_keynames: if value_key not in logdata: logdata[value_key] = 0 status_code = int(logdata["status_code"]) if 100 <= status_code <= 199: logdata["rsp_1xx"] = 1 elif 200 <= status_code <= 299: logdata["rsp_2xx"] = 1 elif 300 <= status_code <= 399: logdata["rsp_3xx"] = 1 elif 400 <= status_code <= 499: logdata["rsp_4xx"] = 1 elif 500 <= status_code <= 599: logdata["rsp_5xx"] = 1 else: logdata["rsp_other"] = 1 ret_data = dict(zip(index_keynames, (logdata[index_key] for index_key in index_keynames))) ret_data.update(dict(zip(value_keynames, (logdata[value_key] for value_key in value_keynames)))) yield (logdata["ts"], ret_data) return inner def generate_datalogger_csv(logdir, datestring, keys, values, ts_keyname): """ create CSV like file with StringIO """ if datestring == datetime.date.today().isoformat(): logging.error("todays Logs are actually written and cannot used in datalogger") return headers = [ts_keyname, ] + list(keys) + list(values) linebuffer = [] linebuffer.append("\t".join(headers)) filename = os.path.join(logdir, "haproxylog_%s.gz" % datestring) logging.info("parsing file %s", filename) try: parser = parser_generator(keys, values, gzip.open(filename, "rb")) for line in aggregator(keys, values, ts_keyname, parser): linebuffer.append(line) except IOError as exc: logging.exception(exc) return StringIO.StringIO("\n".join(linebuffer)) def datestring_to_date(datestring): """ convert string in format YYYY-MM-DD into date object """ year, month, day = datestring.split("-") date = datetime.date(year=int(year), month=int(month), day=int(day)) return date def datewalk(datestring1, datestring2): """ count up from datestring1 to datestring2 in single day steps yield in isoformat() """ date1 = datestring_to_date(datestring1) date2 = datestring_to_date(datestring2) assert date2 > date1 oneday = datetime.timedelta(1) while date1 < date2: yield date1.isoformat() date1 += oneday def main(): """ what do you think, what main should do """ yesterday_datestring = (datetime.date.today() - datetime.timedelta(1)).isoformat() parser = argparse.ArgumentParser(description='generate TimeseriesArrays on local backend') parser.add_argument('--url', default="https://datalogger-api.tirol-kliniken.cc/DataLogger", help="url of DataLogger Webapplication") parser.add_argument('--logdir', default="/data1/haproxy_daily/", help="directory where to find day sorted haproxylogs") parser.add_argument("-b", '--back', help="how many days back from now") parser.add_argument("-s", '--startdate', help="start date in isoformat YYY-MM-DD") parser.add_argument("-e", '--enddate', default=yesterday_datestring, help="stop date in isoformat YYY-MM-DD") parser.add_argument("-q", '--quiet', action='store_true', help="set to loglevel ERROR") parser.add_argument("-v", '--verbose', action='store_true', help="set to loglevel DEBUG") args = parser.parse_args() if args.quiet is True: logging.getLogger("").setLevel(logging.ERROR) if args.verbose is True: logging.getLogger("").setLevel(logging.DEBUG) if (args.back is not None) == (args.startdate is not None): logging.error("option -b and -e are mutual exclusive, use only one") sys.exit(1) startdate = None if args.back is not None: startdate = (datetime.date.today() - datetime.timedelta(int(args.back))).isoformat() elif args.startdate is not None: startdate = args.startdate else: logging.error("you have to provide either -b or -s") sys.exit(1) # lets get started datalogger = DataLoggerWeb(args.url) project = "haproxy" tablename = "http_host" baseurl = "%s/upload_raw_file/" % args.url logdir = args.logdir # where to find haproxy logs keys = ("http_host", ) values = ("bytes_read", "rsp_1xx", "rsp_2xx", "rsp_3xx", "rsp_4xx", "rsp_5xx", "rsp_other", "srv_queue", "backend_queue", "actconn", "feconn", "beconn", "srv_conn", "retries", "tq", "tw", "tc", "tr", "tt", "hits") ts_keyname = "ts" for datestring in datewalk(startdate, args.enddate): caches = datalogger.get_caches(project, tablename, datestring) if caches["tsa"]["raw"] is not None: logging.info("Skipping this datestring, raw data is already available") continue try: stringio = generate_datalogger_csv(logdir, datestring, keys, values, ts_keyname) #upload data files = {'myfile': stringio} url = "/".join((baseurl, project, tablename, datestring)) logging.info("calling %s", url) response = requests.post(url, files=files) print response.content except StandardError as exc: logging.error("Exception on file datestring %si, skipping this date", datestring) except zlib.error as exc: logging.error(exc) if __name__ == "__main__": main()
7,386
2,317