Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_transmit_raises_if_no_connection_to_destination(self, db_session):
net1 = models.Network()
net2 = models.Network()
agent1 = nodes.ReplicatorAgent(network=net1)
agent2 = nodes.ReplicatorAgent(network=net2)
info1 = models.Info(origin=agent1, contents="foo")
info2 = models.Info(origin=agent1, contents="bar")
self.add(db_session, info1, info2)
with raises(ValueError) as excinfo:
agent1.transmit(what=None, to_whom=agent2)
assert excinfo.match("cannot transmit to {}".format(agent2))
def test_transmission_order(self, db_session):
net = models.Network()
db_session.add(net)
agent1 = nodes.ReplicatorAgent(network=net)
agent2 = nodes.ReplicatorAgent(network=net)
agent3 = nodes.ReplicatorAgent(network=net)
self.add(db_session, agent1, agent2, agent3)
db_session.commit()
agent1.connect(whom=agent2)
agent1.connect(whom=agent3)
self.add(db_session, agent1, agent2, agent3)
info1 = models.Info(origin=agent1, contents="foo")
info2 = models.Info(origin=agent1, contents="bar")
info3 = models.Info(origin=agent1, contents="baz")
info4 = models.Info(origin=agent1, contents="spam")
self.add(db_session, info1, info2, info3, info4)
agent1.transmit(what=info1, to_whom=agent2)
agent2.receive()
agent1.transmit(what=info2, to_whom=agent3)
agent3.receive()
agent1.transmit(what=info3, to_whom=agent2)
agent2.receive()
agent1.transmit(what=info4, to_whom=agent3)
agent3.receive()
db_session.commit()
transmissions = agent1.transmissions()
assert len(transmissions) == 4
assert transmissions[0].receive_time < transmissions[1].receive_time
def test_transmission_repr(self, db_session):
net = models.Network()
db_session.add(net)
node1 = models.Node(network=net)
node2 = models.Node(network=net)
self.add(db_session, node1, node2)
node1.connect(whom=node2)
models.Info(origin=node1)
node1.transmit(what=node1.infos()[0], to_whom=node2)
transmission = node1.transmissions()[0]
node1.vectors()[0]
assert repr(transmission).split("-") == ["Transmission", str(transmission.id)]
def test_agent_transmit_no_connection(self, a):
net = a.network()
agent1 = a.replicator(network=net)
agent2 = a.replicator(network=net)
info = models.Info(origin=agent1)
with raises(ValueError):
agent1.transmit(what=info, to_whom=agent2)
def test_transmit_to_class_finds_nodes_in_network(self, db_session):
net = models.Network()
agent1 = nodes.ReplicatorAgent(network=net)
agent2 = nodes.ReplicatorAgent(network=net)
agent1.connect(whom=agent2)
info1 = models.Info(origin=agent1, contents="foo")
self.add(db_session, info1)
transmissions = agent1.transmit(what=info1, to_whom=nodes.ReplicatorAgent)
assert len(transmissions) == 1
assert transmissions[0].origin is agent1
assert transmissions[0].destination is agent2
def test_transmission_order(self, db_session):
net = models.Network()
db_session.add(net)
agent1 = nodes.ReplicatorAgent(network=net)
agent2 = nodes.ReplicatorAgent(network=net)
agent3 = nodes.ReplicatorAgent(network=net)
self.add(db_session, agent1, agent2, agent3)
db_session.commit()
agent1.connect(whom=agent2)
agent1.connect(whom=agent3)
self.add(db_session, agent1, agent2, agent3)
info1 = models.Info(origin=agent1, contents="foo")
info2 = models.Info(origin=agent1, contents="bar")
info3 = models.Info(origin=agent1, contents="baz")
info4 = models.Info(origin=agent1, contents="spam")
self.add(db_session, info1, info2, info3, info4)
agent1.transmit(what=info1, to_whom=agent2)
agent2.receive()
agent1.transmit(what=info2, to_whom=agent3)
agent3.receive()
agent1.transmit(what=info3, to_whom=agent2)
agent2.receive()
agent1.transmit(what=info4, to_whom=agent3)
agent3.receive()
db_session.commit()
transmissions = agent1.transmissions()
assert len(transmissions) == 4
def vote(self, nodes):
votes = {}
for node in nodes:
vote = choice(Node.query.filter_by(
network_id=self.id,
property2='True'
).all()).property1
node_votes = Info.query.filter_by(
origin_id=node.id,
type='vote'
).order_by('creation_time')
if node_votes.first() is not None:
node_vote = node_votes[-1].contents.split(': ')[1]
if Node.query.filter_by(
property1=node_vote).one().property2 == 'True':
vote = node_vote
if vote in votes:
votes[vote] += 1
else:
votes[vote] = 1
k = list(votes.keys())
v = list(votes.values())
victim_name = k[v.index(max(v))]
victim_node = Node.query.filter_by(property1=victim_name).one()
def node_infos(node_id):
"""Get all the infos of a node.
The node id must be specified in the url.
You can also pass info_type.
"""
exp = Experiment(session)
# get the parameters
info_type = request_parameter(parameter="info_type",
parameter_type="known_class",
default=models.Info)
if type(info_type) == Response:
return info_type
# check the node exists
node = models.Node.query.get(node_id)
if node is None:
return error_response(error_type="/node/infos, node does not exist")
try:
# execute the request:
infos = node.infos(type=info_type)
# ping the experiment
exp.info_get_request(
node=node,
infos=infos)
def infos(self, type=None, failed=False):
"""Get infos that originate from this node.
Type must be a subclass of :class:`~dallinger.models.Info`, the default is
``Info``. Failed can be True, False or "all".
"""
if type is None:
type = Info
if not issubclass(type, Info):
raise TypeError(
"Cannot get infos of type {} " "as it is not a valid type.".format(type)
)
if failed not in ["all", False, True]:
raise ValueError("{} is not a valid vector failed".format(failed))
if failed == "all":
return type.query.filter_by(origin_id=self.id).all()
else:
return type.query.filter_by(origin_id=self.id, failed=failed).all()
def getdrawings():
"""Get all the drawings."""
infos = Info.query.all()
sketches = [json.loads(info.contents) for info in infos]
return jsonify(drawings=sketches)