content
stringlengths
0
1.05M
origin
stringclasses
2 values
type
stringclasses
2 values
SECRET_KEY = "it's-a-secret-to-everyone" INSTALLED_APPS = [ 'channels', 'channels_irc', ] DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", } } CHANNEL_LAYERS = { "default": { "BACKEND": "channels.layers.InMemoryChannelLayer", }, }
nilq/baby-python
python
# -*- coding: utf-8 -*- import io import os import time import fcntl import socket import struct import picamera import threading from Led import * from Servo import * from Thread import * from Buzzer import * from Control import * from ADS7830 import * from Ultrasonic import * from Command import COMMAND as cmd class Server: def __init__(self): self.tcp_flag=False self.led=Led() self.servo=Servo() self.adc=ADS7830() self.buzzer=Buzzer() self.control=Control() self.sonic=Ultrasonic() self.control.Thread_conditiona.start() self.battery_voltage=[8.4,8.4,8.4,8.4,8.4] def get_interface_ip(self): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s',b'wlan0'[:15]) )[20:24]) def turn_on_server(self): #ip adress HOST=self.get_interface_ip() #Port 8000 for video transmission self.server_socket = socket.socket() self.server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEPORT,1) self.server_socket.bind((HOST, 8001)) self.server_socket.listen(1) #Port 5000 is used for instruction sending and receiving self.server_socket1 = socket.socket() self.server_socket1.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEPORT,1) self.server_socket1.bind((HOST, 5001)) self.server_socket1.listen(1) print('Server address: '+HOST) def turn_off_server(self): try: self.connection.close() self.connection1.close() except : print ('\n'+"No client connection") def reset_server(self): self.turn_off_server() self.turn_on_server() self.video=threading.Thread(target=self.transmission_video) self.instruction=threading.Thread(target=self.receive_instruction) self.video.start() self.instruction.start() def send_data(self,connect,data): try: connect.send(data.encode('utf-8')) #print("send",data) except Exception as e: print(e) def transmission_video(self): try: self.connection,self.client_address = self.server_socket.accept() self.connection=self.connection.makefile('wb') except: pass self.server_socket.close() try: with picamera.PiCamera() as camera: camera.resolution = (400,300) # pi camera resolution camera.framerate = 15 # 15 frames/sec camera.saturation = 80 # Set image video saturation camera.brightness = 50 # Set the brightness of the image (50 indicates the state of white balance) start = time.time() stream = io.BytesIO() # send jpeg format video stream print ("Start transmit ... ") for foo in camera.capture_continuous(stream, 'jpeg', use_video_port = True): try: self.connection.flush() stream.seek(0) b = stream.read() lengthBin = struct.pack('L', len(b)) self.connection.write(lengthBin) self.connection.write(b) stream.seek(0) stream.truncate() except BaseException as e: #print (e) print ("End transmit ... " ) break except BaseException as e: #print(e) print ("Camera unintall") def measuring_voltage(self,connect): try: for i in range(5): self.battery_voltage[i]=round(self.adc.power(0),2) command=cmd.CMD_POWER+'#'+str(max(self.battery_voltage))+"\n" self.send_data(connect,command) self.sednRelaxFlag() self.battery_reminder() except Exception as e: print(e) def battery_reminder(self): if max(self.battery_voltage) < 6.4: self.turn_off_server() self.control.relax(True) print("The batteries power are too low. Please recharge the batteries or replace batteries.") print("Close the server") os._exit(0) def sednRelaxFlag(self): if self.control.move_flag!=2: command=cmd.CMD_RELAX+"#"+str(self.control.move_flag)+"\n" self.send_data(self.connection1,command) self.control.move_flag= 2 def receive_instruction(self): try: self.connection1,self.client_address1 = self.server_socket1.accept() print ("Client connection successful !") except: print ("Client connect failed") self.server_socket1.close() while True: try: allData=self.connection1.recv(1024).decode('utf-8') #print(allData) except: if self.tcp_flag: if max(self.battery_voltage) > 6.4: self.reset_server() break else: break if allData=="" and self.tcp_flag: self.reset_server() break else: cmdArray=allData.split('\n') #print(cmdArray) if cmdArray[-1] !="": cmdArray==cmdArray[:-1] for oneCmd in cmdArray: data=oneCmd.split("#") if data==None or data[0]=='': continue elif cmd.CMD_BUZZER in data: self.buzzer.run(data[1]) elif cmd.CMD_LED in data: try: stop_thread(thread_led) except: pass thread_led=threading.Thread(target=self.led.light,args=(data,)) thread_led.start() elif cmd.CMD_LED_MOD in data: try: stop_thread(thread_led) except: pass thread_led=threading.Thread(target=self.led.light,args=(data,)) thread_led.start() elif cmd.CMD_HEAD in data: self.servo.setServoAngle(15,int(data[1])) elif cmd.CMD_SONIC in data: command=cmd.CMD_SONIC+'#'+str(self.sonic.getDistance())+"\n" self.send_data(self.connection1,command) elif cmd.CMD_POWER in data: self.measuring_voltage(self.connection1) elif cmd.CMD_WORKING_TIME in data: if self.control.move_timeout!=0 and self.control.relax_flag==True: if self.control.move_count >180: command=cmd.CMD_WORKING_TIME+'#'+str(180)+'#'+str(round(self.control.move_count-180))+"\n" else: if self.control.move_count==0: command=cmd.CMD_WORKING_TIME+'#'+str(round(self.control.move_count))+'#'+str(round((time.time()-self.control.move_timeout)+60))+"\n" else: command=cmd.CMD_WORKING_TIME+'#'+str(round(self.control.move_count))+'#'+str(round(time.time()-self.control.move_timeout))+"\n" else: command=cmd.CMD_WORKING_TIME+'#'+str(round(self.control.move_count))+'#'+str(0)+"\n" self.send_data(self.connection1,command) else: self.control.order=data self.control.timeout=time.time() try: stop_thread(thread_power) except: pass try: stop_thread(thread_led) except: pass print("close_recv") self.control.relax_flag=False self.control.order[0]=cmd.CMD_RELAX if __name__ == '__main__': pass
nilq/baby-python
python
import os data_path_root = "E:\Projects\projectHO\data" stock_list_path = os.path.join(data_path_root, "stock_list.csv") split_span = 365 * 5 # split dates to download pre-historical data retry_count = 5 # downloader max retry count log_path = "E:\Projects\projectHO\log"
nilq/baby-python
python
# -*- coding: utf-8 -*- # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file # except in compliance with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software distributed under the # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, # either express or implied. See the License for the specific language governing permissions # and limitations under the License. """ This module for test. """ import os import sys import unittest import uuid file_path = os.path.normpath(os.path.dirname(__file__)) sys.path.append(file_path + '/../../') import baidubce from baidubce.auth.bce_credentials import BceCredentials from baidubce.bce_client_configuration import BceClientConfiguration from baidubce.services.vpc import vpc_client if sys.version < '3': reload(sys) sys.setdefaultencoding('utf-8') # config parameters vpc_id = 'vpc-51csm6rxs9mg' def generate_client_token_by_uuid(): """ The default method to generate the random string for client_token if the optional parameter client_token is not specified by the user. :return: :rtype string """ return str(uuid.uuid4()) generate_client_token = generate_client_token_by_uuid class TestVpcClient(unittest.TestCase): """ unit test """ def setUp(self): """ set up """ HOST = b'bcc.bj.baidubce.com' AK = b'' SK = b'' config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST) self.the_client = vpc_client.VpcClient(config) def test_create_vpc(self): """ test case for create_vpc """ client_token = generate_client_token() vpc_name = 'test_vpc_name' + client_token vpc_cidr = '192.168.240.0/20' description = 'test_vpc_descrition' + client_token self.assertEqual( type(self.the_client.create_vpc(vpc_name, vpc_cidr, description, client_token=client_token)), baidubce.bce_response.BceResponse) def test_list_vpcs(self): """ test case for list_vpcs """ print(self.the_client.list_vpcs()) def test_get_vpc(self): """ test case for get_vpc """ self.assertEqual( type(self.the_client.get_vpc(vpc_id)), baidubce.bce_response.BceResponse) def test_delete_vpc(self): """ test case for delete_vpc """ self.assertEqual( type(self.the_client.delete_vpc(vpc_id)), baidubce.bce_response.BceResponse) def test_update_vpc(self): """ test case for delete_vpc """ self.assertEqual( type(self.the_client.update_vpc(vpc_id, 'test_update_name', 'test_update_description')), baidubce.bce_response.BceResponse) if __name__ == "__main__": suite = unittest.TestSuite() # suite.addTest(TestVpcClient("test_create_vpc")) # suite.addTest(TestVpcClient("test_list_vpcs")) # suite.addTest(TestVpcClient("test_get_vpc")) # suite.addTest(TestVpcClient("test_delete_vpc")) suite.addTest(TestVpcClient("test_update_vpc")) runner = unittest.TextTestRunner() runner.run(suite)
nilq/baby-python
python
#!/usr/bin/python3 -OO # Copyright 2007-2022 The SABnzbd-Team <team@sabnzbd.org> # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. """ tests.test_newsunpack - Tests of various functions in newspack """ import glob import logging import os.path import shutil from unittest.mock import call from sabnzbd.filesystem import build_filelists from tests.testhelper import * import sabnzbd import sabnzbd.newsunpack as newsunpack from sabnzbd.constants import JOB_ADMIN class TestNewsUnpackFunctions: def test_is_sfv_file(self): assert newsunpack.is_sfv_file("tests/data/good_sfv_unicode.sfv") assert newsunpack.is_sfv_file("tests/data/one_line.sfv") assert not newsunpack.is_sfv_file("tests/data/only_comments.sfv") assert not newsunpack.is_sfv_file("tests/data/random.bin") def test_is_sevenfile(self): # False, because the command is not set assert not newsunpack.SEVENZIP_COMMAND assert not newsunpack.is_sevenfile("tests/data/test_7zip/testfile.7z") # Set the command to get some real results newsunpack.find_programs(".") assert newsunpack.SEVENZIP_COMMAND assert not newsunpack.is_sevenfile("tests/data/only_comments.sfv") assert not newsunpack.is_sevenfile("tests/data/random.bin") assert not newsunpack.is_sevenfile("tests/data/par2file/basic_16k.par2") assert newsunpack.is_sevenfile("tests/data/test_7zip/testfile.7z") def test_sevenzip(self): testzip = newsunpack.SevenZip("tests/data/test_7zip/testfile.7z") assert testzip.namelist() == ["testfile.bin"] # Basic check that we can get data from the 7zip assert len(testzip.open(testzip.namelist()[0]).read()) == 102400 # Test with a non-7zip file with pytest.raises(TypeError): newsunpack.SevenZip("tests/data/basic_rar5/testfile.rar") @pytest.mark.usefixtures("clean_cache_dir") class TestPar2Repair: @staticmethod def _run_par2repair(test_dir, caplog, break_file=None, remove_file=None): # Create data-directory with copy of our test-files temp_test_dir = os.path.join(SAB_CACHE_DIR, "par2repair_temp") test_dir_admin = os.path.join(temp_test_dir, JOB_ADMIN) os.mkdir(temp_test_dir) assert os.path.exists(temp_test_dir) os.mkdir(test_dir_admin) assert os.path.exists(test_dir_admin) # Copy all test files for file in glob.glob(test_dir + "/*"): shutil.copy(file, temp_test_dir) # Break a specific file, if requested if break_file: with open(os.path.join(temp_test_dir, break_file), "wb") as bf: bf.seek(10) bf.write(b"booh") # Remove a specific file, if requested if remove_file: os.unlink(os.path.join(temp_test_dir, remove_file)) # Make sure all programs are found newsunpack.find_programs(".") # Needed to store the POpen-reference sabnzbd.PostProcessor = mock.Mock() # Mock basic NZO structure nzo = mock.Mock() nzo.download_path = temp_test_dir nzo.admin_path = test_dir_admin nzo.fail_msg = "" nzo.extrapars = {"test": []} nzo.md5packs = {"test": None} for file in glob.glob(test_dir + "/*.par2"): # Simple NZF mock for the filename parfile = mock.Mock() parfile.filename = os.path.basename(file) nzo.extrapars["test"].append(parfile) # We want to collect all updates nzo.set_action_line = mock.Mock() nzo.set_unpack_info = mock.Mock() nzo.renamed_file = mock.Mock() # Run repair with caplog.at_level(logging.DEBUG): readd, result = newsunpack.par2_repair(nzo=nzo, setname="test") # Verify we only have the rar-files left dir_contents = os.listdir(temp_test_dir) dir_contents.sort() # Always cleanup, to be sure shutil.rmtree(temp_test_dir) assert not os.path.exists(temp_test_dir) # Verify result assert result assert not readd # Verify history updates nzo.set_unpack_info.assert_has_calls( [ call("Repair", "[test] Verified in 0 seconds, repair is required"), call("Repair", "[test] Repaired in 0 seconds"), ] ) # Check externally return nzo, dir_contents def test_basic(self, caplog): # Run code nzo, dir_contents = self._run_par2repair("tests/data/par2repair/basic", caplog) assert dir_contents == [ "__ADMIN__", "notarealfile.rar", "par2test.part1.rar", "par2test.part2.rar", "par2test.part3.rar", "par2test.part4.rar", "par2test.part5.rar", "par2test.part6.rar", ] # Verify renames nzo.renamed_file.assert_has_calls( [ call( { "par2test.part3.rar": "foorbar.rar", "par2test.part4.rar": "stillrarbutnotagoodname.txt", "par2test.part1.rar": "par2test.part1.11.rar", } ) ] ) if sabnzbd.WIN32: # Multipar output status updates nzo.set_action_line.assert_has_calls( [ call("Repair", "Quick Checking"), call("Repair", "Starting Repair"), call("Checking", "01/06"), call("Checking", "02/06"), call("Checking", "03/06"), call("Checking", "04/06"), call("Checking", "05/06"), call("Checking", "06/06"), # We only know total of missing files, so not how many will be found call("Checking extra files", "01/05"), call("Checking extra files", "02/05"), call("Verifying", "01/03"), call("Verifying", "02/03"), call("Verifying", "03/03"), call("Repairing", " 0%"), call("Repairing", "100% "), call("Verifying repair", "01/03"), call("Verifying repair", "02/03"), call("Verifying repair", "03/03"), ] ) else: # par2cmdline output status updates # Verify output in chunks, as it outputs every single % during repair nzo.set_action_line.assert_has_calls( [ call("Repair", "Quick Checking"), call("Repair", "Starting Repair"), call("Verifying", "01/06"), call("Verifying", "02/06"), call("Verifying", "03/06"), call("Verifying", "04/06"), call("Verifying", "05/06"), call("Verifying", "06/06"), call("Checking extra files", "01"), call("Checking extra files", "02"), call("Checking extra files", "03"), call("Repairing", " 0%"), ] ) nzo.set_action_line.assert_has_calls( [ call("Repairing", "100% "), call("Verifying repair", "01/03"), call("Verifying repair", "02/03"), call("Verifying repair", "03/03"), ] ) def test_filejoin(self, caplog): # Run code nzo, dir_contents = self._run_par2repair("tests/data/par2repair/filejoin", caplog) # All joinable files will be removed assert dir_contents == ["__ADMIN__", "par2test.bin"] # There are no renames in case of filejoin by par2repair! nzo.renamed_file.assert_not_called() if sabnzbd.WIN32: # Multipar output status updates, which is limited because Multipar doesn't say much.. nzo.set_action_line.assert_has_calls( [ call("Repair", "Quick Checking"), call("Repair", "Starting Repair"), call("Checking", "01/01"), call("Verifying", "01"), call("Verifying", "02"), call("Verifying", "03"), call("Verifying", "04"), call("Verifying", "05"), call("Verifying", "06"), call("Verifying", "07"), call("Verifying", "08"), call("Verifying", "09"), call("Verifying", "10"), call("Verifying", "11"), call("Joining", "11"), call("Verifying repair", "01/01"), ] ) else: # par2cmdline output status updates # Verify output in chunks, as it outputs every single % during repair nzo.set_action_line.assert_has_calls( [ call("Repair", "Quick Checking"), call("Repair", "Starting Repair"), call("Verifying", "01/01"), call("Checking extra files", "01"), call("Checking extra files", "02"), call("Checking extra files", "03"), call("Checking extra files", "04"), call("Checking extra files", "05"), call("Checking extra files", "06"), call("Checking extra files", "07"), call("Checking extra files", "08"), call("Checking extra files", "09"), call("Checking extra files", "10"), call("Checking extra files", "11"), call("Repairing", " 0%"), ] ) nzo.set_action_line.assert_has_calls( [ call("Repairing", "100% "), call("Verifying repair", "01/01"), ] ) def test_broken_filejoin(self, caplog): # Run code nzo, dir_contents = self._run_par2repair( "tests/data/par2repair/filejoin", caplog, break_file="par2test.bin.005", remove_file="par2test.bin.010" ) # There are no renames in case of filejoin by par2repair! nzo.renamed_file.assert_not_called() # All joinable files should be removed assert dir_contents == ["__ADMIN__", "par2test.bin"] if sabnzbd.WIN32: # Multipar output status updates, which is limited because Multipar doesn't say much.. nzo.set_action_line.assert_has_calls( [ call("Repair", "Quick Checking"), call("Repair", "Starting Repair"), call("Checking", "01/01"), call("Verifying", "01"), call("Verifying", "02"), call("Verifying", "03"), call("Verifying", "04"), call("Verifying", "05"), call("Verifying", "06"), call("Verifying", "07"), call("Verifying", "08"), call("Verifying", "09"), call("Repairing", " 0%"), call("Repairing", "100% "), call("Verifying repair", "01/01"), ] ) else: # Verify output in chunks, as it outputs every single % during repair nzo.set_action_line.assert_has_calls( [ call("Repair", "Quick Checking"), call("Repair", "Starting Repair"), call("Verifying", "01/01"), call("Checking extra files", "01"), call("Checking extra files", "02"), call("Checking extra files", "03"), call("Checking extra files", "04"), call("Checking extra files", "05"), call("Checking extra files", "06"), call("Checking extra files", "07"), call("Checking extra files", "08"), call("Checking extra files", "09"), call("Repairing", " 0%"), ] ) nzo.set_action_line.assert_has_calls( [ call("Repairing", "100% "), call("Verifying repair", "01/01"), ] )
nilq/baby-python
python
''' generic functions ''' def myfunc( x:int ): print( x * 100 ) def myfunc( x:string ): print( x + 'world' ) def main(): myfunc( 10 ) myfunc( 'hello' )
nilq/baby-python
python
# Third-Party Imports from django.core.exceptions import ValidationError from django.db.models import ProtectedError # App Imports from core.tests import CoreBaseTestCase from ..models import AssetCategory class AssetCategoryModelTest(CoreBaseTestCase): """ Tests for the Asset Category Model """ def test_can_save_a_category(self): AssetCategory.objects.create(name="Electronics") new_category = AssetCategory.objects.get(name="Electronics") new_category_count = AssetCategory.objects.count() self.assertEqual(new_category_count, 2) self.assertIn(new_category.name, "Electronics") def test_cannot_add_existing_category_name(self): self.assertEqual(AssetCategory.objects.count(), 1) cat_name = AssetCategory.objects.first().name with self.assertRaises(ValidationError): AssetCategory.objects.create(name=cat_name) self.assertEqual(AssetCategory.objects.count(), 1) def test_can_edit_a_category(self): self.category.name = "Accessory" self.category.save() self.assertIn("Accessory", self.category.name) def test_asset_category_model_string_representation(self): self.assertEqual(str(self.category), self.category.name) def test_cannot_delete_category_with_existing_subcategories(self): count_before_delete = AssetCategory.objects.count() with self.assertRaises(ProtectedError): self.category.delete() count_after_delete = AssetCategory.objects.count() self.assertEqual(count_before_delete, count_after_delete) def test_can_delete_category_without_existing_subcategories(self): new_category_without_subcategories = AssetCategory.objects.create( name="New Category" ) count_before_delete = AssetCategory.objects.count() new_category_without_subcategories.delete() count_after_delete = AssetCategory.objects.count() self.assertEqual(count_after_delete, count_before_delete - 1)
nilq/baby-python
python
from unittest.mock import Mock, patch, call import pytest from sqlalchemy import column from sqlalchemy import text from sqlalchemy import func from sqlalchemy import not_ from sqlalchemy_filters import operators from sqlalchemy_filters.exceptions import InvalidParamError from tests.utils import compares_expressions def compile_sql(expression): return expression.compile(compile_kwargs={"literal_binds": True}) def test_register_operator(): op = Mock() @operators.register_operator(sql_operator=op) class F: pass assert F().operator is op @pytest.mark.parametrize("version", ["1", "1.1", "1.2", "1.3", "1.3.22"]) def test_sa_1_4_compatible_for_older_versions(version): with patch.object(operators, "SQLALCHEMY_VERSION", version): f = Mock() assert operators.sa_1_4_compatible(f) is f @pytest.mark.parametrize( "sql_exp, params", [ [text(""), column("a").is_(None)], [text("1 = 1"), column("a").is_(None)], ], ) def test_sa_1_4_compatible(sql_exp, params): with patch.object(operators, "SQLALCHEMY_VERSION", "1.4"): to_sql = Mock() self = Mock(get_sql_expression=Mock(return_value=sql_exp), params=[params]) to_sql_v2 = operators.sa_1_4_compatible(to_sql) to_sql_v2(self) assert not to_sql.called assert self.operator.called_once param1, param2 = self.operator.call_args[0] assert compares_expressions(param1, params) assert compares_expressions(param2, sql_exp) @pytest.mark.parametrize( "sql_exp, params", [ [column("a").is_(None), text("")], [column("a").is_(None), text("1 = 1")], ], ) def test_sa_1_4_compatible_should_not_alter_params(sql_exp, params): with patch.object(operators, "SQLALCHEMY_VERSION", "1.4"): to_sql = Mock() self = Mock(get_sql_expression=Mock(return_value=sql_exp), params=[params]) to_sql_v2 = operators.sa_1_4_compatible(to_sql) to_sql_v2(self) assert to_sql.called assert not self.operator.called assert to_sql.call_args == call(self) def test_operator_init(): op = operators.BaseOperator(sql_expression="A", params=["B"]) assert op.sql_expression == "A" assert op.params == ["B"] assert str(op.get_sql_expression()) == '"A"' def test_equals_operator(): _column = column("my_column") op = operators.EqualsOperator(sql_expression=_column, params=["A"]) assert op.to_sql().compare(_column == "A") def test_is_operator(): _column = column("my_column") op = operators.IsOperator(sql_expression=_column, params=["A"]) assert op.to_sql().compare(_column.is_("A")) def test_is_not_operator(): _column = column("my_column") op = operators.IsOperator(sql_expression=_column, params=["A"]) assert op.to_sql().compare(not_(_column.is_("A"))) def test_gte_operator(): _column = column("my_column") op = operators.GTEOperator(sql_expression=_column, params=["A"]) assert op.to_sql().compare(_column >= "A") def test_lte_operator(): _column = column("my_column") op = operators.LTEOperator(sql_expression=_column, params=["A"]) assert op.to_sql().compare(_column <= "A") def test_starts_with_operator(): _column = column("my_column") op = operators.StartsWithOperator(sql_expression=_column, params=["A"]) assert op.to_sql().compare(_column.startswith("A")) def test_istarts_with_operator(): _column = column("my_column") op = operators.IStartsWithOperator(sql_expression=_column, params=["A"]) expected = compile_sql(func.lower(_column).startswith(func.lower("A"))) assert str(compile_sql(op.to_sql())) == str(expected) def test_ends_with_operator(): _column = column("my_column") op = operators.EndsWithOperator(sql_expression=_column, params=["A"]) assert op.to_sql().compare(_column.endswith("A")) def test_iends_with_operator(): _column = column("my_column") op = operators.IEndsWithOperator(sql_expression=_column, params=["A"]) expected = compile_sql(func.lower(_column).endswith(func.lower("A"))) assert str(compile_sql(op.to_sql())) == str(expected) def test_contains_operator(): _column = column("my_column") op = operators.ContainsOperator(sql_expression=_column, params=["A"]) assert op.to_sql().compare(_column.contains("A")) @pytest.mark.parametrize("value", [[], ["A"], ["A", "B"]]) def test_in_operator(value): _column = column("my_column") op = operators.INOperator(sql_expression=_column, params=value) assert op.to_sql().compare(_column.in_(value)) def test_range_operator(): _column = column("my_column") op = operators.RangeOperator(sql_expression=_column, params=[0, 10]) assert op.to_sql().compare(_column.between(0, 10)) def test_in_operator_invalid_params(): with pytest.raises(InvalidParamError) as exc: operators.BaseOperator(sql_expression="A", params="A") assert str(exc.value) == "BaseOperator.params expected to be a list, got str." def test_range_operator_invalid_params(): with pytest.raises(InvalidParamError) as exc: operators.RangeOperator(sql_expression="A", params=[]) assert str(exc.value) == "RangeOperator.params should have exactly 2 values, got 0."
nilq/baby-python
python
import auditor from event_manager.events import tensorboard auditor.subscribe(tensorboard.TensorboardStartedEvent) auditor.subscribe(tensorboard.TensorboardStartedTriggeredEvent) auditor.subscribe(tensorboard.TensorboardSoppedEvent) auditor.subscribe(tensorboard.TensorboardSoppedTriggeredEvent) auditor.subscribe(tensorboard.TensorboardViewedEvent) auditor.subscribe(tensorboard.TensorboardBookmarkedEvent) auditor.subscribe(tensorboard.TensorboardUnBookmarkedEvent) auditor.subscribe(tensorboard.TensorboardNewStatusEvent) auditor.subscribe(tensorboard.TensorboardFailedEvent) auditor.subscribe(tensorboard.TensorboardSucceededEvent)
nilq/baby-python
python
# -*- coding: utf-8 -*- """ Created on Mon Feb 13 09:50:51 2017 @author: mkonrad """ import math import pytest from hypothesis import given import hypothesis.strategies as st import numpy as np from pdftabextract.geom import (pt, ptdist, vecangle, vecrotate, overlap, lineintersect, rect, rectcenter, rectarea, rectintersect, normalize_angle, normalize_angle_halfcircle, project_polarcoord_lines) FMIN = np.finfo(np.float32).min FMAX = np.finfo(np.float32).max def test_pt(): x = 0 y = 1 pt0 = pt(x, y) assert type(pt0) is np.ndarray assert pt0.dtype == np.float assert pt0[0] == x assert pt0[1] == y pt1 = pt(x, y, np.int) assert pt1.dtype == np.int assert pt1[0] == x assert pt1[1] == y def test_ptdist(): p1 = pt(0, 0) p2 = pt(1, 0) p3 = pt(1, 1) assert ptdist(p1, p1) == 0 assert ptdist(p1, p2) == 1 assert ptdist(p2, p1) == ptdist(p1, p2) assert ptdist(p1, p3) == math.sqrt(2) def test_vecangle(): v1 = pt(1, 0) v2 = pt(2, 0) v3 = pt(1, 1) v4 = pt(0, 1) v5 = pt(0, -1) assert np.isnan(vecangle(pt(0, 0), v1)) # pt(0, 0) is vec of no length assert vecangle(v1, v2) == 0 assert round(vecangle(v1, v3), 4) == round(math.radians(45), 4) assert vecangle(v2, v4) == vecangle(v1, v4) == math.radians(90) assert vecangle(v2, v5) == math.radians(90) # always the smaller angle @given(st.floats(min_value=FMIN, max_value=FMAX), st.floats(min_value=FMIN, max_value=FMAX), st.floats(min_value=FMIN, max_value=FMAX), st.floats(min_value=FMIN, max_value=FMAX)) def test_vecangle_2(x1, y1, x2, y2): v0 = pt(0, 0) v1 = pt(x1, y1) v2 = pt(x2, y2) try: alpha = vecangle(v1, v2) except ValueError: # math domain error in some edge cases? return if np.allclose(v1, v0) or np.allclose(v2, v0): assert np.isnan(alpha) else: assert 0 <= alpha <= np.pi def test_vecrotate(): assert np.array_equal(vecrotate(pt(0, 0), 0.123), pt(0, 0)) assert np.allclose(vecrotate(pt(1, 0), math.radians(90)), pt(0, 1)) assert np.allclose(vecrotate(pt(1, 0), math.radians(90), about=pt(1, 1)), pt(2, 1)) def test_overlap(): assert overlap(0, 1, 0, 1) is True assert overlap(0, 0, 1, 1) is False assert overlap(0, 10, 5, 15) is True assert overlap(-10, 10, -20, -10) is True assert overlap(-9, 10, -20, -10) is False def test_lineintersect(): # first with check_in_segm = True X = lineintersect(pt(0, 0), pt(0, 0), pt(0, 0), pt(0, 0)) # coincident I assert sum(np.isnan(X)) == len(X) X = lineintersect(pt(0, 0), pt(0, 1), pt(0, 0), pt(0, 1)) # coincident II assert sum(np.isnan(X)) == len(X) assert lineintersect(pt(0, 0), pt(0, 1), pt(1, 0), pt(1, 1)) is None # parallel, non coincident assert lineintersect(pt(0, 0), pt(0, 1), pt(1, 1), pt(2, 2)) is None # non-parellel, no intersection assert lineintersect(pt(0, 0), pt(2, 2), pt(0, 5), pt(5, 0)) is None # non-parellel, no intersection II assert np.array_equal(lineintersect(pt(0, 0), pt(0, 1), pt(0, 1), pt(2, 2)), pt(0, 1)) # intersection - touch assert np.array_equal(lineintersect(pt(0, 0), pt(2, 2), pt(0, 2), pt(2, 0)), pt(1, 1)) # intersection # now with check_in_segm = False X = lineintersect(pt(0, 0), pt(0, 0), pt(0, 0), pt(0, 0), False) # coincident I assert sum(np.isnan(X)) == len(X) X = lineintersect(pt(0, 0), pt(0, 1), pt(0, 0), pt(0, 1), False) # coincident II assert sum(np.isnan(X)) == len(X) X = lineintersect(pt(0, 0), pt(1, 1), pt(2, 2), pt(3, 3), False) # coincident III assert sum(np.isnan(X)) == len(X) assert np.array_equal(lineintersect(pt(0, 0), pt(0, 1), pt(1, 1), pt(2, 2), False), pt(0, 0)) # intersection (out of segments) assert np.array_equal(lineintersect(pt(0, 0), pt(0, 1), pt(0, 1), pt(2, 2), False), pt(0, 1)) # intersection - touch assert np.array_equal(lineintersect(pt(0, 0), pt(2, 2), pt(0, 2), pt(2, 0), False), pt(1, 1)) # intersection def test_rect(): with pytest.raises(ValueError): rect(pt(0, 0), pt(1, 1, dtype=np.int)) # dtypes do not match with pytest.raises(ValueError): rect(pt(0, 0), pt(0, 0)) # doesn't form rect with pytest.raises(ValueError): rect(pt(1, 1), pt(0, 0)) # doesn't form rect with pytest.raises(ValueError): rect(pt(0, 0), pt(1, 0)) # doesn't form rect a = pt(0, 0) b = pt(1, 1) r = rect(a, b) assert r.dtype == a.dtype == b.dtype assert np.array_equal(r[0], a) assert np.array_equal(r[1], b) a = pt(-3, -1) b = pt(8, 1.2) r = rect(a, b) assert r.dtype == a.dtype == b.dtype assert np.array_equal(r[0], a) assert np.array_equal(r[1], b) def test_rectcenter(): a = pt(0, 0) b = pt(1, 1) r = rect(a, b) center = rectcenter(r) assert type(center) is np.ndarray assert np.array_equal(center, pt(0.5, 0.5)) a = pt(-3, -1) b = pt(2, 5) r = rect(a, b) assert np.array_equal(rectcenter(r), pt(-0.5, 2)) def test_rectarea(): a = pt(0, 0) b = pt(1, 1) r = rect(a, b) assert rectarea(r) == 1 a = pt(-3, -1) b = pt(2, 5) r = rect(a, b) assert rectarea(r) == 30 def test_rectintersect(): a = rect(pt(0, 0), pt(1, 1)) b = rect(pt(-3, -1), pt(2, 5)) assert rectintersect(a, a) == rectarea(a) assert rectintersect(b, b) == rectarea(b) assert rectintersect(a, a, norm_intersect_area='a') == 1 assert rectintersect(a, a, norm_intersect_area='b') == 1 with pytest.raises(ValueError): rectintersect(a, a, norm_intersect_area='c') # complete intersect assert rectintersect(a, b) == rectarea(a) assert rectintersect(b, a) == rectarea(a) assert rectintersect(a, b, norm_intersect_area='a') == 1 assert rectintersect(b, a, norm_intersect_area='b') == 1 assert rectintersect(b, a, norm_intersect_area='a') < 1 assert rectintersect(a, b, norm_intersect_area='b') < 1 # partial intersect a = rect(pt(0, 0), pt(1, 1)) b = rect(pt(0.5, 0.5), pt(1.5, 1.5)) assert rectintersect(a, b) == 0.25 assert rectintersect(a, b, norm_intersect_area='a') == 0.25 assert rectintersect(a, b, norm_intersect_area='b') == 0.25 b = rect(pt(0.75, 0.5), pt(1.5, 1.5)) assert rectintersect(a, b) == 0.125 # touch a = rect(pt(0, 0), pt(1, 1)) b = rect(pt(1, 1), pt(1.5, 1.5)) assert rectintersect(a, b) == 0 # no intersection a = rect(pt(0, 0), pt(1, 1)) b = rect(pt(1.1, 1.1), pt(1.5, 1.5)) assert rectintersect(a, b) is None def test_normalize_angle(): for i in range(-10, 10): theta = i * np.pi norm = normalize_angle(theta) assert 0 <= norm < 2 * np.pi assert norm / np.pi == i % 2 def test_normalize_angle_halfcircle(): for i in range(-10, 10): theta = 0.5 * i * np.pi norm = normalize_angle_halfcircle(theta) assert 0 <= norm < np.pi assert norm / np.pi * 2 == i % 2 @given( st.lists(st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=2, max_size=2)), st.integers(), st.integers() ) def test_project_polarcoord_lines(hough_lines, img_w, img_h): if img_w <= 0 or img_h <= 0: with pytest.raises(ValueError): project_polarcoord_lines(hough_lines, img_w, img_h) return else: res = project_polarcoord_lines(hough_lines, img_w, img_h) assert type(res) is list assert len(res) == len(hough_lines) for pts in res: assert len(pts) == 2 assert type(pts[0]) == type(pts[1]) == np.ndarray assert len(pts[0]) == len(pts[1]) == 2
nilq/baby-python
python
""" Open stuff in Chromium """ from albertv0 import Item, ProcAction, ClipAction import json from shutil import which __iid__ = "PythonInterface/v0.1" __prettyname__ = "Web Browser" __version__ = "1.0" __trigger__ = "web " __author__ = "Michael Farber Brodsky" def handleQuery(query): if query.isTriggered: stripped = query.string.strip() items = [] url = stripped if not (url[:7] in ["http://", "https:/"]): url = "https://" + url normal_item = Item( id="website-"+stripped, text=stripped, subtext="open in chromium", completion=stripped, actions=[ ProcAction(text="Open this", commandline=["/usr/bin/chromium", "--app="+url]) ] ) items.append(normal_item) return items
nilq/baby-python
python
from urllib.request import urlopen SIMPLE_URL = "http://www.dinopass.com/password/simple" COMPLEX_URL = "http://www.dinopass.com/password/strong" def GetSimplePassword(): response = urlopen(SIMPLE_URL) bytes = response.readline() return bytes.decode() def GetComplexPassword(): response = urlopen(COMPLEX_URL) bytes = response.readline() return bytes.decode()
nilq/baby-python
python
# Copyright (C) 2020 FireEye, Inc. All Rights Reserved. import io import os from urllib.parse import urlparse from io import BytesIO from speakeasy.errors import NetworkEmuError def is_empty(bio): if len(bio.getbuffer()) == bio.tell(): return True return False def normalize_response_path(path): def _get_speakeasy_root(): return os.path.join(os.path.dirname(__file__), os.pardir) root_var = "$ROOT$" if root_var in path: root = _get_speakeasy_root() return path.replace(root_var, root) return path class Socket(object): """ Represents a Windows network socket """ def __init__(self, fd, family, stype, protocol, flags): self.fd = fd self.family = family self.type = stype self.protocol = protocol self.flags = flags self.connected_host = "" self.connected_port = 0 self.curr_packet = BytesIO(b"") self.packet_queue = [] def get_fd(self): return self.fd def get_type(self): return self.type def set_connection_info(self, host, port): self.connected_host = host self.connected_port = port def get_connection_info(self): return (self.connected_host, self.connected_port) def fill_recv_queue(self, responses): for resp in responses: mode = resp.get("mode", "") if mode.lower() == "default": default_resp_path = resp.get("path") if default_resp_path: default_resp_path = normalize_response_path(default_resp_path) with open(default_resp_path, "rb") as f: self.curr_packet = BytesIO(f.read()) def get_recv_data(self, size, peek=False): data = self.curr_packet.read(size) if not peek: return data elif peek: self.curr_packet.seek(-size, os.SEEK_CUR) return data class WSKSocket(Socket): """ Represents a WSK socket used in kernel mode applications """ def __init__(self, fd, family, stype, protocol, flags): super(WSKSocket, self).__init__(self, fd, family, stype, protocol, flags) class WininetComponent(object): """ Base class used for WinInet connections """ curr_handle = 0x20 config = None def __init__(self): super(WininetComponent, self).__init__() self.handle = self.new_handle() def new_handle(self): tmp = WininetComponent.curr_handle WininetComponent.curr_handle += 4 return tmp def get_handle(self): return self.handle class WininetRequest(WininetComponent): """ WinInet request object """ def __init__(self, session, verb, objname, ver, ref, accepts, flags, ctx): super(WininetRequest, self).__init__() # The WiniNet APIs default to a HTTP "GET" if no verb is specified if not verb: self.verb = "get" else: self.verb = verb.lower() self.objname = objname if not self.objname: self.objname = "" self.objname = urlparse(self.objname) self.session = session if not ver: ver = "HTTP/1.1" self.ver = ver self.referrer = ref self.accept_types = accepts self.flags = flags self.ctx = ctx self.response = None def get_session(self): return self.session def get_server(self): return self.get_session().server def get_port(self): return self.get_session().port def get_instance(self): sess = self.get_session() return sess.get_instance() def is_secure(self): if "INTERNET_FLAG_SECURE" in self.flags: return True return False def format_http_request(self, headers=None): request_string = "" if headers: request_string += headers inst = self.get_instance() sess = self.get_session() host = sess.server request_string += "Host: %s\n" % (host) ua = inst.get_user_agent() if ua: request_string += "User-Agent: %s\n" % (ua) if "INTERNET_FLAG_KEEP_CONNECTION" in self.flags: request_string += "Connection: Keep-Alive\n" else: request_string += "Connection: Close\n" if "INTERNET_FLAG_DONT_CACHE" in self.flags: request_string += "Cache-Control: no-cache\n" return request_string def get_response_size(self): resp = self.get_response() off = resp.tell() size = len(resp.read()) resp.seek(off, io.SEEK_SET) return size def get_response(self): """ Check the configuration file so see if there is a handler for the current WinInet request """ cfg = WininetComponent.config if self.response: return self.response http = cfg.get("http") if not http: raise NetworkEmuError("No HTTP configuration supplied") resps = http.get("responses") if not resps: raise NetworkEmuError("No HTTP responses supplied") self.response = None for res in resps: verb = res.get("verb", "") if verb.lower() == self.verb: resp_files = res.get("files", []) if resp_files: for file in resp_files: mode = file.get("mode", "") if mode.lower() == "by_ext": ext = file.get("ext", "") fn, obj_ext = os.path.splitext(self.objname.path) if ext.lower().strip(".") == obj_ext.lower().strip("."): path = file.get("path") path = normalize_response_path(path) with open(path, "rb") as f: self.response = BytesIO(f.read()) elif mode.lower() == "default": default_resp_path = file.get("path") default_resp_path = normalize_response_path( default_resp_path ) if not self.response and default_resp_path: default_resp_path = normalize_response_path(default_resp_path) with open(default_resp_path, "rb") as f: self.response = BytesIO(f.read()) return self.response def get_object_path(self): return self.objname class WininetSession(WininetComponent): def __init__(self, instance, server, port, user, password, service, flags, ctx): super(WininetSession, self).__init__() self.server = server self.port = port self.user = user self.password = password self.service = service self.flags = flags self.ctx = ctx self.requests = {} self.instance = instance def get_instance(self): return self.instance def get_flags(self): return self.flags def new_request(self, verb, objname, ver, ref, accepts, flags, ctx): req = WininetRequest(self, verb, objname, ver, ref, accepts, flags, ctx) hdl = req.get_handle() self.requests.update({hdl: req}) return req class WininetInstance(WininetComponent): def __init__(self, user_agent, access, proxy, bypass, flags): super(WininetInstance, self).__init__() self.user_agent = user_agent self.access = access self.proxy = proxy self.bypass = bypass self.flags = flags self.sessions = {} def get_session(self, sess_handle): self.sessions.get(sess_handle) def add_session(self, handle, session): self.sessions.update({handle: session}) def new_session(self, server, port, user, password, service, flags, ctx): sess = WininetSession(self, server, port, user, password, service, flags, ctx) hdl = sess.get_handle() self.sessions.update({hdl: sess}) return sess def get_user_agent(self): return self.user_agent class NetworkManager(object): """ Class that manages network connections during emulation """ def __init__(self, config): super(NetworkManager, self).__init__() self.sockets = {} self.wininets = {} self.curr_fd = 4 self.curr_handle = 0x20 self.config = config self.dns = {} WininetComponent.config = config self.dns = self.config.get("dns") def new_socket(self, family, stype, protocol, flags): fd = self.curr_fd sock = Socket(fd, family, stype, protocol, flags) self.curr_fd += 4 if self.config: winsock = self.config.get("winsock") if winsock: responses = winsock.get("responses") if responses: sock.fill_recv_queue(responses) self.sockets.update({fd: sock}) return sock def name_lookup(self, domain): if not self.dns: return None names = self.dns.get("names") # Do we have an IP for this name? if domain.lower() not in names.keys(): # use the default IP (if any) return names.get("default") return names.get(domain) def get_dns_txt(self, domain): """ Return a configured DNS TXT record (if any) """ def _read_txt_data(txt): path = txt.get("path") if path: path = normalize_response_path(path) with open(path, "rb") as f: return f.read() if not self.dns: return None txts = self.dns.get("txt", []) txt = [t for t in txts if t.get("name", "") == domain] if txt: return _read_txt_data(txt[0]) txt = [t for t in txts if t.get("name", "") == "default"] if txt: return _read_txt_data(txt[0]) def ip_lookup(self, ip): for item in self.dns: if item["response"] == ip: return item["query"] return None def new_wininet_inst(self, user_agent, access, proxy, bypass, flags): wini = WininetInstance(user_agent, access, proxy, bypass, flags) self.wininets.update({wini.get_handle(): wini}) return wini def get_wininet_object(self, handle): for hinst, inst in self.wininets.items(): if hinst == handle: return inst for hsess, sess in inst.sessions.items(): if hsess == handle: return sess for hreq, req in sess.requests.items(): if hreq == handle: return req def close_wininet_object(self, handle): if self.wininets.get(handle): self.wininets.pop(handle) def get_socket(self, fd): return self.sockets.get(fd) def close_socket(self, fd): self.sockets.pop(fd)
nilq/baby-python
python
from os.path import join, dirname, realpath from re import match def parse_input(): planties = set() with open(join(dirname(realpath(__file__)), "input.txt")) as f: # Parse initial config m = match("initial state: (?P<init>[#.]+)", f.readline()) for i, c in enumerate(m.group("init")): if c == '#': planties.add(i) # Empty line f.readline() # Rest of the lines are rules rule_dict = {} for rule in f: m = match("(?P<in>[#.]{5}) => (?P<out>[#.])", rule) rule_in = [True if c == '#' else False for c in m.group("in")] rule_out = True if m.group("out") == '#' else False rule_dict[tuple(rule_in)] = rule_out return planties, rule_dict def grow(p): new_p = set() for i in range(min(p)-2, max(p)+3): needed_rule = tuple([True if j in p else False for j in range(i-2, i+3)]) if rules[needed_rule]: new_p.add(i) return new_p plants, rules = parse_input() nr_of_generations = 50000000000 gen = 0 new_sum = previous_sum = 0 new_delta = previous_delta = 0 while gen < nr_of_generations: plants = grow(plants) gen += 1 previous_sum = new_sum new_sum = sum(plants) previous_delta = new_delta new_delta = new_sum - previous_sum if previous_delta == new_delta: print("[GENERATION {}] Delta stayed the same (at {})".format(gen, new_delta)) break final_sum = new_sum + (new_delta * (nr_of_generations - gen)) print("Sum of all plant indices is: {}".format(final_sum))
nilq/baby-python
python
#!/usr/bin/env python """ Generate an input MAF file for MutSigCV by converting SNV/INDEL calls by the Genomon2 pipeline. The input is a multi-sample variant call in post_analysis/ directory. Usage: python genomon2maf.py merge_mutation_filt.txt """ __author__ = "Masashi Fujita <mssfjt@gmail.com>" __version__ = "0.0.1" __date__ = "September 17, 2019" import sys import argparse import pandas as pd import numpy as np func_dict = { 'intronic': 'Intron', 'ncRNA_exonic': 'RNA', 'ncRNA_intronic': 'RNA', 'ncRNA_splicing': 'RNA', 'splicing': 'Splice_Site', 'UTR3': '3\'UTR', 'UTR5': '5\'UTR' } exonic_func_dict = { 'frameshift deletion': 'Frame_Shift_Del', 'frameshift insertion': 'Frame_Shift_Ins', 'nonframeshift deletion': 'In_Frame_Del', 'nonframeshift insertion': 'In_Frame_Ins', 'nonsynonymous SNV': 'Missense_Mutation', 'stopgain': 'Nonsense_Mutation', 'stoploss': 'Nonstop_Mutation', 'synonymous SNV': 'Silent' } def fetch_variant_classification(row): func = row['Func.refGene'] exonicFunc = row['ExonicFunc.refGene'] if func in 'exonic' and exonicFunc in exonic_func_dict: return exonic_func_dict[exonicFunc] elif func in func_dict: return func_dict[func] else: return np.nan def fetch_variant_type(row): ref = row['Reference_Allele'] alt = row['Tumor_Seq_Allele1'] if ref == "-": return "INS" elif alt == "-": return "DEL" else: return "SNP" # # parse args # parser = argparse.ArgumentParser(description='Convert Genomon2 SNV/INDEL calls to MAF.') parser.add_argument('infile', metavar='genomon_file', help='Genomon2 SNV/INDEL file for a single sample') parser.add_argument('--out', '-o', metavar='MAF', help='Output MAF file [default: stdout]') args = parser.parse_args() # # open input # df = pd.read_csv(args.infile, sep='\t', skiprows=3, dtype={'Chr': 'str'}, low_memory=False) # # format into MAF # df.rename(columns={ 'Gene.refGene': 'Hugo_Symbol', 'Chr': 'Chromosome', 'Start': 'Start_position', 'End': 'End_position', 'Ref': 'Reference_Allele', 'Alt': 'Tumor_Seq_Allele1', 'id': 'Tumor_Sample_Barcode' }, inplace=True) df['NCBI_Build'] = '37' df['Tumor_Seq_Allele2'] = df['Tumor_Seq_Allele1'] df['Variant_Classification'] = df.apply(lambda row: fetch_variant_classification(row), axis=1) df['Variant_Type'] = df.apply(lambda row: fetch_variant_type(row), axis=1) df = df[['Hugo_Symbol', 'NCBI_Build', 'Chromosome', 'Start_position', 'End_position', 'Variant_Classification', 'Variant_Type', 'Reference_Allele', 'Tumor_Seq_Allele1', 'Tumor_Seq_Allele2', 'Tumor_Sample_Barcode', 'ExonicFunc.refGene', 'Func.refGene']] # # save # if args.out is not None: f_out = open(args.out, 'w') else: f_out = sys.stdout df.to_csv(f_out, sep='\t', header=True, index=False, na_rep="NA") f_out.close()
nilq/baby-python
python
import asyncio from pypuss.app import Master async def main(): my_app = Master() await my_app.run() if __name__ == '__main__': print("[info]", "started running") loop = asyncio.get_event_loop() try: loop.run_until_complete(main()) except BaseException: print("[info]", "shutting down")
nilq/baby-python
python
from rest_framework import generics, status, permissions, mixins from .models import Post, Vote from .serializers import PostSerializer, VoteSerializer from .permissions import IsPostAuthorOrReadOnly from helpers.response import Response class PostView(generics.ListCreateAPIView): serializer_class = PostSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly, ) queryset = Post.objects.all() def perform_create(self, serializer): serializer.save(author=self.request.user) def list(self, request): queryset = self.get_queryset() serializer = PostSerializer(queryset, many=True) return Response({'posts': serializer.data, 'total': queryset.count()}, status=status.HTTP_200_OK) def create(self, request): serializer = PostSerializer(data=request.data) if serializer.is_valid(): self.perform_create(serializer) return Response(data=serializer.data, status=status.HTTP_201_CREATED) return Response(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) class PostDetailView(generics.RetrieveUpdateDestroyAPIView): serializer_class = PostSerializer permission_classes = (permissions.IsAuthenticated, IsPostAuthorOrReadOnly) queryset = Post.objects.all() lookup_url_kwarg = 'id' lookup_field = 'id' def get(self, request, id): try: post = Post.objects.get(id=id) serializer = PostSerializer(post) return Response(data=serializer.data, status=status.HTTP_200_OK) except Post.DoesNotExist: return Response(errors={'detail': 'post does not exist'}, status=status.HTTP_404_NOT_FOUND) def perform_update(self, serializer): serializer.save(author=self.request.user) def update(self, request, *args, **kwargs): partial = kwargs.pop('partial', False) instance = self.get_object() serializer = PostSerializer( instance, data=request.data, partial=partial) if serializer.is_valid(): self.perform_update(serializer) return Response(data=serializer.data, status=status.HTTP_200_OK) return Response(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST) def destroy(self, request, *args, **kwargs): instance = self.get_object() self.perform_destroy(instance) return Response(data={'detail': 'post deleted'}, status=status.HTTP_204_NO_CONTENT) class VoteView(generics.CreateAPIView, mixins.DestroyModelMixin): serializer_class = VoteSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly,) def get_queryset(self): voter = self.request.user post = Post.objects.get(id=self.kwargs['id']) return Vote.objects.filter(voter=voter, post=post) def perform_create(self, serializer): post = Post.objects.get(id=self.kwargs['id']) serializer.save(voter=self.request.user, post=post) def create(self, request, *args, **kwargs): serializer = VoteSerializer(data=request.data) if self.get_queryset().exists(): return Response(errors={'detail': 'you have already voted for this post'}, status=status.HTTP_400_BAD_REQUEST) if serializer.is_valid(): self.perform_create(serializer) return Response(data=serializer.data, status=status.HTTP_201_CREATED) def delete(self, request, *args, **kwargs): if self.get_queryset().exists(): self.get_queryset().delete() return Response(data={'detail': 'vote deleted'}, status=status.HTTP_204_NO_CONTENT) return Response(errors={'detail': 'you have not voted for this post'}, status=status.HTTP_400_BAD_REQUEST)
nilq/baby-python
python
import numpy as np import xarray as xr from constants import R_earth from xr_DataArrays import dll_from_arb_da def xr_int_global(da, AREA, DZ): """ global volume integral *[m^3] """ (z, lat, lon) = dll_from_arb_da(da) return (da*AREA*DZ).sum(dim=[z, lat, lon]) # 0D def xr_int_global_level(da, AREA, DZ): """ global volume integral *[m^3] """ (z, lat, lon) = dll_from_arb_da(da) return (da*AREA*DZ).sum(dim=[lat, lon]) # 1D (z) def xr_int_vertical(da, DZ): """ vertical integral *[m] """ (z, lat, lon) = dll_from_arb_da(da) return (da*DZ).sum(dim=z) # 2D (lat, lon) def xr_int_zonal(da, HTN, LATS, AREA, DZ): """ integral along depth and zonal coordinates *[m^2] rectangular grid""" shape = np.shape(da) assert shape[-2:]==np.shape(HTN)[-2:] assert shape[-2:]==np.shape(DZ)[-2:] (z, lat, lon) = dll_from_arb_da(da) if shape[-1] in [900, 320]: # rectangular `ocn_low` or `ocn_rect` grid # print(np.shape(da)) # print(np.shape(HTN)) # print(np.shape(DZ)) int_zonal = (da*HTN*DZ).sum(dim=[z, lon]) # 1D (lat) elif shape[-1]==3600: # tripolar grid int_vert = xr_int_vertical(da, DZ) # 2D int_zonal = xr_zonal_int_bins(int_vert, LATS, AREA) return int_zonal def xr_int_zonal_level(da, HTN, LATS, AREA, DZ, dx=1): """ zonal integrals for each level *[m] rectangular grid""" (z, lat, lon) = dll_from_arb_da(da) shape = np.shape(da) assert shape[-2:]==np.shape(HTN)[-2:] assert shape[-2:]==np.shape(DZ)[-2:] if shape[-1] in [900, 320]: # rectangular grid int_zonal_level = (da*HTN).sum(dim=[lon]) # 2D (z, lat) elif shape[-1]==3600: # tripolar grid lat_bins, lat_centers, lat_width = lat_binning(dx) km = len(da[z]) dz = DZ.max(dim=(lon,lat)) # construct new xr DataArray # assert 'time' in da.coords lat_bin_name = f'TLAT_bins' if da.coords['time'].size==1: # single time files array = np.zeros((km, len(lat_centers))) coords = {z: da.coords[z], lat_bin_name: lat_centers} int_zonal_level = xr.DataArray(data=array, coords=coords, dims=(z, lat_bin_name)) for k in range(km): da_k = (da[k,:,:]*DZ[k,:,:]).drop('z_t') int_zonal_level[k,:] = xr_zonal_int_bins(da_k, LATS, AREA)/dz[k] else: array = np.zeros((da.coords['time'].size, km, len(lat_centers))) coords = {'time': da.coords['time'], z: da.coords[z], lat_bin_name: lat_centers} int_zonal_level = xr.DataArray(data=array, coords=coords, dims=('time', z, lat_bin_name)) for k in range(km): da_k = (da[:,k,:,:]*DZ[k,:,:]).drop('z_t') int_zonal_level[:,k,:] = xr_zonal_int_bins(da_k, LATS, AREA)/dz[k] return int_zonal_level def xr_zonal_int_bins(da, LATS, AREA, dx=1): """ integral over dx wide latitude bins integrates da with AREA, then divides by width of zonal strip dx input: da .. 2D xr DataArray to be "zonally" integrated LATS .. 2D xr DataArray latitude values of each cell AREA .. 2D xr DataArray dx .. width of latitude bands in degrees lat_name .. xa/AREA coordinate name of the latitude variable output: xa_zonal_int .. 1D xr DataArray lat centers can be accessed through xa_zonal_int.coords[f'{lat_name}_bins'] """ assert type(da)==xr.core.dataarray.DataArray assert type(AREA)==xr.core.dataarray.DataArray assert np.shape(da)[-2:]==np.shape(AREA) (z, lat, lon) = dll_from_arb_da(da) lat_bins, lat_centers, lat_width = lat_binning(dx) da_new = da*AREA da_zonal_int = da_new.groupby_bins(LATS, lat_bins, labels=lat_centers).sum(dim=f'stacked_{lat}_{lon}')/lat_width return da_zonal_int def lat_binning(dx): """ create latitude bins """ lat_width = dx*R_earth*np.pi/180 lat_bins = np.arange(-90, 90+dx, dx) lat_centers = np.arange(-90+dx/2, 90, dx) return lat_bins, lat_centers, lat_width def xr_vol_int(xa, AREA, DZ, levels=False, zonal=False): """ volume integral of xarray *[m^3] input: xa .. 3D xr DataArray with data to be integrated AREA .. 2D xr DataArray of cell areas DZ .. 3D xr DataArray of cell depths levels .. option to output results for all level zonal .. option to output zonal integrals output: integral .. float integral int_levels .. integrals of each level xa_zonal_int .. 1D array of vert.+zonally integrated quantity xa_zonal_level_int .. 2D (km, lat_bin) *[m^2] (integrated in depth and lon) xa_zonal_level_mean .. 2D (km, lat_bin) *[m^1] (weighted by bottom cell depth) """ assert type(xa)==xr.core.dataarray.DataArray assert len(np.shape(xa))==3 assert type(AREA)==xr.core.dataarray.DataArray assert type(DZ)==xr.core.dataarray.DataArray assert np.shape(AREA)==np.shape(xa)[-2:] assert np.shape(DZ)==np.shape(xa)[-3:] if zonal==True: dx = 1 # latitude bin width if np.shape(DZ)==(2,3,4): # simple test case lat_name = 'y' elif np.shape(DZ)==(42,2400,3600): # hires ocean lat_name = 'nlat' elif np.shape(DZ)==(30,384,576): # atm fields lat_name = 'lat' else: raise ValueError('unknown shape: lat_name not implemented') assert lat_name in DZ.coords if levels==False: integral = np.sum(xa[:,:,:]*AREA[:,:]*DZ[:,:,:]).item() if zonal==False: # just global integral return integral elif zonal==True: xa_vert = xr_int_along_axis(xa, DZ, 0) xa_zonal_int = xr_zonal_int(xa_vert, AREA, dx, lat_name) return integral, xa_zonal_int elif levels==True: km = len(xa[:,0,0]) int_levels = np.zeros((km)) for k in range(km): int_levels[k] = np.sum(xa[k,:,:]*AREA[:,:]*DZ[k,:,:]).item() integral = np.sum(int_levels) if zonal==False: return integral, int_levels if zonal==True: ONES = AREA.copy() ONES[:,:] = 1. for k in range(km): xa_zonal_int = xr_zonal_int(xa[k,:,:]*DZ[k,:,:], AREA, dx, lat_name) DZ_zonal_int = xr_zonal_int(DZ[k,:,:] , ONES, dx, lat_name) if k==0: xa_zonal_level_int = np.zeros((km, len(xa_zonal_int))) xa_zonal_level_mean = np.zeros((km, len(xa_zonal_int))) xa_zonal_level_int[k,:] = xa_zonal_int xa_zonal_level_mean[k,:] = xa_zonal_int/DZ_zonal_int return integral, int_levels, xa_zonal_level_int, xa_zonal_level_mean def xr_int_along_axis(xa, DZ, axis): """ integral of xr DataArray along a specific axis input: xa .. 3D xr DataArray of quantity to be integrated DZ .. 3D xr DataArray of vertical cell extents [m] axis .. int axis to be integrated over output: int .. 2D xr DataArray of integrated quantitity """ assert type(axis)==np.dtype(int) or axis in xa.dims assert np.shape(xa)==np.shape(DZ) assert axis<=len(np.shape(xa)) integral = np.sum(xa*DZ, axis=axis) return integral def xr_vol_int_regional(xa, AREA, DZ, MASK): """ volumen integral with regional MASK input: xa, AREA, DZ .. same as in 'xr_vol_int' MASK .. 2D xr DataArray of booleans with the same dimensions as xa output: integral, int_levels .. same as in 'xr_vol_int' """ assert type(xa)==xr.core.dataarray.DataArray assert type(AREA)==xr.core.dataarray.DataArray assert type(DZ)==xr.core.dataarray.DataArray assert np.shape(AREA)==np.shape(xa)[-2:] assert np.shape(DZ)==np.shape(xa)[-3:] assert np.dtype(MASK)==np.dtype('bool') # determine min/max i/j of masked region (imin, imax, jmin, jmax) = find_regional_coord_extent(MASK) xa_reg = xa.where(MASK)[:,jmin:jmax+1,imin:imax+1] AREA_reg = AREA.where(MASK)[jmin:jmax+1,imin:imax+1] DZ_reg = DZ.where(MASK)[:,jmin:jmax+1,imin:imax+1] integral, int_levels = xr_vol_int(xa_reg, AREA_reg, DZ_reg) return integral, int_levels def find_regional_coord_extent(MASK): """ finds coordinates of a boolean mask input: MASK .. 2D xr DataArray of booleans output: (imin, imax, jmin, jmax) .. lon/lat extent of True area """ assert type(MASK)==xr.core.dataarray.DataArray jmin = np.where(MASK)[0].min() jmax = np.where(MASK)[0].max() imin = np.where(MASK)[1].min() imax = np.where(MASK)[1].max() return (imin, imax, jmin, jmax) def xr_vol_mean(xa, AREA, DZ): """ mean over quantity stored in xa input: xa .. 3D xr DataArray of quantity AREA .. 2D xr DataArray of cell surface area DZ .. 3D xr DataArray of cell depths output: mean .. (float) """ assert type(xa)==xr.core.dataarray.DataArray assert type(DZ)==xr.core.dataarray.DataArray assert type(AREA)==xr.core.dataarray.DataArray assert np.shape(xa)==np.shape(DZ) assert np.shape(xa[0,:,:])==np.shape(AREA) integral = xr_vol_int(xa, AREA, DZ, levels=False, zonal=False) ONES = xa.copy() ONES[:,:,:] = 1. volume = xr_vol_int(ONES, AREA, DZ, levels=False, zonal=False) mean = integral/volume return mean def xr_surf_int(xa, AREA): """ surface integral of xarray DataArray *[m^2] input: xa .. 2D xr DataArray AREA .. 2D xr DataArray of surface area output: integral .. float integrated """ assert type(xa)==xr.core.dataarray.DataArray assert type(AREA)==xr.core.dataarray.DataArray assert np.shape(xa)==np.shape(AREA) assert len(np.shape(xa))==2 integral = np.sum(xa*AREA) return integral.item() def xr_surf_mean(xa, AREA): """ mean over a surface *[1] input: xa .. 2D xr DataArray of quantity AREA .. 2D xr DataArrayof cell surfaces output: mean .. (float) mean of quantity in xa """ assert type(xa)==xr.core.dataarray.DataArray assert type(AREA)==xr.core.dataarray.DataArray assert np.shape(xa)==np.shape(AREA) assert len(np.shape(xa))==2 integral = xr_surf_int(xa, AREA) ONES = xa.copy() ONES[:,:] = 1. surface = xr_surf_int(ONES, AREA) mean = integral/surface return mean def xr_zonal_mean(xa, AREA, dx, lat_name): """ area weighted mean over dx wide latitude bins input: xa .. 2D xr DataArray AREA .. 2D xr DataArray dx .. width of latitude bands lat_name .. xa/AREA coordinate name of the latitude variable output: xa_zonal_mean .. 1D xr DataArray """ assert type(xa)==xr.core.dataarray.DataArray assert type(AREA)==xr.core.dataarray.DataArray assert len(np.shape(xa))==2 assert np.shape(xa)==np.shape(AREA) assert dx>180/len(AREA[0,:]) xa_zonal_int = xr_zonal_int(xa, AREA, dx, lat_name) AREA_zonal_int = xr_zonal_int(AREA/AREA, AREA, dx, lat_name) xa_zonal_mean = xa_zonal_int/AREA_zonal_int return xa_zonal_mean
nilq/baby-python
python
class Packet_Ops(): def __init__( self ): self.frame_id = 1 self.rx_packet_table = { 0x88: self.parse_command_resp, 0x8a: self.parse_modem_status, 0x8b: self.parse_tx_status, 0x8d: self.parse_route_information, 0x8e: self.aggreate_address_update, 0x90: self.parse_rx_indicator, 0x91: self.parse_rx_explict_indicator, 0x92: self.parse_sample_rx_indicator, 0x95: self.parse_node_identifier, 0x97: self.parse_remote_commad_resp } def construct_packet( self, input_data ): #data is a length return_value = [] return_value.append(0x7e) length = len(input_data) return_value.append( (length >>8 ) & 0xff ) return_value.append( length & 0xff ) sum = 0 for i in input_data: return_value.append(i) sum = sum + i return_value.append((0xff-sum)&0xff) return return_value def generate_AT_cmd( self, command, parameters = None, frame_id = None ): data = [] if frame_id == None: frame_id = self.frame_id else: self.frame_id = frame_id data.append( 0x8) data.append( frame_id ) data.append(ord(command[0] )) data.append(ord(command[1])) if parameters != None: data.extend(parameters) self.frame_id = self.frame_id + 1 return self.construct_packet(data) def generate_ATQueue_cmd( self, command, parameters = None, frame_id = None ): data = [] if frame_id == None: frame_id = self.frame_id else: self.frame_id = frame_id data.append( 0x9) data.append( frame_id ) data.append(ord(command[0] )) data.append(ord(command[1])) if parameters != None: data.extend(parameters) self.frame_id = self.frame_id + 1 return self.construct_packet(data) def generate_TX_req( self, destination_address, tx_data, broadcast_radius = 0, transmit_option = 0, frame_id = None ): data = [] if frame_id == None: frame_id = self.frame_id else: self.frame_id = frame_id data.append( 0x10) data.append( frame_id ) data.extend( destination_address ) data.extend( [0xff, 0xfe] ) data.append( broadcast_radius ) data.append( transmit_option ) data.extend(tx_data) self.frame_id = self.frame_id + 1 return self.construct_packet(data) def generate_Explicit_Tx_cmd( self, destination_address, source_endpoint, dest_endpoint, cluster_id, profile_id, tx_data, broadcast_radius = 0, transmit_option = 0, frame_id = None ): data = [] if frame_id == None: frame_id = self.frame_id else: self.frame_id = frame_id data.append( 0x11) data.append( frame_id ) data.extend( destination_address) data.extend( [ 0xff, 0xfe ] ) data.append( source_endpoint) data.append( dest_endpoint ) data.extend( cluster_id) data.extend( profile_id) data.append( broadcast_radius ) data.append( transmit_option ) data.extend(tx_data) self.frame_id = self.frame_id + 1 return self.construct_packet(data) def generate_Remote_cmd( self,destination_address, command, parameters = None, frame_id = None, update_flag = True ): data = [] if frame_id == None: frame_id = self.frame_id else: self.frame_id = frame_id data.append( 0x17) data.append( frame_id ) data.extend( destination_address) data.extend( [ 0xff , 0xfe ] ) if update_flag == True: data.append(2) else: data.append(0) data.append(ord(command[0] )) data.append(ord(command[1])) if parameters != None: data.extend(parameters) self.frame_id = self.frame_id + 1 return self.construct_packet(data) def parse_command_resp( self, data ): return_value = {} return_value["frame_type"] = data[0] return_value["frame_id"] = data[1] return_value["at_command"] = [ chr(data[2]),chr(data[3]) ] return_value["command_status"] = data[4] if len(data) >= 5: return_value["command_data"] = data[5:] else: return_value["command_data"] = None return return_value def parse_modem_status( self, data ): return_value = {} return_value["frame_type"] = data[0] return_value["status"] = data[1] return return_value def parse_tx_status( self, data ): return_value = {} return_value["frame_type"] = data[0] return_value["frame_id"] = data[1] return_value["retry_count"] = data[4] return_value["delivery_status"] = data[5] return_value["discovery_status"] = data[6] return return_value def parse_route_information( self, data ): return_value = {} return_value["frame_type"] = data[0] return_value["source_event"] = data[1] return_value["length"] = data[2] return_value["time_stamp"] = data[3:7] return_value["ack_timeout_count"] = data[7] return_value["addresses"] = [] number = data[2] -7 address_number = number/8 index_start = 10 for i in range( 0,address_number): return_value["addresses"].append( data[index_start : index_start+8 ] ) index_start = index_start+8 return return_value def aggreate_address_update( self, data ): return_value = {} return_value["frame_type"] = data[0] return_value["format_id"] = data[1] return_value["new_address"] = data[2:10] return_value["old_address"] = data[10:18] return return_value def parse_rx_indicator( self, data ): return_value = {} return_value["frame_type"] = data[0] return_value["address"] = data[1:9] return_value["receive_option"] = data[11] return_value["data"] = data[12:] return return_value def parse_rx_explict_indicator( self, data ): return_value = {} return_value["frame_type"] = data[0] return_value["address"] = data[1:9] return_value["source_endpoint"] = data[11] return_value["destination_endpoint"] = data[12] return_value["cluster_id"] = data[13] return_value["profile_id"] = data[14:16] return_value["receive_options"] = data[16] return_value["data"] = data[17:] return return_value #implemented later def parse_sample_rx_indicator( self, data ): pass ### implinented later def parse_node_identifier( self,data ): pass def parse_remote_commad_resp( self, data ): return_value = {} return_value["frame_type"] = data[0] return_value["frame_id"] = data[1] return_value["address"] = data[2:10] return_value["at_command"] = [ chr(data[12]), chr(data[13]) ] return_value["command_status"] = data[14] return_value["command_data"] = data[15:] return return_value def parse_packet( self,data ): #print "parse_packet data",data if self.rx_packet_table.has_key( data[0] ): return [ True, self.rx_packet_table[data[0] ](data) ] else: return [ False]
nilq/baby-python
python
from importlib import import_module import six from six.moves import reload_module from typing import Optional, Union # noqa from configloader import ConfigLoader from wrapt import ObjectProxy """ Usage: from flexisettings.conf import settings There are two important env vars: `<inital_namespace>_CONFIG_NAMESPACE` Sets the prefix used for loading further config values from env and config file. Set when instantiating the `Settings` object. `<inital_namespace>_APP_CONFIG` Import path to a python object to load futher config values from. Defaults to None. e.g. 'django.conf.settings' or 'celeryconfig'. Although we can load further keys from the env, now prefixed using our custom namespace, by preference all further keys should be loaded from a python obj because all values loaded from env will be strings, there is no way to automatically type cast them. Keys in the config obj must be prefixed with the namespace, but will be provided in `settings` without the prefix. e.g. if you set your env to: MYAPP_CONFIG_NAMESPACE=MYAPP_EVENTS MYAPP_APP_CONFIG=django.conf.settings and in your Django settings you have: MYAPP_EVENTS_SERIALIZER = 'json' then: from event_consumer.conf import settings print(settings.SERIALIZER) > json """ if six.PY2: import_error_cls = ImportError else: import_error_cls = ModuleNotFoundError # noqa class Settings(ObjectProxy): def __init__(self, initial_namespace=None, defaults=None): # type: (Optional[str], Optional[str]) -> None self._self_initial_namespace = initial_namespace self._self_defaults = defaults config = _load_config(initial_namespace, defaults) super(Settings, self).__init__(config) def __dir__(self): base = super(Settings, self).__dir__() return base + list(self.keys()) def _reload(self): # type: () -> None if self._self_defaults: # we have to reload the `defaults` module otherwise # changed values (e.g. loaded from env var) won't show up try: module = import_module(self._self_defaults) except import_error_cls: # must have been a '{module.attr}' import path module = import_module(self._self_defaults.rsplit('.', 1)[0]) reload_module(module) self._replace_wrapped( _load_config(self._self_initial_namespace, self._self_defaults) ) def _replace_wrapped(self, new): # type: (ConfigLoader) -> None self.__wrapped__ = new def _load_config(initial_namespace=None, defaults=None): # type: (Optional[str], Optional[str]) -> ConfigLoader """ Kwargs: initial_namespace: defaults: """ # load defaults if defaults: config = ConfigLoader() config.update_from_object(defaults) namespace = getattr(config, 'CONFIG_NAMESPACE', initial_namespace) app_config = getattr(config, 'APP_CONFIG', None) # load customised config if app_config: if namespace is None: config.update_from_object(app_config) else: _temp = ConfigLoader() _temp.update_from_object(app_config, lambda key: key.startswith(namespace)) config.update(_temp.namespace(namespace)) return config
nilq/baby-python
python
""" Contains a class that converts the pre-processed binary file into a numpy array. The logic behind each conversion step is thoroughly document through comments in the code. """ import pickle import os import struct import logging import hashlib import numpy as np from tqdm import tqdm import matplotlib class Preprocessor(): """ Reads a pre-processed binary file (see README) into a pretty numpy array, which can be feed to a ML model. :param settings: a dictionary of data-related simulation settings """ def __init__(self, settings): self.input_file = settings['input_file'] self.preprocessed_file = settings['preprocessed_file'] self.run_sanity_checks = settings['run_sanity_checks'] # Inputs that dictate the dataset ID: self.max_time = settings['max_time'] self.sample_freq = settings['sample_freq'] self.beamformings = settings['beamformings'] self.power_offset = settings['power_offset'] self.power_scale = settings['power_scale'] self.pos_grid = settings['pos_grid'] self.pos_shift = settings['pos_shift'] self.keep_timeslots = settings['keep_timeslots'] # Precomputes other needed variables, and initializes others self.dataset_id = get_dataset_id(settings) self.time_slots = self.max_time * self.sample_freq self.features_size = self.time_slots * self.beamformings self.features = None self.labels = None def check_existing_dataset(self): """ Checks whether the dataset we are trying to create already exists :returns: Boolean flag, with `True` meaning that the dataset already exists """ dataset_exists = False if os.path.isfile(self.preprocessed_file): with open(self.preprocessed_file, 'rb') as dataset_file: _, _, target_dataset_id = pickle.load(dataset_file) if target_dataset_id == self.dataset_id: dataset_exists = True return dataset_exists def create_bff_dataset(self): """ Creates a BFF experiments-ready dataset. The dataset contains `X`, the matrix containing the received radiation, and `y`, the true position for that received radiation. :returns: `X` and `y`. `X` is a matrix with dimentions (number_of_positions x radiation_samples_per_position). y is matrix with dimentions (number_of_positions x 2) The radiation samples per position's size is given by the number of used beamformings times the sampling frequency time the receiving time per beamforming """ labels_size = 2 sample_size = int(self.features_size + labels_size) # Converts the dataset into features/labels logging.info("Converting the dataset into features/labels...") features, labels = self._data_to_dataset(sample_size) # Converting the features/labels into numpy arrays logging.info("Converting features/labels into numpy arrays...") self.features = np.array(features) self.labels = np.array(labels) del features, labels # Printing the ranges of the features logging.info("[label] x range: %s - %s", self.labels[:, 0].min(), self.labels[:, 0].max()) logging.info("[label] y range: %s - %s", self.labels[:, 1].min(), self.labels[:, 1].max()) logging.info("[features] power range: %s - %s", self.features[:].min(), self.features[:].max()) # Removes unwanted timeslots self._delete_timeslots() # Removes dataless positions self._remove_dataless_positions() def _data_to_dataset(self, sample_size): """ `create_bff_dataset` auxiliary function. Converts the raw (floating point) input data into features and labels, that will be further filtered. The features that come out of this operation should have a range close to [0, 1] -- please set the simulation parameters accordingly. :param sample_size: length of the input data for each position in the dataset """ # Unpacks stored variables x_shift, y_shift = self.pos_shift x_grid, y_grid = self.pos_grid if x_grid != y_grid: logging.warning("WARNING: the area on which the experiments are going to be " "performes is not square. The distance metric during the model training " "will be ill defined. (e.g. the validation error in meters will not be correct)" "\nPlease keep that in mind (or add that exception to the code :D).") # Loads the binary mode dataset (the step that creates this binary data will # be rewritten in python in the near future) logging.info("Loading the binary dataset from %s...", self.input_file) with open(self.input_file, mode='rb') as file: data_binary = file.read() # Converts the binary dataset into float 32 logging.info("Converting the binary data to float_32...") logging.info("[** this may take a couple of minutes and it will not print any progress **]") binary_size = os.path.getsize(self.input_file) size_bytes = int(binary_size/4) data = struct.unpack('f'*size_bytes, data_binary) del data_binary num_samples = int(size_bytes / sample_size) features = [] labels = [] # For each sample in the data logging.info("Creating features and labels from the float_32 data...") for sample_idx in tqdm(range(num_samples)): tmp_features = [] tmp_labels = [] data_start_pos = sample_idx * sample_size # For each data item in the sample # (0 = Position data - X) # (1 = Position data - Y) # (2, ..., sample_size-1 = Feature data) for data_idx in range(sample_size): item = data[data_start_pos + data_idx] if data_idx == 0: item += x_shift item /= x_grid tmp_labels.append(item) elif data_idx == 1: item += y_shift item /= y_grid tmp_labels.append(item) else: # Important notes regarding feature data: # 1) item == 0 -> there is no data here (there are no values > 0) # 2) The check for the minimum power threshold (e.g. -100 dBm) is performed # after the noise is added, not here. # 3) Nevertheless, to speed up downstream operations code, filters out values with # very little power. For the default simulation parameters, this filters samples # with less than -170 dBm. Since the default "minimum_power" is -125 dBm [check # an example for the meaning of this variable], this means we can reliably test # (log-normal) noises with STD up to 15 dB [margin = (-125) - -170 = 45 dB = # 3*STD of 15 dB] if -(self.power_offset) < item < 0: tmp_features.append((item + self.power_offset) * self.power_scale) else: assert item <= 0.0, "There cannot be any value here above 0.0 (got {})"\ .format(item) tmp_features.append(0.0) features.append(tmp_features) labels.append(tmp_labels) return features, labels def _delete_timeslots(self): """ Removes unwanted timeslots (Keep in mind that this feature's usefulness is super dataset-dependent! In my experiments, I removed the timeslots with very little data, corresponding to less than 1% of the non-zero features) """ if self.keep_timeslots: logging.warning("Removing unwanted timeslots (keeping timeslots with indexes between" " '%s' and '%s')", self.keep_timeslots[0], self.keep_timeslots[1]-1) mask = np.ones(self.features.shape[1], dtype=bool) ts_to_keep = [ts for ts in range(*self.keep_timeslots)] ts_to_delete = [ts for ts in range(self.time_slots) if ts not in ts_to_keep] logging.info("Time slots to remove: %s", ts_to_delete) for idx in tqdm(range(self.features.shape[1])): # DIM 1 = BF, DIM 2 = TS if idx % self.time_slots in ts_to_delete: mask[idx] = False # Removes those slots from the data logging.info("Shape before TS reduction: %s", self.features.shape) self.features = self.features[:, mask] logging.info("Shape after TS reduction: %s", self.features.shape) def _remove_dataless_positions(self): """ Removes invalid [x, y] positions (invalid positions = positions with no data, i.e. only zeroes) """ logging.info("Detecting the invalid (data-less) positions... ") mask = np.ones(self.features.shape[0], dtype=bool) removed_pos = 0 for idx in tqdm(range(self.features.shape[0])): if sum(self.features[idx, :]) == 0: mask[idx] = False removed_pos += 1 self.features = self.features[mask, :] self.labels = self.labels[mask, :] logging.info("%s data-less positions removed.", removed_pos) def store_dataset(self): """ Stores the result of data preprocessing """ # Final data reports logging.info("Usable positions: %s", self.features.shape[0]) target_folder = os.path.split(self.preprocessed_file)[0] if not os.path.exists(target_folder): logging.info("Target folder (%s) not found, creating it...", target_folder) os.makedirs(target_folder) logging.info("Storing the result ...") with open(self.preprocessed_file, 'wb') as data_file: pickle.dump([self.features, self.labels, self.dataset_id], data_file) # Optional: plots the existing data points on a 2D image if self.run_sanity_checks: logging.info("Preparing plot to double-check existing data points...") # Creates (N+1) by (M+1) matrix. This means that its indexes go from 0 through N/M to_plot = np.full([int(self.pos_grid[0]) + 1, int(self.pos_grid[1]) + 1], 0.0) for pos_idx in tqdm(range(self.labels.shape[0])): # Scales 0-1 to 0-N/M pos_x = int(round(self.labels[pos_idx, 0] * self.pos_grid[0])) pos_y = int(round(self.labels[pos_idx, 1] * self.pos_grid[1])) # Flips Y (to correctly plot with imshow) to_plot[pos_x, int(round(self.pos_grid[1] - pos_y))] = 1.0 # Local import to avoid messing non-gaphical interfaces matplotlib.use('agg') import matplotlib.pyplot as plt plt.imshow(np.transpose(to_plot)) image_locaton = os.path.join(target_folder, 'existing_data_points.pdf') plt.savefig(image_locaton) logging.info("Done! (Check %s)", image_locaton) def load_dataset(self): """ Loads the previously stored dataset, returning it :returns: previously stored features and labels """ assert self.check_existing_dataset(), "The dataset with the specified path ({}) either "\ "does not exists or was built with different simulation settings. Please run the "\ "data preprocessing step with the new simulation settings!".format( self.preprocessed_file) with open(self.preprocessed_file, 'rb') as dataset_file: features, labels, _ = pickle.load(dataset_file) return features, labels def get_dataset_id(settings): """ Creates and returns an unique ID (for practical purposes), given the data parameters. The main use of this ID is to make sure we are using the correct data source, and that the data parameters weren't changed halfway through the simulation sequence. """ hashing_features = [ settings['max_time'], settings['sample_freq'], settings['beamformings'], settings['power_offset'], settings['power_scale'], settings['pos_grid'], settings['pos_shift'], settings['keep_timeslots'], ] hash_sha256 = hashlib.sha256() for feature in hashing_features: if isinstance(feature, list): inner_features = feature else: inner_features = [feature] for item in inner_features: if isinstance(item, float): item = "{:.4f}".format(item) hash_sha256.update(bytes(item, encoding='utf8')) else: hash_sha256.update(bytes(item)) return hash_sha256.hexdigest()
nilq/baby-python
python
# ConnexionApiGenerator.py - Creates an object implementing a Connexion API import os from jinja2 import Environment, Template, FileSystemLoader from smoacks.sconfig import sconfig class ConnexionApiGenerator: def __init__(self, app_object): self._app_object = app_object self.name = self._app_object.name def getJinjaDict(self): # Establish constant values and the overall dictionary structure result = { 'name': self.name, 'idList': None, 'pkList': None, 'idCount': self._app_object._idCount, 'hasSearch': False } # Loop through the properties and update the structure where needed properties = self._app_object.getAllProperties() for prop in properties: if prop.isId: result['name_id'] = prop.name if not result['idList']: result['idList'] = prop.name result['idStrings'] = "'" + prop.name + "'" result['pkList'] = self.name + '.' + prop.name else: result['idList'] += ', ' + prop.name result['idStrings'] += ", '" + prop.name + "'" result['pkList'] += ', ' + self.name + '.' + prop.name if prop.searchField: result['hasSearch'] = True result['search_field'] = prop.name if self._app_object._idCount == 1: result['primary_keys'] = result['pkList'] result['id_list'] = result['idList'] result['id_strings'] = result['idStrings'] else: result['primary_keys'] = '[' + result['pkList'] + ']' result['id_list'] = '[' + result['idList'] + ']' result['id_strings'] = '[' + result['idStrings'] + ']' result.update(sconfig['env_defaults']) return result def render(self): env = Environment( loader = FileSystemLoader('templates') ) template = env.get_template('ConnexionAPIs.jinja') filedir = os.path.join(sconfig['structure']['root'], sconfig['structure']['apiobjectdir']) if not os.path.isdir(filedir): os.makedirs(filedir, exist_ok=True) outfilename = os.path.join(filedir, "{}s.py".format(self._app_object.getSnakeName())) if not os.path.isfile(outfilename): outfile = open(outfilename, "w") outfile.write(template.render(self.getJinjaDict())) outfile.close()
nilq/baby-python
python
"""PyTest fixtures and helper functions, etc.""" import pprint import uuid from configparser import ConfigParser from configparser import ExtendedInterpolation from inspect import getframeinfo from pathlib import Path import pytest # ========================================================= # H E L P E R S # ========================================================= class Helpers: """Generic helper class. This class provides utility methods that can be accessed from within test fumnctions. """ @staticmethod def pp(capsys, data, frame=None): """(Magic) Pretty Print function.""" with capsys.disabled(): _PP_ = pprint.PrettyPrinter(indent=4) print("\n") if frame is not None: print(f"LINE #: {getframeinfo(frame).lineno}\n") _PP_.pprint(data) # ========================================================= # G L O B A L P Y T E S T F I X T U R E S # ========================================================= _KWD_TEST_SCTN_ = "test_sctn" # NOTE: ConfigParser converts all keys to _KWD_TEST_KEY_ = "test_key" # lower case as they follow .ini syntax _KWD_TEST_VAL_ = "test_val" # rules for key attributes _DEFAULT_ATTRIBS_DICT_ = { _KWD_TEST_KEY_: _KWD_TEST_VAL_, "k11": "v11", "k12": "v12", } _DEFAULT_CONFIG_DICT_ = {_KWD_TEST_SCTN_: _DEFAULT_ATTRIBS_DICT_} _DEFAULT_CONFIG_STR_ = ( f"{_KWD_TEST_SCTN_}|{_KWD_TEST_KEY_}:{_KWD_TEST_VAL_},k11:v11,k12:v12" ) _DEFAULT_CHANNELS_STR_ = "f451_twitter|f451_slack" _DEFAULT_TEST_SECRETS_ = { "f451_mailgun": { "priv_api_key": "_YOUR_PRIVATE_API_KEY_", "publ_val_key": "_YOUR_PUBLIC_API_KEY_", "webhook_sign_key": "_YOUR_WEBHOOK_SIGNING_KEY_", "from_domain": "_YOUR_DOMAIN_NAME_", }, "f451_slack": { "signing_secret": "_YOUR_SLACK_SIGNING_SECRET_", "auth_token": "_YOUR_SLACK_AUTH_TOKEN_", "app_token": "_YOUR_SLACK_APP_TOKEN_", }, "f451_twilio": { "acct_sid": "_YOUR_TWILIO_SID_", "auth_token": "_YOUR_TWILIO_TOKEN_", "from_phone": "_YOUR_TWILIO_FROM_PHN_", }, "f451_twitter": { "user_key": "_YOUR_TWITTER_USER_KEY_", "user_secret": "_YOUR_TWITTER_SECRET_KEY_", "auth_token": "_YOUR_TWITTER_AUTH_TOKEN_", "auth_secret": "_YOUR_TWITTER_AUTH_SECRET_", }, } _DEFAULT_TEST_CONFIG_ = { "f451_main": { "channels": _DEFAULT_CHANNELS_STR_, "channel_map": "email:f451_mailgun|sms:f451_twilio|twitter:f451_twitter|slack:f451_slack|forums:f451_slack", # noqa: B950 }, "f451_mailgun": { "from_name": "_DEFAULT_FROM_NAME_", "from_email": "email@example.com", }, "f451_slack": { "from_name": "_DEFAULT_FROM_NAME_", }, "f451_twitter": {}, "f451_twilio": { "to_phone": "_DEFAULT_TO_PHONE_", }, } @pytest.fixture() def default_test_section(): """Return default test values.""" return _KWD_TEST_SCTN_ @pytest.fixture() def default_test_key(): """Return default test values.""" return _KWD_TEST_KEY_ @pytest.fixture() def default_test_val(): """Return default test values.""" return _KWD_TEST_VAL_ @pytest.fixture() def valid_config(): """Return valid config values.""" parser = ConfigParser(interpolation=ExtendedInterpolation()) parser.read_dict(_DEFAULT_CONFIG_DICT_) return parser @pytest.fixture() def valid_config_dict(): """Return valid config values as `dict`.""" return _DEFAULT_CONFIG_DICT_ @pytest.fixture() def valid_config_string(): """Return valid config values as `str`.""" return _DEFAULT_CONFIG_STR_ @pytest.fixture() def valid_attribs_dict(): """Return attributes.""" return _DEFAULT_ATTRIBS_DICT_ @pytest.fixture() def new_config_file(tmpdir_factory): """Only create the filename, but not the actual file.""" configFile = tmpdir_factory.mktemp("test").join(f"{uuid.uuid4().hex}.ini") configFile.write("[section]\nkey = value") return str(configFile) @pytest.fixture(scope="session") def new_attachment_file(tmpdir_factory): """Create an actual dummy file.""" testFile = tmpdir_factory.mktemp("test").join(f"{uuid.uuid4().hex}.txt") testFile.write("THIS IS A TEST FILE") return str(testFile) @pytest.fixture(scope="session") def new_media_file(): """Link to an actual test image file.""" testFile = Path("test/test-image-small.gif") return str(testFile) @pytest.fixture() def helpers(): """Return `Helper` object. This makes it easier to use helper functions inside tests. """ return Helpers @pytest.fixture() def default_test_msg(prefix="", suffix="", sep=" "): """Create a random test string.""" return sep.join([prefix, uuid.uuid4().hex, suffix]) @pytest.fixture() def invalid_file(): """Create an invalid filename string.""" return "/tmp/INVALID.FILE" # noqa: S108 @pytest.fixture() def invalid_string(): """Create an invalid string.""" return "INVALID_STRING" @pytest.fixture() def valid_settings(): """Return valid config values.""" parser = ConfigParser() parser.read_dict(_DEFAULT_TEST_CONFIG_) parser.read_dict(_DEFAULT_TEST_SECRETS_) return parser @pytest.fixture() def default_channels_string(): """Return test values.""" return _DEFAULT_CHANNELS_STR_
nilq/baby-python
python
import copy import itertools import re import threading import html5lib import requests import urllib.parse from bs4 import BeautifulSoup from typing import List try: import basesite except (ModuleNotFoundError, ImportError) as e: from . import basesite class DaocaorenshuwuSite(basesite.BaseSite): def __init__(self): self.site_info = basesite.SiteInfo( type='文学', statue='上线版本', url='https://www.daocaorenshuwu.com', name='稻草人书屋', brief_name='稻草人', version='1.1', max_threading_number=10, # 每个chapter会进行多线程下载(get_chapter_content),所以总线程数量设置为10 ) super().__init__(self.site_info) self.base_url = 'https://www.daocaorenshuwu.com' self.encoding = 'utf-8' self.search_url = 'https://www.daocaorenshuwu.com/plus/search.php?q=%s' self.session = requests.session() @basesite.print_in_out def get_books(self, search_info: str) -> List[basesite.Book]: url = self.search_url % urllib.parse.quote(search_info) r = self.try_get_url(self.session, url, try_timeout=5) soup = BeautifulSoup(r.content, 'html.parser') book_soup_list = soup.select('tbody > tr') search_book_results = [] for book_soup in book_soup_list: td_soup_list = book_soup.select('td') book_url = self.base_url + td_soup_list[0].select_one('a').attrs['href'] if book_url.find('search.html') != -1: continue book_name = td_soup_list[0].text book_author = td_soup_list[1].text book_brief = "无" book = basesite.Book(site=self, url=book_url, name=book_name, author=book_author, brief=book_brief) search_book_results.append(book) return search_book_results @basesite.print_in_out def get_chapters(self, book: basesite.Book) -> List[basesite.Chapter]: r = self.try_get_url(self.session, book.url) if r is None: return [] soup = BeautifulSoup(r.content, 'html.parser') chapter_soup_list = soup.select('div#all-chapter div.panel-body div.item a') chapters = [basesite.Chapter(site=self, url='https:' + chapter.attrs['href'], title=chapter.text) for chapter in chapter_soup_list] return chapters def get_chapter_content(self, chapter: basesite.Chapter) -> str: class _InnerDown(threading.Thread): def __init__(self, func, session_, url): super().__init__() self.func = func self.session = copy.deepcopy(session_) self.url = url self.r = None def run(self) -> None: self.r = self.func(self.session, self.url) self.session.close() session = copy.deepcopy(self.session) partial_url = chapter.url[:-5] # step1: 先下载第一页和第二页, 判断总共多少页 tasks = [_InnerDown(self.try_get_url, session, chapter.url), _InnerDown(self.try_get_url, session, partial_url + "_2.html")] for task in tasks: task.start() for task in tasks: task.join() r1, r2 = tasks[0].r, tasks[1].r if r1 is None: session.close() return f'\r\n{chapter.title}\r\n下载失败' soup1 = BeautifulSoup(r1.content, 'html5lib') # 文档格式有错误,不能使用速度较快的html.parser has_multipages = False try: if soup1.select('div.text-center')[0].select('button.btn-info')[2].text.find('下一页') >= 0: has_multipages = True except IndexError: pass if has_multipages: if r2 is None: session.close() return f'\r\n{chapter.title}\r\n下载失败' soup2 = BeautifulSoup(r2.content, 'html5lib') page_info = soup2.select_one('div.book-type li.active').text pages = int(re.search(r'/(\d+)页)', page_info).group(1)) soup_list = [soup1, soup2] else: pages = 1 soup_list = [soup1] # step2: 多线程下载 url_list = ([f'{partial_url}_{i}.html' for i in range(3, pages + 1)]) tasks = [_InnerDown(self.try_get_url, session, url) for url in url_list] for task in tasks: task.start() for task in tasks: task.join() session.close() for task in tasks: if task.r is None: return f'\r\n{chapter.title}\r\n下载失败' else: soup_list.append(BeautifulSoup(task.r.content, 'html5lib')) # step3: 合并下载内容 content_list = [] for soup in soup_list: content_soup = soup.select_one('div#cont-text') for i in content_soup.select('script,style,[class]'): i.decompose() content_list.append(content_soup.text.strip()) # return f'\r\n{chapter.title}\r\n{"".join(content_list)}' return "\r\n".join(content_list) def save_chapter(self, chapter, filename): content = self.get_chapter_content(chapter) with open(filename, 'w', encoding=self.encoding) as f: f.write(content)
nilq/baby-python
python
from flask import render_template, flash, redirect, url_for, session from flask_login import login_user, current_user, login_required from app import app, db, lm from app.models.tables import User, Challenges from app.models.forms import FlagForm import datetime from notifications import * @lm.user_loader def load_user(id): return User.query.filter_by(id=id).first() @app.route("/index") @app.route("/") def index(): return render_template('index.html') @app.route("/dashboard", methods=["GET", "POST"]) @login_required def dashboard(): errors = [] users = User.query.filter_by(username=current_user.username).first() chall = Challenges.query.all() if not current_user.is_authenticated: # if user is logged in we get out of here return redirect(url_for('login')) form = FlagForm() chall = Challenges.query.all() if form.validate_on_submit(): chall1 = Challenges.query.filter_by(flag=form.flag.data).first() if not chall1: flash(FLAG_INCORRECT) return redirect(url_for('dashboard')) if chall1.flag == form.flag.data: user = User.query.filter_by(username=current_user.username).first() if str(chall1.id) in user.solved: flash(FLAG_SUBMITTED_ALREADY) print(chall) return render_template('dashboard/index.html', form=form, users=users, chall=chall) user.score = str(int(user.score) + int(chall1.value)) user.solved = user.solved + str(chall1.id) + ', ' user.lastSubmit = datetime.datetime.utcnow() db.session.commit() flash(FLAG_SUCCESS) return redirect(url_for('dashboard')) return render_template('dashboard/index.html', form=form, users=users, chall=chall )
nilq/baby-python
python
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'ipetrash' import re import requests from bs4 import BeautifulSoup def get_last_series(url: str) -> int: rs = requests.get(url) root = BeautifulSoup(rs.content, 'html.parser') fields_str = root.select_one('ul.flist').get_text(strip=True) if not fields_str: raise Exception('Не удалось найти описание полей аниме!') m = re.search(r'Добавлена:\s*(\d+)\s*серия', fields_str) if not m: raise Exception('Не удалось найти номер последней серии!') return int(m.group(1)) if __name__ == '__main__': url = 'https://anivost.org/24-chernyy-klever.html' print(get_last_series(url)) # 170
nilq/baby-python
python
import os from pathlib import Path from shutil import rmtree from tests.lib.base import DeloggerTestBase from tests.lib.urlopen_mock import UrlopenMock class TestPresets(DeloggerTestBase): def teardown_method(self): log_dir = self.OUTPUT_DIRPATH if not Path(log_dir).is_dir(): return False rmtree(log_dir) def test_info(self, capsys): from delogger.presets.info import logger self.execute_log(logger) self.check_normal_stream_log(logger, capsys, is_color=True) assert getattr(logger, "debuglog") def test_debug(self, capsys): from delogger.presets.debug import logger self.execute_log(logger) self.check_debug_stream_log(logger, capsys, is_color=True) run_rotating_hdlr = logger.handlers[1] self.check_log_file(run_rotating_hdlr.filepath) assert getattr(logger, "debuglog") def test_debug_stream(self, capsys): from delogger.presets.debug_stream import logger self.execute_log(logger) self.check_debug_stream_log(logger, capsys, is_color=True) assert getattr(logger, "debuglog") def test_output(self, capsys): from delogger.presets.output import logger self.execute_log(logger) assert getattr(logger, "debuglog") assert Path(self.OUTPUT_DIRPATH).is_dir() def test_output_env(self, capsys): urlopen_mock = UrlopenMock() filepath = f"{self.OUTPUT_DIRPATH}/test_output.log" os.environ["DELOGGER_NAME"] = "test_output_env" os.environ["DELOGGER_FILEPATH"] = filepath os.environ["DELOGGER_SLACK_WEBHOOK"] = "http://slack_webhook" from delogger.presets.output import OutputPresets logger = OutputPresets("no_name").get_logger() del os.environ["DELOGGER_NAME"] del os.environ["DELOGGER_FILEPATH"] del os.environ["DELOGGER_SLACK_WEBHOOK"] self.execute_log(logger) assert getattr(logger, "debuglog") assert Path(filepath).is_file() assert urlopen_mock.call_count == 4 def test_profiler(self): from delogger.presets.profiler import logger self.execute_log(logger) assert getattr(logger, "debuglog") assert getattr(logger, "line_profile") assert getattr(logger, "add_line_profile") assert getattr(logger, "memory_profile") assert getattr(logger, "line_memory_profile")
nilq/baby-python
python
"""Top-level package for the 'dltf' framework. Running ``import dltf`` will recursively import all important subpackages and modules. """ import logging import dogs_vs_cats.src.inception_resnet_v2 logger = logging.getLogger("dogs_vs_cats") __url__ = "https://github.com/ShiNik/DeepLearning_Tensorflow" __version__ = "0.1.0"
nilq/baby-python
python
"""Class for symbolic expression object or program.""" import array import os import warnings from textwrap import indent import numpy as np from sympy.parsing.sympy_parser import parse_expr from sympy import pretty from dsr.functions import PlaceholderConstant from dsr.const import make_const_optimizer from dsr.utils import cached_property import dsr.utils as U def _finish_tokens(tokens): """ Complete the pre-order traversal. Parameters ---------- tokens : list of integers A list of integers corresponding to tokens in the library. The list defines an expression's pre-order traversal. Returns _______ tokens : list of integers A list of integers corresponding to tokens in the library. The list defines an expression's pre-order traversal. "Dangling" programs are completed with repeated "x1" until the expression completes. """ arities = np.array([Program.library.arities[t] for t in tokens]) dangling = 1 + np.cumsum(arities - 1) if 0 in dangling: expr_length = 1 + np.argmax(dangling == 0) tokens = tokens[:expr_length] else: # Extend with first variable until complete tokens = np.append(tokens, np.random.choice(Program.library.input_tokens, size=dangling[-1])) return tokens def from_str_tokens(str_tokens, optimize, skip_cache=False): """ Memoized function to generate a Program from a list of str and/or float. See from_tokens() for details. Parameters ---------- str_tokens : str | list of (str | float) Either a comma-separated string of tokens and/or floats, or a list of str and/or floats. optimize : bool See from_tokens(). skip_cache : bool See from_tokens(). Returns ------- program : Program See from_tokens(). """ # Convert str to list of str if isinstance(str_tokens, str): str_tokens = str_tokens.split(",") # Convert list of str|float to list of tokens if isinstance(str_tokens, list): traversal = [] constants = [] for s in str_tokens: if s in Program.library.names: t = Program.library.names.index(s.lower()) elif U.is_float(s): assert "const" not in str_tokens, "Currently does not support both placeholder and hard-coded constants." assert not optimize, "Currently does not support optimization with hard-coded constants." t = Program.library.const_token constants.append(float(s)) else: raise ValueError("Did not recognize token {}.".format(s)) traversal.append(t) traversal = np.array(traversal, dtype=np.int32) else: raise ValueError("Input must be list or string.") # Generate base Program (with "const" for constants) p = from_tokens(traversal, optimize=optimize, skip_cache=skip_cache) # Replace any constants p.set_constants(constants) return p def from_tokens(tokens, optimize, skip_cache=False): """ Memoized function to generate a Program from a list of tokens. Since some tokens are nonfunctional, this first computes the corresponding traversal. If that traversal exists in the cache, the corresponding Program is returned. Otherwise, a new Program is returned. Parameters ---------- tokens : list of integers A list of integers corresponding to tokens in the library. The list defines an expression's pre-order traversal. "Dangling" programs are completed with repeated "x1" until the expression completes. optimize : bool Whether to optimize the program before returning it. skip_cache : bool Whether to bypass the cache when creating the program. Returns _______ program : Program The Program corresponding to the tokens, either pulled from memoization or generated from scratch. """ ''' Truncate expressions that complete early; extend ones that don't complete ''' tokens = _finish_tokens(tokens) # For stochastic Tasks, there is no cache; always generate a new Program. # For deterministic Programs, if the Program is in the cache, return it; # otherwise, create a new one and add it to the cache. if skip_cache: p = Program(tokens, optimize=optimize) elif Program.task.stochastic: p = Program(tokens, optimize=optimize) else: key = tokens.tostring() if key in Program.cache: p = Program.cache[key] p.count += 1 else: p = Program(tokens, optimize=optimize) Program.cache[key] = p return p class Program(object): """ The executable program representing the symbolic expression. The program comprises unary/binary operators, constant placeholders (to-be-optimized), input variables, and hard-coded constants. Parameters ---------- tokens : list of integers A list of integers corresponding to tokens in the library. "Dangling" programs are completed with repeated "x1" until the expression completes. optimize : bool Whether to optimize the program upon initializing it. Attributes ---------- traversal : list List of operators (type: Function) and terminals (type: int, float, or str ("const")) encoding the pre-order traversal of the expression tree. tokens : np.ndarry (dtype: int) Array of integers whose values correspond to indices const_pos : list of int A list of indicies of constant placeholders along the traversal. float_pos : list of float A list of indices of constants placeholders or floating-point constants along the traversal. sympy_expr : str The (lazily calculated) SymPy expression corresponding to the program. Used for pretty printing _only_. base_r : float The base reward (reward without penalty) of the program on the training data. complexity : float The (lazily calcualted) complexity of the program. r : float The (lazily calculated) reward of the program on the training data. count : int The number of times this Program has been sampled. str : str String representation of tokens. Useful as unique identifier. """ # Static variables task = None # Task library = None # Library const_optimizer = None # Function to optimize constants cache = {} # Cython-related static variables have_cython = None # Do we have cython installed execute = None # Link to execute. Either cython or python cyfunc = None # Link to cyfunc lib since we do an include inline def __init__(self, tokens, optimize): """ Builds the Program from a list of Tokens, optimizes the Constants against reward function, and evalutes the reward. """ self.traversal = [Program.library[t] for t in tokens] self.const_pos = [i for i, t in enumerate(tokens) if Program.library[t].name == "const"] # Just constant placeholder positions self.len_traversal = len(self.traversal) if self.have_cython and self.len_traversal > 1: self.is_input_var = array.array('i', [t.input_var is not None for t in self.traversal]) self.invalid = False self.str = tokens.tostring() if optimize: _ = self.optimize() self.count = 1 def cython_execute(self, X): """Executes the program according to X using Cython. Parameters ---------- X : array-like, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. Returns ------- y_hats : array-like, shape = [n_samples] The result of executing the program on X. """ if self.len_traversal > 1: return self.cyfunc.execute(X, self.len_traversal, self.traversal, self.is_input_var) else: return self.python_execute(X) def python_execute(self, X): """Executes the program according to X using Python. Parameters ---------- X : array-like, shape = [n_samples, n_features] Training vectors, where n_samples is the number of samples and n_features is the number of features. Returns ------- y_hats : array-like, shape = [n_samples] The result of executing the program on X. """ # # Check for single-node programs # node = self.traversal[0] # if isinstance(node, float): # return np.repeat(node, X.shape[0]) # if isinstance(node, int): # return X[:, node] apply_stack = [] for node in self.traversal: apply_stack.append([node]) while len(apply_stack[-1]) == apply_stack[-1][0].arity + 1: # Apply functions that have sufficient arguments token = apply_stack[-1][0] terminals = apply_stack[-1][1:] # terminals = [np.repeat(t, X.shape[0]) if isinstance(t, float) # else X[:, t] if isinstance(t, int) # else t for t in apply_stack[-1][1:]] if token.input_var is not None: intermediate_result = X[:, token.input_var] else: intermediate_result = token(*terminals) if len(apply_stack) != 1: apply_stack.pop() apply_stack[-1].append(intermediate_result) else: return intermediate_result # We should never get here assert False, "Function should never get here!" return None def optimize(self): """ Optimizes the constant tokens against the training data and returns the optimized constants. This function generates an objective function based on the training dataset, reward function, and constant optimizer. It ignores penalties because the Program structure is fixed, thus penalties are all the same. It then optimizes the constants of the program and returns the optimized constants. Returns _______ optimized_constants : vector Array of optimized constants. """ # Create the objective function, which is a function of the constants being optimized def f(consts): self.set_constants(consts) r = self.task.reward_function(self) obj = -r # Constant optimizer minimizes the objective function # Need to reset to False so that a single invalid call during # constant optimization doesn't render the whole Program invalid. self.invalid = False return obj assert self.execute is not None, "set_execute needs to be called first" if len(self.const_pos) > 0: # Do the optimization x0 = np.ones(len(self.const_pos)) # Initial guess optimized_constants = Program.const_optimizer(f, x0) self.set_constants(optimized_constants) else: # No need to optimize if there are no constants optimized_constants = [] return optimized_constants def set_constants(self, consts): """Sets the program's constants to the given values""" for i, const in enumerate(consts): # Create a new instance of PlaceholderConstant instead of changing # the "values" attribute, otherwise all Programs will have the same # instance and just overwrite each other's value. self.traversal[self.const_pos[i]] = PlaceholderConstant(const) @classmethod def clear_cache(cls): """Clears the class' cache""" cls.cache = {} @classmethod def set_task(cls, task): """Sets the class' Task""" Program.task = task Program.library = task.library @classmethod def set_const_optimizer(cls, name, **kwargs): """Sets the class' constant optimizer""" const_optimizer = make_const_optimizer(name, **kwargs) Program.const_optimizer = const_optimizer @classmethod def set_complexity_penalty(cls, name, weight): """Sets the class' complexity penalty""" all_functions = { # No penalty None : lambda p : 0.0, # Length of tree "length" : lambda p : len(p) } assert name in all_functions, "Unrecognzied complexity penalty name" if weight == 0: Program.complexity_penalty = lambda p : 0.0 else: Program.complexity_penalty = lambda p : weight * all_functions[name](p) @classmethod def set_execute(cls, protected): """Sets which execute method to use""" """ If cython ran, we will have a 'c' file generated. The dynamic libary can be given different names, so it's not reliable for testing if cython ran. """ cpath = os.path.join(os.path.dirname(__file__),'cyfunc.c') if os.path.isfile(cpath): from . import cyfunc Program.cyfunc = cyfunc execute_function = Program.cython_execute Program.have_cython = True else: execute_function = Program.python_execute Program.have_cython = False if protected: Program.execute = execute_function else: class InvalidLog(): """Log class to catch and record numpy warning messages""" def __init__(self): self.error_type = None # One of ['divide', 'overflow', 'underflow', 'invalid'] self.error_node = None # E.g. 'exp', 'log', 'true_divide' self.new_entry = False # Flag for whether a warning has been encountered during a call to Program.execute() def write(self, message): """This is called by numpy when encountering a warning""" if not self.new_entry: # Only record the first warning encounter message = message.strip().split(' ') self.error_type = message[1] self.error_node = message[-1] self.new_entry = True def update(self, p): """If a floating-point error was encountered, set Program.invalid to True and record the error type and error node.""" if self.new_entry: p.invalid = True p.error_type = self.error_type p.error_node = self.error_node self.new_entry = False invalid_log = InvalidLog() np.seterrcall(invalid_log) # Tells numpy to call InvalidLog.write() when encountering a warning # Define closure for execute function def unsafe_execute(p, X): """This is a wrapper for execute_function. If a floating-point error would be hit, a warning is logged instead, p.invalid is set to True, and the appropriate nan/inf value is returned. It's up to the task's reward function to decide how to handle nans/infs.""" with np.errstate(all='log'): y = execute_function(p, X) invalid_log.update(p) return y Program.execute = unsafe_execute @cached_property def complexity(self): """Evaluates and returns the complexity of the program""" return Program.complexity_penalty(self.traversal) @cached_property def base_r(self): """Evaluates and returns the base reward of the program on the training set""" with warnings.catch_warnings(): warnings.simplefilter("ignore") return self.task.reward_function(self) @cached_property def r(self): """Evaluates and returns the reward of the program on the training set""" with warnings.catch_warnings(): warnings.simplefilter("ignore") return self.base_r - self.complexity @cached_property def evaluate(self): """Evaluates and returns the evaluation metrics of the program.""" with warnings.catch_warnings(): warnings.simplefilter("ignore") return self.task.evaluate(self) @cached_property def complexity_eureqa(self): """Computes sum of token complexity based on Eureqa complexity measures.""" complexity = sum([t.complexity for t in self.traversal]) return complexity @cached_property def sympy_expr(self): """ Returns the attribute self.sympy_expr. This is actually a bit complicated because we have to go: traversal --> tree --> serialized tree --> SymPy expression """ tree = self.traversal.copy() tree = build_tree(tree) tree = convert_to_sympy(tree) try: expr = parse_expr(tree.__repr__()) # SymPy expression except: expr = "N/A" return expr def pretty(self): """Returns pretty printed string of the program""" return pretty(self.sympy_expr) def print_stats(self): """Prints the statistics of the program""" print("\tReward: {}".format(self.r)) print("\tBase reward: {}".format(self.base_r)) print("\tCount: {}".format(self.count)) print("\tInvalid: {}".format(self.invalid)) print("\tTraversal: {}".format(self)) print("\tExpression:") print("{}\n".format(indent(self.pretty(), '\t '))) def __repr__(self): """Prints the program's traversal""" return ','.join([repr(t) for t in self.traversal]) ############################################################################### # Everything below this line is currently only being used for pretty printing # ############################################################################### # Possible library elements that sympy capitalizes capital = ["add", "mul", "pow"] class Node(object): """Basic tree class supporting printing""" def __init__(self, val): self.val = val self.children = [] def __repr__(self): children_repr = ",".join(repr(child) for child in self.children) if len(self.children) == 0: return self.val # Avoids unnecessary parantheses, e.g. x1() return "{}({})".format(self.val, children_repr) def build_tree(traversal): """Recursively builds tree from pre-order traversal""" op = traversal.pop(0) n_children = op.arity val = repr(op) if val in capital: val = val.capitalize() node = Node(val) for _ in range(n_children): node.children.append(build_tree(traversal)) return node def convert_to_sympy(node): """Adjusts trees to only use node values supported by sympy""" if node.val == "div": node.val = "Mul" new_right = Node("Pow") new_right.children.append(node.children[1]) new_right.children.append(Node("-1")) node.children[1] = new_right elif node.val == "sub": node.val = "Add" new_right = Node("Mul") new_right.children.append(node.children[1]) new_right.children.append(Node("-1")) node.children[1] = new_right elif node.val == "inv": node.val = Node("Pow") node.children.append(Node("-1")) elif node.val == "neg": node.val = Node("Mul") node.children.append(Node("-1")) elif node.val == "n2": node.val = "Pow" node.children.append(Node("2")) elif node.val == "n3": node.val = "Pow" node.children.append(Node("3")) elif node.val == "n4": node.val = "Pow" node.children.append(Node("4")) for child in node.children: convert_to_sympy(child) return node
nilq/baby-python
python
import os.path import sys import warnings from setuptools import find_packages, setup if sys.version_info < (2, 7): raise NotImplementedError( """\n ############################################################## # globus-sdk does not support python versions older than 2.7 # ##############################################################""" ) # warn on older/untested python3s # it's not disallowed, but it could be an issue for some people if sys.version_info > (3,) and sys.version_info < (3, 5): warnings.warn( "Installing globus-sdk on Python 3 versions older than 3.5 " "may result in degraded functionality or even errors." ) # single source of truth for package version version_ns = {} with open(os.path.join("globus_sdk", "version.py")) as f: exec(f.read(), version_ns) setup( name="globus-sdk", version=version_ns["__version__"], description="Globus SDK for Python", long_description=open("README.rst").read(), author="Globus Team", author_email="support@globus.org", url="https://github.com/globus/globus-sdk-python", packages=find_packages(exclude=["tests", "tests.*"]), install_requires=[ "requests>=2.9.2,<3.0.0", "six>=1.10.0,<2.0.0", "pyjwt[crypto]>=1.5.3,<2.0.0", ], extras_require={ # empty extra included to support older installs "jwt": [], # the development extra is for SDK developers only "development": [ # drive testing with tox "tox>=3.5.3,<4.0", # linting "flake8>=3.0,<4.0", 'isort>=5.1.4,<6.0;python_version>="3.6"', # black requires py3.6+ # refrain from using 19.10b0 or later until # https://github.com/psf/black/issues/1288 # is fixed 'black==19.3b0;python_version>="3.6"', # flake-bugbear requires py3.6+ 'flake8-bugbear==20.1.4;python_version>="3.6"', # testing "pytest<5.0", "pytest-cov<3.0", "pytest-xdist<2.0", # mock on py2, py3.4 and py3.5 # not just py2: py3 versions of mock don't all have the same # interface! 'mock==2.0.0;python_version<"3.6"', # mocking HTTP responses "httpretty==0.9.5", # builds + uploads to pypi 'twine==3.2.0;python_version>="3.6"', 'wheel==0.34.2;python_version>="3.6"', # docs 'sphinx==3.1.2;python_version>="3.6"', 'sphinx-material==0.0.30;python_version>="3.6"', ], }, include_package_data=True, keywords=["globus", "file transfer"], classifiers=[ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Operating System :: MacOS :: MacOS X", "Operating System :: Microsoft :: Windows", "Operating System :: POSIX", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Topic :: Communications :: File Sharing", "Topic :: Internet :: WWW/HTTP", "Topic :: Software Development :: Libraries :: Python Modules", ], )
nilq/baby-python
python
import json from collections import defaultdict from decimal import Decimal import bleach import dateutil.parser import pytz from django.dispatch import receiver from django.urls import reverse from django.utils.formats import date_format from django.utils.html import escape from django.utils.safestring import mark_safe from django.utils.translation import pgettext_lazy, ugettext_lazy as _ from i18nfield.strings import LazyI18nString from pretix.base.models import ( CheckinList, Event, ItemVariation, LogEntry, OrderPosition, ) from pretix.base.signals import logentry_display from pretix.base.templatetags.money import money_filter OVERVIEW_BLACKLIST = [ 'pretix.plugins.sendmail.order.email.sent' ] def _display_order_changed(event: Event, logentry: LogEntry): data = json.loads(logentry.data) text = _('The order has been changed:') if logentry.action_type == 'pretix.event.order.changed.item': old_item = str(event.items.get(pk=data['old_item'])) if data['old_variation']: old_item += ' - ' + str(ItemVariation.objects.get(item__event=event, pk=data['old_variation'])) new_item = str(event.items.get(pk=data['new_item'])) if data['new_variation']: new_item += ' - ' + str(ItemVariation.objects.get(item__event=event, pk=data['new_variation'])) return text + ' ' + _('Position #{posid}: {old_item} ({old_price}) changed ' 'to {new_item} ({new_price}).').format( posid=data.get('positionid', '?'), old_item=old_item, new_item=new_item, old_price=money_filter(Decimal(data['old_price']), event.currency), new_price=money_filter(Decimal(data['new_price']), event.currency), ) elif logentry.action_type == 'pretix.event.order.changed.subevent': old_se = str(event.subevents.get(pk=data['old_subevent'])) new_se = str(event.subevents.get(pk=data['new_subevent'])) return text + ' ' + _('Position #{posid}: Event date "{old_event}" ({old_price}) changed ' 'to "{new_event}" ({new_price}).').format( posid=data.get('positionid', '?'), old_event=old_se, new_event=new_se, old_price=money_filter(Decimal(data['old_price']), event.currency), new_price=money_filter(Decimal(data['new_price']), event.currency), ) elif logentry.action_type == 'pretix.event.order.changed.price': return text + ' ' + _('Price of position #{posid} changed from {old_price} ' 'to {new_price}.').format( posid=data.get('positionid', '?'), old_price=money_filter(Decimal(data['old_price']), event.currency), new_price=money_filter(Decimal(data['new_price']), event.currency), ) elif logentry.action_type == 'pretix.event.order.changed.cancel': old_item = str(event.items.get(pk=data['old_item'])) if data['old_variation']: old_item += ' - ' + str(ItemVariation.objects.get(pk=data['old_variation'])) return text + ' ' + _('Position #{posid} ({old_item}, {old_price}) canceled.').format( posid=data.get('positionid', '?'), old_item=old_item, old_price=money_filter(Decimal(data['old_price']), event.currency), ) elif logentry.action_type == 'pretix.event.order.changed.add': item = str(event.items.get(pk=data['item'])) if data['variation']: item += ' - ' + str(ItemVariation.objects.get(item__event=event, pk=data['variation'])) if data['addon_to']: addon_to = OrderPosition.objects.get(order__event=event, pk=data['addon_to']) return text + ' ' + _('Position #{posid} created: {item} ({price}) as an add-on to ' 'position #{addon_to}.').format( posid=data.get('positionid', '?'), item=item, addon_to=addon_to.positionid, price=money_filter(Decimal(data['price']), event.currency), ) else: return text + ' ' + _('Position #{posid} created: {item} ({price}).').format( posid=data.get('positionid', '?'), item=item, price=money_filter(Decimal(data['price']), event.currency), ) elif logentry.action_type == 'pretix.event.order.changed.secret': return text + ' ' + _('A new secret has been generated for position #{posid}.').format( posid=data.get('positionid', '?'), ) elif logentry.action_type == 'pretix.event.order.changed.split': old_item = str(event.items.get(pk=data['old_item'])) if data['old_variation']: old_item += ' - ' + str(ItemVariation.objects.get(pk=data['old_variation'])) url = reverse('control:event.order', kwargs={ 'event': event.slug, 'organizer': event.organizer.slug, 'code': data['new_order'] }) return mark_safe(escape(text) + ' ' + _('Position #{posid} ({old_item}, {old_price}) split into new order: {order}').format( old_item=escape(old_item), posid=data.get('positionid', '?'), order='<a href="{}">{}</a>'.format(url, data['new_order']), old_price=money_filter(Decimal(data['old_price']), event.currency), )) elif logentry.action_type == 'pretix.event.order.changed.split_from': return _('This order has been created by splitting the order {order}').format( order=data['original_order'], ) def _display_checkin(event, logentry): data = logentry.parsed_data show_dt = False if 'datetime' in data: dt = dateutil.parser.parse(data.get('datetime')) show_dt = abs((logentry.datetime - dt).total_seconds()) > 60 or 'forced' in data tz = pytz.timezone(event.settings.timezone) dt_formatted = date_format(dt.astimezone(tz), "SHORT_DATETIME_FORMAT") if 'list' in data: try: checkin_list = event.checkin_lists.get(pk=data.get('list')).name except CheckinList.DoesNotExist: checkin_list = _("(unknown)") else: checkin_list = _("(unknown)") if data.get('first'): if show_dt: return _('Position #{posid} has been checked in at {datetime} for list "{list}".').format( posid=data.get('positionid'), datetime=dt_formatted, list=checkin_list ) else: return _('Position #{posid} has been checked in for list "{list}".').format( posid=data.get('positionid'), list=checkin_list ) else: if data.get('forced'): return _( 'A scan for position #{posid} at {datetime} for list "{list}" has been uploaded even though it has ' 'been scanned already.'.format( posid=data.get('positionid'), datetime=dt_formatted, list=checkin_list ) ) return _( 'Position #{posid} has been scanned and rejected because it has already been scanned before ' 'on list "{list}".'.format( posid=data.get('positionid'), list=checkin_list ) ) @receiver(signal=logentry_display, dispatch_uid="pretixcontrol_logentry_display") def pretixcontrol_logentry_display(sender: Event, logentry: LogEntry, **kwargs): plains = { 'pretix.event.comment': _('The event\'s internal comment has been updated.'), 'pretix.event.order.modified': _('The order details have been changed.'), 'pretix.event.order.unpaid': _('The order has been marked as unpaid.'), 'pretix.event.order.secret.changed': _('The order\'s secret has been changed.'), 'pretix.event.order.expirychanged': _('The order\'s expiry date has been changed.'), 'pretix.event.order.expired': _('The order has been marked as expired.'), 'pretix.event.order.paid': _('The order has been marked as paid.'), 'pretix.event.order.refunded': _('The order has been refunded.'), 'pretix.event.order.canceled': _('The order has been canceled.'), 'pretix.event.order.deleted': _('The test mode order {code} has been deleted.'), 'pretix.event.order.placed': _('The order has been created.'), 'pretix.event.order.placed.require_approval': _('The order requires approval before it can continue to be processed.'), 'pretix.event.order.approved': _('The order has been approved.'), 'pretix.event.order.denied': _('The order has been denied.'), 'pretix.event.order.contact.changed': _('The email address has been changed from "{old_email}" ' 'to "{new_email}".'), 'pretix.event.order.locale.changed': _('The order locale has been changed.'), 'pretix.event.order.invoice.generated': _('The invoice has been generated.'), 'pretix.event.order.invoice.regenerated': _('The invoice has been regenerated.'), 'pretix.event.order.invoice.reissued': _('The invoice has been reissued.'), 'pretix.event.order.comment': _('The order\'s internal comment has been updated.'), 'pretix.event.order.checkin_attention': _('The order\'s flag to require attention at check-in has been ' 'toggled.'), 'pretix.event.order.payment.changed': _('A new payment {local_id} has been started instead of the previous one.'), 'pretix.event.order.email.sent': _('An unidentified type email has been sent.'), 'pretix.event.order.email.error': _('Sending of an email has failed.'), 'pretix.event.order.email.attachments.skipped': _('The email has been sent without attachments since they ' 'would have been too large to be likely to arrive.'), 'pretix.event.order.email.custom_sent': _('A custom email has been sent.'), 'pretix.event.order.email.download_reminder_sent': _('An email has been sent with a reminder that the ticket ' 'is available for download.'), 'pretix.event.order.email.expire_warning_sent': _('An email has been sent with a warning that the order is about ' 'to expire.'), 'pretix.event.order.email.order_canceled': _('An email has been sent to notify the user that the order has been canceled.'), 'pretix.event.order.email.order_changed': _('An email has been sent to notify the user that the order has been changed.'), 'pretix.event.order.email.order_free': _('An email has been sent to notify the user that the order has been received.'), 'pretix.event.order.email.order_paid': _('An email has been sent to notify the user that payment has been received.'), 'pretix.event.order.email.order_denied': _('An email has been sent to notify the user that the order has been denied.'), 'pretix.event.order.email.order_approved': _('An email has been sent to notify the user that the order has ' 'been approved.'), 'pretix.event.order.email.order_placed': _('An email has been sent to notify the user that the order has been received and requires payment.'), 'pretix.event.order.email.order_placed_require_approval': _('An email has been sent to notify the user that ' 'the order has been received and requires ' 'approval.'), 'pretix.event.order.email.resend': _('An email with a link to the order detail page has been resent to the user.'), 'pretix.event.order.payment.confirmed': _('Payment {local_id} has been confirmed.'), 'pretix.event.order.payment.canceled': _('Payment {local_id} has been canceled.'), 'pretix.event.order.payment.started': _('Payment {local_id} has been started.'), 'pretix.event.order.payment.failed': _('Payment {local_id} has failed.'), 'pretix.event.order.quotaexceeded': _('The order could not be marked as paid: {message}'), 'pretix.event.order.overpaid': _('The order has been overpaid.'), 'pretix.event.order.refund.created': _('Refund {local_id} has been created.'), 'pretix.event.order.refund.created.externally': _('Refund {local_id} has been created by an external entity.'), 'pretix.event.order.refund.requested': _('The customer requested you to issue a refund.'), 'pretix.event.order.refund.done': _('Refund {local_id} has been completed.'), 'pretix.event.order.refund.canceled': _('Refund {local_id} has been canceled.'), 'pretix.event.order.refund.failed': _('Refund {local_id} has failed.'), 'pretix.control.auth.user.created': _('The user has been created.'), 'pretix.user.settings.2fa.enabled': _('Two-factor authentication has been enabled.'), 'pretix.user.settings.2fa.disabled': _('Two-factor authentication has been disabled.'), 'pretix.user.settings.2fa.regenemergency': _('Your two-factor emergency codes have been regenerated.'), 'pretix.user.settings.2fa.device.added': _('A new two-factor authentication device "{name}" has been added to ' 'your account.'), 'pretix.user.settings.2fa.device.deleted': _('The two-factor authentication device "{name}" has been removed ' 'from your account.'), 'pretix.user.settings.notifications.enabled': _('Notifications have been enabled.'), 'pretix.user.settings.notifications.disabled': _('Notifications have been disabled.'), 'pretix.user.settings.notifications.changed': _('Your notification settings have been changed.'), 'pretix.user.anonymized': _('This user has been anonymized.'), 'pretix.user.oauth.authorized': _('The application "{application_name}" has been authorized to access your ' 'account.'), 'pretix.control.auth.user.forgot_password.mail_sent': _('Password reset mail sent.'), 'pretix.control.auth.user.forgot_password.recovered': _('The password has been reset.'), 'pretix.organizer.deleted': _('The organizer "{name}" has been deleted.'), 'pretix.voucher.added': _('The voucher has been created.'), 'pretix.voucher.added.waitinglist': _('The voucher has been created and sent to a person on the waiting list.'), 'pretix.voucher.changed': _('The voucher has been changed.'), 'pretix.voucher.deleted': _('The voucher has been deleted.'), 'pretix.voucher.redeemed': _('The voucher has been redeemed in order {order_code}.'), 'pretix.event.item.added': _('The product has been created.'), 'pretix.event.item.changed': _('The product has been changed.'), 'pretix.event.item.deleted': _('The product has been deleted.'), 'pretix.event.item.variation.added': _('The variation "{value}" has been created.'), 'pretix.event.item.variation.deleted': _('The variation "{value}" has been deleted.'), 'pretix.event.item.variation.changed': _('The variation "{value}" has been changed.'), 'pretix.event.item.addons.added': _('An add-on has been added to this product.'), 'pretix.event.item.addons.removed': _('An add-on has been removed from this product.'), 'pretix.event.item.addons.changed': _('An add-on has been changed on this product.'), 'pretix.event.quota.added': _('The quota has been added.'), 'pretix.event.quota.deleted': _('The quota has been deleted.'), 'pretix.event.quota.changed': _('The quota has been changed.'), 'pretix.event.category.added': _('The category has been added.'), 'pretix.event.category.deleted': _('The category has been deleted.'), 'pretix.event.category.changed': _('The category has been changed.'), 'pretix.event.question.added': _('The question has been added.'), 'pretix.event.question.deleted': _('The question has been deleted.'), 'pretix.event.question.changed': _('The question has been changed.'), 'pretix.event.taxrule.added': _('The tax rule has been added.'), 'pretix.event.taxrule.deleted': _('The tax rule has been deleted.'), 'pretix.event.taxrule.changed': _('The tax rule has been changed.'), 'pretix.event.checkinlist.added': _('The check-in list has been added.'), 'pretix.event.checkinlist.deleted': _('The check-in list has been deleted.'), 'pretix.event.checkinlist.changed': _('The check-in list has been changed.'), 'pretix.event.settings': _('The event settings have been changed.'), 'pretix.event.tickets.settings': _('The ticket download settings have been changed.'), 'pretix.event.plugins.enabled': _('A plugin has been enabled.'), 'pretix.event.plugins.disabled': _('A plugin has been disabled.'), 'pretix.event.live.activated': _('The shop has been taken live.'), 'pretix.event.live.deactivated': _('The shop has been taken offline.'), 'pretix.event.testmode.activated': _('The shop has been taken into test mode.'), 'pretix.event.testmode.deactivated': _('The test mode has been disabled.'), 'pretix.event.added': _('The event has been created.'), 'pretix.event.changed': _('The event settings have been changed.'), 'pretix.event.question.option.added': _('An answer option has been added to the question.'), 'pretix.event.question.option.deleted': _('An answer option has been removed from the question.'), 'pretix.event.question.option.changed': _('An answer option has been changed.'), 'pretix.event.permissions.added': _('A user has been added to the event team.'), 'pretix.event.permissions.invited': _('A user has been invited to the event team.'), 'pretix.event.permissions.changed': _('A user\'s permissions have been changed.'), 'pretix.event.permissions.deleted': _('A user has been removed from the event team.'), 'pretix.waitinglist.voucher': _('A voucher has been sent to a person on the waiting list.'), 'pretix.event.orders.waitinglist.deleted': _('An entry has been removed from the waiting list.'), 'pretix.event.orders.waitinglist.changed': _('An entry has been changed on the waiting list.'), 'pretix.event.orders.waitinglist.added': _('An entry has been added to the waiting list.'), 'pretix.team.created': _('The team has been created.'), 'pretix.team.changed': _('The team settings have been changed.'), 'pretix.team.deleted': _('The team has been deleted.'), 'pretix.subevent.deleted': pgettext_lazy('subevent', 'The event date has been deleted.'), 'pretix.subevent.changed': pgettext_lazy('subevent', 'The event date has been changed.'), 'pretix.subevent.added': pgettext_lazy('subevent', 'The event date has been created.'), 'pretix.subevent.quota.added': pgettext_lazy('subevent', 'A quota has been added to the event date.'), 'pretix.subevent.quota.changed': pgettext_lazy('subevent', 'A quota has been changed on the event date.'), 'pretix.subevent.quota.deleted': pgettext_lazy('subevent', 'A quota has been removed from the event date.'), 'pretix.device.created': _('The device has been created.'), 'pretix.device.changed': _('The device has been changed.'), 'pretix.device.revoked': _('Access of the device has been revoked.'), 'pretix.device.initialized': _('The device has been initialized.'), 'pretix.device.keyroll': _('The access token of the device has been regenerated.'), 'pretix.device.updated': _('The device has notified the server of an hardware or software update.'), } data = json.loads(logentry.data) if logentry.action_type.startswith('pretix.event.item.variation'): if 'value' not in data: # Backwards compatibility var = ItemVariation.objects.filter(id=data['id']).first() if var: data['value'] = str(var.value) else: data['value'] = '?' else: data['value'] = LazyI18nString(data['value']) if logentry.action_type in plains: data = defaultdict(lambda: '?', data) return plains[logentry.action_type].format_map(data) if logentry.action_type.startswith('pretix.event.order.changed'): return _display_order_changed(sender, logentry) if logentry.action_type.startswith('pretix.event.payment.provider.'): return _('The settings of a payment provider have been changed.') if logentry.action_type.startswith('pretix.event.tickets.provider.'): return _('The settings of a ticket output provider have been changed.') if logentry.action_type == 'pretix.event.order.consent': return _('The user confirmed the following message: "{}"').format( bleach.clean(logentry.parsed_data.get('msg'), tags=[], strip=True) ) if logentry.action_type == 'pretix.event.checkin': return _display_checkin(sender, logentry) if logentry.action_type == 'pretix.control.views.checkin': # deprecated dt = dateutil.parser.parse(data.get('datetime')) tz = pytz.timezone(sender.settings.timezone) dt_formatted = date_format(dt.astimezone(tz), "SHORT_DATETIME_FORMAT") if 'list' in data: try: checkin_list = sender.checkin_lists.get(pk=data.get('list')).name except CheckinList.DoesNotExist: checkin_list = _("(unknown)") else: checkin_list = _("(unknown)") if data.get('first'): return _('Position #{posid} has been checked in manually at {datetime} on list "{list}".').format( posid=data.get('positionid'), datetime=dt_formatted, list=checkin_list, ) return _('Position #{posid} has been checked in again at {datetime} on list "{list}".').format( posid=data.get('positionid'), datetime=dt_formatted, list=checkin_list ) if logentry.action_type in ('pretix.control.views.checkin.reverted', 'pretix.event.checkin.reverted'): if 'list' in data: try: checkin_list = sender.checkin_lists.get(pk=data.get('list')).name except CheckinList.DoesNotExist: checkin_list = _("(unknown)") else: checkin_list = _("(unknown)") return _('The check-in of position #{posid} on list "{list}" has been reverted.').format( posid=data.get('positionid'), list=checkin_list, ) if logentry.action_type == 'pretix.team.member.added': return _('{user} has been added to the team.').format(user=data.get('email')) if logentry.action_type == 'pretix.team.member.removed': return _('{user} has been removed from the team.').format(user=data.get('email')) if logentry.action_type == 'pretix.team.member.joined': return _('{user} has joined the team using the invite sent to {email}.').format( user=data.get('email'), email=data.get('invite_email') ) if logentry.action_type == 'pretix.team.invite.created': return _('{user} has been invited to the team.').format(user=data.get('email')) if logentry.action_type == 'pretix.team.invite.resent': return _('Invite for {user} has been resent.').format(user=data.get('email')) if logentry.action_type == 'pretix.team.invite.deleted': return _('The invite for {user} has been revoked.').format(user=data.get('email')) if logentry.action_type == 'pretix.team.token.created': return _('The token "{name}" has been created.').format(name=data.get('name')) if logentry.action_type == 'pretix.team.token.deleted': return _('The token "{name}" has been revoked.').format(name=data.get('name')) if logentry.action_type == 'pretix.user.settings.changed': text = str(_('Your account settings have been changed.')) if 'email' in data: text = text + ' ' + str(_('Your email address has been changed to {email}.').format(email=data['email'])) if 'new_pw' in data: text = text + ' ' + str(_('Your password has been changed.')) if data.get('is_active') is True: text = text + ' ' + str(_('Your account has been enabled.')) elif data.get('is_active') is False: text = text + ' ' + str(_('Your account has been disabled.')) return text if logentry.action_type == 'pretix.control.auth.user.impersonated': return str(_('You impersonated {}.')).format(data['other_email']) if logentry.action_type == 'pretix.control.auth.user.impersonate_stopped': return str(_('You stopped impersonating {}.')).format(data['other_email'])
nilq/baby-python
python
import mcradar as mcr import xarray as xr import numpy as np import pandas as pd import os from IPython.core.debugger import Tracer ; debug=Tracer() #insert this line somewhere to debug def getApectRatio(radii): # imput radii [mm] # auer et all 1970 (The Dimension of Ice Crystals in Natural Clouds) diameter = 2 * radii *1e3 # calculating the diameter in [mu m] h = 2.020 * (diameter)**0.449 as_ratio = h / diameter return as_ratio #reading the data file dataPath = "data" fileName = "mass2fr_0300-0600min_avtstep_5.ncdf" filePath = os.path.join(dataPath, fileName) data = xr.open_dataset(filePath) #fake time time = np.ones_like(data.dim_SP_all_av150) #calculating the aspec ratio sPhi = np.ones_like(data.dim_SP_all_av150)*np.nan sPhi = getApectRatio(data.diam * 1e3) sPhi[data.mm.values > 1]=0.6 #converting to pandas dataframe dataTable = data.to_dataframe() dataTable = dataTable.rename(columns={'m_tot':'mTot', 'height':'sHeight', 'vt':'vel', 'diam':'dia','xi':'sMult'}) #settings dicSettings = mcr.loadSettings(dataPath='_', freq=np.array([9.6e9]), maxHeight=3000, minHeight=2500, heightRes=5) #adding required variables dataTable['radii'] = dataTable['dia'] / 2.# particle radius in m dataTable['time']=time PSD_method="bin" #"bin": count SP and their multiplicity in height and size bins; "1D_KDE": #DOES NOT WORK YET!! 1-dimensional kernel density estimate, "discrete_SP": calculate scattering properties of each SP individually if PSD_method in ["bin","1D_KDE"]: #some definitions nbins = 100 #number of used bins n_heights = 50 model_top = 3850 #[m] #TODO: read this from output minR =-4 #minimum R considered (log10-space) maxR = 0 #maximum R considered (log10-space) area_box = 5 #[m2] #TODO: read this from output Rgrid=np.logspace(minR,maxR,nbins) Rgrid_log=np.linspace(minR,maxR,nbins) Rgrid_logdiff=Rgrid_log[1]-Rgrid_log[0] heightvec_bound = np.linspace(0,model_top,n_heights) #heightvec_bound = np.linspace(2900,3000,5) #TODO: remove (only for debugging) Vbox = area_box*heightvec_bound[1]-heightvec_bound[0] #[m3] reducedDataTable = pd.DataFrame() for i_height in range(len(heightvec_bound)-1): print("calculate h=",heightvec_bound[i_height]) #initialize as many dataFrame as categories #one category must have the same particle properties (mass, velocity) at the same size dataBINmono = pd.DataFrame(data={"Rgrid": Rgrid}) #initialize dataFrame dataBINagg = pd.DataFrame(data={"Rgrid": Rgrid}) #initialize dataFrame #select subset of particles in a given height range condition_in_height = np.logical_and(heightvec_bound[i_height]<dataTable["sHeight"],heightvec_bound[i_height+1]>dataTable["sHeight"]) #select monomers and aggregates cond_mono=np.logical_and(dataTable["mm"]==1, condition_in_height) #monomers cond_agg=np.logical_and(dataTable["mm"]>1, condition_in_height) #aggregates datamono = dataTable[cond_mono] dataagg = dataTable[cond_agg] for key in ["sMult","vel","mTot"]: dataBINmono[key] = np.zeros_like(Rgrid) dataBINagg[key] = np.zeros_like(Rgrid) for i_rad,rad in enumerate(Rgrid[:-1]): inbinmono = np.logical_and(Rgrid[i_rad]<datamono["radii"],Rgrid[i_rad+1]>datamono["radii"]) inbinagg = np.logical_and(Rgrid[i_rad]<dataagg["radii"],Rgrid[i_rad+1]>dataagg["radii"]) if sum(inbinmono)>0: for var_key in ["mTot","vel"]: dataBINmono[var_key][i_rad] = datamono[inbinmono].iloc[0][var_key] #mass in grams #TODO: calculate m (either by picking a particle from inside the bin or from the m-D relation or build an average of the particle) if sum(inbinagg)>0: for var_key in ["mTot","vel"]: dataBINagg[var_key][i_rad] = dataagg[inbinagg].iloc[0][var_key] #mass in grams #TODO: calculate m (either by picking a particle from inside the bin or from the m-D relation or build an average of the particle) if PSD_method=="bin": for i_rad,rad in enumerate(Rgrid[:-1]): inbinmono = np.logical_and(Rgrid[i_rad]<datamono["radii"],Rgrid[i_rad+1]>datamono["radii"]) inbinagg = np.logical_and(Rgrid[i_rad]<dataagg["radii"],Rgrid[i_rad+1]>dataagg["radii"]) if sum(inbinmono)>0: dataBINmono["sMult"][i_rad] = np.nansum(datamono[inbinmono]["sMult"]) if sum(inbinagg)>0: dataBINagg["sMult"][i_rad] = np.nansum(dataagg[inbinagg]["sMult"]) #print(i_rad,dataBINmono["sMult"][i_rad],dataBINagg["sMult"][i_rad],dataBINagg["mTot"][i_rad],dataBINagg["vel"][i_rad]) elif PSD_method=="1D_KDE": #does not work yet!! #calculating number density [#/m3] #MONOMERS dataBINmono["sMult"] = mcr.tableOperator.kernel_estimate(dataTable["radii"][cond_mono],np.log(Rgrid),weight=dataTable["sMult"][cond_mono],sigma0=0.001)*Rgrid_logdiff #/Vbox #TODO: #AGGREGATES dataBINagg["sMult"] = mcr.tableOperator.kernel_estimate(dataTable["radii"][cond_agg],np.log(Rgrid),weight=dataTable["sMult"][cond_agg])*Rgrid_logdiff/Vbox #for i_rad,Mult in enumerate(dataBINagg["sMult"]): # print(i_rad,dataBINmono["sMult"][i_rad],dataBINagg["sMult"][i_rad],dataBINagg["mTot"][i_rad],dataBINagg["vel"][i_rad]) #some general properties and conversions which are independent of the actual SP-list dataBINmono['radii_mm'] = dataBINmono['Rgrid'] * 1e3 # particle radius in mm dataBINagg['radii_mm'] = dataBINagg['Rgrid'] * 1e3 # particle radius in mm dataBINmono['sPhi'] = getApectRatio(dataBINmono.radii_mm) dataBINagg['sPhi'] = 0.6 for df in [dataBINmono,dataBINagg]: df['dia_cm'] = df['Rgrid'] * 1e2*2 # particle radius in mm df['time']=np.ones_like(df.radii_mm) df['sHeight'] = (heightvec_bound[i_height+1]+heightvec_bound[i_height+1])/2 df['mTot_g'] = dataTable['mTot'] * 1e3 # mass in grams #calculating density df = mcr.tableOperator.calcRho(df) df = df[(df['sPhi'] >= 0.015)] #TODO: this kills everything larger than 3.8mm reducedDataTable = pd.concat([reducedDataTable, df]) reducedDataTable = reducedDataTable[(reducedDataTable['sMult']>1.0)] print(reducedDataTable) print("?") #starting the simulation output = mcr.fullRadar(dicSettings, reducedDataTable) print(output) elif PSD_method=="discrete_SP": #adding required variables dataTable['radii_mm'] = dataTable['dia'] * 1e3 /2.# particle radius in mm dataTable['mTot_g'] = dataTable['mTot'] * 1e3 # mass in grams dataTable['dia_cm'] = dataTable['dia'] * 1e2 # diameter in centimeters dataTable['sPhi']=sPhi dataTable = dataTable[(dataTable['sPhi'] >= 0.015)] # dataTable['sMult']=1 #(it deactivates the multiplicity) #calculating density dataTable = mcr.tableOperator.calcRho(dataTable) #settings dicSettings = mcr.loadSettings(dataPath='_', freq=np.array([9.6e9]), maxHeight=3000, minHeight=2500, heightRes=5) #starting the simulation output = mcr.fullRadar(dicSettings, dataTable) #saving the data #output.to_netcdf('comp_smult1.nc') output.to_netcdf('comp.nc') debug()
nilq/baby-python
python
import numpy as np """ v_l object. c*r^{n-2}*exp{-e*r^2} """ class rnExp: def __init__(self, n, e, c): self.n = np.asarray(n) self.e = np.asarray(e) self.c = np.asarray(c) def __call__(self, r): return np.sum( r[:, np.newaxis] ** self.n * self.c * np.exp(-self.e * r[:, np.newaxis] ** 2), axis=1, ) def generate_ecp_functors(coeffs): """ Returns a functor, with keys as the angular momenta: -1 stands for the nonlocal part, 0,1,2,... are the s,p,d channels, etc. Parameters: mol._ecp[atom_name][1] (coefficients of the ECP) Returns: v_l function, with key = angular momentum """ d = {} for c in coeffs: el = c[0] rn = [] exponent = [] coefficient = [] for n, expand in enumerate(c[1]): # print("r",n-2,"coeff",expand) for line in expand: rn.append(n - 2) exponent.append(line[0]) coefficient.append(line[1]) d[el] = rnExp(rn, exponent, coefficient) return d ######################################################################### def P_l(x, l): """ Legendre functions, returns a nconf x naip array for a given l, x=r_ea(i) Parameters: x: nconf array, l: integer Returns: P_l values: nconf x naip array """ if l == 0: return np.ones(x.shape) elif l == 1: return x elif l == 2: return 0.5 * (3 * x * x - np.ones(x.shape)) elif l == 3: return 0.5 * (5 * x * x * x - 3 * x) elif l == 4: return 0.125 * (35 * x * x * x * x - 30 * x * x + 3 * np.ones(x.shape)) else: return np.zeros(x.shape) def get_r_ea(mol, configs, e, at): """ Returns a nconf x 3 array, distances between electron e and atom at Parameters: e,at: integers, eletron and atom indices configs: nconf x nelec x 3 array Returns: epos-apos, electron-atom distances """ epos = configs[:, e, :] nconf = configs.shape[0] apos = np.outer( np.ones(nconf), np.array(mol._atom[at][1]) ) # nconf x 3 array, position of atom at return epos - apos def get_r_ea_i(mol, epos_rot, e, at): """ Returns a nconf x naip x 3 array, distances between the rotated electron (e) and the atom at Parameters: epos_rot: rotated positions of electron e, nconf x naip x 3 Returns: epos_rot-apos, (rotated) electron-atom distances """ nconf, naip = epos_rot.shape[0:2] apos = np.zeros( [nconf, naip, 3] ) # position of the atom, broadcasted into nconf x naip x 3 for aip in range(naip): apos[:, aip, :] = np.outer(np.ones(nconf), np.array(mol._atom[at][1])) return epos_rot - apos def get_v_l(mol, configs, e, at): """ Returns list of the l's, and a nconf x nl array, v_l values for each l: l= 0,1,2,...,-1 """ nconf = configs.shape[0] at_name = mol._atom[at][0] r_ea = np.linalg.norm(get_r_ea(mol, configs, e, at), axis=1) vl = generate_ecp_functors(mol._ecp[at_name][1]) Lmax = len(vl) v_l = np.zeros([nconf, Lmax]) for l in vl.keys(): # -1,0,1,... v_l[:, l] = vl[l](r_ea) return vl.keys(), v_l def get_wf_ratio(wf, epos_rot, e): """ Returns a nconf x naip array, which is the Psi(r_e(i))/Psi(r_e) values """ nconf, naip = epos_rot.shape[0:2] wf_ratio = np.zeros([nconf, naip]) for aip in range(naip): wf_ratio[:, aip] = wf.testvalue(e, epos_rot[:, aip, :]) return wf_ratio def get_P_l(mol, configs, weights, epos_rot, l_list, e, at): """ Returns a nconf x naip x nl array, which is the legendre function values for each l channel. The factor (2l+1) and the quadrature weights are included. Parameters: l_list: [-1,0,1,...] list of given angular momenta weights: integration weights Return: P_l values: nconf x naip x nl array """ # at_name = mol._atom[at][0] nconf, naip = epos_rot.shape[0:2] P_l_val = np.zeros([nconf, naip, len(l_list)]) r_ea = get_r_ea(mol, configs, e, at) # nconf x 3 r_ea_i = get_r_ea_i(mol, epos_rot, e, at) # nconf x naip x 3 rdotR = np.zeros(r_ea_i.shape[0:2]) # nconf x naip # get the cosine values for aip in range(naip): rdotR[:, aip] = ( r_ea[:, 0] * r_ea_i[:, aip, 0] + r_ea[:, 1] * r_ea_i[:, aip, 1] + r_ea[:, 2] * r_ea_i[:, aip, 2] ) rdotR[:, aip] /= np.linalg.norm(r_ea, axis=1) * np.linalg.norm( r_ea_i[:, aip, :], axis=1 ) # print('cosine values',rdotR) # already included the factor (2l+1), and the integration weights here for l in l_list: P_l_val[:, :, l] = ( (2 * l + 1) * P_l(rdotR, l) * np.outer(np.ones(nconf), weights) ) return P_l_val ######################################################################### def ecp_ea(mol, configs, wf, e, at): """ Returns the ECP value between electron e and atom at, local+nonlocal. """ l_list, v_l = get_v_l(mol, configs, e, at) naip = 6 if len(l_list) > 2: naip = 12 weights, epos_rot = get_rot(mol, configs, e, at, naip) P_l = get_P_l(mol, configs, weights, epos_rot, l_list, e, at) ratio = get_wf_ratio(wf, epos_rot, e) ecp_val = np.einsum("ij,ik,ijk->i", ratio, v_l, P_l) # compute the local part local_l = -1 ecp_val += v_l[:, local_l] return ecp_val def ecp(mol, configs, wf): """ Returns the ECP value, summed over all the electrons and atoms. """ nconf, nelec = configs.shape[0:2] ecp_tot = np.zeros(nconf) if mol._ecp != {}: for e in range(nelec): for at in range(len(mol._atom)): ecp_tot += ecp_ea(mol, configs, wf, e, at) return ecp_tot #################### Quadrature Rules ############################ def get_rot(mol, configs, e, at, naip): """ Returns the integration weights (naip), and the positions of the rotated electron e (nconf x naip x 3) Parameters: configs[:,e,:]: epos of the electron e to be rotated Returns: weights: naip array epos_rot: positions of the rotated electron, nconf x naip x 3 """ nconf = configs.shape[0] apos = np.outer(np.ones(nconf), np.array(mol._atom[at][1])) r_ea_vec = get_r_ea(mol, configs, e, at) r_ea = np.linalg.norm(r_ea_vec, axis=1) # t and p are sampled randomly over a sphere around the atom t = np.random.uniform(low=0.0, high=np.pi, size=nconf) p = np.random.uniform(low=0.0, high=2 * np.pi, size=nconf) # rotated unit vectors: i_rot, j_rot, k_rot = ( np.zeros([nconf, 3]), np.zeros([nconf, 3]), np.zeros([nconf, 3]), ) i_rot[:, 0] = np.cos(p - np.pi / 2.0) i_rot[:, 1] = np.sin(p - np.pi / 2.0) j_rot[:, 0] = np.sin(t + np.pi / 2.0) * np.cos(p) j_rot[:, 1] = np.sin(t + np.pi / 2.0) * np.sin(p) j_rot[:, 2] = np.cos(t + np.pi / 2.0) k_rot[:, 0] = np.sin(t) * np.cos(p) k_rot[:, 1] = np.sin(t) * np.sin(p) k_rot[:, 2] = np.cos(t) d1, d2 = np.zeros(naip), np.zeros(naip) if naip == 6: d1[1] = np.pi d1[2] = np.pi / 2.0 d1[3] = np.pi / 2.0 d2[3] = np.pi d1[4] = np.pi / 2.0 d2[4] = np.pi / 2.0 d1[5] = np.pi / 2.0 d2[5] = 3.0 * np.pi / 2.0 elif naip == 12: d1[1] = np.pi fi0 = np.pi / 5.0 tha = np.arccos(1.0 / np.sqrt(5.0)) for i in range(5): rk2 = 2 * i d1[i + 2] = tha d2[i + 2] = rk2 * fi0 d1[i + 7] = np.pi - tha d2[i + 7] = (rk2 + 1) * fi0 epos_rot = np.zeros([nconf, naip, 3]) for aip in range(naip): for d in range(3): epos_rot[:, aip, d] = apos[:, d] + r_ea * ( np.sin(d1[aip]) * np.cos(d2[aip]) * i_rot[:, d] + np.sin(d1[aip]) * np.sin(d2[aip]) * j_rot[:, d] + np.cos(d1[aip]) * k_rot[:, d] ) weights = 1.0 / naip * np.ones(naip) return weights, epos_rot
nilq/baby-python
python
#!/usr/bin/python3.8 import os import requests import argparse import queue import threading import logging DOWNLOAD_THREADS = 6 IO_THREADS = 2 download_queue = queue.Queue() write_queue = queue.Queue() files_lock = threading.Lock() def download_worker(download_queue, write_queue, url, chunk_size): finished = False logging.info("Download thread started") while not finished: chunk_offset = download_queue.get() if chunk_offset is not None: chunk_end = chunk_offset + chunk_size - 1 logging.debug(f"GET: {url} - {chunk_offset}-{chunk_end}") resume_headers = {} if chunk_size != 0: resume_headers = {"Range":f"bytes={chunk_offset}-{chunk_end}"} r = requests.get(url, stream=True, headers=resume_headers) chunk = r.content write_queue.put((chunk_offset, chunk_size, chunk)) else: finished = True download_queue.task_done() def io_worker(write_queue, file_size, file_chunk_size, files): finished = False while not finished: task = write_queue.get() if task is not None: chunk_offset, chunk_size, chunk = task file_start = file_chunk_size * (chunk_offset // file_chunk_size) # Calculate which file the received chunk belongs to file_offset = chunk_offset - file_start # Calculate where in the file to put the chunk filename = f"{file_start}.dat" logging.debug(f"File: {filename}. {chunk_offset} -> {file_start}+{file_offset}") if file_offset + chunk_size > file_chunk_size: # If not all of the chunk fits in current file, put rest back for later overflow = file_offset + chunk_size - file_chunk_size new_offset = chunk_offset + file_chunk_size - file_offset logging.debug(f"File: {filename}. Splitting {overflow} bytes to next file at {new_offset} ") write_queue.put((new_offset, overflow, chunk[-overflow:])) chunk = chunk[:-overflow] chunk_size = file_chunk_size - file_offset with files_lock: with files[file_start]: if os.path.isfile(filename): with open(filename, "r+b") as f: f.seek(file_offset) f.write(chunk) else: with open(filename, "wb") as f: logging.debug(f"Starting new file {filename}") if file_size < file_start + file_chunk_size: newfile = file_size - file_start else: newfile = file_chunk_size f.truncate(newfile) f.seek(file_offset) f.write(chunk) else: finished = True write_queue.task_done() def main(url, http_chunk, file_chunk): logging.basicConfig(level=logging.DEBUG, format='%(relativeCreated)6d %(threadName)s %(message)s') r = requests.head(url, allow_redirects=True) if not r.ok: logging.error(f"Server returned {r.status_code}, aborting") return size = int(r.headers.get("Content-Length", -1)) ranges = r.headers.get("Accept-Ranges","none") if ranges.lower() == "none": logging.error("Remote server doesn't support download ranges, aborting") return http_chunks = [i * http_chunk for i in range( 1 + size // http_chunk)] file_chunks = {i * file_chunk : threading.Lock() for i in range( 1 + size // file_chunk)} logging.debug(f"{size}: {http_chunks}") for chunk_start in http_chunks: download_queue.put(chunk_start) download_workers = [] io_workers = [] for thread_no in range(DOWNLOAD_THREADS): t = threading.Thread(target=download_worker, kwargs={ "download_queue": download_queue, "write_queue": write_queue, "url": url, "chunk_size": http_chunk}) t.start() download_workers.append(t) download_queue.put(None) for thread_no in range(IO_THREADS): t = threading.Thread(target=io_worker, kwargs={ "write_queue": write_queue, "file_size": size, "file_chunk_size": file_chunk, "files": file_chunks}, daemon=True) t.start() io_workers.append(t) download_queue.join() write_queue.join() if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("url", help="URL to download") parser.add_argument("http_chunk", help="Size (in bytes) of download chunk", type=int) parser.add_argument("file_chunk", help="Size (in bytes) of output file chunk", type=int) args = parser.parse_args() main(args.url, args.http_chunk, args.file_chunk)
nilq/baby-python
python
from spendfrom import determine_db_dir class Options(object): def __init__(self): self.fromaddresses = list() self.toaddress = None self.new = False self.datadir = determine_db_dir() self.conffile = "crown.conf" self.fee = "0.025" self.amount = None self.upto = None self.select = None self.passphrase = None self.pswdcanceled = False self.testnet = False
nilq/baby-python
python
# Need to import path to test/fixtures and test/scripts/ # Ex : export PYTHONPATH='$PATH:/root/test/fixtures/:/root/test/scripts/' # # To run tests, you can do 'python -m testtools.run tests'. To run specific tests, # You can do 'python -m testtools.run -l tests' # Set the env variable PARAMS_FILE to point to your ini file. Else it will try to pick params.ini in PWD # import os from novaclient import client as mynovaclient from novaclient import exceptions as novaException import unittest import fixtures import testtools import traceback from contrail_test_init import * from vn_test import * from quantum_test import * from vnc_api_test import * from nova_test import * from vm_test import * from connections import ContrailConnections from floating_ip import * from policy_test import * from multiple_vn_vm_test import * from contrail_fixtures import * from tcutils.wrappers import prepost_wrapper from tcutils.poc import (TemplateTestCase, template, Call) from test_arguments import * class TestSanityFixture(testtools.TestCase, fixtures.TestWithFixtures): __metaclass__ = TemplateTestCase # @classmethod def setUp(self): super(TestSanityFixture, self).setUp() if 'PARAMS_FILE' in os.environ: self.ini_file = os.environ.get('PARAMS_FILE') else: self.ini_file = 'params.ini' self.inputs = self.useFixture(ContrailTestInit(self.ini_file)) self.connections = ContrailConnections(self.inputs) self.quantum_fixture = self.connections.quantum_fixture self.nova_fixture = self.connections.nova_fixture self.vnc_lib = self.connections.vnc_lib self.logger = self.inputs.logger self.agent_inspect = self.connections.agent_inspect self.cn_inspect = self.connections.cn_inspect # end setUpClass def cleanUp(self): super(TestSanityFixture, self).cleanUp() # end cleanUp def runTest(self): pass # end runTest @template(env.test_vn_add_delete_params) @preposttest_wrapper def test_vn_add_delete(self, vn_name, vn_subnets): '''Test to validate VN creation and deletion. ''' vn_obj = self.useFixture( VNFixture( project_name=self.inputs.project_name, connections=self.connections, vn_name=vn_name, inputs=self.inputs, subnets=vn_subnets)) assert vn_obj.verify_on_setup() assert vn_obj return True # end # end TestSanityFixture
nilq/baby-python
python
# -*- coding: utf-8 -*- """ @author: Jatin Goel """ import os from flask import Flask APP = Flask(__name__) APP.config['SQLALCHEMY_DATABASE_URI'] = ( 'sqlite:///' f'{os.path.join(os.getcwd(), "database.db")}' ).replace("\\", "/") APP.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False APP.config['SECRET_KEY'] = 'SpiceworksHealthCheckerApp'
nilq/baby-python
python
import unittest from code.core.enumerations import * import json class TestEnumBase(unittest.TestCase): @classmethod def dummy(cls, One = 1, Two = 2, Three = 3, Four = 4): pass # class TestEnumBase class TestEnum(TestEnumBase): @enumeration class ParsingSample: One = 1 Two = 2 Three = 3 DifficultCase = TestEnumBase.dummy( One = 10 ) MoreDifficultCase = TestEnumBase.dummy( \ Two = 11) EvenMoreDifficultCase = TestEnumBase.dummy( \ Three = 13) Last = Enum.Auto # class ParsingSample def testParsing(self): enum = self.ParsingSample count = len(enum) self.assertEqual(count, 7) # testParsing def testJson(self): enum = self.ParsingSample count = len(enum) dictionary = enum.dictionary() jsonString = json.dumps(dictionary) self.assertEqual(jsonString, '{"One": 1, "Two": 2, "Three": 3, "DifficultCase": null, "MoreDifficultCase": null, "EvenMoreDifficultCase": null, "Last": 4}') # testJson def testConversions(self): enum = self.ParsingSample length = len(enum) sameLength = enum.length() firstMember = enum[0] secondMember = enum("Two") # not a constructor! self.assertEqual(firstMember, enum.One) self.assertEqual(secondMember, enum.Two) self.assertEquals(length, 7) self.assertEquals(length, sameLength) # testConversions # class TestEnum if __name__ == '__main__': unittest.main()
nilq/baby-python
python
import json def load_expressions(): """Returns the expressions_file loaded from JSON""" with open('../data/BotCycle/expressions.json') as expressions_file: return json.load(expressions_file) def load_intents(): """Returns a list of names of the intents""" with open('../data/BotCycle/entities/intent.json') as intents_file: intents = json.load(intents_file) return list(map(lambda x: x['value'], intents['data']['values'])) def get_train_data(expressions): """Returns a list of tuples (text, intent)""" array = expressions['data'] result = [] # substitute the entities with their name for sentence in array: text = sentence['text'] intent = None for entity in sentence['entities']: if entity['entity'] != 'intent': text = text.replace(entity['value'].strip( '"'), entity['entity'].upper()) else: intent = entity['value'].strip('"') result.append((text, intent)) # print(result) return result def main(): expressions = load_expressions() return get_train_data(expressions) if __name__ == '__main__': print(main())
nilq/baby-python
python
import os import h5py import numpy as np def copy_file_struct(f, hmap, fo): """ Iteratively copy a HDF5 file structure to a new file Arguments: -f : File from which to copy [OPEN HDF5 file handle] -hmap : Current file object of interest to copy from -fo : File to copy structure to [OPEN HDF5 file handle] """ # First, see if object is a group if type(hmap) == h5py._hl.group.Group: name = hmap.name.encode("ascii") # Copy attributes associated with group atts = f[name].attrs.keys() if len(atts) > 0: group = fo.create_group(name) for v in atts: group.attrs[v] = f[name].attrs[v] # Now deal with subgroups and datasets for w in hmap: if type(f[name][w]) == h5py._hl.group.Group: atts = f[name][w].attrs.keys() if len(atts) > 0: group = fo.create_group("{0}/{1}".format(name, w)) for v in atts: group.attrs[v] = f[name][w].attrs[v] copy_file_struct(f, f[name][w], fo) elif type(f[name][w]) == h5py._hl.dataset.Dataset: tmp = np.zeros(f[name][w][:].shape, dtype=f[name][w][:].dtype) tmp[:] = f[name][w][:] dset = fo.create_dataset( "{0}/{1}".format(name.decode("utf-8"), w), data=tmp, dtype=f[name][w][:].dtype, ) del tmp atts = f[name][w].attrs.keys() if len(atts) > 0: for v in atts: dset.attrs[v] = f[name][w].attrs[v] # Otherwise deal with the dataset elif type(hmap) == h5py._hl.dataset.Dataset: name = hmap.name.encode("ascii") tmp = np.zeros(f[name][:].shape, dtype=f[name][:].dtype) tmp[:] = f[name][:] dset = fo.create_dataset(name.decode("utf-8"), data=tmp, dtype=f[name][:].dtype) atts = f[name].attrs.keys() if len(atts) > 0: for v in atts: dset.attrs[v] = f[name].attrs[v] del tmp return def merge_outputs(outputs): """ Merge all outputs from different processors into one file Arguments: -outputs : Search term for output files of interest [STRING] """ print(" > Merging {0}*".format(outputs), flush=True) # Find outputs of interest files = [] for y in os.listdir("output/"): if y.startswith(outputs): files.append("output/{0}".format(y)) files.sort() # Create output file flib = "output/{0}.hdf5".format(outputs) outf = h5py.File(flib, "w") # Loop over files of interest, iteratively copying data to consolidated output file for y in files: if y == flib: continue print(" -{0}".format(y), flush=True) f = h5py.File(y, "r") fk = sorted(f.keys()) for z in fk: print(" --> {0}".format(z), flush=True) copy_file_struct(f, f[z], outf) f.close() os.remove(y) outf.close() del files, flib return
nilq/baby-python
python
""" The child process. """ import time from asyncio import get_event_loop from typing import Callable, Optional from prompt_toolkit.eventloop import call_soon_threadsafe from .backends import Backend from .key_mappings import prompt_toolkit_key_to_vt100_key from .screen import BetterScreen from .stream import BetterStream __all__ = ["Process"] class Process: """ Child process. Functionality for parsing the vt100 output (the Pyte screen and stream), as well as sending input to the process. Usage: p = Process(loop, ...): p.start() :param invalidate: When the screen content changes, and the renderer needs to redraw the output, this callback is called. :param bell_func: Called when the process does a `bell`. :param done_callback: Called when the process terminates. :param has_priority: Callable that returns True when this Process should get priority in the event loop. (When this pane has the focus.) Otherwise output can be delayed. """ def __init__( self, invalidate: Callable[[], None], backend: Backend, bell_func: Optional[Callable[[], None]] = None, done_callback: Optional[Callable[[], None]] = None, has_priority: Optional[Callable[[], bool]] = None, ) -> None: self.loop = get_event_loop() self.invalidate = invalidate self.backend = backend self.done_callback = done_callback self.has_priority = has_priority or (lambda: True) self.suspended = False self._reader_connected = False # Create terminal interface. self.backend.add_input_ready_callback(self._read) if done_callback is not None: self.backend.ready_f.add_done_callback(lambda _: done_callback()) # Create output stream and attach to screen self.sx = 0 self.sy = 0 self.screen = BetterScreen( self.sx, self.sy, write_process_input=self.write_input, bell_func=bell_func ) self.stream = BetterStream(self.screen) self.stream.attach(self.screen) def start(self) -> None: """ Start the process: fork child. """ self.set_size(120, 24) self.backend.start() self.backend.connect_reader() def set_size(self, width: int, height: int) -> None: """ Set terminal size. """ if (self.sx, self.sy) != (width, height): self.backend.set_size(width, height) self.screen.resize(lines=height, columns=width) self.screen.lines = height self.screen.columns = width self.sx = width self.sy = height def write_input(self, data: str, paste: bool = False) -> None: """ Write user key strokes to the input. :param data: (text, not bytes.) The input. :param paste: When True, and the process running here understands bracketed paste. Send as pasted text. """ # send as bracketed paste? if paste and self.screen.bracketed_paste_enabled: data = "\x1b[200~" + data + "\x1b[201~" self.backend.write_text(data) def write_key(self, key: str) -> None: """ Write prompt_toolkit Key. """ data = prompt_toolkit_key_to_vt100_key( key, application_mode=self.screen.in_application_mode ) self.write_input(data) def _read(self) -> None: """ Read callback, called by the loop. """ d = self.backend.read_text(4096) assert isinstance(d, str), "got %r" % type(d) # Make sure not to read too much at once. (Otherwise, this # could block the event loop.) if not self.backend.closed: def process() -> None: self.stream.feed(d) self.invalidate() # Feed directly, if this process has priority. (That is when this # pane has the focus in any of the clients.) if self.has_priority(): process() # Otherwise, postpone processing until we have CPU time available. else: self.backend.disconnect_reader() def do_asap(): " Process output and reconnect to event loop. " process() if not self.suspended: self.backend.connect_reader() # When the event loop is saturated because of CPU, we will # postpone this processing max 'x' seconds. # '1' seems like a reasonable value, because that way we say # that we will process max 1k/1s in case of saturation. # That should be enough to prevent the UI from feeling # unresponsive. timestamp = time.time() + 1 call_soon_threadsafe(do_asap, max_postpone_time=timestamp) else: # End of stream. Remove child. self.backend.disconnect_reader() def suspend(self) -> None: """ Suspend process. Stop reading stdout. (Called when going into copy mode.) """ if not self.suspended: self.suspended = True self.backend.disconnect_reader() def resume(self) -> None: """ Resume from 'suspend'. """ if self.suspended: self.backend.connect_reader() self.suspended = False def get_cwd(self) -> str: """ The current working directory for this process. (Or `None` when unknown.) """ return self.backend.get_cwd() def get_name(self) -> str: """ The name for this process. (Or `None` when unknown.) """ # TODO: Maybe cache for short time. return self.backend.get_name() def kill(self) -> None: """ Kill process. """ self.backend.kill() @property def is_terminated(self) -> bool: return self.backend.closed
nilq/baby-python
python
# Generated by Django 3.2 on 2022-02-19 13:03 from django.conf import settings from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('products', '0005_remove_product_isfavourite'), ] operations = [ migrations.CreateModel( name='Reviews', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('review_title', models.TextField(max_length=254)), ('review_body', models.TextField(max_length=1000)), ('date_added', models.DateTimeField(auto_now_add=True)), ('active', models.BooleanField(default=True)), ('author', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), ('product', models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, related_name='reviews', to='products.product')), ], options={ 'ordering': ['-review_title'], }, ), ]
nilq/baby-python
python
# -*- coding: utf-8 -*- import scrapy class AdzanItem(scrapy.Item): c = scrapy.Field() # city d = scrapy.Field() # day s = scrapy.Field() # shubuh t = scrapy.Field() # terbit z = scrapy.Field() # zuhur a = scrapy.Field() # ashr m = scrapy.Field() # maghrib i = scrapy.Field() # isya
nilq/baby-python
python
import os import sys from os import path from flask import Flask from flask.ext.login import LoginManager from flask_sqlalchemy import SQLAlchemy from flask_recaptcha import ReCaptcha app = Flask(__name__) sys.path.append(path.dirname(path.dirname(path.abspath(__file__)))) #config for Forms, Register and FCM tokens app.config.update(dict( DEBUG = True, SECRET_KEY = 'you-will-never-guess', SECURITY_PASSWORD_SALT = 'my_precious_two', FCM_APP_TOKEN = 'AAAAXUWoieY:APA91bGcVQ67M5mAEl7e2OSb5yKko8J17NH7GZtOspoq9NKjnHMyD9RiCePjLKUHfyBzn4II0aVJx_JnyyBHQijdbT6sYwxAoDrI15bZX_0FdBpHKgAVqMBpKMQAxIggXxakcZ3It54f', RECAPTCHA_ENABLED = True, RECAPTCHA_SITE_KEY = '6LetACUUAAAAAPckPB-tmBZdLo9eZDp5tacC1XA9', RECAPTCHA_SECRET_KEY = '6LetACUUAAAAAMUPZ3N1gjDO1AHxq8AVAXau9Fg-', RECAPTCHA_THEME = 'light')) #recaptcha init recaptcha = ReCaptcha() recaptcha.init_app(app) #conection to database app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL'] db = SQLAlchemy(app) #Configure Flask-Login login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = 'login' ### move to other module and resolve problem with second import models (Table 'user' is already defined for this MetaData instance) import config_celery #Configure Celery app.config.update(CELERY_BROKER_URL=os.environ['REDIS_URL'], CELERY_RESULT_BACKEND=os.environ['REDIS_URL']) celery = config_celery.make_celery(app) ### #User types #adm - admin, usr - regular user, oth - for later use def enum(**enums): return type('Enum', (), enums) UserType = enum(adm=1, usr=2, oth=3) ServiceState = enum(up=1, down=2, unspecified=3) from webapp import tasks from webapp import views
nilq/baby-python
python
""" Standalone program to copy a file to azure, and return the URL to access the file. The idea is that you can run from the command line and escape to shell because sometimes the write takes awhile. Then look in the log file to get the URL for sharing. To test: run copy_to_azure To copy a file: python copy_to_azure.py -fn [string of full path] > az.log & Note that the last line of screen output from extract_box gives you the [string of full path]. """ from azure.storage.blob import BlobServiceClient from azure.storage.blob import PublicAccess from lo_tools import Lfun import argparse from time import time from datetime import datetime from pathlib import Path # get and process arguments parser = argparse.ArgumentParser() parser.add_argument('-fn', type=str) # full path to input file parser.add_argument('-out_name', type=str) # name of output file parser.add_argument('-container_name', type=str, default='pm-share') args = parser.parse_args() fn = args.fn out_name = args.out_name container_name = args.container_name Ldir = Lfun.Lstart() if fn == None: fn = Ldir['data'] / 'accounts' / 'az_testfile.txt' else: fn = Path(fn) if out_name == None: out_name = fn.name # screen output print(' copy_to_azure '.center(60,'=')) print(datetime.now().strftime('%Y.%m.%d %H:%M:%S')) print('Copying: %s' % (fn)) tt0 = time() # get the blob_service account_fn = Ldir['data'] / 'accounts' / 'azure_pm_2015.05.25.txt' with account_fn.open() as aa: # make sure to strip newline characters account = aa.readline().strip() key = aa.readline().strip() connection_string = ('DefaultEndpointsProtocol=https' + ';AccountName=' + account + ';AccountKey=' + key + ';EndpointSuffix=core.windows.net') blob_service = BlobServiceClient.from_connection_string(conn_str=connection_string) try: # create the container if needed blob_service.create_container(container_name, public_access=PublicAccess.Container) except: pass # assume error is because container exists # write it to Azure try: from azure.storage.blob import BlobClient blob = BlobClient.from_connection_string(conn_str=connection_string, container_name=container_name, blob_name=out_name) with fn.open('rb') as data: blob.upload_blob(data, overwrite=True) az_url = ('https://pm2.blob.core.windows.net/' + container_name + '/' + out_name) print('URL to access file: %s' % (az_url)) print('Took %0.2f sec' % (time()-tt0)) except Exception as e: print('Copy failed: %s' % (str(e)))
nilq/baby-python
python
#!/usr/bin/env python # Copied from https://github.com/xantares/mingw-ldd/blob/master/mingw-ldd.py # Modified to point to right prefix location on Fedora. # WTFPL - Do What the Fuck You Want to Public License from __future__ import print_function import pefile import os import sys def get_dependency(filename): deps = [] pe = pefile.PE(filename) for imp in pe.DIRECTORY_ENTRY_IMPORT: deps.append(imp.dll.decode()) return deps def dep_tree(root, prefix=None): if not prefix: arch = get_arch(root) #print('Arch =', arch) prefix = '/usr/'+arch+'-w64-mingw32/sys-root/mingw/bin' #print('Using default prefix', prefix) dep_dlls = dict() def dep_tree_impl(root, prefix): for dll in get_dependency(root): if dll in dep_dlls: continue full_path = os.path.join(prefix, dll) if os.path.exists(full_path): dep_dlls[dll] = full_path dep_tree_impl(full_path, prefix=prefix) else: dep_dlls[dll] = 'not found' dep_tree_impl(root, prefix) return (dep_dlls) def get_arch(filename): type2arch= {pefile.OPTIONAL_HEADER_MAGIC_PE: 'i686', pefile.OPTIONAL_HEADER_MAGIC_PE_PLUS: 'x86_64'} pe = pefile.PE(filename) try: return type2arch[pe.PE_TYPE] except KeyError: sys.stderr.write('Error: unknown architecture') sys.exit(1) if __name__ == '__main__': filename = sys.argv[1] for dll, full_path in dep_tree(filename).items(): print(' ' * 7, dll, '=>', full_path)
nilq/baby-python
python
import torch import torch.utils.data as tud import numpy as np from utils import create_space import os # from PIL import Image, ImageFile import torchvision.transforms.functional as TF from collections import defaultdict import rasterio from rasterio.windows import Window import pyproj # ImageFile.LOAD_TRUNCATED_IMAGES = True class LandsatViirs(tud.Dataset): """ A data loader that samples pairs of Landsat and VIIRS images for matching land areas. """ def __init__( self, df, landsat_transform, viirs_transform ): self.df = df self.landsat_transform = landsat_transform self.viirs_transform = viirs_transform self.idxs = df.index.to_list() def __len__(self): return len(self.df) def __zero_pad_pathrow(self, path, row): return ("000" + str(path))[-3:] + ("000" + str(row))[-3:] def __getitem__(self, idx): idx = self.idxs[idx] cols = ['# lon', 'lat', 'path', 'row', 'B2_link', 'B3_link', 'B4_link'] lon, lat, path, row, B2_url, B3_url, B4_url = self.df.loc[idx, cols] # 0 pad !!!! < --------------------------------------------------------v------- pathrow = self.__zero_pad_pathrow(path, row) landsat = self.landsat_transform((lon, lat), pathrow, (B2_url, B3_url, B4_url)) viirs = self.viirs_transform((lon, lat)) return landsat, viirs class LandsatTransform: """ A callable object that, given a pair of coordinates, returns the Landsat image formatted as a 3D Tensor [bands, height, width]. """ def __init__(self, xdim=224, ydim=224): """ Inputs: None """ self.coord_imgs = {} # self.pathrow_tifs = {} self.xdim = xdim self.ydim = ydim if not os.path.isdir('landsat'): os.mkdir('landsat') def _get_tif(self, img_urls, pathrow): rgb_path = 'landsat/' + pathrow + '.tiff' if not os.path.isfile(rgb_path): self._fetch_tif_from_s3(img_urls, pathrow) return rgb_path def _fetch_tif_from_s3(self, img_urls, pathrow): """ Given a tuple of urls to R, G, and B TIF bands for a single scene, return a single TIF """ rgb_path = 'landsat/' + pathrow + '.tiff' r_url, g_url, b_url = img_urls b2 = rasterio.open(r_url) b3 = rasterio.open(g_url) b4 = rasterio.open(b_url) with rasterio.open(rgb_path, 'w+', driver='Gtiff', width=b2.width, height=b2.height, count=3, crs=b2.crs, tranform=b2.transform, dtype='float32') as rgb: rgb.write(b2.read(1), 1) rgb.write(b3.read(1), 2) rgb.write(b4.read(1), 3) b2.close() b3.close() b4.close() return def __call__(self, coord, pathrow, img_urls): """ Extracts the 21x21 sub-array from the Landsat scene corresponding to the provided coordinates, and returns a normalized 3D tensor. Input: coord (tuple of 2 floats) pathrow (str) img_urls: tuple of urls to R, G, and B TIF bands to a single scene Returns a 3D tensor. """ r_url, b_url, g_url = img_urls lon, lat = coord # If we already have the 3d tensor, just return it if coord in self.coord_imgs: return self.coord_imgs[coord].new_tensor() else: # Get TIF with 3 bands tif = rasterio.open(self._get_tif(img_urls, pathrow), 'r') # Extract 224x224 subarray from landsat scene min_lat, min_lon, max_lat, max_lon = create_space(lat, lon) utm = pyproj.Proj(tif.crs) lonlat = pyproj.Proj(init='epsg:4326') east, north = pyproj.transform(lonlat, utm, max_lon, max_lat) west, south = pyproj.transform(lonlat, utm, min_lon, min_lat) north_idx, west_idx = tif.index(west, north) south_idx, east_idx = tif.index(east, south) raw_array = tif.read(window=Window( west_idx, north_idx, abs(west_idx - east_idx), abs(north_idx - south_idx) )) tif.close() # Convert array to tensor landsat_tensor = torch.tensor(raw_array).type(torch.FloatTensor) # resize tensor landsat_tensor = TF.resize( landsat_tensor, size = (self.ydim, self.xdim) ) # Add tensor to dict self.coord_imgs[coord] = landsat_tensor return landsat_tensor.new_tensor() class ViirsTransform: """ A callable object that, given a pair of coordinates, returns a the VIIIRS Day/Night Band image formatted as a 3D Tensor [bands, height, width]. """ def __init__(self, tif, subsetBBox=None): """ Inputs: tif (rasterio.DatasetReader) subsetBBox: tuple of 2 coords (float,float) representing ((min lon, min lat), (max lon, max lat)) of the desired subset. All points must be in WGS84. """ self.tif = tif if subsetBBox: (west, south), (east, north) = subsetBBox miny, minx = tif.index(west, south) maxy, maxx = tif.index(east, north) height = abs(maxy - miny) width = abs(maxx - minx) self.col_offset = minx self.row_offset = maxy self.tif_data = tif.read(window=Window(minx, maxy, width, height)) else: self.col_offset = 0 self.row_off = 0 self.tif_data = tif.read() def __call__(self, coord): """ Extracts the 21x21 sub-array from the the full VIIRS tile set corresponding to the provided coordinates, and returns a normalized 3D tensor. Normalization: output = ln(input + 1)/ln(max) Where max = 92,000 radiance in our dataset. Maps to [0,1] and reduces outsize influence of outliers. Input: coord (tuple of 2 floats) Returns a 3D tensor. """ lon, lat = coord min_lat, min_lon, max_lat, max_lon = create_space(lat, lon) row, col = self.tif.index(min_lon, max_lat) row -= self.row_offset col -= self.col_offset array = self.tif_data[:, row:row+21, col:col+21] viirs_tensor = torch.tensor(array.reshape((-1,21,21))).type(torch.FloatTensor) return torch.clamp(torch.log(viirs_tensor + 1) / 11.43, min=0, max=1) ############################# DEPRECATED ##################################### # class deprecated_LandsatTransform: # """ # A callable object that, given a pair of coordinates, returns a the # Landsat image formatted as a 3D Tensor [bands, height, width]. # """ # def __init__(self, base_path, width=256, height=256): # self.base_path = base_path # self.width = width # self.height = height # def __call__(self, image_name, country='ng'): # path = '/'.join([self.base_path, country, image_name]) # img = Image.open(path) # img = img.resize((self.width, self.height)) # return TF.pil_to_tensor(img.convert('RGB')).type(torch.FloatTensor)/255 # class deprecated_ViirsTransform: # """ # A callable object that, given a pair of coordinates, returns a the # VIIIRS Day/Night Band image formatted as a 3D Tensor [bands, height, width]. # """ # def __init__(self, tifs): # """ # Inputs: # tif # """ # self.tifs = { # 'ng' : tifs[1], # 'eth' : tifs[1], # 'mw' : tifs[0] # } # tif0_data = tifs[0].get_data() # tif1_data = tifs[1].get_data() # self.arrays = { # 'ng' : tif1_data, # 'eth' : tif1_data, # 'mw' : tif0_data # } # def __call__(self, coord, country): # """ # Extracts the 21x21 sub-array from the the full VIIRS tile set # corresponding to the provided coordinates, # and returns a normalized 3D tensor. # Normalization: # output = ln(input + 1)/ln(max) # Where max = 92,000 radiance in our dataset. # Maps to [0,1] and reduces outsize influence of outliers. # Input: # coord (tuple of 2 floats) # country (str): One of ['eth', 'mw', 'ng'] # Returns a 3D tensor. # """ # min_lat, min_lon, max_lat, max_lon = create_space( # coord[0], coord[1]) # xminPixel, ymaxPixel = self.tifs[country].proj_to_raster(min_lon, min_lat) # xminPixel, ymaxPixel = int(xminPixel), int(ymaxPixel) # array = self.arrays[country][:, ymaxPixel-21:ymaxPixel, xminPixel:xminPixel+21] # viirs_tensor = torch.tensor(array.reshape((-1,21,21))).type(torch.FloatTensor) # return torch.clamp( # (torch.log(viirs_tensor + 1) / 11.43), # min=0, max=1 # ) # class LandsatDataset(tud.Subset): # """ # A data loader that samples pairs of Landsat # and VIIRS images for matching land areas. # """ # def __init__( # self, df, landsat_transform # ): # self.df = df # self.landsat_transform = landsat_transform # self.idxs = df.index.to_list() # def __len__(self): # return len(self.df) # def __getitem__(self, idx): # idx = self.idxs[idx] # cols = ['image_lat', 'image_lon', 'image_name', 'country'] # lat, lon, img, country = self.df.loc[idx, cols] # landsat = self.landsat_transform(img, country) # return landsat # class ViirsDataset(tud.Subset): # """ # A data loader that samples pairs of Landsat # and VIIRS images for matching land areas. # """ # def __init__( # self, df, viirs_transform # ): # self.df = df # self.viirs_transform = viirs_transform # self.idxs = df.index.to_list() # def __len__(self): # return len(self.df) # def __getitem__(self, idx): # idx = self.idxs[idx] # cols = ['image_lat', 'image_lon', 'image_name', 'country'] # lat, lon, img, country = self.df.loc[idx, cols] # viirs = self.viirs_transform((lat, lon), country) # return viirs # class Landsat(tud.Dataset): # """ # A data loader that samples pairs of Landsat images. # """ # def __init__(self, df, landsat_transform): # self.df = df # self.landsat_transform = landsat_transform # self.idxs = df.index.to_list() # def __len__(self): # return len(self.df) # def __getitem__(self, idx): # idx = self.idxs[idx] # img, country = self.df.loc[idx, ['image_name', 'country']] # landsat = self.landsat_transform(img, country) # return landsat, landsat # class ToTensor(object): # def __init__(self, bands, height, width): # self.x = width # self.y = height # self.z = bands # def __call__(self, sample): # m = torch.from_numpy(sample).type(torch.float).reshape( # (self.z, self.y, self.x)) # return m # class MNIST(tud.Dataset): # def __init__(self, X, transform=ToTensor): # self.X = X # self.transform = transform(1, 28, 28) # def __len__(self): # return len(self.X) # def __getitem__(self, idx): # x = self.X[idx] # if self.transform: # x = self.transform(x) # return (x, x)
nilq/baby-python
python
from distutils.core import setup setup( name='page-objects', version='1.1.0', packages=['page_objects'], url='https://github.com/krizo/page-objects.git', license='MIT', author='Edward Easton', author_email='eeaston@gmail.com', description='Page Objects for Python', requires=['selenium', 'pytest', 'mock'] )
nilq/baby-python
python
from enum import Enum from typing import Union class CONNECTION_STYLE(Enum): angle = 1 angle_3 = 2 arc = 3 arc_3 = 4 bar = 5 def get_name(self) -> str: return { 'angle': 'angle', 'angle_3': 'angle3', 'arc': 'arc', 'arc_3': 'arc3', 'bar': 'bar' }[self.name] @staticmethod def get_connection_style( connection_style: Union[str, 'CONNECTION_STYLE'] ) -> str: if connection_style and isinstance(connection_style, CONNECTION_STYLE): connection_style = connection_style.get_name() return connection_style
nilq/baby-python
python
# Librerias Future from __future__ import unicode_literals # Librerias Django from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import render from django.views.generic import DetailView, ListView # Librerias de terceros from apps.website.submodels.post import PyPost def index(request): return render(request, 'index.html') def shop(request): return render(request, 'shop.html') def product(request): return render(request, 'product.html') def post(request): return render(request, 'post.html') def license(request): return render(request, 'license.html') def UnderConstruction(request): return render(request, 'under_construction.html') """ BLOG """ POST_FIELDS = [ {'string': 'Título', 'field': 'title'}, {'string': 'Creado en', 'field': 'created_on'}, {'string': 'Contenido', 'field': 'content'}, ] POST_FIELDS_SHORT = ['title','content','created_on'] class BlogView(LoginRequiredMixin, ListView): login_url = "login" model = PyPost template_name = 'blog.html' fields = POST_FIELDS paginate_by = 8 def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) return context class PostDetailView(LoginRequiredMixin, DetailView): login_url = "login" model = PyPost template_name = 'post.html' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) return context
nilq/baby-python
python
###################################################################################### ### This is a read-only file to allow participants to run their code locally. ### ### It will be over-writter during the evaluation, Please do not make any changes ### ### to this file. ### ###################################################################################### import traceback import os import signal from contextlib import contextmanager from os import listdir from os.path import isfile, join import soundfile as sf import numpy as np class TimeoutException(Exception): pass class MusicDemixingPredictor: def __init__(self, model_name='baseline'): self.test_data_path = os.getenv("TEST_DATASET_PATH", os.getcwd() + "/input/") self.results_data_path = os.getenv("RESULTS_DATASET_PATH", os.getcwd() + "/output") self.results = [] self.current_music_name = None self.model_name = model_name def get_all_music_names(self): return [file.split('.')[0] for file in listdir(self.test_data_path) if isfile(join(self.test_data_path, file))] def get_music_file_location(self, music_name, instrument=None): if instrument is None: return join(self.test_data_path, f"{music_name}.wav") if not os.path.exists(self.results_data_path): os.makedirs(self.results_data_path) return join(self.results_data_path, f"{music_name}_{instrument}.wav") def evaluation(self): self.prediction_setup() music_names = self.get_all_music_names() for music_name in music_names: print(f'{music_name}: {self.get_music_file_location(music_name)}') self.prediction(mixture_file_path=self.get_music_file_location(music_name), bass_file_path=self.get_music_file_location(music_name, "bass"), drums_file_path=self.get_music_file_location(music_name, "drums"), other_file_path=self.get_music_file_location(music_name, "other"), vocals_file_path=self.get_music_file_location(music_name, "vocals"), ) def run(self): try: self.evaluation() except Exception as e: print(traceback.format_exc())
nilq/baby-python
python
print('Cliente da Vagalume API Iniciado: \n') nome_artista = input('Digite o nome de um Artista\n') import busca resultado = busca.artista(nome_artista) newinput = input('Digite:\n-pos: para saber a posição do artista no ranking\n-album: para saber o ultimo álbum do artista\n-frequencia: para saber as palavras mais frequentes nas letras mais acessadas do artista\n-musicas n: para exibir as n musicas mais acessadas do artista (n = 0 para todas)\n') if newinput == '-pos': print ('\nA posição do artista é '+ resultado.rank.pos) elif newinput == '-frequencia': print('\nAs palavras mais frequêntes são:\n' + resultado.toplyrics.freq_palavras_toplyrics) elif newinput == '-album': print('\nO ultimo album do artista é: '+resultado.albuns.ultimo) else: newlist = newinput.split() resultado2 = busca.artista(nome_artista,int(newlist(1))) if newinput(0)=='-musicas': print('\nAs '+newlist(1)+'músicas mais acessadas do artista são: \n'+ resultado2.toplyrics.ntoplyrics)
nilq/baby-python
python
import json import logging from homeassistant.components.http import HomeAssistantView from .util import DOMAIN, XIAOAI_API, matcher_query_state, find_entity from .xiaoai import (XiaoAIAudioItem, XiaoAIDirective, XiaoAIOpenResponse, XiaoAIResponse, XiaoAIStream, XiaoAIToSpeak, XiaoAITTSItem, xiaoai_request, xiaoai_response) _LOGGER = logging.getLogger(__name__) # 文本回复 def build_text_message(to_speak, is_session_end, open_mic): xiao_ai_response = XiaoAIResponse( to_speak=XiaoAIToSpeak(type_=0, text=to_speak), open_mic=open_mic) response = xiaoai_response(XiaoAIOpenResponse(version='1.0', is_session_end=is_session_end, response=xiao_ai_response)) return response # 音乐回复 def build_music_message(to_speak, mp3_urls): all_list = [] if to_speak is not None: info_tts = XiaoAIDirective( type_='tts', tts_item=XiaoAITTSItem( type_='0', text=to_speak )) all_list.append(info_tts) for url in mp3_urls: info_audio = XiaoAIDirective( type_='audio', audio_item=XiaoAIAudioItem(stream=XiaoAIStream(url=url)) ) all_list.append(info_audio) xiao_ai_response = XiaoAIResponse(directives=all_list, open_mic=False) response = xiaoai_response(XiaoAIOpenResponse( version='1.0', is_session_end=True, response=xiao_ai_response)) return response # 格式转换 def parse_input(event, hass): req = xiaoai_request(event) text = req.query # 判断当前用户是否是自己 cfg = hass.data['conversation_voice'].api_config.get_config() user_id = cfg.get('user_id', '') if user_id != '' and user_id != req.session.user.user_id: return build_text_message('我真的好笨笨哦,不知道你在说啥,换个方式叫我吧', is_session_end=True, open_mic=False) # 插槽:req.request.slot_info.intent_name intent_name = '' if hasattr(req.request.slot_info, 'intent_name'): intent_name = req.request.slot_info.intent_name # 消息内容:req.query if req.request.type == 0: # 技能进入请求 if intent_name == 'Mi_Welcome': return build_text_message('欢迎使用您的家庭助理', is_session_end=False, open_mic=True) # 初始化识别内容 return conversation_process(hass, text, cfg) elif req.request.type == 1: # 退出意图 if intent_name == 'Mi_Exit' or ['没事了', '退下', '没有了', '没有', '没用了', '没了', '没有呢'].count(text) > 0: return build_text_message('再见了您!', is_session_end=True, open_mic=False) else: return conversation_process(hass, text, cfg) elif req.request.type == 2: return build_text_message('再见了您!', is_session_end=True, open_mic=False) return build_text_message('我没听懂欸', is_session_end=True, open_mic=False) # 消息处理 def conversation_process(hass, text, cfg): open_mic = cfg.get('open_mic', True) is_session_end = (open_mic == False) hass.async_create_task(hass.services.async_call('conversation', 'process', {'source': 'XiaoAi','text': text})) # 如果配置到了查询,则不进入系统意图 result = matcher_query_state(text) if result is not None: friendly_name = result state = find_entity(hass, friendly_name) if state is not None: message = f'{friendly_name}的状态是{state.state}' if open_mic: message += ',请问还有什么事吗?' return build_text_message(message, is_session_end, open_mic) message = '收到' if open_mic: message += ',还有什么事吗?' return build_text_message(message, is_session_end, open_mic) # 网关视图 class XiaoaiGateView(HomeAssistantView): url = XIAOAI_API name = DOMAIN requires_auth = False async def post(self, request): data = await request.json() _LOGGER.info('======= 小爱API接口信息 =========') _LOGGER.info(data) _LOGGER.info('======= 小爱API接口信息 =========') hass = request.app["hass"] response = parse_input(data, hass) return self.json(json.loads(response))
nilq/baby-python
python
from apistar import Include, Route from settings.config import DEBUG from views.spider import callback from views.crate import crate_list, crate_detail routes = [ Route('/', 'GET', crate_list), Route('/crate/{crate_id}', 'GET', crate_detail), Route('/spider', 'POST', callback), ] if DEBUG: from apistar.handlers import docs_urls, static_urls routes += [ Include('/docs', docs_urls), Include('/static', static_urls), ]
nilq/baby-python
python
#!/usr/bin/env python3 """Générateur de service AVAHI pour Freebox en mode bridge Lit les informations depuis http://mafreebox.freebox.fr/api_version Crée un fichier utilisable comme service avahi. Permet d’utiliser Freebox Compagnon avec une Freebox en mode Bridge : https://dev.freebox.fr/bugs/task/22301 """ import argparse import sys import logging import logging.handlers import os import urllib.request, json logger = logging.getLogger(os.path.splitext(os.path.basename(sys.argv[0]))[0]) class CustomFormatter(argparse.RawDescriptionHelpFormatter, argparse.ArgumentDefaultsHelpFormatter): pass def parse_args(args=sys.argv[1:]): """Parse arguments.""" parser = argparse.ArgumentParser( description=sys.modules[__name__].__doc__, formatter_class=CustomFormatter) parser.add_argument("output_file", help="Write to") g = parser.add_mutually_exclusive_group() g.add_argument("--debug", "-d", action="store_true", default=False, help="enable debugging") g.add_argument("--silent", "-s", action="store_true", default=False, help="don't log to console") return parser.parse_args(args) def setup_logging(options): """Configure logging.""" root = logging.getLogger("") root.setLevel(logging.WARNING) logger.setLevel(options.debug and logging.DEBUG or logging.INFO) if not options.silent: ch = logging.StreamHandler() ch.setFormatter(logging.Formatter( "%(levelname)s[%(name)s] %(message)s")) root.addHandler(ch) def get_fbx_params(api_url): """Read parms from Freebox API url Returns an array of strings""" with urllib.request.urlopen(api_url) as url: data = json.loads(url.read().decode()) logger.debug("Downloaded content: {}".format(data)) params=[] for param, value in data.items(): params.append("{}={}".format(param,value)) logger.debug("Array of params: {}".format(params)) return params def write_to_service_file(file_name,params): """Write Avahi service file""" with open(file_name,'w') as output: output.write("""<service-group> <name replace-wildcards="yes">Freebox Server</name> <service protocol="ipv4"> <type>_fbx-api._tcp</type> <port>80</port> <host-name>mafreebox.freebox.fr</host-name> """) for param in params: output.write(" <txt-record>{}</txt-record>\n".format(param)) output.write(""" </service> </service-group>""") if __name__ == "__main__": options = parse_args() setup_logging(options) try: logger.debug("Parameters: {}".format(options.output_file)) fbx_params = get_fbx_params("http://mafreebox.freebox.fr/api_version") write_to_service_file(options.output_file,fbx_params) except Exception as e: logger.exception("%s", e) sys.exit(1) sys.exit(0)
nilq/baby-python
python
import matplotlib.pyplot as plt from matplotlib import ticker from matplotlib.colors import LogNorm import matplotlib import numpy as np get_ipython().run_line_magic('matplotlib', 'inline') def Plotting(ebs,aps,Na,stime,mus,Np,Ne,Nmu): t,ax = plt.subplots(nrows =2, ncols = 1, figsize=(7,5)) #extent=[ebs.min(), ebs.max(), aps.min(), aps.max()] #for i, mu in enumerate(mus): #for j in range(0,Np):\ Arr = np.zeros(Ne*Na*Nmu) for i in range(0,Nmu): Arr.append(np.concatenate(stime[i,:,j,:]) #.ravel() Arr = np.array(Arr).reshape([Ne,Na,Nmu]) print(Arr) extent=[ebs.min(), ebs.max(), aps.min(), aps.max()] ax.set_xlim(extent[0], extent[1]) ax.set_ylim(extent[2], extent[3]) ax.set_xlabel("Binary Eccentricity $e_b$ ") ax.set_ylabel("Test particle semimajor axis $a_p$") im = ax.imshow(stime1, aspect='auto', origin="lower", interpolation='nearest', cmap="viridis",extent=extent) # ebs = np.linspace(0.,0.7,Ne) ab_s = np.zeros(Na) mu = MU for i,eb in enumerate(ebs): ab_s[i] = 1.6 + 5.1*eb-2.22*(eb**2)+4.12*mu-4.27*eb*mu-5.09*(mu**2)+4.61*(eb**2)*mu**2 # ab_s[i] = 2.278 + 3.824*eb - 1.71*(eb**2) plt.plot(ebs,ab_s,'c', marker = "^",markersize = 7) plt.xlabel('$e_b$') plt.ylabel('$a_b(a_c$)') plt.title('MU = {} : Critical semimajor axis $a_c$ as a function of eccentricity $e_b$'.format(mu)) cb = plt.colorbar(im, ax=ax) cb.solids.set_rasterized(True) cb.set_label("Particle Survival Times") plt.show() #plt.savefig("Classic_results.pdf")
nilq/baby-python
python
import re import math import operator import collections import unittest __version__ = (0, 0, 11) _non_word_re = re.compile(r'[^\w, ]+') __all__ = ('FuzzySet',) class FuzzySet(object): def __init__(self, iterable=(), gram_size_lower=2, gram_size_upper=3): self.exact_set = {} self.match_dict = collections.defaultdict(list) self.items = {} self.gram_size_lower = gram_size_lower self.gram_size_upper = gram_size_upper for i in range(gram_size_lower, gram_size_upper + 1): self.items[i] = [] for value in iterable: self.add(value) def add(self, value): lvalue = value.lower() if lvalue in self.exact_set: return False for i in range(self.gram_size_lower, self.gram_size_upper + 1): self.__add(value, i) def __add(self, value, gram_size): lvalue = value.lower() items = self.items[gram_size] idx = len(items) items.append(0) grams = _gram_counter(lvalue, gram_size) norm = math.sqrt(sum(x**2 for x in grams.values())) for gram, occ in grams.items(): self.match_dict[gram].append((idx, occ)) items[idx] = (norm, lvalue) self.exact_set[lvalue] = value def __getitem__(self, value): return self._getitem(value, exact_match_only=True, min_match_score=0.5) def _getitem(self, value, exact_match_only, min_match_score): lvalue = value.lower() exact_match = self.exact_set.get(lvalue) if exact_match_only and exact_match: return [(1, exact_match)] for i in range(self.gram_size_upper, self.gram_size_lower - 1, -1): results = self.__get(value, i, min_match_score) if exact_match: assert exact_match in [row for val, row in results] if results: return results raise KeyError(value) def __get(self, value, gram_size, min_match_score=0.5): lvalue = value.lower() matches = collections.defaultdict(float) grams = _gram_counter(lvalue, gram_size) items = self.items[gram_size] norm = math.sqrt(sum(x**2 for x in grams.values())) for gram, occ in grams.items(): for idx, other_occ in self.match_dict.get(gram, ()): matches[idx] += occ * other_occ if not matches: return None # cosine similarity results = [(match_score / (norm * items[idx][0]), items[idx][1]) for idx, match_score in matches.items()] results.sort(reverse=True, key=operator.itemgetter(0)) return [(score, self.exact_set[lval]) for score, lval in results if score >= min_match_score] def get(self, key, default=None, exact_match_only=True, min_match_score=0.5): try: return self._getitem(key, exact_match_only, min_match_score) except KeyError: return default def __nonzero__(self): return bool(self.exact_set) def __len__(self): return len(self.exact_set) def _gram_counter(value, gram_size=2): result = collections.defaultdict(int) for value in _iterate_grams(value, gram_size): result[value] += 1 return result def _iterate_grams(value, gram_size=2): simplified = '-' + value + '-' len_diff = gram_size - len(simplified) if len_diff > 0: value += '-' * len_diff for i in range(len(simplified) - gram_size + 1): yield simplified[i:i + gram_size] class FuzzySetTest(unittest.TestCase): def get_from_set(self, fuzzy_set, search_term, expected_rows, exact_match_only=False, min_match_score=0.5): rows = fuzzy_set.get(search_term, [], exact_match_only=exact_match_only, min_match_score=min_match_score) vals = [val for _, val in rows] self.assertEqual(expected_rows, vals) def test_simple(self): rows = [ "Ala ma kota", "Ala ma psa", "Zuzia ma psa", "Zuzia ma kanarka" ] fuzzy_set = FuzzySet(rows) self.get_from_set(fuzzy_set, "ia ma psa", ["Zuzia ma psa", "Ala ma psa"]) def test_fuzzy_set_return_all_matching_rows_even_when_exact_match_is_there(self): rows = [ "Ala ma kota", "Ala ma kota.", ] fuzzy_set = FuzzySet(rows) self.get_from_set(fuzzy_set, "Ala ma kota", ["Ala ma kota"], exact_match_only=True) self.get_from_set(fuzzy_set, "Ala ma kota", ["Ala ma kota", "Ala ma kota."], exact_match_only=False) def test_fuzzy_set_works_well_for_short_words(self): rows = [ "}", "{,", ",{", "a", "b", "c", "xyz", "xyzabc", ] fuzzy_set = FuzzySet(rows) self.get_from_set(fuzzy_set, "}", ["}"]) self.get_from_set(fuzzy_set, "{", ["{,", ",{"], min_match_score=0.35) self.get_from_set(fuzzy_set, "{", [], min_match_score=0.5) self.get_from_set(fuzzy_set, "ab", [], min_match_score=0.5) self.get_from_set(fuzzy_set, "ab", ["a", "b"], min_match_score=0.35) self.get_from_set(fuzzy_set, "ab", ["a", "b"], min_match_score=0.35) self.get_from_set(fuzzy_set, "xy", ["xyz"], min_match_score=0.35) # TODO conclusion - use 0.35 for 1 or 2 sign words and 0.5 or more for rest
nilq/baby-python
python
import requests import autoscaler.conf.engine_config as eng import os def remove_old_create_new(f_name, header): if os.access(f_name, os.R_OK): os.remove(f_name) write_to_file(header, f_name) def write_to_file(stats, f_name): csv = open(f_name, "a") csv.write(stats) def get_dpid(ip): ryu_controller = eng.RYU_CONTROLLER url = 'http://'+ryu_controller+'/v1.0/topology/switches' data = requests.get(url).json() #print("data: %s" %str(data)) for item in data: if ip == item["ip"]: return item["dpid"] def calc_bw(prev, curr): prev = float(prev) curr = float(curr) bw = ((curr - prev)/eng.NETWORK_MONITORING_INTERVAL) return bw def calc_bw_left(prev, curr): bw = calc_bw(prev, curr) max_bw = 125000000 #500000000 Bits bw_left = max_bw - bw return bw_left # Reference: https://stackoverflow.com/questions/12523586/python-format-size-application-converting-b-to-kb-mb-gb-tb/37423778 def bytes_2_human_readable_bits(number_of_bytes): if number_of_bytes < 0: raise ValueError("!!! number_of_bytes can't be smaller than 0 !!!") step_to_greater_unit = 1000. number_of_bits = float(number_of_bytes)*8 unit = 'bits/s' if (number_of_bits / step_to_greater_unit) >= 1: number_of_bits /= step_to_greater_unit unit = 'Kb/s' if (number_of_bits / step_to_greater_unit) >= 1: number_of_bits /= step_to_greater_unit unit = 'Mb/s' if (number_of_bits / step_to_greater_unit) >= 1: number_of_bits /= step_to_greater_unit unit = 'Gb/s' if (number_of_bits / step_to_greater_unit) >= 1: number_of_bits /= step_to_greater_unit unit = 'Tb/s' number_of_bits = "%.3f" % number_of_bits return str(number_of_bits) + ' ' + unit
nilq/baby-python
python
from pathlib import Path from math import ceil from sys import maxsize from pprint import pprint def parse_reactions(raw_text): reactions = dict() r_inputs = reactions["inputs"] = list() r_outputs = reactions["outputs"] = list() for line in raw_text.split("\n"): if len(line) == 0: continue inputs, output = line.strip().split(" => ") output_value, output_key = output.split(" ") r_outputs.append({output_key: int(output_value)}) pairs = dict() for pair in inputs.split(", "): value, key = pair.split(" ") pairs[key] = int(value) r_inputs.append(pairs) return reactions def get_chemical_quantity(reactions, chemical, fuel=1): inputs = reactions.get("inputs") outputs = reactions.get("outputs") if chemical == "FUEL": return fuel quantity = 0 for p, pairs in enumerate(inputs): if chemical in pairs: output = outputs[p] for output_chemical, output_quantity in output.items(): args = reactions, output_chemical, fuel chemical_quantity = get_chemical_quantity(*args) current_quantity = pairs.get(chemical) used_quantity = ceil(chemical_quantity / output_quantity) quantity = quantity + used_quantity * current_quantity return quantity def get_fuel_units(reactions): min_fuel = 1 max_fuel = maxsize - 1 available_ore = 10**12 while (max_fuel - min_fuel) > 1: make = (min_fuel + max_fuel) // 2 ores = get_chemical_quantity(reactions, "ORE", make) if ores <= available_ore: min_fuel = make else: max_fuel = make return min_fuel if __name__ == "__main__": #reactions = parse_reactions("\n".join([ # "157 ORE => 5 NZVS", # "165 ORE => 6 DCFZ", # "44 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL", # "12 HKGWZ, 1 GPVTF, 8 PSHF => 9 QDVJ","179 ORE => 7 PSHF", # "177 ORE => 5 HKGWZ", # "7 DCFZ, 7 PSHF => 2 XJWVT", # "165 ORE => 2 GPVTF", # "3 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT" #])) reactions = parse_reactions(Path("../etc/aoc14.txt").read_text()) count = get_chemical_quantity(reactions, "ORE") print("Part 1:", count) fuel = get_fuel_units(reactions) print("Part 2:", fuel)
nilq/baby-python
python
from dependency_injector.wiring import inject, Provide from ...container import Container from ...service import Service @inject def test_function(service: Service = Provide[Container.service]): return service
nilq/baby-python
python
from .models import Podcasts,Votes,Comments,Profile from django import forms class RateForm(forms.ModelForm): class Meta: model=Votes exclude=['user','podcast'] class PostForm(forms.ModelForm): class Meta: model=Podcasts exclude=['user','design','usability','content'] class ReviewForm(forms.ModelForm): class Meta: model=Comments exclude=['user','pro_id'] class UpdateForm(forms.ModelForm): class Meta: model=Profile exclude=['user']
nilq/baby-python
python
"""Test suite for the pyproject_cookiecutter_test package."""
nilq/baby-python
python
import tensorflow as tf from einops.layers import tensorflow as tfeinsum from tensorflow.keras import initializers, layers from .layers import DiagonalAffine, PatchEmbed, PerSampleDropPath def mlp_block(x, hidden_units, out_units, activation, dropout_rate, name=None): x = layers.Dense(units=hidden_units, name=name + "_dense_1")(x) x = layers.Activation(activation, name=name + "_act_1")(x) x = layers.Dropout(dropout_rate, name=name + "_dropout_1")(x) x = layers.Dense(units=out_units, name=name + "_dense_2")(x) x = layers.Dropout(dropout_rate, name=name + "_dropout_2")(x) return x def layers_scale_mlp_blocks( x, dims, dropout_rate, drop_path_rate, activation, init_values, num_patches, name=None, ): inputs = tf.identity(x) x = DiagonalAffine(dims, name=name + "_affine_1")(x) x = tf.transpose(x, (0, 2, 1), name=name + "_transpose_1") x = layers.Dense(num_patches, name=name + "_dense_1")(x) x = tf.transpose(x, (0, 2, 1), name=name + "_transpose_2") x = DiagonalAffine( dims, alpha_initializer=initializers.Constant(tf.fill([dims], init_values)), use_beta=False, name=name + "_affine_2", )(x) x = PerSampleDropPath(drop_path_rate, name=name + "_drop_path_1")(x) x = layers.Add(name=name + "_add_1")([inputs, x]) z = x x = DiagonalAffine(dims, name=name + "_affine_3")(x) x = mlp_block(x, 4 * dims, dims, activation, dropout_rate, name=name + "_mlp") x = DiagonalAffine( dims, alpha_initializer=initializers.Constant(tf.fill([dims], init_values)), use_beta=False, name=name + "_affine_4", )(x) x = PerSampleDropPath(drop_path_rate, name=name + "_drop_path_2")(x) x = layers.Add(name=name + "_add_2")([z, x]) return x def resmlp( input_shape=(224, 224, 3), patch_width=16, patch_height=16, num_classes=1000, embed_dims=768, depth=12, dropout_rate=0.0, drop_path_rate=0.0, init_scale=1e-4, activation="gelu", include_top=True, model_name=None, ): inputs = x = tf.keras.layers.Input(shape=input_shape) x = PatchEmbed( x, patch_width=patch_width, patch_height=patch_height, embed_dims=embed_dims, name="patch_embedding", ) shape = x.get_shape() for i in range(depth): x = layers_scale_mlp_blocks( x, dims=embed_dims, dropout_rate=dropout_rate, drop_path_rate=drop_path_rate, init_values=init_scale, activation=activation, num_patches=shape[1], name=f"block_{i}", ) x = DiagonalAffine(dims=embed_dims, name="feature_affine")(x) if include_top: x = tfeinsum.Reduce("b n c -> b c", "mean")(x) x = layers.Dense(num_classes, name="predictions")(x) model = tf.keras.models.Model(inputs, x, name=model_name) return model def ResMlp12( input_shape=(224, 224, 3), patch_width=16, patch_height=16, embed_dims=384, model_name="resmlp12", **kwargs, ): return resmlp( input_shape=input_shape, patch_width=patch_width, patch_height=patch_height, embed_dims=embed_dims, depth=12, init_scale=1e-1, model_name=model_name, **kwargs, ) def ResMlp24( input_shape=(224, 224, 3), patch_width=16, patch_height=16, embed_dims=384, model_name="resmlp24", **kwargs, ): return resmlp( input_shape=input_shape, patch_width=patch_width, patch_height=patch_height, embed_dims=embed_dims, depth=24, init_scale=1e-5, model_name=model_name, **kwargs, ) def ResMlp36( input_shape=(224, 224, 3), patch_width=16, patch_height=16, embed_dims=384, model_name="resmlp36", **kwargs, ): return resmlp( input_shape=input_shape, patch_width=patch_width, patch_height=patch_height, embed_dims=embed_dims, depth=36, init_scale=1e-6, model_name=model_name, **kwargs, ) def ResMlpB24( input_shape=(224, 224, 3), patch_width=8, patch_height=8, embed_dims=768, model_name="resmlpB24", **kwargs, ): return resmlp( input_shape=input_shape, patch_width=patch_width, patch_height=patch_height, embed_dims=embed_dims, depth=24, init_scale=1e-6, model_name=model_name, **kwargs, )
nilq/baby-python
python
#!/usr/bin/python # import RPi.GPIO as GPIO import spidev, time, math, random, logging class Gibson_LED_Driver: ## name constants ## proglist = {'static':0, 'fadeupdown':1, 'flyingcolor':2, 'throb':3, 'randomstatic':4, 'spin':5} colorlist = {'single':0, 'alternating':1, 'wide-alternating':2, 'colorwheel':3, 'rgb':4, 'random':5} def __init__(self, spi_x=0, spi_y=0, nLeds=50): self.spi = spidev.SpiDev() self.spi.open(spi_x,spi_y) self.spi.max_speed_hz = 25000000 # set the number of LEDs in string self.nLeds = nLeds #fixing scope self.correctPixels = [] # store every LED as a pixel self.pixels = [] for b in range(0,nLeds): self.pixels.append(1) self.pixels.append(0) self.pixels.append(0) self.pixels.append(0) #make the duplicate for the fade function self.pixelsCopy = list(self.pixels) #init program values self.stepnum=0 self.progname = Gibson_LED_Driver.proglist['static'] self.colorpattern = Gibson_LED_Driver.colorlist['single'] self.color = (255,100,255) self.altcolor = (255,0,255) #default calibration self.progsleep = 0.1 self.width = 3 #log the start up logging.info("New Gibson_LED_Driver initiated @" + str(time.time())) logging.info("Programs: "+str(self.proglist.keys())) logging.info("Color Patterns: "+str(self.colorlist.keys())) ## global settings aka 'calibration' #speed is in Hz or steps/second def calibrate(self, speed, width): logging.info("calibrate speed: "+str(speed)+" & width: "+str(width)) self.progsleep = 1.0/float(speed) self.width = int(width) # not super neccesarry as it should close on it's own def close(self): logging.info("Closing") if (self.spi != None): self.spi.close() self.f = None def brightnessCorrect(self, val): ##this corrects the luminosity of LEDs - they are not linear ## ## This should done in a look up table for speed, but all we really need is 30 frames ## per second to appear fluid so no one should notice ## not perfect yet, but much better than direct metering #return int(math.ceil(math.pow(2,(val*4) / 128) - 1)) #return int(math.ceil((math.pow(2,val/255) - 1) * 255)) return int(val) ## This function actually puts the bits on the wire ## in some cases a [1,0,0,0] string may be read as an end of line ## for SPI and the rest of the LED's will not update - still researching def update(self): # logging.debug("update") #correct for LED brightness s-curve instead of linear self.correctPixels = [] for i in range(0, self.nLeds): #4 values for every pixel (y, R, G, B) self.correctPixels.append(1) red = self.brightnessCorrect(self.pixels[i*4 + 1]) gr = self.brightnessCorrect(self.pixels[i*4 + 2]) blu = self.brightnessCorrect(self.pixels[i*4 + 3]) self.correctPixels.append(red) self.correctPixels.append(gr) self.correctPixels.append(blu) #print str(self.correctPixels) self.spi.writebytes(self.correctPixels) #self.spi.writebytes(self.pixels) self.spi.writebytes([0,0,0,0]) def setPixel(self, index, color): # if(color[0] == 0 and color[1] == 0 and color[2] == 0): # #fixing the blackout glitch when the color is black, possibly not an issue # color = (1,1,1) self.pixels[index*4:index*4+4] = (1, color[0], color[1], color[2]) def getPixelColor(self, index): thiscolor = [] thiscolor.append(self.pixels[index*4 + 1]) thiscolor.append(self.pixels[index*4 + 2]) thiscolor.append(self.pixels[index*4 + 3]) return thiscolor def getPixelCopyColor(self, index): thiscolor = [] thiscolor.append(self.pixelsCopy[index*4 + 1]) thiscolor.append(self.pixelsCopy[index*4 + 2]) thiscolor.append(self.pixelsCopy[index*4 + 3]) return thiscolor def setAll(self, color): for i in range(0, self.nLeds): self.setPixel(i,color) def fadeupdown(self, speed): for i in range(1,256): self.setAll([i,i,i]) self.update() time.sleep(speed/1000.0) for i in reversed(range(1,256)): self.setAll([i,i,i]) self.update() time.sleep(speed/1000.0) # this sends a color orbiting the strand once def flyingcolor(self, color, speed=50): for p in range(self.nLeds): temppixel = self.getPixelColor(p) self.setPixel(p, color) self.update() self.setPixel(p, temppixel) time.sleep(speed/1000.0) def throb(self, speed): math.sin() def static(self, color): self.setAll(color) self.update() def randomstatic(self): for p in range(self.nLeds): randomred = random.randint(1,255) randomgreen = random.randint(1,255) randomblue = random.randint(1,255) randcolor = [randomred, randomgreen, randomblue] self.setPixel(p, randcolor) #self.update() def rgb(self): for p in range(self.nLeds): if p%3 == 0: self.setPixel(p, [255,0,0]) elif p%3 == 1: self.setPixel(p, [0,255,0]) else: self.setPixel(p, [0,0,255]) #self.update() def colorwheel(self): self.setPixel(0, [125,255,0]) # self.setPixel(1, [157,255,0]) self.setPixel(2, [189,255,0]) self.setPixel(3, [255,255,0]) # self.setPixel(4, [125,222,0]) self.setPixel(5, [125,190,0]) self.setPixel(6, [125,158,0]) self.setPixel(7, [255,125,0]) # self.setPixel(8, [255,94,0]) self.setPixel(9, [255,62,0]) self.setPixel(10,[255,30,0]) self.setPixel(11,[255,0,0]) # self.setPixel(12,[255,0,30]) self.setPixel(13,[255,0,62]) self.setPixel(14,[255,0,94]) self.setPixel(15,[255,0,126]) # self.setPixel(16,[255,0,158]) self.setPixel(17,[255,0,190]) self.setPixel(18,[255,0,222]) self.setPixel(19,[255,0,255]) # self.setPixel(20,[222,0,255]) self.setPixel(21,[190,0,255]) self.setPixel(22,[158,0,255]) self.setPixel(23,[124,0,255]) # self.setPixel(24,[94,0,255]) self.setPixel(25,[62,0,255]) self.setPixel(26,[32,0,255]) self.setPixel(27,[0,0,255]) # self.setPixel(28,[32,0,255]) self.setPixel(29,[62,0,255]) self.setPixel(30,[94,0,255]) self.setPixel(31,[124,0,255]) # self.setPixel(32,[83,43,255]) self.setPixel(33,[42,83,255]) self.setPixel(34,[0,125,255]) # self.setPixel(35,[0,158,255]) self.setPixel(36,[0,190,255]) self.setPixel(37,[0,222,255]) self.setPixel(38,[0,255,255]) # self.setPixel(39,[0,255,222]) self.setPixel(40,[0,255,190]) self.setPixel(41,[0,255,157]) self.setPixel(42,[0,255,123]) # self.setPixel(43,[0,255,94]) self.setPixel(44,[0,255,62]) self.setPixel(45,[0,255,30]) self.setPixel(46,[0,255,0]) # self.setPixel(47,[32,255,0]) self.setPixel(48,[64,255,0]) self.setPixel(49,[96,255,0]) #self.update() def spiniteration(self, msdelay=25): tempcolor = self.getPixelColor(49) for p in reversed(range(1,self.nLeds)): self.setPixel(p, self.getPixelColor(p-1)) self.setPixel(0,tempcolor) time.sleep(msdelay/1000.0) self.update() def alternate(self, color1, color2): for p in range(self.nLeds): if p%self.width == 0: self.setPixel(p, color1) else: self.setPixel(p, color2) #self.update() def resetStepNum(self): self.stepnum = 0 def setColor(self,name,color1=[255,255,255],color2=[255,0,255]): # print("setting color") try: self.color=(color1[0], color1[1], color1[2]) self.altcolor=(color2[0], color2[1], color2[2]) self.colorpattern = self.colorlist[name] # print("Setting color: "+str(self.color)+" alt color: "+str(self.altcolor)+" and pattern: "+str(self.colorpattern)) logging.debug("Setting color: "+str(self.color)+" alt color: "+str(self.altcolor)+" and pattern: "+str(self.colorpattern)) except KeyError as e: logging.error(e) self.colorpattern = self.colorlist['single'] except: logging.error(e) # print(e) self.colorpattern = self.colorlist['single'] # print("..color complete") def setProgram(self,name): # logging.info("Set program to: "+name) self.resetStepNum() try: self.progname = Gibson_LED_Driver.proglist[name] except KeyError as e: logging.error(e) self.progname = self.proglist['static'] # logging.debug("progname: "+str(self.progname)) # logging.debug("colorpattern: "+str(self.colorpattern)) # logging.info("Set colorpattern to: "+str(self.colorpattern)) if(self.colorpattern == self.colorlist['alternating']): for p in range(self.nLeds): if p%2 == 0: self.setPixel(p, self.color) else: self.setPixel(p, self.altcolor) elif(self.colorpattern == self.colorlist['colorwheel']): self.colorwheel() elif(self.colorpattern == self.colorlist['wide-alternating']): for p in range(self.nLeds): if p%(2*self.width) < self.width: self.setPixel(p, self.color) else: self.setPixel(p, self.altcolor) elif(self.colorpattern == self.colorlist['rgb']): self.rgb() elif(self.colorpattern == self.colorlist['random']): self.randomstatic() elif(self.colorpattern == self.colorlist['single']): # logging.debug('Color pattern single '+str(self.color)) self.setAll(self.color) if(self.progname == self.proglist['fadeupdown']): #self.setAll(self.color) #uses the previous pattern to fade instead of just one color #to avoid really bad rounding closer to the bottom of cycles we duplicate the original pattern: self.pixelsCopy = list(self.pixels) elif(self.progname == self.proglist['flyingcolor']): self.setAll(self.color) elif(self.progname == self.proglist['throb']): pass elif(self.progname == self.proglist['randomstatic']): self.randomstatic() elif(self.progname == self.proglist['spin']): pass self.update() def increment(self): #if(self.progname == self.proglist['static']): # do nothing! # logging.debug("inc:"+str(self.stepnum)) if(self.progname == self.proglist['fadeupdown']): if(self.stepnum > self.nLeds*2): self.resetStepNum() if(self.stepnum < self.nLeds): #get darker for n in range(self.nLeds): nColor = self.getPixelCopyColor(n) newR = nColor[0]*(float(self.nLeds-self.stepnum)/float(self.nLeds)) newG = nColor[1]*(float(self.nLeds-self.stepnum)/float(self.nLeds)) newB = nColor[2]*(float(self.nLeds-self.stepnum)/float(self.nLeds)) nextColor = ( int(round(newR,0)), int(round(newG,0)), int(round(newB,0)) ) self.setPixel(n, nextColor) else: #gets brighter for n in range(self.nLeds): nColor = self.getPixelCopyColor(n) newR = nColor[0]*(float(self.stepnum % self.nLeds)/float(self.nLeds)) newG = nColor[1]*(float(self.stepnum % self.nLeds)/float(self.nLeds)) newB = nColor[2]*(float(self.stepnum % self.nLeds)/float(self.nLeds)) nextColor = ( int(round(newR,0)), int(round(newG,0)), int(round(newB,0)) ) self.setPixel(n, nextColor) elif(self.progname == self.proglist['flyingcolor']): # NOT IMPLEMENTED YET logging.error("flyingcolor not implemented"+str(self.stepnum)) elif(self.progname == self.proglist['throb']): # NOT IMPLEMENTED YET logging.error("throb not implemented"+str(self.stepnum)) elif(self.progname == self.proglist['randomstatic']): self.randomstatic() elif(self.progname == self.proglist['spin']): tempcolor = self.getPixelColor(49) for p in reversed(range(1,self.nLeds)): self.setPixel(p, self.getPixelColor(p-1)) self.setPixel(0,tempcolor) elif(self.progname == self.proglist['static']): # logging.debug('static prog') pass time.sleep(self.progsleep) self.stepnum = self.stepnum + 1 self.update()
nilq/baby-python
python
# Copyright (c) Meta Platforms, Inc. and affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from __future__ import annotations import math from typing import Dict, Optional from tabulate import tabulate class StatsItem: def __init__(self, key: Optional[str] = None) -> None: self._key = key self.reset() @property def key(self) -> str: return self._key def reset(self): self._m0 = 0 self._m1 = 0.0 self._m2 = 0.0 self._min_val = float("inf") self._max_val = float("-inf") def add(self, v: float) -> None: # Welford algorithm. self._m0 += 1 delta = v - self._m1 self._m1 += delta / self._m0 self._m2 += delta * (v - self._m1) self._min_val = min(self._min_val, v) self._max_val = max(self._max_val, v) def count(self) -> int: return self._m0 def mean(self) -> float: return self._m1 def var(self, ddof: int = 0) -> float: return self._m2 / (self._m0 - ddof) if self._m0 > 1 else float("nan") def std(self, ddof: int = 0) -> float: return math.sqrt(self.var(ddof)) def min(self) -> float: return self._min_val def max(self) -> float: return self._max_val class StatsDict: def __init__(self) -> None: self._dict = {} def __getitem__(self, key: str) -> StatsItem: return self._dict[key] def reset(self): self._dict.clear() def add(self, k: str, v: float) -> None: if k in self._dict: self._dict[k].add(v) else: item = StatsItem(k) item.add(v) self._dict[k] = item def add_dict(self, d: Dict[str, float]) -> None: for k, v in d.items(): self.add(k, v) def update(self, stats: StatsDict) -> None: self._dict.update(stats._dict) def table(self, info: Optional[str] = None) -> str: h = ["info"] if info is not None else [] h += ["key", "mean", "std", "min", "max", "count"] t = [] for k, v in self._dict.items(): row = [info] if info is not None else [] row += [k, v.mean(), v.std(), v.min(), v.max(), v.count()] t.append(row) return tabulate(t, h, numalign="right", stralign="right", floatfmt=".8f")
nilq/baby-python
python
# encoding: utf-8 import copy import datetime from secrets import token_urlsafe from sqlalchemy import types, Column, Table, ForeignKey, orm from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.ext.mutable import MutableDict import ckan.plugins.toolkit as tk from ckan.model import meta, User, DomainObject __all__ = [u"ApiToken", u"api_token_table"] def _make_token(): nbytes = tk.asint(tk.config.get(u"api_token.nbytes", 32)) return token_urlsafe(nbytes) api_token_table = Table( u"api_token", meta.metadata, Column(u"id", types.UnicodeText, primary_key=True, default=_make_token), Column(u"name", types.UnicodeText), Column(u"user_id", types.UnicodeText, ForeignKey(u"user.id")), Column(u"created_at", types.DateTime, default=datetime.datetime.utcnow), Column(u"last_access", types.DateTime, nullable=True), Column(u"plugin_extras", MutableDict.as_mutable(JSONB)), ) class ApiToken(DomainObject): def __init__(self, user_id=None, name=None): self.id = _make_token() self.user_id = user_id self.name = name @classmethod def get(cls, id): if not id: return None return meta.Session.query(cls).get(id) @classmethod def revoke(cls, id): token = cls.get(id) if token: meta.Session.delete(token) meta.Session.commit() return True return False def touch(self, commit=False): self.last_access = datetime.datetime.utcnow() if commit: meta.Session.commit() def set_extra(self, key, value, commit=False): extras = self.plugin_extras or {} extras[key] = value self.plugin_extras = copy.deepcopy(extras) if commit: meta.Session.commit() meta.mapper( ApiToken, api_token_table, properties={ u"owner": orm.relation( User, backref=orm.backref(u"api_tokens", cascade=u"all, delete") ) }, )
nilq/baby-python
python
# Copyright (c) 2021, NVIDIA CORPORATION. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from cugraph.structure import graph_primtypes_wrapper from cugraph.structure.number_map import NumberMap import cudf import dask_cudf class simpleDistributedGraphImpl: class EdgeList: def __init__(self, ddf): self.edgelist_df = ddf self.weights = False # FIXME: Edge Attribute not handled # class AdjList: # Not Supported # class transposedAdjList: # Not Supported class Properties: def __init__(self, properties): self.multi_edge = getattr(properties, 'multi_edge', False) self.directed = properties.directed self.renumbered = False self.store_transposed = False self.self_loop = None self.isolated_vertices = None self.node_count = None self.edge_count = None self.weighted = False def __init__(self, properties): # Structure self.edgelist = None self.renumber_map = None self.properties = simpleDistributedGraphImpl.Properties(properties) self.source_columns = None self.destination_columns = None # Functions def __from_edgelist( self, input_ddf, source="source", destination="destination", edge_attr=None, renumber=True, store_transposed=False, ): if not isinstance(input_ddf, dask_cudf.DataFrame): raise Exception("input should be a dask_cudf dataFrame") if self.properties.directed is False: raise Exception("Undirected distributed graph not supported") s_col = source d_col = destination if not isinstance(s_col, list): s_col = [s_col] if not isinstance(d_col, list): d_col = [d_col] if not ( set(s_col).issubset(set(input_ddf.columns)) and set(d_col).issubset(set(input_ddf.columns)) ): raise Exception( "source column names and/or destination column " "names not found in input. Recheck the source " "and destination parameters" ) ddf_columns = s_col + d_col if edge_attr is not None: if not (set([edge_attr]).issubset(set(input_ddf.columns))): raise Exception( "edge_attr column name not found in input." "Recheck the edge_attr parameter") self.weighted = True ddf_columns = ddf_columns + [edge_attr] input_ddf = input_ddf[ddf_columns] if edge_attr is not None: input_ddf = input_ddf.rename(columns={edge_attr: 'value'}) # # Keep all of the original parameters so we can lazily # evaluate this function # # FIXME: Edge Attribute not handled self.properties.renumbered = renumber self.input_df = input_ddf self.source_columns = source self.destination_columns = destination def view_edge_list(self): """ Display the edge list. Compute it if needed. NOTE: If the graph is of type Graph() then the displayed undirected edges are the same as displayed by networkx Graph(), but the direction could be different i.e. an edge displayed by cugraph as (src, dst) could be displayed as (dst, src) by networkx. cugraph.Graph stores symmetrized edgelist internally. For displaying undirected edgelist for a Graph the upper trianglar matrix of the symmetrized edgelist is returned. networkx.Graph renumbers the input and stores the upper triangle of this renumbered input. Since the internal renumbering of networx and cugraph is different, the upper triangular matrix of networkx renumbered input may not be the same as cugraph's upper trianglar matrix of the symmetrized edgelist. Hence the displayed source and destination pairs in both will represent the same edge but node values could be swapped. Returns ------- df : cudf.DataFrame This cudf.DataFrame wraps source, destination and weight df[src] : cudf.Series contains the source index for each edge df[dst] : cudf.Series contains the destination index for each edge df[weight] : cusd.Series Column is only present for weighted Graph, then containing the weight value for each edge """ if self.edgelist is None: raise Exception("Graph has no Edgelist.") return self.edgelist.edgelist_df def delete_edge_list(self): """ Delete the edge list. """ # decrease reference count to free memory if the referenced objects are # no longer used. self.edgelist = None def clear(self): """ Empty this graph. This function is added for NetworkX compatibility. """ self.edgelist = None def number_of_vertices(self): """ Get the number of nodes in the graph. """ if self.properties.node_count is None: if self.edgelist is not None: ddf = self.edgelist.edgelist_df[["src", "dst"]] self.properties.node_count = ddf.max().max().compute() + 1 else: raise Exception("Graph is Empty") return self.properties.node_count def number_of_nodes(self): """ An alias of number_of_vertices(). This function is added for NetworkX compatibility. """ return self.number_of_vertices() def number_of_edges(self, directed_edges=False): """ Get the number of edges in the graph. """ if self.edgelist is not None: return len(self.edgelist.edgelist_df) else: raise Exception("Graph is Empty") def in_degree(self, vertex_subset=None): """ Compute vertex in-degree. Vertex in-degree is the number of edges pointing into the vertex. By default, this method computes vertex degrees for the entire set of vertices. If vertex_subset is provided, this method optionally filters out all but those listed in vertex_subset. Parameters ---------- vertex_subset : cudf.Series or iterable container, optional A container of vertices for displaying corresponding in-degree. If not set, degrees are computed for the entire set of vertices. Returns ------- df : cudf.DataFrame GPU DataFrame of size N (the default) or the size of the given vertices (vertex_subset) containing the in_degree. The ordering is relative to the adjacency list, or that given by the specified vertex_subset. df[vertex] : cudf.Series The vertex IDs (will be identical to vertex_subset if specified). df[degree] : cudf.Series The computed in-degree of the corresponding vertex. Examples -------- >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', >>> dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, '0', '1') >>> df = G.in_degree([0,9,12]) """ return self._degree(vertex_subset, x=1) def out_degree(self, vertex_subset=None): """ Compute vertex out-degree. Vertex out-degree is the number of edges pointing out from the vertex. By default, this method computes vertex degrees for the entire set of vertices. If vertex_subset is provided, this method optionally filters out all but those listed in vertex_subset. Parameters ---------- vertex_subset : cudf.Series or iterable container, optional A container of vertices for displaying corresponding out-degree. If not set, degrees are computed for the entire set of vertices. Returns ------- df : cudf.DataFrame GPU DataFrame of size N (the default) or the size of the given vertices (vertex_subset) containing the out_degree. The ordering is relative to the adjacency list, or that given by the specified vertex_subset. df[vertex] : cudf.Series The vertex IDs (will be identical to vertex_subset if specified). df[degree] : cudf.Series The computed out-degree of the corresponding vertex. Examples -------- >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', >>> dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, '0', '1') >>> df = G.out_degree([0,9,12]) """ # TODO: Add support raise Exception("Not supported for distributed graph") def degree(self, vertex_subset=None): """ Compute vertex degree, which is the total number of edges incident to a vertex (both in and out edges). By default, this method computes degrees for the entire set of vertices. If vertex_subset is provided, then this method optionally filters out all but those listed in vertex_subset. Parameters ---------- vertex_subset : cudf.Series or iterable container, optional a container of vertices for displaying corresponding degree. If not set, degrees are computed for the entire set of vertices. Returns ------- df : cudf.DataFrame GPU DataFrame of size N (the default) or the size of the given vertices (vertex_subset) containing the degree. The ordering is relative to the adjacency list, or that given by the specified vertex_subset. df['vertex'] : cudf.Series The vertex IDs (will be identical to vertex_subset if specified). df['degree'] : cudf.Series The computed degree of the corresponding vertex. Examples -------- >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', >>> dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, '0', '1') >>> all_df = G.degree() >>> subset_df = G.degree([0,9,12]) """ raise Exception("Not supported for distributed graph") # FIXME: vertex_subset could be a DataFrame for multi-column vertices def degrees(self, vertex_subset=None): """ Compute vertex in-degree and out-degree. By default, this method computes vertex degrees for the entire set of vertices. If vertex_subset is provided, this method optionally filters out all but those listed in vertex_subset. Parameters ---------- vertex_subset : cudf.Series or iterable container, optional A container of vertices for displaying corresponding degree. If not set, degrees are computed for the entire set of vertices. Returns ------- df : cudf.DataFrame GPU DataFrame of size N (the default) or the size of the given vertices (vertex_subset) containing the degrees. The ordering is relative to the adjacency list, or that given by the specified vertex_subset. df['vertex'] : cudf.Series The vertex IDs (will be identical to vertex_subset if specified). df['in_degree'] : cudf.Series The in-degree of the vertex. df['out_degree'] : cudf.Series The out-degree of the vertex. Examples -------- >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', >>> dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, '0', '1') >>> df = G.degrees([0,9,12]) """ raise Exception("Not supported for distributed graph") def _degree(self, vertex_subset, x=0): vertex_col, degree_col = graph_primtypes_wrapper._degree(self, x) df = cudf.DataFrame() df["vertex"] = vertex_col df["degree"] = degree_col if self.renumbered is True: df = self.unrenumber(df, "vertex") if vertex_subset is not None: df = df[df['vertex'].isin(vertex_subset)] return df def to_directed(self, DiG): """ Return a directed representation of the graph. This function sets the type of graph as DiGraph() and returns the directed view. Returns ------- G : DiGraph A directed graph with the same nodes, and each edge (u,v,weights) replaced by two directed edges (u,v,weights) and (v,u,weights). Examples -------- >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', >>> dtype=['int32', 'int32', 'float32'], header=None) >>> G = cugraph.Graph() >>> G.from_cudf_edgelist(M, '0', '1') >>> DiG = G.to_directed() """ # TODO: Add support raise Exception("Not supported for distributed graph") def to_undirected(self, G): """ Return an undirected copy of the graph. Returns ------- G : Graph A undirected graph with the same nodes, and each directed edge (u,v,weights) replaced by an undirected edge (u,v,weights). Examples -------- >>> M = cudf.read_csv('datasets/karate.csv', delimiter=' ', >>> dtype=['int32', 'int32', 'float32'], header=None) >>> DiG = cugraph.DiGraph() >>> DiG.from_cudf_edgelist(M, '0', '1') >>> G = DiG.to_undirected() """ # TODO: Add support raise Exception("Not supported for distributed graph") def has_node(self, n): """ Returns True if the graph contains the node n. """ if self.edgelist is None: raise Exception("Graph has no Edgelist.") # FIXME: Check renumber map ddf = self.edgelist.edgelist_df[["src", "dst"]] return (ddf == n).any().any().compute() def has_edge(self, u, v): """ Returns True if the graph contains the edge (u,v). """ # TODO: Verify Correctness if self.properties.renumbered: tmp = cudf.DataFrame({"src": [u, v]}) tmp = tmp.astype({"src": "int"}) tmp = self.add_internal_vertex_id( tmp, "id", "src", preserve_order=True ) u = tmp["id"][0] v = tmp["id"][1] df = self.edgelist.edgelist_df return ((df["src"] == u) & (df["dst"] == v)).any().compute() def edges(self): """ Returns all the edges in the graph as a cudf.DataFrame containing sources and destinations. It does not return the edge weights. For viewing edges with weights use view_edge_list() """ return self.view_edge_list()[["src", "dst"]] def nodes(self): """ Returns all the nodes in the graph as a cudf.Series """ # FIXME: Return renumber map nodes raise Exception("Not supported for distributed graph") def neighbors(self, n): if self.edgelist is None: raise Exception("Graph has no Edgelist.") # FIXME: Add renumbering of node n ddf = self.edgelist.edgelist_df return ddf[ddf["src"] == n]["dst"].reset_index(drop=True) def compute_renumber_edge_list(self, transposed=False): """ Compute a renumbered edge list This function works in the MNMG pipeline and will transform the input dask_cudf.DataFrame into a renumbered edge list in the prescribed direction. This function will be called by the algorithms to ensure that the graph is renumbered properly. The graph object will cache the most recent renumbering attempt. For benchmarking purposes, this function can be called prior to calling a graph algorithm so we can measure the cost of computing the renumbering separately from the cost of executing the algorithm. When creating a CSR-like structure, set transposed to False. When creating a CSC-like structure, set transposed to True. Parameters ---------- transposed : (optional) bool If True, renumber with the intent to make a CSC-like structure. If False, renumber with the intent to make a CSR-like structure. Defaults to False. """ # FIXME: What to do about edge_attr??? # currently ignored for MNMG if not self.properties.renumbered: self.edgelist = self.EdgeList(self.input_df) self.renumber_map = None else: if self.edgelist is not None: if self.properties.directed is False: return if self.properties.store_transposed == transposed: return del self.edgelist renumbered_ddf, number_map = NumberMap.renumber( self.input_df, self.source_columns, self.destination_columns, store_transposed=transposed, ) self.edgelist = self.EdgeList(renumbered_ddf) self.renumber_map = number_map self.properties.store_transposed = transposed
nilq/baby-python
python
#!/usr/bin/env python import os import re import sys try: from setuptools import setup except ImportError: from distutils.core import setup def read(*names, **kwargs): with open(os.path.join(os.path.dirname(__file__), *names), 'r') as fp: return fp.read() def find_version(*file_paths): version_file = read(*file_paths) version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M) if version_match: return version_match.group(1) raise RuntimeError("Unable to find version string.") classifiers = [ "License :: OSI Approved :: MIT License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3", "Topic :: Software Development" ] readme = os.path.join(os.path.dirname(__file__), 'README.md') try: import pypandoc long_description = pypandoc.convert(readme, 'rst') except ImportError: long_description = open(readme).read() setup( name='berryditos', version=find_version('berryditos', 'base.py'), description='Berryditos is a tool that can edit and burn Raspbian images.', long_description=long_description, keywords=['raspberry','raspbian'], author='tovam', author_email='tovamdvlp@gmail.com', url='https://github.com/tovam/berryditos', license='MIT', classifiers=classifiers, install_requires=['requests'], packages=['berryditos'], scripts=['bin/berryditos'], zip_safe=True )
nilq/baby-python
python
import bcrypt from app.database import BaseMixin, db from app.serializer import ma class User(db.Model, BaseMixin): __tablename__ = 'user_table' userID = db.Column(db.Integer, primary_key=True) username = db.Column(db.String, nullable=False) _password = db.Column(db.Binary(60)) email = db.Column(db.String, nullable=False) guest = db.Column(db.Boolean, default=False) user = db.Column(db.Boolean, default=True) admin = db.Column(db.Boolean, default=False) def __init__(self, username, password, email): self.username = username self._password = self.hash_pw(password).encode('utf-8') self.email = email def check_pw(self, hashed_pw, password): return bcrypt.checkpw(password.encode('utf-8'), hash_pw) def hash_pw(self, password): return bcrypt.hashpw(password, bcrypt.gensalt(12)) @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() class UserSchema(ma.ModelSchema): class Meta: model = User fields = ( "id", "username", "email", "guest", "user", "admin" )
nilq/baby-python
python
import boto3 from .. import config # Module API def change_acl_on_s3(bucket, acl, path='', endpoint_url=None): def func(package): # Prepare client s3_url = endpoint_url or config.S3_ENDPOINT_URL s3_client = boto3.client('s3', endpoint_url=s3_url) # Change ACL # list_objects returns max 1000 keys (even if MaxKeys is >1000) marker = '' is_truncated = True while is_truncated: objs = s3_client.list_objects(Bucket=bucket, Prefix=path, Marker=marker) is_truncated = objs.get('IsTruncated') for obj in objs.get('Contents', []): s3_client.put_object_acl(Bucket=bucket, Key=obj['Key'], ACL=acl) marker = obj['Key'] # Return to flow yield package.pkg yield from package return func
nilq/baby-python
python
# Description: Count number of *.log files in current directory. # Source: placeHolder """ cmd.do('print("Count the number of log image files in current directory.");') cmd.do('print("Usage: cntlogs");') cmd.do('myPath = os.getcwd();') cmd.do('logCounter = len(glob.glob1(myPath,"*.log"));') cmd.do('print("Number of number of log image files in the current directory: ", logCounter);') """ cmd.do('print("Count the number of log image files in current directory.");') cmd.do('print("Usage: cntlogs");') cmd.do('myPath = os.getcwd();') cmd.do('logCounter = len(glob.glob1(myPath,"*.log"));') cmd.do('print("Number of number of log image files in the current directory: ", logCounter);')
nilq/baby-python
python
""" A terminal based ray-casting engine. 'esc' to exit 't' to turn off textures 'wasdqe' or arrow-keys to move 'space' to jump Depending on your terminal font, Renderer.ascii_map may need to be adjusted. If you'd like to make an ascii map more suitable to your terminal's font, check my Snippets repository for a script that grabs mean brightness of unicode characters. Values stored in textures should range from 0-9. Values below 6 are subtractive and above 6 are additive. """ import curses from .map_loader import Map from .player import Player from .renderer import Renderer from .controller import Controller def init_curses(screen): curses.curs_set(0) curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) screen.attron(curses.color_pair(1)) screen.nodelay(True) @curses.wrapper def main(screen): init_curses(screen) game_map = Map("map_1") player = Player(game_map) wall_textures = "wall_1", "wall_2" sprite_textures = "dragon", "tree" Controller(Renderer(screen, player, wall_textures, sprite_textures)).start() curses.flushinp() curses.endwin()
nilq/baby-python
python
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @author: edwardahn Evaluate a policy and publish metrics. """ import argparse import cProfile import pstats import sys import joblib import matplotlib.pyplot as plt import numpy as np from rllab.sampler.utils import rollout def profile_code(profiler): """ Use cProfile to profile code, listing functions with most cumulative time spent. """ print('\n') ps = pstats.Stats(profiler).strip_dirs().sort_stats('cumulative') ps.print_stats(10) def plot_curve(error, name, units): """ Plot error over time. """ title = '%s Error over Time in Final Policy' % name plt.figure() t = np.arange(error.size) plt.plot(t, error) plt.title(title) plt.xlabel('Time steps') plt.ylabel('Error (%s)' % units) if name == 'Distance': plt.gca().set_ylim((-0.01, 0.01)) else: plt.gca().set_ylim((-0.7, 0.7)) plt.show() def plot_distribution(error, name, units): """ Plot histogram showing distribution of error. """ mean = error.mean() std = error.std() maximum = error.max() minimum = error.min() stats = 'Mean = %.5f\nStd = %.5f\nMax = %.5f\nMin = %.5f' % \ (mean, std, maximum, minimum) title = 'Distribution of %s Errors in Final Policy' % name plt.figure() plt.hist(error) plt.title(title) plt.xlabel('Error (%s)' % units) plt.ylabel('Number of Time Steps') plt.axvline(mean, color='k', linestyle='dashed', linewidth=1) plt.axvline(mean+std, color='r', linestyle='dashed', linewidth=1) plt.axvline(mean-std, color='r', linestyle='dashed', linewidth=1) plt.text(0.87, 0.9, stats, ha='center', va='center', transform=plt.gca().transAxes) plt.show() def parse_arguments(): parser = argparse.ArgumentParser() parser.add_argument('file', type=str, help='path to the snapshot file') parser.add_argument('--max_path_length', type=int, default=100, help='Max length of rollout') parser.add_argument('--speedup', type=float, default=100000, help='Speedup') parser.add_argument('--render', dest='render', action='store_true', help='Rendering') parser.add_argument('--no-render', dest='render', action='store_false', help='Rendering') parser.set_defaults(render=False) args = parser.parse_args() return args def main(): args = parse_arguments() profiler = cProfile.Profile() data = joblib.load(args.file) policy = data['policy'] env = data['env'] plt.ion() # Set fixed random seed np.random.seed(9) # Sample one rollout profiler.enable() path = rollout(env, policy, max_path_length=args.max_path_length, animated=args.render, speedup=args.speedup, always_return_paths=True) profiler.disable() # Policy analysis profile_code(profiler) plot_curve(path['env_infos']['dist'], 'Distance', 'm') plot_curve(path['env_infos']['vel'], 'Velocity', 'm/s') plot_distribution(path['env_infos']['dist'], 'Distance', 'm') plot_distribution(path['env_infos']['vel'], 'Velocity', 'm/s') # Block until key is pressed sys.stdout.write("Press <enter> to continue: ") input() if __name__ == "__main__": main()
nilq/baby-python
python
#!/usr/bin/env python3 # Copyright (c) Microsoft Corporation. # Licensed under the MIT license. import tensorflow as tf from tensorflow.contrib import antares if tf.version.VERSION.startswith('2.'): tf = tf.compat.v1 tf.disable_eager_execution() from _common import * x = create_variable([64, 224, 224, 3], dtype=tf.float32) compare_ops( tf.transpose(x, [0, 3, 1, 2]), antares.make_op('output0[N, C, H, W] = input0[N, H, W, C]', [x]), )
nilq/baby-python
python
import datetime import sqlalchemy # noinspection PyPackageRequirements from models.model_base import ModelBase class Roll(ModelBase): __tablename__ = 'rolls' id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True, autoincrement=True) created = sqlalchemy.Column(sqlalchemy.DateTime, default=datetime.datetime.now) name = sqlalchemy.Column(sqlalchemy.String, unique=True, nullable=False)
nilq/baby-python
python
import json import os class Const: def __init__(self, logger = None): self.init_const() self.logger = logger controllerAddr = "NOT SET" controllerPort = "NOT SET" BASE_PATH = "/home/mmeinen/polybox/code/DC-MONDRIAN" #TODO: needs to be set whenever run somewhere else... (not elegant but works...) PATH_TO_CONFIG_FILE = "Endpoint_TP/config/config.json" tpAddr = "NOT SET" # Timeouts are in seconds and 0 menas it never times out IDLE_TIMEOUT = 0#10*60 HARD_TIMEOUT = 0#60*60 endpointTPPort = "6633" TCP_PROTO = "TCP" UDP_PROTO = "UDP" ENDPOINT_TP_PREFIX = "[EndpointTP] " TRANSFER_MODULE_PREFIX = ENDPOINT_TP_PREFIX+"[Transfer Module] " FETCHER_PREFIX = ENDPOINT_TP_PREFIX+"[Fetcher] " CONNECTION_STATE = ENDPOINT_TP_PREFIX+"[Connection State] " @classmethod def init_const(self): ''' Initialize the constants according to the config.json file ''' Const.PATH_TO_CONFIG_FILE = os.path.join(Const.BASE_PATH, Const.PATH_TO_CONFIG_FILE) try: with open(Const.PATH_TO_CONFIG_FILE, "r") as jsonFile: data = json.load(jsonFile) Const.controllerAddr = data["controllerAddr"] Const.controllerPort = data["controllerPort"] Const.tpAddr = data["tpAddr"] Const.IDLE_TIMEOUT = data["idleTimeout"] Const.HARD_TIMEOUT = data["hardTimeout"] Const.endpointTPPort = data["endpointTPPort"] except json.JSONDecodeError as e: self.logger.info("[Const] ERROR: Reading config.json failed!") exit(1)
nilq/baby-python
python
donations = [ { 'price': 1, 'thanks': True, 'col_size': 12, }, { 'price': 5, 'thanks': True, 'link': True, 'col_size': 12, }, { 'price': 10, 'thanks': True, 'link': True, 'status': 'SUPPORTER', 'col_size': 6, }, { 'price': 25, 'thanks': True, 'link': True, 'postcard': True, 'status': 'SUPPORTER', 'col_size': 4, }, { 'price': 50, 'thanks': True, 'link': True, 'postcard': True, 'status': 'LOVER', 'col_size': 4, }, { 'price': 100, 'thanks': True, 'link': True, 'postcard': True, 'status': 'AMBASSADOR', 'col_size': 4, }, { 'price': 200, 'thanks': True, 'link': True, 'postcard': True, 'status': 'AMBASSADOR', 'box': True, 'col_size': 3, }, { 'price': 500, 'thanks': True, 'link': True, 'postcard': True, 'status': 'PRODUCER', 'feature': True, 'box': True, 'homepage_link': True, 'col_size': 2, }, { 'price': 800, 'thanks': True, 'link': True, 'postcard': True, 'status': 'DEVOTEE', 'homepage_link': True, 'homepage_character': True, 'col_size': 3, }, ]
nilq/baby-python
python
def merge_the_tools(string, n): out = [list(string)[k:k+n] for k in range(0,len(list(string)),n)] for x in out: print(''.join(sorted(set(x), key=x.index)))
nilq/baby-python
python
# Write the pseudo code for a program that reads a target csv file and adds the correct typing to its contents. # After that it should transpose the resulting csv file
nilq/baby-python
python
# Author: Simon Liedtke <liedtke.simon@googlemail.com> # # This module was developed with funding provided by # the Google Summer of Code (2013). """ Overview ^^^^^^^^ The database package exports the following classes and exceptions: :classes: - Database :exceptions: - EntryAlreadyAddedError - NoSuchEntryError - EntryAlreadyStarredError - EntryAlreadyUnstarredError - EntryNotFoundError - TagAlreadyAssignedError - NoSuchTagError - NonRemovableTagError :functions: - disable_undo """ from __future__ import absolute_import from sunpy.database.database import Database, EntryAlreadyAddedError,\ EntryAlreadyStarredError, EntryAlreadyUnstarredError, NoSuchTagError,\ EntryNotFoundError, TagAlreadyAssignedError, disable_undo, split_database from sunpy.database.commands import NoSuchEntryError, NonRemovableTagError __all__ = [ 'Database', 'EntryAlreadyAddedError', 'NoSuchEntryError', 'NoSuchTagError', 'NonRemovableTagError', 'EntryAlreadyStarredError', 'EntryAlreadyUnstarredError', 'EntryNotFoundError', 'TagAlreadyAssignedError', 'disable_undo', 'split_database']
nilq/baby-python
python
import numpy as np import torch import matplotlib.pyplot as plt from collections import OrderedDict def linear_size(output): output_size = np.array(output.size()) h, w = output_size[2], output_size[3] size = int(h * w) return size def count_parameters(model): return sum(p.numel() for p in model.parameters() if p.requires_grad) conv_normal_mean = 0.0 conv_normal_sd = 0.02 bnorm_mean = 1.0 bnorm_sd = 0.02 bnorm_fill = 0 def weights_init(m): classname = m.__class__.__name__ if classname.find('Conv') != -1: m.weight.data.normal_(conv_normal_mean, conv_normal_sd) elif classname.find('BatchNorm') != -1: m.weight.data.normal_(bnorm_mean, bnorm_sd) m.bias.data.fill_(bnorm_fill) def aduc(x, use_gpu=None): if use_gpu is None: use_gpu = torch.cuda.is_available() if use_gpu: return x.cuda() else: return x def set_names(discr_a, discr_b, gener_a, gener_b, opt_gener_a, opt_gener_b, opt_discr_a, opt_discr_b): discr_a.__doc__ = 'discr_a' discr_b.__doc__ = 'discr_b' gener_a.__doc__ = 'gener_a' gener_b.__doc__ = 'gener_b' opt_gener_a.__doc__ = 'opt_gener_a' opt_gener_b.__doc__ = 'opt_gener_b' opt_discr_a.__doc__ = 'opt_discr_a' opt_discr_b.__doc__ = 'opt_discr_b' pass def train_stage(*args): for arg in args: arg.train() def create_checkpoint(*args): checkpoint = OrderedDict() for net in args: name = net.__doc__ checkpoint[name] = net.state_dict() return checkpoint def exp_moving_mean(data, window=250): if not isinstance(data, np.ndarray): data = np.array(data) alpha = 2 / (window + 1.0) alpha_rev = 1 - alpha n = data.shape[0] pows = alpha_rev**(np.arange(n + 1)) scale_arr = 1 / pows[:-1] offset = data[0] * pows[1:] pw0 = alpha * alpha_rev**(n - 1) mult = data * pw0 * scale_arr cumsums = mult.cumsum() out = offset + cumsums * scale_arr[::-1] return out def visualize_loss(da_loss_log, db_loss_log, ga_loss_log, gb_loss_log, exp_window=None): if exp_window is not None: da_loss_log = exp_moving_mean(da_loss_log, exp_window) db_loss_log = exp_moving_mean(db_loss_log, exp_window) ga_loss_log = exp_moving_mean(ga_loss_log, exp_window) gb_loss_log = exp_moving_mean(gb_loss_log, exp_window) plt.figure(figsize=(10, 4)) plt.tight_layout() plt.subplot(1, 2, 1) plt.plot(ga_loss_log, label="gener_a") plt.plot(gb_loss_log, label="gener_b") plt.xlabel("train step") plt.ylabel("MSE") plt.title("generators loss") plt.legend() plt.subplot(1, 2, 2) plt.plot(da_loss_log, label="discr_a") plt.plot(db_loss_log, label="discr_b") plt.tight_layout() plt.xlabel("train step") plt.ylabel("MSE") plt.title("discriminators loss") plt.legend() plt.tight_layout() plt.show() def plot_geners(sample_a, sample_b, gener_a, gener_b): gener_a.eval() gener_b.eval() plt.figure(figsize=(8, 6)) plt.subplot(2, 3, 1) plt.imshow(sample_b.cpu().view(1, 1, 28, 28).data[0] .numpy().reshape((28, 28)), cmap='binary') plt.title("b") plt.subplot(2, 3, 2) plt.imshow(gener_a(sample_b.view(1, 1, 28, 28)).cpu().data[0] .numpy().reshape((28, 28)), cmap='binary') plt.title("gener_a(b)") plt.subplot(2, 3, 3) plt.imshow(gener_b(gener_a(sample_b.view(1, 1, 28, 28))).cpu().data[0] .numpy().reshape((28, 28)), cmap='binary') plt.title("gener_b(gener_a(b))") plt.subplot(2, 3, 4) plt.imshow(sample_a.cpu().view(1, 1, 28, 28).data[0] .numpy().reshape((28, 28)), cmap='binary') plt.title("a") plt.subplot(2, 3, 5) plt.imshow(gener_b(sample_a.view(1, 1, 28, 28)).cpu().data[0] .numpy().reshape((28, 28)), cmap='binary') plt.title("gener_b(a)") plt.subplot(2, 3, 6) plt.imshow(gener_a(gener_b(sample_a.view(1, 1, 28, 28))).cpu().data[0] .numpy().reshape((28, 28)), cmap='binary') plt.title("gener_a(gener_b(a))") plt.tight_layout() plt.show() def grad_norm(model, norm_type=2): total_norm = 0 for param in model.parameters(): param_norm = param.grad.data.norm(norm_type) total_norm += param_norm ** norm_type total_norm = total_norm ** (1. / norm_type) return total_norm def plot_grad_norms(da_grad_log, db_grad_log, ga_grad_log, gb_grad_log): plt.figure(figsize=(10, 4)) plt.subplot(1, 2, 1) plt.plot(ga_grad_log, label="gener_a") plt.plot(gb_grad_log, label="gener_b") plt.xlabel("step") plt.ylabel("grad norm") plt.legend() plt.subplot(1, 2, 2) plt.plot(da_grad_log, label="disrc_a") plt.plot(db_grad_log, label="discr_b") plt.xlabel("step") plt.ylabel("grad norm") plt.legend() plt.tight_layout() plt.show()
nilq/baby-python
python
"""treelstm.py - TreeLSTM RNN models Written by Riddhiman Dasgupta (https://github.com/dasguptar/treelstm.pytorch) Rewritten in 2018 by Long-Huei Chen <longhuei@g.ecc.u-tokyo.ac.jp> To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. """ import torch import torch.nn as nn class TreeLSTMBase(nn.Module): @staticmethod def extract_tree(parse): """ Args: line: A list of tokens, where each token consists of a word, optionally followed by u"│"-delimited features. Returns: A sequence of words, a sequence of features, and num of features. """ if parse is None: return [], [], -1 parents = parse.cpu().numpy() trees = dict() root = None for i in range(1, len(parents) + 1): if i - 1 not in trees.keys() and parents[i - 1] != -1: idx = i prev = None while True: parent = parents[idx - 1] if parent == -1: break tree = Tree() if prev is not None: tree.add_child(prev) trees[idx - 1] = tree tree.idx = idx - 1 if parent - 1 in trees.keys(): trees[parent - 1].add_child(tree) break elif parent == 0: root = tree break else: prev = tree idx = parent return root class ChildSumTreeLSTM(TreeLSTMBase): def __init__(self, rnn_type, input_size, hidden_size, bias=True): super(ChildSumTreeLSTM, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.ioux = nn.Linear(input_size, 3 * self.hidden_size, bias=bias) self.iouh = nn.Linear( self.hidden_size, 3 * self.hidden_size, bias=bias) self.fx = nn.Linear(input_size, self.hidden_size, bias=bias) self.fh = nn.Linear(self.hidden_size, self.hidden_size, bias=bias) def forward(self, parses, embeds): states_c, states_h = zip(*[ self.tree_forward( ChildSumTreeLSTM.extract_tree(parses[:, j]), embeds[:, j, :]) for j in range(parses.size(1)) ]) states_c = torch.cat(states_c, dim=1) states_h = torch.cat(states_h, dim=1) return (states_c, states_h) def tree_forward(self, tree, embed): for idx in range(tree.num_children): self.tree_forward(tree.children[idx], embed) if tree.num_children > 0: child_c, child_h = zip(*map(lambda x: x.state, tree.children)) child_c = torch.cat(child_c, dim=0) child_h = torch.cat(child_h, dim=0) else: # leaf nodes child_c = embed[0].detach().new_zeros( 1, self.hidden_size).requires_grad_() child_h = embed[0].detach().new_zeros( 1, self.hidden_size).requires_grad_() tree.state = self.node_forward(embed[tree.idx], child_c, child_h) return tree.state def node_forward(self, embeds, child_c, child_h): child_h_sum = torch.sum(child_h, dim=0, keepdim=True) iou = self.ioux(embeds) + self.iouh(child_h_sum) i, o, u = torch.chunk(iou, 3, dim=1) i, o, u = torch.sigmoid(i), torch.sigmoid(o), torch.tanh(u) f = self.fh(child_h) + self.fx(embeds).repeat(len(child_h), 1) fc = torch.mul(torch.sigmoid(f), child_c) c = torch.mul(i, u) + torch.sum(fc, dim=0, keepdim=True) h = torch.mul(o, torch.tanh(c)) return c, h class BinaryTreeLSTM(TreeLSTMBase): def __init__(self, rnn_type, hidden_size, bias=False): super(BinaryTreeLSTM, self).__init__() self.hidden_size = hidden_size self.iou0 = nn.Linear( self.hidden_size, 3 * self.hidden_size, bias=bias) self.iou1 = nn.Linear( self.hidden_size, 3 * self.hidden_size, bias=bias) self.f0 = nn.Linear(self.hidden_size, self.hidden_size, bias=bias) self.f1 = nn.Linear(self.hidden_size, self.hidden_size, bias=bias) def forward(self, child_c, child_h): iou = self.iou0(child_h[0]) + self.iou1(child_h[1]) i, o, u = torch.chunk(iou, 3, dim=2) i, o, u = torch.sigmoid(i), torch.sigmoid(o), torch.tanh(u) f = torch.cat((self.f0(child_h[0]), self.f1(child_h[1])), dim=0) fc = torch.mul(torch.sigmoid(f), torch.cat(child_c, dim=0)).sum( dim=0, keepdim=True) c = torch.mul(i, u) + fc h = torch.mul(o, torch.tanh(c)) return c, h class Tree(): def __init__(self): self.parent = None self.num_children = 0 self.children = list() self.state = None self.idx = None def add_child(self, child): child.parent = self self.num_children += 1 self.children.append(child) def __len__(self): if getattr(self, '_size'): return self._size count = 1 for i in range(self.num_children): count += self.children[i].size() self._size = count return self._size def depth(self): if getattr(self, '_depth'): return self._depth count = 0 if self.num_children > 0: for i in range(self.num_children): child_depth = self.children[i].depth() if child_depth > count: count = child_depth count += 1 self._depth = count return self._depth
nilq/baby-python
python
#! /usr/bin/env python3 # -*- coding: utf-8 -*- # File : losses.py # Author : Jiayuan Mao # Email : maojiayuan@gmail.com # Date : 10/04/2018 # # This file is part of NSCL-PyTorch. # Distributed under terms of the MIT license. import torch import torch.nn as nn import torch.nn.functional as F import jactorch __all__ = ['SigmoidCrossEntropy', 'MultilabelSigmoidCrossEntropy'] class SigmoidCrossEntropy(nn.Module): def __init__(self, one_hot=False): super().__init__() self.one_hot = one_hot self.bce = nn.BCEWithLogitsLoss(reduction='none') def forward(self, input, target): if not self.one_hot: target = jactorch.one_hot_nd(target, input.size(-1)) return self.bce(input, target).sum(dim=-1).mean() class MultilabelSigmoidCrossEntropy(nn.Module): def __init__(self, one_hot=False): super().__init__() self.one_hot = one_hot self.bce = nn.BCEWithLogitsLoss(reduction='none') def forward(self, input, labels): if type(labels) in (tuple, list): labels = torch.tensor(labels, dtype=torch.int64, device=input.device) assert input.dim() == 1 if not self.one_hot: with torch.no_grad(): mask = torch.zeros_like(input) if labels.size(0) > 0: ones = torch.ones_like(labels, dtype=torch.float32) mask.scatter_(0, labels, ones) labels = mask return self.bce(input, labels).sum(dim=-1).mean() class MultitaskLossBase(nn.Module): def __init__(self): super().__init__() self._sigmoid_xent_loss = SigmoidCrossEntropy() self._multilabel_sigmoid_xent_loss = MultilabelSigmoidCrossEntropy() def _mse_loss(self, pred, label): return (pred - label).abs() def _bce_loss(self, pred, label): return -( jactorch.log_sigmoid(pred) * label + jactorch.log_sigmoid(-pred) * (1 - label) ).mean() def _xent_loss(self, pred, label): logp = F.log_softmax(pred, dim=-1) return -logp[label].mean()
nilq/baby-python
python
from typing import List class Solution1: def rotate(self, matrix: List[List[int]]) -> None: n = len(matrix) for i in range(n//2): for j in range(n-n//2): matrix[i][j], matrix[~j][i], matrix[~i][~j], matrix[j][~i] = \ matrix[~j][i], matrix[~i][~j], matrix[j][~i], matrix[i][j] class Solution2: def rotate(self, matrix): matrix[:] = zip(*matrix[::-1])
nilq/baby-python
python
# -*- coding: utf-8 -*- from .darts import * from .priors import * from .posterior import * from .plotting import * from .utils import * from .plot_system_evolution import * __version__ = "1.1.0" # try: # __DART_BOARD_SETUP__ # except NameError: # __DART_BOARD_SETUP__ = False # # if not __DART_BOARD_SETUP__: # __all__ = ["DartBoard"]
nilq/baby-python
python
# Input: a comma-separated list of integers, representing a program and its data TEST = "1,9,10,3,2,3,11,0,99,30,40,50" with open('day02.txt', 'r') as infile: PUZZLE = infile.read() def part1(data): """Return the value at position 0 after running the program. Put 12 and 2 in positions 1 and 2 before running. Instructions are: 1, x, y, r - add the values at positions x and y and put result at r 2, x, y, r - multiply instead of adding 99 - halt the execution """ program = data.split(',') for index in range(len(program)): program[index] = int(program[index]) # Test data has no position 12 and shouldn't be changed if len(program) > 12: program[1] = 12 program[2] = 2 index = 0 operation = program[index] while operation != 99: input1 = program[index + 1] input2 = program[index + 2] output = program[index + 3] if operation == 1: program[output] = program[input1] + program[input2] else: program[output] = program[input1] * program[input2] index += 4 operation = program[index] return program[0] assert part1(TEST) == 3500 assert part1("1,0,0,0,99") == 2 # p[0] = p[0] + p[0] = 1 + 1 = 2 assert part1("2,3,0,3,99") == 2 # p[3] = p[3] * p[0] = 3 * 2 = 6 assert part1("1,1,1,4,99,5,6,0,99") == 30 # p[4] = p[1] + p[1] = 1 + 1 = 2 # p[0] = p[5] * p[6] = 5 * 6 = 30 print(part1(PUZZLE)) def part2(data): """Return 100 * x + y where x and y in positions 1 and 2 make the program halt with 19690720 in position 0. In the problem description, x and y are called noun and verb. The values will be between 0 and 99, inclusive. """ def result(memory, noun, verb): memory[1] = noun memory[2] = verb ip = 0 # instruction pointer opcode = memory[ip] while opcode != 99: parameter1 = memory[ip+1] parameter2 = memory[ip+2] parameter3 = memory[ip+3] if opcode == 1: memory[parameter3] = memory[parameter1] + memory[parameter2] else: memory[parameter3] = memory[parameter1] * memory[parameter2] ip += 4 opcode = memory[ip] return memory[0] program = data.split(',') for index in range(len(program)): program[index] = int(program[index]) # exhaustive search for the two values for noun in range(100): for verb in range(100): # each run modifies the program, so use a copy of the original one if result(program.copy(), noun, verb) == 19690720: return 100*noun + verb print(part2(PUZZLE))
nilq/baby-python
python
# -*- coding: utf-8 -*- """ Created on Mon Apr 29 18:28:55 2019 @author: yoelr """ from . import Facility from ..decorators import cost from thermosteam import Stream import numpy as np from ... import HeatUtility # from copy import copy __all__ = ('CoolingTower',) #'CoolingTowerWithPowerDemand') @cost('Flow rate', 'Cooling water pump', S=557183, kW=1021, cost=283671, CE=551, n=0.8, BM=3.1) @cost('Flow rate', 'Cooling tower', S=557183, kW=1598, cost=1375e3, CE=551, n=0.7, BM=1.5) class CoolingTower(Facility): """Create a cooling tower that is cost based on flow rate of cooling water.""" _units = {'Flow rate': 'kmol/hr'} _N_heat_utilities = 1 _N_outs = _N_ins = 2 evaporation = 0.01 blowdown = 0.001 def __init__(self, ID=''): thermo = HeatUtility.cooling_agents['Cooling water'].thermo self.makeup_water = makeup_water = Stream('cooling_tower_makeup_water', thermo=thermo) loss = makeup_water.flow_proxy() loss.ID = 'evaporation_and_blowdown' super().__init__(ID, ('return_cooling_water', makeup_water), ('cooling_water', loss), thermo=thermo) self.cooling_water_utilities = set() def _design(self): cwu = self.cooling_water_utilities if not cwu: for u in self.system.units: if u is self: continue for hu in u.heat_utilities: if hu.ID == 'Cooling water': cwu.add(hu) used = self._ins[0] #: Cooling water flow rate (kmol/hr) used.mol[0] = \ self.design_results['Flow rate'] = \ self.cooling_water = sum([i.flow for i in cwu]) hu = self.heat_utilities[0] cw = hu.cooling_agents['Cooling water'] self._outs[0].T = cw.T hu.ID = 'Cooling water' hu.cost = -self.cooling_water*cw.price_kmol self.makeup_water.mol[0] = self.cooling_water * (self.evaporation + self.blowdown) CoolingTower._N_outs = CoolingTower._N_ins = 2 # class CoolingTowerWithPowerDemand(CoolingTower): # _has_power_utility = True # _N_heat_utilities = 1 # cost_options = copy(CoolingTower.cost_items) # cost_options['Cooling tower'].kW = 0.1 # def _cost(self): # super()._cost() # q = self._molar_flow # kmol/hr # hu = self.heat_utilities[0] # cw = hu.cooling_agents['Cooling water'] # hu.ID = 'Cooling water' # hu.flow = -q # hu.cost = -q*cw.price_kmol
nilq/baby-python
python
# Evalúa qué puntos del grid del CRU corresponden a # la Cuenca del Valle de México. import os import pandas as pd import numpy as np import geoviews as gv import geopandas as gpd gv.extension("matplotlib") gv.output(size = 150) fdir_d = os.getcwd() + "/data/Cuencas/Regiones_Hidrologicas_Administrativas/" fdir_r = os.getcwd() + "/results/sequia/" fname = "rha250kgw.shp" # Si no existe la carpeta, la crea. if not os.path.exists(fdir_r): os.mkdir(fdir_r) # Se cargan las regiones hidrológico administrativas. gdf = gpd.read_file(fdir_d + fname) # Se obtiene el contorno de las cuencas- gdf["boundary"] = gdf.boundary # Se selecciona la Cuenca del Valle de México. cuenca = gv.Path( gdf[gdf["ORG_CUENCA"] == "Aguas del Valle de México"]).opts( color = "black") # Número de puntos de grid a revisar en dirección de la longitud y la latitud. n = 4 lon = np.empty((1, n)) lat = np.empty((n, 1)) lonp = np.empty((1, n)) latp = np.empty((n, 1)) # Pivotes iniciales de lon y lat. lon_0 = -99.75 lat_0 = 20.75 # Número de punto de grid con respecto a los archivos mexico_cru. lonp_0 = 38 latp_0 = 12 # Se genera la lista de longitudes y latitudes cada 0.5°. for i in range(0,n): lon[0, i] = lon_0 + 0.5 * i lat[i, 0] = lat_0 - 0.5 * i lonp[0, i] = lonp_0 + 1 * i latp[i, 0] = latp_0 - 1 * i # Se genera la malla. lons = np.repeat(lon, n, axis = 0) lats = np.repeat(lat, n, axis = 1) lons_p = np.repeat(lonp, n, axis = 0) lats_p = np.repeat(latp, n, axis = 1) # Se concatenan y crean pares de lon y lat en toda la malla. points = list(zip(lons.flatten(), lats.flatten(), lons_p.flatten(), lats_p.flatten())) # Se convierten a puntos de geoviews. points_gv = [gv.Points(x).opts(color = "black") for x in points] carre = [] # Se crea cadena wkt de perímetro de área de influencia de los puntos de grid. for i, element in enumerate(points): carre.append("POLYGON((" + str(element[0] - 0.25) + " " + str(element[1] - 0.25) + ", " + str(element[0] - 0.25) + " " + str(element[1] + 0.25) + ", " + str(element[0] + 0.25) + " " + str(element[1] + 0.25) + ", " + str(element[0] + 0.25) + " " + str(element[1] - 0.25) + ", " + str(element[0] - 0.25) + " " + str(element[1] - 0.25) + "))") # Se crea GeoDataFrame con geometría a partir de wkt. df_poly = pd.DataFrame({"geometry": carre}) df_poly["geometry"] = gpd.GeoSeries.from_wkt(df_poly["geometry"]) gdf_poly = gpd.GeoDataFrame( df_poly, geometry = df_poly.geometry, crs = "epsg:4326") # Se proyecta la geometría a UTM y calcular área. gdf_poly["Area"] = gdf_poly.to_crs("epsg:32633").area gdf_poly["Intersect"] = 0 for i in gdf_poly.index: # Se calcula área de intersección entre áreas de influencia y cuenca. overlay = gpd.overlay( gdf[gdf["ORG_CUENCA"] == "Aguas del Valle de México"], gdf_poly[gdf_poly.index == i], how = "intersection" )["geometry"].to_crs("epsg:32633").area # Se asegura que haya ceros en el GeoDataFrame en caso de no haber # intersección. if len(overlay.index) > 0: gdf_poly.loc[i, "Intersect"] = overlay[0] # Se calcula el porcentaje de intersección entre áreas de influencia # y cuenca. gdf_poly["Per_intersect"] = gdf_poly["Intersect"] / gdf_poly["Area"] # Se calcula el porcentaje del área de la cuenca cubierto por cada área # de influencia. gdf_poly["Per_cuenca"] = ( gdf_poly["Intersect"] / gdf[gdf["ORG_CUENCA"] == "Aguas del Valle de México"].to_crs( "epsg:32633").area.iloc[0] ) opts_yes = {"alpha": 0.3, "edgecolor": "black", "facecolor": "blue", "linewidth": 1.5} opts_no = {"alpha": 0.3, "edgecolor": "black", "facecolor": "red", "linewidth": 1.5} graph = cuenca columns = ["lon", "lat", "lonp", "latp", "Per_intersect", "Per_cuenca"] df_cuenca = pd.DataFrame(columns = columns) # Se concatenan las gráficas de la cuenca y los puntos de grid. for element in points_gv: graph *= element # Se concatenan las gráficas de la cuenca y los puntos de grid. for i in range(0, len(gdf_poly)): # Si el porcentaje de intersección es menor a 0.5, el área de # influencia se grafica de color rojo. if gdf_poly.Per_intersect[i] < 0.5: graph *= gv.Shape(gdf_poly.geometry[i]).opts(**opts_no) # Si el porcentaje de intersección es mayor o igual a 0.5, el # área de influencia se grafica de color azul. else: graph *= gv.Shape(gdf_poly.geometry[i]).opts(**opts_yes) # Se crea un DataFrame con la información de los puntos de # grid a utilizar. df_cuenca = df_cuenca.append({ columns[0]: points[i][0], columns[1]: points[i][1], columns[2]: points[i][2], columns[3]: points[i][3], columns[4]: gdf_poly.at[i, columns[4]], columns[5]: gdf_poly.at[i, columns[5]]}, ignore_index = True) # Se asegura que el número de punto de grid con respecto a los archivos # mexico_cru sea un entero. df_cuenca = df_cuenca.astype({columns[2]: "int32", columns[3]: "int32"}) # Se calcula la suma acumulada del área de la cuenca cubierto por cada # área de influencia. df_cuenca["Per_cuenca_cumsum"] = df_cuenca.Per_cuenca.cumsum() graph.opts( title = "Región Hidrológico Administrativa\nAguas del Valle de México", fontsize = 18) gv.save(graph, fdir_r + "cuenca_grid.png") df_cuenca.to_csv(fdir_r + "grid_points.csv", index = False)
nilq/baby-python
python
# -*- coding: utf-8 -*- # Copyright (c) 2019-2021 Ramon van der Winkel. # All rights reserved. # Licensed under BSD-3-Clause-Clear. See LICENSE file for details. # importeer individuele competitie historie from django.core.management.base import BaseCommand from HistComp.models import HistCompetitie, HistCompetitieIndividueel from NhbStructuur.models import NhbVereniging from Sporter.models import Sporter import argparse TOEGESTANE_KLASSEN = ('Recurve', 'Compound', 'Barebow', 'Longbow', 'Instinctive Bow') class Command(BaseCommand): help = "Importeer historische competitie uitslag, individueel" verbose = False def __init__(self, stdout=None, stderr=None, no_color=False, force_color=False): super().__init__(stdout, stderr, no_color, force_color) self._count_not6scores = 0 self._count_noname = 0 self._count_skip = 0 self._count_error = 0 self._count_added = 0 self._count_dupe = 0 self._count_dupe_bow = 0 self._boogtype2histcomp = dict() # [boogtype] = HistCompetitie def add_arguments(self, parser): parser.add_argument('filename', nargs=1, type=argparse.FileType("r"), help="in te lezen file") parser.add_argument('seizoen', nargs=1, help="competitie seizoen: 20xx/20yy") parser.add_argument('comptype', nargs=1, choices=('18', '25'), help="competitie type: 18 of 25") parser.add_argument('--verbose', action='store_true') @staticmethod def make_or_find_histcompetitie(seizoen, comp_type, klasse): # check if the record already exists objs = HistCompetitie.objects.filter( seizoen=seizoen, comp_type=comp_type, klasse=klasse, is_team=False) if len(objs): # return existing object histcompetitie = objs[0] else: # create new object histcompetitie = HistCompetitie() histcompetitie.seizoen = seizoen histcompetitie.comp_type = comp_type histcompetitie.klasse = klasse histcompetitie.is_team = False histcompetitie.save() return histcompetitie def _verwijder_eerdere_import(self, seizoen, comptype): objs = HistCompetitie.objects.filter(seizoen=seizoen, comp_type=comptype) if len(objs): objs.delete() @staticmethod def _convert_scores(scores): aantal = 0 totaal = 0 getallen = list() for score in scores: getal = int(score) getallen.append(getal) if getal: aantal += 1 totaal += getal # for return getallen, aantal, totaal def _import(self, lines, seizoen, comptype): # sanity-check voor de hele file linenr = 0 for line in lines: linenr += 1 spl = line.split(';') if len(spl) != 11: self.stderr.write("[ERROR] Fout in regel %s: niet 11 kolommen" % linenr) self._count_error += 1 if linenr > 1 and spl[2] not in TOEGESTANE_KLASSEN: self.stderr.write('[ERROR] Regel %s: onbekende klasse %s' % (linenr, repr(spl[1]))) self._count_error += 1 # for if lines[0].split(";")[0] != "bondsnummer": self.stderr.write("[ERROR] Eerste regels bevat geen headers: %s" % repr(lines[0])) self._count_error += 1 del lines[0] if self._count_error > 0: return histcomps = dict() # ['klasse'] = HistCompetitie() indiv_scores = list() # (gem, scores, HistCompetitieIndividueel) bulk = list() line_nr = 0 for line in lines: line_nr += 1 spl = line.strip().split(";") # spl = [lid_nr, klasse, score1..7, gemiddelde] lid_nr = spl[0] ver_nr = spl[1] klasse = spl[2] # boogtype try: histcompetitie = histcomps[klasse] except KeyError: # nieuwe klasse histcomps[klasse] = histcompetitie = self.make_or_find_histcompetitie(seizoen, comptype, klasse) # fantaseer een redelijk boogtype voor elke klasse if "Recurve" in klasse: boogtype = "R" elif "Compound" in klasse: boogtype = "C" elif "Barebow" in klasse: boogtype = "BB" elif "Longbow" in klasse: boogtype = "LB" elif "Instinctive" in klasse: boogtype = "IB" else: self.stdout.write('[WARNING] Onzeker welk boogtype voor klasse %s' % repr(klasse)) self._count_skip += 1 continue self._boogtype2histcomp[boogtype] = histcompetitie # overslaan als er niet ten minste 6 scores zijn scores, _, totaal = self._convert_scores(spl[3:3+7]) # naam van het lid erbij zoeken (spelling in CRM is leidend) try: sporter = Sporter.objects.get(lid_nr=lid_nr) except Sporter.DoesNotExist: # kan naam nu niet vonden - toch importeren en later aanvullen print("[WARNING] Kan naam niet vinden bij NHB nummer %s" % repr(lid_nr)) sporter = None self._count_noname += 1 # naam van de vereniging opzoeken en opslaan try: ver = NhbVereniging.objects.get(ver_nr=ver_nr) ver_naam = ver.naam except NhbVereniging.DoesNotExist: # fall-back voor recent verwijderde verenigingen if ver_nr == '1026': ver_naam = 'Victoria' elif ver_nr == '1058': ver_naam = 'Willem Tell' elif ver_nr == '1093': ver_naam = 'De Bosjagers' elif ver_nr == '1147': ver_naam = 'Diana' elif ver_nr == '1152': ver_naam = 'Ons Genoegen' elif ver_nr == '1170': ver_naam = 'Batavieren Treffers' elif ver_nr == '1191': ver_naam = 'Eendracht St Sebast' elif ver_nr == '1226': ver_naam = 'Centaur Asten' else: ver_naam = '?' self.stdout.write('[WARNING] Kan geen naam opzoeken voor verwijderde vereniging %s' % ver_nr) gemiddelde = float(spl[10].replace(',', '.')) # 9,123 --> 9.123 hist = HistCompetitieIndividueel() hist.histcompetitie = histcompetitie hist.rank = 0 hist.schutter_nr = lid_nr if sporter: hist.schutter_naam = " ".join([sporter.voornaam, sporter.achternaam]) hist.boogtype = boogtype hist.vereniging_nr = ver_nr hist.vereniging_naam = ver_naam hist.score1 = scores[0] hist.score2 = scores[1] hist.score3 = scores[2] hist.score4 = scores[3] hist.score5 = scores[4] hist.score6 = scores[5] hist.score7 = scores[6] scores.sort(reverse=True) lowest = scores[-1] if hist.score7 == lowest: hist.laagste_score_nr = 7 elif hist.score6 == lowest: hist.laagste_score_nr = 6 elif hist.score5 == lowest: hist.laagste_score_nr = 5 elif hist.score4 == lowest: hist.laagste_score_nr = 4 elif hist.score3 == lowest: hist.laagste_score_nr = 3 elif hist.score2 == lowest: hist.laagste_score_nr = 2 else: hist.laagste_score_nr = 1 hist.gemiddelde = gemiddelde hist.totaal = totaal - lowest # check if the record already exists dupe = HistCompetitieIndividueel.objects.filter( histcompetitie=hist.histcompetitie, schutter_nr=hist.schutter_nr, vereniging_nr=hist.vereniging_nr) if len(dupe) > 0: tup = (gemiddelde, scores, len(indiv_scores), dupe[0]) indiv_scores.append(tup) self._count_dupe += 1 else: tup = (gemiddelde, scores, len(indiv_scores), hist) indiv_scores.append(tup) bulk.append(hist) self._count_added += 1 if len(bulk) >= 100: HistCompetitieIndividueel.objects.bulk_create(bulk) bulk = list() # for if len(bulk): HistCompetitieIndividueel.objects.bulk_create(bulk) # deel de rank nummers opnieuw uit ranks = dict() # ['boogtype'] = int indiv_scores.sort(reverse=True) for gem, scores, nr, hist in indiv_scores: try: ranks[hist.boogtype] += 1 hist.rank = ranks[hist.boogtype] except KeyError: hist.rank = 1 ranks[hist.boogtype] = hist.rank hist.save() # for def _delete_dupes(self): """ Sommige BB/IB/LB schutters staan in de geïmporteerde data OOK genoemd in de recurve klasse, met exact dezelfde scores. Dit was nodig in het oude programma voor het team schieten waarbij een Recurve team ook BB/IB/LB schutters mag bevatten. Andere schutters schieten zowel de R als C klasse of the BB en R klasse en hebben dan niet dezelfde scores. Zoek de NHB nummers van R schutters die ook in de BB/IB/LB voorkomen Als de scores ook overeen komen, verwijder dan het records in de R klasse. """ self.stdout.write("[INFO] Removing duplicates from Recurve results (dupe with BB/IB/LB)") try: histcomp_r = self._boogtype2histcomp['R'] except KeyError: return # doorloop de kleinste klassen for boogtype in ('BB', 'IB', 'LB'): for houtobj in HistCompetitieIndividueel.objects.filter(boogtype=boogtype, histcompetitie=self._boogtype2histcomp[boogtype]): # zoek dit nummer op in de Recurve klasse try: robj = HistCompetitieIndividueel.objects.get(boogtype='R', histcompetitie=histcomp_r, schutter_nr=houtobj.schutter_nr) except HistCompetitieIndividueel.DoesNotExist: pass else: if houtobj.totaal == robj.totaal: # controleer dat alle scores overeen komen if (houtobj.score1 == robj.score1 and houtobj.score2 == robj.score2 and houtobj.score3 == robj.score3 and houtobj.score4 == robj.score4 and houtobj.score5 == robj.score5 and houtobj.score6 == robj.score6 and houtobj.score7 == robj.score7): # gevonden # verwijder het recurve object # hierdoor valt helaas een gat in de ranking self._count_dupe_bow += 1 robj.delete() # print("lid_nr:%s, hout:%s, totaal_1:%s, totaal_2:%s" % (houtobj.schutter_nr, houtobj.boogtype, houtobj.totaal, robj.totaal)) # if # for # for def handle(self, *args, **options): # self.stderr.write("import individuele competitie historie. args=%s, options=%s" % (repr(args), repr(options))) self.verbose = options['verbose'] comptype = options['comptype'][0] seizoen = options['seizoen'][0] if len(seizoen) != 9 or seizoen[4] != "/": self.stderr.write("[ERROR] Seizoen moet het formaat 'jaar/jaar+1' hebben, bijvoorbeeld '2010/2011' (was %s)" % repr(seizoen)) return try: lines = options['filename'][0].readlines() except UnicodeDecodeError as exc: self.stderr.write("File has format issues (%s)" % str(exc)) return linecount = len(lines) # verwijder de eerder geïmporteerde uitslag self._verwijder_eerdere_import(seizoen, comptype) self._import(lines, seizoen, comptype) self._delete_dupes() self.stdout.write("Read %s lines; skipped %s dupes; %s skipped;" " %s skip with errors; %s skip dupe bow score; added %s records;" " %s without name" % (linecount, self._count_dupe, self._count_skip, self._count_error, self._count_dupe_bow, self._count_added - self._count_dupe_bow, self._count_noname)) # end of file
nilq/baby-python
python
#!/usr/bin/env python2 # Copyright (c) 2017, ESET # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from Crypto.Cipher import CAST import sys import argparse def main(): parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter) parser.add_argument("-e", "--encrypt", help="encrypt carbon file", required=False) parser.add_argument("-d", "--decrypt", help="decrypt carbon file", required=False) try: args = parser.parse_args() except IOError as e: parser.error(e) return 0 if len(sys.argv) != 3: parser.print_help() return 0 key = "\x12\x34\x56\x78\x9A\xBC\xDE\xF0\xFE\xFC\xBA\x98\x76\x54\x32\x10" iv = "\x12\x34\x56\x78\x9A\xBC\xDE\xF0" cipher = CAST.new(key, CAST.MODE_OFB, iv) if args.encrypt: plaintext = open(args.encrypt, "rb").read() while len(plaintext) % 8 != 0: plaintext += "\x00" data = cipher.encrypt(plaintext) open(args.encrypt + "_encrypted", "wb").write(data) else: ciphertext = open(args.decrypt, "rb").read() while len(ciphertext) % 8 != 0: ciphertext += "\x00" data = cipher.decrypt(ciphertext) open(args.decrypt + "_decrypted", "wb").write(data) if __name__ == "__main__": main()
nilq/baby-python
python
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : Joe Gao (jeusgao@163.com) import os import numpy as np from fastapi import FastAPI from predictor import main from storages import DIC_ZODB, milvusDB app = FastAPI() collection_cosine = 'dependency' partition_tags_cosine = '202103' collection_words = 'words' partition_tags_subjects = 'subjects' partition_tags_objects = 'objects' zodb_code = 'dependency' db = DIC_ZODB.get(zodb_code) db.open() '''ZODB API # from pydantic import BaseModel # class ZodbItem(BaseModel): # key: int # subj: str # obj: str # score: float # text: str # @app.post("/zodb_insert") # async def zodb_insert(item: ZodbItem, db_code: str='dependency'): # db = DIC_ZODB.get(db_code) # key = item.key # value = {'text': item.text, 'subject': item.subj, 'object': item.obj, 'score': item.score} # msg = db.insert(key, value) # return {'result': msg} # @app.post("/zodb_search") # async def zodb_search(key: int, db_code: str='dependency'): # db = DIC_ZODB.get(db_code) # return {'result': db.search(key)} ''' def normalization(x): return (x - np.min(x)) * 1 / (np.max(x) - np.min(x)) + 1e-9 def _get_rels(text, rels): vecs_s, vecs_o, values = [], [], [] for r in rels: subj = r.get('from_word') pos_subj = r.get('from_pos') obj = r.get('to_word') pos_obj = r.get('to_pos') score = r.get('score') tensors = r.get('tensors') tensor_s = np.array(tensors.get('subject')) tensor_o = np.array(tensors.get('object')) vecs_s.append(tensor_s) vecs_o.append(tensor_o) values.append({ 'text': text, 'subject': subj, 'subject_pos': pos_subj, 'object': obj, 'object_pos': pos_obj, 'score': score, }) return vecs_s, vecs_o, values def _get_qqp(text, _text_candidate): _r_qqp = main('qqp', text, _text_candidate) _is_match, _score = _r_qqp.get('result'), _r_qqp.get('score') return _score @app.get("/{api_name}") async def pred(api_name: str, input1: str, input2: str=None): rst = main(api_name, input1, input2) return rst # @app.post("/ke_insert") # async def ke_insert( # input1: str, # input2: str=None, # dim: int=2048, # model: str='test', # db_code: str='dependency', # partition_tag: str='202103', # index_file_size: int=1024, # ): # rst = main(model, input1, input2) # text, rels = rst.get('text'), rst.get('rels') # vecs, values = _get_rels(text, rels) # ids = milvusDB.insert( # vecs, # dim=dim, # collection_name=db_code, # partition_tag=partition_tag, # index_file_size=index_file_size, # ) # milvusDB.commit() # db = DIC_ZODB.get(db_code) # msg = db.insert(ids, values) # return {'result': msg} def search_cosine(_cosines, values, top_k=5): rst = [] simis_cosine = milvusDB.search( _cosines, collection_name=collection_cosine, partition_tags=partition_tags_cosine, top_k=top_k ) simis_cosine = [[(s.id, s.distance) for s in simi] for simi in simis_cosine] for simi, value in zip(simis_cosine, values): _milvus_candidates = [] for _id, _distance in simi: _milvus_search_rst = db.root.dic_cosines[_id] _milvus_search_rst['distance'] = _distance _text_candidate = _milvus_search_rst.get('text') # qqp_score = _get_qqp(text, _text_candidate) # _milvus_search_rst['text_match_score'] = qqp_score _milvus_candidates.append(_milvus_search_rst) _milvus_candidates = list(sorted(_milvus_candidates, key=lambda x: x.get('distance'))) value['candidates'] = _milvus_candidates del value['text'] rst.append(value) return rst def search_pairs(_vs_s, _vs_o, _cosines, values, top_k=5): rst = [] simis_subjects = milvusDB.search(np.mean(np.array(_vs_s), axis=1), collection_name=collection_words, partition_tags=partition_tags_subjects, top_k=top_k) simis_subjects = [[(s.id, s.distance) for s in simi] for simi in simis_subjects] simis_subjects = list(sorted(simis_subjects, key=lambda x: x[1])) simis_objects = milvusDB.search(np.mean(np.array(_vs_o), axis=1), collection_name=collection_words, partition_tags=partition_tags_objects, top_k=top_k) simis_objects = [[(s.id, s.distance) for s in simi] for simi in simis_objects] simis_objects = list(sorted(simis_objects, key=lambda x: x[1])) all_ids_cosine = [] for simis_subject, simis_object, _cosine, value in zip(simis_subjects, simis_objects, _cosines, values): _cosine_candidates = [] for simi_s, simi_o in zip(simis_subject, simis_object): id_s, dist_s = simi_s id_o, dist_o = simi_o _key = (id_s, id_o) if _key in db.root.dic_pairs.keys(): id_cosine = db.root.dic_pairs[_key] _entity = milvusDB.db.get_entity_by_id(collection_cosine, [id_cosine])[1][0] cosine_simi = np.mean(np.cos(_cosine, np.array(_entity))) _value = db.root.dic_cosines[id_cosine] _value['distance'] = cosine_simi _cosine_candidates.append(_value) if len(_cosine_candidates): value['candidates'] = _cosine_candidates del value['text'] rst.append(value) return rst @app.post("/ke_search") async def ke_search( input1: str, input2: str=None, search_mod: str='cosine', model: str='test', top_k: int=5, ): rst = main(model, input1, input2, from_api=False) text, words, rels = rst.get('text'), rst.get('words'), rst.get('rels') vecs_s, vecs_o, values = _get_rels(text, rels) _maxlen = max(max(len(_s), len(_o)) for _s, _o in zip(vecs_s, vecs_o)) _vs_s = [np.pad(_v, ((0, _maxlen - len(_v)), (0, 0)), 'mean') for _v in vecs_s] _vs_o = [np.pad(_v, ((0, _maxlen - len(_v)), (0, 0)), 'mean') for _v in vecs_o] _cosines = np.mean(np.cos(np.array(_vs_s), np.array(_vs_o)), axis=1) if search_mod == 'cosine': rst = search_cosine(_cosines, values, top_k=top_k) else: rst = search_pairs(_vs_s, _vs_o, _cosines, values, top_k=top_k) # if not len(rst): # rst = search_cosine(_cosines, values, top_k=top_k) return {'result': {'text': text, 'words': words, 'pairs': rst}}
nilq/baby-python
python
# -*- coding: utf-8 -*- import json from flask.ext.script import Manager from app import app import config app.config.from_object(config.DevelopmentConfig) manager = Manager(app) @manager.command def index_data(): print("Indexing...") '''try: app.elasticsearch.indices.create(index='big-one', ignore=400) app.elasticsearch.cluster.health(wait_for_status='yellow', request_timeout=10) # Index the data with open('presentation-data.json') as data_file: data = json.load(data_file) for entry in data: app.elasticsearch.index( index="big-one", doc_type="pizza", id=entry['id'], body=entry['name']) except Exception, e: print "Indexing error: ", str(e)''' if __name__ == '__main__': manager.run()
nilq/baby-python
python
# GOMC Example for the Gibbs Ensemble (GEMC) using MoSDeF [1, 2, 5-10, 13-17] # Note: In this specific example, we will be using the GEMC_NVT ensemble. # Import the required packages and specify the force field (FF) being used. # Note: For GOMC, the residue names are treated as molecules, so the residue names must be unique for each different molecule. [1, 2, 13-17] # Note: Each residue can be set to a different FF, which is done by setting the residue name to a FF in a dictionary (FF_Dict). The FF selection can be a FF name (set from foyer FF repositor) or a specified FF xml file. [1, 2, 13-17] import shutil import pathlib import random from pathlib import Path import os import shutil WolfDefaultKind = "VlugtWIntraCutoff" WolfDefaultPotential = "DSF" WolfDefaultAlpha = [0.21] WolfCutoffBoxList = [0] WolfCutoffCoulombLowerBoundList = [10] WolfCutoffCoulombUpperBoundList = [15] WolfCutoffCoulombIntervalList = [0.5] WolfAlphaLowerBoundList = [0.0] WolfAlphabUpperBoundList = [0.5] WolfAlphaIntervalList = [0.01] shellFile = "cal.sh" wolfCalFreq = 100 for root, dirs, files in os.walk(".", topdown=False): for name in files: if(name == "NVT_Cal_water_ethanol_fe.conf"): shutil.copy2(shellFile, root) path2File = os.path.join(root, name) with open(path2File, "a") as myfile: defPotLine = "Wolf\tTrue\t{pot}\n".format(pot=WolfDefaultPotential) myfile.write(defPotLine) defKindLine = "WolfKind\t{kind}\n".format(kind=WolfDefaultKind) myfile.write(defKindLine) defPotLine = "WolfCalibrationFreq\tTrue\t{freq}\n".format(freq=wolfCalFreq) myfile.write(defPotLine) for box, wolfCutoffLower, wolfCutoffUpper, wolfCutoffInterval, wolfAlphaLower, wolfAlphaUpper, wolfAlphaInterval, defaultAlpha \ in zip(WolfCutoffBoxList, WolfCutoffCoulombLowerBoundList, WolfCutoffCoulombUpperBoundList, WolfCutoffCoulombIntervalList, \ WolfAlphaLowerBoundList, WolfAlphabUpperBoundList, WolfAlphaIntervalList, WolfDefaultAlpha): defAlphaLine = "WolfAlpha\t{box}\t{val}\n".format(box=box, val=defaultAlpha) myfile.write(defAlphaLine) CutoffLine = "WolfCutoffCoulombRange\t{box}\t{lb}\t{ub}\t{inter}\n".format(box=box, lb=wolfCutoffLower, ub=wolfCutoffUpper, inter=wolfCutoffInterval) myfile.write(CutoffLine) alphaLine = "WolfAlphaRange\t{box}\t{lb}\t{ub}\t{inter}\n".format(box=box, lb=wolfAlphaLower, ub=wolfAlphaUpper, inter=wolfAlphaInterval) myfile.write(alphaLine)
nilq/baby-python
python
class Solution: def largestValues(self, root): maxes = [] if not root: return maxes stack = [(root, 0)] while stack: node, level = stack.pop() if level >= len(maxes): # first time reaching this level] # append directly. maxes.append(node.val) else: # update maxes if node.val is larger. maxes[level] = max(node.val, maxes[level]) left, right = node.left, node.right if left: stack.append((left, level+1)) if right: stack.append((right, level+1)) return maxes
nilq/baby-python
python
import math import pickle import torch from torch import distributed as dist from torch.utils.data.sampler import Sampler from torch.nn import SyncBatchNorm import numpy as np def get_rank(): if not dist.is_available(): return 0 if not dist.is_initialized(): return 0 return dist.get_rank() def synchronize(): if not dist.is_available(): return if not dist.is_initialized(): return world_size = dist.get_world_size() if world_size == 1: return dist.barrier() def get_world_size(): if not dist.is_available(): return 1 if not dist.is_initialized(): return 1 return dist.get_world_size() def all_gather(data): world_size = get_world_size() if world_size == 1: return [data] buffer = pickle.dumps(data) storage = torch.ByteStorage.from_buffer(buffer) tensor = torch.ByteTensor(storage).to('cuda') local_size = torch.IntTensor([tensor.numel()]).to('cuda') size_list = [torch.IntTensor([0]).to('cuda') for _ in range(world_size)] dist.all_gather(size_list, local_size) size_list = [int(size.item()) for size in size_list] max_size = max(size_list) tensor_list = [] for _ in size_list: tensor_list.append(torch.ByteTensor(size=(max_size,)).to('cuda')) if local_size != max_size: padding = torch.ByteTensor(size=(max_size - local_size,)).to('cuda') tensor = torch.cat((tensor, padding), 0) dist.all_gather(tensor_list, tensor) data_list = [] for size, tensor in zip(size_list, tensor_list): buffer = tensor.cpu().numpy().tobytes()[:size] data_list.append(pickle.loads(buffer)) return data_list def reduce_loss_dict(loss_dict): world_size = get_world_size() if world_size < 2: return loss_dict with torch.no_grad(): keys = [] losses = [] for k in sorted(loss_dict.keys()): keys.append(k) losses.append(loss_dict[k]) losses = torch.stack(losses, 0) dist.reduce(losses, dst=0) if dist.get_rank() == 0: losses /= world_size reduced_losses = {k: v for k, v in zip(keys, losses)} return reduced_losses # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. # Code is copy-pasted exactly as in torch.utils.data.distributed. # FIXME remove this once c10d fixes the bug it has class DistributedSampler(Sampler): """Sampler that restricts data loading to a subset of the dataset. It is especially useful in conjunction with :class:`torch.nn.parallel.DistributedDataParallel`. In such case, each process can pass a DistributedSampler instance as a DataLoader sampler, and load a subset of the original dataset that is exclusive to it. .. note:: Dataset is assumed to be of constant size. Arguments: dataset: Dataset used for sampling. num_replicas (optional): Number of processes participating in distributed training. rank (optional): Rank of the current process within num_replicas. """ def __init__(self, dataset, num_replicas=None, rank=None, shuffle=True): if num_replicas is None: if not dist.is_available(): raise RuntimeError("Requires distributed package to be available") num_replicas = dist.get_world_size() if rank is None: if not dist.is_available(): raise RuntimeError("Requires distributed package to be available") rank = dist.get_rank() self.dataset = dataset self.num_replicas = num_replicas self.rank = rank self.epoch = 0 self.num_samples = int(math.ceil(len(self.dataset) * 1.0 / self.num_replicas)) self.total_size = self.num_samples * self.num_replicas self.shuffle = shuffle def __iter__(self): if self.shuffle: # deterministically shuffle based on epoch g = torch.Generator() g.manual_seed(self.epoch) indices = torch.randperm(len(self.dataset), generator=g).tolist() else: indices = torch.arange(len(self.dataset)).tolist() # add extra samples to make it evenly divisible indices += indices[: (self.total_size - len(indices))] assert len(indices) == self.total_size # subsample offset = self.num_samples * self.rank indices = indices[offset : offset + self.num_samples] assert len(indices) == self.num_samples return iter(indices) def __len__(self): return self.num_samples def set_epoch(self, epoch): self.epoch = epoch # def sync_batchnorm(model): # world_size = get_world_size() # process_ids = list(range(world_size)) # process_group = dist.new_group(process_ids) # model = SyncBatchNorm.convert_sync_batchnorm(model,process_group) # return model def simple_group_split(world_size, rank, num_groups): # world_size: number of all processes # rank: current process ID # num_groups: number of groups in total, e.g. world_size=8 and you want to use 4 GPUs in a syncBN group, so num_groups=2 groups = [] rank_list = np.split(np.arange(world_size), num_groups) rank_list = [list(map(int, x)) for x in rank_list] for i in range(num_groups): groups.append(dist.new_group(rank_list[i])) group_size = world_size // num_groups print ("Rank no.{} start sync BN on the process group of {}".format(rank, rank_list[rank//group_size])) return groups[rank//group_size] def convert_sync_bn(model, process_group, gpu=None): # convert all BN layers in the model to syncBN for _, (child_name, child) in enumerate(model.named_children()): if isinstance(child, torch.nn.modules.batchnorm._BatchNorm): m = torch.nn.SyncBatchNorm.convert_sync_batchnorm(child, process_group) if (gpu is not None): m = m.cuda(gpu) setattr(model, child_name, m) else: convert_sync_bn(child, process_group, gpu)
nilq/baby-python
python
""" Contains special test cases that fall outside the scope of remaining test files. """ import textwrap from unittest.mock import patch from flake8_type_checking.checker import ImportVisitor from flake8_type_checking.codes import TC001, TC002 from tests import REPO_ROOT, _get_error, mod class TestFoundBugs: def test_mixed_errors(self): example = textwrap.dedent( f""" import {mod} import pytest from x import y """ ) assert _get_error(example) == { '2:0 ' + TC001.format(module=f'{mod}'), '3:0 ' + TC002.format(module='pytest'), '4:0 ' + TC002.format(module='x.y'), } def test_type_checking_block_imports_dont_generate_errors(self): example = textwrap.dedent( """ import x from y import z if TYPE_CHECKING: import a # arbitrary whitespace from b import c def test(): pass """ ) assert _get_error(example) == { '2:0 ' + TC002.format(module='x'), '3:0 ' + TC002.format(module='y.z'), } def test_model_declarations_dont_trigger_error(self): """ Initially found false positives in Django project, because name visitor did not capture the SomeModel usage in the example below. """ example = textwrap.dedent( """ from django.db import models from app.models import SomeModel class LoanProvider(models.Model): fk: SomeModel = models.ForeignKey( SomeModel, on_delete=models.CASCADE, ) """ ) assert _get_error(example) == set() def test_all_list_declaration(self): """ __all__ declarations originally generated false positives. """ example = textwrap.dedent( """ from app.models import SomeModel from another_app.models import AnotherModel __all__ = [ 'SomeModel', 'AnotherModel' ] """ ) assert _get_error(example) == set() def test_all_tuple_declaration(self): """ __all__ declarations originally generated false positives. """ example = textwrap.dedent( """ from app.models import SomeModel from another_app.models import AnotherModel __all__ = ( 'SomeModel', 'AnotherModel' ) """ ) assert _get_error(example) == set() def test_callable_import(self): """ __all__ declarations originally generated false positives. """ example = textwrap.dedent( """ from x import y class X: def __init__(self): self.all_sellable_models: list[CostModel] = y( country=self.country ) """ ) assert _get_error(example) == set() def test_ellipsis(self): example = textwrap.dedent( """ x: Tuple[str, ...] """ ) assert _get_error(example) == set() def test_literal(self): example = textwrap.dedent( """ from __future__ import annotations x: Literal['string'] """ ) assert _get_error(example) == set() def test_conditional_import(self): example = textwrap.dedent( """ version = 2 if version == 2: import x else: import y as x var: x """ ) assert _get_error(example) == {"7:4 TC002 Move third-party import 'x' into a type-checking block"} def test_import_is_local(): """ Check that if ValueErrors are raised in _import_is_local, we bump it into the TC002 bucket. """ def raise_value_error(*args, **kwargs): raise ValueError('test') visitor = ImportVisitor(REPO_ROOT, False, False, False, []) assert visitor._import_is_local(mod) is True patch('flake8_type_checking.checker.find_spec', raise_value_error).start() assert visitor._import_is_local(mod) is False patch.stopall()
nilq/baby-python
python
from .resources import BaseResource, BaseWithSubclasses class SwitchConnection: def __init__(self, resource, connection_alias): self.resource = resource self._connection_alias = connection_alias def __enter__(self): if issubclass(self.resource, BaseWithSubclasses): modified = self._create_modified_base_with_subclasses() elif issubclass(self.resource, BaseResource): modified = self._create_modified_base() else: raise ValueError("'{}' is not a mass_api_client resource.".format(type(self.resource))) modified.__name__ = "Modified{}".format(self.resource.__name__) return modified def __exit__(self, exc_type, exc_val, exc_tb): pass def _create_modified_base(self): class ModifiedResource(self.resource): _connection_alias = self._connection_alias return ModifiedResource def _create_modified_base_with_subclasses(self): class ModifiedResource(self.resource): _connection_alias = self._connection_alias _unmodified_cls = self.resource @classmethod def _create_instance_from_data(cls, data): subcls = cls._unmodified_cls._search_subclass(data['_cls']) return subcls(cls._connection_alias, **data) @classmethod def _deserialize(cls, data, many=False): if many: return [cls._deserialize(item) for item in data] subcls = cls._unmodified_cls._search_subclass(data['_cls']) return subcls._deserialize(data, many) return ModifiedResource
nilq/baby-python
python
# !/usr/bin/env python # -*- coding: utf-8 -*- """ Connector to generate connect, engine and session object based on parameters defined in config.yaml """ __author__ = 'Laurent.Chen' __date__ = '2019/7/15' __version__ = '1.0.0' import os import yaml from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker with open(os.path.join(os.path.dirname(__file__), 'config.yaml'), 'r', encoding='utf-8') as f: config_dict = yaml.full_load(f) class Connector(object): """ MySQL connector :param str schema: name of target schema """ host = config_dict['host'] port = config_dict['port'] user = config_dict['user'] password = config_dict['password'] charset = config_dict['charset'] def __init__(self, schema): if not isinstance(schema, str): raise ValueError('schema must be str type') self.schema = schema def get_engine(self): """ get SQLAlchemy engine :return: SQLAlchemy engine :rtype: sqlalchemy.engine.base.Engine """ engine = create_engine(f"mysql+mysqldb://{self.user}:{self.password}@{self.host}:{self.port}/{self.schema}" f"?charset=utf8mb4") return engine def get_connect(self): """ get SQLAlchemy connect :return: SQLAlchemy connect :rtype: sqlalchemy.engine.base.Connection """ conn = self.get_engine().connect() return conn def get_session(self): """ get SQLAlchemy session :return: SQLAlchemy session """ session = sessionmaker(bind=self.get_engine()) return session() if __name__ == '__main__': pass
nilq/baby-python
python
from django.contrib import admin from django.contrib.auth.admin import UserAdmin from app1.models import MyUser # Register your models here. admin.site.register(MyUser, UserAdmin)
nilq/baby-python
python
class Solution: def countPairs(self, nums: List[int], k: int) -> int: result=0 for i in range(len(nums)-1): for j in range(i+1, len(nums)): if nums[i]==nums[j] and (i*j)%k==0: result+=1 return result
nilq/baby-python
python