for name in "t0,t1,t2,t4,s0,s1,s2,s4".split(","):
            self.check_function(getattr(self.module, name))

from numpy.testing import *
from numpy import array
import util

class TestReturnLogical(util.F2PyTest):
    def check_function(self, t):
        assert_( t(True)==1,repr(t(True)))
        assert_( t(False)==0,repr(t(False)))
        assert_( t(0)==0)
        assert_( t(None)==0)
        assert_( t(0.0)==0)
        assert_( t(0j)==0)
        assert_( t(1j)==1)
        assert_( t(234)==1)
        assert_( t(234.6)==1)
        assert_( t(234)==1)
        assert_( t(234.6+3j)==1)
        assert_( t('234')==1)
        assert_( t('aaa')==1)
        assert_( t('')==0)
        assert_( t([])==0)
        assert_( t(())==0)
        assert_( t({})==0)
        assert_( t(t)==1)
        assert_( t(-234)==1)
        assert_( t(10**100)==1)
        assert_( t([234])==1)
        assert_( t((234,))==1)
        assert_( t(array(234))==1)
        assert_( t(array([234]))==1)
        assert_( t(array([[234]]))==1)
        assert_( t(array([234],'b'))==1)
        assert_( t(array([234],'h'))==1)
        assert_( t(array([234],'i'))==1)
        assert_( t(array([234],'l'))==1)
        assert_( t(array([234],'f'))==1)
        assert_( t(array([234],'d'))==1)
        assert_( t(array([234+3j],'F'))==1)
        assert_( t(array([234],'D'))==1)
        assert_( t(array(0))==0)
        assert_( t(array([0]))==0)
        assert_( t(array([[0]]))==0)
        assert_( t(array([0j]))==0)
        assert_( t(array([1]))==1)
        assert_raises(ValueError, t, array([0,0]))

class TestF77ReturnLogical(TestReturnLogical):
    code = """
       function t0(value)
         logical value
         logical t0
         t0 = value
       end
       function t1(value)
         logical*1 value
         logical*1 t1
         t1 = value
       end
       function t2(value)
         logical*2 value
         logical*2 t2
         t2 = value
       end
       function t4(value)
         logical*4 value
         logical*4 t4
         t4 = value
       end
c       function t8(value)
c         logical*8 value
c         logical*8 t8
c         t8 = value
c       end

       subroutine s0(t0,value)
         logical value
         logical t0
cf2py    intent(out) t0
         t0 = value
       end
       subroutine s1(t1,value)
         logical*1 value
         logical*1 t1
cf2py    intent(out) t1
         t1 = value
       end
       subroutine s2(t2,value)
         logical*2 value
         logical*2 t2
cf2py    intent(out) t2
         t2 = value
       end
       subroutine s4(t4,value)
         logical*4 value
         logical*4 t4
cf2py    intent(out) t4
         t4 = value
       end
c       subroutine s8(t8,value)
c         logical*8 value
c         logical*8 t8
cf2py    intent(out) t8
c         t8 = value
c       end
    """

    @dec.slow
    def test_all(self):

class TestF90ReturnLogical(TestReturnLogical):
    suffix = ".f90"
    code = """
module f90_return_logical
  contains
       function t0(value)
         logical :: value
         logical :: t0
         t0 = value
       end function t0
       function t1(value)
         logical(kind=1) :: value
         logical(kind=1) :: t1
         t1 = value
       end function t1
       function t2(value)
         logical(kind=2) :: value
         logical(kind=2) :: t2
         t2 = value
       end function t2
       function t4(value)
         logical(kind=4) :: value
         logical(kind=4) :: t4
         t4 = value
       end function t4
       function t8(value)
         logical(kind=8) :: value
         logical(kind=8) :: t8
         t8 = value
       end function t8

       subroutine s0(t0,value)
         logical :: value
         logical :: t0
!f2py    intent(out) t0
         t0 = value
       end subroutine s0
       subroutine s1(t1,value)
         logical(kind=1) :: value
         logical(kind=1) :: t1
!f2py    intent(out) t1
         t1 = value
       end subroutine s1
       subroutine s2(t2,value)
         logical(kind=2) :: value
         logical(kind=2) :: t2
!f2py    intent(out) t2
         t2 = value
       end subroutine s2
       subroutine s4(t4,value)
         logical(kind=4) :: value
         logical(kind=4) :: t4
!f2py    intent(out) t4
         t4 = value
       end subroutine s4
       subroutine s8(t8,value)
         logical(kind=8) :: value
         logical(kind=8) :: t8
!f2py    intent(out) t8
         t8 = value
       end subroutine s8
end module f90_return_logical
    """

    @dec.slow
    def test_all(self):
        for name in "t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","):
            self.check_function(getattr(self.module.f90_return_logical, name))

if __name__ == "__main__":
    import nose
    nose.runmodule()


from numpy.testing import *
from numpy import array
import util

class TestReturnLogical(util.F2PyTest):
    def check_function(self, t):
        assert_( t(True)==1,repr(t(True)))
        assert_( t(False)==0,repr(t(False)))
        assert_( t(0)==0)
        assert_( t(None)==0)
        assert_( t(0.0)==0)
        assert_( t(0j)==0)
        assert_( t(1j)==1)
        assert_( t(234)==1)
        assert_( t(234.6)==1)
        assert_( t(234)==1)
        assert_( t(234.6+3j)==1)
        assert_( t('234')==1)
        assert_( t('aaa')==1)
        assert_( t('')==0)
        assert_( t([])==0)
        assert_( t(())==0)
        assert_( t({})==0)
        assert_( t(t)==1)
        assert_( t(-234)==1)
        assert_( t(10**100)==1)
        assert_( t([234])==1)
        assert_( t((234,))==1)
        assert_

        assert_( t(array([234]))==1)
        assert_( t(array([[234]]))==1)
        assert_( t(array([234],'b'))==1)
        assert_( t(array([234],'h'))==1)
        assert_( t(array([234],'i'))==1)
        assert_( t(array([234],'l'))==1)
        assert_( t(array([234],'f'))==1)
        assert_( t(array([234],'d'))==1)
        assert_( t(array([234+3j],'F'))==1)
        assert_( t(array([234],'D'))==1)
        assert_( t(array(0))==0)
        assert_( t(array([0]))==0)
        assert_( t(array([[0]]))==0)
        assert_( t(array([0j]))==0)
        assert_( t(array([1]))==1)
        assert_raises(ValueError, t, array([0,0]))

class TestF77ReturnLogical(TestReturnLogical):
    code = """
       function t0(value)
         logical value
         logical t0
         t0 = value
       end
       function t1(value)
         logical*1 value
         logical*1 t1
         t1 = value
       end
       function t2(value)
         logical*2 value
         logical*2 t2
         t2 = value
       end
       function t4(value)
         logical*4 value
         logical*4 t4
         t4 = value
       end
c       function t8(value)
c         logical*8 value
c         logical*8 t8
c         t8 = value
c       end

       subroutine s0(t0,value)
         logical value
         logical t0
cf2py    intent(out) t0
         t0 = value
       end
       subroutine s1(t1,value)
         logical*1 value
         logical*1 t1
cf2py    intent(out) t1
         t1 = value
       end
       subroutine s2(t2,value)
         logical*2 value
         logical*2 t2
cf2py    intent(out) t2
         t2 = value
       end
       subroutine s4(t4,value)
         logical*4 value
         logical*4 t4
cf2py    intent(out) t4
         t4 = value
       end
c       subroutine s8(t8,value)
c         logical*8 value
c         logical*8 t8
cf2py    intent(out) t8
c         t8 = value
c       end
    """

    @dec.slow
    def test_all(self):
        for name in "t0,t1,t2,t4,s0,s1,s2,s4".split(","):
            self.check_function(getattr(self.module, name))

class TestF90ReturnLogical(TestReturnLogical):
    suffix = ".f90"
    code = """
module f90_return_logical
  contains
       function t0(value)
         logical :: value
         logical :: t0
         t0 = value
       end function t0
       function t1(value)
         logical(kind=1) :: value
         logical(kind=1) :: t1
         t1 = value
       end function t1
       function t2(value)
         logical(kind=2) :: value
         logical(kind=2) :: t2
         t2 = value
       end function t2
       function t4(value)
         logical(kind=4) :: value
         logical(kind=4) :: t4
         t4 = value
       end function t4
       function t8(value)
         logical(kind=8) :: value
         logical(kind=8) :: t8
         t8 = value
       end function t8

       subroutine s0(t0,value)
         logical :: value
         logical :: t0
!f2py    intent(out) t0
         t0 = value
       end subroutine s0
       subroutine s1(t1,value)
         logical(kind=1) :: value
         logical(kind=1) :: t1
!f2py    intent(out) t1
         t1 = value
       end subroutine s1
       subroutine s2(t2,value)
         logical(kind=2) :: value
         logical(kind=2) :: t2
!f2py    intent(out) t2
         t2 = value
       end subroutine s2
       subroutine s4(t4,value)
         logical(kind=4) :: value
         logical(kind=4) :: t4
!f2py    intent(out) t4
         t4 = value
       end subroutine s4
       subroutine s8(t8,value)
         logical(kind=8) :: value
         logical(kind=8) :: t8
!f2py    intent(out) t8
         t8 = value
       end subroutine s8
end module f90_return_logical
    """

    @dec.slow
    def test_all(self):
        for name in "t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","):
            self.check_function(getattr(self.module.f90_return_logical, name))

if __name__ == "__main__":
    import nose
    nose.runmodule()


mat_shape = get_shape_list(mat, expected_rank=[2,3,4])
  I = tf.cast(tf.eye(mat_shape[-1]), dtype=tf.float32)
  K = tf.identity(mat)
  V = 1 / (tf.reduce_max(tf.reduce_sum(tf.abs(K), axis=-2)) * tf.reduce_max(tf.reduce_sum(tf.abs(K), axis=-1))) * tf.transpose(K, [0,1,3,2])

  for _ in range(n_iter):
      KV = tf.matmul(K, V)
      V = tf.matmul(0.25 * V, 13 * I - tf.matmul(KV, 15 * I - tf.matmul(KV, 7 * I - KV)))
  V = tf.stop_gradient(V)
  return V

from utils.bert.bert_utils import get_shape_list

import tensorflow as tf
import numpy as np
from itertools import permutations

def cost_matrix(x,y, x_weight, y_weight, p=2, distance_type='wmd'):
    "Returns the cost matrix C_{ij}=|x_i - y_j|^p"
    x_shape_list = get_shape_list(x, expected_rank=3)
    y_shape_list = get_shape_list(y, expected_rank=3)
    x_col = tf.expand_dims(x, axis=2)
    y_lin = tf.expand_dims(y, axis=1)
    x_weight = tf.expand_dims(x_weight, axis=2)
    y_weight = tf.expand_dims(y_weight, axis=1)
    if distance_type == 'wmd':
        c = tf.sqrt(tf.reduce_sum((tf.abs(x_col-y_lin))**p,axis=-1))
    elif distance_type == 'wrd':
        c = (tf.reduce_sum((tf.abs(x_col-y_lin))**p,axis=-1)) / 2.0
    c_weight = (x_weight * y_weight)
    c = c * c_weight
    c += (1.0-c_weight)*1000.0
    return c, c_weight

def sinkhorn_loss(x, y, x_weight, y_weight, epsilon=1,
                numItermax=10, stopThr=1e-9, p=2, distance_type='wmd'):
    """
    Given two emprical measures with n points each with locations x and y
    outputs an approximation of the OT cost with regularization parameter epsilon
    niter is the max. number of steps in sinkhorn loop

    Inputs:
        x,y:  The input sets representing the empirical measures.  Each are a tensor of shape (n,D)
        epsilon:  The entropy weighting factor in the sinkhorn distance, epsilon -> 0 gets closer to the true wasserstein distance
        n:  The number of support points in the empirical measures
        niter:  The number of iterations in the sinkhorn algorithm, more iterations yields a more accurate estimate
    Outputs:

    """
    x_shape_list = get_shape_list(x, expected_rank=3)
    batch_size = x_shape_list[0]
    seq_num = x_shape_list[1]
    hidden_dims = x_shape_list[2]

    # both marginals are fixed with equal weights
    x_weight = tf.cast(x_weight, dtype=tf.float32)
    y_weight = tf.cast(y_weight, dtype=tf.float32)

    if distance_type == 'wrd':

        x_norm = tf.sqrt(tf.reduce_sum(tf.pow(x, 2.0), axis=-1, keepdims=True))
        y_norm = tf.sqrt(tf.reduce_sum(tf.pow(y, 2.0), axis=-1, keepdims=True))
        x_normed = x / (x_norm+1e-10)
        y_normed = y / (y_norm+1e-10)

        # The Sinkhorn algorithm takes as input three variables :

        cost, cost_weight = cost_matrix(x_normed, y_normed, x_weight, y_weight,
                                        p=p, distance_type=distance_type)  # Wasserstein cost function

        mu_weight = tf.expand_dims(x_weight, axis=-1)
        mu = x_norm * mu_weight / tf.reduce_sum(x_norm*mu_weight, axis=-2, keepdims=True)
        nu_weight = tf.expand_dims(y_weight, axis=-1)
        nu = y_norm * nu_weight / tf.reduce_sum(y_norm*nu_weight, axis=-2, keepdims=True)
        mu = tf.squeeze(mu, axis=-1)
        nu = tf.squeeze(nu, axis=-1)

    elif distance_type == 'wmd':

        cost, cost_weight = cost_matrix(x, y, x_weight, y_weight,
                                        p=p, distance_type=distance_type)  # Wasserstein cost function
        mu = x_weight / tf.reduce_sum(x_weight, axis=-1, keep_dims=True) # [batch_size, seq]
        nu = y_weight / tf.reduce_sum(y_weight, axis=-1, keep_dims=True) # [batch_size, seq]

    u = tf.zeros_like(mu)
    v = tf.zeros_like(nu)

    # To check if algorithm terminates because of threshold
    cpt = tf.constant(0)
    err = tf.constant(1.0)

    c = lambda cpt, u, v, err: tf.logical_and(cpt < numItermax, err > stopThr)

    # Elementary operations
    def M(cost, u, v, cost_weight=None):
        "Modified cost for logarithmic updates"
        "$M_{ij} = (-c_{ij} + u_i + v_j) / \\epsilon$"
        return (-cost + tf.expand_dims(u, -1) + tf.expand_dims(v, -2) )/epsilon

    def loop_func(cpt, u, v, err):
        u1 = tf.identity(u)
        u = epsilon * x_weight * (tf.log(mu+1e-8) - tf.reduce_logsumexp(M(cost, u, v, cost_weight), axis=-1)) + u
        v = epsilon * y_weight * (tf.log(nu+1e-8) - tf.reduce_logsumexp(tf.transpose(M(cost, u, v, cost_weight), perm=(0, 2, 1)), axis=-1)) + v

        err = tf.reduce_mean(tf.reduce_sum(tf.abs(u - u1), axis=-1))

        cpt = tf.add(cpt, 1)
        return cpt, u, v, err

    _, u, v, _ = tf.while_loop(c, loop_func, loop_vars=[cpt, u, v, err])
    u_final, v_final = u, v
    pi = tf.exp(M(cost, u_final, v_final, cost_weight=None))
    # Sinkhorn distance
    # ignore the gradient flow through \pi
    pi = tf.stop_gradient(pi)
    final_cost = tf.reduce_sum(pi * cost * cost_weight, axis=(-2, -1))
    return final_cost, coste-1

def iterative_inv(mat, n_iter=10):


from utils.bert.bert_utils import get_shape_list

import tensorflow as tf
import numpy as np
from itertools import permutations

def cost_matrix(x,y, x_weight, y_weight, p=2, distance_type='wmd'):
    "Returns the cost matrix C_{ij}=|x_i - y_j|^p"
    x_shape_list = get_shape_list(x, expected_rank=3)
    y_shape_list = get_shape_list(y, expected_rank=3)
    x_col = tf.expand_dims(x, axis=2)
    y_lin = tf.expand_dims(y, axis=1)
    x_weight = tf.expand_dims(x_weight, axis=2)
    y_weight = tf.expand_dims(y_weight, axis=1)
    if distance_type == 'wmd':
        c = tf.sqrt(tf.reduce_sum((tf.abs(x_col-y_lin))**p,axis=-1))
    elif distance_type == 'wrd':
        c = (tf.reduce_sum((tf.abs(x_col-y_lin))**p,axis=-1)) / 2.0
    c_weight = (x_weight * y_weight)
    c = c * c_weight
    c += (1.0-c_weight)*1000.0
    return c, c_weight

def sinkhorn_loss(x, y, x_weight, y_weight, epsilon=1,
                numItermax=10, stopThr=1e-9, p=2, distance_type='wmd'):
    """
    Given two emprical measures with n points each with locations x and y
    outputs an approximation of the OT cost with regularization parameter epsilon
    niter is the max. number of steps in sinkhorn loop

    Inputs:
        x,y:  The input sets representing the empirical measures.  Each are a tensor of shape (n,D)
        epsilon:  The entropy weighting factor in the sinkhorn distance, epsilon -> 0 gets closer to the true wasserstein distance
        n:  The number of support points in the empirical measures
        niter:  The number of iterations in the sinkhorn algorithm, more iterations yields a more accurate estimate
    Outputs:

    """
    x_shape_list = get_shape_list(x, expected_rank=3)
    batch_size = x_shape_list[0]
    seq_num = x_shape_list[1]
    hidden_dims = x_shape_list[2]

    # both marginals are fixed with equal weights
    x_weight = tf.cast(x_weight, dtype=tf.float32)
    y_weight = tf.cast(y_weight, dtype=tf.float32)

    if distance_type == 'wrd':

        x_norm = tf.sqrt(tf.reduce_sum(tf.pow(x, 2.0), axis=-1, keepdims=True))
        y_norm = tf.sqrt(tf.reduce_sum(tf.pow(y, 2.0), axis=-1, keepdims=True))
        x_normed = x / (x_norm+1e-10)
        y_normed = y / (y_norm+1e-10)

        # The Sinkhorn algorithm takes as input three variables :

        cost, cost_weight = cost_matrix(x_normed, y_normed, x_weight, y_weight,
                                        p=p, distance_type=distance_type)  # Wasserstein cost function

        mu_weight = tf.expand_dims(x_weight, axis=-1)
        mu = x_norm * mu_weight / tf.reduce_sum(x_norm*mu_weight, axis=-2, keepdims=True)
        nu_weight = tf.expand_dims(y_weight, axis=-1)
        nu = y_norm * nu_weight / tf.reduce_sum(y_norm*nu_weight, axis=-2, keepdims=True)
        mu = tf.squeeze(mu, axis=-1)
        nu = tf.squeeze(nu, axis=-1)

    elif distance_type == 'wmd':

        cost, cost_weight = cost_matrix(x, y, x_weight, y_weight,
                                        p=p, distance_type=distance_type)  # Wasserstein cost function
        mu = x_weight / tf.reduce_sum(x_weight, axis=-1, keep_dims=True) # [batch_size, seq]
        nu = y_weight / tf.reduce_sum(y_weight, axis=-1, keep_dims=True) # [batch_size, seq]

    u = tf.zeros_like(mu)
    v = tf.zeros_like(nu)

    # To check if algorithm terminates because of threshold
    cpt = tf.constant(0)
    err = tf.constant(1.0)

    c = lambda cpt, u, v, err: tf.logical_and(cpt < numItermax, err > stopThr)

    # Elementary operations
    def M(cost, u, v, cost_weight=None):
        "Modified cost for logarithmic updates"
        "$M_{ij} = (-c_{ij} + u_i + v_j) / \\epsilon$"
        return (-cost + tf.expand_dims(u, -1) + tf.expand_dims(v, -2) )/epsilon

    def loop_func(cpt, u, v, err):
        u1 = tf.identity(u)
        u = epsilon * x_weight * (tf.log(mu+1e-8) - tf.reduce_logsumexp(M(cost, u, v, cost_weight), axis=-1)) + u
        v = epsilon * y_weight * (tf.log(nu+1e-8) - tf.reduce_logsumexp(tf.transpose(M(cost, u, v, cost_weight), perm=(0, 2, 1)), axis=-1)) + v

        err = tf.reduce_mean(tf.reduce_sum(tf.abs(u - u1), axis=-1))

        cpt = tf.

        return cpt, u, v, err

    _, u, v, _ = tf.while_loop(c, loop_func, loop_vars=[cpt, u, v, err])
    u_final, v_final = u, v
    pi = tf.exp(M(cost, u_final, v_final, cost_weight=None))
    # Sinkhorn distance
    # ignore the gradient flow through \pi
    pi = tf.stop_gradient(pi)
    final_cost = tf.reduce_sum(pi * cost * cost_weight, axis=(-2, -1))
    return final_cost, coste-1

def iterative_inv(mat, n_iter=10):
  mat_shape = get_shape_list(mat, expected_rank=[2,3,4])
  I = tf.cast(tf.eye(mat_shape[-1]), dtype=tf.float32)
  K = tf.identity(mat)
  V = 1 / (tf.reduce_max(tf.reduce_sum(tf.abs(K), axis=-2)) * tf.reduce_max(tf.reduce_sum(tf.abs(K), axis=-1))) * tf.transpose(K, [0,1,3,2])

  for _ in range(n_iter):
      KV = tf.matmul(K, V)
      V = tf.matmul(0.25 * V, 13 * I - tf.matmul(KV, 15 * I - tf.matmul(KV, 7 * I - KV)))
  V = tf.stop_gradient(V)
  return V


        "$M_{ij} = (-c_{ij} + u_i + v_j) / \\epsilon$"
        return (-cost + tf.expand_dims(u, -1) + tf.expand_dims(v, -2) )/epsilon

from utils.bert.bert_utils import get_shape_list

import tensorflow as tf
import numpy as np
from itertools import permutations

def cost_matrix(x,y, x_weight, y_weight, p=2, distance_type='wmd'):
    "Returns the cost matrix C_{ij}=|x_i - y_j|^p"
    x_shape_list = get_shape_list(x, expected_rank=3)
    y_shape_list = get_shape_list(y, expected_rank=3)
    x_col = tf.expand_dims(x, axis=2)
    y_lin = tf.expand_dims(y, axis=1)
    x_weight = tf.expand_dims(x_weight, axis=2)
    y_weight = tf.expand_dims(y_weight, axis=1)
    if distance_type == 'wmd':
        c = tf.sqrt(tf.reduce_sum((tf.abs(x_col-y_lin))**p,axis=-1))
    elif distance_type == 'wrd':
        c = (tf.reduce_sum((tf.abs(x_col-y_lin))**p,axis=-1)) / 2.0
    c_weight = (x_weight * y_weight)
    c = c * c_weight
    c += (1.0-c_weight)*1000.0
    return c, c_weight

def sinkhorn_loss(x, y, x_weight, y_weight, epsilon=1,
                numItermax=10, stopThr=1e-9, p=2, distance_type='wmd'):
    """
    Given two emprical measures with n points each with locations x and y
    outputs an approximation of the OT cost with regularization parameter epsilon
    niter is the max. number of steps in sinkhorn loop

    Inputs:
        x,y:  The input sets representing the empirical measures.  Each are a tensor of shape (n,D)
        epsilon:  The entropy weighting factor in the sinkhorn distance, epsilon -> 0 gets closer to the true wasserstein distance
        n:  The number of support points in the empirical measures
        niter:  The number of iterations in the sinkhorn algorithm, more iterations yields a more accurate estimate
    Outputs:

    """
    x_shape_list = get_shape_list(x, expected_rank=3)
    batch_size = x_shape_list[0]
    seq_num = x_shape_list[1]
    hidden_dims = x_shape_list[2]

    # both marginals are fixed with equal weights
    x_weight = tf.cast(x_weight, dtype=tf.float32)
    y_weight = tf.cast(y_weight, dtype=tf.float32)

    if distance_type == 'wrd':

        x_norm = tf.sqrt(tf.reduce_sum(tf.pow(x, 2.0), axis=-1, keepdims=True))
        y_norm = tf.sqrt(tf.reduce_sum(tf.pow(y, 2.0), axis=-1, keepdims=True))
        x_normed = x / (x_norm+1e-10)
        y_normed = y / (y_norm+1e-10)

        # The Sinkhorn algorithm takes as input three variables :

        cost, cost_weight = cost_matrix(x_normed, y_normed, x_weight, y_weight,
                                        p=p, distance_type=distance_type)  # Wasserstein cost function

        mu_weight = tf.expand_dims(x_weight, axis=-1)
        mu = x_norm * mu_weight / tf.reduce_sum(x_norm*mu_weight, axis=-2, keepdims=True)
        nu_weight = tf.expand_dims(y_weight, axis=-1)
        nu = y_norm * nu_weight / tf.reduce_sum(y_norm*nu_weight, axis=-2, keepdims=True)
        mu = tf.squeeze(mu, axis=-1)
        nu = tf.squeeze(nu, axis=-1)

    elif distance_type == 'wmd':

        cost, cost_weight = cost_matrix(x, y, x_weight, y_weight,
                                        p=p, distance_type=distance_type)  # Wasserstein cost function
        mu = x_weight / tf.reduce_sum(x_weight, axis=-1, keep_dims=True) # [batch_size, seq]
        nu = y_weight / tf.reduce_sum(y_weight, axis=-1, keep_dims=True) # [batch_size, seq]

    u = tf.zeros_like(mu)
    v = tf.zeros_like(nu)

    # To check if algorithm terminates because of threshold
    cpt = tf.constant(0)
    err = tf.constant(1.0)

    c = lambda cpt, u, v, err: tf.logical_and(cpt < numItermax, err > stopThr)

    # Elementary operations
    def M(cost, u, v, cost_weight=None):
        "Modified cost for logarithmic updates"

    def loop_func(cpt, u, v, err):
        u1 = tf.identity(u)
        u = epsilon * x_weight * (tf.log(mu+1e-8) - tf.reduce_logsumexp(M(cost, u, v, cost_weight), axis=-1)) + u
        v = epsilon * y_weight * (tf.log(nu+1e-8) - tf.reduce_logsumexp(tf.transpose(M(cost, u, v, cost_weight), perm=(0, 2, 1)), axis=-1)) + v

        err = tf.reduce_mean(tf.reduce_sum(tf.abs(u - u1), axis=-1))

        cpt = tf.add(cpt, 1)
        return cpt, u, v, err

    _, u, v, _ = tf.while_loop(c, loop_func, loop_vars=[cpt, u, v, err])
    u_final, v_final = u, v
    pi = tf.exp(M(cost, u_final, v_final, cost_weight=None))
    # Sinkhorn distance
    # ignore the gradient flow through \pi
    pi = tf.stop_gradient(pi)
    final_cost = tf.reduce_sum(pi * cost * cost_weight, axis=(-2, -1))
    return final_cost, coste-1

def iterative_inv(mat, n_iter=10):
  mat_shape = get_shape_list(mat, expected_rank=[2,3,4])
  I = tf.cast(tf.eye(mat_shape[-1]), dtype=tf.float32)
  K = tf.identity(mat)
  V = 1 / (tf.reduce_max(tf.reduce_sum(tf.abs(K), axis=-2)) * tf.reduce_max(tf.reduce_sum(tf.abs(K), axis=-1))) * tf.transpose(K, [0,1,3,2])

  for _ in range(n_iter):
      KV = tf.matmul(K, V)
      V = tf.matmul(0.25 * V, 13 * I - tf.matmul(KV, 15 * I - tf.matmul(KV, 7 * I - KV)))
  V = tf.stop_gradient(V)
  return V


# .________     /\  ________________                __
# |   ____/    / / /   __   \______ \ _____ _______|  | __
# |____  \    / /  \____    /|    |  \\__  \\_  __ \  |/ /
#  - Made with <3 by the FiveNineDark Dev team

import os
import sys
import random
import time

class color:
    HEADER = '\033[95m'
    IMPORTANT = '\33[35m'
    NOTICE = '\033[33m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    RED = '\033[91m'
    END = '\033[0m'
    UNDERLINE = '\033[4m'
    LOGGING = '\33[34m'
    # BG
    LIGHTGREY_BG ='\033[47m'
    DARKGREY_BG = '\033[90m'
    RED_BG = '\033[41m'
    BLACK_TEXT = '\033[40m'

color_random=[color.HEADER,color.IMPORTANT,color.NOTICE,color.OKBLUE,color.OKGREEN,color.WARNING,color.RED,color.END,color.UNDERLINE,color.LOGGING]
random.shuffle(color_random)

def clearScr():
    os.system('clear')

def qm_1():
  clearScr()
def qm_2():
  clearScr()
def qm_3():
  clearScr()
def main_menu():


# .________     /\  ________________                __
# |   ____/    / / /   __   \______ \ _____ _______|  | __
# |____  \    / /  \____    /|    |  \\__  \\_  __ \  |/ /
#  - Made with <3 by the FiveNineDark Dev team

import os
import sys
import random
import time

class color:
    HEADER = '\033[95m'
    IMPORTANT = '\33[35m'
    NOTICE = '\033[33m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    RED = '\033[91m'
    END = '\033[0m'
    UNDERLINE = '\033[4m'
    LOGGING = '\33[34m'
    # BG
    LIGHTGREY_BG ='\033[47m'
    DARKGREY_BG = '\033[90m'
    RED_BG = '\033[41m'
    BLACK_TEXT = '\033[40m'

color_random=[color.HEADER,color.IMPORTANT,color.NOTICE,color.OKBLUE,color.OKGREEN,color.WARNING,color.RED,color.END,color.UNDERLINE,color.LOGGING]
random.shuffle(color_random)

def clearScr():
    os.system('clear')

def qm_1():
  clearScr()
def qm_2():
  clearScr()
def qm_3():
  clearS


while True:
    menu_opt = input("Project-SKYLIT$> "S)
    if menu_opt == "help":
      print(help_screen)
    elif menu_opt == "menu":

    elif menu_opt == "":

# main
clearScr()
print(start_menu)

sel_term = input("Command: ")

if sel_term == "1":
  qm_1()
elif sel_term == "2":
  qm_2()
elif sel_term == "3":
  qm_3()
elif sel_term == "4":
  main_menu()


"""
    Checks whether the request of a response has been a HEAD-request.
    Handles the quirks of AppEngine.

    :param conn:
    :type conn: :class:`httplib.HTTPResponse`
    """
    # FIXME: Can we do this somehow without accessing private httplib _method?
    method = response._method
    if isinstance(method, int):  # Platform-specific: Appengine
        return method == 3
    return method.upper() == 'HEAD'

from __future__ import absolute_import
from ..packages.six.moves import http_client as httplib

from ..exceptions import HeaderParsingError

def is_fp_closed(obj):
    """
    Checks whether a given file-like object is closed.

    :param obj:
        The file-like object to check.
    """

    try:
        # Check via the official file-like-object way.
        return obj.closed
    except AttributeError:
        pass

    try:
        # Check if the object is a container for another file-like object that
        # gets released on exhaustion (e.g. HTTPResponse).
        return obj.fp is None
    except AttributeError:
        pass

    raise ValueError("Unable to determine whether fp is closed.")

def assert_header_parsing(headers):
    """
    Asserts whether all headers have been successfully parsed.
    Extracts encountered errors from the result of parsing headers.

    Only works on Python 3.

    :param headers: Headers to verify.
    :type headers: `httplib.HTTPMessage`.

    :raises urllib3.exceptions.HeaderParsingError:
        If parsing errors are found.
    """

    # This will fail silently if we pass in the wrong kind of parameter.
    # To make debugging easier add an explicit check.
    if not isinstance(headers, httplib.HTTPMessage):
        raise TypeError('expected httplib.Message, got {0}.'.format(
            type(headers)))

    defects = getattr(headers, 'defects', None)
    get_payload = getattr(headers, 'get_payload', None)

    unparsed_data = None
    if get_payload:  # Platform-specific: Python 3.
        unparsed_data = get_payload()

    if defects or unparsed_data:
        raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)

def is_response_to_head(response):


from __future__ import absolute_import
from ..packages.six.moves import http_client as httplib

from ..exceptions import HeaderParsingError

def is_fp_closed(obj):
    """
    Checks whether a given file-like object is closed.

    :param obj:
        The file-like object to check.
    """

    try:
        # Check via the official file-like-object way.
        return obj.closed
    except AttributeError:
        pass

    try:
        # Check if the object is a container for another file-like object that
        # gets released on exhaustion (e.g. HTTPResponse).
        return obj.fp is None
    except AttributeError:
        pass

    raise ValueError("Unable to determine whether fp is closed.")

def assert_header_parsing(headers):
    """
    Asserts whether all headers have been successfully parsed.
    Extracts encountered errors from the result of parsing headers.

    Only works on Python 3.

    :param headers: Headers to verify.
    :type headers: `httplib.HTTPMessage`.

    :raises urllib3.exceptions.HeaderParsingError:
        If parsing errors are found.
    """

    # This will fail silently if we pass in the wrong kind of parameter.
    # To make debugging easier add an explicit check.
    if not isinstance(headers, httplib.HTTPMessage):
        raise TypeError('expected httplib.Message, got {0}.'.format(
            type(headers)))

    defects = getattr(headers, 'defects', None)
    get_payload = getattr(headers, 'get_payload', None)

    unparsed_data = None
    if get_payload:  # Platform-specific: Python 3.
        unparsed_data = get_payload()

    if defects or unparsed_data:
        raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)

def is_response_to_head(response):
    """
    Checks whether the request of a response has been a HEAD-request.
    Handles the quirks of AppEngine.

    :param conn:
    :type conn: :class:`httplib.HTTPResponse`
    """
    # FIXME: Can we do this somehow without accessing private httplib _method?
    method = response._method
    if isinstance(method, int):  # Platform-specific: Appengine
        return method == 3
    return method.upper()


    try:
        # Check via the official file-like-object way.
        return obj.closed
    except AttributeError:
        pass

    try:
        # Check if the object is a container for another file-like object that
        # gets released on exhaustion (e.g. HTTPResponse).
        return obj.fp is None
    except AttributeError:
        pass

    raise ValueError("Unable to determine whether fp is closed.")

from __future__ import absolute_import
from ..packages.six.moves import http_client as httplib

from ..exceptions import HeaderParsingError

def is_fp_closed(obj):
    """
    Checks whether a given file-like object is closed.

    :param obj:
        The file-like object to check.
    """

def assert_header_parsing(headers):
    """
    Asserts whether all headers have been successfully parsed.
    Extracts encountered errors from the result of parsing headers.

    Only works on Python 3.

    :param headers: Headers to verify.
    :type headers: `httplib.HTTPMessage`.

    :raises urllib3.exceptions.HeaderParsingError:
        If parsing errors are found.
    """

    # This will fail silently if we pass in the wrong kind of parameter.
    # To make debugging easier add an explicit check.
    if not isinstance(headers, httplib.HTTPMessage):
        raise TypeError('expected httplib.Message, got {0}.'.format(
            type(headers)))

    defects = getattr(headers, 'defects', None)
    get_payload = getattr(headers, 'get_payload', None)

    unparsed_data = None
    if get_payload:  # Platform-specific: Python 3.
        unparsed_data = get_payload()

    if defects or unparsed_data:
        raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)

def is_response_to_head(response):
    """
    Checks whether the request of a response has been a HEAD-request.
    Handles the quirks of AppEngine.

    :param conn:
    :type conn: :class:`httplib.HTTPResponse`
    """
    # FIXME: Can we do this somehow without accessing private httplib _method?
    method = response._method
    if isinstance(method, int):  # Platform-specific: Appengine
        return method == 3
    return method.upper() == 'HEAD'


try:
        # Check if the object is a container for another file-like object that
        # gets released on exhaustion (e.g. HTTPResponse).
        return obj.fp is None
    except AttributeError:
        pass

from __future__ import absolute_import
from ..packages.six.moves import http_client as httplib

from ..exceptions import HeaderParsingError

def is_fp_closed(obj):
    """
    Checks whether a given file-like object is closed.

    :param obj:
        The file-like object to check.
    """

    try:
        # Check via the official file-like-object way.
        return obj.closed
    except AttributeError:
        pass

    raise ValueError("Unable to determine whether fp is closed.")

def assert_header_parsing(headers):
    """
    Asserts whether all headers have been successfully parsed.
    Extracts encountered errors from the result of parsing headers.

    Only works on Python 3.

    :param headers: Headers to verify.
    :type headers: `httplib.HTTPMessage`.

    :raises urllib3.exceptions.HeaderParsingError:
        If parsing errors are found.
    """

    # This will fail silently if we pass in the wrong kind of parameter.
    # To make debugging easier add an explicit check.
    if not isinstance(headers, httplib.HTTPMessage):
        raise TypeError('expected httplib.Message, got {0}.'.format(
            type(headers)))

    defects = getattr(headers, 'defects', None)
    get_payload = getattr(headers, 'get_payload', None)

    unparsed_data = None
    if get_payload:  # Platform-specific: Python 3.
        unparsed_data = get_payload()

    if defects or unparsed_data:
        raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)

def is_response_to_head(response):
    """
    Checks whether the request of a response has been a HEAD-request.
    Handles the quirks of AppEngine.

    :param conn:
    :type conn: :class:`httplib.HTTPResponse`
    """
    # FIXME: Can we do this somehow without accessing private httplib _method?
    method = response._method
    if isinstance(method, int):  # Platform-specific: Appengine
        return method == 3
    return method.upper() == 'HEAD'


from rpython.jit.metainterp.history import BasicFinalDescr, BoxInt,\
     JitCellToken, ConstInt, BoxFloat, ConstFloat
from rpython.jit.metainterp.resoperation import rop
from rpython.jit.codewriter.effectinfo import EffectInfo
from rpython.jit.tool.oparser import parse
from rpython.rtyper.lltypesystem import lltype, llmemory
from rpython.rtyper.annlowlevel import llhelper
from rpython.jit.codewriter import heaptracker, longlong
from rpython.jit.backend.detect_cpu import getcpuclass
from rpython.jit.backend.test.runner_test import Runner
import py
import sys
import platform

def boxfloat(x):

def constfloat(x):
    return ConstFloat(longlong.getfloatstorage(x))

class FakeStats(object):
    pass

class CallingConvTests(Runner):
    type_system = 'lltype'
    Ptr = lltype.Ptr
    FuncType = lltype.FuncType

    def setup_class(cls):
        cls.cpu = getcpuclass()(rtyper=None, stats=FakeStats())
        cls.cpu.setup_once()

    def _prepare_args(self, args, floats, ints):
        local_floats = list(floats)
        local_ints = list(ints)
        expected_result = 0.0
        arguments = []
        for i in range(len(args)):
            x = args[i]
            if x[0] == 'f':
                x = local_floats.pop()
                t = longlong.getfloatstorage(x)
                arguments.append(t)
            else:
                x = local_ints.pop()
                arguments.append(x)
            expected_result += x
        return arguments, expected_result

    @classmethod
    def get_funcbox(cls, cpu, func_ptr):
        addr = llmemory.cast_ptr_to_adr(func_ptr)
        return ConstInt(heaptracker.adr2int(addr))

    def test_call_aligned_with_spilled_values(self):
        cpu = self.cpu
        if not cpu.supports_floats:
            py.test.skip('requires floats')

        def func(*args):
            return float(sum(args))

        F = lltype.Float
        I = lltype.Signed
        floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
        ints = [7, 11, 23, 13, -42, 1111, 95, 1]
        for case in range(256):
            local_floats = list(floats)
            local_ints = list(ints)
            args = []
            spills = []
            funcargs = []
            float_count = 0
            int_count = 0
            for i in range(8):
                if case & (1<<i):
                    args.append('f%d' % float_count)
                    spills.append('force_spill(f%d)' % float_count)
                    float_count += 1
                    funcargs.append(F)
                else:
                    args.append('i%d' % int_count)
                    spills.append('force_spill(i%d)' % int_count)
                    int_count += 1
                    funcargs.append(I)

            arguments = ', '.join(args)
            spill_ops = '\n'.join(spills)

            FUNC = self.FuncType(funcargs, F)
            FPTR = self.Ptr(FUNC)
            func_ptr = llhelper(FPTR, func)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)
            funcbox = self.get_funcbox(cpu, func_ptr)

            ops = '[%s]\n' % arguments
            ops += '%s\n' % spill_ops
            ops += 'f99 = call(ConstClass(func_ptr), %s, descr=calldescr)\n' % arguments
            ops += 'i99 = same_as(0)\n'
            ops += 'guard_true(i99) [f99, %s]\n' % arguments
            ops += 'finish()\n'

            loop = parse(ops, namespace=locals())
            looptoken = JitCellToken()
            self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken)
            argvals, expected_result = self._prepare_args(args, floats, ints)

            deadframe = self.cpu.execute_token(looptoken, *argvals)
            x = longlong.getrealfloat(cpu.get_float_value(deadframe, 0))
            assert abs(x - expected_result) < 0.0001

    def test_call_aligned_with_imm_values(self):
        cpu = self.cpu
        if not cpu.supports_floats:
            py.test.skip('requires floats')

        def func(*args):
            return float(sum(args))

        F = lltype.Float
        I = lltype.Signed
        floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
        ints = [7, 11, 23, 13, -42, 1111, 95, 1]
        for case in range(256):
            result = 0.0
            args = []
            argslist = []
            local_floats = list(floats)
            local_ints = list(ints)
            for i in range(8):
                if case & (1<<i):
                    args.append(F)
                    arg = local_floats.pop()
                    result += arg
                    argslist.append(constfloat(arg))
                else:
                    args.append(I)
                    arg = local_ints.pop()
                    result += arg
                    argslist.append(ConstInt(arg))
            FUNC = self.FuncType(args, F)
            FPTR = self.Ptr(FUNC)
            func_ptr = llhelper(FPTR, func)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)
            funcbox = self.get_funcbox(cpu, func_ptr)

            res = self.execute_operation(rop.CALL,
                                         [funcbox] + argslist,
                                         'float', descr=calldescr)
            assert abs(res.getfloat() - result) < 0.0001

    def test_call_aligned_with_args_on_the_stack(self):
        cpu = self.cpu
        if not cpu.supports_floats:
            py.test.skip('requires floats')

        def func(*args):
            return float(sum(args))

        F = lltype.Float
        I = lltype.Signed
        floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
        ints = [7, 11, 23, 13, -42, 1111, 95, 1]
        for case in range(256):
            result = 0.0
            args = []
            argslist = []
            local_floats = list(floats)
            local_ints = list(ints)
            for i in range(8):
                if case & (1<<i):
                    args.append(F)
                    arg = local_floats.pop()
                    result += arg
                    argslist.append(boxfloat(arg))
                else:
                    args.append(I)
                    arg = local_ints.pop()
                    result += arg
                    argslist.append(BoxInt(arg))
            FUNC = self.FuncType(args, F)
            FPTR = self.Ptr(FUNC)
            func_ptr = llhelper(FPTR, func)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)
            funcbox = self.get_funcbox(cpu, func_ptr)

            res = self.execute_operation(rop.CALL,
                                         [funcbox] + argslist,
                                         'float', descr=calldescr)
            assert abs(res.getfloat() - result) < 0.0001

    def test_call_alignment_call_assembler(self):
        cpu = self.cpu
        if not cpu.supports_floats:
            py.test.skip('requires floats')

        fdescr3 = BasicFinalDescr(3)
        fdescr4 = BasicFinalDescr(4)

        def assembler_helper(failindex, virtualizable):
            assert 0, 'should not be called, but was with failindex (%d)' % failindex
            return 13

        FUNCPTR = lltype.Ptr(lltype.FuncType([lltype.Signed, llmemory.GCREF],
                                             lltype.Signed))
        class FakeJitDriverSD:
            index_of_virtualizable = -1
            _assembler_helper_ptr = llhelper(FUNCPTR, assembler_helper)
            assembler_helper_adr = llmemory.cast_ptr_to_adr(
                _assembler_helper_ptr)

        floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
        ints = [7, 11, 23, 42, -42, 1111, 95, 1]

        for case in range(256):
            float_count = 0
            int_count = 0
            args = []
            called_ops = ''
            total_index = -1
            for i in range(8):
                if case & (1<<i):
                    args.append('f%d' % float_count)
                else:
                    args.append('i%d' % int_count)
                    called_ops += 'f%d = cast_int_to_float(i%d)\n' % (
                        float_count, int_count)
                    int_count += 1
                if total_index == -1:
                    total_index = float_count
                    float_count += 1
                else:
                    called_ops += 'f%d = float_add(f%d, f%d)\n' % (
                        float_count + 1, total_index, float_count)
                    total_index = float_count + 1
                    float_count += 2
            arguments = ', '.join(args)
            called_ops = '[%s]\n' % arguments + called_ops
            called_ops += 'finish(f%d, descr=fdescr3)\n' % total_index
            # compile called loop
            called_loop = parse(called_ops, namespace=locals())
            called_looptoken = JitCellToken()
            called_looptoken.outermost_jitdriver_sd = FakeJitDriverSD()
            done_descr = called_loop.operations[-1].getdescr()
            self.cpu.compile_loop(called_loop.inputargs, called_loop.operations, called_looptoken)

            argvals, expected_result = self._prepare_args(args, floats, ints)
            deadframe = cpu.execute_token(called_looptoken, *argvals)
            assert cpu.get_latest_descr(deadframe) == fdescr3
            t = longlong.getrealfloat(cpu.get_float_value(deadframe, 0))
            assert abs(t - expected_result) < 0.0001

            ARGS = []
            RES = lltype.Float
            for x in args:
                if x[0] == 'f':
                    ARGS.append(lltype.Float)
                else:
                    ARGS.append(lltype.Signed)
            FakeJitDriverSD.portal_calldescr = self.cpu.calldescrof(
                lltype.Ptr(lltype.FuncType(ARGS, RES)), ARGS, RES,
                EffectInfo.MOST_GENERAL)
            ops = '''
            [%s]
            f99 = call_assembler(%s, descr=called_looptoken)
            guard_not_forced()[]
            finish(f99, descr=fdescr4)
            ''' % (arguments, arguments)
            loop = parse(ops, namespace=locals())
            # we want to take the fast path
            self.cpu.done_with_this_frame_descr_float = done_descr
            try:
                othertoken = JitCellToken()
                self.cpu.compile_loop(loop.inputargs, loop.operations, othertoken)

                # prepare call to called_loop
                argvals, _ = self._prepare_args(args, floats, ints)
                deadframe = cpu.execute_token(othertoken, *argvals)
                x = longlong.getrealfloat(
                    cpu.get_float_value(deadframe, 0))
                assert cpu.get_latest_descr(deadframe) == fdescr4
                assert abs(x - expected_result) < 0.0001
            finally:
                del self.cpu.done_with_this_frame_descr_float

    def test_call_with_imm_values_bug_constint0(self):
        cpu = self.cpu

        I = lltype.Signed
        ints = [7, 11, 23, 13, -42, 0, 0, 9]

        def func(*args):
            for i in range(len(args)):
                assert args[i] == ints[i]
            return sum(args)

        result = sum(ints)
        args = [I] * len(ints)
        argslist = [ConstInt(i) for i in ints]
        FUNC = self.FuncType(args, I)
        FPTR = self.Ptr(FUNC)
        func_ptr = llhelper(FPTR, func)
        calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo.MOST_GENERAL)
        funcbox = self.get_funcbox(cpu, func_ptr)

        res = self.execute_operation(rop.CALL,
                                     [funcbox] + argslist,
                                     'int', descr=calldescr)
        assert res.value == result

    def test_call_with_singlefloats(self):
        cpu = self.cpu
        if not cpu.supports_floats or not cpu.supports_singlefloats:
            py.test.skip('requires floats and singlefloats')

        import random
        from rpython.rlib.rarithmetic import r_singlefloat

        def func(*args):
            res = 0.0
            for i, x in enumerate(args):
                res += (i + 1.1) * float(x)
            return res

        F = lltype.Float
        S = lltype.SingleFloat
        I = lltype.Signed
        floats = [random.random() - 0.5 for i in range(20)]
        singlefloats = [r_singlefloat(random.random() - 0.5) for i in range(20)]
        ints = [random.randrange(-99, 99) for i in range(20)]
        for repeat in range(100):
            args = []
            argvalues = []
            argslist = []
            local_floats = list(floats)
            local_singlefloats = list(singlefloats)
            local_ints = list(ints)
            for i in range(random.randrange(4, 20)):
                case = random.randrange(0, 6)
                if case & 1: boxme = BoxInt
                else:        boxme = ConstInt
                if case < 2:
                    args.append(F)
                    arg = arg1 = local_floats.pop()
                    if case & 1: boxme = boxfloat
                    else:        boxme = constfloat
                elif case < 4:
                    args.append(S)
                    arg = local_singlefloats.pop()
                    arg1 = longlong.singlefloat2int(arg)
                else:
                    args.append(I)
                    arg = arg1 = local_ints.pop()
                argslist.append(boxme(arg1))
                argvalues.append(arg)
            FUNC = self.FuncType(args, F)
            FPTR = self.Ptr(FUNC)
            func_ptr = llhelper(FPTR, func)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)
            funcbox = self.get_funcbox(cpu, func_ptr)

            res = self.execute_operation(rop.CALL,
                                         [funcbox] + argslist,
                                         'float', descr=calldescr)
            expected = func(*argvalues)
            assert abs(res.getfloat() - expected) < 0.0001

    def make_function_returning_stack_pointer(self):
        raise NotImplementedError

    def get_alignment_requirements(self):
        raise NotImplementedError

    def test_call_aligned_explicit_check(self):
        if (not platform.machine().startswith('arm') and
                sys.maxint == 2 ** 31 - 1): # XXX is still necessary on x86?
            py.test.skip("libffi on 32bit is broken")
        cpu = self.cpu
        if not cpu.supports_floats:
            py.test.skip('requires floats')

        func_addr = self.make_function_returning_stack_pointer()

        F = lltype.Float
        I = lltype.Signed
        floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
        ints = [7, 11, 23, 13, -42, 1111, 95, 1]
        for case in range(256):
            args = []
            funcargs = []
            float_count = 0
            int_count = 0
            for i in range(8):
                if case & (1<<i):
                    args.append('f%d' % float_count)
                    float_count += 1
                    funcargs.append(F)
                else:
                    args.append('i%d' % int_count)
                    int_count += 1
                    funcargs.append(I)

            arguments = ', '.join(args)

            FUNC = self.FuncType(funcargs, I)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)

            ops = '[%s]\n' % arguments
            ops += 'i99 = call(%d, %s, descr=calldescr)\n' % (func_addr,
                                                              arguments)
            ops += 'finish(i99)\n'

            loop = parse(ops, namespace=locals())
            looptoken = JitCellToken()
            self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken)
            argvals, expected_result = self._prepare_args(args, floats, ints)

            deadframe = self.cpu.execute_token(looptoken, *argvals)
            x = cpu.get_int_value(deadframe, 0)
            align_req = self.get_alignment_requirements()
            assert x % align_req == 0


from rpython.jit.metainterp.history import BasicFinalDescr, BoxInt,\
     JitCellToken, ConstInt, BoxFloat, ConstFloat
from rpython.jit.metainterp.resoperation import rop
from rpython.jit.codewriter.effectinfo import EffectInfo
from rpython.jit.tool.oparser import parse
from rpython.rtyper.lltypesystem import lltype, llmemory
from rpython.rtyper.annlowlevel import llhelper
from rpython.jit.codewriter import heaptracker, longlong
from rpython.jit.backend.detect_cpu import getcpuclass
from rpython.jit.backend.test.runner_test import Runner
import py
import sys
import platform

def boxfloat(x):
    return BoxFloat(longlong.getfloatstorage(x))

def constfloat(x):
    return ConstFloat(longlong.getfloatstorage(x))

class FakeStats(object):
    pass

class CallingConvTests(Runner):
    type_system = 'lltype'
    Ptr = lltype.Ptr
    FuncType = lltype.FuncType

    def setup_class(cls):
        cls.cpu = getcpuclass()(rtyper=None, stats=FakeStats())
        cls.cpu.setup_once()

    def _prepare_args(self, args, floats, ints):
        local_floats = list(floats)
        local_ints = list(ints)
        expected_result = 0.0
        arguments = []
        for i in range(len(args)):
            x = args[i]
            if x[0] == 'f':
                x = local_floats.pop()
                t = longlong.getfloatstorage(x)
                arguments.append(t)
            else:
                x = local_ints.pop()
                arguments.append(x)
            expected_result += x
        return arguments, expected_result

    @classmethod
    def get_funcbox(cls, cpu, func_ptr):
        addr = llmemory.cast_ptr_to_adr(func_ptr)
        return ConstInt(heaptracker.adr2int(addr))

    def test_call_aligned_with_spilled_values(self):
        cpu = self.cpu
        if not cpu.supports_floats:
            py.test.skip('requires floats')

        def func(*args):
            return float(sum(args))

        F = lltype.Float
        I = lltype.Signed
        floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
        ints = [7, 11, 23, 13, -42, 1111, 95, 1]
        for case in range(256):
            local_floats = list(floats)
            local_ints = list(ints)
            args = []
            spills = []
            funcargs = []
            float_count = 0
            int_count = 0
            for i in range(8):
                if case & (1<<i):
                    args.append('f%d' % float_count)
                    spills.append('force_spill(f%d)' % float_count)
                    float_count += 1
                    funcargs.append(F)
                else:
                    args.append('i%d' % int_count)
                    spills.append('force_spill(i%d)' % int_count)
                    int_count += 1
                    funcargs.append(I)

            arguments = ', '.join(args)
            spill_ops = '\n'.join(spills)

            FUNC = self.FuncType(funcargs, F)
            FPTR = self.Ptr(FUNC)
            func_ptr = llhelper(FPTR, func)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)
            funcbox = self.get_funcbox(cpu, func_ptr)

            ops = '[%s]\n' % arguments
            ops += '%s\n' % spill_ops
            ops += 'f99 = call(ConstClass(func_ptr), %s, descr=calldescr)\n' % arguments
            ops += 'i99 = same_as(0)\n'
            ops += 'guard_true(i99) [f99, %s]\n' % arguments
            ops += 'finish()\n'

            loop = parse(ops, namespace=locals())
            looptoken = JitCellToken()
            self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken)
            argvals, expected_result = self._prepare_args(args, floats, ints)

            deadframe = self.cpu.execute_token(looptoken, *argvals)
            x = longlong.getrealfloat(cpu.get_float_value(deadframe, 0))
            assert abs(x - expected_result) < 0.0001

    def test_call_aligned_with_imm_values(self):
        cpu = self.cpu
        if not cpu.supports_floats:
            py.test.skip('requires floats')

        def func(*args):
            return float(sum(args))

        F = lltype.Float
        I = lltype.Signed
        floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
        ints = [7, 11, 23, 13, -42, 1111, 95, 1]
        for case in range(256):
            result = 0.0
            args = []
            argslist = []
            local_floats = list(floats)
            local_ints = list(ints)
            for i in range(8):
                if case & (1<<i):
                    args.append(F)
                    arg = local_floats.pop()
                    result += arg
                    argslist.append(constfloat(arg))
                else:
                    args.append(I)
                    arg = local_ints.pop()
                    result += arg
                    argslist.append(ConstInt(arg))
            FUNC = self.FuncType(args, F)
            FPTR = self.Ptr(FUNC)
            func_ptr = llhelper(FPTR, func)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)
            funcbox = self.get_funcbox(cpu, func_ptr)

            res = self.execute_operation(rop.CALL,
                                         [funcbox] + argslist,
                                         'float', descr=calldescr)
            assert abs(res.getfloat() - result) < 0.0001

    def test_call_aligned_with_args_on_the_stack(self):
        cpu = self.cpu
        if not cpu.supports_floats:
            py.test.skip('requires floats')

        def func(*args):
            return float(sum(args))

        F = lltype.Float
        I = lltype.Signed
        floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
        ints = [7, 11, 23, 13, -42, 1111, 95, 1]
        for case in range(256):
            result = 0.0
            args = []
            argslist = []
            local_floats = list(floats)
            local_ints = list(ints)
            for i in range(8):
                if case & (1<<i):
                    args.append(F)
                    arg = local_floats.pop()
                    result += arg
                    argslist.append(boxfloat(arg))
                else:
                    args.append(I)
                    arg = local_ints.pop()
                    result += arg
                    argslist.append(BoxInt(arg))
            FUNC = self.FuncType(args, F)
            FPTR = self.Ptr(FUNC)
            func_ptr = llhelper(FPTR, func)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)
            funcbox = self.get_funcbox(cpu, func_ptr)

            res = self.execute_operation(rop.CALL,
                                         [funcbox] + argslist,
                                         'float', descr=calldescr)
            assert abs(res.getfloat() - result) < 0.0001

    def test_call_alignment_call_assembler(self):
        cpu = self.cpu
        if not cpu.supports_floats:
            py.test.skip('requires floats')

        fdescr3 = BasicFinalDescr(3)
        fdescr4 = BasicFinalDescr(4)

        def assembler_helper(failindex, virtualizable):
            assert 0, 'should not be called, but was with failindex (%d)' % failindex
            return 13

        FUNCPTR = lltype.Ptr(lltype.FuncType([lltype.Signed, llmemory.GCREF],
                                             lltype.Signed))
        class FakeJitDriverSD:
            index_of_virtualizable = -1
            _assembler_helper_ptr = llhelper(FUNCPTR, assembler_helper)
            assembler_helper_adr = llmemory.cast_ptr_to_adr(
                _assembler_helper_ptr)

        floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
        ints = [7, 11, 23, 42, -42, 1111, 95, 1]

        for case in range(256):
            float_count = 0
            int_count = 0
            args = []
            called_ops = ''
            total_index = -1
            for i in range(8):
                if case & (1<<i):
                    args.append('f%d' % float_count)
                else:
                    args.append('i%d' % int_count)
                    called_ops += 'f%d = cast_int_to_float(i%d)\n' % (
                        float_count, int_count)
                    int_count += 1
                if total_index == -1:
                    total_index = float_count
                    float_count += 1
                else:
                    called_ops += 'f%d = float_add(f%d, f%d)\n' % (
                        float_count + 1, total_index, float_count)
                    total_index = float_count + 1
                    float_count += 2
            arguments = ', '.join(args)
            called_ops = '[%s]\n' % arguments + called_ops
            called_ops += 'finish(f%d, descr=fdescr3)\n' % total_index
            # compile called loop
            called_loop = parse(called_ops, namespace=locals())
            called_looptoken = JitCellToken()
            called_looptoken.outermost_jitdriver_sd = FakeJitDriverSD()
            done_descr = called_loop.operations[-1].getdescr()
            self.cpu.compile_loop(called_loop.inputargs, called_loop.operations, called_looptoken)

            argvals, expected_result = self._prepare_args(args, floats, ints)
            deadframe = cpu.execute_token(called_looptoken, *argvals)
            assert cpu.get_latest_descr(deadframe) == fdescr3
            t = longlong.getrealfloat(cpu.get_float_value(deadframe, 0))
            assert abs(t - expected_result) < 0.0001

            ARGS = []
            RES = lltype.Float
            for x in args:
                if x[0] == 'f':
                    ARGS.append(lltype.Float)
                else:
                    ARGS.append(lltype.Signed)
            FakeJitDriverSD.portal_calldescr = self.cpu.calldescrof(
                lltype.Ptr(lltype.FuncType(ARGS, RES)), ARGS, RES,
                EffectInfo.MOST_GENERAL)
            ops = '''
            [%s]
            f99 = call_assembler(%s, descr=called_looptoken)
            guard_not_forced()[]
            finish(f99, descr=fdescr4)
            ''' % (arguments, arguments)
            loop = parse(ops, namespace=locals())
            # we want to take the fast path
            self.cpu.done_with_this_frame_descr_float = done_descr
            try:
                othertoken = JitCellToken()
                self.cpu.compile_loop(loop.inputargs, loop.operations, othertoken)

                # prepare call to called_loop
                argvals, _ = self._prepare_args(args, floats, ints)
                deadframe = cpu.execute_token(othertoken, *argvals)
                x = longlong.getrealfloat(
                    cpu.get_float_value(deadframe, 0))
                assert cpu.get_latest_descr(deadframe) == fdescr4
                assert abs(x - expected_result) < 0.0001
            finally:
                del self.cpu.done_with_this_frame_descr_float

    def test_call_with_imm_values_bug_constint0(self):
        cpu = self.cpu

        I = lltype.Signed
        ints = [7, 11, 23, 13, -42, 0, 0, 9]

        def func(*args):
            for i in range(len(args)):
                assert args[i] == ints[i]
            return sum(args)

        result = sum(ints)
        args = [I] * len(int

        argslist = [ConstInt(i) for i in ints]
        FUNC = self.FuncType(args, I)
        FPTR = self.Ptr(FUNC)
        func_ptr = llhelper(FPTR, func)
        calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo.MOST_GENERAL)
        funcbox = self.get_funcbox(cpu, func_ptr)

        res = self.execute_operation(rop.CALL,
                                     [funcbox] + argslist,
                                     'int', descr=calldescr)
        assert res.value == result

    def test_call_with_singlefloats(self):
        cpu = self.cpu
        if not cpu.supports_floats or not cpu.supports_singlefloats:
            py.test.skip('requires floats and singlefloats')

        import random
        from rpython.rlib.rarithmetic import r_singlefloat

        def func(*args):
            res = 0.0
            for i, x in enumerate(args):
                res += (i + 1.1) * float(x)
            return res

        F = lltype.Float
        S = lltype.SingleFloat
        I = lltype.Signed
        floats = [random.random() - 0.5 for i in range(20)]
        singlefloats = [r_singlefloat(random.random() - 0.5) for i in range(20)]
        ints = [random.randrange(-99, 99) for i in range(20)]
        for repeat in range(100):
            args = []
            argvalues = []
            argslist = []
            local_floats = list(floats)
            local_singlefloats = list(singlefloats)
            local_ints = list(ints)
            for i in range(random.randrange(4, 20)):
                case = random.randrange(0, 6)
                if case & 1: boxme = BoxInt
                else:        boxme = ConstInt
                if case < 2:
                    args.append(F)
                    arg = arg1 = local_floats.pop()
                    if case & 1: boxme = boxfloat
                    else:        boxme = constfloat
                elif case < 4:
                    args.append(S)
                    arg = local_singlefloats.pop()
                    arg1 = longlong.singlefloat2int(arg)
                else:
                    args.append(I)
                    arg = arg1 = local_ints.pop()
                argslist.append(boxme(arg1))
                argvalues.append(arg)
            FUNC = self.FuncType(args, F)
            FPTR = self.Ptr(FUNC)
            func_ptr = llhelper(FPTR, func)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)
            funcbox = self.get_funcbox(cpu, func_ptr)

            res = self.execute_operation(rop.CALL,
                                         [funcbox] + argslist,
                                         'float', descr=calldescr)
            expected = func(*argvalues)
            assert abs(res.getfloat() - expected) < 0.0001

    def make_function_returning_stack_pointer(self):
        raise NotImplementedError

    def get_alignment_requirements(self):
        raise NotImplementedError

    def test_call_aligned_explicit_check(self):
        if (not platform.machine().startswith('arm') and
                sys.maxint == 2 ** 31 - 1): # XXX is still necessary on x86?
            py.test.skip("libffi on 32bit is broken")
        cpu = self.cpu
        if not cpu.supports_floats:
            py.test.skip('requires floats')

        func_addr = self.make_function_returning_stack_pointer()

        F = lltype.Float
        I = lltype.Signed
        floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
        ints = [7, 11, 23, 13, -42, 1111, 95, 1]
        for case in range(256):
            args = []
            funcargs = []
            float_count = 0
            int_count = 0
            for i in range(8):
                if case & (1<<i):
                    args.append('f%d' % float_count)
                    float_count += 1
                    funcargs.append(F)
                else:
                    args.append('i%d' % int_count)
                    int_count += 1
                    funcargs.append(I)

            arguments = ', '.join(args)

            FUNC = self.FuncType(funcargs, I)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)

            ops = '[%s]\n' % arguments
            ops += 'i99 = call(%d, %s, descr=calldescr)\n' % (func_addr,
                                                              arguments)
            ops += 'finish(i99)\n'

            loop = parse(ops, namespace=locals())
            looptoken = JitCellToken()
            self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken)
            argvals, expected_result = self._prepare_args(args, floats, ints)

            deadframe = self.cpu.execute_token(looptoken, *argvals)
            x = cpu.get_int_value(deadframe, 0)
            align_req = self.get_alignment_requirements()
            assert x % align_req == 0


python/github/greatabel/PythonRepository/03Programming in Python 3/6ObjectOrient/Shape.py

import math
import doctest

"""
运行python Shape.py
如果doctest通过，不会有任何输出。可以加-v参数来查看测试细节
"""
class Point:
	def __init__(self,x=0,y=0):
		self.x = x
		self.y = y

	def distance_from_origin(self):

	def __eq__(self,other):
		return self.x == other.x and self.y == other.y

	def __repr__(self):
		return "Point({0.x!r},{0.y!r})".format(self)

	def __str__(self):
		return "({0.x!r},{0.y!r})".format(self)

class Circle(Point):

    def __init__(self, radius, x=0, y=0):
        """A Circle
        >>> circle = Circle(2)
        >>> circle
        Circle(2, 0, 0)
        """
        super().__init__(x, y)
        self.radius = radius

    def edge_distance_from_origin(self):
        """The distance of the circle's edge from the origin
        >>> circle = Circle(2, 3, 4)
        >>> circle.edge_distance_from_origin()
        3.0
        """
        return abs(self.distance_from_origin() - self.radius)

    def area(self):
        """The circle's area
        >>> circle = Circle(10)
        >>> a = circle.area()
        >>> int(a)
        314
        """
        return math.pi * (self.radius ** 2)

    def circumference(self):
        """The circle's circumference
        >>> circle = Circle(3)
        >>> d = circle.circumference()
        >>> int(d)
        18
        """
        return 2 * math.pi * self.radius

    def __eq__(self, other):
        return self.radius == other.radius and super().__eq__(other)

    def __repr__(self):
        return "Circle({0.radius!r}, {0.x!r}, {0.y!r})".format(self)

    def __str__(self):
        return repr(self)

if __name__ == "__main__":
    doctest.testmod()


import math
import doctest

"""
运行python Shape.py
如果doctest通过，不会有任何输出。可以加-v参数来查看测试细节
"""
class Point:
	def __init__(self,x=0,y=0):
		self.x = x
		self.y = y

	def distance_from_origin(self):
		return math.hypot(self.x,self.y)

	def __eq__(self,other):
		return self.x == other.x and self.y == other.y

	def __repr__(self):
		return "Point({0.x!r},{0.y!r})".format(self)

	def __str__(self):
		return "({0.x!r},{0.y!r})".format(self)

class Circle(Point):

    def __init__(self, radius, x=0, y=0):
        """A Circle
        >>> circle = Circle(2)
        >>> circle
        Circle(2, 0, 0)
        """
        super().__init__(x, y)
        self.radius = radius

    def edge_distance_from_origin(self):
        """The distance of the circle's edge from the origin
        >>> circle = Circle(2, 3, 4)
        >>> circle.edge_distance_from_origin()
        3.0
        """
        return abs(self.distance_from_origin() - self.radius)

    def area(self):
        """The circle's area
        >>> circle = Circle(10)
        >>> a = circle.area()
        >>> int(a)
        314
        """
        return math.pi * (self.radius ** 2)

    def circumference(self):
        """The circle's circumference
        >>> circle = Circle(3)
        >>> d = circle.circumference()
        >>> int(d)
        18
        """
        return 2 * math.pi * self.radius

    def __eq__(self, other):
        return self.radius == other.radius and super().__eq__(other)

    def __repr__(self):
        return "Circle({0.

    def __str__(self):
        return repr(self)

if __name__ == "__main__":
    doctest.testmod()


import math
import doctest

"""
运行python Shape.py
如果doctest通过，不会有任何输出。可以加-v参数来查看测试细节
"""
class Point:
	def __init__(self,x=0,y=0):
		self.x = x
		self.y = y

	def distance_from_origin(self):
		return math.hypot(self.x,self.y)

	def __eq__(self,other):
		return self.x == other.x and self.y == other.y

	def __repr__(self):
		return "Point({0.x!r},{0.y!r})".format(self)

	def __str__(self):
		return "({0.x!r},{0.y!r})".format(self)

class Circle(Point):

    def __init__(self, radius, x=0, y=0):
        """A Circle
        >>> circle = Circle(2)
        >>> circle
        Circle(2, 0, 0)
        """
        super().__init__(x, y)
        self.radius = radius

    def edge_distance_from_origin(self):
        """The distance of the circle's edge from the origin
        >>> circle = Circle(2, 3, 4)
        >>> circle.edge_distance_from_origin()
        3.0
        """
        return abs(self.distance_from_origin() - self.radius)

    def area(self):
        """The circle's area
        >>> circle = Circle(10)
        >>> a = circle.area()
        >>> int(a)
        314
        """
        return math.pi * (self.radius ** 2)

    def circumference(self):
        """The circle's circumference
        >>> circle = Circle(3)
        >>> d = circle.circumference()
        >>> int(d)
        18
        """

    def __eq__(self, other):
        return self.radius == other.radius and super().__eq__(other)

    def __repr__(self):
        return "Circle({0.radius!r}, {0.x!r}, {0.y!r})".format(self)

    def __str__(self):
        return repr(self)

if __name__ == "__main__":
    doctest.testmod()


outblock = [0]*8
        off = 0
        for i in key:
            for k in range(7):
                ptr = iperm[(off+(i >> 4))*8+k]
                pobyte = iperm[(off+16)*8 + 8*(i & 0xF) + k]
                outblock[k] |= pobyte | ptr
            outblock[7] |= (iperm[(off + (i >> 4))*8+7])
            outblock[7] |= iperm[(off + 16) * 8 + 8 * (i & 0xF) + 7]
            off += 32
        return outblock


        tmp_2 = self._f(tmp_r, keys[24:32]) ^ tmp_1
        tmp_r ^= self._f(tmp_2, keys[32:40])
        tmp_3 = self._f(tmp_r, keys[40:48]) ^ tmp_2
        tmp_r ^= self._f(tmp_3, keys[48:56])
        tmp_4 = self._f(tmp_r, keys[56:64]) ^ tmp_3
        tmp_r ^= self._f(tmp_4, keys[64:72])
        tmp_5 = self._f(tmp_r, keys[72:80]) ^ tmp_4
        tmp_r ^= self._f(tmp_5, keys[80:88])
        tmp_6 = self._f(tmp_r, keys[88:96]) ^ tmp_5
        tmp_r ^= self._f(tmp_6, keys[96:104])
        tmp_7 = self._f(tmp_r, keys[104:112]) ^ tmp_6
        tmp_r ^= self._f(tmp_7, keys[112:120])
        tmp_8 = self._f(tmp_r, keys[120:128]) ^ tmp_7
        blocks[0] = self._get_bit(tmp_8, 3)
        blocks[1] = self._get_bit(tmp_8, 2)
        blocks[2] = self._get_bit(tmp_8, 1)
        blocks[3] = self._get_bit(tmp_8, 0)
        blocks[4] = self._get_bit(tmp_r, 3)
        blocks[5] = self._get_bit(tmp_r, 2)
        blocks[6] = self._get_bit(tmp_r, 1)
        blocks[7] = self._get_bit(tmp_r, 0)
        block = self._permute(self.fperm, blocks)
        if chain:
            chain = block
        return block, chain

    def _generate_keys(self, key):
        keys = [0] * 128
        array_1 = [0] * 56
        array_2 = [0] * 56
        for i in range(56):
            idx_bb = (self.pc1[i] - 1) & 7
            idx_k = (self.pc1[i] - 1) >> 3
            array_1[i] = int(self.byte_bit[idx_bb] & key[idx_k] != 0)
        for j in range(16):
            value = self.totrot[j]
            for k in range(56):
                idx = value
                if k >= 28:
                    if value >= 56:
                        idx = value - 28
                else:
                    if value >= 28:
                        idx = value - 28
                array_2[k] = (array_1[idx])
                value += 1
            for n in range(48):
                if array_2[self.pc2[n] - 1]:
                    tmp = self.byte_bit[n % 6]
                    tmp = tmp >> 2
                    keys[8 * j + n / 6] |= tmp
        return keys

    def crypt(self, mode, blob, key, length):
        """Runs a round of encoding/decoding using SAP's custom TripleDES implementation as in SSFS
        and RSECTAB.

        :param mode: encode or decode mode
        :type mode: int

        :param blob: blob to encode
        :type blob: list of int

        :param key: key to use to encode
        :type key: list of int

        :return: encoded blob
        :rtype: list of int

        :raise Exception: if encoding failed
        """

        def transf_encode(blocks, blob, length):
            return blocks[len(blocks) - 8:]

        def transf_decode(blocks, blob, length):
            return blob[length - (length % 8) - 8:length - (length % 8)]

        # Pick the right functions and padding transforms according to the mode
        if mode == self.MODE_ENCODE:
            func_1 = self._encode_v1
            transf = transf_encode
        elif mode == self.MODE_DECODE:
            func_1 = self._decode_v1
            transf = transf_decode
        else:
            raise AttributeError("Invalid mode")

        blocks = []
        keys = self._generate_keys(key)
        chain = [0] * 8
        if length >= 8:
            num_block = length >> 3
            for i in range(num_block):
                inout, chain = func_1(keys, blob[i * 8:8 + i * 8], chain)
                for j in inout:
                    blocks.append(j)
        if length % 8:
            inout, chain = self._encode_v1(keys, transf(blocks, blob, length), [0] * 8)
            inout = self._xor(blob[length - (length % 8):], inout)
            for k in inout:
                blocks.append(k)
        return blocks


lang_id_model = LanguageIdentifier(algo=algo)
    lang_label, score = lang_id_model('你好，世界')
    assert lang_label == 'zh'
    with multiprocessing.Pool(2) as pool:
        out = pool.map(lang_id_model, ['你好，世界', 'Hello World'])
    assert out[0][0] == 'zh'
    assert out[1][0] == 'en'

import pytest
from gluonnlp.data.filtering import ProfanityFilter, MosesNormalizer, LanguageIdentifier
import multiprocessing

def test_profanity_filter():
    profanity_filter = ProfanityFilter('en')
    filter_word = 'anal'
    unfilter_word = 'analysis'
    for text in [' ' + filter_word, ' ' + filter_word + ' ',
                 filter_word, filter_word + ' ' + unfilter_word]:
        assert profanity_filter.match(text) is True
    for text in [' ' + unfilter_word, unfilter_word, unfilter_word + ' ']:
        assert profanity_filter.match(text) is False

def test_sentence_normalizer():
    normalizer = MosesNormalizer('en')
    assert normalizer('    hello  world!!".\t\t\r') == ' hello world!!."  '
    assert normalizer(
        b'We therefore defend, and will continue to defend wherever necessary, our position of \xe2\x80\x98no diversion\xe2\x80\x99.\n'.decode('utf-8')) == \
           "We therefore defend, and will continue to defend wherever necessary, our position of 'no diversion'. "
    normalizer = MosesNormalizer('en', remove_non_printable_char=False)
    assert normalizer('    hello  world!!".\t\t\r') == ' hello world!!."\t\t'
    normalizer = MosesNormalizer('en', remove_non_printable_char=False, unicode_norm_form='NFKC')
    assert normalizer('    hello  world!!"⁵.\t\t\r') == ' hello world!!"5.\t\t'

@pytest.mark.parametrize('algo', ['fasttext', 'fasttext_compressed', 'langid'])
def test_language_identifier(algo):


import pytest
from gluonnlp.data.filtering import ProfanityFilter, MosesNormalizer, LanguageIdentifier
import multiprocessing

def test_profanity_filter():
    profanity_filter = ProfanityFilter('en')
    filter_word = 'anal'
    unfilter_word = 'analysis'
    for text in [' ' + filter_word, ' ' + filter_word + ' ',
                 filter_word, filter_word + ' ' + unfilter_word]:
        assert profanity_filter.match(text) is True
    for text in [' ' + unfilter_word, unfilter_word, unfilter_word + ' ']:
        assert profanity_filter.match(text) is False

def test_sentence_normalizer():
    normalizer = MosesNormalizer('en')
    assert normalizer('    hello  world!!".\t\t\r') == ' hello world!!."  '
    assert normalizer(
        b'We therefore defend, and will continue to defend wherever necessary, our position of \xe2\x80\x98no diversion\xe2\x80\x99.\n'.decode('utf-8')) == \
           "We therefore defend, and will continue to defend wherever necessary, our position of 'no diversion'. "
    normalizer = MosesNormalizer('en', remove_non_printable_char=False)
    assert normalizer('    hello  world!!".\t\t\r') == ' hello world!!."\t\t'
    normalizer = MosesNormalizer('en', remove_non_printable_char=False, unico

    assert normalizer('    hello  world!!"⁵.\t\t\r') == ' hello world!!"5.\t\t'

@pytest.mark.parametrize('algo', ['fasttext', 'fasttext_compressed', 'langid'])
def test_language_identifier(algo):
    lang_id_model = LanguageIdentifier(algo=algo)
    lang_label, score = lang_id_model('你好，世界')
    assert lang_label == 'zh'
    with multiprocessing.Pool(2) as pool:
        out = pool.map(lang_id_model, ['你好，世界', 'Hello World'])
    assert out[0][0] == 'zh'
    assert out[1][0] == 'en'


if os.path.isfile(self.log_path):
            if self.output_bucket:
                key = self.output_bucket.new_key(self.log_file)
                key.set_contents_from_filename(self.log_path)
        Service.shutdown(self)

import boto
from boto.services.service import Service
from boto.services.message import ServiceMessage
import os, time, mimetypes

class SonOfMMM(Service):

    def __init__(self, config_file=None):
        Service.__init__(self, config_file)
        self.log_file = '%s.log' % self.instance_id
        self.log_path = os.path.join(self.working_dir, self.log_file)
        boto.set_file_logger(self.name, self.log_path)
        if self.sd.has_option('ffmpeg_args'):
            self.command = '/usr/local/bin/ffmpeg ' + self.sd.get('ffmpeg_args')
        else:
            self.command = '/usr/local/bin/ffmpeg -y -i %s %s'
        self.output_mimetype = self.sd.get('output_mimetype')
        if self.sd.has_option('output_ext'):
            self.output_ext = self.sd.get('output_ext')
        else:
            self.output_ext = mimetypes.guess_extension(self.output_mimetype)
        self.output_bucket = self.sd.get_obj('output_bucket')
        self.input_bucket = self.sd.get_obj('input_bucket')
        # check to see if there are any messages queue
        m = self.input_queue.read(1)
        if not m:
            self.queue_files()

    def queue_files(self):
        boto.log.info('Queueing files from %s' % self.input_bucket.name)
        for key in self.input_bucket:
            boto.log.info('Queueing %s' % key.name)
            m = ServiceMessage()
            if self.output_bucket:
                d = {'OutputBucket' : self.output_bucket.name}
            else:
                d = None
            m.for_key(key, d)
            self.input_queue.write(m)

    def process_file(self, in_file_name, msg):
        base, ext = os.path.splitext(in_file_name)
        out_file_name = os.path.join(self.working_dir,
                                     base+self.output_ext)
        command = self.command % (in_file_name, out_file_name)
        boto.log.info('running:\n%s' % command)
        status = self.run(command)
        if status == 0:
            return [(out_file_name, self.output_mimetype)]
        else:
            return []

    def shutdown(self):


import boto
from boto.services.service import Service
from boto.services.message import ServiceMessage
import os, time, mimetypes

class SonOfMMM(Service):

    def __init__(self, config_file=None):
        Service.__init__(self, config_file)
        self.log_file = '%s.log' % self.instance_id
        self.log_path = os.path.join(self.working_dir, self.log_file)
        boto.set_file_logger(self.name, self.log_path)
        if self.sd.has_option('ffmpeg_args'):
            self.command = '/usr/local/bin/ffmpeg ' + self.sd.get('ffmpeg_args')
        else:
            self.command = '/usr/local/bin/ffmpeg -y -i %s %s'
        se

        if self.sd.has_option('output_ext'):
            self.output_ext = self.sd.get('output_ext')
        else:
            self.output_ext = mimetypes.guess_extension(self.output_mimetype)
        self.output_bucket = self.sd.get_obj('output_bucket')
        self.input_bucket = self.sd.get_obj('input_bucket')
        # check to see if there are any messages queue
        m = self.input_queue.read(1)
        if not m:
            self.queue_files()

    def queue_files(self):
        boto.log.info('Queueing files from %s' % self.input_bucket.name)
        for key in self.input_bucket:
            boto.log.info('Queueing %s' % key.name)
            m = ServiceMessage()
            if self.output_bucket:
                d = {'OutputBucket' : self.output_bucket.name}
            else:
                d = None
            m.for_key(key, d)
            self.input_queue.write(m)

    def process_file(self, in_file_name, msg):
        base, ext = os.path.splitext(in_file_name)
        out_file_name = os.path.join(self.working_dir,
                                     base+self.output_ext)
        command = self.command % (in_file_name, out_file_name)
        boto.log.info('running:\n%s' % command)
        status = self.run(command)
        if status == 0:
            return [(out_file_name, self.output_mimetype)]
        else:
            return []

    def shutdown(self):
        if os.path.isfile(self.log_path):
            if self.output_bucket:
                key = self.output_bucket.new_key(self.log_file)
                key.set_contents_from_filename(self.log_path)
        Service.shutdown(self)