<commit_before># -*- coding: utf-8 -*-
'''
Encapsulate the different transports available to Salt.  Currently this is only ZeroMQ.
'''
import time
import os
import threading

# Import Salt Libs
import salt.payload
import salt.auth
import salt.utils
import logging
from collections import defaultdict

log = logging.getLogger(__name__)

try:
    from raet import raeting, nacling
    from raet.lane.stacking import LaneStack
    from raet.lane.yarding import RemoteYard

except ImportError:
    # Don't die on missing transport libs since only one transport is required
    pass

jobber_stack = None  # global that holds raet jobber LaneStack
jobber_rxMsgs = {}  # dict of deques one for each RaetChannel

class Channel(object):
    '''
    Factory class to create communication-channels for different transport
    '''
    @staticmethod
    def factory(opts, **kwargs):
        # Default to ZeroMQ for now
        ttype = 'zeromq'

        # determine the ttype
        if 'transport' in opts:
            ttype = opts['transport']
        elif 'transport' in opts.get('pillar', {}).get('master', {}):
            ttype = opts['pillar']['master']['transport']

        # switch on available ttypes
        if ttype == 'zeromq':
            return ZeroMQChannel(opts, **kwargs)
        elif ttype == 'raet':
            return RAETChannel(opts, **kwargs)
        else:
            raise Exception('Channels are only defined for ZeroMQ and raet')
            # return NewKindOfChannel(opts, **kwargs)

class RAETChannel(Channel):
    '''
    Build the communication framework to communicate over the local process
    uxd socket and send messages forwarded to the master. then wait for the
    relative return message.

    Two use cases:
        mininion to master communication, normal use case
           Minion is communicating via yard through minion Road to master
           The destination route needs the estate name of the associated master
        master call via runner, special use case
           In the special case the master call external process is communicating
           via a yard with the master manor yard
           The destination route estate is None to indicate local estate

        The difference between the two is how the destination route
        is assigned.
    '''
    def __init__(self, opts, usage=None, **kwargs):
        self.opts = opts
        self.ttype = 'raet'
        if usage == 'master_call':
            self.dst = (None, None, 'local_cmd')  # runner.py master_call
        elif usage == 'salt_call':
            self.dst = (None, None, 'remote_cmd')  # salt_call caller
        else: # everything else
            self.dst = (None, None, 'remote_cmd')  # normal use case minion to master
        self.stack = None

    def _setup_stack(self):
        '''
        Setup and return the LaneStack and Yard used by by channel when global
        not already setup such as in salt-call to communicate to-from the minion

        '''
        mid = self.opts.get('id', 'master')
        uid = nacling.uuid(size=18)
        name = 'channel' + uid
        stack = LaneStack(name=name,
                          lanename=mid,
                          sockdirpath=self.opts['sock_dir'])

        stack.Pk = raeting.packKinds.pack
        stack.addRemote(RemoteYard(stack=stack,
                                   name='manor',
                                   lanename=mid,
                                   dirpath=self.opts['sock_dir']))
        log.debug("Created Channel Jobber Stack {0}\n".format(stack.name))
        return stack

    def __prep_stack(self):
        '''
        Prepare the stack objects
        '''
        global jobber_stack
        if not self.stack:
            if jobber_stack:
                self.stack = jobber_stack
            else:
                self.stack = jobber_stack = self._setup_stack()
        log.debug("Using Jobber Stack at = {0}\n".format(self.stack.ha))

    def crypted_transfer_decode_dictentry(self, load, dictkey=None, tries=3, timeout=60):
        '''
        We don't need to do the crypted_transfer_decode_dictentry routine for
        raet, just wrap send.
        '''
        return self.send(load, tries, timeout)

    def send(self, load, tries=3, timeout=60):
        '''
        Send a message load and wait for a relative reply
        One shot wonder
        '''
        self.__prep_stack()
        tried = 1
        start = time.time()
        track = nacling.uuid(18)
        src = (None, self.stack.local.name, track)
        self.route = {'src': src, 'dst': self.dst}
        msg = {'route': self.route, 'load': load}
        self.stack.transmit(msg, self.stack.nameRemotes['manor'].uid)
        while track not in jobber_rxMsgs:
            self.stack.serviceAll()
            while self.stack.rxMsgs:
                msg, sender = self.stack.rxMsgs.popleft()
                jobber_rxMsgs[msg['route']['dst'][2]] = msg
                continue
            if track in jobber_rxMsgs:
                break
            if time.time() - start > timeout:
                if tried >= tries:
                    raise ValueError
                self.stack.transmit(msg, self.stack.nameRemotes['manor'].uid)
                tried += 1
            time.sleep(0.01)
        return jobber_rxMsgs.pop(track).get('return', {})

class ZeroMQChannel(Channel):
    '''
    Encapsulate sending routines to ZeroMQ.

    ZMQ Channels default to 'crypt=aes'
    '''
    # the sreq is the zmq connection, since those are relatively expensive to
    # set up, we are going to reuse them as much as possible.
    sreq_cache = defaultdict(dict)

    @property
    def sreq_key(self):
        '''
        Return a tuple which uniquely defines this channel (for caching)
        '''
        return (self.master_uri,                  # which master you want to talk to
                os.getpid(),                      # per process
                threading.current_thread().name,  # per per-thread
                )

    @property
    def sreq(self):
        key = self.sreq_key

        if key not in ZeroMQChannel.sreq_cache:
            master_type = self.opts.get('master_type', None)
            if master_type == 'failover':
                # remove all cached sreqs to the old master to prevent
                # zeromq from reconnecting to old masters automagically
                for check_key in self.sreq_cache.keys():
                    if self.opts['master_uri'] != check_key[0]:
                        del self.sreq_cache[check_key]
                        log.debug('Removed obsolete sreq-object from '
                                  'sreq_cache for master {0}'.format(check_key[0]))

            ZeroMQChannel.sreq_cache[key] = salt.payload.SREQ(self.master_uri)

        return ZeroMQChannel.sreq_cache[key]

    def __init__(self, opts, **kwargs):
        self.opts = opts
        self.ttype = 'zeromq'

        # crypt defaults to 'aes'
        self.crypt = kwargs.get('crypt', 'aes')

        self.serial = salt.payload.Serial(opts)
        if self.crypt != 'clear':
            if 'auth' in kwargs:
                self.auth = kwargs['auth']
            else:
                self.auth = salt.crypt.SAuth(opts)
        if 'master_uri' in kwargs:
            self.master_uri = kwargs['master_uri']
        else:
            self.master_uri = opts['master_uri']

    def crypted_transfer_decode_dictentry(self, load, dictkey=None, tries=3, timeout=60):
        ret = self.sreq.send('aes', self.auth.crypticle.dumps(load), tries, timeout)
        key = self.auth.get_keys()
        aes = key.private_decrypt(ret['key'], 4)
        pcrypt = salt.crypt.Crypticle(self.opts, aes)
        return pcrypt.loads(ret[dictkey])

    def _crypted_transfer(self, load, tries=3, timeout=60):
        '''
        In case of authentication errors, try to renegotiate authentication
        and retry the method.
        Indeed, we can fail too early in case of a master restart during a
        minion state execution call
        '''
        def _do_transfer():
            data = self.sreq.send(
                self.crypt,
                self.auth.crypticle.dumps(load),
                tries,
                timeout)
            # we may not have always data
            # as for example for saltcall ret submission, this is a blind
            # communication, we do not subscribe to return events, we just
            # upload the results to the master
            if data:
                data = self.auth.crypticle.loads(data)
            return data
        try:
            return _do_transfer()
        except salt.crypt.AuthenticationError:
            self.auth = salt.crypt.SAuth(self.opts)
            return _do_transfer()

    def _uncrypted_transfer(self, load, tries=3, timeout=60):
        return self.sreq.send(self.crypt, load, tries, timeout)

    def send(self, load, tries=3, timeout=60):

        if self.crypt != 'clear':
            return self._crypted_transfer(load, tries, timeout)
        else:
            return self._uncrypted_transfer(load, tries, timeout)
        # Do we ever do non-crypted transfers?
<commit_msg>White-space lint fix<commit_after># -*- coding: utf-8 -*-
'''
Encapsulate the different transports available to Salt.  Currently this is only ZeroMQ.
'''
import time
import os
import threading

# Import Salt Libs
import salt.payload
import salt.auth
import salt.utils
import logging
from collections import defaultdict

log = logging.getLogger(__name__)

try:
    from raet import raeting, nacling
    from raet.lane.stacking import LaneStack
    from raet.lane.yarding import RemoteYard

except ImportError:
    # Don't die on missing transport libs since only one transport is required
    pass

jobber_stack = None  # global that holds raet jobber LaneStack
jobber_rxMsgs = {}  # dict of deques one for each RaetChannel

class Channel(object):
    '''
    Factory class to create communication-channels for different transport
    '''
    @staticmethod
    def factory(opts, **kwargs):
        # Default to ZeroMQ for now
        ttype = 'zeromq'

        # determine the ttype
        if 'transport' in opts:
            ttype = opts['transport']
        elif 'transport' in opts.get('pillar', {}).get('master', {}):
            ttype = opts['pillar']['master']['transport']

        # switch on available ttypes
        if ttype == 'zeromq':
            return ZeroMQChannel(opts, **kwargs)
        elif ttype == 'raet':
            return RAETChannel(opts, **kwargs)
        else:
            raise Exception('Channels are only defined for ZeroMQ and raet')
            # return NewKindOfChannel(opts, **kwargs)

class RAETChannel(Channel):
    '''
    Build the communication framework to communicate over the local process
    uxd socket and send messages forwarded to the master. then wait for the
    relative return message.

    Two use cases:
        mininion to master communication, normal use case
           Minion is communicating via yard through minion Road to master
           The destination route needs the estate name of the associated master
        master call via runner, special use case
           In the special case the master call external process is communicating
           via a yard with the master manor yard
           The destination route estate is None to indicate local estate

        The difference between the two is how the destination route
        is assigned.
    '''
    def __init__(self, opts, usage=None, **kwargs):
        self.opts = opts
        self.ttype = 'raet'
        if usage == 'master_call':
            self.dst = (None, None, 'local_cmd')  # runner.py master_call
        elif usage == 'salt_call':
            self.dst = (None, None, 'remote_cmd')  # salt_call caller
        else:  # everything else
            self.dst = (None, None, 'remote_cmd')  # normal use case minion to master
        self.stack = None

    def _setup_stack(self):
        '''
        Setup and return the LaneStack and Yard used by by channel when global
        not already setup such as in salt-call to communicate to-from the minion

        '''
        mid = self.opts.get('id', 'master')
        uid = nacling.uuid(size=18)
        name = 'channel' + uid
        stack = LaneStack(name=name,
                          lanename=mid,
                          sockdirpath=self.opts['sock_dir'])

        stack.Pk = raeting.packKinds.pack
        stack.addRemote(RemoteYard(stack=stack,
                                   name='manor',
                                   lanename=mid,
                                   dirpath=self.opts['sock_dir']))
        log.debug("Created Channel Jobber Stack {0}\n".format(stack.name))
        return stack

    def __prep_stack(self):
        '''
        Prepare the stack objects
        '''
        global jobber_stack
        if not self.stack:
            if jobber_stack:
                self.stack = jobber_stack
            else:
                self.stack = jobber_stack = self._setup_stack()
        log.debug("Using Jobber Stack at = {0}\n".format(self.stack.ha))

    def crypted_transfer_decode_dictentry(self, load, dictkey=None, tries=3, timeout=60):
        '''
        We don't need to do the crypted_transfer_decode_dictentry routine for
        raet, just wrap send.
        '''
        return self.send(load, tries, timeout)

    def send(self, load, tries=3, timeout=60):
        '''
        Send a message load and wait for a relative reply
        One shot wonder
        '''
        self.__prep_stack()
        tried = 1
        start = time.time()
        track = nacling.uuid(18)
        src = (None, self.stack.local.name, track)
        self.route = {'src': src, 'dst': self.dst}
        msg = {'route': self.route, 'load': load}
        self.stack.transmit(msg, self.stack.nameRemotes['manor'].uid)
        while track not in jobber_rxMsgs:
            self.stack.serviceAll()
            while self.stack.rxMsgs:
                msg, sender = self.stack.rxMsgs.popleft()
                jobber_rxMsgs[msg['route']['dst'][2]] = msg
                continue
            if track in jobber_rxMsgs:
                break
            if time.time() - start > timeout:
                if tried >= tries:
                    raise ValueError
                self.stack.transmit(msg, self.stack.nameRemotes['manor'].uid)
                tried += 1
            time.sleep(0.01)
        return jobber_rxMsgs.pop(track).get('return', {})

class ZeroMQChannel(Channel):
    '''
    Encapsulate sending routines to ZeroMQ.

    ZMQ Channels default to 'crypt=aes'
    '''
    # the sreq is the zmq connection, since those are relatively expensive to
    # set up, we are going to reuse them as much as possible.
    sreq_cache = defaultdict(dict)

    @property
    def sreq_key(self):
        '''
        Return a tuple which uniquely defines this channel (for caching)
        '''
        return (self.master_uri,                  # which master you want to talk to
                os.getpid(),                      # per process
                threading.current_thread().name,  # per per-thread
                )

    @property
    def sreq(self):
        key = self.sreq_key

        if key not in ZeroMQChannel.sreq_cache:
            master_type = self.opts.get('master_type', None)
            if master_type == 'failover':
                # remove all cached sreqs to the old master to prevent
                # zeromq from reconnecting to old masters automagically
                for check_key in self.sreq_cache.keys():
                    if self.opts['master_uri'] != check_key[0]:
                        del self.sreq_cache[check_key]
                        log.debug('Removed obsolete sreq-object from '
                                  'sreq_cache for master {0}'.format(check_key[0]))

            ZeroMQChannel.sreq_cache[key] = salt.payload.SREQ(self.master_uri)

        return ZeroMQChannel.sreq_cache[key]

    def __init__(self, opts, **kwargs):
        self.opts = opts
        self.ttype = 'zeromq'

        # crypt defaults to 'aes'
        self.crypt = kwargs.get('crypt', 'aes')

        self.serial = salt.payload.Serial(opts)
        if self.crypt != 'clear':
            if 'auth' in kwargs:
                self.auth = kwargs['auth']
            else:
                self.auth = salt.crypt.SAuth(opts)
        if 'master_uri' in kwargs:
            self.master_uri = kwargs['master_uri']
        else:
            self.master_uri = opts['master_uri']

    def crypted_transfer_decode_dictentry(self, load, dictkey=None, tries=3, timeout=60):
        ret = self.sreq.send('aes', self.auth.crypticle.dumps(load), tries, timeout)
        key = self.auth.get_keys()
        aes = key.private_decrypt(ret['key'], 4)
        pcrypt = salt.crypt.Crypticle(self.opts, aes)
        return pcrypt.loads(ret[dictkey])

    def _crypted_transfer(self, load, tries=3, timeout=60):
        '''
        In case of authentication errors, try to renegotiate authentication
        and retry the method.
        Indeed, we can fail too early in case of a master restart during a
        minion state execution call
        '''
        def _do_transfer():
            data = self.sreq.send(
                self.crypt,
                self.auth.crypticle.dumps(load),
                tries,
                timeout)
            # we may not have always data
            # as for example for saltcall ret submission, this is a blind
            # communication, we do not subscribe to return events, we just
            # upload the results to the master
            if data:
                data = self.auth.crypticle.loads(data)
            return data
        try:
            return _do_transfer()
        except salt.crypt.AuthenticationError:
            self.auth = salt.crypt.SAuth(self.opts)
            return _do_transfer()

    def _uncrypted_transfer(self, load, tries=3, timeout=60):
        return self.sreq.send(self.crypt, load, tries, timeout)

    def send(self, load, tries=3, timeout=60):

        if self.crypt != 'clear':
            return self._crypted_transfer(load, tries, timeout)
        else:
            return self._uncrypted_transfer(load, tries, timeout)
        # Do we ever do non-crypted transfers?
<|endoftext|>


<commit_before># (C) 2019, Stefan Edlich, Apache 2 License
# Main entry point for the chess program and UCI command processing

import sys
import time
import chess # https://github.com/niklasf/python-chess
from control import controller

line = None # save the move line for go
board = None

def quit(clist):
  sys.exit()

def uci(clist):
  print("id name FINNY 0.1")
  print("id author Stefan Edlich")
  print("uciok") ## do not forget!!

def isready(clist): #sync engine + gui
  print("readyok")

def ucinewgame(clist): # init engine. No answer required
  global board
  board = chess.Board()
  print(">>> finny initialized")

def position(clist): # position startpos moves e2e4 e7e5 | position startpos | position fen 'fen'
  global line
  if clist[0] == 'startpos':
    clist.pop(0) # remove startpos or fen
    # if not clist:
    if len(clist) != 0: # position startpos
      clist.pop(0) # remove moves
    line = clist
  else: # must be fen then
    print(">>> fen not yet implemented!")

def go(clist): # pass [timestamp, line, goargs=clist]
  mytup = board, time.time(), line, clist
  controller(mytup)

commands = { # implemented
		"quit" : quit,
		"uci" : uci,
		"isready" : isready,
		"ucinewgame" : ucinewgame,
		"position" : position,
		"go" : go
} # globals()[cmd](clist) => is undebuggable. Hence a dict

uinput = "empty"

while uinput != "quit": # main UCI loop
  uinput = input()
  clist = uinput.split(" ")
  cmd = clist.pop(0)
  result = commands.get(cmd, "empty")
  if result == "empty":
    print(">>> invalid command")
  else:
    result(clist)

<commit_msg>need good version numbers<commit_after># (C) 2019, Stefan Edlich, Apache 2 License
# Main entry point for the chess program and UCI command processing

import sys
import time
import chess # https://github.com/niklasf/python-chess
from control import controller

line = None # save the move line for go
board = None

def quit(clist):
  sys.exit()

def uci(clist):
  print("id name FINNY 0.1") # Command 1/3 real version number!
  print("id author Stefan Edlich") # Command 2/3
  print("uciok") ## do not forget!! # Command 3/3

def isready(clist): #sync engine + gui
  print("readyok")

def ucinewgame(clist): # init engine. No answer required
  global board
  board = chess.Board()
  print(">>> finny initialized")

def position(clist): # position startpos moves e2e4 e7e5 | position startpos | position fen 'fen'
  global line
  if clist[0] == 'startpos':
    clist.pop(0) # remove startpos or fen
    # if not clist:
    if len(clist) != 0: # position startpos
      clist.pop(0) # remove moves
    line = clist
  else: # must be fen then
    print(">>> fen not yet implemented!")

def go(clist): # pass [timestamp, line, goargs=clist]
  mytup = board, time.time(), line, clist
  controller(mytup)

commands = { # implemented
		"quit" : quit,
		"uci" : uci,
		"isready" : isready,
		"ucinewgame" : ucinewgame,
		"position" : position,
		"go" : go
} # globals()[cmd](clist) => is undebuggable. Hence a dict

uinput = "empty"

while uinput != "quit": # main UCI loop
  uinput = input()
  clist = uinput.split(" ")
  cmd = clist.pop(0)
  result = commands.get(cmd, "empty")
  if result == "empty":
    print(">>> invalid command")
  else:
    result(clist)

<|endoftext|>


<commit_before>from random import randint
from collections import OrderedDict

COMP_SIGN = 'O'
INFINITY = 100

class Board:
    def __init__(self, is_computer, fields=[]):
        self.size = 3
        self.empty = '_'
        self.fields = fields if fields else OrderedDict(
            {(i, j): self.empty for j in range(self.size) for i in range(self.size)})
        self.sign = COMP_SIGN if is_computer else self.get_enemy(COMP_SIGN)
        self.children = []

    def __str__(self):
        board = []
        for i in range(self.size):
            board.append('|'.join([self.fields[i, j] for j in range(self.size)]))
        return '\n'.join(board)

    def __repr__(self):
        return str(self.fields)

    def get_enemy(self, sign):
        return 'O' if sign == 'x' else 'X'

    def get_empty_fields(self):
        return [pos for pos, val in self.fields.items() if val == self.empty]

    def get_sign(self, is_computer):
        return 'O' if is_computer else 'X'

    def generate_children(self, is_computer):
        self.children = []
        for pos in self.get_empty_fields():
            new_fields = self.fields.copy()
            new_fields[pos] = self.get_sign(is_computer)
            self.children.append(Board(is_computer, fields=new_fields))

    def play(self, is_max):
        while True:
            if is_max:
                self.computer_play()
                is_max = False
            self.user_play()
            is_max = True

    def user_play(self):
        position = input('Enter position with space: ')
        x, y = map(int, position.split(' '))
        if self.fields[x, y] == self.empty:
            self.fields[x, y] = 'X'

    def computer_play(self):
        print(self.get_empty_fields())
        self.generate_children(True)
        print(self.children)

    def minimax(self, board, player, alpha, beta):
        # is game terminated
        if player == COMP_SIGN:
            for child in self.children:
                pass

def main():
    rand_num = randint(0, 1000)
    print('Fill in board with 0-based numbers!!!')
    if rand_num % 2 == 0:
        # computer
        print('The first player is computer. He plays with O. You play with X.')
        is_computer = True
    else:
        # player
        print('Yey, the first player is you! The computer plays with O. You play with X.')
        is_computer = False

    board = Board(is_computer)

    board.play(is_computer)
    print(board)

def test_main():
    is_computer = True
    board = Board(is_computer)
    board.player, board.opponent = 'X', 'O'
    # X is computer
    # board.fields.update({
    #     (0, 0): 'X',
    #     (0, 1): 'O',
    #     (2, 0): 'O',
    #     # (2, 2): 'X'
    # })
    # board.player, board.opponent = board.opponent, board.player
    board.fields.update({
        (0, 0): 'O',
        (1, 0): 'O',
        (1, 1): 'X',
        (2, 2): 'X'
    })
    # O | _ | _
    # O | X | _
    # _ | _ | X
    print(board)

if __name__ == '__main__':
    main()
<commit_msg>Remove old code<commit_after>from random import randint
from collections import OrderedDict

COMP_SIGN = 'O'
INFINITY = 100

class Board:
    def __init__(self, is_computer, fields=[]):
        self.size = 3
        self.empty = '_'
        self.fields = fields if fields else OrderedDict(
            {(i, j): self.empty for j in range(self.size) for i in range(self.size)})
        self.sign = COMP_SIGN if is_computer else self.get_enemy(COMP_SIGN)
        self.children = []

    def __str__(self):
        board = []
        for i in range(self.size):
            board.append('|'.join([self.fields[i, j] for j in range(self.size)]))
        return '\n'.join(board)

    def __repr__(self):
        return str(self.fields)

    def get_enemy(self, sign):
        return 'O' if sign == 'x' else 'X'

    def get_empty_fields(self):
        return [pos for pos, val in self.fields.items() if val == self.empty]

    def get_sign(self, is_computer):
        return 'O' if is_computer else 'X'

    def generate_children(self, is_computer):
        self.children = []
        for pos in self.get_empty_fields():
            new_fields = self.fields.copy()
            new_fields[pos] = self.get_sign(is_computer)
            self.children.append(Board(is_computer, fields=new_fields))

    def play(self, is_max):
        while True:
            if is_max:
                self.computer_play()
                is_max = False
            self.user_play()
            is_max = True

    def user_play(self):
        position = input('Enter position with space: ')
        x, y = map(int, position.split(' '))
        if self.fields[x, y] == self.empty:
            self.fields[x, y] = 'X'

    def computer_play(self):
        print(self.get_empty_fields())
        self.generate_children(True)
        print(self.children)

    def minimax(self, board, player, alpha, beta):
        # is game terminated
        if player == COMP_SIGN:
            for child in self.children:
                pass

def main():
    rand_num = randint(0, 1000)
    print('Fill in board with 0-based numbers!!!')
    if rand_num % 2 == 0:
        # computer
        print('The first player is computer. He plays with O. You play with X.')
        is_computer = True
    else:
        # player
        print('Yey, the first player is you! The computer plays with O. You play with X.')
        is_computer = False

    board = Board(is_computer)

    board.play(is_computer)
    print(board)

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>import hoomd
import pytest
import numpy as np
import itertools
import hoomd.hpmc.pytest.conftest

np.random.seed(0)

def test_attach(simulation_factory, two_particle_snapshot_factory, pair_and_params):
    pair = pair_and_params[0]
    params = pair_and_params[1]

    sim = simulation_factory(two_particle_snapshot_factory(dimensions=3, d=.5))
    integrator = hoomd.md.Integrator(dt=0.5)
    integrator.methods.append(hoomd.md.methods.Langevin(hoomd.filter.All(), kT=1, seed=1))
    pair.params[('A', 'A')] = params
    pair.r_cut[('A', 'A')] = .02
    integrator.forces.append(pair)
    sim.operations.integrator = integrator
    sim.operations.schedule()
    sim.run(10)

def assert_equivalent_type_params(type_param1, type_param2):
    for pair in type_param1:
        if isinstance(type_param1[pair], dict):
            for key in type_param1[pair]:
                np.testing.assert_allclose(type_param1[pair][key],
                                           type_param2[pair][key])
        else:
            assert type_param1[pair] == type_param2[pair]

def assert_equivalent_parameter_dicts(param_dict1, param_dict2):
    for key in param_dict1:
        assert param_dict1[key] == param_dict2[key]

def test_valid_params(valid_params):
    pair_potential, pair_potential_dict, r_cut, r_on, mode = valid_params
    cell = hoomd.md.nlist.Cell()
    if mode is not None:
        pot = pair_potential(nlist=cell, mode=mode)
    else:
        pot = pair_potential(nlist=cell)
    for pair in pair_potential_dict:
        pot.params[pair] = pair_potential_dict[pair]
        pot.r_cut[pair] = r_cut[pair]
        if r_on is not None:
            pot.r_on[pair] = r_on[pair]

    assert_equivalent_type_params(pot.params.to_dict(), pair_potential_dict)
    assert_equivalent_type_params(pot.r_cut.to_dict(), r_cut)
    assert_equivalent_type_params(pot.r_on.to_dict(), r_on)
    assert_equivalent_parameter_dicts(pot.nlist._param_dict, cell._param_dict)

def test_invalid_params(invalid_params):
    pair_potential, pair_potential_dict, r_cut, r_on, mode = invalid_params
    cell = hoomd.md.nlist.Cell()
    if isinstance(mode, str):
        pot = pair_potential(nlist=cell, mode=mode)
        for pair in pair_potential_dict:
            if isinstance(pair, tuple):
                with pytest.raises(hoomd.typeconverter.TypeConversionError):
                    pot.params[pair] = pair_potential_dict[pair]
                    pot.r_cut[pair] = r_cut[pair]
                    pot.r_on[pair] = r_on[pair]
            else:
                with pytest.raises(KeyError):
                    pot.params[pair] = pair_potential_dict[pair]
                    pot.r_cut[pair] = r_cut[pair]
                    pot.r_on[pair] = r_on[pair]
    else:
        with pytest.raises(hoomd.typeconverter.TypeConversionError):
            pot = pair_potential(nlist=cell, mode=mode)

def test_attached_params(simulation_factory, lattice_snapshot_factory,
                         valid_params):
    pair_potential, pair_potential_dict, r_cut, r_on, mode = valid_params
    particle_types = list(set(itertools.chain.from_iterable(r_cut.keys())))
    cell = hoomd.md.nlist.Cell()
    if mode is not None:
        pot = pair_potential(nlist=cell, r_cut=2.5, mode=mode)
    else:
        pot = pair_potential(nlist=cell, r_cut=2.5)

    for pair in pair_potential_dict:
        pot.params[pair] = pair_potential_dict[pair]
        pot.r_cut[pair] = r_cut[pair]
        if r_on is not None:
            pot.r_on[pair] = r_on[pair]

    snap = lattice_snapshot_factory(particle_types=particle_types,
                                    n=10, a=0.5, r=0.01)
    if snap.exists:
        snap.particles.typeid[:] = np.random.randint(0,
                                                     len(snap.particles.types),
                                                     snap.particles.N)
    sim = simulation_factory(snap)
    sim.operations.integrator = hoomd.md.Integrator(dt=0.005)
    sim.operations.integrator.forces.append(pot)
    sim.operations.schedule()
    attached_pot = sim.operations.integrator.forces[0]
    assert_equivalent_type_params(attached_pot.params.to_dict(),
                                  pair_potential_dict)
    assert_equivalent_type_params(attached_pot.r_cut.to_dict(), r_cut)
    if r_on is None:
        assert attached_pot.r_on.to_dict() == {key: 0.0 for key in r_cut.keys()}
    else:
        assert_equivalent_type_params(attached_pot.r_on.to_dict(), r_on)
    assert_equivalent_parameter_dicts(attached_pot.nlist._param_dict,
                                      cell._param_dict)

@pytest.mark.parametrize("nsteps", [1, 5, 10])
def test_run(simulation_factory, lattice_snapshot_factory,
             valid_params, nsteps):
    pair_potential, pair_potential_dict, r_cut, r_on, mode = valid_params
    particle_types = list(set(itertools.chain.from_iterable(r_cut.keys())))
    cell = hoomd.md.nlist.Cell()
    if mode is not None:
        pot = pair_potential(nlist=cell, r_cut=2.5, mode=mode)
    else:
        pot = pair_potential(nlist=cell, r_cut=2.5)

    for pair in pair_potential_dict:
        pot.params[pair] = pair_potential_dict[pair]
        pot.r_cut[pair] = r_cut[pair]
        if r_on is not None:
            pot.r_on[pair] = r_on[pair]

    snap = lattice_snapshot_factory(particle_types=particle_types,
                                    n=10, a=1, r=0.01)
    if snap.exists:
        snap.particles.typeid[:] = np.random.randint(0,
                                                     len(snap.particles.types),
                                                     snap.particles.N)
    sim = simulation_factory(snap)
    integrator = hoomd.md.Integrator(dt=0.005)
    integrator.forces.append(pot)
    integrator.methods.append(hoomd.md.methods.Langevin(hoomd.filter.All(),
                                                        kT=1, seed=1))
    sim.operations.integrator = integrator
    sim.operations.schedule()
    initial_pos = sim.state.snapshot.particles.position
    sim.run(nsteps)
    with pytest.raises(AssertionError):
        np.testing.assert_allclose(sim.state.snapshot.particles.position,
                                   initial_pos)

@pytest.mark.parametrize("nsteps", [1, 5, 10])
def test_compute_energy(simulation_factory, two_particle_snapshot_factory,
                        valid_params, nsteps):
    pair_potential, pair_potential_dict, r_cut, r_on, mode = valid_params
    particle_types = list(set(itertools.chain.from_iterable(r_cut.keys())))
    cell = hoomd.md.nlist.Cell()
    if mode is not None:
        pot = pair_potential(nlist=cell, r_cut=2.5, mode=mode)
    else:
        pot = pair_potential(nlist=cell, r_cut=2.5)
    for pair in pair_potential_dict:
        pot.params[pair] = pair_potential_dict[pair]
        pot.r_cut[pair] = r_cut[pair]
        if r_on is not None:
            pot.r_on[pair] = r_on[pair]
    snap = two_particle_snapshot_factory(particle_types=particle_types)
    sim = simulation_factory(snap)
    integrator = hoomd.md.Integrator(dt=0.005)
    integrator.forces.append(pot)
    integrator.methods.append(hoomd.md.methods.Langevin(hoomd.filter.All(),
                                                        kT=1, seed=1))
    sim.operations.integrator = integrator
    sim.operations.schedule()
    for pair in pair_potential_dict:
        snap = sim.state.snapshot
        if snap.exists:
            snap.particles.typeid[0] = particle_types.index(pair[0])
            snap.particles.typeid[1] = particle_types.index(pair[1])

        sim.state.snapshot = snap
        sim.run(nsteps)
        sim_energies = sim.operations.integrator.forces[0].energies
        pair_energy = pot.compute_energy(np.array([0], dtype=np.int32),
                                         np.array([1], dtype=np.int32))
        assert pair_energy / 2 == sim_energies[0]
        assert pair_energy / 2 == sim_energies[1]
<commit_msg>Lower the number of timesteps in test_run<commit_after>import hoomd
import pytest
import numpy as np
import itertools
import hoomd.hpmc.pytest.conftest

np.random.seed(0)

def test_attach(simulation_factory, two_particle_snapshot_factory, pair_and_params):
    pair = pair_and_params[0]
    params = pair_and_params[1]

    sim = simulation_factory(two_particle_snapshot_factory(dimensions=3, d=.5))
    integrator = hoomd.md.Integrator(dt=0.5)
    integrator.methods.append(hoomd.md.methods.Langevin(hoomd.filter.All(), kT=1, seed=1))
    pair.params[('A', 'A')] = params
    pair.r_cut[('A', 'A')] = .02
    integrator.forces.append(pair)
    sim.operations.integrator = integrator
    sim.operations.schedule()
    sim.run(10)

def assert_equivalent_type_params(type_param1, type_param2):
    for pair in type_param1:
        if isinstance(type_param1[pair], dict):
            for key in type_param1[pair]:
                np.testing.assert_allclose(type_param1[pair][key],
                                           type_param2[pair][key])
        else:
            assert type_param1[pair] == type_param2[pair]

def assert_equivalent_parameter_dicts(param_dict1, param_dict2):
    for key in param_dict1:
        assert param_dict1[key] == param_dict2[key]

def test_valid_params(valid_params):
    pair_potential, pair_potential_dict, r_cut, r_on, mode = valid_params
    cell = hoomd.md.nlist.Cell()
    if mode is not None:
        pot = pair_potential(nlist=cell, mode=mode)
    else:
        pot = pair_potential(nlist=cell)
    for pair in pair_potential_dict:
        pot.params[pair] = pair_potential_dict[pair]
        pot.r_cut[pair] = r_cut[pair]
        if r_on is not None:
            pot.r_on[pair] = r_on[pair]

    assert_equivalent_type_params(pot.params.to_dict(), pair_potential_dict)
    assert_equivalent_type_params(pot.r_cut.to_dict(), r_cut)
    assert_equivalent_type_params(pot.r_on.to_dict(), r_on)
    assert_equivalent_parameter_dicts(pot.nlist._param_dict, cell._param_dict)

def test_invalid_params(invalid_params):
    pair_potential, pair_potential_dict, r_cut, r_on, mode = invalid_params
    cell = hoomd.md.nlist.Cell()
    if isinstance(mode, str):
        pot = pair_potential(nlist=cell, mode=mode)
        for pair in pair_potential_dict:
            if isinstance(pair, tuple):
                with pytest.raises(hoomd.typeconverter.TypeConversionError):
                    pot.params[pair] = pair_potential_dict[pair]
                    pot.r_cut[pair] = r_cut[pair]
                    pot.r_on[pair] = r_on[pair]
            else:
                with pytest.raises(KeyError):
                    pot.params[pair] = pair_potential_dict[pair]
                    pot.r_cut[pair] = r_cut[pair]
                    pot.r_on[pair] = r_on[pair]
    else:
        with pytest.raises(hoomd.typeconverter.TypeConversionError):
            pot = pair_potential(nlist=cell, mode=mode)

def test_attached_params(simulation_factory, lattice_snapshot_factory,
                         valid_params):
    pair_potential, pair_potential_dict, r_cut, r_on, mode = valid_params
    particle_types = list(set(itertools.chain.from_iterable(r_cut.keys())))
    cell = hoomd.md.nlist.Cell()
    if mode is not None:
        pot = pair_potential(nlist=cell, r_cut=2.5, mode=mode)
    else:
        pot = pair_potential(nlist=cell, r_cut=2.5)

    for pair in pair_potential_dict:
        pot.params[pair] = pair_potential_dict[pair]
        pot.r_cut[pair] = r_cut[pair]
        if r_on is not None:
            pot.r_on[pair] = r_on[pair]

    snap = lattice_snapshot_factory(particle_types=particle_types,
                                    n=10, a=0.5, r=0.01)
    if snap.exists:
        snap.particles.typeid[:] = np.random.randint(0,
                                                     len(snap.particles.types),
                                                     snap.particles.N)
    sim = simulation_factory(snap)
    sim.operations.integrator = hoomd.md.Integrator(dt=0.005)
    sim.operations.integrator.forces.append(pot)
    sim.operations.schedule()
    attached_pot = sim.operations.integrator.forces[0]
    assert_equivalent_type_params(attached_pot.params.to_dict(),
                                  pair_potential_dict)
    assert_equivalent_type_params(attached_pot.r_cut.to_dict(), r_cut)
    if r_on is None:
        assert attached_pot.r_on.to_dict() == {key: 0.0 for key in r_cut.keys()}
    else:
        assert_equivalent_type_params(attached_pot.r_on.to_dict(), r_on)
    assert_equivalent_parameter_dicts(attached_pot.nlist._param_dict,
                                      cell._param_dict)

@pytest.mark.parametrize("nsteps", [1, 3, 5])
def test_run(simulation_factory, lattice_snapshot_factory,
             valid_params, nsteps):
    pair_potential, pair_potential_dict, r_cut, r_on, mode = valid_params
    particle_types = list(set(itertools.chain.from_iterable(r_cut.keys())))
    cell = hoomd.md.nlist.Cell()
    if mode is not None:
        pot = pair_potential(nlist=cell, r_cut=2.5, mode=mode)
    else:
        pot = pair_potential(nlist=cell, r_cut=2.5)

    for pair in pair_potential_dict:
        pot.params[pair] = pair_potential_dict[pair]
        pot.r_cut[pair] = r_cut[pair]
        if r_on is not None:
            pot.r_on[pair] = r_on[pair]

    snap = lattice_snapshot_factory(particle_types=particle_types,
                                    n=10, a=1, r=0.01)
    if snap.exists:
        snap.particles.typeid[:] = np.random.randint(0,
                                                     len(snap.particles.types),
                                                     snap.particles.N)
    sim = simulation_factory(snap)
    integrator = hoomd.md.Integrator(dt=0.005)
    integrator.forces.append(pot)
    integrator.methods.append(hoomd.md.methods.Langevin(hoomd.filter.All(),
                                                        kT=1, seed=1))
    sim.operations.integrator = integrator
    sim.operations.schedule()
    initial_pos = sim.state.snapshot.particles.position
    sim.run(nsteps)
    with pytest.raises(AssertionError):
        np.testing.assert_allclose(sim.state.snapshot.particles.position,
                                   initial_pos)

@pytest.mark.parametrize("nsteps", [1, 5, 10])
def test_compute_energy(simulation_factory, two_particle_snapshot_factory,
                        valid_params, nsteps):
    pair_potential, pair_potential_dict, r_cut, r_on, mode = valid_params
    particle_types = list(set(itertools.chain.from_iterable(r_cut.keys())))
    cell = hoomd.md.nlist.Cell()
    if mode is not None:
        pot = pair_potential(nlist=cell, r_cut=2.5, mode=mode)
    else:
        pot = pair_potential(nlist=cell, r_cut=2.5)
    for pair in pair_potential_dict:
        pot.params[pair] = pair_potential_dict[pair]
        pot.r_cut[pair] = r_cut[pair]
        if r_on is not None:
            pot.r_on[pair] = r_on[pair]
    snap = two_particle_snapshot_factory(particle_types=particle_types)
    sim = simulation_factory(snap)
    integrator = hoomd.md.Integrator(dt=0.005)
    integrator.forces.append(pot)
    integrator.methods.append(hoomd.md.methods.Langevin(hoomd.filter.All(),
                                                        kT=1, seed=1))
    sim.operations.integrator = integrator
    sim.operations.schedule()
    for pair in pair_potential_dict:
        snap = sim.state.snapshot
        if snap.exists:
            snap.particles.typeid[0] = particle_types.index(pair[0])
            snap.particles.typeid[1] = particle_types.index(pair[1])

        sim.state.snapshot = snap
        sim.run(nsteps)
        sim_energies = sim.operations.integrator.forces[0].energies
        pair_energy = pot.compute_energy(np.array([0], dtype=np.int32),
                                         np.array([1], dtype=np.int32))
        assert pair_energy / 2 == sim_energies[0]
        assert pair_energy / 2 == sim_energies[1]
<|endoftext|>


<commit_before>#!/usr/bin/env python

import sys
import psycopg2
from psycopg2.extensions import adapt
import requests
import random
import lxml.etree
import json
import os
import uuid
import sys
import re
import config

IATI_DIR = config.DATA_STORAGE_DIR

class NoIATIActivityFound(Exception):
    pass

def save_url(url, filename):
    resp = requests.get(url)
    with file(filename, 'w') as f:
        f.write(resp.content)

def query(*args):
    import config
    db_config = config.db_config
    db = psycopg2.connect(**db_config)
    c = db.cursor()
    c.execute(*args)
    return c.fetchall()

def organisation_ids():
    return query('select id from organisation;')

class WorkItems(object):
    def __init__(self, org_ids, test_ids):
        self.org_ids = org_ids
        self.test_ids = test_ids

    def test_string_of_test_id(self, test_id):
        results = query('''select name from test where id = %s;''', (test_id,));
        assert len(results) == 1
        return results[0][0]

    def kind_of_test(self, test_id):
        test_string = self.test_string_of_test_id(test_id)
        return self.kind_of_test_string(test_string)

    def kind_of_test_string(self, test_string):
        import test_mapping

        return test_mapping.test_to_kind[test_string]

    def __iter__(self):
        for org_id in self.org_ids:
            for test_id in self.test_ids:
                sot = SampleOrgTest(org_id, test_id)
                if not sot.qualifies():
                    continue
                act_ids = sot.activity_ids()
                sample_ids = sot.sample_activity_ids(10)

                test_kind = self.kind_of_test(test_id)

                for act_id in sample_ids:
                    try:
                        act = sot.xml_of_activity(act_id)
                        parent_act = sot.xml_of_parent_activity(act_id)
                    except NoIATIActivityFound:
                        continue

                    u = str(uuid.uuid4())

                    args = {
                        "uuid": u,
                        "organisation_id": org_id,
                        "test_id": test_id,
                        "activity_id": act_id[0],
                        "package_id": act_id[1],
                        "xml_data": act,
                        "xml_parent_data": parent_act,
                        "test_kind": test_kind
                        }

                    yield args

class SampleOrgTest(object):
    def __init__(self, organisation_id, test_id):
        self.org_id = organisation_id
        self.test_id = test_id

        if self.qualifies():
            self.activities = self.activity_ids()
        else:
            self.activities = []

    def qualifies(self):
        rows = query('''select result_data from result
                          where organisation_id = %s
                            and test_id = %s
                            and result_data != 0''',
                     [self.org_id, self.test_id])
        return len(rows) >= 1

    def activity_ids(self):
        rows = query('''select result_identifier, package_name from result
                          left join package on result.package_id = package.id
                          where organisation_id = %s
                            and test_id = %s
                            and result_data = 1''',
                     [self.org_id, self.test_id])
        ids = [ i for i in rows ]
        return ids

    def sample_activity_ids(self, max):
        act_ids = [i for i in self.activities]
        random.shuffle(act_ids)
        return act_ids[:max]

    def xml_of_package(self, package_name):
        filename = package_name + '.xml'
        path = os.path.join(IATI_DIR, filename)
        return lxml.etree.parse(path)

    def xml_of_activity(self, activity):
        activity_id, pkg = activity
        xml = self.xml_of_package(pkg)

        xpath_str = '//iati-activity[iati-identifier/text()="%s"]'

        activities = xml.xpath(xpath_str % activity_id)
        if 0 == len(activities):
            raise NoIATIActivityFound
        assert len(activities) == 1
        return lxml.etree.tostring(activities[0], pretty_print=True)

    def xml_of_parent_activity(self, activity):
        activity_id, pkg = activity
        xml = self.xml_of_package(pkg)

        xpath_str = '//iati-activity[iati-identifier/text()="%s"]'
        activities = xml.xpath(xpath_str % activity_id)
        assert len(activities) == 1
        activity_xml = activities[0]

        xpath_str = '''related-activity[@type='1']/@ref'''
        related_activity_ids = activity_xml.xpath(xpath_str)

        count_relateds = len(related_activity_ids)
        if 0 == count_relateds:
            return None

        assert 1 == count_relateds

        xpath_str = '''//iati-activity[iati-identifier/text()="%s"]'''

        parent_id = related_activity_ids[0]

        return lxml.etree.tostring(xml.xpath(xpath_str % parent_id)[0])

class DocumentLink(object):
    def __init__(self, url, title, elt, codelists):
        self.elt = elt
        self.title = title
        self.url = url
        self.codelists = codelists

    def __repr__(self):
        return '''<DocumentLink: %s>''' % self.url

    def to_dict(self):
        def getCategory(category, codelists):
            return {"category": codelists[category],
                    "category_code": category }

        def getCategories(categories, codelists):
            return [ getCategory(category, codelists) for category in categories ]

        data = {
            "name": self.title,
            "url": self.url,
            "categories": getCategories(self.elt.xpath('category/@code'),
                                       self.codelists)
            }
        return data

class DocumentLinks(object):
    def __init__(self, xml_string, codelists):
        root = lxml.etree.fromstring(xml_string)
        self.root = root
        self.codelists = codelists

    def get_links(self):
        for i in self.root.iterfind('document-link'):
            url = i.attrib["url"]
            title = i.xpath('title/text()')
            codelists = self.codelists
            yield DocumentLink(url, title, i, codelists)

class Location(object):
    def __init__(self, elt):
        self.elt = elt

    def __repr__(self):
        return '''<Location: %s>''' % self.elt

    def to_dict(self):
        data = {
            "name": self.elt.xpath('name/text()'),
            "description": self.elt.xpath('description/text()'),
            "longitude": self.elt.xpath('coordinates/@longitude'),
            "latitude": self.elt.xpath('coordinates/@latitude'),
            }
        return data

class Locations(object):
    def __init__(self,xml_string):
        root = lxml.etree.fromstring(xml_string)
        self.root = root

    def get_locations(self):
        for i in self.root.iterfind('location'):
            yield Location(i)

class Period(object):
    def __init__(self, elt):
        self.elt = elt

    def to_dict(self):
        def format_pct(value):
            return '

'

        def calc_pct(target, actual):
            if (target and actual):
                try:
                    target = re.sub(",", "", target[0])
                    target = re.sub("%", "", target)
                    actual = re.sub(",", "", actual[0])
                    actual = re.sub("%", "", actual)
                    return format_pct((float(actual) / float(target)) * 100.00)
                except Exception, e:
                    return ""

        data = {
            'start_date': self.elt.xpath('period-start/@iso-date'),
            'end_date': self.elt.xpath('period-end/@iso-date'),
            'target': self.elt.xpath('target/@value'),
            'actual': self.elt.xpath('actual/@value'),
            'pct': calc_pct(self.elt.xpath('target/@value'),
                            self.elt.xpath('actual/@value'))
        }
        return data

class Indicator(object):
    def __init__(self,elt):
        self.elt = elt

    def elt_text_or_BLANK(self, key):
        elt = self.elt.find(key)
        return getattr(elt, "text", "")

    def to_dict(self):
        def get_period(period):
            return Period(period).to_dict()

        def get_periods(periods):
            return [ get_period(period)
                        for period in periods ]
        data = {
            "title": self.elt_text_or_BLANK("title"),
            "description": self.elt_text_or_BLANK("description"),
            "periods": get_periods(self.elt.xpath('period')),
        }
        return data

class Result(object):
    def __init__(self, elt):
        self.elt = elt

    def __repr__(self):
        return '''<Result: %s>''' % self.elt

    def elt_text_or_BLANK(self, key):
        elt = self.elt.find(key)
        return getattr(elt, "text", "")

    def to_dict(self):
        def get_indicator(indicator):
            return Indicator(indicator).to_dict()

        def get_indicators(indicators):
            return [ get_indicator(indicator)
                        for indicator in indicators ]

        data = {
            "title": self.elt_text_or_BLANK("title"),
            "description": self.elt_text_or_BLANK("description"),
            "indicators": get_indicators(self.elt.xpath('indicator')),
            }
        return data

class Results(object):
    def __init__(self, xml_string):
        root = lxml.etree.fromstring(xml_string)
        self.root = root

    def get_results(self):
        for i in self.root.iterfind('result'):
            yield Result(i)

class Condition(object):
    def __init__(self, elt):
        self.elt = elt

    def __repr__(self):
        return '''<Condition: %s>''' % self.elt

    def to_dict(self):
        data = {
            "type": self.elt.xpath("@type"),
            "text": self.elt.text,
            }
        return data

class Conditions(object):
    def __init__(self, xml_string):
        root = lxml.etree.fromstring(xml_string)
        self.root = root

    def get_conditions(self):
        return { 'attached': self.root.xpath("conditions/@attached"),
                 'conditions': [ Condition(condition).to_dict()
                        for condition in self.root.xpath('conditions/condition') ] }

class ActivityInfo(object):
    def __init__(self, xml_string):
        self.root = lxml.etree.fromstring(xml_string)
        self.title = self.elt_text_or_MISSING("title")
        self.description = self.elt_text_or_MISSING("description")

    def elt_text_or_MISSING(self, key):
        elt = self.root.find(key)
        return getattr(elt, "text", "MISSING")

class NoMatchingTest(Exception):
    pass

class TestInfo(object):
    def __init__(self, test_foxpath):
        self.test_string = test_foxpath
        self.test_id = self.id_of_string()

    def id_of_string(self):
        sql = '''select id from test where name = %s;'''
        q = sql % adapt(self.test_string).getquoted()

        results = query(q)
        if len(results) < 1:
            raise NoMatchingTest(self.test_string)
        return results[0][0]
<commit_msg>Avoid breaking if parent activity can't be found in the same file<commit_after>#!/usr/bin/env python

import sys
import psycopg2
from psycopg2.extensions import adapt
import requests
import random
import lxml.etree
import json
import os
import uuid
import sys
import re
import config

IATI_DIR = config.DATA_STORAGE_DIR

class NoIATIActivityFound(Exception):
    pass

def save_url(url, filename):
    resp = requests.get(url)
    with file(filename, 'w') as f:
        f.write(resp.content)

def query(*args):
    import config
    db_config = config.db_config
    db = psycopg2.connect(**db_config)
    c = db.cursor()
    c.execute(*args)
    return c.fetchall()

def organisation_ids():
    return query('select id from organisation;')

class WorkItems(object):
    def __init__(self, org_ids, test_ids):
        self.org_ids = org_ids
        self.test_ids = test_ids

    def test_string_of_test_id(self, test_id):
        results = query('''select name from test where id = %s;''', (test_id,));
        assert len(results) == 1
        return results[0][0]

    def kind_of_test(self, test_id):
        test_string = self.test_string_of_test_id(test_id)
        return self.kind_of_test_string(test_string)

    def kind_of_test_string(self, test_string):
        import test_mapping

        return test_mapping.test_to_kind[test_string]

    def __iter__(self):
        for org_id in self.org_ids:
            for test_id in self.test_ids:
                sot = SampleOrgTest(org_id, test_id)
                if not sot.qualifies():
                    continue
                act_ids = sot.activity_ids()
                sample_ids = sot.sample_activity_ids(10)

                test_kind = self.kind_of_test(test_id)

                for act_id in sample_ids:
                    try:
                        act = sot.xml_of_activity(act_id)
                        parent_act = sot.xml_of_parent_activity(act_id)
                    except NoIATIActivityFound:
                        continue

                    u = str(uuid.uuid4())

                    args = {
                        "uuid": u,
                        "organisation_id": org_id,
                        "test_id": test_id,
                        "activity_id": act_id[0],
                        "package_id": act_id[1],
                        "xml_data": act,
                        "xml_parent_data": parent_act,
                        "test_kind": test_kind
                        }

                    yield args

class SampleOrgTest(object):
    def __init__(self, organisation_id, test_id):
        self.org_id = organisation_id
        self.test_id = test_id

        if self.qualifies():
            self.activities = self.activity_ids()
        else:
            self.activities = []

    def qualifies(self):
        rows = query('''select result_data from result
                          where organisation_id = %s
                            and test_id = %s
                            and result_data != 0''',
                     [self.org_id, self.test_id])
        return len(rows) >= 1

    def activity_ids(self):
        rows = query('''select result_identifier, package_name from result
                          left join package on result.package_id = package.id
                          where organisation_id = %s
                            and test_id = %s
                            and result_data = 1''',
                     [self.org_id, self.test_id])
        ids = [ i for i in rows ]
        return ids

    def sample_activity_ids(self, max):
        act_ids = [i for i in self.activities]
        random.shuffle(act_ids)
        return act_ids[:max]

    def xml_of_package(self, package_name):
        filename = package_name + '.xml'
        path = os.path.join(IATI_DIR, filename)
        return lxml.etree.parse(path)

    def xml_of_activity(self, activity):
        activity_id, pkg = activity
        xml = self.xml_of_package(pkg)

        xpath_str = '//iati-activity[iati-identifier/text()="%s"]'

        activities = xml.xpath(xpath_str % activity_id)
        if 0 == len(activities):
            raise NoIATIActivityFound
        assert len(activities) == 1
        return lxml.etree.tostring(activities[0], pretty_print=True)

    def xml_of_parent_activity(self, activity):
        activity_id, pkg = activity
        xml = self.xml_of_package(pkg)

        xpath_str = '//iati-activity[iati-identifier/text()="%s"]'
        activities = xml.xpath(xpath_str % activity_id)
        assert len(activities) == 1
        activity_xml = activities[0]

        xpath_str = '''related-activity[@type='1']/@ref'''
        related_activity_ids = activity_xml.xpath(xpath_str)

        count_relateds = len(related_activity_ids)
        if 0 == count_relateds:
            return None

        assert 1 == count_relateds

        xpath_str = '''//iati-activity[iati-identifier/text()="%s"]'''

        parent_id = related_activity_ids[0]

        try:
            return lxml.etree.tostring(xml.xpath(xpath_str % parent_id)[0])
        except IndexError:
            return None

class DocumentLink(object):
    def __init__(self, url, title, elt, codelists):
        self.elt = elt
        self.title = title
        self.url = url
        self.codelists = codelists

    def __repr__(self):
        return '''<DocumentLink: %s>''' % self.url

    def to_dict(self):
        def getCategory(category, codelists):
            return {"category": codelists[category],
                    "category_code": category }

        def getCategories(categories, codelists):
            return [ getCategory(category, codelists) for category in categories ]

        data = {
            "name": self.title,
            "url": self.url,
            "categories": getCategories(self.elt.xpath('category/@code'),
                                       self.codelists)
            }
        return data

class DocumentLinks(object):
    def __init__(self, xml_string, codelists):
        root = lxml.etree.fromstring(xml_string)
        self.root = root
        self.codelists = codelists

    def get_links(self):
        for i in self.root.iterfind('document-link'):
            url = i.attrib["url"]
            title = i.xpath('title/text()')
            codelists = self.codelists
            yield DocumentLink(url, title, i, codelists)

class Location(object):
    def __init__(self, elt):
        self.elt = elt

    def __repr__(self):
        return '''<Location: %s>''' % self.elt

    def to_dict(self):
        data = {
            "name": self.elt.xpath('name/text()'),
            "description": self.elt.xpath('description/text()'),
            "longitude": self.elt.xpath('coordinates/@longitude'),
            "latitude": self.elt.xpath('coordinates/@latitude'),
            }
        return data

class Locations(object):
    def __init__(self,xml_string):
        root = lxml.etree.fromstring(xml_string)
        self.root = root

    def get_locations(self):
        for i in self.root.iterfind('location'):
            yield Location(i)

class Period(object):
    def __init__(self, elt):
        self.elt = elt

    def to_dict(self):
        def format_pct(value):
            return '

'

        def calc_pct(target, actual):
            if (target and actual):
                try:
                    target = re.sub(",", "", target[0])
                    target = re.sub("%", "", target)
                    actual = re.sub(",", "", actual[0])
                    actual = re.sub("%", "", actual)
                    return format_pct((float(actual) / float(target)) * 100.00)
                except Exception, e:
                    return ""

        data = {
            'start_date': self.elt.xpath('period-start/@iso-date'),
            'end_date': self.elt.xpath('period-end/@iso-date'),
            'target': self.elt.xpath('target/@value'),
            'actual': self.elt.xpath('actual/@value'),
            'pct': calc_pct(self.elt.xpath('target/@value'),
                            self.elt.xpath('actual/@value'))
        }
        return data

class Indicator(object):
    def __init__(self,elt):
        self.elt = elt

    def elt_text_or_BLANK(self, key):
        elt = self.elt.find(key)
        return getattr(elt, "text", "")

    def to_dict(self):
        def get_period(period):
            return Period(period).to_dict()

        def get_periods(periods):
            return [ get_period(period)
                        for period in periods ]
        data = {
            "title": self.elt_text_or_BLANK("title"),
            "description": self.elt_text_or_BLANK("description"),
            "periods": get_periods(self.elt.xpath('period')),
        }
        return data

class Result(object):
    def __init__(self, elt):
        self.elt = elt

    def __repr__(self):
        return '''<Result: %s>''' % self.elt

    def elt_text_or_BLANK(self, key):
        elt = self.elt.find(key)
        return getattr(elt, "text", "")

    def to_dict(self):
        def get_indicator(indicator):
            return Indicator(indicator).to_dict()

        def get_indicators(indicators):
            return [ get_indicator(indicator)
                        for indicator in indicators ]

        data = {
            "title": self.elt_text_or_BLANK("title"),
            "description": self.elt_text_or_BLANK("description"),
            "indicators": get_indicators(self.elt.xpath('indicator')),
            }
        return data

class Results(object):
    def __init__(self, xml_string):
        root = lxml.etree.fromstring(xml_string)
        self.root = root

    def get_results(self):
        for i in self.root.iterfind('result'):
            yield Result(i)

class Condition(object):
    def __init__(self, elt):
        self.elt = elt

    def __repr__(self):
        return '''<Condition: %s>''' % self.elt

    def to_dict(self):
        data = {
            "type": self.elt.xpath("@type"),
            "text": self.elt.text,
            }
        return data

class Conditions(object):
    def __init__(self, xml_string):
        root = lxml.etree.fromstring(xml_string)
        self.root = root

    def get_conditions(self):
        return { 'attached': self.root.xpath("conditions/@attached"),
                 'conditions': [ Condition(condition).to_dict()
                        for condition in self.root.xpath('conditions/condition') ] }

class ActivityInfo(object):
    def __init__(self, xml_string):
        self.root = lxml.etree.fromstring(xml_string)
        self.title = self.elt_text_or_MISSING("title")
        self.description = self.elt_text_or_MISSING("description")

    def elt_text_or_MISSING(self, key):
        elt = self.root.find(key)
        return getattr(elt, "text", "MISSING")

class NoMatchingTest(Exception):
    pass

class TestInfo(object):
    def __init__(self, test_foxpath):
        self.test_string = test_foxpath
        self.test_id = self.id_of_string()

    def id_of_string(self):
        sql = '''select id from test where name = %s;'''
        q = sql % adapt(self.test_string).getquoted()

        results = query(q)
        if len(results) < 1:
            raise NoMatchingTest(self.test_string)
        return results[0][0]
<|endoftext|>


<commit_before># coding=utf-8
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import absolute_import, division, print_function, unicode_literals

from pants.bin.pants_loader import PantsLoader
from pants_test.pants_run_integration_test import PantsRunIntegrationTest

class LoaderIntegrationTest(PantsRunIntegrationTest):
  def test_invalid_locale(self):
    bypass_env = PantsLoader.ENCODING_IGNORE_ENV_VAR
    pants_run = self.run_pants(command=['help'], extra_env={'LC_ALL': 'iNvALiD-lOcALe'})
    self.assert_failure(pants_run)
    self.assertIn('Pants requires', pants_run.stderr_data)
    self.assertIn(bypass_env, pants_run.stderr_data)

    pants_run = self.run_pants(command=['help'], extra_env={'LC_ALL': 'iNvALiD-lOcALe',
                                                            bypass_env: '1'})
    self.assert_success(pants_run)

  def test_alternate_entrypoint(self):
    pants_run = self.run_pants(
      command=['help'],
      extra_env={'PANTS_ENTRYPOINT': 'pants.bin.pants_exe:test'}
    )
    self.assert_success(pants_run)
    self.assertIn('T E S T', pants_run.stdout_data)

  def test_alternate_entrypoint_bad(self):
    pants_run = self.run_pants(command=['help'], extra_env={'PANTS_ENTRYPOINT': 'badness'})
    self.assert_failure(pants_run)
    self.assertIn('entrypoint must be', pants_run.stderr_data)

  def test_alternate_entrypoint_not_callable(self):
    pants_run = self.run_pants(
      command=['help'],
      extra_env={'PANTS_ENTRYPOINT': 'pants.bin.pants_exe:TEST_STR'}
    )
    self.assert_failure(pants_run)
    self.assertIn('TEST_STR', pants_run.stderr_data)
    self.assertIn('not callable', pants_run.stderr_data)

  def test_alternate_entrypoint_scrubbing(self):
    pants_run = self.run_pants(
      command=['help'],
      extra_env={'PANTS_ENTRYPOINT': 'pants.bin.pants_exe:test_env'}
    )
    self.assert_success(pants_run)
    self.assertIn('PANTS_ENTRYPOINT=None', pants_run.stdout_data)
<commit_msg>Fix locale test failing on Python 3.7 due to forced UTF-8 mode (#7412)<commit_after># coding=utf-8
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import absolute_import, division, print_function, unicode_literals

from pants.bin.pants_loader import PantsLoader
from pants_test.pants_run_integration_test import PantsRunIntegrationTest

class LoaderIntegrationTest(PantsRunIntegrationTest):
  def test_invalid_locale(self):
    bypass_env = PantsLoader.ENCODING_IGNORE_ENV_VAR
    pants_run = self.run_pants(
      command=['help'],
      extra_env={'LC_ALL': 'iNvALiD-lOcALe', "PYTHONUTF8": "0"}
    )
    self.assert_failure(pants_run)
    self.assertIn('Pants requires', pants_run.stderr_data)
    self.assertIn(bypass_env, pants_run.stderr_data)

    pants_run = self.run_pants(
      command=['help'],
      extra_env={'LC_ALL': 'iNvALiD-lOcALe', "PYTHONUTF8": "0", bypass_env: '1'}
    )
    self.assert_success(pants_run)

  def test_alternate_entrypoint(self):
    pants_run = self.run_pants(
      command=['help'],
      extra_env={'PANTS_ENTRYPOINT': 'pants.bin.pants_exe:test'}
    )
    self.assert_success(pants_run)
    self.assertIn('T E S T', pants_run.stdout_data)

  def test_alternate_entrypoint_bad(self):
    pants_run = self.run_pants(command=['help'], extra_env={'PANTS_ENTRYPOINT': 'badness'})
    self.assert_failure(pants_run)
    self.assertIn('entrypoint must be', pants_run.stderr_data)

  def test_alternate_entrypoint_not_callable(self):
    pants_run = self.run_pants(
      command=['help'],
      extra_env={'PANTS_ENTRYPOINT': 'pants.bin.pants_exe:TEST_STR'}
    )
    self.assert_failure(pants_run)
    self.assertIn('TEST_STR', pants_run.stderr_data)
    self.assertIn('not callable', pants_run.stderr_data)

  def test_alternate_entrypoint_scrubbing(self):
    pants_run = self.run_pants(
      command=['help'],
      extra_env={'PANTS_ENTRYPOINT': 'pants.bin.pants_exe:test_env'}
    )
    self.assert_success(pants_run)
    self.assertIn('PANTS_ENTRYPOINT=None', pants_run.stdout_data)
<|endoftext|>


<commit_before>from django.db import models

class Building(models.Model):
    name = models.CharField(max_length=10)

    def __unicode__(self):
        return u"Building: %s" % self.name

class Device(models.Model):
    building = models.ForeignKey('Building')
    name = models.CharField(max_length=10)

    def __unicode__(self):
        return u"device '%s' in building %s" % (self.name, self.building)

class Port(models.Model):
    device = models.ForeignKey('Device')
    number = models.CharField(max_length=10)

    def __unicode__(self):
        return u"%s/%s" % (self.device.name, self.number)

class Connection(models.Model):
    start = models.ForeignKey(Port, related_name='connection_start',
            unique=True)
    end = models.ForeignKey(Port, related_name='connection_end', unique=True)

    def __unicode__(self):
        return u"%s to %s" % (self.start, self.end)

__test__ = {'API_TESTS': """
Regression test for bug #7110. When using select_related(), we must query the
Device and Building tables using two different aliases (each) in order to
differentiate the start and end Connection fields. The net result is that both
the "connections = ..." queries here should give the same results.

>>> b=Building.objects.create(name='101')
>>> dev1=Device.objects.create(name="router", building=b)
>>> dev2=Device.objects.create(name="switch", building=b)
>>> dev3=Device.objects.create(name="server", building=b)
>>> port1=Port.objects.create(number='4',device=dev1)
>>> port2=Port.objects.create(number='7',device=dev2)
>>> port3=Port.objects.create(number='1',device=dev3)
>>> c1=Connection.objects.create(start=port1, end=port2)
>>> c2=Connection.objects.create(start=port2, end=port3)

>>> connections=Connection.objects.filter(start__device__building=b, end__device__building=b).order_by('id')
>>> [(c.id, unicode(c.start), unicode(c.end)) for c in connections]
[(1, u'router/4', u'switch/7'), (2, u'switch/7', u'server/1')]

>>> connections=Connection.objects.filter(start__device__building=b, end__device__building=b).select_related().order_by('id')
>>> [(c.id, unicode(c.start), unicode(c.end)) for c in connections]
[(1, u'router/4', u'switch/7'), (2, u'switch/7', u'server/1')]

# This final query should only join seven tables (port, device and building
# twice each, plus connection once).
>>> connections.query.count_active_tables()
7

"""}
<commit_msg>Renamed a field in a test case that was preventing the tests from running in Oracle.<commit_after>from django.db import models

class Building(models.Model):
    name = models.CharField(max_length=10)

    def __unicode__(self):
        return u"Building: %s" % self.name

class Device(models.Model):
    building = models.ForeignKey('Building')
    name = models.CharField(max_length=10)

    def __unicode__(self):
        return u"device '%s' in building %s" % (self.name, self.building)

class Port(models.Model):
    device = models.ForeignKey('Device')
    port_number = models.CharField(max_length=10)

    def __unicode__(self):
        return u"%s/%s" % (self.device.name, self.port_number)

class Connection(models.Model):
    start = models.ForeignKey(Port, related_name='connection_start',
            unique=True)
    end = models.ForeignKey(Port, related_name='connection_end', unique=True)

    def __unicode__(self):
        return u"%s to %s" % (self.start, self.end)

__test__ = {'API_TESTS': """
Regression test for bug #7110. When using select_related(), we must query the
Device and Building tables using two different aliases (each) in order to
differentiate the start and end Connection fields. The net result is that both
the "connections = ..." queries here should give the same results.

>>> b=Building.objects.create(name='101')
>>> dev1=Device.objects.create(name="router", building=b)
>>> dev2=Device.objects.create(name="switch", building=b)
>>> dev3=Device.objects.create(name="server", building=b)
>>> port1=Port.objects.create(port_number='4',device=dev1)
>>> port2=Port.objects.create(port_number='7',device=dev2)
>>> port3=Port.objects.create(port_number='1',device=dev3)
>>> c1=Connection.objects.create(start=port1, end=port2)
>>> c2=Connection.objects.create(start=port2, end=port3)

>>> connections=Connection.objects.filter(start__device__building=b, end__device__building=b).order_by('id')
>>> [(c.id, unicode(c.start), unicode(c.end)) for c in connections]
[(1, u'router/4', u'switch/7'), (2, u'switch/7', u'server/1')]

>>> connections=Connection.objects.filter(start__device__building=b, end__device__building=b).select_related().order_by('id')
>>> [(c.id, unicode(c.start), unicode(c.end)) for c in connections]
[(1, u'router/4', u'switch/7'), (2, u'switch/7', u'server/1')]

# This final query should only join seven tables (port, device and building
# twice each, plus connection once).
>>> connections.query.count_active_tables()
7

"""}
<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-

__author__ = 'ray'
__date__ = '3/20/15'

import os

from PIL import Image

from stonemason.renderer.cartographer import MapnikLayer
from stonemason.renderer.maplayer import MapContext
from stonemason.provider.pyramid import Pyramid

from tests import SAMPLE_THEME_DIRECTORY, DATA_DIRECTORY
from tests import skipUnlessHasMapnik, ImageTestCase

@skipUnlessHasMapnik()
class TestMapnikLayer(ImageTestCase):
    def setUp(self):
        theme_root = os.path.join(SAMPLE_THEME_DIRECTORY, 'sample_world')
        style_sheet = os.path.join(theme_root, 'sample_world.xml')

        self._layer = MapnikLayer('mapnik', style_sheet=style_sheet)

    def test_image(self):
        pyramid = Pyramid()

        context = MapContext(
            pyramid=pyramid,
            target_bbox=(-10037508.34, -10037508.34, 10037508.34, 10037508.34),
            target_size=(512, 512),
        )

        image = self._layer.image(context)

        test_data_root = os.path.join(DATA_DIRECTORY, 'cartographer')
        test_file = os.path.join(test_data_root, 'mapnik_sample_world.png')
        # image.save(test_file, 'png')

        expected = Image.open(test_file)
        self.assertImageEqual(expected, image)<commit_msg>TEST: Fix test errors. Skip test for mapnik layer when mapping is not available.<commit_after># -*- encoding: utf-8 -*-

__author__ = 'ray'
__date__ = '3/20/15'

import os

from tests import SAMPLE_THEME_DIRECTORY, DATA_DIRECTORY
from tests import skipUnlessHasMapnik, HAS_MAPNIK, ImageTestCase

if HAS_MAPNIK:
    from PIL import Image
    from stonemason.renderer.cartographer import MapnikLayer
    from stonemason.renderer.maplayer import MapContext
    from stonemason.pyramid import Pyramid

@skipUnlessHasMapnik()
class TestMapnikLayer(ImageTestCase):
    def setUp(self):
        theme_root = os.path.join(SAMPLE_THEME_DIRECTORY, 'sample_world')
        style_sheet = os.path.join(theme_root, 'sample_world.xml')

        self._layer = MapnikLayer('mapnik', style_sheet=style_sheet)

    def test_image(self):
        pyramid = Pyramid()

        context = MapContext(
            pyramid=pyramid,
            target_bbox=(-10037508.34, -10037508.34, 10037508.34, 10037508.34),
            target_size=(512, 512),
        )

        image = self._layer.image(context)

        test_data_root = os.path.join(DATA_DIRECTORY, 'cartographer')
        test_file = os.path.join(test_data_root, 'mapnik_sample_world.png')
        # image.save(test_file, 'png')

        expected = Image.open(test_file)
        self.assertImageEqual(expected, image)
<|endoftext|>


<commit_before>import unittest
import collections
import logging

from AlphaTwirl.ConcurrentlySP import CommunicationChannel, TaskPackage

##__________________________________________________________________||
MockTask = collections.namedtuple('MockTask', 'name')
MockResult = collections.namedtuple('MockResult', 'name')

##__________________________________________________________________||
class MockDropbox(object):
    def __init__(self):
        self.nopened = 0
        self.nclosed = 0
        self.packages = [ ]
        self.result = None

    def open(self):
        self.nopened += 1

    def put(self, package):
        self.packages.append(package)

    def receive(self):
        return self.result

    def close(self):
        self.nclosed += 1

##__________________________________________________________________||
class MockProgressReporter(object): pass

##__________________________________________________________________||
class MockProgressMonitor(object):
    def __init__(self):
        self.reporters = [ ]

    def createReporter(self):
        reporter = MockProgressReporter()
        self.reporters.append(reporter)
        return reporter

##__________________________________________________________________||
class TestCommunicationChannel(unittest.TestCase):

    def test_begin_end(self):
        dropbox = MockDropbox()
        obj = CommunicationChannel(dropbox = dropbox)

        self.assertEqual(0, dropbox.nopened)
        self.assertEqual(0, dropbox.nclosed)

        obj.begin()
        self.assertEqual(1, dropbox.nopened)
        self.assertEqual(0, dropbox.nclosed)

        obj.begin()
        self.assertEqual(1, dropbox.nopened) # don't open twice
        self.assertEqual(0, dropbox.nclosed)

        obj.end()
        self.assertEqual(1, dropbox.nopened)
        self.assertEqual(1, dropbox.nclosed)

        obj.end()
        self.assertEqual(1, dropbox.nopened)
        self.assertEqual(1, dropbox.nclosed) # don't close twice

        obj.begin()
        self.assertEqual(2, dropbox.nopened) # can open again
        self.assertEqual(1, dropbox.nclosed)

    def test_put(self):
        dropbox = MockDropbox()
        obj = CommunicationChannel(dropbox = dropbox)
        obj.begin()

        task1 = MockTask('task1')
        obj.put(task1)

        task2 = MockTask('task2')
        obj.put(task2, 123, 'ABC', A = 34)

        self.assertEqual([
            TaskPackage(task = task1, progressReporter = None, args = (), kwargs = {}),
            TaskPackage(task = task2, progressReporter = None, args = (123, 'ABC'), kwargs = {'A': 34}),
        ], dropbox.packages)

        obj.end()

    def test_receive(self):
        dropbox = MockDropbox()
        obj = CommunicationChannel(dropbox = dropbox)
        obj.begin()

        result1 = MockResult('result1')
        dropbox.result = result1
        self.assertEqual(result1, obj.receive())

        obj.end()

    def test_put_when_closed(self):
        dropbox = MockDropbox()
        obj = CommunicationChannel(dropbox = dropbox)

        # logging.getLogger('AlphaTwirl').setLevel(logging.DEBUG)
        task1 = MockTask('task1')
        obj.put(task1)

        self.assertEqual([ ], dropbox.packages) # empty

    def test_receive_when_closed(self):
        dropbox = MockDropbox()
        obj = CommunicationChannel(dropbox = dropbox)

        # logging.getLogger('AlphaTwirl').setLevel(logging.DEBUG)
        result1 = MockResult('result1')
        dropbox.result = result1
        self.assertIsNone(obj.receive())

        obj.end()

    def test_put_progressMonitor(self):
        dropbox = MockDropbox()

        progressMonitor = MockProgressMonitor()
        obj = CommunicationChannel(
            dropbox = dropbox,
            progressMonitor = progressMonitor
        )
        obj.begin()

        task1 = MockTask('task1')
        obj.put(task1)

        task2 = MockTask('task2')
        obj.put(task2, 123, 'ABC', A = 34)

        self.assertEqual([
            TaskPackage(
                task = task1,
                progressReporter = progressMonitor.reporters[0],
                args = (), kwargs = {}
            ),
            TaskPackage(
                task = task2,
                progressReporter = progressMonitor.reporters[1],
                args = (123, 'ABC'), kwargs = {'A': 34}
            ),
        ], dropbox.packages)

        obj.end()

##__________________________________________________________________||
<commit_msg>update a comment line<commit_after># Tai Sakuma <tai.sakuma@cern.ch>
import unittest
import collections
import logging

from AlphaTwirl.ConcurrentlySP import CommunicationChannel, TaskPackage

##__________________________________________________________________||
MockTask = collections.namedtuple('MockTask', 'name')
MockResult = collections.namedtuple('MockResult', 'name')

##__________________________________________________________________||
class MockDropbox(object):
    def __init__(self):
        self.nopened = 0
        self.nclosed = 0
        self.packages = [ ]
        self.result = None

    def open(self):
        self.nopened += 1

    def put(self, package):
        self.packages.append(package)

    def receive(self):
        return self.result

    def close(self):
        self.nclosed += 1

##__________________________________________________________________||
class MockProgressReporter(object): pass

##__________________________________________________________________||
class MockProgressMonitor(object):
    def __init__(self):
        self.reporters = [ ]

    def createReporter(self):
        reporter = MockProgressReporter()
        self.reporters.append(reporter)
        return reporter

##__________________________________________________________________||
class TestCommunicationChannel(unittest.TestCase):

    def test_begin_end(self):
        dropbox = MockDropbox()
        obj = CommunicationChannel(dropbox = dropbox)

        self.assertEqual(0, dropbox.nopened)
        self.assertEqual(0, dropbox.nclosed)

        obj.begin()
        self.assertEqual(1, dropbox.nopened)
        self.assertEqual(0, dropbox.nclosed)

        obj.begin()
        self.assertEqual(1, dropbox.nopened) # don't open twice
        self.assertEqual(0, dropbox.nclosed)

        obj.end()
        self.assertEqual(1, dropbox.nopened)
        self.assertEqual(1, dropbox.nclosed)

        obj.end()
        self.assertEqual(1, dropbox.nopened)
        self.assertEqual(1, dropbox.nclosed) # don't close twice

        obj.begin()
        self.assertEqual(2, dropbox.nopened) # can open again
        self.assertEqual(1, dropbox.nclosed)

    def test_put(self):
        dropbox = MockDropbox()
        obj = CommunicationChannel(dropbox = dropbox)
        obj.begin()

        task1 = MockTask('task1')
        obj.put(task1)

        task2 = MockTask('task2')
        obj.put(task2, 123, 'ABC', A = 34)

        self.assertEqual([
            TaskPackage(task = task1, progressReporter = None, args = (), kwargs = {}),
            TaskPackage(task = task2, progressReporter = None, args = (123, 'ABC'), kwargs = {'A': 34}),
        ], dropbox.packages)

        obj.end()

    def test_receive(self):
        dropbox = MockDropbox()
        obj = CommunicationChannel(dropbox = dropbox)
        obj.begin()

        result1 = MockResult('result1')
        dropbox.result = result1
        self.assertEqual(result1, obj.receive())

        obj.end()

    def test_put_when_closed(self):
        dropbox = MockDropbox()
        obj = CommunicationChannel(dropbox = dropbox)

        # logging.getLogger('AlphaTwirl').setLevel(logging.DEBUG)
        task1 = MockTask('task1')
        obj.put(task1)

        self.assertEqual([ ], dropbox.packages) # empty

    def test_receive_when_closed(self):
        dropbox = MockDropbox()
        obj = CommunicationChannel(dropbox = dropbox)

        # logging.getLogger('AlphaTwirl').setLevel(logging.DEBUG)
        result1 = MockResult('result1')
        dropbox.result = result1
        self.assertIsNone(obj.receive())

        obj.end()

    def test_put_progressMonitor(self):
        dropbox = MockDropbox()

        progressMonitor = MockProgressMonitor()
        obj = CommunicationChannel(
            dropbox = dropbox,
            progressMonitor = progressMonitor
        )
        obj.begin()

        task1 = MockTask('task1')
        obj.put(task1)

        task2 = MockTask('task2')
        obj.put(task2, 123, 'ABC', A = 34)

        self.assertEqual([
            TaskPackage(
                task = task1,
                progressReporter = progressMonitor.reporters[0],
                args = (), kwargs = {}
            ),
            TaskPackage(
                task = task2,
                progressReporter = progressMonitor.reporters[1],
                args = (123, 'ABC'), kwargs = {'A': 34}
            ),
        ], dropbox.packages)

        obj.end()

##__________________________________________________________________||
<|endoftext|>


<commit_before>import asyncio

from src import anoncreds, signus, ledger, agent

async def main():
    await anoncreds.demo()
    await signus.demo()
    await ledger.demo()
    await agent.demo()

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
<commit_msg>Fix python sample.<commit_after>import asyncio
import time

from src import anoncreds, signus, ledger, agent

async def main():
    await anoncreds.demo()
    await signus.demo()
    await ledger.demo()
    await agent.demo()

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    time.sleep(1)  # FIXME waiting for libindy thread complete
<|endoftext|>